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