blob: a5413dd2ad181ae0b31a9a7581011f34aaf40edc [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $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
83
84
85#include <sys/types.h>
86#include <sys/param.h>
87#include <sys/socket.h>
88#include <sys/time.h>
89
90#include <netinet/in.h>
91#include <arpa/inet.h>
Calin Juravle569fb982014-03-04 15:01:29 +000092#include <arpa/nameser.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
94#include <ctype.h>
95#include <stdio.h>
96#include <stdlib.h>
97#include <string.h>
98#include <unistd.h>
99#include <netdb.h>
100
101#ifdef ANDROID_CHANGES
Geremy Condrab23f1932012-05-21 14:20:59 -0700102#include <errno.h>
103#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104#include <sys/system_properties.h>
105#endif /* ANDROID_CHANGES */
106
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
108#ifdef ANDROID_CHANGES
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500109#include "resolv_netid.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110#include "resolv_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111#else
112#include <resolv.h>
113#endif
114
115#include "res_private.h"
116
117/* Options. Should all be left alone. */
118#ifndef DEBUG
119#define DEBUG
120#endif
121
122static void res_setoptions __P((res_state, const char *, const char *));
123
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700124#ifdef RESOLVSORT
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125static const char sort_mask[] = "/&";
126#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700127static uint32_t net_mask(struct in_addr);
128#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129
130#if !defined(isascii) /* XXX - could be a function */
131# define isascii(c) (!(c & 0200))
132#endif
133
134/*
135 * Resolver state default settings.
136 */
137
138/*
139 * Set up default settings. If the configuration file exist, the values
140 * there will have precedence. Otherwise, the server address is set to
141 * INADDR_ANY and the default domain name comes from the gethostname().
142 *
143 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
144 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
145 * since it was noted that INADDR_ANY actually meant ``the first interface
146 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
147 * it had to be "up" in order for you to reach your own name server. It
148 * was later decided that since the recommended practice is to always
149 * install local static routes through 127.0.0.1 for all your network
150 * interfaces, that we could solve this problem without a code change.
151 *
152 * The configuration file should always be used, since it is the only way
153 * to specify a default domain. If you are running a server on your local
154 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
155 * in the configuration file.
156 *
157 * Return 0 if completes successfully, -1 on error
158 */
159int
160res_ninit(res_state statp) {
161 extern int __res_vinit(res_state, int);
162
163 return (__res_vinit(statp, 0));
164}
165
166/* This function has to be reachable by res_data.c but not publicly. */
167int
168__res_vinit(res_state statp, int preinit) {
Elliott Hughesa9209d72016-09-16 18:16:47 -0700169#if !defined(__BIONIC__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170 register FILE *fp;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700171#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172 register char *cp, **pp;
Elliott Hughesa9209d72016-09-16 18:16:47 -0700173#if !defined(__BIONIC__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174 register int n;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700175#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176 char buf[BUFSIZ];
177 int nserv = 0; /* number of nameserver records read from file */
Elliott Hughesa9209d72016-09-16 18:16:47 -0700178#if !defined(__BIONIC__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179 int haveenv = 0;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700180#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181 int havesearch = 0;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700182#ifdef RESOLVSORT
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183 int nsort = 0;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700184#endif
Elliott Hughesa9209d72016-09-16 18:16:47 -0700185#if !defined(__BIONIC__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186 char *net;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700187#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188 int dots;
189 union res_sockaddr_union u[2];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
David 'Digit' Turner4661fda2011-03-17 21:31:33 +0100191 if ((statp->options & RES_INIT) != 0U)
192 res_ndestroy(statp);
193
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194 if (!preinit) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500195 statp->netid = NETID_UNSET;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196 statp->retrans = RES_TIMEOUT;
197 statp->retry = RES_DFLRETRY;
198 statp->options = RES_DEFAULT;
199 statp->id = res_randomid();
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500200 statp->_mark = MARK_UNSET;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201 }
202
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203 memset(u, 0, sizeof(u));
204#ifdef USELOOPBACK
205 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
206#else
207 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
208#endif
209 u[nserv].sin.sin_family = AF_INET;
210 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
211#ifdef HAVE_SA_LEN
212 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
213#endif
214 nserv++;
215#ifdef HAS_INET6_STRUCTS
216#ifdef USELOOPBACK
217 u[nserv].sin6.sin6_addr = in6addr_loopback;
218#else
219 u[nserv].sin6.sin6_addr = in6addr_any;
220#endif
221 u[nserv].sin6.sin6_family = AF_INET6;
222 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
223#ifdef HAVE_SA_LEN
224 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
225#endif
226 nserv++;
227#endif
228 statp->nscount = 0;
229 statp->ndots = 1;
230 statp->pfcode = 0;
231 statp->_vcsock = -1;
232 statp->_flags = 0;
233 statp->qhook = NULL;
234 statp->rhook = NULL;
235 statp->_u._ext.nscount = 0;
236 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
237 if (statp->_u._ext.ext != NULL) {
238 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
239 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
240 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
241 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
242 }
243 statp->nsort = 0;
244 res_setservers(statp, u, nserv);
245
Elliott Hughesa9209d72016-09-16 18:16:47 -0700246#if defined(__BIONIC__)
247 /* Ignore the environment. */
248#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800249 /* Allow user to override the local domain definition */
250 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
251 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
252 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
253 haveenv++;
254
255 /*
256 * Set search list to be blank-separated strings
257 * from rest of env value. Permits users of LOCALDOMAIN
258 * to still have a search list, and anyone to set the
259 * one that they want to use as an individual (even more
260 * important now that the rfc1535 stuff restricts searches)
261 */
262 cp = statp->defdname;
263 pp = statp->dnsrch;
264 *pp++ = cp;
265 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
266 if (*cp == '\n') /* silly backwards compat */
267 break;
268 else if (*cp == ' ' || *cp == '\t') {
269 *cp = 0;
270 n = 1;
271 } else if (n) {
272 *pp++ = cp;
273 n = 0;
274 havesearch = 1;
275 }
276 }
277 /* null terminate last domain if there are excess */
278 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
279 cp++;
280 *cp = '\0';
281 *pp++ = 0;
282 }
283 if (nserv > 0)
284 statp->nscount = nserv;
285#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286
Mattias Falkc63e5902011-08-23 14:34:14 +0200287#ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288#define MATCH(line, name) \
289 (!strncmp(line, name, sizeof(name) - 1) && \
290 (line[sizeof(name) - 1] == ' ' || \
291 line[sizeof(name) - 1] == '\t'))
292
293 nserv = 0;
Elliott Hughesc674edb2014-08-26 15:56:54 -0700294 if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800295 /* read the config file */
296 while (fgets(buf, sizeof(buf), fp) != NULL) {
297 /* skip comments */
298 if (*buf == ';' || *buf == '#')
299 continue;
300 /* read default domain name */
301 if (MATCH(buf, "domain")) {
302 if (haveenv) /* skip if have from environ */
303 continue;
304 cp = buf + sizeof("domain") - 1;
305 while (*cp == ' ' || *cp == '\t')
306 cp++;
307 if ((*cp == '\0') || (*cp == '\n'))
308 continue;
309 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
310 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
311 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
312 *cp = '\0';
313 havesearch = 0;
314 continue;
315 }
316 /* set search list */
317 if (MATCH(buf, "search")) {
318 if (haveenv) /* skip if have from environ */
319 continue;
320 cp = buf + sizeof("search") - 1;
321 while (*cp == ' ' || *cp == '\t')
322 cp++;
323 if ((*cp == '\0') || (*cp == '\n'))
324 continue;
325 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
326 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
327 if ((cp = strchr(statp->defdname, '\n')) != NULL)
328 *cp = '\0';
329 /*
330 * Set search list to be blank-separated strings
331 * on rest of line.
332 */
333 cp = statp->defdname;
334 pp = statp->dnsrch;
335 *pp++ = cp;
336 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
337 if (*cp == ' ' || *cp == '\t') {
338 *cp = 0;
339 n = 1;
340 } else if (n) {
341 *pp++ = cp;
342 n = 0;
343 }
344 }
345 /* null terminate last domain if there are excess */
346 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
347 cp++;
348 *cp = '\0';
349 *pp++ = 0;
350 havesearch = 1;
351 continue;
352 }
353 /* read nameservers to query */
354 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
355 struct addrinfo hints, *ai;
356 char sbuf[NI_MAXSERV];
357 const size_t minsiz =
358 sizeof(statp->_u._ext.ext->nsaddrs[0]);
359
360 cp = buf + sizeof("nameserver") - 1;
361 while (*cp == ' ' || *cp == '\t')
362 cp++;
363 cp[strcspn(cp, ";# \t\n")] = '\0';
364 if ((*cp != '\0') && (*cp != '\n')) {
365 memset(&hints, 0, sizeof(hints));
366 hints.ai_family = PF_UNSPEC;
367 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
368 hints.ai_flags = AI_NUMERICHOST;
369 sprintf(sbuf, "%u", NAMESERVER_PORT);
370 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
371 ai->ai_addrlen <= minsiz) {
372 if (statp->_u._ext.ext != NULL) {
373 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
374 ai->ai_addr, ai->ai_addrlen);
375 }
376 if (ai->ai_addrlen <=
377 sizeof(statp->nsaddr_list[nserv])) {
378 memcpy(&statp->nsaddr_list[nserv],
379 ai->ai_addr, ai->ai_addrlen);
380 } else
381 statp->nsaddr_list[nserv].sin_family = 0;
382 freeaddrinfo(ai);
383 nserv++;
384 }
385 }
386 continue;
387 }
388 if (MATCH(buf, "sortlist")) {
389 struct in_addr a;
390
391 cp = buf + sizeof("sortlist") - 1;
392 while (nsort < MAXRESOLVSORT) {
393 while (*cp == ' ' || *cp == '\t')
394 cp++;
395 if (*cp == '\0' || *cp == '\n' || *cp == ';')
396 break;
397 net = cp;
398 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
399 isascii(*cp) && !isspace((unsigned char)*cp))
400 cp++;
401 n = *cp;
402 *cp = 0;
403 if (inet_aton(net, &a)) {
404 statp->sort_list[nsort].addr = a;
405 if (ISSORTMASK(n)) {
406 *cp++ = n;
407 net = cp;
408 while (*cp && *cp != ';' &&
409 isascii(*cp) &&
410 !isspace((unsigned char)*cp))
411 cp++;
412 n = *cp;
413 *cp = 0;
414 if (inet_aton(net, &a)) {
415 statp->sort_list[nsort].mask = a.s_addr;
416 } else {
417 statp->sort_list[nsort].mask =
418 net_mask(statp->sort_list[nsort].addr);
419 }
420 } else {
421 statp->sort_list[nsort].mask =
422 net_mask(statp->sort_list[nsort].addr);
423 }
424 nsort++;
425 }
426 *cp = n;
427 }
428 continue;
429 }
430 if (MATCH(buf, "options")) {
431 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
432 continue;
433 }
434 }
435 if (nserv > 0)
436 statp->nscount = nserv;
437 statp->nsort = nsort;
438 (void) fclose(fp);
439 }
David 'Digit' Turnerd378c682010-03-08 15:13:04 -0800440#endif /* !ANDROID_CHANGES */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800441/*
442 * Last chance to get a nameserver. This should not normally
443 * be necessary
444 */
445#ifdef NO_RESOLV_CONF
446 if(nserv == 0)
447 nserv = get_nameservers(statp);
448#endif
449
450 if (statp->defdname[0] == 0 &&
451 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
452 (cp = strchr(buf, '.')) != NULL)
453 strcpy(statp->defdname, cp + 1);
454
455 /* find components of local domain that might be searched */
456 if (havesearch == 0) {
457 pp = statp->dnsrch;
458 *pp++ = statp->defdname;
459 *pp = NULL;
460
461 dots = 0;
462 for (cp = statp->defdname; *cp; cp++)
463 dots += (*cp == '.');
464
465 cp = statp->defdname;
466 while (pp < statp->dnsrch + MAXDFLSRCH) {
467 if (dots < LOCALDOMAINPARTS)
468 break;
469 cp = strchr(cp, '.') + 1; /* we know there is one */
470 *pp++ = cp;
471 dots--;
472 }
473 *pp = NULL;
474#ifdef DEBUG
475 if (statp->options & RES_DEBUG) {
476 printf(";; res_init()... default dnsrch list:\n");
477 for (pp = statp->dnsrch; *pp; pp++)
478 printf(";;\t%s\n", *pp);
479 printf(";;\t..END..\n");
480 }
481#endif
482 }
483
484 if ((cp = getenv("RES_OPTIONS")) != NULL)
485 res_setoptions(statp, cp, "env");
486 if (nserv > 0) {
487 statp->nscount = nserv;
488 statp->options |= RES_INIT;
489 }
490 return (0);
491}
492
493static void
494res_setoptions(res_state statp, const char *options, const char *source)
495{
496 const char *cp = options;
497 int i;
498 struct __res_state_ext *ext = statp->_u._ext.ext;
499
500#ifdef DEBUG
501 if (statp->options & RES_DEBUG)
502 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
503 options, source);
504#endif
505 while (*cp) {
506 /* skip leading and inner runs of spaces */
507 while (*cp == ' ' || *cp == '\t')
508 cp++;
509 /* search for and process individual options */
510 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
511 i = atoi(cp + sizeof("ndots:") - 1);
512 if (i <= RES_MAXNDOTS)
513 statp->ndots = i;
514 else
515 statp->ndots = RES_MAXNDOTS;
516#ifdef DEBUG
517 if (statp->options & RES_DEBUG)
518 printf(";;\tndots=%d\n", statp->ndots);
519#endif
520 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
521 i = atoi(cp + sizeof("timeout:") - 1);
522 if (i <= RES_MAXRETRANS)
523 statp->retrans = i;
524 else
525 statp->retrans = RES_MAXRETRANS;
526#ifdef DEBUG
527 if (statp->options & RES_DEBUG)
528 printf(";;\ttimeout=%d\n", statp->retrans);
529#endif
530 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
531 i = atoi(cp + sizeof("attempts:") - 1);
532 if (i <= RES_MAXRETRY)
533 statp->retry = i;
534 else
535 statp->retry = RES_MAXRETRY;
536#ifdef DEBUG
537 if (statp->options & RES_DEBUG)
538 printf(";;\tattempts=%d\n", statp->retry);
539#endif
540 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
541#ifdef DEBUG
542 if (!(statp->options & RES_DEBUG)) {
543 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
544 options, source);
545 statp->options |= RES_DEBUG;
546 }
547 printf(";;\tdebug\n");
548#endif
549 } else if (!strncmp(cp, "no_tld_query",
550 sizeof("no_tld_query") - 1) ||
551 !strncmp(cp, "no-tld-query",
552 sizeof("no-tld-query") - 1)) {
553 statp->options |= RES_NOTLDQUERY;
554 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
555 statp->options |= RES_USE_INET6;
556 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
557 statp->options |= RES_ROTATE;
558 } else if (!strncmp(cp, "no-check-names",
559 sizeof("no-check-names") - 1)) {
560 statp->options |= RES_NOCHECKNAME;
561 }
562#ifdef RES_USE_EDNS0
563 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
564 statp->options |= RES_USE_EDNS0;
565 }
566#endif
567 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
568 statp->options |= RES_USE_DNAME;
569 }
570 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
571 if (ext == NULL)
572 goto skip;
573 cp += sizeof("nibble:") - 1;
574 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
575 strncpy(ext->nsuffix, cp, (size_t)i);
576 ext->nsuffix[i] = '\0';
577 }
578 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
579 if (ext == NULL)
580 goto skip;
581 cp += sizeof("nibble2:") - 1;
582 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
583 strncpy(ext->nsuffix2, cp, (size_t)i);
584 ext->nsuffix2[i] = '\0';
585 }
586 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
587 cp += sizeof("v6revmode:") - 1;
588 /* "nibble" and "bitstring" used to be valid */
589 if (!strncmp(cp, "single", sizeof("single") - 1)) {
590 statp->options |= RES_NO_NIBBLE2;
591 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
592 statp->options &=
593 ~RES_NO_NIBBLE2;
594 }
595 }
596 else {
597 /* XXX - print a warning here? */
598 }
599 skip:
600 /* skip to next run of spaces */
601 while (*cp && *cp != ' ' && *cp != '\t')
602 cp++;
603 }
604}
605
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700606#ifdef RESOLVSORT
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607/* XXX - should really support CIDR which means explicit masks always. */
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700608static uint32_t
609net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610{
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700611 register uint32_t i = ntohl(in.s_addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800612
613 if (IN_CLASSA(i))
614 return (htonl(IN_CLASSA_NET));
615 else if (IN_CLASSB(i))
616 return (htonl(IN_CLASSB_NET));
617 return (htonl(IN_CLASSC_NET));
618}
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700619#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800620
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700621/*%
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800622 * This routine is for closing the socket if a virtual circuit is used and
623 * the program wants to close it. This provides support for endhostent()
624 * which expects to close the socket.
625 *
626 * This routine is not expected to be user visible.
627 */
628void
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700629res_nclose(res_state statp)
630{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800631 int ns;
632
633 if (statp->_vcsock >= 0) {
634 (void) close(statp->_vcsock);
635 statp->_vcsock = -1;
636 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
637 }
638 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
639 if (statp->_u._ext.nssocks[ns] != -1) {
640 (void) close(statp->_u._ext.nssocks[ns]);
641 statp->_u._ext.nssocks[ns] = -1;
642 }
643 }
644}
645
646void
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700647res_ndestroy(res_state statp)
648{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800649 res_nclose(statp);
650 if (statp->_u._ext.ext != NULL)
651 free(statp->_u._ext.ext);
652 statp->options &= ~RES_INIT;
653 statp->_u._ext.ext = NULL;
654}
655
656const char *
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700657res_get_nibblesuffix(res_state statp)
658{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800659 if (statp->_u._ext.ext)
660 return (statp->_u._ext.ext->nsuffix);
661 return ("ip6.arpa");
662}
663
664const char *
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700665res_get_nibblesuffix2(res_state statp)
666{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667 if (statp->_u._ext.ext)
668 return (statp->_u._ext.ext->nsuffix2);
669 return ("ip6.int");
670}
671
672void
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700673res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt)
674{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800675 int i, nserv;
676 size_t size;
677
678 /* close open servers */
679 res_nclose(statp);
680
681 /* cause rtt times to be forgotten */
682 statp->_u._ext.nscount = 0;
683
684 nserv = 0;
685 for (i = 0; i < cnt && nserv < MAXNS; i++) {
686 switch (set->sin.sin_family) {
687 case AF_INET:
688 size = sizeof(set->sin);
689 if (statp->_u._ext.ext)
690 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
691 &set->sin, size);
692 if (size <= sizeof(statp->nsaddr_list[nserv]))
693 memcpy(&statp->nsaddr_list[nserv],
694 &set->sin, size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800695 else
696 statp->nsaddr_list[nserv].sin_family = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697 nserv++;
698 break;
699
700#ifdef HAS_INET6_STRUCTS
701 case AF_INET6:
702 size = sizeof(set->sin6);
703 if (statp->_u._ext.ext)
704 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
705 &set->sin6, size);
706 if (size <= sizeof(statp->nsaddr_list[nserv]))
707 memcpy(&statp->nsaddr_list[nserv],
708 &set->sin6, size);
709 else
710 statp->nsaddr_list[nserv].sin_family = 0;
711 nserv++;
712 break;
713#endif
714
715 default:
716 break;
717 }
718 set++;
719 }
720 statp->nscount = nserv;
721
722}
723
724int
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700725res_getservers(res_state statp, union res_sockaddr_union *set, int cnt)
726{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727 int i;
728 size_t size;
Elliott Hughes37b1b5b2014-07-02 16:27:20 -0700729 uint16_t family;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800730
731 for (i = 0; i < statp->nscount && i < cnt; i++) {
732 if (statp->_u._ext.ext)
733 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
734 else
735 family = statp->nsaddr_list[i].sin_family;
736
737 switch (family) {
738 case AF_INET:
739 size = sizeof(set->sin);
740 if (statp->_u._ext.ext)
741 memcpy(&set->sin,
742 &statp->_u._ext.ext->nsaddrs[i],
743 size);
744 else
745 memcpy(&set->sin, &statp->nsaddr_list[i],
746 size);
747 break;
748
749#ifdef HAS_INET6_STRUCTS
750 case AF_INET6:
751 size = sizeof(set->sin6);
752 if (statp->_u._ext.ext)
753 memcpy(&set->sin6,
754 &statp->_u._ext.ext->nsaddrs[i],
755 size);
756 else
757 memcpy(&set->sin6, &statp->nsaddr_list[i],
758 size);
759 break;
760#endif
761
762 default:
763 set->sin.sin_family = 0;
764 break;
765 }
766 set++;
767 }
768 return (statp->nscount);
769}
770
771#ifdef ANDROID_CHANGES
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500772void res_setnetid(res_state statp, unsigned netid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800773{
Mattias Falkc63e5902011-08-23 14:34:14 +0200774 if (statp != NULL) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500775 statp->netid = netid;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800776 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777}
Chad Brubakerc39214e2013-06-20 10:36:56 -0700778
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500779void res_setmark(res_state statp, unsigned mark)
Chad Brubakerc39214e2013-06-20 10:36:56 -0700780{
781 if (statp != NULL) {
782 statp->_mark = mark;
783 }
784}
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500785
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786#endif /* ANDROID_CHANGES */