res_init.c revision 156956
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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34/* 35 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies, and that 40 * the name of Digital Equipment Corporation not be used in advertising or 41 * publicity pertaining to distribution of the document or software without 42 * specific, written prior permission. 43 * 44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51 * SOFTWARE. 52 */ 53 54/* 55 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 56 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 57 * 58 * Permission to use, copy, modify, and distribute this software for any 59 * purpose with or without fee is hereby granted, provided that the above 60 * copyright notice and this permission notice appear in all copies. 61 * 62 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 63 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 64 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 65 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 67 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 68 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 69 */ 70 71#if defined(LIBC_SCCS) && !defined(lint) 72static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 73static const char rcsid[] = "$Id: res_init.c,v 1.9.2.5.4.5 2005/11/03 00:00:52 marka Exp $"; 74#endif /* LIBC_SCCS and not lint */ 75#include <sys/cdefs.h> 76__FBSDID("$FreeBSD: head/lib/libc/resolv/res_init.c 156956 2006-03-21 15:37:16Z ume $"); 77 78#include "port_before.h" 79 80#include <sys/types.h> 81#include <sys/param.h> 82#include <sys/socket.h> 83#include <sys/time.h> 84 85#include <netinet/in.h> 86#include <arpa/inet.h> 87#include <arpa/nameser.h> 88 89#include <ctype.h> 90#include <stdio.h> 91#include <stdlib.h> 92#include <string.h> 93#include <unistd.h> 94#include <netdb.h> 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 111static void res_setoptions(res_state, const char *, const char *); 112 113#ifdef RESOLVSORT 114static const char sort_mask[] = "/&"; 115#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) 116static u_int32_t net_mask(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 */ 148int 149res_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. */ 156int 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 172 if (statp->_u._ext.ext != NULL) 173 res_ndestroy(statp); 174 175 if (!preinit) { 176 statp->retrans = RES_TIMEOUT; 177 statp->retry = RES_DFLRETRY; 178 statp->options = RES_DEFAULT; 179 statp->id = res_randomid(); 180 } 181 182 memset(u, 0, sizeof(u)); 183#ifdef USELOOPBACK 184 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 185#else 186 u[nserv].sin.sin_addr.s_addr = INADDR_ANY; 187#endif 188 u[nserv].sin.sin_family = AF_INET; 189 u[nserv].sin.sin_port = htons(NAMESERVER_PORT); 190#ifdef HAVE_SA_LEN 191 u[nserv].sin.sin_len = sizeof(struct sockaddr_in); 192#endif 193 nserv++; 194#ifdef HAS_INET6_STRUCTS 195#ifdef USELOOPBACK 196 u[nserv].sin6.sin6_addr = in6addr_loopback; 197#else 198 u[nserv].sin6.sin6_addr = in6addr_any; 199#endif 200 u[nserv].sin6.sin6_family = AF_INET6; 201 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); 202#ifdef HAVE_SA_LEN 203 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); 204#endif 205 nserv++; 206#endif 207 statp->nscount = 0; 208 statp->ndots = 1; 209 statp->pfcode = 0; 210 statp->_vcsock = -1; 211 statp->_flags = 0; 212 statp->qhook = NULL; 213 statp->rhook = NULL; 214 statp->_u._ext.nscount = 0; 215 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); 216 if (statp->_u._ext.ext != NULL) { 217 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); 218 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; 219 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); 220 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); 221 } else 222 return (-1); 223#ifdef RESOLVSORT 224 statp->nsort = 0; 225#endif 226 res_setservers(statp, u, nserv); 227 228#ifdef SOLARIS2 229 /* 230 * The old libresolv derived the defaultdomain from NIS/NIS+. 231 * We want to keep this behaviour 232 */ 233 { 234 char buf[sizeof(statp->defdname)], *cp; 235 int ret; 236 237 if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && 238 (unsigned int)ret <= sizeof(buf)) { 239 if (buf[0] == '+') 240 buf[0] = '.'; 241 cp = strchr(buf, '.'); 242 if (cp == NULL) { 243 if (strlcpy(statp->defdname, buf, 244 sizeof(statp->defdname)) 245 >= sizeof(statp->defdname)) 246 goto freedata; 247 } else { 248 if (strlcpy(statp->defdname, cp+1, 249 sizeof(statp->defdname)) 250 >= sizeof(statp->defdname)) 251 goto freedata; 252 } 253 } 254 } 255#endif /* SOLARIS2 */ 256 257 /* Allow user to override the local domain definition */ 258 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { 259 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 260 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 261 haveenv++; 262 263 /* 264 * Set search list to be blank-separated strings 265 * from rest of env value. Permits users of LOCALDOMAIN 266 * to still have a search list, and anyone to set the 267 * one that they want to use as an individual (even more 268 * important now that the rfc1535 stuff restricts searches) 269 */ 270 cp = statp->defdname; 271 pp = statp->dnsrch; 272 *pp++ = cp; 273 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 274 if (*cp == '\n') /* silly backwards compat */ 275 break; 276 else if (*cp == ' ' || *cp == '\t') { 277 *cp = 0; 278 n = 1; 279 } else if (n) { 280 *pp++ = cp; 281 n = 0; 282 havesearch = 1; 283 } 284 } 285 /* null terminate last domain if there are excess */ 286 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') 287 cp++; 288 *cp = '\0'; 289 *pp++ = 0; 290 } 291 292#define MATCH(line, name) \ 293 (!strncmp(line, name, sizeof(name) - 1) && \ 294 (line[sizeof(name) - 1] == ' ' || \ 295 line[sizeof(name) - 1] == '\t')) 296 297 nserv = 0; 298 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 299 /* read the config file */ 300 while (fgets(buf, sizeof(buf), fp) != NULL) { 301 /* skip comments */ 302 if (*buf == ';' || *buf == '#') 303 continue; 304 /* read default domain name */ 305 if (MATCH(buf, "domain")) { 306 if (haveenv) /* skip if have from environ */ 307 continue; 308 cp = buf + sizeof("domain") - 1; 309 while (*cp == ' ' || *cp == '\t') 310 cp++; 311 if ((*cp == '\0') || (*cp == '\n')) 312 continue; 313 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 314 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 315 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) 316 *cp = '\0'; 317 havesearch = 0; 318 continue; 319 } 320 /* set search list */ 321 if (MATCH(buf, "search")) { 322 if (haveenv) /* skip if have from environ */ 323 continue; 324 cp = buf + sizeof("search") - 1; 325 while (*cp == ' ' || *cp == '\t') 326 cp++; 327 if ((*cp == '\0') || (*cp == '\n')) 328 continue; 329 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 330 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 331 if ((cp = strchr(statp->defdname, '\n')) != NULL) 332 *cp = '\0'; 333 /* 334 * Set search list to be blank-separated strings 335 * on rest of line. 336 */ 337 cp = statp->defdname; 338 pp = statp->dnsrch; 339 *pp++ = cp; 340 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 341 if (*cp == ' ' || *cp == '\t') { 342 *cp = 0; 343 n = 1; 344 } else if (n) { 345 *pp++ = cp; 346 n = 0; 347 } 348 } 349 /* null terminate last domain if there are excess */ 350 while (*cp != '\0' && *cp != ' ' && *cp != '\t') 351 cp++; 352 *cp = '\0'; 353 *pp++ = 0; 354 havesearch = 1; 355 continue; 356 } 357 /* read nameservers to query */ 358 if (MATCH(buf, "nameserver") && nserv < MAXNS) { 359 struct addrinfo hints, *ai; 360 char sbuf[NI_MAXSERV]; 361 const size_t minsiz = 362 sizeof(statp->_u._ext.ext->nsaddrs[0]); 363 364 cp = buf + sizeof("nameserver") - 1; 365 while (*cp == ' ' || *cp == '\t') 366 cp++; 367 cp[strcspn(cp, ";# \t\n")] = '\0'; 368 if ((*cp != '\0') && (*cp != '\n')) { 369 memset(&hints, 0, sizeof(hints)); 370 hints.ai_family = PF_UNSPEC; 371 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 372 hints.ai_flags = AI_NUMERICHOST; 373 sprintf(sbuf, "%u", NAMESERVER_PORT); 374 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && 375 ai->ai_addrlen <= minsiz) { 376 if (statp->_u._ext.ext != NULL) { 377 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 378 ai->ai_addr, ai->ai_addrlen); 379 } 380 if (ai->ai_addrlen <= 381 sizeof(statp->nsaddr_list[nserv])) { 382 memcpy(&statp->nsaddr_list[nserv], 383 ai->ai_addr, ai->ai_addrlen); 384 } else 385 statp->nsaddr_list[nserv].sin_family = 0; 386 freeaddrinfo(ai); 387 nserv++; 388 } 389 } 390 continue; 391 } 392#ifdef RESOLVSORT 393 if (MATCH(buf, "sortlist")) { 394 struct in_addr a; 395 struct in6_addr a6; 396 int m, i; 397 u_char *u; 398 struct __res_state_ext *ext = statp->_u._ext.ext; 399 400 cp = buf + sizeof("sortlist") - 1; 401 while (nsort < MAXRESOLVSORT) { 402 while (*cp == ' ' || *cp == '\t') 403 cp++; 404 if (*cp == '\0' || *cp == '\n' || *cp == ';') 405 break; 406 net = cp; 407 while (*cp && !ISSORTMASK(*cp) && *cp != ';' && 408 isascii(*cp) && !isspace((unsigned char)*cp)) 409 cp++; 410 n = *cp; 411 *cp = 0; 412 if (inet_aton(net, &a)) { 413 statp->sort_list[nsort].addr = a; 414 if (ISSORTMASK(n)) { 415 *cp++ = n; 416 net = cp; 417 while (*cp && *cp != ';' && 418 isascii(*cp) && 419 !isspace((unsigned char)*cp)) 420 cp++; 421 n = *cp; 422 *cp = 0; 423 if (inet_aton(net, &a)) { 424 statp->sort_list[nsort].mask = a.s_addr; 425 } else { 426 statp->sort_list[nsort].mask = 427 net_mask(statp->sort_list[nsort].addr); 428 } 429 } else { 430 statp->sort_list[nsort].mask = 431 net_mask(statp->sort_list[nsort].addr); 432 } 433 ext->sort_list[nsort].af = AF_INET; 434 ext->sort_list[nsort].addr.ina = 435 statp->sort_list[nsort].addr; 436 ext->sort_list[nsort].mask.ina.s_addr = 437 statp->sort_list[nsort].mask; 438 nsort++; 439 } 440 else if (inet_pton(AF_INET6, net, &a6) == 1) { 441 442 ext->sort_list[nsort].af = AF_INET6; 443 ext->sort_list[nsort].addr.in6a = a6; 444 u = (u_char *)&ext->sort_list[nsort].mask.in6a; 445 *cp++ = n; 446 net = cp; 447 while (*cp && *cp != ';' && 448 isascii(*cp) && !isspace(*cp)) 449 cp++; 450 m = n; 451 n = *cp; 452 *cp = 0; 453 switch (m) { 454 case '/': 455 m = atoi(net); 456 break; 457 case '&': 458 if (inet_pton(AF_INET6, net, u) == 1) { 459 m = -1; 460 break; 461 } 462 /*FALLTHROUGH*/ 463 default: 464 m = sizeof(struct in6_addr) * CHAR_BIT; 465 break; 466 } 467 if (m >= 0) { 468 for (i = 0; i < sizeof(struct in6_addr); i++) { 469 if (m <= 0) { 470 *u = 0; 471 } else { 472 m -= CHAR_BIT; 473 *u = (u_char)~0; 474 if (m < 0) 475 *u <<= -m; 476 } 477 u++; 478 } 479 } 480 statp->sort_list[nsort].addr.s_addr = 481 (u_int32_t)0xffffffff; 482 statp->sort_list[nsort].mask = 483 (u_int32_t)0xffffffff; 484 nsort++; 485 } 486 *cp = n; 487 } 488 continue; 489 } 490#endif 491 if (MATCH(buf, "options")) { 492 res_setoptions(statp, buf + sizeof("options") - 1, "conf"); 493 continue; 494 } 495 } 496 if (nserv > 0) 497 statp->nscount = nserv; 498#ifdef RESOLVSORT 499 statp->nsort = nsort; 500#endif 501 (void) fclose(fp); 502 } 503/* 504 * Last chance to get a nameserver. This should not normally 505 * be necessary 506 */ 507#ifdef NO_RESOLV_CONF 508 if(nserv == 0) 509 nserv = get_nameservers(statp); 510#endif 511 512 if (statp->defdname[0] == 0 && 513 gethostname(buf, sizeof(statp->defdname) - 1) == 0 && 514 (cp = strchr(buf, '.')) != NULL) 515 strcpy(statp->defdname, cp + 1); 516 517 /* find components of local domain that might be searched */ 518 if (havesearch == 0) { 519 pp = statp->dnsrch; 520 *pp++ = statp->defdname; 521 *pp = NULL; 522 523 dots = 0; 524 for (cp = statp->defdname; *cp; cp++) 525 dots += (*cp == '.'); 526 527 cp = statp->defdname; 528 while (pp < statp->dnsrch + MAXDFLSRCH) { 529 if (dots < LOCALDOMAINPARTS) 530 break; 531 cp = strchr(cp, '.') + 1; /* we know there is one */ 532 *pp++ = cp; 533 dots--; 534 } 535 *pp = NULL; 536#ifdef DEBUG 537 if (statp->options & RES_DEBUG) { 538 printf(";; res_init()... default dnsrch list:\n"); 539 for (pp = statp->dnsrch; *pp; pp++) 540 printf(";;\t%s\n", *pp); 541 printf(";;\t..END..\n"); 542 } 543#endif 544 } 545 546 if (issetugid()) 547 statp->options |= RES_NOALIASES; 548 else if ((cp = getenv("RES_OPTIONS")) != NULL) 549 res_setoptions(statp, cp, "env"); 550 statp->options |= RES_INIT; 551 return (0); 552 553#ifdef SOLARIS2 554 freedata: 555 if (statp->_u._ext.ext != NULL) { 556 free(statp->_u._ext.ext); 557 statp->_u._ext.ext = NULL; 558 } 559 return (-1); 560#endif 561} 562 563static void 564res_setoptions(res_state statp, const char *options, const char *source) 565{ 566 const char *cp = options; 567 int i; 568#ifndef _LIBC 569 struct __res_state_ext *ext = statp->_u._ext.ext; 570#endif 571 572#ifdef DEBUG 573 if (statp->options & RES_DEBUG) 574 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 575 options, source); 576#endif 577 while (*cp) { 578 /* skip leading and inner runs of spaces */ 579 while (*cp == ' ' || *cp == '\t') 580 cp++; 581 /* search for and process individual options */ 582 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { 583 i = atoi(cp + sizeof("ndots:") - 1); 584 if (i <= RES_MAXNDOTS) 585 statp->ndots = i; 586 else 587 statp->ndots = RES_MAXNDOTS; 588#ifdef DEBUG 589 if (statp->options & RES_DEBUG) 590 printf(";;\tndots=%d\n", statp->ndots); 591#endif 592 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { 593 i = atoi(cp + sizeof("timeout:") - 1); 594 if (i <= RES_MAXRETRANS) 595 statp->retrans = i; 596 else 597 statp->retrans = RES_MAXRETRANS; 598#ifdef DEBUG 599 if (statp->options & RES_DEBUG) 600 printf(";;\ttimeout=%d\n", statp->retrans); 601#endif 602#ifdef SOLARIS2 603 } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { 604 /* 605 * For backward compatibility, 'retrans' is 606 * supported as an alias for 'timeout', though 607 * without an imposed maximum. 608 */ 609 statp->retrans = atoi(cp + sizeof("retrans:") - 1); 610 } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ 611 /* 612 * For backward compatibility, 'retry' is 613 * supported as an alias for 'attempts', though 614 * without an imposed maximum. 615 */ 616 statp->retry = atoi(cp + sizeof("retry:") - 1); 617#endif /* SOLARIS2 */ 618 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ 619 i = atoi(cp + sizeof("attempts:") - 1); 620 if (i <= RES_MAXRETRY) 621 statp->retry = i; 622 else 623 statp->retry = RES_MAXRETRY; 624#ifdef DEBUG 625 if (statp->options & RES_DEBUG) 626 printf(";;\tattempts=%d\n", statp->retry); 627#endif 628 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { 629#ifdef DEBUG 630 if (!(statp->options & RES_DEBUG)) { 631 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 632 options, source); 633 statp->options |= RES_DEBUG; 634 } 635 printf(";;\tdebug\n"); 636#endif 637 } else if (!strncmp(cp, "no_tld_query", 638 sizeof("no_tld_query") - 1) || 639 !strncmp(cp, "no-tld-query", 640 sizeof("no-tld-query") - 1)) { 641 statp->options |= RES_NOTLDQUERY; 642 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { 643 statp->options |= RES_USE_INET6; 644 } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { 645 statp->options |= RES_INSECURE1; 646 } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { 647 statp->options |= RES_INSECURE2; 648 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { 649 statp->options |= RES_ROTATE; 650 } else if (!strncmp(cp, "no-check-names", 651 sizeof("no-check-names") - 1)) { 652 statp->options |= RES_NOCHECKNAME; 653 } 654#ifdef RES_USE_EDNS0 655 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { 656 statp->options |= RES_USE_EDNS0; 657 } 658#endif 659#ifndef _LIBC 660 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { 661 statp->options |= RES_USE_DNAME; 662 } 663 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { 664 if (ext == NULL) 665 goto skip; 666 cp += sizeof("nibble:") - 1; 667 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); 668 strncpy(ext->nsuffix, cp, i); 669 ext->nsuffix[i] = '\0'; 670 } 671 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { 672 if (ext == NULL) 673 goto skip; 674 cp += sizeof("nibble2:") - 1; 675 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); 676 strncpy(ext->nsuffix2, cp, i); 677 ext->nsuffix2[i] = '\0'; 678 } 679 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { 680 cp += sizeof("v6revmode:") - 1; 681 /* "nibble" and "bitstring" used to be valid */ 682 if (!strncmp(cp, "single", sizeof("single") - 1)) { 683 statp->options |= RES_NO_NIBBLE2; 684 } else if (!strncmp(cp, "both", sizeof("both") - 1)) { 685 statp->options &= 686 ~RES_NO_NIBBLE2; 687 } 688 } 689#endif 690 else { 691 /* XXX - print a warning here? */ 692 } 693#ifndef _LIBC 694 skip: 695#endif 696 /* skip to next run of spaces */ 697 while (*cp && *cp != ' ' && *cp != '\t') 698 cp++; 699 } 700} 701 702#ifdef RESOLVSORT 703/* XXX - should really support CIDR which means explicit masks always. */ 704static u_int32_t 705net_mask(in) /* XXX - should really use system's version of this */ 706 struct in_addr in; 707{ 708 u_int32_t i = ntohl(in.s_addr); 709 710 if (IN_CLASSA(i)) 711 return (htonl(IN_CLASSA_NET)); 712 else if (IN_CLASSB(i)) 713 return (htonl(IN_CLASSB_NET)); 714 return (htonl(IN_CLASSC_NET)); 715} 716#endif 717 718u_int 719res_randomid(void) { 720 struct timeval now; 721 722 gettimeofday(&now, NULL); 723 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); 724} 725 726/* 727 * This routine is for closing the socket if a virtual circuit is used and 728 * the program wants to close it. This provides support for endhostent() 729 * which expects to close the socket. 730 * 731 * This routine is not expected to be user visible. 732 */ 733void 734res_nclose(res_state statp) { 735 int ns; 736 737 if (statp->_vcsock >= 0) { 738 (void) close(statp->_vcsock); 739 statp->_vcsock = -1; 740 statp->_flags &= ~(RES_F_VC | RES_F_CONN); 741 } 742 for (ns = 0; ns < statp->_u._ext.nscount; ns++) { 743 if (statp->_u._ext.nssocks[ns] != -1) { 744 (void) close(statp->_u._ext.nssocks[ns]); 745 statp->_u._ext.nssocks[ns] = -1; 746 } 747 } 748} 749 750void 751res_ndestroy(res_state statp) { 752 res_nclose(statp); 753 if (statp->_u._ext.ext != NULL) 754 free(statp->_u._ext.ext); 755 statp->options &= ~RES_INIT; 756 statp->_u._ext.ext = NULL; 757} 758 759#ifndef _LIBC 760const char * 761res_get_nibblesuffix(res_state statp) { 762 if (statp->_u._ext.ext) 763 return (statp->_u._ext.ext->nsuffix); 764 return ("ip6.arpa"); 765} 766 767const char * 768res_get_nibblesuffix2(res_state statp) { 769 if (statp->_u._ext.ext) 770 return (statp->_u._ext.ext->nsuffix2); 771 return ("ip6.int"); 772} 773#endif 774 775void 776res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { 777 int i, nserv; 778 size_t size; 779 780 /* close open servers */ 781 res_nclose(statp); 782 783 /* cause rtt times to be forgotten */ 784 statp->_u._ext.nscount = 0; 785 786 nserv = 0; 787 for (i = 0; i < cnt && nserv < MAXNS; i++) { 788 switch (set->sin.sin_family) { 789 case AF_INET: 790 size = sizeof(set->sin); 791 if (statp->_u._ext.ext) 792 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 793 &set->sin, size); 794 if (size <= sizeof(statp->nsaddr_list[nserv])) 795 memcpy(&statp->nsaddr_list[nserv], 796 &set->sin, size); 797 else 798 statp->nsaddr_list[nserv].sin_family = 0; 799 nserv++; 800 break; 801 802#ifdef HAS_INET6_STRUCTS 803 case AF_INET6: 804 size = sizeof(set->sin6); 805 if (statp->_u._ext.ext) 806 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 807 &set->sin6, size); 808 if (size <= sizeof(statp->nsaddr_list[nserv])) 809 memcpy(&statp->nsaddr_list[nserv], 810 &set->sin6, size); 811 else 812 statp->nsaddr_list[nserv].sin_family = 0; 813 nserv++; 814 break; 815#endif 816 817 default: 818 break; 819 } 820 set++; 821 } 822 statp->nscount = nserv; 823 824} 825 826int 827res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { 828 int i; 829 size_t size; 830 u_int16_t family; 831 832 for (i = 0; i < statp->nscount && i < cnt; i++) { 833 if (statp->_u._ext.ext) 834 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; 835 else 836 family = statp->nsaddr_list[i].sin_family; 837 838 switch (family) { 839 case AF_INET: 840 size = sizeof(set->sin); 841 if (statp->_u._ext.ext) 842 memcpy(&set->sin, 843 &statp->_u._ext.ext->nsaddrs[i], 844 size); 845 else 846 memcpy(&set->sin, &statp->nsaddr_list[i], 847 size); 848 break; 849 850#ifdef HAS_INET6_STRUCTS 851 case AF_INET6: 852 size = sizeof(set->sin6); 853 if (statp->_u._ext.ext) 854 memcpy(&set->sin6, 855 &statp->_u._ext.ext->nsaddrs[i], 856 size); 857 else 858 memcpy(&set->sin6, &statp->nsaddr_list[i], 859 size); 860 break; 861#endif 862 863 default: 864 set->sin.sin_family = 0; 865 break; 866 } 867 set++; 868 } 869 return (statp->nscount); 870} 871