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