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