Path: blob/master/src/java.base/share/classes/java/net/Inet6Address.java
41152 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;28import java.io.InvalidObjectException;29import java.io.ObjectInputStream;30import java.io.ObjectOutputStream;31import java.io.ObjectStreamField;32import java.util.Enumeration;33import java.util.Arrays;3435/**36* This class represents an Internet Protocol version 6 (IPv6) address.37* Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt">38* <i>RFC 2373: IP Version 6 Addressing Architecture</i></a>.39*40* <h2> <a id="format">Textual representation of IP addresses</a> </h2>41*42* Textual representation of IPv6 address used as input to methods43* takes one of the following forms:44*45* <ol>46* <li><p> <a id="lform">The preferred form</a> is x:x:x:x:x:x:x:x,47* where the 'x's are48* the hexadecimal values of the eight 16-bit pieces of the49* address. This is the full form. For example,50*51* <blockquote><ul style="list-style-type:none">52* <li>{@code 1080:0:0:0:8:800:200C:417A}</li>53* </ul></blockquote>54*55* <p> Note that it is not necessary to write the leading zeros in56* an individual field. However, there must be at least one numeral57* in every field, except as described below.</li>58*59* <li><p> Due to some methods of allocating certain styles of IPv660* addresses, it will be common for addresses to contain long61* strings of zero bits. In order to make writing addresses62* containing zero bits easier, a special syntax is available to63* compress the zeros. The use of "::" indicates multiple groups64* of 16-bits of zeros. The "::" can only appear once in an address.65* The "::" can also be used to compress the leading and/or trailing66* zeros in an address. For example,67*68* <blockquote><ul style="list-style-type:none">69* <li>{@code 1080::8:800:200C:417A}</li>70* </ul></blockquote>71*72* <li><p> An alternative form that is sometimes more convenient73* when dealing with a mixed environment of IPv4 and IPv6 nodes is74* x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values75* of the six high-order 16-bit pieces of the address, and the 'd's76* are the decimal values of the four low-order 8-bit pieces of the77* standard IPv4 representation address, for example,78*79* <blockquote><ul style="list-style-type:none">80* <li>{@code ::FFFF:129.144.52.38}</li>81* <li>{@code ::129.144.52.38}</li>82* </ul></blockquote>83*84* <p> where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the85* general forms of an IPv4-mapped IPv6 address and an86* IPv4-compatible IPv6 address. Note that the IPv4 portion must be87* in the "d.d.d.d" form. The following forms are invalid:88*89* <blockquote><ul style="list-style-type:none">90* <li>{@code ::FFFF:d.d.d}</li>91* <li>{@code ::FFFF:d.d}</li>92* <li>{@code ::d.d.d}</li>93* <li>{@code ::d.d}</li>94* </ul></blockquote>95*96* <p> The following form:97*98* <blockquote><ul style="list-style-type:none">99* <li>{@code ::FFFF:d}</li>100* </ul></blockquote>101*102* <p> is valid, however it is an unconventional representation of103* the IPv4-compatible IPv6 address,104*105* <blockquote><ul style="list-style-type:none">106* <li>{@code ::255.255.0.d}</li>107* </ul></blockquote>108*109* <p> while "::d" corresponds to the general IPv6 address110* "0:0:0:0:0:0:0:d".</li>111* </ol>112*113* <p> For methods that return a textual representation as output114* value, the full form is used. Inet6Address will return the full115* form because it is unambiguous when used in combination with other116* textual data.117*118* <h3> Special IPv6 address </h3>119*120* <blockquote>121* <table class="borderless">122* <caption style="display:none">Description of IPv4-mapped address</caption>123* <tr><th style="vertical-align:top; padding-right:2px"><i>IPv4-mapped address</i></th>124* <td>Of the form ::ffff:w.x.y.z, this IPv6 address is used to125* represent an IPv4 address. It allows the native program to126* use the same address data structure and also the same127* socket when communicating with both IPv4 and IPv6 nodes.128*129* <p>In InetAddress and Inet6Address, it is used for internal130* representation; it has no functional role. Java will never131* return an IPv4-mapped address. These classes can take an132* IPv4-mapped address as input, both in byte array and text133* representation. However, it will be converted into an IPv4134* address.</td></tr>135* </table></blockquote>136*137* <h3><a id="scoped">Textual representation of IPv6 scoped addresses</a></h3>138*139* <p> The textual representation of IPv6 addresses as described above can be140* extended to specify IPv6 scoped addresses. This extension to the basic141* addressing architecture is described in [draft-ietf-ipngwg-scoping-arch-04.txt].142*143* <p> Because link-local and site-local addresses are non-global, it is possible144* that different hosts may have the same destination address and may be145* reachable through different interfaces on the same originating system. In146* this case, the originating system is said to be connected to multiple zones147* of the same scope. In order to disambiguate which is the intended destination148* zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an149* IPv6 address.150*151* <p> The general format for specifying the <i>scope_id</i> is the following:152*153* <blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote>154* <p> The IPv6-address is a literal IPv6 address as described above.155* The <i>scope_id</i> refers to an interface on the local system, and it can be156* specified in two ways.157* <ol><li><i>As a numeric identifier.</i> This must be a positive integer158* that identifies the particular interface and scope as understood by the159* system. Usually, the numeric values can be determined through administration160* tools on the system. Each interface may have multiple values, one for each161* scope. If the scope is unspecified, then the default value used is zero.</li>162* <li><i>As a string.</i> This must be the exact string that is returned by163* {@link java.net.NetworkInterface#getName()} for the particular interface in164* question. When an Inet6Address is created in this way, the numeric scope-id165* is determined at the time the object is created by querying the relevant166* NetworkInterface.</li></ol>167*168* <p> Note also, that the numeric <i>scope_id</i> can be retrieved from169* Inet6Address instances returned from the NetworkInterface class. This can be170* used to find out the current scope ids configured on the system.171* @since 1.4172*/173174public final175class Inet6Address extends InetAddress {176static final int INADDRSZ = 16;177178private static class Inet6AddressHolder {179180private Inet6AddressHolder() {181ipaddress = new byte[INADDRSZ];182}183184private Inet6AddressHolder(185byte[] ipaddress, int scope_id, boolean scope_id_set,186NetworkInterface ifname, boolean scope_ifname_set)187{188this.ipaddress = ipaddress;189this.scope_id = scope_id;190this.scope_id_set = scope_id_set;191this.scope_ifname_set = scope_ifname_set;192this.scope_ifname = ifname;193}194195/**196* Holds a 128-bit (16 bytes) IPv6 address.197*/198byte[] ipaddress;199200/**201* scope_id. The scope specified when the object is created. If the object202* is created with an interface name, then the scope_id is not determined203* until the time it is needed.204*/205int scope_id; // 0206207/**208* This will be set to true when the scope_id field contains a valid209* integer scope_id.210*/211boolean scope_id_set; // false212213/**214* scoped interface. scope_id is derived from this as the scope_id of the first215* address whose scope is the same as this address for the named interface.216*/217NetworkInterface scope_ifname; // null218219/**220* set if the object is constructed with a scoped221* interface instead of a numeric scope id.222*/223boolean scope_ifname_set; // false;224225void setAddr(byte addr[]) {226if (addr.length == INADDRSZ) { // normal IPv6 address227System.arraycopy(addr, 0, ipaddress, 0, INADDRSZ);228}229}230231void init(byte addr[], int scope_id) {232setAddr(addr);233234if (scope_id >= 0) {235this.scope_id = scope_id;236this.scope_id_set = true;237}238}239240void init(byte addr[], NetworkInterface nif)241throws UnknownHostException242{243setAddr(addr);244245if (nif != null) {246this.scope_id = deriveNumericScope(ipaddress, nif);247this.scope_id_set = true;248this.scope_ifname = nif;249this.scope_ifname_set = true;250}251}252253String getHostAddress() {254String s = numericToTextFormat(ipaddress);255if (scope_ifname != null) { /* must check this first */256s = s + "%" + scope_ifname.getName();257} else if (scope_id_set) {258s = s + "%" + scope_id;259}260return s;261}262263public boolean equals(Object o) {264if (!(o instanceof Inet6AddressHolder that)) {265return false;266}267268return Arrays.equals(this.ipaddress, that.ipaddress);269}270271public int hashCode() {272if (ipaddress != null) {273274int hash = 0;275int i=0;276while (i<INADDRSZ) {277int j=0;278int component=0;279while (j<4 && i<INADDRSZ) {280component = (component << 8) + ipaddress[i];281j++;282i++;283}284hash += component;285}286return hash;287288} else {289return 0;290}291}292293boolean isIPv4CompatibleAddress() {294if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&295(ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&296(ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&297(ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&298(ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&299(ipaddress[10] == 0x00) && (ipaddress[11] == 0x00)) {300return true;301}302return false;303}304305boolean isMulticastAddress() {306return ((ipaddress[0] & 0xff) == 0xff);307}308309boolean isAnyLocalAddress() {310byte test = 0x00;311for (int i = 0; i < INADDRSZ; i++) {312test |= ipaddress[i];313}314return (test == 0x00);315}316317boolean isLoopbackAddress() {318byte test = 0x00;319for (int i = 0; i < 15; i++) {320test |= ipaddress[i];321}322return (test == 0x00) && (ipaddress[15] == 0x01);323}324325boolean isLinkLocalAddress() {326return ((ipaddress[0] & 0xff) == 0xfe327&& (ipaddress[1] & 0xc0) == 0x80);328}329330331boolean isSiteLocalAddress() {332return ((ipaddress[0] & 0xff) == 0xfe333&& (ipaddress[1] & 0xc0) == 0xc0);334}335336boolean isMCGlobal() {337return ((ipaddress[0] & 0xff) == 0xff338&& (ipaddress[1] & 0x0f) == 0x0e);339}340341boolean isMCNodeLocal() {342return ((ipaddress[0] & 0xff) == 0xff343&& (ipaddress[1] & 0x0f) == 0x01);344}345346boolean isMCLinkLocal() {347return ((ipaddress[0] & 0xff) == 0xff348&& (ipaddress[1] & 0x0f) == 0x02);349}350351boolean isMCSiteLocal() {352return ((ipaddress[0] & 0xff) == 0xff353&& (ipaddress[1] & 0x0f) == 0x05);354}355356boolean isMCOrgLocal() {357return ((ipaddress[0] & 0xff) == 0xff358&& (ipaddress[1] & 0x0f) == 0x08);359}360}361362private final transient Inet6AddressHolder holder6;363364@java.io.Serial365private static final long serialVersionUID = 6880410070516793377L;366367// Perform native initialization368static { init(); }369370Inet6Address() {371super();372holder.init(null, IPv6);373holder6 = new Inet6AddressHolder();374}375376/* checking of value for scope_id should be done by caller377* scope_id must be >= 0, or -1 to indicate not being set378*/379Inet6Address(String hostName, byte addr[], int scope_id) {380holder.init(hostName, IPv6);381holder6 = new Inet6AddressHolder();382holder6.init(addr, scope_id);383}384385Inet6Address(String hostName, byte addr[]) {386holder6 = new Inet6AddressHolder();387try {388initif (hostName, addr, null);389} catch (UnknownHostException e) {} /* cant happen if ifname is null */390}391392Inet6Address (String hostName, byte addr[], NetworkInterface nif)393throws UnknownHostException394{395holder6 = new Inet6AddressHolder();396initif (hostName, addr, nif);397}398399Inet6Address (String hostName, byte addr[], String ifname)400throws UnknownHostException401{402holder6 = new Inet6AddressHolder();403initstr (hostName, addr, ifname);404}405406/**407* Create an Inet6Address in the exact manner of {@link408* InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is409* set to the value corresponding to the given interface for the address410* type specified in {@code addr}. The call will fail with an411* UnknownHostException if the given interface does not have a numeric412* scope_id assigned for the given address type (e.g. link-local or site-local).413* See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6414* scoped addresses.415*416* @param host the specified host417* @param addr the raw IP address in network byte order418* @param nif an interface this address must be associated with.419* @return an Inet6Address object created from the raw IP address.420* @throws UnknownHostException421* if IP address is of illegal length, or if the interface does not422* have a numeric scope_id assigned for the given address type.423*424* @since 1.5425*/426public static Inet6Address getByAddress(String host, byte[] addr,427NetworkInterface nif)428throws UnknownHostException429{430if (host != null && !host.isEmpty() && host.charAt(0) == '[') {431if (host.charAt(host.length()-1) == ']') {432host = host.substring(1, host.length() -1);433}434}435if (addr != null) {436if (addr.length == Inet6Address.INADDRSZ) {437return new Inet6Address(host, addr, nif);438}439}440throw new UnknownHostException("addr is of illegal length");441}442443/**444* Create an Inet6Address in the exact manner of {@link445* InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is446* set to the given numeric value. The scope_id is not checked to determine447* if it corresponds to any interface on the system.448* See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6449* scoped addresses.450*451* @param host the specified host452* @param addr the raw IP address in network byte order453* @param scope_id the numeric scope_id for the address.454* @return an Inet6Address object created from the raw IP address.455* @throws UnknownHostException if IP address is of illegal length.456*457* @since 1.5458*/459public static Inet6Address getByAddress(String host, byte[] addr,460int scope_id)461throws UnknownHostException462{463if (host != null && !host.isEmpty() && host.charAt(0) == '[') {464if (host.charAt(host.length()-1) == ']') {465host = host.substring(1, host.length() -1);466}467}468if (addr != null) {469if (addr.length == Inet6Address.INADDRSZ) {470return new Inet6Address(host, addr, scope_id);471}472}473throw new UnknownHostException("addr is of illegal length");474}475476private void initstr(String hostName, byte addr[], String ifname)477throws UnknownHostException478{479try {480NetworkInterface nif = NetworkInterface.getByName (ifname);481if (nif == null) {482throw new UnknownHostException ("no such interface " + ifname);483}484initif (hostName, addr, nif);485} catch (SocketException e) {486throw new UnknownHostException ("SocketException thrown" + ifname);487}488}489490private void initif(String hostName, byte addr[], NetworkInterface nif)491throws UnknownHostException492{493int family = -1;494holder6.init(addr, nif);495496if (addr.length == INADDRSZ) { // normal IPv6 address497family = IPv6;498}499holder.init(hostName, family);500}501502/* check the two Ipv6 addresses and return false if they are both503* non global address types, but not the same.504* (i.e. one is site-local and the other link-local)505* return true otherwise.506*/507508private static boolean isDifferentLocalAddressType(509byte[] thisAddr, byte[] otherAddr) {510511if (Inet6Address.isLinkLocalAddress(thisAddr) &&512!Inet6Address.isLinkLocalAddress(otherAddr)) {513return false;514}515if (Inet6Address.isSiteLocalAddress(thisAddr) &&516!Inet6Address.isSiteLocalAddress(otherAddr)) {517return false;518}519return true;520}521522private static int deriveNumericScope (byte[] thisAddr, NetworkInterface ifc) throws UnknownHostException {523Enumeration<InetAddress> addresses = ifc.getInetAddresses();524while (addresses.hasMoreElements()) {525InetAddress addr = addresses.nextElement();526if (!(addr instanceof Inet6Address ia6_addr)) {527continue;528}529/* check if site or link local prefixes match */530if (!isDifferentLocalAddressType(thisAddr, ia6_addr.getAddress())){531/* type not the same, so carry on searching */532continue;533}534/* found a matching address - return its scope_id */535return ia6_addr.getScopeId();536}537throw new UnknownHostException ("no scope_id found");538}539540private int deriveNumericScope (String ifname) throws UnknownHostException {541Enumeration<NetworkInterface> en;542try {543en = NetworkInterface.getNetworkInterfaces();544} catch (SocketException e) {545throw new UnknownHostException ("could not enumerate local network interfaces");546}547while (en.hasMoreElements()) {548NetworkInterface ifc = en.nextElement();549if (ifc.getName().equals (ifname)) {550return deriveNumericScope(holder6.ipaddress, ifc);551}552}553throw new UnknownHostException ("No matching address found for interface : " +ifname);554}555556/**557* @serialField ipaddress byte[] holds a 128-bit (16 bytes) IPv6 address558* @serialField scope_id int the address scope id. {@code 0} if undefined559* @serialField scope_id_set boolean {@code true} when the scope_id field560* contains a valid integer scope_id561* @serialField scope_ifname_set boolean {@code true} if the object is562* constructed with a scoped interface instead of a numeric563* scope id564* @serialField ifname String the name of the scoped network interface.565* {@code null} if undefined566*/567@java.io.Serial568private static final ObjectStreamField[] serialPersistentFields = {569new ObjectStreamField("ipaddress", byte[].class),570new ObjectStreamField("scope_id", int.class),571new ObjectStreamField("scope_id_set", boolean.class),572new ObjectStreamField("scope_ifname_set", boolean.class),573new ObjectStreamField("ifname", String.class)574};575576private static final jdk.internal.misc.Unsafe UNSAFE577= jdk.internal.misc.Unsafe.getUnsafe();578private static final long FIELDS_OFFSET = UNSAFE.objectFieldOffset(579Inet6Address.class, "holder6");580581/**582* Restores the state of this object from the stream.583* This includes the scope information, but only if the584* scoped interface name is valid on this system.585*586* @param s the {@code ObjectInputStream} from which data is read587* @throws IOException if an I/O error occurs588* @throws ClassNotFoundException if a serialized class cannot be loaded589*/590@java.io.Serial591private void readObject(ObjectInputStream s)592throws IOException, ClassNotFoundException {593NetworkInterface scope_ifname = null;594595if (getClass().getClassLoader() != null) {596throw new SecurityException ("invalid address type");597}598599ObjectInputStream.GetField gf = s.readFields();600byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);601int scope_id = gf.get("scope_id", -1);602boolean scope_id_set = gf.get("scope_id_set", false);603boolean scope_ifname_set = gf.get("scope_ifname_set", false);604String ifname = (String)gf.get("ifname", null);605606if (ifname != null && !ifname.isEmpty()) {607try {608scope_ifname = NetworkInterface.getByName(ifname);609if (scope_ifname == null) {610/* the interface does not exist on this system, so we clear611* the scope information completely */612scope_id_set = false;613scope_ifname_set = false;614scope_id = 0;615} else {616scope_ifname_set = true;617try {618scope_id = deriveNumericScope (ipaddress, scope_ifname);619} catch (UnknownHostException e) {620// typically should not happen, but it may be that621// the machine being used for deserialization has622// the same interface name but without IPv6 configured.623}624}625} catch (SocketException e) {}626}627628/* if ifname was not supplied, then the numeric info is used */629630ipaddress = ipaddress.clone();631632// Check that our invariants are satisfied633if (ipaddress.length != INADDRSZ) {634throw new InvalidObjectException("invalid address length: "+635ipaddress.length);636}637638if (holder.getFamily() != IPv6) {639throw new InvalidObjectException("invalid address family type");640}641642Inet6AddressHolder h = new Inet6AddressHolder(643ipaddress, scope_id, scope_id_set, scope_ifname, scope_ifname_set644);645646UNSAFE.putReference(this, FIELDS_OFFSET, h);647}648649/**650* The default behavior of this method is overridden in order to651* write the scope_ifname field as a {@code String}, rather than a652* {@code NetworkInterface} which is not serializable.653*654* @param s the {@code ObjectOutputStream} to which data is written655* @throws IOException if an I/O error occurs656*/657@java.io.Serial658private synchronized void writeObject(ObjectOutputStream s)659throws IOException660{661String ifname = null;662663if (holder6.scope_ifname != null) {664ifname = holder6.scope_ifname.getName();665holder6.scope_ifname_set = true;666}667ObjectOutputStream.PutField pfields = s.putFields();668pfields.put("ipaddress", holder6.ipaddress);669pfields.put("scope_id", holder6.scope_id);670pfields.put("scope_id_set", holder6.scope_id_set);671pfields.put("scope_ifname_set", holder6.scope_ifname_set);672pfields.put("ifname", ifname);673s.writeFields();674}675676/**677* Utility routine to check if the InetAddress is an IP multicast678* address. 11111111 at the start of the address identifies the679* address as being a multicast address.680*681* @return a {@code boolean} indicating if the InetAddress is an IP682* multicast address683*/684@Override685public boolean isMulticastAddress() {686return holder6.isMulticastAddress();687}688689/**690* Utility routine to check if the InetAddress is a wildcard address.691*692* @return a {@code boolean} indicating if the InetAddress is693* a wildcard address.694*/695@Override696public boolean isAnyLocalAddress() {697return holder6.isAnyLocalAddress();698}699700/**701* Utility routine to check if the InetAddress is a loopback address.702*703* @return a {@code boolean} indicating if the InetAddress is a loopback704* address; or false otherwise.705*/706@Override707public boolean isLoopbackAddress() {708return holder6.isLoopbackAddress();709}710711/**712* Utility routine to check if the InetAddress is an link local address.713*714* @return a {@code boolean} indicating if the InetAddress is a link local715* address; or false if address is not a link local unicast address.716*/717@Override718public boolean isLinkLocalAddress() {719return holder6.isLinkLocalAddress();720}721722/* static version of above */723static boolean isLinkLocalAddress(byte[] ipaddress) {724return ((ipaddress[0] & 0xff) == 0xfe725&& (ipaddress[1] & 0xc0) == 0x80);726}727728/**729* Utility routine to check if the InetAddress is a site local address.730*731* @return a {@code boolean} indicating if the InetAddress is a site local732* address; or false if address is not a site local unicast address.733*/734@Override735public boolean isSiteLocalAddress() {736return holder6.isSiteLocalAddress();737}738739/* static version of above */740static boolean isSiteLocalAddress(byte[] ipaddress) {741return ((ipaddress[0] & 0xff) == 0xfe742&& (ipaddress[1] & 0xc0) == 0xc0);743}744745/**746* Utility routine to check if the multicast address has global scope.747*748* @return a {@code boolean} indicating if the address has is a multicast749* address of global scope, false if it is not of global scope or750* it is not a multicast address751*/752@Override753public boolean isMCGlobal() {754return holder6.isMCGlobal();755}756757/**758* Utility routine to check if the multicast address has node scope.759*760* @return a {@code boolean} indicating if the address has is a multicast761* address of node-local scope, false if it is not of node-local762* scope or it is not a multicast address763*/764@Override765public boolean isMCNodeLocal() {766return holder6.isMCNodeLocal();767}768769/**770* Utility routine to check if the multicast address has link scope.771*772* @return a {@code boolean} indicating if the address has is a multicast773* address of link-local scope, false if it is not of link-local774* scope or it is not a multicast address775*/776@Override777public boolean isMCLinkLocal() {778return holder6.isMCLinkLocal();779}780781/**782* Utility routine to check if the multicast address has site scope.783*784* @return a {@code boolean} indicating if the address has is a multicast785* address of site-local scope, false if it is not of site-local786* scope or it is not a multicast address787*/788@Override789public boolean isMCSiteLocal() {790return holder6.isMCSiteLocal();791}792793/**794* Utility routine to check if the multicast address has organization scope.795*796* @return a {@code boolean} indicating if the address has is a multicast797* address of organization-local scope, false if it is not of798* organization-local scope or it is not a multicast address799*/800@Override801public boolean isMCOrgLocal() {802return holder6.isMCOrgLocal();803}804805/**806* Returns the raw IP address of this {@code InetAddress} object. The result807* is in network byte order: the highest order byte of the address is in808* {@code getAddress()[0]}.809*810* @return the raw IP address of this object.811*/812@Override813public byte[] getAddress() {814return holder6.ipaddress.clone();815}816817/**818* Returns a reference to the byte[] with the IPv6 address.819*/820byte[] addressBytes() {821return holder6.ipaddress;822}823824/**825* Returns the numeric scopeId, if this instance is associated with826* an interface. If no scoped_id is set, the returned value is zero.827*828* @return the scopeId, or zero if not set.829*830* @since 1.5831*/832public int getScopeId() {833return holder6.scope_id;834}835836/**837* Returns the scoped interface, if this instance was created with838* a scoped interface.839*840* @return the scoped interface, or null if not set.841* @since 1.5842*/843public NetworkInterface getScopedInterface() {844return holder6.scope_ifname;845}846847/**848* Returns the IP address string in textual presentation. If the instance849* was created specifying a scope identifier then the scope id is appended850* to the IP address preceded by a "%" (per-cent) character. This can be851* either a numeric value or a string, depending on which was used to create852* the instance.853*854* @return the raw IP address in a string format.855*/856@Override857public String getHostAddress() {858return holder6.getHostAddress();859}860861/**862* Returns a hashcode for this IP address.863*864* @return a hash code value for this IP address.865*/866@Override867public int hashCode() {868return holder6.hashCode();869}870871/**872* Compares this object against the specified object. The result is {@code873* true} if and only if the argument is not {@code null} and it represents874* the same IP address as this object.875*876* <p> Two instances of {@code InetAddress} represent the same IP address877* if the length of the byte arrays returned by {@code getAddress} is the878* same for both, and each of the array components is the same for the byte879* arrays.880*881* @param obj the object to compare against.882*883* @return {@code true} if the objects are the same; {@code false} otherwise.884*885* @see java.net.InetAddress#getAddress()886*/887@Override888public boolean equals(Object obj) {889if (obj instanceof Inet6Address inetAddr) {890return holder6.equals(inetAddr.holder6);891}892return false;893}894895/**896* Utility routine to check if the InetAddress is an897* IPv4 compatible IPv6 address.898*899* @return a {@code boolean} indicating if the InetAddress is an IPv4900* compatible IPv6 address; or false if address is IPv4 address.901*/902public boolean isIPv4CompatibleAddress() {903return holder6.isIPv4CompatibleAddress();904}905906// Utilities907908private static final int INT16SZ = 2;909910/**911* Convert IPv6 binary address into presentation (printable) format.912*913* @param src a byte array representing the IPv6 numeric address914* @return a String representing an IPv6 address in915* textual representation format916*/917static String numericToTextFormat(byte[] src) {918StringBuilder sb = new StringBuilder(39);919for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {920sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)921| (src[(i<<1)+1] & 0xff)));922if (i < (INADDRSZ / INT16SZ) -1 ) {923sb.append(":");924}925}926return sb.toString();927}928929/**930* Perform class load-time initializations.931*/932private static native void init();933}934935936