xref: /freebsd/lib/libc/net/gethostnamadr.c (revision 2be1a816)
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 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
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, res_state statp)
529 {
530 	const char *cp;
531 	int rval, ret_errno;
532 	char abuf[MAXDNAME];
533 
534 #ifdef NS_CACHING
535 	static const nss_cache_info cache_info =
536 		NS_COMMON_CACHE_INFO_INITIALIZER(
537 		hosts, (void *)nss_lt_name,
538 		host_id_func, host_marshal_func, host_unmarshal_func);
539 #endif
540 	static const ns_dtab dtab[] = {
541 		NS_FILES_CB(_ht_gethostbyname, NULL)
542 		{ NSSRC_DNS, _dns_gethostbyname, NULL },
543 		NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
544 #ifdef NS_CACHING
545 		NS_CACHE_CB(&cache_info)
546 #endif
547 		{ 0 }
548 	};
549 
550 	switch (af) {
551 	case AF_INET:
552 	case AF_INET6:
553 		break;
554 	default:
555 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
556 		*h_errnop = statp->res_h_errno;
557 		errno = EAFNOSUPPORT;
558 		return (-1);
559 	}
560 
561 	/*
562 	 * if there aren't any dots, it could be a user-level alias.
563 	 * this is also done in res_query() since we are not the only
564 	 * function that looks up host names.
565 	 */
566 	if (!strchr(name, '.') &&
567 	    (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
568 		name = cp;
569 
570 	if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
571 		*result = hp;
572 		return (0);
573 	}
574 
575 	rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
576 	    "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
577 	    &ret_errno, h_errnop);
578 
579 	return ((rval == NS_SUCCESS) ? 0 : -1);
580 }
581 
582 int
583 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
584     char *buf, size_t buflen, struct hostent **result, int *h_errnop)
585 {
586 	const u_char *uaddr = (const u_char *)addr;
587 	const struct in6_addr *addr6;
588 	socklen_t size;
589 	int rval, ret_errno;
590 	res_state statp;
591 
592 #ifdef NS_CACHING
593 	static const nss_cache_info cache_info =
594 		NS_COMMON_CACHE_INFO_INITIALIZER(
595 		hosts, (void *)nss_lt_id,
596 		host_id_func, host_marshal_func, host_unmarshal_func);
597 #endif
598 	static const ns_dtab dtab[] = {
599 		NS_FILES_CB(_ht_gethostbyaddr, NULL)
600 		{ NSSRC_DNS, _dns_gethostbyaddr, NULL },
601 		NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
602 #ifdef NS_CACHING
603 		NS_CACHE_CB(&cache_info)
604 #endif
605 		{ 0 }
606 	};
607 
608 	statp = __res_state();
609 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
610 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
611 		*h_errnop = statp->res_h_errno;
612 		return (-1);
613 	}
614 
615 	if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
616 		addr6 = (const struct in6_addr *)addr;
617 		if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
618 			RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
619 			*h_errnop = statp->res_h_errno;
620 			return (-1);
621 		}
622 		if (IN6_IS_ADDR_V4MAPPED(addr6) ||
623 		    IN6_IS_ADDR_V4COMPAT(addr6)) {
624 			/* Unmap. */
625 			uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
626 			af = AF_INET;
627 			len = NS_INADDRSZ;
628 		}
629 	}
630 	switch (af) {
631 	case AF_INET:
632 		size = NS_INADDRSZ;
633 		break;
634 	case AF_INET6:
635 		size = NS_IN6ADDRSZ;
636 		break;
637 	default:
638 		errno = EAFNOSUPPORT;
639 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
640 		*h_errnop = statp->res_h_errno;
641 		return (-1);
642 	}
643 	if (size != len) {
644 		errno = EINVAL;
645 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
646 		*h_errnop = statp->res_h_errno;
647 		return (-1);
648 	}
649 
650 	rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
651 	    "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
652 	    &ret_errno, h_errnop);
653 
654 	return ((rval == NS_SUCCESS) ? 0 : -1);
655 }
656 
657 struct hostent *
658 gethostbyname(const char *name)
659 {
660 	struct hostdata *hd;
661 	struct hostent *rval;
662 	int ret_h_errno;
663 
664 	if ((hd = __hostdata_init()) == NULL)
665 		return (NULL);
666 	if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
667 	    &ret_h_errno) != 0)
668 		return (NULL);
669 	return (rval);
670 }
671 
672 struct hostent *
673 gethostbyname2(const char *name, int af)
674 {
675 	struct hostdata *hd;
676 	struct hostent *rval;
677 	int ret_h_errno;
678 
679 	if ((hd = __hostdata_init()) == NULL)
680 		return (NULL);
681 	if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
682 	    &rval, &ret_h_errno) != 0)
683 		return (NULL);
684 	return (rval);
685 }
686 
687 struct hostent *
688 gethostbyaddr(const void *addr, socklen_t len, int af)
689 {
690 	struct hostdata *hd;
691 	struct hostent *rval;
692 	int ret_h_errno;
693 
694 	if ((hd = __hostdata_init()) == NULL)
695 		return (NULL);
696 	if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
697 	    sizeof(hd->data), &rval, &ret_h_errno) != 0)
698 		return (NULL);
699 	return (rval);
700 }
701 
702 void
703 sethostent(int stayopen)
704 {
705 	struct hostent_data *hed;
706 
707 	if ((hed = __hostent_data_init()) == NULL)
708 		return;
709 	_sethosthtent(stayopen, hed);
710 	_sethostdnsent(stayopen);
711 }
712 
713 void
714 endhostent(void)
715 {
716 	struct hostent_data *hed;
717 
718 	if ((hed = __hostent_data_init()) == NULL)
719 		return;
720 	_endhosthtent(hed);
721 	_endhostdnsent();
722 }
723