author | aeubanks |
Wed, 15 May 2019 09:15:59 -0700 | |
changeset 54945 | cc92a45f4c73 |
parent 47216 | 71c04702a3d5 |
child 55596 | d01b345865d7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package java.net; |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
26 |
|
2 | 27 |
import java.io.IOException; |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
28 |
import static java.net.InetAddress.PREFER_IPV6_VALUE; |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
29 |
import static java.net.InetAddress.PREFER_SYSTEM_VALUE; |
2 | 30 |
|
31 |
/* |
|
32 |
* Package private implementation of InetAddressImpl for dual |
|
33 |
* IPv4/IPv6 stack. |
|
34 |
* <p> |
|
54945
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
35 |
* If InetAddress.preferIPv6Address is true then anyLocalAddress() |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
36 |
* and localHost() will return IPv6 addresses, otherwise IPv4 addresses. |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
37 |
* |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
38 |
* loopbackAddress() will return the first valid loopback address in |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
39 |
* [IPv6 loopback, IPv4 loopback] if InetAddress.preferIPv6Address is true, |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
40 |
* else [IPv4 loopback, IPv6 loopback]. |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
41 |
* If neither are valid it will fallback to the first address tried. |
2 | 42 |
* |
43 |
* @since 1.4 |
|
44 |
*/ |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
45 |
class Inet6AddressImpl implements InetAddressImpl { |
2 | 46 |
|
47 |
public native String getLocalHostName() throws UnknownHostException; |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
48 |
|
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
49 |
public native InetAddress[] lookupAllHostAddr(String hostname) |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
50 |
throws UnknownHostException; |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
51 |
|
2 | 52 |
public native String getHostByAddr(byte[] addr) throws UnknownHostException; |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
53 |
|
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
54 |
private native boolean isReachable0(byte[] addr, int scope, int timeout, |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
55 |
byte[] inf, int ttl, int if_scope) |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
56 |
throws IOException; |
2 | 57 |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
58 |
public boolean isReachable(InetAddress addr, int timeout, |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
59 |
NetworkInterface netif, int ttl) |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
60 |
throws IOException |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
61 |
{ |
2 | 62 |
byte[] ifaddr = null; |
63 |
int scope = -1; |
|
64 |
int netif_scope = -1; |
|
65 |
if (netif != null) { |
|
66 |
/* |
|
67 |
* Let's make sure we bind to an address of the proper family. |
|
68 |
* Which means same family as addr because at this point it could |
|
69 |
* be either an IPv6 address or an IPv4 address (case of a dual |
|
70 |
* stack system). |
|
71 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
72 |
java.util.Enumeration<InetAddress> it = netif.getInetAddresses(); |
2 | 73 |
InetAddress inetaddr = null; |
74 |
while (it.hasMoreElements()) { |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
75 |
inetaddr = it.nextElement(); |
2 | 76 |
if (inetaddr.getClass().isInstance(addr)) { |
77 |
ifaddr = inetaddr.getAddress(); |
|
78 |
if (inetaddr instanceof Inet6Address) { |
|
79 |
netif_scope = ((Inet6Address) inetaddr).getScopeId(); |
|
80 |
} |
|
81 |
break; |
|
82 |
} |
|
83 |
} |
|
84 |
if (ifaddr == null) { |
|
85 |
// Interface doesn't support the address family of |
|
86 |
// the destination |
|
87 |
return false; |
|
88 |
} |
|
89 |
} |
|
90 |
if (addr instanceof Inet6Address) |
|
91 |
scope = ((Inet6Address) addr).getScopeId(); |
|
92 |
return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope); |
|
93 |
} |
|
94 |
||
95 |
public synchronized InetAddress anyLocalAddress() { |
|
96 |
if (anyLocalAddress == null) { |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
97 |
if (InetAddress.preferIPv6Address == PREFER_IPV6_VALUE || |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
98 |
InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE) { |
2 | 99 |
anyLocalAddress = new Inet6Address(); |
16870 | 100 |
anyLocalAddress.holder().hostName = "::"; |
2 | 101 |
} else { |
102 |
anyLocalAddress = (new Inet4AddressImpl()).anyLocalAddress(); |
|
103 |
} |
|
104 |
} |
|
105 |
return anyLocalAddress; |
|
106 |
} |
|
107 |
||
108 |
public synchronized InetAddress loopbackAddress() { |
|
109 |
if (loopbackAddress == null) { |
|
54945
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
110 |
boolean preferIPv6Address = |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
111 |
InetAddress.preferIPv6Address == PREFER_IPV6_VALUE || |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
112 |
InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
113 |
InetAddress loopback4 = (new Inet4AddressImpl()).loopbackAddress(); |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
114 |
InetAddress loopback6 = new Inet6Address("localhost", |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
115 |
new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
116 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
117 |
// Order the candidate addresses by preference. |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
118 |
InetAddress[] addresses = preferIPv6Address |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
119 |
? new InetAddress[] {loopback6, loopback4} |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
120 |
: new InetAddress[] {loopback4, loopback6}; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
121 |
// In case of failure, default to the preferred address. |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
122 |
loopbackAddress = addresses[0]; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
123 |
// Pick the first candidate address that actually exists. |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
124 |
for (InetAddress address : addresses) { |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
125 |
try { |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
126 |
if (NetworkInterface.getByInetAddress(address) == null) { |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
127 |
continue; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
128 |
} |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
129 |
} catch (SocketException e) { |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
130 |
continue; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
131 |
} |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
132 |
loopbackAddress = address; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
133 |
break; |
cc92a45f4c73
8223214: Inet6AddressImpl.loopbackAddress() should choose loopback address that is available
aeubanks
parents:
47216
diff
changeset
|
134 |
} |
2 | 135 |
} |
136 |
return loopbackAddress; |
|
137 |
} |
|
138 |
||
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
139 |
private InetAddress anyLocalAddress; |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
25859
diff
changeset
|
140 |
private InetAddress loopbackAddress; |
2 | 141 |
} |