fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_ipq.c
Go to the documentation of this file.
1#include "case.h"
2#include "byte.h"
3#include "str.h"
4#include "stralloc.h"
5#include "dnsresolv.h"
6#include "socket_if.h"
7#include "ip.h"
8
18static int doit(stralloc *work,const char *rule)
19{
20 char ch;
21 unsigned int colon;
22 unsigned int prefixlen;
23
24 ch = *rule++;
25 if ((ch != '?') && (ch != '=') && (ch != '*') && (ch != '-')) return 1;
26 colon = str_chr((char *)rule,':');
27 if (!rule[colon]) return 1;
28
29 if (work->len < colon) return 1;
30 prefixlen = work->len - colon;
31 if ((ch == '=') && prefixlen) return 1;
32 if (case_diffb((char *)rule,colon,work->s + prefixlen)) return 1;
33 if (ch == '?') {
34 if (byte_chr(work->s,prefixlen,'.') < prefixlen) return 1;
35 if (byte_chr(work->s,prefixlen,'[') < prefixlen) return 1;
36 if (byte_chr(work->s,prefixlen,']') < prefixlen) return 1;
37 }
38
39 work->len = prefixlen;
40 if (ch == '-') work->len = 0;
41 return stralloc_cats(work,rule + colon + 1);
42}
43
46int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules)
47{
48 unsigned int i;
49 unsigned int j;
50 unsigned int plus;
51 unsigned int fqdnlen;
52 int rc = 0;
53
54 if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM;
55
56 for (j = i = 0; j < rules->len; ++j)
57 if (!rules->s[j]) {
58 if (!doit(fqdn,rules->s + i)) return DNS_INT;
59 i = j + 1;
60 }
61
62 fqdnlen = fqdn->len;
63 plus = byte_chr(fqdn->s,fqdnlen,'+');
64 if (plus >= fqdnlen)
65 return dns_ip4(ipout,fqdn);
66
67 i = plus + 1;
68 for (;;) {
69 j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
70 byte_copy(fqdn->s + plus,j,fqdn->s + i);
71 fqdn->len = plus + j;
72 if (rc += dns_ip4(ipout,fqdn) < 0) return DNS_ERR;
73 i += j;
74 if (i >= fqdnlen) return rc;
75 ++i;
76 }
77 return 0;
78}
79
82int dns_ip4_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in)
83{
84 int r;
85 static stralloc rules;
86
87 if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0 ) return r;
88 if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT;
89 return dns_ip4_qualify_rules(ipout,fqdn,in,&rules);
90}
91
94int dns_ip6_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules)
95{
96 unsigned int i;
97 unsigned int j;
98 unsigned int plus;
99 unsigned int fqdnlen;
100 int rc = 0;
101
102 if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM;
103
104 for (j = i = 0; j < rules->len; ++j)
105 if (!rules->s[j]) {
106 if (!doit(fqdn,rules->s + i)) return DNS_INT;
107 i = j + 1;
108 }
109
110 fqdnlen = fqdn->len;
111 plus = byte_chr(fqdn->s,fqdnlen,'+');
112 if (plus >= fqdnlen)
113 return dns_ip6(ipout,fqdn);
114
115 i = plus + 1;
116 for (;;) {
117 j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
118 byte_copy(fqdn->s + plus,j,fqdn->s + i);
119 fqdn->len = plus + j;
120 if ((rc += dns_ip6(ipout,fqdn)) < 0) return DNS_ERR;
121 i += j;
122 if (i >= fqdnlen) return rc;
123 ++i;
124 }
125 return 0;
126}
127
130int dns_ip6_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in)
131{
132 int r;
133 static stralloc rules;
134
135 if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0) return r;
136 if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT;
137 return dns_ip6_qualify_rules(ipout,fqdn,in,&rules);
138}
139
142int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules)
143{
144 unsigned int i;
145 unsigned int j;
146 unsigned int k;
147 unsigned int plus;
148 unsigned int fqdnlen;
149 stralloc tmp = {0};
150 int rc = 0;
151
152 if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM;
153 if (!stralloc_copys(ipout,"")) return DNS_MEM;
154
155 for (j = i = 0; j < rules->len; ++j)
156 if (!rules->s[j]) {
157 if (!doit(fqdn,rules->s + i)) return DNS_INT;
158 i = j + 1;
159 }
160
161 fqdnlen = fqdn->len;
162 plus = byte_chr(fqdn->s,fqdnlen,'+');
163 if (plus >= fqdnlen) {
164 rc = dns_ip6(ipout,fqdn);
165 if (dns_ip4(&tmp,fqdn) > 0) {
166 for (k = 0; k < tmp.len; k += 4) {
167 if (!stralloc_catb(ipout,(const char *) V4mappedprefix,12)) return DNS_MEM;
168 if (!stralloc_catb(ipout,tmp.s + k,4)) return DNS_MEM;
169 rc++;
170 }
171 }
172 return rc;
173 }
174
175 i = plus + 1;
176 for (;;) {
177 j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
178 byte_copy(fqdn->s + plus,j,fqdn->s + i);
179 fqdn->len = plus + j;
180 if (!stralloc_copys(ipout,"")) return DNS_MEM;
181 rc = dns_ip6(&tmp,fqdn);
182 if (rc) if (!stralloc_cat(ipout,&tmp)) return DNS_MEM;
183 if (dns_ip4(&tmp,fqdn) > 0) {
184 for (k = 0; k < tmp.len; k += 4) {
185 if (!stralloc_catb(ipout,(const char *) V4mappedprefix,12)) return DNS_MEM;
186 if (!stralloc_catb(ipout,tmp.s + k,4)) return DNS_MEM;
187 rc++;
188 }
189 }
190
191 if (rc < 0) return DNS_ERR;
192 i += j;
193 if (i >= fqdnlen) return rc;
194 ++i;
195 }
196 return 0;
197}
198
202{
203 if (!stralloc_copys(ipout,"")) return DNS_MEM;
204 if (!stralloc_copys(fqdn,"")) return DNS_MEM;
205 ipout->len = 0;
206
207 if (byte_equal(in->s,9,LOCALHOST)) {
208 if (!stralloc_copyb(ipout,(const char *) V6loopback,16)) return DNS_MEM;
209 if (!stralloc_catb(ipout,(const char *) V46loopback,16)) return DNS_MEM;
210 if (!stralloc_copys(fqdn,"localhost.localhost.")) return DNS_MEM;
211 }
212 if (byte_equal(in->s,13,IP4_LOOPBACK)) {
213 if (!stralloc_copyb(ipout,(const char *) V46loopback,16)) return DNS_MEM;
214 if (!stralloc_copys(fqdn,"ip4-loopback.localhost.")) return DNS_MEM;
215 }
216 if (byte_equal(in->s,13,IP6_LOOPBACK)) {
217 if (!stralloc_copyb(ipout,(const char *) V6loopback,16)) return DNS_MEM;
218 if (!stralloc_copys(fqdn,"ip6-loopback.localhost.")) return DNS_MEM;
219 }
220// if (!stralloc_0(fqdn)) return DNS_MEM; // don't do it
221
222 return ipout->len ? ipout->len % 15 : 0;
223}
224
227int dns_ip_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in)
228{
229 int r;
230 static stralloc rules;
231
232 if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0 ) return r;
233 if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT;
234 return dns_ip_qualify_rules(ipout,fqdn,in,&rules);
235}
const unsigned char V6loopback[16]
Definition: socket_if.c:20
const unsigned char V4mappedprefix[12]
Definition: socket_if.c:18
int dns_ip4_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in)
Definition: dns_ipq.c:82
int dns_ip_qualify_localhost(stralloc *ipout, stralloc *fqdn, const stralloc *in)
Definition: dns_ipq.c:201
int dns_ip6_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in)
Definition: dns_ipq.c:130
int dns_ip4_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules)
Definition: dns_ipq.c:46
int dns_ip6_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules)
Definition: dns_ipq.c:94
int dns_ip_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in)
Definition: dns_ipq.c:227
int dns_ip_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules)
Definition: dns_ipq.c:142
int case_diffb(char *, unsigned int, char *)
Definition: case.c:10
unsigned int byte_chr(char *, unsigned int, int)
Definition: byte.c:9
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
#define byte_equal(s, n, t)
Definition: byte.h:18
int dns_ip4(stralloc *, stralloc *)
Definition: dns_ip.c:49
#define DNS_ERR
Definition: dnsresolv.h:44
#define IP4_LOOPBACK
Definition: dnsresolv.h:96
#define DNS_INT
Definition: dnsresolv.h:46
#define IP6_LOOPBACK
Definition: dnsresolv.h:97
int dns_resolvconfrewrite(stralloc *)
Definition: dns_rcrw.c:122
int dns_ip6(stralloc *, stralloc *)
Definition: dns_ip.c:148
#define DNS_MEM
Definition: dnsresolv.h:43
#define LOCALHOST
Definition: dnsresolv.h:95
unsigned int str_chr(const char *, int)
Definition: str.c:81
int stralloc_copy(stralloc *, stralloc *)
Definition: stralloc.c:41
int stralloc_catb(stralloc *, const char *, unsigned int)
Definition: stralloc.c:26
int stralloc_copyb(stralloc *, const char *, unsigned int)
Definition: stralloc.c:70
int stralloc_cat(stralloc *, stralloc *)
Definition: stralloc.c:21
int stralloc_cats(stralloc *, const char *)
Definition: stralloc.c:36
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
size_t len
Definition: stralloc.h:19
char * s
Definition: stralloc.h:18