Add an option to not error out when only a local IP address can be found.
authorCameron Dale <camrdale@gmail.com>
Tue, 15 Jan 2008 22:36:19 +0000 (14:36 -0800)
committerCameron Dale <camrdale@gmail.com>
Tue, 15 Jan 2008 22:36:19 +0000 (14:36 -0800)
apt_dht/apt_dht.py
apt_dht/apt_dht_conf.py
apt_dht/util.py

index 047d1c2b815dbf7c3d3643244c5f04fa9b8d0fad..749396b3a36f9b3922c79af4c30bc487f5572594 100644 (file)
@@ -42,7 +42,9 @@ class AptDHT:
         return self.http_site
     
     def joinComplete(self, result):
         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()
         if not self.my_addr:
             raise RuntimeError, "IP address for this machine could not be found"
         self.cache.scanDirectories()
index 621ef367e9e999a440ea82a278aa57e4ef077fcd..4f77d6f61188ad0550cce674c182b18b9d9cff2a 100644 (file)
@@ -32,6 +32,9 @@ DEFAULTS = {
     
     # User name to try and run as
     'USERNAME': '',
     
     # 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>.DHT import DHT" to get a class that
 
     # Which DHT implementation to use.
     # It must be possile to do "from <DHT>.DHT import DHT" to get a class that
index 84449006336bad591d449fd1c29ea40fff7c5ae7..df8d014558c31d4cc42a08daea8d5f5a087e1cf6 100644 (file)
@@ -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})$')
 
                      '(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
     
     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:
     # 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
     
             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:
     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)
             addr_count.setdefault(addr[0], 0)
             addr_count[addr[0]] += 1
             port_count.setdefault(addr[1], 0)