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