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