1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
6 from twisted.python import log
7 from twisted.trial import unittest
9 isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+
10 '(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|'+
11 '(172\.0?([1][6-9])|([2][0-9])|([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})|'+
12 '(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$')
14 def findMyIPAddr(addrs, intended_port, local_ok = False):
15 """Find the best IP address to use from a list of possibilities.
17 @param addrs: the list of possible IP addresses
18 @param intended_port: the port that was supposed to be used
19 @param local_ok: whether known local/private IP ranges are allowed
21 @return: the preferred IP address, or None if one couldn't be found
23 log.msg("got addrs: %r" % (addrs,))
27 ifconfig = os.popen("/sbin/ifconfig |/bin/grep inet|"+
28 "/usr/bin/awk '{print $2}' | "+
29 "sed -e s/.*://", "r").read().strip().split('\n')
33 # Get counts for all the non-local addresses returned
36 if local_ok or not isLocal.match(addr):
37 addr_count.setdefault(addr, 0)
40 local_addrs = addr_count.keys()
41 if len(local_addrs) == 1:
42 my_addr = local_addrs[0]
43 log.msg('Found remote address from ifconfig: %r' % (my_addr,))
45 # Get counts for all the non-local addresses returned
49 if local_ok or not isLocal.match(addr[0]):
50 addr_count.setdefault(addr[0], 0)
51 addr_count[addr[0]] += 1
52 port_count.setdefault(addr[1], 0)
53 port_count[addr[1]] += 1
55 # Find the most popular address
58 for addr in addr_count:
59 if addr_count[addr] > popular_count:
61 popular_count = addr_count[addr]
62 elif addr_count[addr] == popular_count:
63 popular_addr.append(addr)
65 # Find the most popular port
68 for port in port_count:
69 if port_count[port] > popular_count:
71 popular_count = port_count[port]
72 elif port_count[port] == popular_count:
73 popular_port.append(port)
76 if len(port_count.keys()) > 1:
77 log.msg('Problem, multiple ports have been found: %r' % (port_count,))
78 if port not in port_count.keys():
79 log.msg('And none of the ports found match the intended one')
80 elif len(port_count.keys()) == 1:
81 port = port_count.keys()[0]
83 log.msg('Port was not found')
85 if len(popular_addr) == 1:
86 log.msg('Found popular address: %r' % (popular_addr[0],))
87 if my_addr and my_addr != popular_addr[0]:
88 log.msg('But the popular address does not match: %s != %s' % (popular_addr[0], my_addr))
89 my_addr = popular_addr[0]
90 elif len(popular_addr) > 1:
91 log.msg('Found multiple popular addresses: %r' % (popular_addr,))
92 if my_addr and my_addr not in popular_addr:
93 log.msg('And none of the addresses found match the ifconfig one')
95 log.msg('No non-local addresses found: %r' % (popular_addr,))
98 log.msg("Remote IP Address could not be found for this machine")
102 def ipAddrFromChicken():
104 ip_search = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
106 f = urllib.urlopen("http://www.ipchicken.com")
109 current_ip = ip_search.findall(data)
115 """Extract the contatc info from a compact peer representation.
118 @param s: the compact representation
119 @rtype: (C{string}, C{int})
120 @return: the IP address and port number to contact the peer on
121 @raise ValueError: if the compact representation doesn't exist
125 ip = '.'.join([str(ord(i)) for i in s[0:4]])
126 port = (ord(s[4]) << 8) | ord(s[5])
129 def compact(ip, port):
130 """Create a compact representation of peer contact info.
133 @param ip: the IP address of the peer
135 @param port: the port number to contact the peer on
137 @return: the compact representation
138 @raise ValueError: if the compact representation doesn't exist
141 s = ''.join([chr(int(i)) for i in ip.split('.')]) + \
142 chr((port & 0xFF00) >> 8) + chr(port & 0xFF)
147 class TestUtil(unittest.TestCase):
148 """Tests for the utilities."""
154 def test_compact(self):
155 d = uncompact(compact(self.ip, self.port))
156 self.failUnlessEqual(d[0], self.ip)
157 self.failUnlessEqual(d[1], self.port)