]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
102c933f4062d2b6289d782333fa8102a8ff3926
[apple/network_cmds.git] / netstat.tproj / main.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
62 char 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
67 #include <sys/param.h>
68 #include <sys/file.h>
69 #include <sys/socket.h>
70
71 #include <netinet/in.h>
72 #include <net/pfkeyv2.h>
73
74 #include <ctype.h>
75 #include <err.h>
76 #include <errno.h>
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
89 #ifdef __APPLE__
90 #include <TargetConditionals.h>
91 #endif
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 *
101 * $Id: main.c,v 1.8 2004/10/14 22:24:09 lindak Exp $
102 *
103 */
104
105 static 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" },
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"},
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" },
186 { "" },
187 };
188
189
190 struct 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 */
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 */
201 } protox[] = {
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 },
214 #ifdef IPSEC
215 { -1, -1, 1, 0,
216 ipsec_stats, NULL, "ipsec", IPPROTO_ESP},
217 #endif
218 { -1, -1, 0, 0,
219 0, NULL, 0 }
220 };
221
222 #ifdef INET6
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 },
232 #ifdef IPSEC
233 { -1, N_IPSEC6STAT, 1, 0,
234 ipsec_stats, NULL, "ipsec6",IPPROTO_ESP },
235 #endif
236 #ifdef notyet
237 { -1, N_PIM6STAT, 1, 0,
238 pim6_stats, NULL, "pim6", 0 },
239 #endif
240 { -1, -1, 1, 0,
241 rip6_stats, NULL, "rip6", IPPROTO_RAW },
242 { -1, -1, 0, 0,
243 0, NULL, 0, 0 }
244 };
245 #endif /*INET6*/
246
247 #ifdef IPSEC
248 struct protox pfkeyprotox[] = {
249 { -1, N_PFKEYSTAT, 1, 0,
250 pfkey_stats, NULL, "pfkey", PF_KEY_V2 },
251 { -1, -1, 0, 0,
252 0, NULL, 0, 0 }
253 };
254 #endif
255
256 struct protox *protoprotox[] = {
257 protox,
258 #ifdef INET6
259 ip6protox,
260 #endif
261 #ifdef IPSEC
262 pfkeyprotox,
263 #endif
264 NULL };
265
266 static void printproto (struct protox *, char *);
267 static void usage (void);
268 static struct protox *name2protox (char *);
269 static struct protox *knownname (char *);
270 #ifdef SRVCACHE
271 extern void _serv_cache_close();
272 #endif
273
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 */
280 #endif
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 */
292
293 int interval; /* repeat interval for i/f stats */
294
295 char *interface; /* desired i/f for stats, or NULL for all i/fs */
296 int unit; /* unit number for above */
297
298 int af; /* address family */
299
300 int
301 main(argc, argv)
302 int argc;
303 char *argv[];
304 {
305 register struct protox *tp = NULL; /* for printing cblocks & stats */
306 int ch;
307
308 af = AF_UNSPEC;
309
310 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlmnPp:rRstuWw:")) != -1)
311 switch(ch) {
312 case 'A':
313 Aflag = 1;
314 break;
315 case 'a':
316 aflag = 1;
317 break;
318 case 'b':
319 bflag = 1;
320 break;
321 case 'd':
322 dflag = 1;
323 break;
324 case 'f':
325 if (strcmp(optarg, "ipx") == 0)
326 af = AF_IPX;
327 else if (strcmp(optarg, "inet") == 0)
328 af = AF_INET;
329 #ifdef INET6
330 else if (strcmp(optarg, "inet6") == 0)
331 af = AF_INET6;
332 #endif /*INET6*/
333 #ifdef INET6
334 else if (strcmp(optarg, "pfkey") == 0)
335 af = PF_KEY;
336 #endif /*INET6*/
337 else if (strcmp(optarg, "unix") == 0)
338 af = AF_UNIX;
339 else {
340 errx(1, "%s: unknown address family", optarg);
341 }
342 break;
343 #if defined(__APPLE__)
344 case 'g':
345 gflag = 1;
346 break;
347 #endif
348 case 'I': {
349 char *cp;
350
351 iflag = 1;
352 for (cp = interface = optarg; isalpha(*cp); cp++)
353 continue;
354 unit = atoi(cp);
355 break;
356 }
357 case 'i':
358 iflag = 1;
359 break;
360 case 'l':
361 lflag = 1;
362 break;
363 case 'L':
364 Lflag = 1;
365 break;
366 case 'm':
367 mflag++;
368 break;
369 case 'n':
370 nflag = 1;
371 break;
372 case 'P':
373 prioflag = 1;
374 break;
375 case 'p':
376 if ((tp = name2protox(optarg)) == NULL) {
377 errx(1,
378 "%s: unknown or uninstrumented protocol",
379 optarg);
380 }
381 pflag = 1;
382 break;
383 case 'r':
384 rflag = 1;
385 break;
386 case 's':
387 ++sflag;
388 break;
389 case 't':
390 tflag = 1;
391 break;
392 case 'u':
393 af = AF_UNIX;
394 break;
395 case 'W':
396 Wflag = 1;
397 break;
398 case 'w':
399 interval = atoi(optarg);
400 iflag = 1;
401 break;
402 case '?':
403 default:
404 usage();
405 }
406 argv += optind;
407 argc -= optind;
408
409 #define BACKWARD_COMPATIBILITY
410 #ifdef BACKWARD_COMPATIBILITY
411 if (*argv) {
412 if (isdigit(**argv)) {
413 interval = atoi(*argv);
414 if (interval <= 0)
415 usage();
416 ++argv;
417 iflag = 1;
418 }
419 }
420 #endif
421
422 if (mflag) {
423 mbpr();
424 exit(0);
425 }
426 if (iflag && !sflag) {
427 intpr(NULL);
428 exit(0);
429 }
430 if (rflag) {
431 if (sflag)
432 rt_stats();
433 else
434 routepr(nl[N_RTREE].n_value);
435 exit(0);
436 }
437 #if defined(__APPLE__)
438 if (gflag) {
439 #if !TARGET_OS_EMBEDDED
440 if (sflag) {
441 if (af == AF_INET || af == AF_UNSPEC)
442 mrt_stats();
443 #ifdef INET6
444 if (af == AF_INET6 || af == AF_UNSPEC)
445 mrt6_stats();
446 #endif
447 } else {
448 if (af == AF_INET || af == AF_UNSPEC)
449 mroutepr();
450 #ifdef INET6
451 if (af == AF_INET6 || af == AF_UNSPEC)
452 mroute6pr();
453 #endif
454 }
455 #endif /* !TARGET_OS_EMBEDDED */
456 ifmalist_dump();
457 exit(0);
458 }
459 #endif
460
461 if (tp) {
462 printproto(tp, tp->pr_name);
463 exit(0);
464 }
465 if (af == AF_INET || af == AF_UNSPEC)
466 for (tp = protox; tp->pr_name; tp++)
467 printproto(tp, tp->pr_name);
468 #ifdef INET6
469 if (af == AF_INET6 || af == AF_UNSPEC)
470 for (tp = ip6protox; tp->pr_name; tp++)
471 printproto(tp, tp->pr_name);
472 #endif /*INET6*/
473 #ifdef IPSEC
474 if (af == PF_KEY || af == AF_UNSPEC)
475 for (tp = pfkeyprotox; tp->pr_name; tp++)
476 printproto(tp, tp->pr_name);
477 #endif /*IPSEC*/
478 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
479 unixpr();
480 #ifdef SRVCACHE
481 _serv_cache_close();
482 #endif
483 exit(0);
484 }
485
486 /*
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.
490 */
491 static void
492 printproto(tp, name)
493 register struct protox *tp;
494 char *name;
495 {
496 void (*pr)(uint32_t, char *, int);
497 uint32_t off;
498
499 if (sflag) {
500 if (iflag && !pflag) {
501 if (tp->pr_istats)
502 intpr(tp->pr_istats);
503 else
504 printf("%s: no per-interface stats routine\n",
505 tp->pr_name);
506 return;
507 }
508 else {
509 pr = tp->pr_stats;
510 if (!pr) {
511 if (pflag)
512 printf("%s: no stats routine\n",
513 tp->pr_name);
514 return;
515 }
516 off = tp->pr_usesysctl ? tp->pr_usesysctl
517 : nl[tp->pr_sindex].n_value;
518 }
519 } else {
520 pr = tp->pr_cblocks;
521 if (!pr) {
522 if (pflag)
523 printf("%s: no PCB routine\n", tp->pr_name);
524 return;
525 }
526 off = tp->pr_usesysctl ? tp->pr_usesysctl
527 : nl[tp->pr_index].n_value;
528 }
529 if (pr != NULL && (off || af != AF_UNSPEC)) {
530 if (sflag && iflag && pflag)
531 intervalpr(pr, off, name, af);
532 else
533 (*pr)(off, name, af);
534 } else {
535 printf("### no stats for %s\n", name);
536 }
537 }
538
539 char *
540 plural(int n)
541 {
542 return (n != 1 ? "s" : "");
543 }
544
545 char *
546 plurales(int n)
547 {
548 return (n != 1 ? "es" : "");
549 }
550
551 /*
552 * Find the protox for the given "well-known" name.
553 */
554 static struct protox *
555 knownname(char *name)
556 {
557 struct protox **tpp, *tp;
558
559 for (tpp = protoprotox; *tpp; tpp++)
560 for (tp = *tpp; tp->pr_name; tp++)
561 if (strcmp(tp->pr_name, name) == 0)
562 return (tp);
563 return (NULL);
564 }
565
566 /*
567 * Find the protox corresponding to name.
568 */
569 static struct protox *
570 name2protox(char *name)
571 {
572 struct protox *tp;
573 char **alias; /* alias from p->aliases */
574 struct protoent *p;
575
576 /*
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.
579 */
580 if ((tp = knownname(name)) != NULL)
581 return (tp);
582
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) {
588 endprotoent();
589 return (knownname(p->p_name));
590 }
591 }
592 endprotoent();
593 return (NULL);
594 }
595
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\
602 netstat -m [-m]\n\
603 netstat -r [-Aaln] [-f address_family]\n\
604 netstat -rs [-s]\n\
605 "
606
607 static void
608 usage(void)
609 {
610 (void) fprintf(stderr, "%s\n", NETSTAT_USAGE);
611 exit(1);
612 }