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