From 3fddef95af35202b43175fe6bb92f8be98dc7cee Mon Sep 17 00:00:00 2001 From: Hiroki Sato Date: Fri, 12 Jul 2013 17:11:30 +0000 Subject: [PATCH] Add -F fibnum option to specify an FIB number for -r flag. --- usr.bin/netstat/main.c | 12 ++++++++++-- usr.bin/netstat/netstat.1 | 23 +++++++++++++++++++---- usr.bin/netstat/netstat.h | 2 +- usr.bin/netstat/route.c | 14 ++++++++++---- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index d9c8334afaf1..249650a43104 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -363,10 +363,12 @@ main(int argc, char *argv[]) { struct protox *tp = NULL; /* for printing cblocks & stats */ int ch; + int fib = -1; + char *endptr; af = AF_UNSPEC; - while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:Qq:rSTsuWw:xz")) + while ((ch = getopt(argc, argv, "AaBbdF:f:ghI:iLlM:mN:np:Qq:rSTsuWw:xz")) != -1) switch(ch) { case 'A': @@ -384,6 +386,12 @@ main(int argc, char *argv[]) case 'd': dflag = 1; break; + case 'F': + fib = strtol(optarg, &endptr, 0); + if (*endptr != '\0' || + (fib == 0 && (errno == EINVAL || errno == ERANGE))) + errx(1, "%s: invalid fib", optarg); + break; case 'f': if (strcmp(optarg, "ipx") == 0) af = AF_IPX; @@ -571,7 +579,7 @@ main(int argc, char *argv[]) if (sflag) rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value); else - routepr(nl[N_RTREE].n_value); + routepr(nl[N_RTREE].n_value, fib); exit(0); } if (gflag) { diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1 index c52340a2f241..0a37994ca3b8 100644 --- a/usr.bin/netstat/netstat.1 +++ b/usr.bin/netstat/netstat.1 @@ -28,7 +28,7 @@ .\" @(#)netstat.1 8.8 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" -.Dd March 10, 2013 +.Dd May 17, 2013 .Dt NETSTAT 1 .Os .Sh NAME @@ -217,14 +217,29 @@ states. .Nm .Fl r .Op Fl AanW +.Op Fl F Ar fibnum .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system .Ek .Xc -Display the contents of all routing tables, -or a routing table for a particular -.Ar address_family . +Display the contents of routing tables. +When +.Fl f +is specified, a routing table for a particular +.Ar address_family +is displayed. +When +.Fl F +is specified, a routing table with the number +.Ar fibnum +is displayed. +If the specified +.Ar fibnum +is -1 or +.Fl F +is not specified, +the default routing table is displayed. If .Fl A is also present, diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 318fcbedfadb..02b8a17c309c 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -135,7 +135,7 @@ char *atalk_print(struct sockaddr *, int); char *atalk_print2(struct sockaddr *, struct sockaddr *, int); char *ipx_print(struct sockaddr *); char *ns_print(struct sockaddr *); -void routepr(u_long); +void routepr(u_long, int); void ipxprotopr(u_long, const char *, int, int); void spx_stats(u_long, const char *, int, int); diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c index dd91330bd190..1de12cc9fd5d 100644 --- a/usr.bin/netstat/route.c +++ b/usr.bin/netstat/route.c @@ -143,17 +143,20 @@ static void domask(char *, in_addr_t, u_long); * Print routing tables. */ void -routepr(u_long rtree) +routepr(u_long rtree, int fibnum) { struct radix_node_head **rnhp, *rnh, head; size_t intsize; - int fam, fibnum, numfibs; + int fam, numfibs; intsize = sizeof(int); - if (sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) + if (fibnum == -1 && + sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) fibnum = 0; if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) numfibs = 1; + if (fibnum < 0 || fibnum > numfibs - 1) + errx(EX_USAGE, "%d: invalid fib", fibnum); rt_tables = calloc(numfibs * (AF_MAX+1), sizeof(struct radix_node_head *)); if (rt_tables == NULL) @@ -166,7 +169,10 @@ routepr(u_long rtree) if (clock_gettime(CLOCK_UPTIME, &uptime) < 0) err(EX_OSERR, "clock_gettime() failed"); - printf("Routing tables\n"); + printf("Routing tables"); + if (fibnum) + printf(" (fib: %d)", fibnum); + printf("\n"); if (Aflag == 0 && NewTree) ntreestuff();