我有一个应用程序,允许管理员指定有效的 IP 地址,可以从中发出 Web 服务请求。我只需获取配置的 IP 地址并将它们与传入的请求进行比较。比较两个 IPv4 地址是微不足道的,我认为比较两个 IPv6 地址也是如此。
然而,当我注意到 IPv6 地址有点复杂时,我的网络无知开始显现。我注意到的一个问题是,如果我查看机器上的 IP 地址(正在查看 VMWare 控制台显示的 IP 地址)与来自 Web 请求的 IP 地址(.NET 中的 HttpContext.Current.Request.UserHostAddress
),我注意到其中一个它们以 %10 结束,另一个以 %11 结束:
唯一的区别是 %10 和 %11 - 什么给出?
我还看到 IPv6 地址以“/”结尾,后跟 2 位数字。在进行比较时,我是否应该忽略这最后 3 位数字(如果它们存在)?如果是这样,我需要寻找哪些有效的替代结局?
- - - - - - 编辑 - - - - - - -
这是我根据提供的答案的解决方案...
我只是存储一个“清理过的”IP 地址并将其与“清理过的”IP 地址进行比较。在此处使用 .NET 是我清理 IP 地址的方式。从性能的角度来看不是最好的,但它确实有效。我宁愿只比较
GetAddressBytes()
但我使用的是 Dictionary
我决定放弃创建自己的 ByteComparer
的额外步骤.IPAddress incomingIp = null;
bool ipAddressParsePassed = IPAddress.TryParse(userHostAddress, out incomingIp);
if (ipAddressParsePassed)
{
IPAddress scrubbedIp = new IPAddress(incomingIp.GetAddressBytes());
string scrubbedIpStr = scrubbedIp.ToString()
}
最佳答案
Because all link-local addresses in a host have a common prefix, normal routing procedures cannot be used to choose the outgoing interface when sending packets to a link-local destination. A special identifier, known as a zone index, is needed to provide the additional routing information; in the case of link-local addresses, zone indices correspond to interface identifiers.
When an address is written textually, the zone index is appended to the address, separated by a percent sign "%". The actual syntax of zone indices depends on the operating system [...]
因此,这些后缀是区域指示符,将地址与物理接口(interface)相关联。例如,这也解释了为什么有线和无线接口(interface)之间的区别就足够了。
为了帮助回答这个问题,我认为后缀不应包含在任何比较中。根据定义,IPv6 地址是 128 位,后缀是严格的本地信息,在您自己的机器及其当前操作系统之外没有意义。
比较 128 位就足够了。
关于ipv6 - 如何确定两个 IPv6 地址之间的相等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1787530/