xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1985, 1989, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 4. Neither the name of the University nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 32 | * |
| 33 | * Permission to use, copy, modify, and distribute this software for any |
| 34 | * purpose with or without fee is hereby granted, provided that the above |
| 35 | * copyright notice and this permission notice appear in all copies, and that |
| 36 | * the name of Digital Equipment Corporation not be used in advertising or |
| 37 | * publicity pertaining to distribution of the document or software without |
| 38 | * specific, written prior permission. |
| 39 | * |
| 40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 43 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 44 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 45 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 46 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 47 | * SOFTWARE. |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
| 52 | * |
| 53 | * Permission to use, copy, modify, and distribute this software for any |
| 54 | * purpose with or without fee is hereby granted, provided that the above |
| 55 | * copyright notice and this permission notice appear in all copies. |
| 56 | * |
| 57 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 58 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 59 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 60 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 61 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 62 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 63 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 64 | * SOFTWARE. |
| 65 | */ |
| 66 | |
| 67 | #if defined(LIBC_SCCS) && !defined(lint) |
| 68 | static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; |
| 69 | static const char rcsid[] = "$BINDId: res_init.c,v 8.16 2000/05/09 07:10:12 vixie Exp $"; |
| 70 | #endif /* LIBC_SCCS and not lint */ |
| 71 | |
| 72 | #include <ctype.h> |
| 73 | #include <netdb.h> |
| 74 | #include <resolv.h> |
| 75 | #include <stdio.h> |
| 76 | #include <stdio_ext.h> |
| 77 | #include <stdlib.h> |
| 78 | #include <string.h> |
| 79 | #include <unistd.h> |
| 80 | #include <stdint.h> |
| 81 | #include <arpa/inet.h> |
| 82 | #include <arpa/nameser.h> |
| 83 | #include <net/if.h> |
| 84 | #include <netinet/in.h> |
| 85 | #include <sys/param.h> |
| 86 | #include <sys/socket.h> |
| 87 | #include <sys/time.h> |
| 88 | #include <sys/types.h> |
| 89 | |
| 90 | #include <not-cancel.h> |
| 91 | |
| 92 | /* Options. Should all be left alone. */ |
| 93 | #define RESOLVSORT |
| 94 | #define RFC1535 |
| 95 | /* #undef DEBUG */ |
| 96 | |
| 97 | static void res_setoptions (res_state, const char *, const char *) |
| 98 | internal_function; |
| 99 | |
| 100 | #ifdef RESOLVSORT |
| 101 | static const char sort_mask_chars[] = "/&"; |
| 102 | #define ISSORTMASK(ch) (strchr(sort_mask_chars, ch) != NULL) |
| 103 | static u_int32_t net_mask (struct in_addr) __THROW; |
| 104 | #endif |
| 105 | |
| 106 | #if !defined(isascii) /* XXX - could be a function */ |
| 107 | # define isascii(c) (!(c & 0200)) |
| 108 | #endif |
| 109 | |
| 110 | #ifdef _LIBC |
| 111 | unsigned long long int __res_initstamp attribute_hidden; |
| 112 | #endif |
| 113 | |
| 114 | /* |
| 115 | * Resolver state default settings. |
| 116 | */ |
| 117 | |
| 118 | /* |
| 119 | * Set up default settings. If the configuration file exist, the values |
| 120 | * there will have precedence. Otherwise, the server address is set to |
| 121 | * INADDR_ANY and the default domain name comes from the gethostname(). |
| 122 | * |
| 123 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 |
| 124 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address |
| 125 | * since it was noted that INADDR_ANY actually meant ``the first interface |
| 126 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, |
| 127 | * it had to be "up" in order for you to reach your own name server. It |
| 128 | * was later decided that since the recommended practice is to always |
| 129 | * install local static routes through 127.0.0.1 for all your network |
| 130 | * interfaces, that we could solve this problem without a code change. |
| 131 | * |
| 132 | * The configuration file should always be used, since it is the only way |
| 133 | * to specify a default domain. If you are running a server on your local |
| 134 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" |
| 135 | * in the configuration file. |
| 136 | * |
| 137 | * Return 0 if completes successfully, -1 on error |
| 138 | */ |
| 139 | int |
| 140 | res_ninit(res_state statp) { |
| 141 | extern int __res_vinit(res_state, int); |
| 142 | |
| 143 | return (__res_vinit(statp, 0)); |
| 144 | } |
| 145 | #ifdef _LIBC |
| 146 | libc_hidden_def (__res_ninit) |
| 147 | #endif |
| 148 | |
| 149 | /* This function has to be reachable by res_data.c but not publically. */ |
| 150 | int |
| 151 | __res_vinit(res_state statp, int preinit) { |
| 152 | FILE *fp; |
| 153 | char *cp, **pp; |
| 154 | int n; |
| 155 | char buf[BUFSIZ]; |
| 156 | int nserv = 0; /* number of nameservers read from file */ |
| 157 | int have_serv6 = 0; |
| 158 | int haveenv = 0; |
| 159 | int havesearch = 0; |
| 160 | #ifdef RESOLVSORT |
| 161 | int nsort = 0; |
| 162 | char *net; |
| 163 | #endif |
| 164 | #ifndef RFC1535 |
| 165 | int dots; |
| 166 | #endif |
| 167 | #ifdef _LIBC |
| 168 | statp->_u._ext.initstamp = __res_initstamp; |
| 169 | #endif |
| 170 | |
| 171 | if (!preinit) { |
| 172 | statp->retrans = RES_TIMEOUT; |
| 173 | statp->retry = RES_DFLRETRY; |
| 174 | statp->options = RES_DEFAULT; |
| 175 | statp->id = res_randomid(); |
| 176 | } |
| 177 | |
| 178 | statp->nscount = 0; |
| 179 | statp->defdname[0] = '\0'; |
| 180 | statp->ndots = 1; |
| 181 | statp->pfcode = 0; |
| 182 | statp->_vcsock = -1; |
| 183 | statp->_flags = 0; |
| 184 | statp->qhook = NULL; |
| 185 | statp->rhook = NULL; |
| 186 | statp->_u._ext.nscount = 0; |
| 187 | for (n = 0; n < MAXNS; n++) |
| 188 | statp->_u._ext.nsaddrs[n] = NULL; |
| 189 | |
| 190 | /* Allow user to override the local domain definition */ |
| 191 | if ((cp = getenv("LOCALDOMAIN")) != NULL) { |
| 192 | (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 193 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 194 | haveenv++; |
| 195 | |
| 196 | /* |
| 197 | * Set search list to be blank-separated strings |
| 198 | * from rest of env value. Permits users of LOCALDOMAIN |
| 199 | * to still have a search list, and anyone to set the |
| 200 | * one that they want to use as an individual (even more |
| 201 | * important now that the rfc1535 stuff restricts searches) |
| 202 | */ |
| 203 | cp = statp->defdname; |
| 204 | pp = statp->dnsrch; |
| 205 | *pp++ = cp; |
| 206 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
| 207 | if (*cp == '\n') /* silly backwards compat */ |
| 208 | break; |
| 209 | else if (*cp == ' ' || *cp == '\t') { |
| 210 | *cp = 0; |
| 211 | n = 1; |
| 212 | } else if (n) { |
| 213 | *pp++ = cp; |
| 214 | n = 0; |
| 215 | havesearch = 1; |
| 216 | } |
| 217 | } |
| 218 | /* null terminate last domain if there are excess */ |
| 219 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') |
| 220 | cp++; |
| 221 | *cp = '\0'; |
| 222 | *pp++ = 0; |
| 223 | } |
| 224 | |
| 225 | #define MATCH(line, name) \ |
| 226 | (!strncmp(line, name, sizeof(name) - 1) && \ |
| 227 | (line[sizeof(name) - 1] == ' ' || \ |
| 228 | line[sizeof(name) - 1] == '\t')) |
| 229 | |
| 230 | if ((fp = fopen(_PATH_RESCONF, "rce")) != NULL) { |
| 231 | /* No threads use this stream. */ |
| 232 | __fsetlocking (fp, FSETLOCKING_BYCALLER); |
| 233 | /* read the config file */ |
| 234 | while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) { |
| 235 | /* skip comments */ |
| 236 | if (*buf == ';' || *buf == '#') |
| 237 | continue; |
| 238 | /* read default domain name */ |
| 239 | if (MATCH(buf, "domain")) { |
| 240 | if (haveenv) /* skip if have from environ */ |
| 241 | continue; |
| 242 | cp = buf + sizeof("domain") - 1; |
| 243 | while (*cp == ' ' || *cp == '\t') |
| 244 | cp++; |
| 245 | if ((*cp == '\0') || (*cp == '\n')) |
| 246 | continue; |
| 247 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 248 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 249 | if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) |
| 250 | *cp = '\0'; |
| 251 | havesearch = 0; |
| 252 | continue; |
| 253 | } |
| 254 | /* set search list */ |
| 255 | if (MATCH(buf, "search")) { |
| 256 | if (haveenv) /* skip if have from environ */ |
| 257 | continue; |
| 258 | cp = buf + sizeof("search") - 1; |
| 259 | while (*cp == ' ' || *cp == '\t') |
| 260 | cp++; |
| 261 | if ((*cp == '\0') || (*cp == '\n')) |
| 262 | continue; |
| 263 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 264 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 265 | if ((cp = strchr(statp->defdname, '\n')) != NULL) |
| 266 | *cp = '\0'; |
| 267 | /* |
| 268 | * Set search list to be blank-separated strings |
| 269 | * on rest of line. |
| 270 | */ |
| 271 | cp = statp->defdname; |
| 272 | pp = statp->dnsrch; |
| 273 | *pp++ = cp; |
| 274 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
| 275 | if (*cp == ' ' || *cp == '\t') { |
| 276 | *cp = 0; |
| 277 | n = 1; |
| 278 | } else if (n) { |
| 279 | *pp++ = cp; |
| 280 | n = 0; |
| 281 | } |
| 282 | } |
| 283 | /* null terminate last domain if there are excess */ |
| 284 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
| 285 | cp++; |
| 286 | *cp = '\0'; |
| 287 | *pp++ = 0; |
| 288 | havesearch = 1; |
| 289 | continue; |
| 290 | } |
| 291 | /* read nameservers to query */ |
| 292 | if (MATCH(buf, "nameserver") && nserv < MAXNS) { |
| 293 | struct in_addr a; |
| 294 | |
| 295 | cp = buf + sizeof("nameserver") - 1; |
| 296 | while (*cp == ' ' || *cp == '\t') |
| 297 | cp++; |
| 298 | if ((*cp != '\0') && (*cp != '\n') |
| 299 | && __inet_aton(cp, &a)) { |
| 300 | statp->nsaddr_list[nserv].sin_addr = a; |
| 301 | statp->nsaddr_list[nserv].sin_family = AF_INET; |
| 302 | statp->nsaddr_list[nserv].sin_port = |
| 303 | htons(NAMESERVER_PORT); |
| 304 | nserv++; |
| 305 | #ifdef _LIBC |
| 306 | } else { |
| 307 | struct in6_addr a6; |
| 308 | char *el; |
| 309 | |
| 310 | if ((el = strpbrk(cp, " \t\n")) != NULL) |
| 311 | *el = '\0'; |
| 312 | if ((el = strchr(cp, SCOPE_DELIMITER)) != NULL) |
| 313 | *el = '\0'; |
| 314 | if ((*cp != '\0') && |
| 315 | (__inet_pton(AF_INET6, cp, &a6) > 0)) { |
| 316 | struct sockaddr_in6 *sa6; |
| 317 | |
| 318 | sa6 = malloc(sizeof(*sa6)); |
| 319 | if (sa6 != NULL) { |
| 320 | sa6->sin6_family = AF_INET6; |
| 321 | sa6->sin6_port = htons(NAMESERVER_PORT); |
| 322 | sa6->sin6_flowinfo = 0; |
| 323 | sa6->sin6_addr = a6; |
| 324 | |
| 325 | if (__glibc_likely (el == NULL)) |
| 326 | sa6->sin6_scope_id = 0; |
| 327 | else { |
| 328 | int try_numericscope = 1; |
| 329 | if (IN6_IS_ADDR_LINKLOCAL (&a6) |
| 330 | || IN6_IS_ADDR_MC_LINKLOCAL (&a6)) { |
| 331 | sa6->sin6_scope_id |
| 332 | = __if_nametoindex (el + 1); |
| 333 | if (sa6->sin6_scope_id != 0) |
| 334 | try_numericscope = 0; |
| 335 | } |
| 336 | |
| 337 | if (try_numericscope) { |
| 338 | char *end; |
| 339 | sa6->sin6_scope_id |
| 340 | = (uint32_t) strtoul (el + 1, &end, |
| 341 | 10); |
| 342 | if (*end != '\0') |
| 343 | sa6->sin6_scope_id = 0; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | statp->nsaddr_list[nserv].sin_family = 0; |
| 348 | statp->_u._ext.nsaddrs[nserv] = sa6; |
| 349 | statp->_u._ext.nssocks[nserv] = -1; |
| 350 | have_serv6 = 1; |
| 351 | nserv++; |
| 352 | } |
| 353 | } |
| 354 | #endif |
| 355 | } |
| 356 | continue; |
| 357 | } |
| 358 | #ifdef RESOLVSORT |
| 359 | if (MATCH(buf, "sortlist")) { |
| 360 | struct in_addr a; |
| 361 | |
| 362 | cp = buf + sizeof("sortlist") - 1; |
| 363 | while (nsort < MAXRESOLVSORT) { |
| 364 | while (*cp == ' ' || *cp == '\t') |
| 365 | cp++; |
| 366 | if (*cp == '\0' || *cp == '\n' || *cp == ';') |
| 367 | break; |
| 368 | net = cp; |
| 369 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && |
| 370 | isascii(*cp) && !isspace(*cp)) |
| 371 | cp++; |
| 372 | n = *cp; |
| 373 | *cp = 0; |
| 374 | if (__inet_aton(net, &a)) { |
| 375 | statp->sort_list[nsort].addr = a; |
| 376 | if (ISSORTMASK(n)) { |
| 377 | *cp++ = n; |
| 378 | net = cp; |
| 379 | while (*cp && *cp != ';' && |
| 380 | isascii(*cp) && !isspace(*cp)) |
| 381 | cp++; |
| 382 | n = *cp; |
| 383 | *cp = 0; |
| 384 | if (__inet_aton(net, &a)) { |
| 385 | statp->sort_list[nsort].mask = a.s_addr; |
| 386 | } else { |
| 387 | statp->sort_list[nsort].mask = |
| 388 | net_mask(statp->sort_list[nsort].addr); |
| 389 | } |
| 390 | } else { |
| 391 | statp->sort_list[nsort].mask = |
| 392 | net_mask(statp->sort_list[nsort].addr); |
| 393 | } |
| 394 | nsort++; |
| 395 | } |
| 396 | *cp = n; |
| 397 | } |
| 398 | continue; |
| 399 | } |
| 400 | #endif |
| 401 | if (MATCH(buf, "options")) { |
| 402 | res_setoptions(statp, buf + sizeof("options") - 1, "conf"); |
| 403 | continue; |
| 404 | } |
| 405 | } |
| 406 | statp->nscount = nserv; |
| 407 | #ifdef _LIBC |
| 408 | if (have_serv6) { |
| 409 | /* We try IPv6 servers again. */ |
| 410 | statp->ipv6_unavail = false; |
| 411 | } |
| 412 | #endif |
| 413 | #ifdef RESOLVSORT |
| 414 | statp->nsort = nsort; |
| 415 | #endif |
| 416 | (void) fclose(fp); |
| 417 | } |
| 418 | if (__builtin_expect(statp->nscount == 0, 0)) { |
| 419 | statp->nsaddr.sin_addr = __inet_makeaddr(IN_LOOPBACKNET, 1); |
| 420 | statp->nsaddr.sin_family = AF_INET; |
| 421 | statp->nsaddr.sin_port = htons(NAMESERVER_PORT); |
| 422 | statp->nscount = 1; |
| 423 | } |
| 424 | if (statp->defdname[0] == 0 && |
| 425 | __gethostname(buf, sizeof(statp->defdname) - 1) == 0 && |
| 426 | (cp = strchr(buf, '.')) != NULL) |
| 427 | strcpy(statp->defdname, cp + 1); |
| 428 | |
| 429 | /* find components of local domain that might be searched */ |
| 430 | if (havesearch == 0) { |
| 431 | pp = statp->dnsrch; |
| 432 | *pp++ = statp->defdname; |
| 433 | *pp = NULL; |
| 434 | |
| 435 | #ifndef RFC1535 |
| 436 | dots = 0; |
| 437 | for (cp = statp->defdname; *cp; cp++) |
| 438 | dots += (*cp == '.'); |
| 439 | |
| 440 | cp = statp->defdname; |
| 441 | while (pp < statp->dnsrch + MAXDFLSRCH) { |
| 442 | if (dots < LOCALDOMAINPARTS) |
| 443 | break; |
| 444 | cp = __rawmemchr(cp, '.') + 1; /* we know there is one */ |
| 445 | *pp++ = cp; |
| 446 | dots--; |
| 447 | } |
| 448 | *pp = NULL; |
| 449 | #ifdef DEBUG |
| 450 | if (statp->options & RES_DEBUG) { |
| 451 | printf(";; res_init()... default dnsrch list:\n"); |
| 452 | for (pp = statp->dnsrch; *pp; pp++) |
| 453 | printf(";;\t%s\n", *pp); |
| 454 | printf(";;\t..END..\n"); |
| 455 | } |
| 456 | #endif |
| 457 | #endif /* !RFC1535 */ |
| 458 | } |
| 459 | |
| 460 | if ((cp = getenv("RES_OPTIONS")) != NULL) |
| 461 | res_setoptions(statp, cp, "env"); |
| 462 | statp->options |= RES_INIT; |
| 463 | return (0); |
| 464 | } |
| 465 | |
| 466 | static void |
| 467 | internal_function |
| 468 | res_setoptions(res_state statp, const char *options, const char *source) { |
| 469 | const char *cp = options; |
| 470 | int i; |
| 471 | |
| 472 | #ifdef DEBUG |
| 473 | if (statp->options & RES_DEBUG) |
| 474 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", |
| 475 | options, source); |
| 476 | #endif |
| 477 | while (*cp) { |
| 478 | /* skip leading and inner runs of spaces */ |
| 479 | while (*cp == ' ' || *cp == '\t') |
| 480 | cp++; |
| 481 | /* search for and process individual options */ |
| 482 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { |
| 483 | i = atoi(cp + sizeof("ndots:") - 1); |
| 484 | if (i <= RES_MAXNDOTS) |
| 485 | statp->ndots = i; |
| 486 | else |
| 487 | statp->ndots = RES_MAXNDOTS; |
| 488 | #ifdef DEBUG |
| 489 | if (statp->options & RES_DEBUG) |
| 490 | printf(";;\tndots=%d\n", statp->ndots); |
| 491 | #endif |
| 492 | } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { |
| 493 | i = atoi(cp + sizeof("timeout:") - 1); |
| 494 | if (i <= RES_MAXRETRANS) |
| 495 | statp->retrans = i; |
| 496 | else |
| 497 | statp->retrans = RES_MAXRETRANS; |
| 498 | } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ |
| 499 | i = atoi(cp + sizeof("attempts:") - 1); |
| 500 | if (i <= RES_MAXRETRY) |
| 501 | statp->retry = i; |
| 502 | else |
| 503 | statp->retry = RES_MAXRETRY; |
| 504 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { |
| 505 | #ifdef DEBUG |
| 506 | if (!(statp->options & RES_DEBUG)) { |
| 507 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", |
| 508 | options, source); |
| 509 | statp->options |= RES_DEBUG; |
| 510 | } |
| 511 | printf(";;\tdebug\n"); |
| 512 | #endif |
| 513 | } else { |
| 514 | static const struct |
| 515 | { |
| 516 | char str[22]; |
| 517 | uint8_t len; |
| 518 | uint8_t clear; |
| 519 | unsigned long int flag; |
| 520 | } options[] = { |
| 521 | #define STRnLEN(str) str, sizeof (str) - 1 |
| 522 | { STRnLEN ("inet6"), 0, RES_USE_INET6 }, |
| 523 | { STRnLEN ("ip6-bytestring"), 0, RES_USEBSTRING }, |
| 524 | { STRnLEN ("no-ip6-dotint"), 0, RES_NOIP6DOTINT }, |
| 525 | { STRnLEN ("ip6-dotint"), 1, ~RES_NOIP6DOTINT }, |
| 526 | { STRnLEN ("rotate"), 0, RES_ROTATE }, |
| 527 | { STRnLEN ("no-check-names"), 0, RES_NOCHECKNAME }, |
| 528 | { STRnLEN ("edns0"), 0, RES_USE_EDNS0 }, |
| 529 | { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP }, |
| 530 | { STRnLEN ("single-request"), 0, RES_SNGLKUP }, |
| 531 | { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY }, |
| 532 | { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY }, |
| 533 | { STRnLEN ("use-vc"), 0, RES_USEVC } |
| 534 | }; |
| 535 | #define noptions (sizeof (options) / sizeof (options[0])) |
| 536 | int i; |
| 537 | for (i = 0; i < noptions; ++i) |
| 538 | if (strncmp (cp, options[i].str, options[i].len) == 0) |
| 539 | { |
| 540 | if (options[i].clear) |
| 541 | statp->options &= options[i].flag; |
| 542 | else |
| 543 | statp->options |= options[i].flag; |
| 544 | break; |
| 545 | } |
| 546 | if (i == noptions) { |
| 547 | /* XXX - print a warning here? */ |
| 548 | } |
| 549 | } |
| 550 | /* skip to next run of spaces */ |
| 551 | while (*cp && *cp != ' ' && *cp != '\t') |
| 552 | cp++; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | #ifdef RESOLVSORT |
| 557 | /* XXX - should really support CIDR which means explicit masks always. */ |
| 558 | /* XXX - should really use system's version of this */ |
| 559 | static u_int32_t |
| 560 | net_mask (struct in_addr in) |
| 561 | { |
| 562 | u_int32_t i = ntohl(in.s_addr); |
| 563 | |
| 564 | if (IN_CLASSA(i)) |
| 565 | return (htonl(IN_CLASSA_NET)); |
| 566 | else if (IN_CLASSB(i)) |
| 567 | return (htonl(IN_CLASSB_NET)); |
| 568 | return (htonl(IN_CLASSC_NET)); |
| 569 | } |
| 570 | #endif |
| 571 | |
| 572 | u_int |
| 573 | res_randomid(void) { |
| 574 | return 0xffff & __getpid(); |
| 575 | } |
| 576 | #ifdef _LIBC |
| 577 | libc_hidden_def (__res_randomid) |
| 578 | #endif |
| 579 | |
| 580 | |
| 581 | /* |
| 582 | * This routine is for closing the socket if a virtual circuit is used and |
| 583 | * the program wants to close it. This provides support for endhostent() |
| 584 | * which expects to close the socket. |
| 585 | * |
| 586 | * This routine is not expected to be user visible. |
| 587 | */ |
| 588 | void |
| 589 | __res_iclose(res_state statp, bool free_addr) { |
| 590 | int ns; |
| 591 | |
| 592 | if (statp->_vcsock >= 0) { |
| 593 | close_not_cancel_no_status(statp->_vcsock); |
| 594 | statp->_vcsock = -1; |
| 595 | statp->_flags &= ~(RES_F_VC | RES_F_CONN); |
| 596 | } |
| 597 | for (ns = 0; ns < statp->_u._ext.nscount; ns++) |
| 598 | if (statp->_u._ext.nsaddrs[ns]) { |
| 599 | if (statp->_u._ext.nssocks[ns] != -1) { |
| 600 | close_not_cancel_no_status(statp->_u._ext.nssocks[ns]); |
| 601 | statp->_u._ext.nssocks[ns] = -1; |
| 602 | } |
| 603 | if (free_addr) { |
| 604 | free (statp->_u._ext.nsaddrs[ns]); |
| 605 | statp->_u._ext.nsaddrs[ns] = NULL; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | libc_hidden_def (__res_iclose) |
| 610 | |
| 611 | void |
| 612 | res_nclose(res_state statp) |
| 613 | { |
| 614 | __res_iclose (statp, true); |
| 615 | } |
| 616 | #ifdef _LIBC |
| 617 | libc_hidden_def (__res_nclose) |
| 618 | #endif |
| 619 | |
| 620 | #ifdef _LIBC |
| 621 | # ifdef _LIBC_REENTRANT |
| 622 | /* This is called when a thread is exiting to free resources held in _res. */ |
| 623 | static void __attribute__ ((section ("__libc_thread_freeres_fn"))) |
| 624 | res_thread_freeres (void) |
| 625 | { |
| 626 | if (_res.nscount == 0) |
| 627 | /* Never called res_ninit. */ |
| 628 | return; |
| 629 | |
| 630 | __res_iclose (&_res, true); /* Close any VC sockets. */ |
| 631 | |
| 632 | /* Make sure we do a full re-initialization the next time. */ |
| 633 | _res.options = 0; |
| 634 | } |
| 635 | text_set_element (__libc_thread_subfreeres, res_thread_freeres); |
| 636 | text_set_element (__libc_subfreeres, res_thread_freeres); |
| 637 | # endif |
| 638 | #endif |