您如何确定两个IPv6地址之间的相等性? [英] How do you determine equality between two IPv6 addresses?

查看:123
本文介绍了您如何确定两个IPv6地址之间的相等性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个允许管理员指定有效IP地址的应用程序,可以从中提出Web服务请求.我只是简单地获取已配置的IP地址,并将其与传入请求进行比较.比较两个IPv4地址是微不足道的,我认为比较两个IPv6地址也是一样.

I have an application that allows administrators to specify valid IP addresses from which web service requests can be made. I simply take the configured IP addresses and compare them against the incoming request. Comparing two IPv4 addresses is trivial and I thought comparing two IPv6 addresses would be as well.

但是,当我注意到IPv6地址稍微复杂一点时,我的网络无知开始显现出来.我注意到的一个问题是,如果我查看机器上的IP地址(正在查看VMWare控制台显示的IP地址是)与Web请求中的IP地址(.NET中的HttpContext.Current.Request.UserHostAddress)相比,我注意到其中一个他们以%10结尾,另一个以%11结尾:

However, my networking ignorance started to show when I noticed that IPv6 addresses are a little more complex. One issue I noticed is that if I look at the IP address on the machine (was viewing what VMWare console showed the IP address to be) versus the IP address from the web request (HttpContext.Current.Request.UserHostAddress within .NET) I noticed that one of them ended in %10 and another in %11:

  • ipconfig显示:fe80:8179:5576:c6d0:8b16%11
  • UserHostAddress显示:fe80 :: 8179:5576:c6d0:8b16%10

唯一的区别是%10和%11-产生了什么?

The only difference is the %10 and %11 - what gives?

我还看到IPv6地址以"/"结尾,后跟2位数字.在进行比较时,我是否应该忽略这最后的3位数字(如果存在)?如果是这样,我需要寻找哪些有效的替代结尾?

I have also seen IPv6 addresses end in "/" followed by 2 digits. Should I just ignore these final 3 digits (if they exist) when doing a comparison? If so, what are the valid alternate endings that I need to look for?

-----------编辑-------------

----------- EDIT -------------

这是我基于提供的答案的解决方案...

Here is my solution based on the answer provided...

我只存储一个清理的" IP地址,然后将其与清理的" IP地址进行比较.我在这里使用.NET来清理IP地址.从性能的角度来看,这并不是最好的方法,但是它确实可以工作.我宁愿只是对GetAddressBytes()进行比较,但我使用的是Dictionary,因此我决定放弃创建自己的ByteComparer的额外步骤.

I simply store a "scrubbed" IP address and compare that with a "scrubbed" IP address. Using .NET here is how I scrub an IP address. Not the best from a performance standpoint, but it does work. I would rather just do a comparison of the GetAddressBytes() but I'm using a Dictionary and I decided against the extra step of creating my own 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 [...]

因此,这些后缀是区域指示符,将地址与物理接口相关联.例如,这也解释了为什么有线和无线接口的足够性不同.

So, those suffixes are zone indicators, that associate the address with a physical interface. This also explains why the suffices differ between wired and wireless interfaces, for instance.

为了帮助回答这个问题,我认为在任何比较中都不应包含后缀. IPv6地址根据定义为128位,后缀严格来说是本地信息,在您自己的计算机及其当前操作系统之外是没有意义的.

To help answer the question, I don't think the suffixes should be included in any comparison. IPv6 addresses are 128 bits by definition, and the suffixes are strictly local information that does not make sense outside your own machine and it's current operating system.

比较128位就足够了.

Comparing the 128 bits should be enough.

这篇关于您如何确定两个IPv6地址之间的相等性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆