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


A slightly incorrect, naive option would be:

8 * 4 + 7 = 39

8 groups of 4 digits with 7 ':' between them.

But to take into account the IPv4-mapped IPv6 addresses feature, eg. [::ffff:192.168.0.1] , written out fully:

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

Note, this is an input/display convention - the amount of data is the same and for storage it would probably be best to standardise on the raw colon separated format, ie. [0000:0000:0000:0000:ffff:7fa8:0001] for the address above.


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.


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.

链接地址: http://www.djcxy.com/p/40608.html

上一篇: 如何检查是否有人通过IPv6 / IPv4连接

下一篇: IPv6地址的文本表示的最大长度?