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