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