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