[tor-commits] [stem/master] Expand Connetion enum with an is_ipv6 attribute

atagar at torproject.org atagar at torproject.org
Sun Jan 24 00:00:50 UTC 2016


commit d32bd6f09f7a2edae4c8e46787a252ab7df632a2
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jan 23 15:59:38 2016 -0800

    Expand Connetion enum with an is_ipv6 attribute
    
    Providing an attribute to easily distinguish if the connection is IPv6 or IPv4.
---
 docs/change_log.rst          |    3 ++-
 stem/util/connection.py      |    5 ++--
 stem/util/proc.py            |    6 ++---
 test/unit/util/connection.py |   58 +++++++++++++++++++++---------------------
 test/unit/util/proc.py       |    4 +--
 5 files changed, 39 insertions(+), 37 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 9c6b88b..b1e26a1 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -72,9 +72,10 @@ The following are only available within Stem's `git repository
  * **Utilities**
 
   * Added :func:`~stem.util.__init__.datetime_to_unix`
-  * Basic IPv6 support in :func:`~stem.util.connection.get_connections`
+  * IPv6 support in :func:`~stem.util.connection.get_connections`
   * The 'ss' connection resolver didn't work on Gentoo (:trac:`18079`)
   * Recognize IPv4-mapped IPv6 addresses in our utils (:trac:`18079`)
+  * Added an **is_ipv6** value to Connection instances
   * Allow :func:`stem.util.conf.Config.set` to remove values when provided with a **None** value
 
  * **Interpreter**
diff --git a/stem/util/connection.py b/stem/util/connection.py
index 09a1fff..567ca9e 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -132,7 +132,7 @@ RESOLVER_FILTER = {
 }
 
 
-class Connection(collections.namedtuple('Connection', ['local_address', 'local_port', 'remote_address', 'remote_port', 'protocol'])):
+class Connection(collections.namedtuple('Connection', ['local_address', 'local_port', 'remote_address', 'remote_port', 'protocol', 'is_ipv6'])):
   """
   Network connection information.
 
@@ -141,6 +141,7 @@ class Connection(collections.namedtuple('Connection', ['local_address', 'local_p
   :var str remote_address: destionation ip address
   :var int remote_port: destination port
   :var str protocol: protocol of the connection ('tcp', 'udp', etc)
+  :var bool is_ipv6: addresses are ipv6 if true, and ipv4 otherwise
   """
 
 
@@ -246,7 +247,7 @@ def get_connections(resolver, process_pid = None, process_name = None):
         _log('Unrecognized protocol (%s): %s' % (protocol, line))
         continue
 
-      conn = Connection(local_addr, local_port, remote_addr, remote_port, protocol)
+      conn = Connection(local_addr, local_port, remote_addr, remote_port, protocol, is_valid_ipv6_address(local_addr))
       connections.append(conn)
       _log(str(conn))
 
diff --git a/stem/util/proc.py b/stem/util/proc.py
index 5d2219c..1432710 100644
--- a/stem/util/proc.py
+++ b/stem/util/proc.py
@@ -333,8 +333,8 @@ def connections(pid):
   :param int pid: process id of the process to be queried
 
   :returns: A listing of connection tuples of the form **[(local_ipAddr1,
-    local_port1, foreign_ipAddr1, foreign_port1, protocol), ...]** (addresses
-    and protocols are strings and ports are ints)
+    local_port1, foreign_ipAddr1, foreign_port1, protocol, is_ipv6), ...]**
+    (addresses and protocols are strings and ports are ints)
 
   :raises: **IOError** if it can't be determined
   """
@@ -403,7 +403,7 @@ def connections(pid):
           local_ip, local_port = _decode_proc_address_encoding(l_addr)
           foreign_ip, foreign_port = _decode_proc_address_encoding(f_addr)
           protocol = proc_file_path[10:]
-          conn.append((local_ip, local_port, foreign_ip, foreign_port, protocol))
+          conn.append((local_ip, local_port, foreign_ip, foreign_port, protocol, False))
 
       proc_file.close()
     except IOError as exc:
diff --git a/test/unit/util/connection.py b/test/unit/util/connection.py
index 3442326..2baedc5 100644
--- a/test/unit/util/connection.py
+++ b/test/unit/util/connection.py
@@ -185,13 +185,13 @@ class TestConnection(unittest.TestCase):
     """
 
     proc_mock.return_value = [
-      ('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp'),
-      ('187.187.187.187', 48059, '204.204.204.204', 52428, 'tcp'),
+      ('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp', False),
+      ('187.187.187.187', 48059, '204.204.204.204', 52428, 'tcp', False),
     ]
 
     expected = [
-      Connection('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp'),
-      Connection('187.187.187.187', 48059, '204.204.204.204', 52428, 'tcp'),
+      Connection('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp', False),
+      Connection('187.187.187.187', 48059, '204.204.204.204', 52428, 'tcp', False),
     ]
 
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.PROC, process_pid = 1111))
@@ -206,7 +206,7 @@ class TestConnection(unittest.TestCase):
     """
 
     call_mock.return_value = NETSTAT_OUTPUT.split('\n')
-    expected = [Connection('192.168.0.1', 44284, '38.229.79.2', 443, 'tcp')]
+    expected = [Connection('192.168.0.1', 44284, '38.229.79.2', 443, 'tcp', False)]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.NETSTAT, process_pid = 15843, process_name = 'tor'))
 
     self.assertRaises(IOError, stem.util.connection.get_connections, Resolver.NETSTAT, process_pid = 15843, process_name = 'stuff')
@@ -222,7 +222,7 @@ class TestConnection(unittest.TestCase):
     """
 
     call_mock.return_value = NETSTAT_WINDOWS_OUTPUT.split('\n')
-    expected = [Connection('192.168.0.1', 44284, '38.229.79.2', 443, 'tcp')]
+    expected = [Connection('192.168.0.1', 44284, '38.229.79.2', 443, 'tcp', False)]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.NETSTAT_WINDOWS, process_pid = 15843, process_name = 'tor'))
 
     self.assertRaises(IOError, stem.util.connection.get_connections, Resolver.NETSTAT_WINDOWS, process_pid = 1111, process_name = 'tor')
@@ -238,8 +238,8 @@ class TestConnection(unittest.TestCase):
 
     call_mock.return_value = SS_OUTPUT.split('\n')
     expected = [
-      Connection('192.168.0.1', 44092, '23.112.135.72', 443, 'tcp'),
-      Connection('192.168.0.1', 44415, '38.229.79.2', 443, 'tcp'),
+      Connection('192.168.0.1', 44092, '23.112.135.72', 443, 'tcp', False),
+      Connection('192.168.0.1', 44415, '38.229.79.2', 443, 'tcp', False),
     ]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.SS, process_pid = 15843, process_name = 'tor'))
 
@@ -260,10 +260,10 @@ class TestConnection(unittest.TestCase):
     """
 
     expected = [
-      Connection('5.9.158.75', 443, '107.170.93.13', 56159, 'tcp'),
-      Connection('5.9.158.75', 443, '159.203.97.91', 37802, 'tcp'),
-      Connection('2a01:4f8:190:514a::2', 443, '2001:638:a000:4140::ffff:189', 38556, 'tcp'),
-      Connection('2a01:4f8:190:514a::2', 443, '2001:858:2:2:aabb:0:563b:1526', 51428, 'tcp'),
+      Connection('5.9.158.75', 443, '107.170.93.13', 56159, 'tcp', False),
+      Connection('5.9.158.75', 443, '159.203.97.91', 37802, 'tcp', False),
+      Connection('2a01:4f8:190:514a::2', 443, '2001:638:a000:4140::ffff:189', 38556, 'tcp', True),
+      Connection('2a01:4f8:190:514a::2', 443, '2001:858:2:2:aabb:0:563b:1526', 51428, 'tcp', True),
     ]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.SS, process_pid = 25056, process_name = 'tor'))
     self.assertEqual(2, len(stem.util.connection.get_connections(Resolver.SS, process_name = 'beam')))
@@ -276,8 +276,8 @@ class TestConnection(unittest.TestCase):
 
     call_mock.return_value = LSOF_OUTPUT.split('\n')
     expected = [
-      Connection('192.168.0.1', 44415, '38.229.79.2', 443, 'tcp'),
-      Connection('192.168.0.1', 44092, '68.169.35.102', 443, 'tcp'),
+      Connection('192.168.0.1', 44415, '38.229.79.2', 443, 'tcp', False),
+      Connection('192.168.0.1', 44092, '68.169.35.102', 443, 'tcp', False),
     ]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.LSOF, process_pid = 15843, process_name = 'tor'))
 
@@ -293,7 +293,7 @@ class TestConnection(unittest.TestCase):
     Checks the get_connections function with the lsof resolver for IPv6.
     """
 
-    expected = [Connection('2a01:4f8:190:514a::2', 443, '2001:858:2:2:aabb:0:563b:1526', 44811, 'tcp')]
+    expected = [Connection('2a01:4f8:190:514a::2', 443, '2001:858:2:2:aabb:0:563b:1526', 44811, 'tcp', True)]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.LSOF, process_pid = 1904, process_name = 'tor'))
 
   @patch('stem.util.system.call', Mock(return_value = LSOF_OUTPUT_OSX.split('\n')))
@@ -304,9 +304,9 @@ class TestConnection(unittest.TestCase):
     """
 
     expected = [
-      Connection('192.168.1.20', 9090, '38.229.79.2', 14010, 'tcp'),
-      Connection('192.168.1.20', 9090, '68.169.35.102', 14815, 'tcp'),
-      Connection('192.168.1.20', 9090, '62.135.16.134', 14456, 'tcp'),
+      Connection('192.168.1.20', 9090, '38.229.79.2', 14010, 'tcp', False),
+      Connection('192.168.1.20', 9090, '68.169.35.102', 14815, 'tcp', False),
+      Connection('192.168.1.20', 9090, '62.135.16.134', 14456, 'tcp', False),
     ]
 
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.LSOF, process_pid = 129, process_name = 'tor'))
@@ -319,8 +319,8 @@ class TestConnection(unittest.TestCase):
 
     call_mock.return_value = SOCKSTAT_OUTPUT.split('\n')
     expected = [
-      Connection('192.168.0.1', 44415, '38.229.79.2', 443, 'tcp'),
-      Connection('192.168.0.1', 44092, '68.169.35.102', 443, 'tcp'),
+      Connection('192.168.0.1', 44415, '38.229.79.2', 443, 'tcp', False),
+      Connection('192.168.0.1', 44092, '68.169.35.102', 443, 'tcp', False),
     ]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.SOCKSTAT, process_pid = 15843, process_name = 'tor'))
 
@@ -339,11 +339,11 @@ class TestConnection(unittest.TestCase):
 
     call_mock.return_value = BSD_SOCKSTAT_OUTPUT.split('\n')
     expected = [
-      Connection('172.27.72.202', 54011, '38.229.79.2', 9001, 'tcp'),
-      Connection('172.27.72.202', 59374, '68.169.35.102', 9001, 'tcp'),
-      Connection('172.27.72.202', 59673, '213.24.100.160', 9001, 'tcp'),
-      Connection('172.27.72.202', 51946, '32.188.221.72', 443, 'tcp'),
-      Connection('172.27.72.202', 60344, '21.89.91.78', 9001, 'tcp'),
+      Connection('172.27.72.202', 54011, '38.229.79.2', 9001, 'tcp', False),
+      Connection('172.27.72.202', 59374, '68.169.35.102', 9001, 'tcp', False),
+      Connection('172.27.72.202', 59673, '213.24.100.160', 9001, 'tcp', False),
+      Connection('172.27.72.202', 51946, '32.188.221.72', 443, 'tcp', False),
+      Connection('172.27.72.202', 60344, '21.89.91.78', 9001, 'tcp', False),
     ]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.BSD_SOCKSTAT, process_pid = 4397, process_name = 'tor'))
 
@@ -361,10 +361,10 @@ class TestConnection(unittest.TestCase):
 
     call_mock.return_value = BSD_PROCSTAT_OUTPUT.split('\n')
     expected = [
-      Connection('10.0.0.2', 9050, '10.0.0.1', 22370, 'tcp'),
-      Connection('10.0.0.2', 9050, '10.0.0.1', 44381, 'tcp'),
-      Connection('10.0.0.2', 33734, '38.229.79.2', 443, 'tcp'),
-      Connection('10.0.0.2', 47704, '68.169.35.102', 9001, 'tcp'),
+      Connection('10.0.0.2', 9050, '10.0.0.1', 22370, 'tcp', False),
+      Connection('10.0.0.2', 9050, '10.0.0.1', 44381, 'tcp', False),
+      Connection('10.0.0.2', 33734, '38.229.79.2', 443, 'tcp', False),
+      Connection('10.0.0.2', 47704, '68.169.35.102', 9001, 'tcp', False),
     ]
     self.assertEqual(expected, stem.util.connection.get_connections(Resolver.BSD_PROCSTAT, process_pid = 3561, process_name = 'tor'))
 
diff --git a/test/unit/util/proc.py b/test/unit/util/proc.py
index ea206b8..b6cb0d6 100644
--- a/test/unit/util/proc.py
+++ b/test/unit/util/proc.py
@@ -210,8 +210,8 @@ class TestProc(unittest.TestCase):
     self.assertEqual([], proc.connections(0))
 
     expected_results = [
-      ('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp'),
-      ('187.187.187.187', 48059, '204.204.204.204', 52428, 'udp'),
+      ('17.17.17.17', 4369, '34.34.34.34', 8738, 'tcp', False),
+      ('187.187.187.187', 48059, '204.204.204.204', 52428, 'udp', False),
     ]
 
     self.assertEqual(expected_results, proc.connections(pid))



More information about the tor-commits mailing list