]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/main.c
8ca3a725171db41237d9ca2caeec87012417fc98
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1988, 1993
26 * Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 char const copyright
[] =
59 "@(#) Copyright (c) 1983, 1988, 1993\n\
60 Regents of the University of California. All rights reserved.\n";
65 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 3/1/94";
67 static const char rcsid
[] =
68 "$Id: main.c,v 1.2 2000/06/16 03:37:29 lindak Exp $";
71 #include <sys/param.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
76 #include <netinet/in.h>
91 #include <sys/types.h>
92 #include <sys/sysctl.h>
96 * ----------------------------------------------------------------------------
97 * "THE BEER-WARE LICENSE" (Revision 42):
98 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
99 * can do whatever you want with this stuff. If we meet some day, and you think
100 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
101 * ----------------------------------------------------------------------------
103 * $Id: main.c,v 1.2 2000/06/16 03:37:29 lindak Exp $
108 static struct nlist nl
[] = {
133 #define N_ESISSTAT 12
141 #define N_CLTPSTAT 16
149 #define N_MFCTABLE 20
151 #define N_VIFTABLE 21
171 u_char pr_index
; /* index into nlist of cb head */
172 u_char pr_sindex
; /* index into nlist of stat block */
173 u_char pr_wanted
; /* 1 if wanted, 0 otherwise */
174 void (*pr_cblocks
)(); /* control blocks printing routine */
175 void (*pr_stats
)(); /* statistics printing routine */
176 char *pr_name
; /* well-known name */
177 int pr_usesysctl
; /* true if we use sysctl, not kvm */
179 { -1, -1, 1, protopr
,
180 tcp_stats
, "tcp", IPPROTO_TCP
},
181 { -1, -1, 1, protopr
,
182 udp_stats
, "udp", IPPROTO_UDP
},
183 { -1, -1, 1, protopr
,
184 NULL
, "divert", IPPROTO_DIVERT
},
185 { -1, -1, 1, protopr
,
186 ip_stats
, "ip", IPPROTO_RAW
},
187 { -1, -1, 1, protopr
,
188 icmp_stats
, "icmp", IPPROTO_ICMP
},
189 { -1, -1, 1, protopr
,
190 igmp_stats
, "igmp", IPPROTO_IGMP
},
196 struct protox atalkprotox
[] = {
197 { N_DDPCB
, N_DDPSTAT
, 1, atalkprotopr
,
205 struct protox ipxprotox
[] = {
206 { N_IPX
, N_IPXSTAT
, 1, ipxprotopr
,
207 ipx_stats
, "ipx", 0 },
208 { N_IPX
, N_SPXSTAT
, 1, ipxprotopr
,
209 spx_stats
, "spx", 0 },
216 struct protox nsprotox
[] = {
217 { N_IDP
, N_IDPSTAT
, 1, nsprotopr
,
219 { N_IDP
, N_SPPSTAT
, 1, nsprotopr
,
222 nserr_stats
, "ns_err" },
229 struct protox isoprotox
[] = {
230 { ISO_TP
, N_TPSTAT
, 1, iso_protopr
,
232 { N_CLTP
, N_CLTPSTAT
, 1, iso_protopr
,
233 cltp_stats
, "cltp" },
234 { -1, N_CLNPSTAT
, 1, 0,
236 { -1, N_ESISSTAT
, 1, 0,
243 struct protox
*protoprotox
[] = { protox
,
261 static void printproto
__P((struct protox
*, char *));
262 static void usage
__P((void));
263 static struct protox
*name2protox
__P((char *));
264 static struct protox
*knownname
__P((char *));
267 char *nlistf
= NULL
, *memf
= NULL
;
274 register struct protoent
*p
;
275 register struct protox
*tp
; /* for printing cblocks & stats */
280 while ((ch
= getopt(argc
, argv
, "Aabdf:ghI:iM:mN:np:rstuw:")) != -1)
296 if (strcmp(optarg
, "ns") == 0)
300 if (strcmp(optarg
, "ipx") == 0)
302 else if (strcmp(optarg
, "inet") == 0)
304 else if (strcmp(optarg
, "unix") == 0)
306 else if (strcmp(optarg
, "local") == 0)
308 else if (strcmp(optarg
, "atalk") == 0)
311 else if (strcmp(optarg
, "iso") == 0)
315 errx(1, "%s: unknown address family", optarg
);
325 for (cp
= interface
= optarg
; isalpha(*cp
); cp
++)
346 if ((tp
= name2protox(optarg
)) == NULL
) {
348 "%s: unknown or uninstrumented protocol",
366 interval
= atoi(optarg
);
376 #define BACKWARD_COMPATIBILITY
377 #ifdef BACKWARD_COMPATIBILITY
379 if (isdigit(**argv
)) {
380 interval
= atoi(*argv
);
395 * Discard setgid privileges if not the running kernel so that bad
396 * guys can't print interesting stuff from kernel memory.
398 if (nlistf
!= NULL
|| memf
!= NULL
)
403 mbpr(nl
[N_MBSTAT
].n_value
);
408 printf("%s: no stats routine\n", tp
->pr_name
);
411 if (tp
->pr_usesysctl
) {
412 (*tp
->pr_stats
)(tp
->pr_usesysctl
, tp
->pr_name
);
415 (*tp
->pr_stats
)(nl
[tp
->pr_sindex
].n_value
,
422 * Keep file descriptors open to avoid overhead
423 * of open/close on each call to get* routines.
429 * This does not make sense any more with DNS being default over
430 * the files. Doing a setXXXXent(1) causes a tcp connection to be
431 * used for the queries, which is slower.
436 intpr(interval
, nl
[N_IFNET
].n_value
);
442 rt_stats(nl
[N_RTSTAT
].n_value
);
444 routepr(nl
[N_RTREE
].n_value
);
450 mrt_stats(nl
[N_MRTSTAT
].n_value
);
452 mroutepr(nl
[N_MFCTABLE
].n_value
,
453 nl
[N_VIFTABLE
].n_value
);
456 if (af
== AF_INET
|| af
== AF_UNSPEC
) {
459 /* ugh, this is O(MN) ... why do we do this? */
460 while ((p
= getprotoent())) {
461 for (tp
= protox
; tp
->pr_name
; tp
++)
462 if (strcmp(tp
->pr_name
, p
->p_name
) == 0)
464 if (tp
->pr_name
== 0 || tp
->pr_wanted
== 0)
466 printproto(tp
, p
->p_name
);
472 if (af
== AF_IPX
|| af
== AF_UNSPEC
) {
474 for (tp
= ipxprotox
; tp
->pr_name
; tp
++)
475 printproto(tp
, tp
->pr_name
);
479 if (af
== AF_APPLETALK
|| af
== AF_UNSPEC
)
480 for (tp
= atalkprotox
; tp
->pr_name
; tp
++)
481 printproto(tp
, tp
->pr_name
);
484 if (af
== AF_NS
|| af
== AF_UNSPEC
)
485 for (tp
= nsprotox
; tp
->pr_name
; tp
++)
486 printproto(tp
, tp
->pr_name
);
489 if (af
== AF_ISO
|| af
== AF_UNSPEC
)
490 for (tp
= isoprotox
; tp
->pr_name
; tp
++)
491 printproto(tp
, tp
->pr_name
);
493 if ((af
== AF_UNIX
|| af
== AF_LOCAL
|| af
== AF_UNSPEC
) && !sflag
)
499 * Print out protocol statistics or control blocks (per sflag).
500 * If the interface was not specifically requested, and the symbol
501 * is not in the namelist, ignore this one.
505 register struct protox
*tp
;
513 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
514 : nl
[tp
->pr_sindex
].n_value
;
517 off
= tp
->pr_usesysctl
? tp
->pr_usesysctl
518 : nl
[tp
->pr_index
].n_value
;
520 if (pr
!= NULL
&& (off
|| af
!= AF_UNSPEC
))
525 * Read kernel memory, return 0 on success.
528 kread(addr
, buf
, size
)
537 kvmd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
539 if (kvm_nlist(kvmd
, nl
) < 0) {
541 errx(1, "%s: kvm_nlist: %s", nlistf
,
544 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd
));
547 if (nl
[0].n_type
== 0) {
549 errx(1, "%s: no namelist", nlistf
);
551 errx(1, "no namelist");
554 warnx("kvm not available");
560 if (kvm_read(kvmd
, addr
, buf
, size
) != size
) {
561 warnx("%s", kvm_geterr(kvmd
));
571 return (n
!= 1 ? "s" : "");
578 return (n
!= 1 ? "es" : "");
582 * Find the protox for the given "well-known" name.
584 static struct protox
*
588 struct protox
**tpp
, *tp
;
590 for (tpp
= protoprotox
; *tpp
; tpp
++)
591 for (tp
= *tpp
; tp
->pr_name
; tp
++)
592 if (strcmp(tp
->pr_name
, name
) == 0)
598 * Find the protox corresponding to name.
600 static struct protox
*
605 char **alias
; /* alias from p->aliases */
609 * Try to find the name in the list of "well-known" names. If that
610 * fails, check if name is an alias for an Internet protocol.
612 if ((tp
= knownname(name
)))
615 setprotoent(1); /* make protocol lookup cheaper */
616 while ((p
= getprotoent())) {
617 /* assert: name not same as p->name */
618 for (alias
= p
->p_aliases
; *alias
; alias
++)
619 if (strcmp(name
, *alias
) == 0) {
621 return (knownname(p
->p_name
));
631 (void)fprintf(stderr
, "%s\n%s\n%s\n%s\n",
632 "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]",
633 " netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]",
634 " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
635 " netstat [-M core] [-N system] [-p protocol]");
643 static char domain
[MAXHOSTNAMELEN
+ 1];
644 static int first
= 1;
649 if (gethostname(domain
, MAXHOSTNAMELEN
) == 0 &&
650 (s
= strchr(domain
, '.')))
651 (void) strcpy(domain
, s
+ 1);
657 while ((cp
= strchr(cp
, '.'))) {
658 if (!strcasecmp(cp
+ 1, domain
)) {
659 *cp
= 0; /* hit it */