djbdnscurve6 53
djbdnscurve6
Loading...
Searching...
No Matches
dnsfilter.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "buffer.h"
3#include "stralloc.h"
4#include "alloc.h"
5#include "ip.h"
6#include "byte.h"
7#include "scan.h"
8#include "taia.h"
9#include "getoptb.h"
10#include "iopause.h"
11#include "logmsg.h"
12#include "exit.h"
13#include "str.h"
14#include "scan.h"
15#include "query.h"
16#include "dns.h"
17#include "curvedns.h"
18
19#define WHO "dnsfilter"
20
21unsigned int msgsize = MSGSIZE;
22unsigned int flagedns0 = 0;
23
24void nomem(void)
25{
26 logmsg(WHO,111,FATAL,"out of memory");
27}
28
29struct line {
30 stralloc left;
31 stralloc middle;
32 stralloc right;
35 iopause_fd *io;
36} *x;
37
38struct line tmp;
39unsigned int xmax = 1000;
40unsigned int xnum = 0;
41unsigned int numactive = 0;
42unsigned int maxactive = 10;
43
44static stralloc partial;
45
46char inbuf[1024];
47int inbuflen = 0;
48iopause_fd *inio;
49int flag0 = 1;
50
51iopause_fd *io;
53
56char ip4[4];
57char ip6[16];
59
60void errout(int i)
61{
62 int j;
63
64 if (!stralloc_copys(&x[i].middle,"?")) nomem();
65 if (!stralloc_cats(&x[i].middle,errstr(errno))) nomem();
66
67 for (j = 0; j < x[i].middle.len; ++j)
68 if (x[i].middle.s[j] == ' ')
69 x[i].middle.s[j] = '-';
70}
71
72int main(int argc,char **argv)
73{
74 struct taia stamp;
75 struct taia deadline;
76 int opt;
77 unsigned long u;
78 int i;
79 int j;
80 int r;
81 char qflags[2] = { 1, 0 }; // flagrecursive, flagedns0
82
83 while ((opt = getoptb(argc,argv,"c:l:")) != opteof)
84 switch(opt) {
85 case 'c':
86 scan_dnum(optarg,&u);
87 if (u < 1) u = 1;
88 if (u > 1000) u = 1000;
89 maxactive = u;
90 break;
91 case 'l':
92 scan_dnum(optarg,&u);
93 if (u < 1) u = 1;
94 if (u > 1000000) u = 1000000;
95 xmax = u;
96 break;
97 default:
98 logmsg(WHO,100,USAGE,"dnsfilter [ -c concurrency ] [ -l lines ]");
99 }
100
101 x = (struct line *) alloc(xmax * sizeof(struct line));
102 if (!x) nomem();
103 byte_zero((char *)x,xmax * sizeof(struct line));
104
105 io = (iopause_fd *) alloc((xmax + 1) * sizeof(iopause_fd));
106 if (!io) nomem();
107
108 if (!stralloc_copys(&partial,"")) nomem();
109
110
111 while (flag0 || inbuflen || partial.len || xnum) {
112 taia_now(&stamp);
113 taia_uint(&deadline,120);
114 taia_add(&deadline,&deadline,&stamp);
115
116 iolen = 0;
117
118 if (flag0)
119 if (inbuflen < sizeof(inbuf)) {
120 inio = io + iolen++;
121 inio->fd = 0;
122 inio->events = IOPAUSE_READ;
123 }
124
125 for (i = 0; i < xnum; ++i)
126 if (x[i].flagactive) {
127 x[i].io = io + iolen++;
128 dns_transmit_io(&x[i].dt,x[i].io,&deadline);
129 }
130
131 iopause(io,iolen,&deadline,&stamp);
132
133 if (flag0)
134 if (inbuflen < sizeof(inbuf))
135 if (inio->revents) {
136 r = read(0,inbuf + inbuflen,(sizeof(inbuf)) - inbuflen);
137 if (r <= 0)
138 flag0 = 0;
139 else
140 inbuflen += r;
141 }
142
143 for (i = 0; i < xnum; ++i)
144 if (x[i].flagactive) {
145 r = dns_transmit_get(&x[i].dt,x[i].io,&stamp);
146 if (r < 0) {
147 errout(i);
148 x[i].flagactive = 0;
149 --numactive;
150 }
151 else if (r == 1) {
152 if (dns_name_packet(&x[i].middle,x[i].dt.packet,x[i].dt.packetlen) < 0)
153 errout(i);
154 if (x[i].middle.len)
155 if (!stralloc_cats(&x[i].left,"=")) nomem();
156 x[i].flagactive = 0;
157 --numactive;
158 }
159 }
160
161 for (;;) {
162 if (xnum && !x[0].flagactive) {
163 buffer_put(buffer_1,x[0].left.s,x[0].left.len);
164 buffer_put(buffer_1,x[0].middle.s,x[0].middle.len);
165 buffer_put(buffer_1,x[0].right.s,x[0].right.len);
166 buffer_flush(buffer_1);
167 --xnum;
168 tmp = x[0];
169 for (i = 0;i < xnum;++i) x[i] = x[i + 1];
170 x[xnum] = tmp;
171 continue;
172 }
173
174 if ((xnum < xmax) && (numactive < maxactive)) {
175 i = byte_chr(inbuf,inbuflen,'\n');
176 if (inbuflen && (i == inbuflen)) {
177 if (!stralloc_catb(&partial,inbuf,inbuflen)) nomem();
178 inbuflen = 0;
179 continue;
180 }
181
182 if ((i < inbuflen) || (!flag0 && partial.len)) {
183 if (i < inbuflen) ++i;
184 if (!stralloc_catb(&partial,inbuf,i)) nomem();
185 inbuflen -= i;
186 for (j = 0; j < inbuflen; ++j) inbuf[j] = inbuf[j + i];
187
188 if (partial.len) {
189 i = byte_chr(partial.s,partial.len,'\n');
190 i = byte_chr(partial.s,i,'\t');
191 i = byte_chr(partial.s,i,' ');
192
193 if (!stralloc_copyb(&x[xnum].left,partial.s,i)) nomem();
194 if (!stralloc_copys(&x[xnum].middle,"")) nomem();
195 if (!stralloc_copyb(&x[xnum].right,partial.s + i,partial.len - i)) nomem();
196 x[xnum].flagactive = 0;
197
198 partial.len = i;
199 if (!stralloc_0(&partial)) nomem();
200 if (str_chr(partial.s,':') == partial.len - 1) {
201 if (ip4_scan(partial.s,ip4)) dns_name4_domain(name,ip4);
202 } else {
203 if (ip6_scan(partial.s,ip6)) dns_name6_domain(name,ip6);
204 }
205
207 logmsg(WHO,111,FATAL,"unable to read /etc/resolv.conf");
208
209 if (dns_transmit_start6(&x[xnum].dt,servers,qflags,name,DNS_T_PTR,V6localnet,scopes) < 0)
210 errout(xnum);
211 else {
212 x[xnum].flagactive = 1;
213 ++numactive;
214 }
215 ++xnum;
216 }
217
218 partial.len = 0;
219 continue;
220 }
221 }
222
223 break;
224 }
225 }
226
227 _exit(0);
228}
int main()
Definition: axfrdns.c:326
#define QUERY_MAXIPLEN
Definition: dns.h:55
#define MSGSIZE
Definition: dns.h:47
#define DNS_T_PTR
Definition: dns.h:69
#define QUERY_MAXNS
Definition: dns.h:54
#define DNS_NAME6_DOMAIN
Definition: dns.h:191
int dns_name_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_name.c:9
int dns_name4_domain(char name[DNS_NAME4_DOMAIN], const char ip[4])
Definition: dns_nd.c:6
int dns_name6_domain(char name[DNS_NAME6_DOMAIN], const char ip[16])
Definition: dns_nd.c:28
int dns_resolvconfip(char s[QUERY_MAXIPLEN], uint32 scope[QUERY_MAXNS])
Definition: dns_rcip.c:88
void dns_transmit_io(struct dns_transmit *d, iopause_fd *x, struct taia *deadline)
Definition: dns_transmit.c:285
int dns_transmit_start6(struct dns_transmit *d, const char servers[QUERY_MAXIPLEN], char qflags[2], const char *q, const char qtype[2], const char localip[16], const uint32 scopes[QUERY_MAXNS])
Definition: dns_transmit.c:276
int dns_transmit_get(struct dns_transmit *d, const iopause_fd *x, const struct taia *when)
Definition: dns_transmit.c:302
char inbuf[1024]
Definition: dnsfilter.c:46
int iolen
Definition: dnsfilter.c:52
unsigned int numactive
Definition: dnsfilter.c:41
iopause_fd * inio
Definition: dnsfilter.c:48
char ip6[16]
Definition: dnsfilter.c:57
unsigned int maxactive
Definition: dnsfilter.c:42
iopause_fd * io
Definition: dnsfilter.c:51
int flag0
Definition: dnsfilter.c:49
int inbuflen
Definition: dnsfilter.c:47
char servers[QUERY_MAXIPLEN]
Definition: dnsfilter.c:54
struct line * x
unsigned int xmax
Definition: dnsfilter.c:39
void nomem(void)
Definition: dnsfilter.c:24
uint32 scopes[QUERY_MAXNS]
Definition: dnsfilter.c:55
unsigned int xnum
Definition: dnsfilter.c:40
unsigned int flagedns0
Definition: dnsfilter.c:22
char name[DNS_NAME6_DOMAIN]
Definition: dnsfilter.c:58
char ip4[4]
Definition: dnsfilter.c:56
struct line tmp
Definition: dnsfilter.c:38
void errout(int i)
Definition: dnsfilter.c:60
unsigned int msgsize
Definition: dnsfilter.c:21
#define WHO
Definition: dnsfilter.c:19
unsigned int packetlen
Definition: dns.h:109
char * packet
Definition: dns.h:108
Definition: dnsfilter.c:29
iopause_fd * io
Definition: dnsfilter.c:35
stralloc left
Definition: dnsfilter.c:30
int flagactive
Definition: dnsfilter.c:34
stralloc middle
Definition: dnsfilter.c:31
stralloc right
Definition: dnsfilter.c:32
struct dns_transmit dt
Definition: dnsfilter.c:33
unsigned long u
Definition: utime.c:10