+ * Understand and sort of check OpenBSD `lookup' resolv.conf directive.
[adns] / src / setup.c
CommitLineData
e576be50 1/*
2 * setup.c
3 * - configuration file parsing
4 * - management of global state
5 */
6/*
d942707d 7 * This file is
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
9 *
10 * It is part of adns, which is
3d5cde09 11 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
bef232ae 12 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
e576be50 13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
656b2da9 28
4353a5c4 29#include <stdlib.h>
30#include <errno.h>
78bcc172 31#include <limits.h>
4353a5c4 32#include <unistd.h>
33#include <fcntl.h>
656b2da9 34
4353a5c4 35#include <netdb.h>
71a6ff46 36#include <sys/socket.h>
37#include <netinet/in.h>
4353a5c4 38#include <arpa/inet.h>
39
40#include "internal.h"
41
fb7fbb66 42static void readconfig(adns_state ads, const char *filename, int warnmissing);
36369543 43
656b2da9 44static void addserver(adns_state ads, struct in_addr addr) {
45 int i;
46 struct server *ss;
47
48 for (i=0; i<ads->nservers; i++) {
49 if (ads->servers[i].addr.s_addr == addr.s_addr) {
3955725c 50 adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr));
656b2da9 51 return;
52 }
53 }
54
55 if (ads->nservers>=MAXSERVERS) {
3955725c 56 adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr));
656b2da9 57 return;
58 }
59
60 ss= ads->servers+ads->nservers;
61 ss->addr= addr;
656b2da9 62 ads->nservers++;
63}
64
914a5ff5 65static void freesearchlist(adns_state ads) {
66 if (ads->nsearchlist) free(*ads->searchlist);
67 free(ads->searchlist);
68}
69
36369543 70static void saveerr(adns_state ads, int en) {
71 if (!ads->configerrno) ads->configerrno= en;
72}
73
656b2da9 74static void configparseerr(adns_state ads, const char *fn, int lno,
75 const char *fmt, ...) {
76 va_list al;
36369543 77
78 saveerr(ads,EINVAL);
79 if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return;
80
81 if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn);
82 else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno);
656b2da9 83 va_start(al,fmt);
36369543 84 vfprintf(ads->diagfile,fmt,al);
656b2da9 85 va_end(al);
36369543 86 fputc('\n',ads->diagfile);
656b2da9 87}
88
32af6b2a 89static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
90 const char *p, *q;
91
92 p= *bufp_io;
93 while (ctype_whitespace(*p)) p++;
94 if (!*p) return 0;
95
96 q= p;
97 while (*q && !ctype_whitespace(*q)) q++;
98
99 *l_r= q-p;
100 *word_r= p;
101 *bufp_io= q;
102
103 return 1;
104}
105
656b2da9 106static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
107 struct in_addr ia;
108
109 if (!inet_aton(buf,&ia)) {
110 configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
111 return;
112 }
3955725c 113 adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia));
656b2da9 114 addserver(ads,ia);
115}
116
117static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
32af6b2a 118 const char *bufp, *word;
119 char *newchars, **newptrs, **pp;
120 int count, tl, l;
121
656b2da9 122 if (!buf) return;
32af6b2a 123
124 bufp= buf;
125 count= 0;
126 tl= 0;
127 while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
128
129 newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; }
130 newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
131
132 bufp= buf;
133 pp= newptrs;
134 while (nextword(&bufp,&word,&l)) {
135 *pp++= newchars;
136 memcpy(newchars,word,l);
137 newchars += l;
138 *newchars++ = 0;
139 }
140
914a5ff5 141 freesearchlist(ads);
32af6b2a 142 ads->nsearchlist= count;
143 ads->searchlist= newptrs;
656b2da9 144}
145
32af6b2a 146static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
147 const char *word;
09957b1c 148 char tbuf[200], *slash, *ep;
149 struct in_addr base, mask;
150 int l;
151 unsigned long initial, baselocal;
152
32af6b2a 153 if (!buf) return;
154
09957b1c 155 ads->nsortlist= 0;
32af6b2a 156 while (nextword(&buf,&word,&l)) {
09957b1c 157 if (ads->nsortlist >= MAXSORTLIST) {
32af6b2a 158 adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word);
09957b1c 159 return;
160 }
161
162 if (l >= sizeof(tbuf)) {
32af6b2a 163 configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
09957b1c 164 continue;
165 }
166
78bcc172 167 memcpy(tbuf,word,l); tbuf[l]= 0;
09957b1c 168 slash= strchr(tbuf,'/');
169 if (slash) *slash++= 0;
170
171 if (!inet_aton(tbuf,&base)) {
172 configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
173 continue;
174 }
175
176 if (slash) {
177 if (strchr(slash,'.')) {
178 if (!inet_aton(slash,&mask)) {
179 configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
180 continue;
181 }
182 if (base.s_addr & ~mask.s_addr) {
183 configparseerr(ads,fn,lno,
184 "mask `%s' in sortlist overlaps address `%s'",slash,tbuf);
185 continue;
186 }
187 } else {
188 initial= strtoul(slash,&ep,10);
189 if (*ep || initial>32) {
190 configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
191 continue;
192 }
193 mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
194 }
195 } else {
196 baselocal= ntohl(base.s_addr);
197 if (!baselocal & 0x080000000UL) /* class A */
198 mask.s_addr= htonl(0x0ff000000UL);
199 else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
200 mask.s_addr= htonl(0x0ffff0000UL); /* class B */
201 else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
202 mask.s_addr= htonl(0x0ff000000UL); /* class C */
203 else {
204 configparseerr(ads,fn,lno,
205 "network address `%s' in sortlist is not in classed ranges,"
206 " must specify mask explicitly", tbuf);
207 continue;
208 }
209 }
210
211 ads->sortlist[ads->nsortlist].base= base;
212 ads->sortlist[ads->nsortlist].mask= mask;
213 ads->nsortlist++;
214 }
656b2da9 215}
216
217static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
78bcc172 218 const char *word;
219 char *ep;
220 unsigned long v;
221 int l;
222
656b2da9 223 if (!buf) return;
78bcc172 224
225 while (nextword(&buf,&word,&l)) {
226 if (l==5 && !memcmp(word,"debug",5)) {
227 ads->iflags |= adns_if_debug;
228 continue;
229 }
230 if (l>=6 && !memcmp(word,"ndots:",6)) {
231 v= strtoul(word+6,&ep,10);
232 if (l==6 || ep != word+l || v > INT_MAX) {
233 configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word);
234 continue;
235 }
236 ads->searchndots= v;
237 continue;
238 }
3e2e5fab 239 if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
240 if (!strcmp(word+12,"none")) {
241 ads->iflags &= ~adns_if_checkc_freq;
242 ads->iflags |= adns_if_checkc_entex;
243 } else if (!strcmp(word+12,"entex")) {
244 ads->iflags &= ~adns_if_checkc_freq;
245 ads->iflags |= adns_if_checkc_entex;
246 } else if (!strcmp(word+12,"freq")) {
247 ads->iflags |= adns_if_checkc_freq;
248 } else {
249 configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
250 "(must be none, entex or freq", word+12);
251 }
252 continue;
253 }
78bcc172 254 adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
255 }
656b2da9 256}
257
258static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
259 ads->nservers= 0;
260}
261
36369543 262static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) {
263 if (!*buf) {
264 configparseerr(ads,fn,lno,"`include' directive with no filename");
265 return;
266 }
fb7fbb66 267 readconfig(ads,buf,1);
36369543 268}
269
0f15dd7b 270static void ccf_lookup(adns_state ads, const char *fn, int lno,
271 const char *buf) {
272 int found_bind=0;
273 const char *word;
274 int l;
275
276 if (!*buf) {
277 configparseerr(ads,fn,lno,"`lookup' directive with no databases");
278 return;
279 }
280
281 while (nextword(&buf,&word,&l)) {
282 if (l==4 && !memcmp(word,"bind",4)) {
283 found_bind=1;
284 } else if (l==4 && !memcmp(word,"file",4)) {
285 /* ignore this and hope /etc/hosts is not essential */
286 } else if (l==2 && !memcmp(word,"yp",2)) {
287 adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
288 found_bind=-1;
289 } else {
290 adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
291 fn,lno, l,word);
292 found_bind=-1;
293 }
294 }
295 if (!found_bind)
296 adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
297}
298
656b2da9 299static const struct configcommandinfo {
300 const char *name;
301 void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
302} configcommandinfos[]= {
303 { "nameserver", ccf_nameserver },
304 { "domain", ccf_search },
305 { "search", ccf_search },
306 { "sortlist", ccf_sortlist },
307 { "options", ccf_options },
308 { "clearnameservers", ccf_clearnss },
36369543 309 { "include", ccf_include },
0f15dd7b 310 { "lookup", ccf_lookup }, /* OpenBSD */
656b2da9 311 { 0 }
312};
313
36369543 314typedef union {
656b2da9 315 FILE *file;
36369543 316 const char *text;
317} getline_ctx;
656b2da9 318
36369543 319static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
320 int lno, char *buf, int buflen) {
321 FILE *file= src_io->file;
322 int c, i;
323 char *p;
324
325 p= buf;
326 buflen--;
327 i= 0;
328
329 for (;;) { /* loop over chars */
330 if (i == buflen) {
331 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
332 goto x_badline;
333 }
334 c= getc(file);
335 if (!c) {
336 adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
337 goto x_badline;
338 } else if (c == '\n') {
339 break;
340 } else if (c == EOF) {
341 if (ferror(file)) {
342 saveerr(ads,errno);
343 adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno));
344 return -1;
345 }
346 if (!i) return -1;
347 break;
348 } else {
349 *p++= c;
350 i++;
656b2da9 351 }
656b2da9 352 }
353
36369543 354 *p++= 0;
355 return i;
356
357 x_badline:
358 saveerr(ads,EINVAL);
359 while ((c= getc(file)) != EOF && c != '\n');
360 return -2;
361}
362
363static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
364 int lno, char *buf, int buflen) {
09957b1c 365 const char *cp= src_io->text;
36369543 366 int l;
367
09957b1c 368 if (!cp || !*cp) return -1;
36369543 369
09957b1c 370 if (*cp == ';' || *cp == '\n') cp++;
371 l= strcspn(cp,";\n");
372 src_io->text = cp+l;
36369543 373
374 if (l >= buflen) {
375 adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
376 saveerr(ads,EINVAL);
377 return -2;
378 }
379
380 memcpy(buf,cp,l);
381 buf[l]= 0;
382 return l;
383}
384
385static void readconfiggeneric(adns_state ads, const char *filename,
386 int (*getline)(adns_state ads, getline_ctx*,
387 const char *filename, int lno,
388 char *buf, int buflen),
389 /* Returns >=0 for success, -1 for EOF or error
390 * (error will have been reported), or -2 for
391 * bad line was encountered, try again.
392 */
393 getline_ctx gl_ctx) {
394 char linebuf[2000], *p, *q;
395 int lno, l, dirl;
396 const struct configcommandinfo *ccip;
397
398 for (lno=1;
399 (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
400 lno++) {
401 if (l == -2) continue;
656b2da9 402 while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
403 linebuf[l]= 0;
404 p= linebuf;
405 while (ctype_whitespace(*p)) p++;
36369543 406 if (*p == '#' || !*p) continue;
656b2da9 407 q= p;
408 while (*q && !ctype_whitespace(*q)) q++;
36369543 409 dirl= q-p;
656b2da9 410 for (ccip=configcommandinfos;
36369543 411 ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
656b2da9 412 ccip++);
413 if (!ccip->name) {
3955725c 414 adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
4353a5c4 415 filename,lno,q-p,p);
656b2da9 416 continue;
417 }
418 while (ctype_whitespace(*q)) q++;
419 ccip->fn(ads,filename,lno,q);
420 }
656b2da9 421}
422
423static const char *instrum_getenv(adns_state ads, const char *envvar) {
424 const char *value;
425
426 value= getenv(envvar);
3955725c 427 if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
428 else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value);
656b2da9 429 return value;
430}
431
fb7fbb66 432static void readconfig(adns_state ads, const char *filename, int warnmissing) {
36369543 433 getline_ctx gl_ctx;
434
435 gl_ctx.file= fopen(filename,"r");
436 if (!gl_ctx.file) {
437 if (errno == ENOENT) {
fb7fbb66 438 if (warnmissing)
439 adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename);
36369543 440 return;
441 }
442 saveerr(ads,errno);
443 adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
444 filename,strerror(errno));
445 return;
446 }
447
448 readconfiggeneric(ads,filename,gl_file,gl_ctx);
449
450 fclose(gl_ctx.file);
451}
452
453static void readconfigtext(adns_state ads, const char *text, const char *showname) {
454 getline_ctx gl_ctx;
455
456 gl_ctx.text= text;
457 readconfiggeneric(ads,showname,gl_text,gl_ctx);
458}
459
656b2da9 460static void readconfigenv(adns_state ads, const char *envvar) {
461 const char *filename;
462
463 if (ads->iflags & adns_if_noenv) {
3955725c 464 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
656b2da9 465 return;
466 }
467 filename= instrum_getenv(ads,envvar);
fb7fbb66 468 if (filename) readconfig(ads,filename,1);
656b2da9 469}
4353a5c4 470
09957b1c 471static void readconfigenvtext(adns_state ads, const char *envvar) {
472 const char *textdata;
473
474 if (ads->iflags & adns_if_noenv) {
475 adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
476 return;
477 }
478 textdata= instrum_getenv(ads,envvar);
479 if (textdata) readconfigtext(ads,textdata,envvar);
480}
481
4353a5c4 482
483int adns__setnonblock(adns_state ads, int fd) {
484 int r;
656b2da9 485
4353a5c4 486 r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
487 r |= O_NONBLOCK;
488 r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
489 return 0;
490}
491
36369543 492static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
656b2da9 493 adns_state ads;
656b2da9 494
495 ads= malloc(sizeof(*ads)); if (!ads) return errno;
36369543 496
656b2da9 497 ads->iflags= flags;
36369543 498 ads->diagfile= diagfile;
d855b532 499 ads->configerrno= 0;
f7f83b4a 500 LIST_INIT(ads->udpw);
501 LIST_INIT(ads->tcpw);
4353a5c4 502 LIST_INIT(ads->childw);
503 LIST_INIT(ads->output);
8ce38e76 504 ads->forallnext= 0;
4353a5c4 505 ads->nextid= 0x311f;
506 ads->udpsocket= ads->tcpsocket= -1;
4353a5c4 507 adns__vbuf_init(&ads->tcpsend);
508 adns__vbuf_init(&ads->tcprecv);
70ad7a2a 509 ads->tcprecv_skip= 0;
32af6b2a 510 ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
660d7d3b 511 ads->searchndots= 1;
d855b532 512 ads->tcpstate= server_disconnected;
4353a5c4 513 timerclear(&ads->tcptimeout);
d855b532 514 ads->searchlist= 0;
656b2da9 515
36369543 516 *ads_r= ads;
517 return 0;
518}
656b2da9 519
36369543 520static int init_finish(adns_state ads) {
521 struct in_addr ia;
522 struct protoent *proto;
523 int r;
524
656b2da9 525 if (!ads->nservers) {
36369543 526 if (ads->diagfile && ads->iflags & adns_if_debug)
527 fprintf(ads->diagfile,"adns: no nameservers, using localhost\n");
09957b1c 528 ia.s_addr= htonl(INADDR_LOOPBACK);
656b2da9 529 addserver(ads,ia);
530 }
531
532 proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
533 ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
8402e34c 534 if (ads->udpsocket<0) { r= errno; goto x_free; }
4bec51a4 535
94436798 536 r= adns__setnonblock(ads,ads->udpsocket);
537 if (r) { r= errno; goto x_closeudp; }
656b2da9 538
656b2da9 539 return 0;
540
94436798 541 x_closeudp:
542 close(ads->udpsocket);
656b2da9 543 x_free:
544 free(ads);
545 return r;
546}
547
32af6b2a 548static void init_abort(adns_state ads) {
549 if (ads->nsearchlist) {
550 free(ads->searchlist[0]);
551 free(ads->searchlist);
552 }
553 free(ads);
554}
555
e563872a 556int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
36369543 557 adns_state ads;
558 const char *res_options, *adns_res_options;
559 int r;
560
561 r= init_begin(&ads, flags, diagfile ? diagfile : stderr);
562 if (r) return r;
563
564 res_options= instrum_getenv(ads,"RES_OPTIONS");
565 adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
566 ccf_options(ads,"RES_OPTIONS",-1,res_options);
567 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
568
fb7fbb66 569 readconfig(ads,"/etc/resolv.conf",1);
570 readconfig(ads,"/etc/resolv-adns.conf",0);
36369543 571 readconfigenv(ads,"RES_CONF");
572 readconfigenv(ads,"ADNS_RES_CONF");
573
09957b1c 574 readconfigenvtext(ads,"RES_CONF_TEXT");
575 readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
576
36369543 577 ccf_options(ads,"RES_OPTIONS",-1,res_options);
578 ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
579
580 ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
581 ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
582
32af6b2a 583 if (ads->configerrno && ads->configerrno != EINVAL) {
584 r= ads->configerrno;
585 init_abort(ads);
586 return r;
587 }
588
36369543 589 r= init_finish(ads);
590 if (r) return r;
591
28de6442 592 adns__consistency(ads,0,cc_entex);
36369543 593 *ads_r= ads;
594 return 0;
595}
596
e563872a 597int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
36369543 598 FILE *diagfile, const char *configtext) {
599 adns_state ads;
600 int r;
601
602 r= init_begin(&ads, flags, diagfile); if (r) return r;
603
604 readconfigtext(ads,configtext,"<supplied configuration text>");
605 if (ads->configerrno) {
606 r= ads->configerrno;
32af6b2a 607 init_abort(ads);
36369543 608 return r;
609 }
610
611 r= init_finish(ads); if (r) return r;
28de6442 612 adns__consistency(ads,0,cc_entex);
36369543 613 *ads_r= ads;
614 return 0;
615}
616
3e2e5fab 617
9ec44266 618void adns_finish(adns_state ads) {
28de6442 619 adns__consistency(ads,0,cc_entex);
9ec44266 620 for (;;) {
f7f83b4a 621 if (ads->udpw.head) adns_cancel(ads->udpw.head);
622 else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
9ec44266 623 else if (ads->childw.head) adns_cancel(ads->childw.head);
624 else if (ads->output.head) adns_cancel(ads->output.head);
625 else break;
626 }
627 close(ads->udpsocket);
628 if (ads->tcpsocket >= 0) close(ads->tcpsocket);
629 adns__vbuf_free(&ads->tcpsend);
630 adns__vbuf_free(&ads->tcprecv);
914a5ff5 631 freesearchlist(ads);
9ec44266 632 free(ads);
656b2da9 633}
8ce38e76 634
635void adns_forallqueries_begin(adns_state ads) {
28de6442 636 adns__consistency(ads,0,cc_entex);
8ce38e76 637 ads->forallnext=
f7f83b4a 638 ads->udpw.head ? ads->udpw.head :
639 ads->tcpw.head ? ads->tcpw.head :
8ce38e76 640 ads->childw.head ? ads->childw.head :
641 ads->output.head;
642}
643
644adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
645 adns_query qu, nqu;
646
28de6442 647 adns__consistency(ads,0,cc_entex);
8ce38e76 648 nqu= ads->forallnext;
649 for (;;) {
650 qu= nqu;
651 if (!qu) return 0;
4218fb9a 652 if (qu->next) {
653 nqu= qu->next;
f7f83b4a 654 } else if (qu == ads->udpw.tail) {
655 nqu=
656 ads->tcpw.head ? ads->tcpw.head :
657 ads->childw.head ? ads->childw.head :
658 ads->output.head;
659 } else if (qu == ads->tcpw.tail) {
660 nqu=
661 ads->childw.head ? ads->childw.head :
662 ads->output.head;
4218fb9a 663 } else if (qu == ads->childw.tail) {
664 nqu= ads->output.head;
665 } else {
666 nqu= 0;
667 }
8ce38e76 668 if (!qu->parent) break;
669 }
670 ads->forallnext= nqu;
671 if (context_r) *context_r= qu->ctx.ext;
672 return qu;
673}