JDK-6707289 : InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2008-05-27
  • Updated: 2014-06-11
  • Resolved: 2010-02-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b84Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) 64-Bit Server VM (build 10.0-b19, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6001]

A DESCRIPTION OF THE PROBLEM :
I need to determine the network address for each interface address on the computer, so that from a list of addresses, I can eliminate addresses which correspond to the same network.

In the process of trying to do this, I discovered that getNetworkPrefixLength() does not do what the javadoc says, at least for IPv4 addresses.

(Of course, what would be very useful, is some other means of determining the network address.)


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the provided code.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The prefix length on IPv4 addresses cannot be outside the range 0..24.
ACTUAL -
The prefix length on IPv4 addresses is completely bogus, and seems to always report 128 except for the loopback address which correctly reports 8.

Examples from my own machine:

eth3: /192.168.222.60/128
eth6: /192.168.80.1/128
eth7: /192.168.83.1/128


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
    public static void main(String[] args) throws Exception {
        Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = ifaces.nextElement();
            for (InterfaceAddress ifaceAddress : iface.getInterfaceAddresses()) {
                System.out.println("iface " + iface.getName() +
                                   " has address " + ifaceAddress.getAddress() +
                                   "/" + ifaceAddress.getNetworkPrefixLength());
            }
        }
    }

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I haven't found one.  If anyone has, please contribute it.

Comments
EVALUATION The simplest way to resolve this issue to use enumAddresses_win to determine the IPv4 addresses, and skip IPv4 addresses in getAddrsFromAdapter. enumAddresses_win is not IPv6 aware so we call getAddrsFromAdapter after to determine IPv6 addresses. changeset for this fix: http://hg.openjdk.java.net/jdk7/jdk7/jdk/rev/e67bf9abc6a5
11-06-2014

EVALUATION getAddrsFromAdapter (src/windows/native/java/net/NetworkInterface_winXP.c) makes an incorrect assumption that the list of IP_ADAPTER_PREFIX structures pointed to by the FirstPrefix member has a linear relationship with the IP_ADAPTER_UNICAST_ADDRESS structures pointed to by the FirstUnicastAddress member. This is incorrect. It appears there is no guarantee about the returned IP_ADAPTER_PREFIX order, making it very difficult to determine what (if any) unicast address they relate to.
18-12-2009