xref: /onnv-gate/usr/src/cmd/fs.d/nfs/nfslog/nfslog_ipaddr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1991, 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <ctype.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <malloc.h>
34*0Sstevel@tonic-gate #include <sys/socket.h>
35*0Sstevel@tonic-gate #include <sys/sockio.h>
36*0Sstevel@tonic-gate #include <arpa/inet.h>
37*0Sstevel@tonic-gate #include <net/if.h>
38*0Sstevel@tonic-gate #include <netinet/in.h>
39*0Sstevel@tonic-gate #include <netinet/in_systm.h>
40*0Sstevel@tonic-gate #include <netinet/if_ether.h>
41*0Sstevel@tonic-gate #include <netinet/ip.h>
42*0Sstevel@tonic-gate #include <netdb.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <signal.h>
45*0Sstevel@tonic-gate #include <setjmp.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Note: If making changes to this file, check also the file
49*0Sstevel@tonic-gate  *	 cmd/cmd-inet/usr.sbin/snoop/snoop_ipaddr.c
50*0Sstevel@tonic-gate  *	 as it has the same functions there.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate static jmp_buf nisjmp;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	MAXHASH 1024  /* must be a power of 2 */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate struct hostdata {
57*0Sstevel@tonic-gate 	struct hostdata *h_next;
58*0Sstevel@tonic-gate 	char		*h_hostname;
59*0Sstevel@tonic-gate 	int		h_pktsout;
60*0Sstevel@tonic-gate 	int		h_pktsin;
61*0Sstevel@tonic-gate };
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate struct hostdata4 {
64*0Sstevel@tonic-gate 	struct hostdata4	*h4_next;
65*0Sstevel@tonic-gate 	char		*h4_hostname;
66*0Sstevel@tonic-gate 	int		h4_pktsout;
67*0Sstevel@tonic-gate 	int		h4_pktsin;
68*0Sstevel@tonic-gate 	struct in_addr	h4_addr;
69*0Sstevel@tonic-gate };
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate struct hostdata6 {
72*0Sstevel@tonic-gate 	struct hostdata6	*h6_next;
73*0Sstevel@tonic-gate 	char		*h6_hostname;
74*0Sstevel@tonic-gate 	int		h6_pktsout;
75*0Sstevel@tonic-gate 	int		h6_pktsin;
76*0Sstevel@tonic-gate 	struct in6_addr	h6_addr;
77*0Sstevel@tonic-gate };
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static struct hostdata *addhost(int, void *, char *);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static struct hostdata4 *h_table4[MAXHASH];
82*0Sstevel@tonic-gate static struct hostdata6 *h_table6[MAXHASH];
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #define	iphash(e)  ((e) & (MAXHASH-1))
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /* ARGSUSED */
87*0Sstevel@tonic-gate static void
wakeup(int n)88*0Sstevel@tonic-gate wakeup(int n)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	longjmp(nisjmp, 1);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate extern char *inet_ntoa();
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static struct hostdata *
iplookup(ipaddr)96*0Sstevel@tonic-gate iplookup(ipaddr)
97*0Sstevel@tonic-gate 	struct in_addr *ipaddr;
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	register struct hostdata4 *h;
100*0Sstevel@tonic-gate 	struct hostent *hp = NULL;
101*0Sstevel@tonic-gate 	struct netent *np;
102*0Sstevel@tonic-gate 	int error_num;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	for (h = h_table4[iphash(ipaddr->s_addr)]; h; h = h->h4_next) {
105*0Sstevel@tonic-gate 		if (h->h4_addr.s_addr == ipaddr->s_addr)
106*0Sstevel@tonic-gate 			return ((struct hostdata *)h);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* not found.  Put it in */
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (ipaddr->s_addr == htonl(INADDR_BROADCAST))
112*0Sstevel@tonic-gate 		return (addhost(AF_INET, ipaddr, "BROADCAST"));
113*0Sstevel@tonic-gate 	if (ipaddr->s_addr == htonl(INADDR_ANY))
114*0Sstevel@tonic-gate 		return (addhost(AF_INET, ipaddr, "OLD-BROADCAST"));
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/*
117*0Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
118*0Sstevel@tonic-gate 	 * an unresponsive name server.
119*0Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
120*0Sstevel@tonic-gate 	 */
121*0Sstevel@tonic-gate 	if (setjmp(nisjmp) == 0) {
122*0Sstevel@tonic-gate 		(void) signal(SIGALRM, wakeup);
123*0Sstevel@tonic-gate 		(void) alarm(3);
124*0Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)ipaddr, sizeof (struct in_addr),
125*0Sstevel@tonic-gate 		    AF_INET, &error_num);
126*0Sstevel@tonic-gate 		if (hp == NULL && inet_lnaof(*ipaddr) == 0) {
127*0Sstevel@tonic-gate 			np = getnetbyaddr(inet_netof(*ipaddr), AF_INET);
128*0Sstevel@tonic-gate 			if (np)
129*0Sstevel@tonic-gate 				return (addhost(AF_INET, ipaddr, np->n_name));
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 		(void) alarm(0);
132*0Sstevel@tonic-gate 	} else {
133*0Sstevel@tonic-gate 		hp = NULL;
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return (addhost(AF_INET, ipaddr, hp ? hp->h_name : inet_ntoa(*ipaddr)));
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate static struct hostdata *
ip6lookup(ip6addr)140*0Sstevel@tonic-gate ip6lookup(ip6addr)
141*0Sstevel@tonic-gate 	struct in6_addr *ip6addr;
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	struct hostdata6 *h;
144*0Sstevel@tonic-gate 	struct hostent *hp = NULL;
145*0Sstevel@tonic-gate 	int error_num;
146*0Sstevel@tonic-gate 	char addrstr[INET6_ADDRSTRLEN];
147*0Sstevel@tonic-gate 	char *addname;
148*0Sstevel@tonic-gate 	struct hostdata *retval;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	for (h = h_table6[iphash(((uint32_t *)ip6addr)[3])]; h;
151*0Sstevel@tonic-gate 	    h = h->h6_next) {
152*0Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&h->h6_addr, ip6addr))
153*0Sstevel@tonic-gate 			return ((struct hostdata *)h);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* not in the hash table, put it in */
157*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(ip6addr))
158*0Sstevel@tonic-gate 		return (addhost(AF_INET6, ip6addr, "UNSPECIFIED"));
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/*
161*0Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
162*0Sstevel@tonic-gate 	 * an unresponsive name server.
163*0Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
164*0Sstevel@tonic-gate 	 */
165*0Sstevel@tonic-gate 	if (setjmp(nisjmp) == 0) {
166*0Sstevel@tonic-gate 		(void) signal(SIGALRM, wakeup);
167*0Sstevel@tonic-gate 		(void) alarm(3);
168*0Sstevel@tonic-gate 		hp = getipnodebyaddr(ip6addr, sizeof (struct in6_addr),
169*0Sstevel@tonic-gate 		    AF_INET6, &error_num);
170*0Sstevel@tonic-gate 		(void) alarm(0);
171*0Sstevel@tonic-gate 	} else {
172*0Sstevel@tonic-gate 		hp = NULL;
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (hp != NULL)
176*0Sstevel@tonic-gate 		addname = hp->h_name;
177*0Sstevel@tonic-gate 	else {
178*0Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, ip6addr, addrstr, INET6_ADDRSTRLEN);
179*0Sstevel@tonic-gate 		addname = addrstr;
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	retval = addhost(AF_INET6, ip6addr, addname);
183*0Sstevel@tonic-gate 	freehostent(hp);
184*0Sstevel@tonic-gate 	return (retval);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static struct hostdata *
addhost(family,ipaddr,name)188*0Sstevel@tonic-gate addhost(family, ipaddr, name)
189*0Sstevel@tonic-gate 	int family;
190*0Sstevel@tonic-gate 	void *ipaddr;
191*0Sstevel@tonic-gate 	char *name;
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	register struct hostdata **hp, *n;
194*0Sstevel@tonic-gate 	int hashval;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	switch (family) {
197*0Sstevel@tonic-gate 	case AF_INET:
198*0Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata4));
199*0Sstevel@tonic-gate 		if (n == NULL)
200*0Sstevel@tonic-gate 			goto alloc_failed;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 		(void) memset(n, 0, sizeof (struct hostdata4));
203*0Sstevel@tonic-gate 		n->h_hostname = strdup(name);
204*0Sstevel@tonic-gate 		if (n->h_hostname == NULL)
205*0Sstevel@tonic-gate 			goto alloc_failed;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 		((struct hostdata4 *)n)->h4_addr = *(struct in_addr *)ipaddr;
208*0Sstevel@tonic-gate 		hashval = ((struct in_addr *)ipaddr)->s_addr;
209*0Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table4[iphash(hashval)];
210*0Sstevel@tonic-gate 		break;
211*0Sstevel@tonic-gate 	case AF_INET6:
212*0Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata6));
213*0Sstevel@tonic-gate 		if (n == NULL)
214*0Sstevel@tonic-gate 			goto alloc_failed;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		(void) memset(n, 0, sizeof (struct hostdata6));
217*0Sstevel@tonic-gate 		n->h_hostname = strdup(name);
218*0Sstevel@tonic-gate 		if (n->h_hostname == NULL)
219*0Sstevel@tonic-gate 			goto alloc_failed;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		(void) memcpy(&((struct hostdata6 *)n)->h6_addr, ipaddr,
222*0Sstevel@tonic-gate 		    sizeof (struct in6_addr));
223*0Sstevel@tonic-gate 		hashval = ((int *)ipaddr)[3];
224*0Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table6[iphash(hashval)];
225*0Sstevel@tonic-gate 		break;
226*0Sstevel@tonic-gate 	default:
227*0Sstevel@tonic-gate 		(void) fprintf(stderr,
228*0Sstevel@tonic-gate 			"nfslog: addhost ERROR: Unknown address family: %d",
229*0Sstevel@tonic-gate 			family);
230*0Sstevel@tonic-gate 		return (NULL);
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	n->h_next = *hp;
234*0Sstevel@tonic-gate 	*hp = n;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	return (n);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate alloc_failed:
239*0Sstevel@tonic-gate 	(void) fprintf(stderr, "addhost: no mem\n");
240*0Sstevel@tonic-gate 	if (n != NULL)
241*0Sstevel@tonic-gate 		free(n);
242*0Sstevel@tonic-gate 	return (NULL);
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate char *
addrtoname(void * sockp)246*0Sstevel@tonic-gate addrtoname(void *sockp)
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	struct hostdata *hostp;
249*0Sstevel@tonic-gate 	int family = ((struct sockaddr_in *)sockp)->sin_family;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	switch (family) {
252*0Sstevel@tonic-gate 	case AF_INET:
253*0Sstevel@tonic-gate 		hostp = iplookup(&((struct sockaddr_in *)sockp)->sin_addr);
254*0Sstevel@tonic-gate 		break;
255*0Sstevel@tonic-gate 	case AF_INET6:
256*0Sstevel@tonic-gate 		hostp = ip6lookup(&((struct sockaddr_in6 *)sockp)->sin6_addr);
257*0Sstevel@tonic-gate 		break;
258*0Sstevel@tonic-gate 	default:
259*0Sstevel@tonic-gate 		(void) fprintf(stderr, "nfslog: ERROR: unknown address " \
260*0Sstevel@tonic-gate 		    "family: %d\n", family);
261*0Sstevel@tonic-gate 		hostp = NULL;
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 	return ((hostp != NULL) ? hostp->h_hostname : NULL);
264*0Sstevel@tonic-gate }
265