Add an -L option to ignore loopback Internet sockets.

MFC after:	2 weeks
This commit is contained in:
Bruce M Simpson 2008-05-19 11:32:44 +00:00
parent 83a17b90eb
commit 9b6ca89250
2 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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;