]>
git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/netstat.tproj/main.c
2 * Copyright (c) 2008-2020 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>
70 #include <sys/sys_domain.h>
72 #include <netinet/in.h>
73 #include <net/pfkeyv2.h>
87 #include <sys/types.h>
88 #include <sys/sysctl.h>
91 #include <TargetConditionals.h>
95 * ----------------------------------------------------------------------------
96 * "THE BEER-WARE LICENSE" (Revision 42):
97 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
98 * can do whatever you want with this stuff. If we meet some day, and you think
99 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
100 * ----------------------------------------------------------------------------
102 * $Id: main.c,v 1.8 2004/10/14 22:24:09 lindak Exp $
107 void (*pr_cblocks
)(uint32_t, char *, int);
108 /* control blocks printing routine */
109 void (*pr_stats
)(uint32_t, char *, int);
110 /* statistics printing routine */
111 void (*pr_istats
)(char *); /* per/if statistics printing routine */
112 char *pr_name
; /* well-known name */
115 { protopr
, tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
116 { protopr
, udp_stats
, NULL
, "udp", IPPROTO_UDP
},
117 { protopr
, NULL
, NULL
, "divert", IPPROTO_DIVERT
},
118 { protopr
, ip_stats
, NULL
, "ip", IPPROTO_RAW
},
119 { protopr
, icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
120 { protopr
, igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
122 { NULL
, ipsec_stats
, NULL
, "ipsec", IPPROTO_ESP
},
124 { NULL
, arp_stats
, NULL
, "arp", 0 },
125 { mptcppr
, mptcp_stats
, NULL
, "mptcp", IPPROTO_TCP
},
126 { NULL
, NULL
, NULL
, NULL
, 0 }
130 struct protox ip6protox
[] = {
131 { protopr
, tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
132 { protopr
, udp_stats
, NULL
, "udp", IPPROTO_UDP
},
133 { protopr
, ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
134 { protopr
, icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
136 { NULL
, ipsec_stats
, NULL
, "ipsec6", IPPROTO_ESP
},
138 { NULL
, rip6_stats
, NULL
, "rip6", IPPROTO_RAW
},
139 { mptcppr
, mptcp_stats
, NULL
, "mptcp", IPPROTO_TCP
},
140 { NULL
, NULL
, NULL
, NULL
, 0 }
145 struct protox pfkeyprotox
[] = {
146 { NULL
, pfkey_stats
, NULL
, "pfkey", PF_KEY_V2
},
147 { NULL
, NULL
, NULL
, NULL
, 0 }
152 struct protox systmprotox
[] = {
153 { systmpr
, NULL
, NULL
, "reg", 0 },
154 { systmpr
, kevt_stats
, NULL
, "kevt", SYSPROTO_EVENT
},
155 { systmpr
, kctl_stats
, NULL
, "kctl", SYSPROTO_CONTROL
},
156 { NULL
, NULL
, NULL
, NULL
, 0 }
159 struct protox nstatprotox
[] = {
160 { NULL
, print_nstat_stats
, NULL
, "nstat", 0 },
161 { NULL
, NULL
, NULL
, NULL
, 0 }
164 struct protox ipcprotox
[] = {
165 { NULL
, print_extbkidle_stats
, NULL
, "xbkidle", 0 },
166 { NULL
, NULL
, NULL
, NULL
, 0 }
169 struct protox kernprotox
[] = {
170 { NULL
, print_net_api_stats
, NULL
, "net_api", 0 },
171 { NULL
, NULL
, NULL
, NULL
, 0 }
175 struct protox vsockprotox
[] = {
176 { vsockpr
, NULL
, NULL
, "vsock", 0 },
177 { NULL
, NULL
, NULL
, NULL
, 0 }
181 struct protox
*protoprotox
[] = {
199 static void printproto (struct protox
*, char *);
200 static void usage (void);
201 static struct protox
*name2protox (char *);
202 static struct protox
*knownname (char *);
204 extern void _serv_cache_close();
207 int Aflag
; /* show addresses of protocol control block */
208 int aflag
; /* show all sockets (including servers) */
209 int bflag
; /* show i/f total bytes in/out */
210 int cflag
; /* show specific classq */
211 int dflag
; /* show i/f dropped packets */
212 int Fflag
; /* show i/f forwarded packets */
213 #if defined(__APPLE__)
214 int gflag
; /* show group (multicast) routing or stats */
216 int iflag
; /* show interfaces */
217 int lflag
; /* show routing table with more information */
218 int Lflag
; /* show size of listen queues */
219 int mflag
; /* show memory stats */
220 int nflag
; /* show addresses numerically */
221 static int pflag
; /* show given protocol */
222 int prioflag
= -1; /* show packet priority statistics */
223 int Rflag
; /* show reachability information */
224 int rflag
; /* show routing tables (or routing stats) */
225 int sflag
; /* show protocol statistics */
226 int Sflag
; /* show additional i/f link status */
227 int tflag
; /* show i/f watchdog timers */
228 int vflag
; /* more verbose */
229 int Wflag
; /* wide display */
230 int qflag
; /* classq stats display */
231 int Qflag
; /* opportunistic polling stats display */
232 int xflag
; /* show extended link-layer reachability information */
233 int zflag
; /* show only entries with non zero rtt metrics */
235 int cq
= -1; /* send classq index (-1 for all) */
236 int interval
; /* repeat interval for i/f stats */
238 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
239 int unit
; /* unit number for above */
241 int af
; /* address family */
248 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
253 while ((ch
= getopt(argc
, argv
, "Aabc:dFf:gI:ikLlmnP:p:qQrRsStuvWw:xz")) != -1)
275 if (strcmp(optarg
, "ipx") == 0)
277 else if (strcmp(optarg
, "inet") == 0)
280 else if (strcmp(optarg
, "inet6") == 0)
284 else if (strcmp(optarg
, "pfkey") == 0)
287 else if (strcmp(optarg
, "unix") == 0)
289 else if (strcmp(optarg
, "systm") == 0)
292 errx(1, "%s: unknown address family", optarg
);
295 #if defined(__APPLE__)
304 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
325 prioflag
= atoi(optarg
);
328 if ((tp
= name2protox(optarg
)) == NULL
) {
330 "%s: unknown or uninstrumented protocol",
366 interval
= atoi(optarg
);
383 #define BACKWARD_COMPATIBILITY
384 #ifdef BACKWARD_COMPATIBILITY
386 if (isdigit(**argv
)) {
387 interval
= atoi(*argv
);
400 if (iflag
&& !sflag
&& !Sflag
&& !gflag
&& !qflag
&& !Qflag
) {
414 if (qflag
|| Qflag
) {
415 if (interface
== NULL
) {
416 fprintf(stderr
, "%s statistics option "
417 "requires interface name\n", qflag
? "Queue" :
427 if (interface
== NULL
) {
428 fprintf(stderr
, "additional link status option"
429 " requires interface name\n");
431 print_link_status(interface
);
436 #if defined(__APPLE__)
444 printproto(tp
, tp
->pr_name
);
447 if (af
== AF_INET
|| af
== AF_UNSPEC
)
448 for (tp
= protox
; tp
->pr_name
; tp
++)
449 printproto(tp
, tp
->pr_name
);
451 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
452 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
453 printproto(tp
, tp
->pr_name
);
456 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
457 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
458 printproto(tp
, tp
->pr_name
);
460 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
463 if ((af
== AF_SYSTEM
|| af
== AF_UNSPEC
) && !Lflag
)
464 for (tp
= systmprotox
; tp
->pr_name
; tp
++)
465 printproto(tp
, tp
->pr_name
);
467 if (af
== AF_UNSPEC
&& !Lflag
)
468 for (tp
= nstatprotox
; tp
->pr_name
; tp
++)
469 printproto(tp
, tp
->pr_name
);
470 #endif /* TARGET_OS_IPHONE */
472 if (af
== AF_UNSPEC
&& !Lflag
)
473 for (tp
= ipcprotox
; tp
->pr_name
; tp
++)
474 printproto(tp
, tp
->pr_name
);
476 if (af
== AF_UNSPEC
&& !Lflag
)
477 for (tp
= kernprotox
; tp
->pr_name
; tp
++)
478 printproto(tp
, tp
->pr_name
);
481 if (af
== AF_VSOCK
|| af
== AF_UNSPEC
)
482 for (tp
= vsockprotox
; tp
->pr_name
; tp
++)
483 printproto(tp
, tp
->pr_name
);
493 * Print out protocol statistics or control blocks (per sflag).
494 * If the interface was not specifically requested, and the symbol
495 * is not in the namelist, ignore this one.
499 register struct protox
*tp
;
502 void (*pr
)(uint32_t, char *, int);
506 if (iflag
&& !pflag
) {
508 intpr(tp
->pr_istats
);
510 printf("%s: no per-interface stats routine\n",
518 printf("%s: no stats routine\n",
522 off
= tp
->pr_protocol
;
528 printf("%s: no PCB routine\n", tp
->pr_name
);
531 off
= tp
->pr_protocol
;
534 if (sflag
&& iflag
&& pflag
)
535 intervalpr(pr
, off
, name
, af
);
537 (*pr
)(off
, name
, af
);
539 printf("### no stats for %s\n", name
);
546 return (n
> 1 ? "s" : "");
552 return (n
> 1 ? "es" : "");
558 return (n
> 1 ? "ies" : "y");
562 * Find the protox for the given "well-known" name.
564 static struct protox
*
565 knownname(char *name
)
567 struct protox
**tpp
, *tp
;
569 for (tpp
= protoprotox
; *tpp
; tpp
++)
570 for (tp
= *tpp
; tp
->pr_name
; tp
++)
571 if (strcmp(tp
->pr_name
, name
) == 0)
577 * Find the protox corresponding to name.
579 static struct protox
*
580 name2protox(char *name
)
583 char **alias
; /* alias from p->aliases */
587 * Try to find the name in the list of "well-known" names. If that
588 * fails, check if name is an alias for an Internet protocol.
590 if ((tp
= knownname(name
)) != NULL
)
593 setprotoent(1); /* make protocol lookup cheaper */
594 while ((p
= getprotoent()) != NULL
) {
595 /* assert: name not same as p->name */
596 for (alias
= p
->p_aliases
; *alias
; alias
++)
597 if (strcmp(name
, *alias
) == 0) {
598 tp
= knownname(p
->p_name
);
607 #define NETSTAT_USAGE "\
608 Usage: netstat [-AaLlnW] [-f address_family | -p protocol]\n\
609 netstat [-gilns] [-f address_family]\n\
610 netstat -i | -I interface [-w wait] [-abdgRtS]\n\
611 netstat -s [-s] [-f address_family | -p protocol] [-w wait]\n\
612 netstat -i | -I interface -s [-f address_family | -p protocol]\n\
614 netstat -r [-Aaln] [-f address_family]\n\
621 (void) fprintf(stderr
, "%s\n", NETSTAT_USAGE
);
633 (void) localtime_r(&now
, &tm
);
635 num_written
+= printf("%02d:%02d:%02d ", tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
637 return (num_written
);