1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact 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. 24 */ 25 26 package java.net; 27 28 import java.io.IOException; 29 import java.io.InvalidObjectException; 30 import java.io.ObjectInputStream; 31 import java.io.ObjectOutputStream; 32 import java.io.ObjectStreamField; 33 import java.util.Enumeration; 34 import java.util.Arrays; 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 * 41 * <h3> <a id="format">Textual representation of IP addresses</a> </h3> 42 * 43 * Textual representation of IPv6 address used as input to methods 44 * takes one of the following forms: 45 * 46 * <ol> 47 * <li><p> <a id="lform">The preferred form</a> is x:x:x:x:x:x:x:x, 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 * 52 * <blockquote><ul style="list-style-type:none"> 53 * <li>{@code 1080:0:0:0:8:800:200C:417A}</li> 54 * </ul></blockquote> 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 * 69 * <blockquote><ul style="list-style-type:none"> 70 * <li>{@code 1080::8:800:200C:417A}</li> 71 * </ul></blockquote> 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 * 80 * <blockquote><ul style="list-style-type:none"> 81 * <li>{@code ::FFFF:129.144.52.38}</li> 82 * <li>{@code ::129.144.52.38}</li> 83 * </ul></blockquote> 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 * 90 * <blockquote><ul style="list-style-type:none"> 91 * <li>{@code ::FFFF:d.d.d}</li> 92 * <li>{@code ::FFFF:d.d}</li> 93 * <li>{@code ::d.d.d}</li> 94 * <li>{@code ::d.d}</li> 95 * </ul></blockquote> 96 * 97 * <p> The following form: 98 * 99 * <blockquote><ul style="list-style-type:none"> 100 * <li>{@code ::FFFF:d}</li> 101 * </ul></blockquote> 102 * 103 * <p> is valid, however it is an unconventional representation of 104 * the IPv4-compatible IPv6 address, 105 * 106 * <blockquote><ul style="list-style-type:none"> 107 * <li>{@code ::255.255.0.d}</li> 108 * </ul></blockquote> 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 * 119 * <h4> Special IPv6 address </h4> 120 * 121 * <blockquote> 122 * <table class="borderless"> 123 * <caption style="display:none">Description of IPv4-mapped address</caption> 124 * <tr><th style="vertical-align:top; padding-right:2px"><i>IPv4-mapped address</i></th> 125 * <td>Of the form ::ffff:w.x.y.z, this IPv6 address is used to 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> 137 * 138 * <h4><a id="scoped">Textual representation of IPv6 scoped addresses</a></h4> 139 * 140 * <p> The textual representation of IPv6 addresses as described above can be 141 * extended to specify IPv6 scoped addresses. This extension to the basic 142 * addressing architecture is described in 143 * <a href="https://tools.ietf.org/html/rfc4007"> 144 * <i>RFC 4007: IPv6 Scoped Address Architecture</i></a>. 145 * 146 * <p> Because link-local and site-local addresses are non-global, it is possible 147 * that different hosts may have the same destination address and may be 148 * reachable through different interfaces on the same originating system. In 149 * this case, the originating system is said to be connected to multiple zones 150 * of the same scope. In order to disambiguate which is the intended destination 151 * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an 152 * IPv6 address. 153 * 154 * <p> The general format for specifying the <i>scope_id</i> is the following: 155 * 156 * <blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote> 157 * <p> The IPv6-address is a literal IPv6 address as described above. 158 * The <i>scope_id</i> refers to an interface on the local system, and it can be 159 * specified in two ways. 160 * <ol><li><i>As a numeric identifier.</i> This must be a positive integer 161 * that identifies the particular interface and scope as understood by the 162 * system. Usually, the numeric values can be determined through administration 163 * tools on the system. Each interface may have multiple values, one for each 164 * scope. If the scope is unspecified, then the default value used is zero.</li> 165 * <li><i>As a string.</i> This must be the exact string that is returned by 166 * {@link java.net.NetworkInterface#getName()} for the particular interface in 167 * question. When an Inet6Address is created in this way, the numeric scope-id 168 * is determined at the time the object is created by querying the relevant 169 * NetworkInterface.</li></ol> 170 * 171 * <p> Note also, that the numeric <i>scope_id</i> can be retrieved from 172 * Inet6Address instances returned from the NetworkInterface class. This can be 173 * used to find out the current scope ids configured on the system. 174 * @since 1.4 175 */ 176 177 public final 178 class Inet6Address extends InetAddress { 179 static final int INADDRSZ = 16; 180 181 /* 182 * cached scope_id - for link-local address use only. 183 */ 184 private transient int cached_scope_id; // 0 185 186 private class Inet6AddressHolder { 187 188 private Inet6AddressHolder() { 189 ipaddress = new byte[INADDRSZ]; 190 } 191 192 private Inet6AddressHolder( 193 byte[] ipaddress, int scope_id, boolean scope_id_set, 194 NetworkInterface ifname, boolean scope_ifname_set) 195 { 196 this.ipaddress = ipaddress; 197 this.scope_id = scope_id; 198 this.scope_id_set = scope_id_set; 199 this.scope_ifname_set = scope_ifname_set; 200 this.scope_ifname = ifname; 201 } 202 203 /** 204 * Holds a 128-bit (16 bytes) IPv6 address. 205 */ 206 byte[] ipaddress; 207 208 /** 209 * scope_id. The scope specified when the object is created. If the object 210 * is created with an interface name, then the scope_id is not determined 211 * until the time it is needed. 212 */ 213 int scope_id; // 0 214 215 /** 216 * This will be set to true when the scope_id field contains a valid 217 * integer scope_id. 218 */ 219 boolean scope_id_set; // false 220 221 /** 222 * scoped interface. scope_id is derived from this as the scope_id of the first 223 * address whose scope is the same as this address for the named interface. 224 */ 225 NetworkInterface scope_ifname; // null 226 227 /** 228 * set if the object is constructed with a scoped 229 * interface instead of a numeric scope id. 230 */ 231 boolean scope_ifname_set; // false; 232 233 void setAddr(byte addr[]) { 234 if (addr.length == INADDRSZ) { // normal IPv6 address 235 System.arraycopy(addr, 0, ipaddress, 0, INADDRSZ); 236 } 237 } 238 239 void init(byte addr[], int scope_id) { 240 setAddr(addr); 241 242 if (scope_id >= 0) { 243 this.scope_id = scope_id; 244 this.scope_id_set = true; 245 } 246 } 247 248 void init(byte addr[], NetworkInterface nif) 249 throws UnknownHostException 250 { 251 setAddr(addr); 252 253 if (nif != null) { 254 this.scope_id = deriveNumericScope(ipaddress, nif); 255 this.scope_id_set = true; 256 this.scope_ifname = nif; 257 this.scope_ifname_set = true; 258 } 259 } 260 261 String getHostAddress() { 262 String s = numericToTextFormat(ipaddress); 263 if (scope_ifname != null) { /* must check this first */ 264 s = s + "%" + scope_ifname.getName(); 265 } else if (scope_id_set) { 266 s = s + "%" + scope_id; 267 } 268 return s; 269 } 270 271 public boolean equals(Object o) { 272 if (! (o instanceof Inet6AddressHolder)) { 273 return false; 274 } 275 Inet6AddressHolder that = (Inet6AddressHolder)o; 276 277 return Arrays.equals(this.ipaddress, that.ipaddress); 278 } 279 280 public int hashCode() { 281 if (ipaddress != null) { 282 283 int hash = 0; 284 int i=0; 285 while (i<INADDRSZ) { 286 int j=0; 287 int component=0; 288 while (j<4 && i<INADDRSZ) { 289 component = (component << 8) + ipaddress[i]; 290 j++; 291 i++; 292 } 293 hash += component; 294 } 295 return hash; 296 297 } else { 298 return 0; 299 } 300 } 301 302 boolean isIPv4CompatibleAddress() { 303 if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) && 304 (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) && 305 (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) && 306 (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) && 307 (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) && 308 (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00)) { 309 return true; 310 } 311 return false; 312 } 313 314 boolean isMulticastAddress() { 315 return ((ipaddress[0] & 0xff) == 0xff); 316 } 317 318 boolean isAnyLocalAddress() { 319 byte test = 0x00; 320 for (int i = 0; i < INADDRSZ; i++) { 321 test |= ipaddress[i]; 322 } 323 return (test == 0x00); 324 } 325 326 boolean isLoopbackAddress() { 327 byte test = 0x00; 328 for (int i = 0; i < 15; i++) { 329 test |= ipaddress[i]; 330 } 331 return (test == 0x00) && (ipaddress[15] == 0x01); 332 } 333 334 boolean isLinkLocalAddress() { 335 return ((ipaddress[0] & 0xff) == 0xfe 336 && (ipaddress[1] & 0xc0) == 0x80); 337 } 338 339 340 boolean isSiteLocalAddress() { 341 return ((ipaddress[0] & 0xff) == 0xfe 342 && (ipaddress[1] & 0xc0) == 0xc0); 343 } 344 345 boolean isMCGlobal() { 346 return ((ipaddress[0] & 0xff) == 0xff 347 && (ipaddress[1] & 0x0f) == 0x0e); 348 } 349 350 boolean isMCNodeLocal() { 351 return ((ipaddress[0] & 0xff) == 0xff 352 && (ipaddress[1] & 0x0f) == 0x01); 353 } 354 355 boolean isMCLinkLocal() { 356 return ((ipaddress[0] & 0xff) == 0xff 357 && (ipaddress[1] & 0x0f) == 0x02); 358 } 359 360 boolean isMCSiteLocal() { 361 return ((ipaddress[0] & 0xff) == 0xff 362 && (ipaddress[1] & 0x0f) == 0x05); 363 } 364 365 boolean isMCOrgLocal() { 366 return ((ipaddress[0] & 0xff) == 0xff 367 && (ipaddress[1] & 0x0f) == 0x08); 368 } 369 } 370 371 private final transient Inet6AddressHolder holder6; 372 373 private static final long serialVersionUID = 6880410070516793377L; 374 375 // Perform native initialization 376 static { init(); } 377 378 Inet6Address() { 379 super(); 380 holder.init(null, IPv6); 381 holder6 = new Inet6AddressHolder(); 382 } 383 384 /* checking of value for scope_id should be done by caller 385 * scope_id must be >= 0, or -1 to indicate not being set 386 */ 387 Inet6Address(String hostName, byte addr[], int scope_id) { 388 holder.init(hostName, IPv6); 389 holder6 = new Inet6AddressHolder(); 390 holder6.init(addr, scope_id); 391 } 392 393 Inet6Address(String hostName, byte addr[]) { 394 holder6 = new Inet6AddressHolder(); 395 try { 396 initif (hostName, addr, null); 397 } catch (UnknownHostException e) {} /* cant happen if ifname is null */ 398 } 399 400 Inet6Address (String hostName, byte addr[], NetworkInterface nif) 401 throws UnknownHostException 402 { 403 holder6 = new Inet6AddressHolder(); 404 initif (hostName, addr, nif); 405 } 406 407 Inet6Address (String hostName, byte addr[], String ifname) 408 throws UnknownHostException 409 { 410 holder6 = new Inet6AddressHolder(); 411 initstr (hostName, addr, ifname); 412 } 413 414 /** 415 * Create an Inet6Address in the exact manner of {@link 416 * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is 417 * set to the value corresponding to the given interface for the address 418 * type specified in {@code addr}. The call will fail with an 419 * UnknownHostException if the given interface does not have a numeric 420 * scope_id assigned for the given address type (eg. link-local or site-local). 421 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 422 * scoped addresses. 423 * 424 * @param host the specified host 425 * @param addr the raw IP address in network byte order 426 * @param nif an interface this address must be associated with. 427 * @return an Inet6Address object created from the raw IP address. 428 * @throws UnknownHostException 429 * if IP address is of illegal length, or if the interface does not 430 * have a numeric scope_id assigned for the given address type. 431 * 432 * @since 1.5 433 */ 434 public static Inet6Address getByAddress(String host, byte[] addr, 435 NetworkInterface nif) 436 throws UnknownHostException 437 { 438 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 439 if (host.charAt(host.length()-1) == ']') { 440 host = host.substring(1, host.length() -1); 441 } 442 } 443 if (addr != null) { 444 if (addr.length == Inet6Address.INADDRSZ) { 445 return new Inet6Address(host, addr, nif); 446 } 447 } 448 throw new UnknownHostException("addr is of illegal length"); 449 } 450 451 /** 452 * Create an Inet6Address in the exact manner of {@link 453 * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is 454 * set to the given numeric value. The scope_id is not checked to determine 455 * if it corresponds to any interface on the system. 456 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 457 * scoped addresses. 458 * 459 * @param host the specified host 460 * @param addr the raw IP address in network byte order 461 * @param scope_id the numeric scope_id for the address. 462 * @return an Inet6Address object created from the raw IP address. 463 * @throws UnknownHostException if IP address is of illegal length. 464 * 465 * @since 1.5 466 */ 467 public static Inet6Address getByAddress(String host, byte[] addr, 468 int scope_id) 469 throws UnknownHostException 470 { 471 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 472 if (host.charAt(host.length()-1) == ']') { 473 host = host.substring(1, host.length() -1); 474 } 475 } 476 if (addr != null) { 477 if (addr.length == Inet6Address.INADDRSZ) { 478 return new Inet6Address(host, addr, scope_id); 479 } 480 } 481 throw new UnknownHostException("addr is of illegal length"); 482 } 483 484 private void initstr(String hostName, byte addr[], String ifname) 485 throws UnknownHostException 486 { 487 try { 488 NetworkInterface nif = NetworkInterface.getByName (ifname); 489 if (nif == null) { 490 throw new UnknownHostException ("no such interface " + ifname); 491 } 492 initif (hostName, addr, nif); 493 } catch (SocketException e) { 494 throw new UnknownHostException ("SocketException thrown" + ifname); 495 } 496 } 497 498 private void initif(String hostName, byte addr[], NetworkInterface nif) 499 throws UnknownHostException 500 { 501 int family = -1; 502 holder6.init(addr, nif); 503 504 if (addr.length == INADDRSZ) { // normal IPv6 address 505 family = IPv6; 506 } 507 holder.init(hostName, family); 508 } 509 510 /* check the two Ipv6 addresses and return false if they are both 511 * non global address types, but not the same. 512 * (i.e. one is site-local and the other link-local) 513 * return true otherwise. 514 */ 515 516 private static boolean isDifferentLocalAddressType( 517 byte[] thisAddr, byte[] otherAddr) { 518 519 if (Inet6Address.isLinkLocalAddress(thisAddr) && 520 !Inet6Address.isLinkLocalAddress(otherAddr)) { 521 return false; 522 } 523 if (Inet6Address.isSiteLocalAddress(thisAddr) && 524 !Inet6Address.isSiteLocalAddress(otherAddr)) { 525 return false; 526 } 527 return true; 528 } 529 530 private static int deriveNumericScope (byte[] thisAddr, NetworkInterface ifc) throws UnknownHostException { 531 Enumeration<InetAddress> addresses = ifc.getInetAddresses(); 532 while (addresses.hasMoreElements()) { 533 InetAddress addr = addresses.nextElement(); 534 if (!(addr instanceof Inet6Address)) { 535 continue; 536 } 537 Inet6Address ia6_addr = (Inet6Address)addr; 538 /* check if site or link local prefixes match */ 539 if (!isDifferentLocalAddressType(thisAddr, ia6_addr.getAddress())){ 540 /* type not the same, so carry on searching */ 541 continue; 542 } 543 /* found a matching address - return its scope_id */ 544 return ia6_addr.getScopeId(); 545 } 546 throw new UnknownHostException ("no scope_id found"); 547 } 548 549 private int deriveNumericScope (String ifname) throws UnknownHostException { 550 Enumeration<NetworkInterface> en; 551 try { 552 en = NetworkInterface.getNetworkInterfaces(); 553 } catch (SocketException e) { 554 throw new UnknownHostException ("could not enumerate local network interfaces"); 555 } 556 while (en.hasMoreElements()) { 557 NetworkInterface ifc = en.nextElement(); 558 if (ifc.getName().equals (ifname)) { 559 return deriveNumericScope(holder6.ipaddress, ifc); 560 } 561 } 562 throw new UnknownHostException ("No matching address found for interface : " +ifname); 563 } 564 565 /** 566 * @serialField ipaddress byte[] 567 * @serialField scope_id int 568 * @serialField scope_id_set boolean 569 * @serialField scope_ifname_set boolean 570 * @serialField ifname String 571 */ 572 573 private static final ObjectStreamField[] serialPersistentFields = { 574 new ObjectStreamField("ipaddress", byte[].class), 575 new ObjectStreamField("scope_id", int.class), 576 new ObjectStreamField("scope_id_set", boolean.class), 577 new ObjectStreamField("scope_ifname_set", boolean.class), 578 new ObjectStreamField("ifname", String.class) 579 }; 580 581 private static final jdk.internal.misc.Unsafe UNSAFE 582 = jdk.internal.misc.Unsafe.getUnsafe(); 583 private static final long FIELDS_OFFSET = UNSAFE.objectFieldOffset( 584 Inet6Address.class, "holder6"); 585 586 /** 587 * restore the state of this object from stream 588 * including the scope information, only if the 589 * scoped interface name is valid on this system 590 */ 591 private void readObject(ObjectInputStream s) 592 throws IOException, ClassNotFoundException { 593 NetworkInterface scope_ifname = null; 594 595 if (getClass().getClassLoader() != null) { 596 throw new SecurityException ("invalid address type"); 597 } 598 599 ObjectInputStream.GetField gf = s.readFields(); 600 byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]); 601 int scope_id = gf.get("scope_id", -1); 602 boolean scope_id_set = gf.get("scope_id_set", false); 603 boolean scope_ifname_set = gf.get("scope_ifname_set", false); 604 String ifname = (String)gf.get("ifname", null); 605 606 if (ifname != null && !"".equals (ifname)) { 607 try { 608 scope_ifname = NetworkInterface.getByName(ifname); 609 if (scope_ifname == null) { 610 /* the interface does not exist on this system, so we clear 611 * the scope information completely */ 612 scope_id_set = false; 613 scope_ifname_set = false; 614 scope_id = 0; 615 } else { 616 scope_ifname_set = true; 617 try { 618 scope_id = deriveNumericScope (ipaddress, scope_ifname); 619 } catch (UnknownHostException e) { 620 // typically should not happen, but it may be that 621 // the machine being used for deserialization has 622 // the same interface name but without IPv6 configured. 623 } 624 } 625 } catch (SocketException e) {} 626 } 627 628 /* if ifname was not supplied, then the numeric info is used */ 629 630 ipaddress = ipaddress.clone(); 631 632 // Check that our invariants are satisfied 633 if (ipaddress.length != INADDRSZ) { 634 throw new InvalidObjectException("invalid address length: "+ 635 ipaddress.length); 636 } 637 638 if (holder.getFamily() != IPv6) { 639 throw new InvalidObjectException("invalid address family type"); 640 } 641 642 Inet6AddressHolder h = new Inet6AddressHolder( 643 ipaddress, scope_id, scope_id_set, scope_ifname, scope_ifname_set 644 ); 645 646 UNSAFE.putReference(this, FIELDS_OFFSET, h); 647 } 648 649 /** 650 * default behavior is overridden in order to write the 651 * scope_ifname field as a String, rather than a NetworkInterface 652 * which is not serializable 653 */ 654 private synchronized void writeObject(ObjectOutputStream s) 655 throws IOException 656 { 657 String ifname = null; 658 659 if (holder6.scope_ifname != null) { 660 ifname = holder6.scope_ifname.getName(); 661 holder6.scope_ifname_set = true; 662 } 663 ObjectOutputStream.PutField pfields = s.putFields(); 664 pfields.put("ipaddress", holder6.ipaddress); 665 pfields.put("scope_id", holder6.scope_id); 666 pfields.put("scope_id_set", holder6.scope_id_set); 667 pfields.put("scope_ifname_set", holder6.scope_ifname_set); 668 pfields.put("ifname", ifname); 669 s.writeFields(); 670 } 671 672 /** 673 * Utility routine to check if the InetAddress is an IP multicast 674 * address. 11111111 at the start of the address identifies the 675 * address as being a multicast address. 676 * 677 * @return a {@code boolean} indicating if the InetAddress is an IP 678 * multicast address 679 */ 680 @Override 681 public boolean isMulticastAddress() { 682 return holder6.isMulticastAddress(); 683 } 684 685 /** 686 * Utility routine to check if the InetAddress is a wildcard address. 687 * 688 * @return a {@code boolean} indicating if the InetAddress is 689 * a wildcard address. 690 */ 691 @Override 692 public boolean isAnyLocalAddress() { 693 return holder6.isAnyLocalAddress(); 694 } 695 696 /** 697 * Utility routine to check if the InetAddress is a loopback address. 698 * 699 * @return a {@code boolean} indicating if the InetAddress is a loopback 700 * address; or false otherwise. 701 */ 702 @Override 703 public boolean isLoopbackAddress() { 704 return holder6.isLoopbackAddress(); 705 } 706 707 /** 708 * Utility routine to check if the InetAddress is an link local address. 709 * 710 * @return a {@code boolean} indicating if the InetAddress is a link local 711 * address; or false if address is not a link local unicast address. 712 */ 713 @Override 714 public boolean isLinkLocalAddress() { 715 return holder6.isLinkLocalAddress(); 716 } 717 718 /* static version of above */ 719 static boolean isLinkLocalAddress(byte[] ipaddress) { 720 return ((ipaddress[0] & 0xff) == 0xfe 721 && (ipaddress[1] & 0xc0) == 0x80); 722 } 723 724 /** 725 * Utility routine to check if the InetAddress is a site local address. 726 * 727 * @return a {@code boolean} indicating if the InetAddress is a site local 728 * address; or false if address is not a site local unicast address. 729 */ 730 @Override 731 public boolean isSiteLocalAddress() { 732 return holder6.isSiteLocalAddress(); 733 } 734 735 /* static version of above */ 736 static boolean isSiteLocalAddress(byte[] ipaddress) { 737 return ((ipaddress[0] & 0xff) == 0xfe 738 && (ipaddress[1] & 0xc0) == 0xc0); 739 } 740 741 /** 742 * Utility routine to check if the multicast address has global scope. 743 * 744 * @return a {@code boolean} indicating if the address has is a multicast 745 * address of global scope, false if it is not of global scope or 746 * it is not a multicast address 747 */ 748 @Override 749 public boolean isMCGlobal() { 750 return holder6.isMCGlobal(); 751 } 752 753 /** 754 * Utility routine to check if the multicast address has node scope. 755 * 756 * @return a {@code boolean} indicating if the address has is a multicast 757 * address of node-local scope, false if it is not of node-local 758 * scope or it is not a multicast address 759 */ 760 @Override 761 public boolean isMCNodeLocal() { 762 return holder6.isMCNodeLocal(); 763 } 764 765 /** 766 * Utility routine to check if the multicast address has link scope. 767 * 768 * @return a {@code boolean} indicating if the address has is a multicast 769 * address of link-local scope, false if it is not of link-local 770 * scope or it is not a multicast address 771 */ 772 @Override 773 public boolean isMCLinkLocal() { 774 return holder6.isMCLinkLocal(); 775 } 776 777 /** 778 * Utility routine to check if the multicast address has site scope. 779 * 780 * @return a {@code boolean} indicating if the address has is a multicast 781 * address of site-local scope, false if it is not of site-local 782 * scope or it is not a multicast address 783 */ 784 @Override 785 public boolean isMCSiteLocal() { 786 return holder6.isMCSiteLocal(); 787 } 788 789 /** 790 * Utility routine to check if the multicast address has organization scope. 791 * 792 * @return a {@code boolean} indicating if the address has is a multicast 793 * address of organization-local scope, false if it is not of 794 * organization-local scope or it is not a multicast address 795 */ 796 @Override 797 public boolean isMCOrgLocal() { 798 return holder6.isMCOrgLocal(); 799 } 800 /** 801 * Returns the raw IP address of this {@code InetAddress} object. The result 802 * is in network byte order: the highest order byte of the address is in 803 * {@code getAddress()[0]}. 804 * 805 * @return the raw IP address of this object. 806 */ 807 @Override 808 public byte[] getAddress() { 809 return holder6.ipaddress.clone(); 810 } 811 812 /** 813 * Returns the numeric scopeId, if this instance is associated with 814 * an interface. If no scoped_id is set, the returned value is zero. 815 * 816 * @return the scopeId, or zero if not set. 817 * 818 * @since 1.5 819 */ 820 public int getScopeId() { 821 return holder6.scope_id; 822 } 823 824 /** 825 * Returns the scoped interface, if this instance was created with 826 * a scoped interface. 827 * 828 * @return the scoped interface, or null if not set. 829 * @since 1.5 830 */ 831 public NetworkInterface getScopedInterface() { 832 return holder6.scope_ifname; 833 } 834 835 /** 836 * Returns the IP address string in textual presentation. If the instance 837 * was created specifying a scope identifier then the scope id is appended 838 * to the IP address preceded by a "%" (per-cent) character. This can be 839 * either a numeric value or a string, depending on which was used to create 840 * the instance. 841 * 842 * @return the raw IP address in a string format. 843 */ 844 @Override 845 public String getHostAddress() { 846 return holder6.getHostAddress(); 847 } 848 849 /** 850 * Returns a hashcode for this IP address. 851 * 852 * @return a hash code value for this IP address. 853 */ 854 @Override 855 public int hashCode() { 856 return holder6.hashCode(); 857 } 858 859 /** 860 * Compares this object against the specified object. The result is {@code 861 * true} if and only if the argument is not {@code null} and it represents 862 * the same IP address as this object. 863 * 864 * <p> Two instances of {@code InetAddress} represent the same IP address 865 * if the length of the byte arrays returned by {@code getAddress} is the 866 * same for both, and each of the array components is the same for the byte 867 * arrays. 868 * 869 * @param obj the object to compare against. 870 * 871 * @return {@code true} if the objects are the same; {@code false} otherwise. 872 * 873 * @see java.net.InetAddress#getAddress() 874 */ 875 @Override 876 public boolean equals(Object obj) { 877 if (obj == null || !(obj instanceof Inet6Address)) 878 return false; 879 880 Inet6Address inetAddr = (Inet6Address)obj; 881 882 return holder6.equals(inetAddr.holder6); 883 } 884 885 /** 886 * Utility routine to check if the InetAddress is an 887 * IPv4 compatible IPv6 address. 888 * 889 * @return a {@code boolean} indicating if the InetAddress is an IPv4 890 * compatible IPv6 address; or false if address is IPv4 address. 891 */ 892 public boolean isIPv4CompatibleAddress() { 893 return holder6.isIPv4CompatibleAddress(); 894 } 895 896 // Utilities 897 898 private static final int INT16SZ = 2; 899 900 /** 901 * Convert IPv6 binary address into presentation (printable) format. 902 * 903 * @param src a byte array representing the IPv6 numeric address 904 * @return a String representing an IPv6 address in 905 * textual representation format 906 */ 907 static String numericToTextFormat(byte[] src) { 908 StringBuilder sb = new StringBuilder(39); 909 for (int i = 0; i < (INADDRSZ / INT16SZ); i++) { 910 sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00) 911 | (src[(i<<1)+1] & 0xff))); 912 if (i < (INADDRSZ / INT16SZ) -1 ) { 913 sb.append(":"); 914 } 915 } 916 return sb.toString(); 917 } 918 919 /** 920 * Perform class load-time initializations. 921 */ 922 private static native void init(); 923 }