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