diff options
author | bms <bms@FreeBSD.org> | 2008-05-19 11:32:44 +0000 |
---|---|---|
committer | bms <bms@FreeBSD.org> | 2008-05-19 11:32:44 +0000 |
commit | 1d1522666eae498dcd96628e3d169803671f069a (patch) | |
tree | e05fb36aabba7c8cb1128d2cc568704f5d3c92cd /usr.bin/sockstat | |
parent | 11daf29917a19191a1f9a850dc322a36d215e144 (diff) | |
download | FreeBSD-src-1d1522666eae498dcd96628e3d169803671f069a.zip FreeBSD-src-1d1522666eae498dcd96628e3d169803671f069a.tar.gz |
Add an -L option to ignore loopback Internet sockets.
MFC after: 2 weeks
Diffstat (limited to 'usr.bin/sockstat')
-rw-r--r-- | usr.bin/sockstat/sockstat.1 | 10 | ||||
-rw-r--r-- | usr.bin/sockstat/sockstat.c | 17 |
2 files changed, 24 insertions, 3 deletions
diff --git a/usr.bin/sockstat/sockstat.1 b/usr.bin/sockstat/sockstat.1 index 0fa6bad..3edf0cf 100644 --- a/usr.bin/sockstat/sockstat.1 +++ b/usr.bin/sockstat/sockstat.1 @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd November 11, 2006 +.Dd May 18, 2008 .Dt SOCKSTAT 1 .Os .Sh NAME @@ -35,7 +35,7 @@ .Nd list open sockets .Sh SYNOPSIS .Nm -.Op Fl 46clu +.Op Fl 46cLlu .Op Fl p Ar ports .Op Fl P Ar protocols .Sh DESCRIPTION @@ -57,6 +57,12 @@ Show (IPv6) sockets. .It Fl c Show connected sockets. +.It Fl L +Only show Internet sockets if the local or foreign addresses are not +in the loopback network prefix +.Li 127.0.0.0/8 , +or do not contain the IPv6 loopback address +.Li ::1 . .It Fl l Show listening sockets. .It Fl p Ar ports diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c index 8c29843..fafdd01 100644 --- a/usr.bin/sockstat/sockstat.c +++ b/usr.bin/sockstat/sockstat.c @@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$"); static int opt_4; /* Show IPv4 sockets */ static int opt_6; /* Show IPv6 sockets */ static int opt_c; /* Show connected sockets */ +static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */ static int opt_l; /* Show listening sockets */ static int opt_u; /* Show Unix domain sockets */ static int opt_v; /* Verbose mode */ @@ -342,10 +343,21 @@ gather_inet(int proto) if ((inp->inp_fport == 0 && !opt_l) || (inp->inp_fport != 0 && !opt_c)) continue; +#define __IN_IS_ADDR_LOOPBACK(pina) \ + ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) + if (opt_L && + (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) || + __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr))) + continue; +#undef __IN_IS_ADDR_LOOPBACK } else if (inp->inp_vflag & INP_IPV6) { if ((inp->in6p_fport == 0 && !opt_l) || (inp->in6p_fport != 0 && !opt_c)) continue; + if (opt_L && + (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) || + IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr))) + continue; } else { if (opt_v) warnx("invalid vflag 0x%x", inp->inp_vflag); @@ -679,7 +691,7 @@ main(int argc, char *argv[]) int protos_defined = -1; int o, i; - while ((o = getopt(argc, argv, "46clp:P:uv")) != -1) + while ((o = getopt(argc, argv, "46cLlp:P:uv")) != -1) switch (o) { case '4': opt_4 = 1; @@ -690,6 +702,9 @@ main(int argc, char *argv[]) case 'c': opt_c = 1; break; + case 'L': + opt_L = 1; + break; case 'l': opt_l = 1; break; |