Question

I'm working on a rails app that has an IPv6 model. I'm storing the IPv6 address in 2 32-bit ints and a 64-bit int:

+-----------------------+---------------------+------+-----+---------+----------------+
| Field                 | Type                | Null | Key | Default | Extra          |
+-----------------------+---------------------+------+-----+---------+----------------+
| global_routing_prefix | int(11) unsigned    | YES  |     | NULL    |                | 
| subnet_identifier     | int(11) unsigned    | YES  |     | NULL    |                | 
| interface_identifier  | bigint(20) unsigned | YES  |     | NULL    |                | 

Unfortunately, when I go to find IPs in a range, MySQL does all arithmetic with signed bigints, so:

mysql> select 2 BETWEEN 0 AND 18446744073709551614;
+--------------------------------------+
| 2 BETWEEN 0 AND 18446744073709551614 |
+--------------------------------------+
|                                    0 | 
+--------------------------------------+

Is there a work around I can do, or do I need to split up by interface_identifier into 2 unsigned ints?

Thanks, Donald

Was it helpful?

Solution

Doing it without using BETWEEN seems to work

mysql> select 0 <= 2 and 2 <= 18446744073709551614;
+--------------------------------------+
| 0 <= 2 and 2 <= 18446744073709551614 |
+--------------------------------------+
|                                    1 |
+--------------------------------------+

OTHER TIPS

Ugly solution:

select 2 BETWEEN 0 AND CAST(18446744073709551614 AS DECIMAL);

People at MontyAB are working on native IPv6 column type for MariaDB/MySQL. If you can support them, we're likely to have this feature sooner. ;)

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