# HG changeset patch # User vtewari # Date 1464117318 -3600 # Node ID ca398af91529aa9857a2a40f4953c19cd03c8f67 # Parent 82c48058acc2ce1447de2b1eb5ca3d978a2883fc 8016521: InetAddress should not always re-order addresses returned from name service Reviewed-by: chegar diff -r 82c48058acc2 -r ca398af91529 jdk/src/java.base/share/classes/java/net/Inet6AddressImpl.java --- a/jdk/src/java.base/share/classes/java/net/Inet6AddressImpl.java Tue May 24 10:14:41 2016 -0700 +++ b/jdk/src/java.base/share/classes/java/net/Inet6AddressImpl.java Tue May 24 20:15:18 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,10 @@ * questions. */ package java.net; + import java.io.IOException; +import static java.net.InetAddress.PREFER_IPV6_VALUE; +import static java.net.InetAddress.PREFER_SYSTEM_VALUE; /* * Package private implementation of InetAddressImpl for dual @@ -35,15 +38,23 @@ * * @since 1.4 */ +class Inet6AddressImpl implements InetAddressImpl { -class Inet6AddressImpl implements InetAddressImpl { public native String getLocalHostName() throws UnknownHostException; - public native InetAddress[] - lookupAllHostAddr(String hostname) throws UnknownHostException; + + public native InetAddress[] lookupAllHostAddr(String hostname) + throws UnknownHostException; + public native String getHostByAddr(byte[] addr) throws UnknownHostException; - private native boolean isReachable0(byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope) throws IOException; + + private native boolean isReachable0(byte[] addr, int scope, int timeout, + byte[] inf, int ttl, int if_scope) + throws IOException; - public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl) throws IOException { + public boolean isReachable(InetAddress addr, int timeout, + NetworkInterface netif, int ttl) + throws IOException + { byte[] ifaddr = null; int scope = -1; int netif_scope = -1; @@ -79,7 +90,8 @@ public synchronized InetAddress anyLocalAddress() { if (anyLocalAddress == null) { - if (InetAddress.preferIPv6Address) { + if (InetAddress.preferIPv6Address == PREFER_IPV6_VALUE || + InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE) { anyLocalAddress = new Inet6Address(); anyLocalAddress.holder().hostName = "::"; } else { @@ -91,7 +103,8 @@ public synchronized InetAddress loopbackAddress() { if (loopbackAddress == null) { - if (InetAddress.preferIPv6Address) { + if (InetAddress.preferIPv6Address == PREFER_IPV6_VALUE || + InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE) { byte[] loopback = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; @@ -103,6 +116,6 @@ return loopbackAddress; } - private InetAddress anyLocalAddress; - private InetAddress loopbackAddress; + private InetAddress anyLocalAddress; + private InetAddress loopbackAddress; } diff -r 82c48058acc2 -r ca398af91529 jdk/src/java.base/share/classes/java/net/InetAddress.java --- a/jdk/src/java.base/share/classes/java/net/InetAddress.java Tue May 24 10:14:41 2016 -0700 +++ b/jdk/src/java.base/share/classes/java/net/InetAddress.java Tue May 24 20:15:18 2016 +0100 @@ -26,8 +26,6 @@ package java.net; import java.util.NavigableSet; -import java.util.Iterator; -import java.util.List; import java.util.ArrayList; import java.util.Objects; import java.util.Scanner; @@ -41,6 +39,7 @@ import java.io.ObjectInputStream.GetField; import java.io.ObjectOutputStream; import java.io.ObjectOutputStream.PutField; +import java.lang.annotation.Native; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentSkipListSet; @@ -193,6 +192,11 @@ */ public class InetAddress implements java.io.Serializable { + + @Native static final int PREFER_IPV4_VALUE = 0; + @Native static final int PREFER_IPV6_VALUE = 1; + @Native static final int PREFER_SYSTEM_VALUE = 2; + /** * Specify the address family: Internet Protocol, Version 4 * @since 1.4 @@ -206,8 +210,7 @@ static final int IPv6 = 2; /* Specify address family preference */ - static transient boolean preferIPv6Address = false; - + static transient final int preferIPv6Address; static class InetAddressHolder { /** @@ -293,8 +296,19 @@ * Load net library into runtime, and perform initializations. */ static { - preferIPv6Address = java.security.AccessController.doPrivileged( - new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue(); + String str = java.security.AccessController.doPrivileged( + new GetPropertyAction("java.net.preferIPv6Addresses")); + if (str == null) { + preferIPv6Address = PREFER_IPV4_VALUE; + } else if (str.equalsIgnoreCase("true")) { + preferIPv6Address = PREFER_IPV6_VALUE; + } else if (str.equalsIgnoreCase("false")) { + preferIPv6Address = PREFER_IPV4_VALUE; + } else if (str.equalsIgnoreCase("system")) { + preferIPv6Address = PREFER_SYSTEM_VALUE; + } else { + preferIPv6Address = PREFER_IPV4_VALUE; + } AccessController.doPrivileged( new java.security.PrivilegedAction<>() { public Void run() { diff -r 82c48058acc2 -r ca398af91529 jdk/src/java.base/share/classes/java/net/doc-files/net-properties.html --- a/jdk/src/java.base/share/classes/java/net/doc-files/net-properties.html Tue May 24 10:14:41 2016 -0700 +++ b/jdk/src/java.base/share/classes/java/net/doc-files/net-properties.html Tue May 24 20:15:18 2016 +0100 @@ -1,5 +1,5 @@