From 7f3c522c564b79e0f96a25c7a94705a99a65d09e Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Wed, 26 Jan 2011 04:32:00 -0800 Subject: [PATCH] Move client._islocal() to helpers.islocal() in preparation for sharing. --- client.py | 17 +---------------- helpers.py | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/client.py b/client.py index a824bff..4f47226 100644 --- a/client.py +++ b/client.py @@ -6,21 +6,6 @@ from helpers import * _extra_fd = os.open('/dev/null', os.O_RDONLY) -def _islocal(ip): - sock = socket.socket() - try: - try: - sock.bind((ip, 0)) - except socket.error, e: - if e.args[0] == errno.EADDRNOTAVAIL: - return False # not a local IP - else: - raise - finally: - sock.close() - return True # it's a local IP, or there would have been an error - - def got_signal(signum, frame): log('exiting on signal %d\n' % signum) sys.exit(1) @@ -283,7 +268,7 @@ def _main(listener, fw, ssh_cmd, remotename, python, latency_control, dstip = original_dst(sock) debug1('Accept: %s:%r -> %s:%r.\n' % (srcip[0],srcip[1], dstip[0],dstip[1])) - if dstip[1] == listener.getsockname()[1] and _islocal(dstip[0]): + if dstip[1] == listener.getsockname()[1] and islocal(dstip[0]): debug1("-- ignored: that's my address!\n") sock.close() return diff --git a/helpers.py b/helpers.py index d8d7e85..c169d0c 100644 --- a/helpers.py +++ b/helpers.py @@ -1,4 +1,4 @@ -import sys, os +import sys, os, socket logprefix = '' verbose = 0 @@ -57,4 +57,19 @@ def resolvconf_random_nameserver(): else: return '127.0.0.1' - + +def islocal(ip): + sock = socket.socket() + try: + try: + sock.bind((ip, 0)) + except socket.error, e: + if e.args[0] == errno.EADDRNOTAVAIL: + return False # not a local IP + else: + raise + finally: + sock.close() + return True # it's a local IP, or there would have been an error + +