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