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