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