xref: /netbsd-src/usr.bin/netstat/atalk.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: atalk.c,v 1.10 2006/04/06 18:30:31 rpaulo Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from @(#)atalk.c	1.1 (Whistle) 6/6/96";
36 #else
37 __RCSID("$NetBSD: atalk.c,v 1.10 2006/04/06 18:30:31 rpaulo Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 
48 #include <net/route.h>
49 #include <net/if.h>
50 
51 #include <netinet/tcp_fsm.h>
52 
53 #include <netatalk/at.h>
54 #include <netatalk/ddp_var.h>
55 
56 #include <nlist.h>
57 #include <kvm.h>
58 #include <errno.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include "netstat.h"
62 
63 struct ddpcb    ddpcb;
64 struct socket   sockb;
65 
66 static int first = 1;
67 
68 static char *at_pr_net __P((struct sockaddr_at *, int));
69 static char *at_pr_host __P((struct sockaddr_at *, int));
70 static char *at_pr_range __P((struct sockaddr_at *));
71 static char *at_pr_port __P((struct sockaddr_at *));
72 
73 /*
74  * Print a summary of connections related to a Network Systems
75  * protocol.  For XXX, also give state of connection.
76  * Listening processes (aflag) are suppressed unless the
77  * -a (all) flag is specified.
78  */
79 
80 static char *
81 at_pr_net(sat, numeric)
82 	struct sockaddr_at *sat;
83 	int numeric;
84 {
85 	static char mybuf[50];
86 
87 	if (!numeric) {
88 		switch (sat->sat_addr.s_net) {
89 		case 0xffff:
90 			return "????";
91 		case ATADDR_ANYNET:
92 			return ("*");
93 		}
94 	}
95 	(void)snprintf(mybuf, sizeof(mybuf), "%hu", ntohs(sat->sat_addr.s_net));
96 	return (mybuf);
97 }
98 
99 static char *
100 at_pr_host(sat, numeric)
101 	struct sockaddr_at *sat;
102 	int numeric;
103 {
104 	static char mybuf[50];
105 
106 	if (!numeric) {
107 		switch (sat->sat_addr.s_node) {
108 		case ATADDR_BCAST:
109 			return "bcast";
110 		case ATADDR_ANYNODE:
111 			return ("*");
112 		}
113 	}
114 	(void)snprintf(mybuf, sizeof(mybuf), "%d",
115 	    (unsigned int)sat->sat_addr.s_node);
116 	return (mybuf);
117 }
118 
119 static char *
120 at_pr_port(sat)
121 	struct sockaddr_at *sat;
122 {
123 	static char mybuf[50];
124 
125 	switch (sat->sat_port) {
126 	case ATADDR_ANYPORT:
127 		return ("*");
128 	case 0xff:
129 		return "????";
130 	default:
131 		(void)snprintf(mybuf, sizeof(mybuf), "%d",
132 		    (unsigned int)sat->sat_port);
133 		return (mybuf);
134 	}
135 }
136 
137 static char *
138 at_pr_range(sat)
139 	struct sockaddr_at *sat;
140 {
141 	static char mybuf[50];
142 
143 	if (sat->sat_range.r_netrange.nr_firstnet
144 	    != sat->sat_range.r_netrange.nr_lastnet) {
145 		(void)snprintf(mybuf, sizeof(mybuf), "%d-%d",
146 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
147 			ntohs(sat->sat_range.r_netrange.nr_lastnet));
148 	} else {
149 		(void)snprintf(mybuf, sizeof(mybuf), "%d",
150 			ntohs(sat->sat_range.r_netrange.nr_firstnet));
151 	}
152 	return (mybuf);
153 }
154 
155 
156 /* what == 0 for addr only == 3
157  *	1 for net
158  *	2 for host
159  *	4 for port
160  *	8 for numeric only
161  */
162 char *
163 atalk_print(sa, what)
164 	const struct sockaddr *sa;
165 	int what;
166 {
167 	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
168 	static char mybuf[50];
169 	int numeric = (what & 0x08);
170 
171 	mybuf[0] = 0;
172 	switch (what & 0x13) {
173 	case 0:
174 		mybuf[0] = 0;
175 		break;
176 	case 1:
177 		(void)snprintf(mybuf, sizeof(mybuf), "%s",
178 		    at_pr_net(sat, numeric));
179 		break;
180 	case 2:
181 		(void)snprintf(mybuf, sizeof(mybuf), "%s",
182 		    at_pr_host(sat, numeric));
183 		break;
184 	case 3:
185 		(void)snprintf(mybuf, sizeof(mybuf), "%s.%s",
186 			at_pr_net(sat, numeric),
187 			at_pr_host(sat, numeric));
188 		break;
189 	case 0x10:
190 		(void)snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
191 	}
192 	if (what & 4) {
193 		(void)snprintf(mybuf + strlen(mybuf),
194 		    sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat));
195 	}
196 	return (mybuf);
197 }
198 
199 char *
200 atalk_print2(sa, mask, what)
201 	const struct sockaddr *sa;
202 	const struct sockaddr *mask;
203 	int what;
204 {
205 	int		n, l;
206 	static char     buf[100];
207 	struct sockaddr_at *sat1, *sat2;
208 	struct sockaddr_at thesockaddr;
209 	struct sockaddr *sa2;
210 
211 	sat1 = (struct sockaddr_at *) sa;
212 	sat2 = (struct sockaddr_at *) mask;
213 	sa2 = (struct sockaddr *) & thesockaddr;
214 
215 	thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
216 	    sat2->sat_addr.s_net;
217 	n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8)));
218 	if (n >= sizeof(buf))
219 		n = sizeof(buf) - 1;
220 	else if (n == -1)
221 		n = 0;	/* What else can be done ? */
222 	if (sat2->sat_addr.s_net != 0xFFFF) {
223 		thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
224 		    ~sat2->sat_addr.s_net;
225 		l = snprintf(buf + n, sizeof(buf) - n,
226 		    "-%s", atalk_print(sa2, 1 | (what & 8)));
227 		if (l >= sizeof(buf) - n)
228 			l = sizeof(buf) - n - 1;
229 		if (l > 0)
230 			n += l;
231 	}
232 	if (what & 2) {
233 		l = snprintf(buf + n, sizeof(buf) - n, ".%s",
234 		    atalk_print(sa, what & (~1)));
235 		if (l >= sizeof(buf) - n)
236 			l = sizeof(buf) - n - 1;
237 		if (l > 0)
238 			n += l;
239 	}
240 	return (buf);
241 }
242 
243 void
244 atalkprotopr(off, name)
245 	u_long off;
246 	char  *name;
247 {
248 	struct ddpcb    cb;
249 	struct ddpcb *prev, *next;
250 	struct ddpcb   *initial;
251 	int width = 22;
252 	if (off == 0)
253 		return;
254 	if (kread(off, (char *)&initial, sizeof(struct ddpcb *)) < 0)
255 		return;
256 	ddpcb = cb;
257 	prev = (struct ddpcb *)off;
258 	for (next = initial; next != NULL; prev = next) {
259 		u_long	ppcb = (u_long)next;
260 
261 		if (kread((u_long)next, (char *)&ddpcb, sizeof(ddpcb)) < 0)
262 			return;
263 		next = ddpcb.ddp_next;
264 #if 0
265 		if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) {
266 			continue;
267 		}
268 #endif
269 		if (kread((u_long)ddpcb.ddp_socket,
270 			  (char *)&sockb, sizeof(sockb)) < 0)
271 			return;
272 		if (first) {
273 			printf("Active ATALK connections");
274 			if (aflag)
275 				printf(" (including servers)");
276 			putchar('\n');
277 			if (Aflag) {
278 				width = 18;
279 				printf("%-8.8s ", "PCB");
280 			}
281 			printf("%-5.5s %-6.6s %-6.6s  %*.*s %*.*s %s\n",
282 			       "Proto", "Recv-Q", "Send-Q",
283 			       -width, width, "Local Address",
284 			       -width, width, "Foreign Address", "(state)");
285 			first = 0;
286 		}
287 		if (Aflag)
288 			printf("%8lx ", ppcb);
289 		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
290 		       sockb.so_snd.sb_cc);
291 		printf(" %*.*s", -width, width,
292 		       atalk_print((struct sockaddr *)&ddpcb.ddp_lsat, 7));
293 		printf(" %*.*s", -width, width,
294 		       atalk_print((struct sockaddr *)&ddpcb.ddp_fsat, 7));
295 		putchar('\n');
296 	}
297 }
298 #define ANY(x,y,z) \
299 	((sflag==1 || (x)) ? printf("\t%ld %s%s%s\n",x,y,plural(x),z) : 0)
300 
301 /*
302  * Dump DDP statistics structure.
303  */
304 void
305 ddp_stats(off, name)
306 	u_long off;
307 	char *name;
308 {
309 	struct ddpstat  ddpstat;
310 
311 	if (off == 0)
312 		return;
313 	if (kread(off, (char *)&ddpstat, sizeof(ddpstat)) < 0)
314 		return;
315 	printf("%s:\n", name);
316 	ANY(ddpstat.ddps_short, "packet", " with short headers ");
317 	ANY(ddpstat.ddps_long, "packet", " with long headers ");
318 	ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
319 	ANY(ddpstat.ddps_tooshort, "packet", " too short ");
320 	ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
321 	ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
322 	ANY(ddpstat.ddps_forward, "packet", " forwarded ");
323 	ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
324 	ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
325 	ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
326 }
327 #undef ANY
328