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