在同一子网中的本地计算机的IP地址(IPv6的支持)网中、地址、计算机、在同一

2023-09-03 00:35:12 作者:几分曾经

有没有人有一些code,将确定一个IP地址(IPv4或IPv6)是在同一子网中运行应用程序的机器吗?我已经看到了code无数的例子,这是否与IPv4的,但我无法找到任何支持IPv6。

Does anyone have some code that will determine if an IP address (IPv4 or IPv6) is on the same subnet as the machine running the application? I've seen numerous examples of code that does this with IPv4 but I can't find any that support IPv6.

编辑:

我不确定如果我理解的IPv4和IPv6所以这里有一点所有的分歧,我的问题。我有一个应用程序,它提供对Internet客户端和Intranet的客户端,也就是有属于同一物理网络服务器上的客户端。所以有时也有客户端之间的路由器,有时不会有。在IPv4中,我可以通过检查服务器的IP地址的客户端的IP地址和子网掩码确定这所以如果我的服务器的IP地址和子网掩码分别为:

I'm unsure if I'm understanding all the differences between v4 and v6 so here's a bit more to my question. I have an application that serves both internet clients and intranet clients, that is to say there are clients that are on the same physical network as the server. So sometimes there are routers between the client and sometimes there aren't. With IPv4 I can determine this by checking the client IP address against the server IP address and the subnet so if my server's IP and subnet mask are respectively:

192.168.123.15 255.255.255.0

192.168.123.15 255.255.255.0

和服务器接收来自192.168.123.100我知道,有没有路由器的客户端和服务器之间站着一​​个客户端请求。然而,如果服务器从192.168.1.100收到客户端请求或也许67.7.23.4我知道有路由器这些客户端和服务器之间站立。在.NET中我可以收集客户端和服务器的IP地址(包括IPv4和IPv6),但我找不到IPv6的子网掩码。

And the server receives a client request from 192.168.123.100 I know that there is no router standing between the client and server. However, if the server receives a client request from 192.168.1.100 or perhaps 67.7.23.4 I know that there is a router standing between those clients and the server. In .Net I can gather the client and server IP addresses (both v4 and v6) but I cannot find the IPv6 subnet mask.

有没有办法来收集这些信息在.net中还是有说我误会IPv4和IPv6之间的一些区别?

Is there a way to gather this info in .Net or is there some difference between IPv4 and IPv6 that I'm misunderstanding?

修改X2:

我张贴这在MS连接网站,看看它的东西,他们正在努力或者如果有一个原因,他们并没有添加一个IPv6Mask属性的UnicastIPAddressInformation类。

I posted this on the MS connect site to see if it's something they're working on or if there's a reason they haven't added an IPv6Mask property to the UnicastIPAddressInformation class.

https://connect.microsoft.com/VisualStudio/feedback/details/643031/unicastipaddressinformation-class-has-no-ipv6mask-property

我还贴大约在同一时间在MSDN论坛上同样的问题。 1800+的意见,而不是一个单一的答复。猜猜我不是唯一一个谁是好奇这一点。

I also posted the same question on the MSDN forums around the same time. 1800+ views and not a single reply. Guess I'm not the only one who's curious about this.

http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/dd30e161-9be5-4d70-97c0-22e2756ce953

推荐答案

它看起来并不像该框架有办法做到这一点。最准确的方法是做一个路由查找,但我看不出一个好办法,在C#。 (在Linux下,我会做 / sbin目录/ IP -6途径获得< IPv6的地址> ,看看哪条路线被退回。)你必须找到一个本地电话要做到这一点在Windows中;我没有看到一个命令行应用程序。

It doesn't look like the framework has a way to do this. The most accurate way would be to do a route lookup, but I don't see a good way to do that in C#. (Under Linux, I'd do /sbin/ip -6 route get <ipv6-addr> and see which route gets returned.) You'd have to find a native call to do this in Windows; I don't see a command line application.

最好的办法可能是解析的 netsh接口的IPv6显示路线冗长的输出。你可以查找任何非/ 128 prefixes并做了最长的preFIX匹配这些。 (当然,如果你打一个/ 128,这是分配给框地址)

The best way might be to parse the output of netsh interface ipv6 show route verbose. You could look for any non-/128 prefixes and do a longest prefix match on those. (well, if you hit a /128, that's an address assigned to the box)

您也可以查看邻居表。 ( netsh接口的IPv6秀邻居),但可能不包含你正在寻找,如果你还没有谈到该主机最近的条目。

You could also check the neighbor table. (netsh interface ipv6 show neighbors), but that might not contain the entry you're looking for if you haven't talked to that host recently.

其他潜在的问题,你需要考虑:

Other potential issues you'll need to consider:

链路本地地址(FE80 :: / 10)(和多播,环回和未指定的 - 一切都在表) ,在IPv6中,所分配的地址并不意味着是上链路preFIX的事实。在preFIX表是分开的。目前尚不清楚如何在Windows下测试这一点,但 netsh接口的IPv6展会现场prefixes 可能的帮助。它看起来像Windows实际上可能把这个更像是IPv4的比标准的预期。 Link-local addresses (fe80::/10) (and multicast, loopback, and unspecified - everything in the table) The fact that in IPv6, an assigned address does not imply an on-link prefix. The prefix table is separate. It's not clear how to test for this under Windows, but netsh interface ipv6 show siteprefixes might help. It looks like Windows might actually treat this more like IPv4 than the standard expects.

修改:这听起来像检查邻居表将是阻力最小为你做这个的路径;如果你接受来自内部网络的连接,然后转身检查邻居表,你可以合理放心,如果邻居是本地的,也将存在于表。如果查看邻居表,要小心只查找局域网接口的邻居表。 (ISATAP接口,默认情况下,在许多Windows系统的安装,暴露在整个IPv4因特网作为链路本地子网。)

Edit: It sounds like checking the neighbor table will be the path of least resistance for you to do this; if you're accepting a connection from the intranet and then turning around and checking the neighbor table, you can be reasonably assured that if the neighbor is local, it will exist in the table. If you check the neighbor table, BE CAREFUL to only look at the neighbor table for LAN interfaces. (The ISATAP interface, installed by default on many Windows system, exposes the entire IPv4 internet as a link-local "subnet".)

再次IPv6地址具有一个掩码的概念,因为在链路preFIX表是分开的地址分配。 然而,如果你有一台服务器坐的地方,你可以大概99%肯定它是一个 / 64 。 (但你必须要小心,如果它本身是一个隧道端点,有时我看到分配给6in4隧道技术隧道较长prefixes)因此,一个快速和肮脏的算法是:

Again, IPv6 addresses have no concept of a "netmask", since the on-link prefix table is separate from address assignment. However, if you have a server sitting somewhere you can be probably 99% certain that it is on a /64. (though you'd have to be careful; if it itself was a tunnel endpoint, sometimes I've seen longer prefixes assigned for 6in4 tunnels) So a quick and dirty algorithm would be:

忽略其中第64位的所有地址0(本地环路) 忽略所有地址匹配FF00 :: / 8(多播) 如果匹配的地址为fe80 :: / 10,它的接口本地。要小心,因为如果你有一个启用ISATAP接口,本地链接是指自动隧道到整个IPv4的互联网! (不好)。因此,它可能是最好的不信任链路本地地址,除非你确定他们是从一个LAN接口到来。 (它们不能被路由) (现在复杂的部分)确定的地址是邻居。 (邻居查找)的黑客和斜线的解决方案是检查在系统上配置的所有IPv6地址(通过无状态地址自动配置,DHCPv6报,或静态自动配置的,如果你能确定它),检查前64位。 在服务器(假设没有时髦的隧道配置与非/ 64 prefixes),这将是在极少数情况下的一个错误,因为 - 了 - 你不能肯定,如果该地址是真正的链接,除非您检查链路上preFIX表。 (这Windows没有的概念,它似乎是存储在路由表中。),其可以被配置为发送路由器通告以太网接口将总是发布一条/ 64 preFIX大多数网络设备。如果你可以检查数据包排在一个LAN接口上,它会更不太可能,这将是一个错误。 Ignore all addresses where the first 64 bits are 0 (local loopback) Ignore all addresses matching ff00::/8 (multicast) If the address matches fe80::/10, it is interface local. Be careful with this, because if you have an ISATAP interface enabled, "link-local" means "automatic tunnel to the entire IPv4 internet"! (not good.) So it might be best to not trust link-local addresses unless you are certain they are coming from a LAN interface. (they can't be routed) (Now the complicated part) Determine if the address is a neighbor. (neighbor lookup) The hack-and-slash solution is to check all the IPv6 addresses configured on the system (autoconfigured via stateless address autoconfiguration, DHCPv6, or static, if you can determine it) and check the first 64 bits. On a server (assuming no funky tunnels are configured with non-/64 prefixes) this will be a bug in rare cases, since - again - you can't be sure if the address is really on-link unless you check the on-link prefix table. (which Windows doesn't have a concept of; it seems to be stored in the route table.) Most network devices that can be configured to send out router advertisements on an Ethernet interface will always advertise a /64 prefix. If you can check that the packet came in on a LAN interface, it will be even less likely that this will be a bug.

编辑2

我已经写了一些code解析IPv6路由表,贴吧的此处。它并没有解决在提出这个问题,但在困难的问题,但它是朝着正确方向迈出的一步。

I have written some code to parse the IPv6 route table and posted it here. It doesn't solve the hard problems posed in this question yet, but it's a step in the right direction.