]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
Move the main util functions to the new main util module.
authorCameron Dale <camrdale@gmail.com>
Sat, 12 Jan 2008 19:28:13 +0000 (11:28 -0800)
committerCameron Dale <camrdale@gmail.com>
Sat, 12 Jan 2008 19:28:13 +0000 (11:28 -0800)
apt_dht/apt_dht.py
apt_dht/util.py [new file with mode: 0644]
apt_dht_Khashmir/util.py

index 0682c8add3928ab324b60e11126b97518fb0f9cf..f25e2347a39609169f9a855f555646119887be4b 100644 (file)
@@ -33,6 +33,8 @@ class AptDHT:
     
     def joinComplete(self, result):
         self.my_addr = findMyIPAddr(result, config.getint(config.get('DEFAULT', 'DHT'), 'PORT'))
+        if not self.my_addr:
+            raise RuntimeError, "IP address for this machine could not be found"
 
     def joinError(self, failure):
         log.msg("joining DHT failed miserably")
diff --git a/apt_dht/util.py b/apt_dht/util.py
new file mode 100644 (file)
index 0000000..8444900
--- /dev/null
@@ -0,0 +1,103 @@
+## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
+# see LICENSE.txt for license information
+
+import os, re
+
+from twisted.python import log
+
+isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+
+                     '(10\.[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):
+    log.msg("got addrs: %r" % (addrs,))
+    my_addr = None
+    
+    try:
+        ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+
+                            "/usr/bin/awk '{print $2}' | "+
+                            "sed -e s/.*://", "r").read().strip().split('\n')
+    except:
+        ifconfig = []
+
+    # Get counts for all the non-local addresses returned
+    addr_count = {}
+    for addr in ifconfig:
+        if not isLocal.match(addr):
+            addr_count.setdefault(addr, 0)
+            addr_count[addr] += 1
+    
+    local_addrs = addr_count.keys()    
+    if len(local_addrs) == 1:
+        my_addr = local_addrs[0]
+        log.msg('Found remote address from ifconfig: %r' % (my_addr,))
+    
+    # Get counts for all the non-local addresses returned
+    addr_count = {}
+    port_count = {}
+    for addr in addrs:
+        if not isLocal.match(addr[0]):
+            addr_count.setdefault(addr[0], 0)
+            addr_count[addr[0]] += 1
+            port_count.setdefault(addr[1], 0)
+            port_count[addr[1]] += 1
+    
+    # Find the most popular address
+    popular_addr = []
+    popular_count = 0
+    for addr in addr_count:
+        if addr_count[addr] > popular_count:
+            popular_addr = [addr]
+            popular_count = addr_count[addr]
+        elif addr_count[addr] == popular_count:
+            popular_addr.append(addr)
+    
+    # Find the most popular port
+    popular_port = []
+    popular_count = 0
+    for port in port_count:
+        if port_count[port] > popular_count:
+            popular_port = [port]
+            popular_count = port_count[port]
+        elif port_count[port] == popular_count:
+            popular_port.append(port)
+            
+    port = intended_port
+    if len(port_count.keys()) > 1:
+        log.msg('Problem, multiple ports have been found: %r' % (port_count,))
+        if port not in port_count.keys():
+            log.msg('And none of the ports found match the intended one')
+    elif len(port_count.keys()) == 1:
+        port = port_count.keys()[0]
+    else:
+        log.msg('Port was not found')
+
+    if len(popular_addr) == 1:
+        log.msg('Found popular address: %r' % (popular_addr[0],))
+        if my_addr and my_addr != popular_addr[0]:
+            log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], my_addr))
+        my_addr = popular_addr[0]
+    elif len(popular_addr) > 1:
+        log.msg('Found multiple popular addresses: %r' % (popular_addr,))
+        if my_addr and my_addr not in popular_addr:
+            log.msg('And none of the addresses found match the ifconfig one')
+    else:
+        log.msg('No non-local addresses found: %r' % (popular_addr,))
+        
+    if not my_addr:
+        log.msg("Remote IP Address could not be found for this machine")
+        
+    return my_addr
+
+def ipAddrFromChicken():
+    import urllib
+    ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
+    try:
+         f = urllib.urlopen("http://www.ipchicken.com")
+         data = f.read()
+         f.close()
+         current_ip = ip_search.findall(data)
+         return current_ip
+    except Exception:
+         return []
index 43c3e443d2b5c6fefa901ad1e6fee6cb645e081e..780885789496c9afbbfe0ec0ae247d1f1048d6cc 100644 (file)
@@ -1,10 +1,6 @@
 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
 # see LICENSE.txt for license information
 
-import os, re
-
-from twisted.python import log
-
 def bucket_stats(l):
     """given a list of khashmir instances, finds min, max, and average number of nodes in tables"""
     max = avg = 0
@@ -25,100 +21,3 @@ def bucket_stats(l):
         avg = avg + c
     avg = avg / len(l)
     return {'min':min, 'max':max, 'avg':avg}
-
-isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+
-                     '(10\.[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):
-    log.msg("got addrs: %r" % (addrs,))
-    my_addr = None
-    
-    try:
-        ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+
-                            "/usr/bin/awk '{print $2}' | "+
-                            "sed -e s/.*://", "r").read().strip().split('\n')
-    except:
-        ifconfig = []
-
-    # Get counts for all the non-local addresses returned
-    addr_count = {}
-    for addr in ifconfig:
-        if not isLocal.match(addr):
-            addr_count.setdefault(addr, 0)
-            addr_count[addr] += 1
-    
-    local_addrs = addr_count.keys()    
-    if len(local_addrs) == 1:
-        my_addr = local_addrs[0]
-        log.msg('Found remote address from ifconfig: %r' % (my_addr,))
-    
-    # Get counts for all the non-local addresses returned
-    addr_count = {}
-    port_count = {}
-    for addr in addrs:
-        if not isLocal.match(addr[0]):
-            addr_count.setdefault(addr[0], 0)
-            addr_count[addr[0]] += 1
-            port_count.setdefault(addr[1], 0)
-            port_count[addr[1]] += 1
-    
-    # Find the most popular address
-    popular_addr = []
-    popular_count = 0
-    for addr in addr_count:
-        if addr_count[addr] > popular_count:
-            popular_addr = [addr]
-            popular_count = addr_count[addr]
-        elif addr_count[addr] == popular_count:
-            popular_addr.append(addr)
-    
-    # Find the most popular port
-    popular_port = []
-    popular_count = 0
-    for port in port_count:
-        if port_count[port] > popular_count:
-            popular_port = [port]
-            popular_count = port_count[port]
-        elif port_count[port] == popular_count:
-            popular_port.append(port)
-            
-    port = intended_port
-    if len(port_count.keys()) > 1:
-        log.msg('Problem, multiple ports have been found: %r' % (port_count,))
-        if port not in port_count.keys():
-            log.msg('And none of the ports found match the intended one')
-    elif len(port_count.keys()) == 1:
-        port = port_count.keys()[0]
-    else:
-        log.msg('Port was not found')
-
-    if len(popular_addr) == 1:
-        log.msg('Found popular address: %r' % (popular_addr[0],))
-        if my_addr and my_addr != popular_addr[0]:
-            log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], my_addr))
-        my_addr = popular_addr[0]
-    elif len(popular_addr) > 1:
-        log.msg('Found multiple popular addresses: %r' % (popular_addr,))
-        if my_addr and my_addr not in popular_addr:
-            log.msg('And none of the addresses found match the ifconfig one')
-    else:
-        log.msg('No non-local addresses found: %r' % (popular_addr,))
-        
-    if not my_addr:
-        log.msg("Remote IP Address could not be found for this machine")
-        
-    return my_addr
-
-def ipAddrFromChicken():
-    import urllib
-    ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
-    try:
-         f = urllib.urlopen("http://www.ipchicken.com")
-         data = f.read()
-         f.close()
-         current_ip = ip_search.findall(data)
-         return current_ip
-    except Exception:
-         return []