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