blob: a4167a230aad377d93bf2b6169836c6bbd9a5b21 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $ */
2
3/*
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 */
55
56/*
57 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59 *
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies.
63 *
64 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 */
72
73#include <sys/cdefs.h>
74#if defined(LIBC_SCCS) && !defined(lint)
75#ifdef notdef
76static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
77static const char rcsid[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp";
78#else
79__RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $");
80#endif
81#endif /* LIBC_SCCS and not lint */
82
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090083#include <sys/param.h>
84#include <sys/socket.h>
85#include <sys/time.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090086#include <sys/types.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090087
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090088#include <arpa/inet.h>
89#include <arpa/nameser.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090090#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090091
92#include <ctype.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090093#include <netdb.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090094#include <stdio.h>
95#include <stdlib.h>
96#include <string.h>
97#include <unistd.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090098
99#ifdef ANDROID_CHANGES
100#include <errno.h>
101#include <fcntl.h>
102#endif /* ANDROID_CHANGES */
103
104/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
105#ifdef ANDROID_CHANGES
106#include "resolv_netid.h"
107#include "resolv_private.h"
108#else
109#include <resolv.h>
110#endif
111
112#include "res_private.h"
113
114/* Options. Should all be left alone. */
115#ifndef DEBUG
116#define DEBUG
117#endif
118
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900119static void res_setoptions __P((res_state, const char*, const char*) );
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900120
121#ifdef RESOLVSORT
122static const char sort_mask[] = "/&";
123#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
124static uint32_t net_mask(struct in_addr);
125#endif
126
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900127#if !defined(isascii) /* XXX - could be a function */
128#define isascii(c) (!(c & 0200))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900129#endif
130
131/*
132 * Resolver state default settings.
133 */
134
135/*
136 * Set up default settings. If the configuration file exist, the values
137 * there will have precedence. Otherwise, the server address is set to
138 * INADDR_ANY and the default domain name comes from the gethostname().
139 *
140 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
141 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
142 * since it was noted that INADDR_ANY actually meant ``the first interface
143 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
144 * it had to be "up" in order for you to reach your own name server. It
145 * was later decided that since the recommended practice is to always
146 * install local static routes through 127.0.0.1 for all your network
147 * interfaces, that we could solve this problem without a code change.
148 *
149 * The configuration file should always be used, since it is the only way
150 * to specify a default domain. If you are running a server on your local
151 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
152 * in the configuration file.
153 *
154 * Return 0 if completes successfully, -1 on error
155 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900156int res_ninit(res_state statp) {
157 extern int __res_vinit(res_state, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900158
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900159 return (__res_vinit(statp, 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900160}
161
162/* This function has to be reachable by res_data.c but not publicly. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900163int __res_vinit(res_state statp, int preinit) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900164#if !defined(__BIONIC__)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900165 register FILE* fp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900166#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900167 register char *cp, **pp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900168#if !defined(__BIONIC__)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900169 register int n;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900170#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900171 char buf[BUFSIZ];
172 int nserv = 0; /* number of nameserver records read from file */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900173#if !defined(__BIONIC__)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900174 int haveenv = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900175#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900176 int havesearch = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900177#ifdef RESOLVSORT
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900178 int nsort = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900179#endif
180#if !defined(__BIONIC__)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900181 char* net;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900182#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900183 int dots;
184 union res_sockaddr_union u[2];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900185
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900186 if ((statp->options & RES_INIT) != 0U) res_ndestroy(statp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900187
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900188 if (!preinit) {
189 statp->netid = NETID_UNSET;
190 statp->retrans = RES_TIMEOUT;
191 statp->retry = RES_DFLRETRY;
192 statp->options = RES_DEFAULT;
193 statp->id = res_randomid();
194 statp->_mark = MARK_UNSET;
195 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900196
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900197 memset(u, 0, sizeof(u));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900198#ifdef USELOOPBACK
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900199 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900200#else
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900201 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900202#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900203 u[nserv].sin.sin_family = AF_INET;
204 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900205#ifdef HAVE_SA_LEN
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900206 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900207#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 nserv++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900209#ifdef HAS_INET6_STRUCTS
210#ifdef USELOOPBACK
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900211 u[nserv].sin6.sin6_addr = in6addr_loopback;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900212#else
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900213 u[nserv].sin6.sin6_addr = in6addr_any;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900214#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900215 u[nserv].sin6.sin6_family = AF_INET6;
216 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900217#ifdef HAVE_SA_LEN
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900218 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900219#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900220 nserv++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900221#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900222 statp->nscount = 0;
223 statp->ndots = 1;
224 statp->pfcode = 0;
225 statp->_vcsock = -1;
226 statp->_flags = 0;
227 statp->qhook = NULL;
228 statp->rhook = NULL;
229 statp->_u._ext.nscount = 0;
230 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
231 if (statp->_u._ext.ext != NULL) {
232 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
233 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
234 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
235 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
236 }
237 statp->nsort = 0;
238 res_setservers(statp, u, nserv);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900239
240#if defined(__BIONIC__)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900241 /* Ignore the environment. */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900242#else
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900243 /* Allow user to override the local domain definition */
244 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
245 (void) strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
246 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
247 haveenv++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900248
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900249 /*
250 * Set search list to be blank-separated strings
251 * from rest of env value. Permits users of LOCALDOMAIN
252 * to still have a search list, and anyone to set the
253 * one that they want to use as an individual (even more
254 * important now that the rfc1535 stuff restricts searches)
255 */
256 cp = statp->defdname;
257 pp = statp->dnsrch;
258 *pp++ = cp;
259 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
260 if (*cp == '\n') /* silly backwards compat */
261 break;
262 else if (*cp == ' ' || *cp == '\t') {
263 *cp = 0;
264 n = 1;
265 } else if (n) {
266 *pp++ = cp;
267 n = 0;
268 havesearch = 1;
269 }
270 }
271 /* null terminate last domain if there are excess */
272 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') cp++;
273 *cp = '\0';
274 *pp++ = 0;
275 }
276 if (nserv > 0) statp->nscount = nserv;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900277#endif
278
279#ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900280#define MATCH(line, name) \
281 (!strncmp(line, name, sizeof(name) - 1) && \
282 (line[sizeof(name) - 1] == ' ' || line[sizeof(name) - 1] == '\t'))
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900283
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900284 nserv = 0;
285 if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
286 /* read the config file */
287 while (fgets(buf, sizeof(buf), fp) != NULL) {
288 /* skip comments */
289 if (*buf == ';' || *buf == '#') continue;
290 /* read default domain name */
291 if (MATCH(buf, "domain")) {
292 if (haveenv) /* skip if have from environ */
293 continue;
294 cp = buf + sizeof("domain") - 1;
295 while (*cp == ' ' || *cp == '\t') cp++;
296 if ((*cp == '\0') || (*cp == '\n')) continue;
297 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
298 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
299 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) *cp = '\0';
300 havesearch = 0;
301 continue;
302 }
303 /* set search list */
304 if (MATCH(buf, "search")) {
305 if (haveenv) /* skip if have from environ */
306 continue;
307 cp = buf + sizeof("search") - 1;
308 while (*cp == ' ' || *cp == '\t') cp++;
309 if ((*cp == '\0') || (*cp == '\n')) continue;
310 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
311 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
312 if ((cp = strchr(statp->defdname, '\n')) != NULL) *cp = '\0';
313 /*
314 * Set search list to be blank-separated strings
315 * on rest of line.
316 */
317 cp = statp->defdname;
318 pp = statp->dnsrch;
319 *pp++ = cp;
320 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
321 if (*cp == ' ' || *cp == '\t') {
322 *cp = 0;
323 n = 1;
324 } else if (n) {
325 *pp++ = cp;
326 n = 0;
327 }
328 }
329 /* null terminate last domain if there are excess */
330 while (*cp != '\0' && *cp != ' ' && *cp != '\t') cp++;
331 *cp = '\0';
332 *pp++ = 0;
333 havesearch = 1;
334 continue;
335 }
336 /* read nameservers to query */
337 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
338 struct addrinfo hints, *ai;
339 char sbuf[NI_MAXSERV];
340 const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900341
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900342 cp = buf + sizeof("nameserver") - 1;
343 while (*cp == ' ' || *cp == '\t') cp++;
344 cp[strcspn(cp, ";# \t\n")] = '\0';
345 if ((*cp != '\0') && (*cp != '\n')) {
346 memset(&hints, 0, sizeof(hints));
347 hints.ai_family = PF_UNSPEC;
348 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
349 hints.ai_flags = AI_NUMERICHOST;
350 sprintf(sbuf, "%u", NAMESERVER_PORT);
351 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && ai->ai_addrlen <= minsiz) {
352 if (statp->_u._ext.ext != NULL) {
353 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr,
354 ai->ai_addrlen);
355 }
356 if (ai->ai_addrlen <= sizeof(statp->nsaddr_list[nserv])) {
357 memcpy(&statp->nsaddr_list[nserv], ai->ai_addr, ai->ai_addrlen);
358 } else
359 statp->nsaddr_list[nserv].sin_family = 0;
360 freeaddrinfo(ai);
361 nserv++;
362 }
363 }
364 continue;
365 }
366 if (MATCH(buf, "sortlist")) {
367 struct in_addr a;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900368
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900369 cp = buf + sizeof("sortlist") - 1;
370 while (nsort < MAXRESOLVSORT) {
371 while (*cp == ' ' || *cp == '\t') cp++;
372 if (*cp == '\0' || *cp == '\n' || *cp == ';') break;
373 net = cp;
374 while (*cp && !ISSORTMASK(*cp) && *cp != ';' && isascii(*cp) &&
375 !isspace((unsigned char) *cp))
376 cp++;
377 n = *cp;
378 *cp = 0;
379 if (inet_aton(net, &a)) {
380 statp->sort_list[nsort].addr = a;
381 if (ISSORTMASK(n)) {
382 *cp++ = n;
383 net = cp;
384 while (*cp && *cp != ';' && isascii(*cp) &&
385 !isspace((unsigned char) *cp))
386 cp++;
387 n = *cp;
388 *cp = 0;
389 if (inet_aton(net, &a)) {
390 statp->sort_list[nsort].mask = a.s_addr;
391 } else {
392 statp->sort_list[nsort].mask =
393 net_mask(statp->sort_list[nsort].addr);
394 }
395 } else {
396 statp->sort_list[nsort].mask = net_mask(statp->sort_list[nsort].addr);
397 }
398 nsort++;
399 }
400 *cp = n;
401 }
402 continue;
403 }
404 if (MATCH(buf, "options")) {
405 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
406 continue;
407 }
408 }
409 if (nserv > 0) statp->nscount = nserv;
410 statp->nsort = nsort;
411 (void) fclose(fp);
412 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900413#endif /* !ANDROID_CHANGES */
414/*
415 * Last chance to get a nameserver. This should not normally
416 * be necessary
417 */
418#ifdef NO_RESOLV_CONF
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900419 if (nserv == 0) nserv = get_nameservers(statp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900420#endif
421
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900422 if (statp->defdname[0] == 0 && gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
423 (cp = strchr(buf, '.')) != NULL)
424 strcpy(statp->defdname, cp + 1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900425
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900426 /* find components of local domain that might be searched */
427 if (havesearch == 0) {
428 pp = statp->dnsrch;
429 *pp++ = statp->defdname;
430 *pp = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900431
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900432 dots = 0;
433 for (cp = statp->defdname; *cp; cp++) dots += (*cp == '.');
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900434
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900435 cp = statp->defdname;
436 while (pp < statp->dnsrch + MAXDFLSRCH) {
437 if (dots < LOCALDOMAINPARTS) break;
438 cp = strchr(cp, '.') + 1; /* we know there is one */
439 *pp++ = cp;
440 dots--;
441 }
442 *pp = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900443#ifdef DEBUG
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900444 if (statp->options & RES_DEBUG) {
445 printf(";; res_init()... default dnsrch list:\n");
446 for (pp = statp->dnsrch; *pp; pp++) printf(";;\t%s\n", *pp);
447 printf(";;\t..END..\n");
448 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900449#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900450 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900451
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900452 if ((cp = getenv("RES_OPTIONS")) != NULL) res_setoptions(statp, cp, "env");
453 if (nserv > 0) {
454 statp->nscount = nserv;
455 statp->options |= RES_INIT;
456 }
457 return (0);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900458}
459
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900460static void res_setoptions(res_state statp, const char* options, const char* source) {
461 const char* cp = options;
462 int i;
463 struct __res_state_ext* ext = statp->_u._ext.ext;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900464
465#ifdef DEBUG
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900466 if (statp->options & RES_DEBUG)
467 printf(";; res_setoptions(\"%s\", \"%s\")...\n", options, source);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900468#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900469 while (*cp) {
470 /* skip leading and inner runs of spaces */
471 while (*cp == ' ' || *cp == '\t') cp++;
472 /* search for and process individual options */
473 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
474 i = atoi(cp + sizeof("ndots:") - 1);
475 if (i <= RES_MAXNDOTS)
476 statp->ndots = i;
477 else
478 statp->ndots = RES_MAXNDOTS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900479#ifdef DEBUG
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900480 if (statp->options & RES_DEBUG) printf(";;\tndots=%d\n", statp->ndots);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900481#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900482 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
483 i = atoi(cp + sizeof("timeout:") - 1);
484 if (i <= RES_MAXRETRANS)
485 statp->retrans = i;
486 else
487 statp->retrans = RES_MAXRETRANS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900488#ifdef DEBUG
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900489 if (statp->options & RES_DEBUG) printf(";;\ttimeout=%d\n", statp->retrans);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900490#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900491 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)) {
492 i = atoi(cp + sizeof("attempts:") - 1);
493 if (i <= RES_MAXRETRY)
494 statp->retry = i;
495 else
496 statp->retry = RES_MAXRETRY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900497#ifdef DEBUG
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900498 if (statp->options & RES_DEBUG) printf(";;\tattempts=%d\n", statp->retry);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900499#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900500 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900501#ifdef DEBUG
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900502 if (!(statp->options & RES_DEBUG)) {
503 printf(";; res_setoptions(\"%s\", \"%s\")..\n", options, source);
504 statp->options |= RES_DEBUG;
505 }
506 printf(";;\tdebug\n");
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900507#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900508 } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1) ||
509 !strncmp(cp, "no-tld-query", sizeof("no-tld-query") - 1)) {
510 statp->options |= RES_NOTLDQUERY;
511 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
512 statp->options |= RES_USE_INET6;
513 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
514 statp->options |= RES_ROTATE;
515 } else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) {
516 statp->options |= RES_NOCHECKNAME;
517 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900518#ifdef RES_USE_EDNS0
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900519 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
520 statp->options |= RES_USE_EDNS0;
521 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900522#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900523 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
524 statp->options |= RES_USE_DNAME;
525 } else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
526 if (ext == NULL) goto skip;
527 cp += sizeof("nibble:") - 1;
528 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
529 strncpy(ext->nsuffix, cp, (size_t) i);
530 ext->nsuffix[i] = '\0';
531 } else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
532 if (ext == NULL) goto skip;
533 cp += sizeof("nibble2:") - 1;
534 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
535 strncpy(ext->nsuffix2, cp, (size_t) i);
536 ext->nsuffix2[i] = '\0';
537 } else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
538 cp += sizeof("v6revmode:") - 1;
539 /* "nibble" and "bitstring" used to be valid */
540 if (!strncmp(cp, "single", sizeof("single") - 1)) {
541 statp->options |= RES_NO_NIBBLE2;
542 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
543 statp->options &= ~RES_NO_NIBBLE2;
544 }
545 } else {
546 /* XXX - print a warning here? */
547 }
548 skip:
549 /* skip to next run of spaces */
550 while (*cp && *cp != ' ' && *cp != '\t') cp++;
551 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900552}
553
554#ifdef RESOLVSORT
555/* XXX - should really support CIDR which means explicit masks always. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900556static uint32_t net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900557{
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900558 register uint32_t i = ntohl(in.s_addr);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900559
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900560 if (IN_CLASSA(i))
561 return (htonl(IN_CLASSA_NET));
562 else if (IN_CLASSB(i))
563 return (htonl(IN_CLASSB_NET));
564 return (htonl(IN_CLASSC_NET));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900565}
566#endif
567
568/*%
569 * This routine is for closing the socket if a virtual circuit is used and
570 * the program wants to close it. This provides support for endhostent()
571 * which expects to close the socket.
572 *
573 * This routine is not expected to be user visible.
574 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900575void res_nclose(res_state statp) {
576 int ns;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900577
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900578 if (statp->_vcsock >= 0) {
579 (void) close(statp->_vcsock);
580 statp->_vcsock = -1;
581 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
582 }
583 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
584 if (statp->_u._ext.nssocks[ns] != -1) {
585 (void) close(statp->_u._ext.nssocks[ns]);
586 statp->_u._ext.nssocks[ns] = -1;
587 }
588 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900589}
590
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900591void res_ndestroy(res_state statp) {
592 res_nclose(statp);
593 if (statp->_u._ext.ext != NULL) free(statp->_u._ext.ext);
594 statp->options &= ~RES_INIT;
595 statp->_u._ext.ext = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900596}
597
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900598const char* res_get_nibblesuffix(res_state statp) {
599 if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix);
600 return ("ip6.arpa");
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900601}
602
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900603const char* res_get_nibblesuffix2(res_state statp) {
604 if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix2);
605 return ("ip6.int");
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900606}
607
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900608void res_setservers(res_state statp, const union res_sockaddr_union* set, int cnt) {
609 int i, nserv;
610 size_t size;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900611
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900612 /* close open servers */
613 res_nclose(statp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900614
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900615 /* cause rtt times to be forgotten */
616 statp->_u._ext.nscount = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900617
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900618 nserv = 0;
619 for (i = 0; i < cnt && nserv < MAXNS; i++) {
620 switch (set->sin.sin_family) {
621 case AF_INET:
622 size = sizeof(set->sin);
623 if (statp->_u._ext.ext)
624 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin, size);
625 if (size <= sizeof(statp->nsaddr_list[nserv]))
626 memcpy(&statp->nsaddr_list[nserv], &set->sin, size);
627 else
628 statp->nsaddr_list[nserv].sin_family = 0;
629 nserv++;
630 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900631
632#ifdef HAS_INET6_STRUCTS
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900633 case AF_INET6:
634 size = sizeof(set->sin6);
635 if (statp->_u._ext.ext)
636 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin6, size);
637 if (size <= sizeof(statp->nsaddr_list[nserv]))
638 memcpy(&statp->nsaddr_list[nserv], &set->sin6, size);
639 else
640 statp->nsaddr_list[nserv].sin_family = 0;
641 nserv++;
642 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900643#endif
644
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900645 default:
646 break;
647 }
648 set++;
649 }
650 statp->nscount = nserv;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900651}
652
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900653int res_getservers(res_state statp, union res_sockaddr_union* set, int cnt) {
654 int i;
655 size_t size;
656 uint16_t family;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900657
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900658 for (i = 0; i < statp->nscount && i < cnt; i++) {
659 if (statp->_u._ext.ext)
660 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
661 else
662 family = statp->nsaddr_list[i].sin_family;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900663
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900664 switch (family) {
665 case AF_INET:
666 size = sizeof(set->sin);
667 if (statp->_u._ext.ext)
668 memcpy(&set->sin, &statp->_u._ext.ext->nsaddrs[i], size);
669 else
670 memcpy(&set->sin, &statp->nsaddr_list[i], size);
671 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900672
673#ifdef HAS_INET6_STRUCTS
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900674 case AF_INET6:
675 size = sizeof(set->sin6);
676 if (statp->_u._ext.ext)
677 memcpy(&set->sin6, &statp->_u._ext.ext->nsaddrs[i], size);
678 else
679 memcpy(&set->sin6, &statp->nsaddr_list[i], size);
680 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900681#endif
682
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900683 default:
684 set->sin.sin_family = 0;
685 break;
686 }
687 set++;
688 }
689 return (statp->nscount);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900690}
691
692#ifdef ANDROID_CHANGES
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900693void res_setnetcontext(res_state statp, const struct android_net_context* netcontext) {
694 if (statp != NULL) {
695 statp->netid = netcontext->dns_netid;
696 statp->_mark = netcontext->dns_mark;
697 statp->qhook = netcontext->qhook;
698 if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) {
699 statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC;
700 }
701 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900702}
703
704#endif /* ANDROID_CHANGES */