New directory structure.
[unix-history] / usr / src / usr.bin / netstat / ns.c
CommitLineData
a1f8d2f2
KS
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7
8#ifndef lint
bd91d3f9 9static char sccsid[] = "@(#)ns.c 5.5 (Berkeley) %G%";
a1f8d2f2
KS
10#endif not lint
11
12#include <stdio.h>
13#include <errno.h>
14#include <nlist.h>
15
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <sys/socketvar.h>
19#include <sys/mbuf.h>
20#include <sys/protosw.h>
21
22#include <net/route.h>
23#include <net/if.h>
24
25#include <netinet/tcp_fsm.h>
26#include <netinet/tcp_timer.h>
27
28#include <netns/ns.h>
29#include <netns/ns_pcb.h>
30#include <netns/idp.h>
31#include <netns/idp_var.h>
32#include <netns/ns_error.h>
33#include <netns/sp.h>
34#include <netns/spidp.h>
35#include <netns/spp_var.h>
36#define SANAMES
37#include <netns/spp_debug.h>
38
39
40struct nspcb nspcb;
41struct sppcb sppcb;
42struct socket sockb;
a1f8d2f2
KS
43extern int kmem;
44extern int Aflag;
45extern int aflag;
46extern int nflag;
f7c99b06 47extern char *plural();
a1f8d2f2
KS
48char *ns_prpr();
49
50static int first = 1;
51
52/*
53 * Print a summary of connections related to a Network Systems
54 * protocol. For SPP, also give state of connection.
55 * Listening processes (aflag) are suppressed unless the
56 * -a (all) flag is specified.
57 */
58
59nsprotopr(off, name)
60 off_t off;
61 char *name;
62{
63 struct nspcb cb;
64 register struct nspcb *prev, *next;
65 int isspp;
66
4a920942 67 if (off == 0)
a1f8d2f2 68 return;
a1f8d2f2
KS
69 isspp = strcmp(name, "spp") == 0;
70 klseek(kmem, off, 0);
f7c99b06 71 read(kmem, (char *)&cb, sizeof (struct nspcb));
a1f8d2f2
KS
72 nspcb = cb;
73 prev = (struct nspcb *)off;
4a920942
MK
74 if (nspcb.nsp_next == (struct nspcb *)off)
75 return;
a1f8d2f2 76 for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
a1f8d2f2
KS
77 off_t ppcb;
78
79 next = nspcb.nsp_next;
80 klseek(kmem, (off_t)next, 0);
f7c99b06 81 read(kmem, (char *)&nspcb, sizeof (nspcb));
a1f8d2f2
KS
82 if (nspcb.nsp_prev != prev) {
83 printf("???\n");
84 break;
85 }
86 if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
87 continue;
88 }
89 klseek(kmem, (off_t)nspcb.nsp_socket, 0);
f7c99b06 90 read(kmem, (char *)&sockb, sizeof (sockb));
a1f8d2f2
KS
91 ppcb = (off_t) nspcb.nsp_pcb;
92 if (ppcb) {
93 if (isspp) {
94 klseek(kmem, ppcb, 0);
f7c99b06 95 read(kmem, (char *)&sppcb, sizeof (sppcb));
a1f8d2f2
KS
96 } else continue;
97 } else
98 if (isspp) continue;
26ebe146
MK
99 if (first) {
100 printf("Active NS connections");
101 if (aflag)
102 printf(" (including servers)");
103 putchar('\n');
104 if (Aflag)
105 printf("%-8.8s ", "PCB");
106 printf(Aflag ?
107 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
108 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
109 "Proto", "Recv-Q", "Send-Q",
110 "Local Address", "Foreign Address", "(state)");
111 first = 0;
112 }
a1f8d2f2
KS
113 if (Aflag)
114 printf("%8x ", ppcb);
115 printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
116 sockb.so_snd.sb_cc);
117 printf(" %-22.22s", ns_prpr(&nspcb.nsp_laddr));
118 printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
119 if (isspp) {
120 extern char *tcpstates[];
f7c99b06 121 if (sppcb.s_state >= TCP_NSTATES)
a1f8d2f2
KS
122 printf(" %d", sppcb.s_state);
123 else
124 printf(" %s", tcpstates[sppcb.s_state]);
125 }
126 putchar('\n');
127 prev = next;
128 }
129}
bd91d3f9 130#define ANY(x,y,z) ((x) ? printf("\t%u %s%s%s\n",x,y,plural(x),z) : 0)
a1f8d2f2
KS
131
132/*
133 * Dump SPP statistics structure.
134 */
135spp_stats(off, name)
136 off_t off;
137 char *name;
138{
139 struct spp_istat spp_istat;
140
4a920942 141 if (off == 0)
a1f8d2f2 142 return;
a1f8d2f2
KS
143 klseek(kmem, off, 0);
144 read(kmem, (char *)&spp_istat, sizeof (spp_istat));
145 printf("%s:\n", name);
146 ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
147 ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
148 ANY(spp_istat.nonucn, "connection", " dropped due to inability to connect");
149 ANY(spp_istat.noconn, "connection", " dropped due to inability to connect");
150 ANY(spp_istat.notme, "connection", " incompleted due to mismatched id's");
151 ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
152 ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
153 ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
154 ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
155}
156
157/*
158 * Dump IDP statistics structure.
159 */
160idp_stats(off, name)
161 off_t off;
162 char *name;
163{
164 struct idpstat idpstat;
165
4a920942 166 if (off == 0)
a1f8d2f2 167 return;
a1f8d2f2
KS
168 klseek(kmem, off, 0);
169 read(kmem, (char *)&idpstat, sizeof (idpstat));
f7c99b06 170 printf("%s:\n", name);
a1f8d2f2
KS
171 ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
172 ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
173 ANY(idpstat.idps_badsum, "packet", " with bad checksums");
174}
175
176static char *((ns_errnames[])[2]) = {
177 {"Unspecified Error", " at Destination"},
178 {"Bad Checksum", " at Destination"},
179 {"No Listener", " at Socket"},
180 {"Packet", " Refused due to lack of space at Destination"},
181 {"Unspecified Error", " while gatewayed"},
182 {"Bad Checksum", " while gatewayed"},
183 {"Packet", " forwarded too many times"},
184 {"Packet", " too large to be forwarded"},
185};
186
187/*
188 * Dump NS Error statistics structure.
189 */
f7c99b06 190/*ARGSUSED*/
a1f8d2f2
KS
191nserr_stats(off, name)
192 off_t off;
193 char *name;
194{
195 struct ns_errstat ns_errstat;
196 register int j;
197 register int histoprint = 1;
198 int z;
199
4a920942 200 if (off == 0)
a1f8d2f2 201 return;
a1f8d2f2
KS
202 klseek(kmem, off, 0);
203 read(kmem, (char *)&ns_errstat, sizeof (ns_errstat));
204 printf("NS error statistics:\n");
205 ANY(ns_errstat.ns_es_error, "call", " to ns_error");
206 ANY(ns_errstat.ns_es_oldshort, "error",
207 " ignored due to insufficient addressing");
208 ANY(ns_errstat.ns_es_oldns_err, "error request",
209 " in response to error packets");
210 ANY(ns_errstat.ns_es_tooshort, "error packet",
211 " received incomplete");
212 ANY(ns_errstat.ns_es_badcode, "error packet",
213 " received of unknown type");
214 for(j = 0; j < NS_ERR_MAX; j ++) {
215 z = ns_errstat.ns_es_outhist[j];
216 if (z && histoprint) {
217 printf("Output Error Histogram:\n");
218 histoprint = 0;
219 }
220 ANY(z, ns_errnames[j][0], ns_errnames[j][1]);
221 }
222 histoprint = 1;
223 for(j = 0; j < NS_ERR_MAX; j ++) {
224 z = ns_errstat.ns_es_inhist[j];
225 if (z && histoprint) {
226 printf("Input Error Histogram:\n");
227 histoprint = 0;
228 }
229 ANY(z, ns_errnames[j][0], ns_errnames[j][1]);
230 }
231}
232static struct sockaddr_ns ssns = {AF_NS};
233
234char *ns_prpr(x)
235struct ns_addr *x;
236{
237 extern char *ns_print();
238 struct sockaddr_ns *sns = &ssns;
239 sns->sns_addr = *x;
240 return(ns_print(sns));
241}