]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1988, 1993
26 * Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 char const copyright
[] =
59 "@(#) Copyright (c) 1983, 1988, 1993\n\
60 Regents of the University of California. All rights reserved.\n";
65 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
67 static const char rcsid
[] =
68 "$Id: main.c,v 1.5 2003/07/08 22:49:49 lindak Exp $";
71 #include <sys/param.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
76 #include <netinet/in.h>
91 #include <sys/types.h>
92 #include <sys/sysctl.h>
96 * ----------------------------------------------------------------------------
97 * "THE BEER-WARE LICENSE" (Revision 42):
98 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
99 * can do whatever you want with this stuff. If we meet some day, and you think
100 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
101 * ----------------------------------------------------------------------------
103 * $Id: main.c,v 1.5 2003/07/08 22:49:49 lindak Exp $
108 static struct nlist nl
[] = {
133 #define N_ESISSTAT 12
141 #define N_CLTPSTAT 16
149 #define N_MFCTABLE 20
151 #define N_VIFTABLE 21
167 #define N_ICMP6STAT 29
169 #define N_IPSECSTAT 30
171 #define N_IPSEC6STAT 31
173 #define N_PIM6STAT 32
175 #define N_MRT6PROTO 33
177 #define N_MRT6STAT 34
179 #define N_MF6CTABLE 35
181 #define N_MIF6TABLE 36
183 #define N_PFKEYSTAT 37
189 #define N_NMBCLUSTERS 40
201 u_char pr_index
; /* index into nlist of cb head */
202 u_char pr_sindex
; /* index into nlist of stat block */
203 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
204 void (*pr_cblocks
)(u_long
, char *, int);
205 /* control blocks printing routine */
206 void (*pr_stats
)(u_long
, char *, int);
207 /* statistics printing routine */
208 void (*pr_istats
)(char *); /* per/if statistics printing routine */
209 char *pr_name
; /* well-known name */
210 int pr_usesysctl
; /* true if we use sysctl, not kvm */
212 { -1, -1, 1, protopr
,
213 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
214 { -1, -1, 1, protopr
,
215 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
216 { -1, -1, 1, protopr
,
217 NULL
, NULL
, "divert",IPPROTO_DIVERT
},
218 { -1, -1, 1, protopr
,
219 ip_stats
, NULL
, "ip", IPPROTO_RAW
},
220 { -1, -1, 1, protopr
,
221 icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
222 { -1, -1, 1, protopr
,
223 igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
225 { -1, N_IPSECSTAT
, 1, 0,
226 ipsec_stats
, NULL
, "ipsec", 0},
230 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
237 struct protox ip6protox
[] = {
238 { -1, -1, 1, protopr
,
239 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
240 { -1, -1, 1, protopr
,
241 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
242 { -1, N_IP6STAT
, 1, protopr
,
243 ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
244 { -1, N_ICMP6STAT
, 1, protopr
,
245 icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
247 { -1, N_IPSEC6STAT
, 1, 0,
248 ipsec_stats
, NULL
, "ipsec6",0 },
251 { -1, N_PIM6STAT
, 1, 0,
252 pim6_stats
, NULL
, "pim6", 0 },
255 rip6_stats
, NULL
, "rip6", 0 },
258 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
266 struct protox pfkeyprotox
[] = {
267 { -1, N_PFKEYSTAT
, 1, 0,
268 pfkey_stats
, NULL
, "pfkey", 0 },
274 struct protox atalkprotox
[] = {
275 { N_DDPCB
, N_DDPSTAT
, 1, atalkprotopr
,
276 ddp_stats
, NULL
, "ddp" },
281 struct protox netgraphprotox
[] = {
282 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
283 NULL
, NULL
, "ctrl" },
284 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
285 NULL
, NULL
, "data" },
290 struct protox ipxprotox
[] = {
291 { N_IPX
, N_IPXSTAT
, 1, ipxprotopr
,
292 ipx_stats
, NULL
, "ipx", 0 },
293 { N_IPX
, N_SPXSTAT
, 1, ipxprotopr
,
294 spx_stats
, NULL
, "spx", 0 },
300 struct protox nsprotox
[] = {
301 { N_IDP
, N_IDPSTAT
, 1, nsprotopr
,
302 idp_stats
, NULL
, "idp" },
303 { N_IDP
, N_SPPSTAT
, 1, nsprotopr
,
304 spp_stats
, NULL
, "spp" },
306 nserr_stats
, NULL
, "ns_err" },
313 struct protox isoprotox
[] = {
314 { ISO_TP
, N_TPSTAT
, 1, iso_protopr
,
315 tp_stats
, NULL
, "tp" },
316 { N_CLTP
, N_CLTPSTAT
, 1, iso_protopr
,
317 cltp_stats
, NULL
, "cltp" },
318 { -1, N_CLNPSTAT
, 1, 0,
319 clnp_stats
, NULL
, "clnp"},
320 { -1, N_ESISSTAT
, 1, 0,
321 esis_stats
, NULL
, "esis"},
327 struct protox
*protoprotox
[] = {
336 ipxprotox
, atalkprotox
,
346 static void printproto (struct protox
*, char *);
347 static void usage (void);
348 static struct protox
*name2protox (char *);
349 static struct protox
*knownname (char *);
351 extern void _serv_cache_close();
355 static char *nlistf
= NULL
, *memf
= NULL
;
357 int Aflag
; /* show addresses of protocol control block */
358 int aflag
; /* show all sockets (including servers) */
359 int bflag
; /* show i/f total bytes in/out */
360 int dflag
; /* show i/f dropped packets */
361 int gflag
; /* show group (multicast) routing or stats */
362 int iflag
; /* show interfaces */
363 int lflag
; /* show routing table with use and ref */
364 int Lflag
; /* show size of listen queues */
365 int mflag
; /* show memory stats */
366 int nflag
; /* show addresses numerically */
367 static int pflag
; /* show given protocol */
368 int rflag
; /* show routing tables (or routing stats) */
369 int sflag
; /* show protocol statistics */
370 int tflag
; /* show i/f watchdog timers */
371 int Wflag
; /* wide display */
373 int interval
; /* repeat interval for i/f stats */
375 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
376 int unit
; /* unit number for above */
378 int af
; /* address family */
385 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
390 while ((ch
= getopt(argc
, argv
, "Aabdf:gI:iLlM:mN:np:rstuWw:")) != -1)
406 if (strcmp(optarg
, "ns") == 0)
410 if (strcmp(optarg
, "ipx") == 0)
412 else if (strcmp(optarg
, "inet") == 0)
415 else if (strcmp(optarg
, "inet6") == 0)
419 else if (strcmp(optarg
, "pfkey") == 0)
422 else if (strcmp(optarg
, "unix") == 0)
425 else if (strcmp(optarg
, "atalk") == 0)
427 else if (strcmp(optarg
, "ng") == 0
428 || strcmp(optarg
, "netgraph") == 0)
432 else if (strcmp(optarg
, "iso") == 0)
436 errx(1, "%s: unknown address family", optarg
);
446 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
473 if ((tp
= name2protox(optarg
)) == NULL
) {
475 "%s: unknown or uninstrumented protocol",
496 interval
= atoi(optarg
);
506 #define BACKWARD_COMPATIBILITY
507 #ifdef BACKWARD_COMPATIBILITY
509 if (isdigit(**argv
)) {
510 interval
= atoi(*argv
);
525 * Discard setgid privileges if not the running kernel so that bad
526 * guys can't print interesting stuff from kernel memory.
528 if (nlistf
!= NULL
|| memf
!= NULL
)
532 //if (memf != NULL) {
533 if (kread(0, 0, 0) == 0)
534 mbpr(nl
[N_MBSTAT
].n_value
);
535 //mbpr(nl[N_MBSTAT].n_value,
536 // nl[N_MBTYPES].n_value,
537 // nl[N_NMBCLUSTERS].n_value,
538 // nl[N_NMBUFS].n_value);
546 * Keep file descriptors open to avoid overhead
547 * of open/close on each call to get* routines.
553 * This does not make sense any more with DNS being default over
554 * the files. Doing a setXXXXent(1) causes a tcp connection to be
555 * used for the queries, which is slower.
558 if (iflag
&& !sflag
) {
560 intpr(interval
, nl
[N_IFNET
].n_value
, NULL
);
566 rt_stats(nl
[N_RTSTAT
].n_value
, nl
[N_RTTRASH
].n_value
);
568 routepr(nl
[N_RTREE
].n_value
);
574 if (af
== AF_INET
|| af
== AF_UNSPEC
)
575 mrt_stats(nl
[N_MRTSTAT
].n_value
);
577 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
578 mrt6_stats(nl
[N_MRT6STAT
].n_value
);
581 if (af
== AF_INET
|| af
== AF_UNSPEC
)
582 mroutepr(nl
[N_MFCTABLE
].n_value
,
583 nl
[N_VIFTABLE
].n_value
);
585 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
586 mroute6pr(nl
[N_MF6CTABLE
].n_value
,
587 nl
[N_MIF6TABLE
].n_value
);
595 printproto(tp
, tp
->pr_name
);
598 if (af
== AF_INET
|| af
== AF_UNSPEC
)
599 for (tp
= protox
; tp
->pr_name
; tp
++)
600 printproto(tp
, tp
->pr_name
);
602 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
603 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
604 printproto(tp
, tp
->pr_name
);
607 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
608 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
609 printproto(tp
, tp
->pr_name
);
612 if (af
== AF_IPX
|| af
== AF_UNSPEC
) {
614 for (tp
= ipxprotox
; tp
->pr_name
; tp
++)
615 printproto(tp
, tp
->pr_name
);
617 if (af
== AF_APPLETALK
|| af
== AF_UNSPEC
)
618 for (tp
= atalkprotox
; tp
->pr_name
; tp
++)
619 printproto(tp
, tp
->pr_name
);
620 if (af
== AF_NETGRAPH
|| af
== AF_UNSPEC
)
621 for (tp
= netgraphprotox
; tp
->pr_name
; tp
++)
622 printproto(tp
, tp
->pr_name
);
625 if (af
== AF_NS
|| af
== AF_UNSPEC
)
626 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
627 printproto(tp
, tp
->pr_name
);
630 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
631 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
632 printproto(tp
, tp
->pr_name
);
634 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
643 * Print out protocol statistics or control blocks (per sflag).
644 * If the interface was not specifically requested, and the symbol
645 * is not in the namelist, ignore this one.
649 register struct protox
*tp
;
652 void (*pr
)(u_long
, char *, int);
658 intpr(interval
, nl
[N_IFNET
].n_value
,
661 printf("%s: no per-interface stats routine\n",
669 printf("%s: no stats routine\n",
673 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
674 : nl
[tp
->pr_sindex
].n_value
;
680 printf("%s: no PCB routine\n", tp
->pr_name
);
683 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
684 : nl
[tp
->pr_index
].n_value
;
686 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
687 (*pr
)(off
, name
, af
);
691 * Read kernel memory, return 0 on success.
694 kread(u_long addr
, char *buf
, int size
)
700 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
702 if (kvm_nlist(kvmd
, nl
) < 0) {
704 errx(1, "%s: kvm_nlist: %s", nlistf
,
707 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
710 if (nl
[0].n_type
== 0) {
712 errx(1, "%s: no namelist", nlistf
);
714 errx(1, "no namelist");
717 warnx("kvm not available");
723 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
724 warnx("%s", kvm_geterr(kvmd
));
733 return (n
!= 1 ? "s" : "");
739 return (n
!= 1 ? "es" : "");
743 * Find the protox for the given "well-known" name.
745 static struct protox
*
746 knownname(char *name
)
748 struct protox
**tpp
, *tp
;
750 for (tpp
= protoprotox
; *tpp
; tpp
++)
751 for (tp
= *tpp
; tp
->pr_name
; tp
++)
752 if (strcmp(tp
->pr_name
, name
) == 0)
758 * Find the protox corresponding to name.
760 static struct protox
*
761 name2protox(char *name
)
764 char **alias
; /* alias from p->aliases */
768 * Try to find the name in the list of "well-known" names. If that
769 * fails, check if name is an alias for an Internet protocol.
771 if ((tp
= knownname(name
)) != NULL
)
774 setprotoent(1); /* make protocol lookup cheaper */
775 while ((p
= getprotoent()) != NULL
) {
776 /* assert: name not same as p->name */
777 for (alias
= p
->p_aliases
; *alias
; alias
++)
778 if (strcmp(name
, *alias
) == 0) {
780 return (knownname(p
->p_name
));
790 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n",
791 "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]",
792 " netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]",
793 " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
794 " netstat -m [-M core] [-N system]",
795 " netstat [-M core] [-N system] [-p protocol]");