How the INET6_ATON() function works in Mariadb?

The INET6_ATON() function is a string function that converts an IPv6 address in standard or compressed notation to a binary value.

Posted on

The MariaDB INET6_ATON() function is used to convert an IPv6 address into a binary string. This function is essential for storing or processing IPv6 addresses in a database, as it allows for efficient storage and retrieval of these addresses in their binary form.

Syntax

MariaDB INET6_ATON() function’s syntax is as follows:

INET6_ATON(ipv6_address)

Where ipv6_address is an IPv6 address in its standard text representation. The function returns a binary string representing the IPv6 address.

Examples

Example 1: Basic Conversion

This example demonstrates converting a standard IPv6 address to its binary string equivalent.

SELECT INET6_ATON('2001:0db8:85a3:0000:0000:8a2e:0370:7334');

The output for this statement is:

+--------------------------------------------------------------------------------------------------------------+
| INET6_ATON('2001:0db8:85a3:0000:0000:8a2e:0370:7334')                                                        |
+--------------------------------------------------------------------------------------------------------------+
| 0x20010DB885A3000000008A2E03707334                                                                           |
+--------------------------------------------------------------------------------------------------------------+

This result represents the binary version of the IPv6 address.

Example 2: Conversion with Shortened Notation

Illustrating the function’s handling of IPv6 addresses with shortened notation.

SELECT INET6_ATON('2001:db8:85a3::8a2e:370:7334');

The output for this statement is:

+----------------------------------------------------------------------------------------+
| INET6_ATON('2001:db8:85a3::8a2e:370:7334')                                             |
+----------------------------------------------------------------------------------------+
| 0x20010DB885A3000000008A2E03707334                                                     |
+----------------------------------------------------------------------------------------+

The function correctly interprets the shortened notation and provides the same result as the full notation.

Example 3: Invalid IPv6 Address

Showing the function’s response to an invalid IPv6 address.

SELECT INET6_ATON('2001:db8::85a3::370:7334');

The output for this statement is:

NULL

Since the IPv6 address is invalid, the function returns NULL.

Example 4: Using INET6_ATON() with a Table

Creating a table to store IPv6 addresses and demonstrating the conversion.

DROP TABLE IF EXISTS ipv6_addresses;
CREATE TABLE ipv6_addresses (ipv6 VARCHAR(45));
INSERT INTO ipv6_addresses (ipv6) VALUES ('2001:db8:85a3::8a2e:370:7334'), ('::1');

SELECT ipv6, INET6_ATON(ipv6) AS binary_ipv6 FROM ipv6_addresses;

The output for this statement is:

+------------------------------+------------------------------------+
| ipv6                         | binary_ipv6                        |
+------------------------------+------------------------------------+
| 2001:db8:85a3::8a2e:370:7334 | 0x20010DB885A3000000008A2E03707334 |
| ::1                          | 0x00000000000000000000000000000001 |
+------------------------------+------------------------------------+

This table shows the original IPv6 addresses and their binary string equivalents.

Example 5: Loopback Address

Demonstrating the conversion of the loopback IPv6 address.

SELECT INET6_ATON('::1');

The output for this statement is:

+--------------------------------------+
| INET6_ATON('::1')                    |
+--------------------------------------+
| 0x00000000000000000000000000000001   |
+--------------------------------------+

The result is the binary representation of the loopback address ::1.

Below are a few functions related to the MariaDB INET6_ATON() function:

  • MariaDB INET6_NTOA() function is used to convert a binary string representing an IPv6 address back to its standard text representation.
  • MariaDB INET_ATON() function is used to convert an IPv4 address in dotted-quad notation to a numerical representation.
  • MariaDB INET_NTOA() function is used to convert a numerical representation of an IPv4 address back to its dotted-quad format.

Conclusion

The INET6_ATON() function in MariaDB is a crucial tool for handling IPv6 addresses within a database environment. It allows for the conversion of IPv6 addresses to a binary form, which is essential for efficient storage and manipulation of these addresses in MariaDB. Understanding how to use this function and its related functions can significantly enhance the management of IPv6 address data.