]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
e097bc665f41e1f580ff5af1e9822ff90b8312cc
2 * Copyright (c) 2008-2013 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 $
106 void (*pr_cblocks
)(uint32_t, char *, int);
107 /* control blocks printing routine */
108 void (*pr_stats
)(uint32_t, char *, int);
109 /* statistics printing routine */
110 void (*pr_istats
)(char *); /* per/if statistics printing routine */
111 char *pr_name
; /* well-known name */
114 { protopr
, tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
115 { protopr
, udp_stats
, NULL
, "udp", IPPROTO_UDP
},
116 { protopr
, NULL
, NULL
, "divert", IPPROTO_DIVERT
},
117 { protopr
, ip_stats
, NULL
, "ip", IPPROTO_RAW
},
118 { protopr
, icmp_stats
, NULL
, "icmp", IPPROTO_ICMP
},
119 { protopr
, igmp_stats
, NULL
, "igmp", IPPROTO_IGMP
},
121 { NULL
, ipsec_stats
, NULL
, "ipsec", IPPROTO_ESP
},
123 { NULL
, arp_stats
, NULL
, "arp", 0 },
124 #if TARGET_OS_EMBEDDED
125 { mptcppr
, mptcp_stats
, NULL
, "mptcp", IPPROTO_TCP
},
127 { NULL
, NULL
, NULL
, NULL
, 0 }
131 struct protox ip6protox
[] = {
132 { protopr
, tcp_stats
, NULL
, "tcp", IPPROTO_TCP
},
133 { protopr
, udp_stats
, NULL
, "udp", IPPROTO_UDP
},
134 { protopr
, ip6_stats
, ip6_ifstats
, "ip6", IPPROTO_RAW
},
135 { protopr
, icmp6_stats
, icmp6_ifstats
, "icmp6",IPPROTO_ICMPV6
},
137 { NULL
, ipsec_stats
, NULL
, "ipsec6", IPPROTO_ESP
},
140 { NULL
, pim6_stats
, NULL
, "pim6", 0 },
142 { NULL
, rip6_stats
, NULL
, "rip6", IPPROTO_RAW
},
143 #if TARGET_OS_EMBEDDED
144 { mptcppr
, mptcp_stats
, NULL
, "mptcp", IPPROTO_TCP
},
146 { NULL
, NULL
, NULL
, NULL
, 0 }
151 struct protox pfkeyprotox
[] = {
152 { NULL
, pfkey_stats
, NULL
, "pfkey", PF_KEY_V2
},
153 { NULL
, NULL
, NULL
, NULL
, 0 }
157 struct protox
*protoprotox
[] = {
168 static void printproto (struct protox
*, char *);
169 static void usage (void);
170 static struct protox
*name2protox (char *);
171 static struct protox
*knownname (char *);
173 extern void _serv_cache_close();
176 int Aflag
; /* show addresses of protocol control block */
177 int aflag
; /* show all sockets (including servers) */
178 int bflag
; /* show i/f total bytes in/out */
179 int cflag
; /* show specific classq */
180 int dflag
; /* show i/f dropped packets */
181 int Fflag
; /* show i/f forwarded packets */
182 #if defined(__APPLE__)
183 int gflag
; /* show group (multicast) routing or stats */
185 int iflag
; /* show interfaces */
186 int lflag
; /* show routing table with use and ref */
187 int Lflag
; /* show size of listen queues */
188 int mflag
; /* show memory stats */
189 int nflag
; /* show addresses numerically */
190 static int pflag
; /* show given protocol */
191 int prioflag
= -1; /* show packet priority statistics */
192 int Rflag
; /* show reachability information */
193 int rflag
; /* show routing tables (or routing stats) */
194 int sflag
; /* show protocol statistics */
195 int tflag
; /* show i/f watchdog timers */
196 int vflag
; /* more verbose */
197 int Wflag
; /* wide display */
198 int qflag
; /* classq stats display */
199 int Qflag
; /* opportunistic polling stats display */
200 int xflag
; /* show extended link-layer reachability information */
202 int cq
= -1; /* send classq index (-1 for all) */
203 int interval
; /* repeat interval for i/f stats */
205 char *interface
; /* desired i/f for stats, or NULL for all i/fs */
206 int unit
; /* unit number for above */
208 int af
; /* address family */
215 register struct protox
*tp
= NULL
; /* for printing cblocks & stats */
220 while ((ch
= getopt(argc
, argv
, "Aabc:dFf:gI:iLlmnP:p:qQrRstuvWw:x")) != -1)
242 if (strcmp(optarg
, "ipx") == 0)
244 else if (strcmp(optarg
, "inet") == 0)
247 else if (strcmp(optarg
, "inet6") == 0)
251 else if (strcmp(optarg
, "pfkey") == 0)
254 else if (strcmp(optarg
, "unix") == 0)
257 errx(1, "%s: unknown address family", optarg
);
260 #if defined(__APPLE__)
269 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
290 prioflag
= atoi(optarg
);
293 if ((tp
= name2protox(optarg
)) == NULL
) {
295 "%s: unknown or uninstrumented protocol",
328 interval
= atoi(optarg
);
342 #define BACKWARD_COMPATIBILITY
343 #ifdef BACKWARD_COMPATIBILITY
345 if (isdigit(**argv
)) {
346 interval
= atoi(*argv
);
359 if (iflag
&& !sflag
&& !gflag
&& !qflag
&& !Qflag
) {
373 if (qflag
|| Qflag
) {
374 if (interface
== NULL
) {
375 fprintf(stderr
, "%s statistics option "
376 "requires interface name\n", qflag
? "Queue" :
386 #if defined(__APPLE__)
388 #if !TARGET_OS_EMBEDDED
390 if (af
== AF_INET
|| af
== AF_UNSPEC
)
393 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
397 if (af
== AF_INET
|| af
== AF_UNSPEC
)
400 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
404 #endif /* !TARGET_OS_EMBEDDED */
411 printproto(tp
, tp
->pr_name
);
414 if (af
== AF_INET
|| af
== AF_UNSPEC
)
415 for (tp
= protox
; tp
->pr_name
; tp
++)
416 printproto(tp
, tp
->pr_name
);
418 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
419 for (tp
= ip6protox
; tp
->pr_name
; tp
++)
420 printproto(tp
, tp
->pr_name
);
423 if (af
== PF_KEY
|| af
== AF_UNSPEC
)
424 for (tp
= pfkeyprotox
; tp
->pr_name
; tp
++)
425 printproto(tp
, tp
->pr_name
);
427 if ((af
== AF_UNIX
|| af
== AF_UNSPEC
) && !Lflag
&& !sflag
)
436 * Print out protocol statistics or control blocks (per sflag).
437 * If the interface was not specifically requested, and the symbol
438 * is not in the namelist, ignore this one.
442 register struct protox
*tp
;
445 void (*pr
)(uint32_t, char *, int);
449 if (iflag
&& !pflag
) {
451 intpr(tp
->pr_istats
);
453 printf("%s: no per-interface stats routine\n",
461 printf("%s: no stats routine\n",
465 off
= tp
->pr_protocol
;
471 printf("%s: no PCB routine\n", tp
->pr_name
);
474 off
= tp
->pr_protocol
;
477 if (sflag
&& iflag
&& pflag
)
478 intervalpr(pr
, off
, name
, af
);
480 (*pr
)(off
, name
, af
);
482 printf("### no stats for %s\n", name
);
489 return (n
!= 1 ? "s" : "");
495 return (n
!= 1 ? "es" : "");
501 return (n
!= 1 ? "ies" : "y");
505 * Find the protox for the given "well-known" name.
507 static struct protox
*
508 knownname(char *name
)
510 struct protox
**tpp
, *tp
;
512 for (tpp
= protoprotox
; *tpp
; tpp
++)
513 for (tp
= *tpp
; tp
->pr_name
; tp
++)
514 if (strcmp(tp
->pr_name
, name
) == 0)
520 * Find the protox corresponding to name.
522 static struct protox
*
523 name2protox(char *name
)
526 char **alias
; /* alias from p->aliases */
530 * Try to find the name in the list of "well-known" names. If that
531 * fails, check if name is an alias for an Internet protocol.
533 if ((tp
= knownname(name
)) != NULL
)
536 setprotoent(1); /* make protocol lookup cheaper */
537 while ((p
= getprotoent()) != NULL
) {
538 /* assert: name not same as p->name */
539 for (alias
= p
->p_aliases
; *alias
; alias
++)
540 if (strcmp(name
, *alias
) == 0) {
542 return (knownname(p
->p_name
));
549 #define NETSTAT_USAGE "\
550 Usage: netstat [-AaLlnW] [-f address_family | -p protocol]\n\
551 netstat [-gilns] [-f address_family]\n\
552 netstat -i | -I interface [-w wait] [-abdgRt]\n\
553 netstat -s [-s] [-f address_family | -p protocol] [-w wait]\n\
554 netstat -i | -I interface -s [-f address_family | -p protocol]\n\
556 netstat -r [-Aaln] [-f address_family]\n\
563 (void) fprintf(stderr
, "%s\n", NETSTAT_USAGE
);
575 (void) localtime_r(&now
, &tm
);
577 num_written
+= printf("%02d:%02d:%02d ", tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
579 return (num_written
);