1 /* $NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $ */
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
57 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
74 * This version of this file is derived from Android 2.3 "Gingerbread",
75 * which contains uncredited changes by Android/Google developers. It has
76 * been modified in 2011 for use in the Android build of Mozilla Firefox by
77 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
78 * and Steve Workman <sjhworkman@gmail.com>).
79 * These changes are offered under the same license as the original NetBSD
80 * file, whose copyright and license are unchanged above.
83 #define ANDROID_CHANGES 1
84 #define MOZILLE_NECKO_EXCLUDE_CODE 1
86 #include <sys/cdefs.h>
87 #if defined(LIBC_SCCS) && !defined(lint)
89 static const char sccsid
[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
90 static const char rcsid
[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp";
92 __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $");
94 #endif /* LIBC_SCCS and not lint */
98 #include <sys/types.h>
99 #include <sys/param.h>
100 #include <sys/socket.h>
101 #include <sys/time.h>
103 #include <netinet/in.h>
104 #include <arpa/inet.h>
105 #include "arpa_nameser.h"
114 #ifdef ANDROID_CHANGES
115 #include <sys/system_properties.h>
116 #endif /* ANDROID_CHANGES */
119 #define MIN(x,y) ((x)<(y)?(x):(y))
122 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
123 #ifdef ANDROID_CHANGES
124 #include "resolv_private.h"
125 #define MAX_DNS_PROPERTIES 8
126 #define DNS_PROP_NAME_PREFIX "net.dns"
127 #define DNS_CHANGE_PROP_NAME "net.dnschange"
128 #define DNS_SEARCH_PROP_NAME "net.dns.search"
129 const prop_info
*dns_change_prop
;
130 int dns_last_change_counter
;
131 static int _get_dns_change_count();
136 #include "res_private.h"
138 /* Options. Should all be left alone. */
143 static void res_setoptions
__P((res_state
, const char *, const char *));
145 static const char sort_mask
[] = "/&";
146 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
147 static u_int32_t net_mask
__P((struct in_addr
));
149 #if !defined(isascii) /* XXX - could be a function */
150 # define isascii(c) (!(c & 0200))
154 * Resolver state default settings.
158 * Set up default settings. If the configuration file exist, the values
159 * there will have precedence. Otherwise, the server address is set to
160 * INADDR_ANY and the default domain name comes from the gethostname().
162 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
163 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
164 * since it was noted that INADDR_ANY actually meant ``the first interface
165 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
166 * it had to be "up" in order for you to reach your own name server. It
167 * was later decided that since the recommended practice is to always
168 * install local static routes through 127.0.0.1 for all your network
169 * interfaces, that we could solve this problem without a code change.
171 * The configuration file should always be used, since it is the only way
172 * to specify a default domain. If you are running a server on your local
173 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
174 * in the configuration file.
176 * Return 0 if completes successfully, -1 on error
179 res_ninit(res_state statp
) {
180 extern int __res_vinit(res_state
, int);
182 return (__res_vinit(statp
, 0));
185 #ifdef ANDROID_CHANGES
186 int load_domain_search_list(res_state statp
) {
187 char propvalue
[PROP_VALUE_MAX
];
188 register char *cp
, **pp
;
190 if(__system_property_get(DNS_SEARCH_PROP_NAME
, propvalue
) >= 1) {
191 strlcpy(statp
->defdname
, propvalue
, sizeof(statp
->defdname
));
192 if ((cp
= strchr(statp
->defdname
, '\n')) != NULL
)
194 cp
= statp
->defdname
;
196 while ( pp
< statp
->dnsrch
+ MAXDNSRCH
) {
197 while (*cp
== ' ' || *cp
== '\t') /* skip leading white space */
199 if (*cp
== '\0') /* stop if nothing more */
201 *pp
++ = cp
; /* record this search domain */
202 while (*cp
) { /* zero-terminate it */
203 if (*cp
== ' ' || *cp
== '\t') {
210 *pp
= NULL
; /* statp->dnsrch has MAXDNSRCH+1 items */
211 if (pp
> statp
->dnsrch
)
214 statp
->defdname
[0] = '\0'; /* no default domain name on Android */
215 statp
->dnsrch
[0] = NULL
;
220 /* This function has to be reachable by res_data.c but not publicly. */
222 __res_vinit(res_state statp
, int preinit
) {
224 register char *cp
, **pp
;
227 int nserv
= 0; /* number of nameserver records read from file */
233 union res_sockaddr_union u
[2];
234 #ifdef ANDROID_CHANGES
235 pid_t mypid
= getpid();
236 int use_proc_props
= 0;
238 char dnsProperty
[PROP_VALUE_MAX
];
242 statp
->retrans
= RES_TIMEOUT
;
243 statp
->retry
= RES_DFLRETRY
;
244 statp
->options
= RES_DEFAULT
;
245 statp
->id
= res_randomid();
248 if ((statp
->options
& RES_INIT
) != 0U)
251 memset(u
, 0, sizeof(u
));
253 u
[nserv
].sin
.sin_addr
= inet_makeaddr(IN_LOOPBACKNET
, 1);
255 u
[nserv
].sin
.sin_addr
.s_addr
= INADDR_ANY
;
257 u
[nserv
].sin
.sin_family
= AF_INET
;
258 u
[nserv
].sin
.sin_port
= htons(NAMESERVER_PORT
);
260 u
[nserv
].sin
.sin_len
= sizeof(struct sockaddr_in
);
263 #ifdef HAS_INET6_STRUCTS
265 u
[nserv
].sin6
.sin6_addr
= in6addr_loopback
;
267 u
[nserv
].sin6
.sin6_addr
= in6addr_any
;
269 u
[nserv
].sin6
.sin6_family
= AF_INET6
;
270 u
[nserv
].sin6
.sin6_port
= htons(NAMESERVER_PORT
);
272 u
[nserv
].sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
283 statp
->_u
._ext
.nscount
= 0;
284 statp
->_u
._ext
.ext
= malloc(sizeof(*statp
->_u
._ext
.ext
));
285 if (statp
->_u
._ext
.ext
!= NULL
) {
286 memset(statp
->_u
._ext
.ext
, 0, sizeof(*statp
->_u
._ext
.ext
));
287 statp
->_u
._ext
.ext
->nsaddrs
[0].sin
= statp
->nsaddr
;
288 strcpy(statp
->_u
._ext
.ext
->nsuffix
, "ip6.arpa");
289 strcpy(statp
->_u
._ext
.ext
->nsuffix2
, "ip6.int");
292 res_setservers(statp
, u
, nserv
);
294 #if 0 /* IGNORE THE ENVIRONMENT */
295 /* Allow user to override the local domain definition */
296 if ((cp
= getenv("LOCALDOMAIN")) != NULL
) {
297 (void)strncpy(statp
->defdname
, cp
, sizeof(statp
->defdname
) - 1);
298 statp
->defdname
[sizeof(statp
->defdname
) - 1] = '\0';
302 * Set search list to be blank-separated strings
303 * from rest of env value. Permits users of LOCALDOMAIN
304 * to still have a search list, and anyone to set the
305 * one that they want to use as an individual (even more
306 * important now that the rfc1535 stuff restricts searches)
308 cp
= statp
->defdname
;
311 for (n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++) {
312 if (*cp
== '\n') /* silly backwards compat */
314 else if (*cp
== ' ' || *cp
== '\t') {
323 /* null terminate last domain if there are excess */
324 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
330 statp
->nscount
= nserv
;
332 #ifdef ANDROID_CHANGES /* READ FROM SYSTEM PROPERTIES */
333 dns_last_change_counter
= _get_dns_change_count();
336 for(n
= 1; n
<= MAX_DNS_PROPERTIES
&& nserv
< MAXNS
; n
++) {
337 char propname
[PROP_NAME_MAX
];
338 char propvalue
[PROP_VALUE_MAX
];
340 struct addrinfo hints
, *ai
;
341 char sbuf
[NI_MAXSERV
];
342 const size_t minsiz
= sizeof(statp
->_u
._ext
.ext
->nsaddrs
[0]);
345 * Check first for process-specific properties, and if those don't
346 * exist, try the generic properties.
349 if (n
== 1 || use_proc_props
) {
350 snprintf(propname
, sizeof(propname
), "%s%d.%d", DNS_PROP_NAME_PREFIX
, n
, mypid
);
351 if(__system_property_get(propname
, propvalue
) < 1) {
352 if (use_proc_props
) {
361 snprintf(propname
, sizeof(propname
), "%s%d", DNS_PROP_NAME_PREFIX
, n
);
362 if(__system_property_get(propname
, propvalue
) < 1) {
369 while (*cp
== ' ' || *cp
== '\t')
371 cp
[strcspn(cp
, ";# \t\n")] = '\0';
372 if ((*cp
!= '\0') && (*cp
!= '\n')) {
373 memset(&hints
, 0, sizeof(hints
));
374 hints
.ai_family
= PF_UNSPEC
;
375 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
376 hints
.ai_flags
= AI_NUMERICHOST
;
377 sprintf(sbuf
, "%u", NAMESERVER_PORT
);
378 if (getaddrinfo(cp
, sbuf
, &hints
, &ai
) == 0 &&
379 (size_t)ai
->ai_addrlen
<= minsiz
) {
380 if (statp
->_u
._ext
.ext
!= NULL
) {
381 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
382 ai
->ai_addr
, ai
->ai_addrlen
);
384 if ((size_t)ai
->ai_addrlen
<=
385 sizeof(statp
->nsaddr_list
[nserv
])) {
386 memcpy(&statp
->nsaddr_list
[nserv
],
387 ai
->ai_addr
, ai
->ai_addrlen
);
389 statp
->nsaddr_list
[nserv
].sin_family
= 0;
397 /* Add the domain search list */
398 havesearch
= load_domain_search_list(statp
);
399 #else /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
400 #define MATCH(line, name) \
401 (!strncmp(line, name, sizeof(name) - 1) && \
402 (line[sizeof(name) - 1] == ' ' || \
403 line[sizeof(name) - 1] == '\t'))
406 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
407 /* read the config file */
408 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
410 if (*buf
== ';' || *buf
== '#')
412 /* read default domain name */
413 if (MATCH(buf
, "domain")) {
414 if (haveenv
) /* skip if have from environ */
416 cp
= buf
+ sizeof("domain") - 1;
417 while (*cp
== ' ' || *cp
== '\t')
419 if ((*cp
== '\0') || (*cp
== '\n'))
421 strncpy(statp
->defdname
, cp
, sizeof(statp
->defdname
) - 1);
422 statp
->defdname
[sizeof(statp
->defdname
) - 1] = '\0';
423 if ((cp
= strpbrk(statp
->defdname
, " \t\n")) != NULL
)
428 /* set search list */
429 if (MATCH(buf
, "search")) {
430 if (haveenv
) /* skip if have from environ */
432 cp
= buf
+ sizeof("search") - 1;
433 while (*cp
== ' ' || *cp
== '\t')
435 if ((*cp
== '\0') || (*cp
== '\n'))
437 strncpy(statp
->defdname
, cp
, sizeof(statp
->defdname
) - 1);
438 statp
->defdname
[sizeof(statp
->defdname
) - 1] = '\0';
439 if ((cp
= strchr(statp
->defdname
, '\n')) != NULL
)
442 * Set search list to be blank-separated strings
445 cp
= statp
->defdname
;
448 for (n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++) {
449 if (*cp
== ' ' || *cp
== '\t') {
457 /* null terminate last domain if there are excess */
458 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
465 /* read nameservers to query */
466 if (MATCH(buf
, "nameserver") && nserv
< MAXNS
) {
467 struct addrinfo hints
, *ai
;
468 char sbuf
[NI_MAXSERV
];
469 const size_t minsiz
=
470 sizeof(statp
->_u
._ext
.ext
->nsaddrs
[0]);
472 cp
= buf
+ sizeof("nameserver") - 1;
473 while (*cp
== ' ' || *cp
== '\t')
475 cp
[strcspn(cp
, ";# \t\n")] = '\0';
476 if ((*cp
!= '\0') && (*cp
!= '\n')) {
477 memset(&hints
, 0, sizeof(hints
));
478 hints
.ai_family
= PF_UNSPEC
;
479 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
480 hints
.ai_flags
= AI_NUMERICHOST
;
481 sprintf(sbuf
, "%u", NAMESERVER_PORT
);
482 if (getaddrinfo(cp
, sbuf
, &hints
, &ai
) == 0 &&
483 ai
->ai_addrlen
<= minsiz
) {
484 if (statp
->_u
._ext
.ext
!= NULL
) {
485 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
486 ai
->ai_addr
, ai
->ai_addrlen
);
488 if (ai
->ai_addrlen
<=
489 sizeof(statp
->nsaddr_list
[nserv
])) {
490 memcpy(&statp
->nsaddr_list
[nserv
],
491 ai
->ai_addr
, ai
->ai_addrlen
);
493 statp
->nsaddr_list
[nserv
].sin_family
= 0;
500 if (MATCH(buf
, "sortlist")) {
503 cp
= buf
+ sizeof("sortlist") - 1;
504 while (nsort
< MAXRESOLVSORT
) {
505 while (*cp
== ' ' || *cp
== '\t')
507 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
510 while (*cp
&& !ISSORTMASK(*cp
) && *cp
!= ';' &&
511 isascii(*cp
) && !isspace((unsigned char)*cp
))
515 if (inet_aton(net
, &a
)) {
516 statp
->sort_list
[nsort
].addr
= a
;
520 while (*cp
&& *cp
!= ';' &&
522 !isspace((unsigned char)*cp
))
526 if (inet_aton(net
, &a
)) {
527 statp
->sort_list
[nsort
].mask
= a
.s_addr
;
529 statp
->sort_list
[nsort
].mask
=
530 net_mask(statp
->sort_list
[nsort
].addr
);
533 statp
->sort_list
[nsort
].mask
=
534 net_mask(statp
->sort_list
[nsort
].addr
);
542 if (MATCH(buf
, "options")) {
543 res_setoptions(statp
, buf
+ sizeof("options") - 1, "conf");
548 statp
->nscount
= nserv
;
549 statp
->nsort
= nsort
;
552 #endif /* !ANDROID_CHANGES */
554 * Last chance to get a nameserver. This should not normally
557 #ifdef NO_RESOLV_CONF
559 nserv
= get_nameservers(statp
);
562 if (statp
->defdname
[0] == 0 &&
563 gethostname(buf
, sizeof(statp
->defdname
) - 1) == 0 &&
564 (cp
= strchr(buf
, '.')) != NULL
)
565 strcpy(statp
->defdname
, cp
+ 1);
567 /* find components of local domain that might be searched */
568 if (havesearch
== 0) {
570 *pp
++ = statp
->defdname
;
574 for (cp
= statp
->defdname
; *cp
; cp
++)
575 dots
+= (*cp
== '.');
577 cp
= statp
->defdname
;
578 while (pp
< statp
->dnsrch
+ MAXDFLSRCH
) {
579 if (dots
< LOCALDOMAINPARTS
)
581 cp
= strchr(cp
, '.') + 1; /* we know there is one */
587 if (statp
->options
& RES_DEBUG
) {
588 printf(";; res_init()... default dnsrch list:\n");
589 for (pp
= statp
->dnsrch
; *pp
; pp
++)
590 printf(";;\t%s\n", *pp
);
591 printf(";;\t..END..\n");
596 if ((cp
= getenv("RES_OPTIONS")) != NULL
)
597 res_setoptions(statp
, cp
, "env");
599 statp
->nscount
= nserv
;
600 statp
->options
|= RES_INIT
;
606 res_setoptions(res_state statp
, const char *options
, const char *source
)
608 const char *cp
= options
;
610 struct __res_state_ext
*ext
= statp
->_u
._ext
.ext
;
613 if (statp
->options
& RES_DEBUG
)
614 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
618 /* skip leading and inner runs of spaces */
619 while (*cp
== ' ' || *cp
== '\t')
621 /* search for and process individual options */
622 if (!strncmp(cp
, "ndots:", sizeof("ndots:") - 1)) {
623 i
= atoi(cp
+ sizeof("ndots:") - 1);
624 if (i
<= RES_MAXNDOTS
)
627 statp
->ndots
= RES_MAXNDOTS
;
629 if (statp
->options
& RES_DEBUG
)
630 printf(";;\tndots=%d\n", statp
->ndots
);
632 } else if (!strncmp(cp
, "timeout:", sizeof("timeout:") - 1)) {
633 i
= atoi(cp
+ sizeof("timeout:") - 1);
634 if (i
<= RES_MAXRETRANS
)
637 statp
->retrans
= RES_MAXRETRANS
;
639 if (statp
->options
& RES_DEBUG
)
640 printf(";;\ttimeout=%d\n", statp
->retrans
);
642 } else if (!strncmp(cp
, "attempts:", sizeof("attempts:") - 1)){
643 i
= atoi(cp
+ sizeof("attempts:") - 1);
644 if (i
<= RES_MAXRETRY
)
647 statp
->retry
= RES_MAXRETRY
;
649 if (statp
->options
& RES_DEBUG
)
650 printf(";;\tattempts=%d\n", statp
->retry
);
652 } else if (!strncmp(cp
, "debug", sizeof("debug") - 1)) {
654 if (!(statp
->options
& RES_DEBUG
)) {
655 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
657 statp
->options
|= RES_DEBUG
;
659 printf(";;\tdebug\n");
661 } else if (!strncmp(cp
, "no_tld_query",
662 sizeof("no_tld_query") - 1) ||
663 !strncmp(cp
, "no-tld-query",
664 sizeof("no-tld-query") - 1)) {
665 statp
->options
|= RES_NOTLDQUERY
;
666 } else if (!strncmp(cp
, "inet6", sizeof("inet6") - 1)) {
667 statp
->options
|= RES_USE_INET6
;
668 } else if (!strncmp(cp
, "rotate", sizeof("rotate") - 1)) {
669 statp
->options
|= RES_ROTATE
;
670 } else if (!strncmp(cp
, "no-check-names",
671 sizeof("no-check-names") - 1)) {
672 statp
->options
|= RES_NOCHECKNAME
;
675 else if (!strncmp(cp
, "edns0", sizeof("edns0") - 1)) {
676 statp
->options
|= RES_USE_EDNS0
;
679 else if (!strncmp(cp
, "dname", sizeof("dname") - 1)) {
680 statp
->options
|= RES_USE_DNAME
;
682 else if (!strncmp(cp
, "nibble:", sizeof("nibble:") - 1)) {
685 cp
+= sizeof("nibble:") - 1;
686 i
= MIN(strcspn(cp
, " \t"), sizeof(ext
->nsuffix
) - 1);
687 strncpy(ext
->nsuffix
, cp
, (size_t)i
);
688 ext
->nsuffix
[i
] = '\0';
690 else if (!strncmp(cp
, "nibble2:", sizeof("nibble2:") - 1)) {
693 cp
+= sizeof("nibble2:") - 1;
694 i
= MIN(strcspn(cp
, " \t"), sizeof(ext
->nsuffix2
) - 1);
695 strncpy(ext
->nsuffix2
, cp
, (size_t)i
);
696 ext
->nsuffix2
[i
] = '\0';
698 else if (!strncmp(cp
, "v6revmode:", sizeof("v6revmode:") - 1)) {
699 cp
+= sizeof("v6revmode:") - 1;
700 /* "nibble" and "bitstring" used to be valid */
701 if (!strncmp(cp
, "single", sizeof("single") - 1)) {
702 statp
->options
|= RES_NO_NIBBLE2
;
703 } else if (!strncmp(cp
, "both", sizeof("both") - 1)) {
709 /* XXX - print a warning here? */
712 /* skip to next run of spaces */
713 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
718 /* XXX - should really support CIDR which means explicit masks always. */
720 net_mask(in
) /* XXX - should really use system's version of this */
723 register u_int32_t i
= ntohl(in
.s_addr
);
726 return (htonl(IN_CLASSA_NET
));
727 else if (IN_CLASSB(i
))
728 return (htonl(IN_CLASSB_NET
));
729 return (htonl(IN_CLASSC_NET
));
736 gettimeofday(&now
, NULL
);
737 return (0xffff & (now
.tv_sec
^ now
.tv_usec
^ getpid()));
741 * This routine is for closing the socket if a virtual circuit is used and
742 * the program wants to close it. This provides support for endhostent()
743 * which expects to close the socket.
745 * This routine is not expected to be user visible.
748 res_nclose(res_state statp
) {
751 if (statp
->_vcsock
>= 0) {
752 (void) close(statp
->_vcsock
);
754 statp
->_flags
&= ~(RES_F_VC
| RES_F_CONN
);
756 for (ns
= 0; ns
< statp
->_u
._ext
.nscount
; ns
++) {
757 if (statp
->_u
._ext
.nssocks
[ns
] != -1) {
758 (void) close(statp
->_u
._ext
.nssocks
[ns
]);
759 statp
->_u
._ext
.nssocks
[ns
] = -1;
765 res_ndestroy(res_state statp
) {
767 if (statp
->_u
._ext
.ext
!= NULL
)
768 free(statp
->_u
._ext
.ext
);
769 statp
->options
&= ~RES_INIT
;
770 statp
->_u
._ext
.ext
= NULL
;
774 res_get_nibblesuffix(res_state statp
) {
775 if (statp
->_u
._ext
.ext
)
776 return (statp
->_u
._ext
.ext
->nsuffix
);
781 res_get_nibblesuffix2(res_state statp
) {
782 if (statp
->_u
._ext
.ext
)
783 return (statp
->_u
._ext
.ext
->nsuffix2
);
788 res_setservers(res_state statp
, const union res_sockaddr_union
*set
, int cnt
) {
792 /* close open servers */
795 /* cause rtt times to be forgotten */
796 statp
->_u
._ext
.nscount
= 0;
799 for (i
= 0; i
< cnt
&& nserv
< MAXNS
; i
++) {
800 switch (set
->sin
.sin_family
) {
802 size
= sizeof(set
->sin
);
803 if (statp
->_u
._ext
.ext
)
804 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
806 if (size
<= sizeof(statp
->nsaddr_list
[nserv
]))
807 memcpy(&statp
->nsaddr_list
[nserv
],
811 statp
->nsaddr_list
[nserv
].sin_family
= 0;
816 #ifdef HAS_INET6_STRUCTS
818 size
= sizeof(set
->sin6
);
819 if (statp
->_u
._ext
.ext
)
820 memcpy(&statp
->_u
._ext
.ext
->nsaddrs
[nserv
],
822 if (size
<= sizeof(statp
->nsaddr_list
[nserv
]))
823 memcpy(&statp
->nsaddr_list
[nserv
],
826 statp
->nsaddr_list
[nserv
].sin_family
= 0;
836 statp
->nscount
= nserv
;
841 res_getservers(res_state statp
, union res_sockaddr_union
*set
, int cnt
) {
846 for (i
= 0; i
< statp
->nscount
&& i
< cnt
; i
++) {
847 if (statp
->_u
._ext
.ext
)
848 family
= statp
->_u
._ext
.ext
->nsaddrs
[i
].sin
.sin_family
;
850 family
= statp
->nsaddr_list
[i
].sin_family
;
854 size
= sizeof(set
->sin
);
855 if (statp
->_u
._ext
.ext
)
857 &statp
->_u
._ext
.ext
->nsaddrs
[i
],
860 memcpy(&set
->sin
, &statp
->nsaddr_list
[i
],
864 #ifdef HAS_INET6_STRUCTS
866 size
= sizeof(set
->sin6
);
867 if (statp
->_u
._ext
.ext
)
869 &statp
->_u
._ext
.ext
->nsaddrs
[i
],
872 memcpy(&set
->sin6
, &statp
->nsaddr_list
[i
],
878 set
->sin
.sin_family
= 0;
883 return (statp
->nscount
);
886 #ifdef ANDROID_CHANGES
887 static int _get_dns_change_count()
889 if (dns_change_prop
== NULL
) {
890 dns_change_prop
= __system_property_find(DNS_CHANGE_PROP_NAME
);
892 if (dns_change_prop
!= NULL
) {
893 char propvalue
[PROP_VALUE_MAX
];
894 if (__system_property_read(dns_change_prop
, NULL
, propvalue
) >= 1) {
895 return atoi(propvalue
);
901 int res_get_dns_changed()
905 change_count
= _get_dns_change_count();
906 if (change_count
!= dns_last_change_counter
) {
907 if (change_count
!= -1) {
908 dns_last_change_counter
= change_count
;
915 #endif /* ANDROID_CHANGES */