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 * 3. 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 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 53 * 54 * Permission to use, copy, modify, and distribute this software for any 55 * purpose with or without fee is hereby granted, provided that the above 56 * copyright notice and this permission notice appear in all copies. 57 * 58 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 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[] = "$Id: res_init.c,v 1.23 2007/07/09 01:43:23 marka Exp $"; 70 #endif /* LIBC_SCCS and not lint */ 71 72 #include "port_before.h" 73 74 #ifdef _LIBC 75 #include "namespace.h" 76 #endif 77 #include <sys/types.h> 78 #include <sys/param.h> 79 #include <sys/socket.h> 80 #include <sys/time.h> 81 82 #include <netinet/in.h> 83 #include <arpa/inet.h> 84 #include <arpa/nameser.h> 85 86 #include <ctype.h> 87 #include <stdio.h> 88 #include <stdlib.h> 89 #include <string.h> 90 #include <unistd.h> 91 #include <netdb.h> 92 #ifdef _LIBC 93 #include "un-namespace.h" 94 #endif 95 96 #include "port_after.h" 97 98 /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ 99 #include <resolv.h> 100 101 #include "res_private.h" 102 103 /*% Options. Should all be left alone. */ 104 #define RESOLVSORT 105 #define DEBUG 106 107 #ifdef SOLARIS2 108 #include <sys/systeminfo.h> 109 #endif 110 111 static void res_setoptions __P((res_state, const char *, const char *)); 112 113 #ifdef RESOLVSORT 114 static const char sort_mask[] = "/&"; 115 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) 116 static u_int32_t net_mask __P((struct in_addr)); 117 #endif 118 119 #if !defined(isascii) /*%< XXX - could be a function */ 120 # define isascii(c) (!(c & 0200)) 121 #endif 122 123 /* 124 * Resolver state default settings. 125 */ 126 127 /*% 128 * Set up default settings. If the configuration file exist, the values 129 * there will have precedence. Otherwise, the server address is set to 130 * INADDR_ANY and the default domain name comes from the gethostname(). 131 * 132 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 133 * rather than INADDR_ANY ("0.0.0.0") as the default name server address 134 * since it was noted that INADDR_ANY actually meant ``the first interface 135 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, 136 * it had to be "up" in order for you to reach your own name server. It 137 * was later decided that since the recommended practice is to always 138 * install local static routes through 127.0.0.1 for all your network 139 * interfaces, that we could solve this problem without a code change. 140 * 141 * The configuration file should always be used, since it is the only way 142 * to specify a default domain. If you are running a server on your local 143 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" 144 * in the configuration file. 145 * 146 * Return 0 if completes successfully, -1 on error 147 */ 148 int 149 res_ninit(res_state statp) { 150 extern int __res_vinit(res_state, int); 151 152 return (__res_vinit(statp, 0)); 153 } 154 155 /*% This function has to be reachable by res_data.c but not publically. */ 156 int 157 __res_vinit(res_state statp, int preinit) { 158 FILE *fp; 159 char *cp, **pp; 160 int n; 161 char buf[BUFSIZ]; 162 int nserv = 0; /*%< number of nameserver records read from file */ 163 int haveenv = 0; 164 int havesearch = 0; 165 #ifdef RESOLVSORT 166 int nsort = 0; 167 char *net; 168 #endif 169 int dots; 170 union res_sockaddr_union u[2]; 171 int maxns = MAXNS; 172 173 RES_SET_H_ERRNO(statp, 0); 174 if (statp->_u._ext.ext != NULL) 175 res_ndestroy(statp); 176 177 if (!preinit) { 178 statp->retrans = RES_TIMEOUT; 179 statp->retry = RES_DFLRETRY; 180 statp->options = RES_DEFAULT; 181 statp->id = res_randomid(); 182 } 183 184 memset(u, 0, sizeof(u)); 185 #ifdef USELOOPBACK 186 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 187 #else 188 u[nserv].sin.sin_addr.s_addr = INADDR_ANY; 189 #endif 190 u[nserv].sin.sin_family = AF_INET; 191 u[nserv].sin.sin_port = htons(NAMESERVER_PORT); 192 #ifdef HAVE_SA_LEN 193 u[nserv].sin.sin_len = sizeof(struct sockaddr_in); 194 #endif 195 nserv++; 196 #ifdef HAS_INET6_STRUCTS 197 #ifdef USELOOPBACK 198 u[nserv].sin6.sin6_addr = in6addr_loopback; 199 #else 200 u[nserv].sin6.sin6_addr = in6addr_any; 201 #endif 202 u[nserv].sin6.sin6_family = AF_INET6; 203 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); 204 #ifdef HAVE_SA_LEN 205 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); 206 #endif 207 nserv++; 208 #endif 209 statp->nscount = 0; 210 statp->ndots = 1; 211 statp->pfcode = 0; 212 statp->_vcsock = -1; 213 statp->_flags = 0; 214 statp->qhook = NULL; 215 statp->rhook = NULL; 216 statp->_u._ext.nscount = 0; 217 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); 218 if (statp->_u._ext.ext != NULL) { 219 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); 220 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; 221 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); 222 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); 223 } else { 224 /* 225 * Historically res_init() rarely, if at all, failed. 226 * Examples and applications exist which do not check 227 * our return code. Furthermore several applications 228 * simply call us to get the systems domainname. So 229 * rather then immediately fail here we store the 230 * failure, which is returned later, in h_errno. And 231 * prevent the collection of 'nameserver' information 232 * by setting maxns to 0. Thus applications that fail 233 * to check our return code wont be able to make 234 * queries anyhow. 235 */ 236 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 237 maxns = 0; 238 } 239 #ifdef RESOLVSORT 240 statp->nsort = 0; 241 #endif 242 res_setservers(statp, u, nserv); 243 244 #ifdef SOLARIS2 245 /* 246 * The old libresolv derived the defaultdomain from NIS/NIS+. 247 * We want to keep this behaviour 248 */ 249 { 250 char buf[sizeof(statp->defdname)], *cp; 251 int ret; 252 253 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && 254 (unsigned int)ret <= sizeof(buf)) { 255 if (buf[0] == '+') 256 buf[0] = '.'; 257 cp = strchr(buf, '.'); 258 cp = (cp == NULL) ? buf : (cp + 1); 259 strncpy(statp->defdname, cp, 260 sizeof(statp->defdname) - 1); 261 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 262 } 263 } 264 #endif /* SOLARIS2 */ 265 266 /* Allow user to override the local domain definition */ 267 if ((cp = getenv("LOCALDOMAIN")) != NULL) { 268 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 269 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 270 haveenv++; 271 272 /* 273 * Set search list to be blank-separated strings 274 * from rest of env value. Permits users of LOCALDOMAIN 275 * to still have a search list, and anyone to set the 276 * one that they want to use as an individual (even more 277 * important now that the rfc1535 stuff restricts searches) 278 */ 279 cp = statp->defdname; 280 pp = statp->dnsrch; 281 *pp++ = cp; 282 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 283 if (*cp == '\n') /*%< silly backwards compat */ 284 break; 285 else if (*cp == ' ' || *cp == '\t') { 286 *cp = 0; 287 n = 1; 288 } else if (n) { 289 *pp++ = cp; 290 n = 0; 291 havesearch = 1; 292 } 293 } 294 /* null terminate last domain if there are excess */ 295 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') 296 cp++; 297 *cp = '\0'; 298 *pp++ = NULL; 299 } 300 301 #define MATCH(line, name) \ 302 (!strncmp(line, name, sizeof(name) - 1) && \ 303 (line[sizeof(name) - 1] == ' ' || \ 304 line[sizeof(name) - 1] == '\t')) 305 306 nserv = 0; 307 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 308 /* read the config file */ 309 while (fgets(buf, sizeof(buf), fp) != NULL) { 310 /* skip comments */ 311 if (*buf == ';' || *buf == '#') 312 continue; 313 /* read default domain name */ 314 if (MATCH(buf, "domain")) { 315 if (haveenv) /*%< skip if have from environ */ 316 continue; 317 cp = buf + sizeof("domain") - 1; 318 while (*cp == ' ' || *cp == '\t') 319 cp++; 320 if ((*cp == '\0') || (*cp == '\n')) 321 continue; 322 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 323 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 324 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) 325 *cp = '\0'; 326 havesearch = 0; 327 continue; 328 } 329 /* set search list */ 330 if (MATCH(buf, "search")) { 331 if (haveenv) /*%< skip if have from environ */ 332 continue; 333 cp = buf + sizeof("search") - 1; 334 while (*cp == ' ' || *cp == '\t') 335 cp++; 336 if ((*cp == '\0') || (*cp == '\n')) 337 continue; 338 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 339 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 340 if ((cp = strchr(statp->defdname, '\n')) != NULL) 341 *cp = '\0'; 342 /* 343 * Set search list to be blank-separated strings 344 * on rest of line. 345 */ 346 cp = statp->defdname; 347 pp = statp->dnsrch; 348 *pp++ = cp; 349 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 350 if (*cp == ' ' || *cp == '\t') { 351 *cp = 0; 352 n = 1; 353 } else if (n) { 354 *pp++ = cp; 355 n = 0; 356 } 357 } 358 /* null terminate last domain if there are excess */ 359 while (*cp != '\0' && *cp != ' ' && *cp != '\t') 360 cp++; 361 *cp = '\0'; 362 *pp++ = NULL; 363 havesearch = 1; 364 continue; 365 } 366 /* read nameservers to query */ 367 if (MATCH(buf, "nameserver") && nserv < maxns) { 368 struct addrinfo hints, *ai; 369 char sbuf[NI_MAXSERV]; 370 const size_t minsiz = 371 sizeof(statp->_u._ext.ext->nsaddrs[0]); 372 373 cp = buf + sizeof("nameserver") - 1; 374 while (*cp == ' ' || *cp == '\t') 375 cp++; 376 cp[strcspn(cp, ";# \t\n")] = '\0'; 377 if ((*cp != '\0') && (*cp != '\n')) { 378 memset(&hints, 0, sizeof(hints)); 379 hints.ai_family = PF_UNSPEC; 380 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 381 hints.ai_flags = AI_NUMERICHOST; 382 sprintf(sbuf, "%u", NAMESERVER_PORT); 383 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && 384 ai->ai_addrlen <= minsiz) { 385 if (statp->_u._ext.ext != NULL) { 386 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 387 ai->ai_addr, ai->ai_addrlen); 388 } 389 if (ai->ai_addrlen <= 390 sizeof(statp->nsaddr_list[nserv])) { 391 memcpy(&statp->nsaddr_list[nserv], 392 ai->ai_addr, ai->ai_addrlen); 393 } else 394 statp->nsaddr_list[nserv].sin_family = 0; 395 freeaddrinfo(ai); 396 nserv++; 397 } 398 } 399 continue; 400 } 401 #ifdef RESOLVSORT 402 if (MATCH(buf, "sortlist")) { 403 struct in_addr a; 404 405 cp = buf + sizeof("sortlist") - 1; 406 while (nsort < MAXRESOLVSORT) { 407 while (*cp == ' ' || *cp == '\t') 408 cp++; 409 if (*cp == '\0' || *cp == '\n' || *cp == ';') 410 break; 411 net = cp; 412 while (*cp && !ISSORTMASK(*cp) && *cp != ';' && 413 isascii(*cp) && !isspace((unsigned char)*cp)) 414 cp++; 415 n = *cp; 416 *cp = 0; 417 if (inet_aton(net, &a)) { 418 statp->sort_list[nsort].addr = a; 419 if (ISSORTMASK(n)) { 420 *cp++ = n; 421 net = cp; 422 while (*cp && *cp != ';' && 423 isascii(*cp) && 424 !isspace((unsigned char)*cp)) 425 cp++; 426 n = *cp; 427 *cp = 0; 428 if (inet_aton(net, &a)) { 429 statp->sort_list[nsort].mask = a.s_addr; 430 } else { 431 statp->sort_list[nsort].mask = 432 net_mask(statp->sort_list[nsort].addr); 433 } 434 } else { 435 statp->sort_list[nsort].mask = 436 net_mask(statp->sort_list[nsort].addr); 437 } 438 nsort++; 439 } 440 *cp = n; 441 } 442 continue; 443 } 444 #endif 445 if (MATCH(buf, "options")) 446 res_setoptions(statp, buf + sizeof("options") - 1, "conf"); 447 } 448 if (nserv > 0) 449 statp->nscount = nserv; 450 #ifdef RESOLVSORT 451 statp->nsort = nsort; 452 #endif 453 (void) fclose(fp); 454 } 455 /* 456 * Last chance to get a nameserver. This should not normally 457 * be necessary 458 */ 459 #ifdef NO_RESOLV_CONF 460 if(nserv == 0) 461 nserv = get_nameservers(statp); 462 #endif 463 464 if (statp->defdname[0] == 0 && 465 gethostname(buf, sizeof(statp->defdname) - 1) == 0 && 466 (cp = strchr(buf, '.')) != NULL) 467 strcpy(statp->defdname, cp + 1); 468 469 /* find components of local domain that might be searched */ 470 if (havesearch == 0) { 471 pp = statp->dnsrch; 472 *pp++ = statp->defdname; 473 *pp = NULL; 474 475 dots = 0; 476 for (cp = statp->defdname; *cp; cp++) 477 dots += (*cp == '.'); 478 479 cp = statp->defdname; 480 while (pp < statp->dnsrch + MAXDFLSRCH) { 481 if (dots < LOCALDOMAINPARTS) 482 break; 483 cp = strchr(cp, '.') + 1; /*%< we know there is one */ 484 *pp++ = cp; 485 dots--; 486 } 487 *pp = NULL; 488 #ifdef DEBUG 489 if (statp->options & RES_DEBUG) { 490 printf(";; res_init()... default dnsrch list:\n"); 491 for (pp = statp->dnsrch; *pp; pp++) 492 printf(";;\t%s\n", *pp); 493 printf(";;\t..END..\n"); 494 } 495 #endif 496 } 497 498 if ((cp = getenv("RES_OPTIONS")) != NULL) 499 res_setoptions(statp, cp, "env"); 500 statp->options |= RES_INIT; 501 return (statp->res_h_errno); 502 } 503 504 static void 505 res_setoptions(res_state statp, const char *options, const char *source) 506 { 507 const char *cp = options; 508 int i; 509 #ifndef _LIBC 510 struct __res_state_ext *ext = statp->_u._ext.ext; 511 #endif 512 513 #ifdef DEBUG 514 if (statp->options & RES_DEBUG) 515 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 516 options, source); 517 #endif 518 while (*cp) { 519 /* skip leading and inner runs of spaces */ 520 while (*cp == ' ' || *cp == '\t') 521 cp++; 522 /* search for and process individual options */ 523 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { 524 i = atoi(cp + sizeof("ndots:") - 1); 525 if (i <= RES_MAXNDOTS) 526 statp->ndots = i; 527 else 528 statp->ndots = RES_MAXNDOTS; 529 #ifdef DEBUG 530 if (statp->options & RES_DEBUG) 531 printf(";;\tndots=%d\n", statp->ndots); 532 #endif 533 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { 534 i = atoi(cp + sizeof("timeout:") - 1); 535 if (i <= RES_MAXRETRANS) 536 statp->retrans = i; 537 else 538 statp->retrans = RES_MAXRETRANS; 539 #ifdef DEBUG 540 if (statp->options & RES_DEBUG) 541 printf(";;\ttimeout=%d\n", statp->retrans); 542 #endif 543 #ifdef SOLARIS2 544 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { 545 /* 546 * For backward compatibility, 'retrans' is 547 * supported as an alias for 'timeout', though 548 * without an imposed maximum. 549 */ 550 statp->retrans = atoi(cp + sizeof("retrans:") - 1); 551 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ 552 /* 553 * For backward compatibility, 'retry' is 554 * supported as an alias for 'attempts', though 555 * without an imposed maximum. 556 */ 557 statp->retry = atoi(cp + sizeof("retry:") - 1); 558 #endif /* SOLARIS2 */ 559 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ 560 i = atoi(cp + sizeof("attempts:") - 1); 561 if (i <= RES_MAXRETRY) 562 statp->retry = i; 563 else 564 statp->retry = RES_MAXRETRY; 565 #ifdef DEBUG 566 if (statp->options & RES_DEBUG) 567 printf(";;\tattempts=%d\n", statp->retry); 568 #endif 569 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { 570 #ifdef DEBUG 571 if (!(statp->options & RES_DEBUG)) { 572 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 573 options, source); 574 statp->options |= RES_DEBUG; 575 } 576 printf(";;\tdebug\n"); 577 #endif 578 } else if (!strncmp(cp, "no_tld_query", 579 sizeof("no_tld_query") - 1) || 580 !strncmp(cp, "no-tld-query", 581 sizeof("no-tld-query") - 1)) { 582 statp->options |= RES_NOTLDQUERY; 583 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { 584 statp->options |= RES_USE_INET6; 585 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { 586 statp->options |= RES_ROTATE; 587 } else if (!strncmp(cp, "no-check-names", 588 sizeof("no-check-names") - 1)) { 589 statp->options |= RES_NOCHECKNAME; 590 } 591 #ifdef RES_USE_EDNS0 592 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { 593 statp->options |= RES_USE_EDNS0; 594 } 595 #endif 596 #ifndef _LIBC 597 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { 598 statp->options |= RES_USE_DNAME; 599 } 600 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { 601 if (ext == NULL) 602 goto skip; 603 cp += sizeof("nibble:") - 1; 604 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); 605 strncpy(ext->nsuffix, cp, i); 606 ext->nsuffix[i] = '\0'; 607 } 608 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { 609 if (ext == NULL) 610 goto skip; 611 cp += sizeof("nibble2:") - 1; 612 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); 613 strncpy(ext->nsuffix2, cp, i); 614 ext->nsuffix2[i] = '\0'; 615 } 616 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { 617 cp += sizeof("v6revmode:") - 1; 618 /* "nibble" and "bitstring" used to be valid */ 619 if (!strncmp(cp, "single", sizeof("single") - 1)) { 620 statp->options |= RES_NO_NIBBLE2; 621 } else if (!strncmp(cp, "both", sizeof("both") - 1)) { 622 statp->options &= 623 ~RES_NO_NIBBLE2; 624 } 625 } 626 #endif 627 else { 628 /* XXX - print a warning here? */ 629 } 630 #ifndef _LIBC 631 skip: 632 #endif 633 /* skip to next run of spaces */ 634 while (*cp && *cp != ' ' && *cp != '\t') 635 cp++; 636 } 637 } 638 639 #ifdef RESOLVSORT 640 /* XXX - should really support CIDR which means explicit masks always. */ 641 static u_int32_t 642 net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */ 643 { 644 u_int32_t i = ntohl(in.s_addr); 645 646 if (IN_CLASSA(i)) 647 return (htonl(IN_CLASSA_NET)); 648 else if (IN_CLASSB(i)) 649 return (htonl(IN_CLASSB_NET)); 650 return (htonl(IN_CLASSC_NET)); 651 } 652 #endif 653 654 u_int 655 res_randomid(void) { 656 struct timeval now; 657 658 gettimeofday(&now, NULL); 659 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); 660 } 661 662 /*% 663 * This routine is for closing the socket if a virtual circuit is used and 664 * the program wants to close it. This provides support for endhostent() 665 * which expects to close the socket. 666 * 667 * This routine is not expected to be user visible. 668 */ 669 void 670 res_nclose(res_state statp) { 671 int ns; 672 673 if (statp->_vcsock >= 0) { 674 #ifndef _LIBC 675 (void) close(statp->_vcsock); 676 #else 677 _close(statp->_vcsock); 678 #endif 679 statp->_vcsock = -1; 680 statp->_flags &= ~(RES_F_VC | RES_F_CONN); 681 } 682 for (ns = 0; ns < statp->_u._ext.nscount; ns++) { 683 if (statp->_u._ext.nssocks[ns] != -1) { 684 #ifndef _LIBC 685 (void) close(statp->_u._ext.nssocks[ns]); 686 #else 687 _close(statp->_u._ext.nssocks[ns]); 688 #endif 689 statp->_u._ext.nssocks[ns] = -1; 690 } 691 } 692 } 693 694 void 695 res_ndestroy(res_state statp) { 696 res_nclose(statp); 697 if (statp->_u._ext.ext != NULL) 698 free(statp->_u._ext.ext); 699 statp->options &= ~RES_INIT; 700 statp->_u._ext.ext = NULL; 701 } 702 703 #ifndef _LIBC 704 const char * 705 res_get_nibblesuffix(res_state statp) { 706 if (statp->_u._ext.ext) 707 return (statp->_u._ext.ext->nsuffix); 708 return ("ip6.arpa"); 709 } 710 711 const char * 712 res_get_nibblesuffix2(res_state statp) { 713 if (statp->_u._ext.ext) 714 return (statp->_u._ext.ext->nsuffix2); 715 return ("ip6.int"); 716 } 717 #endif 718 719 void 720 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { 721 int i, nserv; 722 size_t size; 723 724 /* close open servers */ 725 res_nclose(statp); 726 727 /* cause rtt times to be forgotten */ 728 statp->_u._ext.nscount = 0; 729 730 nserv = 0; 731 for (i = 0; i < cnt && nserv < MAXNS; i++) { 732 switch (set->sin.sin_family) { 733 case AF_INET: 734 size = sizeof(set->sin); 735 if (statp->_u._ext.ext) 736 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 737 &set->sin, size); 738 if (size <= sizeof(statp->nsaddr_list[nserv])) 739 memcpy(&statp->nsaddr_list[nserv], 740 &set->sin, size); 741 else 742 statp->nsaddr_list[nserv].sin_family = 0; 743 nserv++; 744 break; 745 746 #ifdef HAS_INET6_STRUCTS 747 case AF_INET6: 748 size = sizeof(set->sin6); 749 if (statp->_u._ext.ext) 750 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 751 &set->sin6, size); 752 if (size <= sizeof(statp->nsaddr_list[nserv])) 753 memcpy(&statp->nsaddr_list[nserv], 754 &set->sin6, size); 755 else 756 statp->nsaddr_list[nserv].sin_family = 0; 757 nserv++; 758 break; 759 #endif 760 761 default: 762 break; 763 } 764 set++; 765 } 766 statp->nscount = nserv; 767 768 } 769 770 int 771 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { 772 int i; 773 size_t size; 774 u_int16_t family; 775 776 for (i = 0; i < statp->nscount && i < cnt; i++) { 777 if (statp->_u._ext.ext) 778 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; 779 else 780 family = statp->nsaddr_list[i].sin_family; 781 782 switch (family) { 783 case AF_INET: 784 size = sizeof(set->sin); 785 if (statp->_u._ext.ext) 786 memcpy(&set->sin, 787 &statp->_u._ext.ext->nsaddrs[i], 788 size); 789 else 790 memcpy(&set->sin, &statp->nsaddr_list[i], 791 size); 792 break; 793 794 #ifdef HAS_INET6_STRUCTS 795 case AF_INET6: 796 size = sizeof(set->sin6); 797 if (statp->_u._ext.ext) 798 memcpy(&set->sin6, 799 &statp->_u._ext.ext->nsaddrs[i], 800 size); 801 else 802 memcpy(&set->sin6, &statp->nsaddr_list[i], 803 size); 804 break; 805 #endif 806 807 default: 808 set->sin.sin_family = 0; 809 break; 810 } 811 set++; 812 } 813 return (statp->nscount); 814 } 815 /*! \file */ 816