1 /*- 2 * Copyright (c) 1994, Garrett Wollman 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD: src/lib/libc/net/gethostnamadr.c,v 1.33 2006/05/21 11:27:28 ume Exp $ 26 * $DragonFly: src/lib/libc/net/gethostnamadr.c,v 1.6 2007/12/29 22:55:29 matthias Exp $ 27 */ 28 29 #include "namespace.h" 30 #include "reentrant.h" 31 #include <sys/param.h> 32 #include <sys/socket.h> 33 #include <netinet/in.h> 34 #include <arpa/inet.h> 35 #include <netdb.h> 36 #include <stdio.h> 37 #include <ctype.h> 38 #include <errno.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <stdarg.h> 42 #include <nsswitch.h> 43 #include <arpa/nameser.h> /* XXX hack for _res */ 44 #include <resolv.h> /* XXX hack for _res */ 45 #include "un-namespace.h" 46 #include "netdb_private.h" 47 #ifdef NS_CACHING 48 #include "nscache.h" 49 #endif 50 51 extern int _ht_gethostbyname(void *, void *, va_list); 52 extern int _dns_gethostbyname(void *, void *, va_list); 53 extern int _nis_gethostbyname(void *, void *, va_list); 54 extern int _ht_gethostbyaddr(void *, void *, va_list); 55 extern int _dns_gethostbyaddr(void *, void *, va_list); 56 extern int _nis_gethostbyaddr(void *, void *, va_list); 57 58 static int gethostbyname_internal(const char *, int, struct hostent *, char *, 59 size_t, struct hostent **, int *, res_state); 60 61 /* Host lookup order if nsswitch.conf is broken or nonexistant */ 62 static const ns_src default_src[] = { 63 { NSSRC_FILES, NS_SUCCESS }, 64 { NSSRC_DNS, NS_SUCCESS }, 65 { 0 } 66 }; 67 #ifdef NS_CACHING 68 static int host_id_func(char *, size_t *, va_list, void *); 69 static int host_marshal_func(char *, size_t *, void *, va_list, void *); 70 static int host_unmarshal_func(char *, size_t, void *, va_list, void *); 71 #endif 72 73 NETDB_THREAD_ALLOC(hostent) 74 NETDB_THREAD_ALLOC(hostent_data) 75 NETDB_THREAD_ALLOC(hostdata) 76 77 static void 78 hostent_free(void *ptr) 79 { 80 free(ptr); 81 } 82 83 static void 84 hostent_data_free(void *ptr) 85 { 86 struct hostent_data *hed = ptr; 87 88 if (hed == NULL) 89 return; 90 hed->stayopen = 0; 91 _endhosthtent(hed); 92 free(hed); 93 } 94 95 static void 96 hostdata_free(void *ptr) 97 { 98 free(ptr); 99 } 100 101 int 102 __copy_hostent(struct hostent *he, struct hostent *hptr, char *buf, 103 size_t buflen) 104 { 105 char *cp; 106 char **ptr; 107 int i, n; 108 int nptr, len; 109 110 /* Find out the amount of space required to store the answer. */ 111 nptr = 2; /* NULL ptrs */ 112 len = (char *)ALIGN(buf) - buf; 113 for (i = 0; he->h_addr_list[i]; i++, nptr++) { 114 len += he->h_length; 115 } 116 for (i = 0; he->h_aliases[i]; i++, nptr++) { 117 len += strlen(he->h_aliases[i]) + 1; 118 } 119 len += strlen(he->h_name) + 1; 120 len += nptr * sizeof(char*); 121 122 if (len > buflen) { 123 errno = ERANGE; 124 return (-1); 125 } 126 127 /* copy address size and type */ 128 hptr->h_addrtype = he->h_addrtype; 129 n = hptr->h_length = he->h_length; 130 131 ptr = (char **)ALIGN(buf); 132 cp = (char *)ALIGN(buf) + nptr * sizeof(char *); 133 134 /* copy address list */ 135 hptr->h_addr_list = ptr; 136 for (i = 0; he->h_addr_list[i]; i++ , ptr++) { 137 memcpy(cp, he->h_addr_list[i], n); 138 hptr->h_addr_list[i] = cp; 139 cp += n; 140 } 141 hptr->h_addr_list[i] = NULL; 142 ptr++; 143 144 /* copy official name */ 145 n = strlen(he->h_name) + 1; 146 strcpy(cp, he->h_name); 147 hptr->h_name = cp; 148 cp += n; 149 150 /* copy aliases */ 151 hptr->h_aliases = ptr; 152 for (i = 0 ; he->h_aliases[i]; i++) { 153 n = strlen(he->h_aliases[i]) + 1; 154 strcpy(cp, he->h_aliases[i]); 155 hptr->h_aliases[i] = cp; 156 cp += n; 157 } 158 hptr->h_aliases[i] = NULL; 159 160 return (0); 161 } 162 163 #ifdef NS_CACHING 164 static int 165 host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata) 166 { 167 res_state statp; 168 u_long res_options; 169 170 const int op_id = 1; 171 char *str; 172 void *addr; 173 socklen_t len; 174 int type; 175 176 size_t desired_size, size; 177 enum nss_lookup_type lookup_type; 178 char *p; 179 int res = NS_UNAVAIL; 180 181 statp = __res_state(); 182 res_options = statp->options & (RES_RECURSE | RES_DEFNAMES | 183 RES_DNSRCH | RES_NOALIASES | RES_USE_INET6); 184 185 lookup_type = (enum nss_lookup_type)cache_mdata; 186 switch (lookup_type) { 187 case nss_lt_name: 188 str = va_arg(ap, char *); 189 type = va_arg(ap, int); 190 191 size = strlen(str); 192 desired_size = sizeof(res_options) + sizeof(int) + 193 sizeof(enum nss_lookup_type) + sizeof(int) + size + 1; 194 195 if (desired_size > *buffer_size) { 196 res = NS_RETURN; 197 goto fin; 198 } 199 200 p = buffer; 201 202 memcpy(p, &res_options, sizeof(res_options)); 203 p += sizeof(res_options); 204 205 memcpy(p, &op_id, sizeof(int)); 206 p += sizeof(int); 207 208 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type)); 209 p += sizeof(int); 210 211 memcpy(p, &type, sizeof(int)); 212 p += sizeof(int); 213 214 memcpy(p, str, size + 1); 215 216 res = NS_SUCCESS; 217 break; 218 case nss_lt_id: 219 addr = va_arg(ap, void *); 220 len = va_arg(ap, socklen_t); 221 type = va_arg(ap, int); 222 223 desired_size = sizeof(res_options) + sizeof(int) + 224 sizeof(enum nss_lookup_type) + sizeof(int) + 225 sizeof(socklen_t) + len; 226 227 if (desired_size > *buffer_size) { 228 res = NS_RETURN; 229 goto fin; 230 } 231 232 p = buffer; 233 memcpy(p, &res_options, sizeof(res_options)); 234 p += sizeof(res_options); 235 236 memcpy(p, &op_id, sizeof(int)); 237 p += sizeof(int); 238 239 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type)); 240 p += sizeof(int); 241 242 memcpy(p, &type, sizeof(int)); 243 p += sizeof(int); 244 245 memcpy(p, &len, sizeof(socklen_t)); 246 p += sizeof(socklen_t); 247 248 memcpy(p, addr, len); 249 250 res = NS_SUCCESS; 251 break; 252 default: 253 /* should be unreachable */ 254 return (NS_UNAVAIL); 255 } 256 257 fin: 258 *buffer_size = desired_size; 259 return (res); 260 } 261 262 static int 263 host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap, 264 void *cache_mdata) 265 { 266 char *str; 267 void *addr; 268 socklen_t len; 269 int type; 270 struct hostent *ht; 271 272 struct hostent new_ht; 273 size_t desired_size, aliases_size, addr_size, size; 274 char *p, **iter; 275 276 switch ((enum nss_lookup_type)cache_mdata) { 277 case nss_lt_name: 278 str = va_arg(ap, char *); 279 type = va_arg(ap, int); 280 break; 281 case nss_lt_id: 282 addr = va_arg(ap, void *); 283 len = va_arg(ap, socklen_t); 284 type = va_arg(ap, int); 285 break; 286 default: 287 /* should be unreachable */ 288 return (NS_UNAVAIL); 289 } 290 ht = va_arg(ap, struct hostent *); 291 292 desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *); 293 if (ht->h_name != NULL) 294 desired_size += strlen(ht->h_name) + 1; 295 296 if (ht->h_aliases != NULL) { 297 aliases_size = 0; 298 for (iter = ht->h_aliases; *iter; ++iter) { 299 desired_size += strlen(*iter) + 1; 300 ++aliases_size; 301 } 302 303 desired_size += _ALIGNBYTES + 304 (aliases_size + 1) * sizeof(char *); 305 } 306 307 if (ht->h_addr_list != NULL) { 308 addr_size = 0; 309 for (iter = ht->h_addr_list; *iter; ++iter) 310 ++addr_size; 311 312 desired_size += addr_size * _ALIGN(ht->h_length); 313 desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *); 314 } 315 316 if (desired_size > *buffer_size) { 317 /* this assignment is here for future use */ 318 *buffer_size = desired_size; 319 return (NS_RETURN); 320 } 321 322 memcpy(&new_ht, ht, sizeof(struct hostent)); 323 memset(buffer, 0, desired_size); 324 325 *buffer_size = desired_size; 326 p = buffer + sizeof(struct hostent) + sizeof(char *); 327 memcpy(buffer + sizeof(struct hostent), &p, sizeof(char *)); 328 p = (char *)_ALIGN(p); 329 330 if (new_ht.h_name != NULL) { 331 size = strlen(new_ht.h_name); 332 memcpy(p, new_ht.h_name, size); 333 new_ht.h_name = p; 334 p += size + 1; 335 } 336 337 if (new_ht.h_aliases != NULL) { 338 p = (char *)_ALIGN(p); 339 memcpy(p, new_ht.h_aliases, sizeof(char *) * aliases_size); 340 new_ht.h_aliases = (char **)p; 341 p += sizeof(char *) * (aliases_size + 1); 342 343 for (iter = new_ht.h_aliases; *iter; ++iter) { 344 size = strlen(*iter); 345 memcpy(p, *iter, size); 346 *iter = p; 347 p += size + 1; 348 } 349 } 350 351 if (new_ht.h_addr_list != NULL) { 352 p = (char *)_ALIGN(p); 353 memcpy(p, new_ht.h_addr_list, sizeof(char *) * addr_size); 354 new_ht.h_addr_list = (char **)p; 355 p += sizeof(char *) * (addr_size + 1); 356 357 size = _ALIGN(new_ht.h_length); 358 for (iter = new_ht.h_addr_list; *iter; ++iter) { 359 memcpy(p, *iter, size); 360 *iter = p; 361 p += size + 1; 362 } 363 } 364 memcpy(buffer, &new_ht, sizeof(struct hostent)); 365 return (NS_SUCCESS); 366 } 367 368 static int 369 host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap, 370 void *cache_mdata) 371 { 372 char *str; 373 void *addr; 374 socklen_t len; 375 int type; 376 struct hostent *ht; 377 378 char *p; 379 char **iter; 380 char *orig_buf; 381 size_t orig_buf_size; 382 383 switch ((enum nss_lookup_type)cache_mdata) { 384 case nss_lt_name: 385 str = va_arg(ap, char *); 386 type = va_arg(ap, int); 387 break; 388 case nss_lt_id: 389 addr = va_arg(ap, void *); 390 len = va_arg(ap, socklen_t); 391 type = va_arg(ap, int); 392 break; 393 default: 394 /* should be unreachable */ 395 return (NS_UNAVAIL); 396 } 397 398 ht = va_arg(ap, struct hostent *); 399 orig_buf = va_arg(ap, char *); 400 orig_buf_size = va_arg(ap, size_t); 401 402 if (orig_buf_size < 403 buffer_size - sizeof(struct hostent) - sizeof(char *)) { 404 errno = ERANGE; 405 return (NS_RETURN); 406 } 407 408 memcpy(ht, buffer, sizeof(struct hostent)); 409 memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *)); 410 411 orig_buf = (char *)_ALIGN(orig_buf); 412 memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) + 413 _ALIGN(p) - (size_t)p, 414 buffer_size - sizeof(struct hostent) - sizeof(char *) - 415 _ALIGN(p) + (size_t)p); 416 p = (char *)_ALIGN(p); 417 418 NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *); 419 if (ht->h_aliases != NULL) { 420 NS_APPLY_OFFSET(ht->h_aliases, orig_buf, p, char **); 421 422 for (iter = ht->h_aliases; *iter; ++iter) 423 NS_APPLY_OFFSET(*iter, orig_buf, p, char *); 424 } 425 426 if (ht->h_addr_list != NULL) { 427 NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **); 428 429 for (iter = ht->h_addr_list; *iter; ++iter) 430 NS_APPLY_OFFSET(*iter, orig_buf, p, char *); 431 } 432 433 *((struct hostent **)retval) = ht; 434 return (NS_SUCCESS); 435 } 436 #endif /* NS_CACHING */ 437 438 static int 439 fakeaddr(const char *name, int af, struct hostent *hp, char *buf, 440 size_t buflen, res_state statp) 441 { 442 struct hostent_data *hed; 443 struct hostent he; 444 445 if ((hed = __hostent_data_init()) == NULL) { 446 errno = ENOMEM; 447 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 448 return (-1); 449 } 450 451 if ((af != AF_INET || 452 inet_aton(name, (struct in_addr *)hed->host_addr) != 1) && 453 inet_pton(af, name, hed->host_addr) != 1) { 454 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); 455 return (-1); 456 } 457 strncpy(hed->hostbuf, name, MAXDNAME); 458 hed->hostbuf[MAXDNAME] = '\0'; 459 if (af == AF_INET && (statp->options & RES_USE_INET6) != 0U) { 460 _map_v4v6_address((char *)hed->host_addr, 461 (char *)hed->host_addr); 462 af = AF_INET6; 463 } 464 he.h_addrtype = af; 465 switch(af) { 466 case AF_INET: 467 he.h_length = NS_INADDRSZ; 468 break; 469 case AF_INET6: 470 he.h_length = NS_IN6ADDRSZ; 471 break; 472 default: 473 errno = EAFNOSUPPORT; 474 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 475 return (-1); 476 } 477 he.h_name = hed->hostbuf; 478 he.h_aliases = hed->host_aliases; 479 hed->host_aliases[0] = NULL; 480 hed->h_addr_ptrs[0] = (char *)hed->host_addr; 481 hed->h_addr_ptrs[1] = NULL; 482 he.h_addr_list = hed->h_addr_ptrs; 483 RES_SET_H_ERRNO(statp, NETDB_SUCCESS); 484 return (__copy_hostent(&he, hp, buf, buflen)); 485 } 486 487 int 488 gethostbyname_r(const char *name, struct hostent *he, char *buffer, 489 size_t buflen, struct hostent **result, int *h_errnop) 490 { 491 res_state statp; 492 493 statp = __res_state(); 494 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 495 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 496 return (-1); 497 } 498 if (statp->options & RES_USE_INET6) { 499 if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) { 500 *result = he; 501 return (0); 502 } 503 if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen, 504 result, h_errnop, statp) == 0) 505 return (0); 506 } 507 return (gethostbyname_internal(name, AF_INET, he, buffer, buflen, 508 result, h_errnop, statp)); 509 } 510 511 int 512 gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer, 513 size_t buflen, struct hostent **result, int *h_errnop) 514 { 515 res_state statp; 516 517 statp = __res_state(); 518 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 519 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 520 return (-1); 521 } 522 return (gethostbyname_internal(name, af, he, buffer, buflen, result, 523 h_errnop, statp)); 524 } 525 526 int 527 gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf, 528 size_t buflen, struct hostent **result, int *h_errnop, 529 res_state statp) 530 { 531 const char *cp; 532 int rval, ret_errno; 533 char abuf[MAXDNAME]; 534 535 #ifdef NS_CACHING 536 static const nss_cache_info cache_info = 537 NS_COMMON_CACHE_INFO_INITIALIZER( 538 hosts, (void *)nss_lt_name, 539 host_id_func, host_marshal_func, host_unmarshal_func); 540 #endif 541 static const ns_dtab dtab[] = { 542 NS_FILES_CB(_ht_gethostbyname, NULL) 543 { NSSRC_DNS, _dns_gethostbyname, NULL }, 544 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */ 545 #ifdef NS_CACHING 546 NS_CACHE_CB(&cache_info) 547 #endif 548 { 0 } 549 }; 550 551 switch (af) { 552 case AF_INET: 553 case AF_INET6: 554 break; 555 default: 556 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 557 *h_errnop = statp->res_h_errno; 558 errno = EAFNOSUPPORT; 559 return (-1); 560 } 561 562 /* 563 * if there aren't any dots, it could be a user-level alias. 564 * this is also done in res_query() since we are not the only 565 * function that looks up host names. 566 */ 567 if (!strchr(name, '.') && 568 (cp = res_hostalias(statp, name, abuf, sizeof abuf))) 569 name = cp; 570 571 if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) { 572 *result = hp; 573 return (0); 574 } 575 576 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS, 577 "gethostbyname2_r", default_src, name, af, hp, buf, buflen, 578 &ret_errno, h_errnop); 579 580 return ((rval == NS_SUCCESS) ? 0 : -1); 581 } 582 583 int 584 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp, 585 char *buf, size_t buflen, struct hostent **result, 586 int *h_errnop) 587 { 588 const u_char *uaddr = (const u_char *)addr; 589 const struct in6_addr *addr6; 590 socklen_t size; 591 int rval, ret_errno; 592 res_state statp; 593 594 #ifdef NS_CACHING 595 static const nss_cache_info cache_info = 596 NS_COMMON_CACHE_INFO_INITIALIZER( 597 hosts, (void *)nss_lt_id, 598 host_id_func, host_marshal_func, host_unmarshal_func); 599 #endif 600 static const ns_dtab dtab[] = { 601 NS_FILES_CB(_ht_gethostbyaddr, NULL) 602 { NSSRC_DNS, _dns_gethostbyaddr, NULL }, 603 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */ 604 #ifdef NS_CACHING 605 NS_CACHE_CB(&cache_info) 606 #endif 607 { 0 } 608 }; 609 610 statp = __res_state(); 611 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 612 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 613 *h_errnop = statp->res_h_errno; 614 return (-1); 615 } 616 617 if (af == AF_INET6 && len == NS_IN6ADDRSZ) { 618 addr6 = (const struct in6_addr *)addr; 619 if (IN6_IS_ADDR_LINKLOCAL(addr6)) { 620 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); 621 *h_errnop = statp->res_h_errno; 622 return (-1); 623 } 624 if (IN6_IS_ADDR_V4MAPPED(addr6) || 625 IN6_IS_ADDR_V4COMPAT(addr6)) { 626 /* Unmap. */ 627 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; 628 af = AF_INET; 629 len = NS_INADDRSZ; 630 } 631 } 632 switch (af) { 633 case AF_INET: 634 size = NS_INADDRSZ; 635 break; 636 case AF_INET6: 637 size = NS_IN6ADDRSZ; 638 break; 639 default: 640 errno = EAFNOSUPPORT; 641 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 642 *h_errnop = statp->res_h_errno; 643 return (-1); 644 } 645 if (size != len) { 646 errno = EINVAL; 647 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 648 *h_errnop = statp->res_h_errno; 649 return (-1); 650 } 651 652 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS, 653 "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen, 654 &ret_errno, h_errnop); 655 656 return ((rval == NS_SUCCESS) ? 0 : -1); 657 } 658 659 struct hostent * 660 gethostbyname(const char *name) 661 { 662 struct hostdata *hd; 663 struct hostent *rval; 664 int ret_h_errno; 665 666 if ((hd = __hostdata_init()) == NULL) 667 return (NULL); 668 if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval, 669 &ret_h_errno) != 0) 670 return (NULL); 671 return (rval); 672 } 673 674 struct hostent * 675 gethostbyname2(const char *name, int af) 676 { 677 struct hostdata *hd; 678 struct hostent *rval; 679 int ret_h_errno; 680 681 if ((hd = __hostdata_init()) == NULL) 682 return (NULL); 683 if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data), 684 &rval, &ret_h_errno) != 0) 685 return (NULL); 686 return (rval); 687 } 688 689 struct hostent * 690 gethostbyaddr(const void *addr, socklen_t len, int af) 691 { 692 struct hostdata *hd; 693 struct hostent *rval; 694 int ret_h_errno; 695 696 if ((hd = __hostdata_init()) == NULL) 697 return (NULL); 698 if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data, 699 sizeof(hd->data), &rval, &ret_h_errno) != 0) 700 return (NULL); 701 return (rval); 702 } 703 704 void 705 sethostent(int stayopen) 706 { 707 struct hostent_data *hed; 708 709 if ((hed = __hostent_data_init()) == NULL) 710 return; 711 _sethosthtent(stayopen, hed); 712 _sethostdnsent(stayopen); 713 } 714 715 void 716 endhostent(void) 717 { 718 struct hostent_data *hed; 719 720 if ((hed = __hostent_data_init()) == NULL) 721 return; 722 _endhosthtent(hed); 723 _endhostdnsent(); 724 } 725