Skip to content

Commit

Permalink
Accept netstat-style address.port syntax too.
Browse files Browse the repository at this point in the history
OK bluhm@ deraadt@ jmc@
  • Loading branch information
millert committed Feb 6, 2023
1 parent 67a77ff commit 8aa0c37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
21 changes: 16 additions & 5 deletions usr.sbin/tcpdrop/tcpdrop.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $OpenBSD: tcpdrop.8,v 1.13 2014/08/28 08:22:42 jmc Exp $
.\" $OpenBSD: tcpdrop.8,v 1.14 2023/02/06 18:14:10 millert Exp $
.\"
.\" Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
.\"
Expand All @@ -14,7 +14,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: August 28 2014 $
.Dd $Mdocdate: February 6 2023 $
.Dt TCPDROP 8
.Os
.Sh NAME
Expand All @@ -26,9 +26,6 @@
.Ar local-port
.Ar remote-addr
.Ar remote-port
.Nm tcpdrop
.Ar local-addr : Ns Ar local-port
.Ar remote-addr : Ns Ar remote-port
.Sh DESCRIPTION
The
.Nm
Expand All @@ -41,6 +38,18 @@ and the foreign address
port
.Ar remote-port .
Addresses and ports can be specified by name or numeric value.
.Pp
To simplify dropping TCP connections using the output of
.Xr fstat 1
and
.Xr netstat 1 ,
.Nm
also supports a two-argument form where the address and port are
separated by a colon
.Pq Sq \&:
or dot
.Pq Sq \&.
character.
.Sh EXAMPLES
If a connection to
.Xr httpd 8
Expand All @@ -57,6 +66,8 @@ Either of the following commands will drop the connection:
# tcpdrop 192.168.5.41 80 192.168.5.1 26747

# tcpdrop 192.168.5.41:80 192.168.5.1:26747

# tcpdrop 192.168.5.41.80 192.168.5.1.26747
.Ed
.Sh SEE ALSO
.Xr fstat 1 ,
Expand Down
13 changes: 9 additions & 4 deletions usr.sbin/tcpdrop/tcpdrop.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: tcpdrop.c,v 1.20 2021/07/12 15:09:21 beck Exp $ */
/* $OpenBSD: tcpdrop.c,v 1.21 2023/02/06 18:14:10 millert Exp $ */

/*
* Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
Expand Down Expand Up @@ -44,9 +44,6 @@ usage(void)
fprintf(stderr,
"usage: %s local-addr local-port remote-addr remote-port\n",
__progname);
fprintf(stderr,
" %s local-addr:local-port remote-addr:remote-port\n",
__progname);
exit(1);
}

Expand Down Expand Up @@ -76,10 +73,15 @@ main(int argc, char **argv)
hints.ai_socktype = SOCK_STREAM;

if (argc == 3) {
char *dot;

laddr1 = addr1 = strdup(argv[1]);
if (!addr1)
err(1, "strdup");
port1 = strrchr(addr1, ':');
dot = strrchr(addr1, '.');
if (dot > port1)
port1 = dot;
if (port1)
*port1++ = '\0';
else
Expand All @@ -89,6 +91,9 @@ main(int argc, char **argv)
if (!addr2)
err(1, "strdup");
port2 = strrchr(addr2, ':');
dot = strrchr(addr2, '.');
if (dot > port2)
port2 = dot;
if (port2)
*port2++ = '\0';
else
Expand Down

0 comments on commit 8aa0c37

Please sign in to comment.