为什么 UnicastIPAddressInformation.IPv4Mask 在 IPv4 地址上返回 null?
以下是从本地系统构建 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!?");
}
}
}
}
}
The following is a section of code which builds a list of IP addresses and their subnet masks from the local system, however the Warn function seems to get triggered regularly which should in theory be impossible - as it should not be possible to have an IPv4 address without the associated subnet mask[?].
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!?");
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为您遇到了环回适配器。 即 127.0.0.1 这些 IPv4Mask 设置为 null。 参考 ni.Name 看看我是否正确。
要检查环回适配器,请执行...
if (ni.NetworkInterfaceType != NetworkInterfaceType.Loopback) ...
It is because you have come across a loop back adapter. ie 127.0.0.1 These have the IPv4Mask set to null. Refer to ni.Name to see if I am right.
To check for a loopback adapter do...
if (ni.NetworkInterfaceType != NetworkInterfaceType.Loopback) ...
当网络关闭时,IPv4Mask 返回 null(请参阅 ni.OperationalStatus),在迭代 GetAllNetworkInterfaces() 返回的所有内容时,您经常会遇到这种情况。
这似乎已在 .Net 4.0 中修复。
有时可以推断掩码,例如,所有以 169.254 开头的 IP 地址(这是 DHCP 失败时 Windows 将选择的地址)的掩码为 255.255.0.0
IPv4Mask returns null when the network is down (see ni.OperationalStatus), which you'll often encounter when iterating through everything GetAllNetworkInterfaces() returns.
This appears to have been fixed in .Net 4.0
Sometimes the masks can be inferred, e.g. all IP addresses that start with 169.254 (which is what Windows will pick when DHCP fails) have a mask of 255.255.0.0
这很奇怪; 显然,其语义并不完全符合人们的合理预期。
您能否检查相关接口的子网掩码是否有类? 即,对于那些
IPv4Mask
为空的接口地址,它们的网络掩码分别为/8、/16 和/24 的A、B 和C 类地址吗?也许这个函数并不是真正的 CIDR 世界的成员,他们实际上只做“子网”掩码; 假设没有地址的地址使用有类网络掩码。
That is rather odd; obviously the semantics of this are not quite what one could reasonably expect.
Can you check to see if the subnet masks of the interfaces in question are classful? I.e., for those interface addresses where
IPv4Mask
is null, are they class A, B and C addresses with a network mask of /8, /16 and /24 respectively?Perhaps this function is not really a member of the CIDR world, and they really do only "subnet" masks; an address without one is assumed to use the classful network mask.
我刚刚遇到的原因之一是:
我使用 .NET Framework 4 客户端配置文件,IPv4Mask 工作得很好。 当切换到.NET Framework 2或3.5时,它返回NULL。 我多次重现了这个问题。
One reason I've just come across:
I use .NET framework 4 Client profile, IPv4Mask worked fine with me. When switching to .NET framework 2 or 3.5, It returned NULL. I reproduced the issue multiple times .