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

NetworkInterface.getHardwareAddress does not report proper addresses

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • tbd
    • 7u4, 7u11
    • core-libs

      FULL PRODUCT VERSION :
      java version " 1.7.0_25 "
      Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      This is essentially the same problem described in JDK-6763420, but it's occurring under Windows 7.
      Multiple MAC addresses are reported by 'ipconfig /all', only one is reported by java.lang.NetworkInterface.
      The repro code below reports the same number of interfaces from NetworkInterface and ipconfig, but the list from NetworkInterface repeats a single address.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the repro program below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -

      MAC addresses derived from NetworkInterface:
         (your MAC addr)
         (your other MAC addr)

      MAC addresses derived from ipconfig:
         (your MAC addr)
         (your other MAC addr)

      (Actual number of addresses and address values will vary.)
      ACTUAL -

      MAC addresses derived from NetworkInterface:
         (your other MAC addr)
         (your other MAC addr)

      MAC addresses derived from ipconfig:
         (your MAC addr)
         (your other MAC addr)

      (The final address from ipconfig is consistently the only one listed by NetworkInterface.)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Demonstrate a bug in java.net.NetworkInterface class on Windows.
       * This class uses Windows-specific 'ipconfig' command.
       */
      import java.net.NetworkInterface;
      import java.net.SocketException;
      import java.io.*;
      import java.util.*;

      public class NetInterfaceBug {

          public static void main(String[] inArgs) {
              NetInterfaceBug subject = new NetInterfaceBug();
              subject.dummpNetworkInterfaceMacs();
              subject.dumpIpConfigMacs();
          }

          void dummpNetworkInterfaceMacs() {
              ArrayList<String> macAddrs = new ArrayList<String>();
              try {
                  Enumeration niEnum = NetworkInterface.getNetworkInterfaces();
                  while ( niEnum.hasMoreElements() ) {
                      String macAddr = extractMac( ( NetworkInterface ) niEnum.nextElement() );
                      if ( macAddr != null && macAddr.length() > 0 ) {
                          macAddrs.add( macAddr );
                      }
                  }
              } catch ( Exception e ) {
                  System.err.println( " can't get network interfaces: " + e );
              }
              dumpMacs( macAddrs, " NetworkInterface " );
          }

          String extractMac(NetworkInterface inNi) throws SocketException {
              byte[] addrBytes = inNi.getHardwareAddress();
              StringBuilder addrBuff = new StringBuilder();
              if ( addrBytes != null && addrBytes.length == 6 ) {
                  for ( int ndx = 0; ndx < addrBytes.length; ndx++ ) {
                      String sep = ( ndx == 0 ) ? " " : " - " ;
                      addrBuff.append( String.format( " %s%02X " , sep, addrBytes[ndx] ) );
                  }
              }
              return addrBuff.toString();
          }

          void dumpIpConfigMacs() {
              String cmd = " ipconfig /all " ;
              BufferedReader reader = null;
              try {
                  Process proc = Runtime.getRuntime().exec( cmd );
                  StringBuilder outBuff = new StringBuilder();
                  proc.waitFor();
                  reader = new BufferedReader( new InputStreamReader( proc.getInputStream() ) );
                  ArrayList<String> addrsList = new ArrayList<String>();
                  String nextLine = null;
                  do {
                      nextLine = reader.readLine();
                      if ( nextLine != null ) {
                          if ( nextLine.contains( " Physical Address " ) ) {
                              String[] parts = nextLine.split( " : " );
                              addrsList.add( parts[1].trim() );
                          }
                      }
                  } while ( nextLine != null );
                  dumpMacs( addrsList, " ipconfig " );
              } catch ( Exception e ) {
                  System.err.println( " can't execute " + cmd + " : " + e );
              } finally {
                  if ( reader != null ) {
                      try {
                          reader.close();
                      } catch ( Exception e ) {
                          System.err.println( " can't execute " + cmd + " : " + e );
                      }
                  }
              }
          }

          void dumpMacs(List<String> inMacs, String inSource) {
              System.err.println( " " );
              System.err.println( " MAC addresses derived from " + inSource + " : " );
              for ( String nextMac : inMacs ) {
                  System.err.println( " " + nextMac );
              }
          }
      }
      ---------- END SOURCE ----------

            chegar Chris Hegarty
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: