chiark / gitweb /
96a2a213cde3d4cb699af092d01909b109cd139e
[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   const char *word;
154   char tbuf[200], *slash, *ep;
155   struct in_addr base, mask;
156   int l;
157   unsigned long initial, baselocal;
158
159   if (!buf) return;
160   
161   ads->nsortlist= 0;
162   while (nextword(&buf,&word,&l)) {
163     if (ads->nsortlist >= MAXSORTLIST) {
164       adns__diag(ads,-1,0,"too many sortlist entries,"
165                  " ignoring %.*s onwards",l,word);
166       return;
167     }
168
169     if (l >= sizeof(tbuf)) {
170       configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
171       continue;
172     }
173     
174     memcpy(tbuf,word,l); tbuf[l]= 0;
175     slash= strchr(tbuf,'/');
176     if (slash) *slash++= 0;
177     
178     if (!inet_aton(tbuf,&base)) {
179       configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
180       continue;
181     }
182
183     if (slash) {
184       if (strchr(slash,'.')) {
185         if (!inet_aton(slash,&mask)) {
186           configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
187           continue;
188         }
189         if (base.s_addr & ~mask.s_addr) {
190           configparseerr(ads,fn,lno, "mask `%s' in sortlist"
191                          " overlaps address `%s'",slash,tbuf);
192           continue;
193         }
194       } else {
195         initial= strtoul(slash,&ep,10);
196         if (*ep || initial>32) {
197           configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
198           continue;
199         }
200         mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
201       }
202     } else {
203       baselocal= ntohl(base.s_addr);
204       if (!(baselocal & 0x080000000UL)) /* class A */
205         mask.s_addr= htonl(0x0ff000000UL);
206       else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
207         mask.s_addr= htonl(0x0ffff0000UL); /* class B */
208       else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
209         mask.s_addr= htonl(0x0ff000000UL); /* class C */
210       else {
211         configparseerr(ads,fn,lno, "network address `%s'"
212                        " in sortlist is not in classed ranges,"
213                        " must specify mask explicitly", tbuf);
214         continue;
215       }
216     }
217
218     ads->sortlist[ads->nsortlist].base= base;
219     ads->sortlist[ads->nsortlist].mask= mask;
220     ads->nsortlist++;
221   }
222 }
223
224 static void ccf_options(adns_state ads, const char *fn,
225                         int lno, const char *buf) {
226   const char *word;
227   char *ep;
228   unsigned long v;
229   int l;
230
231   if (!buf) return;
232
233   while (nextword(&buf,&word,&l)) {
234     if (l==5 && !memcmp(word,"debug",5)) {
235       ads->iflags |= adns_if_debug;
236       continue;
237     }
238     if (l>=6 && !memcmp(word,"ndots:",6)) {
239       v= strtoul(word+6,&ep,10);
240       if (l==6 || ep != word+l || v > INT_MAX) {
241         configparseerr(ads,fn,lno,"option `%.*s' malformed"
242                        " or has bad value",l,word);
243         continue;
244       }
245       ads->searchndots= v;
246       continue;
247     }
248     if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
249       if (!strcmp(word+12,"none")) {
250         ads->iflags &= ~adns_if_checkc_freq;
251         ads->iflags |= adns_if_checkc_entex;
252       } else if (!strcmp(word+12,"entex")) {
253         ads->iflags &= ~adns_if_checkc_freq;
254         ads->iflags |= adns_if_checkc_entex;
255       } else if (!strcmp(word+12,"freq")) {
256         ads->iflags |= adns_if_checkc_freq;
257       } else {
258         configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
259                        "(must be none, entex or freq", word+12);
260       }
261       continue;
262     }
263     adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
264   }
265 }
266
267 static void ccf_clearnss(adns_state ads, const char *fn,
268                          int lno, const char *buf) {
269   ads->nservers= 0;
270 }
271
272 static void ccf_include(adns_state ads, const char *fn,
273                         int lno, const char *buf) {
274   if (!*buf) {
275     configparseerr(ads,fn,lno,"`include' directive with no filename");
276     return;
277   }
278   readconfig(ads,buf,1);
279 }
280
281 static void ccf_lookup(adns_state ads, const char *fn, int lno,
282                        const char *buf) {
283   int found_bind=0;
284   const char *word;
285   int l;
286
287   if (!*buf) {
288     configparseerr(ads,fn,lno,"`lookup' directive with no databases");
289     return;
290   }
291
292   while (nextword(&buf,&word,&l)) {
293     if (l==4 && !memcmp(word,"bind",4)) {
294       found_bind=1;
295     } else if (l==4 && !memcmp(word,"file",4)) {
296       /* ignore this and hope /etc/hosts is not essential */
297     } else if (l==2 && !memcmp(word,"yp",2)) {
298       adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
299       found_bind=-1;
300     } else {
301       adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
302                  fn,lno, l,word);
303       found_bind=-1;
304     }
305   }
306   if (!found_bind)
307     adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
308 }
309
310 static const struct configcommandinfo {
311   const char *name;
312   void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
313 } configcommandinfos[]= {
314   { "nameserver",        ccf_nameserver  },
315   { "domain",            ccf_search      },
316   { "search",            ccf_search      },
317   { "sortlist",          ccf_sortlist    },
318   { "options",           ccf_options     },
319   { "clearnameservers",  ccf_clearnss    },
320   { "include",           ccf_include     },
321   { "lookup",            ccf_lookup      }, /* OpenBSD */
322   {  0                                   }
323 };
324
325 typedef union {
326   FILE *file;
327   const char *text;
328 } getline_ctx;
329
330 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
331                    int lno, char *buf, int buflen) {
332   FILE *file= src_io->file;
333   int c, i;
334   char *p;
335
336   p= buf;
337   buflen--;
338   i= 0;
339     
340   for (;;) { /* loop over chars */
341     if (i == buflen) {
342       adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
343       goto x_badline;
344     }
345     c= getc(file);
346     if (!c) {
347       adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
348       goto x_badline;
349     } else if (c == '\n') {
350       break;
351     } else if (c == EOF) {
352       if (ferror(file)) {
353         saveerr(ads,errno);
354         adns__diag(ads,-1,0,"%s:%d: read error: %s",
355                    filename,lno,strerror(errno));
356         return -1;
357       }
358       if (!i) return -1;
359       break;
360     } else {
361       *p++= c;
362       i++;
363     }
364   }
365
366   *p++= 0;
367   return i;
368
369  x_badline:
370   saveerr(ads,EINVAL);
371   while ((c= getc(file)) != EOF && c != '\n');
372   return -2;
373 }
374
375 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
376                    int lno, char *buf, int buflen) {
377   const char *cp= src_io->text;
378   int l;
379
380   if (!cp || !*cp) return -1;
381
382   if (*cp == ';' || *cp == '\n') cp++;
383   l= strcspn(cp,";\n");
384   src_io->text = cp+l;
385
386   if (l >= buflen) {
387     adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
388     saveerr(ads,EINVAL);
389     return -2;
390   }
391     
392   memcpy(buf,cp,l);
393   buf[l]= 0;
394   return l;
395 }
396
397 static void readconfiggeneric(adns_state ads, const char *filename,
398                               int (*getline)(adns_state ads, getline_ctx*,
399                                              const char *filename, int lno,
400                                              char *buf, int buflen),
401                               /* Returns >=0 for success, -1 for EOF or error
402                                * (error will have been reported), or -2 for
403                                * bad line was encountered, try again.
404                                */
405                               getline_ctx gl_ctx) {
406   char linebuf[2000], *p, *q;
407   int lno, l, dirl;
408   const struct configcommandinfo *ccip;
409
410   for (lno=1;
411        (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
412        lno++) {
413     if (l == -2) continue;
414     while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
415     linebuf[l]= 0;
416     p= linebuf;
417     while (ctype_whitespace(*p)) p++;
418     if (*p == '#' || *p == ';' || !*p) continue;
419     q= p;
420     while (*q && !ctype_whitespace(*q)) q++;
421     dirl= q-p;
422     for (ccip=configcommandinfos;
423          ccip->name &&
424            !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
425          ccip++);
426     if (!ccip->name) {
427       adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
428                  filename,lno,(int)(q-p),p);
429       continue;
430     }
431     while (ctype_whitespace(*q)) q++;
432     ccip->fn(ads,filename,lno,q);
433   }
434 }
435
436 static const char *instrum_getenv(adns_state ads, const char *envvar) {
437   const char *value;
438
439   value= getenv(envvar);
440   if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
441   else adns__debug(ads,-1,0,"environment variable %s"
442                    " set to `%s'",envvar,value);
443   return value;
444 }
445
446 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
447   getline_ctx gl_ctx;
448   
449   gl_ctx.file= fopen(filename,"r");
450   if (!gl_ctx.file) {
451     if (errno == ENOENT) {
452       if (warnmissing)
453         adns__debug(ads,-1,0, "configuration file"
454                     " `%s' does not exist",filename);
455       return;
456     }
457     saveerr(ads,errno);
458     adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
459                filename,strerror(errno));
460     return;
461   }
462
463   readconfiggeneric(ads,filename,gl_file,gl_ctx);
464   
465   fclose(gl_ctx.file);
466 }
467
468 static void readconfigtext(adns_state ads, const char *text,
469                            const char *showname) {
470   getline_ctx gl_ctx;
471   
472   gl_ctx.text= text;
473   readconfiggeneric(ads,showname,gl_text,gl_ctx);
474 }
475   
476 static void readconfigenv(adns_state ads, const char *envvar) {
477   const char *filename;
478
479   if (ads->iflags & adns_if_noenv) {
480     adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
481     return;
482   }
483   filename= instrum_getenv(ads,envvar);
484   if (filename) readconfig(ads,filename,1);
485 }
486
487 static void readconfigenvtext(adns_state ads, const char *envvar) {
488   const char *textdata;
489
490   if (ads->iflags & adns_if_noenv) {
491     adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
492     return;
493   }
494   textdata= instrum_getenv(ads,envvar);
495   if (textdata) readconfigtext(ads,textdata,envvar);
496 }
497
498
499 int adns__setnonblock(adns_state ads, int fd) {
500   int r;
501   
502   r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
503   r |= O_NONBLOCK;
504   r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
505   return 0;
506 }
507
508 static int init_begin(adns_state *ads_r, adns_initflags flags,
509                       adns_logcallbackfn *logfn, void *logfndata) {
510   adns_state ads;
511   pid_t pid;
512   
513   ads= malloc(sizeof(*ads)); if (!ads) return errno;
514
515   ads->iflags= flags;
516   ads->logfn= logfn;
517   ads->logfndata= logfndata;
518   ads->configerrno= 0;
519   LIST_INIT(ads->udpw);
520   LIST_INIT(ads->tcpw);
521   LIST_INIT(ads->childw);
522   LIST_INIT(ads->output);
523   ads->forallnext= 0;
524   ads->nextid= 0x311f;
525   ads->udpsocket= ads->tcpsocket= -1;
526   adns__vbuf_init(&ads->tcpsend);
527   adns__vbuf_init(&ads->tcprecv);
528   ads->tcprecv_skip= 0;
529   ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
530   ads->searchndots= 1;
531   ads->tcpstate= server_disconnected;
532   timerclear(&ads->tcptimeout);
533   ads->searchlist= 0;
534
535   pid= getpid();
536   ads->rand48xsubi[0]= pid;
537   ads->rand48xsubi[1]= (unsigned long)pid >> 16;
538   ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
539
540   *ads_r= ads;
541   return 0;
542 }
543
544 static int init_finish(adns_state ads) {
545   struct in_addr ia;
546   struct protoent *proto;
547   int r;
548   
549   if (!ads->nservers) {
550     if (ads->logfn && ads->iflags & adns_if_debug)
551       adns__lprintf(ads,"adns: no nameservers, using localhost\n");
552     ia.s_addr= htonl(INADDR_LOOPBACK);
553     addserver(ads,ia);
554   }
555
556   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
557   ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
558   if (ads->udpsocket<0) { r= errno; goto x_free; }
559
560   r= adns__setnonblock(ads,ads->udpsocket);
561   if (r) { r= errno; goto x_closeudp; }
562   
563   return 0;
564
565  x_closeudp:
566   close(ads->udpsocket);
567  x_free:
568   free(ads);
569   return r;
570 }
571
572 static void init_abort(adns_state ads) {
573   if (ads->nsearchlist) {
574     free(ads->searchlist[0]);
575     free(ads->searchlist);
576   }
577   free(ads);
578 }
579
580 static void logfn_file(adns_state ads, void *logfndata,
581                        const char *fmt, va_list al) {
582   vfprintf(logfndata,fmt,al);
583 }
584
585 static int init_files(adns_state *ads_r, adns_initflags flags,
586                       adns_logcallbackfn *logfn, void *logfndata) {
587   adns_state ads;
588   const char *res_options, *adns_res_options;
589   int r;
590   
591   r= init_begin(&ads, flags, logfn, logfndata);
592   if (r) return r;
593   
594   res_options= instrum_getenv(ads,"RES_OPTIONS");
595   adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
596   ccf_options(ads,"RES_OPTIONS",-1,res_options);
597   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
598
599   readconfig(ads,"/etc/resolv.conf",1);
600   readconfig(ads,"/etc/resolv-adns.conf",0);
601   readconfigenv(ads,"RES_CONF");
602   readconfigenv(ads,"ADNS_RES_CONF");
603
604   readconfigenvtext(ads,"RES_CONF_TEXT");
605   readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
606
607   ccf_options(ads,"RES_OPTIONS",-1,res_options);
608   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
609
610   ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
611   ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
612
613   if (ads->configerrno && ads->configerrno != EINVAL) {
614     r= ads->configerrno;
615     init_abort(ads);
616     return r;
617   }
618
619   r= init_finish(ads);
620   if (r) return r;
621
622   adns__consistency(ads,0,cc_entex);
623   *ads_r= ads;
624   return 0;
625 }
626
627 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
628   return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
629 }
630
631 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
632                        adns_logcallbackfn *logfn, void *logfndata,
633                        const char *configtext) {
634   adns_state ads;
635   int r;
636
637   r= init_begin(&ads, flags, logfn, logfndata);
638   if (r) return r;
639
640   readconfigtext(ads,configtext,"<supplied configuration text>");
641   if (ads->configerrno) {
642     r= ads->configerrno;
643     init_abort(ads);
644     return r;
645   }
646
647   r= init_finish(ads);  if (r) return r;
648   adns__consistency(ads,0,cc_entex);
649   *ads_r= ads;
650   return 0;
651 }
652
653 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
654                      FILE *diagfile, const char *configtext) {
655   return init_strcfg(ads_r, flags,
656                      diagfile ? logfn_file : 0, diagfile,
657                      configtext);
658 }
659
660 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
661                     const char *configtext /*0=>use default config files*/,
662                     adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
663                     void *logfndata /*0 with logfn==0 => discard*/) {
664   if (!logfn && logfndata)
665     logfn= logfn_file;
666   if (configtext)
667     return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
668   else
669     return init_files(newstate_r, flags, logfn, logfndata);
670 }
671
672 void adns_finish(adns_state ads) {
673   adns__consistency(ads,0,cc_entex);
674   for (;;) {
675     if (ads->udpw.head) adns_cancel(ads->udpw.head);
676     else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
677     else if (ads->childw.head) adns_cancel(ads->childw.head);
678     else if (ads->output.head) adns_cancel(ads->output.head);
679     else break;
680   }
681   close(ads->udpsocket);
682   if (ads->tcpsocket >= 0) close(ads->tcpsocket);
683   adns__vbuf_free(&ads->tcpsend);
684   adns__vbuf_free(&ads->tcprecv);
685   freesearchlist(ads);
686   free(ads);
687 }
688
689 void adns_forallqueries_begin(adns_state ads) {
690   adns__consistency(ads,0,cc_entex);
691   ads->forallnext=
692     ads->udpw.head ? ads->udpw.head :
693     ads->tcpw.head ? ads->tcpw.head :
694     ads->childw.head ? ads->childw.head :
695     ads->output.head;
696 }
697   
698 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
699   adns_query qu, nqu;
700
701   adns__consistency(ads,0,cc_entex);
702   nqu= ads->forallnext;
703   for (;;) {
704     qu= nqu;
705     if (!qu) return 0;
706     if (qu->next) {
707       nqu= qu->next;
708     } else if (qu == ads->udpw.tail) {
709       nqu=
710         ads->tcpw.head ? ads->tcpw.head :
711         ads->childw.head ? ads->childw.head :
712         ads->output.head;
713     } else if (qu == ads->tcpw.tail) {
714       nqu=
715         ads->childw.head ? ads->childw.head :
716         ads->output.head;
717     } else if (qu == ads->childw.tail) {
718       nqu= ads->output.head;
719     } else {
720       nqu= 0;
721     }
722     if (!qu->parent) break;
723   }
724   ads->forallnext= nqu;
725   if (context_r) *context_r= qu->ctx.ext;
726   return qu;
727 }