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.
73 #include <sys/cdefs.h>
74 #if defined(LIBC_SCCS) && !defined(lint)
76 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
77 static const char rcsid[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp";
79 __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $");
81 #endif /* LIBC_SCCS and not lint */
85 #include <sys/types.h>
86 #include <sys/param.h>
87 #include <sys/socket.h>
90 #include <netinet/in.h>
91 #include <arpa/inet.h>
92 #include "arpa_nameser.h"
101 #ifdef ANDROID_CHANGES
102 #include <sys/system_properties.h>
103 #endif /* ANDROID_CHANGES */
106 #define MIN(x,y) ((x)<(y)?(x):(y))
109 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
110 #ifdef ANDROID_CHANGES
111 #include "resolv_private.h"
112 #define MAX_DNS_PROPERTIES 8
113 #define DNS_PROP_NAME_PREFIX "net.dns"
114 #define DNS_CHANGE_PROP_NAME "net.dnschange"
115 #define DNS_SEARCH_PROP_NAME "net.dns.search"
116 const prop_info *dns_change_prop;
117 int dns_last_change_counter;
118 static int _get_dns_change_count();
123 #include "res_private.h"
125 /* Options. Should all be left alone. */
130 static void res_setoptions __P((res_state, const char *, const char *));
132 static const char sort_mask[] = "/&";
133 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
134 static u_int32_t net_mask __P((struct in_addr));
136 #if !defined(isascii) /* XXX - could be a function */
137 # define isascii(c) (!(c & 0200))
141 * Resolver state default settings.
145 * Set up default settings. If the configuration file exist, the values
146 * there will have precedence. Otherwise, the server address is set to
147 * INADDR_ANY and the default domain name comes from the gethostname().
149 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
150 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
151 * since it was noted that INADDR_ANY actually meant ``the first interface
152 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
153 * it had to be "up" in order for you to reach your own name server. It
154 * was later decided that since the recommended practice is to always
155 * install local static routes through 127.0.0.1 for all your network
156 * interfaces, that we could solve this problem without a code change.
158 * The configuration file should always be used, since it is the only way
159 * to specify a default domain. If you are running a server on your local
160 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
161 * in the configuration file.
163 * Return 0 if completes successfully, -1 on error
166 res_ninit(res_state statp) {
167 extern int __res_vinit(res_state, int);
169 return (__res_vinit(statp, 0));
172 #ifdef ANDROID_CHANGES
173 int load_domain_search_list(res_state statp) {
174 char propvalue[PROP_VALUE_MAX];
175 register char *cp, **pp;
177 if(__system_property_get(DNS_SEARCH_PROP_NAME, propvalue) >= 1) {
178 strlcpy(statp->defdname, propvalue, sizeof(statp->defdname));
179 if ((cp = strchr(statp->defdname, '\n')) != NULL)
181 cp = statp->defdname;
183 while ( pp < statp->dnsrch + MAXDNSRCH ) {
184 while (*cp == ' ' || *cp == '\t') /* skip leading white space */
186 if (*cp == '\0') /* stop if nothing more */
188 *pp++ = cp; /* record this search domain */
189 while (*cp) { /* zero-terminate it */
190 if (*cp == ' ' || *cp == '\t') {
197 *pp = NULL; /* statp->dnsrch has MAXDNSRCH+1 items */
198 if (pp > statp->dnsrch)
201 statp->defdname[0] = '\0'; /* no default domain name on Android */
202 statp->dnsrch[0] = NULL;
207 /* This function has to be reachable by res_data.c but not publicly. */
209 __res_vinit(res_state statp, int preinit) {
211 register char *cp, **pp;
214 int nserv = 0; /* number of nameserver records read from file */
220 union res_sockaddr_union u[2];
221 #ifdef ANDROID_CHANGES
222 pid_t mypid = getpid();
223 int use_proc_props = 0;
225 char dnsProperty[PROP_VALUE_MAX];
229 statp->retrans = RES_TIMEOUT;
230 statp->retry = RES_DFLRETRY;
231 statp->options = RES_DEFAULT;
232 statp->id = res_randomid();
235 if ((statp->options & RES_INIT) != 0U)
238 memset(u, 0, sizeof(u));
240 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
242 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
244 u[nserv].sin.sin_family = AF_INET;
245 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
247 u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
250 #ifdef HAS_INET6_STRUCTS
252 u[nserv].sin6.sin6_addr = in6addr_loopback;
254 u[nserv].sin6.sin6_addr = in6addr_any;
256 u[nserv].sin6.sin6_family = AF_INET6;
257 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
259 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
270 statp->_u._ext.nscount = 0;
271 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
272 if (statp->_u._ext.ext != NULL) {
273 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
274 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
275 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
276 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
279 res_setservers(statp, u, nserv);
281 #if 0 /* IGNORE THE ENVIRONMENT */
282 /* Allow user to override the local domain definition */
283 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
284 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
285 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
289 * Set search list to be blank-separated strings
290 * from rest of env value. Permits users of LOCALDOMAIN
291 * to still have a search list, and anyone to set the
292 * one that they want to use as an individual (even more
293 * important now that the rfc1535 stuff restricts searches)
295 cp = statp->defdname;
298 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
299 if (*cp == '\n') /* silly backwards compat */
301 else if (*cp == ' ' || *cp == '\t') {
310 /* null terminate last domain if there are excess */
311 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
317 statp->nscount = nserv;
319 #ifdef ANDROID_CHANGES /* READ FROM SYSTEM PROPERTIES */
320 dns_last_change_counter = _get_dns_change_count();
323 for(n = 1; n <= MAX_DNS_PROPERTIES && nserv < MAXNS; n++) {
324 char propname[PROP_NAME_MAX];
325 char propvalue[PROP_VALUE_MAX];
327 struct addrinfo hints, *ai;
328 char sbuf[NI_MAXSERV];
329 const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]);
332 * Check first for process-specific properties, and if those don't
333 * exist, try the generic properties.
336 if (n == 1 || use_proc_props) {
337 snprintf(propname, sizeof(propname), "%s%d.%d", DNS_PROP_NAME_PREFIX, n, mypid);
338 if(__system_property_get(propname, propvalue) < 1) {
339 if (use_proc_props) {
348 snprintf(propname, sizeof(propname), "%s%d", DNS_PROP_NAME_PREFIX, n);
349 if(__system_property_get(propname, propvalue) < 1) {
356 while (*cp == ' ' || *cp == '\t')
358 cp[strcspn(cp, ";# \t\n")] = '\0';
359 if ((*cp != '\0') && (*cp != '\n')) {
360 memset(&hints, 0, sizeof(hints));
361 hints.ai_family = PF_UNSPEC;
362 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
363 hints.ai_flags = AI_NUMERICHOST;
364 sprintf(sbuf, "%u", NAMESERVER_PORT);
365 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
366 (size_t)ai->ai_addrlen <= minsiz) {
367 if (statp->_u._ext.ext != NULL) {
368 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
369 ai->ai_addr, ai->ai_addrlen);
371 if ((size_t)ai->ai_addrlen <=
372 sizeof(statp->nsaddr_list[nserv])) {
373 memcpy(&statp->nsaddr_list[nserv],
374 ai->ai_addr, ai->ai_addrlen);
376 statp->nsaddr_list[nserv].sin_family = 0;
384 /* Add the domain search list */
385 havesearch = load_domain_search_list(statp);
386 #else /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
387 #define MATCH(line, name) \
388 (!strncmp(line, name, sizeof(name) - 1) && \
389 (line[sizeof(name) - 1] == ' ' || \
390 line[sizeof(name) - 1] == '\t'))
393 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
394 /* read the config file */
395 while (fgets(buf, sizeof(buf), fp) != NULL) {
397 if (*buf == ';' || *buf == '#')
399 /* read default domain name */
400 if (MATCH(buf, "domain")) {
401 if (haveenv) /* skip if have from environ */
403 cp = buf + sizeof("domain") - 1;
404 while (*cp == ' ' || *cp == '\t')
406 if ((*cp == '\0') || (*cp == '\n'))
408 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
409 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
410 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
415 /* set search list */
416 if (MATCH(buf, "search")) {
417 if (haveenv) /* skip if have from environ */
419 cp = buf + sizeof("search") - 1;
420 while (*cp == ' ' || *cp == '\t')
422 if ((*cp == '\0') || (*cp == '\n'))
424 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
425 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
426 if ((cp = strchr(statp->defdname, '\n')) != NULL)
429 * Set search list to be blank-separated strings
432 cp = statp->defdname;
435 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
436 if (*cp == ' ' || *cp == '\t') {
444 /* null terminate last domain if there are excess */
445 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
452 /* read nameservers to query */
453 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
454 struct addrinfo hints, *ai;
455 char sbuf[NI_MAXSERV];
456 const size_t minsiz =
457 sizeof(statp->_u._ext.ext->nsaddrs[0]);
459 cp = buf + sizeof("nameserver") - 1;
460 while (*cp == ' ' || *cp == '\t')
462 cp[strcspn(cp, ";# \t\n")] = '\0';
463 if ((*cp != '\0') && (*cp != '\n')) {
464 memset(&hints, 0, sizeof(hints));
465 hints.ai_family = PF_UNSPEC;
466 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
467 hints.ai_flags = AI_NUMERICHOST;
468 sprintf(sbuf, "%u", NAMESERVER_PORT);
469 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
470 ai->ai_addrlen <= minsiz) {
471 if (statp->_u._ext.ext != NULL) {
472 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
473 ai->ai_addr, ai->ai_addrlen);
475 if (ai->ai_addrlen <=
476 sizeof(statp->nsaddr_list[nserv])) {
477 memcpy(&statp->nsaddr_list[nserv],
478 ai->ai_addr, ai->ai_addrlen);
480 statp->nsaddr_list[nserv].sin_family = 0;
487 if (MATCH(buf, "sortlist")) {
490 cp = buf + sizeof("sortlist") - 1;
491 while (nsort < MAXRESOLVSORT) {
492 while (*cp == ' ' || *cp == '\t')
494 if (*cp == '\0' || *cp == '\n' || *cp == ';')
497 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
498 isascii(*cp) && !isspace((unsigned char)*cp))
502 if (inet_aton(net, &a)) {
503 statp->sort_list[nsort].addr = a;
507 while (*cp && *cp != ';' &&
509 !isspace((unsigned char)*cp))
513 if (inet_aton(net, &a)) {
514 statp->sort_list[nsort].mask = a.s_addr;
516 statp->sort_list[nsort].mask =
517 net_mask(statp->sort_list[nsort].addr);
520 statp->sort_list[nsort].mask =
521 net_mask(statp->sort_list[nsort].addr);
529 if (MATCH(buf, "options")) {
530 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
535 statp->nscount = nserv;
536 statp->nsort = nsort;
539 #endif /* !ANDROID_CHANGES */
541 * Last chance to get a nameserver. This should not normally
544 #ifdef NO_RESOLV_CONF
546 nserv = get_nameservers(statp);
549 if (statp->defdname[0] == 0 &&
550 gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
551 (cp = strchr(buf, '.')) != NULL)
552 strcpy(statp->defdname, cp + 1);
554 /* find components of local domain that might be searched */
555 if (havesearch == 0) {
557 *pp++ = statp->defdname;
561 for (cp = statp->defdname; *cp; cp++)
562 dots += (*cp == '.');
564 cp = statp->defdname;
565 while (pp < statp->dnsrch + MAXDFLSRCH) {
566 if (dots < LOCALDOMAINPARTS)
568 cp = strchr(cp, '.') + 1; /* we know there is one */
574 if (statp->options & RES_DEBUG) {
575 printf(";; res_init()... default dnsrch list:\n");
576 for (pp = statp->dnsrch; *pp; pp++)
577 printf(";;\t%s\n", *pp);
578 printf(";;\t..END..\n");
583 if ((cp = getenv("RES_OPTIONS")) != NULL)
584 res_setoptions(statp, cp, "env");
586 statp->nscount = nserv;
587 statp->options |= RES_INIT;
593 res_setoptions(res_state statp, const char *options, const char *source)
595 const char *cp = options;
597 struct __res_state_ext *ext = statp->_u._ext.ext;
600 if (statp->options & RES_DEBUG)
601 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
605 /* skip leading and inner runs of spaces */
606 while (*cp == ' ' || *cp == '\t')
608 /* search for and process individual options */
609 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
610 i = atoi(cp + sizeof("ndots:") - 1);
611 if (i <= RES_MAXNDOTS)
614 statp->ndots = RES_MAXNDOTS;
616 if (statp->options & RES_DEBUG)
617 printf(";;\tndots=%d\n", statp->ndots);
619 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
620 i = atoi(cp + sizeof("timeout:") - 1);
621 if (i <= RES_MAXRETRANS)
624 statp->retrans = RES_MAXRETRANS;
626 if (statp->options & RES_DEBUG)
627 printf(";;\ttimeout=%d\n", statp->retrans);
629 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
630 i = atoi(cp + sizeof("attempts:") - 1);
631 if (i <= RES_MAXRETRY)
634 statp->retry = RES_MAXRETRY;
636 if (statp->options & RES_DEBUG)
637 printf(";;\tattempts=%d\n", statp->retry);
639 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
641 if (!(statp->options & RES_DEBUG)) {
642 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
644 statp->options |= RES_DEBUG;
646 printf(";;\tdebug\n");
648 } else if (!strncmp(cp, "no_tld_query",
649 sizeof("no_tld_query") - 1) ||
650 !strncmp(cp, "no-tld-query",
651 sizeof("no-tld-query") - 1)) {
652 statp->options |= RES_NOTLDQUERY;
653 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
654 statp->options |= RES_USE_INET6;
655 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
656 statp->options |= RES_ROTATE;
657 } else if (!strncmp(cp, "no-check-names",
658 sizeof("no-check-names") - 1)) {
659 statp->options |= RES_NOCHECKNAME;
662 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
663 statp->options |= RES_USE_EDNS0;
666 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
667 statp->options |= RES_USE_DNAME;
669 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
672 cp += sizeof("nibble:") - 1;
673 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
674 strncpy(ext->nsuffix, cp, (size_t)i);
675 ext->nsuffix[i] = '\0';
677 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
680 cp += sizeof("nibble2:") - 1;
681 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
682 strncpy(ext->nsuffix2, cp, (size_t)i);
683 ext->nsuffix2[i] = '\0';
685 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
686 cp += sizeof("v6revmode:") - 1;
687 /* "nibble" and "bitstring" used to be valid */
688 if (!strncmp(cp, "single", sizeof("single") - 1)) {
689 statp->options |= RES_NO_NIBBLE2;
690 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
696 /* XXX - print a warning here? */
699 /* skip to next run of spaces */
700 while (*cp && *cp != ' ' && *cp != '\t')
705 /* XXX - should really support CIDR which means explicit masks always. */
707 net_mask(in) /* XXX - should really use system's version of this */
710 register u_int32_t i = ntohl(in.s_addr);
713 return (htonl(IN_CLASSA_NET));
714 else if (IN_CLASSB(i))
715 return (htonl(IN_CLASSB_NET));
716 return (htonl(IN_CLASSC_NET));
723 gettimeofday(&now, NULL);
724 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
728 * This routine is for closing the socket if a virtual circuit is used and
729 * the program wants to close it. This provides support for endhostent()
730 * which expects to close the socket.
732 * This routine is not expected to be user visible.
735 res_nclose(res_state statp) {
738 if (statp->_vcsock >= 0) {
739 (void) close(statp->_vcsock);
741 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
743 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
744 if (statp->_u._ext.nssocks[ns] != -1) {
745 (void) close(statp->_u._ext.nssocks[ns]);
746 statp->_u._ext.nssocks[ns] = -1;
752 res_ndestroy(res_state statp) {
754 if (statp->_u._ext.ext != NULL)
755 free(statp->_u._ext.ext);
756 statp->options &= ~RES_INIT;
757 statp->_u._ext.ext = NULL;
761 res_get_nibblesuffix(res_state statp) {
762 if (statp->_u._ext.ext)
763 return (statp->_u._ext.ext->nsuffix);
768 res_get_nibblesuffix2(res_state statp) {
769 if (statp->_u._ext.ext)
770 return (statp->_u._ext.ext->nsuffix2);
775 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
779 /* close open servers */
782 /* cause rtt times to be forgotten */
783 statp->_u._ext.nscount = 0;
786 for (i = 0; i < cnt && nserv < MAXNS; i++) {
787 switch (set->sin.sin_family) {
789 size = sizeof(set->sin);
790 if (statp->_u._ext.ext)
791 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
793 if (size <= sizeof(statp->nsaddr_list[nserv]))
794 memcpy(&statp->nsaddr_list[nserv],
798 statp->nsaddr_list[nserv].sin_family = 0;
803 #ifdef HAS_INET6_STRUCTS
805 size = sizeof(set->sin6);
806 if (statp->_u._ext.ext)
807 memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
809 if (size <= sizeof(statp->nsaddr_list[nserv]))
810 memcpy(&statp->nsaddr_list[nserv],
813 statp->nsaddr_list[nserv].sin_family = 0;
823 statp->nscount = nserv;
828 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
833 for (i = 0; i < statp->nscount && i < cnt; i++) {
834 if (statp->_u._ext.ext)
835 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
837 family = statp->nsaddr_list[i].sin_family;
841 size = sizeof(set->sin);
842 if (statp->_u._ext.ext)
844 &statp->_u._ext.ext->nsaddrs[i],
847 memcpy(&set->sin, &statp->nsaddr_list[i],
851 #ifdef HAS_INET6_STRUCTS
853 size = sizeof(set->sin6);
854 if (statp->_u._ext.ext)
856 &statp->_u._ext.ext->nsaddrs[i],
859 memcpy(&set->sin6, &statp->nsaddr_list[i],
865 set->sin.sin_family = 0;
870 return (statp->nscount);
873 #ifdef ANDROID_CHANGES
874 static int _get_dns_change_count()
876 if (dns_change_prop == NULL) {
877 dns_change_prop = __system_property_find(DNS_CHANGE_PROP_NAME);
879 if (dns_change_prop != NULL) {
880 char propvalue[PROP_VALUE_MAX];
881 if (__system_property_read(dns_change_prop, NULL, propvalue) >= 1) {
882 return atoi(propvalue);
888 int res_get_dns_changed()
892 change_count = _get_dns_change_count();
893 if (change_count != dns_last_change_counter) {
894 if (change_count != -1) {
895 dns_last_change_counter = change_count;
902 #endif /* ANDROID_CHANGES */