1 /* 2 * ++Copyright++ 1985, 1988, 1993 3 * - 4 * Copyright (c) 1985, 1988, 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies, and that 40 * the name of Digital Equipment Corporation not be used in advertising or 41 * publicity pertaining to distribution of the document or software without 42 * specific, written prior permission. 43 * 44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51 * SOFTWARE. 52 * - 53 * --Copyright-- 54 */ 55 56 #if defined(LIBC_SCCS) && !defined(lint) 57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; 58 static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $"; 59 static char rcsid[] = "$FreeBSD$"; 60 #endif /* LIBC_SCCS and not lint */ 61 62 #include <sys/types.h> 63 #include <sys/param.h> 64 #include <sys/socket.h> 65 #include <netinet/in.h> 66 #include <arpa/inet.h> 67 #include <arpa/nameser.h> 68 69 #include <stdio.h> 70 #include <unistd.h> 71 #include <string.h> 72 #include <netdb.h> 73 #include <resolv.h> 74 #include <ctype.h> 75 #include <errno.h> 76 #include <syslog.h> 77 #include <stdarg.h> 78 #include <nsswitch.h> 79 80 #include "res_config.h" 81 82 #define SPRINTF(x) ((size_t)sprintf x) 83 84 #define MAXALIASES 35 85 #define MAXADDRS 35 86 87 static const char AskedForGot[] = 88 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\""; 89 90 static char *h_addr_ptrs[MAXADDRS + 1]; 91 92 static struct hostent host; 93 static char *host_aliases[MAXALIASES]; 94 static char hostbuf[8*1024]; 95 static u_char host_addr[16]; /* IPv4 or IPv6 */ 96 97 #ifdef RESOLVSORT 98 static void addrsort __P((char **, int)); 99 #endif 100 101 #if PACKETSZ > 1024 102 #define MAXPACKET PACKETSZ 103 #else 104 #define MAXPACKET 1024 105 #endif 106 107 typedef union { 108 HEADER hdr; 109 u_char buf[MAXPACKET]; 110 } querybuf; 111 112 typedef union { 113 int32_t al; 114 char ac; 115 } align; 116 117 extern int h_errno; 118 int _dns_ttl_; 119 120 #ifdef DEBUG 121 static void 122 dprintf(msg, num) 123 char *msg; 124 int num; 125 { 126 if (_res.options & RES_DEBUG) { 127 int save = errno; 128 129 printf(msg, num); 130 errno = save; 131 } 132 } 133 #else 134 # define dprintf(msg, num) /*nada*/ 135 #endif 136 137 #define BOUNDED_INCR(x) \ 138 do { \ 139 cp += x; \ 140 if (cp > eom) { \ 141 h_errno = NO_RECOVERY; \ 142 return (NULL); \ 143 } \ 144 } while (0) 145 146 #define BOUNDS_CHECK(ptr, count) \ 147 do { \ 148 if ((ptr) + (count) > eom) { \ 149 h_errno = NO_RECOVERY; \ 150 return (NULL); \ 151 } \ 152 } while (0) 153 154 static struct hostent * 155 gethostanswer(answer, anslen, qname, qtype) 156 const querybuf *answer; 157 int anslen; 158 const char *qname; 159 int qtype; 160 { 161 register const HEADER *hp; 162 register const u_char *cp; 163 register int n; 164 const u_char *eom, *erdata; 165 char *bp, **ap, **hap; 166 int type, class, buflen, ancount, qdcount; 167 int haveanswer, had_error; 168 int toobig = 0; 169 char tbuf[MAXDNAME]; 170 const char *tname; 171 int (*name_ok) __P((const char *)); 172 173 tname = qname; 174 host.h_name = NULL; 175 eom = answer->buf + anslen; 176 switch (qtype) { 177 case T_A: 178 case T_AAAA: 179 name_ok = res_hnok; 180 break; 181 case T_PTR: 182 name_ok = res_dnok; 183 break; 184 default: 185 h_errno = NO_RECOVERY; 186 return (NULL); /* XXX should be abort(); */ 187 } 188 /* 189 * find first satisfactory answer 190 */ 191 hp = &answer->hdr; 192 ancount = ntohs(hp->ancount); 193 qdcount = ntohs(hp->qdcount); 194 bp = hostbuf; 195 buflen = sizeof hostbuf; 196 cp = answer->buf; 197 BOUNDED_INCR(HFIXEDSZ); 198 if (qdcount != 1) { 199 h_errno = NO_RECOVERY; 200 return (NULL); 201 } 202 n = dn_expand(answer->buf, eom, cp, bp, buflen); 203 if ((n < 0) || !(*name_ok)(bp)) { 204 h_errno = NO_RECOVERY; 205 return (NULL); 206 } 207 BOUNDED_INCR(n + QFIXEDSZ); 208 if (qtype == T_A || qtype == T_AAAA) { 209 /* res_send() has already verified that the query name is the 210 * same as the one we sent; this just gets the expanded name 211 * (i.e., with the succeeding search-domain tacked on). 212 */ 213 n = strlen(bp) + 1; /* for the \0 */ 214 if (n >= MAXHOSTNAMELEN) { 215 h_errno = NO_RECOVERY; 216 return (NULL); 217 } 218 host.h_name = bp; 219 bp += n; 220 buflen -= n; 221 /* The qname can be abbreviated, but h_name is now absolute. */ 222 qname = host.h_name; 223 } 224 ap = host_aliases; 225 *ap = NULL; 226 host.h_aliases = host_aliases; 227 hap = h_addr_ptrs; 228 *hap = NULL; 229 host.h_addr_list = h_addr_ptrs; 230 haveanswer = 0; 231 had_error = 0; 232 _dns_ttl_ = -1; 233 while (ancount-- > 0 && cp < eom && !had_error) { 234 n = dn_expand(answer->buf, eom, cp, bp, buflen); 235 if ((n < 0) || !(*name_ok)(bp)) { 236 had_error++; 237 continue; 238 } 239 cp += n; /* name */ 240 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); 241 type = _getshort(cp); 242 cp += INT16SZ; /* type */ 243 class = _getshort(cp); 244 cp += INT16SZ; /* class */ 245 if (qtype == T_A && type == T_A) 246 _dns_ttl_ = _getlong(cp); 247 cp += INT32SZ; /* TTL */ 248 n = _getshort(cp); 249 cp += INT16SZ; /* len */ 250 BOUNDS_CHECK(cp, n); 251 erdata = cp + n; 252 if (class != C_IN) { 253 /* XXX - debug? syslog? */ 254 cp += n; 255 continue; /* XXX - had_error++ ? */ 256 } 257 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { 258 if (ap >= &host_aliases[MAXALIASES-1]) 259 continue; 260 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 261 if ((n < 0) || !(*name_ok)(tbuf)) { 262 had_error++; 263 continue; 264 } 265 cp += n; 266 if (cp != erdata) { 267 h_errno = NO_RECOVERY; 268 return (NULL); 269 } 270 /* Store alias. */ 271 *ap++ = bp; 272 n = strlen(bp) + 1; /* for the \0 */ 273 if (n >= MAXHOSTNAMELEN) { 274 had_error++; 275 continue; 276 } 277 bp += n; 278 buflen -= n; 279 /* Get canonical name. */ 280 n = strlen(tbuf) + 1; /* for the \0 */ 281 if (n > buflen || n >= MAXHOSTNAMELEN) { 282 had_error++; 283 continue; 284 } 285 strcpy(bp, tbuf); 286 host.h_name = bp; 287 bp += n; 288 buflen -= n; 289 continue; 290 } 291 if (qtype == T_PTR && type == T_CNAME) { 292 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 293 if (n < 0 || !res_dnok(tbuf)) { 294 had_error++; 295 continue; 296 } 297 cp += n; 298 if (cp != erdata) { 299 h_errno = NO_RECOVERY; 300 return (NULL); 301 } 302 /* Get canonical name. */ 303 n = strlen(tbuf) + 1; /* for the \0 */ 304 if (n > buflen || n >= MAXHOSTNAMELEN) { 305 had_error++; 306 continue; 307 } 308 strcpy(bp, tbuf); 309 tname = bp; 310 bp += n; 311 buflen -= n; 312 continue; 313 } 314 if (type != qtype) { 315 if (type != T_SIG) 316 syslog(LOG_NOTICE|LOG_AUTH, 317 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"", 318 qname, p_class(C_IN), p_type(qtype), 319 p_type(type)); 320 cp += n; 321 continue; /* XXX - had_error++ ? */ 322 } 323 switch (type) { 324 case T_PTR: 325 if (strcasecmp(tname, bp) != 0) { 326 syslog(LOG_NOTICE|LOG_AUTH, 327 AskedForGot, qname, bp); 328 cp += n; 329 continue; /* XXX - had_error++ ? */ 330 } 331 n = dn_expand(answer->buf, eom, cp, bp, buflen); 332 if ((n < 0) || !res_hnok(bp)) { 333 had_error++; 334 break; 335 } 336 #if MULTI_PTRS_ARE_ALIASES 337 cp += n; 338 if (cp != erdata) { 339 h_errno = NO_RECOVERY; 340 return (NULL); 341 } 342 if (!haveanswer) 343 host.h_name = bp; 344 else if (ap < &host_aliases[MAXALIASES-1]) 345 *ap++ = bp; 346 else 347 n = -1; 348 if (n != -1) { 349 n = strlen(bp) + 1; /* for the \0 */ 350 if (n >= MAXHOSTNAMELEN) { 351 had_error++; 352 break; 353 } 354 bp += n; 355 buflen -= n; 356 } 357 break; 358 #else 359 host.h_name = bp; 360 if (_res.options & RES_USE_INET6) { 361 n = strlen(bp) + 1; /* for the \0 */ 362 if (n >= MAXHOSTNAMELEN) { 363 had_error++; 364 break; 365 } 366 bp += n; 367 buflen -= n; 368 _map_v4v6_hostent(&host, &bp, &buflen); 369 } 370 h_errno = NETDB_SUCCESS; 371 return (&host); 372 #endif 373 case T_A: 374 case T_AAAA: 375 if (strcasecmp(host.h_name, bp) != 0) { 376 syslog(LOG_NOTICE|LOG_AUTH, 377 AskedForGot, host.h_name, bp); 378 cp += n; 379 continue; /* XXX - had_error++ ? */ 380 } 381 if (n != host.h_length) { 382 cp += n; 383 continue; 384 } 385 if (!haveanswer) { 386 register int nn; 387 388 host.h_name = bp; 389 nn = strlen(bp) + 1; /* for the \0 */ 390 bp += nn; 391 buflen -= nn; 392 } 393 394 bp += sizeof(align) - ((u_long)bp % sizeof(align)); 395 396 if (bp + n >= &hostbuf[sizeof hostbuf]) { 397 dprintf("size (%d) too big\n", n); 398 had_error++; 399 continue; 400 } 401 if (hap >= &h_addr_ptrs[MAXADDRS-1]) { 402 if (!toobig++) 403 dprintf("Too many addresses (%d)\n", 404 MAXADDRS); 405 cp += n; 406 continue; 407 } 408 bcopy(cp, *hap++ = bp, n); 409 bp += n; 410 buflen -= n; 411 cp += n; 412 if (cp != erdata) { 413 h_errno = NO_RECOVERY; 414 return (NULL); 415 } 416 break; 417 default: 418 dprintf("Impossible condition (type=%d)\n", type); 419 h_errno = NO_RECOVERY; 420 return (NULL); 421 /* BIND has abort() here, too risky on bad data */ 422 } 423 if (!had_error) 424 haveanswer++; 425 } 426 if (haveanswer) { 427 *ap = NULL; 428 *hap = NULL; 429 # if defined(RESOLVSORT) 430 /* 431 * Note: we sort even if host can take only one address 432 * in its return structures - should give it the "best" 433 * address in that case, not some random one 434 */ 435 if (_res.nsort && haveanswer > 1 && qtype == T_A) 436 addrsort(h_addr_ptrs, haveanswer); 437 # endif /*RESOLVSORT*/ 438 if (!host.h_name) { 439 n = strlen(qname) + 1; /* for the \0 */ 440 if (n > buflen || n >= MAXHOSTNAMELEN) 441 goto no_recovery; 442 strcpy(bp, qname); 443 host.h_name = bp; 444 bp += n; 445 buflen -= n; 446 } 447 if (_res.options & RES_USE_INET6) 448 _map_v4v6_hostent(&host, &bp, &buflen); 449 h_errno = NETDB_SUCCESS; 450 return (&host); 451 } 452 no_recovery: 453 h_errno = NO_RECOVERY; 454 return (NULL); 455 } 456 457 struct hostent * 458 __dns_getanswer(answer, anslen, qname, qtype) 459 const char *answer; 460 int anslen; 461 const char *qname; 462 int qtype; 463 { 464 switch(qtype) { 465 case T_AAAA: 466 host.h_addrtype = AF_INET6; 467 host.h_length = IN6ADDRSZ; 468 break; 469 case T_A: 470 default: 471 host.h_addrtype = AF_INET; 472 host.h_length = INADDRSZ; 473 break; 474 } 475 476 return(gethostanswer((const querybuf *)answer, anslen, qname, qtype)); 477 } 478 479 int 480 _dns_gethostbyname(void *rval, void *cb_data, va_list ap) 481 { 482 const char *name; 483 int af; 484 querybuf buf; 485 register const char *cp; 486 char *bp; 487 int n, size, type, len; 488 489 name = va_arg(ap, const char *); 490 af = va_arg(ap, int); 491 *(struct hostent **)rval = NULL; 492 493 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 494 h_errno = NETDB_INTERNAL; 495 return NS_UNAVAIL; 496 } 497 498 switch (af) { 499 case AF_INET: 500 size = INADDRSZ; 501 type = T_A; 502 break; 503 case AF_INET6: 504 size = IN6ADDRSZ; 505 type = T_AAAA; 506 break; 507 default: 508 h_errno = NETDB_INTERNAL; 509 errno = EAFNOSUPPORT; 510 return NS_UNAVAIL; 511 } 512 513 host.h_addrtype = af; 514 host.h_length = size; 515 516 /* 517 * if there aren't any dots, it could be a user-level alias. 518 * this is also done in res_query() since we are not the only 519 * function that looks up host names. 520 */ 521 if (!strchr(name, '.') && (cp = __hostalias(name))) 522 name = cp; 523 524 /* 525 * disallow names consisting only of digits/dots, unless 526 * they end in a dot. 527 */ 528 if (isdigit((unsigned char)name[0])) 529 for (cp = name;; ++cp) { 530 if (!*cp) { 531 if (*--cp == '.') 532 break; 533 /* 534 * All-numeric, no dot at the end. 535 * Fake up a hostent as if we'd actually 536 * done a lookup. 537 */ 538 if (inet_pton(af, name, host_addr) <= 0) { 539 h_errno = HOST_NOT_FOUND; 540 return NS_NOTFOUND; 541 } 542 strncpy(hostbuf, name, MAXDNAME); 543 hostbuf[MAXDNAME] = '\0'; 544 bp = hostbuf + MAXDNAME; 545 len = sizeof hostbuf - MAXDNAME; 546 host.h_name = hostbuf; 547 host.h_aliases = host_aliases; 548 host_aliases[0] = NULL; 549 h_addr_ptrs[0] = (char *)host_addr; 550 h_addr_ptrs[1] = NULL; 551 host.h_addr_list = h_addr_ptrs; 552 if (_res.options & RES_USE_INET6) 553 _map_v4v6_hostent(&host, &bp, &len); 554 h_errno = NETDB_SUCCESS; 555 *(struct hostent **)rval = &host; 556 return NS_SUCCESS; 557 } 558 if (!isdigit((unsigned char)*cp) && *cp != '.') 559 break; 560 } 561 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) || 562 name[0] == ':') 563 for (cp = name;; ++cp) { 564 if (!*cp) { 565 if (*--cp == '.') 566 break; 567 /* 568 * All-IPv6-legal, no dot at the end. 569 * Fake up a hostent as if we'd actually 570 * done a lookup. 571 */ 572 if (inet_pton(af, name, host_addr) <= 0) { 573 h_errno = HOST_NOT_FOUND; 574 return NS_NOTFOUND; 575 } 576 strncpy(hostbuf, name, MAXDNAME); 577 hostbuf[MAXDNAME] = '\0'; 578 bp = hostbuf + MAXDNAME; 579 len = sizeof hostbuf - MAXDNAME; 580 host.h_name = hostbuf; 581 host.h_aliases = host_aliases; 582 host_aliases[0] = NULL; 583 h_addr_ptrs[0] = (char *)host_addr; 584 h_addr_ptrs[1] = NULL; 585 host.h_addr_list = h_addr_ptrs; 586 h_errno = NETDB_SUCCESS; 587 *(struct hostent **)rval = &host; 588 return NS_SUCCESS; 589 } 590 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.') 591 break; 592 } 593 594 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) { 595 dprintf("res_search failed (%d)\n", n); 596 return NS_UNAVAIL; 597 } 598 *(struct hostent **)rval = gethostanswer(&buf, n, name, type); 599 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 600 } 601 602 int 603 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap) 604 { 605 const char *addr; /* XXX should have been def'd as u_char! */ 606 int len, af; 607 const u_char *uaddr; 608 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; 609 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; 610 int n, size; 611 querybuf buf; 612 register struct hostent *hp; 613 char qbuf[MAXDNAME+1], *qp; 614 #ifdef SUNSECURITY 615 register struct hostent *rhp; 616 char **haddr; 617 u_long old_options; 618 char hname2[MAXDNAME+1]; 619 #endif /*SUNSECURITY*/ 620 621 addr = va_arg(ap, const char *); 622 uaddr = (const u_char *)addr; 623 len = va_arg(ap, int); 624 af = va_arg(ap, int); 625 626 *(struct hostent **)rval = NULL; 627 628 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 629 h_errno = NETDB_INTERNAL; 630 return NS_UNAVAIL; 631 } 632 if (af == AF_INET6 && len == IN6ADDRSZ && 633 (!bcmp(uaddr, mapped, sizeof mapped) || 634 !bcmp(uaddr, tunnelled, sizeof tunnelled))) { 635 /* Unmap. */ 636 addr += sizeof mapped; 637 uaddr += sizeof mapped; 638 af = AF_INET; 639 len = INADDRSZ; 640 } 641 switch (af) { 642 case AF_INET: 643 size = INADDRSZ; 644 break; 645 case AF_INET6: 646 size = IN6ADDRSZ; 647 break; 648 default: 649 errno = EAFNOSUPPORT; 650 h_errno = NETDB_INTERNAL; 651 return NS_UNAVAIL; 652 } 653 if (size != len) { 654 errno = EINVAL; 655 h_errno = NETDB_INTERNAL; 656 return NS_UNAVAIL; 657 } 658 switch (af) { 659 case AF_INET: 660 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", 661 (uaddr[3] & 0xff), 662 (uaddr[2] & 0xff), 663 (uaddr[1] & 0xff), 664 (uaddr[0] & 0xff)); 665 break; 666 case AF_INET6: 667 qp = qbuf; 668 for (n = IN6ADDRSZ - 1; n >= 0; n--) { 669 qp += SPRINTF((qp, "%x.%x.", 670 uaddr[n] & 0xf, 671 (uaddr[n] >> 4) & 0xf)); 672 } 673 strcpy(qp, "ip6.int"); 674 break; 675 default: 676 abort(); 677 } 678 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf); 679 if (n < 0) { 680 dprintf("res_query failed (%d)\n", n); 681 return NS_UNAVAIL; 682 } 683 if (n > sizeof buf.buf) { 684 dprintf("static buffer is too small (%d)\n", n); 685 return NS_UNAVAIL; 686 } 687 if (!(hp = gethostanswer(&buf, n, qbuf, T_PTR))) 688 return NS_NOTFOUND; /* h_errno was set by gethostanswer() */ 689 #ifdef SUNSECURITY 690 if (af == AF_INET) { 691 /* 692 * turn off search as the name should be absolute, 693 * 'localhost' should be matched by defnames 694 */ 695 strncpy(hname2, hp->h_name, MAXDNAME); 696 hname2[MAXDNAME] = '\0'; 697 old_options = _res.options; 698 _res.options &= ~RES_DNSRCH; 699 _res.options |= RES_DEFNAMES; 700 if (!(rhp = gethostbyname(hname2))) { 701 syslog(LOG_NOTICE|LOG_AUTH, 702 "gethostbyaddr: No A record for %s (verifying [%s])", 703 hname2, inet_ntoa(*((struct in_addr *)addr))); 704 _res.options = old_options; 705 h_errno = HOST_NOT_FOUND; 706 return NS_NOTFOUND; 707 } 708 _res.options = old_options; 709 for (haddr = rhp->h_addr_list; *haddr; haddr++) 710 if (!memcmp(*haddr, addr, INADDRSZ)) 711 break; 712 if (!*haddr) { 713 syslog(LOG_NOTICE|LOG_AUTH, 714 "gethostbyaddr: A record of %s != PTR record [%s]", 715 hname2, inet_ntoa(*((struct in_addr *)addr))); 716 h_errno = HOST_NOT_FOUND; 717 return NS_NOTFOUND; 718 } 719 } 720 #endif /*SUNSECURITY*/ 721 hp->h_addrtype = af; 722 hp->h_length = len; 723 bcopy(addr, host_addr, len); 724 h_addr_ptrs[0] = (char *)host_addr; 725 h_addr_ptrs[1] = NULL; 726 if (af == AF_INET && (_res.options & RES_USE_INET6)) { 727 _map_v4v6_address((char*)host_addr, (char*)host_addr); 728 hp->h_addrtype = AF_INET6; 729 hp->h_length = IN6ADDRSZ; 730 } 731 h_errno = NETDB_SUCCESS; 732 *(struct hostent **)rval = hp; 733 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 734 } 735 736 #ifdef RESOLVSORT 737 static void 738 addrsort(ap, num) 739 char **ap; 740 int num; 741 { 742 int i, j; 743 char **p; 744 short aval[MAXADDRS]; 745 int needsort = 0; 746 747 p = ap; 748 for (i = 0; i < num; i++, p++) { 749 for (j = 0 ; (unsigned)j < _res.nsort; j++) 750 if (_res.sort_list[j].addr.s_addr == 751 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) 752 break; 753 aval[i] = j; 754 if (needsort == 0 && i > 0 && j < aval[i-1]) 755 needsort = i; 756 } 757 if (!needsort) 758 return; 759 760 while (needsort < num) { 761 for (j = needsort - 1; j >= 0; j--) { 762 if (aval[j] > aval[j+1]) { 763 char *hp; 764 765 i = aval[j]; 766 aval[j] = aval[j+1]; 767 aval[j+1] = i; 768 769 hp = ap[j]; 770 ap[j] = ap[j+1]; 771 ap[j+1] = hp; 772 773 } else 774 break; 775 } 776 needsort++; 777 } 778 } 779 #endif 780 void 781 _sethostdnsent(stayopen) 782 int stayopen; 783 { 784 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 785 return; 786 if (stayopen) 787 _res.options |= RES_STAYOPEN | RES_USEVC; 788 } 789 790 void 791 _endhostdnsent() 792 { 793 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 794 res_close(); 795 } 796