[tor-commits] [stem/master] Support for TRANSPORT_LAUNCHED events

atagar at torproject.org atagar at torproject.org
Sun Aug 25 18:47:51 UTC 2013


commit f1e771a37828f85e3bd8631ad7234f50b0efac6e
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Aug 25 11:45:08 2013 -0700

    Support for TRANSPORT_LAUNCHED events
    
    Adding support for George's new TRANSPORT_LAUNCHED events...
    
    https://gitweb.torproject.org/torspec.git/commitdiff/48f6dd0
---
 stem/response/events.py      |   31 +++++++++++++++++++++++++++++++
 stem/version.py              |    2 ++
 test/unit/response/events.py |   19 +++++++++++++++++++
 3 files changed, 52 insertions(+)

diff --git a/stem/response/events.py b/stem/response/events.py
index f04a2a7..3a5cf41 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -882,6 +882,36 @@ class StreamBwEvent(Event):
     self.read = long(self.read)
     self.written = long(self.written)
 
+
+class TransportLaunchedEvent(Event):
+  """
+  Event triggered when a pluggable transport is launched.
+
+  The TRANSPORT_LAUNCHED event was introduced in tor version 0.2.5.0-alpha.
+
+  :var str type: 'server' or 'client'
+  :var str name: name of the pluggable transport
+  :var str address: IPv4 or IPv6 address where the transport is listening for
+    connections
+  :var int port: port where the transport is listening for connections
+  """
+
+  _POSITIONAL_ARGS = ("type", "name", "address", "port")
+  _VERSION_ADDED = stem.version.Requirement.EVENT_TRANSPORT_LAUNCHED
+
+  def _parse(self):
+    if not self.type in ('server', 'client'):
+      raise stem.ProtocolError("Transport type should either be 'server' or 'client': %s" % self)
+
+    if not connection.is_valid_ipv4_address(self.address) and \
+       not connection.is_valid_ipv6_address(self.address):
+      raise stem.ProtocolError("Transport address isn't a valid IPv4 or IPv6 address: %s" % self)
+
+    if not connection.is_valid_port(self.port):
+      raise stem.ProtocolError("Transport port is invalid: %s" % self)
+
+    self.port = int(self.port)
+
 EVENT_TYPE_TO_CLASS = {
   "ADDRMAP": AddrMapEvent,
   "AUTHDIR_NEWDESCS": AuthDirNewDescEvent,
@@ -907,6 +937,7 @@ EVENT_TYPE_TO_CLASS = {
   "STATUS_SERVER": StatusEvent,
   "STREAM": StreamEvent,
   "STREAM_BW": StreamBwEvent,
+  "TRANSPORT_LAUNCHED": TransportLaunchedEvent,
   "WARN": LogEvent,
 
   # accounting for a bug in tor 0.2.0.22
diff --git a/stem/version.py b/stem/version.py
index 4d3d772..3630611 100644
--- a/stem/version.py
+++ b/stem/version.py
@@ -42,6 +42,7 @@ easily parsed and compared, for instance...
   **EVENT_SIGNAL**                      SIGNAL events
   **EVENT_STATUS**                      STATUS_GENERAL, STATUS_CLIENT, and STATUS_SERVER events
   **EVENT_STREAM_BW**                   STREAM_BW events
+  **EVENT_TRANSPORT_LAUNCHED**          TRANSPORT_LAUNCHED events
   **EXTENDCIRCUIT_PATH_OPTIONAL**       EXTENDCIRCUIT queries can omit the path if the circuit is zero
   **FEATURE_EXTENDED_EVENTS**           'EXTENDED_EVENTS' optional feature
   **FEATURE_VERBOSE_NAMES**             'VERBOSE_NAMES' optional feature
@@ -311,6 +312,7 @@ Requirement = stem.util.enum.Enum(
   ("EVENT_SIGNAL", Version('0.2.3.1-alpha')),
   ("EVENT_STATUS", Version('0.1.2.3-alpha')),
   ("EVENT_STREAM_BW", Version('0.1.2.8-beta')),
+  ("EVENT_TRANSPORT_LAUNCHED", Version('0.2.5.0-alpha')),
   ("EXTENDCIRCUIT_PATH_OPTIONAL", Version("0.2.2.9")),
   ("FEATURE_EXTENDED_EVENTS", Version("0.2.2.1-alpha")),
   ("FEATURE_VERBOSE_NAMES", Version("0.2.2.1-alpha")),
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 9c4de01..23b7006 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -319,6 +319,11 @@ PURPOSE=DNS_REQUEST"
 
 STREAM_NEWRESOLVE_IP6 = "650 STREAM 23 NEWRESOLVE 0 2001:db8::1:0 PURPOSE=DNS_REQUEST"
 
+TRANSPORT_LAUNCHED = "650 TRANSPORT_LAUNCHED server obfs1 127.0.0.1 1111"
+TRANSPORT_LAUNCHED_BAD_TYPE = "650 TRANSPORT_LAUNCHED unicorn obfs1 127.0.0.1 1111"
+TRANSPORT_LAUNCHED_BAD_ADDRESS = "650 TRANSPORT_LAUNCHED server obfs1 127.0.x.y 1111"
+TRANSPORT_LAUNCHED_BAD_PORT = "650 TRANSPORT_LAUNCHED server obfs1 127.0.0.1 my_port"
+
 
 def _get_event(content):
   controller_event = mocking.get_message(content)
@@ -1161,6 +1166,20 @@ class TestEvents(unittest.TestCase):
     self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2 15 -25")
     self.assertRaises(ProtocolError, _get_event, "650 STREAM_BW 2 x 25")
 
+  def test_transport_launched_event(self):
+    event = _get_event(TRANSPORT_LAUNCHED)
+
+    self.assertTrue(isinstance(event, stem.response.events.TransportLaunchedEvent))
+    self.assertEqual(TRANSPORT_LAUNCHED.lstrip("650 "), str(event))
+    self.assertEqual("server", event.type)
+    self.assertEqual("obfs1", event.name)
+    self.assertEqual("127.0.0.1", event.address)
+    self.assertEqual(1111, event.port)
+
+    self.assertRaises(ProtocolError, _get_event, TRANSPORT_LAUNCHED_BAD_TYPE)
+    self.assertRaises(ProtocolError, _get_event, TRANSPORT_LAUNCHED_BAD_ADDRESS)
+    self.assertRaises(ProtocolError, _get_event, TRANSPORT_LAUNCHED_BAD_PORT)
+
   def test_unrecognized_enum_logging(self):
     """
     Checks that when event parsing gets a value that isn't recognized by stem's



More information about the tor-commits mailing list