1 /* $NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 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 <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.9.2.5.4.2 2004/03/16 12:34:18 marka Exp"; 78 #else 79 __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos 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 static void res_setoptions __P((res_state, const char *, const char *)); 129 130 static const char sort_mask[] = "/&"; 131 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) 132 static u_int32_t net_mask __P((struct in_addr)); 133 134 #if !defined(isascii) /* XXX - could be a function */ 135 # define isascii(c) (!(c & 0200)) 136 #endif 137 138 /* 139 * Resolver state default settings. 140 */ 141 142 /* 143 * Set up default settings. If the configuration file exist, the values 144 * there will have precedence. Otherwise, the server address is set to 145 * INADDR_ANY and the default domain name comes from the gethostname(). 146 * 147 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 148 * rather than INADDR_ANY ("0.0.0.0") as the default name server address 149 * since it was noted that INADDR_ANY actually meant ``the first interface 150 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, 151 * it had to be "up" in order for you to reach your own name server. It 152 * was later decided that since the recommended practice is to always 153 * install local static routes through 127.0.0.1 for all your network 154 * interfaces, that we could solve this problem without a code change. 155 * 156 * The configuration file should always be used, since it is the only way 157 * to specify a default domain. If you are running a server on your local 158 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" 159 * in the configuration file. 160 * 161 * Return 0 if completes successfully, -1 on error 162 */ 163 int 164 res_ninit(res_state statp) { 165 extern int __res_vinit(res_state, int); 166 167 return (__res_vinit(statp, 0)); 168 } 169 170 /* This function has to be reachable by res_data.c but not publically. */ 171 int 172 __res_vinit(res_state statp, int preinit) { 173 register FILE *fp; 174 register char *cp, **pp; 175 register int n; 176 char buf[BUFSIZ]; 177 int nserv = 0; /* number of nameserver records read from file */ 178 int haveenv = 0; 179 int havesearch = 0; 180 int nsort = 0; 181 char *net; 182 int dots; 183 union res_sockaddr_union u[2]; 184 185 if (!preinit) { 186 statp->retrans = RES_TIMEOUT; 187 statp->retry = RES_DFLRETRY; 188 statp->options = RES_DEFAULT; 189 statp->id = res_randomid(); 190 } 191 192 if ((statp->options & RES_INIT) != 0U) 193 res_ndestroy(statp); 194 195 memset(u, 0, sizeof(u)); 196 #ifdef USELOOPBACK 197 u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 198 #else 199 u[nserv].sin.sin_addr.s_addr = INADDR_ANY; 200 #endif 201 u[nserv].sin.sin_family = AF_INET; 202 u[nserv].sin.sin_port = htons(NAMESERVER_PORT); 203 #ifdef HAVE_SA_LEN 204 u[nserv].sin.sin_len = sizeof(struct sockaddr_in); 205 #endif 206 nserv++; 207 #ifdef HAS_INET6_STRUCTS 208 #ifdef USELOOPBACK 209 u[nserv].sin6.sin6_addr = in6addr_loopback; 210 #else 211 u[nserv].sin6.sin6_addr = in6addr_any; 212 #endif 213 u[nserv].sin6.sin6_family = AF_INET6; 214 u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); 215 #ifdef HAVE_SA_LEN 216 u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); 217 #endif 218 nserv++; 219 #endif 220 statp->nscount = 0; 221 statp->ndots = 1; 222 statp->pfcode = 0; 223 statp->_vcsock = -1; 224 statp->_flags = 0; 225 statp->qhook = NULL; 226 statp->rhook = NULL; 227 statp->_u._ext.nscount = 0; 228 statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); 229 if (statp->_u._ext.ext != NULL) { 230 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); 231 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; 232 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); 233 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); 234 } 235 statp->nsort = 0; 236 res_setservers(statp, u, nserv); 237 238 /* Allow user to override the local domain definition */ 239 if ((cp = getenv("LOCALDOMAIN")) != NULL) { 240 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 241 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 242 haveenv++; 243 244 /* 245 * Set search list to be blank-separated strings 246 * from rest of env value. Permits users of LOCALDOMAIN 247 * to still have a search list, and anyone to set the 248 * one that they want to use as an individual (even more 249 * important now that the rfc1535 stuff restricts searches) 250 */ 251 cp = statp->defdname; 252 pp = statp->dnsrch; 253 *pp++ = cp; 254 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 255 if (*cp == '\n') /* silly backwards compat */ 256 break; 257 else if (*cp == ' ' || *cp == '\t') { 258 *cp = 0; 259 n = 1; 260 } else if (n) { 261 *pp++ = cp; 262 n = 0; 263 havesearch = 1; 264 } 265 } 266 /* null terminate last domain if there are excess */ 267 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') 268 cp++; 269 *cp = '\0'; 270 *pp++ = 0; 271 } 272 273 #define MATCH(line, name) \ 274 (!strncmp(line, name, sizeof(name) - 1) && \ 275 (line[sizeof(name) - 1] == ' ' || \ 276 line[sizeof(name) - 1] == '\t')) 277 278 nserv = 0; 279 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 280 /* read the config file */ 281 while (fgets(buf, sizeof(buf), fp) != NULL) { 282 /* skip comments */ 283 if (*buf == ';' || *buf == '#') 284 continue; 285 /* read default domain name */ 286 if (MATCH(buf, "domain")) { 287 if (haveenv) /* skip if have from environ */ 288 continue; 289 cp = buf + sizeof("domain") - 1; 290 while (*cp == ' ' || *cp == '\t') 291 cp++; 292 if ((*cp == '\0') || (*cp == '\n')) 293 continue; 294 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 295 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 296 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) 297 *cp = '\0'; 298 havesearch = 0; 299 continue; 300 } 301 /* set search list */ 302 if (MATCH(buf, "search")) { 303 if (haveenv) /* skip if have from environ */ 304 continue; 305 cp = buf + sizeof("search") - 1; 306 while (*cp == ' ' || *cp == '\t') 307 cp++; 308 if ((*cp == '\0') || (*cp == '\n')) 309 continue; 310 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 311 statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 312 if ((cp = strchr(statp->defdname, '\n')) != NULL) 313 *cp = '\0'; 314 /* 315 * Set search list to be blank-separated strings 316 * on rest of line. 317 */ 318 cp = statp->defdname; 319 pp = statp->dnsrch; 320 *pp++ = cp; 321 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 322 if (*cp == ' ' || *cp == '\t') { 323 *cp = 0; 324 n = 1; 325 } else if (n) { 326 *pp++ = cp; 327 n = 0; 328 } 329 } 330 /* null terminate last domain if there are excess */ 331 while (*cp != '\0' && *cp != ' ' && *cp != '\t') 332 cp++; 333 *cp = '\0'; 334 *pp++ = 0; 335 havesearch = 1; 336 continue; 337 } 338 /* read nameservers to query */ 339 if (MATCH(buf, "nameserver") && nserv < MAXNS) { 340 struct addrinfo hints, *ai; 341 char sbuf[NI_MAXSERV]; 342 const size_t minsiz = 343 sizeof(statp->_u._ext.ext->nsaddrs[0]); 344 345 cp = buf + sizeof("nameserver") - 1; 346 while (*cp == ' ' || *cp == '\t') 347 cp++; 348 cp[strcspn(cp, ";# \t\n")] = '\0'; 349 if ((*cp != '\0') && (*cp != '\n')) { 350 memset(&hints, 0, sizeof(hints)); 351 hints.ai_family = PF_UNSPEC; 352 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 353 hints.ai_flags = AI_NUMERICHOST; 354 sprintf(sbuf, "%u", NAMESERVER_PORT); 355 if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && 356 ai->ai_addrlen <= minsiz) { 357 if (statp->_u._ext.ext != NULL) { 358 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 359 ai->ai_addr, ai->ai_addrlen); 360 } 361 if (ai->ai_addrlen <= 362 sizeof(statp->nsaddr_list[nserv])) { 363 memcpy(&statp->nsaddr_list[nserv], 364 ai->ai_addr, ai->ai_addrlen); 365 } else 366 statp->nsaddr_list[nserv].sin_family = 0; 367 freeaddrinfo(ai); 368 nserv++; 369 } 370 } 371 continue; 372 } 373 if (MATCH(buf, "sortlist")) { 374 struct in_addr a; 375 376 cp = buf + sizeof("sortlist") - 1; 377 while (nsort < MAXRESOLVSORT) { 378 while (*cp == ' ' || *cp == '\t') 379 cp++; 380 if (*cp == '\0' || *cp == '\n' || *cp == ';') 381 break; 382 net = cp; 383 while (*cp && !ISSORTMASK(*cp) && *cp != ';' && 384 isascii(*cp) && !isspace((unsigned char)*cp)) 385 cp++; 386 n = *cp; 387 *cp = 0; 388 if (inet_aton(net, &a)) { 389 statp->sort_list[nsort].addr = a; 390 if (ISSORTMASK(n)) { 391 *cp++ = n; 392 net = cp; 393 while (*cp && *cp != ';' && 394 isascii(*cp) && 395 !isspace((unsigned char)*cp)) 396 cp++; 397 n = *cp; 398 *cp = 0; 399 if (inet_aton(net, &a)) { 400 statp->sort_list[nsort].mask = a.s_addr; 401 } else { 402 statp->sort_list[nsort].mask = 403 net_mask(statp->sort_list[nsort].addr); 404 } 405 } else { 406 statp->sort_list[nsort].mask = 407 net_mask(statp->sort_list[nsort].addr); 408 } 409 nsort++; 410 } 411 *cp = n; 412 } 413 continue; 414 } 415 if (MATCH(buf, "options")) { 416 res_setoptions(statp, buf + sizeof("options") - 1, "conf"); 417 continue; 418 } 419 } 420 if (nserv > 0) 421 statp->nscount = nserv; 422 statp->nsort = nsort; 423 (void) fclose(fp); 424 } 425 /* 426 * Last chance to get a nameserver. This should not normally 427 * be necessary 428 */ 429 #ifdef NO_RESOLV_CONF 430 if(nserv == 0) 431 nserv = get_nameservers(statp); 432 #endif 433 434 if (statp->defdname[0] == 0 && 435 gethostname(buf, sizeof(statp->defdname) - 1) == 0 && 436 (cp = strchr(buf, '.')) != NULL) 437 strcpy(statp->defdname, cp + 1); 438 439 /* find components of local domain that might be searched */ 440 if (havesearch == 0) { 441 pp = statp->dnsrch; 442 *pp++ = statp->defdname; 443 *pp = NULL; 444 445 dots = 0; 446 for (cp = statp->defdname; *cp; cp++) 447 dots += (*cp == '.'); 448 449 cp = statp->defdname; 450 while (pp < statp->dnsrch + MAXDFLSRCH) { 451 if (dots < LOCALDOMAINPARTS) 452 break; 453 cp = strchr(cp, '.') + 1; /* we know there is one */ 454 *pp++ = cp; 455 dots--; 456 } 457 *pp = NULL; 458 #ifdef DEBUG 459 if (statp->options & RES_DEBUG) { 460 printf(";; res_init()... default dnsrch list:\n"); 461 for (pp = statp->dnsrch; *pp; pp++) 462 printf(";;\t%s\n", *pp); 463 printf(";;\t..END..\n"); 464 } 465 #endif 466 } 467 468 if ((cp = getenv("RES_OPTIONS")) != NULL) 469 res_setoptions(statp, cp, "env"); 470 statp->options |= RES_INIT; 471 return (0); 472 } 473 474 static void 475 res_setoptions(res_state statp, const char *options, const char *source) 476 { 477 const char *cp = options; 478 int i; 479 struct __res_state_ext *ext = statp->_u._ext.ext; 480 481 #ifdef DEBUG 482 if (statp->options & RES_DEBUG) 483 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 484 options, source); 485 #endif 486 while (*cp) { 487 /* skip leading and inner runs of spaces */ 488 while (*cp == ' ' || *cp == '\t') 489 cp++; 490 /* search for and process individual options */ 491 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { 492 i = atoi(cp + sizeof("ndots:") - 1); 493 if (i <= RES_MAXNDOTS) 494 statp->ndots = i; 495 else 496 statp->ndots = RES_MAXNDOTS; 497 #ifdef DEBUG 498 if (statp->options & RES_DEBUG) 499 printf(";;\tndots=%d\n", statp->ndots); 500 #endif 501 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { 502 i = atoi(cp + sizeof("timeout:") - 1); 503 if (i <= RES_MAXRETRANS) 504 statp->retrans = i; 505 else 506 statp->retrans = RES_MAXRETRANS; 507 #ifdef DEBUG 508 if (statp->options & RES_DEBUG) 509 printf(";;\ttimeout=%d\n", statp->retrans); 510 #endif 511 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ 512 i = atoi(cp + sizeof("attempts:") - 1); 513 if (i <= RES_MAXRETRY) 514 statp->retry = i; 515 else 516 statp->retry = RES_MAXRETRY; 517 #ifdef DEBUG 518 if (statp->options & RES_DEBUG) 519 printf(";;\tattempts=%d\n", statp->retry); 520 #endif 521 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { 522 #ifdef DEBUG 523 if (!(statp->options & RES_DEBUG)) { 524 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 525 options, source); 526 statp->options |= RES_DEBUG; 527 } 528 printf(";;\tdebug\n"); 529 #endif 530 } else if (!strncmp(cp, "no_tld_query", 531 sizeof("no_tld_query") - 1) || 532 !strncmp(cp, "no-tld-query", 533 sizeof("no-tld-query") - 1)) { 534 statp->options |= RES_NOTLDQUERY; 535 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { 536 statp->options |= RES_USE_INET6; 537 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { 538 statp->options |= RES_ROTATE; 539 } else if (!strncmp(cp, "no-check-names", 540 sizeof("no-check-names") - 1)) { 541 statp->options |= RES_NOCHECKNAME; 542 } 543 #ifdef RES_USE_EDNS0 544 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { 545 statp->options |= RES_USE_EDNS0; 546 } 547 #endif 548 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { 549 statp->options |= RES_USE_DNAME; 550 } 551 else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { 552 if (ext == NULL) 553 goto skip; 554 cp += sizeof("nibble:") - 1; 555 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); 556 strncpy(ext->nsuffix, cp, (size_t)i); 557 ext->nsuffix[i] = '\0'; 558 } 559 else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { 560 if (ext == NULL) 561 goto skip; 562 cp += sizeof("nibble2:") - 1; 563 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); 564 strncpy(ext->nsuffix2, cp, (size_t)i); 565 ext->nsuffix2[i] = '\0'; 566 } 567 else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { 568 cp += sizeof("v6revmode:") - 1; 569 /* "nibble" and "bitstring" used to be valid */ 570 if (!strncmp(cp, "single", sizeof("single") - 1)) { 571 statp->options |= RES_NO_NIBBLE2; 572 } else if (!strncmp(cp, "both", sizeof("both") - 1)) { 573 statp->options &= 574 ~RES_NO_NIBBLE2; 575 } 576 } 577 else { 578 /* XXX - print a warning here? */ 579 } 580 skip: 581 /* skip to next run of spaces */ 582 while (*cp && *cp != ' ' && *cp != '\t') 583 cp++; 584 } 585 } 586 587 /* XXX - should really support CIDR which means explicit masks always. */ 588 static u_int32_t 589 net_mask(in) /* XXX - should really use system's version of this */ 590 struct in_addr in; 591 { 592 register u_int32_t i = ntohl(in.s_addr); 593 594 if (IN_CLASSA(i)) 595 return (htonl(IN_CLASSA_NET)); 596 else if (IN_CLASSB(i)) 597 return (htonl(IN_CLASSB_NET)); 598 return (htonl(IN_CLASSC_NET)); 599 } 600 601 u_int 602 res_randomid(void) { 603 struct timeval now; 604 605 gettimeofday(&now, NULL); 606 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); 607 } 608 609 /* 610 * This routine is for closing the socket if a virtual circuit is used and 611 * the program wants to close it. This provides support for endhostent() 612 * which expects to close the socket. 613 * 614 * This routine is not expected to be user visible. 615 */ 616 void 617 res_nclose(res_state statp) { 618 int ns; 619 620 if (statp->_vcsock >= 0) { 621 (void) close(statp->_vcsock); 622 statp->_vcsock = -1; 623 statp->_flags &= ~(RES_F_VC | RES_F_CONN); 624 } 625 for (ns = 0; ns < statp->_u._ext.nscount; ns++) { 626 if (statp->_u._ext.nssocks[ns] != -1) { 627 (void) close(statp->_u._ext.nssocks[ns]); 628 statp->_u._ext.nssocks[ns] = -1; 629 } 630 } 631 } 632 633 void 634 res_ndestroy(res_state statp) { 635 res_nclose(statp); 636 if (statp->_u._ext.ext != NULL) 637 free(statp->_u._ext.ext); 638 statp->options &= ~RES_INIT; 639 statp->_u._ext.ext = NULL; 640 } 641 642 const char * 643 res_get_nibblesuffix(res_state statp) { 644 if (statp->_u._ext.ext) 645 return (statp->_u._ext.ext->nsuffix); 646 return ("ip6.arpa"); 647 } 648 649 const char * 650 res_get_nibblesuffix2(res_state statp) { 651 if (statp->_u._ext.ext) 652 return (statp->_u._ext.ext->nsuffix2); 653 return ("ip6.int"); 654 } 655 656 void 657 res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { 658 int i, nserv; 659 size_t size; 660 661 /* close open servers */ 662 res_nclose(statp); 663 664 /* cause rtt times to be forgotten */ 665 statp->_u._ext.nscount = 0; 666 667 nserv = 0; 668 for (i = 0; i < cnt && nserv < MAXNS; i++) { 669 switch (set->sin.sin_family) { 670 case AF_INET: 671 size = sizeof(set->sin); 672 if (statp->_u._ext.ext) 673 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 674 &set->sin, size); 675 if (size <= sizeof(statp->nsaddr_list[nserv])) 676 memcpy(&statp->nsaddr_list[nserv], 677 &set->sin, size); 678 #ifdef notdef 679 else 680 statp->nsaddr_list[nserv].sin_family = 0; 681 #endif 682 nserv++; 683 break; 684 685 #ifdef HAS_INET6_STRUCTS 686 case AF_INET6: 687 size = sizeof(set->sin6); 688 if (statp->_u._ext.ext) 689 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 690 &set->sin6, size); 691 if (size <= sizeof(statp->nsaddr_list[nserv])) 692 memcpy(&statp->nsaddr_list[nserv], 693 &set->sin6, size); 694 else 695 statp->nsaddr_list[nserv].sin_family = 0; 696 nserv++; 697 break; 698 #endif 699 700 default: 701 break; 702 } 703 set++; 704 } 705 statp->nscount = nserv; 706 707 } 708 709 int 710 res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { 711 int i; 712 size_t size; 713 u_int16_t family; 714 715 for (i = 0; i < statp->nscount && i < cnt; i++) { 716 if (statp->_u._ext.ext) 717 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; 718 else 719 family = statp->nsaddr_list[i].sin_family; 720 721 switch (family) { 722 case AF_INET: 723 size = sizeof(set->sin); 724 if (statp->_u._ext.ext) 725 memcpy(&set->sin, 726 &statp->_u._ext.ext->nsaddrs[i], 727 size); 728 else 729 memcpy(&set->sin, &statp->nsaddr_list[i], 730 size); 731 break; 732 733 #ifdef HAS_INET6_STRUCTS 734 case AF_INET6: 735 size = sizeof(set->sin6); 736 if (statp->_u._ext.ext) 737 memcpy(&set->sin6, 738 &statp->_u._ext.ext->nsaddrs[i], 739 size); 740 else 741 memcpy(&set->sin6, &statp->nsaddr_list[i], 742 size); 743 break; 744 #endif 745 746 default: 747 set->sin.sin_family = 0; 748 break; 749 } 750 set++; 751 } 752 return (statp->nscount); 753 } 754