23

I have to create a field in a mysql table to store ip addresses. I need to know the maximum characters an ip address can possibly be. Whether ipv4, ipv6 or some other type that can render a valid ip address unusually long. So what should I set the character limit to?

I am asking for length in the sense that the word "foo" has 3 characters, not in terms of bytes or anything like that.

2

2 Answers 2

31

An IPv6 address, at its longest, would be 8 sets of 4 characters (32 total), each set separated by a colon. That makes 39 characters.

An IPv4 address is at most 4 sets of 3 numbers (12 characters), each set separated by a dot (.). That makes 15 characters.

All of this assumes the IP addresses should be stored in human-readable form. I prefer to store IP addresses in decimal form, as you never quite know how it will be used. Your mileage may vary.

3
  • Thanks a lot, human readable form is fine for my use case as I am never going to use them generally. I am only storing the ips of people who signup on my site in case they ever upload anything seriously offending and I have to hand over their info to authorities.
    – Lonand
    Jan 22, 2012 at 0:17
  • 6
    Sorry for the necropost, but actually an IPv6 can be up to 45 characters long in some cases of IPv4-mapped IPv6 addresses, since it is prefixed with :<ipv4 original address>
    – axelduch
    Aug 15, 2019 at 22:58
  • 1
    @axelduch *suffixed
    – Codemonkey
    May 15, 2022 at 13:15
2

This is not as easy as one might think.

In most cases your are safe with 45.

Normal IPv6 address

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

8 * 4 + 7 = 39


IPv4-mapped IPv6 address

See rfc4291

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

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


Header files define INET6_ADDRSTRLEN to be 46


IPv6 with scope zone

See rfc4007

Scope zone can be a number or even a string (interface name).

ff02::5678%5
ff08::9abc%interface10

The length of the interface name depends on the system. In Linux it is defined by the IFNAMSIZ constant. 16 is a common value. With the 45 chars from above:

45 + 1 + 16 = 62


Also see https://stackoverflow.com/questions/166132/maximum-length-of-the-textual-representation-of-an-ipv6-address

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .