author | chegar |
Fri, 09 Aug 2013 13:50:13 +0100 | |
changeset 19379 | 1319fdb10f71 |
parent 19069 | 1d9cb0d080e3 |
child 20848 | 0dc5945a4b01 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18800 | 2 |
* Copyright (c) 1995, 2013, 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 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.util.HashMap; |
|
29 |
import java.util.LinkedHashMap; |
|
30 |
import java.util.Random; |
|
31 |
import java.util.Iterator; |
|
32 |
import java.util.LinkedList; |
|
33 |
import java.util.List; |
|
34 |
import java.util.ArrayList; |
|
11119
6ff03c1202ce
7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents:
10882
diff
changeset
|
35 |
import java.util.ServiceLoader; |
2 | 36 |
import java.security.AccessController; |
37 |
import java.io.ObjectStreamException; |
|
16870 | 38 |
import java.io.ObjectStreamField; |
2 | 39 |
import java.io.IOException; |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
40 |
import java.io.ObjectInputStream; |
16870 | 41 |
import java.io.ObjectInputStream.GetField; |
42 |
import java.io.ObjectOutputStream; |
|
43 |
import java.io.ObjectOutputStream.PutField; |
|
2 | 44 |
import sun.security.action.*; |
45 |
import sun.net.InetAddressCachePolicy; |
|
46 |
import sun.net.util.IPAddressUtil; |
|
47 |
import sun.net.spi.nameservice.*; |
|
48 |
||
49 |
/** |
|
50 |
* This class represents an Internet Protocol (IP) address. |
|
51 |
* |
|
52 |
* <p> An IP address is either a 32-bit or 128-bit unsigned number |
|
53 |
* used by IP, a lower-level protocol on which protocols like UDP and |
|
54 |
* TCP are built. The IP address architecture is defined by <a |
|
55 |
* href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC 790: |
|
56 |
* Assigned Numbers</i></a>, <a |
|
57 |
* href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC 1918: |
|
58 |
* Address Allocation for Private Internets</i></a>, <a |
|
59 |
* href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC 2365: |
|
60 |
* Administratively Scoped IP Multicast</i></a>, and <a |
|
61 |
* href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP |
|
62 |
* Version 6 Addressing Architecture</i></a>. An instance of an |
|
63 |
* InetAddress consists of an IP address and possibly its |
|
64 |
* corresponding host name (depending on whether it is constructed |
|
65 |
* with a host name or whether it has already done reverse host name |
|
66 |
* resolution). |
|
67 |
* |
|
18800 | 68 |
* <h3> Address types </h3> |
2 | 69 |
* |
70 |
* <blockquote><table cellspacing=2 summary="Description of unicast and multicast address types"> |
|
71 |
* <tr><th valign=top><i>unicast</i></th> |
|
72 |
* <td>An identifier for a single interface. A packet sent to |
|
73 |
* a unicast address is delivered to the interface identified by |
|
74 |
* that address. |
|
75 |
* |
|
76 |
* <p> The Unspecified Address -- Also called anylocal or wildcard |
|
77 |
* address. It must never be assigned to any node. It indicates the |
|
78 |
* absence of an address. One example of its use is as the target of |
|
79 |
* bind, which allows a server to accept a client connection on any |
|
80 |
* interface, in case the server host has multiple interfaces. |
|
81 |
* |
|
82 |
* <p> The <i>unspecified</i> address must not be used as |
|
83 |
* the destination address of an IP packet. |
|
84 |
* |
|
85 |
* <p> The <i>Loopback</i> Addresses -- This is the address |
|
86 |
* assigned to the loopback interface. Anything sent to this |
|
87 |
* IP address loops around and becomes IP input on the local |
|
88 |
* host. This address is often used when testing a |
|
89 |
* client.</td></tr> |
|
90 |
* <tr><th valign=top><i>multicast</i></th> |
|
91 |
* <td>An identifier for a set of interfaces (typically belonging |
|
92 |
* to different nodes). A packet sent to a multicast address is |
|
93 |
* delivered to all interfaces identified by that address.</td></tr> |
|
94 |
* </table></blockquote> |
|
95 |
* |
|
96 |
* <h4> IP address scope </h4> |
|
97 |
* |
|
98 |
* <p> <i>Link-local</i> addresses are designed to be used for addressing |
|
99 |
* on a single link for purposes such as auto-address configuration, |
|
100 |
* neighbor discovery, or when no routers are present. |
|
101 |
* |
|
102 |
* <p> <i>Site-local</i> addresses are designed to be used for addressing |
|
103 |
* inside of a site without the need for a global prefix. |
|
104 |
* |
|
105 |
* <p> <i>Global</i> addresses are unique across the internet. |
|
106 |
* |
|
107 |
* <h4> Textual representation of IP addresses </h4> |
|
108 |
* |
|
109 |
* The textual representation of an IP address is address family specific. |
|
110 |
* |
|
111 |
* <p> |
|
112 |
* |
|
113 |
* For IPv4 address format, please refer to <A |
|
114 |
* HREF="Inet4Address.html#format">Inet4Address#format</A>; For IPv6 |
|
115 |
* address format, please refer to <A |
|
116 |
* HREF="Inet6Address.html#format">Inet6Address#format</A>. |
|
117 |
* |
|
118 |
* <P>There is a <a href="doc-files/net-properties.html#Ipv4IPv6">couple of |
|
119 |
* System Properties</a> affecting how IPv4 and IPv6 addresses are used.</P> |
|
120 |
* |
|
121 |
* <h4> Host Name Resolution </h4> |
|
122 |
* |
|
123 |
* Host name-to-IP address <i>resolution</i> is accomplished through |
|
124 |
* the use of a combination of local machine configuration information |
|
125 |
* and network naming services such as the Domain Name System (DNS) |
|
126 |
* and Network Information Service(NIS). The particular naming |
|
127 |
* services(s) being used is by default the local machine configured |
|
128 |
* one. For any host name, its corresponding IP address is returned. |
|
129 |
* |
|
130 |
* <p> <i>Reverse name resolution</i> means that for any IP address, |
|
131 |
* the host associated with the IP address is returned. |
|
132 |
* |
|
133 |
* <p> The InetAddress class provides methods to resolve host names to |
|
134 |
* their IP addresses and vice versa. |
|
135 |
* |
|
136 |
* <h4> InetAddress Caching </h4> |
|
137 |
* |
|
138 |
* The InetAddress class has a cache to store successful as well as |
|
139 |
* unsuccessful host name resolutions. |
|
140 |
* |
|
141 |
* <p> By default, when a security manager is installed, in order to |
|
142 |
* protect against DNS spoofing attacks, |
|
143 |
* the result of positive host name resolutions are |
|
144 |
* cached forever. When a security manager is not installed, the default |
|
145 |
* behavior is to cache entries for a finite (implementation dependent) |
|
146 |
* period of time. The result of unsuccessful host |
|
147 |
* name resolution is cached for a very short period of time (10 |
|
148 |
* seconds) to improve performance. |
|
149 |
* |
|
150 |
* <p> If the default behavior is not desired, then a Java security property |
|
151 |
* can be set to a different Time-to-live (TTL) value for positive |
|
152 |
* caching. Likewise, a system admin can configure a different |
|
153 |
* negative caching TTL value when needed. |
|
154 |
* |
|
155 |
* <p> Two Java security properties control the TTL values used for |
|
156 |
* positive and negative host name resolution caching: |
|
157 |
* |
|
158 |
* <blockquote> |
|
159 |
* <dl> |
|
160 |
* <dt><b>networkaddress.cache.ttl</b></dt> |
|
161 |
* <dd>Indicates the caching policy for successful name lookups from |
|
162 |
* the name service. The value is specified as as integer to indicate |
|
163 |
* the number of seconds to cache the successful lookup. The default |
|
164 |
* setting is to cache for an implementation specific period of time. |
|
165 |
* <p> |
|
166 |
* A value of -1 indicates "cache forever". |
|
167 |
* </dd> |
|
168 |
* <dt><b>networkaddress.cache.negative.ttl</b> (default: 10)</dt> |
|
169 |
* <dd>Indicates the caching policy for un-successful name lookups |
|
170 |
* from the name service. The value is specified as as integer to |
|
171 |
* indicate the number of seconds to cache the failure for |
|
172 |
* un-successful lookups. |
|
173 |
* <p> |
|
174 |
* A value of 0 indicates "never cache". |
|
175 |
* A value of -1 indicates "cache forever". |
|
176 |
* </dd> |
|
177 |
* </dl> |
|
178 |
* </blockquote> |
|
179 |
* |
|
180 |
* @author Chris Warth |
|
181 |
* @see java.net.InetAddress#getByAddress(byte[]) |
|
182 |
* @see java.net.InetAddress#getByAddress(java.lang.String, byte[]) |
|
183 |
* @see java.net.InetAddress#getAllByName(java.lang.String) |
|
184 |
* @see java.net.InetAddress#getByName(java.lang.String) |
|
185 |
* @see java.net.InetAddress#getLocalHost() |
|
186 |
* @since JDK1.0 |
|
187 |
*/ |
|
188 |
public |
|
189 |
class InetAddress implements java.io.Serializable { |
|
190 |
/** |
|
191 |
* Specify the address family: Internet Protocol, Version 4 |
|
192 |
* @since 1.4 |
|
193 |
*/ |
|
194 |
static final int IPv4 = 1; |
|
195 |
||
196 |
/** |
|
197 |
* Specify the address family: Internet Protocol, Version 6 |
|
198 |
* @since 1.4 |
|
199 |
*/ |
|
200 |
static final int IPv6 = 2; |
|
201 |
||
202 |
/* Specify address family preference */ |
|
203 |
static transient boolean preferIPv6Address = false; |
|
204 |
||
16870 | 205 |
static class InetAddressHolder { |
206 |
||
207 |
InetAddressHolder() {} |
|
208 |
||
209 |
InetAddressHolder(String hostName, int address, int family) { |
|
210 |
this.hostName = hostName; |
|
211 |
this.address = address; |
|
212 |
this.family = family; |
|
213 |
} |
|
214 |
||
215 |
String hostName; |
|
216 |
||
217 |
String getHostName() { |
|
218 |
return hostName; |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Holds a 32-bit IPv4 address. |
|
223 |
*/ |
|
224 |
int address; |
|
2 | 225 |
|
16870 | 226 |
int getAddress() { |
227 |
return address; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Specifies the address family type, for instance, '1' for IPv4 |
|
232 |
* addresses, and '2' for IPv6 addresses. |
|
233 |
*/ |
|
234 |
int family; |
|
2 | 235 |
|
16870 | 236 |
int getFamily() { |
237 |
return family; |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
/* Used to store the serializable fields of InetAddress */ |
|
242 |
private final transient InetAddressHolder holder; |
|
243 |
||
244 |
InetAddressHolder holder() { |
|
245 |
return holder; |
|
246 |
} |
|
2 | 247 |
|
248 |
/* Used to store the name service provider */ |
|
249 |
private static List<NameService> nameServices = null; |
|
250 |
||
251 |
/* Used to store the best available hostname */ |
|
252 |
private transient String canonicalHostName = null; |
|
253 |
||
254 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
|
255 |
private static final long serialVersionUID = 3286316764910316507L; |
|
256 |
||
257 |
/* |
|
258 |
* Load net library into runtime, and perform initializations. |
|
259 |
*/ |
|
260 |
static { |
|
261 |
preferIPv6Address = java.security.AccessController.doPrivileged( |
|
262 |
new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue(); |
|
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
263 |
AccessController.doPrivileged( |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
264 |
new java.security.PrivilegedAction<Void>() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
265 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
266 |
System.loadLibrary("net"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
267 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
268 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
11119
diff
changeset
|
269 |
}); |
2 | 270 |
init(); |
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Constructor for the Socket.accept() method. |
|
275 |
* This creates an empty InetAddress, which is filled in by |
|
276 |
* the accept() method. This InetAddress, however, is not |
|
277 |
* put in the address cache, since it is not created by name. |
|
278 |
*/ |
|
279 |
InetAddress() { |
|
16870 | 280 |
holder = new InetAddressHolder(); |
2 | 281 |
} |
282 |
||
283 |
/** |
|
284 |
* Replaces the de-serialized object with an Inet4Address object. |
|
285 |
* |
|
286 |
* @return the alternate object to the de-serialized object. |
|
287 |
* |
|
288 |
* @throws ObjectStreamException if a new object replacing this |
|
289 |
* object could not be created |
|
290 |
*/ |
|
291 |
private Object readResolve() throws ObjectStreamException { |
|
292 |
// will replace the deserialized 'this' object |
|
16870 | 293 |
return new Inet4Address(holder().getHostName(), holder().getAddress()); |
2 | 294 |
} |
295 |
||
296 |
/** |
|
297 |
* Utility routine to check if the InetAddress is an |
|
298 |
* IP multicast address. |
|
19069 | 299 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 300 |
* an IP multicast address |
301 |
* @since JDK1.1 |
|
302 |
*/ |
|
303 |
public boolean isMulticastAddress() { |
|
304 |
return false; |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Utility routine to check if the InetAddress in a wildcard address. |
|
19069 | 309 |
* @return a {@code boolean} indicating if the Inetaddress is |
2 | 310 |
* a wildcard address. |
311 |
* @since 1.4 |
|
312 |
*/ |
|
313 |
public boolean isAnyLocalAddress() { |
|
314 |
return false; |
|
315 |
} |
|
316 |
||
317 |
/** |
|
318 |
* Utility routine to check if the InetAddress is a loopback address. |
|
319 |
* |
|
19069 | 320 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 321 |
* a loopback address; or false otherwise. |
322 |
* @since 1.4 |
|
323 |
*/ |
|
324 |
public boolean isLoopbackAddress() { |
|
325 |
return false; |
|
326 |
} |
|
327 |
||
328 |
/** |
|
329 |
* Utility routine to check if the InetAddress is an link local address. |
|
330 |
* |
|
19069 | 331 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 332 |
* a link local address; or false if address is not a link local unicast address. |
333 |
* @since 1.4 |
|
334 |
*/ |
|
335 |
public boolean isLinkLocalAddress() { |
|
336 |
return false; |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Utility routine to check if the InetAddress is a site local address. |
|
341 |
* |
|
19069 | 342 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 343 |
* a site local address; or false if address is not a site local unicast address. |
344 |
* @since 1.4 |
|
345 |
*/ |
|
346 |
public boolean isSiteLocalAddress() { |
|
347 |
return false; |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* Utility routine to check if the multicast address has global scope. |
|
352 |
* |
|
19069 | 353 |
* @return a {@code boolean} indicating if the address has |
2 | 354 |
* is a multicast address of global scope, false if it is not |
355 |
* of global scope or it is not a multicast address |
|
356 |
* @since 1.4 |
|
357 |
*/ |
|
358 |
public boolean isMCGlobal() { |
|
359 |
return false; |
|
360 |
} |
|
361 |
||
362 |
/** |
|
363 |
* Utility routine to check if the multicast address has node scope. |
|
364 |
* |
|
19069 | 365 |
* @return a {@code boolean} indicating if the address has |
2 | 366 |
* is a multicast address of node-local scope, false if it is not |
367 |
* of node-local scope or it is not a multicast address |
|
368 |
* @since 1.4 |
|
369 |
*/ |
|
370 |
public boolean isMCNodeLocal() { |
|
371 |
return false; |
|
372 |
} |
|
373 |
||
374 |
/** |
|
375 |
* Utility routine to check if the multicast address has link scope. |
|
376 |
* |
|
19069 | 377 |
* @return a {@code boolean} indicating if the address has |
2 | 378 |
* is a multicast address of link-local scope, false if it is not |
379 |
* of link-local scope or it is not a multicast address |
|
380 |
* @since 1.4 |
|
381 |
*/ |
|
382 |
public boolean isMCLinkLocal() { |
|
383 |
return false; |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Utility routine to check if the multicast address has site scope. |
|
388 |
* |
|
19069 | 389 |
* @return a {@code boolean} indicating if the address has |
2 | 390 |
* is a multicast address of site-local scope, false if it is not |
391 |
* of site-local scope or it is not a multicast address |
|
392 |
* @since 1.4 |
|
393 |
*/ |
|
394 |
public boolean isMCSiteLocal() { |
|
395 |
return false; |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Utility routine to check if the multicast address has organization scope. |
|
400 |
* |
|
19069 | 401 |
* @return a {@code boolean} indicating if the address has |
2 | 402 |
* is a multicast address of organization-local scope, |
403 |
* false if it is not of organization-local scope |
|
404 |
* or it is not a multicast address |
|
405 |
* @since 1.4 |
|
406 |
*/ |
|
407 |
public boolean isMCOrgLocal() { |
|
408 |
return false; |
|
409 |
} |
|
410 |
||
411 |
||
412 |
/** |
|
413 |
* Test whether that address is reachable. Best effort is made by the |
|
414 |
* implementation to try to reach the host, but firewalls and server |
|
415 |
* configuration may block requests resulting in a unreachable status |
|
416 |
* while some specific ports may be accessible. |
|
417 |
* A typical implementation will use ICMP ECHO REQUESTs if the |
|
418 |
* privilege can be obtained, otherwise it will try to establish |
|
419 |
* a TCP connection on port 7 (Echo) of the destination host. |
|
420 |
* <p> |
|
421 |
* The timeout value, in milliseconds, indicates the maximum amount of time |
|
422 |
* the try should take. If the operation times out before getting an |
|
423 |
* answer, the host is deemed unreachable. A negative value will result |
|
424 |
* in an IllegalArgumentException being thrown. |
|
425 |
* |
|
426 |
* @param timeout the time, in milliseconds, before the call aborts |
|
19069 | 427 |
* @return a {@code boolean} indicating if the address is reachable. |
2 | 428 |
* @throws IOException if a network error occurs |
19069 | 429 |
* @throws IllegalArgumentException if {@code timeout} is negative. |
2 | 430 |
* @since 1.5 |
431 |
*/ |
|
432 |
public boolean isReachable(int timeout) throws IOException { |
|
433 |
return isReachable(null, 0 , timeout); |
|
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* Test whether that address is reachable. Best effort is made by the |
|
438 |
* implementation to try to reach the host, but firewalls and server |
|
439 |
* configuration may block requests resulting in a unreachable status |
|
440 |
* while some specific ports may be accessible. |
|
441 |
* A typical implementation will use ICMP ECHO REQUESTs if the |
|
442 |
* privilege can be obtained, otherwise it will try to establish |
|
443 |
* a TCP connection on port 7 (Echo) of the destination host. |
|
444 |
* <p> |
|
19069 | 445 |
* The {@code network interface} and {@code ttl} parameters |
2 | 446 |
* let the caller specify which network interface the test will go through |
447 |
* and the maximum number of hops the packets should go through. |
|
19069 | 448 |
* A negative value for the {@code ttl} will result in an |
2 | 449 |
* IllegalArgumentException being thrown. |
450 |
* <p> |
|
451 |
* The timeout value, in milliseconds, indicates the maximum amount of time |
|
452 |
* the try should take. If the operation times out before getting an |
|
453 |
* answer, the host is deemed unreachable. A negative value will result |
|
454 |
* in an IllegalArgumentException being thrown. |
|
455 |
* |
|
456 |
* @param netif the NetworkInterface through which the |
|
457 |
* test will be done, or null for any interface |
|
458 |
* @param ttl the maximum numbers of hops to try or 0 for the |
|
459 |
* default |
|
460 |
* @param timeout the time, in milliseconds, before the call aborts |
|
19069 | 461 |
* @throws IllegalArgumentException if either {@code timeout} |
462 |
* or {@code ttl} are negative. |
|
463 |
* @return a {@code boolean}indicating if the address is reachable. |
|
2 | 464 |
* @throws IOException if a network error occurs |
465 |
* @since 1.5 |
|
466 |
*/ |
|
467 |
public boolean isReachable(NetworkInterface netif, int ttl, |
|
468 |
int timeout) throws IOException { |
|
469 |
if (ttl < 0) |
|
470 |
throw new IllegalArgumentException("ttl can't be negative"); |
|
471 |
if (timeout < 0) |
|
472 |
throw new IllegalArgumentException("timeout can't be negative"); |
|
473 |
||
474 |
return impl.isReachable(this, timeout, netif, ttl); |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Gets the host name for this IP address. |
|
479 |
* |
|
480 |
* <p>If this InetAddress was created with a host name, |
|
481 |
* this host name will be remembered and returned; |
|
482 |
* otherwise, a reverse name lookup will be performed |
|
483 |
* and the result will be returned based on the system |
|
484 |
* configured name lookup service. If a lookup of the name service |
|
485 |
* is required, call |
|
486 |
* {@link #getCanonicalHostName() getCanonicalHostName}. |
|
487 |
* |
|
488 |
* <p>If there is a security manager, its |
|
19069 | 489 |
* {@code checkConnect} method is first called |
490 |
* with the hostname and {@code -1} |
|
2 | 491 |
* as its arguments to see if the operation is allowed. |
492 |
* If the operation is not allowed, it will return |
|
493 |
* the textual representation of the IP address. |
|
494 |
* |
|
495 |
* @return the host name for this IP address, or if the operation |
|
496 |
* is not allowed by the security check, the textual |
|
497 |
* representation of the IP address. |
|
498 |
* |
|
499 |
* @see InetAddress#getCanonicalHostName |
|
500 |
* @see SecurityManager#checkConnect |
|
501 |
*/ |
|
502 |
public String getHostName() { |
|
503 |
return getHostName(true); |
|
504 |
} |
|
505 |
||
506 |
/** |
|
507 |
* Returns the hostname for this address. |
|
508 |
* If the host is equal to null, then this address refers to any |
|
509 |
* of the local machine's available network addresses. |
|
510 |
* this is package private so SocketPermission can make calls into |
|
511 |
* here without a security check. |
|
512 |
* |
|
513 |
* <p>If there is a security manager, this method first |
|
19069 | 514 |
* calls its {@code checkConnect} method |
515 |
* with the hostname and {@code -1} |
|
2 | 516 |
* as its arguments to see if the calling code is allowed to know |
517 |
* the hostname for this IP address, i.e., to connect to the host. |
|
518 |
* If the operation is not allowed, it will return |
|
519 |
* the textual representation of the IP address. |
|
520 |
* |
|
521 |
* @return the host name for this IP address, or if the operation |
|
522 |
* is not allowed by the security check, the textual |
|
523 |
* representation of the IP address. |
|
524 |
* |
|
525 |
* @param check make security check if true |
|
526 |
* |
|
527 |
* @see SecurityManager#checkConnect |
|
528 |
*/ |
|
529 |
String getHostName(boolean check) { |
|
16870 | 530 |
if (holder().getHostName() == null) { |
531 |
holder().hostName = InetAddress.getHostFromNameService(this, check); |
|
2 | 532 |
} |
16870 | 533 |
return holder().getHostName(); |
2 | 534 |
} |
535 |
||
536 |
/** |
|
537 |
* Gets the fully qualified domain name for this IP address. |
|
538 |
* Best effort method, meaning we may not be able to return |
|
539 |
* the FQDN depending on the underlying system configuration. |
|
540 |
* |
|
541 |
* <p>If there is a security manager, this method first |
|
19069 | 542 |
* calls its {@code checkConnect} method |
543 |
* with the hostname and {@code -1} |
|
2 | 544 |
* as its arguments to see if the calling code is allowed to know |
545 |
* the hostname for this IP address, i.e., to connect to the host. |
|
546 |
* If the operation is not allowed, it will return |
|
547 |
* the textual representation of the IP address. |
|
548 |
* |
|
549 |
* @return the fully qualified domain name for this IP address, |
|
550 |
* or if the operation is not allowed by the security check, |
|
551 |
* the textual representation of the IP address. |
|
552 |
* |
|
553 |
* @see SecurityManager#checkConnect |
|
554 |
* |
|
555 |
* @since 1.4 |
|
556 |
*/ |
|
557 |
public String getCanonicalHostName() { |
|
558 |
if (canonicalHostName == null) { |
|
559 |
canonicalHostName = |
|
560 |
InetAddress.getHostFromNameService(this, true); |
|
561 |
} |
|
562 |
return canonicalHostName; |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* Returns the hostname for this address. |
|
567 |
* |
|
568 |
* <p>If there is a security manager, this method first |
|
19069 | 569 |
* calls its {@code checkConnect} method |
570 |
* with the hostname and {@code -1} |
|
2 | 571 |
* as its arguments to see if the calling code is allowed to know |
572 |
* the hostname for this IP address, i.e., to connect to the host. |
|
573 |
* If the operation is not allowed, it will return |
|
574 |
* the textual representation of the IP address. |
|
575 |
* |
|
576 |
* @return the host name for this IP address, or if the operation |
|
577 |
* is not allowed by the security check, the textual |
|
578 |
* representation of the IP address. |
|
579 |
* |
|
580 |
* @param check make security check if true |
|
581 |
* |
|
582 |
* @see SecurityManager#checkConnect |
|
583 |
*/ |
|
584 |
private static String getHostFromNameService(InetAddress addr, boolean check) { |
|
585 |
String host = null; |
|
586 |
for (NameService nameService : nameServices) { |
|
587 |
try { |
|
588 |
// first lookup the hostname |
|
589 |
host = nameService.getHostByAddr(addr.getAddress()); |
|
590 |
||
591 |
/* check to see if calling code is allowed to know |
|
592 |
* the hostname for this IP address, ie, connect to the host |
|
593 |
*/ |
|
594 |
if (check) { |
|
595 |
SecurityManager sec = System.getSecurityManager(); |
|
596 |
if (sec != null) { |
|
597 |
sec.checkConnect(host, -1); |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
/* now get all the IP addresses for this hostname, |
|
602 |
* and make sure one of them matches the original IP |
|
603 |
* address. We do this to try and prevent spoofing. |
|
604 |
*/ |
|
605 |
||
606 |
InetAddress[] arr = InetAddress.getAllByName0(host, check); |
|
607 |
boolean ok = false; |
|
608 |
||
609 |
if(arr != null) { |
|
610 |
for(int i = 0; !ok && i < arr.length; i++) { |
|
611 |
ok = addr.equals(arr[i]); |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
//XXX: if it looks a spoof just return the address? |
|
616 |
if (!ok) { |
|
617 |
host = addr.getHostAddress(); |
|
618 |
return host; |
|
619 |
} |
|
620 |
||
621 |
break; |
|
622 |
||
623 |
} catch (SecurityException e) { |
|
624 |
host = addr.getHostAddress(); |
|
625 |
break; |
|
626 |
} catch (UnknownHostException e) { |
|
627 |
host = addr.getHostAddress(); |
|
628 |
// let next provider resolve the hostname |
|
629 |
} |
|
630 |
} |
|
631 |
||
632 |
return host; |
|
633 |
} |
|
634 |
||
635 |
/** |
|
19069 | 636 |
* Returns the raw IP address of this {@code InetAddress} |
2 | 637 |
* object. The result is in network byte order: the highest order |
19069 | 638 |
* byte of the address is in {@code getAddress()[0]}. |
2 | 639 |
* |
640 |
* @return the raw IP address of this object. |
|
641 |
*/ |
|
642 |
public byte[] getAddress() { |
|
643 |
return null; |
|
644 |
} |
|
645 |
||
646 |
/** |
|
647 |
* Returns the IP address string in textual presentation. |
|
648 |
* |
|
649 |
* @return the raw IP address in a string format. |
|
650 |
* @since JDK1.0.2 |
|
651 |
*/ |
|
652 |
public String getHostAddress() { |
|
653 |
return null; |
|
654 |
} |
|
655 |
||
656 |
/** |
|
657 |
* Returns a hashcode for this IP address. |
|
658 |
* |
|
659 |
* @return a hash code value for this IP address. |
|
660 |
*/ |
|
661 |
public int hashCode() { |
|
662 |
return -1; |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* Compares this object against the specified object. |
|
19069 | 667 |
* The result is {@code true} if and only if the argument is |
668 |
* not {@code null} and it represents the same IP address as |
|
2 | 669 |
* this object. |
670 |
* <p> |
|
19069 | 671 |
* Two instances of {@code InetAddress} represent the same IP |
2 | 672 |
* address if the length of the byte arrays returned by |
19069 | 673 |
* {@code getAddress} is the same for both, and each of the |
2 | 674 |
* array components is the same for the byte arrays. |
675 |
* |
|
676 |
* @param obj the object to compare against. |
|
19069 | 677 |
* @return {@code true} if the objects are the same; |
678 |
* {@code false} otherwise. |
|
2 | 679 |
* @see java.net.InetAddress#getAddress() |
680 |
*/ |
|
681 |
public boolean equals(Object obj) { |
|
682 |
return false; |
|
683 |
} |
|
684 |
||
685 |
/** |
|
19069 | 686 |
* Converts this IP address to a {@code String}. The |
2 | 687 |
* string returned is of the form: hostname / literal IP |
688 |
* address. |
|
689 |
* |
|
690 |
* If the host name is unresolved, no reverse name service lookup |
|
691 |
* is performed. The hostname part will be represented by an empty string. |
|
692 |
* |
|
693 |
* @return a string representation of this IP address. |
|
694 |
*/ |
|
695 |
public String toString() { |
|
16870 | 696 |
String hostName = holder().getHostName(); |
2 | 697 |
return ((hostName != null) ? hostName : "") |
698 |
+ "/" + getHostAddress(); |
|
699 |
} |
|
700 |
||
701 |
/* |
|
702 |
* Cached addresses - our own litle nis, not! |
|
703 |
*/ |
|
704 |
private static Cache addressCache = new Cache(Cache.Type.Positive); |
|
705 |
||
706 |
private static Cache negativeCache = new Cache(Cache.Type.Negative); |
|
707 |
||
708 |
private static boolean addressCacheInit = false; |
|
709 |
||
710 |
static InetAddress[] unknown_array; // put THIS in cache |
|
711 |
||
712 |
static InetAddressImpl impl; |
|
713 |
||
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
714 |
private static final HashMap<String, Void> lookupTable = new HashMap<>(); |
2 | 715 |
|
716 |
/** |
|
717 |
* Represents a cache entry |
|
718 |
*/ |
|
719 |
static final class CacheEntry { |
|
720 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
721 |
CacheEntry(InetAddress[] addresses, long expiration) { |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
722 |
this.addresses = addresses; |
2 | 723 |
this.expiration = expiration; |
724 |
} |
|
725 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
726 |
InetAddress[] addresses; |
2 | 727 |
long expiration; |
728 |
} |
|
729 |
||
730 |
/** |
|
731 |
* A cache that manages entries based on a policy specified |
|
732 |
* at creation time. |
|
733 |
*/ |
|
734 |
static final class Cache { |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
735 |
private LinkedHashMap<String, CacheEntry> cache; |
2 | 736 |
private Type type; |
737 |
||
738 |
enum Type {Positive, Negative}; |
|
739 |
||
740 |
/** |
|
741 |
* Create cache |
|
742 |
*/ |
|
743 |
public Cache(Type type) { |
|
744 |
this.type = type; |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
745 |
cache = new LinkedHashMap<String, CacheEntry>(); |
2 | 746 |
} |
747 |
||
748 |
private int getPolicy() { |
|
749 |
if (type == Type.Positive) { |
|
750 |
return InetAddressCachePolicy.get(); |
|
751 |
} else { |
|
752 |
return InetAddressCachePolicy.getNegative(); |
|
753 |
} |
|
754 |
} |
|
755 |
||
756 |
/** |
|
757 |
* Add an entry to the cache. If there's already an |
|
758 |
* entry then for this host then the entry will be |
|
759 |
* replaced. |
|
760 |
*/ |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
761 |
public Cache put(String host, InetAddress[] addresses) { |
2 | 762 |
int policy = getPolicy(); |
763 |
if (policy == InetAddressCachePolicy.NEVER) { |
|
764 |
return this; |
|
765 |
} |
|
766 |
||
767 |
// purge any expired entries |
|
768 |
||
769 |
if (policy != InetAddressCachePolicy.FOREVER) { |
|
770 |
||
771 |
// As we iterate in insertion order we can |
|
772 |
// terminate when a non-expired entry is found. |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
773 |
LinkedList<String> expired = new LinkedList<>(); |
2 | 774 |
long now = System.currentTimeMillis(); |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
775 |
for (String key : cache.keySet()) { |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
776 |
CacheEntry entry = cache.get(key); |
2 | 777 |
|
778 |
if (entry.expiration >= 0 && entry.expiration < now) { |
|
779 |
expired.add(key); |
|
780 |
} else { |
|
781 |
break; |
|
782 |
} |
|
783 |
} |
|
784 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
785 |
for (String key : expired) { |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
786 |
cache.remove(key); |
2 | 787 |
} |
788 |
} |
|
789 |
||
790 |
// create new entry and add it to the cache |
|
791 |
// -- as a HashMap replaces existing entries we |
|
792 |
// don't need to explicitly check if there is |
|
793 |
// already an entry for this host. |
|
794 |
long expiration; |
|
795 |
if (policy == InetAddressCachePolicy.FOREVER) { |
|
796 |
expiration = -1; |
|
797 |
} else { |
|
798 |
expiration = System.currentTimeMillis() + (policy * 1000); |
|
799 |
} |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
800 |
CacheEntry entry = new CacheEntry(addresses, expiration); |
2 | 801 |
cache.put(host, entry); |
802 |
return this; |
|
803 |
} |
|
804 |
||
805 |
/** |
|
806 |
* Query the cache for the specific host. If found then |
|
807 |
* return its CacheEntry, or null if not found. |
|
808 |
*/ |
|
809 |
public CacheEntry get(String host) { |
|
810 |
int policy = getPolicy(); |
|
811 |
if (policy == InetAddressCachePolicy.NEVER) { |
|
812 |
return null; |
|
813 |
} |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
814 |
CacheEntry entry = cache.get(host); |
2 | 815 |
|
816 |
// check if entry has expired |
|
817 |
if (entry != null && policy != InetAddressCachePolicy.FOREVER) { |
|
818 |
if (entry.expiration >= 0 && |
|
819 |
entry.expiration < System.currentTimeMillis()) { |
|
820 |
cache.remove(host); |
|
821 |
entry = null; |
|
822 |
} |
|
823 |
} |
|
824 |
||
825 |
return entry; |
|
826 |
} |
|
827 |
} |
|
828 |
||
829 |
/* |
|
830 |
* Initialize cache and insert anyLocalAddress into the |
|
831 |
* unknown array with no expiry. |
|
832 |
*/ |
|
833 |
private static void cacheInitIfNeeded() { |
|
834 |
assert Thread.holdsLock(addressCache); |
|
835 |
if (addressCacheInit) { |
|
836 |
return; |
|
837 |
} |
|
838 |
unknown_array = new InetAddress[1]; |
|
839 |
unknown_array[0] = impl.anyLocalAddress(); |
|
840 |
||
841 |
addressCache.put(impl.anyLocalAddress().getHostName(), |
|
842 |
unknown_array); |
|
843 |
||
844 |
addressCacheInit = true; |
|
845 |
} |
|
846 |
||
847 |
/* |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
848 |
* Cache the given hostname and addresses. |
2 | 849 |
*/ |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
850 |
private static void cacheAddresses(String hostname, |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
851 |
InetAddress[] addresses, |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
852 |
boolean success) { |
2 | 853 |
hostname = hostname.toLowerCase(); |
854 |
synchronized (addressCache) { |
|
855 |
cacheInitIfNeeded(); |
|
856 |
if (success) { |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
857 |
addressCache.put(hostname, addresses); |
2 | 858 |
} else { |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
859 |
negativeCache.put(hostname, addresses); |
2 | 860 |
} |
861 |
} |
|
862 |
} |
|
863 |
||
864 |
/* |
|
865 |
* Lookup hostname in cache (positive & negative cache). If |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
866 |
* found return addresses, null if not found. |
2 | 867 |
*/ |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
868 |
private static InetAddress[] getCachedAddresses(String hostname) { |
2 | 869 |
hostname = hostname.toLowerCase(); |
870 |
||
871 |
// search both positive & negative caches |
|
872 |
||
873 |
synchronized (addressCache) { |
|
874 |
cacheInitIfNeeded(); |
|
875 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
876 |
CacheEntry entry = addressCache.get(hostname); |
2 | 877 |
if (entry == null) { |
878 |
entry = negativeCache.get(hostname); |
|
879 |
} |
|
880 |
||
881 |
if (entry != null) { |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
882 |
return entry.addresses; |
2 | 883 |
} |
884 |
} |
|
885 |
||
886 |
// not found |
|
887 |
return null; |
|
888 |
} |
|
889 |
||
890 |
private static NameService createNSProvider(String provider) { |
|
891 |
if (provider == null) |
|
892 |
return null; |
|
893 |
||
894 |
NameService nameService = null; |
|
895 |
if (provider.equals("default")) { |
|
896 |
// initialize the default name service |
|
897 |
nameService = new NameService() { |
|
898 |
public InetAddress[] lookupAllHostAddr(String host) |
|
899 |
throws UnknownHostException { |
|
900 |
return impl.lookupAllHostAddr(host); |
|
901 |
} |
|
902 |
public String getHostByAddr(byte[] addr) |
|
903 |
throws UnknownHostException { |
|
904 |
return impl.getHostByAddr(addr); |
|
905 |
} |
|
906 |
}; |
|
907 |
} else { |
|
908 |
final String providerName = provider; |
|
909 |
try { |
|
910 |
nameService = java.security.AccessController.doPrivileged( |
|
911 |
new java.security.PrivilegedExceptionAction<NameService>() { |
|
912 |
public NameService run() { |
|
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
9775
diff
changeset
|
913 |
Iterator<NameServiceDescriptor> itr = |
11119
6ff03c1202ce
7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents:
10882
diff
changeset
|
914 |
ServiceLoader.load(NameServiceDescriptor.class) |
6ff03c1202ce
7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents:
10882
diff
changeset
|
915 |
.iterator(); |
2 | 916 |
while (itr.hasNext()) { |
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
9775
diff
changeset
|
917 |
NameServiceDescriptor nsd = itr.next(); |
2 | 918 |
if (providerName. |
919 |
equalsIgnoreCase(nsd.getType()+"," |
|
920 |
+nsd.getProviderName())) { |
|
921 |
try { |
|
922 |
return nsd.createNameService(); |
|
923 |
} catch (Exception e) { |
|
924 |
e.printStackTrace(); |
|
925 |
System.err.println( |
|
926 |
"Cannot create name service:" |
|
927 |
+providerName+": " + e); |
|
928 |
} |
|
929 |
} |
|
930 |
} |
|
931 |
||
932 |
return null; |
|
933 |
} |
|
934 |
} |
|
935 |
); |
|
936 |
} catch (java.security.PrivilegedActionException e) { |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
return nameService; |
|
941 |
} |
|
942 |
||
943 |
static { |
|
944 |
// create the impl |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
945 |
impl = InetAddressImplFactory.create(); |
2 | 946 |
|
947 |
// get name service if provided and requested |
|
948 |
String provider = null;; |
|
949 |
String propPrefix = "sun.net.spi.nameservice.provider."; |
|
950 |
int n = 1; |
|
951 |
nameServices = new ArrayList<NameService>(); |
|
952 |
provider = AccessController.doPrivileged( |
|
953 |
new GetPropertyAction(propPrefix + n)); |
|
954 |
while (provider != null) { |
|
955 |
NameService ns = createNSProvider(provider); |
|
956 |
if (ns != null) |
|
957 |
nameServices.add(ns); |
|
958 |
||
959 |
n++; |
|
960 |
provider = AccessController.doPrivileged( |
|
961 |
new GetPropertyAction(propPrefix + n)); |
|
962 |
} |
|
963 |
||
964 |
// if not designate any name services provider, |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
965 |
// create a default one |
2 | 966 |
if (nameServices.size() == 0) { |
967 |
NameService ns = createNSProvider("default"); |
|
968 |
nameServices.add(ns); |
|
969 |
} |
|
970 |
} |
|
971 |
||
972 |
/** |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
973 |
* Creates an InetAddress based on the provided host name and IP address. |
2 | 974 |
* No name service is checked for the validity of the address. |
975 |
* |
|
976 |
* <p> The host name can either be a machine name, such as |
|
19069 | 977 |
* "{@code java.sun.com}", or a textual representation of its IP |
2 | 978 |
* address. |
979 |
* <p> No validity checking is done on the host name either. |
|
980 |
* |
|
981 |
* <p> If addr specifies an IPv4 address an instance of Inet4Address |
|
982 |
* will be returned; otherwise, an instance of Inet6Address |
|
983 |
* will be returned. |
|
984 |
* |
|
985 |
* <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array |
|
986 |
* must be 16 bytes long |
|
987 |
* |
|
988 |
* @param host the specified host |
|
989 |
* @param addr the raw IP address in network byte order |
|
990 |
* @return an InetAddress object created from the raw IP address. |
|
991 |
* @exception UnknownHostException if IP address is of illegal length |
|
992 |
* @since 1.4 |
|
993 |
*/ |
|
994 |
public static InetAddress getByAddress(String host, byte[] addr) |
|
995 |
throws UnknownHostException { |
|
996 |
if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
|
997 |
if (host.charAt(host.length()-1) == ']') { |
|
998 |
host = host.substring(1, host.length() -1); |
|
999 |
} |
|
1000 |
} |
|
1001 |
if (addr != null) { |
|
1002 |
if (addr.length == Inet4Address.INADDRSZ) { |
|
1003 |
return new Inet4Address(host, addr); |
|
1004 |
} else if (addr.length == Inet6Address.INADDRSZ) { |
|
1005 |
byte[] newAddr |
|
1006 |
= IPAddressUtil.convertFromIPv4MappedAddress(addr); |
|
1007 |
if (newAddr != null) { |
|
1008 |
return new Inet4Address(host, newAddr); |
|
1009 |
} else { |
|
1010 |
return new Inet6Address(host, addr); |
|
1011 |
} |
|
1012 |
} |
|
1013 |
} |
|
1014 |
throw new UnknownHostException("addr is of illegal length"); |
|
1015 |
} |
|
1016 |
||
1017 |
||
1018 |
/** |
|
1019 |
* Determines the IP address of a host, given the host's name. |
|
1020 |
* |
|
1021 |
* <p> The host name can either be a machine name, such as |
|
19069 | 1022 |
* "{@code java.sun.com}", or a textual representation of its |
2 | 1023 |
* IP address. If a literal IP address is supplied, only the |
1024 |
* validity of the address format is checked. |
|
1025 |
* |
|
19069 | 1026 |
* <p> For {@code host} specified in literal IPv6 address, |
2 | 1027 |
* either the form defined in RFC 2732 or the literal IPv6 address |
1028 |
* format defined in RFC 2373 is accepted. IPv6 scoped addresses are also |
|
1029 |
* supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
|
1030 |
* scoped addresses. |
|
1031 |
* |
|
19069 | 1032 |
* <p> If the host is {@code null} then an {@code InetAddress} |
2 | 1033 |
* representing an address of the loopback interface is returned. |
1034 |
* See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC 3330</a> |
|
1035 |
* section 2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC 2373</a> |
|
1036 |
* section 2.5.3. </p> |
|
1037 |
* |
|
19069 | 1038 |
* @param host the specified host, or {@code null}. |
2 | 1039 |
* @return an IP address for the given host name. |
1040 |
* @exception UnknownHostException if no IP address for the |
|
19069 | 1041 |
* {@code host} could be found, or if a scope_id was specified |
2 | 1042 |
* for a global IPv6 address. |
1043 |
* @exception SecurityException if a security manager exists |
|
1044 |
* and its checkConnect method doesn't allow the operation |
|
1045 |
*/ |
|
1046 |
public static InetAddress getByName(String host) |
|
1047 |
throws UnknownHostException { |
|
1048 |
return InetAddress.getAllByName(host)[0]; |
|
1049 |
} |
|
1050 |
||
9775 | 1051 |
// called from deployment cache manager |
1052 |
private static InetAddress getByName(String host, InetAddress reqAddr) |
|
1053 |
throws UnknownHostException { |
|
1054 |
return InetAddress.getAllByName(host, reqAddr)[0]; |
|
1055 |
} |
|
1056 |
||
2 | 1057 |
/** |
1058 |
* Given the name of a host, returns an array of its IP addresses, |
|
1059 |
* based on the configured name service on the system. |
|
1060 |
* |
|
1061 |
* <p> The host name can either be a machine name, such as |
|
19069 | 1062 |
* "{@code java.sun.com}", or a textual representation of its IP |
2 | 1063 |
* address. If a literal IP address is supplied, only the |
1064 |
* validity of the address format is checked. |
|
1065 |
* |
|
19069 | 1066 |
* <p> For {@code host} specified in <i>literal IPv6 address</i>, |
2 | 1067 |
* either the form defined in RFC 2732 or the literal IPv6 address |
1068 |
* format defined in RFC 2373 is accepted. A literal IPv6 address may |
|
1069 |
* also be qualified by appending a scoped zone identifier or scope_id. |
|
1070 |
* The syntax and usage of scope_ids is described |
|
1071 |
* <a href="Inet6Address.html#scoped">here</a>. |
|
19069 | 1072 |
* <p> If the host is {@code null} then an {@code InetAddress} |
2 | 1073 |
* representing an address of the loopback interface is returned. |
1074 |
* See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC 3330</a> |
|
1075 |
* section 2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC 2373</a> |
|
1076 |
* section 2.5.3. </p> |
|
1077 |
* |
|
19069 | 1078 |
* <p> If there is a security manager and {@code host} is not |
1079 |
* null and {@code host.length() } is not equal to zero, the |
|
2 | 1080 |
* security manager's |
19069 | 1081 |
* {@code checkConnect} method is called |
1082 |
* with the hostname and {@code -1} |
|
2 | 1083 |
* as its arguments to see if the operation is allowed. |
1084 |
* |
|
19069 | 1085 |
* @param host the name of the host, or {@code null}. |
2 | 1086 |
* @return an array of all the IP addresses for a given host name. |
1087 |
* |
|
1088 |
* @exception UnknownHostException if no IP address for the |
|
19069 | 1089 |
* {@code host} could be found, or if a scope_id was specified |
2 | 1090 |
* for a global IPv6 address. |
1091 |
* @exception SecurityException if a security manager exists and its |
|
19069 | 1092 |
* {@code checkConnect} method doesn't allow the operation. |
2 | 1093 |
* |
1094 |
* @see SecurityManager#checkConnect |
|
1095 |
*/ |
|
1096 |
public static InetAddress[] getAllByName(String host) |
|
1097 |
throws UnknownHostException { |
|
9775 | 1098 |
return getAllByName(host, null); |
1099 |
} |
|
1100 |
||
1101 |
private static InetAddress[] getAllByName(String host, InetAddress reqAddr) |
|
1102 |
throws UnknownHostException { |
|
2 | 1103 |
|
1104 |
if (host == null || host.length() == 0) { |
|
1105 |
InetAddress[] ret = new InetAddress[1]; |
|
1106 |
ret[0] = impl.loopbackAddress(); |
|
1107 |
return ret; |
|
1108 |
} |
|
1109 |
||
1110 |
boolean ipv6Expected = false; |
|
1111 |
if (host.charAt(0) == '[') { |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1112 |
// This is supposed to be an IPv6 literal |
2 | 1113 |
if (host.length() > 2 && host.charAt(host.length()-1) == ']') { |
1114 |
host = host.substring(1, host.length() -1); |
|
1115 |
ipv6Expected = true; |
|
1116 |
} else { |
|
1117 |
// This was supposed to be a IPv6 address, but it's not! |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1118 |
throw new UnknownHostException(host + ": invalid IPv6 address"); |
2 | 1119 |
} |
1120 |
} |
|
1121 |
||
1122 |
// if host is an IP address, we won't do further lookup |
|
1123 |
if (Character.digit(host.charAt(0), 16) != -1 |
|
1124 |
|| (host.charAt(0) == ':')) { |
|
1125 |
byte[] addr = null; |
|
1126 |
int numericZone = -1; |
|
1127 |
String ifname = null; |
|
1128 |
// see if it is IPv4 address |
|
1129 |
addr = IPAddressUtil.textToNumericFormatV4(host); |
|
1130 |
if (addr == null) { |
|
1131 |
// see if it is IPv6 address |
|
1132 |
// Check if a numeric or string zone id is present |
|
1133 |
int pos; |
|
1134 |
if ((pos=host.indexOf ("%")) != -1) { |
|
1135 |
numericZone = checkNumericZone (host); |
|
1136 |
if (numericZone == -1) { /* remainder of string must be an ifname */ |
|
1137 |
ifname = host.substring (pos+1); |
|
1138 |
} |
|
1139 |
} |
|
1140 |
addr = IPAddressUtil.textToNumericFormatV6(host); |
|
1141 |
} else if (ipv6Expected) { |
|
1142 |
// Means an IPv4 litteral between brackets! |
|
1143 |
throw new UnknownHostException("["+host+"]"); |
|
1144 |
} |
|
1145 |
InetAddress[] ret = new InetAddress[1]; |
|
1146 |
if(addr != null) { |
|
1147 |
if (addr.length == Inet4Address.INADDRSZ) { |
|
1148 |
ret[0] = new Inet4Address(null, addr); |
|
1149 |
} else { |
|
1150 |
if (ifname != null) { |
|
1151 |
ret[0] = new Inet6Address(null, addr, ifname); |
|
1152 |
} else { |
|
1153 |
ret[0] = new Inet6Address(null, addr, numericZone); |
|
1154 |
} |
|
1155 |
} |
|
1156 |
return ret; |
|
1157 |
} |
|
1158 |
} else if (ipv6Expected) { |
|
1159 |
// We were expecting an IPv6 Litteral, but got something else |
|
1160 |
throw new UnknownHostException("["+host+"]"); |
|
1161 |
} |
|
9775 | 1162 |
return getAllByName0(host, reqAddr, true); |
2 | 1163 |
} |
1164 |
||
1165 |
/** |
|
1166 |
* Returns the loopback address. |
|
1167 |
* <p> |
|
1168 |
* The InetAddress returned will represent the IPv4 |
|
1169 |
* loopback address, 127.0.0.1, or the IPv6 loopback |
|
1170 |
* address, ::1. The IPv4 loopback address returned |
|
1171 |
* is only one of many in the form 127.*.*.* |
|
1172 |
* |
|
1173 |
* @return the InetAddress loopback instance. |
|
1174 |
* @since 1.7 |
|
1175 |
*/ |
|
1176 |
public static InetAddress getLoopbackAddress() { |
|
1177 |
return impl.loopbackAddress(); |
|
1178 |
} |
|
1179 |
||
1180 |
||
1181 |
/** |
|
1182 |
* check if the literal address string has %nn appended |
|
1183 |
* returns -1 if not, or the numeric value otherwise. |
|
1184 |
* |
|
1185 |
* %nn may also be a string that represents the displayName of |
|
1186 |
* a currently available NetworkInterface. |
|
1187 |
*/ |
|
1188 |
private static int checkNumericZone (String s) throws UnknownHostException { |
|
1189 |
int percent = s.indexOf ('%'); |
|
1190 |
int slen = s.length(); |
|
1191 |
int digit, zone=0; |
|
1192 |
if (percent == -1) { |
|
1193 |
return -1; |
|
1194 |
} |
|
1195 |
for (int i=percent+1; i<slen; i++) { |
|
1196 |
char c = s.charAt(i); |
|
1197 |
if (c == ']') { |
|
1198 |
if (i == percent+1) { |
|
1199 |
/* empty per-cent field */ |
|
1200 |
return -1; |
|
1201 |
} |
|
1202 |
break; |
|
1203 |
} |
|
1204 |
if ((digit = Character.digit (c, 10)) < 0) { |
|
1205 |
return -1; |
|
1206 |
} |
|
1207 |
zone = (zone * 10) + digit; |
|
1208 |
} |
|
1209 |
return zone; |
|
1210 |
} |
|
1211 |
||
1212 |
private static InetAddress[] getAllByName0 (String host) |
|
1213 |
throws UnknownHostException |
|
1214 |
{ |
|
1215 |
return getAllByName0(host, true); |
|
1216 |
} |
|
1217 |
||
1218 |
/** |
|
1219 |
* package private so SocketPermission can call it |
|
1220 |
*/ |
|
1221 |
static InetAddress[] getAllByName0 (String host, boolean check) |
|
1222 |
throws UnknownHostException { |
|
9775 | 1223 |
return getAllByName0 (host, null, check); |
1224 |
} |
|
1225 |
||
1226 |
private static InetAddress[] getAllByName0 (String host, InetAddress reqAddr, boolean check) |
|
1227 |
throws UnknownHostException { |
|
1228 |
||
2 | 1229 |
/* If it gets here it is presumed to be a hostname */ |
1230 |
/* Cache.get can return: null, unknownAddress, or InetAddress[] */ |
|
1231 |
||
1232 |
/* make sure the connection to the host is allowed, before we |
|
1233 |
* give out a hostname |
|
1234 |
*/ |
|
1235 |
if (check) { |
|
1236 |
SecurityManager security = System.getSecurityManager(); |
|
1237 |
if (security != null) { |
|
1238 |
security.checkConnect(host, -1); |
|
1239 |
} |
|
1240 |
} |
|
1241 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1242 |
InetAddress[] addresses = getCachedAddresses(host); |
2 | 1243 |
|
1244 |
/* If no entry in cache, then do the host lookup */ |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1245 |
if (addresses == null) { |
9775 | 1246 |
addresses = getAddressesFromNameService(host, reqAddr); |
2 | 1247 |
} |
1248 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1249 |
if (addresses == unknown_array) |
2 | 1250 |
throw new UnknownHostException(host); |
1251 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1252 |
return addresses.clone(); |
2 | 1253 |
} |
1254 |
||
9775 | 1255 |
private static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr) |
2 | 1256 |
throws UnknownHostException |
1257 |
{ |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1258 |
InetAddress[] addresses = null; |
2 | 1259 |
boolean success = false; |
1260 |
UnknownHostException ex = null; |
|
1261 |
||
1262 |
// Check whether the host is in the lookupTable. |
|
1263 |
// 1) If the host isn't in the lookupTable when |
|
1264 |
// checkLookupTable() is called, checkLookupTable() |
|
1265 |
// would add the host in the lookupTable and |
|
1266 |
// return null. So we will do the lookup. |
|
1267 |
// 2) If the host is in the lookupTable when |
|
1268 |
// checkLookupTable() is called, the current thread |
|
1269 |
// would be blocked until the host is removed |
|
1270 |
// from the lookupTable. Then this thread |
|
1271 |
// should try to look up the addressCache. |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1272 |
// i) if it found the addresses in the |
2 | 1273 |
// addressCache, checkLookupTable() would |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1274 |
// return the addresses. |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1275 |
// ii) if it didn't find the addresses in the |
2 | 1276 |
// addressCache for any reason, |
1277 |
// it should add the host in the |
|
1278 |
// lookupTable and return null so the |
|
1279 |
// following code would do a lookup itself. |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1280 |
if ((addresses = checkLookupTable(host)) == null) { |
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1281 |
try { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1282 |
// This is the first thread which looks up the addresses |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1283 |
// this host or the cache entry for this host has been |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1284 |
// expired so this thread should do the lookup. |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1285 |
for (NameService nameService : nameServices) { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1286 |
try { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1287 |
/* |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1288 |
* Do not put the call to lookup() inside the |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1289 |
* constructor. if you do you will still be |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1290 |
* allocating space when the lookup fails. |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1291 |
*/ |
2 | 1292 |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1293 |
addresses = nameService.lookupAllHostAddr(host); |
2 | 1294 |
success = true; |
1295 |
break; |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1296 |
} catch (UnknownHostException uhe) { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1297 |
if (host.equalsIgnoreCase("localhost")) { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1298 |
InetAddress[] local = new InetAddress[] { impl.loopbackAddress() }; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1299 |
addresses = local; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1300 |
success = true; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1301 |
break; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1302 |
} |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1303 |
else { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1304 |
addresses = unknown_array; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1305 |
success = false; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1306 |
ex = uhe; |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1307 |
} |
2 | 1308 |
} |
1309 |
} |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1310 |
|
9775 | 1311 |
// More to do? |
1312 |
if (reqAddr != null && addresses.length > 1 && !addresses[0].equals(reqAddr)) { |
|
1313 |
// Find it? |
|
1314 |
int i = 1; |
|
1315 |
for (; i < addresses.length; i++) { |
|
1316 |
if (addresses[i].equals(reqAddr)) { |
|
1317 |
break; |
|
1318 |
} |
|
1319 |
} |
|
1320 |
// Rotate |
|
1321 |
if (i < addresses.length) { |
|
1322 |
InetAddress tmp, tmp2 = reqAddr; |
|
1323 |
for (int j = 0; j < i; j++) { |
|
1324 |
tmp = addresses[j]; |
|
1325 |
addresses[j] = tmp2; |
|
1326 |
tmp2 = tmp; |
|
1327 |
} |
|
1328 |
addresses[i] = tmp2; |
|
1329 |
} |
|
1330 |
} |
|
1331 |
// Cache the address. |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1332 |
cacheAddresses(host, addresses, success); |
9775 | 1333 |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1334 |
if (!success && ex != null) |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1335 |
throw ex; |
9775 | 1336 |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1337 |
} finally { |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1338 |
// Delete host from the lookupTable and notify |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1339 |
// all threads waiting on the lookupTable monitor. |
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1340 |
updateLookupTable(host); |
2 | 1341 |
} |
1342 |
} |
|
1343 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1344 |
return addresses; |
2 | 1345 |
} |
1346 |
||
1347 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1348 |
private static InetAddress[] checkLookupTable(String host) { |
2 | 1349 |
synchronized (lookupTable) { |
1350 |
// If the host isn't in the lookupTable, add it in the |
|
1351 |
// lookuptable and return null. The caller should do |
|
1352 |
// the lookup. |
|
1353 |
if (lookupTable.containsKey(host) == false) { |
|
1354 |
lookupTable.put(host, null); |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1355 |
return null; |
2 | 1356 |
} |
1357 |
||
1358 |
// If the host is in the lookupTable, it means that another |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1359 |
// thread is trying to look up the addresses of this host. |
2 | 1360 |
// This thread should wait. |
1361 |
while (lookupTable.containsKey(host)) { |
|
1362 |
try { |
|
1363 |
lookupTable.wait(); |
|
1364 |
} catch (InterruptedException e) { |
|
1365 |
} |
|
1366 |
} |
|
1367 |
} |
|
1368 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1369 |
// The other thread has finished looking up the addresses of |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1370 |
// the host. This thread should retry to get the addresses |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1371 |
// from the addressCache. If it doesn't get the addresses from |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1372 |
// the cache, it will try to look up the addresses itself. |
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1373 |
InetAddress[] addresses = getCachedAddresses(host); |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1374 |
if (addresses == null) { |
2 | 1375 |
synchronized (lookupTable) { |
1376 |
lookupTable.put(host, null); |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
7668
diff
changeset
|
1377 |
return null; |
2 | 1378 |
} |
1379 |
} |
|
1380 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1381 |
return addresses; |
2 | 1382 |
} |
1383 |
||
1384 |
private static void updateLookupTable(String host) { |
|
1385 |
synchronized (lookupTable) { |
|
1386 |
lookupTable.remove(host); |
|
1387 |
lookupTable.notifyAll(); |
|
1388 |
} |
|
1389 |
} |
|
1390 |
||
1391 |
/** |
|
19069 | 1392 |
* Returns an {@code InetAddress} object given the raw IP address . |
2 | 1393 |
* The argument is in network byte order: the highest order |
19069 | 1394 |
* byte of the address is in {@code getAddress()[0]}. |
2 | 1395 |
* |
1396 |
* <p> This method doesn't block, i.e. no reverse name service lookup |
|
1397 |
* is performed. |
|
1398 |
* |
|
1399 |
* <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array |
|
1400 |
* must be 16 bytes long |
|
1401 |
* |
|
1402 |
* @param addr the raw IP address in network byte order |
|
1403 |
* @return an InetAddress object created from the raw IP address. |
|
1404 |
* @exception UnknownHostException if IP address is of illegal length |
|
1405 |
* @since 1.4 |
|
1406 |
*/ |
|
1407 |
public static InetAddress getByAddress(byte[] addr) |
|
1408 |
throws UnknownHostException { |
|
1409 |
return getByAddress(null, addr); |
|
1410 |
} |
|
1411 |
||
1412 |
private static InetAddress cachedLocalHost = null; |
|
1413 |
private static long cacheTime = 0; |
|
1414 |
private static final long maxCacheTime = 5000L; |
|
1415 |
private static final Object cacheLock = new Object(); |
|
1416 |
||
1417 |
/** |
|
1418 |
* Returns the address of the local host. This is achieved by retrieving |
|
1419 |
* the name of the host from the system, then resolving that name into |
|
19069 | 1420 |
* an {@code InetAddress}. |
2 | 1421 |
* |
1422 |
* <P>Note: The resolved address may be cached for a short period of time. |
|
1423 |
* </P> |
|
1424 |
* |
|
1425 |
* <p>If there is a security manager, its |
|
19069 | 1426 |
* {@code checkConnect} method is called |
1427 |
* with the local host name and {@code -1} |
|
2 | 1428 |
* as its arguments to see if the operation is allowed. |
1429 |
* If the operation is not allowed, an InetAddress representing |
|
1430 |
* the loopback address is returned. |
|
1431 |
* |
|
1432 |
* @return the address of the local host. |
|
1433 |
* |
|
1434 |
* @exception UnknownHostException if the local host name could not |
|
1435 |
* be resolved into an address. |
|
1436 |
* |
|
1437 |
* @see SecurityManager#checkConnect |
|
1438 |
* @see java.net.InetAddress#getByName(java.lang.String) |
|
1439 |
*/ |
|
1440 |
public static InetAddress getLocalHost() throws UnknownHostException { |
|
1441 |
||
1442 |
SecurityManager security = System.getSecurityManager(); |
|
1443 |
try { |
|
1444 |
String local = impl.getLocalHostName(); |
|
1445 |
||
1446 |
if (security != null) { |
|
1447 |
security.checkConnect(local, -1); |
|
1448 |
} |
|
1449 |
||
1450 |
if (local.equals("localhost")) { |
|
1451 |
return impl.loopbackAddress(); |
|
1452 |
} |
|
1453 |
||
1454 |
InetAddress ret = null; |
|
1455 |
synchronized (cacheLock) { |
|
1456 |
long now = System.currentTimeMillis(); |
|
1457 |
if (cachedLocalHost != null) { |
|
1458 |
if ((now - cacheTime) < maxCacheTime) // Less than 5s old? |
|
1459 |
ret = cachedLocalHost; |
|
1460 |
else |
|
1461 |
cachedLocalHost = null; |
|
1462 |
} |
|
1463 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1464 |
// we are calling getAddressesFromNameService directly |
2 | 1465 |
// to avoid getting localHost from cache |
1466 |
if (ret == null) { |
|
1467 |
InetAddress[] localAddrs; |
|
1468 |
try { |
|
1469 |
localAddrs = |
|
9775 | 1470 |
InetAddress.getAddressesFromNameService(local, null); |
2 | 1471 |
} catch (UnknownHostException uhe) { |
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1472 |
// Rethrow with a more informative error message. |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1473 |
UnknownHostException uhe2 = |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1474 |
new UnknownHostException(local + ": " + |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1475 |
uhe.getMessage()); |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1476 |
uhe2.initCause(uhe); |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1477 |
throw uhe2; |
2 | 1478 |
} |
1479 |
cachedLocalHost = localAddrs[0]; |
|
1480 |
cacheTime = now; |
|
1481 |
ret = localAddrs[0]; |
|
1482 |
} |
|
1483 |
} |
|
1484 |
return ret; |
|
1485 |
} catch (java.lang.SecurityException e) { |
|
1486 |
return impl.loopbackAddress(); |
|
1487 |
} |
|
1488 |
} |
|
1489 |
||
1490 |
/** |
|
1491 |
* Perform class load-time initializations. |
|
1492 |
*/ |
|
1493 |
private static native void init(); |
|
1494 |
||
1495 |
||
1496 |
/* |
|
1497 |
* Returns the InetAddress representing anyLocalAddress |
|
1498 |
* (typically 0.0.0.0 or ::0) |
|
1499 |
*/ |
|
1500 |
static InetAddress anyLocalAddress() { |
|
1501 |
return impl.anyLocalAddress(); |
|
1502 |
} |
|
1503 |
||
1504 |
/* |
|
1505 |
* Load and instantiate an underlying impl class |
|
1506 |
*/ |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1507 |
static InetAddressImpl loadImpl(String implName) { |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1508 |
Object impl = null; |
2 | 1509 |
|
1510 |
/* |
|
1511 |
* Property "impl.prefix" will be prepended to the classname |
|
1512 |
* of the implementation object we instantiate, to which we |
|
1513 |
* delegate the real work (like native methods). This |
|
1514 |
* property can vary across implementations of the java. |
|
1515 |
* classes. The default is an empty String "". |
|
1516 |
*/ |
|
1517 |
String prefix = AccessController.doPrivileged( |
|
1518 |
new GetPropertyAction("impl.prefix", "")); |
|
1519 |
try { |
|
1520 |
impl = Class.forName("java.net." + prefix + implName).newInstance(); |
|
1521 |
} catch (ClassNotFoundException e) { |
|
1522 |
System.err.println("Class not found: java.net." + prefix + |
|
1523 |
implName + ":\ncheck impl.prefix property " + |
|
1524 |
"in your properties file."); |
|
1525 |
} catch (InstantiationException e) { |
|
1526 |
System.err.println("Could not instantiate: java.net." + prefix + |
|
1527 |
implName + ":\ncheck impl.prefix property " + |
|
1528 |
"in your properties file."); |
|
1529 |
} catch (IllegalAccessException e) { |
|
1530 |
System.err.println("Cannot access class: java.net." + prefix + |
|
1531 |
implName + ":\ncheck impl.prefix property " + |
|
1532 |
"in your properties file."); |
|
1533 |
} |
|
1534 |
||
1535 |
if (impl == null) { |
|
1536 |
try { |
|
1537 |
impl = Class.forName(implName).newInstance(); |
|
1538 |
} catch (Exception e) { |
|
1539 |
throw new Error("System property impl.prefix incorrect"); |
|
1540 |
} |
|
1541 |
} |
|
1542 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1543 |
return (InetAddressImpl) impl; |
2 | 1544 |
} |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1545 |
|
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1546 |
private void readObjectNoData (ObjectInputStream s) throws |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1547 |
IOException, ClassNotFoundException { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1548 |
if (getClass().getClassLoader() != null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1549 |
throw new SecurityException ("invalid address type"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1550 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1551 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1552 |
|
16870 | 1553 |
private static final long FIELDS_OFFSET; |
1554 |
private static final sun.misc.Unsafe UNSAFE; |
|
1555 |
||
1556 |
static { |
|
1557 |
try { |
|
1558 |
sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe(); |
|
1559 |
FIELDS_OFFSET = unsafe.objectFieldOffset( |
|
1560 |
InetAddress.class.getDeclaredField("holder") |
|
1561 |
); |
|
1562 |
UNSAFE = unsafe; |
|
1563 |
} catch (ReflectiveOperationException e) { |
|
1564 |
throw new Error(e); |
|
1565 |
} |
|
1566 |
} |
|
1567 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1568 |
private void readObject (ObjectInputStream s) throws |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1569 |
IOException, ClassNotFoundException { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1570 |
if (getClass().getClassLoader() != null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1571 |
throw new SecurityException ("invalid address type"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1572 |
} |
16870 | 1573 |
GetField gf = s.readFields(); |
1574 |
String host = (String)gf.get("hostName", null); |
|
1575 |
int address= gf.get("address", 0); |
|
1576 |
int family= gf.get("family", 0); |
|
1577 |
InetAddressHolder h = new InetAddressHolder(host, address, family); |
|
1578 |
UNSAFE.putObject(this, FIELDS_OFFSET, h); |
|
1579 |
} |
|
1580 |
||
1581 |
/* needed because the serializable fields no longer exist */ |
|
1582 |
||
1583 |
/** |
|
1584 |
* @serialField hostName String |
|
1585 |
* @serialField address int |
|
1586 |
* @serialField family int |
|
1587 |
*/ |
|
1588 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
1589 |
new ObjectStreamField("hostName", String.class), |
|
1590 |
new ObjectStreamField("address", int.class), |
|
1591 |
new ObjectStreamField("family", int.class), |
|
1592 |
}; |
|
1593 |
||
1594 |
private void writeObject (ObjectOutputStream s) throws |
|
1595 |
IOException { |
|
1596 |
if (getClass().getClassLoader() != null) { |
|
1597 |
throw new SecurityException ("invalid address type"); |
|
1598 |
} |
|
1599 |
PutField pf = s.putFields(); |
|
1600 |
pf.put("hostName", holder().getHostName()); |
|
1601 |
pf.put("address", holder().getAddress()); |
|
1602 |
pf.put("family", holder().getFamily()); |
|
1603 |
s.writeFields(); |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
1604 |
} |
2 | 1605 |
} |
1606 |
||
1607 |
/* |
|
1608 |
* Simple factory to create the impl |
|
1609 |
*/ |
|
1610 |
class InetAddressImplFactory { |
|
1611 |
||
1612 |
static InetAddressImpl create() { |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1613 |
return InetAddress.loadImpl(isIPv6Supported() ? |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
1614 |
"Inet6AddressImpl" : "Inet4AddressImpl"); |
2 | 1615 |
} |
1616 |
||
1617 |
static native boolean isIPv6Supported(); |
|
1618 |
} |