10000 [3.12] gh-122792: Make IPv4-mapped IPv6 address properties consistent… · python/cpython@e0c69e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0c69e4

Browse files
[3.12] gh-122792: Make IPv4-mapped IPv6 address properties consistent with IPv4 (GH-122793) (GH-123814)
gh-122792: Make IPv4-mapped IPv6 address properties consistent with IPv4 (GH-122793) Make IPv4-mapped IPv6 address properties consistent with IPv4. (cherry picked from commit 76a1c5d) Co-authored-by: Seth Michael Larson <seth@python.org>
1 parent ca3ac30 commit e0c69e4

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Lib/ipaddress.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,9 @@ def is_multicast(self):
20012001
See RFC 2373 2.7 for details.
20022002
20032003
"""
2004+
ipv4_mapped = self.ipv4_mapped
2005+
if ipv4_mapped is not None:
2006+
return ipv4_mapped.is_multicast
20042007
return self in self._constants._multicast_network
20052008

20062009
@property
@@ -2012,6 +2015,9 @@ def is_reserved(self):
20122015
reserved IPv6 Network ranges.
20132016
20142017
"""
2018+
ipv4_mapped = self.ipv4_mapped
2019+
if ipv4_mapped is not None:
2020+
return ipv4_mapped.is_reserved
20152021
return any(self in x for x in self._constants._reserved_networks)
20162022

20172023
@property
@@ -2022,6 +2028,9 @@ def is_link_local(self):
20222028
A boolean, True if the address is reserved per RFC 4291.
20232029
20242030
"""
2031+
ipv4_mapped = self.ipv4_mapped
2032+
if ipv4_mapped is not None:
2033+
return ipv4_mapped.is_link_local
20252034
return self in self._constants._linklocal_network
20262035

20272036
@property
@@ -2078,6 +2087,9 @@ def is_global(self):
20782087
``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
20792088
IPv4 range where they are both ``False``.
20802089
"""
2090+
ipv4_mapped = self.ipv4_mapped
2091+
if ipv4_mapped is not None:
2092+
return ipv4_mapped.is_global
20812093
return not self.is_private
20822094

20832095
@property
@@ -2089,6 +2101,9 @@ def is_unspecified(self):
20892101
RFC 2373 2.5.2.
20902102
20912103
"""
2104+
ipv4_mapped = self.ipv4_mapped
2105+
if ipv4_mapped is not None:
2106+
return ipv4_mapped.is_unspecified
20922107
return self._ip == 0
20932108

20942109
@property

Lib/test/test_ipaddress.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,30 @@ def testIpv4Mapped(self):
24212421
self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
24222422
ipaddress.ip_address('192.168.1.1'))
24232423

2424+
def testIpv4MappedProperties(self):
2425+
# Test that an IPv4 mapped IPv6 address has
2426+
# the same properties as an IPv4 address.
2427+
for addr4 in (
2428+
"178.62.3.251", # global
2429+
"169.254.169.254", # link local
2430+
"127.0.0.1", # loopback
2431+
"224.0.0.1", # multicast
2432+
"192.168.0.1", # private
2433+
"0.0.0.0", # unspecified
2434+
"100.64.0.1", # public and not global
2435+
):
2436+
with self.subTest(addr4):
2437+
ipv4 = ipaddress.IPv4Address(addr4)
2438+
ipv6 = ipaddress.IPv6Address(f"::ffff:{addr4}")
2439+
2440+
self.assertEqual(ipv4.is_global, ipv6.is_global)
2441+
self.assertEqual(ipv4.is_private, ipv6.is_private)
2442+
self.assertEqual(ipv4.is_reserved, ipv6.is_reserved)
2443+
self.assertEqual(ipv4.is_multicast, ipv6.is_multicast)
2444+
self.assertEqual(ipv4.is_unspecified, ipv6.is_unspecified)
2445+
self.assertEqual(ipv4.is_link_local, ipv6.is_link_local)
2446+
self.assertEqual(ipv4.is_loopback, ipv6.is_loopback)
2447+
24242448
def testIpv4MappedPrivateCheck(self):
24252449
self.assertEqual(
24262450
True, ipaddress.ip_address('::ffff:192.168.1.1').is_private)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Changed IPv4-mapped ``ipaddress.IPv6Address`` to consistently use the mapped IPv4
2+
address value for deciding properties. Properties which have their behavior fixed
3+
are ``is_multicast``, ``is_reserved``, ``is_link_local``, ``is_global``, and ``is_unspecified``.

0 commit comments

Comments
 (0)
0