]>
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.8 2004/10/14 22:24:09 lindak Exp $";
71 #include <sys/param.h>
73 #include <sys/socket.h>
75 #include <netinet/in.h>
76 #include <net/pfkeyv2.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.8 2004/10/14 22:24:09 lindak Exp $
107 static struct nlist nl
[] = {
132 #define N_ESISSTAT 12
140 #define N_CLTPSTAT 16
160 #define N_ICMP6STAT 29
162 #define N_IPSECSTAT 30
164 #define N_IPSEC6STAT 31
166 #define N_PIM6STAT 32
168 #define N_MRT6PROTO 33
170 #define N_MRT6STAT 34
172 #define N_MF6CTABLE 35
174 #define N_MIF6TABLE 36
176 #define N_PFKEYSTAT 37
182 #define N_NMBCLUSTERS 40
193 u_char pr_index
; /* index into nlist of cb head */
194 u_char pr_sindex
; /* index into nlist of stat block */
195 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
196 void (*pr_cblocks
)(u_long
, char *, int);
197 /* control blocks printing routine */
198 void (*pr_stats
)(u_long
, char *, int);
199 /* statistics printing routine */
200 void (*pr_istats
)(char *); /* per/if statistics printing routine */
201 char *pr_name
; /* well-known name */
202 int pr_usesysctl
; /* true if we use sysctl, not kvm */
204 { -1, -1, 1, protopr
,
205 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
206 { -1, -1, 1, protopr
,
207 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
208 { -1, -1, 1, protopr
,
209 NULL
, NULL
, "divert",IPPROTO_DIVERT
},
210 { -1, -1, 1, protopr
,
211 ip_stats
, NULL
, "ip", IPPROTO_RAW
},
212 { -1, -1, 1, protopr
,
213 icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
214 { -1, -1, 1, protopr
,
215 igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
218 ipsec_stats
, NULL
, "ipsec", IPPROTO_ESP
},
222 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
229 struct protox ip6protox
[] = {
230 { -1, -1, 1, protopr
,
231 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
232 { -1, -1, 1, protopr
,
233 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
234 { -1, N_IP6STAT
, 1, protopr
,
235 ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
236 { -1, N_ICMP6STAT
, 1, protopr
,
237 icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
239 { -1, N_IPSEC6STAT
, 1, 0,
240 ipsec_stats
, NULL
, "ipsec6",IPPROTO_ESP
},
243 { -1, N_PIM6STAT
, 1, 0,
244 pim6_stats
, NULL
, "pim6", 0 },
247 rip6_stats
, NULL
, "rip6", IPPROTO_RAW
},
250 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
258 struct protox pfkeyprotox
[] = {
259 { -1, N_PFKEYSTAT
, 1, 0,
260 pfkey_stats
, NULL
, "pfkey", PF_KEY_V2
},
266 struct protox atalkprotox
[] = {
267 { N_DDPCB
, N_DDPSTAT
, 1, atalkprotopr
,
268 ddp_stats
, NULL
, "ddp" },
273 struct protox netgraphprotox
[] = {
274 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
275 NULL
, NULL
, "ctrl" },
276 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
277 NULL
, NULL
, "data" },
282 struct protox ipxprotox
[] = {
283 { N_IPX
, N_IPXSTAT
, 1, ipxprotopr
,
284 ipx_stats
, NULL
, "ipx", 0 },
285 { N_IPX
, N_SPXSTAT
, 1, ipxprotopr
,
286 spx_stats
, NULL
, "spx", 0 },
292 struct protox nsprotox
[] = {
293 { N_IDP
, N_IDPSTAT
, 1, nsprotopr
,
294 idp_stats
, NULL
, "idp" },
295 { N_IDP
, N_SPPSTAT
, 1, nsprotopr
,
296 spp_stats
, NULL
, "spp" },
298 nserr_stats
, NULL
, "ns_err" },
305 struct protox isoprotox
[] = {
306 { ISO_TP
, N_TPSTAT
, 1, iso_protopr
,
307 tp_stats
, NULL
, "tp" },
308 { N_CLTP
, N_CLTPSTAT
, 1, iso_protopr
,
309 cltp_stats
, NULL
, "cltp" },
310 { -1, N_CLNPSTAT
, 1, 0,
311 clnp_stats
, NULL
, "clnp"},
312 { -1, N_ESISSTAT
, 1, 0,
313 esis_stats
, NULL
, "esis"},
319 struct protox
*protoprotox
[] = {
328 ipxprotox
, atalkprotox
,
338 static void printproto (struct protox
*, char *);
339 static void usage (void);
340 static struct protox
*name2protox (char *);
341 static struct protox
*knownname (char *);
343 extern void _serv_cache_close();
349 static char *nlistf
= NULL
, *memf
= NULL
;
351 int Aflag
; /* show addresses of protocol control block */
352 int aflag
; /* show all sockets (including servers) */
353 int bflag
; /* show i/f total bytes in/out */
354 int dflag
; /* show i/f dropped packets */
355 int gflag
; /* show group (multicast) routing or stats */
356 int iflag
; /* show interfaces */
357 int lflag
; /* show routing table with use and ref */
358 int Lflag
; /* show size of listen queues */
359 int mflag
; /* show memory stats */
360 int nflag
; /* show addresses numerically */
361 static int pflag
; /* show given protocol */
362 int rflag
; /* show routing tables (or routing stats) */
363 int sflag
; /* show protocol statistics */
364 int tflag
; /* show i/f watchdog timers */
365 int Wflag
; /* wide display */
367 int interval
; /* repeat interval for i/f stats */
369 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
370 int unit
; /* unit number for above */
372 int af
; /* address family */
379 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
384 while ((ch
= getopt(argc
, argv
, "Aabdf:gI:iLlM:mN:np:rRstuWw:")) != -1)
400 if (strcmp(optarg
, "ns") == 0)
404 if (strcmp(optarg
, "ipx") == 0)
406 else if (strcmp(optarg
, "inet") == 0)
409 else if (strcmp(optarg
, "inet6") == 0)
413 else if (strcmp(optarg
, "pfkey") == 0)
416 else if (strcmp(optarg
, "unix") == 0)
419 else if (strcmp(optarg
, "atalk") == 0)
421 else if (strcmp(optarg
, "ng") == 0
422 || strcmp(optarg
, "netgraph") == 0)
426 else if (strcmp(optarg
, "iso") == 0)
430 errx(1, "%s: unknown address family", optarg
);
440 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
467 if ((tp
= name2protox(optarg
)) == NULL
) {
469 "%s: unknown or uninstrumented protocol",
490 interval
= atoi(optarg
);
500 #define BACKWARD_COMPATIBILITY
501 #ifdef BACKWARD_COMPATIBILITY
503 if (isdigit(**argv
)) {
504 interval
= atoi(*argv
);
519 * Discard setgid privileges if not the running kernel so that bad
520 * guys can't print interesting stuff from kernel memory.
522 if (nlistf
!= NULL
|| memf
!= NULL
)
531 * Keep file descriptors open to avoid overhead
532 * of open/close on each call to get* routines.
538 * This does not make sense any more with DNS being default over
539 * the files. Doing a setXXXXent(1) causes a tcp connection to be
540 * used for the queries, which is slower.
543 if (iflag
&& !sflag
) {
551 routepr(nl
[N_RTREE
].n_value
);
556 if (af
== AF_INET
|| af
== AF_UNSPEC
)
559 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
563 if (af
== AF_INET
|| af
== AF_UNSPEC
)
566 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
574 printproto(tp
, tp
->pr_name
);
577 if (af
== AF_INET
|| af
== AF_UNSPEC
)
578 for (tp
= protox
; tp
->pr_name
; tp
++)
579 printproto(tp
, tp
->pr_name
);
581 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
582 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
583 printproto(tp
, tp
->pr_name
);
586 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
587 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
588 printproto(tp
, tp
->pr_name
);
591 if (af
== AF_IPX
|| af
== AF_UNSPEC
) {
592 for (tp
= ipxprotox
; tp
->pr_name
; tp
++)
593 printproto(tp
, tp
->pr_name
);
595 if (af
== AF_APPLETALK
|| af
== AF_UNSPEC
)
596 for (tp
= atalkprotox
; tp
->pr_name
; tp
++)
597 printproto(tp
, tp
->pr_name
);
598 if (af
== AF_NETGRAPH
|| af
== AF_UNSPEC
)
599 for (tp
= netgraphprotox
; tp
->pr_name
; tp
++)
600 printproto(tp
, tp
->pr_name
);
603 if (af
== AF_NS
|| af
== AF_UNSPEC
)
604 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
605 printproto(tp
, tp
->pr_name
);
608 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
609 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
610 printproto(tp
, tp
->pr_name
);
612 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
621 * Print out protocol statistics or control blocks (per sflag).
622 * If the interface was not specifically requested, and the symbol
623 * is not in the namelist, ignore this one.
627 register struct protox
*tp
;
630 void (*pr
)(u_long
, char *, int);
636 intpr(tp
->pr_istats
);
638 printf("%s: no per-interface stats routine\n",
646 printf("%s: no stats routine\n",
650 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
651 : nl
[tp
->pr_sindex
].n_value
;
657 printf("%s: no PCB routine\n", tp
->pr_name
);
660 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
661 : nl
[tp
->pr_index
].n_value
;
663 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
664 (*pr
)(off
, name
, af
);
666 printf("### no stats for %s\n", name
);
670 * Read kernel memory, return 0 on success.
674 kread(u_long addr
, char *buf
, int size
)
680 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
682 if (kvm_nlist(kvmd
, nl
) < 0) {
684 errx(1, "%s: kvm_nlist: %s", nlistf
,
687 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
690 if (nl
[0].n_type
== 0) {
692 errx(1, "%s: no namelist", nlistf
);
694 errx(1, "no namelist");
697 warnx("kvm not available");
703 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
704 warnx("%s", kvm_geterr(kvmd
));
714 return (n
!= 1 ? "s" : "");
720 return (n
!= 1 ? "es" : "");
724 * Find the protox for the given "well-known" name.
726 static struct protox
*
727 knownname(char *name
)
729 struct protox
**tpp
, *tp
;
731 for (tpp
= protoprotox
; *tpp
; tpp
++)
732 for (tp
= *tpp
; tp
->pr_name
; tp
++)
733 if (strcmp(tp
->pr_name
, name
) == 0)
739 * Find the protox corresponding to name.
741 static struct protox
*
742 name2protox(char *name
)
745 char **alias
; /* alias from p->aliases */
749 * Try to find the name in the list of "well-known" names. If that
750 * fails, check if name is an alias for an Internet protocol.
752 if ((tp
= knownname(name
)) != NULL
)
755 setprotoent(1); /* make protocol lookup cheaper */
756 while ((p
= getprotoent()) != NULL
) {
757 /* assert: name not same as p->name */
758 for (alias
= p
->p_aliases
; *alias
; alias
++)
759 if (strcmp(name
, *alias
) == 0) {
761 return (knownname(p
->p_name
));
771 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n",
772 "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]",
773 " netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]",
774 " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
775 " netstat -m [-M core] [-N system]",
776 " netstat [-M core] [-N system] [-p protocol]");