Add a command line option for using a wider field for displaying

addresses. This allows the table to be consistent when IPv6
addresses have to be printed.
While there, document the -v option in the man page.

Sponsored by:	Netflix, Inc.
This commit is contained in:
Michael Tuexen 2017-09-13 06:57:52 +00:00
parent efeb46889f
commit 83f60cb202
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=323521
2 changed files with 21 additions and 8 deletions

View File

@ -35,7 +35,7 @@
.Nd list open sockets .Nd list open sockets
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl 46cLlSsUu .Op Fl 46cLlSsUuvw
.Op Fl j Ar jid .Op Fl j Ar jid
.Op Fl p Ar ports .Op Fl p Ar ports
.Op Fl P Ar protocols .Op Fl P Ar protocols
@ -97,6 +97,10 @@ Show
.Dv AF_LOCAL .Dv AF_LOCAL
.Pq Ux .Pq Ux
sockets. sockets.
.It Fl v
Verbose mode.
.It Fl w
Use wider field size for displaying addresses.
.El .El
.Pp .Pp
If neither If neither

View File

@ -78,6 +78,7 @@ static int opt_s; /* Show protocol state if applicable */
static int opt_U; /* Show remote UDP encapsulation port number */ static int opt_U; /* Show remote UDP encapsulation port number */
static int opt_u; /* Show Unix domain sockets */ static int opt_u; /* Show Unix domain sockets */
static int opt_v; /* Verbose mode */ static int opt_v; /* Verbose mode */
static int opt_w; /* Wide print area for addresses */
/* /*
* Default protocols to use if no -P was defined. * Default protocols to use if no -P was defined.
@ -1020,7 +1021,8 @@ displaysock(struct sock *s, int pos)
faddr = s->faddr; faddr = s->faddr;
first = 1; first = 1;
while (laddr != NULL || faddr != NULL) { while (laddr != NULL || faddr != NULL) {
while (pos < 36) offset = 36;
while (pos < offset)
pos += xprintf(" "); pos += xprintf(" ");
switch (s->family) { switch (s->family) {
case AF_INET: case AF_INET:
@ -1030,10 +1032,12 @@ displaysock(struct sock *s, int pos)
if (s->family == AF_INET6 && pos >= 58) if (s->family == AF_INET6 && pos >= 58)
pos += xprintf(" "); pos += xprintf(" ");
} }
while (pos < 58) offset += opt_w ? 46 : 22;
while (pos < offset)
pos += xprintf(" "); pos += xprintf(" ");
if (faddr != NULL) if (faddr != NULL)
pos += printaddr(&faddr->address); pos += printaddr(&faddr->address);
offset += opt_w ? 46 : 22;
break; break;
case AF_UNIX: case AF_UNIX:
if ((laddr == NULL) || (faddr == NULL)) if ((laddr == NULL) || (faddr == NULL))
@ -1048,6 +1052,7 @@ displaysock(struct sock *s, int pos)
p = *(void **)&(faddr->address); p = *(void **)&(faddr->address);
if (p == NULL) { if (p == NULL) {
pos += xprintf("(not connected)"); pos += xprintf("(not connected)");
offset += opt_w ? 92 : 44;
break; break;
} }
pos += xprintf("-> "); pos += xprintf("-> ");
@ -1065,11 +1070,11 @@ displaysock(struct sock *s, int pos)
pos += xprintf("??"); pos += xprintf("??");
else else
pos += printaddr(&s_tmp->laddr->address); pos += printaddr(&s_tmp->laddr->address);
offset += opt_w ? 92 : 44;
break; break;
default: default:
abort(); abort();
} }
offset = 80;
if (opt_U) { if (opt_U) {
if (faddr != NULL && if (faddr != NULL &&
s->proto == IPPROTO_SCTP && s->proto == IPPROTO_SCTP &&
@ -1147,9 +1152,10 @@ display(void)
struct sock *s; struct sock *s;
int hash, n, pos; int hash, n, pos;
printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s", printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
"USER", "COMMAND", "PID", "FD", "PROTO", "USER", "COMMAND", "PID", "FD", "PROTO",
"LOCAL ADDRESS", "FOREIGN ADDRESS"); opt_w ? 45 : 21, "LOCAL ADDRESS",
opt_w ? 45 : 21, "FOREIGN ADDRESS");
if (opt_U) if (opt_U)
printf(" %-6s", "ENCAPS"); printf(" %-6s", "ENCAPS");
if (opt_s) { if (opt_s) {
@ -1228,7 +1234,7 @@ static void
usage(void) usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"usage: sockstat [-46cLlSsu] [-j jid] [-p ports] [-P protocols]\n"); "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
exit(1); exit(1);
} }
@ -1239,7 +1245,7 @@ main(int argc, char *argv[])
int o, i; int o, i;
opt_j = -1; opt_j = -1;
while ((o = getopt(argc, argv, "46cj:Llp:P:SsUuv")) != -1) while ((o = getopt(argc, argv, "46cj:Llp:P:SsUuvw")) != -1)
switch (o) { switch (o) {
case '4': case '4':
opt_4 = 1; opt_4 = 1;
@ -1280,6 +1286,9 @@ main(int argc, char *argv[])
case 'v': case 'v':
++opt_v; ++opt_v;
break; break;
case 'w':
opt_w = 1;
break;
default: default:
usage(); usage();
} }