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