Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6707289

InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 6
    • core-libs
    • b84
    • x86
    • windows_vista

      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.

            chegar Chris Hegarty
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: