如何获取同时具有 v4 和 v6 地址的接口上的 IPv4 子网掩码?
我有一个返回 ipv4 地址(4 个八位字节)的 InterfaceAddress。然而,网络前缀长度似乎是与接口关联的 ipv6 地址(返回为 128)。如何找到正确的网络前缀长度?
Enumeration<NetworkInterface> NetworkInterface.getNetworkInterfaces()
for (; interfaces.hasMoreElements();) {
final List<InterfaceAddress>interfaceAddresses =
interfaces.nextElement().getInterfaceAddresses();
for (final InterfaceAddress address : interfaceAddresses) {
assert(address.getAddress().getAddress().length == 4); // [sic]
assert(address.getNetworkPrefixLength() < 32); // <- Fails. Actually equals 128
}
}
I have an InterfaceAddress that returns an ipv4 address (4 octets). However the network prefix length seems to be for the ipv6 address associated with the interface (it's returning as 128). How do I find the correct network prefix length?
Enumeration<NetworkInterface> NetworkInterface.getNetworkInterfaces()
for (; interfaces.hasMoreElements();) {
final List<InterfaceAddress>interfaceAddresses =
interfaces.nextElement().getInterfaceAddresses();
for (final InterfaceAddress address : interfaceAddresses) {
assert(address.getAddress().getAddress().length == 4); // [sic]
assert(address.getNetworkPrefixLength() < 32); // <- Fails. Actually equals 128
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我认为这是 JVM 本地代码中的一个错误。当同一个接口同时拥有 v4 和 v6 地址时,就会变得混乱。
设置此属性后,问题就消失了,
Personally, I think this is a bug in the native code of the JVM. When you have both v4 and v6 addresses for the same interface, it gets confused.
The problem went away for us after setting this property,