chiark / gitweb /
21db53cd1db95d80c2b968ab9dedb7ada4c00a7d
[adns] / 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 const afinfo *const transport_aftab[] = {
45   &adns__inet_afinfo,
46   &adns__inet6_afinfo,
47   0
48 };
49
50 static const afinfo *find_afinfo(int af)
51 {
52   int i;
53
54   for (i = 0; transport_aftab[i]; i++)
55     if (transport_aftab[i]->af == af) return transport_aftab[i];
56   return 0;
57 }
58
59 static void addserver(adns_state ads, struct sockaddr *sa, int n) {
60   int i;
61   adns_rr_addr *ss;
62   const afinfo *ai;
63
64   ai = find_afinfo(sa->sa_family);
65   if (!ai) {
66     adns__diag(ads,-1,0,
67                "nameserver %s for unknown address family %d ignored",
68                adns__sockaddr_ntoa(sa, n), sa->sa_family);
69   }
70   
71   for (i=0; i<ads->nservers; i++) {
72     if (ads->servers[i].addr.sa.sa_family == sa->sa_family &&
73         ai->sockaddr_equalp(sa, &ads->servers[i].addr.sa)) {
74       adns__debug(ads,-1,0,"duplicate nameserver %s ignored",
75                   adns__sockaddr_ntoa(sa, n));
76       return;
77     }
78   }
79   
80   if (ads->nservers>=MAXSERVERS) {
81     adns__diag(ads,-1,0,"too many nameservers, ignoring %s",
82                adns__sockaddr_ntoa(sa, n));
83     return;
84   }
85
86   ss= ads->servers+ads->nservers;
87   assert(n <= sizeof(ss->addr));
88   ss->len = n;
89   memcpy(&ss->addr, sa, n);
90   ads->nservers++;
91 }
92
93 static void freesearchlist(adns_state ads) {
94   if (ads->nsearchlist) free(*ads->searchlist);
95   free(ads->searchlist);
96 }
97
98 static void saveerr(adns_state ads, int en) {
99   if (!ads->configerrno) ads->configerrno= en;
100 }
101
102 static void configparseerr(adns_state ads, const char *fn, int lno,
103                            const char *fmt, ...) {
104   va_list al;
105
106   saveerr(ads,EINVAL);
107   if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return;
108
109   if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn);
110   else adns__lprintf(ads,"adns: %s:%d: ",fn,lno);
111   va_start(al,fmt);
112   adns__vlprintf(ads,fmt,al);
113   va_end(al);
114   adns__lprintf(ads,"\n");
115 }
116
117 static int nextword(const char **bufp_io, const char **word_r, int *l_r) {
118   const char *p, *q;
119
120   p= *bufp_io;
121   while (ctype_whitespace(*p)) p++;
122   if (!*p) return 0;
123
124   q= p;
125   while (*q && !ctype_whitespace(*q)) q++;
126
127   *l_r= q-p;
128   *word_r= p;
129   *bufp_io= q;
130
131   return 1;
132 }
133
134 static void ccf_nameserver(adns_state ads, const char *fn,
135                            int lno, const char *buf) {
136   struct addrinfo *ai, ai_hint = { 0 };
137   int err;
138
139   ai_hint.ai_family = AF_UNSPEC;
140   ai_hint.ai_socktype = SOCK_DGRAM;
141   ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
142
143   err = getaddrinfo(buf, STRINGIFY(DNS_PORT), &ai_hint, &ai);
144   if (err) {
145     configparseerr(ads,fn,lno,"invalid nameserver address `%s' (%s)",
146                    buf, gai_strerror(err));
147     return;
148   }
149
150   adns__debug(ads,-1,0,"using nameserver %s",
151               adns__sockaddr_ntoa(ai->ai_addr, ai->ai_addrlen));
152   addserver(ads, ai->ai_addr, ai->ai_addrlen);
153   freeaddrinfo(ai);
154 }
155
156 static void ccf_search(adns_state ads, const char *fn,
157                        int lno, const char *buf) {
158   const char *bufp, *word;
159   char *newchars, **newptrs, **pp;
160   int count, tl, l;
161
162   if (!buf) return;
163
164   bufp= buf;
165   count= 0;
166   tl= 0;
167   while (nextword(&bufp,&word,&l)) { count++; tl += l+1; }
168
169   newptrs= malloc(sizeof(char*)*count);
170   if (!newptrs) { saveerr(ads,errno); return; }
171
172   newchars= malloc(tl);
173   if (!newchars) { saveerr(ads,errno); free(newptrs); return; }
174
175   bufp= buf;
176   pp= newptrs;
177   while (nextword(&bufp,&word,&l)) {
178     *pp++= newchars;
179     memcpy(newchars,word,l);
180     newchars += l;
181     *newchars++ = 0;
182   }
183
184   freesearchlist(ads);
185   ads->nsearchlist= count;
186   ads->searchlist= newptrs;
187 }
188
189 static void ccf_sortlist(adns_state ads, const char *fn,
190                          int lno, const char *buf) {
191   const char *word;
192   char tbuf[200], *slash, *ep;
193   const char *maskwhat;
194   struct sortlist *sl;
195   int l;
196   const afinfo *ai;
197   int initial = -1;
198
199   if (!buf) return;
200   
201   ads->nsortlist= 0;
202   while (nextword(&buf,&word,&l)) {
203     if (ads->nsortlist >= MAXSORTLIST) {
204       adns__diag(ads,-1,0,"too many sortlist entries,"
205                  " ignoring %.*s onwards",l,word);
206       return;
207     }
208
209     if (l >= sizeof(tbuf)) {
210       configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word);
211       continue;
212     }
213     
214     memcpy(tbuf,word,l); tbuf[l]= 0;
215     slash= strchr(tbuf,'/');
216     if (slash) *slash++= 0;
217
218     sl= &ads->sortlist[ads->nsortlist];
219
220     if (strchr(tbuf, ':'))
221       ai= &adns__inet6_afinfo;
222     else
223       ai= &adns__inet_afinfo;
224     
225     if (!inet_pton(ai->af, tbuf, &sl->base)) {
226       configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
227       continue;
228     }
229
230     if (slash) {
231       if (strchr(slash,ai->delim)) {
232         maskwhat = "mask";
233         if (!inet_pton(ai->af,slash,&sl->mask)) {
234           configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
235           continue;
236         }
237       } else {
238         maskwhat = "prefix length";
239         initial= strtoul(slash,&ep,10);
240         if (*ep || initial>ai->width) {
241           configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
242           continue;
243         }
244         ai->prefix_mask(initial, &sl->mask);
245       }
246     } else {
247       maskwhat = "implied prefix length";
248       initial = ai->guess_len(&sl->base);
249       if (initial < 0) {
250         configparseerr(ads,fn,lno, "network address `%s'"
251                        " in sortlist is not in classed ranges,"
252                        " must specify mask explicitly", tbuf);
253         continue;
254       }
255       ai->prefix_mask(initial, &sl->mask);
256     }
257
258     if (!ai->matchp(&sl->base, &sl->base, &sl->mask)) {
259       if (initial >= 0) {
260         configparseerr(ads,fn,lno, "%s %d in sortlist"
261                        " overlaps address `%s'",maskwhat,initial,tbuf);
262       } else {
263         configparseerr(ads,fn,lno, "%s `%s' in sortlist"
264                        " overlaps address `%s'",maskwhat,slash,tbuf);
265       }
266       continue;
267     }
268
269     sl->ai = ai;
270     ads->nsortlist++;
271   }
272 }
273
274 static void ccf_options(adns_state ads, const char *fn,
275                         int lno, const char *buf) {
276   const char *word;
277   char *ep;
278   unsigned long v;
279   int l;
280
281   if (!buf) return;
282
283   while (nextword(&buf,&word,&l)) {
284     if (l==5 && !memcmp(word,"debug",5)) {
285       ads->iflags |= adns_if_debug;
286       continue;
287     }
288     if (l>=6 && !memcmp(word,"ndots:",6)) {
289       v= strtoul(word+6,&ep,10);
290       if (l==6 || ep != word+l || v > INT_MAX) {
291         configparseerr(ads,fn,lno,"option `%.*s' malformed"
292                        " or has bad value",l,word);
293         continue;
294       }
295       ads->searchndots= v;
296       continue;
297     }
298     if (l>=12 && !memcmp(word,"adns_checkc:",12)) {
299       if (!strcmp(word+12,"none")) {
300         ads->iflags &= ~adns_if_checkc_freq;
301         ads->iflags |= adns_if_checkc_entex;
302       } else if (!strcmp(word+12,"entex")) {
303         ads->iflags &= ~adns_if_checkc_freq;
304         ads->iflags |= adns_if_checkc_entex;
305       } else if (!strcmp(word+12,"freq")) {
306         ads->iflags |= adns_if_checkc_freq;
307       } else {
308         configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' "
309                        "(must be none, entex or freq", word+12);
310       }
311       continue;
312     }
313     if (l>=8 && !memcmp(word,"adns_af:",8)) {
314       if (!strcmp(word+8,"v4only"))
315         ads->iflags = (ads->iflags & ~adns_if_afmask) | adns_if_af_v4only;
316       else if (!strcmp(word+8,"v6only"))
317         ads->iflags = (ads->iflags & ~adns_if_afmask) | adns_if_af_v6only;
318       else if (!strcmp(word+8,"any"))
319         ads->iflags = (ads->iflags & ~adns_if_afmask);
320       else {
321         configparseerr(ads,fn,lno, "option adns_af has bad value `%s' "
322                        "(must be any, v4only or v6only", word+8);
323       }
324       continue;
325     }
326     adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
327   }
328 }
329
330 static void ccf_clearnss(adns_state ads, const char *fn,
331                          int lno, const char *buf) {
332   ads->nservers= 0;
333 }
334
335 static void ccf_include(adns_state ads, const char *fn,
336                         int lno, const char *buf) {
337   if (!*buf) {
338     configparseerr(ads,fn,lno,"`include' directive with no filename");
339     return;
340   }
341   readconfig(ads,buf,1);
342 }
343
344 static void ccf_lookup(adns_state ads, const char *fn, int lno,
345                        const char *buf) {
346   int found_bind=0;
347   const char *word;
348   int l;
349
350   if (!*buf) {
351     configparseerr(ads,fn,lno,"`lookup' directive with no databases");
352     return;
353   }
354
355   while (nextword(&buf,&word,&l)) {
356     if (l==4 && !memcmp(word,"bind",4)) {
357       found_bind=1;
358     } else if (l==4 && !memcmp(word,"file",4)) {
359       /* ignore this and hope /etc/hosts is not essential */
360     } else if (l==2 && !memcmp(word,"yp",2)) {
361       adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
362       found_bind=-1;
363     } else {
364       adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
365                  fn,lno, l,word);
366       found_bind=-1;
367     }
368   }
369   if (!found_bind)
370     adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
371 }
372
373 static const struct configcommandinfo {
374   const char *name;
375   void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
376 } configcommandinfos[]= {
377   { "nameserver",        ccf_nameserver  },
378   { "domain",            ccf_search      },
379   { "search",            ccf_search      },
380   { "sortlist",          ccf_sortlist    },
381   { "options",           ccf_options     },
382   { "clearnameservers",  ccf_clearnss    },
383   { "include",           ccf_include     },
384   { "lookup",            ccf_lookup      }, /* OpenBSD */
385   {  0                                   }
386 };
387
388 typedef union {
389   FILE *file;
390   const char *text;
391 } getline_ctx;
392
393 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
394                    int lno, char *buf, int buflen) {
395   FILE *file= src_io->file;
396   int c, i;
397   char *p;
398
399   p= buf;
400   buflen--;
401   i= 0;
402     
403   for (;;) { /* loop over chars */
404     if (i == buflen) {
405       adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
406       goto x_badline;
407     }
408     c= getc(file);
409     if (!c) {
410       adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
411       goto x_badline;
412     } else if (c == '\n') {
413       break;
414     } else if (c == EOF) {
415       if (ferror(file)) {
416         saveerr(ads,errno);
417         adns__diag(ads,-1,0,"%s:%d: read error: %s",
418                    filename,lno,strerror(errno));
419         return -1;
420       }
421       if (!i) return -1;
422       break;
423     } else {
424       *p++= c;
425       i++;
426     }
427   }
428
429   *p++= 0;
430   return i;
431
432  x_badline:
433   saveerr(ads,EINVAL);
434   while ((c= getc(file)) != EOF && c != '\n');
435   return -2;
436 }
437
438 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
439                    int lno, char *buf, int buflen) {
440   const char *cp= src_io->text;
441   int l;
442
443   if (!cp || !*cp) return -1;
444
445   if (*cp == ';' || *cp == '\n') cp++;
446   l= strcspn(cp,";\n");
447   src_io->text = cp+l;
448
449   if (l >= buflen) {
450     adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
451     saveerr(ads,EINVAL);
452     return -2;
453   }
454     
455   memcpy(buf,cp,l);
456   buf[l]= 0;
457   return l;
458 }
459
460 static void readconfiggeneric(adns_state ads, const char *filename,
461                               int (*getline)(adns_state ads, getline_ctx*,
462                                              const char *filename, int lno,
463                                              char *buf, int buflen),
464                               /* Returns >=0 for success, -1 for EOF or error
465                                * (error will have been reported), or -2 for
466                                * bad line was encountered, try again.
467                                */
468                               getline_ctx gl_ctx) {
469   char linebuf[2000], *p, *q;
470   int lno, l, dirl;
471   const struct configcommandinfo *ccip;
472
473   for (lno=1;
474        (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
475        lno++) {
476     if (l == -2) continue;
477     while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
478     linebuf[l]= 0;
479     p= linebuf;
480     while (ctype_whitespace(*p)) p++;
481     if (*p == '#' || *p == ';' || !*p) continue;
482     q= p;
483     while (*q && !ctype_whitespace(*q)) q++;
484     dirl= q-p;
485     for (ccip=configcommandinfos;
486          ccip->name &&
487            !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
488          ccip++);
489     if (!ccip->name) {
490       adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
491                  filename,lno,(int)(q-p),p);
492       continue;
493     }
494     while (ctype_whitespace(*q)) q++;
495     ccip->fn(ads,filename,lno,q);
496   }
497 }
498
499 static const char *instrum_getenv(adns_state ads, const char *envvar) {
500   const char *value;
501
502   value= getenv(envvar);
503   if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
504   else adns__debug(ads,-1,0,"environment variable %s"
505                    " set to `%s'",envvar,value);
506   return value;
507 }
508
509 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
510   getline_ctx gl_ctx;
511   
512   gl_ctx.file= fopen(filename,"r");
513   if (!gl_ctx.file) {
514     if (errno == ENOENT) {
515       if (warnmissing)
516         adns__debug(ads,-1,0, "configuration file"
517                     " `%s' does not exist",filename);
518       return;
519     }
520     saveerr(ads,errno);
521     adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
522                filename,strerror(errno));
523     return;
524   }
525
526   readconfiggeneric(ads,filename,gl_file,gl_ctx);
527   
528   fclose(gl_ctx.file);
529 }
530
531 static void readconfigtext(adns_state ads, const char *text,
532                            const char *showname) {
533   getline_ctx gl_ctx;
534   
535   gl_ctx.text= text;
536   readconfiggeneric(ads,showname,gl_text,gl_ctx);
537 }
538   
539 static void readconfigenv(adns_state ads, const char *envvar) {
540   const char *filename;
541
542   if (ads->iflags & adns_if_noenv) {
543     adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
544     return;
545   }
546   filename= instrum_getenv(ads,envvar);
547   if (filename) readconfig(ads,filename,1);
548 }
549
550 static void readconfigenvtext(adns_state ads, const char *envvar) {
551   const char *textdata;
552
553   if (ads->iflags & adns_if_noenv) {
554     adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
555     return;
556   }
557   textdata= instrum_getenv(ads,envvar);
558   if (textdata) readconfigtext(ads,textdata,envvar);
559 }
560
561
562 int adns__setnonblock(adns_state ads, int fd) {
563   int r;
564   
565   r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
566   r |= O_NONBLOCK;
567   r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
568   return 0;
569 }
570
571 static int init_begin(adns_state *ads_r, adns_initflags flags,
572                       adns_logcallbackfn *logfn, void *logfndata) {
573   adns_state ads;
574   pid_t pid;
575   
576   ads= malloc(sizeof(*ads)); if (!ads) return errno;
577
578   ads->iflags= flags;
579   ads->logfn= logfn;
580   ads->logfndata= logfndata;
581   ads->configerrno= 0;
582   LIST_INIT(ads->udpw);
583   LIST_INIT(ads->tcpw);
584   LIST_INIT(ads->childw);
585   LIST_INIT(ads->output);
586   ads->forallnext= 0;
587   ads->nextid= 0x311f;
588   ads->nudp= 0;
589   ads->tcpsocket= -1;
590   adns__vbuf_init(&ads->tcpsend);
591   adns__vbuf_init(&ads->tcprecv);
592   ads->tcprecv_skip= 0;
593   ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
594   ads->searchndots= 1;
595   ads->tcpstate= server_disconnected;
596   timerclear(&ads->tcptimeout);
597   ads->searchlist= 0;
598
599   pid= getpid();
600   ads->rand48xsubi[0]= pid;
601   ads->rand48xsubi[1]= (unsigned long)pid >> 16;
602   ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
603
604   *ads_r= ads;
605   return 0;
606 }
607
608 static int init_finish(adns_state ads) {
609   struct sockaddr_in sin;
610   struct protoent *proto;
611   struct udpsocket *udp;
612   int i, j;
613   int r;
614   
615   if (!ads->nservers) {
616     if (ads->logfn && ads->iflags & adns_if_debug)
617       adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
618     memset(&sin, 0, sizeof(sin));
619     sin.sin_family = AF_INET;
620     sin.sin_port = htons(DNS_PORT);
621     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
622     addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
623   }
624
625   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
626   ads->nudp = 0;
627   for (i = 0; i < ads->nservers; i++) {
628     for (j = 0; j < ads->nudp; j++) {
629       if (ads->udpsocket[j].ai->af == ads->servers[i].addr.sa.sa_family)
630         goto afmatch;
631     }
632
633     assert(ads->nudp < MAXUDP);
634     udp = &ads->udpsocket[ads->nudp];
635     udp->ai = find_afinfo(ads->servers[i].addr.sa.sa_family);
636     assert(udp->ai);
637     udp->fd = socket(udp->ai->af,SOCK_DGRAM,proto->p_proto);
638     if (udp->fd < 0) { r= errno; goto x_free; }
639     r= adns__setnonblock(ads,udp->fd);
640     if (r) { r= errno; goto x_closeudp; }
641     ads->nudp++;
642
643   afmatch:;
644   }
645   
646   return 0;
647
648  x_closeudp:
649   for (j = 0; j < ads->nudp; j++) close(ads->udpsocket[j].fd);
650  x_free:
651   free(ads);
652   return r;
653 }
654
655 static void init_abort(adns_state ads) {
656   if (ads->nsearchlist) {
657     free(ads->searchlist[0]);
658     free(ads->searchlist);
659   }
660   free(ads);
661 }
662
663 static void logfn_file(adns_state ads, void *logfndata,
664                        const char *fmt, va_list al) {
665   vfprintf(logfndata,fmt,al);
666 }
667
668 static int init_files(adns_state *ads_r, adns_initflags flags,
669                       adns_logcallbackfn *logfn, void *logfndata) {
670   adns_state ads;
671   const char *res_options, *adns_res_options;
672   int r;
673   
674   r= init_begin(&ads, flags, logfn, logfndata);
675   if (r) return r;
676   
677   res_options= instrum_getenv(ads,"RES_OPTIONS");
678   adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
679   ccf_options(ads,"RES_OPTIONS",-1,res_options);
680   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
681
682   readconfig(ads,"/etc/resolv.conf",1);
683   readconfig(ads,"/etc/resolv-adns.conf",0);
684   readconfigenv(ads,"RES_CONF");
685   readconfigenv(ads,"ADNS_RES_CONF");
686
687   readconfigenvtext(ads,"RES_CONF_TEXT");
688   readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
689
690   ccf_options(ads,"RES_OPTIONS",-1,res_options);
691   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
692
693   ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
694   ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
695
696   if (ads->configerrno && ads->configerrno != EINVAL) {
697     r= ads->configerrno;
698     init_abort(ads);
699     return r;
700   }
701
702   r= init_finish(ads);
703   if (r) return r;
704
705   adns__consistency(ads,0,cc_entex);
706   *ads_r= ads;
707   return 0;
708 }
709
710 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
711   return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
712 }
713
714 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
715                        adns_logcallbackfn *logfn, void *logfndata,
716                        const char *configtext) {
717   adns_state ads;
718   int r;
719
720   r= init_begin(&ads, flags, logfn, logfndata);
721   if (r) return r;
722
723   readconfigtext(ads,configtext,"<supplied configuration text>");
724   if (ads->configerrno) {
725     r= ads->configerrno;
726     init_abort(ads);
727     return r;
728   }
729
730   r= init_finish(ads);  if (r) return r;
731   adns__consistency(ads,0,cc_entex);
732   *ads_r= ads;
733   return 0;
734 }
735
736 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
737                      FILE *diagfile, const char *configtext) {
738   return init_strcfg(ads_r, flags,
739                      diagfile ? logfn_file : 0, diagfile,
740                      configtext);
741 }
742
743 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
744                     const char *configtext /*0=>use default config files*/,
745                     adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
746                     void *logfndata /*0 with logfn==0 => discard*/) {
747   if (!logfn && logfndata)
748     logfn= logfn_file;
749   if (configtext)
750     return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
751   else
752     return init_files(newstate_r, flags, logfn, logfndata);
753 }
754
755 void adns_finish(adns_state ads) {
756   int i;
757   adns__consistency(ads,0,cc_entex);
758   for (;;) {
759     if (ads->udpw.head) adns_cancel(ads->udpw.head);
760     else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
761     else if (ads->childw.head) adns_cancel(ads->childw.head);
762     else if (ads->output.head) adns_cancel(ads->output.head);
763     else break;
764   }
765   for (i = 0; i < ads->nudp; i++) close(ads->udpsocket[i].fd);
766   if (ads->tcpsocket >= 0) close(ads->tcpsocket);
767   adns__vbuf_free(&ads->tcpsend);
768   adns__vbuf_free(&ads->tcprecv);
769   freesearchlist(ads);
770   free(ads);
771 }
772
773 void adns_forallqueries_begin(adns_state ads) {
774   adns__consistency(ads,0,cc_entex);
775   ads->forallnext=
776     ads->udpw.head ? ads->udpw.head :
777     ads->tcpw.head ? ads->tcpw.head :
778     ads->childw.head ? ads->childw.head :
779     ads->output.head;
780 }
781   
782 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
783   adns_query qu, nqu;
784
785   adns__consistency(ads,0,cc_entex);
786   nqu= ads->forallnext;
787   for (;;) {
788     qu= nqu;
789     if (!qu) return 0;
790     if (qu->next) {
791       nqu= qu->next;
792     } else if (qu == ads->udpw.tail) {
793       nqu=
794         ads->tcpw.head ? ads->tcpw.head :
795         ads->childw.head ? ads->childw.head :
796         ads->output.head;
797     } else if (qu == ads->tcpw.tail) {
798       nqu=
799         ads->childw.head ? ads->childw.head :
800         ads->output.head;
801     } else if (qu == ads->childw.tail) {
802       nqu= ads->output.head;
803     } else {
804       nqu= 0;
805     }
806     if (!qu->parent) break;
807   }
808   ads->forallnext= nqu;
809   if (context_r) *context_r= qu->ctx.ext;
810   return qu;
811 }