Home | History | Annotate | Line # | Download | only in net
gethnamaddr.c revision 1.59
      1 /*	$NetBSD: gethnamaddr.c,v 1.59 2004/05/21 02:30:03 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.59 2004/05/21 02:30:03 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 	_DIAGASSERT(name != NULL);
    516 
    517 	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
    518 		h_errno = NETDB_INTERNAL;
    519 		__res_put_state(res);
    520 		return 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 	hp = gethostbyname_internal(name, af, res);
    540 	__res_put_state(res);
    541 	return hp;
    542 }
    543 
    544 static struct hostent *
    545 gethostbyname_internal(const char *name, int af, res_state res)
    546 {
    547 	const char *cp;
    548 	char *bp, *ep;
    549 	int size;
    550 	struct hostent *hp;
    551 	static const ns_dtab dtab[] = {
    552 		NS_FILES_CB(_gethtbyname, NULL)
    553 		{ NSSRC_DNS, _dns_gethtbyname, NULL },	/* force -DHESIOD */
    554 		NS_NIS_CB(_yp_gethtbyname, NULL)
    555 		{ 0 }
    556 	};
    557 
    558 	_DIAGASSERT(name != NULL);
    559 
    560 	switch (af) {
    561 	case AF_INET:
    562 		size = INADDRSZ;
    563 		break;
    564 	case AF_INET6:
    565 		size = IN6ADDRSZ;
    566 		break;
    567 	default:
    568 		h_errno = NETDB_INTERNAL;
    569 		errno = EAFNOSUPPORT;
    570 		return NULL;
    571 	}
    572 
    573 	host.h_addrtype = af;
    574 	host.h_length = size;
    575 
    576 	/*
    577 	 * if there aren't any dots, it could be a user-level alias.
    578 	 * this is also done in res_nquery() since we are not the only
    579 	 * function that looks up host names.
    580 	 */
    581 	if (!strchr(name, '.') && (cp = __hostalias(name)))
    582 		name = cp;
    583 
    584 	/*
    585 	 * disallow names consisting only of digits/dots, unless
    586 	 * they end in a dot.
    587 	 */
    588 	if (isdigit((u_char) name[0]))
    589 		for (cp = name;; ++cp) {
    590 			if (!*cp) {
    591 				if (*--cp == '.')
    592 					break;
    593 				/*
    594 				 * All-numeric, no dot at the end.
    595 				 * Fake up a hostent as if we'd actually
    596 				 * done a lookup.
    597 				 */
    598 				if (inet_pton(af, name,
    599 				    (char *)(void *)host_addr) <= 0) {
    600 					h_errno = HOST_NOT_FOUND;
    601 					return NULL;
    602 				}
    603 				strncpy(hostbuf, name, MAXDNAME);
    604 				hostbuf[MAXDNAME] = '\0';
    605 				bp = hostbuf + MAXDNAME;
    606 				ep = hostbuf + sizeof hostbuf;
    607 				host.h_name = hostbuf;
    608 				host.h_aliases = host_aliases;
    609 				host_aliases[0] = NULL;
    610 				h_addr_ptrs[0] = (char *)(void *)host_addr;
    611 				h_addr_ptrs[1] = NULL;
    612 				host.h_addr_list = h_addr_ptrs;
    613 				if (res->options & RES_USE_INET6)
    614 					map_v4v6_hostent(&host, &bp, ep);
    615 				h_errno = NETDB_SUCCESS;
    616 				return &host;
    617 			}
    618 			if (!isdigit((u_char) *cp) && *cp != '.')
    619 				break;
    620 		}
    621 	if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
    622 	    name[0] == ':')
    623 		for (cp = name;; ++cp) {
    624 			if (!*cp) {
    625 				if (*--cp == '.')
    626 					break;
    627 				/*
    628 				 * All-IPv6-legal, no dot at the end.
    629 				 * Fake up a hostent as if we'd actually
    630 				 * done a lookup.
    631 				 */
    632 				if (inet_pton(af, name,
    633 				    (char *)(void *)host_addr) <= 0) {
    634 					h_errno = HOST_NOT_FOUND;
    635 					return NULL;
    636 				}
    637 				strncpy(hostbuf, name, MAXDNAME);
    638 				hostbuf[MAXDNAME] = '\0';
    639 				bp = hostbuf + MAXDNAME;
    640 				ep = hostbuf + sizeof hostbuf;
    641 				host.h_name = hostbuf;
    642 				host.h_aliases = host_aliases;
    643 				host_aliases[0] = NULL;
    644 				h_addr_ptrs[0] = (char *)(void *)host_addr;
    645 				h_addr_ptrs[1] = NULL;
    646 				host.h_addr_list = h_addr_ptrs;
    647 				h_errno = NETDB_SUCCESS;
    648 				return &host;
    649 			}
    650 			if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
    651 				break;
    652 		}
    653 
    654 	hp = NULL;
    655 	h_errno = NETDB_INTERNAL;
    656 	if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname",
    657 	    default_dns_files, name, strlen(name), af) != NS_SUCCESS)
    658 		return NULL;
    659 	h_errno = NETDB_SUCCESS;
    660 	return hp;
    661 }
    662 
    663 struct hostent *
    664 gethostbyaddr(const char *addr,	/* XXX should have been def'd as u_char! */
    665     socklen_t len, int af)
    666 {
    667 	const u_char *uaddr = (const u_char *)addr;
    668 	socklen_t size;
    669 	struct hostent *hp;
    670 	static const ns_dtab dtab[] = {
    671 		NS_FILES_CB(_gethtbyaddr, NULL)
    672 		{ NSSRC_DNS, _dns_gethtbyaddr, NULL },	/* force -DHESIOD */
    673 		NS_NIS_CB(_yp_gethtbyaddr, NULL)
    674 		{ 0 }
    675 	};
    676 
    677 	_DIAGASSERT(addr != NULL);
    678 
    679 	if (af == AF_INET6 && len == IN6ADDRSZ &&
    680 	    (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) ||
    681 	     IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) {
    682 		h_errno = HOST_NOT_FOUND;
    683 		return NULL;
    684 	}
    685 	if (af == AF_INET6 && len == IN6ADDRSZ &&
    686 	    (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) ||
    687 	     IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) {
    688 		/* Unmap. */
    689 		addr += IN6ADDRSZ - INADDRSZ;
    690 		uaddr += IN6ADDRSZ - INADDRSZ;
    691 		af = AF_INET;
    692 		len = INADDRSZ;
    693 	}
    694 	switch (af) {
    695 	case AF_INET:
    696 		size = INADDRSZ;
    697 		break;
    698 	case AF_INET6:
    699 		size = IN6ADDRSZ;
    700 		break;
    701 	default:
    702 		errno = EAFNOSUPPORT;
    703 		h_errno = NETDB_INTERNAL;
    704 		return NULL;
    705 	}
    706 	if (size != len) {
    707 		errno = EINVAL;
    708 		h_errno = NETDB_INTERNAL;
    709 		return NULL;
    710 	}
    711 	hp = NULL;
    712 	h_errno = NETDB_INTERNAL;
    713 	if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
    714 	    default_dns_files, uaddr, len, af) != NS_SUCCESS)
    715 		return NULL;
    716 	h_errno = NETDB_SUCCESS;
    717 	return hp;
    718 }
    719 
    720 void
    721 _sethtent(int f)
    722 {
    723 	if (!hostf)
    724 		hostf = fopen(_PATH_HOSTS, "r" );
    725 	else
    726 		rewind(hostf);
    727 	stayopen = f;
    728 }
    729 
    730 void
    731 _endhtent(void)
    732 {
    733 	if (hostf && !stayopen) {
    734 		(void) fclose(hostf);
    735 		hostf = NULL;
    736 	}
    737 }
    738 
    739 struct hostent *
    740 _gethtent(void)
    741 {
    742 	char *p;
    743 	char *cp, **q;
    744 	int af, len;
    745 
    746 	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
    747 		h_errno = NETDB_INTERNAL;
    748 		return NULL;
    749 	}
    750  again:
    751 	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
    752 		h_errno = HOST_NOT_FOUND;
    753 		return NULL;
    754 	}
    755 	if (*p == '#')
    756 		goto again;
    757 	if (!(cp = strpbrk(p, "#\n")))
    758 		goto again;
    759 	*cp = '\0';
    760 	if (!(cp = strpbrk(p, " \t")))
    761 		goto again;
    762 	*cp++ = '\0';
    763 	if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) {
    764 		af = AF_INET6;
    765 		len = IN6ADDRSZ;
    766 	} else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) {
    767 		res_state res = __res_get_state();
    768 		if (res->options & RES_USE_INET6) {
    769 			map_v4v6_address((char *)(void *)host_addr,
    770 			    (char *)(void *)host_addr);
    771 			af = AF_INET6;
    772 			len = IN6ADDRSZ;
    773 		} else {
    774 			af = AF_INET;
    775 			len = INADDRSZ;
    776 		}
    777 		__res_put_state(res);
    778 	} else {
    779 		goto again;
    780 	}
    781 	/* if this is not something we're looking for, skip it. */
    782 	if (host.h_addrtype != af)
    783 		goto again;
    784 	if (host.h_length != len)
    785 		goto again;
    786 	h_addr_ptrs[0] = (char *)(void *)host_addr;
    787 	h_addr_ptrs[1] = NULL;
    788 	host.h_addr_list = h_addr_ptrs;
    789 	host.h_length = len;
    790 	host.h_addrtype = af;
    791 	while (*cp == ' ' || *cp == '\t')
    792 		cp++;
    793 	host.h_name = cp;
    794 	q = host.h_aliases = host_aliases;
    795 	if ((cp = strpbrk(cp, " \t")) != NULL)
    796 		*cp++ = '\0';
    797 	while (cp && *cp) {
    798 		if (*cp == ' ' || *cp == '\t') {
    799 			cp++;
    800 			continue;
    801 		}
    802 		if (q < &host_aliases[MAXALIASES - 1])
    803 			*q++ = cp;
    804 		if ((cp = strpbrk(cp, " \t")) != NULL)
    805 			*cp++ = '\0';
    806 	}
    807 	*q = NULL;
    808 	h_errno = NETDB_SUCCESS;
    809 	return &host;
    810 }
    811 
    812 /*ARGSUSED*/
    813 int
    814 _gethtbyname(void *rv, void *cb_data, va_list ap)
    815 {
    816 	struct hostent *hp;
    817 	const char *name;
    818 	int af;
    819 
    820 	_DIAGASSERT(rv != NULL);
    821 
    822 	name = va_arg(ap, char *);
    823 	/* NOSTRICT skip len */(void)va_arg(ap, int);
    824 	af = va_arg(ap, int);
    825 
    826 	hp = NULL;
    827 #if 0
    828 	{
    829 		res_state res = __res_get_state();
    830 		if (res->options & RES_USE_INET6)
    831 			hp = _gethtbyname2(name, AF_INET6);
    832 		if (hp==NULL)
    833 			hp = _gethtbyname2(name, AF_INET);
    834 		__res_put_state(res);
    835 	}
    836 #else
    837 	hp = _gethtbyname2(name, af);
    838 #endif
    839 	*((struct hostent **)rv) = hp;
    840 	if (hp == NULL) {
    841 		h_errno = HOST_NOT_FOUND;
    842 		return NS_NOTFOUND;
    843 	}
    844 	return NS_SUCCESS;
    845 }
    846 
    847 struct hostent *
    848 _gethtbyname2(const char *name, int af)
    849 {
    850 	struct hostent *p;
    851 	char *tmpbuf, *ptr, **cp;
    852 	int num;
    853 	size_t len;
    854 
    855 	_DIAGASSERT(name != NULL);
    856 
    857 	_sethtent(0);
    858 	ptr = tmpbuf = NULL;
    859 	num = 0;
    860 	while ((p = _gethtent()) != NULL && num < MAXADDRS) {
    861 		if (p->h_addrtype != af)
    862 			continue;
    863 		if (strcasecmp(p->h_name, name) != 0) {
    864 			for (cp = p->h_aliases; *cp != NULL; cp++)
    865 				if (strcasecmp(*cp, name) == 0)
    866 					break;
    867 			if (*cp == NULL) continue;
    868 		}
    869 
    870 		if (num == 0) {
    871 			size_t bufsize;
    872 			char *src;
    873 
    874 			bufsize = strlen(p->h_name) + 2 +
    875 				  MAXADDRS * p->h_length +
    876 				  ALIGNBYTES;
    877 			for (cp = p->h_aliases; *cp != NULL; cp++)
    878 				bufsize += strlen(*cp) + 1;
    879 
    880 			if ((tmpbuf = malloc(bufsize)) == NULL) {
    881 				h_errno = NETDB_INTERNAL;
    882 				return NULL;
    883 			}
    884 
    885 			ptr = tmpbuf;
    886 			src = p->h_name;
    887 			while ((*ptr++ = *src++) != '\0');
    888 			for (cp = p->h_aliases; *cp != NULL; cp++) {
    889 				src = *cp;
    890 				while ((*ptr++ = *src++) != '\0');
    891 			}
    892 			*ptr++ = '\0';
    893 
    894 			ptr = (char *)(void *)ALIGN(ptr);
    895 		}
    896 
    897 		(void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
    898 		ptr += p->h_length;
    899 		num++;
    900 	}
    901 	_endhtent();
    902 	if (num == 0) return NULL;
    903 
    904 	len = ptr - tmpbuf;
    905 	if (len > (sizeof(hostbuf) - ALIGNBYTES)) {
    906 		free(tmpbuf);
    907 		errno = ENOSPC;
    908 		h_errno = NETDB_INTERNAL;
    909 		return NULL;
    910 	}
    911 	ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len);
    912 	free(tmpbuf);
    913 
    914 	host.h_name = ptr;
    915 	while (*ptr++);
    916 
    917 	cp = host_aliases;
    918 	while (*ptr) {
    919 		*cp++ = ptr;
    920 		while (*ptr++);
    921 	}
    922 	ptr++;
    923 	*cp = NULL;
    924 
    925 	ptr = (char *)(void *)ALIGN(ptr);
    926 	cp = h_addr_ptrs;
    927 	while (num--) {
    928 		*cp++ = ptr;
    929 		ptr += host.h_length;
    930 	}
    931 	*cp = NULL;
    932 
    933 	return &host;
    934 }
    935 
    936 /*ARGSUSED*/
    937 int
    938 _gethtbyaddr(void *rv, void *cb_data, va_list ap)
    939 {
    940 	struct hostent *p;
    941 	const unsigned char *addr;
    942 	int len, af;
    943 
    944 	_DIAGASSERT(rv != NULL);
    945 
    946 	addr = va_arg(ap, unsigned char *);
    947 	len = va_arg(ap, int);
    948 	af = va_arg(ap, int);
    949 
    950 	host.h_length = len;
    951 	host.h_addrtype = af;
    952 
    953 	_sethtent(0);
    954 	while ((p = _gethtent()) != NULL)
    955 		if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
    956 		    (size_t)len))
    957 			break;
    958 	_endhtent();
    959 	*((struct hostent **)rv) = p;
    960 	if (p==NULL) {
    961 		h_errno = HOST_NOT_FOUND;
    962 		return NS_NOTFOUND;
    963 	}
    964 	return NS_SUCCESS;
    965 }
    966 
    967 static void
    968 map_v4v6_address(const char *src, char *dst)
    969 {
    970 	u_char *p = (u_char *)dst;
    971 	char tmp[INADDRSZ];
    972 	int i;
    973 
    974 	_DIAGASSERT(src != NULL);
    975 	_DIAGASSERT(dst != NULL);
    976 
    977 	/* Stash a temporary copy so our caller can update in place. */
    978 	(void)memcpy(tmp, src, INADDRSZ);
    979 	/* Mark this ipv6 addr as a mapped ipv4. */
    980 	for (i = 0; i < 10; i++)
    981 		*p++ = 0x00;
    982 	*p++ = 0xff;
    983 	*p++ = 0xff;
    984 	/* Retrieve the saved copy and we're done. */
    985 	(void)memcpy((void *)p, tmp, INADDRSZ);
    986 }
    987 
    988 static void
    989 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
    990 {
    991 	char **ap;
    992 
    993 	_DIAGASSERT(hp != NULL);
    994 	_DIAGASSERT(bpp != NULL);
    995 	_DIAGASSERT(ep != NULL);
    996 
    997 	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
    998 		return;
    999 	hp->h_addrtype = AF_INET6;
   1000 	hp->h_length = IN6ADDRSZ;
   1001 	for (ap = hp->h_addr_list; *ap; ap++) {
   1002 		int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
   1003 
   1004 		if (ep - *bpp < (i + IN6ADDRSZ)) {
   1005 			/* Out of memory.  Truncate address list here.  XXX */
   1006 			*ap = NULL;
   1007 			return;
   1008 		}
   1009 		*bpp += i;
   1010 		map_v4v6_address(*ap, *bpp);
   1011 		*ap = *bpp;
   1012 		*bpp += IN6ADDRSZ;
   1013 	}
   1014 }
   1015 
   1016 #ifdef RESOLVSORT
   1017 static void
   1018 addrsort(char **ap, int num, res_state res)
   1019 {
   1020 	int i, j;
   1021 	char **p;
   1022 	short aval[MAXADDRS];
   1023 	int needsort = 0;
   1024 
   1025 	_DIAGASSERT(ap != NULL);
   1026 
   1027 	p = ap;
   1028 	for (i = 0; i < num; i++, p++) {
   1029 	    for (j = 0 ; (unsigned)j < res->nsort; j++)
   1030 		if (res->sort_list[j].addr.s_addr ==
   1031 		    (((struct in_addr *)(void *)(*p))->s_addr &
   1032 		    res->sort_list[j].mask))
   1033 			break;
   1034 	    aval[i] = j;
   1035 	    if (needsort == 0 && i > 0 && j < aval[i-1])
   1036 		needsort = i;
   1037 	}
   1038 	if (!needsort)
   1039 	    return;
   1040 
   1041 	while (needsort < num) {
   1042 	    for (j = needsort - 1; j >= 0; j--) {
   1043 		if (aval[j] > aval[j+1]) {
   1044 		    char *hp;
   1045 
   1046 		    i = aval[j];
   1047 		    aval[j] = aval[j+1];
   1048 		    aval[j+1] = i;
   1049 
   1050 		    hp = ap[j];
   1051 		    ap[j] = ap[j+1];
   1052 		    ap[j+1] = hp;
   1053 		} else
   1054 		    break;
   1055 	    }
   1056 	    needsort++;
   1057 	}
   1058 }
   1059 #endif
   1060 
   1061 #if defined(BSD43_BSD43_NFS) || defined(sun)
   1062 /* XXX: should we remove this cruft? - lukem */
   1063 /* some libc's out there are bound internally to these names (UMIPS) */
   1064 void
   1065 ht_sethostent(int stayopen)
   1066 {
   1067 	_sethtent(stayopen);
   1068 }
   1069 
   1070 void
   1071 ht_endhostent(void)
   1072 {
   1073 	_endhtent();
   1074 }
   1075 
   1076 struct hostent *
   1077 ht_gethostbyname(char *name)
   1078 {
   1079 	return _gethtbyname(name);
   1080 }
   1081 
   1082 struct hostent *
   1083 ht_gethostbyaddr(const char *addr, int len, int af)
   1084 {
   1085 	return _gethtbyaddr(addr, len, af);
   1086 }
   1087 
   1088 struct hostent *
   1089 gethostent(void)
   1090 {
   1091 	return _gethtent();
   1092 }
   1093 
   1094 void
   1095 dns_service(void)
   1096 {
   1097 	return;
   1098 }
   1099 
   1100 int
   1101 dn_skipname(const u_char *comp_dn, const u_char *eom)
   1102 {
   1103 	return __dn_skipname(comp_dn, eom);
   1104 }
   1105 #endif /*old-style libc with yp junk in it*/
   1106 
   1107 /*ARGSUSED*/
   1108 int
   1109 _dns_gethtbyname(rv, cb_data, ap)
   1110 	void	*rv;
   1111 	void	*cb_data;
   1112 	va_list	 ap;
   1113 {
   1114 	querybuf *buf;
   1115 	int n, type;
   1116 	struct hostent *hp;
   1117 	const char *name;
   1118 	int af;
   1119 	res_state res;
   1120 
   1121 	_DIAGASSERT(rv != NULL);
   1122 
   1123 	name = va_arg(ap, char *);
   1124 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1125 	af = va_arg(ap, int);
   1126 
   1127 	switch (af) {
   1128 	case AF_INET:
   1129 		type = T_A;
   1130 		break;
   1131 	case AF_INET6:
   1132 		type = T_AAAA;
   1133 		break;
   1134 	default:
   1135 		return NS_UNAVAIL;
   1136 	}
   1137 	buf = malloc(sizeof(*buf));
   1138 	if (buf == NULL) {
   1139 		h_errno = NETDB_INTERNAL;
   1140 		return NS_NOTFOUND;
   1141 	}
   1142 	res = __res_get_state();
   1143 	n = res_nsearch(res, name, C_IN, type, buf->buf, sizeof(buf->buf));
   1144 	if (n < 0) {
   1145 		free(buf);
   1146 		dprintf("res_nsearch failed (%d)\n", res, n);
   1147 		__res_put_state(res);
   1148 		return NS_NOTFOUND;
   1149 	}
   1150 	hp = getanswer(buf, n, name, type, res);
   1151 	free(buf);
   1152 	__res_put_state(res);
   1153 	if (hp == NULL)
   1154 		switch (h_errno) {
   1155 		case HOST_NOT_FOUND:
   1156 			return NS_NOTFOUND;
   1157 		case TRY_AGAIN:
   1158 			return NS_TRYAGAIN;
   1159 		default:
   1160 			return NS_UNAVAIL;
   1161 		}
   1162 	*((struct hostent **)rv) = hp;
   1163 	return NS_SUCCESS;
   1164 }
   1165 
   1166 /*ARGSUSED*/
   1167 int
   1168 _dns_gethtbyaddr(void *rv, void	*cb_data, va_list ap)
   1169 {
   1170 	char qbuf[MAXDNAME + 1], *qp, *ep;
   1171 	int n;
   1172 	querybuf *buf;
   1173 	struct hostent *hp;
   1174 	const unsigned char *uaddr;
   1175 	int len, af, advance;
   1176 	res_state res;
   1177 
   1178 	_DIAGASSERT(rv != NULL);
   1179 
   1180 	uaddr = va_arg(ap, unsigned char *);
   1181 	len = va_arg(ap, int);
   1182 	af = va_arg(ap, int);
   1183 
   1184 	switch (af) {
   1185 	case AF_INET:
   1186 		(void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
   1187 		    (uaddr[3] & 0xff), (uaddr[2] & 0xff),
   1188 		    (uaddr[1] & 0xff), (uaddr[0] & 0xff));
   1189 		break;
   1190 
   1191 	case AF_INET6:
   1192 		qp = qbuf;
   1193 		ep = qbuf + sizeof(qbuf) - 1;
   1194 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
   1195 			advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
   1196 			    uaddr[n] & 0xf,
   1197 			    ((unsigned int)uaddr[n] >> 4) & 0xf);
   1198 			if (advance > 0 && qp + advance < ep)
   1199 				qp += advance;
   1200 			else {
   1201 				h_errno = NETDB_INTERNAL;
   1202 				return NS_NOTFOUND;
   1203 			}
   1204 		}
   1205 		if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
   1206 			h_errno = NETDB_INTERNAL;
   1207 			return NS_NOTFOUND;
   1208 		}
   1209 		break;
   1210 	default:
   1211 		abort();
   1212 	}
   1213 
   1214 	buf = malloc(sizeof(*buf));
   1215 	if (buf == NULL) {
   1216 		h_errno = NETDB_INTERNAL;
   1217 		return NS_NOTFOUND;
   1218 	}
   1219 	res = __res_get_state();
   1220 	n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
   1221 	if (n < 0 && af == AF_INET6) {
   1222 		*qp = '\0';
   1223 		if (strlcat(qbuf, "ip6.int", sizeof(qbuf)) >= sizeof(qbuf)) {
   1224 			free(buf);
   1225 			h_errno = NETDB_INTERNAL;
   1226 			__res_put_state(res);
   1227 			return NS_NOTFOUND;
   1228 		}
   1229 		n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
   1230 	}
   1231 	if (n < 0) {
   1232 		free(buf);
   1233 		dprintf("res_nquery failed (%d)\n", res, n);
   1234 		__res_put_state(res);
   1235 		return NS_NOTFOUND;
   1236 	}
   1237 	hp = getanswer(buf, n, qbuf, T_PTR, res);
   1238 	free(buf);
   1239 	if (hp == NULL) {
   1240 		__res_put_state(res);
   1241 		switch (h_errno) {
   1242 		case HOST_NOT_FOUND:
   1243 			return NS_NOTFOUND;
   1244 		case TRY_AGAIN:
   1245 			return NS_TRYAGAIN;
   1246 		default:
   1247 			return NS_UNAVAIL;
   1248 		}
   1249 	}
   1250 	hp->h_addrtype = af;
   1251 	hp->h_length = len;
   1252 	(void)memcpy(host_addr, uaddr, (size_t)len);
   1253 	h_addr_ptrs[0] = (char *)(void *)host_addr;
   1254 	h_addr_ptrs[1] = NULL;
   1255 	if (af == AF_INET && (res->options & RES_USE_INET6)) {
   1256 		map_v4v6_address((char *)(void *)host_addr,
   1257 		    (char *)(void *)host_addr);
   1258 		hp->h_addrtype = AF_INET6;
   1259 		hp->h_length = IN6ADDRSZ;
   1260 	}
   1261 
   1262 	__res_put_state(res);
   1263 	*((struct hostent **)rv) = hp;
   1264 	h_errno = NETDB_SUCCESS;
   1265 	return NS_SUCCESS;
   1266 }
   1267 
   1268 #ifdef YP
   1269 /*ARGSUSED*/
   1270 struct hostent *
   1271 _yphostent(char *line, int af)
   1272 {
   1273 	static struct in_addr host_addrs[MAXADDRS];
   1274 	static struct in6_addr host6_addrs[MAXADDRS];
   1275 	char *p = line;
   1276 	char *cp, **q;
   1277 	char **hap;
   1278 	int addrok;
   1279 	int more;
   1280 	size_t naddrs;
   1281 
   1282 	_DIAGASSERT(line != NULL);
   1283 
   1284 	host.h_name = NULL;
   1285 	host.h_addr_list = h_addr_ptrs;
   1286 	host.h_addrtype = af;
   1287 	switch (af) {
   1288 	case AF_INET:
   1289 		host.h_length = INADDRSZ;
   1290 		break;
   1291 	case AF_INET6:
   1292 		host.h_length = IN6ADDRSZ;
   1293 		break;
   1294 	default:
   1295 		return NULL;
   1296 	}
   1297 	hap = h_addr_ptrs;
   1298 	q = host.h_aliases = host_aliases;
   1299 	naddrs = 0;
   1300 
   1301 nextline:
   1302 	/* check for host_addrs overflow */
   1303 	if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0]))
   1304 		goto done;
   1305 	if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0]))
   1306 		goto done;
   1307 
   1308 	more = 0;
   1309 	cp = strpbrk(p, " \t");
   1310 	if (cp == NULL)
   1311 		goto done;
   1312 	*cp++ = '\0';
   1313 
   1314 	/* p has should have an address */
   1315 	switch (af) {
   1316 	case AF_INET:
   1317 		addrok = inet_aton(p, &host_addrs[naddrs]);
   1318 		break;
   1319 	case AF_INET6:
   1320 		addrok = inet_pton(af, p, &host6_addrs[naddrs]);
   1321 		break;
   1322 	}
   1323 	if (addrok != 1) {
   1324 		/* skip to the next line */
   1325 		while (cp && *cp) {
   1326 			if (*cp == '\n') {
   1327 				cp++;
   1328 				goto nextline;
   1329 			}
   1330 			cp++;
   1331 		}
   1332 
   1333 		goto done;
   1334 	}
   1335 
   1336 	switch (af) {
   1337 	case AF_INET:
   1338 		*hap++ = (char *)(void *)&host_addrs[naddrs++];
   1339 		break;
   1340 	case AF_INET6:
   1341 		*hap++ = (char *)(void *)&host6_addrs[naddrs++];
   1342 		break;
   1343 	}
   1344 
   1345 	while (*cp == ' ' || *cp == '\t')
   1346 		cp++;
   1347 	p = cp;
   1348 	cp = strpbrk(p, " \t\n");
   1349 	if (cp != NULL) {
   1350 		if (*cp == '\n')
   1351 			more = 1;
   1352 		*cp++ = '\0';
   1353 	}
   1354 	if (!host.h_name)
   1355 		host.h_name = p;
   1356 	else if (strcmp(host.h_name, p)==0)
   1357 		;
   1358 	else if (q < &host_aliases[MAXALIASES - 1])
   1359 		*q++ = p;
   1360 	p = cp;
   1361 	if (more)
   1362 		goto nextline;
   1363 
   1364 	while (cp && *cp) {
   1365 		if (*cp == ' ' || *cp == '\t') {
   1366 			cp++;
   1367 			continue;
   1368 		}
   1369 		if (*cp == '\n') {
   1370 			cp++;
   1371 			goto nextline;
   1372 		}
   1373 		if (q < &host_aliases[MAXALIASES - 1])
   1374 			*q++ = cp;
   1375 		cp = strpbrk(cp, " \t");
   1376 		if (cp != NULL)
   1377 			*cp++ = '\0';
   1378 	}
   1379 
   1380 done:
   1381 	if (host.h_name == NULL)
   1382 		return NULL;
   1383 	*q = NULL;
   1384 	*hap = NULL;
   1385 	return &host;
   1386 }
   1387 
   1388 /*ARGSUSED*/
   1389 int
   1390 _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
   1391 {
   1392 	struct hostent *hp = NULL;
   1393 	static char *__ypcurrent;
   1394 	int __ypcurrentlen, r;
   1395 	char name[INET6_ADDRSTRLEN];	/* XXX enough? */
   1396 	const unsigned char *uaddr;
   1397 	int af;
   1398 	const char *map;
   1399 
   1400 	_DIAGASSERT(rv != NULL);
   1401 
   1402 	uaddr = va_arg(ap, unsigned char *);
   1403 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1404 	af = va_arg(ap, int);
   1405 
   1406 	if (!__ypdomain) {
   1407 		if (_yp_check(&__ypdomain) == 0)
   1408 			return NS_UNAVAIL;
   1409 	}
   1410 	/*
   1411 	 * XXX unfortunately, we cannot support IPv6 extended scoped address
   1412 	 * notation here.  gethostbyaddr() is not scope-aware.  too bad.
   1413 	 */
   1414 	if (inet_ntop(af, uaddr, name, sizeof(name)) == NULL)
   1415 		return NS_UNAVAIL;
   1416 	if (__ypcurrent)
   1417 		free(__ypcurrent);
   1418 	__ypcurrent = NULL;
   1419 	switch (af) {
   1420 	case AF_INET:
   1421 		map = "hosts.byaddr";
   1422 		break;
   1423 	default:
   1424 		map = "ipnodes.byaddr";
   1425 		break;
   1426 	}
   1427 	r = yp_match(__ypdomain, map, name,
   1428 		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
   1429 	if (r == 0)
   1430 		hp = _yphostent(__ypcurrent, af);
   1431 	if (hp == NULL) {
   1432 		h_errno = HOST_NOT_FOUND;
   1433 		return NS_NOTFOUND;
   1434 	}
   1435 	*((struct hostent **)rv) = hp;
   1436 	return NS_SUCCESS;
   1437 }
   1438 
   1439 /*ARGSUSED*/
   1440 int
   1441 _yp_gethtbyname(void *rv, void *cb_data, va_list ap)
   1442 {
   1443 	struct hostent *hp = NULL;
   1444 	static char *__ypcurrent;
   1445 	int __ypcurrentlen, r;
   1446 	const char *name;
   1447 	int af;
   1448 	const char *map;
   1449 
   1450 	_DIAGASSERT(rv != NULL);
   1451 
   1452 	name = va_arg(ap, char *);
   1453 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1454 	af = va_arg(ap, int);
   1455 
   1456 	if (!__ypdomain) {
   1457 		if (_yp_check(&__ypdomain) == 0)
   1458 			return NS_UNAVAIL;
   1459 	}
   1460 	if (__ypcurrent)
   1461 		free(__ypcurrent);
   1462 	__ypcurrent = NULL;
   1463 	switch (af) {
   1464 	case AF_INET:
   1465 		map = "hosts.byname";
   1466 		break;
   1467 	default:
   1468 		map = "ipnodes.byname";
   1469 		break;
   1470 	}
   1471 	r = yp_match(__ypdomain, map, name,
   1472 		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
   1473 	if (r == 0)
   1474 		hp = _yphostent(__ypcurrent, af);
   1475 	if (hp == NULL) {
   1476 		h_errno = HOST_NOT_FOUND;
   1477 		return NS_NOTFOUND;
   1478 	}
   1479 	*((struct hostent **)rv) = hp;
   1480 	return NS_SUCCESS;
   1481 }
   1482 #endif
   1483