in scripts/ValidateParameters/netaddr/fbsocket.py [0:0]
def inet_ntop(af, packed_ip):
"""Convert an packed IP address of the given family to string format."""
if af == AF_INET:
# IPv4.
return inet_ntoa(packed_ip)
elif af == AF_INET6:
# IPv6.
if len(packed_ip) != 16 or not _is_str(packed_ip):
raise ValueError('invalid length of packed IP address string')
tokens = ['%x' % i for i in _unpack('>8H', packed_ip)]
# Convert packed address to an integer value.
words = list(_unpack('>8H', packed_ip))
int_val = 0
for i, num in enumerate(reversed(words)):
word = num
word = word << 16 * i
int_val = int_val | word
if 0xffff < int_val <= 0xffffffff or int_val >> 32 == 0xffff:
# IPv4 compatible / mapped IPv6.
packed_ipv4 = _pack('>2H', *[int(i, 16) for i in tokens[-2:]])
ipv4_str = inet_ntoa(packed_ipv4)
tokens = tokens[0:-2] + [ipv4_str]
return ':'.join(_compact_ipv6_tokens(tokens))
else:
raise ValueError('unknown address family %d' % af)