1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
6 from twisted.python import log
9 """given a list of khashmir instances, finds min, max, and average number of nodes in tables"""
14 for bucket in buckets:
18 c = count(node.table.buckets)
27 return {'min':min, 'max':max, 'avg':avg}
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})$')
34 def findMyIPAddr(addrs, intended_port):
35 log.msg("got addrs: %r" % (addrs,))
39 ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+
40 "/usr/bin/awk '{print $2}' | "+
41 "sed -e s/.*://", "r").read().strip().split('\n')
45 # Get counts for all the non-local addresses returned
48 if not isLocal.match(addr):
49 addr_count.setdefault(addr, 0)
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,))
57 # Get counts for all the non-local addresses returned
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
67 # Find the most popular address
70 for addr in addr_count:
71 if addr_count[addr] > popular_count:
73 popular_count = addr_count[addr]
74 elif addr_count[addr] == popular_count:
75 popular_addr.append(addr)
77 # Find the most popular port
80 for port in port_count:
81 if port_count[port] > popular_count:
83 popular_count = port_count[port]
84 elif port_count[port] == popular_count:
85 popular_port.append(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]
95 log.msg('Port was not found')
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')
107 log.msg('No non-local addresses found: %r' % (popular_addr,))
110 log.msg("Remote IP Address could not be found for this machine")
114 def ipAddrFromChicken():
116 ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
118 f = urllib.urlopen("http://www.ipchicken.com")
121 current_ip = ip_search.findall(data)