Question

As of MySQL 5.6 it there are some IPv6 related functions added.

I'm wondering why the new function IS_IPV4_COMPAT(expr) uses the deprected RFC-4291 to check whether the given binary address is a valid IPv4-compatible IPv6 address.

The MySQL documentation says:

This function takes an IPv6 address represented in numeric form as a binary string, as returned by INET6_ATON(). It returns 1 if the argument is a valid IPv4-compatible IPv6 address, 0 otherwise. IPv4-compatible addresses have the form ::ipv4_address.

mysql> SELECT IS_IPV4_COMPAT(INET6_ATON('::10.0.5.9'));
    -> 1
mysql> SELECT IS_IPV4_COMPAT(INET6_ATON('::ffff:10.0.5.9'));
    -> 0

But the [RFC-4291](https://www.rfc-editor.org/rfc/rfc4291#section-2.5.5) says:

    +--------------------------------------+--------------------------+
    |                80 bits               | 16 |      32 bits        |
    +--------------------------------------+--------------------------+
    |0000..............................0000|0000|    IPv4 address     |
    +--------------------------------------+----+---------------------+

> The "IPv4-Compatible IPv6 address" is now deprecated because the current IPv6 transition mechanisms no longer use these addresses. New or updated implementations are not required to support this address type.

    +--------------------------------------+----+---------------------+
    |                80 bits               | 16 |      32 bits        |
    +--------------------------------------+--------------------------+
    |0000..............................0000|FFFF|    IPv4 address     |
    +--------------------------------------+----+---------------------+

> See [RFC4038](https://www.rfc-editor.org/rfc/rfc4038) for background on the usage of the "IPv4-mapped IPv6 address".

**Which format should I use to store IPv4 addresses within a IPv6 context?**
Was it helpful?

Solution

There are two kinds of ways to address IPv4 in an IPv6 network. A "mapped" address scheme, and a "compatibility" address. RFC-4291 discusses them both. MySQL lets you query both of these seperatly. The compat address scheme has since been deprecated.

IS_IPV4_COMPAT() is for the compat address scheme. Even though that is deprecated, there's implementation that implemented it, and is still in use, and people might need to store them in a database.

Use IS_IPV4_MAPPED if you want the behavior of the mapped address scheme.

Which of these formats you should use depends on what you need to use them for. e.g. if you store addresses that are input to configuration of computers, you'd need to know which format they support. If you don't know, use the newer and current format (::ffff:10.0.5.9)

And if you need to support both formats, use IS_IPV4_COMPAT(field) or IS_IPV4_MAPPED(field) wyhen querying.

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