author | michaelm |
Thu, 13 Jun 2019 09:10:51 +0100 | |
changeset 55375 | 96c7427456f9 |
parent 54206 | 003cc64366da |
child 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54206 | 2 |
* Copyright (c) 2000, 2019, 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; |
20821 | 31 |
import java.io.ObjectOutputStream; |
32 |
import java.io.ObjectStreamField; |
|
2 | 33 |
import java.util.Enumeration; |
20821 | 34 |
import java.util.Arrays; |
2 | 35 |
|
36 |
/** |
|
37 |
* This class represents an Internet Protocol version 6 (IPv6) address. |
|
38 |
* Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt"> |
|
39 |
* <i>RFC 2373: IP Version 6 Addressing Architecture</i></a>. |
|
40 |
* |
|
54206 | 41 |
* <h2> <a id="format">Textual representation of IP addresses</a> </h2> |
2 | 42 |
* |
43 |
* Textual representation of IPv6 address used as input to methods |
|
44 |
* takes one of the following forms: |
|
45 |
* |
|
46 |
* <ol> |
|
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
42474
diff
changeset
|
47 |
* <li><p> <a id="lform">The preferred form</a> is x:x:x:x:x:x:x:x, |
2 | 48 |
* where the 'x's are |
49 |
* the hexadecimal values of the eight 16-bit pieces of the |
|
50 |
* address. This is the full form. For example, |
|
51 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
52 |
* <blockquote><ul style="list-style-type:none"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
53 |
* <li>{@code 1080:0:0:0:8:800:200C:417A}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
54 |
* </ul></blockquote> |
2 | 55 |
* |
56 |
* <p> Note that it is not necessary to write the leading zeros in |
|
57 |
* an individual field. However, there must be at least one numeral |
|
58 |
* in every field, except as described below.</li> |
|
59 |
* |
|
60 |
* <li><p> Due to some methods of allocating certain styles of IPv6 |
|
61 |
* addresses, it will be common for addresses to contain long |
|
62 |
* strings of zero bits. In order to make writing addresses |
|
63 |
* containing zero bits easier, a special syntax is available to |
|
64 |
* compress the zeros. The use of "::" indicates multiple groups |
|
65 |
* of 16-bits of zeros. The "::" can only appear once in an address. |
|
66 |
* The "::" can also be used to compress the leading and/or trailing |
|
67 |
* zeros in an address. For example, |
|
68 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
69 |
* <blockquote><ul style="list-style-type:none"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
70 |
* <li>{@code 1080::8:800:200C:417A}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
71 |
* </ul></blockquote> |
2 | 72 |
* |
73 |
* <li><p> An alternative form that is sometimes more convenient |
|
74 |
* when dealing with a mixed environment of IPv4 and IPv6 nodes is |
|
75 |
* x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values |
|
76 |
* of the six high-order 16-bit pieces of the address, and the 'd's |
|
77 |
* are the decimal values of the four low-order 8-bit pieces of the |
|
78 |
* standard IPv4 representation address, for example, |
|
79 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
80 |
* <blockquote><ul style="list-style-type:none"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
81 |
* <li>{@code ::FFFF:129.144.52.38}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
82 |
* <li>{@code ::129.144.52.38}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
83 |
* </ul></blockquote> |
2 | 84 |
* |
85 |
* <p> where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the |
|
86 |
* general forms of an IPv4-mapped IPv6 address and an |
|
87 |
* IPv4-compatible IPv6 address. Note that the IPv4 portion must be |
|
88 |
* in the "d.d.d.d" form. The following forms are invalid: |
|
89 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
90 |
* <blockquote><ul style="list-style-type:none"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
91 |
* <li>{@code ::FFFF:d.d.d}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
92 |
* <li>{@code ::FFFF:d.d}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
93 |
* <li>{@code ::d.d.d}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
94 |
* <li>{@code ::d.d}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
95 |
* </ul></blockquote> |
2 | 96 |
* |
97 |
* <p> The following form: |
|
98 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
99 |
* <blockquote><ul style="list-style-type:none"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
100 |
* <li>{@code ::FFFF:d}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
101 |
* </ul></blockquote> |
2 | 102 |
* |
103 |
* <p> is valid, however it is an unconventional representation of |
|
104 |
* the IPv4-compatible IPv6 address, |
|
105 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
106 |
* <blockquote><ul style="list-style-type:none"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
107 |
* <li>{@code ::255.255.0.d}</li> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
108 |
* </ul></blockquote> |
2 | 109 |
* |
110 |
* <p> while "::d" corresponds to the general IPv6 address |
|
111 |
* "0:0:0:0:0:0:0:d".</li> |
|
112 |
* </ol> |
|
113 |
* |
|
114 |
* <p> For methods that return a textual representation as output |
|
115 |
* value, the full form is used. Inet6Address will return the full |
|
116 |
* form because it is unambiguous when used in combination with other |
|
117 |
* textual data. |
|
118 |
* |
|
54206 | 119 |
* <h3> Special IPv6 address </h3> |
2 | 120 |
* |
121 |
* <blockquote> |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
122 |
* <table class="borderless"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
123 |
* <caption style="display:none">Description of IPv4-mapped address</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
124 |
* <tr><th style="vertical-align:top; padding-right:2px"><i>IPv4-mapped address</i></th> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44844
diff
changeset
|
125 |
* <td>Of the form ::ffff:w.x.y.z, this IPv6 address is used to |
2 | 126 |
* represent an IPv4 address. It allows the native program to |
127 |
* use the same address data structure and also the same |
|
128 |
* socket when communicating with both IPv4 and IPv6 nodes. |
|
129 |
* |
|
130 |
* <p>In InetAddress and Inet6Address, it is used for internal |
|
131 |
* representation; it has no functional role. Java will never |
|
132 |
* return an IPv4-mapped address. These classes can take an |
|
133 |
* IPv4-mapped address as input, both in byte array and text |
|
134 |
* representation. However, it will be converted into an IPv4 |
|
135 |
* address.</td></tr> |
|
136 |
* </table></blockquote> |
|
21334 | 137 |
* |
54206 | 138 |
* <h3><a id="scoped">Textual representation of IPv6 scoped addresses</a></h3> |
14783
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> 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
|
141 |
* 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
|
142 |
* 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
|
143 |
* |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
144 |
* <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
|
145 |
* 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
|
146 |
* 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
|
147 |
* 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
|
148 |
* 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
|
149 |
* 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
|
150 |
* IPv6 address. |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
151 |
* |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
152 |
* <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
|
153 |
* |
21334 | 154 |
* <blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote> |
2 | 155 |
* <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
|
156 |
* 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
|
157 |
* specified in two ways. |
21334 | 158 |
* <ol><li><i>As a numeric identifier.</i> This must be a positive integer |
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
159 |
* 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
|
160 |
* 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
|
161 |
* 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
|
162 |
* scope. If the scope is unspecified, then the default value used is zero.</li> |
18800 | 163 |
* <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
|
164 |
* {@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
|
165 |
* 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
|
166 |
* 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
|
167 |
* NetworkInterface.</li></ol> |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
168 |
* |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
169 |
* <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
|
170 |
* 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
|
171 |
* used to find out the current scope ids configured on the system. |
2 | 172 |
* @since 1.4 |
173 |
*/ |
|
174 |
||
175 |
public final |
|
176 |
class Inet6Address extends InetAddress { |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
177 |
static final int INADDRSZ = 16; |
2 | 178 |
|
20821 | 179 |
private class Inet6AddressHolder { |
180 |
||
181 |
private Inet6AddressHolder() { |
|
182 |
ipaddress = new byte[INADDRSZ]; |
|
183 |
} |
|
184 |
||
185 |
private Inet6AddressHolder( |
|
186 |
byte[] ipaddress, int scope_id, boolean scope_id_set, |
|
187 |
NetworkInterface ifname, boolean scope_ifname_set) |
|
188 |
{ |
|
189 |
this.ipaddress = ipaddress; |
|
190 |
this.scope_id = scope_id; |
|
191 |
this.scope_id_set = scope_id_set; |
|
192 |
this.scope_ifname_set = scope_ifname_set; |
|
193 |
this.scope_ifname = ifname; |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Holds a 128-bit (16 bytes) IPv6 address. |
|
198 |
*/ |
|
199 |
byte[] ipaddress; |
|
200 |
||
201 |
/** |
|
202 |
* scope_id. The scope specified when the object is created. If the object |
|
203 |
* is created with an interface name, then the scope_id is not determined |
|
204 |
* until the time it is needed. |
|
205 |
*/ |
|
206 |
int scope_id; // 0 |
|
207 |
||
208 |
/** |
|
209 |
* This will be set to true when the scope_id field contains a valid |
|
210 |
* integer scope_id. |
|
211 |
*/ |
|
212 |
boolean scope_id_set; // false |
|
213 |
||
214 |
/** |
|
215 |
* scoped interface. scope_id is derived from this as the scope_id of the first |
|
216 |
* address whose scope is the same as this address for the named interface. |
|
217 |
*/ |
|
218 |
NetworkInterface scope_ifname; // null |
|
2 | 219 |
|
20821 | 220 |
/** |
221 |
* set if the object is constructed with a scoped |
|
222 |
* interface instead of a numeric scope id. |
|
223 |
*/ |
|
224 |
boolean scope_ifname_set; // false; |
|
225 |
||
226 |
void setAddr(byte addr[]) { |
|
227 |
if (addr.length == INADDRSZ) { // normal IPv6 address |
|
228 |
System.arraycopy(addr, 0, ipaddress, 0, INADDRSZ); |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
void init(byte addr[], int scope_id) { |
|
233 |
setAddr(addr); |
|
234 |
||
235 |
if (scope_id >= 0) { |
|
236 |
this.scope_id = scope_id; |
|
237 |
this.scope_id_set = true; |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
void init(byte addr[], NetworkInterface nif) |
|
242 |
throws UnknownHostException |
|
243 |
{ |
|
244 |
setAddr(addr); |
|
245 |
||
246 |
if (nif != null) { |
|
247 |
this.scope_id = deriveNumericScope(ipaddress, nif); |
|
248 |
this.scope_id_set = true; |
|
249 |
this.scope_ifname = nif; |
|
250 |
this.scope_ifname_set = true; |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
String getHostAddress() { |
|
255 |
String s = numericToTextFormat(ipaddress); |
|
256 |
if (scope_ifname != null) { /* must check this first */ |
|
257 |
s = s + "%" + scope_ifname.getName(); |
|
258 |
} else if (scope_id_set) { |
|
259 |
s = s + "%" + scope_id; |
|
260 |
} |
|
261 |
return s; |
|
262 |
} |
|
263 |
||
264 |
public boolean equals(Object o) { |
|
265 |
if (! (o instanceof Inet6AddressHolder)) { |
|
266 |
return false; |
|
267 |
} |
|
268 |
Inet6AddressHolder that = (Inet6AddressHolder)o; |
|
2 | 269 |
|
20821 | 270 |
return Arrays.equals(this.ipaddress, that.ipaddress); |
271 |
} |
|
272 |
||
273 |
public int hashCode() { |
|
274 |
if (ipaddress != null) { |
|
275 |
||
276 |
int hash = 0; |
|
277 |
int i=0; |
|
278 |
while (i<INADDRSZ) { |
|
279 |
int j=0; |
|
280 |
int component=0; |
|
281 |
while (j<4 && i<INADDRSZ) { |
|
282 |
component = (component << 8) + ipaddress[i]; |
|
283 |
j++; |
|
284 |
i++; |
|
285 |
} |
|
286 |
hash += component; |
|
287 |
} |
|
288 |
return hash; |
|
289 |
||
290 |
} else { |
|
291 |
return 0; |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
boolean isIPv4CompatibleAddress() { |
|
296 |
if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) && |
|
297 |
(ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) && |
|
298 |
(ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) && |
|
299 |
(ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) && |
|
300 |
(ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) && |
|
301 |
(ipaddress[10] == 0x00) && (ipaddress[11] == 0x00)) { |
|
302 |
return true; |
|
303 |
} |
|
304 |
return false; |
|
305 |
} |
|
306 |
||
307 |
boolean isMulticastAddress() { |
|
308 |
return ((ipaddress[0] & 0xff) == 0xff); |
|
309 |
} |
|
2 | 310 |
|
20821 | 311 |
boolean isAnyLocalAddress() { |
312 |
byte test = 0x00; |
|
313 |
for (int i = 0; i < INADDRSZ; i++) { |
|
314 |
test |= ipaddress[i]; |
|
315 |
} |
|
316 |
return (test == 0x00); |
|
317 |
} |
|
318 |
||
319 |
boolean isLoopbackAddress() { |
|
320 |
byte test = 0x00; |
|
321 |
for (int i = 0; i < 15; i++) { |
|
322 |
test |= ipaddress[i]; |
|
323 |
} |
|
324 |
return (test == 0x00) && (ipaddress[15] == 0x01); |
|
325 |
} |
|
326 |
||
327 |
boolean isLinkLocalAddress() { |
|
328 |
return ((ipaddress[0] & 0xff) == 0xfe |
|
329 |
&& (ipaddress[1] & 0xc0) == 0x80); |
|
330 |
} |
|
331 |
||
332 |
||
333 |
boolean isSiteLocalAddress() { |
|
334 |
return ((ipaddress[0] & 0xff) == 0xfe |
|
335 |
&& (ipaddress[1] & 0xc0) == 0xc0); |
|
336 |
} |
|
2 | 337 |
|
20821 | 338 |
boolean isMCGlobal() { |
339 |
return ((ipaddress[0] & 0xff) == 0xff |
|
340 |
&& (ipaddress[1] & 0x0f) == 0x0e); |
|
341 |
} |
|
342 |
||
343 |
boolean isMCNodeLocal() { |
|
344 |
return ((ipaddress[0] & 0xff) == 0xff |
|
345 |
&& (ipaddress[1] & 0x0f) == 0x01); |
|
346 |
} |
|
347 |
||
348 |
boolean isMCLinkLocal() { |
|
349 |
return ((ipaddress[0] & 0xff) == 0xff |
|
350 |
&& (ipaddress[1] & 0x0f) == 0x02); |
|
351 |
} |
|
352 |
||
353 |
boolean isMCSiteLocal() { |
|
354 |
return ((ipaddress[0] & 0xff) == 0xff |
|
355 |
&& (ipaddress[1] & 0x0f) == 0x05); |
|
356 |
} |
|
357 |
||
358 |
boolean isMCOrgLocal() { |
|
359 |
return ((ipaddress[0] & 0xff) == 0xff |
|
360 |
&& (ipaddress[1] & 0x0f) == 0x08); |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
private final transient Inet6AddressHolder holder6; |
|
17198
32db080a2a70
8007373: Inet6Address serialization incompatibility
msheppar
parents:
16870
diff
changeset
|
365 |
|
2 | 366 |
private static final long serialVersionUID = 6880410070516793377L; |
367 |
||
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
368 |
// Perform native initialization |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
369 |
static { init(); } |
2 | 370 |
|
371 |
Inet6Address() { |
|
372 |
super(); |
|
20821 | 373 |
holder.init(null, IPv6); |
374 |
holder6 = new Inet6AddressHolder(); |
|
2 | 375 |
} |
376 |
||
377 |
/* checking of value for scope_id should be done by caller |
|
378 |
* scope_id must be >= 0, or -1 to indicate not being set |
|
379 |
*/ |
|
380 |
Inet6Address(String hostName, byte addr[], int scope_id) { |
|
20821 | 381 |
holder.init(hostName, IPv6); |
382 |
holder6 = new Inet6AddressHolder(); |
|
383 |
holder6.init(addr, scope_id); |
|
2 | 384 |
} |
385 |
||
386 |
Inet6Address(String hostName, byte addr[]) { |
|
20821 | 387 |
holder6 = new Inet6AddressHolder(); |
2 | 388 |
try { |
389 |
initif (hostName, addr, null); |
|
390 |
} catch (UnknownHostException e) {} /* cant happen if ifname is null */ |
|
391 |
} |
|
392 |
||
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
393 |
Inet6Address (String hostName, byte addr[], NetworkInterface nif) |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
394 |
throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
395 |
{ |
20821 | 396 |
holder6 = new Inet6AddressHolder(); |
2 | 397 |
initif (hostName, addr, nif); |
398 |
} |
|
399 |
||
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
400 |
Inet6Address (String hostName, byte addr[], String ifname) |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
401 |
throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
402 |
{ |
20821 | 403 |
holder6 = new Inet6AddressHolder(); |
2 | 404 |
initstr (hostName, addr, ifname); |
405 |
} |
|
406 |
||
407 |
/** |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
408 |
* 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
|
409 |
* 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
|
410 |
* set to the value corresponding to the given interface for the address |
19069 | 411 |
* 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
|
412 |
* UnknownHostException if the given interface does not have a numeric |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52220
diff
changeset
|
413 |
* scope_id assigned for the given address type (e.g. link-local or site-local). |
2 | 414 |
* See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
415 |
* scoped addresses. |
|
416 |
* |
|
417 |
* @param host the specified host |
|
418 |
* @param addr the raw IP address in network byte order |
|
419 |
* @param nif an interface this address must be associated with. |
|
420 |
* @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
|
421 |
* @throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
422 |
* 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
|
423 |
* have a numeric scope_id assigned for the given address type. |
2 | 424 |
* |
425 |
* @since 1.5 |
|
426 |
*/ |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
427 |
public static Inet6Address getByAddress(String host, byte[] addr, |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
428 |
NetworkInterface nif) |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
429 |
throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
430 |
{ |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52499
diff
changeset
|
431 |
if (host != null && !host.isEmpty() && host.charAt(0) == '[') { |
2 | 432 |
if (host.charAt(host.length()-1) == ']') { |
433 |
host = host.substring(1, host.length() -1); |
|
434 |
} |
|
435 |
} |
|
436 |
if (addr != null) { |
|
437 |
if (addr.length == Inet6Address.INADDRSZ) { |
|
438 |
return new Inet6Address(host, addr, nif); |
|
439 |
} |
|
440 |
} |
|
441 |
throw new UnknownHostException("addr is of illegal length"); |
|
442 |
} |
|
443 |
||
444 |
/** |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
445 |
* 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
|
446 |
* 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
|
447 |
* 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
|
448 |
* if it corresponds to any interface on the system. |
2 | 449 |
* See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
450 |
* scoped addresses. |
|
451 |
* |
|
452 |
* @param host the specified host |
|
453 |
* @param addr the raw IP address in network byte order |
|
454 |
* @param scope_id the numeric scope_id for the address. |
|
455 |
* @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
|
456 |
* @throws UnknownHostException if IP address is of illegal length. |
2 | 457 |
* |
458 |
* @since 1.5 |
|
459 |
*/ |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
460 |
public static Inet6Address getByAddress(String host, byte[] addr, |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
461 |
int scope_id) |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
462 |
throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
463 |
{ |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52499
diff
changeset
|
464 |
if (host != null && !host.isEmpty() && host.charAt(0) == '[') { |
2 | 465 |
if (host.charAt(host.length()-1) == ']') { |
466 |
host = host.substring(1, host.length() -1); |
|
467 |
} |
|
468 |
} |
|
469 |
if (addr != null) { |
|
470 |
if (addr.length == Inet6Address.INADDRSZ) { |
|
471 |
return new Inet6Address(host, addr, scope_id); |
|
472 |
} |
|
473 |
} |
|
474 |
throw new UnknownHostException("addr is of illegal length"); |
|
475 |
} |
|
476 |
||
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
477 |
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
|
478 |
throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
479 |
{ |
2 | 480 |
try { |
481 |
NetworkInterface nif = NetworkInterface.getByName (ifname); |
|
482 |
if (nif == null) { |
|
483 |
throw new UnknownHostException ("no such interface " + ifname); |
|
484 |
} |
|
485 |
initif (hostName, addr, nif); |
|
486 |
} catch (SocketException e) { |
|
487 |
throw new UnknownHostException ("SocketException thrown" + ifname); |
|
488 |
} |
|
489 |
} |
|
490 |
||
17198
32db080a2a70
8007373: Inet6Address serialization incompatibility
msheppar
parents:
16870
diff
changeset
|
491 |
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
|
492 |
throws UnknownHostException |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
493 |
{ |
20821 | 494 |
int family = -1; |
495 |
holder6.init(addr, nif); |
|
496 |
||
2 | 497 |
if (addr.length == INADDRSZ) { // normal IPv6 address |
20821 | 498 |
family = IPv6; |
2 | 499 |
} |
20821 | 500 |
holder.init(hostName, family); |
2 | 501 |
} |
502 |
||
503 |
/* check the two Ipv6 addresses and return false if they are both |
|
504 |
* non global address types, but not the same. |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52220
diff
changeset
|
505 |
* (i.e. one is site-local and the other link-local) |
2 | 506 |
* return true otherwise. |
507 |
*/ |
|
20821 | 508 |
|
509 |
private static boolean isDifferentLocalAddressType( |
|
510 |
byte[] thisAddr, byte[] otherAddr) { |
|
511 |
||
512 |
if (Inet6Address.isLinkLocalAddress(thisAddr) && |
|
513 |
!Inet6Address.isLinkLocalAddress(otherAddr)) { |
|
2 | 514 |
return false; |
20821 | 515 |
} |
516 |
if (Inet6Address.isSiteLocalAddress(thisAddr) && |
|
517 |
!Inet6Address.isSiteLocalAddress(otherAddr)) { |
|
2 | 518 |
return false; |
20821 | 519 |
} |
2 | 520 |
return true; |
521 |
} |
|
522 |
||
20821 | 523 |
private static int deriveNumericScope (byte[] thisAddr, NetworkInterface ifc) throws UnknownHostException { |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
524 |
Enumeration<InetAddress> addresses = ifc.getInetAddresses(); |
2 | 525 |
while (addresses.hasMoreElements()) { |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
526 |
InetAddress addr = addresses.nextElement(); |
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
527 |
if (!(addr instanceof Inet6Address)) { |
2 | 528 |
continue; |
529 |
} |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
530 |
Inet6Address ia6_addr = (Inet6Address)addr; |
2 | 531 |
/* check if site or link local prefixes match */ |
20821 | 532 |
if (!isDifferentLocalAddressType(thisAddr, ia6_addr.getAddress())){ |
2 | 533 |
/* type not the same, so carry on searching */ |
534 |
continue; |
|
535 |
} |
|
536 |
/* found a matching address - return its scope_id */ |
|
20821 | 537 |
return ia6_addr.getScopeId(); |
2 | 538 |
} |
539 |
throw new UnknownHostException ("no scope_id found"); |
|
540 |
} |
|
541 |
||
20821 | 542 |
private int deriveNumericScope (String ifname) throws UnknownHostException { |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
543 |
Enumeration<NetworkInterface> en; |
2 | 544 |
try { |
545 |
en = NetworkInterface.getNetworkInterfaces(); |
|
546 |
} catch (SocketException e) { |
|
20821 | 547 |
throw new UnknownHostException ("could not enumerate local network interfaces"); |
2 | 548 |
} |
549 |
while (en.hasMoreElements()) { |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
550 |
NetworkInterface ifc = en.nextElement(); |
20821 | 551 |
if (ifc.getName().equals (ifname)) { |
552 |
return deriveNumericScope(holder6.ipaddress, ifc); |
|
2 | 553 |
} |
554 |
} |
|
20821 | 555 |
throw new UnknownHostException ("No matching address found for interface : " +ifname); |
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* @serialField ipaddress byte[] |
|
560 |
* @serialField scope_id int |
|
561 |
* @serialField scope_id_set boolean |
|
562 |
* @serialField scope_ifname_set boolean |
|
563 |
* @serialField ifname String |
|
564 |
*/ |
|
565 |
||
566 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
567 |
new ObjectStreamField("ipaddress", byte[].class), |
|
568 |
new ObjectStreamField("scope_id", int.class), |
|
569 |
new ObjectStreamField("scope_id_set", boolean.class), |
|
570 |
new ObjectStreamField("scope_ifname_set", boolean.class), |
|
571 |
new ObjectStreamField("ifname", String.class) |
|
572 |
}; |
|
573 |
||
46873
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
45124
diff
changeset
|
574 |
private static final jdk.internal.misc.Unsafe UNSAFE |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
45124
diff
changeset
|
575 |
= jdk.internal.misc.Unsafe.getUnsafe(); |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
45124
diff
changeset
|
576 |
private static final long FIELDS_OFFSET = UNSAFE.objectFieldOffset( |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
45124
diff
changeset
|
577 |
Inet6Address.class, "holder6"); |
2 | 578 |
|
579 |
/** |
|
580 |
* restore the state of this object from stream |
|
581 |
* including the scope information, only if the |
|
582 |
* scoped interface name is valid on this system |
|
583 |
*/ |
|
584 |
private void readObject(ObjectInputStream s) |
|
585 |
throws IOException, ClassNotFoundException { |
|
20821 | 586 |
NetworkInterface scope_ifname = null; |
16870 | 587 |
|
588 |
if (getClass().getClassLoader() != null) { |
|
589 |
throw new SecurityException ("invalid address type"); |
|
590 |
} |
|
591 |
||
20821 | 592 |
ObjectInputStream.GetField gf = s.readFields(); |
48666
d626620a1844
8194676: NullPointerException is thrown if ipaddress is not set.
vtewari
parents:
47216
diff
changeset
|
593 |
byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]); |
22106 | 594 |
int scope_id = gf.get("scope_id", -1); |
595 |
boolean scope_id_set = gf.get("scope_id_set", false); |
|
596 |
boolean scope_ifname_set = gf.get("scope_ifname_set", false); |
|
20821 | 597 |
String ifname = (String)gf.get("ifname", null); |
2 | 598 |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52499
diff
changeset
|
599 |
if (ifname != null && !ifname.isEmpty()) { |
2 | 600 |
try { |
601 |
scope_ifname = NetworkInterface.getByName(ifname); |
|
20821 | 602 |
if (scope_ifname == null) { |
603 |
/* the interface does not exist on this system, so we clear |
|
604 |
* the scope information completely */ |
|
605 |
scope_id_set = false; |
|
606 |
scope_ifname_set = false; |
|
607 |
scope_id = 0; |
|
608 |
} else { |
|
17198
32db080a2a70
8007373: Inet6Address serialization incompatibility
msheppar
parents:
16870
diff
changeset
|
609 |
scope_ifname_set = true; |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
610 |
try { |
20821 | 611 |
scope_id = deriveNumericScope (ipaddress, scope_ifname); |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
612 |
} catch (UnknownHostException e) { |
6004
f88fd15b64b9
6966846: Incorrect assertion in java.net.Inet6Address.readObject
chegar
parents:
5506
diff
changeset
|
613 |
// typically should not happen, but it may be that |
f88fd15b64b9
6966846: Incorrect assertion in java.net.Inet6Address.readObject
chegar
parents:
5506
diff
changeset
|
614 |
// the machine being used for deserialization has |
f88fd15b64b9
6966846: Incorrect assertion in java.net.Inet6Address.readObject
chegar
parents:
5506
diff
changeset
|
615 |
// the same interface name but without IPv6 configured. |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
616 |
} |
2 | 617 |
} |
618 |
} catch (SocketException e) {} |
|
20821 | 619 |
} |
2 | 620 |
|
621 |
/* if ifname was not supplied, then the numeric info is used */ |
|
622 |
||
623 |
ipaddress = ipaddress.clone(); |
|
624 |
||
625 |
// Check that our invariants are satisfied |
|
626 |
if (ipaddress.length != INADDRSZ) { |
|
627 |
throw new InvalidObjectException("invalid address length: "+ |
|
628 |
ipaddress.length); |
|
629 |
} |
|
630 |
||
20821 | 631 |
if (holder.getFamily() != IPv6) { |
2 | 632 |
throw new InvalidObjectException("invalid address family type"); |
633 |
} |
|
20821 | 634 |
|
635 |
Inet6AddressHolder h = new Inet6AddressHolder( |
|
636 |
ipaddress, scope_id, scope_id_set, scope_ifname, scope_ifname_set |
|
637 |
); |
|
638 |
||
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
48666
diff
changeset
|
639 |
UNSAFE.putReference(this, FIELDS_OFFSET, h); |
20821 | 640 |
} |
641 |
||
642 |
/** |
|
643 |
* default behavior is overridden in order to write the |
|
644 |
* scope_ifname field as a String, rather than a NetworkInterface |
|
645 |
* which is not serializable |
|
646 |
*/ |
|
647 |
private synchronized void writeObject(ObjectOutputStream s) |
|
648 |
throws IOException |
|
649 |
{ |
|
650 |
String ifname = null; |
|
651 |
||
652 |
if (holder6.scope_ifname != null) { |
|
653 |
ifname = holder6.scope_ifname.getName(); |
|
654 |
holder6.scope_ifname_set = true; |
|
655 |
} |
|
656 |
ObjectOutputStream.PutField pfields = s.putFields(); |
|
657 |
pfields.put("ipaddress", holder6.ipaddress); |
|
658 |
pfields.put("scope_id", holder6.scope_id); |
|
659 |
pfields.put("scope_id_set", holder6.scope_id_set); |
|
660 |
pfields.put("scope_ifname_set", holder6.scope_ifname_set); |
|
661 |
pfields.put("ifname", ifname); |
|
662 |
s.writeFields(); |
|
2 | 663 |
} |
664 |
||
665 |
/** |
|
666 |
* Utility routine to check if the InetAddress is an IP multicast |
|
667 |
* address. 11111111 at the start of the address identifies the |
|
668 |
* address as being a multicast address. |
|
669 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
670 |
* @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
|
671 |
* multicast address |
2 | 672 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
673 |
@Override |
2 | 674 |
public boolean isMulticastAddress() { |
20821 | 675 |
return holder6.isMulticastAddress(); |
2 | 676 |
} |
677 |
||
678 |
/** |
|
40118
f63c761ec2d2
8162819: fix minor Javadoc issues and remove warnings in java.net.Socket
clanger
parents:
33674
diff
changeset
|
679 |
* Utility routine to check if the InetAddress is a wildcard address. |
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
680 |
* |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52220
diff
changeset
|
681 |
* @return a {@code boolean} indicating if the InetAddress is |
2 | 682 |
* a wildcard address. |
683 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
684 |
@Override |
2 | 685 |
public boolean isAnyLocalAddress() { |
20821 | 686 |
return holder6.isAnyLocalAddress(); |
2 | 687 |
} |
688 |
||
689 |
/** |
|
690 |
* Utility routine to check if the InetAddress is a loopback address. |
|
691 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
692 |
* @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
|
693 |
* address; or false otherwise. |
2 | 694 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
695 |
@Override |
2 | 696 |
public boolean isLoopbackAddress() { |
20821 | 697 |
return holder6.isLoopbackAddress(); |
2 | 698 |
} |
699 |
||
700 |
/** |
|
701 |
* Utility routine to check if the InetAddress is an link local address. |
|
702 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
703 |
* @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
|
704 |
* address; or false if address is not a link local unicast address. |
2 | 705 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
706 |
@Override |
2 | 707 |
public boolean isLinkLocalAddress() { |
20821 | 708 |
return holder6.isLinkLocalAddress(); |
709 |
} |
|
710 |
||
711 |
/* static version of above */ |
|
712 |
static boolean isLinkLocalAddress(byte[] ipaddress) { |
|
2 | 713 |
return ((ipaddress[0] & 0xff) == 0xfe |
714 |
&& (ipaddress[1] & 0xc0) == 0x80); |
|
715 |
} |
|
716 |
||
717 |
/** |
|
718 |
* Utility routine to check if the InetAddress is a site local address. |
|
719 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
720 |
* @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
|
721 |
* address; or false if address is not a site local unicast address. |
2 | 722 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
723 |
@Override |
2 | 724 |
public boolean isSiteLocalAddress() { |
20821 | 725 |
return holder6.isSiteLocalAddress(); |
726 |
} |
|
727 |
||
728 |
/* static version of above */ |
|
729 |
static boolean isSiteLocalAddress(byte[] ipaddress) { |
|
2 | 730 |
return ((ipaddress[0] & 0xff) == 0xfe |
731 |
&& (ipaddress[1] & 0xc0) == 0xc0); |
|
732 |
} |
|
733 |
||
734 |
/** |
|
735 |
* Utility routine to check if the multicast address has global scope. |
|
736 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
737 |
* @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
|
738 |
* 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
|
739 |
* it is not a multicast address |
2 | 740 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
741 |
@Override |
2 | 742 |
public boolean isMCGlobal() { |
20821 | 743 |
return holder6.isMCGlobal(); |
2 | 744 |
} |
745 |
||
746 |
/** |
|
747 |
* Utility routine to check if the multicast address has node scope. |
|
748 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
749 |
* @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
|
750 |
* 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
|
751 |
* scope or it is not a multicast address |
2 | 752 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
753 |
@Override |
2 | 754 |
public boolean isMCNodeLocal() { |
20821 | 755 |
return holder6.isMCNodeLocal(); |
2 | 756 |
} |
757 |
||
758 |
/** |
|
759 |
* Utility routine to check if the multicast address has link scope. |
|
760 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
761 |
* @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
|
762 |
* 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
|
763 |
* scope or it is not a multicast address |
2 | 764 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
765 |
@Override |
2 | 766 |
public boolean isMCLinkLocal() { |
20821 | 767 |
return holder6.isMCLinkLocal(); |
2 | 768 |
} |
769 |
||
770 |
/** |
|
771 |
* Utility routine to check if the multicast address has site scope. |
|
772 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
773 |
* @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
|
774 |
* 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
|
775 |
* scope or it is not a multicast address |
2 | 776 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
777 |
@Override |
2 | 778 |
public boolean isMCSiteLocal() { |
20821 | 779 |
return holder6.isMCSiteLocal(); |
2 | 780 |
} |
781 |
||
782 |
/** |
|
783 |
* Utility routine to check if the multicast address has organization scope. |
|
784 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
785 |
* @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
|
786 |
* 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
|
787 |
* organization-local scope or it is not a multicast address |
2 | 788 |
*/ |
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
789 |
@Override |
2 | 790 |
public boolean isMCOrgLocal() { |
20821 | 791 |
return holder6.isMCOrgLocal(); |
2 | 792 |
} |
793 |
/** |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
794 |
* 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
|
795 |
* 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
|
796 |
* {@code getAddress()[0]}. |
2 | 797 |
* |
798 |
* @return the raw IP address of this object. |
|
799 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
800 |
@Override |
2 | 801 |
public byte[] getAddress() { |
20821 | 802 |
return holder6.ipaddress.clone(); |
2 | 803 |
} |
804 |
||
805 |
/** |
|
806 |
* Returns the numeric scopeId, if this instance is associated with |
|
807 |
* an interface. If no scoped_id is set, the returned value is zero. |
|
808 |
* |
|
809 |
* @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
|
810 |
* |
2 | 811 |
* @since 1.5 |
812 |
*/ |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
813 |
public int getScopeId() { |
20821 | 814 |
return holder6.scope_id; |
2 | 815 |
} |
816 |
||
817 |
/** |
|
818 |
* Returns the scoped interface, if this instance was created with |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52220
diff
changeset
|
819 |
* a scoped interface. |
2 | 820 |
* |
821 |
* @return the scoped interface, or null if not set. |
|
822 |
* @since 1.5 |
|
823 |
*/ |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
824 |
public NetworkInterface getScopedInterface() { |
20821 | 825 |
return holder6.scope_ifname; |
2 | 826 |
} |
827 |
||
828 |
/** |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
829 |
* 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
|
830 |
* 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
|
831 |
* 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
|
832 |
* 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
|
833 |
* the instance. |
2 | 834 |
* |
835 |
* @return the raw IP address in a string format. |
|
836 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
837 |
@Override |
2 | 838 |
public String getHostAddress() { |
20821 | 839 |
return holder6.getHostAddress(); |
2 | 840 |
} |
841 |
||
842 |
/** |
|
843 |
* Returns a hashcode for this IP address. |
|
844 |
* |
|
845 |
* @return a hash code value for this IP address. |
|
846 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
847 |
@Override |
2 | 848 |
public int hashCode() { |
20821 | 849 |
return holder6.hashCode(); |
2 | 850 |
} |
851 |
||
852 |
/** |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
853 |
* 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
|
854 |
* 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
|
855 |
* the same IP address as this object. |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
856 |
* |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
857 |
* <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
|
858 |
* 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
|
859 |
* 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
|
860 |
* arrays. |
2 | 861 |
* |
862 |
* @param obj the object to compare against. |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
863 |
* |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
864 |
* @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
|
865 |
* |
2 | 866 |
* @see java.net.InetAddress#getAddress() |
867 |
*/ |
|
902
bbfb1e2369b2
6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents:
2
diff
changeset
|
868 |
@Override |
2 | 869 |
public boolean equals(Object obj) { |
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
870 |
if (obj == null || !(obj instanceof Inet6Address)) |
2 | 871 |
return false; |
872 |
||
873 |
Inet6Address inetAddr = (Inet6Address)obj; |
|
874 |
||
20821 | 875 |
return holder6.equals(inetAddr.holder6); |
2 | 876 |
} |
877 |
||
878 |
/** |
|
879 |
* Utility routine to check if the InetAddress is an |
|
880 |
* IPv4 compatible IPv6 address. |
|
881 |
* |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
882 |
* @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
|
883 |
* compatible IPv6 address; or false if address is IPv4 address. |
2 | 884 |
*/ |
885 |
public boolean isIPv4CompatibleAddress() { |
|
20821 | 886 |
return holder6.isIPv4CompatibleAddress(); |
2 | 887 |
} |
888 |
||
889 |
// Utilities |
|
42474 | 890 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
891 |
private static final int INT16SZ = 2; |
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
892 |
|
42474 | 893 |
/** |
2 | 894 |
* Convert IPv6 binary address into presentation (printable) format. |
895 |
* |
|
896 |
* @param src a byte array representing the IPv6 numeric address |
|
897 |
* @return a String representing an IPv6 address in |
|
898 |
* textual representation format |
|
899 |
*/ |
|
14783
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
900 |
static String numericToTextFormat(byte[] src) { |
2ecc993a8e67
8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents:
10596
diff
changeset
|
901 |
StringBuilder sb = new StringBuilder(39); |
2 | 902 |
for (int i = 0; i < (INADDRSZ / INT16SZ); i++) { |
903 |
sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00) |
|
904 |
| (src[(i<<1)+1] & 0xff))); |
|
905 |
if (i < (INADDRSZ / INT16SZ) -1 ) { |
|
906 |
sb.append(":"); |
|
907 |
} |
|
908 |
} |
|
909 |
return sb.toString(); |
|
910 |
} |
|
911 |
||
912 |
/** |
|
913 |
* Perform class load-time initializations. |
|
914 |
*/ |
|
915 |
private static native void init(); |
|
916 |
} |