xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_ipaddr.c (revision 8023:faf256d5c16c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8023SPhil.Kirk@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
271676Sjpk #include <stdlib.h>
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/socket.h>
310Sstevel@tonic-gate #include <sys/sockio.h>
320Sstevel@tonic-gate #include <net/if.h>
330Sstevel@tonic-gate #include <netinet/in_systm.h>
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <netinet/if_ether.h>
360Sstevel@tonic-gate #include <netinet/ip.h>
370Sstevel@tonic-gate #include <netdb.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <setjmp.h>
411676Sjpk #include <arpa/inet.h>
421676Sjpk #include "snoop.h"
430Sstevel@tonic-gate 
441676Sjpk static sigjmp_buf nisjmp;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	MAXHASH 1024  /* must be a power of 2 */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	SEPARATORS " \t\n"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate struct hostdata {
510Sstevel@tonic-gate 	struct hostdata	*h_next;
520Sstevel@tonic-gate 	char		*h_hostname;
530Sstevel@tonic-gate 	int		h_pktsout;
540Sstevel@tonic-gate 	int		h_pktsin;
550Sstevel@tonic-gate };
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct hostdata4 {
580Sstevel@tonic-gate 	struct hostdata4	*h4_next;
590Sstevel@tonic-gate 	char		*h4_hostname;
600Sstevel@tonic-gate 	int		h4_pktsout;
610Sstevel@tonic-gate 	int		h4_pktsin;
620Sstevel@tonic-gate 	struct in_addr	h4_addr;
630Sstevel@tonic-gate };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate struct hostdata6 {
660Sstevel@tonic-gate 	struct hostdata6	*h6_next;
670Sstevel@tonic-gate 	char		*h6_hostname;
680Sstevel@tonic-gate 	int		h6_pktsout;
690Sstevel@tonic-gate 	int		h6_pktsin;
700Sstevel@tonic-gate 	struct in6_addr	h6_addr;
710Sstevel@tonic-gate };
720Sstevel@tonic-gate 
731676Sjpk static struct hostdata *addhost(int, const void *, const char *, char **);
740Sstevel@tonic-gate 
751676Sjpk static struct hostdata4 *h_table4[MAXHASH];
761676Sjpk static struct hostdata6 *h_table6[MAXHASH];
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	iphash(e)  ((e) & (MAXHASH-1))
790Sstevel@tonic-gate 
801676Sjpk /* ARGSUSED */
810Sstevel@tonic-gate static void
wakeup(int n)821676Sjpk wakeup(int n)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	siglongjmp(nisjmp, 1);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate extern char *inet_ntoa();
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct hostdata *
iplookup(struct in_addr ipaddr)900Sstevel@tonic-gate iplookup(struct in_addr ipaddr)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	register struct hostdata4 *h;
930Sstevel@tonic-gate 	struct hostent *hp = NULL;
940Sstevel@tonic-gate 	struct netent *np;
950Sstevel@tonic-gate 	int error_num;
960Sstevel@tonic-gate 	struct hostdata *retval;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	for (h = h_table4[iphash(ipaddr.s_addr)]; h; h = h->h4_next) {
990Sstevel@tonic-gate 		if (h->h4_addr.s_addr == ipaddr.s_addr)
1000Sstevel@tonic-gate 			return ((struct hostdata *)h);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* not found.  Put it in */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (ipaddr.s_addr == htonl(INADDR_BROADCAST))
1060Sstevel@tonic-gate 		return (addhost(AF_INET, &ipaddr, "BROADCAST", NULL));
1070Sstevel@tonic-gate 	if (ipaddr.s_addr == htonl(INADDR_ANY))
1080Sstevel@tonic-gate 		return (addhost(AF_INET, &ipaddr, "OLD-BROADCAST", NULL));
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
1120Sstevel@tonic-gate 	 * an unresponsive name server.
1130Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	if (! rflg && sigsetjmp(nisjmp, 1) == 0) {
1160Sstevel@tonic-gate 		(void) snoop_alarm(3, wakeup);
1170Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)&ipaddr, sizeof (int),
118*8023SPhil.Kirk@Sun.COM 		    AF_INET, &error_num);
1190Sstevel@tonic-gate 		if (hp == NULL && inet_lnaof(ipaddr) == 0) {
1200Sstevel@tonic-gate 			np = getnetbyaddr(inet_netof(ipaddr), AF_INET);
1210Sstevel@tonic-gate 			if (np)
1220Sstevel@tonic-gate 				return (addhost(AF_INET, &ipaddr, np->n_name,
123*8023SPhil.Kirk@Sun.COM 				    np->n_aliases));
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 		(void) snoop_alarm(0, wakeup);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	retval = addhost(AF_INET, &ipaddr,
1290Sstevel@tonic-gate 	    hp ? hp->h_name : inet_ntoa(ipaddr),
1300Sstevel@tonic-gate 	    hp ? hp->h_aliases : NULL);
1310Sstevel@tonic-gate 	if (hp != NULL)
1320Sstevel@tonic-gate 		freehostent(hp);
1330Sstevel@tonic-gate 	return (retval);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static struct hostdata *
ip6lookup(const struct in6_addr * ip6addr)1371676Sjpk ip6lookup(const struct in6_addr *ip6addr)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	struct hostdata6 *h;
1400Sstevel@tonic-gate 	struct hostent *hp = NULL;
1410Sstevel@tonic-gate 	int error_num;
1420Sstevel@tonic-gate 	char addrstr[INET6_ADDRSTRLEN];
143410Skcpoon 	char *addname;
1440Sstevel@tonic-gate 	struct hostdata *retval;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	for (h = h_table6[iphash(((uint32_t *)ip6addr)[3])]; h;
1470Sstevel@tonic-gate 	    h = h->h6_next) {
1480Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&h->h6_addr, ip6addr))
1490Sstevel@tonic-gate 			return ((struct hostdata *)h);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* not in the hash table, put it in */
1530Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(ip6addr))
1540Sstevel@tonic-gate 		return (addhost(AF_INET6, ip6addr, "UNSPECIFIED", NULL));
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
1580Sstevel@tonic-gate 	 * an unresponsive name server.
1590Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if (! rflg && sigsetjmp(nisjmp, 1) == 0) {
1620Sstevel@tonic-gate 		(void) snoop_alarm(3, wakeup);
1630Sstevel@tonic-gate 		hp = getipnodebyaddr(ip6addr, sizeof (struct in6_addr),
1640Sstevel@tonic-gate 		    AF_INET6, &error_num);
1650Sstevel@tonic-gate 		(void) snoop_alarm(0, wakeup);
1660Sstevel@tonic-gate 	} else {
1670Sstevel@tonic-gate 		hp = NULL;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (hp != NULL)
1710Sstevel@tonic-gate 		addname = hp->h_name;
1720Sstevel@tonic-gate 	else {
1730Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, ip6addr, addrstr, INET6_ADDRSTRLEN);
1740Sstevel@tonic-gate 		addname = addrstr;
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	retval = addhost(AF_INET6, ip6addr, addname, hp ? hp->h_aliases : NULL);
1780Sstevel@tonic-gate 	if (hp != NULL)
1790Sstevel@tonic-gate 		freehostent(hp);
1800Sstevel@tonic-gate 	return (retval);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static struct hostdata *
addhost(int family,const void * ipaddr,const char * name,char ** aliases)1841676Sjpk addhost(int family, const void *ipaddr, const char *name, char **aliases)
1850Sstevel@tonic-gate {
1861676Sjpk 	struct hostdata **hp, *n = NULL;
1870Sstevel@tonic-gate 	extern FILE *namefile;
1880Sstevel@tonic-gate 	int hashval;
1890Sstevel@tonic-gate 	static char aname[128];
1900Sstevel@tonic-gate 	char *np;
1910Sstevel@tonic-gate 	static struct hostdata h;
1920Sstevel@tonic-gate 	int ind;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	switch (family) {
1950Sstevel@tonic-gate 	case AF_INET:
1960Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata4));
1970Sstevel@tonic-gate 		if (n == NULL)
1980Sstevel@tonic-gate 			goto alloc_failed;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		memset(n, 0, sizeof (struct hostdata4));
2010Sstevel@tonic-gate 		n->h_hostname = strdup(name);
2020Sstevel@tonic-gate 		if (n->h_hostname == NULL)
2030Sstevel@tonic-gate 			goto alloc_failed;
2040Sstevel@tonic-gate 
2051676Sjpk 		((struct hostdata4 *)n)->h4_addr =
2061676Sjpk 		    *(const struct in_addr *)ipaddr;
2070Sstevel@tonic-gate 		hashval = ((struct in_addr *)ipaddr)->s_addr;
2080Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table4[iphash(hashval)];
2090Sstevel@tonic-gate 		break;
2100Sstevel@tonic-gate 	case AF_INET6:
2110Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata6));
2120Sstevel@tonic-gate 		if (n == NULL)
2130Sstevel@tonic-gate 			goto alloc_failed;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		memset(n, 0, sizeof (struct hostdata6));
2160Sstevel@tonic-gate 		n->h_hostname = strdup(name);
2170Sstevel@tonic-gate 		if (n->h_hostname == NULL)
2180Sstevel@tonic-gate 			goto alloc_failed;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		memcpy(&((struct hostdata6 *)n)->h6_addr, ipaddr,
2210Sstevel@tonic-gate 		    sizeof (struct in6_addr));
2221676Sjpk 		hashval = ((const int *)ipaddr)[3];
2230Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table6[iphash(hashval)];
2240Sstevel@tonic-gate 		break;
2250Sstevel@tonic-gate 	default:
2260Sstevel@tonic-gate 		fprintf(stderr, "snoop: ERROR: Unknown address family: %d",
2270Sstevel@tonic-gate 		    family);
2280Sstevel@tonic-gate 		exit(1);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	n->h_next = *hp;
2320Sstevel@tonic-gate 	*hp = n;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (namefile != NULL) {
2350Sstevel@tonic-gate 		if (family == AF_INET) {
2361676Sjpk 			np = inet_ntoa(*(const struct in_addr *)ipaddr);
2370Sstevel@tonic-gate 			if (np) {
2380Sstevel@tonic-gate 				(void) fprintf(namefile, "%s\t%s", np, name);
2390Sstevel@tonic-gate 				if (aliases) {
2400Sstevel@tonic-gate 					for (ind = 0;
2410Sstevel@tonic-gate 					    aliases[ind] != NULL;
2420Sstevel@tonic-gate 					    ind++) {
2430Sstevel@tonic-gate 						(void) fprintf(namefile, " %s",
244*8023SPhil.Kirk@Sun.COM 						    aliases[ind]);
2450Sstevel@tonic-gate 					}
2460Sstevel@tonic-gate 				}
2470Sstevel@tonic-gate 				(void) fprintf(namefile, "\n");
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		} else if (family == AF_INET6) {
2500Sstevel@tonic-gate 			np = (char *)inet_ntop(AF_INET6, (void *)ipaddr, aname,
251*8023SPhil.Kirk@Sun.COM 			    sizeof (aname));
2520Sstevel@tonic-gate 			if (np) {
2530Sstevel@tonic-gate 				(void) fprintf(namefile, "%s\t%s", np, name);
2540Sstevel@tonic-gate 				if (aliases) {
2550Sstevel@tonic-gate 					for (ind = 0;
2560Sstevel@tonic-gate 					    aliases[ind] != NULL;
2570Sstevel@tonic-gate 					    ind++) {
2580Sstevel@tonic-gate 						(void) fprintf(namefile, " %s",
259*8023SPhil.Kirk@Sun.COM 						    aliases[ind]);
2600Sstevel@tonic-gate 					}
2610Sstevel@tonic-gate 				}
2620Sstevel@tonic-gate 				(void) fprintf(namefile, "\n");
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 		} else {
2650Sstevel@tonic-gate 			(void) fprintf(stderr, "addhost: unknown family %d\n",
266*8023SPhil.Kirk@Sun.COM 			    family);
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 	return (n);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate alloc_failed:
2720Sstevel@tonic-gate 	if (n)
2730Sstevel@tonic-gate 		free(n);
2740Sstevel@tonic-gate 	(void) fprintf(stderr, "addhost: no mem\n");
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	aname[0] = '\0';
2770Sstevel@tonic-gate 	memset(&h, 0, sizeof (struct hostdata));
2780Sstevel@tonic-gate 	h.h_hostname = aname;
2790Sstevel@tonic-gate 	return (&h);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate char *
addrtoname(int family,const void * ipaddr)2831676Sjpk addrtoname(int family, const void *ipaddr)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	switch (family) {
2860Sstevel@tonic-gate 	case AF_INET:
2871676Sjpk 		return (iplookup(*(const struct in_addr *)ipaddr)->h_hostname);
2880Sstevel@tonic-gate 	case AF_INET6:
2891676Sjpk 		return (ip6lookup((const struct in6_addr *)ipaddr)->h_hostname);
2900Sstevel@tonic-gate 	}
2911676Sjpk 	(void) fprintf(stderr, "snoop: ERROR: unknown address family: %d\n",
2921676Sjpk 	    family);
2931676Sjpk 	exit(1);
2941676Sjpk 	/* NOTREACHED */
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate void
load_names(fname)2980Sstevel@tonic-gate load_names(fname)
2990Sstevel@tonic-gate 	char *fname;
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	char buf[1024];
3020Sstevel@tonic-gate 	char *addr, *name, *alias;
3030Sstevel@tonic-gate 	FILE *f;
3040Sstevel@tonic-gate 	unsigned int addrv4;
3050Sstevel@tonic-gate 	struct in6_addr addrv6;
3060Sstevel@tonic-gate 	int family;
3070Sstevel@tonic-gate 	void *naddr;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	(void) fprintf(stderr, "Loading name file %s\n", fname);
3100Sstevel@tonic-gate 	f = fopen(fname, "r");
3110Sstevel@tonic-gate 	if (f == NULL) {
3120Sstevel@tonic-gate 		perror(fname);
3130Sstevel@tonic-gate 		return;
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	while (fgets(buf, 1024, f) != NULL) {
3170Sstevel@tonic-gate 		addr = strtok(buf, SEPARATORS);
3180Sstevel@tonic-gate 		if (addr == NULL || *addr == '#')
3190Sstevel@tonic-gate 			continue;
3200Sstevel@tonic-gate 		if (inet_pton(AF_INET6, addr, (void *)&addrv6) == 1) {
3210Sstevel@tonic-gate 			family = AF_INET6;
3220Sstevel@tonic-gate 			naddr = (void *)&addrv6;
3231676Sjpk 		} else if ((addrv4 = inet_addr(addr)) != (ulong_t)-1) {
3240Sstevel@tonic-gate 			family = AF_INET;
3250Sstevel@tonic-gate 			naddr = (void *)&addrv4;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 		name = strtok(NULL, SEPARATORS);
3280Sstevel@tonic-gate 		if (name == NULL)
3290Sstevel@tonic-gate 			continue;
3301676Sjpk 		while ((alias = strtok(NULL, SEPARATORS)) != NULL &&
3311676Sjpk 		    (*alias != '#')) {
3320Sstevel@tonic-gate 			(void) addhost(family, naddr, alias, NULL);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		(void) addhost(family, naddr, name, NULL);
3350Sstevel@tonic-gate 		/* Note: certain addresses such as broadcast are skipped */
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	(void) fclose(f);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * lgetipnodebyname: looks up hostname in cached address data. This allows
3430Sstevel@tonic-gate  * filtering on hostnames from the .names file to work properly, and
3440Sstevel@tonic-gate  * avoids name clashes between domains. Note that only the first of the
3450Sstevel@tonic-gate  * ipv4, ipv6, or v4mapped address will be returned, because the
3460Sstevel@tonic-gate  * cache does not contain information on multi-homed hosts.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate /*ARGSUSED*/
3490Sstevel@tonic-gate struct hostent *
lgetipnodebyname(const char * name,int af,int flags,int * error_num)3500Sstevel@tonic-gate lgetipnodebyname(const char *name, int af, int flags, int *error_num)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	int i;
3530Sstevel@tonic-gate 	struct hostdata4 *h;
3540Sstevel@tonic-gate 	struct hostdata6 *h6;
3550Sstevel@tonic-gate 	static struct hostent he;		/* host entry */
3560Sstevel@tonic-gate 	static struct in6_addr h46_addr[MAXADDRS];	/* v4mapped address */
3570Sstevel@tonic-gate 	static char h_name[MAXHOSTNAMELEN];	/* hostname */
3580Sstevel@tonic-gate 	static char *list[MAXADDRS];		/* addr_list array */
3590Sstevel@tonic-gate 	struct hostent *hp = &he;
3600Sstevel@tonic-gate 	int ind;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	(void) memset((char *)hp, 0, sizeof (struct hostent));
3630Sstevel@tonic-gate 	hp->h_name = h_name;
3640Sstevel@tonic-gate 	h_name[0] = '\0';
3650Sstevel@tonic-gate 	strcpy(h_name, name);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	hp->h_addrtype = AF_INET6;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	hp->h_addr_list = list;
3700Sstevel@tonic-gate 	for (i = 0; i < MAXADDRS; i++)
3710Sstevel@tonic-gate 		hp->h_addr_list[i] = NULL;
3720Sstevel@tonic-gate 	ind = 0;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/* ipv6 lookup */
3750Sstevel@tonic-gate 	if (af == AF_INET6) {
3760Sstevel@tonic-gate 		hp->h_length = sizeof (struct in6_addr);
3770Sstevel@tonic-gate 		for (i = 0; i < MAXHASH; i++) {
3780Sstevel@tonic-gate 			for (h6 = h_table6[i]; h6; h6 = h6->h6_next) {
3790Sstevel@tonic-gate 				if (strcmp(name, h6->h6_hostname) == 0) {
3800Sstevel@tonic-gate 					if (ind >= MAXADDRS - 1) {
3810Sstevel@tonic-gate 						/* too many addresses */
3820Sstevel@tonic-gate 						return (hp);
3830Sstevel@tonic-gate 					}
3840Sstevel@tonic-gate 					/* found ipv6 addr */
3850Sstevel@tonic-gate 					hp->h_addr_list[ind] =
386*8023SPhil.Kirk@Sun.COM 					    (char *)&h6->h6_addr;
3870Sstevel@tonic-gate 					ind++;
3880Sstevel@tonic-gate 				}
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	/* ipv4 or v4mapped lookup */
3930Sstevel@tonic-gate 	if (af == AF_INET || (flags & AI_ALL)) {
3940Sstevel@tonic-gate 		for (i = 0; i < MAXHASH; i++) {
3950Sstevel@tonic-gate 			for (h = h_table4[i]; h; h = h->h4_next) {
3960Sstevel@tonic-gate 				if (strcmp(name, h->h4_hostname) == 0) {
3970Sstevel@tonic-gate 					if (ind >= MAXADDRS - 1) {
3980Sstevel@tonic-gate 						/* too many addresses */
3990Sstevel@tonic-gate 						return (hp);
4000Sstevel@tonic-gate 					}
4010Sstevel@tonic-gate 					if (af == AF_INET) {
4020Sstevel@tonic-gate 						/* found ipv4 addr */
4030Sstevel@tonic-gate 						hp->h_addrtype = AF_INET;
4040Sstevel@tonic-gate 						hp->h_length =
4050Sstevel@tonic-gate 						    sizeof (struct in_addr);
4060Sstevel@tonic-gate 						hp->h_addr_list[ind] =
4070Sstevel@tonic-gate 						    (char *)&h->h4_addr;
4080Sstevel@tonic-gate 						ind++;
4090Sstevel@tonic-gate 					} else {
4100Sstevel@tonic-gate 						/* found v4mapped addr */
4110Sstevel@tonic-gate 						hp->h_length =
4120Sstevel@tonic-gate 						    sizeof (struct in6_addr);
4130Sstevel@tonic-gate 						hp->h_addr_list[ind] =
4140Sstevel@tonic-gate 						    (char *)&h46_addr[ind];
4150Sstevel@tonic-gate 						IN6_INADDR_TO_V4MAPPED(
416*8023SPhil.Kirk@Sun.COM 						    &h->h4_addr,
417*8023SPhil.Kirk@Sun.COM 						    &h46_addr[ind]);
4180Sstevel@tonic-gate 						ind++;
4190Sstevel@tonic-gate 					}
4200Sstevel@tonic-gate 				}
4210Sstevel@tonic-gate 			}
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	return (ind > 0 ? hp : NULL);
4250Sstevel@tonic-gate }
426