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