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