INET6_ATON 和 NTOA 函数的 Oracle PL/SQL 版本?
有没有什么好的代码可以将 IPv6 地址字符串转换为整数?使用一种格式转换 IPv4 似乎相当容易。但是,IPv6 有多种不同的地址显示格式:
- XXXX:XXXX:XXXX:XXXX::
- XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX XXXX
- :XXX:XXXX:0:0:XXXX:XXX: XXXX
- XXXX:XXX:XXXX::XXXX:XXX:XXXX
- ::ffff:XXXX:XXX (v6 格式的 IPv4)
- ::ffff:###.#.#.### (也有效v6 格式的 IPv4)
我希望能够获取这些字符串之一并将其转换为 INTEGER 以进行 IP 到网络匹配,并允许使用这些格式中的任何一种作为输入。
Any have any good code for converting a IPv6 address string into an integer? Converting IPv4 seems to be fairly easy, with the one format. However, IPv6 has several different formats to show an address:
- XXXX:XXXX:XXXX:XXXX::
- XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX
- XXXX:XXX:XXXX:0:0:XXXX:XXX:XXXX
- XXXX:XXX:XXXX::XXXX:XXX:XXXX
- ::ffff:XXXX:XXX (IPv4 in v6 format)
- ::ffff:###.#.#.### (also valid IPv4 in v6 format)
I'd like to be able to take one of these strings and translate it into an INTEGER for IP-to-network matching, and allow for any of these formats as the input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终我自己滚动了。还意识到 Oracle 的 126 位 INTEGER 对于 IPv6 的 128 位地址来说不够位。坦率地说,考虑到我从未听说过 16 字节整数,我不知道原始 C 库的 INET6_ATON (或 INET_PTON)是如何做到的。
我最终得到了一个 32 字节的十六进制字符串,这意味着我必须在 nettohex 上进行一些奇特的“半字符串”数学计算,并使用 SUBSTR 以使 FBI 正常工作。 (Blasted PL/SQL 不允许“RETURN CHAR(32)”...)
不过,总体而言,它运行良好,适用于所有格式,并且允许基于索引的字符比较来查明 IP 地址是否为在一个IP范围内。
下面是完整的代码:
更新: Oracle 11g 确实允许将 SUBSTR 条目放入虚拟列。因此,您可以拥有这样的列:
和索引:
使用 WHERE 子句,例如:
Ended up rolling my own. Also realized that Oracle's 126-bit INTEGER is not enough bits for IPv6's 128-bit addresses. Frankly, I don't know how the original C library's INET6_ATON (or INET_PTON) does it, considering that I've never heard of a 16-byte integer.
I ended up with a 32-byte hex string, which means I have to do some fancy "half-string" math on nettohex and use SUBSTR for the FBIs to work correctly. (Blasted PL/SQL doesn't allow for "RETURN CHAR(32)"...)
Overall, though, it works well, works in all formats, and allows for index-based character comparisons to find out if an IP address is within an IP range.
Here's the full code:
UPDATE: Oracle 11g does allow for the SUBSTR entry to be put a virtual column. So, you could have columns like this:
And indexes like:
Using WHERE clauses like: