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