Skip to content

gh-103268: Add IPv4-IPv6 Translation #103270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,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
Expand Down
13 changes: 13 additions & 0 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,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):

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add property to ipaddress.IPv6Address to report RFC6052/RFC8215 embedded IPv4Address
Loading