98db6da3 |
1 | /* |
2 | * setup.c |
3 | * - configuration file parsing |
4 | * - management of global state |
5 | */ |
6 | /* |
a79ac5ba |
7 | * This file is |
8 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
9 | * |
10 | * It is part of adns, which is |
89435c42 |
11 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
a79ac5ba |
12 | * Copyright (C) 1999 Tony Finch <dot@dotat.at> |
98db6da3 |
13 | * |
14 | * This program is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2, or (at your option) |
17 | * any later version. |
18 | * |
19 | * This program is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with this program; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
6f17710a |
28 | |
d05cc330 |
29 | #include <stdlib.h> |
30 | #include <errno.h> |
7ca1d685 |
31 | #include <limits.h> |
d05cc330 |
32 | #include <unistd.h> |
33 | #include <fcntl.h> |
6f17710a |
34 | |
d05cc330 |
35 | #include <netdb.h> |
763d28b9 |
36 | #include <sys/socket.h> |
37 | #include <netinet/in.h> |
d05cc330 |
38 | #include <arpa/inet.h> |
39 | |
40 | #include "internal.h" |
41 | |
45efc92a |
42 | static void readconfig(adns_state ads, const char *filename, int warnmissing); |
d6b271ae |
43 | |
6f17710a |
44 | static void addserver(adns_state ads, struct in_addr addr) { |
45 | int i; |
46 | struct server *ss; |
47 | |
48 | for (i=0; i<ads->nservers; i++) { |
49 | if (ads->servers[i].addr.s_addr == addr.s_addr) { |
11c8bf9b |
50 | adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr)); |
6f17710a |
51 | return; |
52 | } |
53 | } |
54 | |
55 | if (ads->nservers>=MAXSERVERS) { |
11c8bf9b |
56 | adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr)); |
6f17710a |
57 | return; |
58 | } |
59 | |
60 | ss= ads->servers+ads->nservers; |
61 | ss->addr= addr; |
6f17710a |
62 | ads->nservers++; |
63 | } |
64 | |
fc86e61f |
65 | static void freesearchlist(adns_state ads) { |
66 | if (ads->nsearchlist) free(*ads->searchlist); |
67 | free(ads->searchlist); |
68 | } |
69 | |
d6b271ae |
70 | static void saveerr(adns_state ads, int en) { |
71 | if (!ads->configerrno) ads->configerrno= en; |
72 | } |
73 | |
6f17710a |
74 | static void configparseerr(adns_state ads, const char *fn, int lno, |
75 | const char *fmt, ...) { |
76 | va_list al; |
d6b271ae |
77 | |
78 | saveerr(ads,EINVAL); |
79 | if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return; |
80 | |
81 | if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn); |
82 | else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno); |
6f17710a |
83 | va_start(al,fmt); |
d6b271ae |
84 | vfprintf(ads->diagfile,fmt,al); |
6f17710a |
85 | va_end(al); |
d6b271ae |
86 | fputc('\n',ads->diagfile); |
6f17710a |
87 | } |
88 | |
11f553d9 |
89 | static int nextword(const char **bufp_io, const char **word_r, int *l_r) { |
90 | const char *p, *q; |
91 | |
92 | p= *bufp_io; |
93 | while (ctype_whitespace(*p)) p++; |
94 | if (!*p) return 0; |
95 | |
96 | q= p; |
97 | while (*q && !ctype_whitespace(*q)) q++; |
98 | |
99 | *l_r= q-p; |
100 | *word_r= p; |
101 | *bufp_io= q; |
102 | |
103 | return 1; |
104 | } |
105 | |
6f17710a |
106 | static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) { |
107 | struct in_addr ia; |
108 | |
109 | if (!inet_aton(buf,&ia)) { |
110 | configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf); |
111 | return; |
112 | } |
11c8bf9b |
113 | adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia)); |
6f17710a |
114 | addserver(ads,ia); |
115 | } |
116 | |
117 | static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) { |
11f553d9 |
118 | const char *bufp, *word; |
119 | char *newchars, **newptrs, **pp; |
120 | int count, tl, l; |
121 | |
6f17710a |
122 | if (!buf) return; |
11f553d9 |
123 | |
124 | bufp= buf; |
125 | count= 0; |
126 | tl= 0; |
127 | while (nextword(&bufp,&word,&l)) { count++; tl += l+1; } |
128 | |
129 | newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; } |
130 | newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; } |
131 | |
132 | bufp= buf; |
133 | pp= newptrs; |
134 | while (nextword(&bufp,&word,&l)) { |
135 | *pp++= newchars; |
136 | memcpy(newchars,word,l); |
137 | newchars += l; |
138 | *newchars++ = 0; |
139 | } |
140 | |
fc86e61f |
141 | freesearchlist(ads); |
11f553d9 |
142 | ads->nsearchlist= count; |
143 | ads->searchlist= newptrs; |
6f17710a |
144 | } |
145 | |
11f553d9 |
146 | static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) { |
147 | const char *word; |
8c3aa944 |
148 | char tbuf[200], *slash, *ep; |
149 | struct in_addr base, mask; |
150 | int l; |
151 | unsigned long initial, baselocal; |
152 | |
11f553d9 |
153 | if (!buf) return; |
154 | |
8c3aa944 |
155 | ads->nsortlist= 0; |
11f553d9 |
156 | while (nextword(&buf,&word,&l)) { |
8c3aa944 |
157 | if (ads->nsortlist >= MAXSORTLIST) { |
11f553d9 |
158 | adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word); |
8c3aa944 |
159 | return; |
160 | } |
161 | |
162 | if (l >= sizeof(tbuf)) { |
11f553d9 |
163 | configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word); |
8c3aa944 |
164 | continue; |
165 | } |
166 | |
7ca1d685 |
167 | memcpy(tbuf,word,l); tbuf[l]= 0; |
8c3aa944 |
168 | slash= strchr(tbuf,'/'); |
169 | if (slash) *slash++= 0; |
170 | |
171 | if (!inet_aton(tbuf,&base)) { |
172 | configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf); |
173 | continue; |
174 | } |
175 | |
176 | if (slash) { |
177 | if (strchr(slash,'.')) { |
178 | if (!inet_aton(slash,&mask)) { |
179 | configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash); |
180 | continue; |
181 | } |
182 | if (base.s_addr & ~mask.s_addr) { |
183 | configparseerr(ads,fn,lno, |
184 | "mask `%s' in sortlist overlaps address `%s'",slash,tbuf); |
185 | continue; |
186 | } |
187 | } else { |
188 | initial= strtoul(slash,&ep,10); |
189 | if (*ep || initial>32) { |
190 | configparseerr(ads,fn,lno,"mask length `%s' invalid",slash); |
191 | continue; |
192 | } |
193 | mask.s_addr= htonl((0x0ffffffffUL) << (32-initial)); |
194 | } |
195 | } else { |
196 | baselocal= ntohl(base.s_addr); |
197 | if (!baselocal & 0x080000000UL) /* class A */ |
198 | mask.s_addr= htonl(0x0ff000000UL); |
199 | else if ((baselocal & 0x0c0000000UL) == 0x080000000UL) |
200 | mask.s_addr= htonl(0x0ffff0000UL); /* class B */ |
201 | else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL) |
202 | mask.s_addr= htonl(0x0ff000000UL); /* class C */ |
203 | else { |
204 | configparseerr(ads,fn,lno, |
205 | "network address `%s' in sortlist is not in classed ranges," |
206 | " must specify mask explicitly", tbuf); |
207 | continue; |
208 | } |
209 | } |
210 | |
211 | ads->sortlist[ads->nsortlist].base= base; |
212 | ads->sortlist[ads->nsortlist].mask= mask; |
213 | ads->nsortlist++; |
214 | } |
6f17710a |
215 | } |
216 | |
217 | static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) { |
7ca1d685 |
218 | const char *word; |
219 | char *ep; |
220 | unsigned long v; |
221 | int l; |
222 | |
6f17710a |
223 | if (!buf) return; |
7ca1d685 |
224 | |
225 | while (nextword(&buf,&word,&l)) { |
226 | if (l==5 && !memcmp(word,"debug",5)) { |
227 | ads->iflags |= adns_if_debug; |
228 | continue; |
229 | } |
230 | if (l>=6 && !memcmp(word,"ndots:",6)) { |
231 | v= strtoul(word+6,&ep,10); |
232 | if (l==6 || ep != word+l || v > INT_MAX) { |
233 | configparseerr(ads,fn,lno,"option `%.*s' malformed or has bad value",l,word); |
234 | continue; |
235 | } |
236 | ads->searchndots= v; |
237 | continue; |
238 | } |
1389dc72 |
239 | if (l>=12 && !memcmp(word,"adns_checkc:",12)) { |
240 | if (!strcmp(word+12,"none")) { |
241 | ads->iflags &= ~adns_if_checkc_freq; |
242 | ads->iflags |= adns_if_checkc_entex; |
243 | } else if (!strcmp(word+12,"entex")) { |
244 | ads->iflags &= ~adns_if_checkc_freq; |
245 | ads->iflags |= adns_if_checkc_entex; |
246 | } else if (!strcmp(word+12,"freq")) { |
247 | ads->iflags |= adns_if_checkc_freq; |
248 | } else { |
249 | configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' " |
250 | "(must be none, entex or freq", word+12); |
251 | } |
252 | continue; |
253 | } |
7ca1d685 |
254 | adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word); |
255 | } |
6f17710a |
256 | } |
257 | |
258 | static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) { |
259 | ads->nservers= 0; |
260 | } |
261 | |
d6b271ae |
262 | static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) { |
263 | if (!*buf) { |
264 | configparseerr(ads,fn,lno,"`include' directive with no filename"); |
265 | return; |
266 | } |
45efc92a |
267 | readconfig(ads,buf,1); |
d6b271ae |
268 | } |
269 | |
6f17710a |
270 | static const struct configcommandinfo { |
271 | const char *name; |
272 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); |
273 | } configcommandinfos[]= { |
274 | { "nameserver", ccf_nameserver }, |
275 | { "domain", ccf_search }, |
276 | { "search", ccf_search }, |
277 | { "sortlist", ccf_sortlist }, |
278 | { "options", ccf_options }, |
279 | { "clearnameservers", ccf_clearnss }, |
d6b271ae |
280 | { "include", ccf_include }, |
6f17710a |
281 | { 0 } |
282 | }; |
283 | |
d6b271ae |
284 | typedef union { |
6f17710a |
285 | FILE *file; |
d6b271ae |
286 | const char *text; |
287 | } getline_ctx; |
6f17710a |
288 | |
d6b271ae |
289 | static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename, |
290 | int lno, char *buf, int buflen) { |
291 | FILE *file= src_io->file; |
292 | int c, i; |
293 | char *p; |
294 | |
295 | p= buf; |
296 | buflen--; |
297 | i= 0; |
298 | |
299 | for (;;) { /* loop over chars */ |
300 | if (i == buflen) { |
301 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); |
302 | goto x_badline; |
303 | } |
304 | c= getc(file); |
305 | if (!c) { |
306 | adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno); |
307 | goto x_badline; |
308 | } else if (c == '\n') { |
309 | break; |
310 | } else if (c == EOF) { |
311 | if (ferror(file)) { |
312 | saveerr(ads,errno); |
313 | adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno)); |
314 | return -1; |
315 | } |
316 | if (!i) return -1; |
317 | break; |
318 | } else { |
319 | *p++= c; |
320 | i++; |
6f17710a |
321 | } |
6f17710a |
322 | } |
323 | |
d6b271ae |
324 | *p++= 0; |
325 | return i; |
326 | |
327 | x_badline: |
328 | saveerr(ads,EINVAL); |
329 | while ((c= getc(file)) != EOF && c != '\n'); |
330 | return -2; |
331 | } |
332 | |
333 | static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename, |
334 | int lno, char *buf, int buflen) { |
8c3aa944 |
335 | const char *cp= src_io->text; |
d6b271ae |
336 | int l; |
337 | |
8c3aa944 |
338 | if (!cp || !*cp) return -1; |
d6b271ae |
339 | |
8c3aa944 |
340 | if (*cp == ';' || *cp == '\n') cp++; |
341 | l= strcspn(cp,";\n"); |
342 | src_io->text = cp+l; |
d6b271ae |
343 | |
344 | if (l >= buflen) { |
345 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); |
346 | saveerr(ads,EINVAL); |
347 | return -2; |
348 | } |
349 | |
350 | memcpy(buf,cp,l); |
351 | buf[l]= 0; |
352 | return l; |
353 | } |
354 | |
355 | static void readconfiggeneric(adns_state ads, const char *filename, |
356 | int (*getline)(adns_state ads, getline_ctx*, |
357 | const char *filename, int lno, |
358 | char *buf, int buflen), |
359 | /* Returns >=0 for success, -1 for EOF or error |
360 | * (error will have been reported), or -2 for |
361 | * bad line was encountered, try again. |
362 | */ |
363 | getline_ctx gl_ctx) { |
364 | char linebuf[2000], *p, *q; |
365 | int lno, l, dirl; |
366 | const struct configcommandinfo *ccip; |
367 | |
368 | for (lno=1; |
369 | (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1; |
370 | lno++) { |
371 | if (l == -2) continue; |
6f17710a |
372 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
373 | linebuf[l]= 0; |
374 | p= linebuf; |
375 | while (ctype_whitespace(*p)) p++; |
d6b271ae |
376 | if (*p == '#' || !*p) continue; |
6f17710a |
377 | q= p; |
378 | while (*q && !ctype_whitespace(*q)) q++; |
d6b271ae |
379 | dirl= q-p; |
6f17710a |
380 | for (ccip=configcommandinfos; |
d6b271ae |
381 | ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p)); |
6f17710a |
382 | ccip++); |
383 | if (!ccip->name) { |
11c8bf9b |
384 | adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'", |
d05cc330 |
385 | filename,lno,q-p,p); |
6f17710a |
386 | continue; |
387 | } |
388 | while (ctype_whitespace(*q)) q++; |
389 | ccip->fn(ads,filename,lno,q); |
390 | } |
6f17710a |
391 | } |
392 | |
393 | static const char *instrum_getenv(adns_state ads, const char *envvar) { |
394 | const char *value; |
395 | |
396 | value= getenv(envvar); |
11c8bf9b |
397 | if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar); |
398 | else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value); |
6f17710a |
399 | return value; |
400 | } |
401 | |
45efc92a |
402 | static void readconfig(adns_state ads, const char *filename, int warnmissing) { |
d6b271ae |
403 | getline_ctx gl_ctx; |
404 | |
405 | gl_ctx.file= fopen(filename,"r"); |
406 | if (!gl_ctx.file) { |
407 | if (errno == ENOENT) { |
45efc92a |
408 | if (warnmissing) |
409 | adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename); |
d6b271ae |
410 | return; |
411 | } |
412 | saveerr(ads,errno); |
413 | adns__diag(ads,-1,0,"cannot open configuration file `%s': %s", |
414 | filename,strerror(errno)); |
415 | return; |
416 | } |
417 | |
418 | readconfiggeneric(ads,filename,gl_file,gl_ctx); |
419 | |
420 | fclose(gl_ctx.file); |
421 | } |
422 | |
423 | static void readconfigtext(adns_state ads, const char *text, const char *showname) { |
424 | getline_ctx gl_ctx; |
425 | |
426 | gl_ctx.text= text; |
427 | readconfiggeneric(ads,showname,gl_text,gl_ctx); |
428 | } |
429 | |
6f17710a |
430 | static void readconfigenv(adns_state ads, const char *envvar) { |
431 | const char *filename; |
432 | |
433 | if (ads->iflags & adns_if_noenv) { |
11c8bf9b |
434 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
6f17710a |
435 | return; |
436 | } |
437 | filename= instrum_getenv(ads,envvar); |
45efc92a |
438 | if (filename) readconfig(ads,filename,1); |
6f17710a |
439 | } |
d05cc330 |
440 | |
8c3aa944 |
441 | static void readconfigenvtext(adns_state ads, const char *envvar) { |
442 | const char *textdata; |
443 | |
444 | if (ads->iflags & adns_if_noenv) { |
445 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
446 | return; |
447 | } |
448 | textdata= instrum_getenv(ads,envvar); |
449 | if (textdata) readconfigtext(ads,textdata,envvar); |
450 | } |
451 | |
d05cc330 |
452 | |
453 | int adns__setnonblock(adns_state ads, int fd) { |
454 | int r; |
6f17710a |
455 | |
d05cc330 |
456 | r= fcntl(fd,F_GETFL,0); if (r<0) return errno; |
457 | r |= O_NONBLOCK; |
458 | r= fcntl(fd,F_SETFL,r); if (r<0) return errno; |
459 | return 0; |
460 | } |
461 | |
d6b271ae |
462 | static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
6f17710a |
463 | adns_state ads; |
6f17710a |
464 | |
465 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
d6b271ae |
466 | |
6f17710a |
467 | ads->iflags= flags; |
d6b271ae |
468 | ads->diagfile= diagfile; |
7def4935 |
469 | ads->configerrno= 0; |
d0a057ac |
470 | LIST_INIT(ads->udpw); |
471 | LIST_INIT(ads->tcpw); |
d05cc330 |
472 | LIST_INIT(ads->childw); |
473 | LIST_INIT(ads->output); |
8f2aa812 |
474 | ads->forallnext= 0; |
d05cc330 |
475 | ads->nextid= 0x311f; |
476 | ads->udpsocket= ads->tcpsocket= -1; |
d05cc330 |
477 | adns__vbuf_init(&ads->tcpsend); |
478 | adns__vbuf_init(&ads->tcprecv); |
ab898cf4 |
479 | ads->tcprecv_skip= 0; |
11f553d9 |
480 | ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0; |
7e6a84a1 |
481 | ads->searchndots= 1; |
7def4935 |
482 | ads->tcpstate= server_disconnected; |
d05cc330 |
483 | timerclear(&ads->tcptimeout); |
7def4935 |
484 | ads->searchlist= 0; |
6f17710a |
485 | |
d6b271ae |
486 | *ads_r= ads; |
487 | return 0; |
488 | } |
6f17710a |
489 | |
d6b271ae |
490 | static int init_finish(adns_state ads) { |
491 | struct in_addr ia; |
492 | struct protoent *proto; |
493 | int r; |
494 | |
6f17710a |
495 | if (!ads->nservers) { |
d6b271ae |
496 | if (ads->diagfile && ads->iflags & adns_if_debug) |
497 | fprintf(ads->diagfile,"adns: no nameservers, using localhost\n"); |
8c3aa944 |
498 | ia.s_addr= htonl(INADDR_LOOPBACK); |
6f17710a |
499 | addserver(ads,ia); |
500 | } |
501 | |
502 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } |
503 | ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto); |
71324651 |
504 | if (ads->udpsocket<0) { r= errno; goto x_free; } |
48cb0b4b |
505 | |
de8b18da |
506 | r= adns__setnonblock(ads,ads->udpsocket); |
507 | if (r) { r= errno; goto x_closeudp; } |
6f17710a |
508 | |
6f17710a |
509 | return 0; |
510 | |
de8b18da |
511 | x_closeudp: |
512 | close(ads->udpsocket); |
6f17710a |
513 | x_free: |
514 | free(ads); |
515 | return r; |
516 | } |
517 | |
11f553d9 |
518 | static void init_abort(adns_state ads) { |
519 | if (ads->nsearchlist) { |
520 | free(ads->searchlist[0]); |
521 | free(ads->searchlist); |
522 | } |
523 | free(ads); |
524 | } |
525 | |
edb95764 |
526 | int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
d6b271ae |
527 | adns_state ads; |
528 | const char *res_options, *adns_res_options; |
529 | int r; |
530 | |
531 | r= init_begin(&ads, flags, diagfile ? diagfile : stderr); |
532 | if (r) return r; |
533 | |
534 | res_options= instrum_getenv(ads,"RES_OPTIONS"); |
535 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); |
536 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
537 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
538 | |
45efc92a |
539 | readconfig(ads,"/etc/resolv.conf",1); |
540 | readconfig(ads,"/etc/resolv-adns.conf",0); |
d6b271ae |
541 | readconfigenv(ads,"RES_CONF"); |
542 | readconfigenv(ads,"ADNS_RES_CONF"); |
543 | |
8c3aa944 |
544 | readconfigenvtext(ads,"RES_CONF_TEXT"); |
545 | readconfigenvtext(ads,"ADNS_RES_CONF_TEXT"); |
546 | |
d6b271ae |
547 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
548 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
549 | |
550 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); |
551 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); |
552 | |
11f553d9 |
553 | if (ads->configerrno && ads->configerrno != EINVAL) { |
554 | r= ads->configerrno; |
555 | init_abort(ads); |
556 | return r; |
557 | } |
558 | |
d6b271ae |
559 | r= init_finish(ads); |
560 | if (r) return r; |
561 | |
2ac463bf |
562 | adns__consistency(ads,0,cc_entex); |
d6b271ae |
563 | *ads_r= ads; |
564 | return 0; |
565 | } |
566 | |
edb95764 |
567 | int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, |
d6b271ae |
568 | FILE *diagfile, const char *configtext) { |
569 | adns_state ads; |
570 | int r; |
571 | |
572 | r= init_begin(&ads, flags, diagfile); if (r) return r; |
573 | |
574 | readconfigtext(ads,configtext,"<supplied configuration text>"); |
575 | if (ads->configerrno) { |
576 | r= ads->configerrno; |
11f553d9 |
577 | init_abort(ads); |
d6b271ae |
578 | return r; |
579 | } |
580 | |
581 | r= init_finish(ads); if (r) return r; |
2ac463bf |
582 | adns__consistency(ads,0,cc_entex); |
d6b271ae |
583 | *ads_r= ads; |
584 | return 0; |
585 | } |
586 | |
1389dc72 |
587 | |
61093792 |
588 | void adns_finish(adns_state ads) { |
2ac463bf |
589 | adns__consistency(ads,0,cc_entex); |
61093792 |
590 | for (;;) { |
d0a057ac |
591 | if (ads->udpw.head) adns_cancel(ads->udpw.head); |
592 | else if (ads->tcpw.head) adns_cancel(ads->tcpw.head); |
61093792 |
593 | else if (ads->childw.head) adns_cancel(ads->childw.head); |
594 | else if (ads->output.head) adns_cancel(ads->output.head); |
595 | else break; |
596 | } |
597 | close(ads->udpsocket); |
598 | if (ads->tcpsocket >= 0) close(ads->tcpsocket); |
599 | adns__vbuf_free(&ads->tcpsend); |
600 | adns__vbuf_free(&ads->tcprecv); |
fc86e61f |
601 | freesearchlist(ads); |
61093792 |
602 | free(ads); |
6f17710a |
603 | } |
8f2aa812 |
604 | |
605 | void adns_forallqueries_begin(adns_state ads) { |
2ac463bf |
606 | adns__consistency(ads,0,cc_entex); |
8f2aa812 |
607 | ads->forallnext= |
d0a057ac |
608 | ads->udpw.head ? ads->udpw.head : |
609 | ads->tcpw.head ? ads->tcpw.head : |
8f2aa812 |
610 | ads->childw.head ? ads->childw.head : |
611 | ads->output.head; |
612 | } |
613 | |
614 | adns_query adns_forallqueries_next(adns_state ads, void **context_r) { |
615 | adns_query qu, nqu; |
616 | |
2ac463bf |
617 | adns__consistency(ads,0,cc_entex); |
8f2aa812 |
618 | nqu= ads->forallnext; |
619 | for (;;) { |
620 | qu= nqu; |
621 | if (!qu) return 0; |
cb32030b |
622 | if (qu->next) { |
623 | nqu= qu->next; |
d0a057ac |
624 | } else if (qu == ads->udpw.tail) { |
625 | nqu= |
626 | ads->tcpw.head ? ads->tcpw.head : |
627 | ads->childw.head ? ads->childw.head : |
628 | ads->output.head; |
629 | } else if (qu == ads->tcpw.tail) { |
630 | nqu= |
631 | ads->childw.head ? ads->childw.head : |
632 | ads->output.head; |
cb32030b |
633 | } else if (qu == ads->childw.tail) { |
634 | nqu= ads->output.head; |
635 | } else { |
636 | nqu= 0; |
637 | } |
8f2aa812 |
638 | if (!qu->parent) break; |
639 | } |
640 | ads->forallnext= nqu; |
641 | if (context_r) *context_r= qu->ctx.ext; |
642 | return qu; |
643 | } |