Home | History | Annotate | Line # | Download | only in net
gethnamaddr.c revision 1.2
      1 /*	$NetBSD: gethnamaddr.c,v 1.2 1997/07/20 13:33:18 mrg 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.2 1997/07/20 13:33:18 mrg Exp $");
     65 #endif
     66 #endif /* LIBC_SCCS and not lint */
     67 
     68 #include <sys/types.h>
     69 #include <sys/param.h>
     70 #include <sys/socket.h>
     71 #include <netinet/in.h>
     72 #include <arpa/inet.h>
     73 #include <arpa/nameser.h>
     74 
     75 #include <stdio.h>
     76 #include <netdb.h>
     77 #include <resolv.h>
     78 #include <ctype.h>
     79 #include <errno.h>
     80 #include <syslog.h>
     81 
     82 #ifndef LOG_AUTH
     83 # define LOG_AUTH 0
     84 #endif
     85 
     86 #define MULTI_PTRS_ARE_ALIASES 1	/* XXX - experimental */
     87 
     88 #include <stdlib.h>
     89 #include <string.h>
     90 
     91 #ifdef YP
     92 #include <rpc/rpc.h>
     93 #include <rpcsvc/yp_prot.h>
     94 #include <rpcsvc/ypclnt.h>
     95 #endif
     96 
     97 #define	MAXALIASES	35
     98 #define	MAXADDRS	35
     99 
    100 static const char AskedForGot[] =
    101 			  "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
    102 
    103 static char *h_addr_ptrs[MAXADDRS + 1];
    104 
    105 #ifdef YP
    106 static char *__ypdomain;
    107 #endif
    108 
    109 static struct hostent host;
    110 static char *host_aliases[MAXALIASES];
    111 static char hostbuf[8*1024];
    112 static u_char host_addr[16];	/* IPv4 or IPv6 */
    113 static FILE *hostf = NULL;
    114 static int stayopen = 0;
    115 
    116 
    117 #if PACKETSZ > 1024
    118 #define	MAXPACKET	PACKETSZ
    119 #else
    120 #define	MAXPACKET	1024
    121 #endif
    122 
    123 typedef union {
    124     HEADER hdr;
    125     u_char buf[MAXPACKET];
    126 } querybuf;
    127 
    128 typedef union {
    129     int32_t al;
    130     char ac;
    131 } align;
    132 
    133 extern int h_errno;
    134 
    135 #ifdef DEBUG
    136 static void dprintf __P((char *, int));
    137 #endif
    138 static struct hostent *getanswer __P((const querybuf *, int,
    139     const char *, int));
    140 static void map_v4v6_address __P((const char *, char *));
    141 static void map_v4v6_hostent __P((struct hostent *, char **, int *));
    142 #ifdef RESOLVSORT
    143 static void addrsort __P((char **, int));
    144 #endif
    145 
    146 struct hostent *gethostbyname __P((const char *));
    147 struct hostent *gethostbyname2 __P((const char *, int));
    148 struct hostent *gethostbyaddr __P((const char *, int, int ));
    149 void _sethtent __P((int));
    150 void _endhtent __P((void));
    151 struct hostent *_gethtent __P((void));
    152 struct hostent *_gethtbyname __P((const char *));
    153 struct hostent *_gethtbyname2 __P((const char *, int));
    154 struct hostent *_gethtbyaddr __P((const char *, int, int ));
    155 void ht_sethostent __P((int));
    156 void ht_endhostent __P((void));
    157 struct hostent *ht_gethostbyname __P((char *));
    158 struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
    159 struct hostent *gethostent __P((void));
    160 void dns_service __P((void));
    161 int dn_skipname __P((const u_char *, const u_char *));
    162 #ifdef YP
    163 struct hostent *_yphostent __P((char *));
    164 struct hostent *_yp_gethtbyaddr __P((const char *, int, int ));
    165 struct hostent *_yp_gethtbyname __P((const char *));
    166 #endif
    167 
    168 
    169 #ifdef DEBUG
    170 static void
    171 dprintf(msg, num)
    172 	char *msg;
    173 	int num;
    174 {
    175 	if (_res.options & RES_DEBUG) {
    176 		int save = errno;
    177 
    178 		printf(msg, num);
    179 		errno = save;
    180 	}
    181 }
    182 #else
    183 # define dprintf(msg, num) /*nada*/
    184 #endif
    185 
    186 static struct hostent *
    187 getanswer(answer, anslen, qname, qtype)
    188 	const querybuf *answer;
    189 	int anslen;
    190 	const char *qname;
    191 	int qtype;
    192 {
    193 	register const HEADER *hp;
    194 	register const u_char *cp;
    195 	register int n;
    196 	const u_char *eom;
    197 	char *bp, **ap, **hap;
    198 	int type, class, buflen, ancount, qdcount;
    199 	int haveanswer, had_error;
    200 	int toobig = 0;
    201 	char tbuf[MAXDNAME];
    202 	const char *tname;
    203 	int (*name_ok) __P((const char *));
    204 
    205 	tname = qname;
    206 	host.h_name = NULL;
    207 	eom = answer->buf + anslen;
    208 	switch (qtype) {
    209 	case T_A:
    210 	case T_AAAA:
    211 		name_ok = res_hnok;
    212 		break;
    213 	case T_PTR:
    214 		name_ok = res_dnok;
    215 		break;
    216 	default:
    217 		return (NULL);	/* XXX should be abort(); */
    218 	}
    219 	/*
    220 	 * find first satisfactory answer
    221 	 */
    222 	hp = &answer->hdr;
    223 	ancount = ntohs(hp->ancount);
    224 	qdcount = ntohs(hp->qdcount);
    225 	bp = hostbuf;
    226 	buflen = sizeof hostbuf;
    227 	cp = answer->buf + HFIXEDSZ;
    228 	if (qdcount != 1) {
    229 		h_errno = NO_RECOVERY;
    230 		return (NULL);
    231 	}
    232 	n = dn_expand(answer->buf, eom, cp, bp, buflen);
    233 	if ((n < 0) || !(*name_ok)(bp)) {
    234 		h_errno = NO_RECOVERY;
    235 		return (NULL);
    236 	}
    237 	cp += n + QFIXEDSZ;
    238 	if (qtype == T_A || qtype == T_AAAA) {
    239 		/* res_send() has already verified that the query name is the
    240 		 * same as the one we sent; this just gets the expanded name
    241 		 * (i.e., with the succeeding search-domain tacked on).
    242 		 */
    243 		n = strlen(bp) + 1;		/* for the \0 */
    244 		if (n >= MAXHOSTNAMELEN) {
    245 			h_errno = NO_RECOVERY;
    246 			return (NULL);
    247 		}
    248 		host.h_name = bp;
    249 		bp += n;
    250 		buflen -= n;
    251 		/* The qname can be abbreviated, but h_name is now absolute. */
    252 		qname = host.h_name;
    253 	}
    254 	ap = host_aliases;
    255 	*ap = NULL;
    256 	host.h_aliases = host_aliases;
    257 	hap = h_addr_ptrs;
    258 	*hap = NULL;
    259 	host.h_addr_list = h_addr_ptrs;
    260 	haveanswer = 0;
    261 	had_error = 0;
    262 	while (ancount-- > 0 && cp < eom && !had_error) {
    263 		n = dn_expand(answer->buf, eom, cp, bp, buflen);
    264 		if ((n < 0) || !(*name_ok)(bp)) {
    265 			had_error++;
    266 			continue;
    267 		}
    268 		cp += n;			/* name */
    269 		type = _getshort(cp);
    270  		cp += INT16SZ;			/* type */
    271 		class = _getshort(cp);
    272  		cp += INT16SZ + INT32SZ;	/* class, TTL */
    273 		n = _getshort(cp);
    274 		cp += INT16SZ;			/* len */
    275 		if (class != C_IN) {
    276 			/* XXX - debug? syslog? */
    277 			cp += n;
    278 			continue;		/* XXX - had_error++ ? */
    279 		}
    280 		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
    281 			if (ap >= &host_aliases[MAXALIASES-1])
    282 				continue;
    283 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
    284 			if ((n < 0) || !(*name_ok)(tbuf)) {
    285 				had_error++;
    286 				continue;
    287 			}
    288 			cp += n;
    289 			/* Store alias. */
    290 			*ap++ = bp;
    291 			n = strlen(bp) + 1;	/* for the \0 */
    292 			if (n >= MAXHOSTNAMELEN) {
    293 				had_error++;
    294 				continue;
    295 			}
    296 			bp += n;
    297 			buflen -= n;
    298 			/* Get canonical name. */
    299 			n = strlen(tbuf) + 1;	/* for the \0 */
    300 			if (n > buflen || n >= MAXHOSTNAMELEN) {
    301 				had_error++;
    302 				continue;
    303 			}
    304 			strcpy(bp, tbuf);
    305 			host.h_name = bp;
    306 			bp += n;
    307 			buflen -= n;
    308 			continue;
    309 		}
    310 		if (qtype == T_PTR && type == T_CNAME) {
    311 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
    312 			if (n < 0 || !res_dnok(tbuf)) {
    313 				had_error++;
    314 				continue;
    315 			}
    316 			cp += n;
    317 			/* Get canonical name. */
    318 			n = strlen(tbuf) + 1;	/* for the \0 */
    319 			if (n > buflen || n >= MAXHOSTNAMELEN) {
    320 				had_error++;
    321 				continue;
    322 			}
    323 			strcpy(bp, tbuf);
    324 			tname = bp;
    325 			bp += n;
    326 			buflen -= n;
    327 			continue;
    328 		}
    329 		if (type != qtype) {
    330 			syslog(LOG_NOTICE|LOG_AUTH,
    331 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
    332 			       qname, p_class(C_IN), p_type(qtype),
    333 			       p_type(type));
    334 			cp += n;
    335 			continue;		/* XXX - had_error++ ? */
    336 		}
    337 		switch (type) {
    338 		case T_PTR:
    339 			if (strcasecmp(tname, bp) != 0) {
    340 				syslog(LOG_NOTICE|LOG_AUTH,
    341 				       AskedForGot, qname, bp);
    342 				cp += n;
    343 				continue;	/* XXX - had_error++ ? */
    344 			}
    345 			n = dn_expand(answer->buf, eom, cp, bp, buflen);
    346 			if ((n < 0) || !res_hnok(bp)) {
    347 				had_error++;
    348 				break;
    349 			}
    350 #if MULTI_PTRS_ARE_ALIASES
    351 			cp += n;
    352 			if (!haveanswer)
    353 				host.h_name = bp;
    354 			else if (ap < &host_aliases[MAXALIASES-1])
    355 				*ap++ = bp;
    356 			else
    357 				n = -1;
    358 			if (n != -1) {
    359 				n = strlen(bp) + 1;	/* for the \0 */
    360 				if (n >= MAXHOSTNAMELEN) {
    361 					had_error++;
    362 					break;
    363 				}
    364 				bp += n;
    365 				buflen -= n;
    366 			}
    367 			break;
    368 #else
    369 			host.h_name = bp;
    370 			if (_res.options & RES_USE_INET6) {
    371 				n = strlen(bp) + 1;	/* for the \0 */
    372 				if (n >= MAXHOSTNAMELEN) {
    373 					had_error++;
    374 					break;
    375 				}
    376 				bp += n;
    377 				buflen -= n;
    378 				map_v4v6_hostent(&host, &bp, &buflen);
    379 			}
    380 			h_errno = NETDB_SUCCESS;
    381 			return (&host);
    382 #endif
    383 		case T_A:
    384 		case T_AAAA:
    385 			if (strcasecmp(host.h_name, bp) != 0) {
    386 				syslog(LOG_NOTICE|LOG_AUTH,
    387 				       AskedForGot, host.h_name, bp);
    388 				cp += n;
    389 				continue;	/* XXX - had_error++ ? */
    390 			}
    391 			if (n != host.h_length) {
    392 				cp += n;
    393 				continue;
    394 			}
    395 			if (!haveanswer) {
    396 				register int nn;
    397 
    398 				host.h_name = bp;
    399 				nn = strlen(bp) + 1;	/* for the \0 */
    400 				bp += nn;
    401 				buflen -= nn;
    402 			}
    403 
    404 			bp += sizeof(align) - ((u_long)bp % sizeof(align));
    405 
    406 			if (bp + n >= &hostbuf[sizeof hostbuf]) {
    407 				dprintf("size (%d) too big\n", n);
    408 				had_error++;
    409 				continue;
    410 			}
    411 			if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
    412 				if (!toobig++)
    413 					dprintf("Too many addresses (%d)\n",
    414 						MAXADDRS);
    415 				cp += n;
    416 				continue;
    417 			}
    418 			bcopy(cp, *hap++ = bp, n);
    419 			bp += n;
    420 			buflen -= n;
    421 			cp += n;
    422 			break;
    423 		default:
    424 			abort();
    425 		}
    426 		if (!had_error)
    427 			haveanswer++;
    428 	}
    429 	if (haveanswer) {
    430 		*ap = NULL;
    431 		*hap = NULL;
    432 # if defined(RESOLVSORT)
    433 		/*
    434 		 * Note: we sort even if host can take only one address
    435 		 * in its return structures - should give it the "best"
    436 		 * address in that case, not some random one
    437 		 */
    438 		if (_res.nsort && haveanswer > 1 && qtype == T_A)
    439 			addrsort(h_addr_ptrs, haveanswer);
    440 # endif /*RESOLVSORT*/
    441 		if (!host.h_name) {
    442 			n = strlen(qname) + 1;	/* for the \0 */
    443 			if (n > buflen || n >= MAXHOSTNAMELEN)
    444 				goto no_recovery;
    445 			strcpy(bp, qname);
    446 			host.h_name = bp;
    447 			bp += n;
    448 			buflen -= n;
    449 		}
    450 		if (_res.options & RES_USE_INET6)
    451 			map_v4v6_hostent(&host, &bp, &buflen);
    452 		h_errno = NETDB_SUCCESS;
    453 		return (&host);
    454 	}
    455  no_recovery:
    456 	h_errno = NO_RECOVERY;
    457 	return (NULL);
    458 }
    459 
    460 struct hostent *
    461 gethostbyname(name)
    462 	const char *name;
    463 {
    464 	struct hostent *hp;
    465 
    466 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
    467 		h_errno = NETDB_INTERNAL;
    468 		return (NULL);
    469 	}
    470 	if (_res.options & RES_USE_INET6) {
    471 		hp = gethostbyname2(name, AF_INET6);
    472 		if (hp)
    473 			return (hp);
    474 	}
    475 	return (gethostbyname2(name, AF_INET));
    476 }
    477 
    478 struct hostent *
    479 gethostbyname2(name, af)
    480 	const char *name;
    481 	int af;
    482 {
    483 	querybuf buf;
    484 	const char *cp;
    485 	char *bp;
    486 	int n, size, type, len, i;
    487 	char lookups[MAXDNSLUS];
    488 	struct hostent *hp;
    489 
    490 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
    491 		return (_gethtbyname(name));
    492 
    493 	switch (af) {
    494 	case AF_INET:
    495 		size = INADDRSZ;
    496 		type = T_A;
    497 		break;
    498 	case AF_INET6:
    499 		size = IN6ADDRSZ;
    500 		type = T_AAAA;
    501 		break;
    502 	default:
    503 		h_errno = NETDB_INTERNAL;
    504 		errno = EAFNOSUPPORT;
    505 		return (NULL);
    506 	}
    507 
    508 	host.h_addrtype = af;
    509 	host.h_length = size;
    510 
    511 	/*
    512 	 * if there aren't any dots, it could be a user-level alias.
    513 	 * this is also done in res_query() since we are not the only
    514 	 * function that looks up host names.
    515 	 */
    516 	if (!strchr(name, '.') && (cp = __hostalias(name)))
    517 		name = cp;
    518 
    519 	/*
    520 	 * disallow names consisting only of digits/dots, unless
    521 	 * they end in a dot.
    522 	 */
    523 	if (isdigit(name[0]))
    524 		for (cp = name;; ++cp) {
    525 			if (!*cp) {
    526 				if (*--cp == '.')
    527 					break;
    528 				/*
    529 				 * All-numeric, no dot at the end.
    530 				 * Fake up a hostent as if we'd actually
    531 				 * done a lookup.
    532 				 */
    533 				if (inet_pton(af, name, host_addr) <= 0) {
    534 					h_errno = HOST_NOT_FOUND;
    535 					return (NULL);
    536 				}
    537 				strncpy(hostbuf, name, MAXDNAME);
    538 				hostbuf[MAXDNAME] = '\0';
    539 				bp = hostbuf + MAXDNAME;
    540 				len = sizeof hostbuf - MAXDNAME;
    541 				host.h_name = hostbuf;
    542 				host.h_aliases = host_aliases;
    543 				host_aliases[0] = NULL;
    544 				h_addr_ptrs[0] = (char *)host_addr;
    545 				h_addr_ptrs[1] = NULL;
    546 				host.h_addr_list = h_addr_ptrs;
    547 				if (_res.options & RES_USE_INET6)
    548 					map_v4v6_hostent(&host, &bp, &len);
    549 				h_errno = NETDB_SUCCESS;
    550 				return (&host);
    551 			}
    552 			if (!isdigit(*cp) && *cp != '.')
    553 				break;
    554 		}
    555 	if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
    556 	    name[0] == ':')
    557 		for (cp = name;; ++cp) {
    558 			if (!*cp) {
    559 				if (*--cp == '.')
    560 					break;
    561 				/*
    562 				 * All-IPv6-legal, no dot at the end.
    563 				 * Fake up a hostent as if we'd actually
    564 				 * done a lookup.
    565 				 */
    566 				if (inet_pton(af, name, host_addr) <= 0) {
    567 					h_errno = HOST_NOT_FOUND;
    568 					return (NULL);
    569 				}
    570 				strncpy(hostbuf, name, MAXDNAME);
    571 				hostbuf[MAXDNAME] = '\0';
    572 				bp = hostbuf + MAXDNAME;
    573 				len = sizeof hostbuf - MAXDNAME;
    574 				host.h_name = hostbuf;
    575 				host.h_aliases = host_aliases;
    576 				host_aliases[0] = NULL;
    577 				h_addr_ptrs[0] = (char *)host_addr;
    578 				h_addr_ptrs[1] = NULL;
    579 				host.h_addr_list = h_addr_ptrs;
    580 				h_errno = NETDB_SUCCESS;
    581 				return (&host);
    582 			}
    583 			if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
    584 				break;
    585 		}
    586 
    587 	bcopy(_res.lookups, lookups, sizeof lookups);
    588 	if (lookups[0] == '\0')
    589 		strncpy(lookups, "bf", sizeof lookups);
    590 
    591 	hp = (struct hostent *)NULL;
    592 	for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
    593 		switch (lookups[i]) {
    594 #ifdef YP
    595 		case 'y':
    596 			hp = _yp_gethtbyname(name);
    597 			break;
    598 #endif
    599 		case 'b':
    600 			if ((n = res_search(name, C_IN, T_A, buf.buf,
    601 			    sizeof(buf))) < 0) {
    602 				dprintf("res_search failed (%d)\n", n);
    603 				break;
    604 			}
    605 			hp = getanswer(&buf, n, name, type);
    606 			break;
    607 		case 'f':
    608 			hp = _gethtbyname(name);
    609 			break;
    610 		}
    611 	}
    612 	return (hp);
    613 }
    614 
    615 struct hostent *
    616 gethostbyaddr(addr, len, af)
    617 	const char *addr;	/* XXX should have been def'd as u_char! */
    618 	int len, af;
    619 {
    620 	const u_char *uaddr = (const u_char *)addr;
    621 	static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
    622 	static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
    623 	int n, size, i;
    624 	querybuf buf;
    625 	struct hostent *hp;
    626 	char qbuf[MAXDNAME+1], *qp;
    627 	char lookups[MAXDNSLUS];
    628 
    629 	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
    630 		return (_gethtbyaddr(addr, len, af));
    631 
    632 	if (af == AF_INET6 && len == IN6ADDRSZ &&
    633 	    (!bcmp(uaddr, mapped, sizeof mapped) ||
    634 	     !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
    635 		/* Unmap. */
    636 		addr += sizeof mapped;
    637 		uaddr += sizeof mapped;
    638 		af = AF_INET;
    639 		len = INADDRSZ;
    640 	}
    641 	switch (af) {
    642 	case AF_INET:
    643 		size = INADDRSZ;
    644 		break;
    645 	case AF_INET6:
    646 		size = IN6ADDRSZ;
    647 		break;
    648 	default:
    649 		errno = EAFNOSUPPORT;
    650 		h_errno = NETDB_INTERNAL;
    651 		return (NULL);
    652 	}
    653 	if (size != len) {
    654 		errno = EINVAL;
    655 		h_errno = NETDB_INTERNAL;
    656 		return (NULL);
    657 	}
    658 	switch (af) {
    659 	case AF_INET:
    660 		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
    661 			       (uaddr[3] & 0xff),
    662 			       (uaddr[2] & 0xff),
    663 			       (uaddr[1] & 0xff),
    664 			       (uaddr[0] & 0xff));
    665 		break;
    666 	case AF_INET6:
    667 		qp = qbuf;
    668 		for (n = IN6ADDRSZ - 1; n >= 0; n--) {
    669 			qp += sprintf(qp, "%x.%x.",
    670 				       uaddr[n] & 0xf,
    671 				       (uaddr[n] >> 4) & 0xf);
    672 		}
    673 		strcpy(qp, "ip6.int");
    674 		break;
    675 	default:
    676 		abort();
    677 	}
    678 
    679 	bcopy(_res.lookups, lookups, sizeof lookups);
    680 	if (lookups[0] == '\0')
    681 		strncpy(lookups, "bf", sizeof lookups);
    682 
    683 	hp = (struct hostent *)NULL;
    684 	for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) {
    685 		switch (lookups[i]) {
    686 #ifdef YP
    687 		case 'y':
    688 			hp = _yp_gethtbyaddr(addr, len, af);
    689 			break;
    690 #endif
    691 		case 'b':
    692 			n = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf,
    693 			    sizeof(buf));
    694 			if (n < 0) {
    695 				dprintf("res_query failed (%d)\n", n);
    696 				break;
    697 			}
    698 			hp = getanswer(&buf, n, qbuf, T_PTR);
    699 			if (hp == NULL)
    700 				break;
    701 			hp->h_addrtype = af;
    702 			hp->h_length = len;
    703 			bcopy(addr, host_addr, len);
    704 			h_addr_ptrs[0] = (char *)&host_addr;
    705 			h_addr_ptrs[1] = (char *)0;
    706 			if (af == AF_INET && (_res.options & RES_USE_INET6)) {
    707 				map_v4v6_address((char*)host_addr,
    708 				    (char*)host_addr);
    709 				hp->h_addrtype = AF_INET6;
    710 				hp->h_length = IN6ADDRSZ;
    711 			}
    712 			h_errno = NETDB_SUCCESS;
    713 			break;
    714 		case 'f':
    715 			hp = _gethtbyaddr(addr, len, af);
    716 			break;
    717 		}
    718 	}
    719 	return (hp);
    720 }
    721 
    722 void
    723 _sethtent(f)
    724 	int f;
    725 {
    726 	if (!hostf)
    727 		hostf = fopen(_PATH_HOSTS, "r" );
    728 	else
    729 		rewind(hostf);
    730 	stayopen = f;
    731 }
    732 
    733 void
    734 _endhtent()
    735 {
    736 	if (hostf && !stayopen) {
    737 		(void) fclose(hostf);
    738 		hostf = NULL;
    739 	}
    740 }
    741 
    742 struct hostent *
    743 _gethtent()
    744 {
    745 	char *p;
    746 	register char *cp, **q;
    747 	int af, len;
    748 
    749 	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
    750 		h_errno = NETDB_INTERNAL;
    751 		return (NULL);
    752 	}
    753  again:
    754 	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
    755 		h_errno = HOST_NOT_FOUND;
    756 		return (NULL);
    757 	}
    758 	if (*p == '#')
    759 		goto again;
    760 	if (!(cp = strpbrk(p, "#\n")))
    761 		goto again;
    762 	*cp = '\0';
    763 	if (!(cp = strpbrk(p, " \t")))
    764 		goto again;
    765 	*cp++ = '\0';
    766 	if (inet_pton(AF_INET6, p, host_addr) > 0) {
    767 		af = AF_INET6;
    768 		len = IN6ADDRSZ;
    769 	} else if (inet_pton(AF_INET, p, host_addr) > 0) {
    770 		if (_res.options & RES_USE_INET6) {
    771 			map_v4v6_address((char*)host_addr, (char*)host_addr);
    772 			af = AF_INET6;
    773 			len = IN6ADDRSZ;
    774 		} else {
    775 			af = AF_INET;
    776 			len = INADDRSZ;
    777 		}
    778 	} else {
    779 		goto again;
    780 	}
    781 	h_addr_ptrs[0] = (char *)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 struct hostent *
    808 _gethtbyname(name)
    809 	const char *name;
    810 {
    811 	struct hostent *hp;
    812 
    813 	if (_res.options & RES_USE_INET6) {
    814 		hp = _gethtbyname2(name, AF_INET6);
    815 		if (hp)
    816 			return (hp);
    817 	}
    818 	return (_gethtbyname2(name, AF_INET));
    819 }
    820 
    821 struct hostent *
    822 _gethtbyname2(name, af)
    823 	const char *name;
    824 	int af;
    825 {
    826 	register struct hostent *p;
    827 	register char **cp;
    828 
    829 	_sethtent(0);
    830 	while ((p = _gethtent()) != NULL) {
    831 		if (p->h_addrtype != af)
    832 			continue;
    833 		if (strcasecmp(p->h_name, name) == 0)
    834 			break;
    835 		for (cp = p->h_aliases; *cp != 0; cp++)
    836 			if (strcasecmp(*cp, name) == 0)
    837 				goto found;
    838 	}
    839  found:
    840 	_endhtent();
    841 	return (p);
    842 }
    843 
    844 struct hostent *
    845 _gethtbyaddr(addr, len, af)
    846 	const char *addr;
    847 	int len, af;
    848 {
    849 	register struct hostent *p;
    850 
    851 	_sethtent(0);
    852 	while ((p = _gethtent()) != NULL)
    853 		if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
    854 			break;
    855 	_endhtent();
    856 	return (p);
    857 }
    858 
    859 static void
    860 map_v4v6_address(src, dst)
    861 	const char *src;
    862 	char *dst;
    863 {
    864 	u_char *p = (u_char *)dst;
    865 	char tmp[INADDRSZ];
    866 	int i;
    867 
    868 	/* Stash a temporary copy so our caller can update in place. */
    869 	bcopy(src, tmp, INADDRSZ);
    870 	/* Mark this ipv6 addr as a mapped ipv4. */
    871 	for (i = 0; i < 10; i++)
    872 		*p++ = 0x00;
    873 	*p++ = 0xff;
    874 	*p++ = 0xff;
    875 	/* Retrieve the saved copy and we're done. */
    876 	bcopy(tmp, (void*)p, INADDRSZ);
    877 }
    878 
    879 static void
    880 map_v4v6_hostent(hp, bpp, lenp)
    881 	struct hostent *hp;
    882 	char **bpp;
    883 	int *lenp;
    884 {
    885 	char **ap;
    886 
    887 	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
    888 		return;
    889 	hp->h_addrtype = AF_INET6;
    890 	hp->h_length = IN6ADDRSZ;
    891 	for (ap = hp->h_addr_list; *ap; ap++) {
    892 		int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
    893 
    894 		if (*lenp < (i + IN6ADDRSZ)) {
    895 			/* Out of memory.  Truncate address list here.  XXX */
    896 			*ap = NULL;
    897 			return;
    898 		}
    899 		*bpp += i;
    900 		*lenp -= i;
    901 		map_v4v6_address(*ap, *bpp);
    902 		*ap = *bpp;
    903 		*bpp += IN6ADDRSZ;
    904 		*lenp -= IN6ADDRSZ;
    905 	}
    906 }
    907 
    908 #ifdef RESOLVSORT
    909 static void
    910 addrsort(ap, num)
    911 	char **ap;
    912 	int num;
    913 {
    914 	int i, j;
    915 	char **p;
    916 	short aval[MAXADDRS];
    917 	int needsort = 0;
    918 
    919 	p = ap;
    920 	for (i = 0; i < num; i++, p++) {
    921 	    for (j = 0 ; (unsigned)j < _res.nsort; j++)
    922 		if (_res.sort_list[j].addr.s_addr ==
    923 		    (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
    924 			break;
    925 	    aval[i] = j;
    926 	    if (needsort == 0 && i > 0 && j < aval[i-1])
    927 		needsort = i;
    928 	}
    929 	if (!needsort)
    930 	    return;
    931 
    932 	while (needsort < num) {
    933 	    for (j = needsort - 1; j >= 0; j--) {
    934 		if (aval[j] > aval[j+1]) {
    935 		    char *hp;
    936 
    937 		    i = aval[j];
    938 		    aval[j] = aval[j+1];
    939 		    aval[j+1] = i;
    940 
    941 		    hp = ap[j];
    942 		    ap[j] = ap[j+1];
    943 		    ap[j+1] = hp;
    944 
    945 		} else
    946 		    break;
    947 	    }
    948 	    needsort++;
    949 	}
    950 }
    951 #endif
    952 
    953 #if defined(BSD43_BSD43_NFS) || defined(sun)
    954 /* some libc's out there are bound internally to these names (UMIPS) */
    955 void
    956 ht_sethostent(stayopen)
    957 	int stayopen;
    958 {
    959 	_sethtent(stayopen);
    960 }
    961 
    962 void
    963 ht_endhostent()
    964 {
    965 	_endhtent();
    966 }
    967 
    968 struct hostent *
    969 ht_gethostbyname(name)
    970 	char *name;
    971 {
    972 	return (_gethtbyname(name));
    973 }
    974 
    975 struct hostent *
    976 ht_gethostbyaddr(addr, len, af)
    977 	const char *addr;
    978 	int len, af;
    979 {
    980 	return (_gethtbyaddr(addr, len, af));
    981 }
    982 
    983 struct hostent *
    984 gethostent()
    985 {
    986 	return (_gethtent());
    987 }
    988 
    989 void
    990 dns_service()
    991 {
    992 	return;
    993 }
    994 
    995 #undef dn_skipname
    996 dn_skipname(comp_dn, eom)
    997 	const u_char *comp_dn, *eom;
    998 {
    999 	return (__dn_skipname(comp_dn, eom));
   1000 }
   1001 #endif /*old-style libc with yp junk in it*/
   1002 
   1003 #ifdef YP
   1004 struct hostent *
   1005 _yphostent(line)
   1006 	char *line;
   1007 {
   1008 	static struct in_addr host_addrs[MAXADDRS];
   1009 	char *p = line;
   1010 	char *cp, **q;
   1011 	char **hap;
   1012 	struct in_addr *buf;
   1013 	int more;
   1014 
   1015 	host.h_name = NULL;
   1016 	host.h_addr_list = h_addr_ptrs;
   1017 	host.h_length = sizeof(u_int32_t);
   1018 	host.h_addrtype = AF_INET;
   1019 	hap = h_addr_ptrs;
   1020 	buf = host_addrs;
   1021 	q = host.h_aliases = host_aliases;
   1022 
   1023 nextline:
   1024 	more = 0;
   1025 	cp = strpbrk(p, " \t");
   1026 	if (cp == NULL) {
   1027 		if (host.h_name == NULL)
   1028 			return (NULL);
   1029 		else
   1030 			goto done;
   1031 	}
   1032 	*cp++ = '\0';
   1033 
   1034 	*hap++ = (char *)buf;
   1035 	(void) inet_aton(p, buf++);
   1036 
   1037 	while (*cp == ' ' || *cp == '\t')
   1038 		cp++;
   1039 	p = cp;
   1040 	cp = strpbrk(p, " \t\n");
   1041 	if (cp != NULL) {
   1042 		if (*cp == '\n')
   1043 			more = 1;
   1044 		*cp++ = '\0';
   1045 	}
   1046 	if (!host.h_name)
   1047 		host.h_name = p;
   1048 	else if (strcmp(host.h_name, p)==0)
   1049 		;
   1050 	else if (q < &host_aliases[MAXALIASES - 1])
   1051 		*q++ = p;
   1052 	p = cp;
   1053 	if (more)
   1054 		goto nextline;
   1055 
   1056 	while (cp && *cp) {
   1057 		if (*cp == ' ' || *cp == '\t') {
   1058 			cp++;
   1059 			continue;
   1060 		}
   1061 		if (*cp == '\n') {
   1062 			cp++;
   1063 			goto nextline;
   1064 		}
   1065 		if (q < &host_aliases[MAXALIASES - 1])
   1066 			*q++ = cp;
   1067 		cp = strpbrk(cp, " \t");
   1068 		if (cp != NULL)
   1069 			*cp++ = '\0';
   1070 	}
   1071 done:
   1072 	*q = NULL;
   1073 	*hap = NULL;
   1074 	return (&host);
   1075 }
   1076 
   1077 struct hostent *
   1078 _yp_gethtbyaddr(addr, len, type)
   1079 	const char *addr;
   1080 	int len, type;
   1081 {
   1082 	struct hostent *hp = (struct hostent *)NULL;
   1083 	static char *__ypcurrent;
   1084 	int __ypcurrentlen, r;
   1085 	char name[sizeof("xxx.xxx.xxx.xxx") + 1];
   1086 
   1087 	if (!__ypdomain) {
   1088 		if (_yp_check(&__ypdomain) == 0)
   1089 			return (hp);
   1090 	}
   1091 	(void)snprintf(name, sizeof name, "%u.%u.%u.%u",
   1092 		((unsigned)addr[0] & 0xff),
   1093 		((unsigned)addr[1] & 0xff),
   1094 		((unsigned)addr[2] & 0xff),
   1095 		((unsigned)addr[3] & 0xff));
   1096 	if (__ypcurrent)
   1097 		free(__ypcurrent);
   1098 	__ypcurrent = NULL;
   1099 	r = yp_match(__ypdomain, "hosts.byaddr", name,
   1100 		strlen(name), &__ypcurrent, &__ypcurrentlen);
   1101 	if (r==0)
   1102 		hp = _yphostent(__ypcurrent);
   1103 	if (hp==NULL)
   1104 		h_errno = HOST_NOT_FOUND;
   1105 	return (hp);
   1106 }
   1107 
   1108 struct hostent *
   1109 _yp_gethtbyname(name)
   1110 	const char *name;
   1111 {
   1112 	struct hostent *hp = (struct hostent *)NULL;
   1113 	static char *__ypcurrent;
   1114 	int __ypcurrentlen, r;
   1115 
   1116 	if (!__ypdomain) {
   1117 		if (_yp_check(&__ypdomain) == 0)
   1118 			return (hp);
   1119 	}
   1120 	if (__ypcurrent)
   1121 		free(__ypcurrent);
   1122 	__ypcurrent = NULL;
   1123 	r = yp_match(__ypdomain, "hosts.byname", name,
   1124 		strlen(name), &__ypcurrent, &__ypcurrentlen);
   1125 	if (r==0)
   1126 		hp = _yphostent(__ypcurrent);
   1127 	if (hp==NULL)
   1128 		h_errno = HOST_NOT_FOUND;
   1129 	return (hp);
   1130 }
   1131 #endif
   1132