C# 中的 IPv6 地址范围
我正在编写一个套接字服务器,需要能够通过 IP 允许/限制,并且我正在尝试使其与 IPv4 和 IPv6 兼容。
我非常了解 IPv4 原理,例如,我可以允许 192.168.0.0/255.255.255.0 满足所有 192.168.0.* 地址的需求,我的代码基于 http://blogs.msdn.com/b/knom/archive/ 2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx。
但是,这如何与 IPv6 配合使用呢?即我如何处理该协议的范围?
I'm writing a socket server that requires the ability to allow/restrict by IP and I'm trying to make it compatible with both IPv4 and IPv6.
I understand the IPv4 principle fairly well, for example I can allow 192.168.0.0/255.255.255.0 catering for all 192.168.0.* addresses, basing my code off of http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx.
However, how does this work with IPv6? i.e how do I handle ranges with that protocol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照惯例,IPv6 不像 IPv4 那样使用子网掩码格式。然而,你没有理由不能以这种方式想象它。
IPv6 中 /64 的等效“子网掩码”为
ffff:ffff:ffff:fffff:0:0:0:0
。只是不要尝试向任何 IPv6 用户引用该内容,因为没有人会知道您的意思。并且不要尝试将其粘贴到任何 IPv6 配置中 - 人们仅使用 /64、/48、/32 等前缀。(任何小于 /64 的前缀,例如 /96,很少会使用)使用,并且正式不推荐用于 LAN 环境。)这是我刚刚编写的一个方便的 ASCII 艺术图表,以帮助您理解 IPv6 前缀:
希望上面的内容与您可视化 IPv4 子网的方式相同(老实说,它几乎完全相同)。事实上,在你的头脑中计算 IPv6 稍微容易一些,因为 IPv6 使用十六进制,所以如果你坚持使用可被 4 整除的前缀,你就可以在 nybble(即数字)边界处划分你的子网 - 这是 IPv4 无法做到的!
我承认我的答案中没有任何 C# 特定信息。尽管如此,它应该让您走上正确的道路,了解要寻找的内容:指定前缀长度。
IPv6 by convention does not use the subnet mask format like IPv4 does. However, there is no reason why you cannot visualise it that way still.
An equivalent “subnet mask” for /64 in IPv6 would be
ffff:ffff:ffff:fffff:0:0:0:0
. Just don’t try quoting that to any IPv6 user, as nobody will know what you mean. And don’t try pasting that into any IPv6 configuration — people only ever use prefixes like /64, /48, /32, etc. (Anything smaller than /64, such as /96, is rarely used, and officially deprecated for LAN environments.)Here’s a handy ASCII art chart I just wrote up to help you understand IPv6 prefixes:
Hopefully the above is the same way you would visualise IPv4 subnets (it’s pretty much exactly the same, to be honest). In fact, calculating IPv6 is slightly easier in your head, because IPv6 uses hexadecimal, so if you stick to prefixes divisible by 4, you get to divide your subnet at the nybble (i.e. digit) boundary — something you can’t do with IPv4!
I acknowledge that my answer does not have any C# specific information in it. Nevertheless, it should put you on the right track with what to look for: specifying the prefix lengths.