Why is INET6_ADDRSTRLEN defined as 46 in C?

Why is INET6_ADDRSTRLEN defined as 46 in C?

Because POSIX defines it to be 46:

INET6_ADDRSTRLEN
46. Length of the string form for IPv6.

While you are right that longtest IPv6 address takes 39 bytes, with IPv4 tunneling, the longest form can be 45 bytes:

ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255

And the 46th byte is for the terminating nul byte (in C a string). This explains how it came to be 46.


It's probably for the IPv4-mapped form of addresses of the form:

ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255

Read more:

Wireshark-dev mailing list

RFC 4291 section 2.2

Tags:

C

Sockets

Ipv6