Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.23
      1  1.23    itojun /*	$NetBSD: main.c,v 1.23 1999/07/01 18:40:36 itojun Exp $	*/
      2   1.8   thorpej 
      3   1.1       cgd /*
      4   1.6   mycroft  * Copyright (c) 1983, 1988, 1993
      5   1.6   mycroft  *	Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.12     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.12     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\
     39  1.12     lukem 	Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.8   thorpej #if 0
     44   1.8   thorpej static char sccsid[] = "from: @(#)main.c	8.4 (Berkeley) 3/1/94";
     45   1.8   thorpej #else
     46  1.23    itojun __RCSID("$NetBSD: main.c,v 1.23 1999/07/01 18:40:36 itojun Exp $");
     47   1.8   thorpej #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/param.h>
     51   1.6   mycroft #include <sys/file.h>
     52   1.6   mycroft #include <sys/protosw.h>
     53   1.1       cgd #include <sys/socket.h>
     54   1.6   mycroft 
     55   1.6   mycroft #include <netinet/in.h>
     56   1.6   mycroft 
     57   1.6   mycroft #include <ctype.h>
     58  1.15       mrg #include <err.h>
     59   1.1       cgd #include <errno.h>
     60   1.6   mycroft #include <kvm.h>
     61   1.6   mycroft #include <limits.h>
     62   1.1       cgd #include <netdb.h>
     63   1.1       cgd #include <nlist.h>
     64   1.6   mycroft #include <paths.h>
     65   1.1       cgd #include <stdio.h>
     66   1.1       cgd #include <stdlib.h>
     67   1.1       cgd #include <string.h>
     68   1.6   mycroft #include <unistd.h>
     69   1.6   mycroft #include "netstat.h"
     70   1.1       cgd 
     71   1.1       cgd struct nlist nl[] = {
     72   1.1       cgd #define	N_MBSTAT	0
     73   1.1       cgd 	{ "_mbstat" },
     74   1.1       cgd #define	N_IPSTAT	1
     75   1.1       cgd 	{ "_ipstat" },
     76   1.7   mycroft #define	N_TCBTABLE	2
     77   1.7   mycroft 	{ "_tcbtable" },
     78   1.1       cgd #define	N_TCPSTAT	3
     79   1.1       cgd 	{ "_tcpstat" },
     80   1.7   mycroft #define	N_UDBTABLE	4
     81   1.7   mycroft 	{ "_udbtable" },
     82   1.1       cgd #define	N_UDPSTAT	5
     83   1.1       cgd 	{ "_udpstat" },
     84   1.1       cgd #define	N_IFNET		6
     85   1.1       cgd 	{ "_ifnet" },
     86   1.1       cgd #define	N_IMP		7
     87   1.1       cgd 	{ "_imp_softc" },
     88   1.6   mycroft #define	N_ICMPSTAT	8
     89   1.1       cgd 	{ "_icmpstat" },
     90   1.6   mycroft #define	N_RTSTAT	9
     91   1.1       cgd 	{ "_rtstat" },
     92   1.6   mycroft #define	N_UNIXSW	10
     93   1.1       cgd 	{ "_unixsw" },
     94   1.6   mycroft #define N_IDP		11
     95   1.1       cgd 	{ "_nspcb"},
     96   1.6   mycroft #define N_IDPSTAT	12
     97   1.1       cgd 	{ "_idpstat"},
     98   1.6   mycroft #define N_SPPSTAT	13
     99   1.1       cgd 	{ "_spp_istat"},
    100   1.6   mycroft #define N_NSERR		14
    101   1.1       cgd 	{ "_ns_errstat"},
    102   1.6   mycroft #define	N_CLNPSTAT	15
    103   1.1       cgd 	{ "_clnp_stat"},
    104   1.6   mycroft #define	IN_NOTUSED	16
    105   1.1       cgd 	{ "_tp_inpcb" },
    106   1.6   mycroft #define	ISO_TP		17
    107   1.6   mycroft 	{ "_tp_refinfo" },
    108   1.6   mycroft #define	N_TPSTAT	18
    109   1.1       cgd 	{ "_tp_stat" },
    110   1.6   mycroft #define	N_ESISSTAT	19
    111   1.1       cgd 	{ "_esis_stat"},
    112   1.6   mycroft #define N_NIMP		20
    113   1.1       cgd 	{ "_nimp"},
    114   1.6   mycroft #define N_RTREE		21
    115   1.6   mycroft 	{ "_rt_tables"},
    116   1.6   mycroft #define N_CLTP		22
    117   1.1       cgd 	{ "_cltb"},
    118   1.6   mycroft #define N_CLTPSTAT	23
    119   1.1       cgd 	{ "_cltpstat"},
    120   1.6   mycroft #define	N_NFILE		24
    121   1.6   mycroft 	{ "_nfile" },
    122   1.6   mycroft #define	N_FILE		25
    123   1.6   mycroft 	{ "_file" },
    124   1.6   mycroft #define N_IGMPSTAT	26
    125   1.4    brezak 	{ "_igmpstat" },
    126   1.6   mycroft #define N_MRTPROTO	27
    127   1.4    brezak 	{ "_ip_mrtproto" },
    128   1.6   mycroft #define N_MRTSTAT	28
    129   1.4    brezak 	{ "_mrtstat" },
    130   1.7   mycroft #define N_MFCHASHTBL	29
    131   1.7   mycroft 	{ "_mfchashtbl" },
    132   1.7   mycroft #define	N_MFCHASH	30
    133   1.7   mycroft 	{ "_mfchash" },
    134   1.7   mycroft #define N_VIFTABLE	31
    135   1.4    brezak 	{ "_viftable" },
    136  1.10  jonathan #define N_MSIZE		32
    137  1.10  jonathan 	{ "_msize" },
    138  1.10  jonathan #define N_MCLBYTES	33
    139  1.10  jonathan 	{ "_mclbytes" },
    140  1.11  christos #define N_DDPSTAT	34
    141  1.11  christos 	{ "_ddpstat"},
    142  1.11  christos #define N_DDPCB		35
    143  1.11  christos 	{ "_ddpcb"},
    144  1.21  sommerfe #define N_MBPOOL	36
    145  1.21  sommerfe 	{ "_mbpool" },
    146  1.21  sommerfe #define N_MCLPOOL	37
    147  1.21  sommerfe 	{ "_mclpool" },
    148  1.23    itojun #define N_DIVPCB	38
    149  1.23    itojun 	{ "_divcb"},
    150  1.23    itojun #define N_DIVSTAT	39
    151  1.23    itojun 	{ "_divstat"},
    152  1.23    itojun #define N_IP6STAT	40
    153  1.23    itojun 	{ "_ip6stat" },
    154  1.23    itojun #define N_TCB6		41
    155  1.23    itojun 	{ "_tcb6" },
    156  1.23    itojun #define N_TCP6STAT	42
    157  1.23    itojun 	{ "_tcp6stat" },
    158  1.23    itojun #define N_UDB6		43
    159  1.23    itojun 	{ "_udb6" },
    160  1.23    itojun #define N_UDP6STAT	44
    161  1.23    itojun 	{ "_udp6stat" },
    162  1.23    itojun #define N_ICMP6STAT	45
    163  1.23    itojun 	{ "_icmp6stat" },
    164  1.23    itojun #define N_IPSECSTAT	46
    165  1.23    itojun 	{ "_ipsecstat" },
    166  1.23    itojun #define N_IPSEC6STAT	47
    167  1.23    itojun 	{ "_ipsec6stat" },
    168  1.23    itojun #define N_PIM6STAT	48
    169  1.23    itojun 	{ "_pim6stat" },
    170  1.23    itojun #define N_MRT6PROTO	49
    171  1.23    itojun 	{ "_ip6_mrtproto" },
    172  1.23    itojun #define N_MRT6STAT	50
    173  1.23    itojun 	{ "_mrt6stat" },
    174  1.23    itojun #define N_MF6CTABLE	51
    175  1.23    itojun 	{ "_mf6ctable" },
    176  1.23    itojun #define N_MIF6TABLE	52
    177  1.23    itojun 	{ "_mif6table" },
    178  1.11  christos 	{ "" },
    179   1.1       cgd };
    180   1.1       cgd 
    181   1.1       cgd struct protox {
    182   1.1       cgd 	u_char	pr_index;		/* index into nlist of cb head */
    183   1.1       cgd 	u_char	pr_sindex;		/* index into nlist of stat block */
    184   1.1       cgd 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
    185  1.11  christos 	void	(*pr_cblocks)		/* control blocks printing routine */
    186  1.11  christos 			__P((u_long, char *));
    187  1.11  christos 	void	(*pr_stats)		/* statistics printing routine */
    188  1.11  christos 			__P((u_long, char *));
    189  1.14   thorpej 	void	(*pr_dump)		/* PCB state dump routine */
    190  1.14   thorpej 			__P((u_long));
    191   1.1       cgd 	char	*pr_name;		/* well-known name */
    192   1.1       cgd } protox[] = {
    193   1.7   mycroft 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
    194  1.14   thorpej 	  tcp_stats,	tcp_dump,	"tcp" },
    195   1.7   mycroft 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
    196  1.14   thorpej 	  udp_stats,	0,		"udp" },
    197   1.1       cgd 	{ -1,		N_IPSTAT,	1,	0,
    198  1.14   thorpej 	  ip_stats,	0,		"ip" },
    199   1.1       cgd 	{ -1,		N_ICMPSTAT,	1,	0,
    200  1.14   thorpej 	  icmp_stats,	0,		"icmp" },
    201   1.4    brezak 	{ -1,		N_IGMPSTAT,	1,	0,
    202  1.14   thorpej 	  igmp_stats,	0,		"igmp" },
    203  1.23    itojun #ifdef IPSEC
    204  1.23    itojun 	{ -1,		N_IPSECSTAT,	1,	0,
    205  1.23    itojun 	  ipsec_stats,	0,		"ipsec" },
    206  1.23    itojun #endif
    207   1.1       cgd 	{ -1,		-1,		0,	0,
    208  1.14   thorpej 	  0,		0,		0 }
    209   1.1       cgd };
    210   1.1       cgd 
    211  1.23    itojun #ifdef INET6
    212  1.23    itojun struct protox ip6protox[] = {
    213  1.23    itojun 	{ -1,		N_IP6STAT,	1,	0,
    214  1.23    itojun 	  ip6_stats,	0,		"ip6" },
    215  1.23    itojun 	{ -1,		N_ICMP6STAT,	1,	0,
    216  1.23    itojun 	  icmp6_stats,	0,		"icmp6" },
    217  1.23    itojun #ifdef TCP6
    218  1.23    itojun 	{ N_TCB6,	N_TCP6STAT,	1,	ip6protopr,
    219  1.23    itojun 	  tcp6_stats,	tcp6_dump,	"tcp6" },
    220  1.23    itojun #else
    221  1.23    itojun 	{ N_TCB6,	N_TCP6STAT,	1,	ip6protopr,
    222  1.23    itojun 	  tcp_stats,	tcp_dump,	"tcp6" },
    223  1.23    itojun #endif
    224  1.23    itojun 	{ N_UDB6,	N_UDP6STAT,	1,	ip6protopr,
    225  1.23    itojun 	  udp6_stats,	0,		"udp6" },
    226  1.23    itojun #ifdef IPSEC
    227  1.23    itojun 	{ -1,		N_IPSEC6STAT,	1,	0,
    228  1.23    itojun 	  ipsec_stats,	0,		"ipsec6" },
    229  1.23    itojun #endif
    230  1.23    itojun 	{ -1,		N_PIM6STAT,	1,	0,
    231  1.23    itojun 	  pim6_stats,	0,		"pim6" },
    232  1.23    itojun 	{ -1,		-1,		0,	0,
    233  1.23    itojun 	  0,		0,		0 }
    234  1.23    itojun };
    235  1.23    itojun #endif
    236  1.23    itojun 
    237  1.18       mrg #ifndef SMALL
    238  1.11  christos struct protox atalkprotox[] = {
    239  1.11  christos 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
    240  1.14   thorpej 	  ddp_stats,	0,		"ddp" },
    241  1.11  christos 	{ -1,		-1,		0,	0,
    242  1.11  christos 	  0,		0 }
    243  1.11  christos };
    244  1.11  christos 
    245   1.1       cgd struct protox nsprotox[] = {
    246   1.1       cgd 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    247  1.14   thorpej 	  idp_stats,	0,		"idp" },
    248   1.1       cgd 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    249  1.14   thorpej 	  spp_stats,	0,		"spp" },
    250   1.1       cgd 	{ -1,		N_NSERR,	1,	0,
    251  1.14   thorpej 	  nserr_stats,	0,		"ns_err" },
    252   1.1       cgd 	{ -1,		-1,		0,	0,
    253   1.1       cgd 	  0,		0 }
    254   1.1       cgd };
    255   1.1       cgd 
    256   1.1       cgd struct protox isoprotox[] = {
    257   1.1       cgd 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    258  1.14   thorpej 	  tp_stats,	0,		"tp" },
    259   1.1       cgd 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    260  1.14   thorpej 	  cltp_stats,	0,		"cltp" },
    261   1.1       cgd 	{ -1,		N_CLNPSTAT,	1,	 0,
    262  1.14   thorpej 	  clnp_stats,	0,		"clnp"},
    263   1.1       cgd 	{ -1,		N_ESISSTAT,	1,	 0,
    264  1.14   thorpej 	  esis_stats,	0,		"esis"},
    265   1.1       cgd 	{ -1,		-1,		0,	0,
    266  1.14   thorpej 	  0,		0,		0 }
    267   1.1       cgd };
    268  1.18       mrg #endif
    269   1.1       cgd 
    270  1.18       mrg struct protox *protoprotox[] = { protox,
    271  1.23    itojun #ifdef INET6
    272  1.23    itojun 				 ip6protox,
    273  1.23    itojun #endif
    274  1.18       mrg #ifndef SMALL
    275  1.18       mrg 				 atalkprotox, nsprotox, isoprotox,
    276  1.18       mrg #endif
    277  1.18       mrg 				 NULL };
    278   1.1       cgd 
    279  1.11  christos int main __P((int, char *[]));
    280   1.6   mycroft static void printproto __P((struct protox *, char *));
    281   1.6   mycroft static void usage __P((void));
    282   1.6   mycroft static struct protox *name2protox __P((char *));
    283   1.6   mycroft static struct protox *knownname __P((char *));
    284   1.1       cgd 
    285   1.6   mycroft kvm_t *kvmd;
    286   1.1       cgd 
    287   1.6   mycroft int
    288   1.1       cgd main(argc, argv)
    289   1.1       cgd 	int argc;
    290   1.6   mycroft 	char *argv[];
    291   1.1       cgd {
    292   1.1       cgd 	extern char *optarg;
    293   1.1       cgd 	extern int optind;
    294  1.12     lukem 	struct protoent *p;
    295  1.12     lukem 	struct protox *tp;	/* for printing cblocks & stats */
    296   1.1       cgd 	int ch;
    297   1.6   mycroft 	char *nlistf = NULL, *memf = NULL;
    298  1.14   thorpej 	char buf[_POSIX2_LINE_MAX], *cp;
    299  1.14   thorpej 	u_long pcbaddr;
    300  1.15       mrg 	gid_t egid = getegid();
    301   1.6   mycroft 
    302  1.15       mrg 	(void)setegid(getgid());
    303  1.12     lukem 	tp = NULL;
    304   1.6   mycroft 	af = AF_UNSPEC;
    305  1.14   thorpej 	pcbaddr = 0;
    306   1.1       cgd 
    307  1.23    itojun 	while ((ch = getopt(argc, argv, "Aabdf:ghI:liM:mN:nP:p:rstuvw:")) != -1)
    308   1.6   mycroft 		switch(ch) {
    309   1.1       cgd 		case 'A':
    310   1.1       cgd 			Aflag = 1;
    311   1.1       cgd 			break;
    312   1.1       cgd 		case 'a':
    313   1.1       cgd 			aflag = 1;
    314  1.13       kml 			break;
    315  1.13       kml 		case 'b':
    316  1.13       kml 			bflag = 1;
    317   1.1       cgd 			break;
    318   1.1       cgd 		case 'd':
    319   1.1       cgd 			dflag = 1;
    320   1.1       cgd 			break;
    321   1.1       cgd 		case 'f':
    322   1.1       cgd 			if (strcmp(optarg, "ns") == 0)
    323   1.1       cgd 				af = AF_NS;
    324   1.1       cgd 			else if (strcmp(optarg, "inet") == 0)
    325   1.1       cgd 				af = AF_INET;
    326  1.23    itojun 			else if (strcmp(optarg, "inet6") == 0)
    327  1.23    itojun 				af = AF_INET6;
    328  1.17     lukem 			else if (strcmp(optarg, "unix") == 0
    329  1.17     lukem 			    || strcmp(optarg, "local") == 0)
    330  1.17     lukem 				af = AF_LOCAL;
    331   1.1       cgd 			else if (strcmp(optarg, "iso") == 0)
    332   1.1       cgd 				af = AF_ISO;
    333  1.11  christos 			else if (strcmp(optarg, "atalk") == 0)
    334  1.11  christos 				af = AF_APPLETALK;
    335  1.16       mrg 			else
    336  1.16       mrg 				errx(1, "%s: unknown address family",
    337  1.16       mrg 				    optarg);
    338   1.1       cgd 			break;
    339  1.18       mrg #ifndef SMALL
    340   1.6   mycroft 		case 'g':
    341   1.6   mycroft 			gflag = 1;
    342   1.1       cgd 			break;
    343  1.18       mrg #endif
    344   1.9   thorpej 		case 'I':
    345   1.1       cgd 			iflag = 1;
    346   1.9   thorpej 			interface = optarg;
    347   1.1       cgd 			break;
    348   1.1       cgd 		case 'i':
    349   1.1       cgd 			iflag = 1;
    350   1.1       cgd 			break;
    351  1.23    itojun 		case 'l':
    352  1.23    itojun 			lflag = 1;
    353  1.23    itojun 			break;
    354   1.1       cgd 		case 'M':
    355   1.6   mycroft 			memf = optarg;
    356   1.1       cgd 			break;
    357   1.1       cgd 		case 'm':
    358   1.1       cgd 			mflag = 1;
    359   1.1       cgd 			break;
    360   1.1       cgd 		case 'N':
    361   1.6   mycroft 			nlistf = optarg;
    362   1.1       cgd 			break;
    363   1.1       cgd 		case 'n':
    364   1.1       cgd 			nflag = 1;
    365   1.1       cgd 			break;
    366  1.14   thorpej 		case 'P':
    367  1.14   thorpej 			pcbaddr = strtoul(optarg, &cp, 16);
    368  1.16       mrg 			if (*cp != '\0' || errno == ERANGE)
    369  1.16       mrg 				errx(1, "invalid PCB address %s",
    370  1.16       mrg 				    optarg);
    371  1.14   thorpej 			Pflag = 1;
    372  1.14   thorpej 			break;
    373   1.1       cgd 		case 'p':
    374  1.16       mrg 			if ((tp = name2protox(optarg)) == NULL)
    375  1.16       mrg 				errx(1, "%s: unknown or uninstrumented protocol",
    376  1.16       mrg 				    optarg);
    377   1.1       cgd 			pflag = 1;
    378   1.1       cgd 			break;
    379   1.1       cgd 		case 'r':
    380   1.1       cgd 			rflag = 1;
    381   1.1       cgd 			break;
    382   1.1       cgd 		case 's':
    383   1.6   mycroft 			++sflag;
    384   1.1       cgd 			break;
    385   1.1       cgd 		case 't':
    386   1.1       cgd 			tflag = 1;
    387   1.1       cgd 			break;
    388   1.1       cgd 		case 'u':
    389  1.17     lukem 			af = AF_LOCAL;
    390   1.1       cgd 			break;
    391  1.19       kml 		case 'v':
    392  1.19       kml 			vflag = 1;
    393  1.19       kml 			break;
    394   1.1       cgd 		case 'w':
    395   1.1       cgd 			interval = atoi(optarg);
    396   1.6   mycroft 			iflag = 1;
    397   1.1       cgd 			break;
    398   1.1       cgd 		case '?':
    399   1.1       cgd 		default:
    400   1.1       cgd 			usage();
    401   1.1       cgd 		}
    402   1.1       cgd 	argv += optind;
    403   1.1       cgd 	argc -= optind;
    404   1.1       cgd 
    405   1.1       cgd #define	BACKWARD_COMPATIBILITY
    406   1.1       cgd #ifdef	BACKWARD_COMPATIBILITY
    407   1.1       cgd 	if (*argv) {
    408   1.1       cgd 		if (isdigit(**argv)) {
    409   1.1       cgd 			interval = atoi(*argv);
    410   1.1       cgd 			if (interval <= 0)
    411   1.1       cgd 				usage();
    412   1.1       cgd 			++argv;
    413   1.1       cgd 			iflag = 1;
    414   1.1       cgd 		}
    415   1.1       cgd 		if (*argv) {
    416   1.6   mycroft 			nlistf = *argv;
    417   1.6   mycroft 			if (*++argv)
    418   1.6   mycroft 				memf = *argv;
    419   1.1       cgd 		}
    420   1.1       cgd 	}
    421   1.1       cgd #endif
    422   1.6   mycroft 
    423   1.6   mycroft 	/*
    424  1.15       mrg 	 * Discard setgid privileges.  If not the running kernel, we toss
    425  1.15       mrg 	 * them away totally so that bad guys can't print interesting stuff
    426  1.15       mrg 	 * from kernel memory, otherwise switch back to kmem for the
    427  1.15       mrg 	 * duration of the kvm_openfiles() call.
    428   1.6   mycroft 	 */
    429  1.20  explorer 	if (nlistf != NULL || memf != NULL || Pflag)
    430  1.15       mrg 		(void)setgid(getgid());
    431  1.15       mrg 	else
    432  1.15       mrg 		(void)setegid(egid);
    433   1.6   mycroft 
    434   1.9   thorpej 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    435  1.15       mrg 	    buf)) == NULL)
    436  1.15       mrg 		errx(1, "%s", buf);
    437  1.15       mrg 
    438  1.16       mrg 	/* do this now anyway */
    439  1.15       mrg 	if (nlistf == NULL && memf == NULL)
    440  1.15       mrg 		(void)setgid(getgid());
    441  1.15       mrg 
    442   1.6   mycroft 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    443   1.6   mycroft 		if (nlistf)
    444  1.16       mrg 			errx(1, "%s: no namelist", nlistf);
    445   1.6   mycroft 		else
    446  1.16       mrg 			errx(1, "no namelist");
    447   1.1       cgd 	}
    448   1.1       cgd 	if (mflag) {
    449  1.10  jonathan 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
    450  1.21  sommerfe 		    nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
    451  1.21  sommerfe 		    nl[N_MCLPOOL].n_value);
    452   1.1       cgd 		exit(0);
    453   1.1       cgd 	}
    454  1.14   thorpej 	if (Pflag) {
    455  1.14   thorpej 		if (tp == NULL) {
    456  1.14   thorpej 			/* Default to TCP. */
    457  1.14   thorpej 			tp = name2protox("tcp");
    458  1.14   thorpej 		}
    459  1.14   thorpej 		if (tp->pr_dump)
    460  1.14   thorpej 			(*tp->pr_dump)(pcbaddr);
    461  1.14   thorpej 		else
    462  1.14   thorpej 			printf("%s: no PCB dump routine\n", tp->pr_name);
    463  1.14   thorpej 		exit(0);
    464  1.14   thorpej 	}
    465   1.1       cgd 	if (pflag) {
    466   1.1       cgd 		if (tp->pr_stats)
    467   1.1       cgd 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    468   1.1       cgd 				tp->pr_name);
    469   1.1       cgd 		else
    470   1.1       cgd 			printf("%s: no stats routine\n", tp->pr_name);
    471   1.1       cgd 		exit(0);
    472   1.1       cgd 	}
    473   1.1       cgd 	/*
    474   1.1       cgd 	 * Keep file descriptors open to avoid overhead
    475   1.1       cgd 	 * of open/close on each call to get* routines.
    476   1.1       cgd 	 */
    477   1.1       cgd 	sethostent(1);
    478   1.1       cgd 	setnetent(1);
    479   1.1       cgd 	if (iflag) {
    480   1.1       cgd 		intpr(interval, nl[N_IFNET].n_value);
    481   1.1       cgd 		exit(0);
    482   1.1       cgd 	}
    483   1.1       cgd 	if (rflag) {
    484   1.1       cgd 		if (sflag)
    485   1.5       cgd 			rt_stats(nl[N_RTSTAT].n_value);
    486   1.1       cgd 		else
    487   1.6   mycroft 			routepr(nl[N_RTREE].n_value);
    488   1.1       cgd 		exit(0);
    489   1.1       cgd 	}
    490  1.18       mrg #ifndef SMALL
    491   1.6   mycroft 	if (gflag) {
    492  1.23    itojun 		if (sflag) {
    493  1.23    itojun 			if (af == AF_INET || af == AF_UNSPEC)
    494  1.23    itojun 				mrt_stats(nl[N_MRTPROTO].n_value,
    495  1.23    itojun 					  nl[N_MRTSTAT].n_value);
    496  1.23    itojun #ifdef INET6
    497  1.23    itojun 			if (af == AF_INET6 || af == AF_UNSPEC)
    498  1.23    itojun 				mrt6_stats(nl[N_MRT6PROTO].n_value,
    499  1.23    itojun 					   nl[N_MRT6STAT].n_value);
    500  1.23    itojun #endif
    501  1.23    itojun 		}
    502  1.23    itojun 		else {
    503  1.23    itojun 			if (af == AF_INET || af == AF_UNSPEC)
    504  1.23    itojun 				mroutepr(nl[N_MRTPROTO].n_value,
    505  1.23    itojun 					 nl[N_MFCHASHTBL].n_value,
    506  1.23    itojun 					 nl[N_MFCHASH].n_value,
    507  1.23    itojun 					 nl[N_VIFTABLE].n_value);
    508  1.23    itojun #ifdef INET6
    509  1.23    itojun 			if (af == AF_INET6 || af == AF_UNSPEC)
    510  1.23    itojun 				mroute6pr(nl[N_MRT6PROTO].n_value,
    511  1.23    itojun 					  nl[N_MF6CTABLE].n_value,
    512  1.23    itojun 					  nl[N_MIF6TABLE].n_value);
    513  1.23    itojun #endif
    514  1.23    itojun 		}
    515   1.4    brezak 		exit(0);
    516   1.4    brezak 	}
    517  1.18       mrg #endif
    518   1.6   mycroft 	if (af == AF_INET || af == AF_UNSPEC) {
    519   1.6   mycroft 		setprotoent(1);
    520   1.6   mycroft 		setservent(1);
    521   1.6   mycroft 		/* ugh, this is O(MN) ... why do we do this? */
    522  1.11  christos 		while ((p = getprotoent()) != NULL) {
    523   1.6   mycroft 			for (tp = protox; tp->pr_name; tp++)
    524   1.6   mycroft 				if (strcmp(tp->pr_name, p->p_name) == 0)
    525   1.6   mycroft 					break;
    526   1.6   mycroft 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    527   1.6   mycroft 				continue;
    528   1.6   mycroft 			printproto(tp, p->p_name);
    529  1.23    itojun 			tp->pr_wanted = 0;
    530   1.6   mycroft 		}
    531   1.6   mycroft 		endprotoent();
    532  1.23    itojun 		for (tp = protox; tp->pr_name; tp++)
    533  1.23    itojun 			if (tp->pr_wanted)
    534  1.23    itojun 				printproto(tp, tp->pr_name);
    535   1.6   mycroft 	}
    536  1.23    itojun #ifdef INET6
    537  1.23    itojun 	if (af == AF_INET6 || af == AF_UNSPEC)
    538  1.23    itojun 		for (tp = ip6protox; tp->pr_name; tp++)
    539  1.23    itojun 			printproto(tp, tp->pr_name);
    540  1.23    itojun #endif
    541  1.18       mrg #ifndef SMALL
    542  1.11  christos 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    543  1.11  christos 		for (tp = atalkprotox; tp->pr_name; tp++)
    544  1.11  christos 			printproto(tp, tp->pr_name);
    545   1.6   mycroft 	if (af == AF_NS || af == AF_UNSPEC)
    546   1.6   mycroft 		for (tp = nsprotox; tp->pr_name; tp++)
    547   1.6   mycroft 			printproto(tp, tp->pr_name);
    548   1.6   mycroft 	if (af == AF_ISO || af == AF_UNSPEC)
    549   1.6   mycroft 		for (tp = isoprotox; tp->pr_name; tp++)
    550   1.6   mycroft 			printproto(tp, tp->pr_name);
    551  1.17     lukem 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
    552   1.6   mycroft 		unixpr(nl[N_UNIXSW].n_value);
    553  1.18       mrg #endif
    554   1.6   mycroft 	exit(0);
    555   1.6   mycroft }
    556   1.4    brezak 
    557   1.6   mycroft /*
    558   1.6   mycroft  * Print out protocol statistics or control blocks (per sflag).
    559   1.6   mycroft  * If the interface was not specifically requested, and the symbol
    560   1.6   mycroft  * is not in the namelist, ignore this one.
    561   1.6   mycroft  */
    562   1.6   mycroft static void
    563   1.6   mycroft printproto(tp, name)
    564  1.12     lukem 	struct protox *tp;
    565   1.6   mycroft 	char *name;
    566   1.6   mycroft {
    567  1.11  christos 	void (*pr) __P((u_long, char *));
    568   1.6   mycroft 	u_long off;
    569   1.1       cgd 
    570   1.6   mycroft 	if (sflag) {
    571   1.6   mycroft 		pr = tp->pr_stats;
    572   1.6   mycroft 		off = nl[tp->pr_sindex].n_value;
    573   1.6   mycroft 	} else {
    574   1.6   mycroft 		pr = tp->pr_cblocks;
    575   1.6   mycroft 		off = nl[tp->pr_index].n_value;
    576   1.1       cgd 	}
    577   1.6   mycroft 	if (pr != NULL && (off || af != AF_UNSPEC))
    578   1.6   mycroft 		(*pr)(off, name);
    579   1.6   mycroft }
    580   1.6   mycroft 
    581   1.6   mycroft /*
    582   1.6   mycroft  * Read kernel memory, return 0 on success.
    583   1.6   mycroft  */
    584   1.6   mycroft int
    585   1.6   mycroft kread(addr, buf, size)
    586   1.6   mycroft 	u_long addr;
    587   1.6   mycroft 	char *buf;
    588   1.6   mycroft 	int size;
    589   1.6   mycroft {
    590   1.6   mycroft 
    591   1.6   mycroft 	if (kvm_read(kvmd, addr, buf, size) != size) {
    592  1.16       mrg 		warnx("%s\n", kvm_geterr(kvmd));
    593   1.6   mycroft 		return (-1);
    594   1.1       cgd 	}
    595   1.6   mycroft 	return (0);
    596   1.1       cgd }
    597   1.1       cgd 
    598   1.1       cgd char *
    599   1.1       cgd plural(n)
    600   1.1       cgd 	int n;
    601   1.1       cgd {
    602  1.16       mrg 
    603   1.1       cgd 	return (n != 1 ? "s" : "");
    604   1.1       cgd }
    605   1.1       cgd 
    606   1.6   mycroft char *
    607   1.6   mycroft plurales(n)
    608   1.6   mycroft 	int n;
    609   1.6   mycroft {
    610  1.16       mrg 
    611   1.6   mycroft 	return (n != 1 ? "es" : "");
    612   1.6   mycroft }
    613   1.6   mycroft 
    614   1.1       cgd /*
    615   1.1       cgd  * Find the protox for the given "well-known" name.
    616   1.1       cgd  */
    617   1.6   mycroft static struct protox *
    618   1.1       cgd knownname(name)
    619   1.1       cgd 	char *name;
    620   1.1       cgd {
    621   1.1       cgd 	struct protox **tpp, *tp;
    622   1.1       cgd 
    623   1.1       cgd 	for (tpp = protoprotox; *tpp; tpp++)
    624   1.6   mycroft 		for (tp = *tpp; tp->pr_name; tp++)
    625   1.6   mycroft 			if (strcmp(tp->pr_name, name) == 0)
    626   1.6   mycroft 				return (tp);
    627   1.6   mycroft 	return (NULL);
    628   1.1       cgd }
    629   1.1       cgd 
    630   1.1       cgd /*
    631   1.1       cgd  * Find the protox corresponding to name.
    632   1.1       cgd  */
    633   1.6   mycroft static struct protox *
    634   1.1       cgd name2protox(name)
    635   1.1       cgd 	char *name;
    636   1.1       cgd {
    637   1.1       cgd 	struct protox *tp;
    638   1.1       cgd 	char **alias;			/* alias from p->aliases */
    639   1.1       cgd 	struct protoent *p;
    640   1.1       cgd 
    641   1.1       cgd 	/*
    642   1.1       cgd 	 * Try to find the name in the list of "well-known" names. If that
    643   1.1       cgd 	 * fails, check if name is an alias for an Internet protocol.
    644   1.1       cgd 	 */
    645  1.11  christos 	if ((tp = knownname(name)) != NULL)
    646   1.6   mycroft 		return (tp);
    647   1.1       cgd 
    648   1.1       cgd 	setprotoent(1);			/* make protocol lookup cheaper */
    649  1.11  christos 	while ((p = getprotoent()) != NULL) {
    650   1.1       cgd 		/* assert: name not same as p->name */
    651   1.1       cgd 		for (alias = p->p_aliases; *alias; alias++)
    652   1.1       cgd 			if (strcmp(name, *alias) == 0) {
    653   1.1       cgd 				endprotoent();
    654   1.6   mycroft 				return (knownname(p->p_name));
    655   1.1       cgd 			}
    656   1.1       cgd 	}
    657   1.1       cgd 	endprotoent();
    658   1.6   mycroft 	return (NULL);
    659   1.1       cgd }
    660   1.1       cgd 
    661   1.6   mycroft static void
    662   1.1       cgd usage()
    663   1.1       cgd {
    664   1.1       cgd 	(void)fprintf(stderr,
    665   1.9   thorpej "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
    666   1.1       cgd 	(void)fprintf(stderr,
    667  1.22  kristerw "       %s [-gimnrsv] [-f address_family] [-M core] [-N system]\n",
    668  1.19       kml 	__progname);
    669   1.1       cgd 	(void)fprintf(stderr,
    670   1.9   thorpej "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
    671   1.1       cgd 	(void)fprintf(stderr,
    672   1.9   thorpej "       %s [-M core] [-N system] [-p protocol]\n", __progname);
    673  1.14   thorpej 	(void)fprintf(stderr,
    674  1.14   thorpej "       %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname);
    675   1.1       cgd 	exit(1);
    676   1.1       cgd }
    677