From roam@orbitel.bg  Thu Aug 17 05:33:52 2000
Return-Path: <roam@orbitel.bg>
Received: from sentinel.office1.bg (sentinel.office1.bg [195.24.48.182])
	by hub.freebsd.org (Postfix) with SMTP id 3B9B937B616
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 17 Aug 2000 05:32:48 -0700 (PDT)
Received: (qmail 1416 invoked by uid 1001); 17 Aug 2000 12:25:07 -0000
Message-Id: <20000817122507.1415.qmail@ringwraith.office1>
Date: 17 Aug 2000 12:25:07 -0000
From: Peter Pentchev <roam@orbitel.bg>
Reply-To: Peter Pentchev <roam@orbitel.bg>
To: FreeBSD-gnats-submit@freebsd.org
Subject: [PATCH] show service names in netstat and sockstat
X-Send-Pr-Version: 3.2

>Number:         20681
>Category:       bin
>Synopsis:       [PATCH] show service names in netstat and sockstat
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 17 05:40:01 PDT 2000
>Closed-Date:    Fri Jun 15 17:40:14 PDT 2001
>Last-Modified:  Fri Jun 15 17:40:49 PDT 2001
>Originator:     Peter Pentchev <roam@orbitel.bg>
>Release:        FreeBSD 4.1-STABLE i386
>Organization:
Orbitel JSCo
>Environment:

Diffs against RELENG_4, probably easy to port to other releases.

>Description:

Sometimes it is helpful to be able to see port numbers resolved to
service names without attempting to resolve host and network addresses.

The attached patch adds a -S flag to netstat, which overrides the -n
flag for service names (hosts and networks are still shown numerically).

Also included is a trivial patch to the sockstat Perl script, which
makes use of the netstat -S flag, showing service names in sockstat,
which was the whole reason I started doing this at all :)

>How-To-Repeat:

N/A

>Fix:

diff -u -urN src/usr.bin/netstat/inet.c mysrc/usr.bin/netstat/inet.c
--- src/usr.bin/netstat/inet.c	Thu Jul 20 19:38:54 2000
+++ mysrc/usr.bin/netstat/inet.c	Thu Aug 17 14:57:27 2000
@@ -695,7 +695,7 @@
 
 	sprintf(line, "%.*s.", (Aflag && !numeric) ? 12 : 16, inetname(in));
 	cp = index(line, '\0');
-	if (!numeric && port)
+	if ((Sflag || !numeric) && port)
 		sp = getservbyport((int)port, proto);
 	if (sp || port == 0)
 		sprintf(cp, "%.15s", sp ? sp->s_name : "*");
diff -u -urN src/usr.bin/netstat/inet6.c mysrc/usr.bin/netstat/inet6.c
--- src/usr.bin/netstat/inet6.c	Tue Aug  8 16:33:35 2000
+++ mysrc/usr.bin/netstat/inet6.c	Thu Aug 17 14:59:40 2000
@@ -992,7 +992,7 @@
 	sprintf(line, "%.*s.", lflag ? 39 :
 		(Aflag && !numeric) ? 12 : 16, inet6name(in6));
 	cp = index(line, '\0');
-	if (!numeric && port)
+	if ((Sflag || !numeric) && port)
 		GETSERVBYPORT6(port, proto, sp);
 	if (sp || port == 0)
 		sprintf(cp, "%.8s", sp ? sp->s_name : "*");
diff -u -urN src/usr.bin/netstat/iso.c mysrc/usr.bin/netstat/iso.c
--- src/usr.bin/netstat/iso.c	Mon Sep  6 10:34:27 1999
+++ mysrc/usr.bin/netstat/iso.c	Thu Aug 17 15:00:51 2000
@@ -435,7 +435,7 @@
 
 	*cp++ = '.';
 	if(sufxlen) {
-		if( !Aflag && !nflag && (ihe=iso_getserventrybytsel(sufx, sufxlen))) {
+		if( !Aflag && (Sflag || !nflag) && (ihe=iso_getserventrybytsel(sufx, sufxlen))) {
 			Ihe = *ihe;
 			ihe = &Ihe;
 		}
diff -u -urN src/usr.bin/netstat/main.c mysrc/usr.bin/netstat/main.c
--- src/usr.bin/netstat/main.c	Thu Jul 20 19:38:58 2000
+++ mysrc/usr.bin/netstat/main.c	Thu Aug 17 15:01:46 2000
@@ -302,7 +302,7 @@
 
 	af = AF_UNSPEC;
 
-	while ((ch = getopt(argc, argv, "Aabdf:ghI:lLiM:mN:np:rstuw:")) != -1)
+	while ((ch = getopt(argc, argv, "Aabdf:ghI:lLiM:mN:np:rSstuw:")) != -1)
 		switch(ch) {
 		case 'A':
 			Aflag = 1;
@@ -393,6 +393,9 @@
 		case 'r':
 			rflag = 1;
 			break;
+		case 'S':
+			Sflag = 1;
+			break;
 		case 's':
 			++sflag;
 			break;
@@ -696,8 +699,8 @@
 usage()
 {
 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
-"usage: netstat [-AaLln] [-f address_family] [-M core] [-N system]",
-"       netstat [-abdghilmnrs] [-f address_family] [-M core] [-N system]",
+"usage: netstat [-AaLlnS] [-f address_family] [-M core] [-N system]",
+"       netstat [-abdghilmnrSs] [-f address_family] [-M core] [-N system]",
 "       netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
 "       netstat [-M core] [-N system] [-p protocol]");
 	exit(1);
diff -u -urN src/usr.bin/netstat/netstat.1 mysrc/usr.bin/netstat/netstat.1
--- src/usr.bin/netstat/netstat.1	Sat Mar 11 16:33:13 2000
+++ mysrc/usr.bin/netstat/netstat.1	Thu Aug 17 15:05:17 2000
@@ -235,6 +235,10 @@
 The program will complain if
 .Ar protocol
 is unknown or if there is no statistics routine for it.
+.It Fl S
+Show service names even if numeric output has been requested by the
+.Fl n
+option.
 .It Fl s
 Show per-protocol statistics.
 If this option is repeated, counters with a value of zero are suppressed.
diff -u -urN src/usr.bin/netstat/netstat.h mysrc/usr.bin/netstat/netstat.h
--- src/usr.bin/netstat/netstat.h	Thu Jul 20 19:38:59 2000
+++ mysrc/usr.bin/netstat/netstat.h	Thu Aug 17 14:55:54 2000
@@ -48,6 +48,7 @@
 int	nflag;		/* show addresses numerically */
 int	pflag;		/* show given protocol */
 int	rflag;		/* show routing tables (or routing stats) */
+int	Sflag;		/* show service names even if -n specified */
 int	sflag;		/* show protocol statistics */
 int	tflag;		/* show i/f watchdog timers */
 
diff -u -urN src/usr.bin/sockstat/sockstat.pl mysrc/usr.bin/sockstat/sockstat.pl
--- src/usr.bin/sockstat/sockstat.pl	Sat Mar 11 13:25:00 2000
+++ mysrc/usr.bin/sockstat/sockstat.pl	Thu Aug 17 15:08:26 2000
@@ -40,7 +40,7 @@
 $user,   $cmd,    $pid, $fd, $proto,$laddr,               $faddr
 .
 
-open NETSTAT, "netstat -Aan |" or die "'netstat' failed: $!";
+open NETSTAT, "netstat -AanS |" or die "'netstat' failed: $!";
 <NETSTAT>; <NETSTAT>;
 
 while (<NETSTAT>) {

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Tue Aug 22 08:18:12 PDT 2000 
Responsible-Changed-Why:  
Personally, I wouldn't add these options, but DES maintains 
one of these utilities, and whatever decision he makes for 
sockstat probably makes sense for netstat. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=20681 

From: Assar Westerlund <assar@freebsd.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Peter Pentchev <roam@orbitel.bg>, des@freebsd.org
Subject: Re: bin/20681: [PATCH] show service names in netstat and sockstat
Date: 16 Jun 2001 01:59:11 +0200

 Is this patch ok?
 
 /assar
 
 Index: sockstat.pl
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/sockstat/sockstat.pl,v
 retrieving revision 1.11
 diff -u -w -u -w -r1.11 sockstat.pl
 --- sockstat.pl	2000/10/13 16:25:37	1.11
 +++ sockstat.pl	2001/06/15 23:58:36
 @@ -53,7 +53,7 @@
      if (!defined($pid = open(PIPE, "-|"))) {
  	die("open(netstat): $!\n");
      } elsif ($pid == 0) {
 -	exec("/usr/bin/netstat", "-Aan");
 +	exec("/usr/bin/netstat", "-AaS");
  	die("exec(netstat): $!\n");
      }
      while ($line = <PIPE>) {

From: Dag-Erling Smorgrav <des@ofug.org>
To: Assar Westerlund <assar@freebsd.org>
Cc: FreeBSD-gnats-submit@freebsd.org,
	Peter Pentchev <roam@orbitel.bg>
Subject: Re: bin/20681: [PATCH] show service names in netstat and sockstat
Date: 16 Jun 2001 02:26:36 +0200

 Assar Westerlund <assar@freebsd.org> writes:
 > Is this patch ok?
 
 No.  Sockstat purposedly does not resolve host or port names.  Hiding
 a port number behind the first few letters of a name which more often
 than not bears no relation to the traffic actually passing through the
 port is simply idiotic.
 
 DES
 -- 
 Dag-Erling Smorgrav - des@ofug.org

From: Peter Pentchev <roam@orbitel.bg>
To: Dag-Erling Smorgrav <des@ofug.org>
Cc: Assar Westerlund <assar@freebsd.org>,
	FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/20681: [PATCH] show service names in netstat and sockstat
Date: Sat, 16 Jun 2001 03:29:58 +0300

 On Sat, Jun 16, 2001 at 02:26:36AM +0200, Dag-Erling Smorgrav wrote:
 > Assar Westerlund <assar@freebsd.org> writes:
 > > Is this patch ok?
 > 
 > No.  Sockstat purposedly does not resolve host or port names.  Hiding
 > a port number behind the first few letters of a name which more often
 > than not bears no relation to the traffic actually passing through the
 > port is simply idiotic.
 
 Actually, yes, this is true for high ports..
 
 G'luck,
 Peter
 
 -- 
 If you think this sentence is confusing, then change one pig.

From: Assar Westerlund <assar@freebsd.org>
To: Dag-Erling Smorgrav <des@ofug.org>
Cc: FreeBSD-gnats-submit@freebsd.org,
	Peter Pentchev <roam@orbitel.bg>
Subject: Re: bin/20681: [PATCH] show service names in netstat and sockstat
Date: 16 Jun 2001 02:39:24 +0200

 Dag-Erling Smorgrav <des@ofug.org> writes:
 > Assar Westerlund <assar@freebsd.org> writes:
 > > Is this patch ok?
 > 
 > No.  Sockstat purposedly does not resolve host or port names.  Hiding
 > a port number behind the first few letters of a name which more often
 > than not bears no relation to the traffic actually passing through the
 > port is simply idiotic.
 
 Ok, I will close the PR noting that the netstat option has appeared
 and that sockstat will not be changed.
 
 /assar
State-Changed-From-To: open->closed 
State-Changed-By: assar 
State-Changed-When: Fri Jun 15 17:40:14 PDT 2001 
State-Changed-Why:  
the netstat option -S has been added 
sockstat will not be changed 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=20681 
>Unformatted:
