ChatGPT解决这个技术问题 Extra ChatGPT

Maximum length of the textual representation of an IPv6 address?

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"].

What about the zone index?
#define INET_ADDRSTRLEN (16) #define INET6_ADDRSTRLEN (48)
Source: lxr.free-electrons.com/source/include/linux/inet.h
The question stackoverflow.com/questions/1076714/… has some similar but useful answers.

C
Community

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.


Header files define INET6_ADDRSTRLEN to be 46, which fits with 45 chars plus a trailing null.
Worth noting that the IPv6 address might also contain the scope zone stackoverflow.com/questions/5746082/…, e.g. you get that from RemoteEndpointMessageProperty.Address
aren't ipv4 adresses written like ::ffff:127.0.0.1?
@Paladin: That's the shorthand version I believe, yes. They still expand out to full addresses with zeros as above though which means you need to be able to account for that as well.
does this take into account of percentage sign in ipv6 address? Refer - superuser.com/questions/99746/…
Y
Yury

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.


@eis probably was in 2013. Possibly incremented for struct alignment.
G
Gilles

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.


However there's apparently a caveat, see stackoverflow.com/a/7477384/3787376 ("Max length for client ip address") - Quote: "For IPv4 mapped IPv6 addresses, the string can be longer than 39 characters.". It says that an "IPv4-mapped IPv6" is 45 characters and is in the format "NNNN:NNNN:NNNN:NNNN:NNNN:NNNN:192.168.158.190". The maximum should therefore be 45 characters. The answer at stackoverflow.com/a/166157/3787376 (this question) also seems to demonstrate this point.
S
Sean F

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", a small number of chars. The max interface name length in linux is 15 chars, so choosing 61 bytes will cover all interface names on linux.


True, but this is only link-local addresses, and you aren't going to see any of those once your website is running on the Internet (and hopefully not even while developing it).
This answer deserves more attention. It's best to add 5 more characters than to debug such a weird issue for an hour, just to find out such exception. So, for people who want to know the true max amount of characters, this answer is the best of all.
Network interface naming conventions state that names cannot exceed more than 15 characters. Might as well add 45 + 1 (terminating null character) + 15 = 61 characters. Pad it by 3 more characters (for struct byte alignment) and we get 64 characters.
C
Community

I think @Deepak answer in this link is more close to correct answer. 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.


S
Simon_Weaver

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.


A slightly conflicted -1 (conflicted because I've fallen into this trap before, and I guess this answer contains info relevant to some people who will be arriving on this page) because nonetheless this clearly isn't an answer to the question asked. It would probably be better as a comment on the question.