blob: e2d276d28a30a3abbbb4465ffdd401e6922bb30f [file] [log] [blame]
Bernie Innocenti55864192018-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
Bernie Innocenti55864192018-08-30 04:05:20 +090073#include <sys/param.h>
74#include <sys/socket.h>
75#include <sys/time.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090076#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090077
Bernie Innocenti55864192018-08-30 04:05:20 +090078#include <arpa/inet.h>
79#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090080#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090081
82#include <ctype.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090083#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090084#include <stdio.h>
85#include <stdlib.h>
86#include <string.h>
87#include <unistd.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090088
89#ifdef ANDROID_CHANGES
90#include <errno.h>
91#include <fcntl.h>
92#endif /* ANDROID_CHANGES */
93
94/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
95#ifdef ANDROID_CHANGES
96#include "resolv_netid.h"
97#include "resolv_private.h"
98#else
99#include <resolv.h>
100#endif
101
102#include "res_private.h"
103
104/* Options. Should all be left alone. */
105#ifndef DEBUG
106#define DEBUG
107#endif
108
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900109static void res_setoptions __P((res_state, const char*, const char*) );
Bernie Innocenti55864192018-08-30 04:05:20 +0900110
111#ifdef RESOLVSORT
112static const char sort_mask[] = "/&";
113#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
114static uint32_t net_mask(struct in_addr);
115#endif
116
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900117#if !defined(isascii) /* XXX - could be a function */
118#define isascii(c) (!(c & 0200))
Bernie Innocenti55864192018-08-30 04:05:20 +0900119#endif
120
121/*
122 * Resolver state default settings.
123 */
124
125/*
126 * Set up default settings. If the configuration file exist, the values
127 * there will have precedence. Otherwise, the server address is set to
128 * INADDR_ANY and the default domain name comes from the gethostname().
129 *
130 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
131 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
132 * since it was noted that INADDR_ANY actually meant ``the first interface
133 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
134 * it had to be "up" in order for you to reach your own name server. It
135 * was later decided that since the recommended practice is to always
136 * install local static routes through 127.0.0.1 for all your network
137 * interfaces, that we could solve this problem without a code change.
138 *
139 * The configuration file should always be used, since it is the only way
140 * to specify a default domain. If you are running a server on your local
141 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
142 * in the configuration file.
143 *
144 * Return 0 if completes successfully, -1 on error
145 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900146int res_ninit(res_state statp) {
147 extern int __res_vinit(res_state, int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900148
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900149 return (__res_vinit(statp, 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900150}
151
152/* This function has to be reachable by res_data.c but not publicly. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153int __res_vinit(res_state statp, int preinit) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900154#if !defined(__BIONIC__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 register FILE* fp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900156#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 register char *cp, **pp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900158#if !defined(__BIONIC__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900159 register int n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900160#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900161 char buf[BUFSIZ];
162 int nserv = 0; /* number of nameserver records read from file */
Bernie Innocenti55864192018-08-30 04:05:20 +0900163#if !defined(__BIONIC__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 int haveenv = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900165#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 int havesearch = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900167#ifdef RESOLVSORT
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900168 int nsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900169#endif
170#if !defined(__BIONIC__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900171 char* net;
Bernie Innocenti55864192018-08-30 04:05:20 +0900172#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173 int dots;
174 union res_sockaddr_union u[2];
Bernie Innocenti55864192018-08-30 04:05:20 +0900175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176 if ((statp->options & RES_INIT) != 0U) res_ndestroy(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178 if (!preinit) {
179 statp->netid = NETID_UNSET;
180 statp->retrans = RES_TIMEOUT;
181 statp->retry = RES_DFLRETRY;
182 statp->options = RES_DEFAULT;
183 statp->id = res_randomid();
184 statp->_mark = MARK_UNSET;
185 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187 memset(u, 0, sizeof(u));
Bernie Innocenti55864192018-08-30 04:05:20 +0900188#ifdef USELOOPBACK
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900190#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900192#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193 u[nserv].sin.sin_family = AF_INET;
194 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
Bernie Innocenti55864192018-08-30 04:05:20 +0900195#ifdef HAVE_SA_LEN
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
Bernie Innocenti55864192018-08-30 04:05:20 +0900197#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198 nserv++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900199#ifdef HAS_INET6_STRUCTS
200#ifdef USELOOPBACK
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201 u[nserv].sin6.sin6_addr = in6addr_loopback;
Bernie Innocenti55864192018-08-30 04:05:20 +0900202#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203 u[nserv].sin6.sin6_addr = in6addr_any;
Bernie Innocenti55864192018-08-30 04:05:20 +0900204#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900205 u[nserv].sin6.sin6_family = AF_INET6;
206 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
Bernie Innocenti55864192018-08-30 04:05:20 +0900207#ifdef HAVE_SA_LEN
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
Bernie Innocenti55864192018-08-30 04:05:20 +0900209#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900210 nserv++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900211#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 statp->nscount = 0;
213 statp->ndots = 1;
214 statp->pfcode = 0;
215 statp->_vcsock = -1;
216 statp->_flags = 0;
217 statp->qhook = NULL;
218 statp->rhook = NULL;
219 statp->_u._ext.nscount = 0;
220 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
221 if (statp->_u._ext.ext != NULL) {
222 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
223 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
224 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
225 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
226 }
227 statp->nsort = 0;
228 res_setservers(statp, u, nserv);
Bernie Innocenti55864192018-08-30 04:05:20 +0900229
230#if defined(__BIONIC__)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900231 /* Ignore the environment. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900232#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233 /* Allow user to override the local domain definition */
234 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
235 (void) strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
236 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
237 haveenv++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900239 /*
240 * Set search list to be blank-separated strings
241 * from rest of env value. Permits users of LOCALDOMAIN
242 * to still have a search list, and anyone to set the
243 * one that they want to use as an individual (even more
244 * important now that the rfc1535 stuff restricts searches)
245 */
246 cp = statp->defdname;
247 pp = statp->dnsrch;
248 *pp++ = cp;
249 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
250 if (*cp == '\n') /* silly backwards compat */
251 break;
252 else if (*cp == ' ' || *cp == '\t') {
253 *cp = 0;
254 n = 1;
255 } else if (n) {
256 *pp++ = cp;
257 n = 0;
258 havesearch = 1;
259 }
260 }
261 /* null terminate last domain if there are excess */
262 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') cp++;
263 *cp = '\0';
264 *pp++ = 0;
265 }
266 if (nserv > 0) statp->nscount = nserv;
Bernie Innocenti55864192018-08-30 04:05:20 +0900267#endif
268
269#ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270#define MATCH(line, name) \
271 (!strncmp(line, name, sizeof(name) - 1) && \
272 (line[sizeof(name) - 1] == ' ' || line[sizeof(name) - 1] == '\t'))
Bernie Innocenti55864192018-08-30 04:05:20 +0900273
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900274 nserv = 0;
275 if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
276 /* read the config file */
277 while (fgets(buf, sizeof(buf), fp) != NULL) {
278 /* skip comments */
279 if (*buf == ';' || *buf == '#') continue;
280 /* read default domain name */
281 if (MATCH(buf, "domain")) {
282 if (haveenv) /* skip if have from environ */
283 continue;
284 cp = buf + sizeof("domain") - 1;
285 while (*cp == ' ' || *cp == '\t') cp++;
286 if ((*cp == '\0') || (*cp == '\n')) continue;
287 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
288 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
289 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) *cp = '\0';
290 havesearch = 0;
291 continue;
292 }
293 /* set search list */
294 if (MATCH(buf, "search")) {
295 if (haveenv) /* skip if have from environ */
296 continue;
297 cp = buf + sizeof("search") - 1;
298 while (*cp == ' ' || *cp == '\t') cp++;
299 if ((*cp == '\0') || (*cp == '\n')) continue;
300 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
301 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
302 if ((cp = strchr(statp->defdname, '\n')) != NULL) *cp = '\0';
303 /*
304 * Set search list to be blank-separated strings
305 * on rest of line.
306 */
307 cp = statp->defdname;
308 pp = statp->dnsrch;
309 *pp++ = cp;
310 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
311 if (*cp == ' ' || *cp == '\t') {
312 *cp = 0;
313 n = 1;
314 } else if (n) {
315 *pp++ = cp;
316 n = 0;
317 }
318 }
319 /* null terminate last domain if there are excess */
320 while (*cp != '\0' && *cp != ' ' && *cp != '\t') cp++;
321 *cp = '\0';
322 *pp++ = 0;
323 havesearch = 1;
324 continue;
325 }
326 /* read nameservers to query */
327 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
328 struct addrinfo hints, *ai;
329 char sbuf[NI_MAXSERV];
330 const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]);
Bernie Innocenti55864192018-08-30 04:05:20 +0900331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900332 cp = buf + sizeof("nameserver") - 1;
333 while (*cp == ' ' || *cp == '\t') cp++;
334 cp[strcspn(cp, ";# \t\n")] = '\0';
335 if ((*cp != '\0') && (*cp != '\n')) {
336 memset(&hints, 0, sizeof(hints));
337 hints.ai_family = PF_UNSPEC;
338 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
339 hints.ai_flags = AI_NUMERICHOST;
340 sprintf(sbuf, "%u", NAMESERVER_PORT);
341 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && ai->ai_addrlen <= minsiz) {
342 if (statp->_u._ext.ext != NULL) {
343 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr,
344 ai->ai_addrlen);
345 }
346 if (ai->ai_addrlen <= sizeof(statp->nsaddr_list[nserv])) {
347 memcpy(&statp->nsaddr_list[nserv], ai->ai_addr, ai->ai_addrlen);
348 } else
349 statp->nsaddr_list[nserv].sin_family = 0;
350 freeaddrinfo(ai);
351 nserv++;
352 }
353 }
354 continue;
355 }
356 if (MATCH(buf, "sortlist")) {
357 struct in_addr a;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900359 cp = buf + sizeof("sortlist") - 1;
360 while (nsort < MAXRESOLVSORT) {
361 while (*cp == ' ' || *cp == '\t') cp++;
362 if (*cp == '\0' || *cp == '\n' || *cp == ';') break;
363 net = cp;
364 while (*cp && !ISSORTMASK(*cp) && *cp != ';' && isascii(*cp) &&
365 !isspace((unsigned char) *cp))
366 cp++;
367 n = *cp;
368 *cp = 0;
369 if (inet_aton(net, &a)) {
370 statp->sort_list[nsort].addr = a;
371 if (ISSORTMASK(n)) {
372 *cp++ = n;
373 net = cp;
374 while (*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].mask = a.s_addr;
381 } else {
382 statp->sort_list[nsort].mask =
383 net_mask(statp->sort_list[nsort].addr);
384 }
385 } else {
386 statp->sort_list[nsort].mask = net_mask(statp->sort_list[nsort].addr);
387 }
388 nsort++;
389 }
390 *cp = n;
391 }
392 continue;
393 }
394 if (MATCH(buf, "options")) {
395 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
396 continue;
397 }
398 }
399 if (nserv > 0) statp->nscount = nserv;
400 statp->nsort = nsort;
401 (void) fclose(fp);
402 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900403#endif /* !ANDROID_CHANGES */
404/*
405 * Last chance to get a nameserver. This should not normally
406 * be necessary
407 */
408#ifdef NO_RESOLV_CONF
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900409 if (nserv == 0) nserv = get_nameservers(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900410#endif
411
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 if (statp->defdname[0] == 0 && gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
413 (cp = strchr(buf, '.')) != NULL)
414 strcpy(statp->defdname, cp + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900415
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900416 /* find components of local domain that might be searched */
417 if (havesearch == 0) {
418 pp = statp->dnsrch;
419 *pp++ = statp->defdname;
420 *pp = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900421
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900422 dots = 0;
423 for (cp = statp->defdname; *cp; cp++) dots += (*cp == '.');
Bernie Innocenti55864192018-08-30 04:05:20 +0900424
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 cp = statp->defdname;
426 while (pp < statp->dnsrch + MAXDFLSRCH) {
427 if (dots < LOCALDOMAINPARTS) break;
428 cp = strchr(cp, '.') + 1; /* we know there is one */
429 *pp++ = cp;
430 dots--;
431 }
432 *pp = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900434 if (statp->options & RES_DEBUG) {
435 printf(";; res_init()... default dnsrch list:\n");
436 for (pp = statp->dnsrch; *pp; pp++) printf(";;\t%s\n", *pp);
437 printf(";;\t..END..\n");
438 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900439#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 if ((cp = getenv("RES_OPTIONS")) != NULL) res_setoptions(statp, cp, "env");
443 if (nserv > 0) {
444 statp->nscount = nserv;
445 statp->options |= RES_INIT;
446 }
447 return (0);
Bernie Innocenti55864192018-08-30 04:05:20 +0900448}
449
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450static void res_setoptions(res_state statp, const char* options, const char* source) {
451 const char* cp = options;
452 int i;
453 struct __res_state_ext* ext = statp->_u._ext.ext;
Bernie Innocenti55864192018-08-30 04:05:20 +0900454
455#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900456 if (statp->options & RES_DEBUG)
457 printf(";; res_setoptions(\"%s\", \"%s\")...\n", options, source);
Bernie Innocenti55864192018-08-30 04:05:20 +0900458#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 while (*cp) {
460 /* skip leading and inner runs of spaces */
461 while (*cp == ' ' || *cp == '\t') cp++;
462 /* search for and process individual options */
463 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
464 i = atoi(cp + sizeof("ndots:") - 1);
465 if (i <= RES_MAXNDOTS)
466 statp->ndots = i;
467 else
468 statp->ndots = RES_MAXNDOTS;
Bernie Innocenti55864192018-08-30 04:05:20 +0900469#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900470 if (statp->options & RES_DEBUG) printf(";;\tndots=%d\n", statp->ndots);
Bernie Innocenti55864192018-08-30 04:05:20 +0900471#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
473 i = atoi(cp + sizeof("timeout:") - 1);
474 if (i <= RES_MAXRETRANS)
475 statp->retrans = i;
476 else
477 statp->retrans = RES_MAXRETRANS;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 if (statp->options & RES_DEBUG) printf(";;\ttimeout=%d\n", statp->retrans);
Bernie Innocenti55864192018-08-30 04:05:20 +0900480#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)) {
482 i = atoi(cp + sizeof("attempts:") - 1);
483 if (i <= RES_MAXRETRY)
484 statp->retry = i;
485 else
486 statp->retry = RES_MAXRETRY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900487#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 if (statp->options & RES_DEBUG) printf(";;\tattempts=%d\n", statp->retry);
Bernie Innocenti55864192018-08-30 04:05:20 +0900489#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900491#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492 if (!(statp->options & RES_DEBUG)) {
493 printf(";; res_setoptions(\"%s\", \"%s\")..\n", options, source);
494 statp->options |= RES_DEBUG;
495 }
496 printf(";;\tdebug\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900497#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1) ||
499 !strncmp(cp, "no-tld-query", sizeof("no-tld-query") - 1)) {
500 statp->options |= RES_NOTLDQUERY;
501 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
502 statp->options |= RES_USE_INET6;
503 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
504 statp->options |= RES_ROTATE;
505 } else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) {
506 statp->options |= RES_NOCHECKNAME;
507 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900508#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
510 statp->options |= RES_USE_EDNS0;
511 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900512#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
514 statp->options |= RES_USE_DNAME;
515 } else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
516 if (ext == NULL) goto skip;
517 cp += sizeof("nibble:") - 1;
518 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
519 strncpy(ext->nsuffix, cp, (size_t) i);
520 ext->nsuffix[i] = '\0';
521 } else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
522 if (ext == NULL) goto skip;
523 cp += sizeof("nibble2:") - 1;
524 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
525 strncpy(ext->nsuffix2, cp, (size_t) i);
526 ext->nsuffix2[i] = '\0';
527 } else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
528 cp += sizeof("v6revmode:") - 1;
529 /* "nibble" and "bitstring" used to be valid */
530 if (!strncmp(cp, "single", sizeof("single") - 1)) {
531 statp->options |= RES_NO_NIBBLE2;
532 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
533 statp->options &= ~RES_NO_NIBBLE2;
534 }
535 } else {
536 /* XXX - print a warning here? */
537 }
538 skip:
539 /* skip to next run of spaces */
540 while (*cp && *cp != ' ' && *cp != '\t') cp++;
541 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900542}
543
544#ifdef RESOLVSORT
545/* XXX - should really support CIDR which means explicit masks always. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546static uint32_t net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
Bernie Innocenti55864192018-08-30 04:05:20 +0900547{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 register uint32_t i = ntohl(in.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +0900549
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900550 if (IN_CLASSA(i))
551 return (htonl(IN_CLASSA_NET));
552 else if (IN_CLASSB(i))
553 return (htonl(IN_CLASSB_NET));
554 return (htonl(IN_CLASSC_NET));
Bernie Innocenti55864192018-08-30 04:05:20 +0900555}
556#endif
557
558/*%
559 * This routine is for closing the socket if a virtual circuit is used and
560 * the program wants to close it. This provides support for endhostent()
561 * which expects to close the socket.
562 *
563 * This routine is not expected to be user visible.
564 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565void res_nclose(res_state statp) {
566 int ns;
Bernie Innocenti55864192018-08-30 04:05:20 +0900567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 if (statp->_vcsock >= 0) {
569 (void) close(statp->_vcsock);
570 statp->_vcsock = -1;
571 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
572 }
573 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
574 if (statp->_u._ext.nssocks[ns] != -1) {
575 (void) close(statp->_u._ext.nssocks[ns]);
576 statp->_u._ext.nssocks[ns] = -1;
577 }
578 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900579}
580
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900581void res_ndestroy(res_state statp) {
582 res_nclose(statp);
583 if (statp->_u._ext.ext != NULL) free(statp->_u._ext.ext);
584 statp->options &= ~RES_INIT;
585 statp->_u._ext.ext = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900586}
587
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900588const char* res_get_nibblesuffix(res_state statp) {
589 if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix);
590 return ("ip6.arpa");
Bernie Innocenti55864192018-08-30 04:05:20 +0900591}
592
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593const char* res_get_nibblesuffix2(res_state statp) {
594 if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix2);
595 return ("ip6.int");
Bernie Innocenti55864192018-08-30 04:05:20 +0900596}
597
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900598void res_setservers(res_state statp, const union res_sockaddr_union* set, int cnt) {
599 int i, nserv;
600 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900601
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900602 /* close open servers */
603 res_nclose(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 /* cause rtt times to be forgotten */
606 statp->_u._ext.nscount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900607
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 nserv = 0;
609 for (i = 0; i < cnt && nserv < MAXNS; i++) {
610 switch (set->sin.sin_family) {
611 case AF_INET:
612 size = sizeof(set->sin);
613 if (statp->_u._ext.ext)
614 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin, size);
615 if (size <= sizeof(statp->nsaddr_list[nserv]))
616 memcpy(&statp->nsaddr_list[nserv], &set->sin, size);
617 else
618 statp->nsaddr_list[nserv].sin_family = 0;
619 nserv++;
620 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900621
622#ifdef HAS_INET6_STRUCTS
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 case AF_INET6:
624 size = sizeof(set->sin6);
625 if (statp->_u._ext.ext)
626 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin6, size);
627 if (size <= sizeof(statp->nsaddr_list[nserv]))
628 memcpy(&statp->nsaddr_list[nserv], &set->sin6, size);
629 else
630 statp->nsaddr_list[nserv].sin_family = 0;
631 nserv++;
632 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900633#endif
634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635 default:
636 break;
637 }
638 set++;
639 }
640 statp->nscount = nserv;
Bernie Innocenti55864192018-08-30 04:05:20 +0900641}
642
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900643int res_getservers(res_state statp, union res_sockaddr_union* set, int cnt) {
644 int i;
645 size_t size;
646 uint16_t family;
Bernie Innocenti55864192018-08-30 04:05:20 +0900647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 for (i = 0; i < statp->nscount && i < cnt; i++) {
649 if (statp->_u._ext.ext)
650 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
651 else
652 family = statp->nsaddr_list[i].sin_family;
Bernie Innocenti55864192018-08-30 04:05:20 +0900653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900654 switch (family) {
655 case AF_INET:
656 size = sizeof(set->sin);
657 if (statp->_u._ext.ext)
658 memcpy(&set->sin, &statp->_u._ext.ext->nsaddrs[i], size);
659 else
660 memcpy(&set->sin, &statp->nsaddr_list[i], size);
661 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900662
663#ifdef HAS_INET6_STRUCTS
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 case AF_INET6:
665 size = sizeof(set->sin6);
666 if (statp->_u._ext.ext)
667 memcpy(&set->sin6, &statp->_u._ext.ext->nsaddrs[i], size);
668 else
669 memcpy(&set->sin6, &statp->nsaddr_list[i], size);
670 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900671#endif
672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 default:
674 set->sin.sin_family = 0;
675 break;
676 }
677 set++;
678 }
679 return (statp->nscount);
Bernie Innocenti55864192018-08-30 04:05:20 +0900680}
681
682#ifdef ANDROID_CHANGES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683void res_setnetcontext(res_state statp, const struct android_net_context* netcontext) {
684 if (statp != NULL) {
685 statp->netid = netcontext->dns_netid;
686 statp->_mark = netcontext->dns_mark;
687 statp->qhook = netcontext->qhook;
688 if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) {
689 statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC;
690 }
691 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900692}
693
694#endif /* ANDROID_CHANGES */