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