]>
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.4 2002/03/05 20:35:14 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.4 2002/03/05 20:35:14 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 *);
352 static char *nlistf
= NULL
, *memf
= NULL
;
354 int Aflag
; /* show addresses of protocol control block */
355 int aflag
; /* show all sockets (including servers) */
356 int bflag
; /* show i/f total bytes in/out */
357 int dflag
; /* show i/f dropped packets */
358 int gflag
; /* show group (multicast) routing or stats */
359 int iflag
; /* show interfaces */
360 int lflag
; /* show routing table with use and ref */
361 int Lflag
; /* show size of listen queues */
362 int mflag
; /* show memory stats */
363 int nflag
; /* show addresses numerically */
364 static int pflag
; /* show given protocol */
365 int rflag
; /* show routing tables (or routing stats) */
366 int sflag
; /* show protocol statistics */
367 int tflag
; /* show i/f watchdog timers */
368 int Wflag
; /* wide display */
370 int interval
; /* repeat interval for i/f stats */
372 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
373 int unit
; /* unit number for above */
375 int af
; /* address family */
382 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
387 while ((ch
= getopt(argc
, argv
, "Aabdf:gI:iLlM:mN:np:rstuWw:")) != -1)
403 if (strcmp(optarg
, "ns") == 0)
407 if (strcmp(optarg
, "ipx") == 0)
409 else if (strcmp(optarg
, "inet") == 0)
412 else if (strcmp(optarg
, "inet6") == 0)
416 else if (strcmp(optarg
, "pfkey") == 0)
419 else if (strcmp(optarg
, "unix") == 0)
422 else if (strcmp(optarg
, "atalk") == 0)
424 else if (strcmp(optarg
, "ng") == 0
425 || strcmp(optarg
, "netgraph") == 0)
429 else if (strcmp(optarg
, "iso") == 0)
433 errx(1, "%s: unknown address family", optarg
);
443 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
470 if ((tp
= name2protox(optarg
)) == NULL
) {
472 "%s: unknown or uninstrumented protocol",
493 interval
= atoi(optarg
);
503 #define BACKWARD_COMPATIBILITY
504 #ifdef BACKWARD_COMPATIBILITY
506 if (isdigit(**argv
)) {
507 interval
= atoi(*argv
);
522 * Discard setgid privileges if not the running kernel so that bad
523 * guys can't print interesting stuff from kernel memory.
525 if (nlistf
!= NULL
|| memf
!= NULL
)
529 //if (memf != NULL) {
530 if (kread(0, 0, 0) == 0)
531 mbpr(nl
[N_MBSTAT
].n_value
);
532 //mbpr(nl[N_MBSTAT].n_value,
533 // nl[N_MBTYPES].n_value,
534 // nl[N_NMBCLUSTERS].n_value,
535 // nl[N_NMBUFS].n_value);
543 * Keep file descriptors open to avoid overhead
544 * of open/close on each call to get* routines.
550 * This does not make sense any more with DNS being default over
551 * the files. Doing a setXXXXent(1) causes a tcp connection to be
552 * used for the queries, which is slower.
555 if (iflag
&& !sflag
) {
557 intpr(interval
, nl
[N_IFNET
].n_value
, NULL
);
563 rt_stats(nl
[N_RTSTAT
].n_value
, nl
[N_RTTRASH
].n_value
);
565 routepr(nl
[N_RTREE
].n_value
);
571 if (af
== AF_INET
|| af
== AF_UNSPEC
)
572 mrt_stats(nl
[N_MRTSTAT
].n_value
);
574 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
575 mrt6_stats(nl
[N_MRT6STAT
].n_value
);
578 if (af
== AF_INET
|| af
== AF_UNSPEC
)
579 mroutepr(nl
[N_MFCTABLE
].n_value
,
580 nl
[N_VIFTABLE
].n_value
);
582 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
583 mroute6pr(nl
[N_MF6CTABLE
].n_value
,
584 nl
[N_MIF6TABLE
].n_value
);
592 printproto(tp
, tp
->pr_name
);
595 if (af
== AF_INET
|| af
== AF_UNSPEC
)
596 for (tp
= protox
; tp
->pr_name
; tp
++)
597 printproto(tp
, tp
->pr_name
);
599 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
600 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
601 printproto(tp
, tp
->pr_name
);
604 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
605 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
606 printproto(tp
, tp
->pr_name
);
609 if (af
== AF_IPX
|| af
== AF_UNSPEC
) {
611 for (tp
= ipxprotox
; tp
->pr_name
; tp
++)
612 printproto(tp
, tp
->pr_name
);
614 if (af
== AF_APPLETALK
|| af
== AF_UNSPEC
)
615 for (tp
= atalkprotox
; tp
->pr_name
; tp
++)
616 printproto(tp
, tp
->pr_name
);
617 if (af
== AF_NETGRAPH
|| af
== AF_UNSPEC
)
618 for (tp
= netgraphprotox
; tp
->pr_name
; tp
++)
619 printproto(tp
, tp
->pr_name
);
622 if (af
== AF_NS
|| af
== AF_UNSPEC
)
623 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
624 printproto(tp
, tp
->pr_name
);
627 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
628 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
629 printproto(tp
, tp
->pr_name
);
631 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
637 * Print out protocol statistics or control blocks (per sflag).
638 * If the interface was not specifically requested, and the symbol
639 * is not in the namelist, ignore this one.
643 register struct protox
*tp
;
646 void (*pr
)(u_long
, char *, int);
652 intpr(interval
, nl
[N_IFNET
].n_value
,
655 printf("%s: no per-interface stats routine\n",
663 printf("%s: no stats routine\n",
667 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
668 : nl
[tp
->pr_sindex
].n_value
;
674 printf("%s: no PCB routine\n", tp
->pr_name
);
677 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
678 : nl
[tp
->pr_index
].n_value
;
680 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
681 (*pr
)(off
, name
, af
);
685 * Read kernel memory, return 0 on success.
688 kread(u_long addr
, char *buf
, int size
)
694 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
696 if (kvm_nlist(kvmd
, nl
) < 0) {
698 errx(1, "%s: kvm_nlist: %s", nlistf
,
701 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
704 if (nl
[0].n_type
== 0) {
706 errx(1, "%s: no namelist", nlistf
);
708 errx(1, "no namelist");
711 warnx("kvm not available");
717 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
718 warnx("%s", kvm_geterr(kvmd
));
727 return (n
!= 1 ? "s" : "");
733 return (n
!= 1 ? "es" : "");
737 * Find the protox for the given "well-known" name.
739 static struct protox
*
740 knownname(char *name
)
742 struct protox
**tpp
, *tp
;
744 for (tpp
= protoprotox
; *tpp
; tpp
++)
745 for (tp
= *tpp
; tp
->pr_name
; tp
++)
746 if (strcmp(tp
->pr_name
, name
) == 0)
752 * Find the protox corresponding to name.
754 static struct protox
*
755 name2protox(char *name
)
758 char **alias
; /* alias from p->aliases */
762 * Try to find the name in the list of "well-known" names. If that
763 * fails, check if name is an alias for an Internet protocol.
765 if ((tp
= knownname(name
)) != NULL
)
768 setprotoent(1); /* make protocol lookup cheaper */
769 while ((p
= getprotoent()) != NULL
) {
770 /* assert: name not same as p->name */
771 for (alias
= p
->p_aliases
; *alias
; alias
++)
772 if (strcmp(name
, *alias
) == 0) {
774 return (knownname(p
->p_name
));
784 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n",
785 "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]",
786 " netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]",
787 " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
788 " netstat -m [-M core] [-N system]",
789 " netstat [-M core] [-N system] [-p protocol]");