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