How do I get the IPv4 subnetmask on interface with both v4 and v6 address?

Posted by Per Fagrell on Stack Overflow See other posts from Stack Overflow or by Per Fagrell
Published on 2010-06-14T14:34:54Z Indexed on 2010/06/14 17:02 UTC
Read the original article Hit count: 144

Filed under:
|
|

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
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about ipv4