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