[tor-commits] [flashproxy/master] Use an exception in parse_socks_header instead of an error return.

dcf at torproject.org dcf at torproject.org
Wed Jul 4 14:33:23 UTC 2012


commit a04414d0518930fbfcdb21cfb6486e1efc9ed8b4
Author: David Fifield <david at bamsoftware.com>
Date:   Wed Jul 4 06:42:32 2012 -0700

    Use an exception in parse_socks_header instead of an error return.
---
 connector.py |   23 +++++++++++------------
 1 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/connector.py b/connector.py
index ed4f02c..353d6b5 100755
--- a/connector.py
+++ b/connector.py
@@ -554,26 +554,23 @@ def grab_string(s, pos):
     return pos, None
 
 def parse_socks_request(data):
+    """Parse the 8-byte SOCKS header at the beginning of data. Returns a
+    (dest, port) tuple. Raises ValueError on error."""
     try:
         ver, cmd, dport, o1, o2, o3, o4 = struct.unpack(">BBHBBBB", data[:8])
     except struct.error:
-        log(u"Couldn't unpack SOCKS4 header.")
-        return None
+        raise ValueError("Couldn't unpack SOCKS4 header")
     if ver != 4:
-        log(u"SOCKS header has wrong version (%d)." % ver)
-        return None
+        raise ValueError("SOCKS header has wrong version (%d)" % ver)
     if cmd != 1:
-        log(u"SOCKS header had wrong command (%d)." % cmd)
-        return None
+        raise ValueError("SOCKS header had wrong command (%d)" % cmd)
     pos, userid = grab_string(data, 8)
     if userid is None:
-        log(u"Couldn't read userid from SOCKS header.")
-        return None
+        raise ValueError("Couldn't read userid from SOCKS header")
     if o1 == 0 and o2 == 0 and o3 == 0 and o4 != 0:
         pos, dest = grab_string(data, pos)
         if dest is None:
-            log(u"Couldn't read destination from SOCKS4a header.")
-            return None
+            raise ValueError("Couldn't read destination from SOCKS4a header")
     else:
         dest = "%d.%d.%d.%d" % (o1, o2, o3, o4)
     return dest, dport
@@ -585,8 +582,10 @@ def handle_socks_request(fd):
     except socket.error, e:
         log(u"Socket error from SOCKS-pending: %s" % repr(str(e)))
         return False
-    dest_addr = parse_socks_request(data)
-    if dest_addr is None:
+    try:
+        dest_addr = parse_socks_request(data)
+    except ValueError, e:
+        log(u"Error parsing SOCKS header: %s." % str(e))
         # Error reply.
         fd.sendall(struct.pack(">BBHBBBB", 0, 91, 0, 0, 0, 0, 0))
         return False





More information about the tor-commits mailing list