chiark / gitweb /
Support transport over IPv6.
[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 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     adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word);
314   }
315 }
316
317 static void ccf_clearnss(adns_state ads, const char *fn,
318                          int lno, const char *buf) {
319   ads->nservers= 0;
320 }
321
322 static void ccf_include(adns_state ads, const char *fn,
323                         int lno, const char *buf) {
324   if (!*buf) {
325     configparseerr(ads,fn,lno,"`include' directive with no filename");
326     return;
327   }
328   readconfig(ads,buf,1);
329 }
330
331 static void ccf_lookup(adns_state ads, const char *fn, int lno,
332                        const char *buf) {
333   int found_bind=0;
334   const char *word;
335   int l;
336
337   if (!*buf) {
338     configparseerr(ads,fn,lno,"`lookup' directive with no databases");
339     return;
340   }
341
342   while (nextword(&buf,&word,&l)) {
343     if (l==4 && !memcmp(word,"bind",4)) {
344       found_bind=1;
345     } else if (l==4 && !memcmp(word,"file",4)) {
346       /* ignore this and hope /etc/hosts is not essential */
347     } else if (l==2 && !memcmp(word,"yp",2)) {
348       adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno);
349       found_bind=-1;
350     } else {
351       adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'",
352                  fn,lno, l,word);
353       found_bind=-1;
354     }
355   }
356   if (!found_bind)
357     adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno);
358 }
359
360 static const struct configcommandinfo {
361   const char *name;
362   void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
363 } configcommandinfos[]= {
364   { "nameserver",        ccf_nameserver  },
365   { "domain",            ccf_search      },
366   { "search",            ccf_search      },
367   { "sortlist",          ccf_sortlist    },
368   { "options",           ccf_options     },
369   { "clearnameservers",  ccf_clearnss    },
370   { "include",           ccf_include     },
371   { "lookup",            ccf_lookup      }, /* OpenBSD */
372   {  0                                   }
373 };
374
375 typedef union {
376   FILE *file;
377   const char *text;
378 } getline_ctx;
379
380 static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename,
381                    int lno, char *buf, int buflen) {
382   FILE *file= src_io->file;
383   int c, i;
384   char *p;
385
386   p= buf;
387   buflen--;
388   i= 0;
389     
390   for (;;) { /* loop over chars */
391     if (i == buflen) {
392       adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
393       goto x_badline;
394     }
395     c= getc(file);
396     if (!c) {
397       adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno);
398       goto x_badline;
399     } else if (c == '\n') {
400       break;
401     } else if (c == EOF) {
402       if (ferror(file)) {
403         saveerr(ads,errno);
404         adns__diag(ads,-1,0,"%s:%d: read error: %s",
405                    filename,lno,strerror(errno));
406         return -1;
407       }
408       if (!i) return -1;
409       break;
410     } else {
411       *p++= c;
412       i++;
413     }
414   }
415
416   *p++= 0;
417   return i;
418
419  x_badline:
420   saveerr(ads,EINVAL);
421   while ((c= getc(file)) != EOF && c != '\n');
422   return -2;
423 }
424
425 static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename,
426                    int lno, char *buf, int buflen) {
427   const char *cp= src_io->text;
428   int l;
429
430   if (!cp || !*cp) return -1;
431
432   if (*cp == ';' || *cp == '\n') cp++;
433   l= strcspn(cp,";\n");
434   src_io->text = cp+l;
435
436   if (l >= buflen) {
437     adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno);
438     saveerr(ads,EINVAL);
439     return -2;
440   }
441     
442   memcpy(buf,cp,l);
443   buf[l]= 0;
444   return l;
445 }
446
447 static void readconfiggeneric(adns_state ads, const char *filename,
448                               int (*getline)(adns_state ads, getline_ctx*,
449                                              const char *filename, int lno,
450                                              char *buf, int buflen),
451                               /* Returns >=0 for success, -1 for EOF or error
452                                * (error will have been reported), or -2 for
453                                * bad line was encountered, try again.
454                                */
455                               getline_ctx gl_ctx) {
456   char linebuf[2000], *p, *q;
457   int lno, l, dirl;
458   const struct configcommandinfo *ccip;
459
460   for (lno=1;
461        (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1;
462        lno++) {
463     if (l == -2) continue;
464     while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
465     linebuf[l]= 0;
466     p= linebuf;
467     while (ctype_whitespace(*p)) p++;
468     if (*p == '#' || *p == ';' || !*p) continue;
469     q= p;
470     while (*q && !ctype_whitespace(*q)) q++;
471     dirl= q-p;
472     for (ccip=configcommandinfos;
473          ccip->name &&
474            !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p));
475          ccip++);
476     if (!ccip->name) {
477       adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'",
478                  filename,lno,(int)(q-p),p);
479       continue;
480     }
481     while (ctype_whitespace(*q)) q++;
482     ccip->fn(ads,filename,lno,q);
483   }
484 }
485
486 static const char *instrum_getenv(adns_state ads, const char *envvar) {
487   const char *value;
488
489   value= getenv(envvar);
490   if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar);
491   else adns__debug(ads,-1,0,"environment variable %s"
492                    " set to `%s'",envvar,value);
493   return value;
494 }
495
496 static void readconfig(adns_state ads, const char *filename, int warnmissing) {
497   getline_ctx gl_ctx;
498   
499   gl_ctx.file= fopen(filename,"r");
500   if (!gl_ctx.file) {
501     if (errno == ENOENT) {
502       if (warnmissing)
503         adns__debug(ads,-1,0, "configuration file"
504                     " `%s' does not exist",filename);
505       return;
506     }
507     saveerr(ads,errno);
508     adns__diag(ads,-1,0,"cannot open configuration file `%s': %s",
509                filename,strerror(errno));
510     return;
511   }
512
513   readconfiggeneric(ads,filename,gl_file,gl_ctx);
514   
515   fclose(gl_ctx.file);
516 }
517
518 static void readconfigtext(adns_state ads, const char *text,
519                            const char *showname) {
520   getline_ctx gl_ctx;
521   
522   gl_ctx.text= text;
523   readconfiggeneric(ads,showname,gl_text,gl_ctx);
524 }
525   
526 static void readconfigenv(adns_state ads, const char *envvar) {
527   const char *filename;
528
529   if (ads->iflags & adns_if_noenv) {
530     adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
531     return;
532   }
533   filename= instrum_getenv(ads,envvar);
534   if (filename) readconfig(ads,filename,1);
535 }
536
537 static void readconfigenvtext(adns_state ads, const char *envvar) {
538   const char *textdata;
539
540   if (ads->iflags & adns_if_noenv) {
541     adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar);
542     return;
543   }
544   textdata= instrum_getenv(ads,envvar);
545   if (textdata) readconfigtext(ads,textdata,envvar);
546 }
547
548
549 int adns__setnonblock(adns_state ads, int fd) {
550   int r;
551   
552   r= fcntl(fd,F_GETFL,0); if (r<0) return errno;
553   r |= O_NONBLOCK;
554   r= fcntl(fd,F_SETFL,r); if (r<0) return errno;
555   return 0;
556 }
557
558 static int init_begin(adns_state *ads_r, adns_initflags flags,
559                       adns_logcallbackfn *logfn, void *logfndata) {
560   adns_state ads;
561   pid_t pid;
562   
563   ads= malloc(sizeof(*ads)); if (!ads) return errno;
564
565   ads->iflags= flags;
566   ads->logfn= logfn;
567   ads->logfndata= logfndata;
568   ads->configerrno= 0;
569   LIST_INIT(ads->udpw);
570   LIST_INIT(ads->tcpw);
571   LIST_INIT(ads->childw);
572   LIST_INIT(ads->output);
573   ads->forallnext= 0;
574   ads->nextid= 0x311f;
575   ads->nudp= 0;
576   ads->tcpsocket= -1;
577   adns__vbuf_init(&ads->tcpsend);
578   adns__vbuf_init(&ads->tcprecv);
579   ads->tcprecv_skip= 0;
580   ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0;
581   ads->searchndots= 1;
582   ads->tcpstate= server_disconnected;
583   timerclear(&ads->tcptimeout);
584   ads->searchlist= 0;
585
586   pid= getpid();
587   ads->rand48xsubi[0]= pid;
588   ads->rand48xsubi[1]= (unsigned long)pid >> 16;
589   ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16);
590
591   *ads_r= ads;
592   return 0;
593 }
594
595 static int init_finish(adns_state ads) {
596   struct sockaddr_in sin;
597   struct protoent *proto;
598   struct udpsocket *udp;
599   int i, j;
600   int r;
601   
602   if (!ads->nservers) {
603     if (ads->logfn && ads->iflags & adns_if_debug)
604       adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n");
605     memset(&sin, 0, sizeof(sin));
606     sin.sin_port = htons(DNS_PORT);
607     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
608     addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
609   }
610
611   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
612   ads->nudp = 0;
613   for (i = 0; i < ads->nservers; i++) {
614     for (j = 0; j < ads->nudp; j++) {
615       if (ads->udpsocket[j].ai->af == ads->servers[i].addr.sa.sa_family)
616         goto afmatch;
617     }
618
619     assert(ads->nudp < MAXUDP);
620     udp = &ads->udpsocket[ads->nudp];
621     udp->ai = find_afinfo(ads->servers[i].addr.sa.sa_family);
622     assert(udp->ai);
623     udp->fd = socket(udp->ai->af,SOCK_DGRAM,proto->p_proto);
624     if (udp->fd < 0) { r= errno; goto x_free; }
625     r= adns__setnonblock(ads,udp->fd);
626     if (r) { r= errno; goto x_closeudp; }
627     ads->nudp++;
628
629   afmatch:;
630   }
631   
632   return 0;
633
634  x_closeudp:
635   for (j = 0; j < ads->nudp; j++) close(ads->udpsocket[j].fd);
636  x_free:
637   free(ads);
638   return r;
639 }
640
641 static void init_abort(adns_state ads) {
642   if (ads->nsearchlist) {
643     free(ads->searchlist[0]);
644     free(ads->searchlist);
645   }
646   free(ads);
647 }
648
649 static void logfn_file(adns_state ads, void *logfndata,
650                        const char *fmt, va_list al) {
651   vfprintf(logfndata,fmt,al);
652 }
653
654 static int init_files(adns_state *ads_r, adns_initflags flags,
655                       adns_logcallbackfn *logfn, void *logfndata) {
656   adns_state ads;
657   const char *res_options, *adns_res_options;
658   int r;
659   
660   r= init_begin(&ads, flags, logfn, logfndata);
661   if (r) return r;
662   
663   res_options= instrum_getenv(ads,"RES_OPTIONS");
664   adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
665   ccf_options(ads,"RES_OPTIONS",-1,res_options);
666   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
667
668   readconfig(ads,"/etc/resolv.conf",1);
669   readconfig(ads,"/etc/resolv-adns.conf",0);
670   readconfigenv(ads,"RES_CONF");
671   readconfigenv(ads,"ADNS_RES_CONF");
672
673   readconfigenvtext(ads,"RES_CONF_TEXT");
674   readconfigenvtext(ads,"ADNS_RES_CONF_TEXT");
675
676   ccf_options(ads,"RES_OPTIONS",-1,res_options);
677   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
678
679   ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
680   ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
681
682   if (ads->configerrno && ads->configerrno != EINVAL) {
683     r= ads->configerrno;
684     init_abort(ads);
685     return r;
686   }
687
688   r= init_finish(ads);
689   if (r) return r;
690
691   adns__consistency(ads,0,cc_entex);
692   *ads_r= ads;
693   return 0;
694 }
695
696 int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) {
697   return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr);
698 }
699
700 static int init_strcfg(adns_state *ads_r, adns_initflags flags,
701                        adns_logcallbackfn *logfn, void *logfndata,
702                        const char *configtext) {
703   adns_state ads;
704   int r;
705
706   r= init_begin(&ads, flags, logfn, logfndata);
707   if (r) return r;
708
709   readconfigtext(ads,configtext,"<supplied configuration text>");
710   if (ads->configerrno) {
711     r= ads->configerrno;
712     init_abort(ads);
713     return r;
714   }
715
716   r= init_finish(ads);  if (r) return r;
717   adns__consistency(ads,0,cc_entex);
718   *ads_r= ads;
719   return 0;
720 }
721
722 int adns_init_strcfg(adns_state *ads_r, adns_initflags flags,
723                      FILE *diagfile, const char *configtext) {
724   return init_strcfg(ads_r, flags,
725                      diagfile ? logfn_file : 0, diagfile,
726                      configtext);
727 }
728
729 int adns_init_logfn(adns_state *newstate_r, adns_initflags flags,
730                     const char *configtext /*0=>use default config files*/,
731                     adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */,
732                     void *logfndata /*0 with logfn==0 => discard*/) {
733   if (!logfn && logfndata)
734     logfn= logfn_file;
735   if (configtext)
736     return init_strcfg(newstate_r, flags, logfn, logfndata, configtext);
737   else
738     return init_files(newstate_r, flags, logfn, logfndata);
739 }
740
741 void adns_finish(adns_state ads) {
742   int i;
743   adns__consistency(ads,0,cc_entex);
744   for (;;) {
745     if (ads->udpw.head) adns_cancel(ads->udpw.head);
746     else if (ads->tcpw.head) adns_cancel(ads->tcpw.head);
747     else if (ads->childw.head) adns_cancel(ads->childw.head);
748     else if (ads->output.head) adns_cancel(ads->output.head);
749     else break;
750   }
751   for (i = 0; i < ads->nudp; i++) close(ads->udpsocket[i].fd);
752   if (ads->tcpsocket >= 0) close(ads->tcpsocket);
753   adns__vbuf_free(&ads->tcpsend);
754   adns__vbuf_free(&ads->tcprecv);
755   freesearchlist(ads);
756   free(ads);
757 }
758
759 void adns_forallqueries_begin(adns_state ads) {
760   adns__consistency(ads,0,cc_entex);
761   ads->forallnext=
762     ads->udpw.head ? ads->udpw.head :
763     ads->tcpw.head ? ads->tcpw.head :
764     ads->childw.head ? ads->childw.head :
765     ads->output.head;
766 }
767   
768 adns_query adns_forallqueries_next(adns_state ads, void **context_r) {
769   adns_query qu, nqu;
770
771   adns__consistency(ads,0,cc_entex);
772   nqu= ads->forallnext;
773   for (;;) {
774     qu= nqu;
775     if (!qu) return 0;
776     if (qu->next) {
777       nqu= qu->next;
778     } else if (qu == ads->udpw.tail) {
779       nqu=
780         ads->tcpw.head ? ads->tcpw.head :
781         ads->childw.head ? ads->childw.head :
782         ads->output.head;
783     } else if (qu == ads->tcpw.tail) {
784       nqu=
785         ads->childw.head ? ads->childw.head :
786         ads->output.head;
787     } else if (qu == ads->childw.tail) {
788       nqu= ads->output.head;
789     } else {
790       nqu= 0;
791     }
792     if (!qu->parent) break;
793   }
794   ads->forallnext= nqu;
795   if (context_r) *context_r= qu->ctx.ext;
796   return qu;
797 }