xref: /dflybsd-src/usr.bin/netstat/main.c (revision 13b1cc005d3b889fc677a2638fb546c64e5f6ebd)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1988, 1993 Regents of the University of California.  All rights reserved.
34  * @(#)main.c	8.4 (Berkeley) 3/1/94
35  * $FreeBSD: src/usr.bin/netstat/main.c,v 1.34.2.12 2001/09/17 15:17:46 ru Exp $
36  * $DragonFly: src/usr.bin/netstat/main.c,v 1.4 2003/08/24 23:16:53 hsu Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/file.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 
44 #include <netinet/in.h>
45 
46 #include <netgraph/socket/ng_socket.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <kvm.h>
52 #include <limits.h>
53 #include <netdb.h>
54 #include <nlist.h>
55 #include <paths.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include "netstat.h"
61 
62 static struct nlist nl[] = {
63 #define	N_IFNET		0
64 	{ "_ifnet" },
65 #define	N_IMP		1
66 	{ "_imp_softc" },
67 #define	N_RTSTAT	2
68 	{ "_rtstat" },
69 #define	N_UNIXSW	3
70 	{ "_localsw" },
71 #define N_IDP		4
72 	{ "_nspcb"},
73 #define N_IDPSTAT	5
74 	{ "_idpstat"},
75 #define N_SPPSTAT	6
76 	{ "_spp_istat"},
77 #define N_NSERR		7
78 	{ "_ns_errstat"},
79 #define	N_CLNPSTAT	8
80 	{ "_clnp_stat"},
81 #define	IN_NOTUSED	9
82 	{ "_tp_inpcb" },
83 #define	ISO_TP		10
84 	{ "_tp_refinfo" },
85 #define	N_TPSTAT	11
86 	{ "_tp_stat" },
87 #define	N_ESISSTAT	12
88 	{ "_esis_stat"},
89 #define N_NIMP		13
90 	{ "_nimp"},
91 #define N_RTREE		14
92 	{ "_rt_tables"},
93 #define N_CLTP		15
94 	{ "_cltb"},
95 #define N_CLTPSTAT	16
96 	{ "_cltpstat"},
97 #define	N_NFILE		17
98 	{ "_nfile" },
99 #define	N_FILE		18
100 	{ "_file" },
101 #define N_MRTSTAT	19
102 	{ "_mrtstat" },
103 #define N_MFCTABLE	20
104 	{ "_mfctable" },
105 #define N_VIFTABLE	21
106 	{ "_viftable" },
107 #define N_IPX		22
108 	{ "_ipxpcb"},
109 #define N_IPXSTAT	23
110 	{ "_ipxstat"},
111 #define N_SPXSTAT	24
112 	{ "_spx_istat"},
113 #define N_DDPSTAT	25
114 	{ "_ddpstat"},
115 #define N_DDPCB		26
116 	{ "_ddpcb"},
117 #define N_NGSOCKS	27
118 	{ "_ngsocklist"},
119 #define N_IP6STAT	28
120 	{ "_ip6stat" },
121 #define N_ICMP6STAT	29
122 	{ "_icmp6stat" },
123 #define N_IPSECSTAT	30
124 	{ "_ipsecstat" },
125 #define N_IPSEC6STAT	31
126 	{ "_ipsec6stat" },
127 #define N_PIM6STAT	32
128 	{ "_pim6stat" },
129 #define N_MRT6PROTO	33
130 	{ "_ip6_mrtproto" },
131 #define N_MRT6STAT	34
132 	{ "_mrt6stat" },
133 #define N_MF6CTABLE	35
134 	{ "_mf6ctable" },
135 #define N_MIF6TABLE	36
136 	{ "_mif6table" },
137 #define N_PFKEYSTAT	37
138 	{ "_pfkeystat" },
139 #define N_MBSTAT	38
140 	{ "_mbstat" },
141 #define N_MBTYPES	39
142 	{ "_mbtypes" },
143 #define N_NMBCLUSTERS	40
144 	{ "_nmbclusters" },
145 #define N_NMBUFS	41
146 	{ "_nmbufs" },
147 #define	N_RTTRASH	42
148 	{ "_rttrash" },
149 	{ "" },
150 };
151 
152 struct protox {
153 	u_char	pr_index;		/* index into nlist of cb head */
154 	u_char	pr_sindex;		/* index into nlist of stat block */
155 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
156 	void	(*pr_cblocks)(u_long, char *, int);
157 					/* control blocks printing routine */
158 	void	(*pr_stats)(u_long, char *, int);
159 					/* statistics printing routine */
160 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
161 	char	*pr_name;		/* well-known name */
162 	int	pr_usesysctl;		/* true if we use sysctl, not kvm */
163 } protox[] = {
164 	{ -1,		-1,		1,	protopr,
165 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
166 	{ -1,		-1,		1,	protopr,
167 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
168 	{ -1,		-1,		1,	protopr,
169 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
170 	{ -1,		-1,		1,	protopr,
171 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
172 	{ -1,		-1,		1,	protopr,
173 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
174 	{ -1,		-1,		1,	protopr,
175 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
176 #ifdef IPSEC
177 	{ -1,		N_IPSECSTAT,	1,	0,
178 	  ipsec_stats,	NULL,		"ipsec",	0},
179 #endif
180 	{ -1,		-1,		1,	0,
181 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
182 	{ -1,		-1,		0,	0,
183 	  0,		NULL,		0 }
184 };
185 
186 #ifdef INET6
187 struct protox ip6protox[] = {
188 	{ -1,		-1,		1,	protopr,
189 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
190 	{ -1,		-1,		1,	protopr,
191 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
192 	{ -1,		N_IP6STAT,	1,	protopr,
193 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
194 	{ -1,		N_ICMP6STAT,	1,	protopr,
195 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
196 #ifdef IPSEC
197 	{ -1,		N_IPSEC6STAT,	1,	0,
198 	  ipsec_stats,	NULL,		"ipsec6",0 },
199 #endif
200 #ifdef notyet
201 	{ -1,		N_PIM6STAT,	1,	0,
202 	  pim6_stats,	NULL,		"pim6",	0 },
203 #endif
204 	{ -1,		-1,		1,	0,
205 	  rip6_stats,	NULL,		"rip6",	0 },
206 	{ -1,		-1,		1,	0,
207 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
208 	{ -1,		-1,		1,	protopr,
209 	  pim_stats,	NULL,		"pim",	IPPROTO_PIM },
210 	{ -1,		-1,		0,	0,
211 	  0,		NULL,		0,	0 }
212 };
213 #endif /*INET6*/
214 
215 #ifdef IPSEC
216 struct protox pfkeyprotox[] = {
217 	{ -1,		N_PFKEYSTAT,	1,	0,
218 	  pfkey_stats,	NULL,		"pfkey", 0 },
219 	{ -1,		-1,		0,	0,
220 	  0,		NULL,		0,	0 }
221 };
222 #endif
223 
224 struct protox atalkprotox[] = {
225 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
226 	  ddp_stats,	NULL,		"ddp" },
227 	{ -1,		-1,		0,	0,
228 	  0,		NULL,		0 }
229 };
230 
231 struct protox netgraphprotox[] = {
232 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
233 	  NULL,		NULL,		"ctrl" },
234 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
235 	  NULL,		NULL,		"data" },
236 	{ -1,		NULL,		0,	0,
237 	  0,		NULL,		0 }
238 };
239 
240 struct protox ipxprotox[] = {
241 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
242 	  ipx_stats,	NULL,		"ipx",	0 },
243 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
244 	  spx_stats,	NULL,		"spx",	0 },
245 	{ -1,		-1,		0,	0,
246 	  0,		NULL,		0,	0 }
247 };
248 
249 #ifdef NS
250 struct protox nsprotox[] = {
251 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
252 	  idp_stats,	NULL,		"idp" },
253 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
254 	  spp_stats,	NULL,		"spp" },
255 	{ -1,		N_NSERR,	1,	0,
256 	  nserr_stats,	NULL,		"ns_err" },
257 	{ -1,		-1,		0,	0,
258 	  0,		NULL,		0 }
259 };
260 #endif
261 
262 #ifdef ISO
263 struct protox isoprotox[] = {
264 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
265 	  tp_stats,	NULL,		"tp" },
266 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
267 	  cltp_stats,	NULL,		"cltp" },
268 	{ -1,		N_CLNPSTAT,	1,	 0,
269 	  clnp_stats,	NULL,		"clnp"},
270 	{ -1,		N_ESISSTAT,	1,	 0,
271 	  esis_stats,	NULL,		"esis"},
272 	{ -1,		-1,		0,	0,
273 	  0,		NULL,		0 }
274 };
275 #endif
276 
277 struct protox *protoprotox[] = {
278 					 protox,
279 #ifdef INET6
280 					 ip6protox,
281 #endif
282 #ifdef IPSEC
283 					 pfkeyprotox,
284 #endif
285 					 ipxprotox, atalkprotox,
286 #ifdef NS
287 					 nsprotox,
288 #endif
289 #ifdef ISO
290 					 isoprotox,
291 #endif
292 					 NULL };
293 
294 static void printproto (struct protox *, char *);
295 static void usage (void);
296 static struct protox *name2protox (char *);
297 static struct protox *knownname (char *);
298 
299 static kvm_t *kvmd;
300 static char *nlistf = NULL, *memf = NULL;
301 
302 int	Aflag;		/* show addresses of protocol control block */
303 int	aflag;		/* show all sockets (including servers) */
304 int	bflag;		/* show i/f total bytes in/out */
305 int	dflag;		/* show i/f dropped packets */
306 int	gflag;		/* show group (multicast) routing or stats */
307 int	iflag;		/* show interfaces */
308 int	Lflag;		/* show size of listen queues */
309 int	mflag;		/* show memory stats */
310 int	numeric_addr;	/* show addresses numerically */
311 int	numeric_port;	/* show ports numerically */
312 static int pflag;	/* show given protocol */
313 int	rflag;		/* show routing tables (or routing stats) */
314 int	sflag;		/* show protocol statistics */
315 int	tflag;		/* show i/f watchdog timers */
316 int	Wflag;		/* wide display */
317 int	zflag;		/* zero stats */
318 
319 int	interval;	/* repeat interval for i/f stats */
320 
321 char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
322 int	unit;		/* unit number for above */
323 
324 int	af;		/* address family */
325 
326 int
327 main(argc, argv)
328 	int argc;
329 	char *argv[];
330 {
331 	register struct protox *tp = NULL;  /* for printing cblocks & stats */
332 	int ch;
333 
334 	af = AF_UNSPEC;
335 
336 	while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
337 		switch(ch) {
338 		case 'A':
339 			Aflag = 1;
340 			break;
341 		case 'a':
342 			aflag = 1;
343 			break;
344 		case 'b':
345 			bflag = 1;
346 			break;
347 		case 'd':
348 			dflag = 1;
349 			break;
350 		case 'f':
351 #ifdef NS
352 			if (strcmp(optarg, "ns") == 0)
353 				af = AF_NS;
354 			else
355 #endif
356 			if (strcmp(optarg, "ipx") == 0)
357 				af = AF_IPX;
358 			else if (strcmp(optarg, "inet") == 0)
359 				af = AF_INET;
360 #ifdef INET6
361 			else if (strcmp(optarg, "inet6") == 0)
362 				af = AF_INET6;
363 #endif /*INET6*/
364 #ifdef INET6
365 			else if (strcmp(optarg, "pfkey") == 0)
366 				af = PF_KEY;
367 #endif /*INET6*/
368 			else if (strcmp(optarg, "unix") == 0)
369 				af = AF_UNIX;
370 			else if (strcmp(optarg, "atalk") == 0)
371 				af = AF_APPLETALK;
372 			else if (strcmp(optarg, "ng") == 0
373 			    || strcmp(optarg, "netgraph") == 0)
374 				af = AF_NETGRAPH;
375 #ifdef ISO
376 			else if (strcmp(optarg, "iso") == 0)
377 				af = AF_ISO;
378 #endif
379 			else if (strcmp(optarg, "link") == 0)
380 				af = AF_LINK;
381 			else {
382 				errx(1, "%s: unknown address family", optarg);
383 			}
384 			break;
385 		case 'g':
386 			gflag = 1;
387 			break;
388 		case 'I': {
389 			char *cp;
390 
391 			iflag = 1;
392 			for (cp = interface = optarg; isalpha(*cp); cp++)
393 				continue;
394 			unit = atoi(cp);
395 			break;
396 		}
397 		case 'i':
398 			iflag = 1;
399 			break;
400 		case 'L':
401 			Lflag = 1;
402 			break;
403 		case 'M':
404 			memf = optarg;
405 			break;
406 		case 'm':
407 			mflag = 1;
408 			break;
409 		case 'N':
410 			nlistf = optarg;
411 			break;
412 		case 'n':
413 			numeric_addr = numeric_port = 1;
414 			break;
415 		case 'p':
416 			if ((tp = name2protox(optarg)) == NULL) {
417 				errx(1,
418 				     "%s: unknown or uninstrumented protocol",
419 				     optarg);
420 			}
421 			pflag = 1;
422 			break;
423 		case 'r':
424 			rflag = 1;
425 			break;
426 		case 's':
427 			++sflag;
428 			break;
429 		case 'S':
430 			numeric_addr = 1;
431 			break;
432 		case 't':
433 			tflag = 1;
434 			break;
435 		case 'u':
436 			af = AF_UNIX;
437 			break;
438 		case 'W':
439 		case 'l':
440 			Wflag = 1;
441 			break;
442 		case 'w':
443 			interval = atoi(optarg);
444 			iflag = 1;
445 			break;
446 		case 'z':
447 			zflag = 1;
448 			break;
449 		case '?':
450 		default:
451 			usage();
452 		}
453 	argv += optind;
454 	argc -= optind;
455 
456 #define	BACKWARD_COMPATIBILITY
457 #ifdef	BACKWARD_COMPATIBILITY
458 	if (*argv) {
459 		if (isdigit(**argv)) {
460 			interval = atoi(*argv);
461 			if (interval <= 0)
462 				usage();
463 			++argv;
464 			iflag = 1;
465 		}
466 		if (*argv) {
467 			nlistf = *argv;
468 			if (*++argv)
469 				memf = *argv;
470 		}
471 	}
472 #endif
473 
474 	/*
475 	 * Discard setgid privileges if not the running kernel so that bad
476 	 * guys can't print interesting stuff from kernel memory.
477 	 */
478 	if (nlistf != NULL || memf != NULL)
479 		setgid(getgid());
480 
481 	if (mflag) {
482 		if (memf != NULL) {
483 			if (kread(0, 0, 0) == 0)
484 				mbpr(nl[N_MBSTAT].n_value,
485 				    nl[N_MBTYPES].n_value,
486 				    nl[N_NMBCLUSTERS].n_value,
487 				    nl[N_NMBUFS].n_value);
488 		} else
489 			mbpr(0, 0, 0, 0);
490 		exit(0);
491 	}
492 #if 0
493 	/*
494 	 * Keep file descriptors open to avoid overhead
495 	 * of open/close on each call to get* routines.
496 	 */
497 	sethostent(1);
498 	setnetent(1);
499 #else
500 	/*
501 	 * This does not make sense any more with DNS being default over
502 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
503 	 * used for the queries, which is slower.
504 	 */
505 #endif
506 	if (iflag && !sflag) {
507 		kread(0, 0, 0);
508 		intpr(interval, nl[N_IFNET].n_value, NULL);
509 		exit(0);
510 	}
511 	if (rflag) {
512 		kread(0, 0, 0);
513 		if (sflag)
514 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
515 		else
516 			routepr(nl[N_RTREE].n_value);
517 		exit(0);
518 	}
519 	if (gflag) {
520 		kread(0, 0, 0);
521 		if (sflag) {
522 			if (af == AF_INET || af == AF_UNSPEC)
523 				mrt_stats(nl[N_MRTSTAT].n_value);
524 #ifdef INET6
525 			if (af == AF_INET6 || af == AF_UNSPEC)
526 				mrt6_stats(nl[N_MRT6STAT].n_value);
527 #endif
528 		} else {
529 			if (af == AF_INET || af == AF_UNSPEC)
530 				mroutepr(nl[N_MFCTABLE].n_value,
531 					 nl[N_VIFTABLE].n_value);
532 #ifdef INET6
533 			if (af == AF_INET6 || af == AF_UNSPEC)
534 				mroute6pr(nl[N_MF6CTABLE].n_value,
535 					  nl[N_MIF6TABLE].n_value);
536 #endif
537 		}
538 		exit(0);
539 	}
540 
541 	kread(0, 0, 0);
542 	if (tp) {
543 		printproto(tp, tp->pr_name);
544 		exit(0);
545 	}
546 	if (af == AF_INET || af == AF_UNSPEC)
547 		for (tp = protox; tp->pr_name; tp++)
548 			printproto(tp, tp->pr_name);
549 #ifdef INET6
550 	if (af == AF_INET6 || af == AF_UNSPEC)
551 		for (tp = ip6protox; tp->pr_name; tp++)
552 			printproto(tp, tp->pr_name);
553 #endif /*INET6*/
554 #ifdef IPSEC
555 	if (af == PF_KEY || af == AF_UNSPEC)
556 		for (tp = pfkeyprotox; tp->pr_name; tp++)
557 			printproto(tp, tp->pr_name);
558 #endif /*IPSEC*/
559 	if (af == AF_IPX || af == AF_UNSPEC) {
560 		kread(0, 0, 0);
561 		for (tp = ipxprotox; tp->pr_name; tp++)
562 			printproto(tp, tp->pr_name);
563 	}
564 	if (af == AF_APPLETALK || af == AF_UNSPEC)
565 		for (tp = atalkprotox; tp->pr_name; tp++)
566 			printproto(tp, tp->pr_name);
567 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
568 		for (tp = netgraphprotox; tp->pr_name; tp++)
569 			printproto(tp, tp->pr_name);
570 #ifdef NS
571 	if (af == AF_NS || af == AF_UNSPEC)
572 		for (tp = nsprotox; tp->pr_name; tp++)
573 			printproto(tp, tp->pr_name);
574 #endif
575 #ifdef ISO
576 	if (af == AF_ISO || af == AF_UNSPEC)
577 		for (tp = isoprotox; tp->pr_name; tp++)
578 			printproto(tp, tp->pr_name);
579 #endif
580 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
581 		unixpr();
582 	exit(0);
583 }
584 
585 /*
586  * Print out protocol statistics or control blocks (per sflag).
587  * If the interface was not specifically requested, and the symbol
588  * is not in the namelist, ignore this one.
589  */
590 static void
591 printproto(tp, name)
592 	register struct protox *tp;
593 	char *name;
594 {
595 	void (*pr)(u_long, char *, int);
596 	u_long off;
597 
598 	if (sflag) {
599 		if (iflag) {
600 			if (tp->pr_istats)
601 				intpr(interval, nl[N_IFNET].n_value,
602 				      tp->pr_istats);
603 			else if (pflag)
604 				printf("%s: no per-interface stats routine\n",
605 				    tp->pr_name);
606 			return;
607 		}
608 		else {
609 			pr = tp->pr_stats;
610 			if (!pr) {
611 				if (pflag)
612 					printf("%s: no stats routine\n",
613 					    tp->pr_name);
614 				return;
615 			}
616 			off = tp->pr_usesysctl ? tp->pr_usesysctl
617 				: nl[tp->pr_sindex].n_value;
618 		}
619 	} else {
620 		pr = tp->pr_cblocks;
621 		if (!pr) {
622 			if (pflag)
623 				printf("%s: no PCB routine\n", tp->pr_name);
624 			return;
625 		}
626 		off = tp->pr_usesysctl ? tp->pr_usesysctl
627 			: nl[tp->pr_index].n_value;
628 	}
629 	if (pr != NULL && (off || af != AF_UNSPEC))
630 		(*pr)(off, name, af);
631 }
632 
633 /*
634  * Read kernel memory, return 0 on success.
635  */
636 int
637 kread(u_long addr, char *buf, int size)
638 {
639 	if (kvmd == 0) {
640 		/*
641 		 * XXX.
642 		 */
643 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
644 		if (kvmd != NULL) {
645 			if (kvm_nlist(kvmd, nl) < 0) {
646 				if(nlistf)
647 					errx(1, "%s: kvm_nlist: %s", nlistf,
648 					     kvm_geterr(kvmd));
649 				else
650 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
651 			}
652 
653 			if (nl[0].n_type == 0) {
654 				if(nlistf)
655 					errx(1, "%s: no namelist", nlistf);
656 				else
657 					errx(1, "no namelist");
658 			}
659 		} else {
660 			warnx("kvm not available");
661 			return(-1);
662 		}
663 	}
664 	if (!buf)
665 		return (0);
666 	if (kvm_read(kvmd, addr, buf, size) != size) {
667 		warnx("%s", kvm_geterr(kvmd));
668 		return (-1);
669 	}
670 	return (0);
671 }
672 
673 char *
674 plural(int n)
675 {
676 	return (n != 1 ? "s" : "");
677 }
678 
679 char *
680 plurales(int n)
681 {
682 	return (n != 1 ? "es" : "");
683 }
684 
685 /*
686  * Find the protox for the given "well-known" name.
687  */
688 static struct protox *
689 knownname(char *name)
690 {
691 	struct protox **tpp, *tp;
692 
693 	for (tpp = protoprotox; *tpp; tpp++)
694 		for (tp = *tpp; tp->pr_name; tp++)
695 			if (strcmp(tp->pr_name, name) == 0)
696 				return (tp);
697 	return (NULL);
698 }
699 
700 /*
701  * Find the protox corresponding to name.
702  */
703 static struct protox *
704 name2protox(char *name)
705 {
706 	struct protox *tp;
707 	char **alias;			/* alias from p->aliases */
708 	struct protoent *p;
709 
710 	/*
711 	 * Try to find the name in the list of "well-known" names. If that
712 	 * fails, check if name is an alias for an Internet protocol.
713 	 */
714 	if ((tp = knownname(name)) != NULL)
715 		return (tp);
716 
717 	setprotoent(1);			/* make protocol lookup cheaper */
718 	while ((p = getprotoent()) != NULL) {
719 		/* assert: name not same as p->name */
720 		for (alias = p->p_aliases; *alias; alias++)
721 			if (strcmp(name, *alias) == 0) {
722 				endprotoent();
723 				return (knownname(p->p_name));
724 			}
725 	}
726 	endprotoent();
727 	return (NULL);
728 }
729 
730 static void
731 usage(void)
732 {
733 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
734 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
735 "               [-M core] [-N system]",
736 "       netstat -i | -I interface [-abdnt] [-f address_family]\n"
737 "               [-M core] [-N system]",
738 "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
739 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
740 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
741 "               [-M core] [-N system]",
742 "       netstat -m [-M core] [-N system]",
743 "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
744 "       netstat -rs [-s] [-M core] [-N system]",
745 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
746 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
747 	exit(1);
748 }
749