Home | History | Annotate | Line # | Download | only in net
gethnamaddr.c revision 1.33.4.3
      1 /*	$NetBSD: gethnamaddr.c,v 1.33.4.3 2002/06/26 16:30:27 he 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.33.4.3 2002/06/26 16:30:27 he 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 extern int h_errno;
    149 
    150 #ifdef DEBUG
    151 static void dprintf __P((char *, int));
    152 #endif
    153 static struct hostent *getanswer __P((const querybuf *, int,
    154     const char *, int));
    155 static void map_v4v6_address __P((const char *, char *));
    156 static void map_v4v6_hostent __P((struct hostent *, char **, char *));
    157 #ifdef RESOLVSORT
    158 static void addrsort __P((char **, int));
    159 #endif
    160 
    161 struct hostent *gethostbyname __P((const char *));
    162 struct hostent *gethostbyname2 __P((const char *, int));
    163 struct hostent *gethostbyaddr __P((const char *, int, int ));
    164 void _sethtent __P((int));
    165 void _endhtent __P((void));
    166 struct hostent *_gethtent __P((void));
    167 struct hostent *_gethtbyname2 __P((const char *, int));
    168 void ht_sethostent __P((int));
    169 void ht_endhostent __P((void));
    170 struct hostent *ht_gethostbyname __P((char *));
    171 struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
    172 struct hostent *gethostent __P((void));
    173 void dns_service __P((void));
    174 int dn_skipname __P((const u_char *, const u_char *));
    175 int _gethtbyaddr __P((void *, void *, va_list));
    176 int _gethtbyname __P((void *, void *, va_list));
    177 int _dns_gethtbyaddr __P((void *, void *, va_list));
    178 int _dns_gethtbyname __P((void *, void *, va_list));
    179 #ifdef YP
    180 struct hostent *_yphostent __P((char *, int));
    181 int _yp_gethtbyaddr __P((void *, void *, va_list));
    182 int _yp_gethtbyname __P((void *, void *, va_list));
    183 #endif
    184 
    185 static const ns_src default_dns_files[] = {
    186 	{ NSSRC_FILES, 	NS_SUCCESS },
    187 	{ NSSRC_DNS, 	NS_SUCCESS },
    188 	{ 0 }
    189 };
    190 
    191 
    192 #ifdef DEBUG
    193 static void
    194 dprintf(msg, num)
    195 	char *msg;
    196 	int num;
    197 {
    198 
    199 	_DIAGASSERT(msg != NULL);
    200 
    201 	if (_res.options & RES_DEBUG) {
    202 		int save = errno;
    203 
    204 		printf(msg, num);
    205 		errno = save;
    206 	}
    207 }
    208 #else
    209 # define dprintf(msg, num) /*nada*/
    210 #endif
    211 
    212 #define BOUNDED_INCR(x) \
    213 	do { \
    214 		cp += x; \
    215 		if (cp > eom) { \
    216 			h_errno = NO_RECOVERY; \
    217 			return (NULL); \
    218 		} \
    219 	} while (0)
    220 
    221 #define BOUNDS_CHECK(ptr, count) \
    222 	do { \
    223 		if ((ptr) + (count) > eom) { \
    224 			h_errno = NO_RECOVERY; \
    225 			return (NULL); \
    226 		} \
    227 	} while (0)
    228 
    229 static struct hostent *
    230 getanswer(answer, anslen, qname, qtype)
    231 	const querybuf *answer;
    232 	int anslen;
    233 	const char *qname;
    234 	int qtype;
    235 {
    236 	const HEADER *hp;
    237 	const u_char *cp;
    238 	int n;
    239 	const u_char *eom, *erdata;
    240 	char *bp, **ap, **hap, *ep;
    241 	int type, class, ancount, qdcount;
    242 	int haveanswer, had_error;
    243 	int toobig = 0;
    244 	char tbuf[MAXDNAME];
    245 	const char *tname;
    246 	int (*name_ok) __P((const char *));
    247 
    248 	_DIAGASSERT(answer != NULL);
    249 	_DIAGASSERT(qname != NULL);
    250 
    251 	tname = qname;
    252 	host.h_name = NULL;
    253 	eom = answer->buf + anslen;
    254 	switch (qtype) {
    255 	case T_A:
    256 	case T_AAAA:
    257 		name_ok = res_hnok;
    258 		break;
    259 	case T_PTR:
    260 		name_ok = res_dnok;
    261 		break;
    262 	default:
    263 		return (NULL);	/* XXX should be abort(); */
    264 	}
    265 	/*
    266 	 * find first satisfactory answer
    267 	 */
    268 	hp = &answer->hdr;
    269 	ancount = ntohs(hp->ancount);
    270 	qdcount = ntohs(hp->qdcount);
    271 	bp = hostbuf;
    272 	ep = hostbuf + sizeof hostbuf;
    273 	cp = answer->buf;
    274 	BOUNDED_INCR(HFIXEDSZ);
    275 	if (qdcount != 1) {
    276 		h_errno = NO_RECOVERY;
    277 		return (NULL);
    278 	}
    279 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    280 	if ((n < 0) || !(*name_ok)(bp)) {
    281 		h_errno = NO_RECOVERY;
    282 		return (NULL);
    283 	}
    284 	BOUNDED_INCR(n + QFIXEDSZ);
    285 	if (qtype == T_A || qtype == T_AAAA) {
    286 		/* res_send() has already verified that the query name is the
    287 		 * same as the one we sent; this just gets the expanded name
    288 		 * (i.e., with the succeeding search-domain tacked on).
    289 		 */
    290 		n = strlen(bp) + 1;		/* for the \0 */
    291 		if (n >= MAXHOSTNAMELEN) {
    292 			h_errno = NO_RECOVERY;
    293 			return (NULL);
    294 		}
    295 		host.h_name = bp;
    296 		bp += n;
    297 		/* The qname can be abbreviated, but h_name is now absolute. */
    298 		qname = host.h_name;
    299 	}
    300 	ap = host_aliases;
    301 	*ap = NULL;
    302 	host.h_aliases = host_aliases;
    303 	hap = h_addr_ptrs;
    304 	*hap = NULL;
    305 	host.h_addr_list = h_addr_ptrs;
    306 	haveanswer = 0;
    307 	had_error = 0;
    308 	while (ancount-- > 0 && cp < eom && !had_error) {
    309 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    310 		if ((n < 0) || !(*name_ok)(bp)) {
    311 			had_error++;
    312 			continue;
    313 		}
    314 		cp += n;			/* name */
    315 		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
    316 		type = _getshort(cp);
    317  		cp += INT16SZ;			/* type */
    318 		class = _getshort(cp);
    319  		cp += INT16SZ + INT32SZ;	/* class, TTL */
    320 		n = _getshort(cp);
    321 		cp += INT16SZ;			/* len */
    322 		BOUNDS_CHECK(cp, n);
    323 		erdata = cp + n;
    324 		if (class != C_IN) {
    325 			/* XXX - debug? syslog? */
    326 			cp += n;
    327 			continue;		/* XXX - had_error++ ? */
    328 		}
    329 		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
    330 			if (ap >= &host_aliases[MAXALIASES-1])
    331 				continue;
    332 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
    333 			if ((n < 0) || !(*name_ok)(tbuf)) {
    334 				had_error++;
    335 				continue;
    336 			}
    337 			cp += n;
    338 			if (cp != erdata) {
    339 				h_errno = NO_RECOVERY;
    340 				return (NULL);
    341 			}
    342 			/* Store alias. */
    343 			*ap++ = bp;
    344 			n = strlen(bp) + 1;	/* for the \0 */
    345 			if (n >= MAXHOSTNAMELEN) {
    346 				had_error++;
    347 				continue;
    348 			}
    349 			bp += n;
    350 			/* Get canonical name. */
    351 			n = strlen(tbuf) + 1;	/* for the \0 */
    352 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
    353 				had_error++;
    354 				continue;
    355 			}
    356 			strcpy(bp, tbuf);
    357 			host.h_name = bp;
    358 			bp += n;
    359 			continue;
    360 		}
    361 		if (qtype == T_PTR && type == T_CNAME) {
    362 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
    363 			if (n < 0 || !res_dnok(tbuf)) {
    364 				had_error++;
    365 				continue;
    366 			}
    367 			cp += n;
    368 			if (cp != erdata) {
    369 				h_errno = NO_RECOVERY;
    370 				return (NULL);
    371 			}
    372 			/* Get canonical name. */
    373 			n = strlen(tbuf) + 1;	/* for the \0 */
    374 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
    375 				had_error++;
    376 				continue;
    377 			}
    378 			strcpy(bp, tbuf);
    379 			tname = bp;
    380 			bp += n;
    381 			continue;
    382 		}
    383 		if (type != qtype) {
    384 			if (type != T_KEY && type != T_SIG)
    385 				syslog(LOG_NOTICE|LOG_AUTH,
    386 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
    387 				       qname, p_class(C_IN), p_type(qtype),
    388 				       p_type(type));
    389 			cp += n;
    390 			continue;		/* XXX - had_error++ ? */
    391 		}
    392 		switch (type) {
    393 		case T_PTR:
    394 			if (strcasecmp(tname, bp) != 0) {
    395 				syslog(LOG_NOTICE|LOG_AUTH,
    396 				       AskedForGot, qname, bp);
    397 				cp += n;
    398 				continue;	/* XXX - had_error++ ? */
    399 			}
    400 			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
    401 			if ((n < 0) || !res_hnok(bp)) {
    402 				had_error++;
    403 				break;
    404 			}
    405 #if MULTI_PTRS_ARE_ALIASES
    406 			cp += n;
    407 			if (cp != erdata) {
    408 				h_errno = NO_RECOVERY;
    409 				return (NULL);
    410 			}
    411 			if (!haveanswer)
    412 				host.h_name = bp;
    413 			else if (ap < &host_aliases[MAXALIASES-1])
    414 				*ap++ = bp;
    415 			else
    416 				n = -1;
    417 			if (n != -1) {
    418 				n = strlen(bp) + 1;	/* for the \0 */
    419 				if (n >= MAXHOSTNAMELEN) {
    420 					had_error++;
    421 					break;
    422 				}
    423 				bp += n;
    424 			}
    425 			break;
    426 #else
    427 			host.h_name = bp;
    428 			if (_res.options & RES_USE_INET6) {
    429 				n = strlen(bp) + 1;	/* for the \0 */
    430 				if (n >= MAXHOSTNAMELEN) {
    431 					had_error++;
    432 					break;
    433 				}
    434 				bp += n;
    435 				map_v4v6_hostent(&host, &bp, ep);
    436 			}
    437 			h_errno = NETDB_SUCCESS;
    438 			return (&host);
    439 #endif
    440 		case T_A:
    441 		case T_AAAA:
    442 			if (strcasecmp(host.h_name, bp) != 0) {
    443 				syslog(LOG_NOTICE|LOG_AUTH,
    444 				       AskedForGot, host.h_name, bp);
    445 				cp += n;
    446 				continue;	/* XXX - had_error++ ? */
    447 			}
    448 			if (n != host.h_length) {
    449 				cp += n;
    450 				continue;
    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(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(*cp) && *cp != '.')
    616 				break;
    617 		}
    618 	if ((isxdigit(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(*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 	int len, af;
    664 {
    665 	const u_char *uaddr = (const u_char *)addr;
    666 	int size;
    667 	struct hostent *hp;
    668 	static const ns_dtab dtab[] = {
    669 		NS_FILES_CB(_gethtbyaddr, NULL)
    670 		{ NSSRC_DNS, _dns_gethtbyaddr, NULL },	/* force -DHESIOD */
    671 		NS_NIS_CB(_yp_gethtbyaddr, NULL)
    672 		{ 0 }
    673 	};
    674 
    675 	_DIAGASSERT(addr != NULL);
    676 
    677 	if (af == AF_INET6 && len == IN6ADDRSZ &&
    678 	    (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) ||
    679 	     IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) {
    680 		/* Unmap. */
    681 		addr += IN6ADDRSZ - INADDRSZ;
    682 		uaddr += IN6ADDRSZ - INADDRSZ;
    683 		af = AF_INET;
    684 		len = INADDRSZ;
    685 	}
    686 	switch (af) {
    687 	case AF_INET:
    688 		size = INADDRSZ;
    689 		break;
    690 	case AF_INET6:
    691 		size = IN6ADDRSZ;
    692 		break;
    693 	default:
    694 		errno = EAFNOSUPPORT;
    695 		h_errno = NETDB_INTERNAL;
    696 		return (NULL);
    697 	}
    698 	if (size != len) {
    699 		errno = EINVAL;
    700 		h_errno = NETDB_INTERNAL;
    701 		return (NULL);
    702 	}
    703 	hp = (struct hostent *)NULL;
    704 	h_errno = NETDB_INTERNAL;
    705 	if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
    706 	    default_dns_files, uaddr, len, af) != NS_SUCCESS)
    707 		return (struct hostent *)NULL;
    708 	h_errno = NETDB_SUCCESS;
    709 	return (hp);
    710 }
    711 
    712 void
    713 _sethtent(f)
    714 	int f;
    715 {
    716 	if (!hostf)
    717 		hostf = fopen(_PATH_HOSTS, "r" );
    718 	else
    719 		rewind(hostf);
    720 	stayopen = f;
    721 }
    722 
    723 void
    724 _endhtent()
    725 {
    726 	if (hostf && !stayopen) {
    727 		(void) fclose(hostf);
    728 		hostf = NULL;
    729 	}
    730 }
    731 
    732 struct hostent *
    733 _gethtent()
    734 {
    735 	char *p;
    736 	char *cp, **q;
    737 	int af, len;
    738 
    739 	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
    740 		h_errno = NETDB_INTERNAL;
    741 		return (NULL);
    742 	}
    743  again:
    744 	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
    745 		h_errno = HOST_NOT_FOUND;
    746 		return (NULL);
    747 	}
    748 	if (*p == '#')
    749 		goto again;
    750 	if (!(cp = strpbrk(p, "#\n")))
    751 		goto again;
    752 	*cp = '\0';
    753 	if (!(cp = strpbrk(p, " \t")))
    754 		goto again;
    755 	*cp++ = '\0';
    756 	if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) {
    757 		af = AF_INET6;
    758 		len = IN6ADDRSZ;
    759 	} else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) {
    760 		if (_res.options & RES_USE_INET6) {
    761 			map_v4v6_address((char *)(void *)host_addr,
    762 			    (char *)(void *)host_addr);
    763 			af = AF_INET6;
    764 			len = IN6ADDRSZ;
    765 		} else {
    766 			af = AF_INET;
    767 			len = INADDRSZ;
    768 		}
    769 	} else {
    770 		goto again;
    771 	}
    772 	/* if this is not something we're looking for, skip it. */
    773 	if (host.h_addrtype != af)
    774 		goto again;
    775 	if (host.h_length != len)
    776 		goto again;
    777 	h_addr_ptrs[0] = (char *)(void *)host_addr;
    778 	h_addr_ptrs[1] = NULL;
    779 	host.h_addr_list = h_addr_ptrs;
    780 	host.h_length = len;
    781 	host.h_addrtype = af;
    782 	while (*cp == ' ' || *cp == '\t')
    783 		cp++;
    784 	host.h_name = cp;
    785 	q = host.h_aliases = host_aliases;
    786 	if ((cp = strpbrk(cp, " \t")) != NULL)
    787 		*cp++ = '\0';
    788 	while (cp && *cp) {
    789 		if (*cp == ' ' || *cp == '\t') {
    790 			cp++;
    791 			continue;
    792 		}
    793 		if (q < &host_aliases[MAXALIASES - 1])
    794 			*q++ = cp;
    795 		if ((cp = strpbrk(cp, " \t")) != NULL)
    796 			*cp++ = '\0';
    797 	}
    798 	*q = NULL;
    799 	h_errno = NETDB_SUCCESS;
    800 	return (&host);
    801 }
    802 
    803 /*ARGSUSED*/
    804 int
    805 _gethtbyname(rv, cb_data, ap)
    806 	void	*rv;
    807 	void	*cb_data;
    808 	va_list	 ap;
    809 {
    810 	struct hostent *hp;
    811 	const char *name;
    812 	int af;
    813 
    814 	_DIAGASSERT(rv != NULL);
    815 
    816 	name = va_arg(ap, char *);
    817 	/* NOSTRICT skip len */(void)va_arg(ap, int);
    818 	af = va_arg(ap, int);
    819 
    820 	hp = NULL;
    821 #if 0
    822 	if (_res.options & RES_USE_INET6)
    823 		hp = _gethtbyname2(name, AF_INET6);
    824 	if (hp==NULL)
    825 		hp = _gethtbyname2(name, AF_INET);
    826 #else
    827 	hp = _gethtbyname2(name, af);
    828 #endif
    829 	*((struct hostent **)rv) = hp;
    830 	if (hp==NULL) {
    831 		h_errno = HOST_NOT_FOUND;
    832 		return NS_NOTFOUND;
    833 	}
    834 	return NS_SUCCESS;
    835 }
    836 
    837 struct hostent *
    838 _gethtbyname2(name, af)
    839 	const char *name;
    840 	int af;
    841 {
    842 	struct hostent *p;
    843 	char *tmpbuf, *ptr, **cp;
    844 	int num;
    845 	size_t len;
    846 
    847 	_DIAGASSERT(name != NULL);
    848 
    849 	_sethtent(0);
    850 	ptr = tmpbuf = NULL;
    851 	num = 0;
    852 	while ((p = _gethtent()) != NULL && num < MAXADDRS) {
    853 		if (p->h_addrtype != af)
    854 			continue;
    855 		if (strcasecmp(p->h_name, name) != 0) {
    856 			for (cp = p->h_aliases; *cp != NULL; cp++)
    857 				if (strcasecmp(*cp, name) == 0)
    858 					break;
    859 			if (*cp == NULL) continue;
    860 		}
    861 
    862 		if (num == 0) {
    863 			size_t bufsize;
    864 			char *src;
    865 
    866 			bufsize = strlen(p->h_name) + 2 +
    867 				  MAXADDRS * p->h_length +
    868 				  ALIGNBYTES;
    869 			for (cp = p->h_aliases; *cp != NULL; cp++)
    870 				bufsize += strlen(*cp) + 1;
    871 
    872 			if ((tmpbuf = malloc(bufsize)) == NULL) {
    873 				h_errno = NETDB_INTERNAL;
    874 				return NULL;
    875 			}
    876 
    877 			ptr = tmpbuf;
    878 			src = p->h_name;
    879 			while ((*ptr++ = *src++) != '\0');
    880 			for (cp = p->h_aliases; *cp != NULL; cp++) {
    881 				src = *cp;
    882 				while ((*ptr++ = *src++) != '\0');
    883 			}
    884 			*ptr++ = '\0';
    885 
    886 			ptr = (char *)(void *)ALIGN(ptr);
    887 		}
    888 
    889 		(void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
    890 		ptr += p->h_length;
    891 		num++;
    892 	}
    893 	_endhtent();
    894 	if (num == 0) return NULL;
    895 
    896 	len = ptr - tmpbuf;
    897 	if (len > (sizeof(hostbuf) - ALIGNBYTES)) {
    898 		free(tmpbuf);
    899 		errno = ENOSPC;
    900 		h_errno = NETDB_INTERNAL;
    901 		return NULL;
    902 	}
    903 	ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len);
    904 	free(tmpbuf);
    905 
    906 	host.h_name = ptr;
    907 	while (*ptr++);
    908 
    909 	cp = host_aliases;
    910 	while (*ptr) {
    911 		*cp++ = ptr;
    912 		while (*ptr++);
    913 	}
    914 	ptr++;
    915 	*cp = NULL;
    916 
    917 	ptr = (char *)(void *)ALIGN(ptr);
    918 	cp = h_addr_ptrs;
    919 	while (num--) {
    920 		*cp++ = ptr;
    921 		ptr += host.h_length;
    922 	}
    923 	*cp = NULL;
    924 
    925 	return (&host);
    926 }
    927 
    928 /*ARGSUSED*/
    929 int
    930 _gethtbyaddr(rv, cb_data, ap)
    931 	void	*rv;
    932 	void	*cb_data;
    933 	va_list	 ap;
    934 {
    935 	struct hostent *p;
    936 	const unsigned char *addr;
    937 	int len, af;
    938 
    939 	_DIAGASSERT(rv != NULL);
    940 
    941 	addr = va_arg(ap, unsigned char *);
    942 	len = va_arg(ap, int);
    943 	af = va_arg(ap, int);
    944 
    945 	host.h_length = len;
    946 	host.h_addrtype = af;
    947 
    948 	_sethtent(0);
    949 	while ((p = _gethtent()) != NULL)
    950 		if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
    951 		    (size_t)len))
    952 			break;
    953 	_endhtent();
    954 	*((struct hostent **)rv) = p;
    955 	if (p==NULL) {
    956 		h_errno = HOST_NOT_FOUND;
    957 		return NS_NOTFOUND;
    958 	}
    959 	return NS_SUCCESS;
    960 }
    961 
    962 static void
    963 map_v4v6_address(src, dst)
    964 	const char *src;
    965 	char *dst;
    966 {
    967 	u_char *p = (u_char *)dst;
    968 	char tmp[INADDRSZ];
    969 	int i;
    970 
    971 	_DIAGASSERT(src != NULL);
    972 	_DIAGASSERT(dst != NULL);
    973 
    974 	/* Stash a temporary copy so our caller can update in place. */
    975 	(void)memcpy(tmp, src, INADDRSZ);
    976 	/* Mark this ipv6 addr as a mapped ipv4. */
    977 	for (i = 0; i < 10; i++)
    978 		*p++ = 0x00;
    979 	*p++ = 0xff;
    980 	*p++ = 0xff;
    981 	/* Retrieve the saved copy and we're done. */
    982 	(void)memcpy((void *)p, tmp, INADDRSZ);
    983 }
    984 
    985 static void
    986 map_v4v6_hostent(hp, bpp, ep)
    987 	struct hostent *hp;
    988 	char **bpp;
    989 	char *ep;
    990 {
    991 	char **ap;
    992 
    993 	_DIAGASSERT(hp != NULL);
    994 	_DIAGASSERT(bpp != NULL);
    995 	_DIAGASSERT(ep != NULL);
    996 
    997 	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
    998 		return;
    999 	hp->h_addrtype = AF_INET6;
   1000 	hp->h_length = IN6ADDRSZ;
   1001 	for (ap = hp->h_addr_list; *ap; ap++) {
   1002 		int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
   1003 
   1004 		if (ep - *bpp < (i + IN6ADDRSZ)) {
   1005 			/* Out of memory.  Truncate address list here.  XXX */
   1006 			*ap = NULL;
   1007 			return;
   1008 		}
   1009 		*bpp += i;
   1010 		map_v4v6_address(*ap, *bpp);
   1011 		*ap = *bpp;
   1012 		*bpp += IN6ADDRSZ;
   1013 	}
   1014 }
   1015 
   1016 #ifdef RESOLVSORT
   1017 static void
   1018 addrsort(ap, num)
   1019 	char **ap;
   1020 	int num;
   1021 {
   1022 	int i, j;
   1023 	char **p;
   1024 	short aval[MAXADDRS];
   1025 	int needsort = 0;
   1026 
   1027 	_DIAGASSERT(ap != NULL);
   1028 
   1029 	p = ap;
   1030 	for (i = 0; i < num; i++, p++) {
   1031 	    for (j = 0 ; (unsigned)j < _res.nsort; j++)
   1032 		if (_res.sort_list[j].addr.s_addr ==
   1033 		    (((struct in_addr *)(void *)(*p))->s_addr &
   1034 		    _res.sort_list[j].mask))
   1035 			break;
   1036 	    aval[i] = j;
   1037 	    if (needsort == 0 && i > 0 && j < aval[i-1])
   1038 		needsort = i;
   1039 	}
   1040 	if (!needsort)
   1041 	    return;
   1042 
   1043 	while (needsort < num) {
   1044 	    for (j = needsort - 1; j >= 0; j--) {
   1045 		if (aval[j] > aval[j+1]) {
   1046 		    char *hp;
   1047 
   1048 		    i = aval[j];
   1049 		    aval[j] = aval[j+1];
   1050 		    aval[j+1] = i;
   1051 
   1052 		    hp = ap[j];
   1053 		    ap[j] = ap[j+1];
   1054 		    ap[j+1] = hp;
   1055 
   1056 		} else
   1057 		    break;
   1058 	    }
   1059 	    needsort++;
   1060 	}
   1061 }
   1062 #endif
   1063 
   1064 #if defined(BSD43_BSD43_NFS) || defined(sun)
   1065 /* XXX: should we remove this cruft? - lukem */
   1066 /* some libc's out there are bound internally to these names (UMIPS) */
   1067 void
   1068 ht_sethostent(stayopen)
   1069 	int stayopen;
   1070 {
   1071 	_sethtent(stayopen);
   1072 }
   1073 
   1074 void
   1075 ht_endhostent()
   1076 {
   1077 	_endhtent();
   1078 }
   1079 
   1080 struct hostent *
   1081 ht_gethostbyname(name)
   1082 	char *name;
   1083 {
   1084 	return (_gethtbyname(name));
   1085 }
   1086 
   1087 struct hostent *
   1088 ht_gethostbyaddr(addr, len, af)
   1089 	const char *addr;
   1090 	int len, af;
   1091 {
   1092 	return (_gethtbyaddr(addr, len, af));
   1093 }
   1094 
   1095 struct hostent *
   1096 gethostent()
   1097 {
   1098 	return (_gethtent());
   1099 }
   1100 
   1101 void
   1102 dns_service()
   1103 {
   1104 	return;
   1105 }
   1106 
   1107 #undef dn_skipname
   1108 dn_skipname(comp_dn, eom)
   1109 	const u_char *comp_dn, *eom;
   1110 {
   1111 	return (__dn_skipname(comp_dn, eom));
   1112 }
   1113 #endif /*old-style libc with yp junk in it*/
   1114 
   1115 /*ARGSUSED*/
   1116 int
   1117 _dns_gethtbyname(rv, cb_data, ap)
   1118 	void	*rv;
   1119 	void	*cb_data;
   1120 	va_list	 ap;
   1121 {
   1122 	querybuf buf;
   1123 	int n, type;
   1124 	struct hostent *hp;
   1125 	const char *name;
   1126 	int af;
   1127 
   1128 	_DIAGASSERT(rv != NULL);
   1129 
   1130 	name = va_arg(ap, char *);
   1131 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1132 	af = va_arg(ap, int);
   1133 
   1134 	switch (af) {
   1135 	case AF_INET:
   1136 		type = T_A;
   1137 		break;
   1138 	case AF_INET6:
   1139 		type = T_AAAA;
   1140 		break;
   1141 	default:
   1142 		return NS_UNAVAIL;
   1143 	}
   1144 	if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
   1145 		dprintf("res_search failed (%d)\n", n);
   1146 		return NS_NOTFOUND;
   1147 	}
   1148 	hp = getanswer(&buf, n, name, type);
   1149 	if (hp == NULL)
   1150 		switch (h_errno) {
   1151 		case HOST_NOT_FOUND:
   1152 			return NS_NOTFOUND;
   1153 		case TRY_AGAIN:
   1154 			return NS_TRYAGAIN;
   1155 		default:
   1156 			return NS_UNAVAIL;
   1157 		}
   1158 	*((struct hostent **)rv) = hp;
   1159 	return NS_SUCCESS;
   1160 }
   1161 
   1162 /*ARGSUSED*/
   1163 int
   1164 _dns_gethtbyaddr(rv, cb_data, ap)
   1165 	void	*rv;
   1166 	void	*cb_data;
   1167 	va_list	 ap;
   1168 {
   1169 	char qbuf[MAXDNAME + 1], *qp;
   1170 	int n;
   1171 	querybuf buf;
   1172 	struct hostent *hp;
   1173 	const unsigned char *uaddr;
   1174 	int len, af;
   1175 
   1176 	_DIAGASSERT(rv != NULL);
   1177 
   1178 	uaddr = va_arg(ap, unsigned char *);
   1179 	len = va_arg(ap, int);
   1180 	af = va_arg(ap, int);
   1181 
   1182 	switch (af) {
   1183 	case AF_INET:
   1184 		(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
   1185 			(uaddr[3] & 0xff),
   1186 			(uaddr[2] & 0xff),
   1187 			(uaddr[1] & 0xff),
   1188 			(uaddr[0] & 0xff));
   1189 		break;
   1190 
   1191 	case AF_INET6:
   1192 		qp = qbuf;
   1193 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
   1194 			qp += sprintf(qp, "%x.%x.",
   1195 				       uaddr[n] & 0xf,
   1196 				       ((unsigned int)uaddr[n] >> 4) & 0xf);
   1197 		}
   1198 		strcpy(qp, "ip6.int");
   1199 		break;
   1200 	default:
   1201 		abort();
   1202 	}
   1203 
   1204 	n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
   1205 	if (n < 0) {
   1206 		dprintf("res_query failed (%d)\n", n);
   1207 		return NS_NOTFOUND;
   1208 	}
   1209 	hp = getanswer(&buf, n, qbuf, T_PTR);
   1210 	if (hp == NULL)
   1211 		switch (h_errno) {
   1212 		case HOST_NOT_FOUND:
   1213 			return NS_NOTFOUND;
   1214 		case TRY_AGAIN:
   1215 			return NS_TRYAGAIN;
   1216 		default:
   1217 			return NS_UNAVAIL;
   1218 		}
   1219 	hp->h_addrtype = af;
   1220 	hp->h_length = len;
   1221 	(void)memcpy(host_addr, uaddr, (size_t)len);
   1222 	h_addr_ptrs[0] = (char *)(void *)host_addr;
   1223 	h_addr_ptrs[1] = NULL;
   1224 	if (af == AF_INET && (_res.options & RES_USE_INET6)) {
   1225 		map_v4v6_address((char *)(void *)host_addr,
   1226 		    (char *)(void *)host_addr);
   1227 		hp->h_addrtype = AF_INET6;
   1228 		hp->h_length = IN6ADDRSZ;
   1229 	}
   1230 
   1231 	*((struct hostent **)rv) = hp;
   1232 	h_errno = NETDB_SUCCESS;
   1233 	return NS_SUCCESS;
   1234 }
   1235 
   1236 #ifdef YP
   1237 /*ARGSUSED*/
   1238 struct hostent *
   1239 _yphostent(line, af)
   1240 	char *line;
   1241 	int af;
   1242 {
   1243 	static struct in_addr host_addrs[MAXADDRS];
   1244 	char *p = line;
   1245 	char *cp, **q;
   1246 	char **hap;
   1247 	struct in_addr *buf;
   1248 	int more;
   1249 
   1250 	_DIAGASSERT(line != NULL);
   1251 
   1252 	host.h_name = NULL;
   1253 	host.h_addr_list = h_addr_ptrs;
   1254 	host.h_length = sizeof(u_int32_t);
   1255 	host.h_addrtype = AF_INET;
   1256 	hap = h_addr_ptrs;
   1257 	buf = host_addrs;
   1258 	q = host.h_aliases = host_aliases;
   1259 
   1260 	/*
   1261 	 * XXX: maybe support IPv6 parsing, based on 'af' setting
   1262 	 */
   1263 nextline:
   1264 	/* check for host_addrs overflow */
   1265 	if (buf >= &host_addrs[sizeof(host_addrs) / sizeof(host_addrs[0])])
   1266 		goto done;
   1267 
   1268 	more = 0;
   1269 	cp = strpbrk(p, " \t");
   1270 	if (cp == NULL)
   1271 		goto done;
   1272 	*cp++ = '\0';
   1273 
   1274 	*hap++ = (char *)(void *)buf;
   1275 	(void) inet_aton(p, buf++);
   1276 
   1277 	while (*cp == ' ' || *cp == '\t')
   1278 		cp++;
   1279 	p = cp;
   1280 	cp = strpbrk(p, " \t\n");
   1281 	if (cp != NULL) {
   1282 		if (*cp == '\n')
   1283 			more = 1;
   1284 		*cp++ = '\0';
   1285 	}
   1286 	if (!host.h_name)
   1287 		host.h_name = p;
   1288 	else if (strcmp(host.h_name, p)==0)
   1289 		;
   1290 	else if (q < &host_aliases[MAXALIASES - 1])
   1291 		*q++ = p;
   1292 	p = cp;
   1293 	if (more)
   1294 		goto nextline;
   1295 
   1296 	while (cp && *cp) {
   1297 		if (*cp == ' ' || *cp == '\t') {
   1298 			cp++;
   1299 			continue;
   1300 		}
   1301 		if (*cp == '\n') {
   1302 			cp++;
   1303 			goto nextline;
   1304 		}
   1305 		if (q < &host_aliases[MAXALIASES - 1])
   1306 			*q++ = cp;
   1307 		cp = strpbrk(cp, " \t");
   1308 		if (cp != NULL)
   1309 			*cp++ = '\0';
   1310 	}
   1311 done:
   1312 	if (host.h_name == NULL)
   1313 		return (NULL);
   1314 	*q = NULL;
   1315 	*hap = NULL;
   1316 	return (&host);
   1317 }
   1318 
   1319 /*ARGSUSED*/
   1320 int
   1321 _yp_gethtbyaddr(rv, cb_data, ap)
   1322 	void	*rv;
   1323 	void	*cb_data;
   1324 	va_list	 ap;
   1325 {
   1326 	struct hostent *hp = (struct hostent *)NULL;
   1327 	static char *__ypcurrent;
   1328 	int __ypcurrentlen, r;
   1329 	char name[sizeof("xxx.xxx.xxx.xxx") + 1];
   1330 	const unsigned char *uaddr;
   1331 	int af;
   1332 
   1333 	_DIAGASSERT(rv != NULL);
   1334 
   1335 	uaddr = va_arg(ap, unsigned char *);
   1336 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1337 	af = va_arg(ap, int);
   1338 
   1339 	if (!__ypdomain) {
   1340 		if (_yp_check(&__ypdomain) == 0)
   1341 			return NS_UNAVAIL;
   1342 	}
   1343 	/*
   1344 	 * XXX: based on the value of af, it would be possible to lookup
   1345 	 *	IPv6 names in YP, by changing the following snprintf().
   1346 	 *	Is it worth it?
   1347 	 */
   1348 	(void)snprintf(name, sizeof name, "%u.%u.%u.%u",
   1349 		(uaddr[0] & 0xff),
   1350 		(uaddr[1] & 0xff),
   1351 		(uaddr[2] & 0xff),
   1352 		(uaddr[3] & 0xff));
   1353 	if (__ypcurrent)
   1354 		free(__ypcurrent);
   1355 	__ypcurrent = NULL;
   1356 	r = yp_match(__ypdomain, "hosts.byaddr", name,
   1357 		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
   1358 	if (r==0)
   1359 		hp = _yphostent(__ypcurrent, af);
   1360 	if (hp==NULL) {
   1361 		h_errno = HOST_NOT_FOUND;
   1362 		return NS_NOTFOUND;
   1363 	}
   1364 	*((struct hostent **)rv) = hp;
   1365 	return NS_SUCCESS;
   1366 }
   1367 
   1368 /*ARGSUSED*/
   1369 int
   1370 _yp_gethtbyname(rv, cb_data, ap)
   1371 	void	*rv;
   1372 	void	*cb_data;
   1373 	va_list	 ap;
   1374 {
   1375 	struct hostent *hp = (struct hostent *)NULL;
   1376 	static char *__ypcurrent;
   1377 	int __ypcurrentlen, r;
   1378 	const char *name;
   1379 	int af;
   1380 
   1381 	_DIAGASSERT(rv != NULL);
   1382 
   1383 	name = va_arg(ap, char *);
   1384 	/* NOSTRICT skip len */(void)va_arg(ap, int);
   1385 	af = va_arg(ap, int);
   1386 
   1387 	if (!__ypdomain) {
   1388 		if (_yp_check(&__ypdomain) == 0)
   1389 			return NS_UNAVAIL;
   1390 	}
   1391 	if (__ypcurrent)
   1392 		free(__ypcurrent);
   1393 	__ypcurrent = NULL;
   1394 	r = yp_match(__ypdomain, "hosts.byname", name,
   1395 		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
   1396 	if (r==0)
   1397 		hp = _yphostent(__ypcurrent, af);
   1398 	if (hp==NULL) {
   1399 		h_errno = HOST_NOT_FOUND;
   1400 		return NS_NOTFOUND;
   1401 	}
   1402 	*((struct hostent **)rv) = hp;
   1403 	return NS_SUCCESS;
   1404 }
   1405 #endif
   1406