blob: 9f3a9daaa7456ce63f54ae0085a74ff5d20378bf [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
107#ifndef MIN
108#define MIN(x,y) ((x)<(y)?(x):(y))
109#endif
110
111/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
112#ifdef ANDROID_CHANGES
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500113#include "resolv_netid.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114#include "resolv_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115#else
116#include <resolv.h>
117#endif
118
119#include "res_private.h"
120
121/* Options. Should all be left alone. */
122#ifndef DEBUG
123#define DEBUG
124#endif
125
126static void res_setoptions __P((res_state, const char *, const char *));
127
128static const char sort_mask[] = "/&";
129#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
130static u_int32_t net_mask __P((struct in_addr));
131
132#if !defined(isascii) /* XXX - could be a function */
133# define isascii(c) (!(c & 0200))
134#endif
135
136/*
137 * Resolver state default settings.
138 */
139
140/*
141 * Set up default settings. If the configuration file exist, the values
142 * there will have precedence. Otherwise, the server address is set to
143 * INADDR_ANY and the default domain name comes from the gethostname().
144 *
145 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
146 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
147 * since it was noted that INADDR_ANY actually meant ``the first interface
148 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
149 * it had to be "up" in order for you to reach your own name server. It
150 * was later decided that since the recommended practice is to always
151 * install local static routes through 127.0.0.1 for all your network
152 * interfaces, that we could solve this problem without a code change.
153 *
154 * The configuration file should always be used, since it is the only way
155 * to specify a default domain. If you are running a server on your local
156 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
157 * in the configuration file.
158 *
159 * Return 0 if completes successfully, -1 on error
160 */
161int
162res_ninit(res_state statp) {
163 extern int __res_vinit(res_state, int);
164
165 return (__res_vinit(statp, 0));
166}
167
168/* This function has to be reachable by res_data.c but not publicly. */
169int
170__res_vinit(res_state statp, int preinit) {
171 register FILE *fp;
172 register char *cp, **pp;
173 register int n;
174 char buf[BUFSIZ];
175 int nserv = 0; /* number of nameserver records read from file */
176 int haveenv = 0;
177 int havesearch = 0;
178 int nsort = 0;
179 char *net;
180 int dots;
181 union res_sockaddr_union u[2];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
David 'Digit' Turner4661fda2011-03-17 21:31:33 +0100183 if ((statp->options & RES_INIT) != 0U)
184 res_ndestroy(statp);
185
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186 if (!preinit) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500187 statp->netid = NETID_UNSET;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188 statp->retrans = RES_TIMEOUT;
189 statp->retry = RES_DFLRETRY;
190 statp->options = RES_DEFAULT;
191 statp->id = res_randomid();
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500192 statp->_mark = MARK_UNSET;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193 }
194
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195 memset(u, 0, sizeof(u));
196#ifdef USELOOPBACK
197 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
198#else
199 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
200#endif
201 u[nserv].sin.sin_family = AF_INET;
202 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
203#ifdef HAVE_SA_LEN
204 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
205#endif
206 nserv++;
207#ifdef HAS_INET6_STRUCTS
208#ifdef USELOOPBACK
209 u[nserv].sin6.sin6_addr = in6addr_loopback;
210#else
211 u[nserv].sin6.sin6_addr = in6addr_any;
212#endif
213 u[nserv].sin6.sin6_family = AF_INET6;
214 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
215#ifdef HAVE_SA_LEN
216 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
217#endif
218 nserv++;
219#endif
220 statp->nscount = 0;
221 statp->ndots = 1;
222 statp->pfcode = 0;
223 statp->_vcsock = -1;
224 statp->_flags = 0;
225 statp->qhook = NULL;
226 statp->rhook = NULL;
227 statp->_u._ext.nscount = 0;
228 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
229 if (statp->_u._ext.ext != NULL) {
230 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
231 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
232 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
233 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
234 }
235 statp->nsort = 0;
236 res_setservers(statp, u, nserv);
237
238#if 0 /* IGNORE THE ENVIRONMENT */
239 /* Allow user to override the local domain definition */
240 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
241 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
242 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
243 haveenv++;
244
245 /*
246 * Set search list to be blank-separated strings
247 * from rest of env value. Permits users of LOCALDOMAIN
248 * to still have a search list, and anyone to set the
249 * one that they want to use as an individual (even more
250 * important now that the rfc1535 stuff restricts searches)
251 */
252 cp = statp->defdname;
253 pp = statp->dnsrch;
254 *pp++ = cp;
255 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
256 if (*cp == '\n') /* silly backwards compat */
257 break;
258 else if (*cp == ' ' || *cp == '\t') {
259 *cp = 0;
260 n = 1;
261 } else if (n) {
262 *pp++ = cp;
263 n = 0;
264 havesearch = 1;
265 }
266 }
267 /* null terminate last domain if there are excess */
268 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
269 cp++;
270 *cp = '\0';
271 *pp++ = 0;
272 }
273 if (nserv > 0)
274 statp->nscount = nserv;
275#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276
Mattias Falkc63e5902011-08-23 14:34:14 +0200277#ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278#define MATCH(line, name) \
279 (!strncmp(line, name, sizeof(name) - 1) && \
280 (line[sizeof(name) - 1] == ' ' || \
281 line[sizeof(name) - 1] == '\t'))
282
283 nserv = 0;
284 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
285 /* read the config file */
286 while (fgets(buf, sizeof(buf), fp) != NULL) {
287 /* skip comments */
288 if (*buf == ';' || *buf == '#')
289 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')
296 cp++;
297 if ((*cp == '\0') || (*cp == '\n'))
298 continue;
299 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
300 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
301 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
302 *cp = '\0';
303 havesearch = 0;
304 continue;
305 }
306 /* set search list */
307 if (MATCH(buf, "search")) {
308 if (haveenv) /* skip if have from environ */
309 continue;
310 cp = buf + sizeof("search") - 1;
311 while (*cp == ' ' || *cp == '\t')
312 cp++;
313 if ((*cp == '\0') || (*cp == '\n'))
314 continue;
315 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
316 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
317 if ((cp = strchr(statp->defdname, '\n')) != NULL)
318 *cp = '\0';
319 /*
320 * Set search list to be blank-separated strings
321 * on rest of line.
322 */
323 cp = statp->defdname;
324 pp = statp->dnsrch;
325 *pp++ = cp;
326 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
327 if (*cp == ' ' || *cp == '\t') {
328 *cp = 0;
329 n = 1;
330 } else if (n) {
331 *pp++ = cp;
332 n = 0;
333 }
334 }
335 /* null terminate last domain if there are excess */
336 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
337 cp++;
338 *cp = '\0';
339 *pp++ = 0;
340 havesearch = 1;
341 continue;
342 }
343 /* read nameservers to query */
344 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
345 struct addrinfo hints, *ai;
346 char sbuf[NI_MAXSERV];
347 const size_t minsiz =
348 sizeof(statp->_u._ext.ext->nsaddrs[0]);
349
350 cp = buf + sizeof("nameserver") - 1;
351 while (*cp == ' ' || *cp == '\t')
352 cp++;
353 cp[strcspn(cp, ";# \t\n")] = '\0';
354 if ((*cp != '\0') && (*cp != '\n')) {
355 memset(&hints, 0, sizeof(hints));
356 hints.ai_family = PF_UNSPEC;
357 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
358 hints.ai_flags = AI_NUMERICHOST;
359 sprintf(sbuf, "%u", NAMESERVER_PORT);
360 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
361 ai->ai_addrlen <= minsiz) {
362 if (statp->_u._ext.ext != NULL) {
363 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
364 ai->ai_addr, ai->ai_addrlen);
365 }
366 if (ai->ai_addrlen <=
367 sizeof(statp->nsaddr_list[nserv])) {
368 memcpy(&statp->nsaddr_list[nserv],
369 ai->ai_addr, ai->ai_addrlen);
370 } else
371 statp->nsaddr_list[nserv].sin_family = 0;
372 freeaddrinfo(ai);
373 nserv++;
374 }
375 }
376 continue;
377 }
378 if (MATCH(buf, "sortlist")) {
379 struct in_addr a;
380
381 cp = buf + sizeof("sortlist") - 1;
382 while (nsort < MAXRESOLVSORT) {
383 while (*cp == ' ' || *cp == '\t')
384 cp++;
385 if (*cp == '\0' || *cp == '\n' || *cp == ';')
386 break;
387 net = cp;
388 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
389 isascii(*cp) && !isspace((unsigned char)*cp))
390 cp++;
391 n = *cp;
392 *cp = 0;
393 if (inet_aton(net, &a)) {
394 statp->sort_list[nsort].addr = a;
395 if (ISSORTMASK(n)) {
396 *cp++ = n;
397 net = cp;
398 while (*cp && *cp != ';' &&
399 isascii(*cp) &&
400 !isspace((unsigned char)*cp))
401 cp++;
402 n = *cp;
403 *cp = 0;
404 if (inet_aton(net, &a)) {
405 statp->sort_list[nsort].mask = a.s_addr;
406 } else {
407 statp->sort_list[nsort].mask =
408 net_mask(statp->sort_list[nsort].addr);
409 }
410 } else {
411 statp->sort_list[nsort].mask =
412 net_mask(statp->sort_list[nsort].addr);
413 }
414 nsort++;
415 }
416 *cp = n;
417 }
418 continue;
419 }
420 if (MATCH(buf, "options")) {
421 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
422 continue;
423 }
424 }
425 if (nserv > 0)
426 statp->nscount = nserv;
427 statp->nsort = nsort;
428 (void) fclose(fp);
429 }
David 'Digit' Turnerd378c682010-03-08 15:13:04 -0800430#endif /* !ANDROID_CHANGES */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800431/*
432 * Last chance to get a nameserver. This should not normally
433 * be necessary
434 */
435#ifdef NO_RESOLV_CONF
436 if(nserv == 0)
437 nserv = get_nameservers(statp);
438#endif
439
440 if (statp->defdname[0] == 0 &&
441 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
442 (cp = strchr(buf, '.')) != NULL)
443 strcpy(statp->defdname, cp + 1);
444
445 /* find components of local domain that might be searched */
446 if (havesearch == 0) {
447 pp = statp->dnsrch;
448 *pp++ = statp->defdname;
449 *pp = NULL;
450
451 dots = 0;
452 for (cp = statp->defdname; *cp; cp++)
453 dots += (*cp == '.');
454
455 cp = statp->defdname;
456 while (pp < statp->dnsrch + MAXDFLSRCH) {
457 if (dots < LOCALDOMAINPARTS)
458 break;
459 cp = strchr(cp, '.') + 1; /* we know there is one */
460 *pp++ = cp;
461 dots--;
462 }
463 *pp = NULL;
464#ifdef DEBUG
465 if (statp->options & RES_DEBUG) {
466 printf(";; res_init()... default dnsrch list:\n");
467 for (pp = statp->dnsrch; *pp; pp++)
468 printf(";;\t%s\n", *pp);
469 printf(";;\t..END..\n");
470 }
471#endif
472 }
473
474 if ((cp = getenv("RES_OPTIONS")) != NULL)
475 res_setoptions(statp, cp, "env");
476 if (nserv > 0) {
477 statp->nscount = nserv;
478 statp->options |= RES_INIT;
479 }
480 return (0);
481}
482
483static void
484res_setoptions(res_state statp, const char *options, const char *source)
485{
486 const char *cp = options;
487 int i;
488 struct __res_state_ext *ext = statp->_u._ext.ext;
489
490#ifdef DEBUG
491 if (statp->options & RES_DEBUG)
492 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
493 options, source);
494#endif
495 while (*cp) {
496 /* skip leading and inner runs of spaces */
497 while (*cp == ' ' || *cp == '\t')
498 cp++;
499 /* search for and process individual options */
500 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
501 i = atoi(cp + sizeof("ndots:") - 1);
502 if (i <= RES_MAXNDOTS)
503 statp->ndots = i;
504 else
505 statp->ndots = RES_MAXNDOTS;
506#ifdef DEBUG
507 if (statp->options & RES_DEBUG)
508 printf(";;\tndots=%d\n", statp->ndots);
509#endif
510 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
511 i = atoi(cp + sizeof("timeout:") - 1);
512 if (i <= RES_MAXRETRANS)
513 statp->retrans = i;
514 else
515 statp->retrans = RES_MAXRETRANS;
516#ifdef DEBUG
517 if (statp->options & RES_DEBUG)
518 printf(";;\ttimeout=%d\n", statp->retrans);
519#endif
520 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
521 i = atoi(cp + sizeof("attempts:") - 1);
522 if (i <= RES_MAXRETRY)
523 statp->retry = i;
524 else
525 statp->retry = RES_MAXRETRY;
526#ifdef DEBUG
527 if (statp->options & RES_DEBUG)
528 printf(";;\tattempts=%d\n", statp->retry);
529#endif
530 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
531#ifdef DEBUG
532 if (!(statp->options & RES_DEBUG)) {
533 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
534 options, source);
535 statp->options |= RES_DEBUG;
536 }
537 printf(";;\tdebug\n");
538#endif
539 } else if (!strncmp(cp, "no_tld_query",
540 sizeof("no_tld_query") - 1) ||
541 !strncmp(cp, "no-tld-query",
542 sizeof("no-tld-query") - 1)) {
543 statp->options |= RES_NOTLDQUERY;
544 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
545 statp->options |= RES_USE_INET6;
546 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
547 statp->options |= RES_ROTATE;
548 } else if (!strncmp(cp, "no-check-names",
549 sizeof("no-check-names") - 1)) {
550 statp->options |= RES_NOCHECKNAME;
551 }
552#ifdef RES_USE_EDNS0
553 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
554 statp->options |= RES_USE_EDNS0;
555 }
556#endif
557 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
558 statp->options |= RES_USE_DNAME;
559 }
560 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
561 if (ext == NULL)
562 goto skip;
563 cp += sizeof("nibble:") - 1;
564 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
565 strncpy(ext->nsuffix, cp, (size_t)i);
566 ext->nsuffix[i] = '\0';
567 }
568 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
569 if (ext == NULL)
570 goto skip;
571 cp += sizeof("nibble2:") - 1;
572 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
573 strncpy(ext->nsuffix2, cp, (size_t)i);
574 ext->nsuffix2[i] = '\0';
575 }
576 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
577 cp += sizeof("v6revmode:") - 1;
578 /* "nibble" and "bitstring" used to be valid */
579 if (!strncmp(cp, "single", sizeof("single") - 1)) {
580 statp->options |= RES_NO_NIBBLE2;
581 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
582 statp->options &=
583 ~RES_NO_NIBBLE2;
584 }
585 }
586 else {
587 /* XXX - print a warning here? */
588 }
589 skip:
590 /* skip to next run of spaces */
591 while (*cp && *cp != ' ' && *cp != '\t')
592 cp++;
593 }
594}
595
596/* XXX - should really support CIDR which means explicit masks always. */
597static u_int32_t
598net_mask(in) /* XXX - should really use system's version of this */
599 struct in_addr in;
600{
601 register u_int32_t i = ntohl(in.s_addr);
602
603 if (IN_CLASSA(i))
604 return (htonl(IN_CLASSA_NET));
605 else if (IN_CLASSB(i))
606 return (htonl(IN_CLASSB_NET));
607 return (htonl(IN_CLASSC_NET));
608}
609
Geremy Condrab23f1932012-05-21 14:20:59 -0700610#ifdef ANDROID_CHANGES
611static int
612real_randomid(u_int *random_value) {
613 /* open the nonblocking random device, returning -1 on failure */
614 int random_device = open("/dev/urandom", O_RDONLY);
615 if (random_device < 0) {
616 return -1;
617 }
618
619 /* read from the random device, returning -1 on failure (or too many retries)*/
620 u_int retry = 5;
621 for (retry; retry > 0; retry--) {
622 int retval = read(random_device, random_value, sizeof(u_int));
623 if (retval == sizeof(u_int)) {
624 *random_value &= 0xffff;
625 close(random_device);
626 return 0;
627 } else if ((retval < 0) && (errno != EINTR)) {
628 break;
629 }
630 }
631
632 close(random_device);
633 return -1;
634}
635#endif /* ANDROID_CHANGES */
636
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800637u_int
638res_randomid(void) {
Geremy Condrab23f1932012-05-21 14:20:59 -0700639#ifdef ANDROID_CHANGES
640 int status = 0;
641 u_int output = 0;
642 status = real_randomid(&output);
643 if (status != -1) {
644 return output;
645 }
646#endif /* ANDROID_CHANGES */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800647 struct timeval now;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800648 gettimeofday(&now, NULL);
649 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
650}
651
652/*
653 * This routine is for closing the socket if a virtual circuit is used and
654 * the program wants to close it. This provides support for endhostent()
655 * which expects to close the socket.
656 *
657 * This routine is not expected to be user visible.
658 */
659void
660res_nclose(res_state statp) {
661 int ns;
662
663 if (statp->_vcsock >= 0) {
664 (void) close(statp->_vcsock);
665 statp->_vcsock = -1;
666 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
667 }
668 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
669 if (statp->_u._ext.nssocks[ns] != -1) {
670 (void) close(statp->_u._ext.nssocks[ns]);
671 statp->_u._ext.nssocks[ns] = -1;
672 }
673 }
674}
675
676void
677res_ndestroy(res_state statp) {
678 res_nclose(statp);
679 if (statp->_u._ext.ext != NULL)
680 free(statp->_u._ext.ext);
681 statp->options &= ~RES_INIT;
682 statp->_u._ext.ext = NULL;
683}
684
685const char *
686res_get_nibblesuffix(res_state statp) {
687 if (statp->_u._ext.ext)
688 return (statp->_u._ext.ext->nsuffix);
689 return ("ip6.arpa");
690}
691
692const char *
693res_get_nibblesuffix2(res_state statp) {
694 if (statp->_u._ext.ext)
695 return (statp->_u._ext.ext->nsuffix2);
696 return ("ip6.int");
697}
698
699void
700res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
701 int i, nserv;
702 size_t size;
703
704 /* close open servers */
705 res_nclose(statp);
706
707 /* cause rtt times to be forgotten */
708 statp->_u._ext.nscount = 0;
709
710 nserv = 0;
711 for (i = 0; i < cnt && nserv < MAXNS; i++) {
712 switch (set->sin.sin_family) {
713 case AF_INET:
714 size = sizeof(set->sin);
715 if (statp->_u._ext.ext)
716 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
717 &set->sin, size);
718 if (size <= sizeof(statp->nsaddr_list[nserv]))
719 memcpy(&statp->nsaddr_list[nserv],
720 &set->sin, size);
721#ifdef notdef
722 else
723 statp->nsaddr_list[nserv].sin_family = 0;
724#endif
725 nserv++;
726 break;
727
728#ifdef HAS_INET6_STRUCTS
729 case AF_INET6:
730 size = sizeof(set->sin6);
731 if (statp->_u._ext.ext)
732 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
733 &set->sin6, size);
734 if (size <= sizeof(statp->nsaddr_list[nserv]))
735 memcpy(&statp->nsaddr_list[nserv],
736 &set->sin6, size);
737 else
738 statp->nsaddr_list[nserv].sin_family = 0;
739 nserv++;
740 break;
741#endif
742
743 default:
744 break;
745 }
746 set++;
747 }
748 statp->nscount = nserv;
749
750}
751
752int
753res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
754 int i;
755 size_t size;
756 u_int16_t family;
757
758 for (i = 0; i < statp->nscount && i < cnt; i++) {
759 if (statp->_u._ext.ext)
760 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
761 else
762 family = statp->nsaddr_list[i].sin_family;
763
764 switch (family) {
765 case AF_INET:
766 size = sizeof(set->sin);
767 if (statp->_u._ext.ext)
768 memcpy(&set->sin,
769 &statp->_u._ext.ext->nsaddrs[i],
770 size);
771 else
772 memcpy(&set->sin, &statp->nsaddr_list[i],
773 size);
774 break;
775
776#ifdef HAS_INET6_STRUCTS
777 case AF_INET6:
778 size = sizeof(set->sin6);
779 if (statp->_u._ext.ext)
780 memcpy(&set->sin6,
781 &statp->_u._ext.ext->nsaddrs[i],
782 size);
783 else
784 memcpy(&set->sin6, &statp->nsaddr_list[i],
785 size);
786 break;
787#endif
788
789 default:
790 set->sin.sin_family = 0;
791 break;
792 }
793 set++;
794 }
795 return (statp->nscount);
796}
797
798#ifdef ANDROID_CHANGES
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500799void res_setnetid(res_state statp, unsigned netid)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800800{
Mattias Falkc63e5902011-08-23 14:34:14 +0200801 if (statp != NULL) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500802 statp->netid = netid;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800803 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800804}
Chad Brubakerc39214e2013-06-20 10:36:56 -0700805
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500806void res_setmark(res_state statp, unsigned mark)
Chad Brubakerc39214e2013-06-20 10:36:56 -0700807{
808 if (statp != NULL) {
809 statp->_mark = mark;
810 }
811}
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500812
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800813#endif /* ANDROID_CHANGES */