Improve the textual representation of IPv4-mapped IPv6 addresses

Hi, Python supports IPv4-mapped IPv6 addresses as defined by RFC 4038: "the IPv6 address ::FFFF:x.y.z.w represents the IPv4 address x.y.z.w.” The current behavior is as follows: from ipaddress import ip_address addr = ip_address('::ffff:8.8.4.4') # IPv6Address('::ffff:808:404') addr.ipv4_mapped # IPv4Address('8.8.4.4') Note that the textual representation of the IPv6Address is *not* in IPv4-mapped format. It prints ::ffff:808:404 instead of ::ffff:8.8.4.4. This is technically correct, but it’s somewhat frustrating as it makes it harder to read IPv4s embedded in IPv6 addresses. My proposal would be to check, in __str__, if an IPv6 is an IPv4-mapped, and to return the appropriate representation : from ipaddress import ip_address addr = ip_address('::ffff:8.8.4.4') # Current behavior str(addr) # '::ffff:808:404' repr(addr) # IPv6Address('::ffff:808:404') # Proposed behavior str(addr) # '::ffff:8.8.4.4' repr(addr) # IPv6Address('::ffff:8.8.4.4') A few data points: - Julia prints ::ffff:808:404 (current behavior) - C (glibc) and ClickHouse prints ::ffff:8.8.4.4 (proposed behavior) Any thoughts? Maxime

On 18 Mar 2021, at 11:07, Maxime Mouchet <maxime.mouchet@lip6.fr> wrote:
Hi,
Python supports IPv4-mapped IPv6 addresses as defined by RFC 4038: "the IPv6 address ::FFFF:x.y.z.w represents the IPv4 address x.y.z.w.”
The current behavior is as follows:
from ipaddress import ip_address addr = ip_address('::ffff:8.8.4.4') # IPv6Address('::ffff:808:404') addr.ipv4_mapped # IPv4Address('8.8.4.4')
Note that the textual representation of the IPv6Address is *not* in IPv4-mapped format. It prints ::ffff:808:404 instead of ::ffff:8.8.4.4. This is technically correct, but it’s somewhat frustrating as it makes it harder to read IPv4s embedded in IPv6 addresses.
My proposal would be to check, in __str__, if an IPv6 is an IPv4-mapped, and to return the appropriate representation :
from ipaddress import ip_address addr = ip_address('::ffff:8.8.4.4')
# Current behavior str(addr) # '::ffff:808:404' repr(addr) # IPv6Address('::ffff:808:404')
# Proposed behavior str(addr) # '::ffff:8.8.4.4' repr(addr) # IPv6Address('::ffff:8.8.4.4')
A few data points: - Julia prints ::ffff:808:404 (current behavior) - C (glibc) and ClickHouse prints ::ffff:8.8.4.4 (proposed behavior)
Any thoughts?
This looks like a reasonable request (as someone that’s not the ipaddr maintainer and doesn’t really use IPv6). Could you file an issue about this? Ronald
participants (2)
-
Maxime Mouchet
-
Ronald Oussoren