1 /* $NetBSD: gethnamaddr.c,v 1.61 2004/06/04 20:05:25 ginsbach Exp $ */ 2 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 #include <sys/cdefs.h> 55 #if defined(LIBC_SCCS) && !defined(lint) 56 #if 0 57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; 58 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp "; 59 #else 60 __RCSID("$NetBSD: gethnamaddr.c,v 1.61 2004/06/04 20:05:25 ginsbach Exp $"); 61 #endif 62 #endif /* LIBC_SCCS and not lint */ 63 64 #if defined(_LIBC) 65 #include "namespace.h" 66 #endif 67 #include <sys/param.h> 68 #include <sys/socket.h> 69 #include <netinet/in.h> 70 #include <arpa/inet.h> 71 #include <arpa/nameser.h> 72 73 #include <assert.h> 74 #include <ctype.h> 75 #include <errno.h> 76 #include <netdb.h> 77 #include <resolv.h> 78 #include <stdarg.h> 79 #include <stdio.h> 80 #include <syslog.h> 81 82 #ifndef LOG_AUTH 83 # define LOG_AUTH 0 84 #endif 85 86 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ 87 88 #include <nsswitch.h> 89 #include <stdlib.h> 90 #include <string.h> 91 92 #ifdef YP 93 #include <rpc/rpc.h> 94 #include <rpcsvc/yp_prot.h> 95 #include <rpcsvc/ypclnt.h> 96 #endif 97 98 #if defined(_LIBC) && defined(__weak_alias) 99 __weak_alias(gethostbyaddr,_gethostbyaddr) 100 __weak_alias(gethostbyname,_gethostbyname) 101 #endif 102 103 #define MAXALIASES 35 104 #define MAXADDRS 35 105 106 static const char AskedForGot[] = 107 "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 108 109 static char *h_addr_ptrs[MAXADDRS + 1]; 110 111 #ifdef YP 112 static char *__ypdomain; 113 #endif 114 115 static struct hostent host; 116 static char *host_aliases[MAXALIASES]; 117 static char hostbuf[8*1024]; 118 static u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */ 119 static FILE *hostf = NULL; 120 static int stayopen = 0; 121 122 #define MAXPACKET (64*1024) 123 124 typedef union { 125 HEADER hdr; 126 u_char buf[MAXPACKET]; 127 } querybuf; 128 129 typedef union { 130 int32_t al; 131 char ac; 132 } align; 133 134 #ifdef DEBUG 135 static void dprintf(char *, res_state *, ...) 136 __attribute__((__format__(__printf__, 1, 3))); 137 #endif 138 static struct hostent *getanswer(const querybuf *, int, const char *, int, 139 res_state); 140 static void map_v4v6_address(const char *, char *); 141 static void map_v4v6_hostent(struct hostent *, char **, char *); 142 #ifdef RESOLVSORT 143 static void addrsort(char **, int, res_state *); 144 #endif 145 146 void _sethtent(int); 147 void _endhtent(void); 148 struct hostent *_gethtent(void); 149 void ht_sethostent(int); 150 void ht_endhostent(void); 151 struct hostent *ht_gethostbyname(char *); 152 struct hostent *ht_gethostbyaddr(const char *, int, int); 153 void dns_service(void); 154 #undef dn_skipname 155 int dn_skipname(const u_char *, const u_char *); 156 int _gethtbyaddr(void *, void *, va_list); 157 int _gethtbyname(void *, void *, va_list); 158 struct hostent *_gethtbyname2(const char *, int); 159 int _dns_gethtbyaddr(void *, void *, va_list); 160 int _dns_gethtbyname(void *, void *, va_list); 161 #ifdef YP 162 struct hostent *_yphostent(char *, int); 163 int _yp_gethtbyaddr(void *, void *, va_list); 164 int _yp_gethtbyname(void *, void *, va_list); 165 #endif 166 167 static struct hostent *gethostbyname_internal(const char *, int, res_state); 168 169 static const ns_src default_dns_files[] = { 170 { NSSRC_FILES, NS_SUCCESS }, 171 { NSSRC_DNS, NS_SUCCESS }, 172 { 0 } 173 }; 174 175 176 #ifdef DEBUG 177 static void 178 dprintf(char *msg, res_state res, ...) 179 { 180 _DIAGASSERT(msg != NULL); 181 182 if (res->options & RES_DEBUG) { 183 int save = errno; 184 va_list ap; 185 186 va_start (ap, msg); 187 vprintf(msg, ap); 188 va_end (ap); 189 190 errno = save; 191 } 192 } 193 #else 194 # define dprintf(msg, res, num) /*nada*/ 195 #endif 196 197 #define BOUNDED_INCR(x) \ 198 do { \ 199 cp += (x); \ 200 if (cp > eom) { \ 201 h_errno = NO_RECOVERY; \ 202 return NULL; \ 203 } \ 204 } while (/*CONSTCOND*/0) 205 206 #define BOUNDS_CHECK(ptr, count) \ 207 do { \ 208 if ((ptr) + (count) > eom) { \ 209 h_errno = NO_RECOVERY; \ 210 return NULL; \ 211 } \ 212 } while (/*CONSTCOND*/0) 213 214 static struct hostent * 215 getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, 216 res_state res) 217 { 218 const HEADER *hp; 219 const u_char *cp; 220 int n; 221 const u_char *eom, *erdata; 222 char *bp, **ap, **hap, *ep; 223 int type, class, ancount, qdcount; 224 int haveanswer, had_error; 225 int toobig = 0; 226 char tbuf[MAXDNAME]; 227 const char *tname; 228 int (*name_ok)(const char *); 229 230 _DIAGASSERT(answer != NULL); 231 _DIAGASSERT(qname != NULL); 232 233 tname = qname; 234 host.h_name = NULL; 235 eom = answer->buf + anslen; 236 switch (qtype) { 237 case T_A: 238 case T_AAAA: 239 name_ok = res_hnok; 240 break; 241 case T_PTR: 242 name_ok = res_dnok; 243 break; 244 default: 245 return NULL; /* XXX should be abort(); */ 246 } 247 /* 248 * find first satisfactory answer 249 */ 250 hp = &answer->hdr; 251 ancount = ntohs(hp->ancount); 252 qdcount = ntohs(hp->qdcount); 253 bp = hostbuf; 254 ep = hostbuf + sizeof hostbuf; 255 cp = answer->buf; 256 BOUNDED_INCR(HFIXEDSZ); 257 if (qdcount != 1) { 258 h_errno = NO_RECOVERY; 259 return NULL; 260 } 261 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 262 if ((n < 0) || !(*name_ok)(bp)) { 263 h_errno = NO_RECOVERY; 264 return NULL; 265 } 266 BOUNDED_INCR(n + QFIXEDSZ); 267 if (qtype == T_A || qtype == T_AAAA) { 268 /* res_send() has already verified that the query name is the 269 * same as the one we sent; this just gets the expanded name 270 * (i.e., with the succeeding search-domain tacked on). 271 */ 272 n = strlen(bp) + 1; /* for the \0 */ 273 if (n >= MAXHOSTNAMELEN) { 274 h_errno = NO_RECOVERY; 275 return NULL; 276 } 277 host.h_name = bp; 278 bp += n; 279 /* The qname can be abbreviated, but h_name is now absolute. */ 280 qname = host.h_name; 281 } 282 ap = host_aliases; 283 *ap = NULL; 284 host.h_aliases = host_aliases; 285 hap = h_addr_ptrs; 286 *hap = NULL; 287 host.h_addr_list = h_addr_ptrs; 288 haveanswer = 0; 289 had_error = 0; 290 while (ancount-- > 0 && cp < eom && !had_error) { 291 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 292 if ((n < 0) || !(*name_ok)(bp)) { 293 had_error++; 294 continue; 295 } 296 cp += n; /* name */ 297 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); 298 type = _getshort(cp); 299 cp += INT16SZ; /* type */ 300 class = _getshort(cp); 301 cp += INT16SZ + INT32SZ; /* class, TTL */ 302 n = _getshort(cp); 303 cp += INT16SZ; /* len */ 304 BOUNDS_CHECK(cp, n); 305 erdata = cp + n; 306 if (class != C_IN) { 307 /* XXX - debug? syslog? */ 308 cp += n; 309 continue; /* XXX - had_error++ ? */ 310 } 311 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { 312 if (ap >= &host_aliases[MAXALIASES-1]) 313 continue; 314 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 315 if ((n < 0) || !(*name_ok)(tbuf)) { 316 had_error++; 317 continue; 318 } 319 cp += n; 320 if (cp != erdata) { 321 h_errno = NO_RECOVERY; 322 return NULL; 323 } 324 /* Store alias. */ 325 *ap++ = bp; 326 n = strlen(bp) + 1; /* for the \0 */ 327 if (n >= MAXHOSTNAMELEN) { 328 had_error++; 329 continue; 330 } 331 bp += n; 332 /* Get canonical name. */ 333 n = strlen(tbuf) + 1; /* for the \0 */ 334 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 335 had_error++; 336 continue; 337 } 338 strlcpy(bp, tbuf, (size_t)(ep - bp)); 339 host.h_name = bp; 340 bp += n; 341 continue; 342 } 343 if (qtype == T_PTR && type == T_CNAME) { 344 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 345 if (n < 0 || !res_dnok(tbuf)) { 346 had_error++; 347 continue; 348 } 349 cp += n; 350 if (cp != erdata) { 351 h_errno = NO_RECOVERY; 352 return NULL; 353 } 354 /* Get canonical name. */ 355 n = strlen(tbuf) + 1; /* for the \0 */ 356 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 357 had_error++; 358 continue; 359 } 360 strlcpy(bp, tbuf, (size_t)(ep - bp)); 361 tname = bp; 362 bp += n; 363 continue; 364 } 365 if (type != qtype) { 366 if (type != T_KEY && type != T_SIG) 367 syslog(LOG_NOTICE|LOG_AUTH, 368 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 369 qname, p_class(C_IN), p_type(qtype), 370 p_type(type)); 371 cp += n; 372 continue; /* XXX - had_error++ ? */ 373 } 374 switch (type) { 375 case T_PTR: 376 if (strcasecmp(tname, bp) != 0) { 377 syslog(LOG_NOTICE|LOG_AUTH, 378 AskedForGot, qname, bp); 379 cp += n; 380 continue; /* XXX - had_error++ ? */ 381 } 382 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 383 if ((n < 0) || !res_hnok(bp)) { 384 had_error++; 385 break; 386 } 387 #if MULTI_PTRS_ARE_ALIASES 388 cp += n; 389 if (cp != erdata) { 390 h_errno = NO_RECOVERY; 391 return NULL; 392 } 393 if (!haveanswer) 394 host.h_name = bp; 395 else if (ap < &host_aliases[MAXALIASES-1]) 396 *ap++ = bp; 397 else 398 n = -1; 399 if (n != -1) { 400 n = strlen(bp) + 1; /* for the \0 */ 401 if (n >= MAXHOSTNAMELEN) { 402 had_error++; 403 break; 404 } 405 bp += n; 406 } 407 break; 408 #else 409 host.h_name = bp; 410 if (res->options & RES_USE_INET6) { 411 n = strlen(bp) + 1; /* for the \0 */ 412 if (n >= MAXHOSTNAMELEN) { 413 had_error++; 414 break; 415 } 416 bp += n; 417 map_v4v6_hostent(&host, &bp, ep); 418 } 419 h_errno = NETDB_SUCCESS; 420 return &host; 421 #endif 422 case T_A: 423 case T_AAAA: 424 if (strcasecmp(host.h_name, bp) != 0) { 425 syslog(LOG_NOTICE|LOG_AUTH, 426 AskedForGot, host.h_name, bp); 427 cp += n; 428 continue; /* XXX - had_error++ ? */ 429 } 430 if (n != host.h_length) { 431 cp += n; 432 continue; 433 } 434 if (type == T_AAAA) { 435 struct in6_addr in6; 436 memcpy(&in6, cp, IN6ADDRSZ); 437 if (IN6_IS_ADDR_V4MAPPED(&in6)) { 438 cp += n; 439 continue; 440 } 441 } 442 if (!haveanswer) { 443 int nn; 444 445 host.h_name = bp; 446 nn = strlen(bp) + 1; /* for the \0 */ 447 bp += nn; 448 } 449 450 bp += sizeof(align) - 451 (size_t)((u_long)bp % sizeof(align)); 452 453 if (bp + n >= &hostbuf[sizeof hostbuf]) { 454 dprintf("size (%d) too big\n", res, n); 455 had_error++; 456 continue; 457 } 458 if (hap >= &h_addr_ptrs[MAXADDRS-1]) { 459 if (!toobig++) 460 dprintf("Too many addresses (%d)\n", 461 res, MAXADDRS); 462 cp += n; 463 continue; 464 } 465 (void)memcpy(*hap++ = bp, cp, (size_t)n); 466 bp += n; 467 cp += n; 468 if (cp != erdata) { 469 h_errno = NO_RECOVERY; 470 return NULL; 471 } 472 break; 473 default: 474 abort(); 475 } 476 if (!had_error) 477 haveanswer++; 478 } 479 if (haveanswer) { 480 *ap = NULL; 481 *hap = NULL; 482 # if defined(RESOLVSORT) 483 /* 484 * Note: we sort even if host can take only one address 485 * in its return structures - should give it the "best" 486 * address in that case, not some random one 487 */ 488 if (res->nsort && haveanswer > 1 && qtype == T_A) 489 addrsort(h_addr_ptrs, haveanswer, res); 490 # endif /*RESOLVSORT*/ 491 if (!host.h_name) { 492 n = strlen(qname) + 1; /* for the \0 */ 493 if (n > ep - bp || n >= MAXHOSTNAMELEN) 494 goto no_recovery; 495 strlcpy(bp, qname, (size_t)(ep - bp)); 496 host.h_name = bp; 497 bp += n; 498 } 499 if (res->options & RES_USE_INET6) 500 map_v4v6_hostent(&host, &bp, ep); 501 h_errno = NETDB_SUCCESS; 502 return &host; 503 } 504 no_recovery: 505 h_errno = NO_RECOVERY; 506 return NULL; 507 } 508 509 struct hostent * 510 gethostbyname(const char *name) 511 { 512 struct hostent *hp; 513 res_state res = __res_get_state(); 514 515 if (res == NULL) 516 return NULL; 517 518 _DIAGASSERT(name != NULL); 519 520 if (res->options & RES_USE_INET6) { 521 hp = gethostbyname_internal(name, AF_INET6, res); 522 if (hp) { 523 __res_put_state(res); 524 return hp; 525 } 526 } 527 hp = gethostbyname_internal(name, AF_INET, res); 528 __res_put_state(res); 529 return hp; 530 } 531 532 struct hostent * 533 gethostbyname2(const char *name, int af) 534 { 535 struct hostent *hp; 536 res_state res = __res_get_state(); 537 538 if (res == NULL) 539 return NULL; 540 hp = gethostbyname_internal(name, af, res); 541 __res_put_state(res); 542 return hp; 543 } 544 545 static struct hostent * 546 gethostbyname_internal(const char *name, int af, res_state res) 547 { 548 const char *cp; 549 char *bp, *ep; 550 int size; 551 struct hostent *hp; 552 static const ns_dtab dtab[] = { 553 NS_FILES_CB(_gethtbyname, NULL) 554 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */ 555 NS_NIS_CB(_yp_gethtbyname, NULL) 556 { 0 } 557 }; 558 559 _DIAGASSERT(name != NULL); 560 561 switch (af) { 562 case AF_INET: 563 size = INADDRSZ; 564 break; 565 case AF_INET6: 566 size = IN6ADDRSZ; 567 break; 568 default: 569 h_errno = NETDB_INTERNAL; 570 errno = EAFNOSUPPORT; 571 return NULL; 572 } 573 574 host.h_addrtype = af; 575 host.h_length = size; 576 577 /* 578 * if there aren't any dots, it could be a user-level alias. 579 * this is also done in res_nquery() since we are not the only 580 * function that looks up host names. 581 */ 582 if (!strchr(name, '.') && (cp = __hostalias(name))) 583 name = cp; 584 585 /* 586 * disallow names consisting only of digits/dots, unless 587 * they end in a dot. 588 */ 589 if (isdigit((u_char) name[0])) 590 for (cp = name;; ++cp) { 591 if (!*cp) { 592 if (*--cp == '.') 593 break; 594 /* 595 * All-numeric, no dot at the end. 596 * Fake up a hostent as if we'd actually 597 * done a lookup. 598 */ 599 if (inet_pton(af, name, 600 (char *)(void *)host_addr) <= 0) { 601 h_errno = HOST_NOT_FOUND; 602 return NULL; 603 } 604 strncpy(hostbuf, name, MAXDNAME); 605 hostbuf[MAXDNAME] = '\0'; 606 bp = hostbuf + MAXDNAME; 607 ep = hostbuf + sizeof hostbuf; 608 host.h_name = hostbuf; 609 host.h_aliases = host_aliases; 610 host_aliases[0] = NULL; 611 h_addr_ptrs[0] = (char *)(void *)host_addr; 612 h_addr_ptrs[1] = NULL; 613 host.h_addr_list = h_addr_ptrs; 614 if (res->options & RES_USE_INET6) 615 map_v4v6_hostent(&host, &bp, ep); 616 h_errno = NETDB_SUCCESS; 617 return &host; 618 } 619 if (!isdigit((u_char) *cp) && *cp != '.') 620 break; 621 } 622 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || 623 name[0] == ':') 624 for (cp = name;; ++cp) { 625 if (!*cp) { 626 if (*--cp == '.') 627 break; 628 /* 629 * All-IPv6-legal, no dot at the end. 630 * Fake up a hostent as if we'd actually 631 * done a lookup. 632 */ 633 if (inet_pton(af, name, 634 (char *)(void *)host_addr) <= 0) { 635 h_errno = HOST_NOT_FOUND; 636 return NULL; 637 } 638 strncpy(hostbuf, name, MAXDNAME); 639 hostbuf[MAXDNAME] = '\0'; 640 bp = hostbuf + MAXDNAME; 641 ep = hostbuf + sizeof hostbuf; 642 host.h_name = hostbuf; 643 host.h_aliases = host_aliases; 644 host_aliases[0] = NULL; 645 h_addr_ptrs[0] = (char *)(void *)host_addr; 646 h_addr_ptrs[1] = NULL; 647 host.h_addr_list = h_addr_ptrs; 648 h_errno = NETDB_SUCCESS; 649 return &host; 650 } 651 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') 652 break; 653 } 654 655 hp = NULL; 656 h_errno = NETDB_INTERNAL; 657 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname", 658 default_dns_files, name, strlen(name), af) != NS_SUCCESS) 659 return NULL; 660 h_errno = NETDB_SUCCESS; 661 return hp; 662 } 663 664 struct hostent * 665 gethostbyaddr(const char *addr, /* XXX should have been def'd as u_char! */ 666 socklen_t len, int af) 667 { 668 const u_char *uaddr = (const u_char *)addr; 669 socklen_t size; 670 struct hostent *hp; 671 static const ns_dtab dtab[] = { 672 NS_FILES_CB(_gethtbyaddr, NULL) 673 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */ 674 NS_NIS_CB(_yp_gethtbyaddr, NULL) 675 { 0 } 676 }; 677 678 _DIAGASSERT(addr != NULL); 679 680 if (af == AF_INET6 && len == IN6ADDRSZ && 681 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) || 682 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) { 683 h_errno = HOST_NOT_FOUND; 684 return NULL; 685 } 686 if (af == AF_INET6 && len == IN6ADDRSZ && 687 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) || 688 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) { 689 /* Unmap. */ 690 addr += IN6ADDRSZ - INADDRSZ; 691 uaddr += IN6ADDRSZ - INADDRSZ; 692 af = AF_INET; 693 len = INADDRSZ; 694 } 695 switch (af) { 696 case AF_INET: 697 size = INADDRSZ; 698 break; 699 case AF_INET6: 700 size = IN6ADDRSZ; 701 break; 702 default: 703 errno = EAFNOSUPPORT; 704 h_errno = NETDB_INTERNAL; 705 return NULL; 706 } 707 if (size != len) { 708 errno = EINVAL; 709 h_errno = NETDB_INTERNAL; 710 return NULL; 711 } 712 hp = NULL; 713 h_errno = NETDB_INTERNAL; 714 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr", 715 default_dns_files, uaddr, len, af) != NS_SUCCESS) 716 return NULL; 717 h_errno = NETDB_SUCCESS; 718 return hp; 719 } 720 721 void 722 _sethtent(int f) 723 { 724 if (!hostf) 725 hostf = fopen(_PATH_HOSTS, "r" ); 726 else 727 rewind(hostf); 728 stayopen = f; 729 } 730 731 void 732 _endhtent(void) 733 { 734 if (hostf && !stayopen) { 735 (void) fclose(hostf); 736 hostf = NULL; 737 } 738 } 739 740 struct hostent * 741 _gethtent(void) 742 { 743 char *p; 744 char *cp, **q; 745 int af, len; 746 747 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { 748 h_errno = NETDB_INTERNAL; 749 return NULL; 750 } 751 again: 752 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { 753 h_errno = HOST_NOT_FOUND; 754 return NULL; 755 } 756 if (*p == '#') 757 goto again; 758 if (!(cp = strpbrk(p, "#\n"))) 759 goto again; 760 *cp = '\0'; 761 if (!(cp = strpbrk(p, " \t"))) 762 goto again; 763 *cp++ = '\0'; 764 if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) { 765 af = AF_INET6; 766 len = IN6ADDRSZ; 767 } else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) { 768 res_state res = __res_get_state(); 769 if (res == NULL) 770 return NULL; 771 if (res->options & RES_USE_INET6) { 772 map_v4v6_address((char *)(void *)host_addr, 773 (char *)(void *)host_addr); 774 af = AF_INET6; 775 len = IN6ADDRSZ; 776 } else { 777 af = AF_INET; 778 len = INADDRSZ; 779 } 780 __res_put_state(res); 781 } else { 782 goto again; 783 } 784 /* if this is not something we're looking for, skip it. */ 785 if (host.h_addrtype != af) 786 goto again; 787 if (host.h_length != len) 788 goto again; 789 h_addr_ptrs[0] = (char *)(void *)host_addr; 790 h_addr_ptrs[1] = NULL; 791 host.h_addr_list = h_addr_ptrs; 792 host.h_length = len; 793 host.h_addrtype = af; 794 while (*cp == ' ' || *cp == '\t') 795 cp++; 796 host.h_name = cp; 797 q = host.h_aliases = host_aliases; 798 if ((cp = strpbrk(cp, " \t")) != NULL) 799 *cp++ = '\0'; 800 while (cp && *cp) { 801 if (*cp == ' ' || *cp == '\t') { 802 cp++; 803 continue; 804 } 805 if (q < &host_aliases[MAXALIASES - 1]) 806 *q++ = cp; 807 if ((cp = strpbrk(cp, " \t")) != NULL) 808 *cp++ = '\0'; 809 } 810 *q = NULL; 811 h_errno = NETDB_SUCCESS; 812 return &host; 813 } 814 815 /*ARGSUSED*/ 816 int 817 _gethtbyname(void *rv, void *cb_data, va_list ap) 818 { 819 struct hostent *hp; 820 const char *name; 821 int af; 822 823 _DIAGASSERT(rv != NULL); 824 825 name = va_arg(ap, char *); 826 /* NOSTRICT skip len */(void)va_arg(ap, int); 827 af = va_arg(ap, int); 828 829 hp = NULL; 830 #if 0 831 { 832 res_state res = __res_get_state(); 833 if (res == NULL) 834 return NS_NOTFOUND; 835 if (res->options & RES_USE_INET6) 836 hp = _gethtbyname2(name, AF_INET6); 837 if (hp==NULL) 838 hp = _gethtbyname2(name, AF_INET); 839 __res_put_state(res); 840 } 841 #else 842 hp = _gethtbyname2(name, af); 843 #endif 844 *((struct hostent **)rv) = hp; 845 if (hp == NULL) { 846 h_errno = HOST_NOT_FOUND; 847 return NS_NOTFOUND; 848 } 849 return NS_SUCCESS; 850 } 851 852 struct hostent * 853 _gethtbyname2(const char *name, int af) 854 { 855 struct hostent *p; 856 char *tmpbuf, *ptr, **cp; 857 int num; 858 size_t len; 859 860 _DIAGASSERT(name != NULL); 861 862 _sethtent(0); 863 ptr = tmpbuf = NULL; 864 num = 0; 865 while ((p = _gethtent()) != NULL && num < MAXADDRS) { 866 if (p->h_addrtype != af) 867 continue; 868 if (strcasecmp(p->h_name, name) != 0) { 869 for (cp = p->h_aliases; *cp != NULL; cp++) 870 if (strcasecmp(*cp, name) == 0) 871 break; 872 if (*cp == NULL) continue; 873 } 874 875 if (num == 0) { 876 size_t bufsize; 877 char *src; 878 879 bufsize = strlen(p->h_name) + 2 + 880 MAXADDRS * p->h_length + 881 ALIGNBYTES; 882 for (cp = p->h_aliases; *cp != NULL; cp++) 883 bufsize += strlen(*cp) + 1; 884 885 if ((tmpbuf = malloc(bufsize)) == NULL) { 886 h_errno = NETDB_INTERNAL; 887 return NULL; 888 } 889 890 ptr = tmpbuf; 891 src = p->h_name; 892 while ((*ptr++ = *src++) != '\0'); 893 for (cp = p->h_aliases; *cp != NULL; cp++) { 894 src = *cp; 895 while ((*ptr++ = *src++) != '\0'); 896 } 897 *ptr++ = '\0'; 898 899 ptr = (char *)(void *)ALIGN(ptr); 900 } 901 902 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length); 903 ptr += p->h_length; 904 num++; 905 } 906 _endhtent(); 907 if (num == 0) return NULL; 908 909 len = ptr - tmpbuf; 910 if (len > (sizeof(hostbuf) - ALIGNBYTES)) { 911 free(tmpbuf); 912 errno = ENOSPC; 913 h_errno = NETDB_INTERNAL; 914 return NULL; 915 } 916 ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len); 917 free(tmpbuf); 918 919 host.h_name = ptr; 920 while (*ptr++); 921 922 cp = host_aliases; 923 while (*ptr) { 924 *cp++ = ptr; 925 while (*ptr++); 926 } 927 ptr++; 928 *cp = NULL; 929 930 ptr = (char *)(void *)ALIGN(ptr); 931 cp = h_addr_ptrs; 932 while (num--) { 933 *cp++ = ptr; 934 ptr += host.h_length; 935 } 936 *cp = NULL; 937 938 return &host; 939 } 940 941 /*ARGSUSED*/ 942 int 943 _gethtbyaddr(void *rv, void *cb_data, va_list ap) 944 { 945 struct hostent *p; 946 const unsigned char *addr; 947 int len, af; 948 949 _DIAGASSERT(rv != NULL); 950 951 addr = va_arg(ap, unsigned char *); 952 len = va_arg(ap, int); 953 af = va_arg(ap, int); 954 955 host.h_length = len; 956 host.h_addrtype = af; 957 958 _sethtent(0); 959 while ((p = _gethtent()) != NULL) 960 if (p->h_addrtype == af && !memcmp(p->h_addr, addr, 961 (size_t)len)) 962 break; 963 _endhtent(); 964 *((struct hostent **)rv) = p; 965 if (p==NULL) { 966 h_errno = HOST_NOT_FOUND; 967 return NS_NOTFOUND; 968 } 969 return NS_SUCCESS; 970 } 971 972 static void 973 map_v4v6_address(const char *src, char *dst) 974 { 975 u_char *p = (u_char *)dst; 976 char tmp[INADDRSZ]; 977 int i; 978 979 _DIAGASSERT(src != NULL); 980 _DIAGASSERT(dst != NULL); 981 982 /* Stash a temporary copy so our caller can update in place. */ 983 (void)memcpy(tmp, src, INADDRSZ); 984 /* Mark this ipv6 addr as a mapped ipv4. */ 985 for (i = 0; i < 10; i++) 986 *p++ = 0x00; 987 *p++ = 0xff; 988 *p++ = 0xff; 989 /* Retrieve the saved copy and we're done. */ 990 (void)memcpy((void *)p, tmp, INADDRSZ); 991 } 992 993 static void 994 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) 995 { 996 char **ap; 997 998 _DIAGASSERT(hp != NULL); 999 _DIAGASSERT(bpp != NULL); 1000 _DIAGASSERT(ep != NULL); 1001 1002 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) 1003 return; 1004 hp->h_addrtype = AF_INET6; 1005 hp->h_length = IN6ADDRSZ; 1006 for (ap = hp->h_addr_list; *ap; ap++) { 1007 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align)); 1008 1009 if (ep - *bpp < (i + IN6ADDRSZ)) { 1010 /* Out of memory. Truncate address list here. XXX */ 1011 *ap = NULL; 1012 return; 1013 } 1014 *bpp += i; 1015 map_v4v6_address(*ap, *bpp); 1016 *ap = *bpp; 1017 *bpp += IN6ADDRSZ; 1018 } 1019 } 1020 1021 #ifdef RESOLVSORT 1022 static void 1023 addrsort(char **ap, int num, res_state res) 1024 { 1025 int i, j; 1026 char **p; 1027 short aval[MAXADDRS]; 1028 int needsort = 0; 1029 1030 _DIAGASSERT(ap != NULL); 1031 1032 p = ap; 1033 for (i = 0; i < num; i++, p++) { 1034 for (j = 0 ; (unsigned)j < res->nsort; j++) 1035 if (res->sort_list[j].addr.s_addr == 1036 (((struct in_addr *)(void *)(*p))->s_addr & 1037 res->sort_list[j].mask)) 1038 break; 1039 aval[i] = j; 1040 if (needsort == 0 && i > 0 && j < aval[i-1]) 1041 needsort = i; 1042 } 1043 if (!needsort) 1044 return; 1045 1046 while (needsort < num) { 1047 for (j = needsort - 1; j >= 0; j--) { 1048 if (aval[j] > aval[j+1]) { 1049 char *hp; 1050 1051 i = aval[j]; 1052 aval[j] = aval[j+1]; 1053 aval[j+1] = i; 1054 1055 hp = ap[j]; 1056 ap[j] = ap[j+1]; 1057 ap[j+1] = hp; 1058 } else 1059 break; 1060 } 1061 needsort++; 1062 } 1063 } 1064 #endif 1065 1066 #if defined(BSD43_BSD43_NFS) || defined(sun) 1067 /* XXX: should we remove this cruft? - lukem */ 1068 /* some libc's out there are bound internally to these names (UMIPS) */ 1069 void 1070 ht_sethostent(int stayopen) 1071 { 1072 _sethtent(stayopen); 1073 } 1074 1075 void 1076 ht_endhostent(void) 1077 { 1078 _endhtent(); 1079 } 1080 1081 struct hostent * 1082 ht_gethostbyname(char *name) 1083 { 1084 return _gethtbyname(name); 1085 } 1086 1087 struct hostent * 1088 ht_gethostbyaddr(const char *addr, int len, int af) 1089 { 1090 return _gethtbyaddr(addr, len, af); 1091 } 1092 1093 struct hostent * 1094 gethostent(void) 1095 { 1096 return _gethtent(); 1097 } 1098 1099 void 1100 dns_service(void) 1101 { 1102 return; 1103 } 1104 1105 int 1106 dn_skipname(const u_char *comp_dn, const u_char *eom) 1107 { 1108 return __dn_skipname(comp_dn, eom); 1109 } 1110 #endif /*old-style libc with yp junk in it*/ 1111 1112 /*ARGSUSED*/ 1113 int 1114 _dns_gethtbyname(void *rv, void *cb_data, va_list ap) 1115 { 1116 querybuf *buf; 1117 int n, type; 1118 struct hostent *hp; 1119 const char *name; 1120 int af; 1121 res_state res; 1122 1123 _DIAGASSERT(rv != NULL); 1124 1125 name = va_arg(ap, char *); 1126 /* NOSTRICT skip len */(void)va_arg(ap, int); 1127 af = va_arg(ap, int); 1128 1129 switch (af) { 1130 case AF_INET: 1131 type = T_A; 1132 break; 1133 case AF_INET6: 1134 type = T_AAAA; 1135 break; 1136 default: 1137 return NS_UNAVAIL; 1138 } 1139 buf = malloc(sizeof(*buf)); 1140 if (buf == NULL) { 1141 h_errno = NETDB_INTERNAL; 1142 return NS_NOTFOUND; 1143 } 1144 res = __res_get_state(); 1145 if (res == NULL) 1146 return NS_NOTFOUND; 1147 n = res_nsearch(res, name, C_IN, type, buf->buf, sizeof(buf->buf)); 1148 if (n < 0) { 1149 free(buf); 1150 dprintf("res_nsearch failed (%d)\n", res, n); 1151 __res_put_state(res); 1152 return NS_NOTFOUND; 1153 } 1154 hp = getanswer(buf, n, name, type, res); 1155 free(buf); 1156 __res_put_state(res); 1157 if (hp == NULL) 1158 switch (h_errno) { 1159 case HOST_NOT_FOUND: 1160 return NS_NOTFOUND; 1161 case TRY_AGAIN: 1162 return NS_TRYAGAIN; 1163 default: 1164 return NS_UNAVAIL; 1165 } 1166 *((struct hostent **)rv) = hp; 1167 return NS_SUCCESS; 1168 } 1169 1170 /*ARGSUSED*/ 1171 int 1172 _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) 1173 { 1174 char qbuf[MAXDNAME + 1], *qp, *ep; 1175 int n; 1176 querybuf *buf; 1177 struct hostent *hp; 1178 const unsigned char *uaddr; 1179 int len, af, advance; 1180 res_state res; 1181 1182 _DIAGASSERT(rv != NULL); 1183 1184 uaddr = va_arg(ap, unsigned char *); 1185 len = va_arg(ap, int); 1186 af = va_arg(ap, int); 1187 1188 switch (af) { 1189 case AF_INET: 1190 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", 1191 (uaddr[3] & 0xff), (uaddr[2] & 0xff), 1192 (uaddr[1] & 0xff), (uaddr[0] & 0xff)); 1193 break; 1194 1195 case AF_INET6: 1196 qp = qbuf; 1197 ep = qbuf + sizeof(qbuf) - 1; 1198 for (n = IN6ADDRSZ - 1; n >= 0; n--) { 1199 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", 1200 uaddr[n] & 0xf, 1201 ((unsigned int)uaddr[n] >> 4) & 0xf); 1202 if (advance > 0 && qp + advance < ep) 1203 qp += advance; 1204 else { 1205 h_errno = NETDB_INTERNAL; 1206 return NS_NOTFOUND; 1207 } 1208 } 1209 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { 1210 h_errno = NETDB_INTERNAL; 1211 return NS_NOTFOUND; 1212 } 1213 break; 1214 default: 1215 abort(); 1216 } 1217 1218 buf = malloc(sizeof(*buf)); 1219 if (buf == NULL) { 1220 h_errno = NETDB_INTERNAL; 1221 return NS_NOTFOUND; 1222 } 1223 res = __res_get_state(); 1224 if (res == NULL) 1225 return NS_NOTFOUND; 1226 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf)); 1227 if (n < 0 && af == AF_INET6) { 1228 *qp = '\0'; 1229 if (strlcat(qbuf, "ip6.int", sizeof(qbuf)) >= sizeof(qbuf)) { 1230 free(buf); 1231 h_errno = NETDB_INTERNAL; 1232 __res_put_state(res); 1233 return NS_NOTFOUND; 1234 } 1235 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf)); 1236 } 1237 if (n < 0) { 1238 free(buf); 1239 dprintf("res_nquery failed (%d)\n", res, n); 1240 __res_put_state(res); 1241 return NS_NOTFOUND; 1242 } 1243 hp = getanswer(buf, n, qbuf, T_PTR, res); 1244 free(buf); 1245 if (hp == NULL) { 1246 __res_put_state(res); 1247 switch (h_errno) { 1248 case HOST_NOT_FOUND: 1249 return NS_NOTFOUND; 1250 case TRY_AGAIN: 1251 return NS_TRYAGAIN; 1252 default: 1253 return NS_UNAVAIL; 1254 } 1255 } 1256 hp->h_addrtype = af; 1257 hp->h_length = len; 1258 (void)memcpy(host_addr, uaddr, (size_t)len); 1259 h_addr_ptrs[0] = (char *)(void *)host_addr; 1260 h_addr_ptrs[1] = NULL; 1261 if (af == AF_INET && (res->options & RES_USE_INET6)) { 1262 map_v4v6_address((char *)(void *)host_addr, 1263 (char *)(void *)host_addr); 1264 hp->h_addrtype = AF_INET6; 1265 hp->h_length = IN6ADDRSZ; 1266 } 1267 1268 __res_put_state(res); 1269 *((struct hostent **)rv) = hp; 1270 h_errno = NETDB_SUCCESS; 1271 return NS_SUCCESS; 1272 } 1273 1274 #ifdef YP 1275 /*ARGSUSED*/ 1276 struct hostent * 1277 _yphostent(char *line, int af) 1278 { 1279 static struct in_addr host_addrs[MAXADDRS]; 1280 static struct in6_addr host6_addrs[MAXADDRS]; 1281 char *p = line; 1282 char *cp, **q; 1283 char **hap; 1284 int addrok; 1285 int more; 1286 size_t naddrs; 1287 1288 _DIAGASSERT(line != NULL); 1289 1290 host.h_name = NULL; 1291 host.h_addr_list = h_addr_ptrs; 1292 host.h_addrtype = af; 1293 switch (af) { 1294 case AF_INET: 1295 host.h_length = INADDRSZ; 1296 break; 1297 case AF_INET6: 1298 host.h_length = IN6ADDRSZ; 1299 break; 1300 default: 1301 return NULL; 1302 } 1303 hap = h_addr_ptrs; 1304 q = host.h_aliases = host_aliases; 1305 naddrs = 0; 1306 1307 nextline: 1308 /* check for host_addrs overflow */ 1309 if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0])) 1310 goto done; 1311 if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0])) 1312 goto done; 1313 1314 more = 0; 1315 cp = strpbrk(p, " \t"); 1316 if (cp == NULL) 1317 goto done; 1318 *cp++ = '\0'; 1319 1320 /* p has should have an address */ 1321 switch (af) { 1322 case AF_INET: 1323 addrok = inet_aton(p, &host_addrs[naddrs]); 1324 break; 1325 case AF_INET6: 1326 addrok = inet_pton(af, p, &host6_addrs[naddrs]); 1327 break; 1328 } 1329 if (addrok != 1) { 1330 /* skip to the next line */ 1331 while (cp && *cp) { 1332 if (*cp == '\n') { 1333 cp++; 1334 goto nextline; 1335 } 1336 cp++; 1337 } 1338 1339 goto done; 1340 } 1341 1342 switch (af) { 1343 case AF_INET: 1344 *hap++ = (char *)(void *)&host_addrs[naddrs++]; 1345 break; 1346 case AF_INET6: 1347 *hap++ = (char *)(void *)&host6_addrs[naddrs++]; 1348 break; 1349 } 1350 1351 while (*cp == ' ' || *cp == '\t') 1352 cp++; 1353 p = cp; 1354 cp = strpbrk(p, " \t\n"); 1355 if (cp != NULL) { 1356 if (*cp == '\n') 1357 more = 1; 1358 *cp++ = '\0'; 1359 } 1360 if (!host.h_name) 1361 host.h_name = p; 1362 else if (strcmp(host.h_name, p)==0) 1363 ; 1364 else if (q < &host_aliases[MAXALIASES - 1]) 1365 *q++ = p; 1366 p = cp; 1367 if (more) 1368 goto nextline; 1369 1370 while (cp && *cp) { 1371 if (*cp == ' ' || *cp == '\t') { 1372 cp++; 1373 continue; 1374 } 1375 if (*cp == '\n') { 1376 cp++; 1377 goto nextline; 1378 } 1379 if (q < &host_aliases[MAXALIASES - 1]) 1380 *q++ = cp; 1381 cp = strpbrk(cp, " \t"); 1382 if (cp != NULL) 1383 *cp++ = '\0'; 1384 } 1385 1386 done: 1387 if (host.h_name == NULL) 1388 return NULL; 1389 *q = NULL; 1390 *hap = NULL; 1391 return &host; 1392 } 1393 1394 /*ARGSUSED*/ 1395 int 1396 _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap) 1397 { 1398 struct hostent *hp = NULL; 1399 static char *__ypcurrent; 1400 int __ypcurrentlen, r; 1401 char name[INET6_ADDRSTRLEN]; /* XXX enough? */ 1402 const unsigned char *uaddr; 1403 int af; 1404 const char *map; 1405 1406 _DIAGASSERT(rv != NULL); 1407 1408 uaddr = va_arg(ap, unsigned char *); 1409 /* NOSTRICT skip len */(void)va_arg(ap, int); 1410 af = va_arg(ap, int); 1411 1412 if (!__ypdomain) { 1413 if (_yp_check(&__ypdomain) == 0) 1414 return NS_UNAVAIL; 1415 } 1416 /* 1417 * XXX unfortunately, we cannot support IPv6 extended scoped address 1418 * notation here. gethostbyaddr() is not scope-aware. too bad. 1419 */ 1420 if (inet_ntop(af, uaddr, name, sizeof(name)) == NULL) 1421 return NS_UNAVAIL; 1422 if (__ypcurrent) 1423 free(__ypcurrent); 1424 __ypcurrent = NULL; 1425 switch (af) { 1426 case AF_INET: 1427 map = "hosts.byaddr"; 1428 break; 1429 default: 1430 map = "ipnodes.byaddr"; 1431 break; 1432 } 1433 r = yp_match(__ypdomain, map, name, 1434 (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1435 if (r == 0) 1436 hp = _yphostent(__ypcurrent, af); 1437 if (hp == NULL) { 1438 h_errno = HOST_NOT_FOUND; 1439 return NS_NOTFOUND; 1440 } 1441 *((struct hostent **)rv) = hp; 1442 return NS_SUCCESS; 1443 } 1444 1445 /*ARGSUSED*/ 1446 int 1447 _yp_gethtbyname(void *rv, void *cb_data, va_list ap) 1448 { 1449 struct hostent *hp = NULL; 1450 static char *__ypcurrent; 1451 int __ypcurrentlen, r; 1452 const char *name; 1453 int af; 1454 const char *map; 1455 1456 _DIAGASSERT(rv != NULL); 1457 1458 name = va_arg(ap, char *); 1459 /* NOSTRICT skip len */(void)va_arg(ap, int); 1460 af = va_arg(ap, int); 1461 1462 if (!__ypdomain) { 1463 if (_yp_check(&__ypdomain) == 0) 1464 return NS_UNAVAIL; 1465 } 1466 if (__ypcurrent) 1467 free(__ypcurrent); 1468 __ypcurrent = NULL; 1469 switch (af) { 1470 case AF_INET: 1471 map = "hosts.byname"; 1472 break; 1473 default: 1474 map = "ipnodes.byname"; 1475 break; 1476 } 1477 r = yp_match(__ypdomain, map, name, 1478 (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1479 if (r == 0) 1480 hp = _yphostent(__ypcurrent, af); 1481 if (hp == NULL) { 1482 h_errno = HOST_NOT_FOUND; 1483 return NS_NOTFOUND; 1484 } 1485 *((struct hostent **)rv) = hp; 1486 return NS_SUCCESS; 1487 } 1488 #endif 1489