Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store both IPv4 and IPv6 address in a single column

Tags:

mysql

ipv6

ipv4

I want to be able to store both IPv4 and IPv6 addresses in my table. What is the most efficient way to store the IP address of a user regardless of whether it is an IPv4 or IPv6 address?

This will be used in a production environment, so future proof'd suggestions are preferred.

like image 482
user2650277 Avatar asked Mar 25 '14 14:03

user2650277


People also ask

Can I have both IPv4 and IPv6 address?

IPv4 and IPv6 must coexist for some number of years, and their coexistence must be transparent to end users. If an IPv4-to-IPv6 transition is successful, end users should not even notice it. A dual-stack device is a device with network interfaces that can originate and understand both IPv4 and IPv6 packets.

Can a single host support both IPv4 and IPv6?

“IPv4 and IPv6 addresses are not compatible. Pawlik [NRO chairma] said it was technically feasible to make the two types of IP address talk to each other, but it would be best for companies and ISPs to switch to IPv6 as soon as possible, to keep networks stable and avoid complexity when IPv4 addresses run out.”

Can a machine with a single DNS name have both IPv4 and IPv6 addresses?

Yes you can have both IPv4 and IPv6 addresses associated with the same dns name. As a general rule services should be made available over both ipv4 and ipv6 and the same public facing DNS name used for both.

What is IPv4/IPv6 dual stack?

What Is It? Dual stack means that devices are able to run IPv4 and IPv6 in parallel. It allows hosts to simultaneously reach IPv4 and IPv6 content, so it offers a very flexible coexistence strategy.


1 Answers

I would recommend to store every address in IPv6 format. There is an official mapping for that: the IPv4-mapped IPv6 address. It works like this:

Take for example IPv4 address 192.0.2.44
The IPv4-mapped IPv6 address would be ::ffff:192.0.2.44
Which can also be written as ::ffff:c000:022c (192 decimal is c0 hexadecimal, etc)

You can use the inet_pton() function to parse such addresses, and on my local system the inet_ntop() function also outputs them in the most readable format (::ffff:192.0.2.44). That way you only have one format to deal with in your application.

Also see this related answer.

like image 168
Sander Steffann Avatar answered Nov 04 '22 06:11

Sander Steffann