17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b127ac41SPhilip Kirk  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*afee3dc6SRobert Mustacchi  * Copyright (c) 2017, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
2845916cd2Sjpk #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
337c478bd9Sstevel@tonic-gate #include <net/if.h>
347c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
357c478bd9Sstevel@tonic-gate #include <netinet/in.h>
367c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
377c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <signal.h>
417c478bd9Sstevel@tonic-gate #include <setjmp.h>
4245916cd2Sjpk #include <arpa/inet.h>
43*afee3dc6SRobert Mustacchi #include <sys/time.h>
4445916cd2Sjpk #include "snoop.h"
457c478bd9Sstevel@tonic-gate 
4645916cd2Sjpk static sigjmp_buf nisjmp;
47*afee3dc6SRobert Mustacchi static hrtime_t snoop_lastwarn;		/* Last time NS warning fired */
48*afee3dc6SRobert Mustacchi static unsigned snoop_warninter = 60;	/* Time in seconds between warnings */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	MAXHASH 1024  /* must be a power of 2 */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	SEPARATORS " \t\n"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct hostdata {
557c478bd9Sstevel@tonic-gate 	struct hostdata	*h_next;
567c478bd9Sstevel@tonic-gate 	char		*h_hostname;
577c478bd9Sstevel@tonic-gate 	int		h_pktsout;
587c478bd9Sstevel@tonic-gate 	int		h_pktsin;
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct hostdata4 {
627c478bd9Sstevel@tonic-gate 	struct hostdata4	*h4_next;
637c478bd9Sstevel@tonic-gate 	char		*h4_hostname;
647c478bd9Sstevel@tonic-gate 	int		h4_pktsout;
657c478bd9Sstevel@tonic-gate 	int		h4_pktsin;
667c478bd9Sstevel@tonic-gate 	struct in_addr	h4_addr;
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct hostdata6 {
707c478bd9Sstevel@tonic-gate 	struct hostdata6	*h6_next;
717c478bd9Sstevel@tonic-gate 	char		*h6_hostname;
727c478bd9Sstevel@tonic-gate 	int		h6_pktsout;
737c478bd9Sstevel@tonic-gate 	int		h6_pktsin;
747c478bd9Sstevel@tonic-gate 	struct in6_addr	h6_addr;
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
7745916cd2Sjpk static struct hostdata *addhost(int, const void *, const char *, char **);
787c478bd9Sstevel@tonic-gate 
7945916cd2Sjpk static struct hostdata4 *h_table4[MAXHASH];
8045916cd2Sjpk static struct hostdata6 *h_table6[MAXHASH];
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	iphash(e)  ((e) & (MAXHASH-1))
837c478bd9Sstevel@tonic-gate 
8445916cd2Sjpk /* ARGSUSED */
857c478bd9Sstevel@tonic-gate static void
wakeup(int n)8645916cd2Sjpk wakeup(int n)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	siglongjmp(nisjmp, 1);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate extern char *inet_ntoa();
927c478bd9Sstevel@tonic-gate 
93*afee3dc6SRobert Mustacchi static void
snoop_nswarn(void)94*afee3dc6SRobert Mustacchi snoop_nswarn(void)
95*afee3dc6SRobert Mustacchi {
96*afee3dc6SRobert Mustacchi 	hrtime_t now = gethrtime();
97*afee3dc6SRobert Mustacchi 
98*afee3dc6SRobert Mustacchi 	if (now - snoop_lastwarn >= snoop_warninter * NANOSEC) {
99*afee3dc6SRobert Mustacchi 		snoop_lastwarn = now;
100*afee3dc6SRobert Mustacchi 		(void) fprintf(stderr, "snoop: warning: packets captured, but "
101*afee3dc6SRobert Mustacchi 		    "name service lookups are timing out. Use snoop -r to "
102*afee3dc6SRobert Mustacchi 		    "disable name service lookups\n");
103*afee3dc6SRobert Mustacchi 	}
104*afee3dc6SRobert Mustacchi }
105*afee3dc6SRobert Mustacchi 
1067c478bd9Sstevel@tonic-gate static struct hostdata *
iplookup(struct in_addr ipaddr)1077c478bd9Sstevel@tonic-gate iplookup(struct in_addr ipaddr)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	register struct hostdata4 *h;
1107c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
1117c478bd9Sstevel@tonic-gate 	struct netent *np;
1127c478bd9Sstevel@tonic-gate 	int error_num;
1137c478bd9Sstevel@tonic-gate 	struct hostdata *retval;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	for (h = h_table4[iphash(ipaddr.s_addr)]; h; h = h->h4_next) {
1167c478bd9Sstevel@tonic-gate 		if (h->h4_addr.s_addr == ipaddr.s_addr)
1177c478bd9Sstevel@tonic-gate 			return ((struct hostdata *)h);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/* not found.  Put it in */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (ipaddr.s_addr == htonl(INADDR_BROADCAST))
1237c478bd9Sstevel@tonic-gate 		return (addhost(AF_INET, &ipaddr, "BROADCAST", NULL));
1247c478bd9Sstevel@tonic-gate 	if (ipaddr.s_addr == htonl(INADDR_ANY))
1257c478bd9Sstevel@tonic-gate 		return (addhost(AF_INET, &ipaddr, "OLD-BROADCAST", NULL));
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
1297c478bd9Sstevel@tonic-gate 	 * an unresponsive name server.
1307c478bd9Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
1317c478bd9Sstevel@tonic-gate 	 */
132*afee3dc6SRobert Mustacchi 	if (!rflg) {
133*afee3dc6SRobert Mustacchi 		if (sigsetjmp(nisjmp, 1) == 0) {
134*afee3dc6SRobert Mustacchi 			(void) snoop_alarm(3, wakeup);
135*afee3dc6SRobert Mustacchi 			hp = getipnodebyaddr((char *)&ipaddr, sizeof (int),
136*afee3dc6SRobert Mustacchi 			    AF_INET, &error_num);
137*afee3dc6SRobert Mustacchi 			if (hp == NULL && inet_lnaof(ipaddr) == 0) {
138*afee3dc6SRobert Mustacchi 				np = getnetbyaddr(inet_netof(ipaddr), AF_INET);
139*afee3dc6SRobert Mustacchi 				if (np)
140*afee3dc6SRobert Mustacchi 					return (addhost(AF_INET, &ipaddr,
141*afee3dc6SRobert Mustacchi 					    np->n_name, np->n_aliases));
142*afee3dc6SRobert Mustacchi 			}
143*afee3dc6SRobert Mustacchi 			(void) snoop_alarm(0, wakeup);
144*afee3dc6SRobert Mustacchi 		} else {
145*afee3dc6SRobert Mustacchi 			snoop_nswarn();
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	retval = addhost(AF_INET, &ipaddr,
1507c478bd9Sstevel@tonic-gate 	    hp ? hp->h_name : inet_ntoa(ipaddr),
1517c478bd9Sstevel@tonic-gate 	    hp ? hp->h_aliases : NULL);
1527c478bd9Sstevel@tonic-gate 	if (hp != NULL)
1537c478bd9Sstevel@tonic-gate 		freehostent(hp);
1547c478bd9Sstevel@tonic-gate 	return (retval);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static struct hostdata *
ip6lookup(const struct in6_addr * ip6addr)15845916cd2Sjpk ip6lookup(const struct in6_addr *ip6addr)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	struct hostdata6 *h;
1617c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
1627c478bd9Sstevel@tonic-gate 	int error_num;
1637c478bd9Sstevel@tonic-gate 	char addrstr[INET6_ADDRSTRLEN];
1642e3b6467Skcpoon 	char *addname;
1657c478bd9Sstevel@tonic-gate 	struct hostdata *retval;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	for (h = h_table6[iphash(((uint32_t *)ip6addr)[3])]; h;
1687c478bd9Sstevel@tonic-gate 	    h = h->h6_next) {
1697c478bd9Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&h->h6_addr, ip6addr))
1707c478bd9Sstevel@tonic-gate 			return ((struct hostdata *)h);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* not in the hash table, put it in */
1747c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(ip6addr))
1757c478bd9Sstevel@tonic-gate 		return (addhost(AF_INET6, ip6addr, "UNSPECIFIED", NULL));
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/*
1787c478bd9Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
1797c478bd9Sstevel@tonic-gate 	 * an unresponsive name server.
1807c478bd9Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
1817c478bd9Sstevel@tonic-gate 	 */
182*afee3dc6SRobert Mustacchi 	if (!rflg) {
183*afee3dc6SRobert Mustacchi 		if (sigsetjmp(nisjmp, 1) == 0) {
184*afee3dc6SRobert Mustacchi 			(void) snoop_alarm(3, wakeup);
185*afee3dc6SRobert Mustacchi 			hp = getipnodebyaddr(ip6addr, sizeof (struct in6_addr),
186*afee3dc6SRobert Mustacchi 			    AF_INET6, &error_num);
187*afee3dc6SRobert Mustacchi 			(void) snoop_alarm(0, wakeup);
188*afee3dc6SRobert Mustacchi 		} else {
189*afee3dc6SRobert Mustacchi 			snoop_nswarn();
190*afee3dc6SRobert Mustacchi 		}
1917c478bd9Sstevel@tonic-gate 	} else {
1927c478bd9Sstevel@tonic-gate 		hp = NULL;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (hp != NULL)
1967c478bd9Sstevel@tonic-gate 		addname = hp->h_name;
1977c478bd9Sstevel@tonic-gate 	else {
1987c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, ip6addr, addrstr, INET6_ADDRSTRLEN);
1997c478bd9Sstevel@tonic-gate 		addname = addrstr;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	retval = addhost(AF_INET6, ip6addr, addname, hp ? hp->h_aliases : NULL);
2037c478bd9Sstevel@tonic-gate 	if (hp != NULL)
2047c478bd9Sstevel@tonic-gate 		freehostent(hp);
2057c478bd9Sstevel@tonic-gate 	return (retval);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static struct hostdata *
addhost(int family,const void * ipaddr,const char * name,char ** aliases)20945916cd2Sjpk addhost(int family, const void *ipaddr, const char *name, char **aliases)
2107c478bd9Sstevel@tonic-gate {
21145916cd2Sjpk 	struct hostdata **hp, *n = NULL;
2127c478bd9Sstevel@tonic-gate 	extern FILE *namefile;
2137c478bd9Sstevel@tonic-gate 	int hashval;
2147c478bd9Sstevel@tonic-gate 	static char aname[128];
2157c478bd9Sstevel@tonic-gate 	char *np;
2167c478bd9Sstevel@tonic-gate 	static struct hostdata h;
2177c478bd9Sstevel@tonic-gate 	int ind;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	switch (family) {
2207c478bd9Sstevel@tonic-gate 	case AF_INET:
2217c478bd9Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata4));
2227c478bd9Sstevel@tonic-gate 		if (n == NULL)
2237c478bd9Sstevel@tonic-gate 			goto alloc_failed;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		memset(n, 0, sizeof (struct hostdata4));
2267c478bd9Sstevel@tonic-gate 		n->h_hostname = strdup(name);
2277c478bd9Sstevel@tonic-gate 		if (n->h_hostname == NULL)
2287c478bd9Sstevel@tonic-gate 			goto alloc_failed;
2297c478bd9Sstevel@tonic-gate 
23045916cd2Sjpk 		((struct hostdata4 *)n)->h4_addr =
23145916cd2Sjpk 		    *(const struct in_addr *)ipaddr;
2327c478bd9Sstevel@tonic-gate 		hashval = ((struct in_addr *)ipaddr)->s_addr;
2337c478bd9Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table4[iphash(hashval)];
2347c478bd9Sstevel@tonic-gate 		break;
2357c478bd9Sstevel@tonic-gate 	case AF_INET6:
2367c478bd9Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata6));
2377c478bd9Sstevel@tonic-gate 		if (n == NULL)
2387c478bd9Sstevel@tonic-gate 			goto alloc_failed;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		memset(n, 0, sizeof (struct hostdata6));
2417c478bd9Sstevel@tonic-gate 		n->h_hostname = strdup(name);
2427c478bd9Sstevel@tonic-gate 		if (n->h_hostname == NULL)
2437c478bd9Sstevel@tonic-gate 			goto alloc_failed;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		memcpy(&((struct hostdata6 *)n)->h6_addr, ipaddr,
2467c478bd9Sstevel@tonic-gate 		    sizeof (struct in6_addr));
24745916cd2Sjpk 		hashval = ((const int *)ipaddr)[3];
2487c478bd9Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table6[iphash(hashval)];
2497c478bd9Sstevel@tonic-gate 		break;
2507c478bd9Sstevel@tonic-gate 	default:
2517c478bd9Sstevel@tonic-gate 		fprintf(stderr, "snoop: ERROR: Unknown address family: %d",
2527c478bd9Sstevel@tonic-gate 		    family);
2537c478bd9Sstevel@tonic-gate 		exit(1);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	n->h_next = *hp;
2577c478bd9Sstevel@tonic-gate 	*hp = n;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (namefile != NULL) {
2607c478bd9Sstevel@tonic-gate 		if (family == AF_INET) {
26145916cd2Sjpk 			np = inet_ntoa(*(const struct in_addr *)ipaddr);
2627c478bd9Sstevel@tonic-gate 			if (np) {
2637c478bd9Sstevel@tonic-gate 				(void) fprintf(namefile, "%s\t%s", np, name);
2647c478bd9Sstevel@tonic-gate 				if (aliases) {
2657c478bd9Sstevel@tonic-gate 					for (ind = 0;
2667c478bd9Sstevel@tonic-gate 					    aliases[ind] != NULL;
2677c478bd9Sstevel@tonic-gate 					    ind++) {
2687c478bd9Sstevel@tonic-gate 						(void) fprintf(namefile, " %s",
269b127ac41SPhilip Kirk 						    aliases[ind]);
2707c478bd9Sstevel@tonic-gate 					}
2717c478bd9Sstevel@tonic-gate 				}
2727c478bd9Sstevel@tonic-gate 				(void) fprintf(namefile, "\n");
2737c478bd9Sstevel@tonic-gate 			}
2747c478bd9Sstevel@tonic-gate 		} else if (family == AF_INET6) {
2757c478bd9Sstevel@tonic-gate 			np = (char *)inet_ntop(AF_INET6, (void *)ipaddr, aname,
276b127ac41SPhilip Kirk 			    sizeof (aname));
2777c478bd9Sstevel@tonic-gate 			if (np) {
2787c478bd9Sstevel@tonic-gate 				(void) fprintf(namefile, "%s\t%s", np, name);
2797c478bd9Sstevel@tonic-gate 				if (aliases) {
2807c478bd9Sstevel@tonic-gate 					for (ind = 0;
2817c478bd9Sstevel@tonic-gate 					    aliases[ind] != NULL;
2827c478bd9Sstevel@tonic-gate 					    ind++) {
2837c478bd9Sstevel@tonic-gate 						(void) fprintf(namefile, " %s",
284b127ac41SPhilip Kirk 						    aliases[ind]);
2857c478bd9Sstevel@tonic-gate 					}
2867c478bd9Sstevel@tonic-gate 				}
2877c478bd9Sstevel@tonic-gate 				(void) fprintf(namefile, "\n");
2887c478bd9Sstevel@tonic-gate 			}
2897c478bd9Sstevel@tonic-gate 		} else {
2907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "addhost: unknown family %d\n",
291b127ac41SPhilip Kirk 			    family);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	return (n);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate alloc_failed:
2977c478bd9Sstevel@tonic-gate 	if (n)
2987c478bd9Sstevel@tonic-gate 		free(n);
2997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "addhost: no mem\n");
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	aname[0] = '\0';
3027c478bd9Sstevel@tonic-gate 	memset(&h, 0, sizeof (struct hostdata));
3037c478bd9Sstevel@tonic-gate 	h.h_hostname = aname;
3047c478bd9Sstevel@tonic-gate 	return (&h);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate char *
addrtoname(int family,const void * ipaddr)30845916cd2Sjpk addrtoname(int family, const void *ipaddr)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	switch (family) {
3117c478bd9Sstevel@tonic-gate 	case AF_INET:
31245916cd2Sjpk 		return (iplookup(*(const struct in_addr *)ipaddr)->h_hostname);
3137c478bd9Sstevel@tonic-gate 	case AF_INET6:
31445916cd2Sjpk 		return (ip6lookup((const struct in6_addr *)ipaddr)->h_hostname);
3157c478bd9Sstevel@tonic-gate 	}
31645916cd2Sjpk 	(void) fprintf(stderr, "snoop: ERROR: unknown address family: %d\n",
31745916cd2Sjpk 	    family);
31845916cd2Sjpk 	exit(1);
31945916cd2Sjpk 	/* NOTREACHED */
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate void
load_names(char * fname)323*afee3dc6SRobert Mustacchi load_names(char *fname)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	char buf[1024];
3267c478bd9Sstevel@tonic-gate 	char *addr, *name, *alias;
3277c478bd9Sstevel@tonic-gate 	FILE *f;
3287c478bd9Sstevel@tonic-gate 	unsigned int addrv4;
3297c478bd9Sstevel@tonic-gate 	struct in6_addr addrv6;
3307c478bd9Sstevel@tonic-gate 	int family;
3317c478bd9Sstevel@tonic-gate 	void *naddr;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Loading name file %s\n", fname);
3347c478bd9Sstevel@tonic-gate 	f = fopen(fname, "r");
3357c478bd9Sstevel@tonic-gate 	if (f == NULL) {
3367c478bd9Sstevel@tonic-gate 		perror(fname);
3377c478bd9Sstevel@tonic-gate 		return;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	while (fgets(buf, 1024, f) != NULL) {
3417c478bd9Sstevel@tonic-gate 		addr = strtok(buf, SEPARATORS);
3427c478bd9Sstevel@tonic-gate 		if (addr == NULL || *addr == '#')
3437c478bd9Sstevel@tonic-gate 			continue;
3447c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET6, addr, (void *)&addrv6) == 1) {
3457c478bd9Sstevel@tonic-gate 			family = AF_INET6;
3467c478bd9Sstevel@tonic-gate 			naddr = (void *)&addrv6;
34745916cd2Sjpk 		} else if ((addrv4 = inet_addr(addr)) != (ulong_t)-1) {
3487c478bd9Sstevel@tonic-gate 			family = AF_INET;
3497c478bd9Sstevel@tonic-gate 			naddr = (void *)&addrv4;
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		name = strtok(NULL, SEPARATORS);
3527c478bd9Sstevel@tonic-gate 		if (name == NULL)
3537c478bd9Sstevel@tonic-gate 			continue;
35445916cd2Sjpk 		while ((alias = strtok(NULL, SEPARATORS)) != NULL &&
35545916cd2Sjpk 		    (*alias != '#')) {
3567c478bd9Sstevel@tonic-gate 			(void) addhost(family, naddr, alias, NULL);
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 		(void) addhost(family, naddr, name, NULL);
3597c478bd9Sstevel@tonic-gate 		/* Note: certain addresses such as broadcast are skipped */
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	(void) fclose(f);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * lgetipnodebyname: looks up hostname in cached address data. This allows
3677c478bd9Sstevel@tonic-gate  * filtering on hostnames from the .names file to work properly, and
3687c478bd9Sstevel@tonic-gate  * avoids name clashes between domains. Note that only the first of the
3697c478bd9Sstevel@tonic-gate  * ipv4, ipv6, or v4mapped address will be returned, because the
3707c478bd9Sstevel@tonic-gate  * cache does not contain information on multi-homed hosts.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3737c478bd9Sstevel@tonic-gate struct hostent *
lgetipnodebyname(const char * name,int af,int flags,int * error_num)3747c478bd9Sstevel@tonic-gate lgetipnodebyname(const char *name, int af, int flags, int *error_num)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	int i;
3777c478bd9Sstevel@tonic-gate 	struct hostdata4 *h;
3787c478bd9Sstevel@tonic-gate 	struct hostdata6 *h6;
3797c478bd9Sstevel@tonic-gate 	static struct hostent he;		/* host entry */
3807c478bd9Sstevel@tonic-gate 	static struct in6_addr h46_addr[MAXADDRS];	/* v4mapped address */
3817c478bd9Sstevel@tonic-gate 	static char h_name[MAXHOSTNAMELEN];	/* hostname */
3827c478bd9Sstevel@tonic-gate 	static char *list[MAXADDRS];		/* addr_list array */
3837c478bd9Sstevel@tonic-gate 	struct hostent *hp = &he;
3847c478bd9Sstevel@tonic-gate 	int ind;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	(void) memset((char *)hp, 0, sizeof (struct hostent));
3877c478bd9Sstevel@tonic-gate 	hp->h_name = h_name;
3887c478bd9Sstevel@tonic-gate 	h_name[0] = '\0';
3897c478bd9Sstevel@tonic-gate 	strcpy(h_name, name);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	hp->h_addrtype = AF_INET6;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	hp->h_addr_list = list;
3947c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXADDRS; i++)
3957c478bd9Sstevel@tonic-gate 		hp->h_addr_list[i] = NULL;
3967c478bd9Sstevel@tonic-gate 	ind = 0;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/* ipv6 lookup */
3997c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
4007c478bd9Sstevel@tonic-gate 		hp->h_length = sizeof (struct in6_addr);
4017c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAXHASH; i++) {
4027c478bd9Sstevel@tonic-gate 			for (h6 = h_table6[i]; h6; h6 = h6->h6_next) {
4037c478bd9Sstevel@tonic-gate 				if (strcmp(name, h6->h6_hostname) == 0) {
4047c478bd9Sstevel@tonic-gate 					if (ind >= MAXADDRS - 1) {
4057c478bd9Sstevel@tonic-gate 						/* too many addresses */
4067c478bd9Sstevel@tonic-gate 						return (hp);
4077c478bd9Sstevel@tonic-gate 					}
4087c478bd9Sstevel@tonic-gate 					/* found ipv6 addr */
4097c478bd9Sstevel@tonic-gate 					hp->h_addr_list[ind] =
410b127ac41SPhilip Kirk 					    (char *)&h6->h6_addr;
4117c478bd9Sstevel@tonic-gate 					ind++;
4127c478bd9Sstevel@tonic-gate 				}
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	/* ipv4 or v4mapped lookup */
4177c478bd9Sstevel@tonic-gate 	if (af == AF_INET || (flags & AI_ALL)) {
4187c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAXHASH; i++) {
4197c478bd9Sstevel@tonic-gate 			for (h = h_table4[i]; h; h = h->h4_next) {
4207c478bd9Sstevel@tonic-gate 				if (strcmp(name, h->h4_hostname) == 0) {
4217c478bd9Sstevel@tonic-gate 					if (ind >= MAXADDRS - 1) {
4227c478bd9Sstevel@tonic-gate 						/* too many addresses */
4237c478bd9Sstevel@tonic-gate 						return (hp);
4247c478bd9Sstevel@tonic-gate 					}
4257c478bd9Sstevel@tonic-gate 					if (af == AF_INET) {
4267c478bd9Sstevel@tonic-gate 						/* found ipv4 addr */
4277c478bd9Sstevel@tonic-gate 						hp->h_addrtype = AF_INET;
4287c478bd9Sstevel@tonic-gate 						hp->h_length =
4297c478bd9Sstevel@tonic-gate 						    sizeof (struct in_addr);
4307c478bd9Sstevel@tonic-gate 						hp->h_addr_list[ind] =
4317c478bd9Sstevel@tonic-gate 						    (char *)&h->h4_addr;
4327c478bd9Sstevel@tonic-gate 						ind++;
4337c478bd9Sstevel@tonic-gate 					} else {
4347c478bd9Sstevel@tonic-gate 						/* found v4mapped addr */
4357c478bd9Sstevel@tonic-gate 						hp->h_length =
4367c478bd9Sstevel@tonic-gate 						    sizeof (struct in6_addr);
4377c478bd9Sstevel@tonic-gate 						hp->h_addr_list[ind] =
4387c478bd9Sstevel@tonic-gate 						    (char *)&h46_addr[ind];
4397c478bd9Sstevel@tonic-gate 						IN6_INADDR_TO_V4MAPPED(
440b127ac41SPhilip Kirk 						    &h->h4_addr,
441b127ac41SPhilip Kirk 						    &h46_addr[ind]);
4427c478bd9Sstevel@tonic-gate 						ind++;
4437c478bd9Sstevel@tonic-gate 					}
4447c478bd9Sstevel@tonic-gate 				}
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	return (ind > 0 ? hp : NULL);
4497c478bd9Sstevel@tonic-gate }
450