From f492f3916423040afecd433c0b9fde65c2d7a5e8 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 5 Apr 2023 11:11:17 +0200 Subject: [PATCH 1/5] Add IPv4-IPv6 Translation This does not handle translation with a Network-Specific Prefix, which must be known beforehand when embedding IPv4 addresses with RFC 6052. https://www.rfc-editor.org/rfc/rfc6052.html#section-2.2 --- Lib/ipaddress.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index 1cb71d8032e173..e55d4e0c75c428 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -2094,6 +2094,19 @@ def sixtofour(self): return None return IPv4Address((self._ip >> 80) & 0xFFFFFFFF) + @property + def ipv4_translation(self): + """Return the IPv4/IPv6 Translation embedded address. + + Returns: + The IPv4/IPv6 Translation embedded address if present or None + if the address doesn't appear to contain a translation address. + + """ + if (self._ip >> 96) != 0x64FF9B: + return None + return IPv4Address(self._ip & 0xFFFFFFFF) + class IPv6Interface(IPv6Address): From 0cc5ace4de9472e669504cba75dc2e1979695b06 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 6 Apr 2023 09:06:52 +0200 Subject: [PATCH 2/5] Add Test for IPv6 ipv4_translation --- Lib/test/test_ipaddress.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index a5388b2e5debd8..5fca93e21b9d1a 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -2623,6 +2623,16 @@ def testsixtofour(self): sixtofouraddr.sixtofour) self.assertFalse(bad_addr.sixtofour) + def testipv4_translation(self): + embedded_rfc6052 = ipaddress.ip_address('64:ff9b::172.29.45.100') + embedded_rfc8215 = ipaddress.ip_address('64:ff9b:1::172.29.45.100') + bad_addr = ipaddress.ip_address('2001:db8::172.29.45.100') + self.assertEqual(ipaddress.IPv4Address('172.29.45.100'), + embedded_rfc6052.ipv4_translation) + self.assertEqual(ipaddress.IPv4Address('172.29.45.100'), + embedded_rfc8215.ipv4_translation) + self.assertFalse(bad_addr.ipv4_translation) + # issue41004 Hash collisions in IPv4Interface and IPv6Interface def testV4HashIsNotConstant(self): ipv4_address1 = ipaddress.IPv4Interface("1.2.3.4") From dd547851e7a628e15de1449dc41fcb30d67c986f Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 11 Apr 2023 19:55:21 +0200 Subject: [PATCH 3/5] Update ipaddress.rst --- Doc/library/ipaddress.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst index 9c2dff55703273..04f8d483641816 100644 --- a/Doc/library/ipaddress.rst +++ b/Doc/library/ipaddress.rst @@ -343,6 +343,13 @@ write code that handles both IP versions correctly. Address objects are the embedded ``(server, client)`` IP address pair. For any other address, this property will be ``None``. + .. attribute:: ipv4_translation + + For addresses that appear to be IPv6/IPv4 translation addresses + (starting with ``64:ff9b::/96``) as defined by :RFC:`6052` and :RFC:`8215`, + This property will report the embedded IPv4 address. For any other + address, this property will be ``None``. + .. method:: IPv6Address.__format__(fmt) Refer to the corresponding method documentation in From ecd90b8761b4ab248a82f37a5546996737f06d98 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 11 Apr 2023 18:02:39 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-04-11-18-02-38.gh-issue-103268.7FN7nG.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-11-18-02-38.gh-issue-103268.7FN7nG.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-11-18-02-38.gh-issue-103268.7FN7nG.rst b/Misc/NEWS.d/next/Library/2023-04-11-18-02-38.gh-issue-103268.7FN7nG.rst new file mode 100644 index 00000000000000..fade459e3ad591 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-11-18-02-38.gh-issue-103268.7FN7nG.rst @@ -0,0 +1 @@ +Add property to ipaddress.IPv6Address to report RFC6052/RFC8215 embedded IPv4Address From 94518985299d68f76e4e5dff27a8b0c24f8499d1 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 18 Apr 2025 11:52:38 +0200 Subject: [PATCH 5/5] Update ipaddress.rst Remove trailing spaces to appease Lint bot --- Doc/library/ipaddress.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst index 127ef77eafe4ef..cebf8255bc92d7 100644 --- a/Doc/library/ipaddress.rst +++ b/Doc/library/ipaddress.rst @@ -385,8 +385,8 @@ write code that handles both IP versions correctly. Address objects are .. attribute:: ipv4_translation - For addresses that appear to be IPv6/IPv4 translation addresses - (starting with ``64:ff9b::/96``) as defined by :RFC:`6052` and :RFC:`8215`, + For addresses that appear to be IPv6/IPv4 translation addresses + (starting with ``64:ff9b::/96``) as defined by :RFC:`6052` and :RFC:`8215`, This property will report the embedded IPv4 address. For any other address, this property will be ``None``.