author | xdono |
Thu, 02 Oct 2008 19:58:32 -0700 | |
changeset 1247 | b4c26443dee5 |
parent 902 | bbfb1e2369b2 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 2000-2008 Sun Microsystems, Inc. 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 |
|
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.io.ObjectInputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InvalidObjectException; |
|
31 |
import java.util.Enumeration; |
|
32 |
||
33 |
/** |
|
34 |
* This class represents an Internet Protocol version 6 (IPv6) address. |
|
35 |
* Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt"> |
|
36 |
* <i>RFC 2373: IP Version 6 Addressing Architecture</i></a>. |
|
37 |
* |
|
38 |
* <h4> <A NAME="format">Textual representation of IP addresses</a> </h4> |
|
39 |
* |
|
40 |
* Textual representation of IPv6 address used as input to methods |
|
41 |
* takes one of the following forms: |
|
42 |
* |
|
43 |
* <ol> |
|
44 |
* <li><p> <A NAME="lform">The preferred form</a> is x:x:x:x:x:x:x:x, |
|
45 |
* where the 'x's are |
|
46 |
* the hexadecimal values of the eight 16-bit pieces of the |
|
47 |
* address. This is the full form. For example, |
|
48 |
* |
|
49 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
50 |
* <tr><td><tt>1080:0:0:0:8:800:200C:417A</tt><td></tr> |
|
51 |
* </table></blockquote> |
|
52 |
* |
|
53 |
* <p> Note that it is not necessary to write the leading zeros in |
|
54 |
* an individual field. However, there must be at least one numeral |
|
55 |
* in every field, except as described below.</li> |
|
56 |
* |
|
57 |
* <li><p> Due to some methods of allocating certain styles of IPv6 |
|
58 |
* addresses, it will be common for addresses to contain long |
|
59 |
* strings of zero bits. In order to make writing addresses |
|
60 |
* containing zero bits easier, a special syntax is available to |
|
61 |
* compress the zeros. The use of "::" indicates multiple groups |
|
62 |
* of 16-bits of zeros. The "::" can only appear once in an address. |
|
63 |
* The "::" can also be used to compress the leading and/or trailing |
|
64 |
* zeros in an address. For example, |
|
65 |
* |
|
66 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
67 |
* <tr><td><tt>1080::8:800:200C:417A</tt><td></tr> |
|
68 |
* </table></blockquote> |
|
69 |
* |
|
70 |
* <li><p> An alternative form that is sometimes more convenient |
|
71 |
* when dealing with a mixed environment of IPv4 and IPv6 nodes is |
|
72 |
* x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values |
|
73 |
* of the six high-order 16-bit pieces of the address, and the 'd's |
|
74 |
* are the decimal values of the four low-order 8-bit pieces of the |
|
75 |
* standard IPv4 representation address, for example, |
|
76 |
* |
|
77 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
78 |
* <tr><td><tt>::FFFF:129.144.52.38</tt><td></tr> |
|
79 |
* <tr><td><tt>::129.144.52.38</tt><td></tr> |
|
80 |
* </table></blockquote> |
|
81 |
* |
|
82 |
* <p> where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the |
|
83 |
* general forms of an IPv4-mapped IPv6 address and an |
|
84 |
* IPv4-compatible IPv6 address. Note that the IPv4 portion must be |
|
85 |
* in the "d.d.d.d" form. The following forms are invalid: |
|
86 |
* |
|
87 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
88 |
* <tr><td><tt>::FFFF:d.d.d</tt><td></tr> |
|
89 |
* <tr><td><tt>::FFFF:d.d</tt><td></tr> |
|
90 |
* <tr><td><tt>::d.d.d</tt><td></tr> |
|
91 |
* <tr><td><tt>::d.d</tt><td></tr> |
|
92 |
* </table></blockquote> |
|
93 |
* |
|
94 |
* <p> The following form: |
|
95 |
* |
|
96 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
97 |
* <tr><td><tt>::FFFF:d</tt><td></tr> |
|
98 |
* </table></blockquote> |
|
99 |
* |
|
100 |
* <p> is valid, however it is an unconventional representation of |
|
101 |
* the IPv4-compatible IPv6 address, |
|
102 |
* |
|
103 |
* <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> |
|
104 |
* <tr><td><tt>::255.255.0.d</tt><td></tr> |
|
105 |
* </table></blockquote> |
|
106 |
* |
|
107 |
* <p> while "::d" corresponds to the general IPv6 address |
|
108 |
* "0:0:0:0:0:0:0:d".</li> |
|
109 |
* </ol> |
|
110 |
* |
|
111 |
* <p> For methods that return a textual representation as output |
|
112 |
* value, the full form is used. Inet6Address will return the full |
|
113 |
* form because it is unambiguous when used in combination with other |
|
114 |
* textual data. |
|
115 |
* |
|
116 |
* <h4> Special IPv6 address </h4> |
|
117 |
* |
|
118 |
* <blockquote> |
|
119 |
* <table cellspacing=2 summary="Description of IPv4-mapped address"> <tr><th valign=top><i>IPv4-mapped address</i></th> |
|
120 |
* <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to |
|
121 |
* represent an IPv4 address. It allows the native program to |
|
122 |
* use the same address data structure and also the same |
|
123 |
* socket when communicating with both IPv4 and IPv6 nodes. |
|
124 |
* |
|
125 |
* <p>In InetAddress and Inet6Address, it is used for internal |
|
126 |
* representation; it has no functional role. Java will never |
|
127 |
* return an IPv4-mapped address. These classes can take an |
|
128 |
* IPv4-mapped address as input, both in byte array and text |
|
129 |
* representation. However, it will be converted into an IPv4 |
|
130 |
* address.</td></tr> |
|
131 |
* </table></blockquote> |
|
132 |
* <p> |
|
133 |
* <h4> <A NAME="scoped">Textual representation of IPv6 scoped addresses</a> </h4> |
|
134 |
* <p> |
|
135 |
* The textual representation of IPv6 addresses as described above can be extended |
|
136 |
* to specify IPv6 scoped addresses. This extension to the basic addressing architecture |
|
137 |
* is described in [draft-ietf-ipngwg-scoping-arch-04.txt]. |
|
138 |
* <p> |
|
139 |
* Because link-local and site-local addresses are non-global, it is possible that different hosts |
|
140 |
* may have the same destination address and may be reachable through different interfaces on the |
|
141 |
* same originating system. In this case, the originating system is said to be connected |
|
142 |
* to multiple zones of the same scope. In order to disambiguate which is the intended destination |
|
143 |
* zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an IPv6 address. |
|
144 |
* <p> |
|
145 |
* The general format for specifying the <i>scope_id</i> is the following: |
|
146 |
* <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote> |
|
147 |
* <p> The IPv6-address is a literal IPv6 address as described above. |
|
148 |
* The <i>scope_id</i> refers to an interface on the local system, and it can be specified |
|
149 |
* in two ways. |
|
150 |
* <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer that identifies the |
|
151 |
* particular interface and scope as understood by the system. Usually, the numeric |
|
152 |
* values can be determined through administration tools on the system. Each interface may |
|
153 |
* have multiple values, one for each scope. If the scope is unspecified, then the default value |
|
154 |
* used is zero.</li><p> |
|
155 |
* <li><i>As a string.</i> This must be the exact string that is returned by |
|
156 |
* {@link java.net.NetworkInterface#getName()} for the particular interface in question. |
|
157 |
* When an Inet6Address is created in this way, the numeric scope-id is determined at the time |
|
158 |
* the object is created by querying the relevant NetworkInterface.</li> |
|
159 |
* </ol><p> |
|
160 |
* Note also, that the numeric <i>scope_id</i> can be retrieved from Inet6Address instances returned from the |
|
161 |
* NetworkInterface class. This can be used to find out the current scope ids configured on the system. |
|
162 |
* @since 1.4 |
|
163 |
*/ |
|
164 |
||
165 |
public final |
|
166 |
class Inet6Address extends InetAddress { |
|
167 |
final static int INADDRSZ = 16; |
|
168 |
||
169 |
/* |
|
170 |
* cached scope_id - for link-local address use only. |
|
171 |
*/ |
|
172 |
private transient int cached_scope_id = 0; |
|
173 |
||
174 |
/** |
|
175 |
* Holds a 128-bit (16 bytes) IPv6 address. |
|
176 |
* |
|
177 |
* @serial |
|
178 |
*/ |
|
179 |
byte[] ipaddress; |
|
180 |
||
181 |
/** |
|
182 |
* scope_id. The scope specified when the object is created. If the object is created |
|
183 |
* with an interface name, then the scope_id is not determined until the time it is needed. |
|
184 |
*/ |
|
185 |
private int scope_id = 0; |
|
186 |
||
187 |
/** |
|
188 |
* This will be set to true when the scope_id field contains a valid |
|
189 |
* integer scope_id. |
|
190 |
*/ |
|
191 |
private boolean scope_id_set = false; |
|
192 |
||
193 |
/** |
|
194 |
* scoped interface. scope_id is derived from this as the scope_id of the first |
|
195 |
* address whose scope is the same as this address for the named interface. |
|
196 |
*/ |
|
197 |
private transient NetworkInterface scope_ifname = null; |
|
198 |
||
199 |
/** |
|
200 |
* set if the object is constructed with a scoped interface instead of a |
|
201 |
* numeric scope id. |
|
202 |
*/ |
|
203 |
private boolean scope_ifname_set = false; |
|
204 |
||
205 |
private static final long serialVersionUID = 6880410070516793377L; |
|
206 |
||
207 |
/* |
|
208 |
* Perform initializations. |
|
209 |
*/ |
|
210 |
static { |
|
211 |
init(); |
|
212 |
} |
|
213 |
||
214 |
Inet6Address() { |
|
215 |
super(); |
|
216 |
hostName = null; |
|
217 |
ipaddress = new byte[INADDRSZ]; |
|
218 |
family = IPv6; |
|
219 |
} |
|
220 |
||
221 |
/* checking of value for scope_id should be done by caller |
|
222 |
* scope_id must be >= 0, or -1 to indicate not being set |
|
223 |
*/ |
|
224 |
Inet6Address(String hostName, byte addr[], int scope_id) { |
|
225 |
this.hostName = hostName; |
|
226 |
if (addr.length == INADDRSZ) { // normal IPv6 address |
|
227 |
family = IPv6; |
|
228 |
ipaddress = addr.clone(); |
|
229 |
} |
|
230 |
if (scope_id >= 0) { |
|
231 |
this.scope_id = scope_id; |
|
232 |
scope_id_set = true; |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
Inet6Address(String hostName, byte addr[]) { |
|
237 |
try { |
|
238 |
initif (hostName, addr, null); |
|
239 |
} catch (UnknownHostException e) {} /* cant happen if ifname is null */ |
|
240 |
} |
|
241 |
||
242 |
Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException { |
|
243 |
initif (hostName, addr, nif); |
|
244 |
} |
|
245 |
||
246 |
Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException { |
|
247 |
initstr (hostName, addr, ifname); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])} |
|
252 |
* except that the IPv6 scope_id is set to the value corresponding to the given interface |
|
253 |
* for the address type specified in <code>addr</code>. |
|
254 |
* The call will fail with an UnknownHostException if the given interface does not have a numeric |
|
255 |
* scope_id assigned for the given address type (eg. link-local or site-local). |
|
256 |
* See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
|
257 |
* scoped addresses. |
|
258 |
* |
|
259 |
* @param host the specified host |
|
260 |
* @param addr the raw IP address in network byte order |
|
261 |
* @param nif an interface this address must be associated with. |
|
262 |
* @return an Inet6Address object created from the raw IP address. |
|
263 |
* @exception UnknownHostException if IP address is of illegal length, or if the interface |
|
264 |
* does not have a numeric scope_id assigned for the given address type. |
|
265 |
* |
|
266 |
* @since 1.5 |
|
267 |
*/ |
|
268 |
||
269 |
public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif) |
|
270 |
throws UnknownHostException { |
|
271 |
if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
|
272 |
if (host.charAt(host.length()-1) == ']') { |
|
273 |
host = host.substring(1, host.length() -1); |
|
274 |
} |
|
275 |
} |
|
276 |
if (addr != null) { |
|
277 |
if (addr.length == Inet6Address.INADDRSZ) { |
|
278 |
return new Inet6Address(host, addr, nif); |
|
279 |
} |
|
280 |
} |
|
281 |
throw new UnknownHostException("addr is of illegal length"); |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])} |
|
286 |
* except that the IPv6 scope_id is set to the given numeric value. |
|
287 |
* The scope_id is not checked to determine if it corresponds to any interface on the system. |
|
288 |
* See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
|
289 |
* scoped addresses. |
|
290 |
* |
|
291 |
* @param host the specified host |
|
292 |
* @param addr the raw IP address in network byte order |
|
293 |
* @param scope_id the numeric scope_id for the address. |
|
294 |
* @return an Inet6Address object created from the raw IP address. |
|
295 |
* @exception UnknownHostException if IP address is of illegal length. |
|
296 |
* |
|
297 |
* @since 1.5 |
|
298 |
*/ |
|
299 |
||
300 |
public static Inet6Address getByAddress(String host, byte[] addr, int scope_id) |
|
301 |
throws UnknownHostException { |
|
302 |
if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
|
303 |
if (host.charAt(host.length()-1) == ']') { |
|
304 |
host = host.substring(1, host.length() -1); |
|
305 |
} |
|
306 |
} |
|
307 |
if (addr != null) { |
|
308 |
if (addr.length == Inet6Address.INADDRSZ) { |
|
309 |
return new Inet6Address(host, addr, scope_id); |
|
310 |
} |
|
311 |
} |
|
312 |
throw new UnknownHostException("addr is of illegal length"); |
|
313 |
} |
|
314 |
||
315 |
private void initstr (String hostName, byte addr[], String ifname) throws UnknownHostException { |
|
316 |
try { |
|
317 |
NetworkInterface nif = NetworkInterface.getByName (ifname); |
|
318 |
if (nif == null) { |
|
319 |
throw new UnknownHostException ("no such interface " + ifname); |
|
320 |
} |
|
321 |
initif (hostName, addr, nif); |
|
322 |
} catch (SocketException e) { |
|
323 |
throw new UnknownHostException ("SocketException thrown" + ifname); |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException { |
|
328 |
this.hostName = hostName; |
|
329 |
if (addr.length == INADDRSZ) { // normal IPv6 address |
|
330 |
family = IPv6; |
|
331 |
ipaddress = addr.clone(); |
|
332 |
} |
|
333 |
if (nif != null) { |
|
334 |
this.scope_ifname = nif; |
|
335 |
scope_ifname_set = true; |
|
336 |
scope_id = deriveNumericScope (nif); |
|
337 |
scope_id_set = true; |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
/* check the two Ipv6 addresses and return false if they are both |
|
342 |
* non global address types, but not the same. |
|
343 |
* (ie. one is sitelocal and the other linklocal) |
|
344 |
* return true otherwise. |
|
345 |
*/ |
|
346 |
private boolean differentLocalAddressTypes(Inet6Address other) { |
|
347 |
||
348 |
if (isLinkLocalAddress() && !other.isLinkLocalAddress()) { |
|
349 |
return false; |
|
350 |
} |
|
351 |
if (isSiteLocalAddress() && !other.isSiteLocalAddress()) { |
|
352 |
return false; |
|
353 |
} |
|
354 |
return true; |
|
355 |
} |
|
356 |
||
357 |
private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException { |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
358 |
Enumeration<InetAddress> addresses = ifc.getInetAddresses(); |
2 | 359 |
while (addresses.hasMoreElements()) { |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
360 |
InetAddress addr = addresses.nextElement(); |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
361 |
if (!(addr instanceof Inet6Address)) { |
2 | 362 |
continue; |
363 |
} |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
364 |
Inet6Address ia6_addr = (Inet6Address)addr; |
2 | 365 |
/* check if site or link local prefixes match */ |
366 |
if (!differentLocalAddressTypes(ia6_addr)){ |
|
367 |
/* type not the same, so carry on searching */ |
|
368 |
continue; |
|
369 |
} |
|
370 |
/* found a matching address - return its scope_id */ |
|
371 |
return ia6_addr.scope_id; |
|
372 |
} |
|
373 |
throw new UnknownHostException ("no scope_id found"); |
|
374 |
} |
|
375 |
||
376 |
private int deriveNumericScope (String ifname) throws UnknownHostException { |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
377 |
Enumeration<NetworkInterface> en; |
2 | 378 |
try { |
379 |
en = NetworkInterface.getNetworkInterfaces(); |
|
380 |
} catch (SocketException e) { |
|
381 |
throw new UnknownHostException ("could not enumerate local network interfaces"); |
|
382 |
} |
|
383 |
while (en.hasMoreElements()) { |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
384 |
NetworkInterface ifc = en.nextElement(); |
2 | 385 |
if (ifc.getName().equals (ifname)) { |
386 |
Enumeration addresses = ifc.getInetAddresses(); |
|
387 |
while (addresses.hasMoreElements()) { |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
388 |
InetAddress addr = (InetAddress)addresses.nextElement(); |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
389 |
if (!(addr instanceof Inet6Address)) { |
2 | 390 |
continue; |
391 |
} |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
392 |
Inet6Address ia6_addr = (Inet6Address)addr; |
2 | 393 |
/* check if site or link local prefixes match */ |
394 |
if (!differentLocalAddressTypes(ia6_addr)){ |
|
395 |
/* type not the same, so carry on searching */ |
|
396 |
continue; |
|
397 |
} |
|
398 |
/* found a matching address - return its scope_id */ |
|
399 |
return ia6_addr.scope_id; |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
403 |
throw new UnknownHostException ("No matching address found for interface : " +ifname); |
|
404 |
} |
|
405 |
||
406 |
/** |
|
407 |
* restore the state of this object from stream |
|
408 |
* including the scope information, only if the |
|
409 |
* scoped interface name is valid on this system |
|
410 |
*/ |
|
411 |
private void readObject(ObjectInputStream s) |
|
412 |
throws IOException, ClassNotFoundException { |
|
413 |
scope_ifname = null; |
|
414 |
scope_ifname_set = false; |
|
415 |
s.defaultReadObject(); |
|
416 |
||
417 |
if (ifname != null && !"".equals (ifname)) { |
|
418 |
try { |
|
419 |
scope_ifname = NetworkInterface.getByName(ifname); |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
420 |
if (scope_ifname == null) { |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
421 |
/* the interface does not exist on this system, so we clear |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
422 |
* the scope information completely */ |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
423 |
scope_id_set = false; |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
424 |
scope_ifname_set = false; |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
425 |
scope_id = 0; |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
426 |
} else { |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
427 |
try { |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
428 |
scope_id = deriveNumericScope (scope_ifname); |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
429 |
} catch (UnknownHostException e) { |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
430 |
// should not happen |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
431 |
assert false; |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
432 |
} |
2 | 433 |
} |
434 |
} catch (SocketException e) {} |
|
435 |
||
436 |
} |
|
437 |
/* if ifname was not supplied, then the numeric info is used */ |
|
438 |
||
439 |
ipaddress = ipaddress.clone(); |
|
440 |
||
441 |
// Check that our invariants are satisfied |
|
442 |
if (ipaddress.length != INADDRSZ) { |
|
443 |
throw new InvalidObjectException("invalid address length: "+ |
|
444 |
ipaddress.length); |
|
445 |
} |
|
446 |
||
447 |
if (family != IPv6) { |
|
448 |
throw new InvalidObjectException("invalid address family type"); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Utility routine to check if the InetAddress is an IP multicast |
|
454 |
* address. 11111111 at the start of the address identifies the |
|
455 |
* address as being a multicast address. |
|
456 |
* |
|
457 |
* @return a <code>boolean</code> indicating if the InetAddress is |
|
458 |
* an IP multicast address |
|
459 |
* @since JDK1.1 |
|
460 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
461 |
@Override |
2 | 462 |
public boolean isMulticastAddress() { |
463 |
return ((ipaddress[0] & 0xff) == 0xff); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Utility routine to check if the InetAddress in a wildcard address. |
|
468 |
* @return a <code>boolean</code> indicating if the Inetaddress is |
|
469 |
* a wildcard address. |
|
470 |
* @since 1.4 |
|
471 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
472 |
@Override |
2 | 473 |
public boolean isAnyLocalAddress() { |
474 |
byte test = 0x00; |
|
475 |
for (int i = 0; i < INADDRSZ; i++) { |
|
476 |
test |= ipaddress[i]; |
|
477 |
} |
|
478 |
return (test == 0x00); |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* Utility routine to check if the InetAddress is a loopback address. |
|
483 |
* |
|
484 |
* @return a <code>boolean</code> indicating if the InetAddress is |
|
485 |
* a loopback address; or false otherwise. |
|
486 |
* @since 1.4 |
|
487 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
488 |
@Override |
2 | 489 |
public boolean isLoopbackAddress() { |
490 |
byte test = 0x00; |
|
491 |
for (int i = 0; i < 15; i++) { |
|
492 |
test |= ipaddress[i]; |
|
493 |
} |
|
494 |
return (test == 0x00) && (ipaddress[15] == 0x01); |
|
495 |
} |
|
496 |
||
497 |
/** |
|
498 |
* Utility routine to check if the InetAddress is an link local address. |
|
499 |
* |
|
500 |
* @return a <code>boolean</code> indicating if the InetAddress is |
|
501 |
* a link local address; or false if address is not a link local unicast address. |
|
502 |
* @since 1.4 |
|
503 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
504 |
@Override |
2 | 505 |
public boolean isLinkLocalAddress() { |
506 |
return ((ipaddress[0] & 0xff) == 0xfe |
|
507 |
&& (ipaddress[1] & 0xc0) == 0x80); |
|
508 |
} |
|
509 |
||
510 |
/** |
|
511 |
* Utility routine to check if the InetAddress is a site local address. |
|
512 |
* |
|
513 |
* @return a <code>boolean</code> indicating if the InetAddress is |
|
514 |
* a site local address; or false if address is not a site local unicast address. |
|
515 |
* @since 1.4 |
|
516 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
517 |
@Override |
2 | 518 |
public boolean isSiteLocalAddress() { |
519 |
return ((ipaddress[0] & 0xff) == 0xfe |
|
520 |
&& (ipaddress[1] & 0xc0) == 0xc0); |
|
521 |
} |
|
522 |
||
523 |
/** |
|
524 |
* Utility routine to check if the multicast address has global scope. |
|
525 |
* |
|
526 |
* @return a <code>boolean</code> indicating if the address has |
|
527 |
* is a multicast address of global scope, false if it is not |
|
528 |
* of global scope or it is not a multicast address |
|
529 |
* @since 1.4 |
|
530 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
531 |
@Override |
2 | 532 |
public boolean isMCGlobal() { |
533 |
return ((ipaddress[0] & 0xff) == 0xff |
|
534 |
&& (ipaddress[1] & 0x0f) == 0x0e); |
|
535 |
} |
|
536 |
||
537 |
/** |
|
538 |
* Utility routine to check if the multicast address has node scope. |
|
539 |
* |
|
540 |
* @return a <code>boolean</code> indicating if the address has |
|
541 |
* is a multicast address of node-local scope, false if it is not |
|
542 |
* of node-local scope or it is not a multicast address |
|
543 |
* @since 1.4 |
|
544 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
545 |
@Override |
2 | 546 |
public boolean isMCNodeLocal() { |
547 |
return ((ipaddress[0] & 0xff) == 0xff |
|
548 |
&& (ipaddress[1] & 0x0f) == 0x01); |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* Utility routine to check if the multicast address has link scope. |
|
553 |
* |
|
554 |
* @return a <code>boolean</code> indicating if the address has |
|
555 |
* is a multicast address of link-local scope, false if it is not |
|
556 |
* of link-local scope or it is not a multicast address |
|
557 |
* @since 1.4 |
|
558 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
559 |
@Override |
2 | 560 |
public boolean isMCLinkLocal() { |
561 |
return ((ipaddress[0] & 0xff) == 0xff |
|
562 |
&& (ipaddress[1] & 0x0f) == 0x02); |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* Utility routine to check if the multicast address has site scope. |
|
567 |
* |
|
568 |
* @return a <code>boolean</code> indicating if the address has |
|
569 |
* is a multicast address of site-local scope, false if it is not |
|
570 |
* of site-local scope or it is not a multicast address |
|
571 |
* @since 1.4 |
|
572 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
573 |
@Override |
2 | 574 |
public boolean isMCSiteLocal() { |
575 |
return ((ipaddress[0] & 0xff) == 0xff |
|
576 |
&& (ipaddress[1] & 0x0f) == 0x05); |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Utility routine to check if the multicast address has organization scope. |
|
581 |
* |
|
582 |
* @return a <code>boolean</code> indicating if the address has |
|
583 |
* is a multicast address of organization-local scope, |
|
584 |
* false if it is not of organization-local scope |
|
585 |
* or it is not a multicast address |
|
586 |
* @since 1.4 |
|
587 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
588 |
@Override |
2 | 589 |
public boolean isMCOrgLocal() { |
590 |
return ((ipaddress[0] & 0xff) == 0xff |
|
591 |
&& (ipaddress[1] & 0x0f) == 0x08); |
|
592 |
} |
|
593 |
||
594 |
/** |
|
595 |
* Returns the raw IP address of this <code>InetAddress</code> |
|
596 |
* object. The result is in network byte order: the highest order |
|
597 |
* byte of the address is in <code>getAddress()[0]</code>. |
|
598 |
* |
|
599 |
* @return the raw IP address of this object. |
|
600 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
601 |
@Override |
2 | 602 |
public byte[] getAddress() { |
603 |
return ipaddress.clone(); |
|
604 |
} |
|
605 |
||
606 |
/** |
|
607 |
* Returns the numeric scopeId, if this instance is associated with |
|
608 |
* an interface. If no scoped_id is set, the returned value is zero. |
|
609 |
* |
|
610 |
* @return the scopeId, or zero if not set. |
|
611 |
* @since 1.5 |
|
612 |
*/ |
|
613 |
public int getScopeId () { |
|
614 |
return scope_id; |
|
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* Returns the scoped interface, if this instance was created with |
|
619 |
* with a scoped interface. |
|
620 |
* |
|
621 |
* @return the scoped interface, or null if not set. |
|
622 |
* @since 1.5 |
|
623 |
*/ |
|
624 |
public NetworkInterface getScopedInterface () { |
|
625 |
return scope_ifname; |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* Returns the IP address string in textual presentation. If the instance was created |
|
630 |
* specifying a scope identifier then the scope id is appended to the IP address preceded by |
|
631 |
* a "%" (per-cent) character. This can be either a numeric value or a string, depending on which |
|
632 |
* was used to createthe instance. |
|
633 |
* |
|
634 |
* @return the raw IP address in a string format. |
|
635 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
636 |
@Override |
2 | 637 |
public String getHostAddress() { |
638 |
String s = numericToTextFormat(ipaddress); |
|
639 |
if (scope_ifname_set) { /* must check this first */ |
|
640 |
s = s + "%" + scope_ifname.getName(); |
|
641 |
} else if (scope_id_set) { |
|
642 |
s = s + "%" + scope_id; |
|
643 |
} |
|
644 |
return s; |
|
645 |
} |
|
646 |
||
647 |
/** |
|
648 |
* Returns a hashcode for this IP address. |
|
649 |
* |
|
650 |
* @return a hash code value for this IP address. |
|
651 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
652 |
@Override |
2 | 653 |
public int hashCode() { |
654 |
if (ipaddress != null) { |
|
655 |
||
656 |
int hash = 0; |
|
657 |
int i=0; |
|
658 |
while (i<INADDRSZ) { |
|
659 |
int j=0; |
|
660 |
int component=0; |
|
661 |
while (j<4 && i<INADDRSZ) { |
|
662 |
component = (component << 8) + ipaddress[i]; |
|
663 |
j++; |
|
664 |
i++; |
|
665 |
} |
|
666 |
hash += component; |
|
667 |
} |
|
668 |
return hash; |
|
669 |
||
670 |
} else { |
|
671 |
return 0; |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
/** |
|
676 |
* Compares this object against the specified object. |
|
677 |
* The result is <code>true</code> if and only if the argument is |
|
678 |
* not <code>null</code> and it represents the same IP address as |
|
679 |
* this object. |
|
680 |
* <p> |
|
681 |
* Two instances of <code>InetAddress</code> represent the same IP |
|
682 |
* address if the length of the byte arrays returned by |
|
683 |
* <code>getAddress</code> is the same for both, and each of the |
|
684 |
* array components is the same for the byte arrays. |
|
685 |
* |
|
686 |
* @param obj the object to compare against. |
|
687 |
* @return <code>true</code> if the objects are the same; |
|
688 |
* <code>false</code> otherwise. |
|
689 |
* @see java.net.InetAddress#getAddress() |
|
690 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
691 |
@Override |
2 | 692 |
public boolean equals(Object obj) { |
693 |
if (obj == null || |
|
694 |
!(obj instanceof Inet6Address)) |
|
695 |
return false; |
|
696 |
||
697 |
Inet6Address inetAddr = (Inet6Address)obj; |
|
698 |
||
699 |
for (int i = 0; i < INADDRSZ; i++) { |
|
700 |
if (ipaddress[i] != inetAddr.ipaddress[i]) |
|
701 |
return false; |
|
702 |
} |
|
703 |
||
704 |
return true; |
|
705 |
} |
|
706 |
||
707 |
/** |
|
708 |
* Utility routine to check if the InetAddress is an |
|
709 |
* IPv4 compatible IPv6 address. |
|
710 |
* |
|
711 |
* @return a <code>boolean</code> indicating if the InetAddress is |
|
712 |
* an IPv4 compatible IPv6 address; or false if address is IPv4 address. |
|
713 |
* @since 1.4 |
|
714 |
*/ |
|
715 |
public boolean isIPv4CompatibleAddress() { |
|
716 |
if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) && |
|
717 |
(ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) && |
|
718 |
(ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) && |
|
719 |
(ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) && |
|
720 |
(ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) && |
|
721 |
(ipaddress[10] == 0x00) && (ipaddress[11] == 0x00)) { |
|
722 |
return true; |
|
723 |
} |
|
724 |
return false; |
|
725 |
} |
|
726 |
||
727 |
// Utilities |
|
728 |
private final static int INT16SZ = 2; |
|
729 |
/* |
|
730 |
* Convert IPv6 binary address into presentation (printable) format. |
|
731 |
* |
|
732 |
* @param src a byte array representing the IPv6 numeric address |
|
733 |
* @return a String representing an IPv6 address in |
|
734 |
* textual representation format |
|
735 |
* @since 1.4 |
|
736 |
*/ |
|
737 |
static String numericToTextFormat(byte[] src) |
|
738 |
{ |
|
739 |
StringBuffer sb = new StringBuffer(39); |
|
740 |
for (int i = 0; i < (INADDRSZ / INT16SZ); i++) { |
|
741 |
sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00) |
|
742 |
| (src[(i<<1)+1] & 0xff))); |
|
743 |
if (i < (INADDRSZ / INT16SZ) -1 ) { |
|
744 |
sb.append(":"); |
|
745 |
} |
|
746 |
} |
|
747 |
return sb.toString(); |
|
748 |
} |
|
749 |
||
750 |
/** |
|
751 |
* Perform class load-time initializations. |
|
752 |
*/ |
|
753 |
private static native void init(); |
|
754 |
||
755 |
/** |
|
756 |
* Following field is only used during (de)/serialization |
|
757 |
*/ |
|
758 |
private String ifname; |
|
759 |
||
760 |
/** |
|
761 |
* default behavior is overridden in order to write the |
|
762 |
* scope_ifname field as a String, rather than a NetworkInterface |
|
763 |
* which is not serializable |
|
764 |
*/ |
|
765 |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
|
766 |
throws IOException |
|
767 |
{ |
|
768 |
if (scope_ifname_set) { |
|
769 |
ifname = scope_ifname.getName(); |
|
770 |
} |
|
771 |
s.defaultWriteObject(); |
|
772 |
} |
|
773 |
} |