Skip site navigation (1)Skip section navigation (2)
Date:      17 Aug 2000 12:25:07 -0000
From:      Peter Pentchev <roam@orbitel.bg>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/20681: [PATCH] show service names in netstat and sockstat
Message-ID:  <20000817122507.1415.qmail@ringwraith.office1>

next in thread | raw e-mail | index | archive | help

>Number:         20681
>Category:       bin
>Synopsis:       [PATCH] show service names in netstat and sockstat
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 17 05:40:01 PDT 2000
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000817122507.1415.qmail>