Maximum length of the textual representation of an IPv6 address?

IpIp AddressIpv6

Ip Problem Overview


I want to store the data returned by $_SERVER["REMOTE_ADDR"] in PHP into a DB field, pretty simple task, really. The problem is that I can't find any proper information about the maximum length of the textual representation of an IPv6 address, which is what a webserver provides through $_SERVER["REMOTE_ADDR"].

I'm not interested in converting the textual representation into the 128 bits the address is usually encoded in, I just want to know how many characters maximum are needed to store any IPv6 address returned by $_SERVER["REMOTE_ADDR"].

Ip Solutions


Solution 1 - Ip

45 characters.

You might expect an address to be

0000:0000:0000:0000:0000:0000:0000:0000

> 8 * 4 + 7 = 39

8 groups of 4 digits with 7 : between them.

But if you have an IPv4-mapped IPv6 address, the last two groups can be written in base 10 separated by ., eg. [::ffff:192.168.100.228]. Written out fully:

0000:0000:0000:0000:0000:ffff:192.168.100.228

> (6 * 4 + 5) + 1 + (4 * 3 + 3) = 29 + 1 + 15 = 45

Note, this is an input/display convention - it's still a 128 bit address and for storage it would probably be best to standardise on the raw colon separated format, i.e. [0000:0000:0000:0000:0000:ffff:c0a8:64e4] for the address above.

Solution 2 - Ip

On Linux, see constant INET6_ADDRSTRLEN (include <arpa/inet.h>, see man inet_ntop). On my system (header "in.h"):

#define INET6_ADDRSTRLEN 46

The last character is for terminating NULL, as I belive, so the max length is 45, as other answers.

Solution 3 - Ip

Answered my own question:

> IPv6 addresses are normally written as eight groups of four hexadecimal digits, where each group is separated by a colon (:).

So that's 39 characters max.

Solution 4 - Ip

As indicated a standard ipv6 address is at most 45 chars, but an ipv6 address can also include an ending % followed by a "scope" or "zone" string, which has no fixed length but is generally a small positive integer or a network interface name, so in reality it can be bigger than 45 characters. Network interface names are typically "eth0", "eth1", "wlan0", so choosing 50 as the limit is likely good enough. The max interface name length in linux is 15 chars, so choosing 60 bytes will cover unusually long interface names on linux.

Solution 5 - Ip

I think @Deepak answer in this link is more close to correct answer. https://stackoverflow.com/questions/1076714/max-length-for-client-ip-address. So correct size is 45 not 39. Sometimes we try to scrounge in fields size but it seems to better if we prepare enough storage size.

Solution 6 - Ip

Watch out for certain headers such as HTTP_X_FORWARDED_FOR that appear to contain a single IP address. They may actually contain multiple addresses (a chain of proxies I assume).

They will appear to be comma delimited - and can be a lot longer than 45 characters total - so check before storing in DB.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGillesView Question on Stackoverflow
Solution 1 - IpMatthew ScharleyView Answer on Stackoverflow
Solution 2 - IpYuryView Answer on Stackoverflow
Solution 3 - IpGillesView Answer on Stackoverflow
Solution 4 - IpSean FView Answer on Stackoverflow
Solution 5 - IpQMasterView Answer on Stackoverflow
Solution 6 - IpSimon_WeaverView Answer on Stackoverflow