Kernel tree reorganization stage 2: Major cvs repository work.
[dragonfly.git] / usr.bin / netstat / main.c
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.3 2003/08/08 04:18:43 dillon 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,             0,      0,
209           0,            NULL,           0,      0 }
210 };
211 #endif /*INET6*/
212
213 #ifdef IPSEC
214 struct protox pfkeyprotox[] = {
215         { -1,           N_PFKEYSTAT,    1,      0,
216           pfkey_stats,  NULL,           "pfkey", 0 },
217         { -1,           -1,             0,      0,
218           0,            NULL,           0,      0 }
219 };
220 #endif
221
222 struct protox atalkprotox[] = {
223         { N_DDPCB,      N_DDPSTAT,      1,      atalkprotopr,
224           ddp_stats,    NULL,           "ddp" },
225         { -1,           -1,             0,      0,
226           0,            NULL,           0 }
227 };
228
229 struct protox netgraphprotox[] = {
230         { N_NGSOCKS,    -1,             1,      netgraphprotopr,
231           NULL,         NULL,           "ctrl" },
232         { N_NGSOCKS,    -1,             1,      netgraphprotopr,
233           NULL,         NULL,           "data" },
234         { -1,           NULL,           0,      0,
235           0,            NULL,           0 }
236 };
237
238 struct protox ipxprotox[] = {
239         { N_IPX,        N_IPXSTAT,      1,      ipxprotopr,
240           ipx_stats,    NULL,           "ipx",  0 },
241         { N_IPX,        N_SPXSTAT,      1,      ipxprotopr,
242           spx_stats,    NULL,           "spx",  0 },
243         { -1,           -1,             0,      0,
244           0,            NULL,           0,      0 }
245 };
246
247 #ifdef NS
248 struct protox nsprotox[] = {
249         { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
250           idp_stats,    NULL,           "idp" },
251         { N_IDP,        N_SPPSTAT,      1,      nsprotopr,
252           spp_stats,    NULL,           "spp" },
253         { -1,           N_NSERR,        1,      0,
254           nserr_stats,  NULL,           "ns_err" },
255         { -1,           -1,             0,      0,
256           0,            NULL,           0 }
257 };
258 #endif
259
260 #ifdef ISO
261 struct protox isoprotox[] = {
262         { ISO_TP,       N_TPSTAT,       1,      iso_protopr,
263           tp_stats,     NULL,           "tp" },
264         { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
265           cltp_stats,   NULL,           "cltp" },
266         { -1,           N_CLNPSTAT,     1,       0,
267           clnp_stats,   NULL,           "clnp"},
268         { -1,           N_ESISSTAT,     1,       0,
269           esis_stats,   NULL,           "esis"},
270         { -1,           -1,             0,      0,
271           0,            NULL,           0 }
272 };
273 #endif
274
275 struct protox *protoprotox[] = {
276                                          protox,
277 #ifdef INET6
278                                          ip6protox,
279 #endif
280 #ifdef IPSEC
281                                          pfkeyprotox,
282 #endif
283                                          ipxprotox, atalkprotox,
284 #ifdef NS
285                                          nsprotox, 
286 #endif
287 #ifdef ISO
288                                          isoprotox, 
289 #endif
290                                          NULL };
291
292 static void printproto (struct protox *, char *);
293 static void usage (void);
294 static struct protox *name2protox (char *);
295 static struct protox *knownname (char *);
296
297 static kvm_t *kvmd;
298 static char *nlistf = NULL, *memf = NULL;
299
300 int     Aflag;          /* show addresses of protocol control block */
301 int     aflag;          /* show all sockets (including servers) */
302 int     bflag;          /* show i/f total bytes in/out */
303 int     dflag;          /* show i/f dropped packets */
304 int     gflag;          /* show group (multicast) routing or stats */
305 int     iflag;          /* show interfaces */
306 int     Lflag;          /* show size of listen queues */
307 int     mflag;          /* show memory stats */
308 int     numeric_addr;   /* show addresses numerically */
309 int     numeric_port;   /* show ports numerically */
310 static int pflag;       /* show given protocol */
311 int     rflag;          /* show routing tables (or routing stats) */
312 int     sflag;          /* show protocol statistics */
313 int     tflag;          /* show i/f watchdog timers */
314 int     Wflag;          /* wide display */
315 int     zflag;          /* zero stats */
316
317 int     interval;       /* repeat interval for i/f stats */
318
319 char    *interface;     /* desired i/f for stats, or NULL for all i/fs */
320 int     unit;           /* unit number for above */
321
322 int     af;             /* address family */
323
324 int
325 main(argc, argv)
326         int argc;
327         char *argv[];
328 {
329         register struct protox *tp = NULL;  /* for printing cblocks & stats */
330         int ch;
331
332         af = AF_UNSPEC;
333
334         while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
335                 switch(ch) {
336                 case 'A':
337                         Aflag = 1;
338                         break;
339                 case 'a':
340                         aflag = 1;
341                         break;
342                 case 'b':
343                         bflag = 1;
344                         break;
345                 case 'd':
346                         dflag = 1;
347                         break;
348                 case 'f':
349 #ifdef NS
350                         if (strcmp(optarg, "ns") == 0)
351                                 af = AF_NS;
352                         else
353 #endif
354                         if (strcmp(optarg, "ipx") == 0)
355                                 af = AF_IPX;
356                         else if (strcmp(optarg, "inet") == 0)
357                                 af = AF_INET;
358 #ifdef INET6
359                         else if (strcmp(optarg, "inet6") == 0)
360                                 af = AF_INET6;
361 #endif /*INET6*/
362 #ifdef INET6
363                         else if (strcmp(optarg, "pfkey") == 0)
364                                 af = PF_KEY;
365 #endif /*INET6*/
366                         else if (strcmp(optarg, "unix") == 0)
367                                 af = AF_UNIX;
368                         else if (strcmp(optarg, "atalk") == 0)
369                                 af = AF_APPLETALK;
370                         else if (strcmp(optarg, "ng") == 0
371                             || strcmp(optarg, "netgraph") == 0)
372                                 af = AF_NETGRAPH;
373 #ifdef ISO
374                         else if (strcmp(optarg, "iso") == 0)
375                                 af = AF_ISO;
376 #endif
377                         else if (strcmp(optarg, "link") == 0)
378                                 af = AF_LINK;
379                         else {
380                                 errx(1, "%s: unknown address family", optarg);
381                         }
382                         break;
383                 case 'g':
384                         gflag = 1;
385                         break;
386                 case 'I': {
387                         char *cp;
388
389                         iflag = 1;
390                         for (cp = interface = optarg; isalpha(*cp); cp++)
391                                 continue;
392                         unit = atoi(cp);
393                         break;
394                 }
395                 case 'i':
396                         iflag = 1;
397                         break;
398                 case 'L':
399                         Lflag = 1;
400                         break;
401                 case 'M':
402                         memf = optarg;
403                         break;
404                 case 'm':
405                         mflag = 1;
406                         break;
407                 case 'N':
408                         nlistf = optarg;
409                         break;
410                 case 'n':
411                         numeric_addr = numeric_port = 1;
412                         break;
413                 case 'p':
414                         if ((tp = name2protox(optarg)) == NULL) {
415                                 errx(1, 
416                                      "%s: unknown or uninstrumented protocol",
417                                      optarg);
418                         }
419                         pflag = 1;
420                         break;
421                 case 'r':
422                         rflag = 1;
423                         break;
424                 case 's':
425                         ++sflag;
426                         break;
427                 case 'S':
428                         numeric_addr = 1;
429                         break;
430                 case 't':
431                         tflag = 1;
432                         break;
433                 case 'u':
434                         af = AF_UNIX;
435                         break;
436                 case 'W':
437                 case 'l':
438                         Wflag = 1;
439                         break;
440                 case 'w':
441                         interval = atoi(optarg);
442                         iflag = 1;
443                         break;
444                 case 'z':
445                         zflag = 1;
446                         break;
447                 case '?':
448                 default:
449                         usage();
450                 }
451         argv += optind;
452         argc -= optind;
453
454 #define BACKWARD_COMPATIBILITY
455 #ifdef  BACKWARD_COMPATIBILITY
456         if (*argv) {
457                 if (isdigit(**argv)) {
458                         interval = atoi(*argv);
459                         if (interval <= 0)
460                                 usage();
461                         ++argv;
462                         iflag = 1;
463                 }
464                 if (*argv) {
465                         nlistf = *argv;
466                         if (*++argv)
467                                 memf = *argv;
468                 }
469         }
470 #endif
471
472         /*
473          * Discard setgid privileges if not the running kernel so that bad
474          * guys can't print interesting stuff from kernel memory.
475          */
476         if (nlistf != NULL || memf != NULL)
477                 setgid(getgid());
478
479         if (mflag) {
480                 if (memf != NULL) {
481                         if (kread(0, 0, 0) == 0)
482                                 mbpr(nl[N_MBSTAT].n_value,
483                                     nl[N_MBTYPES].n_value,
484                                     nl[N_NMBCLUSTERS].n_value,
485                                     nl[N_NMBUFS].n_value);
486                 } else
487                         mbpr(0, 0, 0, 0);
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 && !sflag) {
505                 kread(0, 0, 0);
506                 intpr(interval, nl[N_IFNET].n_value, NULL);
507                 exit(0);
508         }
509         if (rflag) {
510                 kread(0, 0, 0);
511                 if (sflag)
512                         rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
513                 else
514                         routepr(nl[N_RTREE].n_value);
515                 exit(0);
516         }
517         if (gflag) {
518                 kread(0, 0, 0);
519                 if (sflag) {
520                         if (af == AF_INET || af == AF_UNSPEC)
521                                 mrt_stats(nl[N_MRTSTAT].n_value);
522 #ifdef INET6
523                         if (af == AF_INET6 || af == AF_UNSPEC)
524                                 mrt6_stats(nl[N_MRT6STAT].n_value);
525 #endif
526                 } else {
527                         if (af == AF_INET || af == AF_UNSPEC)
528                                 mroutepr(nl[N_MFCTABLE].n_value,
529                                          nl[N_VIFTABLE].n_value);
530 #ifdef INET6
531                         if (af == AF_INET6 || af == AF_UNSPEC)
532                                 mroute6pr(nl[N_MF6CTABLE].n_value,
533                                           nl[N_MIF6TABLE].n_value);
534 #endif
535                 }
536                 exit(0);
537         }
538
539         kread(0, 0, 0);
540         if (tp) {
541                 printproto(tp, tp->pr_name);
542                 exit(0);
543         }
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)(u_long, char *, int);
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                         else if (pflag)
602                                 printf("%s: no per-interface stats routine\n",
603                                     tp->pr_name);
604                         return;
605                 }
606                 else {
607                         pr = tp->pr_stats;
608                         if (!pr) {
609                                 if (pflag)
610                                         printf("%s: no stats routine\n",
611                                             tp->pr_name);
612                                 return;
613                         }
614                         off = tp->pr_usesysctl ? tp->pr_usesysctl 
615                                 : nl[tp->pr_sindex].n_value;
616                 }
617         } else {
618                 pr = tp->pr_cblocks;
619                 if (!pr) {
620                         if (pflag)
621                                 printf("%s: no PCB routine\n", tp->pr_name);
622                         return;
623                 }
624                 off = tp->pr_usesysctl ? tp->pr_usesysctl
625                         : nl[tp->pr_index].n_value;
626         }
627         if (pr != NULL && (off || af != AF_UNSPEC))
628                 (*pr)(off, name, af);
629 }
630
631 /*
632  * Read kernel memory, return 0 on success.
633  */
634 int
635 kread(u_long addr, char *buf, int size)
636 {
637         if (kvmd == 0) {
638                 /*
639                  * XXX.
640                  */
641                 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
642                 if (kvmd != NULL) {
643                         if (kvm_nlist(kvmd, nl) < 0) {
644                                 if(nlistf)
645                                         errx(1, "%s: kvm_nlist: %s", nlistf,
646                                              kvm_geterr(kvmd));
647                                 else
648                                         errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
649                         }
650
651                         if (nl[0].n_type == 0) {
652                                 if(nlistf)
653                                         errx(1, "%s: no namelist", nlistf);
654                                 else
655                                         errx(1, "no namelist");
656                         }
657                 } else {
658                         warnx("kvm not available");
659                         return(-1);
660                 }
661         }
662         if (!buf)
663                 return (0);
664         if (kvm_read(kvmd, addr, buf, size) != size) {
665                 warnx("%s", kvm_geterr(kvmd));
666                 return (-1);
667         }
668         return (0);
669 }
670
671 char *
672 plural(int n)
673 {
674         return (n != 1 ? "s" : "");
675 }
676
677 char *
678 plurales(int n)
679 {
680         return (n != 1 ? "es" : "");
681 }
682
683 /*
684  * Find the protox for the given "well-known" name.
685  */
686 static struct protox *
687 knownname(char *name)
688 {
689         struct protox **tpp, *tp;
690
691         for (tpp = protoprotox; *tpp; tpp++)
692                 for (tp = *tpp; tp->pr_name; tp++)
693                         if (strcmp(tp->pr_name, name) == 0)
694                                 return (tp);
695         return (NULL);
696 }
697
698 /*
699  * Find the protox corresponding to name.
700  */
701 static struct protox *
702 name2protox(char *name)
703 {
704         struct protox *tp;
705         char **alias;                   /* alias from p->aliases */
706         struct protoent *p;
707
708         /*
709          * Try to find the name in the list of "well-known" names. If that
710          * fails, check if name is an alias for an Internet protocol.
711          */
712         if ((tp = knownname(name)) != NULL)
713                 return (tp);
714
715         setprotoent(1);                 /* make protocol lookup cheaper */
716         while ((p = getprotoent()) != NULL) {
717                 /* assert: name not same as p->name */
718                 for (alias = p->p_aliases; *alias; alias++)
719                         if (strcmp(name, *alias) == 0) {
720                                 endprotoent();
721                                 return (knownname(p->p_name));
722                         }
723         }
724         endprotoent();
725         return (NULL);
726 }
727
728 static void
729 usage(void)
730 {
731         (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
732 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
733 "               [-M core] [-N system]",
734 "       netstat -i | -I interface [-abdnt] [-f address_family]\n"
735 "               [-M core] [-N system]",
736 "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
737 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
738 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
739 "               [-M core] [-N system]",
740 "       netstat -m [-M core] [-N system]",
741 "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
742 "       netstat -rs [-s] [-M core] [-N system]",
743 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
744 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
745         exit(1);
746 }