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