From owner-svn-src-head@FreeBSD.ORG Wed Jan 30 18:24:30 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 3C0ACCC3; Wed, 30 Jan 2013 18:24:30 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 16E2BD8A; Wed, 30 Jan 2013 18:24:30 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id r0UIOTq5039635; Wed, 30 Jan 2013 18:24:29 GMT (envelope-from jhb@svn.freebsd.org) Received: (from jhb@localhost) by svn.freebsd.org (8.14.5/8.14.5/Submit) id r0UIOTED039633; Wed, 30 Jan 2013 18:24:29 GMT (envelope-from jhb@svn.freebsd.org) Message-Id: <201301301824.r0UIOTED039633@svn.freebsd.org> From: John Baldwin Date: Wed, 30 Jan 2013 18:24:29 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r246129 - head/usr.sbin/tcpdrop X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Jan 2013 18:24:30 -0000 Author: jhb Date: Wed Jan 30 18:24:29 2013 New Revision: 246129 URL: http://svnweb.freebsd.org/changeset/base/246129 Log: Allow the address and ports to be separated by a colon or period rather than a space to permit directly pasting the output of commands such as netstat and sockstat on the command line. Reviewed by: net MFC after: 1 week Modified: head/usr.sbin/tcpdrop/tcpdrop.8 head/usr.sbin/tcpdrop/tcpdrop.c Modified: head/usr.sbin/tcpdrop/tcpdrop.8 ============================================================================== --- head/usr.sbin/tcpdrop/tcpdrop.8 Wed Jan 30 18:01:20 2013 (r246128) +++ head/usr.sbin/tcpdrop/tcpdrop.8 Wed Jan 30 18:24:29 2013 (r246129) @@ -17,7 +17,7 @@ .\" .\" $FreeBSD$ .\" -.Dd March 24, 2009 +.Dd January 30, 2013 .Dt TCPDROP 8 .Os .Sh NAME @@ -62,6 +62,9 @@ will be dropped. .Pp Addresses and ports may be specified by name or numeric value. Both IPv4 and IPv6 address formats are supported. +.Pp +The addresses and ports may be separated by periods or colons +instead of spaces. .Sh EXIT STATUS .Ex -std .Sh EXAMPLES Modified: head/usr.sbin/tcpdrop/tcpdrop.c ============================================================================== --- head/usr.sbin/tcpdrop/tcpdrop.c Wed Jan 30 18:01:20 2013 (r246128) +++ head/usr.sbin/tcpdrop/tcpdrop.c Wed Jan 30 18:24:29 2013 (r246129) @@ -50,6 +50,7 @@ struct host_service { static bool tcpdrop_list_commands = false; +static char *findport(const char *); static struct xinpgen *getxpcblist(const char *); static void sockinfo(const struct sockaddr *, struct host_service *); static bool tcpdrop(const struct sockaddr *, const struct sockaddr *); @@ -65,6 +66,7 @@ static void usage(void); int main(int argc, char *argv[]) { + char *lport, *fport; bool dropall; int ch; @@ -93,15 +95,43 @@ main(int argc, char *argv[]) exit(0); } - if (argc != 4 || tcpdrop_list_commands) + if ((argc != 2 && argc != 4) || tcpdrop_list_commands) usage(); - if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3])) + if (argc == 2) { + lport = findport(argv[0]); + fport = findport(argv[1]); + if (lport == NULL || lport[1] == '\0' || fport == NULL || + fport[1] == '\0') + usage(); + *lport++ = '\0'; + *fport++ = '\0'; + if (!tcpdropbyname(argv[0], lport, argv[1], fport)) + exit(1); + } else if (!tcpdropbyname(argv[0], argv[1], argv[2], argv[3])) exit(1); exit(0); } +static char * +findport(const char *arg) +{ + char *dot, *colon; + + /* A strrspn() or strrpbrk() would be nice. */ + dot = strrchr(arg, '.'); + colon = strrchr(arg, ':'); + if (dot == NULL) + return (colon); + if (colon == NULL) + return (dot); + if (dot < colon) + return (colon); + else + return (dot); +} + static struct xinpgen * getxpcblist(const char *name) { @@ -237,7 +267,7 @@ tcpdropbyname(const char *lhost, const c error = getaddrinfo(fhost, fport, &hints, &foreign); if (error != 0) { freeaddrinfo(local); /* XXX gratuitous */ - errx(1, "getaddrinfo: %s port %s: %s", lhost, lport, + errx(1, "getaddrinfo: %s port %s: %s", fhost, fport, gai_strerror(error)); } @@ -318,6 +348,8 @@ usage(void) { fprintf(stderr, "usage: tcpdrop local-address local-port foreign-address foreign-port\n" +" tcpdrop local-address:local-port foreign-address:foreign-port\n" +" tcpdrop local-address.local-port foreign-address.foreign-port\n" " tcpdrop [-l] -a\n"); exit(1); }