19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 39b50d902SRodney W. Grimes * Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 355d422d6aSPhilippe Charnier char const copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\ 379b50d902SRodney W. Grimes Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 415d422d6aSPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 3/1/94"; 435d422d6aSPhilippe Charnier #endif 445d422d6aSPhilippe Charnier static const char rcsid[] = 45c6620a13SPoul-Henning Kamp "$Id: main.c,v 1.21 1998/08/05 13:54:07 phk Exp $"; 469b50d902SRodney W. Grimes #endif /* not lint */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <sys/param.h> 499b50d902SRodney W. Grimes #include <sys/file.h> 509b50d902SRodney W. Grimes #include <sys/protosw.h> 519b50d902SRodney W. Grimes #include <sys/socket.h> 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes #include <netinet/in.h> 549b50d902SRodney W. Grimes 559b50d902SRodney W. Grimes #include <ctype.h> 565d422d6aSPhilippe Charnier #include <err.h> 579b50d902SRodney W. Grimes #include <errno.h> 589b50d902SRodney W. Grimes #include <kvm.h> 599b50d902SRodney W. Grimes #include <limits.h> 609b50d902SRodney W. Grimes #include <netdb.h> 619b50d902SRodney W. Grimes #include <nlist.h> 629b50d902SRodney W. Grimes #include <paths.h> 639b50d902SRodney W. Grimes #include <stdio.h> 649b50d902SRodney W. Grimes #include <stdlib.h> 659b50d902SRodney W. Grimes #include <string.h> 669b50d902SRodney W. Grimes #include <unistd.h> 679b50d902SRodney W. Grimes #include "netstat.h" 689b50d902SRodney W. Grimes 69c6620a13SPoul-Henning Kamp static struct nlist nl[] = { 704f81ef50SGarrett Wollman #define N_IFNET 0 719b50d902SRodney W. Grimes { "_ifnet" }, 724f81ef50SGarrett Wollman #define N_IMP 1 739b50d902SRodney W. Grimes { "_imp_softc" }, 744f81ef50SGarrett Wollman #define N_RTSTAT 2 759b50d902SRodney W. Grimes { "_rtstat" }, 764f81ef50SGarrett Wollman #define N_UNIXSW 3 77f3117d66SPeter Wemm { "_localsw" }, 784f81ef50SGarrett Wollman #define N_IDP 4 799b50d902SRodney W. Grimes { "_nspcb"}, 804f81ef50SGarrett Wollman #define N_IDPSTAT 5 819b50d902SRodney W. Grimes { "_idpstat"}, 824f81ef50SGarrett Wollman #define N_SPPSTAT 6 839b50d902SRodney W. Grimes { "_spp_istat"}, 844f81ef50SGarrett Wollman #define N_NSERR 7 859b50d902SRodney W. Grimes { "_ns_errstat"}, 864f81ef50SGarrett Wollman #define N_CLNPSTAT 8 879b50d902SRodney W. Grimes { "_clnp_stat"}, 884f81ef50SGarrett Wollman #define IN_NOTUSED 9 899b50d902SRodney W. Grimes { "_tp_inpcb" }, 904f81ef50SGarrett Wollman #define ISO_TP 10 919b50d902SRodney W. Grimes { "_tp_refinfo" }, 924f81ef50SGarrett Wollman #define N_TPSTAT 11 939b50d902SRodney W. Grimes { "_tp_stat" }, 944f81ef50SGarrett Wollman #define N_ESISSTAT 12 959b50d902SRodney W. Grimes { "_esis_stat"}, 964f81ef50SGarrett Wollman #define N_NIMP 13 979b50d902SRodney W. Grimes { "_nimp"}, 984f81ef50SGarrett Wollman #define N_RTREE 14 999b50d902SRodney W. Grimes { "_rt_tables"}, 1004f81ef50SGarrett Wollman #define N_CLTP 15 1019b50d902SRodney W. Grimes { "_cltb"}, 1024f81ef50SGarrett Wollman #define N_CLTPSTAT 16 1039b50d902SRodney W. Grimes { "_cltpstat"}, 1044f81ef50SGarrett Wollman #define N_NFILE 17 1059b50d902SRodney W. Grimes { "_nfile" }, 1064f81ef50SGarrett Wollman #define N_FILE 18 1079b50d902SRodney W. Grimes { "_file" }, 1084f81ef50SGarrett Wollman #define N_MRTPROTO 19 1099b50d902SRodney W. Grimes { "_ip_mrtproto" }, 1104f81ef50SGarrett Wollman #define N_MRTSTAT 20 1119b50d902SRodney W. Grimes { "_mrtstat" }, 1124f81ef50SGarrett Wollman #define N_MFCTABLE 21 113ef105c25SGarrett Wollman { "_mfctable" }, 1144f81ef50SGarrett Wollman #define N_VIFTABLE 22 1159b50d902SRodney W. Grimes { "_viftable" }, 1164f81ef50SGarrett Wollman #define N_IPX 23 117cc6a66f2SJulian Elischer { "_ipxpcb"}, 1184f81ef50SGarrett Wollman #define N_IPXSTAT 24 119cc6a66f2SJulian Elischer { "_ipxstat"}, 1204f81ef50SGarrett Wollman #define N_SPXSTAT 25 121cc6a66f2SJulian Elischer { "_spx_istat"}, 1224f81ef50SGarrett Wollman #define N_DDPSTAT 26 12363bf4575SJulian Elischer { "_ddpstat"}, 1244f81ef50SGarrett Wollman #define N_DDPCB 27 12563bf4575SJulian Elischer { "_ddpcb"}, 1267d56c0eeSAlexander Langer { "" }, 1279b50d902SRodney W. Grimes }; 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes struct protox { 1309b50d902SRodney W. Grimes u_char pr_index; /* index into nlist of cb head */ 1319b50d902SRodney W. Grimes u_char pr_sindex; /* index into nlist of stat block */ 1329b50d902SRodney W. Grimes u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 1339b50d902SRodney W. Grimes void (*pr_cblocks)(); /* control blocks printing routine */ 1349b50d902SRodney W. Grimes void (*pr_stats)(); /* statistics printing routine */ 1359b50d902SRodney W. Grimes char *pr_name; /* well-known name */ 1364f81ef50SGarrett Wollman int pr_usesysctl; /* true if we use sysctl, not kvm */ 1379b50d902SRodney W. Grimes } protox[] = { 1384f81ef50SGarrett Wollman { -1, -1, 1, protopr, 1394f81ef50SGarrett Wollman tcp_stats, "tcp", IPPROTO_TCP }, 1404f81ef50SGarrett Wollman { -1, -1, 1, protopr, 1414f81ef50SGarrett Wollman udp_stats, "udp", IPPROTO_UDP }, 1424f81ef50SGarrett Wollman { -1, -1, 1, protopr, 1434f81ef50SGarrett Wollman NULL, "divert", IPPROTO_DIVERT }, 1444f81ef50SGarrett Wollman { -1, -1, 1, protopr, 1454f81ef50SGarrett Wollman ip_stats, "ip", IPPROTO_RAW }, 1464f81ef50SGarrett Wollman { -1, -1, 1, protopr, 1474f81ef50SGarrett Wollman icmp_stats, "icmp", IPPROTO_ICMP }, 1484f81ef50SGarrett Wollman { -1, -1, 1, protopr, 1494f81ef50SGarrett Wollman igmp_stats, "igmp", IPPROTO_IGMP }, 1509b50d902SRodney W. Grimes { -1, -1, 0, 0, 1519b50d902SRodney W. Grimes 0, 0 } 1529b50d902SRodney W. Grimes }; 1539b50d902SRodney W. Grimes 15463bf4575SJulian Elischer struct protox atalkprotox[] = { 15563bf4575SJulian Elischer { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 15663bf4575SJulian Elischer ddp_stats, "ddp" }, 15763bf4575SJulian Elischer { -1, -1, 0, 0, 15863bf4575SJulian Elischer 0, 0 } 15963bf4575SJulian Elischer }; 16063bf4575SJulian Elischer 161cc6a66f2SJulian Elischer struct protox ipxprotox[] = { 162cc6a66f2SJulian Elischer { N_IPX, N_IPXSTAT, 1, ipxprotopr, 163cc6a66f2SJulian Elischer ipx_stats, "ipx" }, 164cc6a66f2SJulian Elischer { N_IPX, N_SPXSTAT, 1, ipxprotopr, 165cc6a66f2SJulian Elischer spx_stats, "spx" }, 166cc6a66f2SJulian Elischer { -1, -1, 0, 0, 167cc6a66f2SJulian Elischer 0, 0 } 168cc6a66f2SJulian Elischer }; 169cc6a66f2SJulian Elischer 170cbc17e71SGarrett Wollman #ifdef NS 1719b50d902SRodney W. Grimes struct protox nsprotox[] = { 1729b50d902SRodney W. Grimes { N_IDP, N_IDPSTAT, 1, nsprotopr, 1739b50d902SRodney W. Grimes idp_stats, "idp" }, 1749b50d902SRodney W. Grimes { N_IDP, N_SPPSTAT, 1, nsprotopr, 1759b50d902SRodney W. Grimes spp_stats, "spp" }, 1769b50d902SRodney W. Grimes { -1, N_NSERR, 1, 0, 1779b50d902SRodney W. Grimes nserr_stats, "ns_err" }, 1789b50d902SRodney W. Grimes { -1, -1, 0, 0, 1799b50d902SRodney W. Grimes 0, 0 } 1809b50d902SRodney W. Grimes }; 181cbc17e71SGarrett Wollman #endif 1829b50d902SRodney W. Grimes 1830761cb29SGarrett Wollman #ifdef ISO 1849b50d902SRodney W. Grimes struct protox isoprotox[] = { 1859b50d902SRodney W. Grimes { ISO_TP, N_TPSTAT, 1, iso_protopr, 1869b50d902SRodney W. Grimes tp_stats, "tp" }, 1879b50d902SRodney W. Grimes { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 1889b50d902SRodney W. Grimes cltp_stats, "cltp" }, 1899b50d902SRodney W. Grimes { -1, N_CLNPSTAT, 1, 0, 1909b50d902SRodney W. Grimes clnp_stats, "clnp"}, 1919b50d902SRodney W. Grimes { -1, N_ESISSTAT, 1, 0, 1929b50d902SRodney W. Grimes esis_stats, "esis"}, 1939b50d902SRodney W. Grimes { -1, -1, 0, 0, 1949b50d902SRodney W. Grimes 0, 0 } 1959b50d902SRodney W. Grimes }; 1960761cb29SGarrett Wollman #endif 1979b50d902SRodney W. Grimes 19863bf4575SJulian Elischer struct protox *protoprotox[] = { protox, ipxprotox, atalkprotox, 199cbc17e71SGarrett Wollman #ifdef NS 200cbc17e71SGarrett Wollman nsprotox, 201cbc17e71SGarrett Wollman #endif 2020761cb29SGarrett Wollman #ifdef ISO 2030761cb29SGarrett Wollman isoprotox, 2040761cb29SGarrett Wollman #endif 2050761cb29SGarrett Wollman NULL }; 2069b50d902SRodney W. Grimes 2079b50d902SRodney W. Grimes static void printproto __P((struct protox *, char *)); 2089b50d902SRodney W. Grimes static void usage __P((void)); 2099b50d902SRodney W. Grimes static struct protox *name2protox __P((char *)); 2109b50d902SRodney W. Grimes static struct protox *knownname __P((char *)); 2119b50d902SRodney W. Grimes 212c6620a13SPoul-Henning Kamp static kvm_t *kvmd; 21399453c6aSPoul-Henning Kamp char *nlistf = NULL, *memf = NULL; 2149b50d902SRodney W. Grimes 2159b50d902SRodney W. Grimes int 2169b50d902SRodney W. Grimes main(argc, argv) 2179b50d902SRodney W. Grimes int argc; 2189b50d902SRodney W. Grimes char *argv[]; 2199b50d902SRodney W. Grimes { 2209b50d902SRodney W. Grimes register struct protoent *p; 2219b50d902SRodney W. Grimes register struct protox *tp; /* for printing cblocks & stats */ 2229b50d902SRodney W. Grimes int ch; 2239b50d902SRodney W. Grimes 2249b50d902SRodney W. Grimes af = AF_UNSPEC; 2259b50d902SRodney W. Grimes 2261c8af878SWarner Losh while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != -1) 2279b50d902SRodney W. Grimes switch(ch) { 2289b50d902SRodney W. Grimes case 'A': 2299b50d902SRodney W. Grimes Aflag = 1; 2309b50d902SRodney W. Grimes break; 2319b50d902SRodney W. Grimes case 'a': 2329b50d902SRodney W. Grimes aflag = 1; 2339b50d902SRodney W. Grimes break; 234e1e293a5SDavid Greenman case 'b': 235e1e293a5SDavid Greenman bflag = 1; 236e1e293a5SDavid Greenman break; 2379b50d902SRodney W. Grimes case 'd': 2389b50d902SRodney W. Grimes dflag = 1; 2399b50d902SRodney W. Grimes break; 2409b50d902SRodney W. Grimes case 'f': 241cbc17e71SGarrett Wollman #ifdef NS 2429b50d902SRodney W. Grimes if (strcmp(optarg, "ns") == 0) 2439b50d902SRodney W. Grimes af = AF_NS; 244cbc17e71SGarrett Wollman else 245cbc17e71SGarrett Wollman #endif 246cbc17e71SGarrett Wollman if (strcmp(optarg, "ipx") == 0) 247cc6a66f2SJulian Elischer af = AF_IPX; 2489b50d902SRodney W. Grimes else if (strcmp(optarg, "inet") == 0) 2499b50d902SRodney W. Grimes af = AF_INET; 2509b50d902SRodney W. Grimes else if (strcmp(optarg, "unix") == 0) 2519b50d902SRodney W. Grimes af = AF_UNIX; 25263bf4575SJulian Elischer else if (strcmp(optarg, "atalk") == 0) 25363bf4575SJulian Elischer af = AF_APPLETALK; 2540761cb29SGarrett Wollman #ifdef ISO 2559b50d902SRodney W. Grimes else if (strcmp(optarg, "iso") == 0) 2569b50d902SRodney W. Grimes af = AF_ISO; 2570761cb29SGarrett Wollman #endif 2589b50d902SRodney W. Grimes else { 25951e7d42cSGarrett Wollman errx(1, "%s: unknown address family", optarg); 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes break; 2629b50d902SRodney W. Grimes case 'g': 2639b50d902SRodney W. Grimes gflag = 1; 2649b50d902SRodney W. Grimes break; 2659b50d902SRodney W. Grimes case 'I': { 2669b50d902SRodney W. Grimes char *cp; 2679b50d902SRodney W. Grimes 2689b50d902SRodney W. Grimes iflag = 1; 2699b50d902SRodney W. Grimes for (cp = interface = optarg; isalpha(*cp); cp++) 2709b50d902SRodney W. Grimes continue; 2719b50d902SRodney W. Grimes unit = atoi(cp); 2729b50d902SRodney W. Grimes break; 2739b50d902SRodney W. Grimes } 2749b50d902SRodney W. Grimes case 'i': 2759b50d902SRodney W. Grimes iflag = 1; 2769b50d902SRodney W. Grimes break; 2779b50d902SRodney W. Grimes case 'M': 2789b50d902SRodney W. Grimes memf = optarg; 2799b50d902SRodney W. Grimes break; 2809b50d902SRodney W. Grimes case 'm': 2819b50d902SRodney W. Grimes mflag = 1; 2829b50d902SRodney W. Grimes break; 2839b50d902SRodney W. Grimes case 'N': 2849b50d902SRodney W. Grimes nlistf = optarg; 2859b50d902SRodney W. Grimes break; 2869b50d902SRodney W. Grimes case 'n': 2879b50d902SRodney W. Grimes nflag = 1; 2889b50d902SRodney W. Grimes break; 2899b50d902SRodney W. Grimes case 'p': 2909b50d902SRodney W. Grimes if ((tp = name2protox(optarg)) == NULL) { 29151e7d42cSGarrett Wollman errx(1, 29251e7d42cSGarrett Wollman "%s: unknown or uninstrumented protocol", 29351e7d42cSGarrett Wollman optarg); 2949b50d902SRodney W. Grimes } 2959b50d902SRodney W. Grimes pflag = 1; 2969b50d902SRodney W. Grimes break; 2979b50d902SRodney W. Grimes case 'r': 2989b50d902SRodney W. Grimes rflag = 1; 2999b50d902SRodney W. Grimes break; 3009b50d902SRodney W. Grimes case 's': 3019b50d902SRodney W. Grimes ++sflag; 3029b50d902SRodney W. Grimes break; 3039b50d902SRodney W. Grimes case 't': 3049b50d902SRodney W. Grimes tflag = 1; 3059b50d902SRodney W. Grimes break; 3069b50d902SRodney W. Grimes case 'u': 3079b50d902SRodney W. Grimes af = AF_UNIX; 3089b50d902SRodney W. Grimes break; 3099b50d902SRodney W. Grimes case 'w': 3109b50d902SRodney W. Grimes interval = atoi(optarg); 3119b50d902SRodney W. Grimes iflag = 1; 3129b50d902SRodney W. Grimes break; 3139b50d902SRodney W. Grimes case '?': 3149b50d902SRodney W. Grimes default: 3159b50d902SRodney W. Grimes usage(); 3169b50d902SRodney W. Grimes } 3179b50d902SRodney W. Grimes argv += optind; 3189b50d902SRodney W. Grimes argc -= optind; 3199b50d902SRodney W. Grimes 3209b50d902SRodney W. Grimes #define BACKWARD_COMPATIBILITY 3219b50d902SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 3229b50d902SRodney W. Grimes if (*argv) { 3239b50d902SRodney W. Grimes if (isdigit(**argv)) { 3249b50d902SRodney W. Grimes interval = atoi(*argv); 3259b50d902SRodney W. Grimes if (interval <= 0) 3269b50d902SRodney W. Grimes usage(); 3279b50d902SRodney W. Grimes ++argv; 3289b50d902SRodney W. Grimes iflag = 1; 3299b50d902SRodney W. Grimes } 3309b50d902SRodney W. Grimes if (*argv) { 3319b50d902SRodney W. Grimes nlistf = *argv; 3329b50d902SRodney W. Grimes if (*++argv) 3339b50d902SRodney W. Grimes memf = *argv; 3349b50d902SRodney W. Grimes } 3359b50d902SRodney W. Grimes } 3369b50d902SRodney W. Grimes #endif 3379b50d902SRodney W. Grimes 3389b50d902SRodney W. Grimes /* 3399b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 3409b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 3419b50d902SRodney W. Grimes */ 3429b50d902SRodney W. Grimes if (nlistf != NULL || memf != NULL) 3439b50d902SRodney W. Grimes setgid(getgid()); 3449b50d902SRodney W. Grimes 3459b50d902SRodney W. Grimes if (mflag) { 3464f81ef50SGarrett Wollman mbpr(); 3479b50d902SRodney W. Grimes exit(0); 3489b50d902SRodney W. Grimes } 3499b50d902SRodney W. Grimes if (pflag) { 350c6620a13SPoul-Henning Kamp if (!tp->pr_stats) { 3519b50d902SRodney W. Grimes printf("%s: no stats routine\n", tp->pr_name); 3529b50d902SRodney W. Grimes exit(0); 3539b50d902SRodney W. Grimes } 354c6620a13SPoul-Henning Kamp if (tp->pr_usesysctl) { 355c6620a13SPoul-Henning Kamp (*tp->pr_stats)(tp->pr_usesysctl, tp->pr_name); 356c6620a13SPoul-Henning Kamp } else { 357c6620a13SPoul-Henning Kamp kread(0, 0, 0); 358c6620a13SPoul-Henning Kamp (*tp->pr_stats)(nl[tp->pr_sindex].n_value, 359c6620a13SPoul-Henning Kamp tp->pr_name); 360c6620a13SPoul-Henning Kamp } 361c6620a13SPoul-Henning Kamp exit(0); 362c6620a13SPoul-Henning Kamp } 363cc63cd56SPeter Wemm #if 0 3649b50d902SRodney W. Grimes /* 3659b50d902SRodney W. Grimes * Keep file descriptors open to avoid overhead 3669b50d902SRodney W. Grimes * of open/close on each call to get* routines. 3679b50d902SRodney W. Grimes */ 3689b50d902SRodney W. Grimes sethostent(1); 3699b50d902SRodney W. Grimes setnetent(1); 370cc63cd56SPeter Wemm #else 371cc63cd56SPeter Wemm /* 372cc63cd56SPeter Wemm * This does not make sense any more with DNS being default over 373cc63cd56SPeter Wemm * the files. Doing a setXXXXent(1) causes a tcp connection to be 374cc63cd56SPeter Wemm * used for the queries, which is slower. 375cc63cd56SPeter Wemm */ 376cc63cd56SPeter Wemm #endif 3779b50d902SRodney W. Grimes if (iflag) { 378c6620a13SPoul-Henning Kamp kread(0, 0, 0); 3799b50d902SRodney W. Grimes intpr(interval, nl[N_IFNET].n_value); 3809b50d902SRodney W. Grimes exit(0); 3819b50d902SRodney W. Grimes } 3829b50d902SRodney W. Grimes if (rflag) { 383c6620a13SPoul-Henning Kamp kread(0, 0, 0); 3849b50d902SRodney W. Grimes if (sflag) 3859b50d902SRodney W. Grimes rt_stats(nl[N_RTSTAT].n_value); 3869b50d902SRodney W. Grimes else 3879b50d902SRodney W. Grimes routepr(nl[N_RTREE].n_value); 3889b50d902SRodney W. Grimes exit(0); 3899b50d902SRodney W. Grimes } 3909b50d902SRodney W. Grimes if (gflag) { 391c6620a13SPoul-Henning Kamp kread(0, 0, 0); 3929b50d902SRodney W. Grimes if (sflag) 3939b50d902SRodney W. Grimes mrt_stats(nl[N_MRTPROTO].n_value, 3949b50d902SRodney W. Grimes nl[N_MRTSTAT].n_value); 3959b50d902SRodney W. Grimes else 3969b50d902SRodney W. Grimes mroutepr(nl[N_MRTPROTO].n_value, 397ef105c25SGarrett Wollman nl[N_MFCTABLE].n_value, 3989b50d902SRodney W. Grimes nl[N_VIFTABLE].n_value); 3999b50d902SRodney W. Grimes exit(0); 4009b50d902SRodney W. Grimes } 4019b50d902SRodney W. Grimes if (af == AF_INET || af == AF_UNSPEC) { 4029b50d902SRodney W. Grimes setprotoent(1); 4039b50d902SRodney W. Grimes setservent(1); 4049b50d902SRodney W. Grimes /* ugh, this is O(MN) ... why do we do this? */ 4057d56c0eeSAlexander Langer while ((p = getprotoent())) { 4069b50d902SRodney W. Grimes for (tp = protox; tp->pr_name; tp++) 4079b50d902SRodney W. Grimes if (strcmp(tp->pr_name, p->p_name) == 0) 4089b50d902SRodney W. Grimes break; 4099b50d902SRodney W. Grimes if (tp->pr_name == 0 || tp->pr_wanted == 0) 4109b50d902SRodney W. Grimes continue; 4119b50d902SRodney W. Grimes printproto(tp, p->p_name); 4129b50d902SRodney W. Grimes } 4139b50d902SRodney W. Grimes endprotoent(); 4149b50d902SRodney W. Grimes } 415cc6a66f2SJulian Elischer if (af == AF_IPX || af == AF_UNSPEC) 416cc6a66f2SJulian Elischer for (tp = ipxprotox; tp->pr_name; tp++) 417cc6a66f2SJulian Elischer printproto(tp, tp->pr_name); 41863bf4575SJulian Elischer if (af == AF_APPLETALK || af == AF_UNSPEC) 41963bf4575SJulian Elischer for (tp = atalkprotox; tp->pr_name; tp++) 42063bf4575SJulian Elischer printproto(tp, tp->pr_name); 421cbc17e71SGarrett Wollman #ifdef NS 4229b50d902SRodney W. Grimes if (af == AF_NS || af == AF_UNSPEC) 4239b50d902SRodney W. Grimes for (tp = nsprotox; tp->pr_name; tp++) 4249b50d902SRodney W. Grimes printproto(tp, tp->pr_name); 425cbc17e71SGarrett Wollman #endif 4260761cb29SGarrett Wollman #ifdef ISO 4279b50d902SRodney W. Grimes if (af == AF_ISO || af == AF_UNSPEC) 4289b50d902SRodney W. Grimes for (tp = isoprotox; tp->pr_name; tp++) 4299b50d902SRodney W. Grimes printproto(tp, tp->pr_name); 4300761cb29SGarrett Wollman #endif 4319b50d902SRodney W. Grimes if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag) 4324f81ef50SGarrett Wollman unixpr(); 4339b50d902SRodney W. Grimes exit(0); 4349b50d902SRodney W. Grimes } 4359b50d902SRodney W. Grimes 4369b50d902SRodney W. Grimes /* 4379b50d902SRodney W. Grimes * Print out protocol statistics or control blocks (per sflag). 4389b50d902SRodney W. Grimes * If the interface was not specifically requested, and the symbol 4399b50d902SRodney W. Grimes * is not in the namelist, ignore this one. 4409b50d902SRodney W. Grimes */ 4419b50d902SRodney W. Grimes static void 4429b50d902SRodney W. Grimes printproto(tp, name) 4439b50d902SRodney W. Grimes register struct protox *tp; 4449b50d902SRodney W. Grimes char *name; 4459b50d902SRodney W. Grimes { 4469b50d902SRodney W. Grimes void (*pr)(); 4479b50d902SRodney W. Grimes u_long off; 4489b50d902SRodney W. Grimes 4499b50d902SRodney W. Grimes if (sflag) { 4509b50d902SRodney W. Grimes pr = tp->pr_stats; 4514f81ef50SGarrett Wollman off = tp->pr_usesysctl ? tp->pr_usesysctl 4524f81ef50SGarrett Wollman : nl[tp->pr_sindex].n_value; 4539b50d902SRodney W. Grimes } else { 4549b50d902SRodney W. Grimes pr = tp->pr_cblocks; 4554f81ef50SGarrett Wollman off = tp->pr_usesysctl ? tp->pr_usesysctl 4564f81ef50SGarrett Wollman : nl[tp->pr_index].n_value; 4579b50d902SRodney W. Grimes } 4589b50d902SRodney W. Grimes if (pr != NULL && (off || af != AF_UNSPEC)) 4599b50d902SRodney W. Grimes (*pr)(off, name); 4609b50d902SRodney W. Grimes } 4619b50d902SRodney W. Grimes 4629b50d902SRodney W. Grimes /* 4639b50d902SRodney W. Grimes * Read kernel memory, return 0 on success. 4649b50d902SRodney W. Grimes */ 4659b50d902SRodney W. Grimes int 4669b50d902SRodney W. Grimes kread(addr, buf, size) 4679b50d902SRodney W. Grimes u_long addr; 4689b50d902SRodney W. Grimes char *buf; 4699b50d902SRodney W. Grimes int size; 4709b50d902SRodney W. Grimes { 4714f81ef50SGarrett Wollman if (kvmd == 0) { 47299453c6aSPoul-Henning Kamp /* 47399453c6aSPoul-Henning Kamp * XXX. 47499453c6aSPoul-Henning Kamp */ 47599453c6aSPoul-Henning Kamp kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 47699453c6aSPoul-Henning Kamp if (kvmd != NULL) { 47799453c6aSPoul-Henning Kamp if (kvm_nlist(kvmd, nl) < 0) { 47899453c6aSPoul-Henning Kamp if(nlistf) 47999453c6aSPoul-Henning Kamp errx(1, "%s: kvm_nlist: %s", nlistf, 48099453c6aSPoul-Henning Kamp kvm_geterr(kvmd)); 48199453c6aSPoul-Henning Kamp else 48299453c6aSPoul-Henning Kamp errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 48399453c6aSPoul-Henning Kamp } 48499453c6aSPoul-Henning Kamp 48599453c6aSPoul-Henning Kamp if (nl[0].n_type == 0) { 48699453c6aSPoul-Henning Kamp if(nlistf) 48799453c6aSPoul-Henning Kamp errx(1, "%s: no namelist", nlistf); 48899453c6aSPoul-Henning Kamp else 48999453c6aSPoul-Henning Kamp errx(1, "no namelist"); 49099453c6aSPoul-Henning Kamp } 49199453c6aSPoul-Henning Kamp } else { 49299453c6aSPoul-Henning Kamp warnx("kvm not available"); 49399453c6aSPoul-Henning Kamp return(-1); 49499453c6aSPoul-Henning Kamp } 4954f81ef50SGarrett Wollman } 496c6620a13SPoul-Henning Kamp if (!buf) 497c6620a13SPoul-Henning Kamp return (0); 4989b50d902SRodney W. Grimes if (kvm_read(kvmd, addr, buf, size) != size) { 4993aa80b1dSDavid Greenman warnx("%s", kvm_geterr(kvmd)); 5009b50d902SRodney W. Grimes return (-1); 5019b50d902SRodney W. Grimes } 5029b50d902SRodney W. Grimes return (0); 5039b50d902SRodney W. Grimes } 5049b50d902SRodney W. Grimes 5059b50d902SRodney W. Grimes char * 5069b50d902SRodney W. Grimes plural(n) 5079b50d902SRodney W. Grimes int n; 5089b50d902SRodney W. Grimes { 5099b50d902SRodney W. Grimes return (n != 1 ? "s" : ""); 5109b50d902SRodney W. Grimes } 5119b50d902SRodney W. Grimes 5129b50d902SRodney W. Grimes char * 5139b50d902SRodney W. Grimes plurales(n) 5149b50d902SRodney W. Grimes int n; 5159b50d902SRodney W. Grimes { 5169b50d902SRodney W. Grimes return (n != 1 ? "es" : ""); 5179b50d902SRodney W. Grimes } 5189b50d902SRodney W. Grimes 5199b50d902SRodney W. Grimes /* 5209b50d902SRodney W. Grimes * Find the protox for the given "well-known" name. 5219b50d902SRodney W. Grimes */ 5229b50d902SRodney W. Grimes static struct protox * 5239b50d902SRodney W. Grimes knownname(name) 5249b50d902SRodney W. Grimes char *name; 5259b50d902SRodney W. Grimes { 5269b50d902SRodney W. Grimes struct protox **tpp, *tp; 5279b50d902SRodney W. Grimes 5289b50d902SRodney W. Grimes for (tpp = protoprotox; *tpp; tpp++) 5299b50d902SRodney W. Grimes for (tp = *tpp; tp->pr_name; tp++) 5309b50d902SRodney W. Grimes if (strcmp(tp->pr_name, name) == 0) 5319b50d902SRodney W. Grimes return (tp); 5329b50d902SRodney W. Grimes return (NULL); 5339b50d902SRodney W. Grimes } 5349b50d902SRodney W. Grimes 5359b50d902SRodney W. Grimes /* 5369b50d902SRodney W. Grimes * Find the protox corresponding to name. 5379b50d902SRodney W. Grimes */ 5389b50d902SRodney W. Grimes static struct protox * 5399b50d902SRodney W. Grimes name2protox(name) 5409b50d902SRodney W. Grimes char *name; 5419b50d902SRodney W. Grimes { 5429b50d902SRodney W. Grimes struct protox *tp; 5439b50d902SRodney W. Grimes char **alias; /* alias from p->aliases */ 5449b50d902SRodney W. Grimes struct protoent *p; 5459b50d902SRodney W. Grimes 5469b50d902SRodney W. Grimes /* 5479b50d902SRodney W. Grimes * Try to find the name in the list of "well-known" names. If that 5489b50d902SRodney W. Grimes * fails, check if name is an alias for an Internet protocol. 5499b50d902SRodney W. Grimes */ 5507d56c0eeSAlexander Langer if ((tp = knownname(name))) 5519b50d902SRodney W. Grimes return (tp); 5529b50d902SRodney W. Grimes 5539b50d902SRodney W. Grimes setprotoent(1); /* make protocol lookup cheaper */ 5547d56c0eeSAlexander Langer while ((p = getprotoent())) { 5559b50d902SRodney W. Grimes /* assert: name not same as p->name */ 5569b50d902SRodney W. Grimes for (alias = p->p_aliases; *alias; alias++) 5579b50d902SRodney W. Grimes if (strcmp(name, *alias) == 0) { 5589b50d902SRodney W. Grimes endprotoent(); 5599b50d902SRodney W. Grimes return (knownname(p->p_name)); 5609b50d902SRodney W. Grimes } 5619b50d902SRodney W. Grimes } 5629b50d902SRodney W. Grimes endprotoent(); 5639b50d902SRodney W. Grimes return (NULL); 5649b50d902SRodney W. Grimes } 5659b50d902SRodney W. Grimes 5669b50d902SRodney W. Grimes static void 5679b50d902SRodney W. Grimes usage() 5689b50d902SRodney W. Grimes { 5695d422d6aSPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 5705d422d6aSPhilippe Charnier "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]", 5715d422d6aSPhilippe Charnier " netstat [-bdghimnrs] [-f address_family] [-M core] [-N system]", 5725d422d6aSPhilippe Charnier " netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]", 5735d422d6aSPhilippe Charnier " netstat [-M core] [-N system] [-p protocol]"); 5749b50d902SRodney W. Grimes exit(1); 5759b50d902SRodney W. Grimes } 5769c437f50SPeter Wemm 5779c437f50SPeter Wemm void 5789c437f50SPeter Wemm trimdomain(cp) 5799c437f50SPeter Wemm char *cp; 5809c437f50SPeter Wemm { 5819c437f50SPeter Wemm static char domain[MAXHOSTNAMELEN + 1]; 5829c437f50SPeter Wemm static int first = 1; 5839c437f50SPeter Wemm char *s; 5849c437f50SPeter Wemm 5859c437f50SPeter Wemm if (first) { 5869c437f50SPeter Wemm first = 0; 5879c437f50SPeter Wemm if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 5889c437f50SPeter Wemm (s = strchr(domain, '.'))) 5899c437f50SPeter Wemm (void) strcpy(domain, s + 1); 5909c437f50SPeter Wemm else 5919c437f50SPeter Wemm domain[0] = 0; 5929c437f50SPeter Wemm } 5939c437f50SPeter Wemm 5949c437f50SPeter Wemm if (domain[0]) { 5959c437f50SPeter Wemm while ((cp = strchr(cp, '.'))) { 5969c437f50SPeter Wemm if (!strcasecmp(cp + 1, domain)) { 5979c437f50SPeter Wemm *cp = 0; /* hit it */ 5989c437f50SPeter Wemm break; 5999c437f50SPeter Wemm } else { 6009c437f50SPeter Wemm cp++; 6019c437f50SPeter Wemm } 6029c437f50SPeter Wemm } 6039c437f50SPeter Wemm } 6049c437f50SPeter Wemm } 6059c437f50SPeter Wemm 606