迭代两个 IPV6 地址之间的地址范围
我需要某种方法来迭代两个 IPv6 地址之间的地址范围。即,如果第一个 IP 为 2a03:6300:1:103:219:5bff:fe31:13e1
,第二个 IP 为 2a03:6300:1:103:219:5bff:fe31: 13f4
,我想访问该范围内的19个地址。
对于 IPv4,我只需执行 inet_aton
来表示字符串,并在结果结构中获取 s_addr
的 htonl
,但是对于 IPv6 我该如何做到这一点呢?
为了简化:
struct in6_addr sn,en;
long i;
s="2a03:6300:1:103:219:5bff:fe31:13e1";
e="2a03:6300:1:103:219:5bff:fe31:13f4";
inet_pton(AF_INET6,s,&sn);
inet_pton(AF_INET6,e,&en);
[..]
for (i = _first_ipv6_representation; i<=_second_ipv6_representation; i++){
/* stuck here */
}
I need some way to iterate over the range of addresses between two IPv6 addresses. i.e. if the first IP is 2a03:6300:1:103:219:5bff:fe31:13e1
and the second one is 2a03:6300:1:103:219:5bff:fe31:13f4
, I would like to visit the 19 addresses in that range.
With IPv4 I just do inet_aton
for string representation and get htonl
of s_addr
in the resulting struct, but how can I do that for IPv6?
For simplify:
struct in6_addr sn,en;
long i;
s="2a03:6300:1:103:219:5bff:fe31:13e1";
e="2a03:6300:1:103:219:5bff:fe31:13f4";
inet_pton(AF_INET6,s,&sn);
inet_pton(AF_INET6,e,&en);
[..]
for (i = _first_ipv6_representation; i<=_second_ipv6_representation; i++){
/* stuck here */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您的评论删除旧答案,更新以迭代一系列地址:
Old answer stricken per your comments, updated to iterate a range of addresses:
这确实很棘手(我喜欢这个问题)。基本上,您需要递增并比较像这样存储的整数:
uint8_t s6_addr[16]
。inc_s6
和cmp_s6
来递增/比较这些数组这是对
inc_s6 的尝试
:比较功能要容易得多。
It's tricky indeed (I like this question). Basically you need to increment and compare integers that are stored like this:
uint8_t s6_addr[16]
.inc_s6
andcmp_s6
that increment / compare such arraysHere is an attempt at
inc_s6
:The compare function is a lot easier.
澄清一下:
我将其用于一些具有大量绑定 IPv6 的代理服务器,并为每个请求委托新的 IP。
我的增量函数带有一些附加说明:
我没有显示比较函数,因为 @sixlettervariables 解决方案工作得足够好。
For clarification:
I'm using that for some proxy-server that have a lot of binded IPv6 and to delegate new IP for each request.
My increment function with some additional explanation:
I don't show compare function because @sixlettervariables solution works good enough.
Python3:
Python3: