]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/ns.c
8ceff197d6523b13a899855f945478b9fbdabb68
[apple/network_cmds.git] / netstat.tproj / ns.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1983, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #ifndef lint
58 static char sccsid[] = "@(#)ns.c 8.1 (Berkeley) 6/6/93";
59 #endif /* not lint */
60
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/mbuf.h>
65
66 #include <net/route.h>
67 #include <net/if.h>
68
69 #include <netinet/tcp_fsm.h>
70
71 #include <netns/ns.h>
72 #include <netns/ns_pcb.h>
73 #include <netns/idp.h>
74 #include <netns/idp_var.h>
75 #include <netns/ns_error.h>
76 #include <netns/sp.h>
77 #include <netns/spidp.h>
78 #include <netns/spp_timer.h>
79 #include <netns/spp_var.h>
80 #define SANAMES
81 #include <netns/spp_debug.h>
82
83 #include <nlist.h>
84 #include <errno.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include "netstat.h"
88
89 struct nspcb nspcb;
90 struct sppcb sppcb;
91 static struct socket sockb;
92
93 static char *ns_prpr __P((struct ns_addr *));
94 static void ns_erputil __P((int, int));
95
96 static int first = 1;
97
98 /*
99 * Print a summary of connections related to a Network Systems
100 * protocol. For SPP, also give state of connection.
101 * Listening processes (aflag) are suppressed unless the
102 * -a (all) flag is specified.
103 */
104
105 void
106 nsprotopr(off, name)
107 u_long off;
108 char *name;
109 {
110 struct nspcb cb;
111 register struct nspcb *prev, *next;
112 int isspp;
113
114 if (off == 0)
115 return;
116 isspp = strcmp(name, "spp") == 0;
117 kread(off, (char *)&cb, sizeof (struct nspcb));
118 nspcb = cb;
119 prev = (struct nspcb *)off;
120 if (nspcb.nsp_next == (struct nspcb *)off)
121 return;
122 for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
123 u_long ppcb;
124
125 next = nspcb.nsp_next;
126 kread((u_long)next, (char *)&nspcb, sizeof (nspcb));
127 if (nspcb.nsp_prev != prev) {
128 printf("???\n");
129 break;
130 }
131 if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
132 continue;
133 }
134 kread((u_long)nspcb.nsp_socket,
135 (char *)&sockb, sizeof (sockb));
136 ppcb = (u_long) nspcb.nsp_pcb;
137 if (ppcb) {
138 if (isspp) {
139 kread(ppcb, (char *)&sppcb, sizeof (sppcb));
140 } else continue;
141 } else
142 if (isspp) continue;
143 if (first) {
144 printf("Active NS connections");
145 if (aflag)
146 printf(" (including servers)");
147 putchar('\n');
148 if (Aflag)
149 printf("%-8.8s ", "PCB");
150 printf(Aflag ?
151 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
152 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
153 "Proto", "Recv-Q", "Send-Q",
154 "Local Address", "Foreign Address", "(state)");
155 first = 0;
156 }
157 if (Aflag)
158 printf("%8x ", ppcb);
159 printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
160 sockb.so_snd.sb_cc);
161 printf(" %-22.22s", ns_prpr(&nspcb.nsp_laddr));
162 printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
163 if (isspp) {
164 extern char *tcpstates[];
165 if (sppcb.s_state >= TCP_NSTATES)
166 printf(" %d", sppcb.s_state);
167 else
168 printf(" %s", tcpstates[sppcb.s_state]);
169 }
170 putchar('\n');
171 prev = next;
172 }
173 }
174 #define ANY(x,y,z) \
175 ((x) ? printf("\t%d %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
176
177 /*
178 * Dump SPP statistics structure.
179 */
180 void
181 spp_stats(off, name)
182 u_long off;
183 char *name;
184 {
185 struct spp_istat spp_istat;
186 #define sppstat spp_istat.newstats
187
188 if (off == 0)
189 return;
190 kread(off, (char *)&spp_istat, sizeof (spp_istat));
191 printf("%s:\n", name);
192 ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
193 ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
194 ANY(spp_istat.nonucn, "connection",
195 " dropped due to inability to connect");
196 ANY(spp_istat.noconn, "connection",
197 " dropped due to inability to connect");
198 ANY(spp_istat.notme, "connection",
199 " incompleted due to mismatched id's");
200 ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
201 ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
202 ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
203 ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
204 ANY(sppstat.spps_connattempt, "connection", " initiated");
205 ANY(sppstat.spps_accepts, "connection", " accepted");
206 ANY(sppstat.spps_connects, "connection", " established");
207 ANY(sppstat.spps_drops, "connection", " dropped");
208 ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
209 ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
210 ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
211 ANY(sppstat.spps_rttupdated, "time", " we got rtt");
212 ANY(sppstat.spps_delack, "delayed ack", " sent");
213 ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
214 ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
215 ANY(sppstat.spps_persisttimeo, "persist timeout", "");
216 ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
217 ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
218 ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
219 ANY(sppstat.spps_sndtotal, "total packet", " sent");
220 ANY(sppstat.spps_sndpack, "data packet", " sent");
221 ANY(sppstat.spps_sndbyte, "data byte", " sent");
222 ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
223 ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
224 ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
225 ANY(sppstat.spps_sndprobe, "window probe", " sent");
226 ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
227 ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
228 ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
229 ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
230 ANY(sppstat.spps_rcvtotal, "total packet", " received");
231 ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
232 ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
233 ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
234 ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
235 ANY(sppstat.spps_rcvshort, "packet", " received too short");
236 ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
237 ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
238 ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
239 ANY(sppstat.spps_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
240 ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
241 ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
242 ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
243 ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
244 ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
245 ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
246 ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
247 ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
248 ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
249 ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
250 ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
251 }
252 #undef ANY
253 #define ANY(x,y,z) ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
254
255 /*
256 * Dump IDP statistics structure.
257 */
258 void
259 idp_stats(off, name)
260 u_long off;
261 char *name;
262 {
263 struct idpstat idpstat;
264
265 if (off == 0)
266 return;
267 kread(off, (char *)&idpstat, sizeof (idpstat));
268 printf("%s:\n", name);
269 ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
270 ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
271 ANY(idpstat.idps_badsum, "packet", " with bad checksums");
272 }
273
274 static struct {
275 u_short code;
276 char *name;
277 char *where;
278 } ns_errnames[] = {
279 {0, "Unspecified Error", " at Destination"},
280 {1, "Bad Checksum", " at Destination"},
281 {2, "No Listener", " at Socket"},
282 {3, "Packet", " Refused due to lack of space at Destination"},
283 {01000, "Unspecified Error", " while gatewayed"},
284 {01001, "Bad Checksum", " while gatewayed"},
285 {01002, "Packet", " forwarded too many times"},
286 {01003, "Packet", " too large to be forwarded"},
287 {-1, 0, 0},
288 };
289
290 /*
291 * Dump NS Error statistics structure.
292 */
293 /*ARGSUSED*/
294 void
295 nserr_stats(off, name)
296 u_long off;
297 char *name;
298 {
299 struct ns_errstat ns_errstat;
300 register int j;
301 register int histoprint = 1;
302 int z;
303
304 if (off == 0)
305 return;
306 kread(off, (char *)&ns_errstat, sizeof (ns_errstat));
307 printf("NS error statistics:\n");
308 ANY(ns_errstat.ns_es_error, "call", " to ns_error");
309 ANY(ns_errstat.ns_es_oldshort, "error",
310 " ignored due to insufficient addressing");
311 ANY(ns_errstat.ns_es_oldns_err, "error request",
312 " in response to error packets");
313 ANY(ns_errstat.ns_es_tooshort, "error packet",
314 " received incomplete");
315 ANY(ns_errstat.ns_es_badcode, "error packet",
316 " received of unknown type");
317 for(j = 0; j < NS_ERR_MAX; j ++) {
318 z = ns_errstat.ns_es_outhist[j];
319 if (z && histoprint) {
320 printf("Output Error Histogram:\n");
321 histoprint = 0;
322 }
323 ns_erputil(z, ns_errstat.ns_es_codes[j]);
324
325 }
326 histoprint = 1;
327 for(j = 0; j < NS_ERR_MAX; j ++) {
328 z = ns_errstat.ns_es_inhist[j];
329 if (z && histoprint) {
330 printf("Input Error Histogram:\n");
331 histoprint = 0;
332 }
333 ns_erputil(z, ns_errstat.ns_es_codes[j]);
334 }
335 }
336
337 static void
338 ns_erputil(z, c)
339 int z, c;
340 {
341 int j;
342 char codebuf[30];
343 char *name, *where;
344
345 for(j = 0;; j ++) {
346 if ((name = ns_errnames[j].name) == 0)
347 break;
348 if (ns_errnames[j].code == c)
349 break;
350 }
351 if (name == 0) {
352 if (c > 01000)
353 where = "in transit";
354 else
355 where = "at destination";
356 sprintf(codebuf, "Unknown XNS error code 0%o", c);
357 name = codebuf;
358 } else
359 where = ns_errnames[j].where;
360 ANY(z, name, where);
361 }
362
363 static struct sockaddr_ns ssns = {AF_NS};
364
365 static
366 char *ns_prpr(x)
367 struct ns_addr *x;
368 {
369 struct sockaddr_ns *sns = &ssns;
370
371 sns->sns_addr = *x;
372 return(ns_print((struct sockaddr *)sns));
373 }