From 61a8482b596a221a97e0f3cdcc7452468bee85f1 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Tue, 15 Jan 2008 14:36:19 -0800 Subject: [PATCH] Add an option to not error out when only a local IP address can be found. --- apt_dht/apt_dht.py | 4 +++- apt_dht/apt_dht_conf.py | 3 +++ apt_dht/util.py | 14 +++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/apt_dht/apt_dht.py b/apt_dht/apt_dht.py index 047d1c2..749396b 100644 --- a/apt_dht/apt_dht.py +++ b/apt_dht/apt_dht.py @@ -42,7 +42,9 @@ class AptDHT: return self.http_site def joinComplete(self, result): - self.my_addr = findMyIPAddr(result, config.getint(config.get('DEFAULT', 'DHT'), 'PORT')) + self.my_addr = findMyIPAddr(result, + config.getint(config.get('DEFAULT', 'DHT'), 'PORT'), + config.getboolean('DEFAULT', 'LOCAL_OK')) if not self.my_addr: raise RuntimeError, "IP address for this machine could not be found" self.cache.scanDirectories() diff --git a/apt_dht/apt_dht_conf.py b/apt_dht/apt_dht_conf.py index 621ef36..4f77d6f 100644 --- a/apt_dht/apt_dht_conf.py +++ b/apt_dht/apt_dht_conf.py @@ -32,6 +32,9 @@ DEFAULTS = { # User name to try and run as 'USERNAME': '', + + # Whether it's OK to use an IP addres from a known local/private range + 'LOCAL_OK': 'no', # Which DHT implementation to use. # It must be possile to do "from .DHT import DHT" to get a class that diff --git a/apt_dht/util.py b/apt_dht/util.py index 8444900..df8d014 100644 --- a/apt_dht/util.py +++ b/apt_dht/util.py @@ -10,7 +10,15 @@ isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+ '(172\.0?([1][6-9])|([2][0-9])|([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})|'+ '(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$') -def findMyIPAddr(addrs, intended_port): +def findMyIPAddr(addrs, intended_port, local_ok = False): + """Find the best IP address to use from a list of possibilities. + + @param addrs: the list of possible IP addresses + @param intended_port: the port that was supposed to be used + @param local_ok: whether known local/private IP ranges are allowed + (defaults to False) + @return: the preferred IP address, or None if one couldn't be found + """ log.msg("got addrs: %r" % (addrs,)) my_addr = None @@ -24,7 +32,7 @@ def findMyIPAddr(addrs, intended_port): # Get counts for all the non-local addresses returned addr_count = {} for addr in ifconfig: - if not isLocal.match(addr): + if local_ok or not isLocal.match(addr): addr_count.setdefault(addr, 0) addr_count[addr] += 1 @@ -37,7 +45,7 @@ def findMyIPAddr(addrs, intended_port): addr_count = {} port_count = {} for addr in addrs: - if not isLocal.match(addr[0]): + if local_ok or not isLocal.match(addr[0]): addr_count.setdefault(addr[0], 0) addr_count[addr[0]] += 1 port_count.setdefault(addr[1], 0) -- 2.39.5