]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
9eea7a37f1f84c67bd22ab2e8265dedda7bbc002
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1988, 1993
27 * Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 char const copyright
[] =
60 "@(#) Copyright (c) 1983, 1988, 1993\n\
61 Regents of the University of California. All rights reserved.\n";
66 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
68 static const char rcsid
[] =
69 "$Id: main.c,v 1.5 2003/07/08 22:49:49 lindak Exp $";
72 #include <sys/param.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
77 #include <netinet/in.h>
92 #include <sys/types.h>
93 #include <sys/sysctl.h>
97 * ----------------------------------------------------------------------------
98 * "THE BEER-WARE LICENSE" (Revision 42):
99 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
100 * can do whatever you want with this stuff. If we meet some day, and you think
101 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
102 * ----------------------------------------------------------------------------
104 * $Id: main.c,v 1.5 2003/07/08 22:49:49 lindak Exp $
109 static struct nlist nl
[] = {
134 #define N_ESISSTAT 12
142 #define N_CLTPSTAT 16
150 #define N_MFCTABLE 20
152 #define N_VIFTABLE 21
168 #define N_ICMP6STAT 29
170 #define N_IPSECSTAT 30
172 #define N_IPSEC6STAT 31
174 #define N_PIM6STAT 32
176 #define N_MRT6PROTO 33
178 #define N_MRT6STAT 34
180 #define N_MF6CTABLE 35
182 #define N_MIF6TABLE 36
184 #define N_PFKEYSTAT 37
190 #define N_NMBCLUSTERS 40
202 u_char pr_index
; /* index into nlist of cb head */
203 u_char pr_sindex
; /* index into nlist of stat block */
204 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
205 void (*pr_cblocks
)(u_long
, char *, int);
206 /* control blocks printing routine */
207 void (*pr_stats
)(u_long
, char *, int);
208 /* statistics printing routine */
209 void (*pr_istats
)(char *); /* per/if statistics printing routine */
210 char *pr_name
; /* well-known name */
211 int pr_usesysctl
; /* true if we use sysctl, not kvm */
213 { -1, -1, 1, protopr
,
214 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
215 { -1, -1, 1, protopr
,
216 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
217 { -1, -1, 1, protopr
,
218 NULL
, NULL
, "divert",IPPROTO_DIVERT
},
219 { -1, -1, 1, protopr
,
220 ip_stats
, NULL
, "ip", IPPROTO_RAW
},
221 { -1, -1, 1, protopr
,
222 icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
223 { -1, -1, 1, protopr
,
224 igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
226 { -1, N_IPSECSTAT
, 1, 0,
227 ipsec_stats
, NULL
, "ipsec", 0},
231 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
238 struct protox ip6protox
[] = {
239 { -1, -1, 1, protopr
,
240 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
241 { -1, -1, 1, protopr
,
242 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
243 { -1, N_IP6STAT
, 1, protopr
,
244 ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
245 { -1, N_ICMP6STAT
, 1, protopr
,
246 icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
248 { -1, N_IPSEC6STAT
, 1, 0,
249 ipsec_stats
, NULL
, "ipsec6",0 },
252 { -1, N_PIM6STAT
, 1, 0,
253 pim6_stats
, NULL
, "pim6", 0 },
256 rip6_stats
, NULL
, "rip6", 0 },
259 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
267 struct protox pfkeyprotox
[] = {
268 { -1, N_PFKEYSTAT
, 1, 0,
269 pfkey_stats
, NULL
, "pfkey", 0 },
275 struct protox atalkprotox
[] = {
276 { N_DDPCB
, N_DDPSTAT
, 1, atalkprotopr
,
277 ddp_stats
, NULL
, "ddp" },
282 struct protox netgraphprotox
[] = {
283 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
284 NULL
, NULL
, "ctrl" },
285 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
286 NULL
, NULL
, "data" },
291 struct protox ipxprotox
[] = {
292 { N_IPX
, N_IPXSTAT
, 1, ipxprotopr
,
293 ipx_stats
, NULL
, "ipx", 0 },
294 { N_IPX
, N_SPXSTAT
, 1, ipxprotopr
,
295 spx_stats
, NULL
, "spx", 0 },
301 struct protox nsprotox
[] = {
302 { N_IDP
, N_IDPSTAT
, 1, nsprotopr
,
303 idp_stats
, NULL
, "idp" },
304 { N_IDP
, N_SPPSTAT
, 1, nsprotopr
,
305 spp_stats
, NULL
, "spp" },
307 nserr_stats
, NULL
, "ns_err" },
314 struct protox isoprotox
[] = {
315 { ISO_TP
, N_TPSTAT
, 1, iso_protopr
,
316 tp_stats
, NULL
, "tp" },
317 { N_CLTP
, N_CLTPSTAT
, 1, iso_protopr
,
318 cltp_stats
, NULL
, "cltp" },
319 { -1, N_CLNPSTAT
, 1, 0,
320 clnp_stats
, NULL
, "clnp"},
321 { -1, N_ESISSTAT
, 1, 0,
322 esis_stats
, NULL
, "esis"},
328 struct protox
*protoprotox
[] = {
337 ipxprotox
, atalkprotox
,
347 static void printproto (struct protox
*, char *);
348 static void usage (void);
349 static struct protox
*name2protox (char *);
350 static struct protox
*knownname (char *);
352 extern void _serv_cache_close();
356 static char *nlistf
= NULL
, *memf
= NULL
;
358 int Aflag
; /* show addresses of protocol control block */
359 int aflag
; /* show all sockets (including servers) */
360 int bflag
; /* show i/f total bytes in/out */
361 int dflag
; /* show i/f dropped packets */
362 int gflag
; /* show group (multicast) routing or stats */
363 int iflag
; /* show interfaces */
364 int lflag
; /* show routing table with use and ref */
365 int Lflag
; /* show size of listen queues */
366 int mflag
; /* show memory stats */
367 int nflag
; /* show addresses numerically */
368 static int pflag
; /* show given protocol */
369 int rflag
; /* show routing tables (or routing stats) */
370 int sflag
; /* show protocol statistics */
371 int tflag
; /* show i/f watchdog timers */
372 int Wflag
; /* wide display */
374 int interval
; /* repeat interval for i/f stats */
376 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
377 int unit
; /* unit number for above */
379 int af
; /* address family */
386 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
391 while ((ch
= getopt(argc
, argv
, "Aabdf:gI:iLlM:mN:np:rstuWw:")) != -1)
407 if (strcmp(optarg
, "ns") == 0)
411 if (strcmp(optarg
, "ipx") == 0)
413 else if (strcmp(optarg
, "inet") == 0)
416 else if (strcmp(optarg
, "inet6") == 0)
420 else if (strcmp(optarg
, "pfkey") == 0)
423 else if (strcmp(optarg
, "unix") == 0)
426 else if (strcmp(optarg
, "atalk") == 0)
428 else if (strcmp(optarg
, "ng") == 0
429 || strcmp(optarg
, "netgraph") == 0)
433 else if (strcmp(optarg
, "iso") == 0)
437 errx(1, "%s: unknown address family", optarg
);
447 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
474 if ((tp
= name2protox(optarg
)) == NULL
) {
476 "%s: unknown or uninstrumented protocol",
497 interval
= atoi(optarg
);
507 #define BACKWARD_COMPATIBILITY
508 #ifdef BACKWARD_COMPATIBILITY
510 if (isdigit(**argv
)) {
511 interval
= atoi(*argv
);
526 * Discard setgid privileges if not the running kernel so that bad
527 * guys can't print interesting stuff from kernel memory.
529 if (nlistf
!= NULL
|| memf
!= NULL
)
533 //if (memf != NULL) {
534 if (kread(0, 0, 0) == 0)
535 mbpr(nl
[N_MBSTAT
].n_value
);
536 //mbpr(nl[N_MBSTAT].n_value,
537 // nl[N_MBTYPES].n_value,
538 // nl[N_NMBCLUSTERS].n_value,
539 // nl[N_NMBUFS].n_value);
547 * Keep file descriptors open to avoid overhead
548 * of open/close on each call to get* routines.
554 * This does not make sense any more with DNS being default over
555 * the files. Doing a setXXXXent(1) causes a tcp connection to be
556 * used for the queries, which is slower.
559 if (iflag
&& !sflag
) {
561 intpr(interval
, nl
[N_IFNET
].n_value
, NULL
);
567 rt_stats(nl
[N_RTSTAT
].n_value
, nl
[N_RTTRASH
].n_value
);
569 routepr(nl
[N_RTREE
].n_value
);
575 if (af
== AF_INET
|| af
== AF_UNSPEC
)
576 mrt_stats(nl
[N_MRTSTAT
].n_value
);
578 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
579 mrt6_stats(nl
[N_MRT6STAT
].n_value
);
582 if (af
== AF_INET
|| af
== AF_UNSPEC
)
583 mroutepr(nl
[N_MFCTABLE
].n_value
,
584 nl
[N_VIFTABLE
].n_value
);
586 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
587 mroute6pr(nl
[N_MF6CTABLE
].n_value
,
588 nl
[N_MIF6TABLE
].n_value
);
596 printproto(tp
, tp
->pr_name
);
599 if (af
== AF_INET
|| af
== AF_UNSPEC
)
600 for (tp
= protox
; tp
->pr_name
; tp
++)
601 printproto(tp
, tp
->pr_name
);
603 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
604 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
605 printproto(tp
, tp
->pr_name
);
608 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
609 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
610 printproto(tp
, tp
->pr_name
);
613 if (af
== AF_IPX
|| af
== AF_UNSPEC
) {
615 for (tp
= ipxprotox
; tp
->pr_name
; tp
++)
616 printproto(tp
, tp
->pr_name
);
618 if (af
== AF_APPLETALK
|| af
== AF_UNSPEC
)
619 for (tp
= atalkprotox
; tp
->pr_name
; tp
++)
620 printproto(tp
, tp
->pr_name
);
621 if (af
== AF_NETGRAPH
|| af
== AF_UNSPEC
)
622 for (tp
= netgraphprotox
; tp
->pr_name
; tp
++)
623 printproto(tp
, tp
->pr_name
);
626 if (af
== AF_NS
|| af
== AF_UNSPEC
)
627 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
628 printproto(tp
, tp
->pr_name
);
631 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
632 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
633 printproto(tp
, tp
->pr_name
);
635 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
644 * Print out protocol statistics or control blocks (per sflag).
645 * If the interface was not specifically requested, and the symbol
646 * is not in the namelist, ignore this one.
650 register struct protox
*tp
;
653 void (*pr
)(u_long
, char *, int);
659 intpr(interval
, nl
[N_IFNET
].n_value
,
662 printf("%s: no per-interface stats routine\n",
670 printf("%s: no stats routine\n",
674 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
675 : nl
[tp
->pr_sindex
].n_value
;
681 printf("%s: no PCB routine\n", tp
->pr_name
);
684 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
685 : nl
[tp
->pr_index
].n_value
;
687 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
688 (*pr
)(off
, name
, af
);
692 * Read kernel memory, return 0 on success.
695 kread(u_long addr
, char *buf
, int size
)
701 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
703 if (kvm_nlist(kvmd
, nl
) < 0) {
705 errx(1, "%s: kvm_nlist: %s", nlistf
,
708 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
711 if (nl
[0].n_type
== 0) {
713 errx(1, "%s: no namelist", nlistf
);
715 errx(1, "no namelist");
718 warnx("kvm not available");
724 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
725 warnx("%s", kvm_geterr(kvmd
));
734 return (n
!= 1 ? "s" : "");
740 return (n
!= 1 ? "es" : "");
744 * Find the protox for the given "well-known" name.
746 static struct protox
*
747 knownname(char *name
)
749 struct protox
**tpp
, *tp
;
751 for (tpp
= protoprotox
; *tpp
; tpp
++)
752 for (tp
= *tpp
; tp
->pr_name
; tp
++)
753 if (strcmp(tp
->pr_name
, name
) == 0)
759 * Find the protox corresponding to name.
761 static struct protox
*
762 name2protox(char *name
)
765 char **alias
; /* alias from p->aliases */
769 * Try to find the name in the list of "well-known" names. If that
770 * fails, check if name is an alias for an Internet protocol.
772 if ((tp
= knownname(name
)) != NULL
)
775 setprotoent(1); /* make protocol lookup cheaper */
776 while ((p
= getprotoent()) != NULL
) {
777 /* assert: name not same as p->name */
778 for (alias
= p
->p_aliases
; *alias
; alias
++)
779 if (strcmp(name
, *alias
) == 0) {
781 return (knownname(p
->p_name
));
791 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n",
792 "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]",
793 " netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]",
794 " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
795 " netstat -m [-M core] [-N system]",
796 " netstat [-M core] [-N system] [-p protocol]");