Secondly are you suggesting that you need 4 bytes minimally to store an IP4 address? e.g. 0000 without the periods?
Yes, that's the optimal way to store it.
I don't see how that would actually work since you could have an IP like 255.255.255.255 would be 15 characters.
I think your confusion results from thinking in terms of strings of digits, rather than binary representation of numbers.
A byte (8 bits) can store a value between 0 and 255. You can remove the periods and represent an IPv4 address as:-
Byte 1 : [0-255]
Byte 2 : [0-255]
Byte 3 : [0-255]
Byte 4 : [0-255]
The PHP function ip2long and MySQL function INET_ATON take the 15 char string sequence of an IP address with the dots and converts it to the above.
This way it's smaller to strore, and also faster to index or search, as you can use integer maths to do compares, rather than a string compare. As far as anything doing something with it is concerned, it looks and behaves like a 4 byte integer.
If you need it back as a string, you can use the PHP long2ip function, which in effect does something akin to:-
printf("%n.%n.%n.%n", byte1, byte2, byte3, byte4)
IPv6 uses 16 byte addresses
That's right - sorry, my original post incorrectly states 6 bytes. So as stated, you'd actually have to store as 2 BIGINTS, which is possibly why the INET_ATON etc functions are not yet in MySQL or PHP.
I can see a good reason here for a new datastructure type - a superBigInt, of perhaps 16 or 32 bytes.
You could use a 16 byte CHAR, but lack of being to use integer type maths on it may be sub-optimal from a performance point of view.
Going back to your original post:-
I noticed that when using long2ip on certain integers for IP addresses that many IP addresses were being listed as 127.255.255.255. Clearly the integers were hitting the 32 bit maximum integer of 2130706433.
That looks to me as though you've used a singed integer type, rather than unsigned?