]> git.saurik.com Git - apple/network_cmds.git/blame - netstat.tproj/main.c
network_cmds-356.9.tar.gz
[apple/network_cmds.git] / netstat.tproj / main.c
CommitLineData
b7080c8e 1/*
fdfd5971 2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
b7080c8e 3 *
9c859447 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
b7080c8e 5 *
9c859447
A
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.
b7080c8e 14 *
9c859447
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
b7080c8e
A
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,
9c859447
A
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.
b7080c8e 25 *
9c859447 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
b7080c8e
A
27 */
28/*
29 * Copyright (c) 1983, 1988, 1993
30 * Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
61#ifndef lint
62char const copyright[] =
63"@(#) Copyright (c) 1983, 1988, 1993\n\
64 Regents of the University of California. All rights reserved.\n";
65#endif /* not lint */
66
b7080c8e
A
67#include <sys/param.h>
68#include <sys/file.h>
b7080c8e
A
69#include <sys/socket.h>
70
71#include <netinet/in.h>
2b484d24 72#include <net/pfkeyv2.h>
b7080c8e
A
73
74#include <ctype.h>
75#include <err.h>
76#include <errno.h>
b7080c8e
A
77#include <limits.h>
78#include <netdb.h>
79#include <nlist.h>
80#include <paths.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <unistd.h>
85#include "netstat.h"
86#include <sys/types.h>
87#include <sys/sysctl.h>
88
b8dff150
A
89#ifdef __APPLE__
90#include <TargetConditionals.h>
91#endif
b7080c8e
A
92
93/*
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 * ----------------------------------------------------------------------------
100 *
2b484d24 101 * $Id: main.c,v 1.8 2004/10/14 22:24:09 lindak Exp $
b7080c8e
A
102 *
103 */
104
b7080c8e
A
105static struct nlist nl[] = {
106#define N_IFNET 0
107 { "_ifnet" },
108#define N_IMP 1
109 { "_imp_softc" },
110#define N_RTSTAT 2
111 { "_rtstat" },
112#define N_UNIXSW 3
113 { "_localsw" },
114#define N_IDP 4
115 { "_nspcb"},
116#define N_IDPSTAT 5
117 { "_idpstat"},
118#define N_SPPSTAT 6
119 { "_spp_istat"},
120#define N_NSERR 7
121 { "_ns_errstat"},
122#define N_CLNPSTAT 8
123 { "_clnp_stat"},
124#define IN_NOTUSED 9
125 { "_tp_inpcb" },
126#define ISO_TP 10
127 { "_tp_refinfo" },
128#define N_TPSTAT 11
129 { "_tp_stat" },
130#define N_ESISSTAT 12
131 { "_esis_stat"},
132#define N_NIMP 13
133 { "_nimp"},
134#define N_RTREE 14
135 { "_rt_tables"},
136#define N_CLTP 15
137 { "_cltb"},
138#define N_CLTPSTAT 16
139 { "_cltpstat"},
140#define N_NFILE 17
141 { "_nfile" },
142#define N_FILE 18
143 { "_file" },
b7080c8e
A
144#define N_IPX 22
145 { "_ipxpcb"},
146#define N_IPXSTAT 23
147 { "_ipxstat"},
148#define N_SPXSTAT 24
149 { "_spx_istat"},
150#define N_DDPSTAT 25
151 { "_ddpstat"},
152#define N_DDPCB 26
153 { "_ddpcb"},
7ba0088d
A
154#define N_NGSOCKS 27
155 { "_ngsocklist"},
156#define N_IP6STAT 28
157 { "_ip6stat" },
158#define N_ICMP6STAT 29
159 { "_icmp6stat" },
160#define N_IPSECSTAT 30
161 { "_ipsecstat" },
162#define N_IPSEC6STAT 31
163 { "_ipsec6stat" },
164#define N_PIM6STAT 32
165 { "_pim6stat" },
166#define N_MRT6PROTO 33
167 { "_ip6_mrtproto" },
168#define N_MRT6STAT 34
169 { "_mrt6stat" },
170#define N_MF6CTABLE 35
171 { "_mf6ctable" },
172#define N_MIF6TABLE 36
173 { "_mif6table" },
174#define N_PFKEYSTAT 37
175 { "_pfkeystat" },
176#define N_MBSTAT 38
177 { "_mbstat" },
178#define N_MBTYPES 39
179 { "_mbtypes" },
180#define N_NMBCLUSTERS 40
181 { "_nmbclusters" },
182#define N_NMBUFS 41
183 { "_nmbufs" },
184#define N_RTTRASH 42
185 { "_rttrash" },
b7080c8e
A
186 { "" },
187};
188
189
b7080c8e
A
190struct protox {
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 */
9c859447 194 void (*pr_cblocks)(uint32_t, char *, int);
7ba0088d 195 /* control blocks printing routine */
9c859447 196 void (*pr_stats)(uint32_t, char *, int);
7ba0088d
A
197 /* statistics printing routine */
198 void (*pr_istats)(char *); /* per/if statistics printing routine */
b7080c8e
A
199 char *pr_name; /* well-known name */
200 int pr_usesysctl; /* true if we use sysctl, not kvm */
201} protox[] = {
202 { -1, -1, 1, protopr,
7ba0088d 203 tcp_stats, NULL, "tcp", IPPROTO_TCP },
b7080c8e 204 { -1, -1, 1, protopr,
7ba0088d 205 udp_stats, NULL, "udp", IPPROTO_UDP },
b7080c8e 206 { -1, -1, 1, protopr,
7ba0088d 207 NULL, NULL, "divert",IPPROTO_DIVERT },
b7080c8e 208 { -1, -1, 1, protopr,
7ba0088d 209 ip_stats, NULL, "ip", IPPROTO_RAW },
b7080c8e 210 { -1, -1, 1, protopr,
7ba0088d 211 icmp_stats, NULL, "icmp", IPPROTO_ICMP },
b7080c8e 212 { -1, -1, 1, protopr,
7ba0088d
A
213 igmp_stats, NULL, "igmp", IPPROTO_IGMP },
214#ifdef IPSEC
2b484d24
A
215 { -1, -1, 1, 0,
216 ipsec_stats, NULL, "ipsec", IPPROTO_ESP},
7ba0088d
A
217#endif
218 { -1, -1, 0, 0,
219 0, NULL, 0 }
220};
221
222#ifdef INET6
223struct 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 },
232#ifdef IPSEC
233 { -1, N_IPSEC6STAT, 1, 0,
2b484d24 234 ipsec_stats, NULL, "ipsec6",IPPROTO_ESP },
7ba0088d
A
235#endif
236#ifdef notyet
237 { -1, N_PIM6STAT, 1, 0,
238 pim6_stats, NULL, "pim6", 0 },
239#endif
240 { -1, -1, 1, 0,
2b484d24 241 rip6_stats, NULL, "rip6", IPPROTO_RAW },
b7080c8e 242 { -1, -1, 0, 0,
7ba0088d 243 0, NULL, 0, 0 }
b7080c8e 244};
7ba0088d 245#endif /*INET6*/
b7080c8e 246
7ba0088d
A
247#ifdef IPSEC
248struct protox pfkeyprotox[] = {
249 { -1, N_PFKEYSTAT, 1, 0,
2b484d24 250 pfkey_stats, NULL, "pfkey", PF_KEY_V2 },
7ba0088d
A
251 { -1, -1, 0, 0,
252 0, NULL, 0, 0 }
253};
254#endif
b7080c8e 255
7ba0088d
A
256struct protox *protoprotox[] = {
257 protox,
258#ifdef INET6
259 ip6protox,
b7080c8e 260#endif
7ba0088d
A
261#ifdef IPSEC
262 pfkeyprotox,
b7080c8e
A
263#endif
264 NULL };
265
7ba0088d
A
266static void printproto (struct protox *, char *);
267static void usage (void);
268static struct protox *name2protox (char *);
269static struct protox *knownname (char *);
ac2f15b3
A
270#ifdef SRVCACHE
271extern void _serv_cache_close();
272#endif
b7080c8e 273
7ba0088d
A
274int Aflag; /* show addresses of protocol control block */
275int aflag; /* show all sockets (including servers) */
276int bflag; /* show i/f total bytes in/out */
277int dflag; /* show i/f dropped packets */
7f5b2e89 278#if defined(__APPLE__)
7ba0088d 279int gflag; /* show group (multicast) routing or stats */
b8dff150 280#endif
7ba0088d
A
281int iflag; /* show interfaces */
282int lflag; /* show routing table with use and ref */
283int Lflag; /* show size of listen queues */
284int mflag; /* show memory stats */
285int nflag; /* show addresses numerically */
286static int pflag; /* show given protocol */
fdfd5971
A
287int prioflag = -1; /* show packet priority statistics */
288int Rflag; /* show reachability information */
7ba0088d
A
289int rflag; /* show routing tables (or routing stats) */
290int sflag; /* show protocol statistics */
291int tflag; /* show i/f watchdog timers */
fdfd5971 292int vflag; /* more verbose */
7ba0088d
A
293int Wflag; /* wide display */
294
295int interval; /* repeat interval for i/f stats */
296
297char *interface; /* desired i/f for stats, or NULL for all i/fs */
298int unit; /* unit number for above */
299
300int af; /* address family */
b7080c8e
A
301
302int
303main(argc, argv)
304 int argc;
305 char *argv[];
306{
7ba0088d 307 register struct protox *tp = NULL; /* for printing cblocks & stats */
b7080c8e
A
308 int ch;
309
310 af = AF_UNSPEC;
311
fdfd5971 312 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlmnP:p:rRstuvWw:")) != -1)
b7080c8e
A
313 switch(ch) {
314 case 'A':
315 Aflag = 1;
316 break;
317 case 'a':
318 aflag = 1;
319 break;
320 case 'b':
321 bflag = 1;
322 break;
323 case 'd':
324 dflag = 1;
325 break;
326 case 'f':
b7080c8e
A
327 if (strcmp(optarg, "ipx") == 0)
328 af = AF_IPX;
329 else if (strcmp(optarg, "inet") == 0)
330 af = AF_INET;
7ba0088d
A
331#ifdef INET6
332 else if (strcmp(optarg, "inet6") == 0)
333 af = AF_INET6;
334#endif /*INET6*/
335#ifdef INET6
336 else if (strcmp(optarg, "pfkey") == 0)
337 af = PF_KEY;
338#endif /*INET6*/
b7080c8e
A
339 else if (strcmp(optarg, "unix") == 0)
340 af = AF_UNIX;
b7080c8e
A
341 else {
342 errx(1, "%s: unknown address family", optarg);
343 }
344 break;
7f5b2e89 345#if defined(__APPLE__)
b7080c8e
A
346 case 'g':
347 gflag = 1;
348 break;
b8dff150 349#endif
b7080c8e
A
350 case 'I': {
351 char *cp;
352
353 iflag = 1;
354 for (cp = interface = optarg; isalpha(*cp); cp++)
355 continue;
356 unit = atoi(cp);
357 break;
358 }
359 case 'i':
360 iflag = 1;
361 break;
7ba0088d
A
362 case 'l':
363 lflag = 1;
364 break;
365 case 'L':
366 Lflag = 1;
367 break;
b7080c8e 368 case 'm':
b8dff150 369 mflag++;
b7080c8e
A
370 break;
371 case 'n':
372 nflag = 1;
373 break;
7f5b2e89 374 case 'P':
5902acfc 375 prioflag = atoi(optarg);
7f5b2e89 376 break;
b7080c8e
A
377 case 'p':
378 if ((tp = name2protox(optarg)) == NULL) {
379 errx(1,
380 "%s: unknown or uninstrumented protocol",
381 optarg);
382 }
383 pflag = 1;
384 break;
fdfd5971
A
385 case 'R':
386 Rflag = 1;
387 break;
b7080c8e
A
388 case 'r':
389 rflag = 1;
390 break;
391 case 's':
392 ++sflag;
393 break;
394 case 't':
395 tflag = 1;
396 break;
397 case 'u':
398 af = AF_UNIX;
399 break;
fdfd5971
A
400 case 'v':
401 vflag++;
402 break;
7ba0088d
A
403 case 'W':
404 Wflag = 1;
405 break;
b7080c8e
A
406 case 'w':
407 interval = atoi(optarg);
408 iflag = 1;
409 break;
410 case '?':
411 default:
412 usage();
413 }
414 argv += optind;
415 argc -= optind;
416
417#define BACKWARD_COMPATIBILITY
418#ifdef BACKWARD_COMPATIBILITY
419 if (*argv) {
420 if (isdigit(**argv)) {
421 interval = atoi(*argv);
422 if (interval <= 0)
423 usage();
424 ++argv;
425 iflag = 1;
426 }
b7080c8e
A
427 }
428#endif
429
b7080c8e 430 if (mflag) {
2b484d24 431 mbpr();
b7080c8e
A
432 exit(0);
433 }
fdfd5971
A
434 if (iflag && !sflag && !gflag) {
435 if (Rflag)
436 intpr_ri(NULL);
437 else
438 intpr(NULL);
b7080c8e
A
439 exit(0);
440 }
441 if (rflag) {
b7080c8e 442 if (sflag)
2b484d24 443 rt_stats();
b7080c8e
A
444 else
445 routepr(nl[N_RTREE].n_value);
446 exit(0);
447 }
7f5b2e89 448#if defined(__APPLE__)
b7080c8e 449 if (gflag) {
7f5b2e89 450#if !TARGET_OS_EMBEDDED
7ba0088d
A
451 if (sflag) {
452 if (af == AF_INET || af == AF_UNSPEC)
2b484d24 453 mrt_stats();
7ba0088d
A
454#ifdef INET6
455 if (af == AF_INET6 || af == AF_UNSPEC)
2b484d24 456 mrt6_stats();
7ba0088d
A
457#endif
458 } else {
459 if (af == AF_INET || af == AF_UNSPEC)
2b484d24 460 mroutepr();
7ba0088d
A
461#ifdef INET6
462 if (af == AF_INET6 || af == AF_UNSPEC)
2b484d24 463 mroute6pr();
7ba0088d 464#endif
b7080c8e 465 }
7f5b2e89 466#endif /* !TARGET_OS_EMBEDDED */
b8dff150 467 ifmalist_dump();
7ba0088d 468 exit(0);
b7080c8e 469 }
b8dff150 470#endif
b7080c8e 471
7ba0088d
A
472 if (tp) {
473 printproto(tp, tp->pr_name);
474 exit(0);
475 }
476 if (af == AF_INET || af == AF_UNSPEC)
477 for (tp = protox; tp->pr_name; tp++)
478 printproto(tp, tp->pr_name);
479#ifdef INET6
480 if (af == AF_INET6 || af == AF_UNSPEC)
481 for (tp = ip6protox; tp->pr_name; tp++)
482 printproto(tp, tp->pr_name);
483#endif /*INET6*/
484#ifdef IPSEC
485 if (af == PF_KEY || af == AF_UNSPEC)
486 for (tp = pfkeyprotox; tp->pr_name; tp++)
487 printproto(tp, tp->pr_name);
488#endif /*IPSEC*/
7ba0088d 489 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
b7080c8e 490 unixpr();
ac2f15b3
A
491#ifdef SRVCACHE
492 _serv_cache_close();
493#endif
b7080c8e
A
494 exit(0);
495}
496
497/*
498 * Print out protocol statistics or control blocks (per sflag).
499 * If the interface was not specifically requested, and the symbol
500 * is not in the namelist, ignore this one.
501 */
502static void
503printproto(tp, name)
504 register struct protox *tp;
505 char *name;
506{
9c859447
A
507 void (*pr)(uint32_t, char *, int);
508 uint32_t off;
b7080c8e
A
509
510 if (sflag) {
b8dff150 511 if (iflag && !pflag) {
7ba0088d 512 if (tp->pr_istats)
2b484d24 513 intpr(tp->pr_istats);
b8dff150 514 else
7ba0088d
A
515 printf("%s: no per-interface stats routine\n",
516 tp->pr_name);
517 return;
518 }
519 else {
520 pr = tp->pr_stats;
521 if (!pr) {
522 if (pflag)
523 printf("%s: no stats routine\n",
524 tp->pr_name);
525 return;
526 }
527 off = tp->pr_usesysctl ? tp->pr_usesysctl
528 : nl[tp->pr_sindex].n_value;
529 }
b7080c8e
A
530 } else {
531 pr = tp->pr_cblocks;
7ba0088d
A
532 if (!pr) {
533 if (pflag)
534 printf("%s: no PCB routine\n", tp->pr_name);
535 return;
536 }
b7080c8e
A
537 off = tp->pr_usesysctl ? tp->pr_usesysctl
538 : nl[tp->pr_index].n_value;
539 }
b8dff150
A
540 if (pr != NULL && (off || af != AF_UNSPEC)) {
541 if (sflag && iflag && pflag)
542 intervalpr(pr, off, name, af);
543 else
544 (*pr)(off, name, af);
545 } else {
2b484d24 546 printf("### no stats for %s\n", name);
b7080c8e 547 }
b7080c8e
A
548}
549
550char *
7ba0088d 551plural(int n)
b7080c8e
A
552{
553 return (n != 1 ? "s" : "");
554}
555
556char *
7ba0088d 557plurales(int n)
b7080c8e
A
558{
559 return (n != 1 ? "es" : "");
560}
561
562/*
563 * Find the protox for the given "well-known" name.
564 */
565static struct protox *
7ba0088d 566knownname(char *name)
b7080c8e
A
567{
568 struct protox **tpp, *tp;
569
570 for (tpp = protoprotox; *tpp; tpp++)
571 for (tp = *tpp; tp->pr_name; tp++)
572 if (strcmp(tp->pr_name, name) == 0)
573 return (tp);
574 return (NULL);
575}
576
577/*
578 * Find the protox corresponding to name.
579 */
580static struct protox *
7ba0088d 581name2protox(char *name)
b7080c8e
A
582{
583 struct protox *tp;
584 char **alias; /* alias from p->aliases */
585 struct protoent *p;
586
587 /*
588 * Try to find the name in the list of "well-known" names. If that
589 * fails, check if name is an alias for an Internet protocol.
590 */
7ba0088d 591 if ((tp = knownname(name)) != NULL)
b7080c8e
A
592 return (tp);
593
594 setprotoent(1); /* make protocol lookup cheaper */
7ba0088d 595 while ((p = getprotoent()) != NULL) {
b7080c8e
A
596 /* assert: name not same as p->name */
597 for (alias = p->p_aliases; *alias; alias++)
598 if (strcmp(name, *alias) == 0) {
599 endprotoent();
600 return (knownname(p->p_name));
601 }
602 }
603 endprotoent();
604 return (NULL);
605}
606
b8dff150
A
607#define NETSTAT_USAGE "\
608Usage: netstat [-AaLlnW] [-f address_family | -p protocol]\n\
609 netstat [-gilns] [-f address_family]\n\
fdfd5971 610 netstat -i | -I interface [-w wait] [-abdgRt]\n\
b8dff150
A
611 netstat -s [-s] [-f address_family | -p protocol] [-w wait]\n\
612 netstat -i | -I interface -s [-f address_family | -p protocol]\n\
613 netstat -m [-m]\n\
614 netstat -r [-Aaln] [-f address_family]\n\
615 netstat -rs [-s]\n\
616"
617
b7080c8e 618static void
7ba0088d 619usage(void)
b7080c8e 620{
b8dff150 621 (void) fprintf(stderr, "%s\n", NETSTAT_USAGE);
b7080c8e
A
622 exit(1);
623}