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