Home | History | Annotate | Line # | Download | only in resolv
res_init.c revision 1.8.4.3
      1 /*	$NetBSD: res_init.c,v 1.8.4.3 2011/01/23 21:47:51 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, 1989, 1993
      5  *    The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  * 	This product includes software developed by the University of
     18  * 	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     38  *
     39  * Permission to use, copy, modify, and distribute this software for any
     40  * purpose with or without fee is hereby granted, provided that the above
     41  * copyright notice and this permission notice appear in all copies, and that
     42  * the name of Digital Equipment Corporation not be used in advertising or
     43  * publicity pertaining to distribution of the document or software without
     44  * specific, written prior permission.
     45  *
     46  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     47  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     48  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     49  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     50  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     51  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     52  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     53  * SOFTWARE.
     54  */
     55 
     56 /*
     57  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     58  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     59  *
     60  * Permission to use, copy, modify, and distribute this software for any
     61  * purpose with or without fee is hereby granted, provided that the above
     62  * copyright notice and this permission notice appear in all copies.
     63  *
     64  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     65  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     66  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     67  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     68  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     69  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     70  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #if defined(LIBC_SCCS) && !defined(lint)
     75 #ifdef notdef
     76 static const char sccsid[] = "@(#)res_init.c	8.1 (Berkeley) 6/7/93";
     77 static const char rcsid[] = "Id: res_init.c,v 1.16.18.7 2007/07/09 01:52:58 mar
     78 ka Exp";
     79 #else
     80 __RCSID("$NetBSD: res_init.c,v 1.8.4.3 2011/01/23 21:47:51 bouyer Exp $");
     81 #endif
     82 #endif /* LIBC_SCCS and not lint */
     83 
     84 #include "port_before.h"
     85 
     86 #include "namespace.h"
     87 #include <sys/types.h>
     88 #include <sys/param.h>
     89 #include <sys/socket.h>
     90 #include <sys/time.h>
     91 
     92 #include <netinet/in.h>
     93 #include <arpa/inet.h>
     94 #include <arpa/nameser.h>
     95 
     96 #include <ctype.h>
     97 #include <stdio.h>
     98 #include <stdlib.h>
     99 #include <string.h>
    100 #include <unistd.h>
    101 #include <netdb.h>
    102 
    103 #include "port_after.h"
    104 
    105 #if 0
    106 #ifdef __weak_alias
    107 __weak_alias(res_ninit,_res_ninit)
    108 __weak_alias(res_randomid,__res_randomid)
    109 __weak_alias(res_nclose,_res_nclose)
    110 __weak_alias(res_ndestroy,_res_ndestroy)
    111 __weak_alias(res_get_nibblesuffix,__res_get_nibblesuffix)
    112 __weak_alias(res_get_nibblesuffix2,__res_get_nibblesuffix2)
    113 __weak_alias(res_getservers,__res_getservers)
    114 __weak_alias(res_setservers,__res_setservers)
    115 #endif
    116 #endif
    117 
    118 
    119 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
    120 #include <resolv.h>
    121 
    122 #include "res_private.h"
    123 
    124 /*% Options.  Should all be left alone. */
    125 #ifndef DEBUG
    126 #define RESOLVSORT
    127 #define DEBUG
    128 #endif
    129 
    130 #ifdef SOLARIS2
    131 #include <sys/systeminfo.h>
    132 #endif
    133 
    134 static void res_setoptions __P((res_state, const char *, const char *));
    135 
    136 #ifdef RESOLVSORT
    137 static const char sort_mask[] = "/&";
    138 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
    139 static u_int32_t net_mask __P((struct in_addr));
    140 #endif
    141 
    142 #if !defined(isascii)	/*%< XXX - could be a function */
    143 # define isascii(c) (!(c & 0200))
    144 #endif
    145 
    146 /*
    147  * Resolver state default settings.
    148  */
    149 
    150 /*%
    151  * Set up default settings.  If the configuration file exist, the values
    152  * there will have precedence.  Otherwise, the server address is set to
    153  * INADDR_ANY and the default domain name comes from the gethostname().
    154  *
    155  * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
    156  * rather than INADDR_ANY ("0.0.0.0") as the default name server address
    157  * since it was noted that INADDR_ANY actually meant ``the first interface
    158  * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
    159  * it had to be "up" in order for you to reach your own name server.  It
    160  * was later decided that since the recommended practice is to always
    161  * install local static routes through 127.0.0.1 for all your network
    162  * interfaces, that we could solve this problem without a code change.
    163  *
    164  * The configuration file should always be used, since it is the only way
    165  * to specify a default domain.  If you are running a server on your local
    166  * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
    167  * in the configuration file.
    168  *
    169  * Return 0 if completes successfully, -1 on error
    170  */
    171 int
    172 res_ninit(res_state statp) {
    173 	extern int __res_vinit(res_state, int);
    174 
    175 	return (__res_vinit(statp, 0));
    176 }
    177 
    178 /*% This function has to be reachable by res_data.c but not publically. */
    179 int
    180 __res_vinit(res_state statp, int preinit) {
    181 	register FILE *fp;
    182 	register char *cp, **pp;
    183 	register int n;
    184 	char buf[BUFSIZ];
    185 	int nserv = 0;    /*%< number of nameserver records read from file */
    186 	int haveenv = 0;
    187 	int havesearch = 0;
    188 #ifdef RESOLVSORT
    189 	int nsort = 0;
    190 	char *net;
    191 #endif
    192 	int dots;
    193 	union res_sockaddr_union u[2];
    194 	int maxns = MAXNS;
    195 
    196 	RES_SET_H_ERRNO(statp, 0);
    197 	if (statp->_u._ext.ext != NULL)
    198 		res_ndestroy(statp);
    199 
    200 	if (!preinit) {
    201 		statp->retrans = RES_TIMEOUT;
    202 		statp->retry = RES_DFLRETRY;
    203 		statp->options = RES_DEFAULT;
    204 		statp->id = res_randomid();
    205 	}
    206 
    207 	if ((statp->options & RES_INIT) != 0U)
    208 		res_ndestroy(statp);
    209 
    210 	memset(u, 0, sizeof(u));
    211 #ifdef USELOOPBACK
    212 	u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
    213 #else
    214 	u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
    215 #endif
    216 	u[nserv].sin.sin_family = AF_INET;
    217 	u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
    218 #ifdef HAVE_SA_LEN
    219 	u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
    220 #endif
    221 	nserv++;
    222 #ifdef HAS_INET6_STRUCTS
    223 #ifdef USELOOPBACK
    224 	u[nserv].sin6.sin6_addr = in6addr_loopback;
    225 #else
    226 	u[nserv].sin6.sin6_addr = in6addr_any;
    227 #endif
    228 	u[nserv].sin6.sin6_family = AF_INET6;
    229 	u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
    230 #ifdef HAVE_SA_LEN
    231 	u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
    232 #endif
    233 	nserv++;
    234 #endif
    235 	statp->nscount = 0;
    236 	statp->ndots = 1;
    237 	statp->pfcode = 0;
    238 	statp->_vcsock = -1;
    239 	statp->_flags = 0;
    240 	statp->qhook = NULL;
    241 	statp->rhook = NULL;
    242 	statp->_u._ext.nscount = 0;
    243 	statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
    244 	if (statp->_u._ext.ext != NULL) {
    245 	        memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
    246 		statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
    247 		strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
    248 		strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
    249 	} else {
    250 		/*
    251 		* Historically res_init() rarely, if at all, failed.
    252 		* Examples and applications exist which do not check
    253 		* our return code.  Furthermore several applications
    254 		* simply call us to get the systems domainname.  So
    255 		* rather then immediately fail here we store the
    256 		* failure, which is returned later, in h_errno.  And
    257 		* prevent the collection of 'nameserver' information
    258 		* by setting maxns to 0.  Thus applications that fail
    259 		* to check our return code wont be able to make
    260 		* queries anyhow.
    261 		*/
    262 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
    263 		maxns = 0;
    264 	}
    265 #ifdef RESOLVSORT
    266 	statp->nsort = 0;
    267 #endif
    268 	res_setservers(statp, u, nserv);
    269 
    270 #ifdef	SOLARIS2
    271 	/*
    272 	 * The old libresolv derived the defaultdomain from NIS/NIS+.
    273 	 * We want to keep this behaviour
    274 	 */
    275 	{
    276 		char buf[sizeof(statp->defdname)], *cp;
    277 		int ret;
    278 
    279 		if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
    280 			(unsigned int)ret <= sizeof(buf)) {
    281 			if (buf[0] == '+')
    282 				buf[0] = '.';
    283 			cp = strchr(buf, '.');
    284 			cp = (cp == NULL) ? buf : (cp + 1);
    285 			strncpy(statp->defdname, cp,
    286 				sizeof(statp->defdname) - 1);
    287 			statp->defdname[sizeof(statp->defdname) - 1] = '\0';
    288 		}
    289 	}
    290 #endif	/* SOLARIS2 */
    291 
    292 	/* Allow user to override the local domain definition */
    293 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
    294 		(void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
    295 		statp->defdname[sizeof(statp->defdname) - 1] = '\0';
    296 		haveenv++;
    297 
    298 		/*
    299 		 * Set search list to be blank-separated strings
    300 		 * from rest of env value.  Permits users of LOCALDOMAIN
    301 		 * to still have a search list, and anyone to set the
    302 		 * one that they want to use as an individual (even more
    303 		 * important now that the rfc1535 stuff restricts searches)
    304 		 */
    305 		cp = statp->defdname;
    306 		pp = statp->dnsrch;
    307 		*pp++ = cp;
    308 		for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
    309 			if (*cp == '\n')	/*%< silly backwards compat */
    310 				break;
    311 			else if (*cp == ' ' || *cp == '\t') {
    312 				*cp = 0;
    313 				n = 1;
    314 			} else if (n) {
    315 				*pp++ = cp;
    316 				n = 0;
    317 				havesearch = 1;
    318 			}
    319 		}
    320 		/* null terminate last domain if there are excess */
    321 		while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
    322 			cp++;
    323 		*cp = '\0';
    324 		*pp++ = 0;
    325 	}
    326 
    327 #define	MATCH(line, name) \
    328 	(!strncmp(line, name, sizeof(name) - 1) && \
    329 	(line[sizeof(name) - 1] == ' ' || \
    330 	 line[sizeof(name) - 1] == '\t'))
    331 
    332 	nserv = 0;
    333 	if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
    334 	    /* read the config file */
    335 	    while (fgets(buf, sizeof(buf), fp) != NULL) {
    336 		/* skip comments */
    337 		if (*buf == ';' || *buf == '#')
    338 			continue;
    339 		/* read default domain name */
    340 		if (MATCH(buf, "domain")) {
    341 		    if (haveenv)	/*%< skip if have from environ */
    342 			    continue;
    343 		    cp = buf + sizeof("domain") - 1;
    344 		    while (*cp == ' ' || *cp == '\t')
    345 			    cp++;
    346 		    if ((*cp == '\0') || (*cp == '\n'))
    347 			    continue;
    348 		    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
    349 		    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
    350 		    if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
    351 			    *cp = '\0';
    352 		    havesearch = 0;
    353 		    continue;
    354 		}
    355 		/* set search list */
    356 		if (MATCH(buf, "search")) {
    357 		    if (haveenv)	/*%< skip if have from environ */
    358 			    continue;
    359 		    cp = buf + sizeof("search") - 1;
    360 		    while (*cp == ' ' || *cp == '\t')
    361 			    cp++;
    362 		    if ((*cp == '\0') || (*cp == '\n'))
    363 			    continue;
    364 		    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
    365 		    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
    366 		    if ((cp = strchr(statp->defdname, '\n')) != NULL)
    367 			    *cp = '\0';
    368 		    /*
    369 		     * Set search list to be blank-separated strings
    370 		     * on rest of line.
    371 		     */
    372 		    cp = statp->defdname;
    373 		    pp = statp->dnsrch;
    374 		    *pp++ = cp;
    375 		    for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
    376 			    if (*cp == ' ' || *cp == '\t') {
    377 				    *cp = 0;
    378 				    n = 1;
    379 			    } else if (n) {
    380 				    *pp++ = cp;
    381 				    n = 0;
    382 			    }
    383 		    }
    384 		    /* null terminate last domain if there are excess */
    385 		    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
    386 			    cp++;
    387 		    *cp = '\0';
    388 		    *pp++ = 0;
    389 		    havesearch = 1;
    390 		    continue;
    391 		}
    392 		/* read nameservers to query */
    393 		if (MATCH(buf, "nameserver") && nserv < maxns) {
    394 		    struct addrinfo hints, *ai;
    395 		    char sbuf[NI_MAXSERV];
    396 		    const size_t minsiz =
    397 		        sizeof(statp->_u._ext.ext->nsaddrs[0]);
    398 
    399 		    cp = buf + sizeof("nameserver") - 1;
    400 		    while (*cp == ' ' || *cp == '\t')
    401 			cp++;
    402 		    cp[strcspn(cp, ";# \t\n")] = '\0';
    403 		    if ((*cp != '\0') && (*cp != '\n')) {
    404 			memset(&hints, 0, sizeof(hints));
    405 			hints.ai_family = PF_UNSPEC;
    406 			hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    407 			hints.ai_flags = AI_NUMERICHOST;
    408 			sprintf(sbuf, "%u", NAMESERVER_PORT);
    409 			if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
    410 			    ai->ai_addrlen <= minsiz) {
    411 			    if (statp->_u._ext.ext != NULL) {
    412 				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
    413 				    ai->ai_addr, ai->ai_addrlen);
    414 			    }
    415 			    if (ai->ai_addrlen <=
    416 			        sizeof(statp->nsaddr_list[nserv])) {
    417 				memcpy(&statp->nsaddr_list[nserv],
    418 				    ai->ai_addr, ai->ai_addrlen);
    419 			    } else
    420 				statp->nsaddr_list[nserv].sin_family = 0;
    421 			    freeaddrinfo(ai);
    422 			    nserv++;
    423 			}
    424 		    }
    425 		    continue;
    426 		}
    427 #ifdef RESOLVSORT
    428 		if (MATCH(buf, "sortlist")) {
    429 		    struct in_addr a;
    430 
    431 		    cp = buf + sizeof("sortlist") - 1;
    432 		    while (nsort < MAXRESOLVSORT) {
    433 			while (*cp == ' ' || *cp == '\t')
    434 			    cp++;
    435 			if (*cp == '\0' || *cp == '\n' || *cp == ';')
    436 			    break;
    437 			net = cp;
    438 			while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
    439 			       isascii(*cp) && !isspace((unsigned char)*cp))
    440 				cp++;
    441 			n = *cp;
    442 			*cp = 0;
    443 			if (inet_aton(net, &a)) {
    444 			    statp->sort_list[nsort].addr = a;
    445 			    if (ISSORTMASK(n)) {
    446 				*cp++ = n;
    447 				net = cp;
    448 				while (*cp && *cp != ';' &&
    449 					isascii(*cp) &&
    450 					!isspace((unsigned char)*cp))
    451 				    cp++;
    452 				n = *cp;
    453 				*cp = 0;
    454 				if (inet_aton(net, &a)) {
    455 				    statp->sort_list[nsort].mask = a.s_addr;
    456 				} else {
    457 				    statp->sort_list[nsort].mask =
    458 					net_mask(statp->sort_list[nsort].addr);
    459 				}
    460 			    } else {
    461 				statp->sort_list[nsort].mask =
    462 				    net_mask(statp->sort_list[nsort].addr);
    463 			    }
    464 			    nsort++;
    465 			}
    466 			*cp = n;
    467 		    }
    468 		    continue;
    469 		}
    470 #endif
    471 		if (MATCH(buf, "options")) {
    472 		    res_setoptions(statp, buf + sizeof("options") - 1, "conf");
    473 		    continue;
    474 		}
    475 	    }
    476 	    if (nserv > 0)
    477 		statp->nscount = nserv;
    478 #ifdef RESOLVSORT
    479 	    statp->nsort = nsort;
    480 #endif
    481 	    (void) fclose(fp);
    482 	}
    483 /*
    484  * Last chance to get a nameserver.  This should not normally
    485  * be necessary
    486  */
    487 #ifdef NO_RESOLV_CONF
    488 	if(nserv == 0)
    489 		nserv = get_nameservers(statp);
    490 #endif
    491 
    492 	if (statp->defdname[0] == 0 &&
    493 	    gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
    494 	    (cp = strchr(buf, '.')) != NULL)
    495 		strcpy(statp->defdname, cp + 1);
    496 
    497 	/* find components of local domain that might be searched */
    498 	if (havesearch == 0) {
    499 		pp = statp->dnsrch;
    500 		*pp++ = statp->defdname;
    501 		*pp = NULL;
    502 
    503 		dots = 0;
    504 		for (cp = statp->defdname; *cp; cp++)
    505 			dots += (*cp == '.');
    506 
    507 		cp = statp->defdname;
    508 		while (pp < statp->dnsrch + MAXDFLSRCH) {
    509 			if (dots < LOCALDOMAINPARTS)
    510 				break;
    511 			cp = strchr(cp, '.') + 1;    /*%< we know there is one */
    512 			*pp++ = cp;
    513 			dots--;
    514 		}
    515 		*pp = NULL;
    516 #ifdef DEBUG
    517 		if (statp->options & RES_DEBUG) {
    518 			printf(";; res_init()... default dnsrch list:\n");
    519 			for (pp = statp->dnsrch; *pp; pp++)
    520 				printf(";;\t%s\n", *pp);
    521 			printf(";;\t..END..\n");
    522 		}
    523 #endif
    524 	}
    525 
    526 	if ((cp = getenv("RES_OPTIONS")) != NULL)
    527 		res_setoptions(statp, cp, "env");
    528 	statp->options |= RES_INIT;
    529 	return (statp->res_h_errno);
    530 }
    531 
    532 static void
    533 res_setoptions(res_state statp, const char *options, const char *source)
    534 {
    535 	const char *cp = options;
    536 	int i;
    537 	struct __res_state_ext *ext = statp->_u._ext.ext;
    538 
    539 #ifdef DEBUG
    540 	if (statp->options & RES_DEBUG)
    541 		printf(";; res_setoptions(\"%s\", \"%s\")...\n",
    542 		       options, source);
    543 #endif
    544 	while (*cp) {
    545 		/* skip leading and inner runs of spaces */
    546 		while (*cp == ' ' || *cp == '\t')
    547 			cp++;
    548 		/* search for and process individual options */
    549 		if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
    550 			i = atoi(cp + sizeof("ndots:") - 1);
    551 			if (i <= RES_MAXNDOTS)
    552 				statp->ndots = i;
    553 			else
    554 				statp->ndots = RES_MAXNDOTS;
    555 #ifdef DEBUG
    556 			if (statp->options & RES_DEBUG)
    557 				printf(";;\tndots=%d\n", statp->ndots);
    558 #endif
    559 		} else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
    560 			i = atoi(cp + sizeof("timeout:") - 1);
    561 			if (i <= RES_MAXRETRANS)
    562 				statp->retrans = i;
    563 			else
    564 				statp->retrans = RES_MAXRETRANS;
    565 #ifdef DEBUG
    566 			if (statp->options & RES_DEBUG)
    567 				printf(";;\ttimeout=%d\n", statp->retrans);
    568 #endif
    569 #ifdef	SOLARIS2
    570 		} else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
    571 			/*
    572 		 	 * For backward compatibility, 'retrans' is
    573 		 	 * supported as an alias for 'timeout', though
    574 		 	 * without an imposed maximum.
    575 		 	 */
    576 			statp->retrans = atoi(cp + sizeof("retrans:") - 1);
    577 		} else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
    578 			/*
    579 			 * For backward compatibility, 'retry' is
    580 			 * supported as an alias for 'attempts', though
    581 			 * without an imposed maximum.
    582 			 */
    583 			statp->retry = atoi(cp + sizeof("retry:") - 1);
    584 #endif	/* SOLARIS2 */
    585 		} else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
    586 			i = atoi(cp + sizeof("attempts:") - 1);
    587 			if (i <= RES_MAXRETRY)
    588 				statp->retry = i;
    589 			else
    590 				statp->retry = RES_MAXRETRY;
    591 #ifdef DEBUG
    592 			if (statp->options & RES_DEBUG)
    593 				printf(";;\tattempts=%d\n", statp->retry);
    594 #endif
    595 		} else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
    596 #ifdef DEBUG
    597 			if (!(statp->options & RES_DEBUG)) {
    598 				printf(";; res_setoptions(\"%s\", \"%s\")..\n",
    599 				       options, source);
    600 				statp->options |= RES_DEBUG;
    601 			}
    602 			printf(";;\tdebug\n");
    603 #endif
    604 		} else if (!strncmp(cp, "no_tld_query",
    605 				    sizeof("no_tld_query") - 1) ||
    606 			   !strncmp(cp, "no-tld-query",
    607 				    sizeof("no-tld-query") - 1)) {
    608 			statp->options |= RES_NOTLDQUERY;
    609 		} else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
    610 			statp->options |= RES_USE_INET6;
    611 		} else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
    612 			statp->options |= RES_ROTATE;
    613 		} else if (!strncmp(cp, "no-check-names",
    614 				    sizeof("no-check-names") - 1)) {
    615 			statp->options |= RES_NOCHECKNAME;
    616 		}
    617 #ifdef RES_USE_EDNS0
    618 		else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
    619 			statp->options |= RES_USE_EDNS0;
    620 		}
    621 #endif
    622 		else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
    623 			statp->options |= RES_USE_DNAME;
    624 		}
    625 		else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
    626 			if (ext == NULL)
    627 				goto skip;
    628 			cp += sizeof("nibble:") - 1;
    629 			i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
    630 			strncpy(ext->nsuffix, cp, (size_t)i);
    631 			ext->nsuffix[i] = '\0';
    632 		}
    633 		else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
    634 			if (ext == NULL)
    635 				goto skip;
    636 			cp += sizeof("nibble2:") - 1;
    637 			i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
    638 			strncpy(ext->nsuffix2, cp, (size_t)i);
    639 			ext->nsuffix2[i] = '\0';
    640 		}
    641 		else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
    642 			cp += sizeof("v6revmode:") - 1;
    643 			/* "nibble" and "bitstring" used to be valid */
    644 			if (!strncmp(cp, "single", sizeof("single") - 1)) {
    645 				statp->options |= RES_NO_NIBBLE2;
    646 			} else if (!strncmp(cp, "both", sizeof("both") - 1)) {
    647 				statp->options &=
    648 					 ~RES_NO_NIBBLE2;
    649 			}
    650 		}
    651 		else {
    652 			/* XXX - print a warning here? */
    653 		}
    654    skip:
    655 		/* skip to next run of spaces */
    656 		while (*cp && *cp != ' ' && *cp != '\t')
    657 			cp++;
    658 	}
    659 }
    660 
    661 #ifdef RESOLVSORT
    662 /* XXX - should really support CIDR which means explicit masks always. */
    663 static u_int32_t
    664 net_mask(in)		/*!< XXX - should really use system's version of this  */
    665 	struct in_addr in;
    666 {
    667 	register u_int32_t i = ntohl(in.s_addr);
    668 
    669 	if (IN_CLASSA(i))
    670 		return (htonl(IN_CLASSA_NET));
    671 	else if (IN_CLASSB(i))
    672 		return (htonl(IN_CLASSB_NET));
    673 	return (htonl(IN_CLASSC_NET));
    674 }
    675 #endif
    676 
    677 u_int
    678 res_randomid(void) {
    679 	struct timeval now;
    680 
    681 	gettimeofday(&now, NULL);
    682 	return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
    683 }
    684 
    685 /*%
    686  * This routine is for closing the socket if a virtual circuit is used and
    687  * the program wants to close it.  This provides support for endhostent()
    688  * which expects to close the socket.
    689  *
    690  * This routine is not expected to be user visible.
    691  */
    692 void
    693 res_nclose(res_state statp) {
    694 	int ns;
    695 
    696 	if (statp->_vcsock >= 0) {
    697 		(void) close(statp->_vcsock);
    698 		statp->_vcsock = -1;
    699 		statp->_flags &= ~(RES_F_VC | RES_F_CONN);
    700 	}
    701 	for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
    702 		if (statp->_u._ext.nssocks[ns] != -1) {
    703 			(void) close(statp->_u._ext.nssocks[ns]);
    704 			statp->_u._ext.nssocks[ns] = -1;
    705 		}
    706 	}
    707 }
    708 
    709 void
    710 res_ndestroy(res_state statp) {
    711 	res_nclose(statp);
    712 	if (statp->_u._ext.ext != NULL)
    713 		free(statp->_u._ext.ext);
    714 	statp->options &= ~RES_INIT;
    715 	statp->_u._ext.ext = NULL;
    716 }
    717 
    718 const char *
    719 res_get_nibblesuffix(res_state statp) {
    720 	if (statp->_u._ext.ext)
    721 		return (statp->_u._ext.ext->nsuffix);
    722 	return ("ip6.arpa");
    723 }
    724 
    725 const char *
    726 res_get_nibblesuffix2(res_state statp) {
    727 	if (statp->_u._ext.ext)
    728 		return (statp->_u._ext.ext->nsuffix2);
    729 	return ("ip6.int");
    730 }
    731 
    732 void
    733 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
    734 	int i, nserv;
    735 	size_t size;
    736 
    737 	/* close open servers */
    738 	res_nclose(statp);
    739 
    740 	/* cause rtt times to be forgotten */
    741 	statp->_u._ext.nscount = 0;
    742 
    743 	nserv = 0;
    744 	for (i = 0; i < cnt && nserv < MAXNS; i++) {
    745 		switch (set->sin.sin_family) {
    746 		case AF_INET:
    747 			size = sizeof(set->sin);
    748 			if (statp->_u._ext.ext)
    749 				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
    750 					&set->sin, size);
    751 			if (size <= sizeof(statp->nsaddr_list[nserv]))
    752 				memcpy(&statp->nsaddr_list[nserv],
    753 					&set->sin, size);
    754 #ifdef notdef
    755 			else
    756 				statp->nsaddr_list[nserv].sin_family = 0;
    757 #endif
    758 			nserv++;
    759 			break;
    760 
    761 #ifdef HAS_INET6_STRUCTS
    762 		case AF_INET6:
    763 			size = sizeof(set->sin6);
    764 			if (statp->_u._ext.ext)
    765 				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
    766 					&set->sin6, size);
    767 			if (size <= sizeof(statp->nsaddr_list[nserv]))
    768 				memcpy(&statp->nsaddr_list[nserv],
    769 					&set->sin6, size);
    770 			else
    771 				statp->nsaddr_list[nserv].sin_family = 0;
    772 			nserv++;
    773 			break;
    774 #endif
    775 
    776 		default:
    777 			break;
    778 		}
    779 		set++;
    780 	}
    781 	statp->nscount = nserv;
    782 
    783 }
    784 
    785 int
    786 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
    787 	int i;
    788 	size_t size;
    789 	u_int16_t family;
    790 
    791 	for (i = 0; i < statp->nscount && i < cnt; i++) {
    792 		if (statp->_u._ext.ext)
    793 			family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
    794 		else
    795 			family = statp->nsaddr_list[i].sin_family;
    796 
    797 		switch (family) {
    798 		case AF_INET:
    799 			size = sizeof(set->sin);
    800 			if (statp->_u._ext.ext)
    801 				memcpy(&set->sin,
    802 				       &statp->_u._ext.ext->nsaddrs[i],
    803 				       size);
    804 			else
    805 				memcpy(&set->sin, &statp->nsaddr_list[i],
    806 				       size);
    807 			break;
    808 
    809 #ifdef HAS_INET6_STRUCTS
    810 		case AF_INET6:
    811 			size = sizeof(set->sin6);
    812 			if (statp->_u._ext.ext)
    813 				memcpy(&set->sin6,
    814 				       &statp->_u._ext.ext->nsaddrs[i],
    815 				       size);
    816 			else
    817 				memcpy(&set->sin6, &statp->nsaddr_list[i],
    818 				       size);
    819 			break;
    820 #endif
    821 
    822 		default:
    823 			set->sin.sin_family = 0;
    824 			break;
    825 		}
    826 		set++;
    827 	}
    828 	return (statp->nscount);
    829 }
    830 
    831 /*! \file */
    832