39static int packetquery(
char *buf,
unsigned int len,
char **q,
char qtype[2],
char qclass[2],
char id[2])
47 if (header[2] & 128)
return 0;
48 if (!(header[2] & 1))
return 0;
49 if (header[2] & 120)
return 0;
50 if (header[2] & 2)
return 0;
51 if (header[3] & 32) ad = 1;
52 if (byte_diff(header + 4,2,
"\0\1"))
return 0;
60 byte_copy(
id,2,header);
64static char ipsending[16];
65static char iplistening[16];
72static struct udpclient {
88 if (!u[j].active)
return;
96 if (!u[j].active)
return;
131 len = socket_recv(udp53,
buf,
sizeof(
buf),
x->ip,&
x->port,&
x->scope_id);
132 if (
len == -1)
return;
133 if (
len >=
sizeof(
buf))
return;
134 if (
x->port < 1024)
if (
x->port != 53)
return;
137 if (ip6_isv4mapped(
x->ip))
len = ip4_fmt(
ipstr,
x->ip + 12);
140 logmsg(
WHO,99,WARN,B(
"client blocked: ",
ipstr));
144 if (!packetquery(
buf,
len,&q,qtype,qclass,
x->id))
return;
151 switch (
query_start(&
x->q,q,qtype,qclass,ipsending,
x->scope_id)) {
152 case -1:
case -2:
u_drop(j);
return;
187 if (!
t[j].
buf)
return;
188 alloc_free(
t[j].
buf);
195 if (!
t[j].active)
return;
197 taia_uint(&
t[j].timeout,10);
198 taia_add(&
t[j].timeout,&
t[j].timeout,&
now);
203 if (!
t[j].active)
return;
220 if (!
t[j].active)
return;
239 unsigned int readsize;
245 if (
x->state == -1) {
246 r = write(
x->tcp,
x->buf +
x->pos,
x->len -
x->pos);
247 if (r <= 0) {
t_close(j);
return; }
249 if (
x->pos ==
x->len) {
257 case 1: readsize = 2U;
break;
258 case 2: readsize = 1U;
break;
259 case 3: readsize =
x->len -
x->pos;
break;
263 r = read(
x->tcp,
buf,readsize);
264 if (r == 0) { errno = EPIPE;
t_close(j);
return; }
265 if (r < 0) {
t_close(j);
return; }
269 x->len = (
unsigned char) *ch++;
272 if (--r <= 0)
return;
275 x->len += (
unsigned char) *ch;
276 if (!
x->len) { errno = EPIPE;
t_close(j);
return; }
277 x->buf = alloc(
x->len);
278 if (!
x->buf) {
t_close(j);
return; }
284 if (
x->state != 3)
return;
286 byte_copy(&
x->buf[
x->pos],r,ch);
288 if (
x->pos <
x->len)
return;
290 if (!packetquery(
x->buf,
x->len,&
q,qtype,qclass,
x->id)) {
t_close(j);
return; }
296 case -1:
case -2:
t_drop(j);
return;
328 x->tcp = socket_accept(tcp53,
x->ip,&
x->port,&
x->scope_id);
329 if (
x->tcp == -1)
return;
330 if (
x->port < 1024)
if (
x->port != 53) { close(
x->tcp);
return; }
333 if (ip6_isv4mapped(
x->ip))
len = ip4_fmt(
ipstr,
x->ip + 12);
336 logmsg(
WHO,99,WARN,B(
"client blocked: ",
ipstr));
340 if (ndelay_on(
x->tcp) == -1) { close(
x->tcp);
return; }
354static void doit(
void)
357 struct taia deadline;
364 taia_uint(&deadline,120);
365 taia_add(&deadline,&deadline,&stamp);
371 udp53io->events = IOPAUSE_READ;
375 tcp53io->events = IOPAUSE_READ;
389 if (taia_less(&
t[j].timeout,&deadline)) deadline =
t[j].
timeout;
391 t[j].
io->events = (
t[j].
state > 0) ? IOPAUSE_READ : IOPAUSE_WRITE;
395 if (iopause(
io,
iolen,&deadline,&stamp) < 0) {
397 logmsg(
WHO,errno,FATAL,
"IO resources not available");
403 if (r == -1 || r == -2 || r == -3) { errno = ECONNRESET;
u_drop(j); }
410 if (
t[j].state == 0) {
412 if (r == -1 || r == -2 || r == -3) { errno = ECONNRESET;
t_drop(j); }
415 else if (
t[j].
io->revents || taia_less(&
t[j].timeout,&stamp))
434 unsigned long cachesize;
440 logmsg(
WHO,111,ERROR,
"$IP not set");
441 if (case_equals(
x,
"::")) {
443 }
else if (case_equals(
x,
":0")) {
448 if (!ip6_ifscan(
x,iplistening,&
ifname))
449 logmsg(
WHO,101,SYNTAX,B(
"unable to parse IP address: ",
x));
453 if (ip6_isv4mapped(iplistening))
454 udp53 = socket_udp4();
456 udp53 = socket_udp();
458 logmsg(
WHO,111,FATAL,
"unable to create UDP socket");
462 if (socket_bind_reuse(udp53,iplistening,
dnsport,
ifidx) == -1)
463 logmsg(
WHO,111,FATAL,
"unable to bind to UDP socket");
465 if (ip6_isv4mapped(iplistening))
466 tcp53 = socket_tcp4();
468 tcp53 = socket_tcp6();
470 logmsg(
WHO,111,FATAL,
"unable to create TCP socket");
474 if (socket_bind_reuse(tcp53,iplistening,
dnsport,
ifidx) == -1)
475 logmsg(
WHO,111,FATAL,
"unable to bind to TCP socket");
479 socket_tryreservein(udp53,131072);
487 x = env_get(
"IPSEND");
489 logmsg(
WHO,111,ERROR,
"$IPSEND not set");
490 if (!ip6_ifscan(
x,ipsending,&
ifname))
491 logmsg(
WHO,100,SYNTAX,B(
"unable to parse IP address: ",
x));
493 x = env_get(
"CACHESIZE");
495 logmsg(
WHO,111,ERROR,
"$CACHESIZE not set");
496 scan_ulong(
x,&cachesize);
498 logmsg(
WHO,111,FATAL,B(
"not enough memory for cache of size: ",
x));
500 if (env_get(
"HIDETTL"))
502 if (env_get(
"FORWARDONLY"))
504 if (env_get(
"USETXTFORMAT"))
506 if (env_get(
"USETEXTFORMAT"))
508 x = env_get(
"UZ5FALLBACK");
510 if (env_get(
"FLAGEDSERVER"))
515 logmsg(
WHO,111,ERROR,
"unable to read servers");
518 logmsg(
WHO,111,FATAL,
"unable to listen on TCP socket");
unsigned int doit(char *buf, unsigned int len, unsigned int pos)
int clientok(char ip[16])
unsigned int flagedserver
unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d)
unsigned int dns_packet_copy(const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
void dns_random_init(const char data[128])
struct tcpclient t[QUERY_MAXTCP]
iopause_fd io[3+QUERY_MAXUDP+QUERY_MAXTCP]
void droproot(const char *fatal)
unsigned int dns_packet_edns0(const char header[12], const char *buf, const int len, unsigned int pos)
void start(const char *s)
void log_querydrop(uint64 *, const char *)
void log_tcpopen(const char *, unsigned int)
void log_query(uint64 *, const char *, unsigned int, const char *, const char *, const char *, const char *)
void log_startup(const char *, uint32, const char *, int)
void log_querydone(uint64 *, unsigned int, const char *)
void log_tcpclose(const char *, unsigned int)
int query_start(struct query *, char *, char *, char *, char *, uint32)
void query_io(struct query *, iopause_fd *, struct taia *)
int query_get(struct query *, iopause_fd *, struct taia *)
void query_forwardonly(void)
void response_hidettl(void)
void response_id(const char *)
unsigned int response_len
int cache_init(unsigned int)