]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
d1fc0a8de33e7a6925239900a89d6ffe9bc7ee08
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>
90 #include <sys/types.h>
91 #include <sys/sysctl.h>
94 #include <TargetConditionals.h>
98 * ----------------------------------------------------------------------------
99 * "THE BEER-WARE LICENSE" (Revision 42):
100 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
101 * can do whatever you want with this stuff. If we meet some day, and you think
102 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
103 * ----------------------------------------------------------------------------
105 * $Id: main.c,v 1.8 2004/10/14 22:24:09 lindak Exp $
109 static struct nlist nl
[] = {
134 #define N_ESISSTAT 12
142 #define N_CLTPSTAT 16
162 #define N_ICMP6STAT 29
164 #define N_IPSECSTAT 30
166 #define N_IPSEC6STAT 31
168 #define N_PIM6STAT 32
170 #define N_MRT6PROTO 33
172 #define N_MRT6STAT 34
174 #define N_MF6CTABLE 35
176 #define N_MIF6TABLE 36
178 #define N_PFKEYSTAT 37
184 #define N_NMBCLUSTERS 40
195 u_char pr_index
; /* index into nlist of cb head */
196 u_char pr_sindex
; /* index into nlist of stat block */
197 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
198 void (*pr_cblocks
)(u_long
, char *, int);
199 /* control blocks printing routine */
200 void (*pr_stats
)(u_long
, char *, int);
201 /* statistics printing routine */
202 void (*pr_istats
)(char *); /* per/if statistics printing routine */
203 char *pr_name
; /* well-known name */
204 int pr_usesysctl
; /* true if we use sysctl, not kvm */
206 { -1, -1, 1, protopr
,
207 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
208 { -1, -1, 1, protopr
,
209 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
210 { -1, -1, 1, protopr
,
211 NULL
, NULL
, "divert",IPPROTO_DIVERT
},
212 { -1, -1, 1, protopr
,
213 ip_stats
, NULL
, "ip", IPPROTO_RAW
},
214 { -1, -1, 1, protopr
,
215 icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
216 { -1, -1, 1, protopr
,
217 igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
220 ipsec_stats
, NULL
, "ipsec", IPPROTO_ESP
},
224 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
231 struct protox ip6protox
[] = {
232 { -1, -1, 1, protopr
,
233 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
234 { -1, -1, 1, protopr
,
235 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
236 { -1, N_IP6STAT
, 1, protopr
,
237 ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
238 { -1, N_ICMP6STAT
, 1, protopr
,
239 icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
241 { -1, N_IPSEC6STAT
, 1, 0,
242 ipsec_stats
, NULL
, "ipsec6",IPPROTO_ESP
},
245 { -1, N_PIM6STAT
, 1, 0,
246 pim6_stats
, NULL
, "pim6", 0 },
249 rip6_stats
, NULL
, "rip6", IPPROTO_RAW
},
252 bdg_stats
, NULL
, "bdg", 1 /* bridging... */ },
260 struct protox pfkeyprotox
[] = {
261 { -1, N_PFKEYSTAT
, 1, 0,
262 pfkey_stats
, NULL
, "pfkey", PF_KEY_V2
},
268 struct protox atalkprotox
[] = {
269 { N_DDPCB
, N_DDPSTAT
, 1, atalkprotopr
,
270 ddp_stats
, NULL
, "ddp" },
275 struct protox netgraphprotox
[] = {
276 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
277 NULL
, NULL
, "ctrl" },
278 { N_NGSOCKS
, -1, 1, netgraphprotopr
,
279 NULL
, NULL
, "data" },
284 struct protox ipxprotox
[] = {
285 { N_IPX
, N_IPXSTAT
, 1, ipxprotopr
,
286 ipx_stats
, NULL
, "ipx", 0 },
287 { N_IPX
, N_SPXSTAT
, 1, ipxprotopr
,
288 spx_stats
, NULL
, "spx", 0 },
294 struct protox nsprotox
[] = {
295 { N_IDP
, N_IDPSTAT
, 1, nsprotopr
,
296 idp_stats
, NULL
, "idp" },
297 { N_IDP
, N_SPPSTAT
, 1, nsprotopr
,
298 spp_stats
, NULL
, "spp" },
300 nserr_stats
, NULL
, "ns_err" },
307 struct protox isoprotox
[] = {
308 { ISO_TP
, N_TPSTAT
, 1, iso_protopr
,
309 tp_stats
, NULL
, "tp" },
310 { N_CLTP
, N_CLTPSTAT
, 1, iso_protopr
,
311 cltp_stats
, NULL
, "cltp" },
312 { -1, N_CLNPSTAT
, 1, 0,
313 clnp_stats
, NULL
, "clnp"},
314 { -1, N_ESISSTAT
, 1, 0,
315 esis_stats
, NULL
, "esis"},
321 struct protox
*protoprotox
[] = {
330 ipxprotox
, atalkprotox
,
340 static void printproto (struct protox
*, char *);
341 static void usage (void);
342 static struct protox
*name2protox (char *);
343 static struct protox
*knownname (char *);
345 extern void _serv_cache_close();
348 int Aflag
; /* show addresses of protocol control block */
349 int aflag
; /* show all sockets (including servers) */
350 int bflag
; /* show i/f total bytes in/out */
351 int dflag
; /* show i/f dropped packets */
352 #if defined(__APPLE__) && !TARGET_OS_EMBEDDED
353 int gflag
; /* show group (multicast) routing or stats */
355 int iflag
; /* show interfaces */
356 int lflag
; /* show routing table with use and ref */
357 int Lflag
; /* show size of listen queues */
358 int mflag
; /* show memory stats */
359 int nflag
; /* show addresses numerically */
360 static int pflag
; /* show given protocol */
361 int rflag
; /* show routing tables (or routing stats) */
362 int sflag
; /* show protocol statistics */
363 int tflag
; /* show i/f watchdog timers */
364 int Wflag
; /* wide display */
366 int interval
; /* repeat interval for i/f stats */
368 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
369 int unit
; /* unit number for above */
371 int af
; /* address family */
378 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
383 while ((ch
= getopt(argc
, argv
, "Aabdf:gI:iLlmnp:rRstuWw:")) != -1)
399 if (strcmp(optarg
, "ns") == 0)
403 if (strcmp(optarg
, "ipx") == 0)
405 else if (strcmp(optarg
, "inet") == 0)
408 else if (strcmp(optarg
, "inet6") == 0)
412 else if (strcmp(optarg
, "pfkey") == 0)
415 else if (strcmp(optarg
, "unix") == 0)
418 else if (strcmp(optarg
, "atalk") == 0)
420 else if (strcmp(optarg
, "ng") == 0
421 || strcmp(optarg
, "netgraph") == 0)
425 else if (strcmp(optarg
, "iso") == 0)
429 errx(1, "%s: unknown address family", optarg
);
432 #if defined(__APPLE__) && !TARGET_OS_EMBEDDED
441 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
462 if ((tp
= name2protox(optarg
)) == NULL
) {
464 "%s: unknown or uninstrumented protocol",
485 interval
= atoi(optarg
);
495 #define BACKWARD_COMPATIBILITY
496 #ifdef BACKWARD_COMPATIBILITY
498 if (isdigit(**argv
)) {
499 interval
= atoi(*argv
);
514 * Keep file descriptors open to avoid overhead
515 * of open/close on each call to get* routines.
521 * This does not make sense any more with DNS being default over
522 * the files. Doing a setXXXXent(1) causes a tcp connection to be
523 * used for the queries, which is slower.
526 if (iflag
&& !sflag
) {
534 routepr(nl
[N_RTREE
].n_value
);
537 #if defined(__APPLE__) && !TARGET_OS_EMBEDDED
540 if (af
== AF_INET
|| af
== AF_UNSPEC
)
543 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
547 if (af
== AF_INET
|| af
== AF_UNSPEC
)
550 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
560 printproto(tp
, tp
->pr_name
);
563 if (af
== AF_INET
|| af
== AF_UNSPEC
)
564 for (tp
= protox
; tp
->pr_name
; tp
++)
565 printproto(tp
, tp
->pr_name
);
567 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
568 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
569 printproto(tp
, tp
->pr_name
);
572 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
573 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
574 printproto(tp
, tp
->pr_name
);
577 if (af
== AF_IPX
|| af
== AF_UNSPEC
) {
578 for (tp
= ipxprotox
; tp
->pr_name
; tp
++)
579 printproto(tp
, tp
->pr_name
);
581 if (af
== AF_APPLETALK
|| af
== AF_UNSPEC
)
582 for (tp
= atalkprotox
; tp
->pr_name
; tp
++)
583 printproto(tp
, tp
->pr_name
);
584 if (af
== AF_NETGRAPH
|| af
== AF_UNSPEC
)
585 for (tp
= netgraphprotox
; tp
->pr_name
; tp
++)
586 printproto(tp
, tp
->pr_name
);
589 if (af
== AF_NS
|| af
== AF_UNSPEC
)
590 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
591 printproto(tp
, tp
->pr_name
);
594 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
595 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
596 printproto(tp
, tp
->pr_name
);
598 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
607 * Print out protocol statistics or control blocks (per sflag).
608 * If the interface was not specifically requested, and the symbol
609 * is not in the namelist, ignore this one.
613 register struct protox
*tp
;
616 void (*pr
)(u_long
, char *, int);
620 if (iflag
&& !pflag
) {
622 intpr(tp
->pr_istats
);
624 printf("%s: no per-interface stats routine\n",
632 printf("%s: no stats routine\n",
636 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
637 : nl
[tp
->pr_sindex
].n_value
;
643 printf("%s: no PCB routine\n", tp
->pr_name
);
646 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
647 : nl
[tp
->pr_index
].n_value
;
649 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
)) {
650 if (sflag
&& iflag
&& pflag
)
651 intervalpr(pr
, off
, name
, af
);
653 (*pr
)(off
, name
, af
);
655 printf("### no stats for %s\n", name
);
662 return (n
!= 1 ? "s" : "");
668 return (n
!= 1 ? "es" : "");
672 * Find the protox for the given "well-known" name.
674 static struct protox
*
675 knownname(char *name
)
677 struct protox
**tpp
, *tp
;
679 for (tpp
= protoprotox
; *tpp
; tpp
++)
680 for (tp
= *tpp
; tp
->pr_name
; tp
++)
681 if (strcmp(tp
->pr_name
, name
) == 0)
687 * Find the protox corresponding to name.
689 static struct protox
*
690 name2protox(char *name
)
693 char **alias
; /* alias from p->aliases */
697 * Try to find the name in the list of "well-known" names. If that
698 * fails, check if name is an alias for an Internet protocol.
700 if ((tp
= knownname(name
)) != NULL
)
703 setprotoent(1); /* make protocol lookup cheaper */
704 while ((p
= getprotoent()) != NULL
) {
705 /* assert: name not same as p->name */
706 for (alias
= p
->p_aliases
; *alias
; alias
++)
707 if (strcmp(name
, *alias
) == 0) {
709 return (knownname(p
->p_name
));
716 #define NETSTAT_USAGE "\
717 Usage: netstat [-AaLlnW] [-f address_family | -p protocol]\n\
718 netstat [-gilns] [-f address_family]\n\
719 netstat -i | -I interface [-w wait] [-abdgt]\n\
720 netstat -s [-s] [-f address_family | -p protocol] [-w wait]\n\
721 netstat -i | -I interface -s [-f address_family | -p protocol]\n\
723 netstat -r [-Aaln] [-f address_family]\n\
730 (void) fprintf(stderr
, "%s\n", NETSTAT_USAGE
);