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