Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / common / C / netstat / main.c
blob31ec2dce6489259e56a1b344ac554a0c8baa975f
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * 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.
38 "@(#) Copyright (c) 1983, 1988, 1993
39 Regents of the University of California. All rights reserved."
41 "@(#)main.c 8.4 (Berkeley) 3/1/94"
45 #include <sys/param.h>
46 #include <sys/file.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <ctype.h>
53 #include <errno.h>
54 #include <kvm.h>
55 #include <limits.h>
56 #include <netdb.h>
57 #include <nlist.h>
58 #include <paths.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <err.h>
64 #include <proto/miami.h>
65 #include "netstat.h"
67 struct nlist nl[] = {
68 #define N_MBSTAT 0
69 { "_mbstat" },
70 #define N_IPSTAT 1
71 { "_ipstat" },
72 #define N_TCB 2
73 { "_tcb" },
74 #define N_TCPSTAT 3
75 { "_tcpstat" },
76 #define N_UDB 4
77 { "_udb" },
78 #define N_UDPSTAT 5
79 { "_udpstat" },
80 #define N_IFNET 6
81 { "_ifnet" },
82 #define N_IMP 7
83 { "_imp_softc" },
84 #define N_ICMPSTAT 8
85 { "_icmpstat" },
86 #define N_RTSTAT 9
87 { "_rtstat" },
88 #define N_UNIXSW 10
89 { "_unixsw" },
90 #define N_IDP 11
91 { "_nspcb"},
92 #define N_IDPSTAT 12
93 { "_idpstat"},
94 #define N_SPPSTAT 13
95 { "_spp_istat"},
96 #define N_NSERR 14
97 { "_ns_errstat"},
98 #define N_CLNPSTAT 15
99 { "_clnp_stat"},
100 #define IN_NOTUSED 16
101 { "_tp_inpcb" },
102 #define ISO_TP 17
103 { "_tp_refinfo" },
104 #define N_TPSTAT 18
105 { "_tp_stat" },
106 #define N_ESISSTAT 19
107 { "_esis_stat"},
108 #define N_NIMP 20
109 { "_nimp"},
110 #define N_RTREE 21
111 { "_rt_tables"},
112 #define N_CLTP 22
113 { "_cltb"},
114 #define N_CLTPSTAT 23
115 { "_cltpstat"},
116 #define N_NFILE 24
117 { "_nfile" },
118 #define N_FILE 25
119 { "_file" },
120 #define N_IGMPSTAT 26
121 { "_igmpstat" },
122 #define N_MRTPROTO 27
123 { "_ip_mrtproto" },
124 #define N_MRTSTAT 28
125 { "_mrtstat" },
126 #define N_MRTTABLE 29
127 { "_mrttable" },
128 #define N_VIFTABLE 30
129 { "_viftable" },
133 struct protox {
134 u_char pr_index; /* index into nlist of cb head */
135 u_char pr_sindex; /* index into nlist of stat block */
136 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
137 void (*pr_cblocks)(); /* control blocks printing routine */
138 void (*pr_stats)(); /* statistics printing routine */
139 char *pr_name; /* well-known name */
140 } protox[] = {
141 { N_TCB, N_TCPSTAT, 1, protopr,
142 tcp_stats, "tcp" },
143 { N_UDB, N_UDPSTAT, 1, protopr,
144 udp_stats, "udp" },
145 { -1, N_IPSTAT, 1, 0,
146 ip_stats, "ip" },
147 { -1, N_ICMPSTAT, 1, 0,
148 icmp_stats, "icmp" },
149 #ifdef ENABLE_IGMP
150 { -1, N_IGMPSTAT, 1, 0,
151 igmp_stats, "igmp" },
152 #endif
153 { -1, -1, 0, 0,
154 0, 0 }
157 #ifdef NETNS
158 struct protox nsprotox[] = {
159 { N_IDP, N_IDPSTAT, 1, nsprotopr,
160 idp_stats, "idp" },
161 { N_IDP, N_SPPSTAT, 1, nsprotopr,
162 spp_stats, "spp" },
163 { -1, N_NSERR, 1, 0,
164 nserr_stats, "ns_err" },
165 { -1, -1, 0, 0,
166 0, 0 }
168 #endif
170 #ifdef ISO
171 struct protox isoprotox[] = {
172 { ISO_TP, N_TPSTAT, 1, iso_protopr,
173 tp_stats, "tp" },
174 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
175 cltp_stats, "cltp" },
176 { -1, N_CLNPSTAT, 1, 0,
177 clnp_stats, "clnp"},
178 { -1, N_ESISSTAT, 1, 0,
179 esis_stats, "esis"},
180 { -1, -1, 0, 0,
181 0, 0 }
183 #endif
185 struct protox *protoprotox[] = { protox,
186 #ifdef NETNS
187 nsprotox,
188 #endif
189 #ifdef ISO
190 isoprotox,
191 #endif
192 NULL };
194 static void printproto __P((struct protox *, char *));
195 static void usage __P((void));
196 static struct protox *name2protox __P((char *));
197 static struct protox *knownname __P((char *));
199 kvm_t *kvmd;
202 main(argc, argv)
203 int argc;
204 char *argv[];
206 extern char *optarg;
207 extern int optind;
208 register struct protoent *p;
209 register struct protox *tp; /* for printing cblocks & stats */
210 register char *cp;
211 int ch;
212 char *nlistf = NULL, *memf = NULL;
213 char buf[_POSIX2_LINE_MAX];
214 char buf2[_POSIX2_LINE_MAX];
216 if (cp = rindex(argv[0], '/'))
217 prog = cp + 1;
218 else
219 prog = argv[0];
220 af = AF_UNSPEC;
222 while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF)
223 switch(ch) {
224 case 'A':
225 Aflag = 1;
226 break;
227 case 'a':
228 aflag = 1;
229 break;
230 case 'b':
231 bflag = 1;
232 break;
233 case 'd':
234 dflag = 1;
235 break;
236 case 'f':
237 if (strcmp(optarg, "ns") == 0)
238 af = AF_NS;
239 else if (strcmp(optarg, "inet") == 0)
240 af = AF_INET;
241 else if (strcmp(optarg, "unix") == 0)
242 af = AF_UNIX;
243 else if (strcmp(optarg, "iso") == 0)
244 af = AF_ISO;
245 else {
246 errx(1, "%s: unknown address family", optarg);
248 break;
249 case 'g':
250 gflag = 1;
251 break;
252 case 'I': {
253 char *cp;
255 iflag = 1;
256 for (cp = interface = optarg; isalpha(*cp); cp++)
257 continue;
258 unit = atoi(cp);
259 *cp = '\0';
260 break;
262 case 'i':
263 iflag = 1;
264 break;
265 case 'M':
266 memf = optarg;
267 break;
268 case 'm':
269 mflag = 1;
270 break;
271 case 'N':
272 nlistf = optarg;
273 break;
274 case 'n':
275 nflag = 1;
276 break;
277 case 'p':
278 if ((tp = name2protox(optarg)) == NULL) {
279 errx(1,
280 "%s: unknown or uninstrumented protocol",
281 optarg);
283 pflag = 1;
284 break;
285 case 'r':
286 rflag = 1;
287 break;
288 case 's':
289 ++sflag;
290 break;
291 case 't':
292 tflag = 1;
293 break;
294 case 'u':
295 af = AF_UNIX;
296 break;
297 case 'w':
298 interval = atoi(optarg);
299 iflag = 1;
300 break;
301 case '?':
302 default:
303 usage();
305 argv += optind;
306 argc -= optind;
308 #define BACKWARD_COMPATIBILITY
309 #ifdef BACKWARD_COMPATIBILITY
310 if (*argv) {
311 if (isdigit(**argv)) {
312 interval = atoi(*argv);
313 if (interval <= 0)
314 usage();
315 ++argv;
316 iflag = 1;
318 if (*argv) {
319 nlistf = *argv;
320 if (*++argv)
321 memf = *argv;
324 #endif
326 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
327 if (kvmd == NULL) {
328 errx(1, "kvm_open: %s", buf);
330 if (kvm_nlist(kvmd, nl) < 0) {
331 if(nlistf)
332 errx(1, "%s: kvm_nlist: %s", nlistf, kvm_geterr(kvmd));
333 else
334 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
337 if (nl[0].n_type == 0) {
338 if(nlistf)
339 errx(1, "%s: no namelist", nlistf);
340 else
341 errx(1, "no namelist");
343 if (mflag) {
344 mbpr(nl[N_MBSTAT].n_value);
345 exit(0);
347 if (pflag) {
348 if (tp->pr_stats)
349 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
350 tp->pr_name);
351 else
352 printf("%s: no stats routine\n", tp->pr_name);
353 exit(0);
356 * Keep file descriptors open to avoid overhead
357 * of open/close on each call to get* routines.
359 if (iflag) {
360 intpr(interval, nl[N_IFNET].n_value);
361 exit(0);
363 if (rflag) {
364 if (sflag)
365 rt_stats(nl[N_RTSTAT].n_value);
366 else
367 routepr(nl[N_RTREE].n_value);
368 exit(0);
370 if (gflag) {
371 #ifdef ENABLE_MULTICAST
372 if (sflag)
373 mrt_stats(nl[N_MRTPROTO].n_value,
374 nl[N_MRTSTAT].n_value);
375 else
376 mroutepr(nl[N_MRTPROTO].n_value,
377 nl[N_MRTTABLE].n_value,
378 nl[N_VIFTABLE].n_value);
379 #else
380 printf ("Multicast routing is not supported\n");
381 #endif
382 exit(0);
384 if (af == AF_INET || af == AF_UNSPEC) {
385 /* ugh, this is O(MN) ... why do we do this? */
386 while (p = getprotoent()) {
387 for (tp = protox; tp->pr_name; tp++)
388 if (strcmp(tp->pr_name, p->p_name) == 0)
389 break;
390 if (tp->pr_name == 0 || tp->pr_wanted == 0)
391 continue;
392 printproto(tp, p->p_name);
394 endprotoent();
396 #ifdef NETNS
397 if (af == AF_NS || af == AF_UNSPEC)
398 for (tp = nsprotox; tp->pr_name; tp++)
399 printproto(tp, tp->pr_name);
400 #endif
401 #ifdef ISO
402 if (af == AF_ISO || af == AF_UNSPEC)
403 for (tp = isoprotox; tp->pr_name; tp++)
404 printproto(tp, tp->pr_name);
405 #endif
406 #ifdef ENABLE_UNIX_DOMAIN
407 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
408 unixpr(nl[N_UNIXSW].n_value);
409 #endif
410 exit(0);
414 * Print out protocol statistics or control blocks (per sflag).
415 * If the interface was not specifically requested, and the symbol
416 * is not in the namelist, ignore this one.
418 static void
419 printproto(tp, name)
420 register struct protox *tp;
421 char *name;
423 void (*pr)();
424 u_long off;
426 if (sflag) {
427 pr = tp->pr_stats;
428 off = nl[tp->pr_sindex].n_value;
429 } else {
430 pr = tp->pr_cblocks;
431 off = nl[tp->pr_index].n_value;
433 if (pr != NULL && (off || af != AF_UNSPEC))
434 (*pr)(off, name);
438 * Read kernel memory, return 0 on success.
441 kread(addr, buf, size)
442 u_long addr;
443 char *buf;
444 int size;
447 if (kvm_read(kvmd, addr, buf, size) != size) {
448 warnx("kvm_read: %s", kvm_geterr(kvmd));
449 return (-1);
451 return (0);
454 char *
455 plural(n)
456 int n;
458 return (n != 1 ? "s" : "");
461 char *
462 plurales(n)
463 int n;
465 return (n != 1 ? "es" : "");
469 * Find the protox for the given "well-known" name.
471 static struct protox *
472 knownname(name)
473 char *name;
475 struct protox **tpp, *tp;
477 for (tpp = protoprotox; *tpp; tpp++)
478 for (tp = *tpp; tp->pr_name; tp++)
479 if (strcmp(tp->pr_name, name) == 0)
480 return (tp);
481 return (NULL);
485 * Find the protox corresponding to name.
487 static struct protox *
488 name2protox(name)
489 char *name;
491 struct protox *tp;
492 char **alias; /* alias from p->aliases */
493 struct protoent *p;
496 * Try to find the name in the list of "well-known" names. If that
497 * fails, check if name is an alias for an Internet protocol.
499 if (tp = knownname(name))
500 return (tp);
502 while (p = getprotoent()) {
503 /* assert: name not same as p->name */
504 for (alias = p->p_aliases; *alias; alias++)
505 if (strcmp(name, *alias) == 0) {
506 endprotoent();
507 return (knownname(p->p_name));
510 endprotoent();
511 return (NULL);
514 static void
515 usage()
517 (void)fprintf(stderr,
518 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
519 (void)fprintf(stderr,
520 " %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
521 (void)fprintf(stderr,
522 " %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
523 (void)fprintf(stderr,
524 " %s [-M core] [-N system] [-p protocol]\n", prog);
525 exit(1);