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