Home | History | Annotate | Line # | Download | only in net
gethnamaddr.c revision 1.87
      1 /*	$NetBSD: gethnamaddr.c,v 1.87 2014/01/17 02:03:44 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.87 2014/01/17 02:03:44 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 #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 - 1]) {			\
    113 		char **xptr = realloc(arr, siz + 10); 	\
    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); 			\
    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 		*q++ = cp;
    812 		if ((cp = strpbrk(cp, " \t")) != NULL)
    813 			*cp++ = '\0';
    814 	}
    815 	hent->h_length = len;
    816 	hent->h_addrtype = af;
    817 	HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
    818 	anum = (size_t)(q - aliases);
    819 	HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
    820 	HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf,
    821 	    buflen);
    822 	hent->h_addr_list[1] = NULL;
    823 
    824 	HENT_SCOPY(hent->h_name, name, buf, buflen);
    825 	for (size_t i = 0; i < anum; i++)
    826 		HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
    827 	hent->h_aliases[anum] = NULL;
    828 
    829 	*he = NETDB_SUCCESS;
    830 	free(aliases);
    831 	return hent;
    832 nospc:
    833 	free(aliases);
    834 	errno = ENOSPC;
    835 	*he = NETDB_INTERNAL;
    836 	return NULL;
    837 }
    838 
    839 static void
    840 map_v4v6_address(const char *src, char *dst)
    841 {
    842 	u_char *p = (u_char *)dst;
    843 	char tmp[NS_INADDRSZ];
    844 	int i;
    845 
    846 	_DIAGASSERT(src != NULL);
    847 	_DIAGASSERT(dst != NULL);
    848 
    849 	/* Stash a temporary copy so our caller can update in place. */
    850 	(void)memcpy(tmp, src, NS_INADDRSZ);
    851 	/* Mark this ipv6 addr as a mapped ipv4. */
    852 	for (i = 0; i < 10; i++)
    853 		*p++ = 0x00;
    854 	*p++ = 0xff;
    855 	*p++ = 0xff;
    856 	/* Retrieve the saved copy and we're done. */
    857 	(void)memcpy(p, tmp, NS_INADDRSZ);
    858 }
    859 
    860 static void
    861 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
    862 {
    863 	char **ap;
    864 
    865 	_DIAGASSERT(hp != NULL);
    866 	_DIAGASSERT(bpp != NULL);
    867 	_DIAGASSERT(ep != NULL);
    868 
    869 	if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
    870 		return;
    871 	hp->h_addrtype = AF_INET6;
    872 	hp->h_length = NS_IN6ADDRSZ;
    873 	for (ap = hp->h_addr_list; *ap; ap++) {
    874 		int i = (int)(sizeof(align) -
    875 		    (size_t)((u_long)*bpp % sizeof(align)));
    876 
    877 		if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
    878 			/* Out of memory.  Truncate address list here.  XXX */
    879 			*ap = NULL;
    880 			return;
    881 		}
    882 		*bpp += i;
    883 		map_v4v6_address(*ap, *bpp);
    884 		*ap = *bpp;
    885 		*bpp += NS_IN6ADDRSZ;
    886 	}
    887 }
    888 
    889 static void
    890 addrsort(char **ap, int num, res_state res)
    891 {
    892 	int i, j;
    893 	char **p;
    894 	short aval[MAXADDRS];
    895 	int needsort = 0;
    896 
    897 	_DIAGASSERT(ap != NULL);
    898 
    899 	p = ap;
    900 	for (i = 0; i < num; i++, p++) {
    901 	    for (j = 0 ; (unsigned)j < res->nsort; j++)
    902 		if (res->sort_list[j].addr.s_addr ==
    903 		    (((struct in_addr *)(void *)(*p))->s_addr &
    904 		    res->sort_list[j].mask))
    905 			break;
    906 	    aval[i] = j;
    907 	    if (needsort == 0 && i > 0 && j < aval[i-1])
    908 		needsort = i;
    909 	}
    910 	if (!needsort)
    911 	    return;
    912 
    913 	while (needsort < num) {
    914 	    for (j = needsort - 1; j >= 0; j--) {
    915 		if (aval[j] > aval[j+1]) {
    916 		    char *hp;
    917 
    918 		    i = aval[j];
    919 		    aval[j] = aval[j+1];
    920 		    aval[j+1] = i;
    921 
    922 		    hp = ap[j];
    923 		    ap[j] = ap[j+1];
    924 		    ap[j+1] = hp;
    925 		} else
    926 		    break;
    927 	    }
    928 	    needsort++;
    929 	}
    930 }
    931 
    932 
    933 /*ARGSUSED*/
    934 int
    935 _dns_gethtbyname(void *rv, void *cb_data, va_list ap)
    936 {
    937 	querybuf *buf;
    938 	int n, type;
    939 	struct hostent *hp;
    940 	const char *name;
    941 	res_state res;
    942 	struct getnamaddr *info = rv;
    943 
    944 	_DIAGASSERT(rv != NULL);
    945 
    946 	name = va_arg(ap, char *);
    947 	/* NOSTRICT skip string len */(void)va_arg(ap, int);
    948 	info->hp->h_addrtype = va_arg(ap, int);
    949 
    950 	switch (info->hp->h_addrtype) {
    951 	case AF_INET:
    952 		info->hp->h_length = NS_INADDRSZ;
    953 		type = T_A;
    954 		break;
    955 	case AF_INET6:
    956 		info->hp->h_length = NS_IN6ADDRSZ;
    957 		type = T_AAAA;
    958 		break;
    959 	default:
    960 		return NS_UNAVAIL;
    961 	}
    962 	buf = malloc(sizeof(*buf));
    963 	if (buf == NULL) {
    964 		*info->he = NETDB_INTERNAL;
    965 		return NS_NOTFOUND;
    966 	}
    967 	res = __res_get_state();
    968 	if (res == NULL) {
    969 		free(buf);
    970 		return NS_NOTFOUND;
    971 	}
    972 	n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
    973 	if (n < 0) {
    974 		free(buf);
    975 		debugprintf("res_nsearch failed (%d)\n", res, n);
    976 		__res_put_state(res);
    977 		return NS_NOTFOUND;
    978 	}
    979 	hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
    980 	    info->buflen, info->he);
    981 	free(buf);
    982 	__res_put_state(res);
    983 	if (hp == NULL)
    984 		switch (h_errno) {
    985 		case HOST_NOT_FOUND:
    986 			return NS_NOTFOUND;
    987 		case TRY_AGAIN:
    988 			return NS_TRYAGAIN;
    989 		default:
    990 			return NS_UNAVAIL;
    991 		}
    992 	return NS_SUCCESS;
    993 }
    994 
    995 /*ARGSUSED*/
    996 int
    997 _dns_gethtbyaddr(void *rv, void	*cb_data, va_list ap)
    998 {
    999 	char qbuf[MAXDNAME + 1], *qp, *ep;
   1000 	int n;
   1001 	querybuf *buf;
   1002 	struct hostent *hp;
   1003 	const unsigned char *uaddr;
   1004 	int advance;
   1005 	res_state res;
   1006 	char *bf;
   1007 	size_t blen;
   1008 	struct getnamaddr *info = rv;
   1009 
   1010 	_DIAGASSERT(rv != NULL);
   1011 
   1012 	uaddr = va_arg(ap, unsigned char *);
   1013 	info->hp->h_length = va_arg(ap, int);
   1014 	info->hp->h_addrtype = va_arg(ap, int);
   1015 
   1016 	switch (info->hp->h_addrtype) {
   1017 	case AF_INET:
   1018 		(void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
   1019 		    (uaddr[3] & 0xff), (uaddr[2] & 0xff),
   1020 		    (uaddr[1] & 0xff), (uaddr[0] & 0xff));
   1021 		break;
   1022 
   1023 	case AF_INET6:
   1024 		qp = qbuf;
   1025 		ep = qbuf + sizeof(qbuf) - 1;
   1026 		for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
   1027 			advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
   1028 			    uaddr[n] & 0xf,
   1029 			    ((unsigned int)uaddr[n] >> 4) & 0xf);
   1030 			if (advance > 0 && qp + advance < ep)
   1031 				qp += advance;
   1032 			else {
   1033 				*info->he = NETDB_INTERNAL;
   1034 				return NS_NOTFOUND;
   1035 			}
   1036 		}
   1037 		if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
   1038 			*info->he = NETDB_INTERNAL;
   1039 			return NS_NOTFOUND;
   1040 		}
   1041 		break;
   1042 	default:
   1043 		return NS_UNAVAIL;
   1044 	}
   1045 
   1046 	buf = malloc(sizeof(*buf));
   1047 	if (buf == NULL) {
   1048 		*info->he = NETDB_INTERNAL;
   1049 		return NS_NOTFOUND;
   1050 	}
   1051 	res = __res_get_state();
   1052 	if (res == NULL) {
   1053 		free(buf);
   1054 		return NS_NOTFOUND;
   1055 	}
   1056 	n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
   1057 	if (n < 0) {
   1058 		free(buf);
   1059 		debugprintf("res_nquery failed (%d)\n", res, n);
   1060 		__res_put_state(res);
   1061 		return NS_NOTFOUND;
   1062 	}
   1063 	hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
   1064 	    info->buflen, info->he);
   1065 	free(buf);
   1066 	if (hp == NULL) {
   1067 		__res_put_state(res);
   1068 		switch (*info->he) {
   1069 		case HOST_NOT_FOUND:
   1070 			return NS_NOTFOUND;
   1071 		case TRY_AGAIN:
   1072 			return NS_TRYAGAIN;
   1073 		default:
   1074 			return NS_UNAVAIL;
   1075 		}
   1076 	}
   1077 
   1078 	bf = (void *)(hp->h_addr_list + 2);
   1079 	blen = (size_t)(bf - info->buf);
   1080 	if (blen + info->hp->h_length > info->buflen)
   1081 		goto nospc;
   1082 	hp->h_addr_list[0] = bf;
   1083 	hp->h_addr_list[1] = NULL;
   1084 	(void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
   1085 	if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
   1086 		if (blen + NS_IN6ADDRSZ > info->buflen)
   1087 			goto nospc;
   1088 		map_v4v6_address(bf, bf);
   1089 		hp->h_addrtype = AF_INET6;
   1090 		hp->h_length = NS_IN6ADDRSZ;
   1091 	}
   1092 
   1093 	__res_put_state(res);
   1094 	*info->he = NETDB_SUCCESS;
   1095 	return NS_SUCCESS;
   1096 nospc:
   1097 	*info->he = NETDB_INTERNAL;
   1098 	return NS_UNAVAIL;
   1099 }
   1100 
   1101 #ifdef YP
   1102 /*ARGSUSED*/
   1103 static struct hostent *
   1104 _yp_hostent(char *line, int af, struct getnamaddr *info)
   1105 {
   1106 	struct in6_addr host_addrs[MAXADDRS];
   1107 	char **aliases;
   1108 	size_t maxaliases;
   1109 	char *p = line;
   1110 	char *cp, **q, *ptr;
   1111 	size_t len, anum, i;
   1112 	int addrok;
   1113 	int more;
   1114 	size_t naddrs;
   1115 	struct hostent *hp = info->hp;
   1116 
   1117 	_DIAGASSERT(line != NULL);
   1118 
   1119 	hp->h_name = NULL;
   1120 	hp->h_addrtype = af;
   1121 	switch (af) {
   1122 	case AF_INET:
   1123 		hp->h_length = NS_INADDRSZ;
   1124 		break;
   1125 	case AF_INET6:
   1126 		hp->h_length = NS_IN6ADDRSZ;
   1127 		break;
   1128 	default:
   1129 		return NULL;
   1130 	}
   1131 	setup(aliases, maxaliases);
   1132 	naddrs = 0;
   1133 	q = aliases;
   1134 
   1135 nextline:
   1136 	/* check for host_addrs overflow */
   1137 	if (naddrs >= __arraycount(host_addrs))
   1138 		goto done;
   1139 
   1140 	more = 0;
   1141 	cp = strpbrk(p, " \t");
   1142 	if (cp == NULL)
   1143 		goto done;
   1144 	*cp++ = '\0';
   1145 
   1146 	/* p has should have an address */
   1147 	addrok = inet_pton(af, p, &host_addrs[naddrs]);
   1148 	if (addrok != 1) {
   1149 		/* skip to the next line */
   1150 		while (cp && *cp) {
   1151 			if (*cp == '\n') {
   1152 				cp++;
   1153 				goto nextline;
   1154 			}
   1155 			cp++;
   1156 		}
   1157 		goto done;
   1158 	}
   1159 	naddrs++;
   1160 
   1161 	while (*cp == ' ' || *cp == '\t')
   1162 		cp++;
   1163 	p = cp;
   1164 	cp = strpbrk(p, " \t\n");
   1165 	if (cp != NULL) {
   1166 		if (*cp == '\n')
   1167 			more = 1;
   1168 		*cp++ = '\0';
   1169 	}
   1170 	if (!hp->h_name)
   1171 		hp->h_name = p;
   1172 	else if (strcmp(hp->h_name, p) == 0)
   1173 		;
   1174 	else
   1175 		addalias(q, p, aliases, maxaliases);
   1176 	p = cp;
   1177 	if (more)
   1178 		goto nextline;
   1179 
   1180 	while (cp && *cp) {
   1181 		if (*cp == ' ' || *cp == '\t') {
   1182 			cp++;
   1183 			continue;
   1184 		}
   1185 		if (*cp == '\n') {
   1186 			cp++;
   1187 			goto nextline;
   1188 		}
   1189 		addalias(q, cp, aliases, maxaliases);
   1190 		*q++ = cp;
   1191 		cp = strpbrk(cp, " \t");
   1192 		if (cp != NULL)
   1193 			*cp++ = '\0';
   1194 	}
   1195 
   1196 done:
   1197 	if (hp->h_name == NULL) {
   1198 		free(aliases);
   1199 		return NULL;
   1200 	}
   1201 
   1202 	ptr = info->buf;
   1203 	len = info->buflen;
   1204 
   1205 	anum = (size_t)(q - aliases);
   1206 	HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
   1207 	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
   1208 
   1209 	for (i = 0; i < naddrs; i++)
   1210 		HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
   1211 		    ptr, len);
   1212 	hp->h_addr_list[naddrs] = NULL;
   1213 
   1214 	HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
   1215 
   1216 	for (i = 0; i < anum; i++)
   1217 		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
   1218 	hp->h_aliases[anum] = NULL;
   1219 	free(aliases);
   1220 
   1221 	return hp;
   1222 nospc:
   1223 	free(aliases);
   1224 	*info->he = NETDB_INTERNAL;
   1225 	errno = ENOSPC;
   1226 	return NULL;
   1227 }
   1228 
   1229 /*ARGSUSED*/
   1230 int
   1231 _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
   1232 {
   1233 	struct hostent *hp = NULL;
   1234 	char *ypcurrent;
   1235 	int ypcurrentlen, r;
   1236 	char name[INET6_ADDRSTRLEN];	/* XXX enough? */
   1237 	const unsigned char *uaddr;
   1238 	int af;
   1239 	const char *map;
   1240 	struct getnamaddr *info = rv;
   1241 
   1242 	_DIAGASSERT(rv != NULL);
   1243 
   1244 	uaddr = va_arg(ap, unsigned char *);
   1245 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1246 	af = va_arg(ap, int);
   1247 
   1248 	if (!__ypdomain) {
   1249 		if (_yp_check(&__ypdomain) == 0)
   1250 			return NS_UNAVAIL;
   1251 	}
   1252 	/*
   1253 	 * XXX unfortunately, we cannot support IPv6 extended scoped address
   1254 	 * notation here.  gethostbyaddr() is not scope-aware.  too bad.
   1255 	 */
   1256 	if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
   1257 		return NS_UNAVAIL;
   1258 	switch (af) {
   1259 	case AF_INET:
   1260 		map = "hosts.byaddr";
   1261 		break;
   1262 	default:
   1263 		map = "ipnodes.byaddr";
   1264 		break;
   1265 	}
   1266 	ypcurrent = NULL;
   1267 	r = yp_match(__ypdomain, map, name,
   1268 		(int)strlen(name), &ypcurrent, &ypcurrentlen);
   1269 	if (r == 0)
   1270 		hp = _yp_hostent(ypcurrent, af, info);
   1271 	else
   1272 		hp = NULL;
   1273 	free(ypcurrent);
   1274 	if (hp == NULL) {
   1275 		*info->he = HOST_NOT_FOUND;
   1276 		return NS_NOTFOUND;
   1277 	}
   1278 	return NS_SUCCESS;
   1279 }
   1280 
   1281 /*ARGSUSED*/
   1282 int
   1283 _yp_gethtbyname(void *rv, void *cb_data, va_list ap)
   1284 {
   1285 	struct hostent *hp;
   1286 	char *ypcurrent;
   1287 	int ypcurrentlen, r;
   1288 	const char *name;
   1289 	int af;
   1290 	const char *map;
   1291 	struct getnamaddr *info = rv;
   1292 
   1293 	_DIAGASSERT(rv != NULL);
   1294 
   1295 	name = va_arg(ap, char *);
   1296 	/* NOSTRICT skip string len */(void)va_arg(ap, int);
   1297 	af = va_arg(ap, int);
   1298 
   1299 	if (!__ypdomain) {
   1300 		if (_yp_check(&__ypdomain) == 0)
   1301 			return NS_UNAVAIL;
   1302 	}
   1303 	switch (af) {
   1304 	case AF_INET:
   1305 		map = "hosts.byname";
   1306 		break;
   1307 	default:
   1308 		map = "ipnodes.byname";
   1309 		break;
   1310 	}
   1311 	ypcurrent = NULL;
   1312 	r = yp_match(__ypdomain, map, name,
   1313 		(int)strlen(name), &ypcurrent, &ypcurrentlen);
   1314 	if (r == 0)
   1315 		hp = _yp_hostent(ypcurrent, af, info);
   1316 	else
   1317 		hp = NULL;
   1318 	free(ypcurrent);
   1319 	if (hp == NULL) {
   1320 		*info->he = HOST_NOT_FOUND;
   1321 		return NS_NOTFOUND;
   1322 	}
   1323 	return NS_SUCCESS;
   1324 }
   1325 #endif
   1326 
   1327 /*
   1328  * Non-reentrant versions.
   1329  */
   1330 FILE *_h_file;
   1331 static struct hostent h_ent;
   1332 static char h_buf[16384];
   1333 
   1334 struct hostent *
   1335 gethostbyaddr(const void *addr, socklen_t len, int af) {
   1336 	return gethostbyaddr_r(addr, len, af, &h_ent, h_buf, sizeof(h_buf),
   1337 	    &h_errno);
   1338 }
   1339 
   1340 struct hostent *
   1341 gethostbyname(const char *name) {
   1342 	return gethostbyname_r(name, &h_ent, h_buf, sizeof(h_buf), &h_errno);
   1343 }
   1344 
   1345 struct hostent *
   1346 gethostbyname2(const char *name, int af) {
   1347 	return gethostbyname2_r(name, af, &h_ent, h_buf, sizeof(h_buf),
   1348 	    &h_errno);
   1349 }
   1350 
   1351 struct hostent *
   1352 gethostent(void)
   1353 {
   1354 	if (_h_file == NULL) {
   1355 		sethostent_r(&_h_file);
   1356 		if (_h_file == NULL) {
   1357 			h_errno = NETDB_INTERNAL;
   1358 			return NULL;
   1359 		}
   1360 	}
   1361 	memset(&h_ent, 0, sizeof(h_ent));
   1362 	return gethostent_r(_h_file, &h_ent, h_buf, sizeof(h_buf), &h_errno);
   1363 }
   1364 
   1365