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