| 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 | #include <ctype.h> |
| 68 | #include <netdb.h> |
| 69 | #include <resolv/resolv-internal.h> |
| 70 | #include <stdio.h> |
| 71 | #include <stdio_ext.h> |
| 72 | #include <stdlib.h> |
| 73 | #include <string.h> |
| 74 | #include <unistd.h> |
| 75 | #include <stdint.h> |
| 76 | #include <arpa/inet.h> |
| 77 | #include <arpa/nameser.h> |
| 78 | #include <net/if.h> |
| 79 | #include <netinet/in.h> |
| 80 | #include <sys/param.h> |
| 81 | #include <sys/socket.h> |
| 82 | #include <sys/time.h> |
| 83 | #include <sys/types.h> |
| 84 | #include <inet/net-internal.h> |
| 85 | |
| 86 | #include <not-cancel.h> |
| 87 | |
| 88 | /* Options. Should all be left alone. */ |
| 89 | /* #undef DEBUG */ |
| 90 | |
| 91 | static void res_setoptions (res_state, const char *, const char *) |
| 92 | internal_function; |
| 93 | |
| 94 | static const char sort_mask_chars[] = "/&" ; |
| 95 | #define ISSORTMASK(ch) (strchr(sort_mask_chars, ch) != NULL) |
| 96 | static u_int32_t net_mask (struct in_addr) __THROW; |
| 97 | |
| 98 | unsigned long long int __res_initstamp attribute_hidden; |
| 99 | |
| 100 | /* |
| 101 | * Resolver state default settings. |
| 102 | */ |
| 103 | |
| 104 | /* |
| 105 | * Set up default settings. If the configuration file exist, the values |
| 106 | * there will have precedence. Otherwise, the server address is set to |
| 107 | * INADDR_LOOPBACK and the default domain name comes from gethostname. |
| 108 | * |
| 109 | * Return 0 if completes successfully, -1 on error |
| 110 | */ |
| 111 | int |
| 112 | res_ninit(res_state statp) { |
| 113 | extern int __res_vinit(res_state, int); |
| 114 | |
| 115 | return (__res_vinit(statp, 0)); |
| 116 | } |
| 117 | libc_hidden_def (__res_ninit) |
| 118 | |
| 119 | /* This function has to be reachable by res_data.c but not publically. */ |
| 120 | int |
| 121 | __res_vinit(res_state statp, int preinit) { |
| 122 | FILE *fp; |
| 123 | char *cp, **pp; |
| 124 | int n; |
| 125 | char buf[BUFSIZ]; |
| 126 | int nserv = 0; /* number of nameservers read from file */ |
| 127 | int have_serv6 = 0; |
| 128 | int haveenv = 0; |
| 129 | int havesearch = 0; |
| 130 | int nsort = 0; |
| 131 | char *net; |
| 132 | statp->_u._ext.initstamp = __res_initstamp; |
| 133 | |
| 134 | if (!preinit) { |
| 135 | statp->retrans = RES_TIMEOUT; |
| 136 | statp->retry = RES_DFLRETRY; |
| 137 | statp->options = RES_DEFAULT; |
| 138 | statp->id = res_randomid(); |
| 139 | } |
| 140 | |
| 141 | statp->nscount = 0; |
| 142 | statp->defdname[0] = '\0'; |
| 143 | statp->ndots = 1; |
| 144 | statp->pfcode = 0; |
| 145 | statp->_vcsock = -1; |
| 146 | statp->_flags = 0; |
| 147 | statp->__glibc_unused_qhook = NULL; |
| 148 | statp->__glibc_unused_rhook = NULL; |
| 149 | statp->_u._ext.nscount = 0; |
| 150 | for (n = 0; n < MAXNS; n++) |
| 151 | statp->_u._ext.nsaddrs[n] = NULL; |
| 152 | |
| 153 | /* Allow user to override the local domain definition */ |
| 154 | if ((cp = getenv("LOCALDOMAIN" )) != NULL) { |
| 155 | (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 156 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 157 | haveenv++; |
| 158 | |
| 159 | /* |
| 160 | * Set search list to be blank-separated strings |
| 161 | * from rest of env value. Permits users of LOCALDOMAIN |
| 162 | * to still have a search list, and anyone to set the |
| 163 | * one that they want to use as an individual (even more |
| 164 | * important now that the rfc1535 stuff restricts searches) |
| 165 | */ |
| 166 | cp = statp->defdname; |
| 167 | pp = statp->dnsrch; |
| 168 | *pp++ = cp; |
| 169 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
| 170 | if (*cp == '\n') /* silly backwards compat */ |
| 171 | break; |
| 172 | else if (*cp == ' ' || *cp == '\t') { |
| 173 | *cp = 0; |
| 174 | n = 1; |
| 175 | } else if (n) { |
| 176 | *pp++ = cp; |
| 177 | n = 0; |
| 178 | havesearch = 1; |
| 179 | } |
| 180 | } |
| 181 | /* null terminate last domain if there are excess */ |
| 182 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') |
| 183 | cp++; |
| 184 | *cp = '\0'; |
| 185 | *pp++ = 0; |
| 186 | } |
| 187 | |
| 188 | #define MATCH(line, name) \ |
| 189 | (!strncmp(line, name, sizeof(name) - 1) && \ |
| 190 | (line[sizeof(name) - 1] == ' ' || \ |
| 191 | line[sizeof(name) - 1] == '\t')) |
| 192 | |
| 193 | if ((fp = fopen(_PATH_RESCONF, "rce" )) != NULL) { |
| 194 | /* No threads use this stream. */ |
| 195 | __fsetlocking (fp, FSETLOCKING_BYCALLER); |
| 196 | /* read the config file */ |
| 197 | while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) { |
| 198 | /* skip comments */ |
| 199 | if (*buf == ';' || *buf == '#') |
| 200 | continue; |
| 201 | /* read default domain name */ |
| 202 | if (MATCH(buf, "domain" )) { |
| 203 | if (haveenv) /* skip if have from environ */ |
| 204 | continue; |
| 205 | cp = buf + sizeof("domain" ) - 1; |
| 206 | while (*cp == ' ' || *cp == '\t') |
| 207 | cp++; |
| 208 | if ((*cp == '\0') || (*cp == '\n')) |
| 209 | continue; |
| 210 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 211 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 212 | if ((cp = strpbrk(statp->defdname, " \t\n" )) != NULL) |
| 213 | *cp = '\0'; |
| 214 | havesearch = 0; |
| 215 | continue; |
| 216 | } |
| 217 | /* set search list */ |
| 218 | if (MATCH(buf, "search" )) { |
| 219 | if (haveenv) /* skip if have from environ */ |
| 220 | continue; |
| 221 | cp = buf + sizeof("search" ) - 1; |
| 222 | while (*cp == ' ' || *cp == '\t') |
| 223 | cp++; |
| 224 | if ((*cp == '\0') || (*cp == '\n')) |
| 225 | continue; |
| 226 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 227 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 228 | if ((cp = strchr(statp->defdname, '\n')) != NULL) |
| 229 | *cp = '\0'; |
| 230 | /* |
| 231 | * Set search list to be blank-separated strings |
| 232 | * on rest of line. |
| 233 | */ |
| 234 | cp = statp->defdname; |
| 235 | pp = statp->dnsrch; |
| 236 | *pp++ = cp; |
| 237 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
| 238 | if (*cp == ' ' || *cp == '\t') { |
| 239 | *cp = 0; |
| 240 | n = 1; |
| 241 | } else if (n) { |
| 242 | *pp++ = cp; |
| 243 | n = 0; |
| 244 | } |
| 245 | } |
| 246 | /* null terminate last domain if there are excess */ |
| 247 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
| 248 | cp++; |
| 249 | *cp = '\0'; |
| 250 | *pp++ = 0; |
| 251 | havesearch = 1; |
| 252 | continue; |
| 253 | } |
| 254 | /* read nameservers to query */ |
| 255 | if (MATCH(buf, "nameserver" ) && nserv < MAXNS) { |
| 256 | struct in_addr a; |
| 257 | |
| 258 | cp = buf + sizeof("nameserver" ) - 1; |
| 259 | while (*cp == ' ' || *cp == '\t') |
| 260 | cp++; |
| 261 | if ((*cp != '\0') && (*cp != '\n') |
| 262 | && __inet_aton(cp, &a)) { |
| 263 | statp->nsaddr_list[nserv].sin_addr = a; |
| 264 | statp->nsaddr_list[nserv].sin_family = AF_INET; |
| 265 | statp->nsaddr_list[nserv].sin_port = |
| 266 | htons(NAMESERVER_PORT); |
| 267 | nserv++; |
| 268 | } else { |
| 269 | struct in6_addr a6; |
| 270 | char *el; |
| 271 | |
| 272 | if ((el = strpbrk(cp, " \t\n" )) != NULL) |
| 273 | *el = '\0'; |
| 274 | if ((el = strchr(cp, SCOPE_DELIMITER)) != NULL) |
| 275 | *el = '\0'; |
| 276 | if ((*cp != '\0') && |
| 277 | (__inet_pton(AF_INET6, cp, &a6) > 0)) { |
| 278 | struct sockaddr_in6 *sa6; |
| 279 | |
| 280 | sa6 = malloc(sizeof(*sa6)); |
| 281 | if (sa6 != NULL) { |
| 282 | sa6->sin6_family = AF_INET6; |
| 283 | sa6->sin6_port = htons(NAMESERVER_PORT); |
| 284 | sa6->sin6_flowinfo = 0; |
| 285 | sa6->sin6_addr = a6; |
| 286 | |
| 287 | sa6->sin6_scope_id = 0; |
| 288 | if (__glibc_likely (el != NULL)) { |
| 289 | /* Ignore errors, for backwards |
| 290 | compatibility. */ |
| 291 | (void) __inet6_scopeid_pton |
| 292 | (&a6, el + 1, &sa6->sin6_scope_id); |
| 293 | } |
| 294 | |
| 295 | statp->nsaddr_list[nserv].sin_family = 0; |
| 296 | statp->_u._ext.nsaddrs[nserv] = sa6; |
| 297 | statp->_u._ext.nssocks[nserv] = -1; |
| 298 | have_serv6 = 1; |
| 299 | nserv++; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | continue; |
| 304 | } |
| 305 | if (MATCH(buf, "sortlist" )) { |
| 306 | struct in_addr a; |
| 307 | |
| 308 | cp = buf + sizeof("sortlist" ) - 1; |
| 309 | while (nsort < MAXRESOLVSORT) { |
| 310 | while (*cp == ' ' || *cp == '\t') |
| 311 | cp++; |
| 312 | if (*cp == '\0' || *cp == '\n' || *cp == ';') |
| 313 | break; |
| 314 | net = cp; |
| 315 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && |
| 316 | isascii(*cp) && !isspace(*cp)) |
| 317 | cp++; |
| 318 | n = *cp; |
| 319 | *cp = 0; |
| 320 | if (__inet_aton(net, &a)) { |
| 321 | statp->sort_list[nsort].addr = a; |
| 322 | if (ISSORTMASK(n)) { |
| 323 | *cp++ = n; |
| 324 | net = cp; |
| 325 | while (*cp && *cp != ';' && |
| 326 | isascii(*cp) && !isspace(*cp)) |
| 327 | cp++; |
| 328 | n = *cp; |
| 329 | *cp = 0; |
| 330 | if (__inet_aton(net, &a)) { |
| 331 | statp->sort_list[nsort].mask = a.s_addr; |
| 332 | } else { |
| 333 | statp->sort_list[nsort].mask = |
| 334 | net_mask(statp->sort_list[nsort].addr); |
| 335 | } |
| 336 | } else { |
| 337 | statp->sort_list[nsort].mask = |
| 338 | net_mask(statp->sort_list[nsort].addr); |
| 339 | } |
| 340 | nsort++; |
| 341 | } |
| 342 | *cp = n; |
| 343 | } |
| 344 | continue; |
| 345 | } |
| 346 | if (MATCH(buf, "options" )) { |
| 347 | res_setoptions(statp, buf + sizeof("options" ) - 1, "conf" ); |
| 348 | continue; |
| 349 | } |
| 350 | } |
| 351 | statp->nscount = nserv; |
| 352 | if (have_serv6) { |
| 353 | /* We try IPv6 servers again. */ |
| 354 | statp->ipv6_unavail = false; |
| 355 | } |
| 356 | statp->nsort = nsort; |
| 357 | (void) fclose(fp); |
| 358 | } |
| 359 | if (__builtin_expect(statp->nscount == 0, 0)) { |
| 360 | statp->nsaddr.sin_addr = __inet_makeaddr(IN_LOOPBACKNET, 1); |
| 361 | statp->nsaddr.sin_family = AF_INET; |
| 362 | statp->nsaddr.sin_port = htons(NAMESERVER_PORT); |
| 363 | statp->nscount = 1; |
| 364 | } |
| 365 | if (statp->defdname[0] == 0 && |
| 366 | __gethostname(buf, sizeof(statp->defdname) - 1) == 0 && |
| 367 | (cp = strchr(buf, '.')) != NULL) |
| 368 | strcpy(statp->defdname, cp + 1); |
| 369 | |
| 370 | /* find components of local domain that might be searched */ |
| 371 | if (havesearch == 0) { |
| 372 | pp = statp->dnsrch; |
| 373 | *pp++ = statp->defdname; |
| 374 | *pp = NULL; |
| 375 | |
| 376 | } |
| 377 | |
| 378 | if ((cp = getenv("RES_OPTIONS" )) != NULL) |
| 379 | res_setoptions(statp, cp, "env" ); |
| 380 | statp->options |= RES_INIT; |
| 381 | return (0); |
| 382 | } |
| 383 | |
| 384 | static void |
| 385 | internal_function |
| 386 | res_setoptions(res_state statp, const char *options, const char *source) { |
| 387 | const char *cp = options; |
| 388 | int i; |
| 389 | |
| 390 | #ifdef DEBUG |
| 391 | if (statp->options & RES_DEBUG) |
| 392 | printf(";; res_setoptions(\"%s\", \"%s\")...\n" , |
| 393 | options, source); |
| 394 | #endif |
| 395 | while (*cp) { |
| 396 | /* skip leading and inner runs of spaces */ |
| 397 | while (*cp == ' ' || *cp == '\t') |
| 398 | cp++; |
| 399 | /* search for and process individual options */ |
| 400 | if (!strncmp(cp, "ndots:" , sizeof("ndots:" ) - 1)) { |
| 401 | i = atoi(cp + sizeof("ndots:" ) - 1); |
| 402 | if (i <= RES_MAXNDOTS) |
| 403 | statp->ndots = i; |
| 404 | else |
| 405 | statp->ndots = RES_MAXNDOTS; |
| 406 | #ifdef DEBUG |
| 407 | if (statp->options & RES_DEBUG) |
| 408 | printf(";;\tndots=%d\n" , statp->ndots); |
| 409 | #endif |
| 410 | } else if (!strncmp(cp, "timeout:" , sizeof("timeout:" ) - 1)) { |
| 411 | i = atoi(cp + sizeof("timeout:" ) - 1); |
| 412 | if (i <= RES_MAXRETRANS) |
| 413 | statp->retrans = i; |
| 414 | else |
| 415 | statp->retrans = RES_MAXRETRANS; |
| 416 | } else if (!strncmp(cp, "attempts:" , sizeof("attempts:" ) - 1)){ |
| 417 | i = atoi(cp + sizeof("attempts:" ) - 1); |
| 418 | if (i <= RES_MAXRETRY) |
| 419 | statp->retry = i; |
| 420 | else |
| 421 | statp->retry = RES_MAXRETRY; |
| 422 | } else if (!strncmp(cp, "debug" , sizeof("debug" ) - 1)) { |
| 423 | #ifdef DEBUG |
| 424 | if (!(statp->options & RES_DEBUG)) { |
| 425 | printf(";; res_setoptions(\"%s\", \"%s\")..\n" , |
| 426 | options, source); |
| 427 | statp->options |= RES_DEBUG; |
| 428 | } |
| 429 | printf(";;\tdebug\n" ); |
| 430 | #endif |
| 431 | } else { |
| 432 | static const struct |
| 433 | { |
| 434 | char str[22]; |
| 435 | uint8_t len; |
| 436 | uint8_t clear; |
| 437 | unsigned long int flag; |
| 438 | } options[] = { |
| 439 | #define STRnLEN(str) str, sizeof (str) - 1 |
| 440 | { STRnLEN ("inet6" ), 0, DEPRECATED_RES_USE_INET6 }, |
| 441 | { STRnLEN ("rotate" ), 0, RES_ROTATE }, |
| 442 | { STRnLEN ("edns0" ), 0, RES_USE_EDNS0 }, |
| 443 | { STRnLEN ("single-request-reopen" ), 0, RES_SNGLKUPREOP }, |
| 444 | { STRnLEN ("single-request" ), 0, RES_SNGLKUP }, |
| 445 | { STRnLEN ("no_tld_query" ), 0, RES_NOTLDQUERY }, |
| 446 | { STRnLEN ("no-tld-query" ), 0, RES_NOTLDQUERY }, |
| 447 | { STRnLEN ("use-vc" ), 0, RES_USEVC } |
| 448 | }; |
| 449 | #define noptions (sizeof (options) / sizeof (options[0])) |
| 450 | int i; |
| 451 | for (i = 0; i < noptions; ++i) |
| 452 | if (strncmp (cp, options[i].str, options[i].len) == 0) |
| 453 | { |
| 454 | if (options[i].clear) |
| 455 | statp->options &= options[i].flag; |
| 456 | else |
| 457 | statp->options |= options[i].flag; |
| 458 | break; |
| 459 | } |
| 460 | if (i == noptions) { |
| 461 | /* XXX - print a warning here? */ |
| 462 | } |
| 463 | } |
| 464 | /* skip to next run of spaces */ |
| 465 | while (*cp && *cp != ' ' && *cp != '\t') |
| 466 | cp++; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* XXX - should really support CIDR which means explicit masks always. */ |
| 471 | /* XXX - should really use system's version of this */ |
| 472 | static u_int32_t |
| 473 | net_mask (struct in_addr in) |
| 474 | { |
| 475 | u_int32_t i = ntohl(in.s_addr); |
| 476 | |
| 477 | if (IN_CLASSA(i)) |
| 478 | return (htonl(IN_CLASSA_NET)); |
| 479 | else if (IN_CLASSB(i)) |
| 480 | return (htonl(IN_CLASSB_NET)); |
| 481 | return (htonl(IN_CLASSC_NET)); |
| 482 | } |
| 483 | |
| 484 | u_int |
| 485 | res_randomid(void) { |
| 486 | return 0xffff & __getpid(); |
| 487 | } |
| 488 | libc_hidden_def (__res_randomid) |
| 489 | |
| 490 | |
| 491 | /* |
| 492 | * This routine is for closing the socket if a virtual circuit is used and |
| 493 | * the program wants to close it. This provides support for endhostent() |
| 494 | * which expects to close the socket. |
| 495 | * |
| 496 | * This routine is not expected to be user visible. |
| 497 | */ |
| 498 | void |
| 499 | __res_iclose(res_state statp, bool free_addr) { |
| 500 | int ns; |
| 501 | |
| 502 | if (statp->_vcsock >= 0) { |
| 503 | close_not_cancel_no_status(statp->_vcsock); |
| 504 | statp->_vcsock = -1; |
| 505 | statp->_flags &= ~(RES_F_VC | RES_F_CONN); |
| 506 | } |
| 507 | for (ns = 0; ns < statp->nscount; ns++) |
| 508 | if (statp->_u._ext.nsaddrs[ns]) { |
| 509 | if (statp->_u._ext.nssocks[ns] != -1) { |
| 510 | close_not_cancel_no_status(statp->_u._ext.nssocks[ns]); |
| 511 | statp->_u._ext.nssocks[ns] = -1; |
| 512 | } |
| 513 | if (free_addr) { |
| 514 | free (statp->_u._ext.nsaddrs[ns]); |
| 515 | statp->_u._ext.nsaddrs[ns] = NULL; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | libc_hidden_def (__res_iclose) |
| 520 | |
| 521 | void |
| 522 | res_nclose(res_state statp) |
| 523 | { |
| 524 | __res_iclose (statp, true); |
| 525 | } |
| 526 | libc_hidden_def (__res_nclose) |
| 527 | |
| 528 | /* This is called when a thread is exiting to free resources held in _res. */ |
| 529 | static void __attribute__ ((section ("__libc_thread_freeres_fn" ))) |
| 530 | res_thread_freeres (void) |
| 531 | { |
| 532 | if (_res.nscount == 0) |
| 533 | /* Never called res_ninit. */ |
| 534 | return; |
| 535 | |
| 536 | __res_iclose (&_res, true); /* Close any VC sockets. */ |
| 537 | |
| 538 | /* Make sure we do a full re-initialization the next time. */ |
| 539 | _res.options = 0; |
| 540 | } |
| 541 | text_set_element (__libc_thread_subfreeres, res_thread_freeres); |
| 542 | text_set_element (__libc_subfreeres, res_thread_freeres); |
| 543 | |