michael@0: /* $NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 1985, 1989, 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. All advertising materials mentioning features or use of this software michael@0: * must display the following acknowledgement: michael@0: * This product includes software developed by the University of michael@0: * California, Berkeley and its contributors. michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: /* michael@0: * Portions Copyright (c) 1993 by Digital Equipment Corporation. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies, and that michael@0: * the name of Digital Equipment Corporation not be used in advertising or michael@0: * publicity pertaining to distribution of the document or software without michael@0: * specific, written prior permission. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL michael@0: * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT michael@0: * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL michael@0: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR michael@0: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS michael@0: * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS michael@0: * SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Portions Copyright (c) 1996-1999 by Internet Software Consortium. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLE_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: #ifdef notdef michael@0: static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; michael@0: static const char rcsid[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp"; michael@0: #else michael@0: __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $"); michael@0: #endif michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include "arpa_nameser.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef ANDROID_CHANGES michael@0: #include michael@0: #endif /* ANDROID_CHANGES */ michael@0: michael@0: #ifndef MIN michael@0: #define MIN(x,y) ((x)<(y)?(x):(y)) michael@0: #endif michael@0: michael@0: /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ michael@0: #ifdef ANDROID_CHANGES michael@0: #include "resolv_private.h" michael@0: #define MAX_DNS_PROPERTIES 8 michael@0: #define DNS_PROP_NAME_PREFIX "net.dns" michael@0: #define DNS_CHANGE_PROP_NAME "net.dnschange" michael@0: #define DNS_SEARCH_PROP_NAME "net.dns.search" michael@0: const prop_info *dns_change_prop; michael@0: int dns_last_change_counter; michael@0: static int _get_dns_change_count(); michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: #include "res_private.h" michael@0: michael@0: /* Options. Should all be left alone. */ michael@0: #ifndef DEBUG michael@0: #define DEBUG michael@0: #endif michael@0: michael@0: static void res_setoptions __P((res_state, const char *, const char *)); michael@0: michael@0: static const char sort_mask[] = "/&"; michael@0: #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) michael@0: static u_int32_t net_mask __P((struct in_addr)); michael@0: michael@0: #if !defined(isascii) /* XXX - could be a function */ michael@0: # define isascii(c) (!(c & 0200)) michael@0: #endif michael@0: michael@0: /* michael@0: * Resolver state default settings. michael@0: */ michael@0: michael@0: /* michael@0: * Set up default settings. If the configuration file exist, the values michael@0: * there will have precedence. Otherwise, the server address is set to michael@0: * INADDR_ANY and the default domain name comes from the gethostname(). michael@0: * michael@0: * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 michael@0: * rather than INADDR_ANY ("0.0.0.0") as the default name server address michael@0: * since it was noted that INADDR_ANY actually meant ``the first interface michael@0: * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, michael@0: * it had to be "up" in order for you to reach your own name server. It michael@0: * was later decided that since the recommended practice is to always michael@0: * install local static routes through 127.0.0.1 for all your network michael@0: * interfaces, that we could solve this problem without a code change. michael@0: * michael@0: * The configuration file should always be used, since it is the only way michael@0: * to specify a default domain. If you are running a server on your local michael@0: * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" michael@0: * in the configuration file. michael@0: * michael@0: * Return 0 if completes successfully, -1 on error michael@0: */ michael@0: int michael@0: res_ninit(res_state statp) { michael@0: extern int __res_vinit(res_state, int); michael@0: michael@0: return (__res_vinit(statp, 0)); michael@0: } michael@0: michael@0: #ifdef ANDROID_CHANGES michael@0: int load_domain_search_list(res_state statp) { michael@0: char propvalue[PROP_VALUE_MAX]; michael@0: register char *cp, **pp; michael@0: michael@0: if(__system_property_get(DNS_SEARCH_PROP_NAME, propvalue) >= 1) { michael@0: strlcpy(statp->defdname, propvalue, sizeof(statp->defdname)); michael@0: if ((cp = strchr(statp->defdname, '\n')) != NULL) michael@0: *cp = '\0'; michael@0: cp = statp->defdname; michael@0: pp = statp->dnsrch; michael@0: while ( pp < statp->dnsrch + MAXDNSRCH ) { michael@0: while (*cp == ' ' || *cp == '\t') /* skip leading white space */ michael@0: cp++; michael@0: if (*cp == '\0') /* stop if nothing more */ michael@0: break; michael@0: *pp++ = cp; /* record this search domain */ michael@0: while (*cp) { /* zero-terminate it */ michael@0: if (*cp == ' ' || *cp == '\t') { michael@0: *cp++ = '\0'; michael@0: break; michael@0: } michael@0: cp++; michael@0: } michael@0: } michael@0: *pp = NULL; /* statp->dnsrch has MAXDNSRCH+1 items */ michael@0: if (pp > statp->dnsrch) michael@0: return 1; michael@0: } michael@0: statp->defdname[0] = '\0'; /* no default domain name on Android */ michael@0: statp->dnsrch[0] = NULL; michael@0: return 0; michael@0: } michael@0: #endif michael@0: michael@0: /* This function has to be reachable by res_data.c but not publicly. */ michael@0: int michael@0: __res_vinit(res_state statp, int preinit) { michael@0: register FILE *fp; michael@0: register char *cp, **pp; michael@0: register int n; michael@0: char buf[BUFSIZ]; michael@0: int nserv = 0; /* number of nameserver records read from file */ michael@0: int haveenv = 0; michael@0: int havesearch = 0; michael@0: int nsort = 0; michael@0: char *net; michael@0: int dots; michael@0: union res_sockaddr_union u[2]; michael@0: #ifdef ANDROID_CHANGES michael@0: pid_t mypid = getpid(); michael@0: int use_proc_props = 0; michael@0: int found_prop; michael@0: char dnsProperty[PROP_VALUE_MAX]; michael@0: #endif michael@0: michael@0: if (!preinit) { michael@0: statp->retrans = RES_TIMEOUT; michael@0: statp->retry = RES_DFLRETRY; michael@0: statp->options = RES_DEFAULT; michael@0: statp->id = res_randomid(); michael@0: } michael@0: michael@0: if ((statp->options & RES_INIT) != 0U) michael@0: res_ndestroy(statp); michael@0: michael@0: memset(u, 0, sizeof(u)); michael@0: #ifdef USELOOPBACK michael@0: u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); michael@0: #else michael@0: u[nserv].sin.sin_addr.s_addr = INADDR_ANY; michael@0: #endif michael@0: u[nserv].sin.sin_family = AF_INET; michael@0: u[nserv].sin.sin_port = htons(NAMESERVER_PORT); michael@0: #ifdef HAVE_SA_LEN michael@0: u[nserv].sin.sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: nserv++; michael@0: #ifdef HAS_INET6_STRUCTS michael@0: #ifdef USELOOPBACK michael@0: u[nserv].sin6.sin6_addr = in6addr_loopback; michael@0: #else michael@0: u[nserv].sin6.sin6_addr = in6addr_any; michael@0: #endif michael@0: u[nserv].sin6.sin6_family = AF_INET6; michael@0: u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); michael@0: #ifdef HAVE_SA_LEN michael@0: u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: nserv++; michael@0: #endif michael@0: statp->nscount = 0; michael@0: statp->ndots = 1; michael@0: statp->pfcode = 0; michael@0: statp->_vcsock = -1; michael@0: statp->_flags = 0; michael@0: statp->qhook = NULL; michael@0: statp->rhook = NULL; michael@0: statp->_u._ext.nscount = 0; michael@0: statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); michael@0: if (statp->_u._ext.ext != NULL) { michael@0: memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); michael@0: statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; michael@0: strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); michael@0: strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); michael@0: } michael@0: statp->nsort = 0; michael@0: res_setservers(statp, u, nserv); michael@0: michael@0: #if 0 /* IGNORE THE ENVIRONMENT */ michael@0: /* Allow user to override the local domain definition */ michael@0: if ((cp = getenv("LOCALDOMAIN")) != NULL) { michael@0: (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); michael@0: statp->defdname[sizeof(statp->defdname) - 1] = '\0'; michael@0: haveenv++; michael@0: michael@0: /* michael@0: * Set search list to be blank-separated strings michael@0: * from rest of env value. Permits users of LOCALDOMAIN michael@0: * to still have a search list, and anyone to set the michael@0: * one that they want to use as an individual (even more michael@0: * important now that the rfc1535 stuff restricts searches) michael@0: */ michael@0: cp = statp->defdname; michael@0: pp = statp->dnsrch; michael@0: *pp++ = cp; michael@0: for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { michael@0: if (*cp == '\n') /* silly backwards compat */ michael@0: break; michael@0: else if (*cp == ' ' || *cp == '\t') { michael@0: *cp = 0; michael@0: n = 1; michael@0: } else if (n) { michael@0: *pp++ = cp; michael@0: n = 0; michael@0: havesearch = 1; michael@0: } michael@0: } michael@0: /* null terminate last domain if there are excess */ michael@0: while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') michael@0: cp++; michael@0: *cp = '\0'; michael@0: *pp++ = 0; michael@0: } michael@0: if (nserv > 0) michael@0: statp->nscount = nserv; michael@0: #endif michael@0: #ifdef ANDROID_CHANGES /* READ FROM SYSTEM PROPERTIES */ michael@0: dns_last_change_counter = _get_dns_change_count(); michael@0: michael@0: nserv = 0; michael@0: for(n = 1; n <= MAX_DNS_PROPERTIES && nserv < MAXNS; n++) { michael@0: char propname[PROP_NAME_MAX]; michael@0: char propvalue[PROP_VALUE_MAX]; michael@0: michael@0: struct addrinfo hints, *ai; michael@0: char sbuf[NI_MAXSERV]; michael@0: const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]); michael@0: michael@0: /* michael@0: * Check first for process-specific properties, and if those don't michael@0: * exist, try the generic properties. michael@0: */ michael@0: found_prop = 0; michael@0: if (n == 1 || use_proc_props) { michael@0: snprintf(propname, sizeof(propname), "%s%d.%d", DNS_PROP_NAME_PREFIX, n, mypid); michael@0: if(__system_property_get(propname, propvalue) < 1) { michael@0: if (use_proc_props) { michael@0: break; michael@0: } michael@0: } else { michael@0: found_prop = 1; michael@0: use_proc_props = 1; michael@0: } michael@0: } michael@0: if (!found_prop) { michael@0: snprintf(propname, sizeof(propname), "%s%d", DNS_PROP_NAME_PREFIX, n); michael@0: if(__system_property_get(propname, propvalue) < 1) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: cp = propvalue; michael@0: michael@0: while (*cp == ' ' || *cp == '\t') michael@0: cp++; michael@0: cp[strcspn(cp, ";# \t\n")] = '\0'; michael@0: if ((*cp != '\0') && (*cp != '\n')) { michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_socktype = SOCK_DGRAM; /*dummy*/ michael@0: hints.ai_flags = AI_NUMERICHOST; michael@0: sprintf(sbuf, "%u", NAMESERVER_PORT); michael@0: if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && michael@0: (size_t)ai->ai_addrlen <= minsiz) { michael@0: if (statp->_u._ext.ext != NULL) { michael@0: memcpy(&statp->_u._ext.ext->nsaddrs[nserv], michael@0: ai->ai_addr, ai->ai_addrlen); michael@0: } michael@0: if ((size_t)ai->ai_addrlen <= michael@0: sizeof(statp->nsaddr_list[nserv])) { michael@0: memcpy(&statp->nsaddr_list[nserv], michael@0: ai->ai_addr, ai->ai_addrlen); michael@0: } else { michael@0: statp->nsaddr_list[nserv].sin_family = 0; michael@0: } michael@0: freeaddrinfo(ai); michael@0: nserv++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Add the domain search list */ michael@0: havesearch = load_domain_search_list(statp); michael@0: #else /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */ michael@0: #define MATCH(line, name) \ michael@0: (!strncmp(line, name, sizeof(name) - 1) && \ michael@0: (line[sizeof(name) - 1] == ' ' || \ michael@0: line[sizeof(name) - 1] == '\t')) michael@0: michael@0: nserv = 0; michael@0: if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { michael@0: /* read the config file */ michael@0: while (fgets(buf, sizeof(buf), fp) != NULL) { michael@0: /* skip comments */ michael@0: if (*buf == ';' || *buf == '#') michael@0: continue; michael@0: /* read default domain name */ michael@0: if (MATCH(buf, "domain")) { michael@0: if (haveenv) /* skip if have from environ */ michael@0: continue; michael@0: cp = buf + sizeof("domain") - 1; michael@0: while (*cp == ' ' || *cp == '\t') michael@0: cp++; michael@0: if ((*cp == '\0') || (*cp == '\n')) michael@0: continue; michael@0: strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); michael@0: statp->defdname[sizeof(statp->defdname) - 1] = '\0'; michael@0: if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) michael@0: *cp = '\0'; michael@0: havesearch = 0; michael@0: continue; michael@0: } michael@0: /* set search list */ michael@0: if (MATCH(buf, "search")) { michael@0: if (haveenv) /* skip if have from environ */ michael@0: continue; michael@0: cp = buf + sizeof("search") - 1; michael@0: while (*cp == ' ' || *cp == '\t') michael@0: cp++; michael@0: if ((*cp == '\0') || (*cp == '\n')) michael@0: continue; michael@0: strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); michael@0: statp->defdname[sizeof(statp->defdname) - 1] = '\0'; michael@0: if ((cp = strchr(statp->defdname, '\n')) != NULL) michael@0: *cp = '\0'; michael@0: /* michael@0: * Set search list to be blank-separated strings michael@0: * on rest of line. michael@0: */ michael@0: cp = statp->defdname; michael@0: pp = statp->dnsrch; michael@0: *pp++ = cp; michael@0: for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { michael@0: if (*cp == ' ' || *cp == '\t') { michael@0: *cp = 0; michael@0: n = 1; michael@0: } else if (n) { michael@0: *pp++ = cp; michael@0: n = 0; michael@0: } michael@0: } michael@0: /* null terminate last domain if there are excess */ michael@0: while (*cp != '\0' && *cp != ' ' && *cp != '\t') michael@0: cp++; michael@0: *cp = '\0'; michael@0: *pp++ = 0; michael@0: havesearch = 1; michael@0: continue; michael@0: } michael@0: /* read nameservers to query */ michael@0: if (MATCH(buf, "nameserver") && nserv < MAXNS) { michael@0: struct addrinfo hints, *ai; michael@0: char sbuf[NI_MAXSERV]; michael@0: const size_t minsiz = michael@0: sizeof(statp->_u._ext.ext->nsaddrs[0]); michael@0: michael@0: cp = buf + sizeof("nameserver") - 1; michael@0: while (*cp == ' ' || *cp == '\t') michael@0: cp++; michael@0: cp[strcspn(cp, ";# \t\n")] = '\0'; michael@0: if ((*cp != '\0') && (*cp != '\n')) { michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_socktype = SOCK_DGRAM; /*dummy*/ michael@0: hints.ai_flags = AI_NUMERICHOST; michael@0: sprintf(sbuf, "%u", NAMESERVER_PORT); michael@0: if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && michael@0: ai->ai_addrlen <= minsiz) { michael@0: if (statp->_u._ext.ext != NULL) { michael@0: memcpy(&statp->_u._ext.ext->nsaddrs[nserv], michael@0: ai->ai_addr, ai->ai_addrlen); michael@0: } michael@0: if (ai->ai_addrlen <= michael@0: sizeof(statp->nsaddr_list[nserv])) { michael@0: memcpy(&statp->nsaddr_list[nserv], michael@0: ai->ai_addr, ai->ai_addrlen); michael@0: } else michael@0: statp->nsaddr_list[nserv].sin_family = 0; michael@0: freeaddrinfo(ai); michael@0: nserv++; michael@0: } michael@0: } michael@0: continue; michael@0: } michael@0: if (MATCH(buf, "sortlist")) { michael@0: struct in_addr a; michael@0: michael@0: cp = buf + sizeof("sortlist") - 1; michael@0: while (nsort < MAXRESOLVSORT) { michael@0: while (*cp == ' ' || *cp == '\t') michael@0: cp++; michael@0: if (*cp == '\0' || *cp == '\n' || *cp == ';') michael@0: break; michael@0: net = cp; michael@0: while (*cp && !ISSORTMASK(*cp) && *cp != ';' && michael@0: isascii(*cp) && !isspace((unsigned char)*cp)) michael@0: cp++; michael@0: n = *cp; michael@0: *cp = 0; michael@0: if (inet_aton(net, &a)) { michael@0: statp->sort_list[nsort].addr = a; michael@0: if (ISSORTMASK(n)) { michael@0: *cp++ = n; michael@0: net = cp; michael@0: while (*cp && *cp != ';' && michael@0: isascii(*cp) && michael@0: !isspace((unsigned char)*cp)) michael@0: cp++; michael@0: n = *cp; michael@0: *cp = 0; michael@0: if (inet_aton(net, &a)) { michael@0: statp->sort_list[nsort].mask = a.s_addr; michael@0: } else { michael@0: statp->sort_list[nsort].mask = michael@0: net_mask(statp->sort_list[nsort].addr); michael@0: } michael@0: } else { michael@0: statp->sort_list[nsort].mask = michael@0: net_mask(statp->sort_list[nsort].addr); michael@0: } michael@0: nsort++; michael@0: } michael@0: *cp = n; michael@0: } michael@0: continue; michael@0: } michael@0: if (MATCH(buf, "options")) { michael@0: res_setoptions(statp, buf + sizeof("options") - 1, "conf"); michael@0: continue; michael@0: } michael@0: } michael@0: if (nserv > 0) michael@0: statp->nscount = nserv; michael@0: statp->nsort = nsort; michael@0: (void) fclose(fp); michael@0: } michael@0: #endif /* !ANDROID_CHANGES */ michael@0: /* michael@0: * Last chance to get a nameserver. This should not normally michael@0: * be necessary michael@0: */ michael@0: #ifdef NO_RESOLV_CONF michael@0: if(nserv == 0) michael@0: nserv = get_nameservers(statp); michael@0: #endif michael@0: michael@0: if (statp->defdname[0] == 0 && michael@0: gethostname(buf, sizeof(statp->defdname) - 1) == 0 && michael@0: (cp = strchr(buf, '.')) != NULL) michael@0: strcpy(statp->defdname, cp + 1); michael@0: michael@0: /* find components of local domain that might be searched */ michael@0: if (havesearch == 0) { michael@0: pp = statp->dnsrch; michael@0: *pp++ = statp->defdname; michael@0: *pp = NULL; michael@0: michael@0: dots = 0; michael@0: for (cp = statp->defdname; *cp; cp++) michael@0: dots += (*cp == '.'); michael@0: michael@0: cp = statp->defdname; michael@0: while (pp < statp->dnsrch + MAXDFLSRCH) { michael@0: if (dots < LOCALDOMAINPARTS) michael@0: break; michael@0: cp = strchr(cp, '.') + 1; /* we know there is one */ michael@0: *pp++ = cp; michael@0: dots--; michael@0: } michael@0: *pp = NULL; michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) { michael@0: printf(";; res_init()... default dnsrch list:\n"); michael@0: for (pp = statp->dnsrch; *pp; pp++) michael@0: printf(";;\t%s\n", *pp); michael@0: printf(";;\t..END..\n"); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: if ((cp = getenv("RES_OPTIONS")) != NULL) michael@0: res_setoptions(statp, cp, "env"); michael@0: if (nserv > 0) { michael@0: statp->nscount = nserv; michael@0: statp->options |= RES_INIT; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: res_setoptions(res_state statp, const char *options, const char *source) michael@0: { michael@0: const char *cp = options; michael@0: int i; michael@0: struct __res_state_ext *ext = statp->_u._ext.ext; michael@0: michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) michael@0: printf(";; res_setoptions(\"%s\", \"%s\")...\n", michael@0: options, source); michael@0: #endif michael@0: while (*cp) { michael@0: /* skip leading and inner runs of spaces */ michael@0: while (*cp == ' ' || *cp == '\t') michael@0: cp++; michael@0: /* search for and process individual options */ michael@0: if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { michael@0: i = atoi(cp + sizeof("ndots:") - 1); michael@0: if (i <= RES_MAXNDOTS) michael@0: statp->ndots = i; michael@0: else michael@0: statp->ndots = RES_MAXNDOTS; michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) michael@0: printf(";;\tndots=%d\n", statp->ndots); michael@0: #endif michael@0: } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { michael@0: i = atoi(cp + sizeof("timeout:") - 1); michael@0: if (i <= RES_MAXRETRANS) michael@0: statp->retrans = i; michael@0: else michael@0: statp->retrans = RES_MAXRETRANS; michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) michael@0: printf(";;\ttimeout=%d\n", statp->retrans); michael@0: #endif michael@0: } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ michael@0: i = atoi(cp + sizeof("attempts:") - 1); michael@0: if (i <= RES_MAXRETRY) michael@0: statp->retry = i; michael@0: else michael@0: statp->retry = RES_MAXRETRY; michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) michael@0: printf(";;\tattempts=%d\n", statp->retry); michael@0: #endif michael@0: } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { michael@0: #ifdef DEBUG michael@0: if (!(statp->options & RES_DEBUG)) { michael@0: printf(";; res_setoptions(\"%s\", \"%s\")..\n", michael@0: options, source); michael@0: statp->options |= RES_DEBUG; michael@0: } michael@0: printf(";;\tdebug\n"); michael@0: #endif michael@0: } else if (!strncmp(cp, "no_tld_query", michael@0: sizeof("no_tld_query") - 1) || michael@0: !strncmp(cp, "no-tld-query", michael@0: sizeof("no-tld-query") - 1)) { michael@0: statp->options |= RES_NOTLDQUERY; michael@0: } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { michael@0: statp->options |= RES_USE_INET6; michael@0: } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { michael@0: statp->options |= RES_ROTATE; michael@0: } else if (!strncmp(cp, "no-check-names", michael@0: sizeof("no-check-names") - 1)) { michael@0: statp->options |= RES_NOCHECKNAME; michael@0: } michael@0: #ifdef RES_USE_EDNS0 michael@0: else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { michael@0: statp->options |= RES_USE_EDNS0; michael@0: } michael@0: #endif michael@0: else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { michael@0: statp->options |= RES_USE_DNAME; michael@0: } michael@0: else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { michael@0: if (ext == NULL) michael@0: goto skip; michael@0: cp += sizeof("nibble:") - 1; michael@0: i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); michael@0: strncpy(ext->nsuffix, cp, (size_t)i); michael@0: ext->nsuffix[i] = '\0'; michael@0: } michael@0: else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { michael@0: if (ext == NULL) michael@0: goto skip; michael@0: cp += sizeof("nibble2:") - 1; michael@0: i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); michael@0: strncpy(ext->nsuffix2, cp, (size_t)i); michael@0: ext->nsuffix2[i] = '\0'; michael@0: } michael@0: else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { michael@0: cp += sizeof("v6revmode:") - 1; michael@0: /* "nibble" and "bitstring" used to be valid */ michael@0: if (!strncmp(cp, "single", sizeof("single") - 1)) { michael@0: statp->options |= RES_NO_NIBBLE2; michael@0: } else if (!strncmp(cp, "both", sizeof("both") - 1)) { michael@0: statp->options &= michael@0: ~RES_NO_NIBBLE2; michael@0: } michael@0: } michael@0: else { michael@0: /* XXX - print a warning here? */ michael@0: } michael@0: skip: michael@0: /* skip to next run of spaces */ michael@0: while (*cp && *cp != ' ' && *cp != '\t') michael@0: cp++; michael@0: } michael@0: } michael@0: michael@0: /* XXX - should really support CIDR which means explicit masks always. */ michael@0: static u_int32_t michael@0: net_mask(in) /* XXX - should really use system's version of this */ michael@0: struct in_addr in; michael@0: { michael@0: register u_int32_t i = ntohl(in.s_addr); michael@0: michael@0: if (IN_CLASSA(i)) michael@0: return (htonl(IN_CLASSA_NET)); michael@0: else if (IN_CLASSB(i)) michael@0: return (htonl(IN_CLASSB_NET)); michael@0: return (htonl(IN_CLASSC_NET)); michael@0: } michael@0: michael@0: u_int michael@0: res_randomid(void) { michael@0: struct timeval now; michael@0: michael@0: gettimeofday(&now, NULL); michael@0: return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); michael@0: } michael@0: michael@0: /* michael@0: * This routine is for closing the socket if a virtual circuit is used and michael@0: * the program wants to close it. This provides support for endhostent() michael@0: * which expects to close the socket. michael@0: * michael@0: * This routine is not expected to be user visible. michael@0: */ michael@0: void michael@0: res_nclose(res_state statp) { michael@0: int ns; michael@0: michael@0: if (statp->_vcsock >= 0) { michael@0: (void) close(statp->_vcsock); michael@0: statp->_vcsock = -1; michael@0: statp->_flags &= ~(RES_F_VC | RES_F_CONN); michael@0: } michael@0: for (ns = 0; ns < statp->_u._ext.nscount; ns++) { michael@0: if (statp->_u._ext.nssocks[ns] != -1) { michael@0: (void) close(statp->_u._ext.nssocks[ns]); michael@0: statp->_u._ext.nssocks[ns] = -1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: res_ndestroy(res_state statp) { michael@0: res_nclose(statp); michael@0: if (statp->_u._ext.ext != NULL) michael@0: free(statp->_u._ext.ext); michael@0: statp->options &= ~RES_INIT; michael@0: statp->_u._ext.ext = NULL; michael@0: } michael@0: michael@0: const char * michael@0: res_get_nibblesuffix(res_state statp) { michael@0: if (statp->_u._ext.ext) michael@0: return (statp->_u._ext.ext->nsuffix); michael@0: return ("ip6.arpa"); michael@0: } michael@0: michael@0: const char * michael@0: res_get_nibblesuffix2(res_state statp) { michael@0: if (statp->_u._ext.ext) michael@0: return (statp->_u._ext.ext->nsuffix2); michael@0: return ("ip6.int"); michael@0: } michael@0: michael@0: void michael@0: res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { michael@0: int i, nserv; michael@0: size_t size; michael@0: michael@0: /* close open servers */ michael@0: res_nclose(statp); michael@0: michael@0: /* cause rtt times to be forgotten */ michael@0: statp->_u._ext.nscount = 0; michael@0: michael@0: nserv = 0; michael@0: for (i = 0; i < cnt && nserv < MAXNS; i++) { michael@0: switch (set->sin.sin_family) { michael@0: case AF_INET: michael@0: size = sizeof(set->sin); michael@0: if (statp->_u._ext.ext) michael@0: memcpy(&statp->_u._ext.ext->nsaddrs[nserv], michael@0: &set->sin, size); michael@0: if (size <= sizeof(statp->nsaddr_list[nserv])) michael@0: memcpy(&statp->nsaddr_list[nserv], michael@0: &set->sin, size); michael@0: #ifdef notdef michael@0: else michael@0: statp->nsaddr_list[nserv].sin_family = 0; michael@0: #endif michael@0: nserv++; michael@0: break; michael@0: michael@0: #ifdef HAS_INET6_STRUCTS michael@0: case AF_INET6: michael@0: size = sizeof(set->sin6); michael@0: if (statp->_u._ext.ext) michael@0: memcpy(&statp->_u._ext.ext->nsaddrs[nserv], michael@0: &set->sin6, size); michael@0: if (size <= sizeof(statp->nsaddr_list[nserv])) michael@0: memcpy(&statp->nsaddr_list[nserv], michael@0: &set->sin6, size); michael@0: else michael@0: statp->nsaddr_list[nserv].sin_family = 0; michael@0: nserv++; michael@0: break; michael@0: #endif michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: set++; michael@0: } michael@0: statp->nscount = nserv; michael@0: michael@0: } michael@0: michael@0: int michael@0: res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { michael@0: int i; michael@0: size_t size; michael@0: u_int16_t family; michael@0: michael@0: for (i = 0; i < statp->nscount && i < cnt; i++) { michael@0: if (statp->_u._ext.ext) michael@0: family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; michael@0: else michael@0: family = statp->nsaddr_list[i].sin_family; michael@0: michael@0: switch (family) { michael@0: case AF_INET: michael@0: size = sizeof(set->sin); michael@0: if (statp->_u._ext.ext) michael@0: memcpy(&set->sin, michael@0: &statp->_u._ext.ext->nsaddrs[i], michael@0: size); michael@0: else michael@0: memcpy(&set->sin, &statp->nsaddr_list[i], michael@0: size); michael@0: break; michael@0: michael@0: #ifdef HAS_INET6_STRUCTS michael@0: case AF_INET6: michael@0: size = sizeof(set->sin6); michael@0: if (statp->_u._ext.ext) michael@0: memcpy(&set->sin6, michael@0: &statp->_u._ext.ext->nsaddrs[i], michael@0: size); michael@0: else michael@0: memcpy(&set->sin6, &statp->nsaddr_list[i], michael@0: size); michael@0: break; michael@0: #endif michael@0: michael@0: default: michael@0: set->sin.sin_family = 0; michael@0: break; michael@0: } michael@0: set++; michael@0: } michael@0: return (statp->nscount); michael@0: } michael@0: michael@0: #ifdef ANDROID_CHANGES michael@0: static int _get_dns_change_count() michael@0: { michael@0: if (dns_change_prop == NULL) { michael@0: dns_change_prop = __system_property_find(DNS_CHANGE_PROP_NAME); michael@0: } michael@0: if (dns_change_prop != NULL) { michael@0: char propvalue[PROP_VALUE_MAX]; michael@0: if (__system_property_read(dns_change_prop, NULL, propvalue) >= 1) { michael@0: return atoi(propvalue); michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: int res_get_dns_changed() michael@0: { michael@0: int change_count; michael@0: michael@0: change_count = _get_dns_change_count(); michael@0: if (change_count != dns_last_change_counter) { michael@0: if (change_count != -1) { michael@0: dns_last_change_counter = change_count; michael@0: } michael@0: return 1; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: #endif /* ANDROID_CHANGES */