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