From owner-freebsd-net@FreeBSD.ORG Tue Jan 29 23:11:44 2013 Return-Path: Delivered-To: net@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 98BD2A85 for ; Tue, 29 Jan 2013 23:11:44 +0000 (UTC) (envelope-from andre@freebsd.org) Received: from c00l3r.networx.ch (c00l3r.networx.ch [62.48.2.2]) by mx1.freebsd.org (Postfix) with ESMTP id 19716E54 for ; Tue, 29 Jan 2013 23:11:43 +0000 (UTC) Received: (qmail 85389 invoked from network); 30 Jan 2013 00:31:50 -0000 Received: from c00l3r.networx.ch (HELO [127.0.0.1]) ([62.48.2.2]) (envelope-sender ) by c00l3r.networx.ch (qmail-ldap-1.03) with SMTP for ; 30 Jan 2013 00:31:50 -0000 Message-ID: <5108572D.1090905@freebsd.org> Date: Wed, 30 Jan 2013 00:11:41 +0100 From: Andre Oppermann User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: John Baldwin Subject: Re: [PATCH] Allow tcpdrop to use non-space separators References: <201301291205.41301.jhb@freebsd.org> In-Reply-To: <201301291205.41301.jhb@freebsd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: net@freebsd.org X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Jan 2013 23:11:44 -0000 On 29.01.2013 18:05, John Baldwin wrote: > A common use case I have at work is to find a busted connection using netstat > -n or sockstat and then want to tcpdrop it. However, tcpdrop requires spaces > between the address and port so I can't simply cut and paste from one terminal > window into another to generate the tcpdrop command. This patch adds support > for having a decimal (netstat output) or colon (sockstat output) between the > address and port instead of a space. It is careful to look for the last of > these tokens to avoid parsing part of the address as the port. Excellent. Can netstat be changed from decimal to colon output as well? The decimal output is completely outdated and probably from a time when not even dinosaurs were [created|evolved] yet. Colon output for port numbers is *the* standard all around. -- Andre > Index: tcpdrop.8 > =================================================================== > --- tcpdrop.8 (revision 246073) > +++ tcpdrop.8 (working copy) > @@ -62,6 +62,9 @@ > .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 > Index: tcpdrop.c > =================================================================== > --- tcpdrop.c (revision 246073) > +++ tcpdrop.c (working copy) > @@ -50,6 +50,7 @@ > > 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 @@ > int > main(int argc, char *argv[]) > { > + char *lport, *fport; > bool dropall; > int ch; > > @@ -93,15 +95,43 @@ > 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' || rport == NULL || > + rport[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 @@ > 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 @@ > { > 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); > } >