Skip to content

Commit 9df171d

Browse files
[3.11] gh-123409: fix IPv6Address.reverse_pointer for IPv4-mapped addresses (GH-123419) (GH-135087)
Fix functionality that was broken with better textual representation for IPv4-mapped addresses (gh-87799) (cherry picked from commit 77a2fb4) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent da37555 commit 9df171d

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

Lib/ipaddress.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,12 +1967,21 @@ def __init__(self, address):
19671967
def _explode_shorthand_ip_string(self):
19681968
ipv4_mapped = self.ipv4_mapped
19691969
if ipv4_mapped is None:
1970-
long_form = super()._explode_shorthand_ip_string()
1971-
else:
1972-
prefix_len = 30
1973-
raw_exploded_str = super()._explode_shorthand_ip_string()
1974-
long_form = "%s%s" % (raw_exploded_str[:prefix_len], str(ipv4_mapped))
1975-
return long_form
1970+
return super()._explode_shorthand_ip_string()
1971+
prefix_len = 30
1972+
raw_exploded_str = super()._explode_shorthand_ip_string()
1973+
return f"{raw_exploded_str[:prefix_len]}{ipv4_mapped!s}"
1974+
1975+
def _reverse_pointer(self):
1976+
ipv4_mapped = self.ipv4_mapped
1977+
if ipv4_mapped is None:
1978+
return super()._reverse_pointer()
1979+
prefix_len = 30
1980+
raw_exploded_str = super()._explode_shorthand_ip_string()[:prefix_len]
1981+
# ipv4 encoded using hexadecimal nibbles instead of decimals
1982+
ipv4_int = ipv4_mapped._ip
1983+
reverse_chars = f"{raw_exploded_str}{ipv4_int:008x}"[::-1].replace(':', '')
1984+
return '.'.join(reverse_chars) + '.ip6.arpa'
19761985

19771986
def _ipv4_mapped_ipv6_to_str(self):
19781987
"""Return convenient text representation of IPv4-mapped IPv6 address

Lib/test/test_ipaddress.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,12 +2596,42 @@ def testExplodeShortHandIpStr(self):
25962596
self.assertEqual('192.168.178.1', addr4.exploded)
25972597

25982598
def testReversePointer(self):
2599-
addr1 = ipaddress.IPv4Address('127.0.0.1')
2600-
addr2 = ipaddress.IPv6Address('2001:db8::1')
2601-
self.assertEqual('1.0.0.127.in-addr.arpa', addr1.reverse_pointer)
2602-
self.assertEqual('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.' +
2603-
'b.d.0.1.0.0.2.ip6.arpa',
2604-
addr2.reverse_pointer)
2599+
for addr_v4, expected in [
2600+
('127.0.0.1', '1.0.0.127.in-addr.arpa'),
2601+
# test vector: https://www.rfc-editor.org/rfc/rfc1035, §3.5
2602+
('10.2.0.52', '52.0.2.10.in-addr.arpa'),
2603+
]:
2604+
with self.subTest('ipv4_reverse_pointer', addr=addr_v4):
2605+
addr = ipaddress.IPv4Address(addr_v4)
2606+
self.assertEqual(addr.reverse_pointer, expected)
2607+
2608+
for addr_v6, expected in [
2609+
(
2610+
'2001:db8::1', (
2611+
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.'
2612+
'0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.'
2613+
'ip6.arpa'
2614+
)
2615+
),
2616+
(
2617+
'::FFFF:192.168.1.35', (
2618+
'3.2.1.0.8.a.0.c.f.f.f.f.0.0.0.0.'
2619+
'0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.'
2620+
'ip6.arpa'
2621+
)
2622+
),
2623+
# test vector: https://www.rfc-editor.org/rfc/rfc3596, §2.5
2624+
(
2625+
'4321:0:1:2:3:4:567:89ab', (
2626+
'b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.'
2627+
'2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.'
2628+
'ip6.arpa'
2629+
)
2630+
)
2631+
]:
2632+
with self.subTest('ipv6_reverse_pointer', addr=addr_v6):
2633+
addr = ipaddress.IPv6Address(addr_v6)
2634+
self.assertEqual(addr.reverse_pointer, expected)
26052635

26062636
def testIntRepresentation(self):
26072637
self.assertEqual(16909060, int(self.ipv4_address))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :attr:`ipaddress.IPv6Address.reverse_pointer` output according to
2+
:rfc:`RFC 3596, §2.5 <3596#section-2.5>`. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)