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