chiark / gitweb /
03d737edd002ccca90932e65dec1ae8978b8c773
[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, const struct sockaddr *sa, int n) {
45   int i;
46   adns_rr_addr *ss;
47   const struct sockaddr_in *sin;
48
49   assert(sa->sa_family==AF_INET); /* for inet_ntoa */
50   sin= (const void *)sa;
51   
52   for (i=0; i<ads->nservers; i++) {
53     if (adns__sockaddr_equal_p(sa, &ads->servers[i].addr.sa)) {
54       adns__debug(ads,-1,0,"duplicate nameserver %s ignored",
55                   inet_ntoa(sin->sin_addr));
56       return;
57     }
58   }
59   
60   if (ads->nservers>=MAXSERVERS) {
61     adns__diag(ads,-1,0,"too many nameservers, ignoring %s",
62                inet_ntoa(sin->sin_addr));
63     return;
64   }
65
66   ss= ads->servers+ads->nservers;
67   assert(n <= sizeof(ss->addr));
68   ss->len = n;
69   memcpy(&ss->addr, sa, n);
70   ads->nservers++;
71 }
72
73 static void freesearchlist(adns_state ads) {
74   if (ads->nsearchlist) free(*ads->searchlist);
75   free(ads->searchlist);
76 }
77
78 static void saveerr(adns_state ads, int en) {
79   if (!ads->configerrno) ads->configerrno= en;
80 }
81
82 static void configparseerr(adns_state ads, const char *fn, int lno,
83                            const char *fmt, ...) {
84   va_list al;
85
86   saveerr(ads,EINVAL);
87   if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
88
89   if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
90   else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
91   va_start(al,fmt);
92   adns__vlprintf(ads,fmt,al);
93   va_end(al);
94   adns__lprintf(ads,"\n");
95 }
96
97 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
98   const char *p, *q;
99
100   p= *bufp_io;
101   while (ctype_whitespace(*p)) p++;
102   if (!*p) return 0;
103
104   q= p;
105   while (*q && !ctype_whitespace(*q)) q++;
106
107   *l_r= q-p;
108   *word_r= p;
109   *bufp_io= q;
110
111   return 1;
112 }
113
114 static void ccf_nameserver(adns_state ads, const char *fn,
115                            int lno, const char *buf) {
116   struct sockaddr_in sin;
117
118   memset(&sin,0,sizeof(sin));
119   sin.sin_family= AF_INET;
120   sin.sin_port= htons(DNS_PORT);
121   if (!inet_aton(buf,&sin.sin_addr)) {
122     configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
123     return;
124   }
125   adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(sin.sin_addr));
126   addserver(ads,(const struct sockaddr *)&sin,sizeof(sin));
127 }
128
129 static void ccf_search(adns_state ads, const char *fn,
130                        int lno, const char *buf) {
131   const char *bufp, *word;
132   char *newchars, **newptrs, **pp;
133   int count, tl, l;
134
135   if (!buf) return;
136
137   bufp= buf;
138   count= 0;
139   tl= 0;
140   while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
141
142   newptrs= malloc(sizeof(char*)*count);
143   if (!newptrs) { saveerr(ads,errno); return; }
144
145   newchars= malloc(tl);
146   if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
147
148   bufp= buf;
149   pp= newptrs;
150   while (nextword(&bufp,&word,&l)) {
151     *pp++= newchars;
152     memcpy(newchars,word,l);
153     newchars += l;
154     *newchars++ = 0;
155   }
156
157   freesearchlist(ads);
158   ads->nsearchlist= count;
159   ads->searchlist= newptrs;
160 }
161
162 static void ccf_sortlist(adns_state ads, const char *fn,
163                          int lno, const char *buf) {
164   const char *word;
165   char tbuf[200], *slash, *ep;
166   const char *maskwhat;
167   struct sortlist *sl;
168   int l;
169   int af= AF_UNSPEC;
170   int initial= -1;
171
172   if (!buf) return;
173   
174   ads->nsortlist= 0;
175   while (nextword(&buf,&word,&l)) {
176     if (ads->nsortlist >= MAXSORTLIST) {
177       adns__diag(ads,-1,0,"too many sortlist entries,"
178                  " ignoring %.*s onwards",l,word);
179       return;
180     }
181
182     if (l >= sizeof(tbuf)) {
183       configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
184       continue;
185     }
186     
187     memcpy(tbuf,word,l); tbuf[l]= 0;
188     slash= strchr(tbuf,'/');
189     if (slash) *slash++= 0;
190
191     sl= &ads->sortlist[ads->nsortlist];
192     if (!inet_aton(tbuf, &sl->base.v4)) {
193       configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
194       continue;
195     }
196     af= AF_INET;
197
198     if (slash) {
199       if (slash[strspn(slash, "0123456789")]) {
200         maskwhat = "mask";
201         if (!inet_aton(slash, &sl->mask.v4)) {
202           configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
203           continue;
204         }
205       } else {
206         maskwhat = "prefix length";
207         initial= strtoul(slash,&ep,10);
208         if (*ep || initial>adns__addr_width(af)) {
209           configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
210           continue;
211         }
212         adns__prefix_mask(af, initial, &sl->mask);
213       }
214     } else {
215       maskwhat = "implied prefix length";
216       initial= adns__guess_prefix_length(af, &sl->base);
217       if (initial < 0) {
218         configparseerr(ads,fn,lno, "network address `%s'"
219                        " in sortlist is not in classed ranges,"
220                        " must specify mask explicitly", tbuf);
221         continue;
222       }
223       adns__prefix_mask(af, initial, &sl->mask);
224     }
225
226     if (!adns__addr_match_p(af,&sl->base, af,&sl->base,&sl->mask)) {
227       if (initial >= 0) {
228         configparseerr(ads,fn,lno, "%s %d in sortlist"
229                        " overlaps address `%s'",maskwhat,initial,tbuf);
230       } else {
231         configparseerr(ads,fn,lno, "%s `%s' in sortlist"
232                        " overlaps address `%s'",maskwhat,slash,tbuf);
233       }
234       continue;
235     }
236
237     sl->af= af;
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,"r");
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 sockaddr_in sin;
564   struct protoent *proto;
565   int i, r;
566   
567   if (!ads->nservers) {
568     if (ads->logfn && ads->iflags & adns_if_debug)
569       adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
570     memset(&sin, 0, sizeof(sin));
571     sin.sin_family = AF_INET;
572     sin.sin_port = htons(DNS_PORT);
573     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
574     addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
575   }
576
577   /* we can't cope with multiple transport address families yet */
578   for (i=0; i<ads->nservers; i++)
579     assert(ads->servers[i].addr.sa.sa_family==AF_INET);
580
581   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
582   ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
583   if (ads->udpsocket<0) { r= errno; goto x_free; }
584
585   r= adns__setnonblock(ads,ads->udpsocket);
586   if (r) { r= errno; goto x_closeudp; }
587   
588   return 0;
589
590  x_closeudp:
591   close(ads->udpsocket);
592  x_free:
593   free(ads);
594   return r;
595 }
596
597 static void init_abort(adns_state ads) {
598   if (ads->nsearchlist) {
599     free(ads->searchlist[0]);
600     free(ads->searchlist);
601   }
602   free(ads);
603 }
604
605 static void logfn_file(adns_state ads, void *logfndata,
606                        const char *fmt, va_list al) {
607   vfprintf(logfndata,fmt,al);
608 }
609
610 static int init_files(adns_state *ads_r, adns_initflags flags,
611                       adns_logcallbackfn *logfn, void *logfndata) {
612   adns_state ads;
613   const char *res_options, *adns_res_options;
614   int r;
615   
616   r= init_begin(&ads, flags, logfn, logfndata);
617   if (r) return r;
618   
619   res_options= instrum_getenv(ads,"RES_OPTIONS");
620   adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
621   ccf_options(ads,"RES_OPTIONS",-1,res_options);
622   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
623
624   readconfig(ads,"/etc/resolv.conf",1);
625   readconfig(ads,"/etc/resolv-adns.conf",0);
626   readconfigenv(ads,"RES_CONF");
627   readconfigenv(ads,"ADNS_RES_CONF");
628
629   readconfigenvtext(ads,"RES_CONF_TEXT");
630   readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
631
632   ccf_options(ads,"RES_OPTIONS",-1,res_options);
633   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
634
635   ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
636   ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
637
638   if (ads->configerrno && ads->configerrno != EINVAL) {
639     r= ads->configerrno;
640     init_abort(ads);
641     return r;
642   }
643
644   r= init_finish(ads);
645   if (r) return r;
646
647   adns__consistency(ads,0,cc_entex);
648   *ads_r= ads;
649   return 0;
650 }
651
652 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
653   return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
654 }
655
656 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
657                        adns_logcallbackfn *logfn, void *logfndata,
658                        const char *configtext) {
659   adns_state ads;
660   int r;
661
662   r= init_begin(&ads, flags, logfn, logfndata);
663   if (r) return r;
664
665   readconfigtext(ads,configtext,"<supplied configuration text>");
666   if (ads->configerrno) {
667     r= ads->configerrno;
668     init_abort(ads);
669     return r;
670   }
671
672   r= init_finish(ads);  if (r) return r;
673   adns__consistency(ads,0,cc_entex);
674   *ads_r= ads;
675   return 0;
676 }
677
678 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
679                      FILE *diagfile, const char *configtext) {
680   return init_strcfg(ads_r, flags,
681                      diagfile ? logfn_file : 0, diagfile,
682                      configtext);
683 }
684
685 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
686                     const char *configtext /*0=>use default config files*/,
687                     adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
688                     void *logfndata /*0 with logfn==0 => discard*/) {
689   if (!logfn && logfndata)
690     logfn= logfn_file;
691   if (configtext)
692     return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
693   else
694     return init_files(newstate_r, flags, logfn, logfndata);
695 }
696
697 void adns_finish(adns_state ads) {
698   adns__consistency(ads,0,cc_entex);
699   for (;;) {
700     if (ads->udpw.head) adns_cancel(ads->udpw.head);
701     else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
702     else if (ads->childw.head) adns_cancel(ads->childw.head);
703     else if (ads->output.head) adns_cancel(ads->output.head);
704     else break;
705   }
706   close(ads->udpsocket);
707   if (ads->tcpsocket >= 0) close(ads->tcpsocket);
708   adns__vbuf_free(&ads->tcpsend);
709   adns__vbuf_free(&ads->tcprecv);
710   freesearchlist(ads);
711   free(ads);
712 }
713
714 void adns_forallqueries_begin(adns_state ads) {
715   adns__consistency(ads,0,cc_entex);
716   ads->forallnext=
717     ads->udpw.head ? ads->udpw.head :
718     ads->tcpw.head ? ads->tcpw.head :
719     ads->childw.head ? ads->childw.head :
720     ads->output.head;
721 }
722   
723 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
724   adns_query qu, nqu;
725
726   adns__consistency(ads,0,cc_entex);
727   nqu= ads->forallnext;
728   for (;;) {
729     qu= nqu;
730     if (!qu) return 0;
731     if (qu->next) {
732       nqu= qu->next;
733     } else if (qu == ads->udpw.tail) {
734       nqu=
735         ads->tcpw.head ? ads->tcpw.head :
736         ads->childw.head ? ads->childw.head :
737         ads->output.head;
738     } else if (qu == ads->tcpw.tail) {
739       nqu=
740         ads->childw.head ? ads->childw.head :
741         ads->output.head;
742     } else if (qu == ads->childw.tail) {
743       nqu= ads->output.head;
744     } else {
745       nqu= 0;
746     }
747     if (!qu->parent) break;
748   }
749   ads->forallnext= nqu;
750   if (context_r) *context_r= qu->ctx.ext;
751   return qu;
752 }