Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPv6 address representation in Python

Tags:

python

ipv6

I was converting IPv6 addresses to the textual representation and noticed a behavior I could not explain:

In[38]: socket.inet_ntop(socket.AF_INET6, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x01')
Out[38]: '::ffff:127.0.0.1'
In[39]: socket.inet_ntop(socket.AF_INET6, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00')
Out[39]: '::ff:ffff:7f00:0'

I am surprised to see ::ffff:127.0.0.1, I'd expect it to be ::ffff:7f00:0. Is it standard or at least common? Which IPv6 addresses are represented this way? The Wikipedia article doesn't mention it at all. I am confused.

like image 393
FireAphis Avatar asked Jun 14 '15 08:06

FireAphis


1 Answers

The POSIX page for inet_ntop specifies that format as one of the options (slightly paraphrased):

A third form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d, where the x characters are the hexadecimal values of the six high-order 16-bit pieces of the address, and the d characters are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation).

like image 75
paxdiablo Avatar answered Sep 20 '22 22:09

paxdiablo