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