xref: /minix3/lib/libc/resolv/res_init.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: res_init.c,v 1.30 2015/02/24 17:56:20 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1985, 1989, 1993
52fe8fb19SBen Gras  *    The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras /*
332fe8fb19SBen Gras  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
342fe8fb19SBen Gras  *
352fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
362fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
372fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies, and that
382fe8fb19SBen Gras  * the name of Digital Equipment Corporation not be used in advertising or
392fe8fb19SBen Gras  * publicity pertaining to distribution of the document or software without
402fe8fb19SBen Gras  * specific, written prior permission.
412fe8fb19SBen Gras  *
422fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
432fe8fb19SBen Gras  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
442fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
452fe8fb19SBen Gras  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
462fe8fb19SBen Gras  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
472fe8fb19SBen Gras  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
482fe8fb19SBen Gras  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
492fe8fb19SBen Gras  * SOFTWARE.
502fe8fb19SBen Gras  */
512fe8fb19SBen Gras 
522fe8fb19SBen Gras /*
532fe8fb19SBen Gras  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
542fe8fb19SBen Gras  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
552fe8fb19SBen Gras  *
562fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
572fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
582fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
592fe8fb19SBen Gras  *
602fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
612fe8fb19SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
622fe8fb19SBen Gras  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
632fe8fb19SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
642fe8fb19SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
652fe8fb19SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
662fe8fb19SBen Gras  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras 
692fe8fb19SBen Gras #include <sys/cdefs.h>
702fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
712fe8fb19SBen Gras #ifdef notdef
722fe8fb19SBen Gras static const char sccsid[] = "@(#)res_init.c	8.1 (Berkeley) 6/7/93";
732fe8fb19SBen Gras static const char rcsid[] = "Id: res_init.c,v 1.26 2008/12/11 09:59:00 marka Exp";
742fe8fb19SBen Gras #else
75*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: res_init.c,v 1.30 2015/02/24 17:56:20 christos Exp $");
762fe8fb19SBen Gras #endif
772fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
782fe8fb19SBen Gras 
792fe8fb19SBen Gras #include "port_before.h"
802fe8fb19SBen Gras 
812fe8fb19SBen Gras #include "namespace.h"
822fe8fb19SBen Gras #include <sys/types.h>
832fe8fb19SBen Gras #include <sys/param.h>
842fe8fb19SBen Gras #include <sys/socket.h>
852fe8fb19SBen Gras #include <sys/stat.h>
862fe8fb19SBen Gras #include <sys/time.h>
8784d9c625SLionel Sambuc #if !defined(__minix)
882fe8fb19SBen Gras #include <sys/event.h>
8984d9c625SLionel Sambuc #endif /* !defined(__minix) */
902fe8fb19SBen Gras 
912fe8fb19SBen Gras #include <netinet/in.h>
922fe8fb19SBen Gras #include <arpa/inet.h>
932fe8fb19SBen Gras #include <arpa/nameser.h>
942fe8fb19SBen Gras 
952fe8fb19SBen Gras #include <ctype.h>
962fe8fb19SBen Gras #include <stdio.h>
972fe8fb19SBen Gras #include <stdlib.h>
982fe8fb19SBen Gras #include <string.h>
992fe8fb19SBen Gras #include <unistd.h>
1002fe8fb19SBen Gras #include <fcntl.h>
1012fe8fb19SBen Gras #include <netdb.h>
1022fe8fb19SBen Gras 
1032fe8fb19SBen Gras #define HAVE_MD5
1042fe8fb19SBen Gras #include <md5.h>
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras #ifndef HAVE_MD5
1072fe8fb19SBen Gras # include "../dst/md5.h"
1082fe8fb19SBen Gras #else
1092fe8fb19SBen Gras # ifdef SOLARIS2
1102fe8fb19SBen Gras #  include <sys/md5.h>
1112fe8fb19SBen Gras # endif
1122fe8fb19SBen Gras #endif
1132fe8fb19SBen Gras #ifndef _MD5_H_
1142fe8fb19SBen Gras # define _MD5_H_ 1	/*%< make sure we do not include rsaref md5.h file */
1152fe8fb19SBen Gras #endif
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras #include "port_after.h"
1182fe8fb19SBen Gras 
1192fe8fb19SBen Gras #if 0
1202fe8fb19SBen Gras #ifdef __weak_alias
1212fe8fb19SBen Gras __weak_alias(res_ninit,_res_ninit)
1222fe8fb19SBen Gras __weak_alias(res_randomid,__res_randomid)
1232fe8fb19SBen Gras __weak_alias(res_nclose,_res_nclose)
1242fe8fb19SBen Gras __weak_alias(res_ndestroy,_res_ndestroy)
1252fe8fb19SBen Gras __weak_alias(res_get_nibblesuffix,__res_get_nibblesuffix)
1262fe8fb19SBen Gras __weak_alias(res_get_nibblesuffix2,__res_get_nibblesuffix2)
1272fe8fb19SBen Gras __weak_alias(res_getservers,__res_getservers)
1282fe8fb19SBen Gras __weak_alias(res_setservers,__res_setservers)
1292fe8fb19SBen Gras #endif
1302fe8fb19SBen Gras #endif
1312fe8fb19SBen Gras 
1322fe8fb19SBen Gras 
1332fe8fb19SBen Gras /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
1342fe8fb19SBen Gras #include <resolv.h>
1352fe8fb19SBen Gras 
1362fe8fb19SBen Gras #include "res_private.h"
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras #define RESOLVSORT
1392fe8fb19SBen Gras /*% Options.  Should all be left alone. */
1402fe8fb19SBen Gras #ifndef DEBUG
1412fe8fb19SBen Gras #define DEBUG
1422fe8fb19SBen Gras #endif
1432fe8fb19SBen Gras 
1442fe8fb19SBen Gras #ifdef SOLARIS2
1452fe8fb19SBen Gras #include <sys/systeminfo.h>
1462fe8fb19SBen Gras #endif
1472fe8fb19SBen Gras 
148f14fb602SLionel Sambuc static void res_setoptions(res_state, const char *, const char *);
1492fe8fb19SBen Gras 
1502fe8fb19SBen Gras #ifdef RESOLVSORT
1512fe8fb19SBen Gras static const char sort_mask[] = "/&";
1522fe8fb19SBen Gras #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
153f14fb602SLionel Sambuc static uint32_t net_mask(struct in_addr);
1542fe8fb19SBen Gras #endif
1552fe8fb19SBen Gras 
1562fe8fb19SBen Gras #if !defined(isascii)	/*%< XXX - could be a function */
1572fe8fb19SBen Gras # define isascii(c) (!(c & 0200))
1582fe8fb19SBen Gras #endif
1592fe8fb19SBen Gras 
1602fe8fb19SBen Gras static struct timespec __res_conf_time;
1614f89addcSEmmanuel Blot #if !defined(__minix)
1622fe8fb19SBen Gras static const struct timespec ts = { 0, 0 };
163*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
164*0a6a1f1dSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc const char *__res_conf_name = _PATH_RESCONF;
1662fe8fb19SBen Gras 
1672fe8fb19SBen Gras /*
1682fe8fb19SBen Gras  * Resolver state default settings.
1692fe8fb19SBen Gras  */
1702fe8fb19SBen Gras 
1712fe8fb19SBen Gras /*%
1722fe8fb19SBen Gras  * Set up default settings.  If the configuration file exist, the values
1732fe8fb19SBen Gras  * there will have precedence.  Otherwise, the server address is set to
1742fe8fb19SBen Gras  * INADDR_ANY and the default domain name comes from the gethostname().
1752fe8fb19SBen Gras  *
1762fe8fb19SBen Gras  * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
1772fe8fb19SBen Gras  * rather than INADDR_ANY ("0.0.0.0") as the default name server address
1782fe8fb19SBen Gras  * since it was noted that INADDR_ANY actually meant ``the first interface
1792fe8fb19SBen Gras  * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
1802fe8fb19SBen Gras  * it had to be "up" in order for you to reach your own name server.  It
1812fe8fb19SBen Gras  * was later decided that since the recommended practice is to always
1822fe8fb19SBen Gras  * install local static routes through 127.0.0.1 for all your network
1832fe8fb19SBen Gras  * interfaces, that we could solve this problem without a code change.
1842fe8fb19SBen Gras  *
1852fe8fb19SBen Gras  * The configuration file should always be used, since it is the only way
1862fe8fb19SBen Gras  * to specify a default domain.  If you are running a server on your local
1872fe8fb19SBen Gras  * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
1882fe8fb19SBen Gras  * in the configuration file.
1892fe8fb19SBen Gras  *
1902fe8fb19SBen Gras  * Return 0 if completes successfully, -1 on error
1912fe8fb19SBen Gras  */
1922fe8fb19SBen Gras int
res_ninit(res_state statp)1932fe8fb19SBen Gras res_ninit(res_state statp) {
1942fe8fb19SBen Gras 	return (__res_vinit(statp, 0));
1952fe8fb19SBen Gras }
1962fe8fb19SBen Gras 
1972fe8fb19SBen Gras /*% This function has to be reachable by res_data.c but not publically. */
1982fe8fb19SBen Gras int
__res_vinit(res_state statp,int preinit)1992fe8fb19SBen Gras __res_vinit(res_state statp, int preinit) {
2002fe8fb19SBen Gras 	register FILE *fp;
2012fe8fb19SBen Gras 	register char *cp, **pp;
2022fe8fb19SBen Gras 	register int n;
2032fe8fb19SBen Gras 	char buf[BUFSIZ];
2042fe8fb19SBen Gras 	int nserv = 0;    /*%< number of nameserver records read from file */
2052fe8fb19SBen Gras 	int haveenv = 0;
2062fe8fb19SBen Gras 	int havesearch = 0;
2072fe8fb19SBen Gras #ifdef RESOLVSORT
2082fe8fb19SBen Gras 	int nsort = 0;
2092fe8fb19SBen Gras 	char *net;
2102fe8fb19SBen Gras #endif
2112fe8fb19SBen Gras 	int dots;
2122fe8fb19SBen Gras 	union res_sockaddr_union u[2];
2132fe8fb19SBen Gras 	int maxns = MAXNS;
2142fe8fb19SBen Gras 
2152fe8fb19SBen Gras 	RES_SET_H_ERRNO(statp, 0);
2162fe8fb19SBen Gras 
2172fe8fb19SBen Gras 	if ((statp->options & RES_INIT) != 0U)
2182fe8fb19SBen Gras 		res_ndestroy(statp);
2192fe8fb19SBen Gras 
2202fe8fb19SBen Gras 	if (!preinit) {
2212fe8fb19SBen Gras 		statp->retrans = RES_TIMEOUT;
2222fe8fb19SBen Gras 		statp->retry = RES_DFLRETRY;
2232fe8fb19SBen Gras 		statp->options = RES_DEFAULT;
2242fe8fb19SBen Gras 	}
2252fe8fb19SBen Gras 	statp->_rnd = malloc(16);
2262fe8fb19SBen Gras 	res_rndinit(statp);
2272fe8fb19SBen Gras 	statp->id = res_nrandomid(statp);
2282fe8fb19SBen Gras 
2292fe8fb19SBen Gras 	memset(u, 0, sizeof(u));
2302fe8fb19SBen Gras #ifdef USELOOPBACK
2312fe8fb19SBen Gras 	u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
2322fe8fb19SBen Gras #else
2332fe8fb19SBen Gras 	u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
2342fe8fb19SBen Gras #endif
2352fe8fb19SBen Gras 	u[nserv].sin.sin_family = AF_INET;
2362fe8fb19SBen Gras 	u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
2372fe8fb19SBen Gras #ifdef HAVE_SA_LEN
2382fe8fb19SBen Gras 	u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
2392fe8fb19SBen Gras #endif
2402fe8fb19SBen Gras 	nserv++;
2412fe8fb19SBen Gras #ifdef HAS_INET6_STRUCTS
2422fe8fb19SBen Gras #ifdef USELOOPBACK
2432fe8fb19SBen Gras 	u[nserv].sin6.sin6_addr = in6addr_loopback;
2442fe8fb19SBen Gras #else
2452fe8fb19SBen Gras 	u[nserv].sin6.sin6_addr = in6addr_any;
2462fe8fb19SBen Gras #endif
2472fe8fb19SBen Gras 	u[nserv].sin6.sin6_family = AF_INET6;
2482fe8fb19SBen Gras 	u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
2492fe8fb19SBen Gras #ifdef HAVE_SA_LEN
2502fe8fb19SBen Gras 	u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
2512fe8fb19SBen Gras #endif
2522fe8fb19SBen Gras 	nserv++;
2532fe8fb19SBen Gras #endif
2542fe8fb19SBen Gras 	statp->nscount = 0;
2552fe8fb19SBen Gras 	statp->ndots = 1;
2562fe8fb19SBen Gras 	statp->pfcode = 0;
2572fe8fb19SBen Gras 	statp->_vcsock = -1;
2582fe8fb19SBen Gras 	statp->_flags = 0;
2592fe8fb19SBen Gras 	statp->qhook = NULL;
2602fe8fb19SBen Gras 	statp->rhook = NULL;
2612fe8fb19SBen Gras 	statp->_u._ext.nscount = 0;
2622fe8fb19SBen Gras 	statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
2632fe8fb19SBen Gras 	if (statp->_u._ext.ext != NULL) {
2642fe8fb19SBen Gras 	        memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
2652fe8fb19SBen Gras 		statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
2662fe8fb19SBen Gras 		strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
2672fe8fb19SBen Gras 		strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
2682fe8fb19SBen Gras 	} else {
2692fe8fb19SBen Gras 		/*
2702fe8fb19SBen Gras 		 * Historically res_init() rarely, if at all, failed.
2712fe8fb19SBen Gras 		 * Examples and applications exist which do not check
2722fe8fb19SBen Gras 		 * our return code.  Furthermore several applications
2732fe8fb19SBen Gras 		 * simply call us to get the systems domainname.  So
274*0a6a1f1dSLionel Sambuc 		 * rather than immediately fail here we store the
2752fe8fb19SBen Gras 		 * failure, which is returned later, in h_errno.  And
2762fe8fb19SBen Gras 		 * prevent the collection of 'nameserver' information
2772fe8fb19SBen Gras 		 * by setting maxns to 0.  Thus applications that fail
2782fe8fb19SBen Gras 		 * to check our return code wont be able to make
2792fe8fb19SBen Gras 		 * queries anyhow.
2802fe8fb19SBen Gras 		 */
2812fe8fb19SBen Gras 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
2822fe8fb19SBen Gras 		maxns = 0;
2832fe8fb19SBen Gras 	}
2842fe8fb19SBen Gras #ifdef RESOLVSORT
2852fe8fb19SBen Gras 	statp->nsort = 0;
2862fe8fb19SBen Gras #endif
2872fe8fb19SBen Gras 	res_setservers(statp, u, nserv);
2882fe8fb19SBen Gras 
2892fe8fb19SBen Gras #ifdef	SOLARIS2
2902fe8fb19SBen Gras 	/*
2912fe8fb19SBen Gras 	 * The old libresolv derived the defaultdomain from NIS/NIS+.
2922fe8fb19SBen Gras 	 * We want to keep this behaviour
2932fe8fb19SBen Gras 	 */
2942fe8fb19SBen Gras 	{
2952fe8fb19SBen Gras 		char buf[sizeof(statp->defdname)], *cp;
2962fe8fb19SBen Gras 		int ret;
2972fe8fb19SBen Gras 
2982fe8fb19SBen Gras 		if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
2992fe8fb19SBen Gras 			(unsigned int)ret <= sizeof(buf)) {
3002fe8fb19SBen Gras 			if (buf[0] == '+')
3012fe8fb19SBen Gras 				buf[0] = '.';
3022fe8fb19SBen Gras 			cp = strchr(buf, '.');
3032fe8fb19SBen Gras 			cp = (cp == NULL) ? buf : (cp + 1);
3042fe8fb19SBen Gras 			(void)strlcpy(statp->defdname, cp,
3052fe8fb19SBen Gras 			    sizeof(statp->defdname));
3062fe8fb19SBen Gras 		}
3072fe8fb19SBen Gras 	}
3082fe8fb19SBen Gras #endif	/* SOLARIS2 */
3092fe8fb19SBen Gras 
3102fe8fb19SBen Gras 	/* Allow user to override the local domain definition */
3112fe8fb19SBen Gras 	if ((cp = getenv("LOCALDOMAIN")) != NULL) {
3122fe8fb19SBen Gras 		(void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3132fe8fb19SBen Gras 		statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3142fe8fb19SBen Gras 		haveenv++;
3152fe8fb19SBen Gras 
3162fe8fb19SBen Gras 		/*
3172fe8fb19SBen Gras 		 * Set search list to be blank-separated strings
3182fe8fb19SBen Gras 		 * from rest of env value.  Permits users of LOCALDOMAIN
3192fe8fb19SBen Gras 		 * to still have a search list, and anyone to set the
3202fe8fb19SBen Gras 		 * one that they want to use as an individual (even more
3212fe8fb19SBen Gras 		 * important now that the rfc1535 stuff restricts searches)
3222fe8fb19SBen Gras 		 */
3232fe8fb19SBen Gras 		cp = statp->defdname;
3242fe8fb19SBen Gras 		pp = statp->dnsrch;
3252fe8fb19SBen Gras 		*pp++ = cp;
3262fe8fb19SBen Gras 		for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
3272fe8fb19SBen Gras 			if (*cp == '\n')	/*%< silly backwards compat */
3282fe8fb19SBen Gras 				break;
3292fe8fb19SBen Gras 			else if (*cp == ' ' || *cp == '\t') {
3302fe8fb19SBen Gras 				*cp = 0;
3312fe8fb19SBen Gras 				n = 1;
3322fe8fb19SBen Gras 			} else if (n) {
3332fe8fb19SBen Gras 				*pp++ = cp;
3342fe8fb19SBen Gras 				n = 0;
3352fe8fb19SBen Gras 				havesearch = 1;
3362fe8fb19SBen Gras 			}
3372fe8fb19SBen Gras 		}
3382fe8fb19SBen Gras 		/* null terminate last domain if there are excess */
3392fe8fb19SBen Gras 		while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
3402fe8fb19SBen Gras 			cp++;
3412fe8fb19SBen Gras 		*cp = '\0';
3422fe8fb19SBen Gras 		*pp++ = 0;
3432fe8fb19SBen Gras 	}
3442fe8fb19SBen Gras 
3452fe8fb19SBen Gras #define	MATCH(line, name) \
3462fe8fb19SBen Gras 	(!strncmp(line, name, sizeof(name) - 1) && \
3472fe8fb19SBen Gras 	(line[sizeof(name) - 1] == ' ' || \
3482fe8fb19SBen Gras 	 line[sizeof(name) - 1] == '\t'))
3492fe8fb19SBen Gras 
3502fe8fb19SBen Gras 	nserv = 0;
351*0a6a1f1dSLionel Sambuc 	if ((fp = fopen(__res_conf_name, "re")) != NULL) {
3522fe8fb19SBen Gras 	    struct stat st;
35384d9c625SLionel Sambuc #if !defined(__minix)
3542fe8fb19SBen Gras 	    struct kevent kc;
35584d9c625SLionel Sambuc #endif /* !defined(__minix) */
3562fe8fb19SBen Gras 
3572fe8fb19SBen Gras 	    /* read the config file */
358f14fb602SLionel Sambuc 	    while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
3592fe8fb19SBen Gras 		/* skip comments */
3602fe8fb19SBen Gras 		if (*buf == ';' || *buf == '#')
3612fe8fb19SBen Gras 			continue;
3622fe8fb19SBen Gras 		/* read default domain name */
3632fe8fb19SBen Gras 		if (MATCH(buf, "domain")) {
3642fe8fb19SBen Gras 		    if (haveenv)	/*%< skip if have from environ */
3652fe8fb19SBen Gras 			    continue;
3662fe8fb19SBen Gras 		    cp = buf + sizeof("domain") - 1;
3672fe8fb19SBen Gras 		    while (*cp == ' ' || *cp == '\t')
3682fe8fb19SBen Gras 			    cp++;
3692fe8fb19SBen Gras 		    if ((*cp == '\0') || (*cp == '\n'))
3702fe8fb19SBen Gras 			    continue;
3712fe8fb19SBen Gras 		    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3722fe8fb19SBen Gras 		    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3732fe8fb19SBen Gras 		    if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
3742fe8fb19SBen Gras 			    *cp = '\0';
3752fe8fb19SBen Gras 		    havesearch = 0;
3762fe8fb19SBen Gras 		    continue;
3772fe8fb19SBen Gras 		}
3782fe8fb19SBen Gras 		/* set search list */
3792fe8fb19SBen Gras 		if (MATCH(buf, "search")) {
3802fe8fb19SBen Gras 		    if (haveenv)	/*%< skip if have from environ */
3812fe8fb19SBen Gras 			    continue;
3822fe8fb19SBen Gras 		    cp = buf + sizeof("search") - 1;
3832fe8fb19SBen Gras 		    while (*cp == ' ' || *cp == '\t')
3842fe8fb19SBen Gras 			    cp++;
3852fe8fb19SBen Gras 		    if ((*cp == '\0') || (*cp == '\n'))
3862fe8fb19SBen Gras 			    continue;
3872fe8fb19SBen Gras 		    strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
3882fe8fb19SBen Gras 		    statp->defdname[sizeof(statp->defdname) - 1] = '\0';
3892fe8fb19SBen Gras 		    if ((cp = strchr(statp->defdname, '\n')) != NULL)
3902fe8fb19SBen Gras 			    *cp = '\0';
3912fe8fb19SBen Gras 		    /*
3922fe8fb19SBen Gras 		     * Set search list to be blank-separated strings
3932fe8fb19SBen Gras 		     * on rest of line.
3942fe8fb19SBen Gras 		     */
3952fe8fb19SBen Gras 		    cp = statp->defdname;
3962fe8fb19SBen Gras 		    pp = statp->dnsrch;
3972fe8fb19SBen Gras 		    *pp++ = cp;
3982fe8fb19SBen Gras 		    for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
3992fe8fb19SBen Gras 			    if (*cp == ' ' || *cp == '\t') {
4002fe8fb19SBen Gras 				    *cp = 0;
4012fe8fb19SBen Gras 				    n = 1;
4022fe8fb19SBen Gras 			    } else if (n) {
4032fe8fb19SBen Gras 				    *pp++ = cp;
4042fe8fb19SBen Gras 				    n = 0;
4052fe8fb19SBen Gras 			    }
4062fe8fb19SBen Gras 		    }
4072fe8fb19SBen Gras 		    /* null terminate last domain if there are excess */
4082fe8fb19SBen Gras 		    while (*cp != '\0' && *cp != ' ' && *cp != '\t')
4092fe8fb19SBen Gras 			    cp++;
4102fe8fb19SBen Gras 		    *cp = '\0';
4112fe8fb19SBen Gras 		    *pp++ = 0;
4122fe8fb19SBen Gras 		    havesearch = 1;
4132fe8fb19SBen Gras 		    continue;
4142fe8fb19SBen Gras 		}
4152fe8fb19SBen Gras 		/* read nameservers to query */
4162fe8fb19SBen Gras 		if (MATCH(buf, "nameserver") && nserv < maxns) {
4172fe8fb19SBen Gras 		    struct addrinfo hints, *ai;
4182fe8fb19SBen Gras 		    char sbuf[NI_MAXSERV];
4192fe8fb19SBen Gras 		    const size_t minsiz =
4202fe8fb19SBen Gras 		        sizeof(statp->_u._ext.ext->nsaddrs[0]);
4212fe8fb19SBen Gras 
4222fe8fb19SBen Gras 		    cp = buf + sizeof("nameserver") - 1;
4232fe8fb19SBen Gras 		    while (*cp == ' ' || *cp == '\t')
4242fe8fb19SBen Gras 			cp++;
4252fe8fb19SBen Gras 		    cp[strcspn(cp, ";# \t\n")] = '\0';
4262fe8fb19SBen Gras 		    if ((*cp != '\0') && (*cp != '\n')) {
4272fe8fb19SBen Gras 			memset(&hints, 0, sizeof(hints));
4282fe8fb19SBen Gras 			hints.ai_family = PF_UNSPEC;
4292fe8fb19SBen Gras 			hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
4302fe8fb19SBen Gras 			hints.ai_flags = AI_NUMERICHOST;
4312fe8fb19SBen Gras 			sprintf(sbuf, "%u", NAMESERVER_PORT);
4322fe8fb19SBen Gras 			if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
4332fe8fb19SBen Gras 			    ai->ai_addrlen <= minsiz) {
4342fe8fb19SBen Gras 			    if (statp->_u._ext.ext != NULL) {
4352fe8fb19SBen Gras 				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
4362fe8fb19SBen Gras 				    ai->ai_addr, ai->ai_addrlen);
4372fe8fb19SBen Gras 			    }
4382fe8fb19SBen Gras 			    if (ai->ai_addrlen <=
4392fe8fb19SBen Gras 			        sizeof(statp->nsaddr_list[nserv])) {
4402fe8fb19SBen Gras 				memcpy(&statp->nsaddr_list[nserv],
4412fe8fb19SBen Gras 				    ai->ai_addr, ai->ai_addrlen);
4422fe8fb19SBen Gras 			    } else
4432fe8fb19SBen Gras 				statp->nsaddr_list[nserv].sin_family = 0;
4442fe8fb19SBen Gras 			    freeaddrinfo(ai);
4452fe8fb19SBen Gras 			    nserv++;
4462fe8fb19SBen Gras 			}
4472fe8fb19SBen Gras 		    }
4482fe8fb19SBen Gras 		    continue;
4492fe8fb19SBen Gras 		}
4502fe8fb19SBen Gras #ifdef RESOLVSORT
4512fe8fb19SBen Gras 		if (MATCH(buf, "sortlist")) {
4522fe8fb19SBen Gras 		    struct in_addr a;
4532fe8fb19SBen Gras 
4542fe8fb19SBen Gras 		    cp = buf + sizeof("sortlist") - 1;
4552fe8fb19SBen Gras 		    while (nsort < MAXRESOLVSORT) {
4562fe8fb19SBen Gras 			while (*cp == ' ' || *cp == '\t')
4572fe8fb19SBen Gras 			    cp++;
4582fe8fb19SBen Gras 			if (*cp == '\0' || *cp == '\n' || *cp == ';')
4592fe8fb19SBen Gras 			    break;
4602fe8fb19SBen Gras 			net = cp;
4612fe8fb19SBen Gras 			while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
4622fe8fb19SBen Gras 			       isascii(*cp) && !isspace((unsigned char)*cp))
4632fe8fb19SBen Gras 				cp++;
4642fe8fb19SBen Gras 			n = *cp;
4652fe8fb19SBen Gras 			*cp = 0;
4662fe8fb19SBen Gras 			if (inet_aton(net, &a)) {
4672fe8fb19SBen Gras 			    statp->sort_list[nsort].addr = a;
4682fe8fb19SBen Gras 			    if (ISSORTMASK(n)) {
4692fe8fb19SBen Gras 				*cp++ = n;
4702fe8fb19SBen Gras 				net = cp;
4712fe8fb19SBen Gras 				while (*cp && *cp != ';' &&
4722fe8fb19SBen Gras 					isascii(*cp) &&
4732fe8fb19SBen Gras 					!isspace((unsigned char)*cp))
4742fe8fb19SBen Gras 				    cp++;
4752fe8fb19SBen Gras 				n = *cp;
4762fe8fb19SBen Gras 				*cp = 0;
4772fe8fb19SBen Gras 				if (inet_aton(net, &a)) {
4782fe8fb19SBen Gras 				    statp->sort_list[nsort].mask = a.s_addr;
4792fe8fb19SBen Gras 				} else {
4802fe8fb19SBen Gras 				    statp->sort_list[nsort].mask =
4812fe8fb19SBen Gras 					net_mask(statp->sort_list[nsort].addr);
4822fe8fb19SBen Gras 				}
4832fe8fb19SBen Gras 			    } else {
4842fe8fb19SBen Gras 				statp->sort_list[nsort].mask =
4852fe8fb19SBen Gras 				    net_mask(statp->sort_list[nsort].addr);
4862fe8fb19SBen Gras 			    }
4872fe8fb19SBen Gras 			    nsort++;
4882fe8fb19SBen Gras 			}
4892fe8fb19SBen Gras 			*cp = n;
4902fe8fb19SBen Gras 		    }
4912fe8fb19SBen Gras 		    continue;
4922fe8fb19SBen Gras 		}
4932fe8fb19SBen Gras #endif
4942fe8fb19SBen Gras 		if (MATCH(buf, "options")) {
4952fe8fb19SBen Gras 		    res_setoptions(statp, buf + sizeof("options") - 1, "conf");
4962fe8fb19SBen Gras 		    continue;
4972fe8fb19SBen Gras 		}
4982fe8fb19SBen Gras 	    }
4992fe8fb19SBen Gras 	    if (nserv > 0)
5002fe8fb19SBen Gras 		statp->nscount = nserv;
5012fe8fb19SBen Gras #ifdef RESOLVSORT
5022fe8fb19SBen Gras 	    statp->nsort = nsort;
5032fe8fb19SBen Gras #endif
504*0a6a1f1dSLionel Sambuc 	    statp->_u._ext.ext->resfd = fcntl(fileno(fp), F_DUPFD_CLOEXEC);
5052fe8fb19SBen Gras 	    (void) fclose(fp);
5062fe8fb19SBen Gras 	    if (fstat(statp->_u._ext.ext->resfd, &st) != -1)
5072fe8fb19SBen Gras 		    __res_conf_time = statp->_u._ext.ext->res_conf_time =
5082fe8fb19SBen Gras 			st.st_mtimespec;
50984d9c625SLionel Sambuc #if !defined(__minix)
510f14fb602SLionel Sambuc 	    statp->_u._ext.ext->kq = kqueue1(O_CLOEXEC);
5112fe8fb19SBen Gras 	    EV_SET(&kc, statp->_u._ext.ext->resfd, EVFILT_VNODE,
5122fe8fb19SBen Gras 		EV_ADD|EV_ENABLE|EV_CLEAR, NOTE_DELETE|NOTE_WRITE| NOTE_EXTEND|
5132fe8fb19SBen Gras 		NOTE_ATTRIB|NOTE_LINK|NOTE_RENAME|NOTE_REVOKE, 0, 0);
5142fe8fb19SBen Gras 	    (void)kevent(statp->_u._ext.ext->kq, &kc, 1, NULL, 0, &ts);
51584d9c625SLionel Sambuc #else
5162fe8fb19SBen Gras 	statp->_u._ext.ext->kq = -1;
51784d9c625SLionel Sambuc #endif /* !defined(__minix) */
5182fe8fb19SBen Gras 	} else {
5192fe8fb19SBen Gras 	    statp->_u._ext.ext->kq = -1;
5202fe8fb19SBen Gras 	    statp->_u._ext.ext->resfd = -1;
5212fe8fb19SBen Gras 	}
5222fe8fb19SBen Gras /*
5232fe8fb19SBen Gras  * Last chance to get a nameserver.  This should not normally
5242fe8fb19SBen Gras  * be necessary
5252fe8fb19SBen Gras  */
5262fe8fb19SBen Gras #ifdef NO_RESOLV_CONF
5272fe8fb19SBen Gras 	if(nserv == 0)
5282fe8fb19SBen Gras 		nserv = get_nameservers(statp);
5292fe8fb19SBen Gras #endif
5302fe8fb19SBen Gras 
5312fe8fb19SBen Gras 	if (statp->defdname[0] == 0 &&
5322fe8fb19SBen Gras 	    gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
5332fe8fb19SBen Gras 	    (cp = strchr(buf, '.')) != NULL)
5342fe8fb19SBen Gras 		strcpy(statp->defdname, cp + 1);
5352fe8fb19SBen Gras 
5362fe8fb19SBen Gras 	/* find components of local domain that might be searched */
5372fe8fb19SBen Gras 	if (havesearch == 0) {
5382fe8fb19SBen Gras 		pp = statp->dnsrch;
5392fe8fb19SBen Gras 		*pp++ = statp->defdname;
5402fe8fb19SBen Gras 		*pp = NULL;
5412fe8fb19SBen Gras 
5422fe8fb19SBen Gras 		dots = 0;
5432fe8fb19SBen Gras 		for (cp = statp->defdname; *cp; cp++)
5442fe8fb19SBen Gras 			dots += (*cp == '.');
5452fe8fb19SBen Gras 
5462fe8fb19SBen Gras 		cp = statp->defdname;
5472fe8fb19SBen Gras 		while (pp < statp->dnsrch + MAXDFLSRCH) {
5482fe8fb19SBen Gras 			if (dots < LOCALDOMAINPARTS)
5492fe8fb19SBen Gras 				break;
5502fe8fb19SBen Gras 			cp = strchr(cp, '.') + 1;    /*%< we know there is one */
5512fe8fb19SBen Gras 			*pp++ = cp;
5522fe8fb19SBen Gras 			dots--;
5532fe8fb19SBen Gras 		}
5542fe8fb19SBen Gras 		*pp = NULL;
5552fe8fb19SBen Gras #ifdef DEBUG
5562fe8fb19SBen Gras 		if (statp->options & RES_DEBUG) {
5572fe8fb19SBen Gras 			printf(";; res_init()... default dnsrch list:\n");
5582fe8fb19SBen Gras 			for (pp = statp->dnsrch; *pp; pp++)
5592fe8fb19SBen Gras 				printf(";;\t%s\n", *pp);
5602fe8fb19SBen Gras 			printf(";;\t..END..\n");
5612fe8fb19SBen Gras 		}
5622fe8fb19SBen Gras #endif
5632fe8fb19SBen Gras 	}
5642fe8fb19SBen Gras 
5652fe8fb19SBen Gras 	if ((cp = getenv("RES_OPTIONS")) != NULL)
5662fe8fb19SBen Gras 		res_setoptions(statp, cp, "env");
5672fe8fb19SBen Gras 	statp->options |= RES_INIT;
5682fe8fb19SBen Gras 	return (statp->res_h_errno);
5692fe8fb19SBen Gras }
5702fe8fb19SBen Gras 
5712fe8fb19SBen Gras int
res_check(res_state statp,struct timespec * mtime)5722fe8fb19SBen Gras res_check(res_state statp, struct timespec *mtime)
5732fe8fb19SBen Gras {
57484d9c625SLionel Sambuc #if defined(__minix)
5752fe8fb19SBen Gras 	/*
5762fe8fb19SBen Gras 	 * XXX: No update on change.
5772fe8fb19SBen Gras 	 */
5782fe8fb19SBen Gras 	return 0;
57984d9c625SLionel Sambuc #else
5802fe8fb19SBen Gras 	/*
5812fe8fb19SBen Gras 	 * If the times are equal, then we check if there
5822fe8fb19SBen Gras 	 * was a kevent related to resolv.conf and reload.
5832fe8fb19SBen Gras 	 * If the times are not equal, then we don't bother
5842fe8fb19SBen Gras 	 * to check the kevent, because another thread already
5852fe8fb19SBen Gras 	 * did, loaded and changed the time.
5862fe8fb19SBen Gras 	 */
5872fe8fb19SBen Gras 	if (timespeccmp(&statp->_u._ext.ext->res_conf_time,
5882fe8fb19SBen Gras 	    &__res_conf_time, ==)) {
5892fe8fb19SBen Gras 		struct kevent ke;
5902fe8fb19SBen Gras 		if (statp->_u._ext.ext->kq == -1)
5912fe8fb19SBen Gras 			goto out;
5922fe8fb19SBen Gras 
5932fe8fb19SBen Gras 		switch (kevent(statp->_u._ext.ext->kq, NULL, 0, &ke, 1, &ts)) {
5942fe8fb19SBen Gras 		case 0:
5952fe8fb19SBen Gras 		case -1:
5962fe8fb19SBen Gras out:
5972fe8fb19SBen Gras 			if (mtime)
5982fe8fb19SBen Gras 				*mtime = __res_conf_time;
5992fe8fb19SBen Gras 			return 0;
6002fe8fb19SBen Gras 		default:
6012fe8fb19SBen Gras 			break;
6022fe8fb19SBen Gras 		}
6032fe8fb19SBen Gras 	}
6042fe8fb19SBen Gras 	(void)__res_vinit(statp, 0);
6052fe8fb19SBen Gras 	if (mtime)
6062fe8fb19SBen Gras 		*mtime = __res_conf_time;
6072fe8fb19SBen Gras 	return 1;
60884d9c625SLionel Sambuc #endif /* defined(__minix) */
6092fe8fb19SBen Gras }
6102fe8fb19SBen Gras 
6112fe8fb19SBen Gras static void
res_setoptions(res_state statp,const char * options,const char * source)6122fe8fb19SBen Gras res_setoptions(res_state statp, const char *options, const char *source)
6132fe8fb19SBen Gras {
6142fe8fb19SBen Gras 	const char *cp = options;
6152fe8fb19SBen Gras 	int i;
616f14fb602SLionel Sambuc 	size_t j;
6172fe8fb19SBen Gras 	struct __res_state_ext *ext = statp->_u._ext.ext;
6182fe8fb19SBen Gras 
6192fe8fb19SBen Gras #ifdef DEBUG
6202fe8fb19SBen Gras 	if (statp->options & RES_DEBUG)
6212fe8fb19SBen Gras 		printf(";; res_setoptions(\"%s\", \"%s\")...\n",
6222fe8fb19SBen Gras 		       options, source);
6232fe8fb19SBen Gras #endif
6242fe8fb19SBen Gras 	while (*cp) {
6252fe8fb19SBen Gras 		/* skip leading and inner runs of spaces */
6262fe8fb19SBen Gras 		while (*cp == ' ' || *cp == '\t')
6272fe8fb19SBen Gras 			cp++;
6282fe8fb19SBen Gras 		/* search for and process individual options */
6292fe8fb19SBen Gras 		if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
6302fe8fb19SBen Gras 			i = atoi(cp + sizeof("ndots:") - 1);
6312fe8fb19SBen Gras 			if (i <= RES_MAXNDOTS)
6322fe8fb19SBen Gras 				statp->ndots = i;
6332fe8fb19SBen Gras 			else
6342fe8fb19SBen Gras 				statp->ndots = RES_MAXNDOTS;
6352fe8fb19SBen Gras #ifdef DEBUG
6362fe8fb19SBen Gras 			if (statp->options & RES_DEBUG)
6372fe8fb19SBen Gras 				printf(";;\tndots=%d\n", statp->ndots);
6382fe8fb19SBen Gras #endif
6392fe8fb19SBen Gras 		} else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
6402fe8fb19SBen Gras 			i = atoi(cp + sizeof("timeout:") - 1);
6412fe8fb19SBen Gras 			if (i <= RES_MAXRETRANS)
6422fe8fb19SBen Gras 				statp->retrans = i;
6432fe8fb19SBen Gras 			else
6442fe8fb19SBen Gras 				statp->retrans = RES_MAXRETRANS;
6452fe8fb19SBen Gras #ifdef DEBUG
6462fe8fb19SBen Gras 			if (statp->options & RES_DEBUG)
6472fe8fb19SBen Gras 				printf(";;\ttimeout=%d\n", statp->retrans);
6482fe8fb19SBen Gras #endif
6492fe8fb19SBen Gras #ifdef	SOLARIS2
6502fe8fb19SBen Gras 		} else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
6512fe8fb19SBen Gras 			/*
6522fe8fb19SBen Gras 		 	 * For backward compatibility, 'retrans' is
6532fe8fb19SBen Gras 		 	 * supported as an alias for 'timeout', though
6542fe8fb19SBen Gras 		 	 * without an imposed maximum.
6552fe8fb19SBen Gras 		 	 */
6562fe8fb19SBen Gras 			statp->retrans = atoi(cp + sizeof("retrans:") - 1);
6572fe8fb19SBen Gras 		} else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
6582fe8fb19SBen Gras 			/*
6592fe8fb19SBen Gras 			 * For backward compatibility, 'retry' is
6602fe8fb19SBen Gras 			 * supported as an alias for 'attempts', though
6612fe8fb19SBen Gras 			 * without an imposed maximum.
6622fe8fb19SBen Gras 			 */
6632fe8fb19SBen Gras 			statp->retry = atoi(cp + sizeof("retry:") - 1);
6642fe8fb19SBen Gras #endif	/* SOLARIS2 */
6652fe8fb19SBen Gras 		} else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
6662fe8fb19SBen Gras 			i = atoi(cp + sizeof("attempts:") - 1);
6672fe8fb19SBen Gras 			if (i <= RES_MAXRETRY)
6682fe8fb19SBen Gras 				statp->retry = i;
6692fe8fb19SBen Gras 			else
6702fe8fb19SBen Gras 				statp->retry = RES_MAXRETRY;
6712fe8fb19SBen Gras #ifdef DEBUG
6722fe8fb19SBen Gras 			if (statp->options & RES_DEBUG)
6732fe8fb19SBen Gras 				printf(";;\tattempts=%d\n", statp->retry);
6742fe8fb19SBen Gras #endif
6752fe8fb19SBen Gras 		} else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
6762fe8fb19SBen Gras #ifdef DEBUG
6772fe8fb19SBen Gras 			if (!(statp->options & RES_DEBUG)) {
6782fe8fb19SBen Gras 				printf(";; res_setoptions(\"%s\", \"%s\")..\n",
6792fe8fb19SBen Gras 				       options, source);
6802fe8fb19SBen Gras 				statp->options |= RES_DEBUG;
6812fe8fb19SBen Gras 			}
6822fe8fb19SBen Gras 			printf(";;\tdebug\n");
6832fe8fb19SBen Gras #endif
6842fe8fb19SBen Gras 		} else if (!strncmp(cp, "no_tld_query",
6852fe8fb19SBen Gras 				    sizeof("no_tld_query") - 1) ||
6862fe8fb19SBen Gras 			   !strncmp(cp, "no-tld-query",
6872fe8fb19SBen Gras 				    sizeof("no-tld-query") - 1)) {
6882fe8fb19SBen Gras 			statp->options |= RES_NOTLDQUERY;
6892fe8fb19SBen Gras 		} else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
6902fe8fb19SBen Gras 			statp->options |= RES_USE_INET6;
6912fe8fb19SBen Gras 		} else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
6922fe8fb19SBen Gras 			statp->options |= RES_ROTATE;
6932fe8fb19SBen Gras 		} else if (!strncmp(cp, "no-check-names",
6942fe8fb19SBen Gras 				    sizeof("no-check-names") - 1)) {
6952fe8fb19SBen Gras 			statp->options |= RES_NOCHECKNAME;
696f14fb602SLionel Sambuc 		} else if (!strncmp(cp, "check-names",
697f14fb602SLionel Sambuc 				    sizeof("check-names") - 1)) {
698f14fb602SLionel Sambuc 			statp->options &= ~RES_NOCHECKNAME;
6992fe8fb19SBen Gras 		}
7002fe8fb19SBen Gras #ifdef RES_USE_EDNS0
7012fe8fb19SBen Gras 		else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
7022fe8fb19SBen Gras 			statp->options |= RES_USE_EDNS0;
7032fe8fb19SBen Gras 		}
7042fe8fb19SBen Gras #endif
7052fe8fb19SBen Gras 		else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
7062fe8fb19SBen Gras 			statp->options |= RES_USE_DNAME;
7072fe8fb19SBen Gras 		}
7082fe8fb19SBen Gras 		else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
7092fe8fb19SBen Gras 			if (ext == NULL)
7102fe8fb19SBen Gras 				goto skip;
7112fe8fb19SBen Gras 			cp += sizeof("nibble:") - 1;
712f14fb602SLionel Sambuc 			j = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
713f14fb602SLionel Sambuc 			strncpy(ext->nsuffix, cp, j);
714f14fb602SLionel Sambuc 			ext->nsuffix[j] = '\0';
7152fe8fb19SBen Gras 		}
7162fe8fb19SBen Gras 		else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
7172fe8fb19SBen Gras 			if (ext == NULL)
7182fe8fb19SBen Gras 				goto skip;
7192fe8fb19SBen Gras 			cp += sizeof("nibble2:") - 1;
720f14fb602SLionel Sambuc 			j = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
721f14fb602SLionel Sambuc 			strncpy(ext->nsuffix2, cp, j);
722f14fb602SLionel Sambuc 			ext->nsuffix2[j] = '\0';
7232fe8fb19SBen Gras 		}
7242fe8fb19SBen Gras 		else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
7252fe8fb19SBen Gras 			cp += sizeof("v6revmode:") - 1;
7262fe8fb19SBen Gras 			/* "nibble" and "bitstring" used to be valid */
7272fe8fb19SBen Gras 			if (!strncmp(cp, "single", sizeof("single") - 1)) {
7282fe8fb19SBen Gras 				statp->options |= RES_NO_NIBBLE2;
7292fe8fb19SBen Gras 			} else if (!strncmp(cp, "both", sizeof("both") - 1)) {
7302fe8fb19SBen Gras 				statp->options &=
7312fe8fb19SBen Gras 					 ~RES_NO_NIBBLE2;
7322fe8fb19SBen Gras 			}
7332fe8fb19SBen Gras 		}
7342fe8fb19SBen Gras 		else {
7352fe8fb19SBen Gras 			/* XXX - print a warning here? */
7362fe8fb19SBen Gras 		}
7372fe8fb19SBen Gras    skip:
7382fe8fb19SBen Gras 		/* skip to next run of spaces */
7392fe8fb19SBen Gras 		while (*cp && *cp != ' ' && *cp != '\t')
7402fe8fb19SBen Gras 			cp++;
7412fe8fb19SBen Gras 	}
7422fe8fb19SBen Gras }
7432fe8fb19SBen Gras 
7442fe8fb19SBen Gras #ifdef RESOLVSORT
7452fe8fb19SBen Gras /* XXX - should really support CIDR which means explicit masks always. */
746f14fb602SLionel Sambuc static uint32_t
net_mask(struct in_addr in)747f14fb602SLionel Sambuc net_mask(struct in_addr in) /*!< XXX - should really use system's version of this  */
7482fe8fb19SBen Gras {
749f14fb602SLionel Sambuc 	register uint32_t i = ntohl(in.s_addr);
7502fe8fb19SBen Gras 
7512fe8fb19SBen Gras 	if (IN_CLASSA(i))
7522fe8fb19SBen Gras 		return (htonl(IN_CLASSA_NET));
7532fe8fb19SBen Gras 	else if (IN_CLASSB(i))
7542fe8fb19SBen Gras 		return (htonl(IN_CLASSB_NET));
7552fe8fb19SBen Gras 	return (htonl(IN_CLASSC_NET));
7562fe8fb19SBen Gras }
7572fe8fb19SBen Gras #endif
7582fe8fb19SBen Gras 
7592fe8fb19SBen Gras static u_char srnd[16];
7602fe8fb19SBen Gras 
7612fe8fb19SBen Gras void
res_rndinit(res_state statp)7622fe8fb19SBen Gras res_rndinit(res_state statp)
7632fe8fb19SBen Gras {
7642fe8fb19SBen Gras 	struct timeval now;
765f14fb602SLionel Sambuc 	uint32_t u32;
766f14fb602SLionel Sambuc 	uint16_t u16;
7672fe8fb19SBen Gras 	u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
7682fe8fb19SBen Gras 
7692fe8fb19SBen Gras 	gettimeofday(&now, NULL);
770f14fb602SLionel Sambuc 	u32 = (uint32_t)now.tv_sec;
7712fe8fb19SBen Gras 	memcpy(rnd, &u32, 4);
7722fe8fb19SBen Gras 	u32 = now.tv_usec;
7732fe8fb19SBen Gras 	memcpy(rnd + 4, &u32, 4);
774f14fb602SLionel Sambuc 	u32 += (uint32_t)now.tv_sec;
7752fe8fb19SBen Gras 	memcpy(rnd + 8, &u32, 4);
7762fe8fb19SBen Gras 	u16 = getpid();
7772fe8fb19SBen Gras 	memcpy(rnd + 12, &u16, 2);
7782fe8fb19SBen Gras }
7792fe8fb19SBen Gras 
7802fe8fb19SBen Gras u_int
res_nrandomid(res_state statp)781f14fb602SLionel Sambuc res_nrandomid(res_state statp)
782f14fb602SLionel Sambuc {
7832fe8fb19SBen Gras 	struct timeval now;
784f14fb602SLionel Sambuc 	uint16_t u16;
7852fe8fb19SBen Gras 	MD5_CTX ctx;
7862fe8fb19SBen Gras 	u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd;
7872fe8fb19SBen Gras 
7882fe8fb19SBen Gras 	gettimeofday(&now, NULL);
789f14fb602SLionel Sambuc 	u16 = (uint16_t) (now.tv_sec ^ now.tv_usec);
7902fe8fb19SBen Gras 	memcpy(rnd + 14, &u16, 2);
7912fe8fb19SBen Gras #ifndef HAVE_MD5
7922fe8fb19SBen Gras 	MD5_Init(&ctx);
7932fe8fb19SBen Gras 	MD5_Update(&ctx, rnd, 16);
7942fe8fb19SBen Gras 	MD5_Final(rnd, &ctx);
7952fe8fb19SBen Gras #else
7962fe8fb19SBen Gras 	MD5Init(&ctx);
7972fe8fb19SBen Gras 	MD5Update(&ctx, rnd, 16);
7982fe8fb19SBen Gras 	MD5Final(rnd, &ctx);
7992fe8fb19SBen Gras #endif
8002fe8fb19SBen Gras 	memcpy(&u16, rnd + 14, 2);
8012fe8fb19SBen Gras 	return ((u_int) u16);
8022fe8fb19SBen Gras }
8032fe8fb19SBen Gras 
8042fe8fb19SBen Gras /*%
8052fe8fb19SBen Gras  * This routine is for closing the socket if a virtual circuit is used and
8062fe8fb19SBen Gras  * the program wants to close it.  This provides support for endhostent()
8072fe8fb19SBen Gras  * which expects to close the socket.
8082fe8fb19SBen Gras  *
8092fe8fb19SBen Gras  * This routine is not expected to be user visible.
8102fe8fb19SBen Gras  */
8112fe8fb19SBen Gras void
res_nclose(res_state statp)812f14fb602SLionel Sambuc res_nclose(res_state statp)
813f14fb602SLionel Sambuc {
8142fe8fb19SBen Gras 	int ns;
8152fe8fb19SBen Gras 
8162fe8fb19SBen Gras 	if (statp->_vcsock >= 0) {
8172fe8fb19SBen Gras 		(void) close(statp->_vcsock);
8182fe8fb19SBen Gras 		statp->_vcsock = -1;
8192fe8fb19SBen Gras 		statp->_flags &= ~(RES_F_VC | RES_F_CONN);
8202fe8fb19SBen Gras 	}
8212fe8fb19SBen Gras 	for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
8222fe8fb19SBen Gras 		if (statp->_u._ext.nssocks[ns] != -1) {
8232fe8fb19SBen Gras 			(void) close(statp->_u._ext.nssocks[ns]);
8242fe8fb19SBen Gras 			statp->_u._ext.nssocks[ns] = -1;
8252fe8fb19SBen Gras 		}
8262fe8fb19SBen Gras 	}
8272fe8fb19SBen Gras }
8282fe8fb19SBen Gras 
8292fe8fb19SBen Gras void
res_ndestroy(res_state statp)830f14fb602SLionel Sambuc res_ndestroy(res_state statp)
831f14fb602SLionel Sambuc {
8322fe8fb19SBen Gras 	res_nclose(statp);
8332fe8fb19SBen Gras 	if (statp->_u._ext.ext != NULL) {
8342fe8fb19SBen Gras 		if (statp->_u._ext.ext->kq != -1)
8352fe8fb19SBen Gras 			(void)close(statp->_u._ext.ext->kq);
8362fe8fb19SBen Gras 		if (statp->_u._ext.ext->resfd != -1)
8372fe8fb19SBen Gras 			(void)close(statp->_u._ext.ext->resfd);
8382fe8fb19SBen Gras 		free(statp->_u._ext.ext);
8392fe8fb19SBen Gras 		statp->_u._ext.ext = NULL;
8402fe8fb19SBen Gras 	}
8412fe8fb19SBen Gras 	if (statp->_rnd != NULL) {
8422fe8fb19SBen Gras 		free(statp->_rnd);
8432fe8fb19SBen Gras 		statp->_rnd = NULL;
8442fe8fb19SBen Gras 	}
8452fe8fb19SBen Gras 	statp->options &= ~RES_INIT;
8462fe8fb19SBen Gras }
8472fe8fb19SBen Gras 
8482fe8fb19SBen Gras const char *
res_get_nibblesuffix(res_state statp)849f14fb602SLionel Sambuc res_get_nibblesuffix(res_state statp)
850f14fb602SLionel Sambuc {
8512fe8fb19SBen Gras 	if (statp->_u._ext.ext)
8522fe8fb19SBen Gras 		return (statp->_u._ext.ext->nsuffix);
8532fe8fb19SBen Gras 	return ("ip6.arpa");
8542fe8fb19SBen Gras }
8552fe8fb19SBen Gras 
8562fe8fb19SBen Gras const char *
res_get_nibblesuffix2(res_state statp)857f14fb602SLionel Sambuc res_get_nibblesuffix2(res_state statp)
858f14fb602SLionel Sambuc {
8592fe8fb19SBen Gras 	if (statp->_u._ext.ext)
8602fe8fb19SBen Gras 		return (statp->_u._ext.ext->nsuffix2);
8612fe8fb19SBen Gras 	return ("ip6.int");
8622fe8fb19SBen Gras }
8632fe8fb19SBen Gras 
8642fe8fb19SBen Gras void
res_setservers(res_state statp,const union res_sockaddr_union * set,int cnt)865f14fb602SLionel Sambuc res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt)
866f14fb602SLionel Sambuc {
8672fe8fb19SBen Gras 	int i, nserv;
8682fe8fb19SBen Gras 	size_t size;
8692fe8fb19SBen Gras 
8702fe8fb19SBen Gras 	/* close open servers */
8712fe8fb19SBen Gras 	res_nclose(statp);
8722fe8fb19SBen Gras 
8732fe8fb19SBen Gras 	/* cause rtt times to be forgotten */
8742fe8fb19SBen Gras 	statp->_u._ext.nscount = 0;
8752fe8fb19SBen Gras 
8762fe8fb19SBen Gras 	nserv = 0;
8772fe8fb19SBen Gras 	for (i = 0; i < cnt && nserv < MAXNS; i++) {
8782fe8fb19SBen Gras 		switch (set->sin.sin_family) {
8792fe8fb19SBen Gras 		case AF_INET:
8802fe8fb19SBen Gras 			size = sizeof(set->sin);
8812fe8fb19SBen Gras 			if (statp->_u._ext.ext)
8822fe8fb19SBen Gras 				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
8832fe8fb19SBen Gras 					&set->sin, size);
8842fe8fb19SBen Gras 			if (size <= sizeof(statp->nsaddr_list[nserv]))
8852fe8fb19SBen Gras 				memcpy(&statp->nsaddr_list[nserv],
8862fe8fb19SBen Gras 					&set->sin, size);
8872fe8fb19SBen Gras 			else
8882fe8fb19SBen Gras 				statp->nsaddr_list[nserv].sin_family = 0;
8892fe8fb19SBen Gras 			nserv++;
8902fe8fb19SBen Gras 			break;
8912fe8fb19SBen Gras 
8922fe8fb19SBen Gras #ifdef HAS_INET6_STRUCTS
8932fe8fb19SBen Gras 		case AF_INET6:
8942fe8fb19SBen Gras 			size = sizeof(set->sin6);
8952fe8fb19SBen Gras 			if (statp->_u._ext.ext)
8962fe8fb19SBen Gras 				memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
8972fe8fb19SBen Gras 					&set->sin6, size);
8982fe8fb19SBen Gras 			if (size <= sizeof(statp->nsaddr_list[nserv]))
8992fe8fb19SBen Gras 				memcpy(&statp->nsaddr_list[nserv],
9002fe8fb19SBen Gras 					&set->sin6, size);
9012fe8fb19SBen Gras 			else
9022fe8fb19SBen Gras 				statp->nsaddr_list[nserv].sin_family = 0;
9032fe8fb19SBen Gras 			nserv++;
9042fe8fb19SBen Gras 			break;
9052fe8fb19SBen Gras #endif
9062fe8fb19SBen Gras 
9072fe8fb19SBen Gras 		default:
9082fe8fb19SBen Gras 			break;
9092fe8fb19SBen Gras 		}
9102fe8fb19SBen Gras 		set++;
9112fe8fb19SBen Gras 	}
9122fe8fb19SBen Gras 	statp->nscount = nserv;
9132fe8fb19SBen Gras 
9142fe8fb19SBen Gras }
9152fe8fb19SBen Gras 
9162fe8fb19SBen Gras int
res_getservers(res_state statp,union res_sockaddr_union * set,int cnt)917f14fb602SLionel Sambuc res_getservers(res_state statp, union res_sockaddr_union *set, int cnt)
918f14fb602SLionel Sambuc {
9192fe8fb19SBen Gras 	int i;
9202fe8fb19SBen Gras 	size_t size;
921f14fb602SLionel Sambuc 	uint16_t family;
9222fe8fb19SBen Gras 
9232fe8fb19SBen Gras 	for (i = 0; i < statp->nscount && i < cnt; i++) {
9242fe8fb19SBen Gras 		if (statp->_u._ext.ext)
9252fe8fb19SBen Gras 			family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
9262fe8fb19SBen Gras 		else
9272fe8fb19SBen Gras 			family = statp->nsaddr_list[i].sin_family;
9282fe8fb19SBen Gras 
9292fe8fb19SBen Gras 		switch (family) {
9302fe8fb19SBen Gras 		case AF_INET:
9312fe8fb19SBen Gras 			size = sizeof(set->sin);
9322fe8fb19SBen Gras 			if (statp->_u._ext.ext)
9332fe8fb19SBen Gras 				memcpy(&set->sin,
9342fe8fb19SBen Gras 				       &statp->_u._ext.ext->nsaddrs[i],
9352fe8fb19SBen Gras 				       size);
9362fe8fb19SBen Gras 			else
9372fe8fb19SBen Gras 				memcpy(&set->sin, &statp->nsaddr_list[i],
9382fe8fb19SBen Gras 				       size);
9392fe8fb19SBen Gras 			break;
9402fe8fb19SBen Gras 
9412fe8fb19SBen Gras #ifdef HAS_INET6_STRUCTS
9422fe8fb19SBen Gras 		case AF_INET6:
9432fe8fb19SBen Gras 			size = sizeof(set->sin6);
9442fe8fb19SBen Gras 			if (statp->_u._ext.ext)
9452fe8fb19SBen Gras 				memcpy(&set->sin6,
9462fe8fb19SBen Gras 				       &statp->_u._ext.ext->nsaddrs[i],
9472fe8fb19SBen Gras 				       size);
9482fe8fb19SBen Gras 			else
9492fe8fb19SBen Gras 				memcpy(&set->sin6, &statp->nsaddr_list[i],
9502fe8fb19SBen Gras 				       size);
9512fe8fb19SBen Gras 			break;
9522fe8fb19SBen Gras #endif
9532fe8fb19SBen Gras 
9542fe8fb19SBen Gras 		default:
9552fe8fb19SBen Gras 			set->sin.sin_family = 0;
9562fe8fb19SBen Gras 			break;
9572fe8fb19SBen Gras 		}
9582fe8fb19SBen Gras 		set++;
9592fe8fb19SBen Gras 	}
9602fe8fb19SBen Gras 	return (statp->nscount);
9612fe8fb19SBen Gras }
9622fe8fb19SBen Gras 
9632fe8fb19SBen Gras /*! \file */
964