如何使用 CIDR 在 .Net 4/4.5 中使用 C# 找到子网所属的地址 Space
How do I find the Address Space to which the Subnet belongs using C# in .Net 4/4.5 using CIDR
我有一个网络配置列表,其中包含地址列表 Space 和子网列表。我需要找出子网所属的地址 Space。子网有自己的 CIDR 格式的 IP 地址范围。
这是关于我如何看待它的工作方式的伪代码。
List<string> AddressSpaces = new List<string>()
{
"10.1.1.9/27",
"10.0.0.0/8",
"10.100.1.0/20",
"10.200.2.0/20"
};
List<string> Subnets = new List<string>()
{
"10.1.1.0/27",
"10.0.0.0/11",
"10.100.0.0/23",
"10.100.2.0/23",
"10.200.0.0/23",
"10.200.2.0/23",
};
private void MapSubnet2AddressSpace()
{
foreach(var Subnet in Subnets)
{
findAddressSpace(); // how to fetch the respective address space from AddressSpaces
}
}
如果您屏蔽地址 space 和屏蔽地址 space 的子网,如果它们相等,则匹配。 IPv4 地址和掩码都是 32 位无符号整数。如果 subnet AND subnet mask
等于 address space AND subnet mask
,则地址 space 在子网中。
您需要注意地址space掩码长度小于或等于子网掩码长度。例如,最后三个地址 space 不在任何子网中。事实上,一些子网在地址 space 中。例如,地址 space 10.0.0.0/8
不适合任何子网,但所有子网都适合该地址 space.
我有一个网络配置列表,其中包含地址列表 Space 和子网列表。我需要找出子网所属的地址 Space。子网有自己的 CIDR 格式的 IP 地址范围。 这是关于我如何看待它的工作方式的伪代码。
List<string> AddressSpaces = new List<string>()
{
"10.1.1.9/27",
"10.0.0.0/8",
"10.100.1.0/20",
"10.200.2.0/20"
};
List<string> Subnets = new List<string>()
{
"10.1.1.0/27",
"10.0.0.0/11",
"10.100.0.0/23",
"10.100.2.0/23",
"10.200.0.0/23",
"10.200.2.0/23",
};
private void MapSubnet2AddressSpace()
{
foreach(var Subnet in Subnets)
{
findAddressSpace(); // how to fetch the respective address space from AddressSpaces
}
}
如果您屏蔽地址 space 和屏蔽地址 space 的子网,如果它们相等,则匹配。 IPv4 地址和掩码都是 32 位无符号整数。如果 subnet AND subnet mask
等于 address space AND subnet mask
,则地址 space 在子网中。
您需要注意地址space掩码长度小于或等于子网掩码长度。例如,最后三个地址 space 不在任何子网中。事实上,一些子网在地址 space 中。例如,地址 space 10.0.0.0/8
不适合任何子网,但所有子网都适合该地址 space.