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