Select IP 范围内的 CIDR
Select CIDR that is in range of IP
所以我有一个像 45.76.255.14 这样的 IP,并且我有一个 table,CIDR 行存储为单个 varchar,如何
我 select 个在该 IP 地址范围内的 CIDR。例如 45.76.255.14/31
所以理论上:select CIDR 在 IP
范围内
借助这个问题:
这是适合我的解决方案:
SELECT
`cidr`
FROM
cidr_list
WHERE
INET_ATON('IP') BETWEEN(
INET_ATON(SUBSTRING_INDEX(`cidr`, '/', 1)) & 0xffffffff ^(
(
0x1 <<(
32 - SUBSTRING_INDEX(`cidr`, '/', -1)
)
) -1
)
) AND(
INET_ATON(SUBSTRING_INDEX(`cidr`, '/', 1)) |(
(
0x100000000 >> SUBSTRING_INDEX(`cidr`, '/', -1)
) -1
)
)
在 VARCHAR
中以点分四组表示法存储 IP 地址并不是最佳的存储方式,因为点分四组是 32 位无符号整数的人性化表示,不适合自己到数据库索引。但有时它从根本上更方便,而且在小规模下,查询需要 table 扫描这一事实通常不是问题。
MySQL 存储函数是将相对复杂的逻辑封装在可在查询中引用的简单函数后面的好方法,可能会导致更易于理解的查询并减少 copy/paste 错误.
所以,这是我写的一个存储函数,叫做 find_ip4_in_cidr4()
。它的工作方式有点类似于内置函数 FIND_IN_SET()
——你给它一个值,然后给它一个 "set"(CIDR 规范),它 returns 一个值来指示该值是否在集合中。
首先,说明函数的作用:
如果地址在块内,return 前缀长度。为什么return前缀长度?非零整数是 "true," 所以我们可以 return 1
,但是如果你想对匹配结果进行排序以找到多个匹配前缀中最短或最长的,你可以 ORDER BY
函数的 return 值。
mysql> SELECT find_ip4_in_cidr4('203.0.113.123','203.0.113.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('203.0.113.123','203.0.113.0/24') |
+-----------------------------------------------------+
| 24 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','192.168.0.0/16');
+-----------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','192.168.0.0/16') |
+-----------------------------------------------------+
| 16 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
不在街区?即 returns 0 (false).
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','203.0.113.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','203.0.113.0/24') |
+-----------------------------------------------------+
| 0 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','192.168.0.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','192.168.0.0/24') |
+-----------------------------------------------------+
| 0 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
全零地址有一个特例,我们return -1(仍然"true",但保留排序顺序):
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','0.0.0.0/0');
+------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','0.0.0.0/0') |
+------------------------------------------------+
| -1 |
+------------------------------------------------+
1 row in set (0.00 sec)
胡说八道return null:
mysql> SELECT find_ip4_in_cidr4('234.467.891.0','192.168.0.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('234.467.891.0','192.168.0.0/24') |
+-----------------------------------------------------+
| NULL |
+-----------------------------------------------------+
1 row in set (0.00 sec)
现在,代码:
DELIMITER $$
DROP FUNCTION IF EXISTS `find_ip4_in_cidr4` $$
CREATE DEFINER=`mezzell`@`%` FUNCTION `find_ip4_in_cidr4`(
_address VARCHAR(15),
_block VARCHAR(18)
) RETURNS TINYINT
DETERMINISTIC /* for a given input, this function always returns the same output */
CONTAINS SQL /* the function does not read from or write to tables */
BEGIN
-- given an IPv4 address and a cidr spec,
-- return -1 for a valid address inside 0.0.0.0/0
-- return prefix length if the address is within the block,
-- return 0 if the address is outside the block,
-- otherwise return null
DECLARE _ip_aton INT UNSIGNED DEFAULT INET_ATON(_address);
DECLARE _cidr_aton INT UNSIGNED DEFAULT INET_ATON(SUBSTRING_INDEX(_block,'/',1));
DECLARE _prefix TINYINT UNSIGNED DEFAULT SUBSTRING_INDEX(_block,'/',-1);
DECLARE _bitmask INT UNSIGNED DEFAULT (0xFFFFFFFF << (32 - _prefix)) & 0xFFFFFFFF;
RETURN CASE /* the first match, not "best" match is used in a CASE expression */
WHEN _ip_aton IS NULL OR _cidr_aton IS NULL OR /* sanity checks */
_prefix IS NULL OR _bitmask IS NULL OR
_prefix NOT BETWEEN 0 AND 32 OR
(_prefix = 0 AND _cidr_aton != 0) THEN NULL
WHEN _cidr_aton = 0 AND _bitmask = 0 THEN -1
WHEN _ip_aton & _bitmask = _cidr_aton & _bitmask THEN _prefix /* here's the only actual test needed */
ELSE 0 END;
END $$
DELIMITER ;
一个不特定于存储函数但适用于大多数 RDBMS 平台上的大多数函数的问题是,当列用作 WHERE
中函数的参数时,服务器无法"look backwards"通过函数使用索引优化查询。
所以我有一个像 45.76.255.14 这样的 IP,并且我有一个 table,CIDR 行存储为单个 varchar,如何 我 select 个在该 IP 地址范围内的 CIDR。例如 45.76.255.14/31
所以理论上:select CIDR 在 IP
范围内借助这个问题:
这是适合我的解决方案:
SELECT
`cidr`
FROM
cidr_list
WHERE
INET_ATON('IP') BETWEEN(
INET_ATON(SUBSTRING_INDEX(`cidr`, '/', 1)) & 0xffffffff ^(
(
0x1 <<(
32 - SUBSTRING_INDEX(`cidr`, '/', -1)
)
) -1
)
) AND(
INET_ATON(SUBSTRING_INDEX(`cidr`, '/', 1)) |(
(
0x100000000 >> SUBSTRING_INDEX(`cidr`, '/', -1)
) -1
)
)
在 VARCHAR
中以点分四组表示法存储 IP 地址并不是最佳的存储方式,因为点分四组是 32 位无符号整数的人性化表示,不适合自己到数据库索引。但有时它从根本上更方便,而且在小规模下,查询需要 table 扫描这一事实通常不是问题。
MySQL 存储函数是将相对复杂的逻辑封装在可在查询中引用的简单函数后面的好方法,可能会导致更易于理解的查询并减少 copy/paste 错误.
所以,这是我写的一个存储函数,叫做 find_ip4_in_cidr4()
。它的工作方式有点类似于内置函数 FIND_IN_SET()
——你给它一个值,然后给它一个 "set"(CIDR 规范),它 returns 一个值来指示该值是否在集合中。
首先,说明函数的作用:
如果地址在块内,return 前缀长度。为什么return前缀长度?非零整数是 "true," 所以我们可以 return 1
,但是如果你想对匹配结果进行排序以找到多个匹配前缀中最短或最长的,你可以 ORDER BY
函数的 return 值。
mysql> SELECT find_ip4_in_cidr4('203.0.113.123','203.0.113.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('203.0.113.123','203.0.113.0/24') |
+-----------------------------------------------------+
| 24 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','192.168.0.0/16');
+-----------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','192.168.0.0/16') |
+-----------------------------------------------------+
| 16 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
不在街区?即 returns 0 (false).
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','203.0.113.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','203.0.113.0/24') |
+-----------------------------------------------------+
| 0 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','192.168.0.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','192.168.0.0/24') |
+-----------------------------------------------------+
| 0 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
全零地址有一个特例,我们return -1(仍然"true",但保留排序顺序):
mysql> SELECT find_ip4_in_cidr4('192.168.100.1','0.0.0.0/0');
+------------------------------------------------+
| find_ip4_in_cidr4('192.168.100.1','0.0.0.0/0') |
+------------------------------------------------+
| -1 |
+------------------------------------------------+
1 row in set (0.00 sec)
胡说八道return null:
mysql> SELECT find_ip4_in_cidr4('234.467.891.0','192.168.0.0/24');
+-----------------------------------------------------+
| find_ip4_in_cidr4('234.467.891.0','192.168.0.0/24') |
+-----------------------------------------------------+
| NULL |
+-----------------------------------------------------+
1 row in set (0.00 sec)
现在,代码:
DELIMITER $$
DROP FUNCTION IF EXISTS `find_ip4_in_cidr4` $$
CREATE DEFINER=`mezzell`@`%` FUNCTION `find_ip4_in_cidr4`(
_address VARCHAR(15),
_block VARCHAR(18)
) RETURNS TINYINT
DETERMINISTIC /* for a given input, this function always returns the same output */
CONTAINS SQL /* the function does not read from or write to tables */
BEGIN
-- given an IPv4 address and a cidr spec,
-- return -1 for a valid address inside 0.0.0.0/0
-- return prefix length if the address is within the block,
-- return 0 if the address is outside the block,
-- otherwise return null
DECLARE _ip_aton INT UNSIGNED DEFAULT INET_ATON(_address);
DECLARE _cidr_aton INT UNSIGNED DEFAULT INET_ATON(SUBSTRING_INDEX(_block,'/',1));
DECLARE _prefix TINYINT UNSIGNED DEFAULT SUBSTRING_INDEX(_block,'/',-1);
DECLARE _bitmask INT UNSIGNED DEFAULT (0xFFFFFFFF << (32 - _prefix)) & 0xFFFFFFFF;
RETURN CASE /* the first match, not "best" match is used in a CASE expression */
WHEN _ip_aton IS NULL OR _cidr_aton IS NULL OR /* sanity checks */
_prefix IS NULL OR _bitmask IS NULL OR
_prefix NOT BETWEEN 0 AND 32 OR
(_prefix = 0 AND _cidr_aton != 0) THEN NULL
WHEN _cidr_aton = 0 AND _bitmask = 0 THEN -1
WHEN _ip_aton & _bitmask = _cidr_aton & _bitmask THEN _prefix /* here's the only actual test needed */
ELSE 0 END;
END $$
DELIMITER ;
一个不特定于存储函数但适用于大多数 RDBMS 平台上的大多数函数的问题是,当列用作 WHERE
中函数的参数时,服务器无法"look backwards"通过函数使用索引优化查询。