tags:

views:

55

answers:

1

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
    }
}
+1  A: 

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,

java.net.preferIPv4Stack=true
ZZ Coder