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