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