2 * Copyright (c) 1983, 1988, 1993
3 * Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
64 #include <proto/miami.h>
100 #define IN_NOTUSED 16
106 #define N_ESISSTAT 19
114 #define N_CLTPSTAT 23
120 #define N_IGMPSTAT 26
122 #define N_MRTPROTO 27
126 #define N_MRTTABLE 29
128 #define N_VIFTABLE 30
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 */
141 { N_TCB
, N_TCPSTAT
, 1, protopr
,
143 { N_UDB
, N_UDPSTAT
, 1, protopr
,
145 { -1, N_IPSTAT
, 1, 0,
147 { -1, N_ICMPSTAT
, 1, 0,
148 icmp_stats
, "icmp" },
150 { -1, N_IGMPSTAT
, 1, 0,
151 igmp_stats
, "igmp" },
158 struct protox nsprotox
[] = {
159 { N_IDP
, N_IDPSTAT
, 1, nsprotopr
,
161 { N_IDP
, N_SPPSTAT
, 1, nsprotopr
,
164 nserr_stats
, "ns_err" },
171 struct protox isoprotox
[] = {
172 { ISO_TP
, N_TPSTAT
, 1, iso_protopr
,
174 { N_CLTP
, N_CLTPSTAT
, 1, iso_protopr
,
175 cltp_stats
, "cltp" },
176 { -1, N_CLNPSTAT
, 1, 0,
178 { -1, N_ESISSTAT
, 1, 0,
185 struct protox
*protoprotox
[] = { protox
,
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 *));
208 register struct protoent
*p
;
209 register struct protox
*tp
; /* for printing cblocks & stats */
212 char *nlistf
= NULL
, *memf
= NULL
;
213 char buf
[_POSIX2_LINE_MAX
];
214 char buf2
[_POSIX2_LINE_MAX
];
216 if (cp
= rindex(argv
[0], '/'))
222 while ((ch
= getopt(argc
, argv
, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF
)
237 if (strcmp(optarg
, "ns") == 0)
239 else if (strcmp(optarg
, "inet") == 0)
241 else if (strcmp(optarg
, "unix") == 0)
243 else if (strcmp(optarg
, "iso") == 0)
246 errx(1, "%s: unknown address family", optarg
);
256 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
278 if ((tp
= name2protox(optarg
)) == NULL
) {
280 "%s: unknown or uninstrumented protocol",
298 interval
= atoi(optarg
);
308 #define BACKWARD_COMPATIBILITY
309 #ifdef BACKWARD_COMPATIBILITY
311 if (isdigit(**argv
)) {
312 interval
= atoi(*argv
);
326 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
328 errx(1, "kvm_open: %s", buf
);
330 if (kvm_nlist(kvmd
, nl
) < 0) {
332 errx(1, "%s: kvm_nlist: %s", nlistf
, kvm_geterr(kvmd
));
334 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
337 if (nl
[0].n_type
== 0) {
339 errx(1, "%s: no namelist", nlistf
);
341 errx(1, "no namelist");
344 mbpr(nl
[N_MBSTAT
].n_value
);
349 (*tp
->pr_stats
)(nl
[tp
->pr_sindex
].n_value
,
352 printf("%s: no stats routine\n", tp
->pr_name
);
356 * Keep file descriptors open to avoid overhead
357 * of open/close on each call to get* routines.
360 intpr(interval
, nl
[N_IFNET
].n_value
);
365 rt_stats(nl
[N_RTSTAT
].n_value
);
367 routepr(nl
[N_RTREE
].n_value
);
371 #ifdef ENABLE_MULTICAST
373 mrt_stats(nl
[N_MRTPROTO
].n_value
,
374 nl
[N_MRTSTAT
].n_value
);
376 mroutepr(nl
[N_MRTPROTO
].n_value
,
377 nl
[N_MRTTABLE
].n_value
,
378 nl
[N_VIFTABLE
].n_value
);
380 printf ("Multicast routing is not supported\n");
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)
390 if (tp
->pr_name
== 0 || tp
->pr_wanted
== 0)
392 printproto(tp
, p
->p_name
);
397 if (af
== AF_NS
|| af
== AF_UNSPEC
)
398 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
399 printproto(tp
, tp
->pr_name
);
402 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
403 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
404 printproto(tp
, tp
->pr_name
);
406 #ifdef ENABLE_UNIX_DOMAIN
407 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !sflag
)
408 unixpr(nl
[N_UNIXSW
].n_value
);
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.
420 register struct protox
*tp
;
428 off
= nl
[tp
->pr_sindex
].n_value
;
431 off
= nl
[tp
->pr_index
].n_value
;
433 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
438 * Read kernel memory, return 0 on success.
441 kread(addr
, buf
, size
)
447 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
448 warnx("kvm_read: %s", kvm_geterr(kvmd
));
458 return (n
!= 1 ? "s" : "");
465 return (n
!= 1 ? "es" : "");
469 * Find the protox for the given "well-known" name.
471 static struct protox
*
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)
485 * Find the protox corresponding to name.
487 static struct protox
*
492 char **alias
; /* alias from p->aliases */
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
))
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) {
507 return (knownname(p
->p_name
));
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
);