Release 6
[unix-history] / usr / src / usr.bin / netstat / main.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1983, 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
15static char sccsid[] = "@(#)main.c 5.31 (Berkeley) %G%";
16#endif /* not lint */
17
18#include <sys/param.h>
19#include <sys/file.h>
20#include <sys/protosw.h>
21#include <sys/socket.h>
22
23#include <netinet/in.h>
24
25#include <ctype.h>
26#include <errno.h>
27#include <kvm.h>
28#include <limits.h>
29#include <netdb.h>
30#include <nlist.h>
31#include <paths.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include "netstat.h"
37
38struct nlist nl[] = {
39#define N_MBSTAT 0
40 { "_mbstat" },
41#define N_IPSTAT 1
42 { "_ipstat" },
43#define N_TCB 2
44 { "_tcb" },
45#define N_TCPSTAT 3
46 { "_tcpstat" },
47#define N_UDB 4
48 { "_udb" },
49#define N_UDPSTAT 5
50 { "_udpstat" },
51#define N_IFNET 6
52 { "_ifnet" },
53#define N_IMP 7
54 { "_imp_softc" },
55#define N_ICMPSTAT 8
56 { "_icmpstat" },
57#define N_RTSTAT 9
58 { "_rtstat" },
59#define N_UNIXSW 10
60 { "_unixsw" },
61#define N_IDP 11
62 { "_nspcb"},
63#define N_IDPSTAT 12
64 { "_idpstat"},
65#define N_SPPSTAT 13
66 { "_spp_istat"},
67#define N_NSERR 14
68 { "_ns_errstat"},
69#define N_CLNPSTAT 15
70 { "_clnp_stat"},
71#define IN_NOTUSED 16
72 { "_tp_inpcb" },
73#define ISO_TP 17
74 { "_tp_refinfo" },
75#define N_TPSTAT 18
76 { "_tp_stat" },
77#define N_ESISSTAT 19
78 { "_esis_stat"},
79#define N_NIMP 20
80 { "_nimp"},
81#define N_RTREE 21
82 { "_rt_tables"},
83#define N_CLTP 22
84 { "_cltb"},
85#define N_CLTPSTAT 23
86 { "_cltpstat"},
87#define N_NFILE 24
88 { "_nfile" },
89#define N_FILE 25
90 { "_file" },
91#define N_IGMPSTAT 26
92 { "_igmpstat" },
93#define N_MRTPROTO 27
94 { "_ip_mrtproto" },
95#define N_MRTSTAT 28
96 { "_mrtstat" },
97#define N_MRTTABLE 29
98 { "_mrttable" },
99#define N_VIFTABLE 30
100 { "_viftable" },
101
102 /* BBN Internet protocol implementation */
103#define N_TCP 23
104 { "_tcp" },
105#define N_UDP 24
106 { "_udp" },
107#define N_RDP 25
108 { "_rdp" },
109#define N_RDPSTAT 26
110 { "_rdpstat" },
111
112 "",
113};
114
115struct protox {
116 u_char pr_index; /* index into nlist of cb head */
117 u_char pr_sindex; /* index into nlist of stat block */
118 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
119 void (*pr_cblocks)(); /* control blocks printing routine */
120 void (*pr_stats)(); /* statistics printing routine */
121 char *pr_name; /* well-known name */
122};
123
124struct protox berkprotox[] = {
125 { N_TCB, N_TCPSTAT, 1, protopr,
126 tcp_stats, "tcp" },
127 { N_UDB, N_UDPSTAT, 1, protopr,
128 udp_stats, "udp" },
129 { -1, N_IPSTAT, 1, 0,
130 ip_stats, "ip" },
131 { -1, N_ICMPSTAT, 1, 0,
132 icmp_stats, "icmp" },
133 { -1, -1, 0, 0,
134 0, 0 }
135};
136
137struct protox bbnprotox[] = {
138 { N_TCP, N_TCPSTAT, 1, bbnprotopr,
139 tcpstats, "tcp" },
140 { N_UDP, N_UDPSTAT, 1, bbnprotopr,
141 udpstats, "udp" },
142 { N_RDP, N_RDPSTAT, 1, bbnprotopr,
143 rdpstats, "rdp" },
144 { N_RAWCB, 0, 1, bbnprotopr,
145 0, "raw" },
146 { -1, N_IPSTAT, 1, 0,
147 ipstats, "ip" },
148 { -1, N_ICMPSTAT, 1, 0,
149 icmpstats, "icmp" },
150 { -1, -1, 0, 0,
151 0, 0 }
152};
153
154struct protox nsprotox[] = {
155 { N_IDP, N_IDPSTAT, 1, nsprotopr,
156 idp_stats, "idp" },
157 { N_IDP, N_SPPSTAT, 1, nsprotopr,
158 spp_stats, "spp" },
159 { -1, N_NSERR, 1, 0,
160 nserr_stats, "ns_err" },
161 { -1, -1, 0, 0,
162 0, 0 }
163};
164
165struct protox isoprotox[] = {
166 { ISO_TP, N_TPSTAT, 1, iso_protopr,
167 tp_stats, "tp" },
168 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
169 cltp_stats, "cltp" },
170 { -1, N_CLNPSTAT, 1, 0,
171 clnp_stats, "clnp"},
172 { -1, N_ESISSTAT, 1, 0,
173 esis_stats, "esis"},
174 { -1, -1, 0, 0,
175 0, 0 }
176};
177
178struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
179
180static void printproto __P((struct protox *, char *));
181static void usage __P(());
182static struct protox *name2protox __P((char *));
183static struct protox *knownname __P((char *));
184
185kvm_t *kvmd;
186
187int
188main(argc, argv)
189 int argc;
190 char **argv;
191{
192 extern char *optarg;
193 extern int optind;
194 register struct protoent *p;
195 register struct protox *tp; /* for printing cblocks & stats */
196 register char *cp;
197 int ch;
198 char *nlistf = NULL, *memf = NULL;
199 char buf[_POSIX2_LINE_MAX];
200
201 if (cp = rindex(argv[0], '/'))
202 prog = cp + 1;
203 else
204 prog = argv[0];
205 af = AF_UNSPEC;
206
207 while ((ch = getopt(argc, argv, "AaBdf:hI:iM:mN:np:rstuw:")) != EOF)
208 switch(ch) {
209 case 'A':
210 Aflag = 1;
211 break;
212 case 'a':
213 aflag = 1;
214 break;
215 case 'B':
216 Bflag = 1;
217 break;
218 case 'd':
219 dflag = 1;
220 break;
221 case 'f':
222 if (strcmp(optarg, "ns") == 0)
223 af = AF_NS;
224 else if (strcmp(optarg, "inet") == 0)
225 af = AF_INET;
226 else if (strcmp(optarg, "unix") == 0)
227 af = AF_UNIX;
228 else if (strcmp(optarg, "iso") == 0)
229 af = AF_ISO;
230 else {
231 (void)fprintf(stderr,
232 "%s: %s: unknown address family\n",
233 prog, optarg);
234 exit(1);
235 }
236 break;
237 case 'h':
238 hflag = 1;
239 break;
240 case 'I': {
241 char *cp;
242
243 iflag = 1;
244 for (cp = interface = optarg; isalpha(*cp); cp++)
245 continue;
246 unit = atoi(cp);
247 *cp = '\0';
248 break;
249 }
250 case 'i':
251 iflag = 1;
252 break;
253 case 'M':
254 memf = optarg;
255 break;
256 case 'm':
257 mflag = 1;
258 break;
259 case 'N':
260 nlistf = optarg;
261 break;
262 case 'n':
263 nflag = 1;
264 break;
265 case 'p':
266 if ((tp = name2protox(optarg)) == NULL) {
267 (void)fprintf(stderr,
268 "%s: %s: unknown or uninstrumented protocol\n",
269 prog, optarg);
270 exit(1);
271 }
272 pflag = 1;
273 break;
274 case 'r':
275 rflag = 1;
276 break;
277 case 's':
278 ++sflag;
279 break;
280 case 't':
281 tflag = 1;
282 break;
283 case 'u':
284 af = AF_UNIX;
285 break;
286 case 'w':
287 interval = atoi(optarg);
288 break;
289 case '?':
290 default:
291 usage();
292 }
293 argv += optind;
294 argc -= optind;
295
296#define BACKWARD_COMPATIBILITY
297#ifdef BACKWARD_COMPATIBILITY
298 if (*argv) {
299 if (isdigit(**argv)) {
300 interval = atoi(*argv);
301 if (interval <= 0)
302 usage();
303 ++argv;
304 iflag = 1;
305 }
306 if (*argv) {
307 nlistf = *argv;
308 if (*++argv)
309 memf = *argv;
310 }
311 }
312#endif
313
314 /*
315 * Discard setgid privileges if not the running kernel so that bad
316 * guys can't print interesting stuff from kernel memory.
317 */
318 if (nlistf != NULL || memf != NULL)
319 setgid(getgid());
320
321 if ((kvmd = kvm_open(nlistf, memf, NULL, O_RDONLY, prog)) == NULL) {
322 fprintf(stderr, "%s: kvm_open: %s\n", prog, buf);
323 exit(1);
324 }
325 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
326 if (nlistf)
327 fprintf(stderr, "%s: %s: no namelist\n", prog, nlistf);
328 else
329 fprintf(stderr, "%s: no namelist\n", prog);
330 exit(1);
331 }
332 if (mflag) {
333 mbpr(nl[N_MBSTAT].n_value);
334 exit(0);
335 }
336 if (pflag) {
337 if (tp->pr_stats)
338 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
339 tp->pr_name);
340 else
341 printf("%s: no stats routine\n", tp->pr_name);
342 exit(0);
343 }
344 if (hflag) {
345 hostpr(nl[N_IMP].n_value, nl[N_NIMP].n_value);
346 exit(0);
347 }
348 /*
349 * Keep file descriptors open to avoid overhead
350 * of open/close on each call to get* routines.
351 */
352 sethostent(1);
353 setnetent(1);
354 if (iflag) {
355 intpr(interval, nl[N_IFNET].n_value);
356 exit(0);
357 }
358 if (rflag) {
359 if (sflag)
360 rt_stats(nl[N_RTSTAT].n_value);
361 else
362 routepr(nl[N_RTREE].n_value);
363 exit(0);
364 }
365 if (Bflag) {
366 if (sflag)
367 mrt_stats(nl[N_MRTPROTO].n_value,
368 nl[N_MRTSTAT].n_value);
369 else
370 mroutepr(nl[N_MRTPROTO].n_value,
371 nl[N_MRTTABLE].n_value,
372 nl[N_VIFTABLE].n_value);
373 exit(0);
374 }
375
376
377 if (sflag) {
378 if (tp->pr_stats)
379 (*tp->pr_stats)(nl[tp->pr_sindex].n_value, tp->pr_name);
380 } else if (tp->pr_cblocks)
381 (*tp->pr_cblocks)(nl[tp->pr_index].n_value, tp->pr_name);
382 }
383 if (pr != NULL && (off || af != AF_UNSPEC))
384 (*pr)(off, name);
385}
386
387/*
388 * Read kernel memory, return 0 on success.
389 */
390int
391kread(addr, buf, size)
392 u_long addr;
393 char *buf;
394 int size;
395{
396
397 if (kvm_read(kvmd, addr, buf, size) != size) {
398 /* XXX this duplicates kvm_read's error printout */
399 (void)fprintf(stderr, "%s: kvm_read %s\n", prog,
400 kvm_geterr(kvmd));
401 return (-1);
402 }
403 return (0);
404}
405
406char *
407plural(n)
408 int n;
409{
410 return (n != 1 ? "s" : "");
411}
412
413char *
414plurales(n)
415 int n;
416{
417 return (n != 1 ? "es" : "");
418}
419
420/*
421 * Find the protox for the given "well-known" name.
422 */
423static struct protox *
424knownname(name)
425 char *name;
426{
427 struct protox **tpp, *tp;
428
429 for (tpp = protoprotox; *tpp; tpp++)
430 for (tp = *tpp; tp->pr_name; tp++)
431 if (strcmp(tp->pr_name, name) == 0)
432 return (tp);
433 return (NULL);
434}
435
436/*
437 * Find the protox corresponding to name.
438 */
439static struct protox *
440name2protox(name)
441 char *name;
442{
443 struct protox *tp;
444 char **alias; /* alias from p->aliases */
445 struct protoent *p;
446
447 /*
448 * Try to find the name in the list of "well-known" names. If that
449 * fails, check if name is an alias for an Internet protocol.
450 */
451 if (tp = knownname(name))
452 return (tp);
453
454 setprotoent(1); /* make protocol lookup cheaper */
455 while (p = getprotoent()) {
456 /* assert: name not same as p->name */
457 for (alias = p->p_aliases; *alias; alias++)
458 if (strcmp(name, *alias) == 0) {
459 endprotoent();
460 return (knownname(p->p_name));
461 }
462 }
463 endprotoent();
464 return (NULL);
465}
466
467static void
468usage()
469{
470 (void)fprintf(stderr,
471"usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
472 (void)fprintf(stderr,
473" [-himnrs] [-f address_family] [-M core] [-N system]\n");
474 (void)fprintf(stderr,
475" [-n] [-I interface] [-M core] [-N system] [-w wait]\n");
476 (void)fprintf(stderr,
477" [-M core] [-N system] [-p protocol]\n");
478 exit(1);
479}