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