From 13bd755ae65c5978243ad6e7028a478dc51b7547 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 31 Mar 2005 05:44:44 +0000 Subject: [PATCH] checkpoint fixes to TorControl svn:r3923 --- contrib/TorControl.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/contrib/TorControl.py b/contrib/TorControl.py index 60ef345827..5a63ad4187 100755 --- a/contrib/TorControl.py +++ b/contrib/TorControl.py @@ -114,7 +114,6 @@ def parseHostAndPort(h): return host, port - def _unpack_msg(msg): "return None, minLength, body or type,body,rest" if len(msg) < 4: @@ -173,6 +172,14 @@ def unpack_msg(msg): minLength = _minLengthToPack(inOtherPackets) return None, len(msg)+leftInPacket+inOtherPackets, msg +def _receive_msg(s): + body = "" + header = s.recv(4) + length,type = struct.unpack("!HH",header) + if length: + body = s.recv(length) + return length,type,body + def receive_message(s): length, tp, body = _receive_msg(s) if tp != MSG_TYPE.FRAGMENTHEADER: @@ -295,10 +302,16 @@ def extend_circuit(s, circid, hops): tp, body = receive_reply(s,[MSG_TYPE.DONE]) if len(body) != 4: raise ProtocolError("Extendcircuit reply too short or long") - return struct.unpack("!L",body) + return struct.unpack("!L",body)[0] def redirect_stream(s, streamid, newtarget): msg = struct.pack("!L",streamid) + newtarget + "\0" + send_message(s,MSG_TYPE.REDIRECTSTREAM,msg) + tp,body = receive_reply(s,[MSG_TYPE.DONE]) + +def attach_stream(s, streamid, circid): + msg = struct.pack("!LL",streamid, circid) + send_message(s,MSG_TYPE.ATTACHSTREAM,msg) tp,body = receive_reply(s,[MSG_TYPE.DONE]) def _unterminate(s): @@ -310,9 +323,9 @@ def _unterminate(s): def unpack_event(body): if len(body)<2: raise ProtocolError("EVENT body too short.") - evtype = struct.unpack("!H", body[:2]) + evtype, = struct.unpack("!H", body[:2]) body = body[2:] - if evtype == EVENT_TYPE.CIRCUITSTATUS: + if evtype == EVENT_TYPE.CIRCSTATUS: if len(body)<5: raise ProtocolError("CIRCUITSTATUS event too short.") status,ident = struct.unpack("!BL", body[:5]) -- 2.11.4.GIT