From 250baa3feaaca651955ddb53324e4ff9faaf278f Mon Sep 17 00:00:00 2001 From: "pmoody@google.com" Date: Wed, 12 Jan 2011 22:56:45 +0000 Subject: + add 6to4 to the ipv6 tunnel decoding. git-svn-id: https://ipaddr-py.googlecode.com/svn@201 09200d28-7f98-11dd-ad27-0f66e57d2035 --- trunk/ipaddr.py | 21 ++++++++++++++++++--- trunk/ipaddr_test.py | 10 ++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/trunk/ipaddr.py b/trunk/ipaddr.py index dff40d3..261f3e2 100644 --- a/trunk/ipaddr.py +++ b/trunk/ipaddr.py @@ -1753,15 +1753,30 @@ class _BaseV6(object): """Tuple of embedded teredo IPs. Returns: - Tuple of the (server, client) IPs. + Tuple of the (server, client) IPs or False if the address + doesn't appear to be a teredo address (doesn't start with + 2001) - Note: - This doesn't try to verify that the address is a teredo address """ bits = self._explode_shorthand_ip_string().split(':') + if not bits[0] == '2001': + return False return (IPv4Address(int(''.join(bits[2:4]), 16)), IPv4Address(int(''.join(bits[6:]), 16) ^ 0xFFFFFFFF)) + def sixtofour(self): + """Return the IPv4 6to4 embedded address. + + Returns: + The IPv4 6to4-embedded address if present or False if the + address doesn't appear to contain a 6to4 embedded address. + + """ + bits = self._explode_shorthand_ip_string().split(':') + if not bits[0] == '2002': + return False + return IPv4Address(int(''.join(bits[1:3]), 16)) + class IPv6Address(_BaseV6, _BaseIP): diff --git a/trunk/ipaddr_test.py b/trunk/ipaddr_test.py index 739c5f6..657ac99 100755 --- a/trunk/ipaddr_test.py +++ b/trunk/ipaddr_test.py @@ -1024,6 +1024,16 @@ class IpaddrUnitTest(unittest.TestCase): teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2' self.assertEqual((server, client), ipaddr.IPAddress(teredo_addr).teredo()) + bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2' + self.assertFalse(ipaddr.IPAddress(bad_addr).teredo()) + + def testsixtofour(self): + sixtofouraddr = ipaddr.IPAddress('2002:ac1d:2d64::1') + bad_addr = ipaddr.IPAddress('2000:ac1d:2d64::1') + self.assertEqual(ipaddr.IPv4Address('172.29.45.100'), + sixtofouraddr.sixtofour()) + self.assertFalse(bad_addr.sixtofour()) + if __name__ == '__main__': unittest.main() -- cgit v1.2.1