]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
102c933f4062d2b6289d782333fa8102a8ff3926
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1983, 1988, 1993
30 * Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 char const copyright
[] =
63 "@(#) Copyright (c) 1983, 1988, 1993\n\
64 Regents of the University of California. All rights reserved.\n";
67 #include <sys/param.h>
69 #include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <net/pfkeyv2.h>
86 #include <sys/types.h>
87 #include <sys/sysctl.h>
90 #include <TargetConditionals.h>
94 * ----------------------------------------------------------------------------
95 * "THE BEER-WARE LICENSE" (Revision 42):
96 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
97 * can do whatever you want with this stuff. If we meet some day, and you think
98 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
99 * ----------------------------------------------------------------------------
101 * $Id: main.c,v 1.8 2004/10/14 22:24:09 lindak Exp $
105 static struct nlist nl
[] = {
130 #define N_ESISSTAT 12
138 #define N_CLTPSTAT 16
158 #define N_ICMP6STAT 29
160 #define N_IPSECSTAT 30
162 #define N_IPSEC6STAT 31
164 #define N_PIM6STAT 32
166 #define N_MRT6PROTO 33
168 #define N_MRT6STAT 34
170 #define N_MF6CTABLE 35
172 #define N_MIF6TABLE 36
174 #define N_PFKEYSTAT 37
180 #define N_NMBCLUSTERS 40
191 u_char pr_index
; /* index into nlist of cb head */
192 u_char pr_sindex
; /* index into nlist of stat block */
193 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
194 void (*pr_cblocks
)(uint32_t, char *, int);
195 /* control blocks printing routine */
196 void (*pr_stats
)(uint32_t, char *, int);
197 /* statistics printing routine */
198 void (*pr_istats
)(char *); /* per/if statistics printing routine */
199 char *pr_name
; /* well-known name */
200 int pr_usesysctl
; /* true if we use sysctl, not kvm */
202 { -1, -1, 1, protopr
,
203 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
204 { -1, -1, 1, protopr
,
205 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
206 { -1, -1, 1, protopr
,
207 NULL
, NULL
, "divert",IPPROTO_DIVERT
},
208 { -1, -1, 1, protopr
,
209 ip_stats
, NULL
, "ip", IPPROTO_RAW
},
210 { -1, -1, 1, protopr
,
211 icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
212 { -1, -1, 1, protopr
,
213 igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
216 ipsec_stats
, NULL
, "ipsec", IPPROTO_ESP
},
223 struct protox ip6protox
[] = {
224 { -1, -1, 1, protopr
,
225 tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
226 { -1, -1, 1, protopr
,
227 udp_stats
, NULL
, "udp", IPPROTO_UDP
},
228 { -1, N_IP6STAT
, 1, protopr
,
229 ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
230 { -1, N_ICMP6STAT
, 1, protopr
,
231 icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
233 { -1, N_IPSEC6STAT
, 1, 0,
234 ipsec_stats
, NULL
, "ipsec6",IPPROTO_ESP
},
237 { -1, N_PIM6STAT
, 1, 0,
238 pim6_stats
, NULL
, "pim6", 0 },
241 rip6_stats
, NULL
, "rip6", IPPROTO_RAW
},
248 struct protox pfkeyprotox
[] = {
249 { -1, N_PFKEYSTAT
, 1, 0,
250 pfkey_stats
, NULL
, "pfkey", PF_KEY_V2
},
256 struct protox
*protoprotox
[] = {
266 static void printproto (struct protox
*, char *);
267 static void usage (void);
268 static struct protox
*name2protox (char *);
269 static struct protox
*knownname (char *);
271 extern void _serv_cache_close();
274 int Aflag
; /* show addresses of protocol control block */
275 int aflag
; /* show all sockets (including servers) */
276 int bflag
; /* show i/f total bytes in/out */
277 int dflag
; /* show i/f dropped packets */
278 #if defined(__APPLE__)
279 int gflag
; /* show group (multicast) routing or stats */
281 int iflag
; /* show interfaces */
282 int lflag
; /* show routing table with use and ref */
283 int Lflag
; /* show size of listen queues */
284 int mflag
; /* show memory stats */
285 int nflag
; /* show addresses numerically */
286 static int pflag
; /* show given protocol */
287 int prioflag
; /* show packet priority statistics */
288 int rflag
; /* show routing tables (or routing stats) */
289 int sflag
; /* show protocol statistics */
290 int tflag
; /* show i/f watchdog timers */
291 int Wflag
; /* wide display */
293 int interval
; /* repeat interval for i/f stats */
295 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
296 int unit
; /* unit number for above */
298 int af
; /* address family */
305 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
310 while ((ch
= getopt(argc
, argv
, "Aabdf:gI:iLlmnPp:rRstuWw:")) != -1)
325 if (strcmp(optarg
, "ipx") == 0)
327 else if (strcmp(optarg
, "inet") == 0)
330 else if (strcmp(optarg
, "inet6") == 0)
334 else if (strcmp(optarg
, "pfkey") == 0)
337 else if (strcmp(optarg
, "unix") == 0)
340 errx(1, "%s: unknown address family", optarg
);
343 #if defined(__APPLE__)
352 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
376 if ((tp
= name2protox(optarg
)) == NULL
) {
378 "%s: unknown or uninstrumented protocol",
399 interval
= atoi(optarg
);
409 #define BACKWARD_COMPATIBILITY
410 #ifdef BACKWARD_COMPATIBILITY
412 if (isdigit(**argv
)) {
413 interval
= atoi(*argv
);
426 if (iflag
&& !sflag
) {
434 routepr(nl
[N_RTREE
].n_value
);
437 #if defined(__APPLE__)
439 #if !TARGET_OS_EMBEDDED
441 if (af
== AF_INET
|| af
== AF_UNSPEC
)
444 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
448 if (af
== AF_INET
|| af
== AF_UNSPEC
)
451 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
455 #endif /* !TARGET_OS_EMBEDDED */
462 printproto(tp
, tp
->pr_name
);
465 if (af
== AF_INET
|| af
== AF_UNSPEC
)
466 for (tp
= protox
; tp
->pr_name
; tp
++)
467 printproto(tp
, tp
->pr_name
);
469 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
470 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
471 printproto(tp
, tp
->pr_name
);
474 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
475 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
476 printproto(tp
, tp
->pr_name
);
478 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
487 * Print out protocol statistics or control blocks (per sflag).
488 * If the interface was not specifically requested, and the symbol
489 * is not in the namelist, ignore this one.
493 register struct protox
*tp
;
496 void (*pr
)(uint32_t, char *, int);
500 if (iflag
&& !pflag
) {
502 intpr(tp
->pr_istats
);
504 printf("%s: no per-interface stats routine\n",
512 printf("%s: no stats routine\n",
516 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
517 : nl
[tp
->pr_sindex
].n_value
;
523 printf("%s: no PCB routine\n", tp
->pr_name
);
526 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
527 : nl
[tp
->pr_index
].n_value
;
529 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
)) {
530 if (sflag
&& iflag
&& pflag
)
531 intervalpr(pr
, off
, name
, af
);
533 (*pr
)(off
, name
, af
);
535 printf("### no stats for %s\n", name
);
542 return (n
!= 1 ? "s" : "");
548 return (n
!= 1 ? "es" : "");
552 * Find the protox for the given "well-known" name.
554 static struct protox
*
555 knownname(char *name
)
557 struct protox
**tpp
, *tp
;
559 for (tpp
= protoprotox
; *tpp
; tpp
++)
560 for (tp
= *tpp
; tp
->pr_name
; tp
++)
561 if (strcmp(tp
->pr_name
, name
) == 0)
567 * Find the protox corresponding to name.
569 static struct protox
*
570 name2protox(char *name
)
573 char **alias
; /* alias from p->aliases */
577 * Try to find the name in the list of "well-known" names. If that
578 * fails, check if name is an alias for an Internet protocol.
580 if ((tp
= knownname(name
)) != NULL
)
583 setprotoent(1); /* make protocol lookup cheaper */
584 while ((p
= getprotoent()) != NULL
) {
585 /* assert: name not same as p->name */
586 for (alias
= p
->p_aliases
; *alias
; alias
++)
587 if (strcmp(name
, *alias
) == 0) {
589 return (knownname(p
->p_name
));
596 #define NETSTAT_USAGE "\
597 Usage: netstat [-AaLlnW] [-f address_family | -p protocol]\n\
598 netstat [-gilns] [-f address_family]\n\
599 netstat -i | -I interface [-w wait] [-abdgt]\n\
600 netstat -s [-s] [-f address_family | -p protocol] [-w wait]\n\
601 netstat -i | -I interface -s [-f address_family | -p protocol]\n\
603 netstat -r [-Aaln] [-f address_family]\n\
610 (void) fprintf(stderr
, "%s\n", NETSTAT_USAGE
);