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