]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - apt_dht_Khashmir/util.py
43c3e443d2b5c6fefa901ad1e6fee6cb645e081e
[quix0rs-apt-p2p.git] / apt_dht_Khashmir / util.py
1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
3
4 import os, re
5
6 from twisted.python import log
7
8 def bucket_stats(l):
9     """given a list of khashmir instances, finds min, max, and average number of nodes in tables"""
10     max = avg = 0
11     min = None
12     def count(buckets):
13         c = 0
14         for bucket in buckets:
15             c = c + len(bucket.l)
16         return c
17     for node in l:
18         c = count(node.table.buckets)
19         if min == None:
20             min = c
21         elif c < min:
22             min = c
23         if c > max:
24             max = c
25         avg = avg + c
26     avg = avg / len(l)
27     return {'min':min, 'max':max, 'avg':avg}
28
29 isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+
30                      '(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|'+
31                      '(172\.0?([1][6-9])|([2][0-9])|([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})|'+
32                      '(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$')
33
34 def findMyIPAddr(addrs, intended_port):
35     log.msg("got addrs: %r" % (addrs,))
36     my_addr = None
37     
38     try:
39         ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+
40                             "/usr/bin/awk '{print $2}' | "+
41                             "sed -e s/.*://", "r").read().strip().split('\n')
42     except:
43         ifconfig = []
44
45     # Get counts for all the non-local addresses returned
46     addr_count = {}
47     for addr in ifconfig:
48         if not isLocal.match(addr):
49             addr_count.setdefault(addr, 0)
50             addr_count[addr] += 1
51     
52     local_addrs = addr_count.keys()    
53     if len(local_addrs) == 1:
54         my_addr = local_addrs[0]
55         log.msg('Found remote address from ifconfig: %r' % (my_addr,))
56     
57     # Get counts for all the non-local addresses returned
58     addr_count = {}
59     port_count = {}
60     for addr in addrs:
61         if not isLocal.match(addr[0]):
62             addr_count.setdefault(addr[0], 0)
63             addr_count[addr[0]] += 1
64             port_count.setdefault(addr[1], 0)
65             port_count[addr[1]] += 1
66     
67     # Find the most popular address
68     popular_addr = []
69     popular_count = 0
70     for addr in addr_count:
71         if addr_count[addr] > popular_count:
72             popular_addr = [addr]
73             popular_count = addr_count[addr]
74         elif addr_count[addr] == popular_count:
75             popular_addr.append(addr)
76     
77     # Find the most popular port
78     popular_port = []
79     popular_count = 0
80     for port in port_count:
81         if port_count[port] > popular_count:
82             popular_port = [port]
83             popular_count = port_count[port]
84         elif port_count[port] == popular_count:
85             popular_port.append(port)
86             
87     port = intended_port
88     if len(port_count.keys()) > 1:
89         log.msg('Problem, multiple ports have been found: %r' % (port_count,))
90         if port not in port_count.keys():
91             log.msg('And none of the ports found match the intended one')
92     elif len(port_count.keys()) == 1:
93         port = port_count.keys()[0]
94     else:
95         log.msg('Port was not found')
96
97     if len(popular_addr) == 1:
98         log.msg('Found popular address: %r' % (popular_addr[0],))
99         if my_addr and my_addr != popular_addr[0]:
100             log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], my_addr))
101         my_addr = popular_addr[0]
102     elif len(popular_addr) > 1:
103         log.msg('Found multiple popular addresses: %r' % (popular_addr,))
104         if my_addr and my_addr not in popular_addr:
105             log.msg('And none of the addresses found match the ifconfig one')
106     else:
107         log.msg('No non-local addresses found: %r' % (popular_addr,))
108         
109     if not my_addr:
110         log.msg("Remote IP Address could not be found for this machine")
111         
112     return my_addr
113
114 def ipAddrFromChicken():
115     import urllib
116     ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
117     try:
118          f = urllib.urlopen("http://www.ipchicken.com")
119          data = f.read()
120          f.close()
121          current_ip = ip_search.findall(data)
122          return current_ip
123     except Exception:
124          return []