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