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