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