C# 如何知道子网是否是更大网络的一部分

发布于 2024-09-10 03:16:49 字数 322 浏览 2 评论 0原文

我正在使用互联网的 BGP 表(一个巨大的文件)。然而,路线汇总可能是一个问题。我的主要问题是,有时会公布大块 IPv4 空间(即 172.16.0.0/16),但也会公布更具体和更小的路由(即 172.16.64.0/18)。因此BGP表中有两个冗余条目。

我想找出一种方法来结束非冗余 IP 地址列表,只是大块。我正在考虑比较所有这些并将它们存储在一个列表中。 C# 中有没有一种方法可以知道 IP 地址是否是更广泛的 IP 地址的一部分?如:

172.16.64.0/18 是 172.16.0.0/16 的一部分 //true

非常感谢您的帮助!

阿莱曼吉

I am working with a BGP table of the Internet (a huge file). However, route summarization can be a problem. My main issue is that, sometimes, big chunks of IPv4 space are announced (i.e., 172.16.0.0/16), but also more specific and smaller routes are announced too (i.e., 172.16.64.0/18). So there are two redundant entries in the BGP table.

I would like to figure out a way to end up with a list of non-redundant IP addresses, just the big chunks. I am thinking of maybe comparing all of them and storing them in a list. Is there a method in C# to know if an IP address is part of a broader IP address? As in:

172.16.64.0/18 is part of 172.16.0.0/16 //true

Thanks a lot for all your help!

alemangui

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-09-17 03:16:49

使用简单的数学。

IP地址是4个字节,是一个32位整数。子网掩码完全相同。

鉴于此,您可以使用算术 AND 来确定它是在定义的网络内部还是外部。

EG:

IP: 192.168.0.1      = C0 . A8 . 00 . 01
Subnet: 192.168.0.0  = C0 . A8 . 00 . 00

Is in subnet?
Thus 0xC0A80001 & 0xC0A80000 == 0xC0A80000 => true

要回答一个网络是否存在于另一个网络中的问题,您可以使用相同的方法,但将两个数字右移为“最大”子网的大小。

例如:

Net A: 172.16.64.0/18 -> AC 10 40 00
Net B: 172.16.0.0/16  -> AC 10 00 00

Thus right shift both with 16 and apply previous op.

AC 10 & AC 10 == AC 10 -> true

Use simple math.

IP address is 4 bytes, iow a 32-bit integer. The subnet mask is exactly the same.

Given this, you can use an arithmetic AND to determine whether it is inside or outside the defined network.

EG:

IP: 192.168.0.1      = C0 . A8 . 00 . 01
Subnet: 192.168.0.0  = C0 . A8 . 00 . 00

Is in subnet?
Thus 0xC0A80001 & 0xC0A80000 == 0xC0A80000 => true

To answer the question of whether one net work exists in another, you can use the same approach, but right shift both numbers with the size of the 'largest' subnet.

EG:

Net A: 172.16.64.0/18 -> AC 10 40 00
Net B: 172.16.0.0/16  -> AC 10 00 00

Thus right shift both with 16 and apply previous op.

AC 10 & AC 10 == AC 10 -> true
春庭雪 2024-09-17 03:16:49

考虑位模式:

172.16.64.0

10101100.00010000.01000000.00000000

172.16.0.0

10101100.00010000.00000000.00000000

请注意,更具体的地址中的设置位是更通用的地址中的设置位,再加上一些位。因此,如果我们对两个地址执行按位与,结果将等于更一般的结果。

这总是正确的测试吗?好吧,如果我们有两个具有包含关系的地址,则对这些位进行“与”操作将清楚地给出与建议的父级至少有一位不同的结果,因此这就是我们想要的测试。

如果您知道两个地址中哪一个是建议的父地址,哪一个是建议的子地址,那么我们可以简单地对这些位进行“与”并与建议的父地址进行比较。如果它们可以是任一顺序,则将它们与并分别与两个输入进行比较。

要获取实际位,如果您已有 IPAddress,请使用 GetAddressBytes 获取 byte[],使用 BitConverter< /code> 获取一个单位,然后只需使用 & 进行按位与。

Consider the bit patterns:

172.16.64.0

10101100.00010000.01000000.00000000

172.16.0.0

10101100.00010000.00000000.00000000

Note that the set bits in the more-specific address are the set bits in the more-general address, plus some more. So if we perform a bitwise AND on the two addresses, the result will be equal to the more-general.

Is this always a correct test? Well, if we have two addresses that do not have a containment relationship, ANDing the bits will clearly give a result which has at least one bit different from the proposed parent, so this is the test we want.

If you know which of your two addresses is the proposed parent, and which is the proposed child, then we can simply AND the bits and compare to the proposed parent. If they could be in either order, AND them and compare to both inputs separately.

To get at the actual bits, if you already have an IPAddress, use GetAddressBytes to get a byte[], use BitConverter to get a unit, then just use & for the bitwise AND.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文