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