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