diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2010-10-11 12:33:37 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2010-10-11 12:33:37 +0000 |
commit | bd11bb0aa24822c431f918f895b03189871a24af (patch) | |
tree | bb42efa6f988f1df789a37797d2a2e7ef9f3d3e1 /usr.bin | |
parent | 53f5bf14f05d657ca488d63435f48e9b3990ea1c (diff) |
Do similar changes to netstat as done to route(8). Try to show the
most expected rtable when doing a netstat -rn.
OK henning@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/netstat/main.c | 41 | ||||
-rw-r--r-- | usr.bin/netstat/netstat.h | 4 | ||||
-rw-r--r-- | usr.bin/netstat/show.c | 17 |
3 files changed, 48 insertions, 14 deletions
diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 07803db6911..2ddd0d64cc7 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.85 2010/07/14 23:44:41 dhill Exp $ */ +/* $OpenBSD: main.c,v 1.86 2010/10/11 12:33:36 claudio Exp $ */ /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */ /* @@ -34,6 +34,7 @@ #include <sys/file.h> #include <sys/protosw.h> #include <sys/socket.h> +#include <sys/sysctl.h> #include <net/route.h> #include <netinet/in.h> @@ -144,6 +145,8 @@ static void printproto(struct protox *, char *, int); static void usage(void); static struct protox *name2protox(char *); static struct protox *knownname(char *); +u_int gettable(const char *); + kvm_t *kvmd; @@ -161,6 +164,7 @@ main(int argc, char *argv[]) gid_t gid; u_long pcbaddr = 0; u_int tableid = 0; + int Tflag = 0; int repeatcount = 0; af = AF_UNSPEC; @@ -267,9 +271,8 @@ main(int argc, char *argv[]) ++sflag; break; case 'T': - tableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr); - if (errstr) - errx(1, "invalid table id: %s", errstr); + Tflag = 1; + tableid = gettable(optarg); break; case 't': tflag = 1; @@ -390,7 +393,7 @@ main(int argc, char *argv[]) nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value, tableid); else - p_rttables(af, tableid); + p_rttables(af, tableid, Tflag); exit(0); } if (gflag) { @@ -547,3 +550,31 @@ usage(void) __progname, __progname, __progname); exit(1); } + +u_int +gettable(const char *s) +{ + const char *errstr; + struct rt_tableinfo info; + int mib[6]; + size_t len; + u_int tableid; + + tableid = strtonum(s, 0, RT_TABLEID_MAX, &errstr); + if (errstr) + errx(1, "invalid table id: %s", errstr); + + mib[0] = CTL_NET; + mib[1] = AF_ROUTE; + mib[2] = 0; + mib[3] = 0; + mib[4] = NET_RT_TABLE; + mib[5] = tableid; + + len = sizeof(info); + if (sysctl(mib, 6, &info, &len, NULL, 0) == -1) + err(1, "routing table %i", tableid); + + return (tableid); +} + diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 77c90c26cf2..9aecd55bd16 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -1,4 +1,4 @@ -/* $OpenBSD: netstat.h,v 1.56 2010/06/29 03:09:29 blambert Exp $ */ +/* $OpenBSD: netstat.h,v 1.57 2010/10/11 12:33:36 claudio Exp $ */ /* $NetBSD: netstat.h,v 1.6 1996/05/07 02:55:05 thorpej Exp $ */ /* @@ -114,7 +114,7 @@ void mrt6_stats(void); char *routename6(struct sockaddr_in6 *); char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *); -void p_rttables(int, u_int); +void p_rttables(int, u_int, int); void p_flags(int, char *); void p_addr(struct sockaddr *, struct sockaddr *, int); void p_gwaddr(struct sockaddr *, int); diff --git a/usr.bin/netstat/show.c b/usr.bin/netstat/show.c index 7c4ea8d12f8..6d27a598e8e 100644 --- a/usr.bin/netstat/show.c +++ b/usr.bin/netstat/show.c @@ -1,4 +1,4 @@ -/* $OpenBSD: show.c,v 1.33 2010/07/30 12:55:12 bluhm Exp $ */ +/* $OpenBSD: show.c,v 1.34 2010/10/11 12:33:36 claudio Exp $ */ /* $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $ */ /* @@ -116,13 +116,13 @@ void index_pfk(struct sadb_msg *, void **); * Print routing tables. */ void -p_rttables(int af, u_int tableid) +p_rttables(int af, u_int tableid, int hastable) { struct rt_msghdr *rtm; struct sadb_msg *msg; char *buf = NULL, *next, *lim = NULL; size_t needed; - int mib[7]; + int mib[7], mcnt; struct sockaddr *sa; mib[0] = CTL_NET; @@ -131,14 +131,18 @@ p_rttables(int af, u_int tableid) mib[3] = af; mib[4] = NET_RT_DUMP; mib[5] = 0; - mib[6] = tableid; + if (hastable) { + mib[6] = tableid; + mcnt = 7; + } else + mcnt = 6; - if (sysctl(mib, 7, NULL, &needed, NULL, 0) < 0) + if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) < 0) err(1, "route-sysctl-estimate"); if (needed > 0) { if ((buf = malloc(needed)) == 0) err(1, NULL); - if (sysctl(mib, 7, buf, &needed, NULL, 0) < 0) + if (sysctl(mib, mcnt, buf, &needed, NULL, 0) < 0) err(1, "sysctl of routing table"); lim = buf + needed; } @@ -835,7 +839,6 @@ char * netname(struct sockaddr *sa, struct sockaddr *mask) { switch (sa->sa_family) { - case AF_INET: return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr, mask->sa_len == 0 ? 0 : |