Question

I want to store ranges of IPs into database. The problem is that some IPs are ipv4 and some ipv6. I want to convert all to ipv6 then store into database. for example: according to converter:

192.168.1.100 => 0:0:0:0:0:ffff:c0a8:164

What makes problem is that php treats them in different way:

echo bin2hex(inet_pton('192.168.1.100'));
c0a80164

echo bin2hex(inet_pton('0:0:0:0:0:ffff:c0a8:164'));
00000000000000000000ffffc0a80164

echo bin2hex(inet_pton('0000:0000:0000:0000:0000:0000:192.168.1.100'));
000000000000000000000000c0a80164

to me it is so important that ip comparison(especially for ipv4) can be done in database with no problem.

Was it helpful?

Solution

I think it is not a good idea to store IPv4 addresses exactly like IPv6 addresses.

They are completely different things.

You may encounter the :ffff: version if you run a server which listens on an IPv6 socket woth IPV6ONLY disabled. Then, you'll notice the IPv4 host 192.168.1.100 connecting as ::ffff:c0a8:164. But you cannot connect to this address via IPv6 - for this purpose, you'd need an IPv4 socket. So better store the addresses in short form which makes it much easier to distinguish between them via the length.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top