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