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