Skip to content

Commit b58da40

Browse files
miss-islingtonsethmlarsonambv
authored
[3.10] gh-122792: Make IPv4-mapped IPv6 address properties consistent with IPv4 (GH-122793) (GH-123819)
Make IPv4-mapped IPv6 address properties consistent with IPv4. (cherry picked from commit 76a1c5d) Co-authored-by: Seth Michael Larson <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
1 parent b3a6042 commit b58da40

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

Lib/ipaddress.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def collapse_addresses(addresses):
310310
[IPv4Network('192.0.2.0/24')]
311311
312312
Args:
313-
addresses: An iterator of IPv4Network or IPv6Network objects.
313+
addresses: An iterable of IPv4Network or IPv6Network objects.
314314
315315
Returns:
316316
An iterator of the collapsed IPv(4|6)Network objects.
@@ -1840,9 +1840,6 @@ def _string_from_ip_int(cls, ip_int=None):
18401840
def _explode_shorthand_ip_string(self):
18411841
"""Expand a shortened IPv6 address.
18421842
1843-
Args:
1844-
ip_str: A string, the IPv6 address.
1845-
18461843
Returns:
18471844
A string, the expanded IPv6 address.
18481845
@@ -1986,6 +1983,9 @@ def is_multicast(self):
19861983
See RFC 2373 2.7 for details.
19871984
19881985
"""
1986+
ipv4_mapped = self.ipv4_mapped
1987+
if ipv4_mapped is not None:
1988+
return ipv4_mapped.is_multicast
19891989
return self in self._constants._multicast_network
19901990

19911991
@property
@@ -1997,6 +1997,9 @@ def is_reserved(self):
19971997
reserved IPv6 Network ranges.
19981998
19991999
"""
2000+
ipv4_mapped = self.ipv4_mapped
2001+
if ipv4_mapped is not None:
2002+
return ipv4_mapped.is_reserved
20002003
return any(self in x for x in self._constants._reserved_networks)
20012004

20022005
@property
@@ -2007,6 +2010,9 @@ def is_link_local(self):
20072010
A boolean, True if the address is reserved per RFC 4291.
20082011
20092012
"""
2013+
ipv4_mapped = self.ipv4_mapped
2014+
if ipv4_mapped is not None:
2015+
return ipv4_mapped.is_link_local
20102016
return self in self._constants._linklocal_network
20112017

20122018
@property
@@ -2063,6 +2069,9 @@ def is_global(self):
20632069
``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
20642070
IPv4 range where they are both ``False``.
20652071
"""
2072+
ipv4_mapped = self.ipv4_mapped
2073+
if ipv4_mapped is not None:
2074+
return ipv4_mapped.is_global
20662075
return not self.is_private
20672076

20682077
@property
@@ -2074,6 +2083,9 @@ def is_unspecified(self):
20742083
RFC 2373 2.5.2.
20752084
20762085
"""
2086+
ipv4_mapped = self.ipv4_mapped
2087+
if ipv4_mapped is not None:
2088+
return ipv4_mapped.is_unspecified
20772089
return self._ip == 0
20782090

20792091
@property
@@ -2085,6 +2097,9 @@ def is_loopback(self):
20852097
RFC 2373 2.5.3.
20862098
20872099
"""
2100+
ipv4_mapped = self.ipv4_mapped
2101+
if ipv4_mapped is not None:
2102+
return ipv4_mapped.is_loopback
20882103
return self._ip == 1
20892104

20902105
@property
@@ -2201,7 +2216,7 @@ def is_unspecified(self):
22012216

22022217
@property
22032218
def is_loopback(self):
2204-
return self._ip == 1 and self.network.is_loopback
2219+
return super().is_loopback and self.network.is_loopback
22052220

22062221

22072222
class IPv6Network(_BaseV6, _BaseNetwork):
@@ -2314,6 +2329,8 @@ class _IPv6Constants:
23142329
IPv6Network('2001:db8::/32'),
23152330
# IANA says N/A, let's consider it not globally reachable to be safe
23162331
IPv6Network('2002::/16'),
2332+
# RFC 9637: https://www.rfc-editor.org/rfc/rfc9637.html#section-6-2.2
2333+
IPv6Network('3fff::/20'),
23172334
IPv6Network('fc00::/7'),
23182335
IPv6Network('fe80::/10'),
23192336
]

Lib/test/test_ipaddress.py

+24
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,30 @@ def testIpv4Mapped(self):
24152415
self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
24162416
ipaddress.ip_address('192.168.1.1'))
24172417

2418+
def testIpv4MappedProperties(self):
2419+
# Test that an IPv4 mapped IPv6 address has
2420+
# the same properties as an IPv4 address.
2421+
for addr4 in (
2422+
"178.62.3.251", # global
2423+
"169.254.169.254", # link local
2424+
"127.0.0.1", # loopback
2425+
"224.0.0.1", # multicast
2426+
"192.168.0.1", # private
2427+
"0.0.0.0", # unspecified
2428+
"100.64.0.1", # public and not global
2429+
):
2430+
with self.subTest(addr4):
2431+
ipv4 = ipaddress.IPv4Address(addr4)
2432+
ipv6 = ipaddress.IPv6Address(f"::ffff:{addr4}")
2433+
2434+
self.assertEqual(ipv4.is_global, ipv6.is_global)
2435+
self.assertEqual(ipv4.is_private, ipv6.is_private)
2436+
self.assertEqual(ipv4.is_reserved, ipv6.is_reserved)
2437+
self.assertEqual(ipv4.is_multicast, ipv6.is_multicast)
2438+
self.assertEqual(ipv4.is_unspecified, ipv6.is_unspecified)
2439+
self.assertEqual(ipv4.is_link_local, ipv6.is_link_local)
2440+
self.assertEqual(ipv4.is_loopback, ipv6.is_loopback)
2441+
24182442
def testIpv4MappedPrivateCheck(self):
24192443
self.assertEqual(
24202444
True, ipaddress.ip_address('::ffff:192.168.1.1').is_private)
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)