From 8bfc52b7958d9eb9e673def23d196441ea6e1b74 Mon Sep 17 00:00:00 2001 From: Claudio-Daniel Freire Date: Fri, 7 Oct 2011 20:01:38 -0300 Subject: [PATCH] Make TCP handshake more robust (dice with 2G sides, rather than 6 - we're digital) --- src/nepi/util/tunchannel.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/nepi/util/tunchannel.py b/src/nepi/util/tunchannel.py index 03adfe54..735d08cd 100644 --- a/src/nepi/util/tunchannel.py +++ b/src/nepi/util/tunchannel.py @@ -669,17 +669,20 @@ def tcp_handshake(rsock, listen, hand): rsock.settimeout(10) try: rsock.send(hand) - peer_hand = rsock.recv(1) - print >>sys.stderr, "tcp_handshake: hand %s, peer_hand %s" % (hand, peer_hand) + peer_hand = rsock.recv(4) + if not peer_hand: + print >>sys.stderr, "tcp_handshake: connection reset by peer" + return False + else: + print >>sys.stderr, "tcp_handshake: hand %r, peer_hand %r" % (hand, peer_hand) if hand < peer_hand: if listen: win = True elif hand > peer_hand: if not listen: win = True - except socket.timeout: - pass - rsock.settimeout(0) + finally: + rsock.settimeout(0) return win def tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port): @@ -706,7 +709,7 @@ def tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port): break if TERMINATE: raise OSError, "Killed" - hand = str(random.randint(1, 6)) + hand = struct.pack("!L", random.randint(0, 2**30)) stop = [] lresult = [] rresult = [] -- 2.47.0