author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 25859 | 3317bb8137f4 |
child 33301 | 9ee33abf5b4c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18800 | 2 |
* Copyright (c) 2000, 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.io.ObjectStreamException; |
|
29 |
||
30 |
/** |
|
31 |
* This class represents an Internet Protocol version 4 (IPv4) address. |
|
32 |
* Defined by <a href="http://www.ietf.org/rfc/rfc790.txt"> |
|
33 |
* <i>RFC 790: Assigned Numbers</i></a>, |
|
34 |
* <a href="http://www.ietf.org/rfc/rfc1918.txt"> |
|
35 |
* <i>RFC 1918: Address Allocation for Private Internets</i></a>, |
|
36 |
* and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC 2365: |
|
37 |
* Administratively Scoped IP Multicast</i></a> |
|
38 |
* |
|
18800 | 39 |
* <h3> <A NAME="format">Textual representation of IP addresses</a> </h3> |
2 | 40 |
* |
41 |
* Textual representation of IPv4 address used as input to methods |
|
42 |
* takes one of the following forms: |
|
43 |
* |
|
44 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
19069 | 45 |
* <tr><td>{@code d.d.d.d}</td></tr> |
46 |
* <tr><td>{@code d.d.d}</td></tr> |
|
47 |
* <tr><td>{@code d.d}</td></tr> |
|
48 |
* <tr><td>{@code d}</td></tr> |
|
2 | 49 |
* </table></blockquote> |
50 |
* |
|
51 |
* <p> When four parts are specified, each is interpreted as a byte of |
|
52 |
* data and assigned, from left to right, to the four bytes of an IPv4 |
|
53 |
* address. |
|
54 |
||
55 |
* <p> When a three part address is specified, the last part is |
|
56 |
* interpreted as a 16-bit quantity and placed in the right most two |
|
57 |
* bytes of the network address. This makes the three part address |
|
58 |
* format convenient for specifying Class B net- work addresses as |
|
59 |
* 128.net.host. |
|
60 |
* |
|
61 |
* <p> When a two part address is supplied, the last part is |
|
62 |
* interpreted as a 24-bit quantity and placed in the right most three |
|
63 |
* bytes of the network address. This makes the two part address |
|
64 |
* format convenient for specifying Class A network addresses as |
|
65 |
* net.host. |
|
66 |
* |
|
67 |
* <p> When only one part is given, the value is stored directly in |
|
68 |
* the network address without any byte rearrangement. |
|
69 |
* |
|
70 |
* <p> For methods that return a textual representation as output |
|
71 |
* value, the first form, i.e. a dotted-quad string, is used. |
|
72 |
* |
|
73 |
* <h4> The Scope of a Multicast Address </h4> |
|
74 |
* |
|
75 |
* Historically the IPv4 TTL field in the IP header has doubled as a |
|
76 |
* multicast scope field: a TTL of 0 means node-local, 1 means |
|
77 |
* link-local, up through 32 means site-local, up through 64 means |
|
78 |
* region-local, up through 128 means continent-local, and up through |
|
79 |
* 255 are global. However, the administrative scoping is preferred. |
|
80 |
* Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt"> |
|
81 |
* <i>RFC 2365: Administratively Scoped IP Multicast</i></a> |
|
82 |
* @since 1.4 |
|
83 |
*/ |
|
84 |
||
85 |
public final |
|
86 |
class Inet4Address extends InetAddress { |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
87 |
static final int INADDRSZ = 4; |
2 | 88 |
|
89 |
/** use serialVersionUID from InetAddress, but Inet4Address instance |
|
90 |
* is always replaced by an InetAddress instance before being |
|
91 |
* serialized */ |
|
92 |
private static final long serialVersionUID = 3286316764910316507L; |
|
93 |
||
94 |
/* |
|
95 |
* Perform initializations. |
|
96 |
*/ |
|
97 |
static { |
|
98 |
init(); |
|
99 |
} |
|
100 |
||
101 |
Inet4Address() { |
|
102 |
super(); |
|
16870 | 103 |
holder().hostName = null; |
104 |
holder().address = 0; |
|
105 |
holder().family = IPv4; |
|
2 | 106 |
} |
107 |
||
108 |
Inet4Address(String hostName, byte addr[]) { |
|
16870 | 109 |
holder().hostName = hostName; |
110 |
holder().family = IPv4; |
|
2 | 111 |
if (addr != null) { |
112 |
if (addr.length == INADDRSZ) { |
|
16870 | 113 |
int address = addr[3] & 0xFF; |
2 | 114 |
address |= ((addr[2] << 8) & 0xFF00); |
115 |
address |= ((addr[1] << 16) & 0xFF0000); |
|
116 |
address |= ((addr[0] << 24) & 0xFF000000); |
|
16870 | 117 |
holder().address = address; |
2 | 118 |
} |
119 |
} |
|
120 |
} |
|
121 |
Inet4Address(String hostName, int address) { |
|
16870 | 122 |
holder().hostName = hostName; |
123 |
holder().family = IPv4; |
|
124 |
holder().address = address; |
|
2 | 125 |
} |
126 |
||
127 |
/** |
|
128 |
* Replaces the object to be serialized with an InetAddress object. |
|
129 |
* |
|
130 |
* @return the alternate object to be serialized. |
|
131 |
* |
|
132 |
* @throws ObjectStreamException if a new object replacing this |
|
133 |
* object could not be created |
|
134 |
*/ |
|
135 |
private Object writeReplace() throws ObjectStreamException { |
|
136 |
// will replace the to be serialized 'this' object |
|
137 |
InetAddress inet = new InetAddress(); |
|
16870 | 138 |
inet.holder().hostName = holder().getHostName(); |
139 |
inet.holder().address = holder().getAddress(); |
|
2 | 140 |
|
141 |
/** |
|
142 |
* Prior to 1.4 an InetAddress was created with a family |
|
143 |
* based on the platform AF_INET value (usually 2). |
|
144 |
* For compatibility reasons we must therefore write the |
|
145 |
* the InetAddress with this family. |
|
146 |
*/ |
|
16870 | 147 |
inet.holder().family = 2; |
2 | 148 |
|
149 |
return inet; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Utility routine to check if the InetAddress is an |
|
154 |
* IP multicast address. IP multicast address is a Class D |
|
155 |
* address i.e first four bits of the address are 1110. |
|
19069 | 156 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 157 |
* an IP multicast address |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
19069
diff
changeset
|
158 |
* @since 1.1 |
2 | 159 |
*/ |
160 |
public boolean isMulticastAddress() { |
|
16870 | 161 |
return ((holder().getAddress() & 0xf0000000) == 0xe0000000); |
2 | 162 |
} |
163 |
||
164 |
/** |
|
165 |
* Utility routine to check if the InetAddress in a wildcard address. |
|
19069 | 166 |
* @return a {@code boolean} indicating if the Inetaddress is |
2 | 167 |
* a wildcard address. |
168 |
* @since 1.4 |
|
169 |
*/ |
|
170 |
public boolean isAnyLocalAddress() { |
|
16870 | 171 |
return holder().getAddress() == 0; |
2 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* Utility routine to check if the InetAddress is a loopback address. |
|
176 |
* |
|
19069 | 177 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 178 |
* a loopback address; or false otherwise. |
179 |
* @since 1.4 |
|
180 |
*/ |
|
181 |
public boolean isLoopbackAddress() { |
|
182 |
/* 127.x.x.x */ |
|
183 |
byte[] byteAddr = getAddress(); |
|
184 |
return byteAddr[0] == 127; |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Utility routine to check if the InetAddress is an link local address. |
|
189 |
* |
|
19069 | 190 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 191 |
* a link local address; or false if address is not a link local unicast address. |
192 |
* @since 1.4 |
|
193 |
*/ |
|
194 |
public boolean isLinkLocalAddress() { |
|
195 |
// link-local unicast in IPv4 (169.254.0.0/16) |
|
196 |
// defined in "Documenting Special Use IPv4 Address Blocks |
|
197 |
// that have been Registered with IANA" by Bill Manning |
|
198 |
// draft-manning-dsua-06.txt |
|
16870 | 199 |
int address = holder().getAddress(); |
2 | 200 |
return (((address >>> 24) & 0xFF) == 169) |
201 |
&& (((address >>> 16) & 0xFF) == 254); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Utility routine to check if the InetAddress is a site local address. |
|
206 |
* |
|
19069 | 207 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 208 |
* a site local address; or false if address is not a site local unicast address. |
209 |
* @since 1.4 |
|
210 |
*/ |
|
211 |
public boolean isSiteLocalAddress() { |
|
212 |
// refer to RFC 1918 |
|
213 |
// 10/8 prefix |
|
214 |
// 172.16/12 prefix |
|
215 |
// 192.168/16 prefix |
|
16870 | 216 |
int address = holder().getAddress(); |
2 | 217 |
return (((address >>> 24) & 0xFF) == 10) |
218 |
|| ((((address >>> 24) & 0xFF) == 172) |
|
219 |
&& (((address >>> 16) & 0xF0) == 16)) |
|
220 |
|| ((((address >>> 24) & 0xFF) == 192) |
|
221 |
&& (((address >>> 16) & 0xFF) == 168)); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Utility routine to check if the multicast address has global scope. |
|
226 |
* |
|
19069 | 227 |
* @return a {@code boolean} indicating if the address has |
2 | 228 |
* is a multicast address of global scope, false if it is not |
229 |
* of global scope or it is not a multicast address |
|
230 |
* @since 1.4 |
|
231 |
*/ |
|
232 |
public boolean isMCGlobal() { |
|
233 |
// 224.0.1.0 to 238.255.255.255 |
|
234 |
byte[] byteAddr = getAddress(); |
|
235 |
return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) && |
|
236 |
!((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 && |
|
237 |
byteAddr[2] == 0); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Utility routine to check if the multicast address has node scope. |
|
242 |
* |
|
19069 | 243 |
* @return a {@code boolean} indicating if the address has |
2 | 244 |
* is a multicast address of node-local scope, false if it is not |
245 |
* of node-local scope or it is not a multicast address |
|
246 |
* @since 1.4 |
|
247 |
*/ |
|
248 |
public boolean isMCNodeLocal() { |
|
249 |
// unless ttl == 0 |
|
250 |
return false; |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Utility routine to check if the multicast address has link scope. |
|
255 |
* |
|
19069 | 256 |
* @return a {@code boolean} indicating if the address has |
2 | 257 |
* is a multicast address of link-local scope, false if it is not |
258 |
* of link-local scope or it is not a multicast address |
|
259 |
* @since 1.4 |
|
260 |
*/ |
|
261 |
public boolean isMCLinkLocal() { |
|
262 |
// 224.0.0/24 prefix and ttl == 1 |
|
16870 | 263 |
int address = holder().getAddress(); |
2 | 264 |
return (((address >>> 24) & 0xFF) == 224) |
265 |
&& (((address >>> 16) & 0xFF) == 0) |
|
266 |
&& (((address >>> 8) & 0xFF) == 0); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Utility routine to check if the multicast address has site scope. |
|
271 |
* |
|
19069 | 272 |
* @return a {@code boolean} indicating if the address has |
2 | 273 |
* is a multicast address of site-local scope, false if it is not |
274 |
* of site-local scope or it is not a multicast address |
|
275 |
* @since 1.4 |
|
276 |
*/ |
|
277 |
public boolean isMCSiteLocal() { |
|
278 |
// 239.255/16 prefix or ttl < 32 |
|
16870 | 279 |
int address = holder().getAddress(); |
2 | 280 |
return (((address >>> 24) & 0xFF) == 239) |
281 |
&& (((address >>> 16) & 0xFF) == 255); |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Utility routine to check if the multicast address has organization scope. |
|
286 |
* |
|
19069 | 287 |
* @return a {@code boolean} indicating if the address has |
2 | 288 |
* is a multicast address of organization-local scope, |
289 |
* false if it is not of organization-local scope |
|
290 |
* or it is not a multicast address |
|
291 |
* @since 1.4 |
|
292 |
*/ |
|
293 |
public boolean isMCOrgLocal() { |
|
294 |
// 239.192 - 239.195 |
|
16870 | 295 |
int address = holder().getAddress(); |
2 | 296 |
return (((address >>> 24) & 0xFF) == 239) |
297 |
&& (((address >>> 16) & 0xFF) >= 192) |
|
298 |
&& (((address >>> 16) & 0xFF) <= 195); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
19069 | 302 |
* Returns the raw IP address of this {@code InetAddress} |
2 | 303 |
* object. The result is in network byte order: the highest order |
19069 | 304 |
* byte of the address is in {@code getAddress()[0]}. |
2 | 305 |
* |
306 |
* @return the raw IP address of this object. |
|
307 |
*/ |
|
308 |
public byte[] getAddress() { |
|
16870 | 309 |
int address = holder().getAddress(); |
2 | 310 |
byte[] addr = new byte[INADDRSZ]; |
311 |
||
312 |
addr[0] = (byte) ((address >>> 24) & 0xFF); |
|
313 |
addr[1] = (byte) ((address >>> 16) & 0xFF); |
|
314 |
addr[2] = (byte) ((address >>> 8) & 0xFF); |
|
315 |
addr[3] = (byte) (address & 0xFF); |
|
316 |
return addr; |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* Returns the IP address string in textual presentation form. |
|
321 |
* |
|
322 |
* @return the raw IP address in a string format. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
19069
diff
changeset
|
323 |
* @since 1.0.2 |
2 | 324 |
*/ |
325 |
public String getHostAddress() { |
|
326 |
return numericToTextFormat(getAddress()); |
|
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Returns a hashcode for this IP address. |
|
331 |
* |
|
332 |
* @return a hash code value for this IP address. |
|
333 |
*/ |
|
334 |
public int hashCode() { |
|
16870 | 335 |
return holder().getAddress(); |
2 | 336 |
} |
337 |
||
338 |
/** |
|
339 |
* Compares this object against the specified object. |
|
19069 | 340 |
* The result is {@code true} if and only if the argument is |
341 |
* not {@code null} and it represents the same IP address as |
|
2 | 342 |
* this object. |
343 |
* <p> |
|
19069 | 344 |
* Two instances of {@code InetAddress} represent the same IP |
2 | 345 |
* address if the length of the byte arrays returned by |
19069 | 346 |
* {@code getAddress} is the same for both, and each of the |
2 | 347 |
* array components is the same for the byte arrays. |
348 |
* |
|
349 |
* @param obj the object to compare against. |
|
19069 | 350 |
* @return {@code true} if the objects are the same; |
351 |
* {@code false} otherwise. |
|
2 | 352 |
* @see java.net.InetAddress#getAddress() |
353 |
*/ |
|
354 |
public boolean equals(Object obj) { |
|
355 |
return (obj != null) && (obj instanceof Inet4Address) && |
|
16870 | 356 |
(((InetAddress)obj).holder().getAddress() == holder().getAddress()); |
2 | 357 |
} |
358 |
||
359 |
// Utilities |
|
360 |
/* |
|
361 |
* Converts IPv4 binary address into a string suitable for presentation. |
|
362 |
* |
|
363 |
* @param src a byte array representing an IPv4 numeric address |
|
364 |
* @return a String representing the IPv4 address in |
|
365 |
* textual representation format |
|
366 |
* @since 1.4 |
|
367 |
*/ |
|
368 |
||
369 |
static String numericToTextFormat(byte[] src) |
|
370 |
{ |
|
371 |
return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff); |
|
372 |
} |
|
373 |
||
374 |
/** |
|
375 |
* Perform class load-time initializations. |
|
376 |
*/ |
|
377 |
private static native void init(); |
|
378 |
} |