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