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