Which IP Address Formats Are Allowed With MySQL INET6_ATON()?

The MySQL INET6_ATON() function accepts both, IPv4 and IPv6 addresses, as an argument:

-- IPv4
SELECT INET6_ATON('127.0.0.1')
-- IPv6
SELECT INET6_ATON('2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF')

If an invalid argument is supplied to INET6_ATON(), then it returns NULL:

-- invalid IP
SELECT INET6_ATON('foo') # null

Below is a list of IP addresses that are not allowed with the INET6_ATON() function:

Trying to convert any of these invalid values to their binary representation using the INET6_ATON() function will result in a NULL value:

-- IP with trailing zone ID
SELECT INET6_ATON('fe80::3%eth0') # null
-- IPv6 with trailing network mask
SELECT INET6_ATON('2001:45f:3:ba::/64') # null
-- IPv4 with trailing network mask
SELECT INET6_ATON('198.51.100.0/24') # null
-- IPv4 classful address
SELECT INET6_ATON('198.51.1') # null
-- IPv4 with trailing port
SELECT INET6_ATON('198.51.100.2:8080') # null
-- IPv4 hexadecimal in address component
SELECT INET6_ATON('198.0xa0.1.2') # null

Other than that, octal numbers are not supported. For example, 198.51.010.1 is interpreted as 198.51.10.1, and not 198.51.8.1:

-- IPv4 octal misinterpretation
SELECT INET6_NTOA(INET6_ATON('198.51.010.1')) # 198.51.10.1

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.