]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Add dns.ipv6.is_mapped(); Reverse IPv6 mapped IPv4 into v4 space 54/head
authorBob Halley <halley@dnspython.org>
Sat, 31 May 2014 18:17:38 +0000 (11:17 -0700)
committerBob Halley <halley@dnspython.org>
Sat, 31 May 2014 18:17:38 +0000 (11:17 -0700)
ChangeLog
dns/ipv6.py
dns/reversename.py
tests/test_name.py
tests/test_ntoaaton.py

index b41357278b741cec38bb7b557d55ffd8bea34798..9ee3bf73e92f160967188f34af77866c69924e31 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-05-31  Bob Halley  <halley@dnspython.org>
+
+       * dns/ipv6.py: Add is_mapped()
+
+       * dns/reversename.py: Lookup IPv6 mapped IPv4 addresses in the v4
+         reverse namespace.  Thanks to Devin Bayer.  Yes, I finally fixed
+         this one :)
+
 2014-04-11  Bob Halley  <halley@dnspython.org>
 
        * dns/zone.py: Do not put back an unescaped token.  This was
index 1ab00da9772b8d1ad075b1f31688b74e5b21c18a..bf658af175baaf73722b2bff516dc5327207c0c6 100644 (file)
@@ -161,3 +161,8 @@ def inet_aton(text):
         return text.decode('hex_codec')
     except TypeError:
         raise dns.exception.SyntaxError
+
+_mapped_prefix = '\x00' * 10 + '\xff\xff'
+
+def is_mapped(address):
+    return address.startswith(_mapped_prefix)
index 4925cfd6ffdf17dc14ee142481cfd87f4fd22407..b80c60a0b19dd159baace79cb4db385c6682a8c2 100644 (file)
@@ -37,8 +37,13 @@ def from_address(text):
     @rtype: dns.name.Name object
     """
     try:
-        parts = list(dns.ipv6.inet_aton(text).encode('hex_codec'))
-        origin = ipv6_reverse_domain
+        v6 = dns.ipv6.inet_aton(text)
+        if dns.ipv6.is_mapped(v6):
+            parts = ['%d' % ord(byte) for byte in v6[12:]]
+            origin = ipv4_reverse_domain
+        else:
+            parts = list(v6.encode('hex_codec'))
+            origin = ipv6_reverse_domain
     except:
         parts = ['%d' % ord(byte) for byte in dns.ipv4.inet_aton(text)]
         origin = ipv4_reverse_domain
index e30e43d3a221eb1fbb6e4b99560deb836037ad5e..894a1a438a5fdd3951ae65d6a6b4e5704bf988ba 100644 (file)
@@ -25,7 +25,7 @@ import dns.e164
 class NameTestCase(unittest.TestCase):
     def setUp(self):
         self.origin = dns.name.from_text('example.')
-        
+
     def testFromTextRel1(self):
         n = dns.name.from_text('foo.bar')
         self.failUnless(n.labels == ('foo', 'bar', ''))
@@ -352,7 +352,7 @@ class NameTestCase(unittest.TestCase):
         n = dns.name.from_text('FOO.bar', None)
         d = n.to_digestable(dns.name.root)
         self.failUnless(d == '\x03foo\x03bar\x00')
-        
+
     def testBadDigestable(self):
         def bad():
             n = dns.name.from_text('FOO.bar', None)
@@ -659,6 +659,11 @@ class NameTestCase(unittest.TestCase):
         n = dns.reversename.from_address('::1')
         self.failUnless(e == n)
 
+    def testReverseIPv6MappedIpv4(self):
+        e = dns.name.from_text('1.0.0.127.in-addr.arpa.')
+        n = dns.reversename.from_address('::ffff:127.0.0.1')
+        self.failUnless(e == n)
+
     def testBadReverseIPv4(self):
         def bad():
             n = dns.reversename.from_address('127.0.foo.1')
index e93de2de21035c0ed97471cc61e31fe5ab85be4a..5e33a92d8e0f534623401d5287d3579242e32397 100644 (file)
@@ -199,5 +199,13 @@ class NtoAAtoNTestCase(unittest.TestCase):
         t1 = ntoa6(b1)
         self.failUnless(t1 == addr)
 
+    def test_is_mapped(self):
+        t1 = '2001:db8:0:1:1:1:1:1'
+        t2 = '::ffff:127.0.0.1'
+        t3 = '1::ffff:127.0.0.1'
+        self.failIf(dns.ipv6.is_mapped(aton6(t1)))
+        self.failUnless(dns.ipv6.is_mapped(aton6(t2)))
+        self.failIf(dns.ipv6.is_mapped(aton6(t3)))
+
 if __name__ == '__main__':
     unittest.main()