From 3a9f5c6d6d9c6f9ab9b07afaa7601e2ceb13c301 Mon Sep 17 00:00:00 2001
From: Stephen Soltesz <soltesz@cs.princeton.edu>
Date: Mon, 22 Sep 2008 16:27:29 +0000
Subject: [PATCH] report source and dest ports.

---
 factory/portsummary | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/factory/portsummary b/factory/portsummary
index 7ff7bc2..f800632 100755
--- a/factory/portsummary
+++ b/factory/portsummary
@@ -3,17 +3,22 @@
 import os
 import sys
 
-ns = os.popen("vcontext --xid 1 --migrate -- netstat -apnlut", 'r')
+# NOTE: '--inet' lists only ipv4 addresses.
+ns = os.popen("ncontext --nid 1 --migrate -- vcontext --xid 1 --migrate -- netstat -apnlut --inet", 'r')
 port_summary = {}
 for line in ns:
 	try:
 		ns_fields = line.split()
 		if ns_fields[0] == "tcp" or ns_fields[0] == "udp":
 			(src_ip, src_port) = ns_fields[3].split(':')
-			if src_ip is not "0.0.0.0" and src_port in port_summary:
+			(dst_ip, dst_port) = ns_fields[4].split(':')
+
+			port_key='%s-%s' % (src_port, dst_port)
+
+			if src_ip is not "0.0.0.0" and port_key in port_summary:
 				# skip INADDR_ANY addresses and ports we've already seen.
 				continue
-			(dst_ip, dst_port) = ns_fields[4].split(':')
+
 			conn_state = ns_fields[5]
 			if ns_fields[0] == "tcp":
 				proc_field = ns_fields[6]
@@ -26,10 +31,8 @@ for line in ns:
 			if proc_field != "-":
 				(pid,procname)= proc_field.split('/')
 			else:
-				# TODO: ephemeral sockets in TIME_WAIT
-				# TODO: XXX: THIS IS FALSE.  Need an actual way of finding the
-				# 			XID associated with a socket in TIME_WAIT
-				(pid,procname)= ("1","")
+				# NOTE: without a PID there is no way to associate with an XID
+				continue
 
 			if ( ns_fields[0] == "tcp" and src_ip == "0.0.0.0" and conn_state == "LISTEN" ) or \
 			   ( ns_fields[0] == "udp" and src_ip == "0.0.0.0" ):
@@ -44,7 +47,11 @@ for line in ns:
 			xid_stream = os.popen("vserver-info %s XID" % pid)
 			xid = xid_stream.read()
 
-			port_summary[src_port] = {'prot' : ns_fields[0], 'port' : src_port, 'slice' : xid[:-1], 'type': type}
+			port_summary[port_key] = {'prot' : ns_fields[0], 
+									  'src_port' : src_port, 
+									  'dst_port' : dst_port, 
+									  'slice' : xid[:-1], 
+									  'type': type}
 	except:
 		import traceback; traceback.print_exc()
 		print line
@@ -52,5 +59,5 @@ for line in ns:
 ports = port_summary.keys()
 ports.sort()
 for port in ports:
-	print "%(prot)4s\t%(port)6s\t%(slice)5s\t%(type)s" % port_summary[port]
+	print "%(prot)4s\t%(src_port)6s\t%(dst_port)6s\t%(slice)5s\t%(type)s" % port_summary[port]
 		
-- 
2.47.0