[tor-commits] [pytorctl/master] 9771 - Fix CIRC event parsing

aagbsn at torproject.org aagbsn at torproject.org
Thu Sep 19 11:06:04 UTC 2013


commit c161f352e7fabd88cdd7eb9249a55957038b07d9
Author: aagbsn <aagbsn at extc.org>
Date:   Thu Sep 19 12:44:50 2013 +0200

    9771 - Fix CIRC event parsing
    
    In torspec.git commit e195a4c8d288eb27385060740b8fde170a5e3e38 the
    format of the CIRC event was changed slightly. This commit parses this
    newer format.
---
 TorCtl.py |   46 ++++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/TorCtl.py b/TorCtl.py
index 9bba58d..c5747f4 100755
--- a/TorCtl.py
+++ b/TorCtl.py
@@ -1357,38 +1357,24 @@ class EventHandler(EventSink):
       evtype,body = body,""
     evtype = evtype.upper()
     if evtype == "CIRC":
-      m = re.match(r"(\d+)\s+(\S+)(\s\S+)?(\s\S+)?(\s\S+)?(\s\S+)?", body)
-      if not m:
-        raise ProtocolError("CIRC event misformatted.")
-      ident,status,path,purpose,reason,remote = m.groups()
-      ident = int(ident)
-      if path:
-        if "PURPOSE=" in path:
-          remote = reason
-          reason = purpose
-          purpose=path
-          path=[]
-        elif "REASON=" in path:
-          remote = reason
-          reason = path
-          purpose = ""
-          path=[]
-        else:
-          path_verb = path.strip().split(",")
-          path = []
-          for p in path_verb:
-            path.append(p.replace("~", "=").split("=")[0])
+      fields = body.split()
+      (ident,status),rest = fields[:2], fields[2:]
+      if rest[0].startswith('$'):
+        path = rest.pop(0)
+        path_verb = path.strip().split(",")
+        path = []
+        for p in path_verb:
+          path.append(p.replace("~", "=").split("=")[0])
       else:
         path = []
-
-      if purpose and "REASON=" in purpose:
-        remote=reason
-        reason=purpose
-        purpose=""
-
-      if purpose: purpose = purpose[9:]
-      if reason: reason = reason[8:]
-      if remote: remote = remote[15:]
+      try:
+        kwargs = dict([i.split('=') for i in rest])
+      except ValueError:
+        raise ProtocolError("CIRC event misformatted.")
+      ident = int(ident)
+      purpose = kwargs.get('PURPOSE', None)
+      reason = kwargs.get('REASON', None)
+      remote = kwargs.get('REMOTE_REASON', None)
       event = CircuitEvent(evtype, ident, status, path, purpose, reason,
                            remote, body)
     elif evtype == "STREAM":



More information about the tor-commits mailing list