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