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

InetAddress.getLocalHost() returns wrong IP address

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • None
    • 7u17, 8
    • core-libs

      FULL PRODUCT VERSION :
      java version " 1.8.0-ea "
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b92)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b34, mixed mode)


      java version " 1.7.0_17 "
      Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Darwin macbook-retina.speedport.ip 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      bug occurs with 1.7.0_17 and 1.8.0b92

      A DESCRIPTION OF THE PROBLEM :
      InetAddress.getLocalHost() returns wrong IP address although the addresses returned by NetworkInterface.getNetworkInterfaces() are correct.
      The call to InetAddress.getAddressesFromNameService(local, null); inside InetAddress.getLocalHost() returns the wrong IP resulting from a buggy DNS lookup implementation.

      An iteration over the NetworkInterface.getNetworkInterfaces() returns the correct IP addresses:
         iadr: /fe80:0:0:0:baf6:b1ff:fe10:c74b%4
         iadr: /192.168.5.179
      But InetAddress.getLocalAddress() returns the correct FQDN but the IP address of the DNS server.
          192.168.56.1


      Shell command output:

      macbook-retina:java snazy$ host macbook-retina.speedport.ip
      macbook-retina.speedport.ip has address 192.168.5.179

      macbook-retina:java snazy$ nslookup macbook-retina.speedport.ip
      Server:192.168.5.1
      Address:192.168.5.1#53

      Name:macbook-retina.speedport.ip
      Address: 192.168.5.179



      REGRESSION. Last worked in version 7u25

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call " InetAddress.getLocalHost() " on MacOS and check returned IP address.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      InetAddress.getLocalHost() must return correct IP address.

      ACTUAL -
      InetAddress.getLocalHost() returns DNS server address.


      Output from JUnit test case:

      macbook-retina.speedport.ip/192.168.56.1
      192.168.56.1
      netIf en0 has inet /fe80:0:0:0:baf6:b1ff:fe10:c74b%4/64 [null]
      netIf en0 has inet /192.168.5.179/24 [/192.168.5.255]
      netIf lo0 has inet /0:0:0:0:0:0:0:1/128 [null]
      netIf lo0 has inet /fe80:0:0:0:0:0:0:1%1/64 [null]
      netIf lo0 has inet /127.0.0.1/0 [null]

      java.lang.AssertionError: local address no

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import org.junit.Assert;
      import org.junit.Test;

      import java.net.*;
      import java.util.Arrays;
      import java.util.Enumeration;

      @SuppressWarnings( " UseOfSystemOutOrSystemErr " )
      public class OwnIpTest {
          @Test
          public void ownIp() throws UnknownHostException, SocketException {
              InetAddress local = InetAddress.getLocalHost();

              System.out.println(local);
              System.out.println(local.getHostAddress());

              boolean found = false;

              for (Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); eni.hasMoreElements(); ) {
                  NetworkInterface ni = eni.nextElement();
                  for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                      System.out.println( " netIf " + ni.getDisplayName() + " has inet " + interfaceAddress);
                      if (Arrays.equals(local.getAddress(), interfaceAddress.getAddress().getAddress())) {
                          found = true;
                          System.out.println( " found local address in network interface " + ni.getDisplayName());
                      }
                  }

              }

              Assert.assertTrue( " local address not found in any network interface " , found);
          }
      }

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

            khazra Kurchi Subhra Hazra
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: