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 #define N_MBSTAT 38 149 { "_mbstat" }, 150 #define N_MBTYPES 39 151 { "_mbtypes" }, 152 #define N_NMBCLUSTERS 40 153 { "_nmbclusters" }, 154 #define N_NMBUFS 41 155 { "_nmbufs" }, 156 #define N_MBLIM 42 157 { "_mbuf_limit" }, 158 #define N_CLLIM 43 159 { "_clust_limit" }, 160 #define N_NCPUS 44 161 { "_smp_cpus" }, 162 #define N_PAGESZ 45 163 { "_pagesize" }, 164 #define N_MBPSTAT 46 165 { "_mb_statpcpu" }, 166 #define N_RTTRASH 47 167 { "_rttrash" }, 168 { "" }, 169 }; 170 171 struct protox { 172 u_char pr_index; /* index into nlist of cb head */ 173 u_char pr_sindex; /* index into nlist of stat block */ 174 u_char pr_wanted; /* 1 if wanted, 0 otherwise */ 175 void (*pr_cblocks)(u_long, char *, int); 176 /* control blocks printing routine */ 177 void (*pr_stats)(u_long, char *, int); 178 /* statistics printing routine */ 179 void (*pr_istats)(char *); /* per/if statistics printing routine */ 180 const char *pr_name; /* well-known name */ 181 int pr_usesysctl; /* true if we use sysctl, not kvm */ 182 } protox[] = { 183 { -1, -1, 1, protopr, 184 tcp_stats, NULL, "tcp", IPPROTO_TCP }, 185 { -1, -1, 1, protopr, 186 udp_stats, NULL, "udp", IPPROTO_UDP }, 187 { -1, -1, 1, protopr, 188 NULL, NULL, "divert",IPPROTO_DIVERT }, 189 { -1, -1, 1, protopr, 190 ip_stats, NULL, "ip", IPPROTO_RAW }, 191 { -1, -1, 1, protopr, 192 icmp_stats, NULL, "icmp", IPPROTO_ICMP }, 193 { -1, -1, 1, protopr, 194 igmp_stats, NULL, "igmp", IPPROTO_IGMP }, 195 #ifdef IPSEC 196 { -1, N_IPSECSTAT, 1, 0, 197 ipsec_stats, NULL, "ipsec", 0}, 198 #endif 199 { -1, -1, 1, 0, 200 bdg_stats, NULL, "bdg", 1 /* bridging... */ }, 201 { -1, -1, 0, 0, 202 0, NULL, 0 } 203 }; 204 205 #ifdef INET6 206 struct protox ip6protox[] = { 207 { -1, -1, 1, protopr, 208 tcp_stats, NULL, "tcp", IPPROTO_TCP }, 209 { -1, -1, 1, protopr, 210 udp_stats, NULL, "udp", IPPROTO_UDP }, 211 { -1, N_IP6STAT, 1, protopr, 212 ip6_stats, ip6_ifstats, "ip6", IPPROTO_RAW }, 213 { -1, N_ICMP6STAT, 1, protopr, 214 icmp6_stats, icmp6_ifstats, "icmp6",IPPROTO_ICMPV6 }, 215 #ifdef IPSEC 216 { -1, N_IPSEC6STAT, 1, 0, 217 ipsec_stats, NULL, "ipsec6",0 }, 218 #endif 219 #ifdef notyet 220 { -1, N_PIM6STAT, 1, 0, 221 pim6_stats, NULL, "pim6", 0 }, 222 #endif 223 { -1, -1, 1, 0, 224 rip6_stats, NULL, "rip6", 0 }, 225 { -1, -1, 1, 0, 226 bdg_stats, NULL, "bdg", 1 /* bridging... */ }, 227 { -1, -1, 0, 0, 228 0, NULL, 0, 0 } 229 }; 230 #endif /*INET6*/ 231 232 #ifdef IPSEC 233 struct protox pfkeyprotox[] = { 234 { -1, N_PFKEYSTAT, 1, 0, 235 pfkey_stats, NULL, "pfkey", 0 }, 236 { -1, -1, 0, 0, 237 0, NULL, 0, 0 } 238 }; 239 #endif 240 241 struct protox atalkprotox[] = { 242 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr, 243 ddp_stats, NULL, "ddp" }, 244 { -1, -1, 0, 0, 245 0, NULL, 0 } 246 }; 247 248 struct protox netgraphprotox[] = { 249 { N_NGSOCKS, -1, 1, netgraphprotopr, 250 NULL, NULL, "ctrl" }, 251 { N_NGSOCKS, -1, 1, netgraphprotopr, 252 NULL, NULL, "data" }, 253 { -1, NULL, 0, 0, 254 0, NULL, 0 } 255 }; 256 257 struct protox ipxprotox[] = { 258 { N_IPX, N_IPXSTAT, 1, ipxprotopr, 259 ipx_stats, NULL, "ipx", 0 }, 260 { N_IPX, N_SPXSTAT, 1, ipxprotopr, 261 spx_stats, NULL, "spx", 0 }, 262 { -1, -1, 0, 0, 263 0, NULL, 0, 0 } 264 }; 265 266 #ifdef NS 267 struct protox nsprotox[] = { 268 { N_IDP, N_IDPSTAT, 1, nsprotopr, 269 idp_stats, NULL, "idp" }, 270 { N_IDP, N_SPPSTAT, 1, nsprotopr, 271 spp_stats, NULL, "spp" }, 272 { -1, N_NSERR, 1, 0, 273 nserr_stats, NULL, "ns_err" }, 274 { -1, -1, 0, 0, 275 0, NULL, 0 } 276 }; 277 #endif 278 279 #ifdef ISO 280 struct protox isoprotox[] = { 281 { ISO_TP, N_TPSTAT, 1, iso_protopr, 282 tp_stats, NULL, "tp" }, 283 { N_CLTP, N_CLTPSTAT, 1, iso_protopr, 284 cltp_stats, NULL, "cltp" }, 285 { -1, N_CLNPSTAT, 1, 0, 286 clnp_stats, NULL, "clnp"}, 287 { -1, N_ESISSTAT, 1, 0, 288 esis_stats, NULL, "esis"}, 289 { -1, -1, 0, 0, 290 0, NULL, 0 } 291 }; 292 #endif 293 294 struct protox *protoprotox[] = { 295 protox, 296 #ifdef INET6 297 ip6protox, 298 #endif 299 #ifdef IPSEC 300 pfkeyprotox, 301 #endif 302 ipxprotox, atalkprotox, 303 #ifdef NS 304 nsprotox, 305 #endif 306 #ifdef ISO 307 isoprotox, 308 #endif 309 NULL }; 310 311 static void printproto (struct protox *, const char *); 312 static void usage (void); 313 static struct protox *name2protox (char *); 314 static struct protox *knownname (char *); 315 316 static kvm_t *kvmd; 317 static char *nlistf = NULL, *memf = NULL; 318 319 int Aflag; /* show addresses of protocol control block */ 320 int aflag; /* show all sockets (including servers) */ 321 int bflag; /* show i/f total bytes in/out */ 322 int dflag; /* show i/f dropped packets */ 323 int gflag; /* show group (multicast) routing or stats */ 324 int iflag; /* show interfaces */ 325 int Lflag; /* show size of listen queues */ 326 int mflag; /* show memory stats */ 327 int numeric_addr; /* show addresses numerically */ 328 int numeric_port; /* show ports numerically */ 329 static int pflag; /* show given protocol */ 330 int rflag; /* show routing tables (or routing stats) */ 331 int sflag; /* show protocol statistics */ 332 int tflag; /* show i/f watchdog timers */ 333 int Wflag; /* wide display */ 334 int zflag; /* zero stats */ 335 336 int interval; /* repeat interval for i/f stats */ 337 338 char *interface; /* desired i/f for stats, or NULL for all i/fs */ 339 int unit; /* unit number for above */ 340 341 int af; /* address family */ 342 343 int 344 main(argc, argv) 345 int argc; 346 char *argv[]; 347 { 348 register struct protox *tp = NULL; /* for printing cblocks & stats */ 349 int ch; 350 351 af = AF_UNSPEC; 352 353 while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1) 354 switch(ch) { 355 case 'A': 356 Aflag = 1; 357 break; 358 case 'a': 359 aflag = 1; 360 break; 361 case 'b': 362 bflag = 1; 363 break; 364 case 'd': 365 dflag = 1; 366 break; 367 case 'f': 368 #ifdef NS 369 if (strcmp(optarg, "ns") == 0) 370 af = AF_NS; 371 else 372 #endif 373 if (strcmp(optarg, "ipx") == 0) 374 af = AF_IPX; 375 else if (strcmp(optarg, "inet") == 0) 376 af = AF_INET; 377 #ifdef INET6 378 else if (strcmp(optarg, "inet6") == 0) 379 af = AF_INET6; 380 #endif /*INET6*/ 381 #ifdef INET6 382 else if (strcmp(optarg, "pfkey") == 0) 383 af = PF_KEY; 384 #endif /*INET6*/ 385 else if (strcmp(optarg, "unix") == 0) 386 af = AF_UNIX; 387 else if (strcmp(optarg, "atalk") == 0) 388 af = AF_APPLETALK; 389 else if (strcmp(optarg, "ng") == 0 390 || strcmp(optarg, "netgraph") == 0) 391 af = AF_NETGRAPH; 392 #ifdef ISO 393 else if (strcmp(optarg, "iso") == 0) 394 af = AF_ISO; 395 #endif 396 else if (strcmp(optarg, "link") == 0) 397 af = AF_LINK; 398 else { 399 errx(1, "%s: unknown address family", optarg); 400 } 401 break; 402 case 'g': 403 gflag = 1; 404 break; 405 case 'I': { 406 char *cp; 407 408 iflag = 1; 409 for (cp = interface = optarg; isalpha(*cp); cp++) 410 continue; 411 unit = atoi(cp); 412 break; 413 } 414 case 'i': 415 iflag = 1; 416 break; 417 case 'L': 418 Lflag = 1; 419 break; 420 case 'M': 421 memf = optarg; 422 break; 423 case 'm': 424 mflag = 1; 425 break; 426 case 'N': 427 nlistf = optarg; 428 break; 429 case 'n': 430 numeric_addr = numeric_port = 1; 431 break; 432 case 'p': 433 if ((tp = name2protox(optarg)) == NULL) { 434 errx(1, 435 "%s: unknown or uninstrumented protocol", 436 optarg); 437 } 438 pflag = 1; 439 break; 440 case 'r': 441 rflag = 1; 442 break; 443 case 's': 444 ++sflag; 445 break; 446 case 'S': 447 numeric_addr = 1; 448 break; 449 case 't': 450 tflag = 1; 451 break; 452 case 'u': 453 af = AF_UNIX; 454 break; 455 case 'W': 456 case 'l': 457 Wflag = 1; 458 break; 459 case 'w': 460 interval = atoi(optarg); 461 iflag = 1; 462 break; 463 case 'z': 464 zflag = 1; 465 break; 466 case '?': 467 default: 468 usage(); 469 } 470 argv += optind; 471 argc -= optind; 472 473 #define BACKWARD_COMPATIBILITY 474 #ifdef BACKWARD_COMPATIBILITY 475 if (*argv) { 476 if (isdigit(**argv)) { 477 interval = atoi(*argv); 478 if (interval <= 0) 479 usage(); 480 ++argv; 481 iflag = 1; 482 } 483 if (*argv) { 484 nlistf = *argv; 485 if (*++argv) 486 memf = *argv; 487 } 488 } 489 #endif 490 491 /* 492 * Discard setgid privileges if not the running kernel so that bad 493 * guys can't print interesting stuff from kernel memory. 494 */ 495 if (nlistf != NULL || memf != NULL) 496 setgid(getgid()); 497 498 if (mflag) { 499 if (memf != NULL) { 500 if (kread(0, 0, 0) == 0) 501 mbpr(nl[N_MBSTAT].n_value, 502 nl[N_MBTYPES].n_value, 503 nl[N_NMBCLUSTERS].n_value, 504 nl[N_NMBUFS].n_value, 505 nl[N_MBLIM].n_value, 506 nl[N_CLLIM].n_value, 507 nl[N_NCPUS].n_value, 508 nl[N_PAGESZ].n_value, 509 nl[N_MBPSTAT].n_value); 510 } else 511 mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0); 512 exit(0); 513 } 514 #if 0 515 /* 516 * Keep file descriptors open to avoid overhead 517 * of open/close on each call to get* routines. 518 */ 519 sethostent(1); 520 setnetent(1); 521 #else 522 /* 523 * This does not make sense any more with DNS being default over 524 * the files. Doing a setXXXXent(1) causes a tcp connection to be 525 * used for the queries, which is slower. 526 */ 527 #endif 528 if (iflag && !sflag) { 529 kread(0, 0, 0); 530 intpr(interval, nl[N_IFNET].n_value, NULL); 531 exit(0); 532 } 533 if (rflag) { 534 kread(0, 0, 0); 535 if (sflag) 536 rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value); 537 else 538 routepr(nl[N_RTREE].n_value); 539 exit(0); 540 } 541 if (gflag) { 542 kread(0, 0, 0); 543 if (sflag) { 544 if (af == AF_INET || af == AF_UNSPEC) 545 mrt_stats(nl[N_MRTSTAT].n_value); 546 #ifdef INET6 547 if (af == AF_INET6 || af == AF_UNSPEC) 548 mrt6_stats(nl[N_MRT6STAT].n_value); 549 #endif 550 } else { 551 if (af == AF_INET || af == AF_UNSPEC) 552 mroutepr(nl[N_MFCTABLE].n_value, 553 nl[N_VIFTABLE].n_value); 554 #ifdef INET6 555 if (af == AF_INET6 || af == AF_UNSPEC) 556 mroute6pr(nl[N_MF6CTABLE].n_value, 557 nl[N_MIF6TABLE].n_value); 558 #endif 559 } 560 exit(0); 561 } 562 563 kread(0, 0, 0); 564 if (tp) { 565 printproto(tp, tp->pr_name); 566 exit(0); 567 } 568 if (af == AF_INET || af == AF_UNSPEC) 569 for (tp = protox; tp->pr_name; tp++) 570 printproto(tp, tp->pr_name); 571 #ifdef INET6 572 if (af == AF_INET6 || af == AF_UNSPEC) 573 for (tp = ip6protox; tp->pr_name; tp++) 574 printproto(tp, tp->pr_name); 575 #endif /*INET6*/ 576 #ifdef IPSEC 577 if (af == PF_KEY || af == AF_UNSPEC) 578 for (tp = pfkeyprotox; tp->pr_name; tp++) 579 printproto(tp, tp->pr_name); 580 #endif /*IPSEC*/ 581 if (af == AF_IPX || af == AF_UNSPEC) { 582 kread(0, 0, 0); 583 for (tp = ipxprotox; tp->pr_name; tp++) 584 printproto(tp, tp->pr_name); 585 } 586 if (af == AF_APPLETALK || af == AF_UNSPEC) 587 for (tp = atalkprotox; tp->pr_name; tp++) 588 printproto(tp, tp->pr_name); 589 if (af == AF_NETGRAPH || af == AF_UNSPEC) 590 for (tp = netgraphprotox; tp->pr_name; tp++) 591 printproto(tp, tp->pr_name); 592 #ifdef NS 593 if (af == AF_NS || af == AF_UNSPEC) 594 for (tp = nsprotox; tp->pr_name; tp++) 595 printproto(tp, tp->pr_name); 596 #endif 597 #ifdef ISO 598 if (af == AF_ISO || af == AF_UNSPEC) 599 for (tp = isoprotox; tp->pr_name; tp++) 600 printproto(tp, tp->pr_name); 601 #endif 602 if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag) 603 unixpr(); 604 exit(0); 605 } 606 607 /* 608 * Print out protocol statistics or control blocks (per sflag). 609 * If the interface was not specifically requested, and the symbol 610 * is not in the namelist, ignore this one. 611 */ 612 static void 613 printproto(tp, name) 614 register struct protox *tp; 615 const char *name; 616 { 617 void (*pr)(u_long, char *, int); 618 u_long off; 619 620 if (sflag) { 621 if (iflag) { 622 if (tp->pr_istats) 623 intpr(interval, nl[N_IFNET].n_value, 624 tp->pr_istats); 625 else if (pflag) 626 printf("%s: no per-interface stats routine\n", 627 tp->pr_name); 628 return; 629 } 630 else { 631 pr = tp->pr_stats; 632 if (!pr) { 633 if (pflag) 634 printf("%s: no stats routine\n", 635 tp->pr_name); 636 return; 637 } 638 off = tp->pr_usesysctl ? tp->pr_usesysctl 639 : nl[tp->pr_sindex].n_value; 640 } 641 } else { 642 pr = tp->pr_cblocks; 643 if (!pr) { 644 if (pflag) 645 printf("%s: no PCB routine\n", tp->pr_name); 646 return; 647 } 648 off = tp->pr_usesysctl ? tp->pr_usesysctl 649 : nl[tp->pr_index].n_value; 650 } 651 if (pr != NULL && (off || af != AF_UNSPEC)) 652 (*pr)(off, name, af); 653 } 654 655 /* 656 * Read kernel memory, return 0 on success. 657 */ 658 int 659 kread(u_long addr, char *buf, int size) 660 { 661 if (kvmd == 0) { 662 /* 663 * XXX. 664 */ 665 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 666 setgid(getgid()); 667 if (kvmd != NULL) { 668 if (kvm_nlist(kvmd, nl) < 0) { 669 if(nlistf) 670 errx(1, "%s: kvm_nlist: %s", nlistf, 671 kvm_geterr(kvmd)); 672 else 673 errx(1, "kvm_nlist: %s", kvm_geterr(kvmd)); 674 } 675 676 if (nl[0].n_type == 0) { 677 if(nlistf) 678 errx(1, "%s: no namelist", nlistf); 679 else 680 errx(1, "no namelist"); 681 } 682 } else { 683 warnx("kvm not available"); 684 return(-1); 685 } 686 } 687 if (!buf) 688 return (0); 689 if (kvm_read(kvmd, addr, buf, size) != size) { 690 warnx("%s", kvm_geterr(kvmd)); 691 return (-1); 692 } 693 return (0); 694 } 695 696 char * 697 plural(int n) 698 { 699 return (n != 1 ? "s" : ""); 700 } 701 702 char * 703 plurales(int n) 704 { 705 return (n != 1 ? "es" : ""); 706 } 707 708 /* 709 * Find the protox for the given "well-known" name. 710 */ 711 static struct protox * 712 knownname(char *name) 713 { 714 struct protox **tpp, *tp; 715 716 for (tpp = protoprotox; *tpp; tpp++) 717 for (tp = *tpp; tp->pr_name; tp++) 718 if (strcmp(tp->pr_name, name) == 0) 719 return (tp); 720 return (NULL); 721 } 722 723 /* 724 * Find the protox corresponding to name. 725 */ 726 static struct protox * 727 name2protox(char *name) 728 { 729 struct protox *tp; 730 char **alias; /* alias from p->aliases */ 731 struct protoent *p; 732 733 /* 734 * Try to find the name in the list of "well-known" names. If that 735 * fails, check if name is an alias for an Internet protocol. 736 */ 737 if ((tp = knownname(name)) != NULL) 738 return (tp); 739 740 setprotoent(1); /* make protocol lookup cheaper */ 741 while ((p = getprotoent()) != NULL) { 742 /* assert: name not same as p->name */ 743 for (alias = p->p_aliases; *alias; alias++) 744 if (strcmp(name, *alias) == 0) { 745 endprotoent(); 746 return (knownname(p->p_name)); 747 } 748 } 749 endprotoent(); 750 return (NULL); 751 } 752 753 static void 754 usage(void) 755 { 756 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 757 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n" 758 " [-M core] [-N system]", 759 " netstat -i | -I interface [-abdnt] [-f address_family]\n" 760 " [-M core] [-N system]", 761 " netstat -w wait [-I interface] [-d] [-M core] [-N system]", 762 " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]", 763 " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n" 764 " [-M core] [-N system]", 765 " netstat -m [-M core] [-N system]", 766 " netstat -r [-AanW] [-f address_family] [-M core] [-N system]", 767 " netstat -rs [-s] [-M core] [-N system]", 768 " netstat -g [-W] [-f address_family] [-M core] [-N system]", 769 " netstat -gs [-s] [-f address_family] [-M core] [-N system]"); 770 exit(1); 771 } 772