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