United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6707289 : InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc

Details
Type:
Bug
Submit Date:
2008-05-27
Status:
Resolved
Updated Date:
2014-06-11
Project Name:
JDK
Resolved Date:
2010-02-16
Component:
core-libs
OS:
windows_vista
Sub-Component:
java.net
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Relates:
Relates:

Sub Tasks

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

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.
                                     
2009-12-18
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
                                     
2010-01-25



Hardware and Software, Engineered to Work Together