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