1 /**	$MirOS: src/lib/libc/net/res_init.c,v 1.4 2005/09/22 20:40:03 tg Exp $	*/
2 /*	$OpenBSD: res_init.c,v 1.33 2005/08/06 20:30:04 espie Exp $	*/
3 
4 /*
5  * Copyright (c) 1985, 1989, 1993
6  *    The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  * -
32  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33  *
34  * Permission to use, copy, modify, and distribute this software for any
35  * purpose with or without fee is hereby granted, provided that the above
36  * copyright notice and this permission notice appear in all copies, and that
37  * the name of Digital Equipment Corporation not be used in advertising or
38  * publicity pertaining to distribution of the document or software without
39  * specific, written prior permission.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
42  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
44  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
45  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
46  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
47  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
48  * SOFTWARE.
49  */
50 
51 #ifndef INET6
52 #define INET6
53 #endif
54 
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <sys/time.h>
58 #include <sys/stat.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <arpa/nameser.h>
62 
63 #include <stdio.h>
64 #include <ctype.h>
65 #include <resolv.h>
66 #include <unistd.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #ifdef INET6
70 #include <netdb.h>
71 #endif /* INET6 */
72 
73 #include "thread_private.h"
74 
75 __RCSID("$MirOS: src/lib/libc/net/res_init.c,v 1.4 2005/09/22 20:40:03 tg Exp $");
76 
77 /*-------------------------------------- info about "sortlist" --------------
78  * Marc Majka		1994/04/16
79  * Allan Nathanson	1994/10/29 (BIND 4.9.3.x)
80  *
81  * NetInfo resolver configuration directory support.
82  *
83  * Allow a NetInfo directory to be created in the hierarchy which
84  * contains the same information as the resolver configuration file.
85  *
86  * - The local domain name is stored as the value of the "domain" property.
87  * - The Internet address(es) of the name server(s) are stored as values
88  *   of the "nameserver" property.
89  * - The name server addresses are stored as values of the "nameserver"
90  *   property.
91  * - The search list for host-name lookup is stored as values of the
92  *   "search" property.
93  * - The sortlist comprised of IP address netmask pairs are stored as
94  *   values of the "sortlist" property. The IP address and optional netmask
95  *   should be separated by a slash (/) or ampersand (&) character.
96  * - Internal resolver variables can be set from the value of the "options"
97  *   property.
98  */
99 
100 static void res_setoptions(char *, char *);
101 
102 #ifdef RESOLVSORT
103 static const char sort_mask[] = "/&";
104 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
105 static u_int32_t net_mask(struct in_addr);
106 #endif
107 
108 /*
109  * Resolver state default settings.
110  */
111 void *__THREAD_NAME(_res);
112 
113 struct __res_state _res
114 # if defined(__BIND_RES_TEXT)
115 	= { RES_TIMEOUT, }	/* Motorola, et al. */
116 # endif
117 	;
118 #ifdef INET6
119 void *__THREAD_NAME(_res_ext);
120 
121 struct __res_state_ext _res_ext;
122 #endif /* INET6 */
123 
124 int __res_chktime = 30;
125 
126 /*
127  * Set up default settings.  If the configuration file exist, the values
128  * there will have precedence.  Otherwise, the server address is set to
129  * INADDR_ANY and the default domain name comes from the gethostname().
130  *
131  * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
132  * rather than INADDR_ANY ("0.0.0.0") as the default name server address
133  * since it was noted that INADDR_ANY actually meant ``the first interface
134  * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
135  * it had to be "up" in order for you to reach your own name server.  It
136  * was later decided that since the recommended practice is to always
137  * install local static routes through 127.0.0.1 for all your network
138  * interfaces, that we could solve this problem without a code change.
139  *
140  * The configuration file should always be used, since it is the only way
141  * to specify a default domain.  If you are running a server on your local
142  * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
143  * in the configuration file.
144  *
145  * Return 0 if completes successfully, -1 on error
146  */
147 int
res_init(void)148 res_init(void)
149 {
150 
151 	return (_res_init(1));
152 }
153 
154 int
_res_init(int usercall)155 _res_init(int usercall)
156 {
157 	struct stat sb;
158 	struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
159 #ifdef INET6
160 	struct __res_state_ext *_res_extp = _THREAD_PRIVATE(_res_ext, _res_ext,
161 							    &_res_ext);
162 #endif
163 	FILE *fp;
164 	char *cp, **pp;
165 	int n;
166 	char buf[BUFSIZ];
167 	int nserv = 0;    /* number of nameserver records read from file */
168 	int haveenv = 0;
169 	int havesearch = 0;
170 	size_t len;
171 #ifdef RESOLVSORT
172 	int nsort = 0;
173 	char *net;
174 #endif
175 #ifndef RFC1535
176 	int dots;
177 #endif
178 
179 	if (usercall == 0) {
180 		if (_resp->options & RES_INIT &&
181 		    _resp->reschktime >= time(NULL))
182 			return (0);
183 		_resp->reschktime = time(NULL) + __res_chktime;
184 		if (stat(_PATH_RESCONF, &sb) != -1) {
185 			if (timespeccmp(&sb.st_mtimespec,
186 			    &_resp->restimespec, ==))
187 				return (0);
188 			else
189 				_resp->restimespec = sb.st_mtimespec;
190 		} else {
191 			/*
192 			 * Lost the file, in chroot?
193 			 * Don' trash settings
194 			 */
195 			if (timespecisset(&_resp->restimespec))
196 				return (0);
197 		}
198 	} else
199 		_resp->reschktime = time(NULL) + __res_chktime;
200 
201 
202 	/*
203 	 * These three fields used to be statically initialized.  This made
204 	 * it hard to use this code in a shared library.  It is necessary,
205 	 * now that we're doing dynamic initialization here, that we preserve
206 	 * the old semantics: if an application modifies one of these three
207 	 * fields of _res before res_init() is called, res_init() will not
208 	 * alter them.  Of course, if an application is setting them to
209 	 * _zero_ before calling res_init(), hoping to override what used
210 	 * to be the static default, we can't detect it and unexpected results
211 	 * will follow.  Zero for any of these fields would make no sense,
212 	 * so one can safely assume that the applications were already getting
213 	 * unexpected results.
214 	 *
215 	 * _res.options is tricky since some apps were known to diddle the bits
216 	 * before res_init() was first called. We can't replicate that semantic
217 	 * with dynamic initialization (they may have turned bits off that are
218 	 * set in RES_DEFAULT).  Our solution is to declare such applications
219 	 * "broken".  They could fool us by setting RES_INIT but none do (yet).
220 	 */
221 	if (!_resp->retrans)
222 		_resp->retrans = RES_TIMEOUT;
223 	if (!_resp->retry)
224 		_resp->retry = 4;
225 	if (!(_resp->options & RES_INIT))
226 		_resp->options = RES_DEFAULT;
227 
228 #ifdef USELOOPBACK
229 	_resp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
230 #else
231 	_resp->nsaddr.sin_addr.s_addr = INADDR_ANY;
232 #endif
233 	_resp->nsaddr.sin_family = AF_INET;
234 	_resp->nsaddr.sin_port = htons(NAMESERVER_PORT);
235 	_resp->nsaddr.sin_len = sizeof(struct sockaddr_in);
236 #ifdef INET6
237 	if (sizeof(_res_extp->nsaddr) >= sizeof(struct sockaddr_in))
238 		memcpy(&_res_extp->nsaddr, &_resp->nsaddr, sizeof(struct sockaddr_in));
239 #endif
240 	_resp->nscount = 1;
241 	_resp->ndots = 1;
242 	_resp->pfcode = 0;
243 	strlcpy(_resp->lookups, "f", sizeof _resp->lookups);
244 
245 	/* Allow user to override the local domain definition */
246 	if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
247 		strlcpy(_resp->defdname, cp, sizeof(_resp->defdname));
248 		haveenv++;
249 
250 		/*
251 		 * Set search list to be blank-separated strings
252 		 * from rest of env value.  Permits users of LOCALDOMAIN
253 		 * to still have a search list, and anyone to set the
254 		 * one that they want to use as an individual (even more
255 		 * important now that the rfc1535 stuff restricts searches)
256 		 */
257 		cp = _resp->defdname;
258 		pp = _resp->dnsrch;
259 		*pp++ = cp;
260 		for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) {
261 			if (*cp == '\n')	/* silly backwards compat */
262 				break;
263 			else if (*cp == ' ' || *cp == '\t') {
264 				*cp = 0;
265 				n = 1;
266 			} else if (n) {
267 				*pp++ = cp;
268 				n = 0;
269 				havesearch = 1;
270 			}
271 		}
272 		/* null terminate last domain if there are excess */
273 		while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
274 			cp++;
275 		*cp = '\0';
276 		*pp++ = 0;
277 	}
278 
279 #define	MATCH(line, name) \
280 	(!strncmp(line, name, sizeof(name) - 1) && \
281 	(line[sizeof(name) - 1] == ' ' || \
282 	 line[sizeof(name) - 1] == '\t'))
283 
284 	if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
285 	    strlcpy(_resp->lookups, "bf", sizeof _resp->lookups);
286 
287 	    /* read the config file */
288 	    buf[0] = '\0';
289 	    while ((cp = fgetln(fp, &len)) != NULL) {
290 		/* skip lines that are too long or zero length */
291 		if (len >= sizeof(buf) || len == 0)
292 		    continue;
293 		(void)memcpy(buf, cp, len);
294 		buf[len] = '\0';
295 		/* skip comments */
296 		if ((cp = strpbrk(buf, ";#")) != NULL)
297 			*cp = '\0';
298 		if (buf[0] == '\0')
299 			continue;
300 		/* read default domain name */
301 		if (MATCH(buf, "domain")) {
302 		    if (haveenv)	/* skip if have from environ */
303 			    continue;
304 		    cp = buf + sizeof("domain") - 1;
305 		    while (*cp == ' ' || *cp == '\t')
306 			    cp++;
307 		    if ((*cp == '\0') || (*cp == '\n'))
308 			    continue;
309 		    strlcpy(_resp->defdname, cp, sizeof(_resp->defdname));
310 		    if ((cp = strpbrk(_resp->defdname, " \t\n")) != NULL)
311 			    *cp = '\0';
312 		    havesearch = 0;
313 		    continue;
314 		}
315 		/* lookup types */
316 		if (MATCH(buf, "lookup")) {
317 		    char *sp = NULL;
318 
319 		    memset(_resp->lookups, 0, sizeof _resp->lookups);
320 		    cp = buf + sizeof("lookup") - 1;
321 		    for (n = 0;; cp++) {
322 		    	    if (n == MAXDNSLUS)
323 				    break;
324 			    if ((*cp == '\0') || (*cp == '\n')) {
325 				    if (sp) {
326 					    if (*sp=='y' || *sp=='b' || *sp=='f')
327 						    _resp->lookups[n++] = *sp;
328 					    sp = NULL;
329 				    }
330 				    break;
331 			    } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) {
332 				    if (sp) {
333 					    if (*sp=='y' || *sp=='b' || *sp=='f')
334 						    _resp->lookups[n++] = *sp;
335 					    sp = NULL;
336 				    }
337 			    } else if (sp == NULL)
338 				    sp = cp;
339 		    }
340 		    continue;
341 		}
342 		/* set search list */
343 		if (MATCH(buf, "search")) {
344 		    if (haveenv)	/* skip if have from environ */
345 			    continue;
346 		    cp = buf + sizeof("search") - 1;
347 		    while (*cp == ' ' || *cp == '\t')
348 			    cp++;
349 		    if ((*cp == '\0') || (*cp == '\n'))
350 			    continue;
351 		    strlcpy(_resp->defdname, cp, sizeof(_resp->defdname));
352 		    if ((cp = strchr(_resp->defdname, '\n')) != NULL)
353 			    *cp = '\0';
354 		    /*
355 		     * Set search list to be blank-separated strings
356 		     * on rest of line.
357 		     */
358 		    cp = _resp->defdname;
359 		    pp = _resp->dnsrch;
360 		    *pp++ = cp;
361 		    for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) {
362 			    if (*cp == ' ' || *cp == '\t') {
363 				    *cp = 0;
364 				    n = 1;
365 			    } else if (n) {
366 				    *pp++ = cp;
367 				    n = 0;
368 			    }
369 		    }
370 		    /* null terminate last domain if there are excess */
371 		    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
372 			    cp++;
373 		    *cp = '\0';
374 		    *pp++ = 0;
375 		    havesearch = 1;
376 		    continue;
377 		}
378 		/* read nameservers to query */
379 		if (MATCH(buf, "nameserver") && nserv < MAXNS) {
380 #ifdef INET6
381 		    char *q;
382 		    struct addrinfo hints, *res;
383 		    char pbuf[NI_MAXSERV];
384 #else
385 		    struct in_addr a;
386 #endif /* INET6 */
387 
388 		    cp = buf + sizeof("nameserver") - 1;
389 		    while (*cp == ' ' || *cp == '\t')
390 			cp++;
391 #ifdef INET6
392 		    if ((*cp == '\0') || (*cp == '\n'))
393 			continue;
394 		    for (q = cp; *q; q++) {
395 			if (isspace(*q)) {
396 			    *q = '\0';
397 			    break;
398 			}
399 		    }
400 		    memset(&hints, 0, sizeof(hints));
401 		    hints.ai_flags = AI_NUMERICHOST;
402 		    hints.ai_socktype = SOCK_DGRAM;
403 		    snprintf(pbuf, sizeof(pbuf), "%u", NAMESERVER_PORT);
404 		    res = NULL;
405 		    if (getaddrinfo(cp, pbuf, &hints, &res) == 0 &&
406 			    res->ai_next == NULL) {
407 			if (res->ai_addrlen <= sizeof(_res_extp->nsaddr_list[nserv])) {
408 			    memcpy(&_res_extp->nsaddr_list[nserv], res->ai_addr,
409 				res->ai_addrlen);
410 			} else {
411 			    memset(&_res_extp->nsaddr_list[nserv], 0,
412 				sizeof(_res_extp->nsaddr_list[nserv]));
413 			}
414 			if (res->ai_addrlen <= sizeof(_resp->nsaddr_list[nserv])) {
415 			    memcpy(&_resp->nsaddr_list[nserv], res->ai_addr,
416 				res->ai_addrlen);
417 			} else {
418 			    memset(&_resp->nsaddr_list[nserv], 0,
419 				sizeof(_resp->nsaddr_list[nserv]));
420 			}
421 			nserv++;
422 		    }
423 		    if (res)
424 			freeaddrinfo(res);
425 #else /* INET6 */
426 		    if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
427 			_resp->nsaddr_list[nserv].sin_addr = a;
428 			_resp->nsaddr_list[nserv].sin_family = AF_INET;
429 			_resp->nsaddr_list[nserv].sin_port =
430 				htons(NAMESERVER_PORT);
431 			_resp->nsaddr_list[nserv].sin_len =
432 				sizeof(struct sockaddr_in);
433 			nserv++;
434 		    }
435 #endif /* INET6 */
436 		    continue;
437 		}
438 #ifdef RESOLVSORT
439 		if (MATCH(buf, "sortlist")) {
440 		    struct in_addr a;
441 #ifdef INET6
442 		    struct in6_addr a6;
443 		    int m, i;
444 		    u_char *u;
445 #endif /* INET6 */
446 
447 		    cp = buf + sizeof("sortlist") - 1;
448 		    while (nsort < MAXRESOLVSORT) {
449 			while (*cp == ' ' || *cp == '\t')
450 			    cp++;
451 			if (*cp == '\0' || *cp == '\n' || *cp == ';')
452 			    break;
453 			net = cp;
454 			while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
455 			       isascii(*cp) && !isspace(*cp))
456 				cp++;
457 			n = *cp;
458 			*cp = 0;
459 			if (inet_aton(net, &a)) {
460 			    _resp->sort_list[nsort].addr = a;
461 			    if (ISSORTMASK(n)) {
462 				*cp++ = n;
463 				net = cp;
464 				while (*cp && *cp != ';' &&
465 					isascii(*cp) && !isspace(*cp))
466 				    cp++;
467 				n = *cp;
468 				*cp = 0;
469 				if (inet_aton(net, &a)) {
470 				    _resp->sort_list[nsort].mask = a.s_addr;
471 				} else {
472 				    _resp->sort_list[nsort].mask =
473 					net_mask(_resp->sort_list[nsort].addr);
474 				}
475 			    } else {
476 				_resp->sort_list[nsort].mask =
477 				    net_mask(_resp->sort_list[nsort].addr);
478 			    }
479 #ifdef INET6
480 			    _res_extp->sort_list[nsort].af = AF_INET;
481 			    _res_extp->sort_list[nsort].addr.ina =
482 				_resp->sort_list[nsort].addr;
483 			    _res_extp->sort_list[nsort].mask.ina.s_addr =
484 				_resp->sort_list[nsort].mask;
485 #endif /* INET6 */
486 			    nsort++;
487 			}
488 #ifdef INET6
489 			else if (inet_pton(AF_INET6, net, &a6) == 1) {
490 			    _res_extp->sort_list[nsort].af = AF_INET6;
491 			    _res_extp->sort_list[nsort].addr.in6a = a6;
492 			    u = (u_char *)&_res_extp->sort_list[nsort].mask.in6a;
493 			    *cp++ = n;
494 			    net = cp;
495 			    while (*cp && *cp != ';' &&
496 				    isascii(*cp) && !isspace(*cp))
497 				cp++;
498 			    m = n;
499 			    n = *cp;
500 			    *cp = 0;
501 			    switch (m) {
502 			    case '/':
503 				m = atoi(net);
504 				break;
505 			    case '&':
506 				if (inet_pton(AF_INET6, net, u) == 1) {
507 				    m = -1;
508 				    break;
509 				}
510 				/*FALLTHRU*/
511 			    default:
512 				m = sizeof(struct in6_addr) * NBBY;
513 				break;
514 			    }
515 			    if (m >= 0) {
516 				for (i = 0; i < sizeof(struct in6_addr); i++) {
517 				    if (m <= 0) {
518 					*u = 0;
519 				    } else {
520 					m -= NBBY;
521 					*u = (u_char)~0;
522 					if (m < 0)
523 					    *u <<= -m;
524 				    }
525 				    u++;
526 				}
527 			    }
528 			    nsort++;
529 			}
530 #endif /* INET6 */
531 			*cp = n;
532 		    }
533 		    continue;
534 		}
535 #endif
536 		if (MATCH(buf, "options")) {
537 		    res_setoptions(buf + sizeof("options") - 1, "conf");
538 		    continue;
539 		}
540 	    }
541 	    if (nserv > 1)
542 		_resp->nscount = nserv;
543 #ifdef RESOLVSORT
544 	    _resp->nsort = nsort;
545 #endif
546 	    (void) fclose(fp);
547 	}
548 	if (_resp->defdname[0] == 0 &&
549 	    gethostname(buf, sizeof(_resp->defdname) - 1) == 0 &&
550 	    (cp = strchr(buf, '.')) != NULL)
551 	{
552 		strlcpy(_resp->defdname, cp + 1,
553 		        sizeof(_resp->defdname));
554 	}
555 
556 	/* find components of local domain that might be searched */
557 	if (havesearch == 0) {
558 		pp = _resp->dnsrch;
559 		*pp++ = _resp->defdname;
560 		*pp = NULL;
561 
562 #ifndef RFC1535
563 		dots = 0;
564 		for (cp = _resp->defdname; *cp; cp++)
565 			dots += (*cp == '.');
566 
567 		cp = _resp->defdname;
568 		while (pp < _resp->dnsrch + MAXDFLSRCH) {
569 			if (dots < LOCALDOMAINPARTS)
570 				break;
571 			cp = strchr(cp, '.') + 1;    /* we know there is one */
572 			*pp++ = cp;
573 			dots--;
574 		}
575 		*pp = NULL;
576 #ifdef DEBUG
577 		if (_resp->options & RES_DEBUG) {
578 			printf(";; res_init()... default dnsrch list:\n");
579 			for (pp = _resp->dnsrch; *pp; pp++)
580 				printf(";;\t%s\n", *pp);
581 			printf(";;\t..END..\n");
582 		}
583 #endif /* DEBUG */
584 #endif /* !RFC1535 */
585 	}
586 
587 	if (issetugid())
588 		_resp->options |= RES_NOALIASES;
589 	else if ((cp = getenv("RES_OPTIONS")) != NULL)
590 		res_setoptions(cp, "env");
591 	_resp->options |= RES_INIT;
592 	return (0);
593 }
594 
595 /* ARGSUSED */
596 static void
res_setoptions(char * options,char * source)597 res_setoptions(char *options, char *source)
598 {
599 	struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
600 	char *cp = options;
601 	char *endp;
602 	long l;
603 
604 #ifdef DEBUG
605 	if (_resp->options & RES_DEBUG)
606 		printf(";; res_setoptions(\"%s\", \"%s\")...\n",
607 		       options, source);
608 #endif
609 	while (*cp) {
610 		/* skip leading and inner runs of spaces */
611 		while (*cp == ' ' || *cp == '\t')
612 			cp++;
613 		/* search for and process individual options */
614 		if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
615 			char *p = cp + sizeof("ndots:") - 1;
616 			l = strtol(p, &endp, 10);
617 			if (l >= 0 && endp != p &&
618 			    (*endp = '\0' || isspace(*endp))) {
619 				if (l <= RES_MAXNDOTS)
620 					_resp->ndots = l;
621 				else
622 					_resp->ndots = RES_MAXNDOTS;
623 #ifdef DEBUG
624 				if (_resp->options & RES_DEBUG)
625 					printf(";;\tndots=%u\n", _resp->ndots);
626 #endif
627 			}
628 		} else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
629 #ifdef DEBUG
630 			if (!(_resp->options & RES_DEBUG)) {
631 				printf(";; res_setoptions(\"%s\", \"%s\")..\n",
632 				       options, source);
633 				_resp->options |= RES_DEBUG;
634 			}
635 			printf(";;\tdebug\n");
636 #endif
637 		} else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
638 			_resp->options |= RES_USE_INET6;
639 		} else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
640 			_resp->options |= RES_INSECURE1;
641 		} else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
642 			_resp->options |= RES_INSECURE2;
643 		} else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
644 			_resp->options |= RES_USE_EDNS0;
645 		} else {
646 			/* XXX - print a warning here? */
647 		}
648 		/* skip to next run of spaces */
649 		while (*cp && *cp != ' ' && *cp != '\t')
650 			cp++;
651 	}
652 }
653 
654 #ifdef RESOLVSORT
655 /* XXX - should really support CIDR which means explicit masks always. */
656 static u_int32_t
net_mask(struct in_addr in)657 net_mask(struct in_addr in)	/* XXX - should really use system's version of this */
658 {
659 	u_int32_t i = ntohl(in.s_addr);
660 
661 	if (IN_CLASSA(i))
662 		return (htonl(IN_CLASSA_NET));
663 	else if (IN_CLASSB(i))
664 		return (htonl(IN_CLASSB_NET));
665 	return (htonl(IN_CLASSC_NET));
666 }
667 #endif
668