JDK-8044306 : java.net.Inet4AddressImpl.getLocalHostName() uses IPv6 rather than IPv4
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: os_x
  • CPU: x86
  • Submitted: 2012-08-03
  • Updated: 2018-06-07
  • Resolved: 2018-06-07
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b22)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin Kernel Version 12.0.0: Sun Jun 24 23:00:16 PDT 2012; root:xnu-2050.7.9~1/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
in jdk/src/solaris/native/java/net/Inet4AddressImpl.c, the method Java_java_net_Inet4AddressImpl_getLocalHostName() calls getaddrinfo() with hints.ai_family = AF_UNSPEC

On Mac OS X, AF_UNSPEC prefers IPv6. To prefer IPv4, the method should use hints.ai_family = AF_INET



REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Connect Mac to a network with broken IPv6 DNS (sorry, I don't know the network configuration specifics)
2. Force Java to use IPv4 by exporting JAVA_TOOL_OPTIONS=-Djava.net.preferIPv4Stack=true
3. Run test case that prints out the results of java.net.InetAddress.getLocalHost()


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Local host address returned in milliseconds
ACTUAL -
Local host address returned after 5 second delay

REPRODUCIBILITY :
This bug can be reproduced always.

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


public class LocalHost
{
	public static void main(String[] args)
	{
		try
		{
			System.out.println(java.net.InetAddress.getLocalHost());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
No run-time workaround.

Applying this patch to the source and re-building the JDK fixes the issue:


--- jdk/src/solaris/native/java/net/Inet4AddressImpl.c	2012-06-04 16:50:06.000000000 -0400
+++ jdk/src/solaris/native/java/net/Inet4AddressImpl.c	2012-06-08 10:34:31.000000000 -0400
@@ -76,7 +76,7 @@
 
          memset(&hints, 0, sizeof(hints));
          hints.ai_flags = AI_CANONNAME;
-         hints.ai_family = AF_UNSPEC;
+         hints.ai_family = AF_INET;
 
          error = getaddrinfo(hostname, NULL, &hints, &res);

Comments
The bug was fixed via JDK-8201369
07-06-2018

The getaddrinfo usage in Java_java_net_Inet4AddressImpl_getLocalHostName probably should be checked anyway as the family should be AF_INET.
29-05-2014