以下是从本地系统构建 IP 地址及其子网掩码列表的代码部分,但是警告功能似乎会定期触发,这在理论上应该是不可能的 - 因为它不应该有 IPv4 地址没有相关的子网掩码[?]。
static NetworkUtil()
{
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation address in ni.GetIPProperties().UnicastAddresses)
{
if (address.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (address.IPv4Mask != null)
{
m_subnets.Add(address.Address, address.IPv4Mask);
}
else
{
m_log.Warn("[NetworkUtil] Found IPv4 Address without Subnet Mask!?");
}
}
}
}
}