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