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

localhost resolved to IPv4 with java.net.preferIPv6Addresses set to true

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Tested on two systems, identical behaviour:

      #1 Microsoft Windows 10 Enterprise, 10.0.19045 Build 19045
      output of uname -o -i -s -r -v in WSL:
      Linux 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 GNU/Linux
      output of java --version in WSL:
      openjdk 21.0.4 2024-07-16
      OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu222.04)
      OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu222.04, mixed mode, sharing)

      #2 Microsoft Windows 11 Pro, 10.0.22631 Build 22631
      output of uname -o -i -s -r -v in WSL:
      Linux 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 GNU/Linux
      output of java --version in WSL:
      openjdk 21.0.4 2024-07-16
      OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu224.04)
      OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      The "localhost" is resolved to the IPv4 address instead of the IPv6 address, this is problematic in case a server started on the loopback address (resolved to the IPv6 when java.net.preferIPv6Addresses is set true) and the HttpClient does try to access it. The URL "http://localhost" will be resolved by the HttpClient to IPv4 and the call will fail.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run code below, the "localhost => " line should print an IPv6 address.
      Running on W10/W11 will print the expected output.
      Running on WSL2 inside W10/W11 will print the - in my mind - incorrect result.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      should return IPv6 address for InetAddress.getByName("localhost") when java.net.preferIPv6Addresses set to true
      ACTUAL -
      IPv4 address

      ---------- BEGIN SOURCE ----------
      import java.net.InetAddress;

      public class Test {
          public static void main(String[] args) throws Exception {
              System.setProperty("java.net.preferIPv6Addresses", "true");
                  System.out.println("null => " + InetAddress.getByName(null));
                  System.out.println("loopback => " + InetAddress.getLoopbackAddress());
                  System.out.println("localhost => " + InetAddress.getByName("localhost"));
          }
      }

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

      FREQUENCY : always


            aefimov Aleksej Efimov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: