Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
      4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      5  *
      6  * This code is free software; you can redistribute it and/or modify it
      7  * under the terms of the GNU General Public License version 2 only, as
      8  * published by the Free Software Foundation.  Oracle designates this
      9  * particular file as subject to the "Classpath" exception as provided
     10  * by Oracle in the LICENSE file that accompanied this code.
     11  *
     12  * This code is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  * version 2 for more details (a copy is included in the LICENSE file that
     16  * accompanied this code).
     17  *
     18  * You should have received a copy of the GNU General Public License version
     19  * 2 along with this work; if not, write to the Free Software Foundation,
     20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     21  *
     22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     23  * or visit www.oracle.com if you need additional information or have any
     24  * questions.
     25  */
     26 
     27 package java.net;
     28 
     29 import java.io.IOException;
     30 import java.io.InvalidObjectException;
     31 import java.io.ObjectInputStream;
     32 import java.io.ObjectOutputStream;
     33 import java.io.ObjectStreamField;
     34 import java.util.Enumeration;
     35 import java.util.Arrays;
     36 import libcore.io.Libcore;
     37 import static android.system.OsConstants.*;
     38 
     39 /**
     40  * This class represents an Internet Protocol version 6 (IPv6) address.
     41  * Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt">
     42  * <i>RFC&nbsp;2373: IP Version 6 Addressing Architecture</i></a>.
     43  *
     44  * <h3> <A NAME="format">Textual representation of IP addresses</a> </h3>
     45  *
     46  * Textual representation of IPv6 address used as input to methods
     47  * takes one of the following forms:
     48  *
     49  * <ol>
     50  *   <li><p> <A NAME="lform">The preferred form</a> is x:x:x:x:x:x:x:x,
     51  *   where the 'x's are
     52  *   the hexadecimal values of the eight 16-bit pieces of the
     53  *   address. This is the full form.  For example,
     54  *
     55  *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
     56  *   <tr><td>{@code 1080:0:0:0:8:800:200C:417A}<td></tr>
     57  *   </table></blockquote>
     58  *
     59  *   <p> Note that it is not necessary to write the leading zeros in
     60  *   an individual field. However, there must be at least one numeral
     61  *   in every field, except as described below.</li>
     62  *
     63  *   <li><p> Due to some methods of allocating certain styles of IPv6
     64  *   addresses, it will be common for addresses to contain long
     65  *   strings of zero bits. In order to make writing addresses
     66  *   containing zero bits easier, a special syntax is available to
     67  *   compress the zeros. The use of "::" indicates multiple groups
     68  *   of 16-bits of zeros. The "::" can only appear once in an address.
     69  *   The "::" can also be used to compress the leading and/or trailing
     70  *   zeros in an address. For example,
     71  *
     72  *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
     73  *   <tr><td>{@code 1080::8:800:200C:417A}<td></tr>
     74  *   </table></blockquote>
     75  *
     76  *   <li><p> An alternative form that is sometimes more convenient
     77  *   when dealing with a mixed environment of IPv4 and IPv6 nodes is
     78  *   x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values
     79  *   of the six high-order 16-bit pieces of the address, and the 'd's
     80  *   are the decimal values of the four low-order 8-bit pieces of the
     81  *   standard IPv4 representation address, for example,
     82  *
     83  *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
     84  *   <tr><td>{@code ::FFFF:129.144.52.38}<td></tr>
     85  *   <tr><td>{@code ::129.144.52.38}<td></tr>
     86  *   </table></blockquote>
     87  *
     88  *   <p> where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the
     89  *   general forms of an IPv4-mapped IPv6 address and an
     90  *   IPv4-compatible IPv6 address. Note that the IPv4 portion must be
     91  *   in the "d.d.d.d" form. The following forms are invalid:
     92  *
     93  *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
     94  *   <tr><td>{@code ::FFFF:d.d.d}<td></tr>
     95  *   <tr><td>{@code ::FFFF:d.d}<td></tr>
     96  *   <tr><td>{@code ::d.d.d}<td></tr>
     97  *   <tr><td>{@code ::d.d}<td></tr>
     98  *   </table></blockquote>
     99  *
    100  *   <p> The following form:
    101  *
    102  *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
    103  *   <tr><td>{@code ::FFFF:d}<td></tr>
    104  *   </table></blockquote>
    105  *
    106  *   <p> is valid, however it is an unconventional representation of
    107  *   the IPv4-compatible IPv6 address,
    108  *
    109  *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
    110  *   <tr><td>{@code ::255.255.0.d}<td></tr>
    111  *   </table></blockquote>
    112  *
    113  *   <p> while "::d" corresponds to the general IPv6 address
    114  *   "0:0:0:0:0:0:0:d".</li>
    115  * </ol>
    116  *
    117  * <p> For methods that return a textual representation as output
    118  * value, the full form is used. Inet6Address will return the full
    119  * form because it is unambiguous when used in combination with other
    120  * textual data.
    121  *
    122  * <h4> Special IPv6 address </h4>
    123  *
    124  * <blockquote>
    125  * <table cellspacing=2 summary="Description of IPv4-mapped address">
    126  * <tr><th valign=top><i>IPv4-mapped address</i></th>
    127  *         <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to
    128  *         represent an IPv4 address. It allows the native program to
    129  *         use the same address data structure and also the same
    130  *         socket when communicating with both IPv4 and IPv6 nodes.
    131  *
    132  *         <p>In InetAddress and Inet6Address, it is used for internal
    133  *         representation; it has no functional role. Java will never
    134  *         return an IPv4-mapped address.  These classes can take an
    135  *         IPv4-mapped address as input, both in byte array and text
    136  *         representation. However, it will be converted into an IPv4
    137  *         address.</td></tr>
    138  * </table></blockquote>
    139  *
    140  * <h4><A NAME="scoped">Textual representation of IPv6 scoped addresses</a></h4>
    141  *
    142  * <p> The textual representation of IPv6 addresses as described above can be
    143  * extended to specify IPv6 scoped addresses. This extension to the basic
    144  * addressing architecture is described in [draft-ietf-ipngwg-scoping-arch-04.txt].
    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     final static int INADDRSZ = 16;
    180 
    181     // BEGIN Android-removed: Remove special handling for link-local addresses
    182     /*
    183     * cached scope_id - for link-local address use only.
    184     *
    185     private transient int cached_scope_id;  // 0
    186     */
    187     // END Android-removed: Remove special handling for link-local addresses
    188 
    189     // BEGIN Android-added: Define special-purpose IPv6 address
    190     /** @hide */
    191     public static final InetAddress ANY =
    192             new Inet6Address("::", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0);
    193 
    194     /** @hide */
    195     public static final InetAddress LOOPBACK = new Inet6Address("ip6-localhost",
    196             new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 0);
    197     // END Android-added: Define special-purpose IPv6 address
    198 
    199     private class Inet6AddressHolder {
    200 
    201         private Inet6AddressHolder() {
    202             ipaddress = new byte[INADDRSZ];
    203         }
    204 
    205         private Inet6AddressHolder(
    206             byte[] ipaddress, int scope_id, boolean scope_id_set,
    207             NetworkInterface ifname, boolean scope_ifname_set)
    208         {
    209             this.ipaddress = ipaddress;
    210             this.scope_id = scope_id;
    211             this.scope_id_set = scope_id_set;
    212             this.scope_ifname_set = scope_ifname_set;
    213             this.scope_ifname = ifname;
    214         }
    215 
    216         /**
    217          * Holds a 128-bit (16 bytes) IPv6 address.
    218          */
    219         byte[] ipaddress;
    220 
    221         /**
    222          * scope_id. The scope specified when the object is created. If the object
    223          * is created with an interface name, then the scope_id is not determined
    224          * until the time it is needed.
    225          */
    226         int scope_id;  // 0
    227 
    228         /**
    229          * This will be set to true when the scope_id field contains a valid
    230          * integer scope_id.
    231          */
    232         boolean scope_id_set;  // false
    233 
    234         /**
    235          * scoped interface. scope_id is derived from this as the scope_id of the first
    236          * address whose scope is the same as this address for the named interface.
    237          */
    238         NetworkInterface scope_ifname;  // null
    239 
    240         /**
    241          * set if the object is constructed with a scoped
    242          * interface instead of a numeric scope id.
    243          */
    244         boolean scope_ifname_set; // false;
    245 
    246         void setAddr(byte addr[]) {
    247             if (addr.length == INADDRSZ) { // normal IPv6 address
    248                 System.arraycopy(addr, 0, ipaddress, 0, INADDRSZ);
    249             }
    250         }
    251 
    252         void init(byte addr[], int scope_id) {
    253             setAddr(addr);
    254 
    255             // Android-changed: was >= 0
    256             if (scope_id > 0) {
    257                 this.scope_id = scope_id;
    258                 this.scope_id_set = true;
    259             }
    260         }
    261 
    262         void init(byte addr[], NetworkInterface nif)
    263             throws UnknownHostException
    264         {
    265             setAddr(addr);
    266 
    267             if (nif != null) {
    268                 this.scope_id = deriveNumericScope(ipaddress, nif);
    269                 this.scope_id_set = true;
    270                 this.scope_ifname = nif;
    271                 this.scope_ifname_set = true;
    272             }
    273         }
    274 
    275         // Android-removed: getnameinfo returns smarter representations than getHostAddress()
    276         /*
    277         String getHostAddress() {
    278             String s = numericToTextFormat(ipaddress);
    279             if (scope_ifname != null) { // must check this first
    280                 s = s + "%" + scope_ifname.getName();
    281             } else if (scope_id_set) {
    282                 s = s + "%" + scope_id;
    283             }
    284             return s;
    285         }
    286         */
    287 
    288         public boolean equals(Object o) {
    289             if (! (o instanceof Inet6AddressHolder)) {
    290                 return false;
    291             }
    292             Inet6AddressHolder that = (Inet6AddressHolder)o;
    293 
    294             return Arrays.equals(this.ipaddress, that.ipaddress);
    295         }
    296 
    297         public int hashCode() {
    298             if (ipaddress != null) {
    299 
    300                 int hash = 0;
    301                 int i=0;
    302                 while (i<INADDRSZ) {
    303                     int j=0;
    304                     int component=0;
    305                     while (j<4 && i<INADDRSZ) {
    306                         component = (component << 8) + ipaddress[i];
    307                         j++;
    308                         i++;
    309                     }
    310                     hash += component;
    311                 }
    312                 return hash;
    313 
    314             } else {
    315                 return 0;
    316             }
    317         }
    318 
    319         boolean isIPv4CompatibleAddress() {
    320             if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
    321                 (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
    322                 (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&
    323                 (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&
    324                 (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&
    325                 (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00))  {
    326                 return true;
    327             }
    328             return false;
    329         }
    330 
    331         boolean isMulticastAddress() {
    332             return ((ipaddress[0] & 0xff) == 0xff);
    333         }
    334 
    335         boolean isAnyLocalAddress() {
    336             byte test = 0x00;
    337             for (int i = 0; i < INADDRSZ; i++) {
    338                 test |= ipaddress[i];
    339             }
    340             return (test == 0x00);
    341         }
    342 
    343         boolean isLoopbackAddress() {
    344             byte test = 0x00;
    345             for (int i = 0; i < 15; i++) {
    346                 test |= ipaddress[i];
    347             }
    348             return (test == 0x00) && (ipaddress[15] == 0x01);
    349         }
    350 
    351         boolean isLinkLocalAddress() {
    352             return ((ipaddress[0] & 0xff) == 0xfe
    353                     && (ipaddress[1] & 0xc0) == 0x80);
    354         }
    355 
    356 
    357         boolean isSiteLocalAddress() {
    358             return ((ipaddress[0] & 0xff) == 0xfe
    359                     && (ipaddress[1] & 0xc0) == 0xc0);
    360         }
    361 
    362         boolean isMCGlobal() {
    363             return ((ipaddress[0] & 0xff) == 0xff
    364                     && (ipaddress[1] & 0x0f) == 0x0e);
    365         }
    366 
    367         boolean isMCNodeLocal() {
    368             return ((ipaddress[0] & 0xff) == 0xff
    369                     && (ipaddress[1] & 0x0f) == 0x01);
    370         }
    371 
    372         boolean isMCLinkLocal() {
    373             return ((ipaddress[0] & 0xff) == 0xff
    374                     && (ipaddress[1] & 0x0f) == 0x02);
    375         }
    376 
    377         boolean isMCSiteLocal() {
    378             return ((ipaddress[0] & 0xff) == 0xff
    379                     && (ipaddress[1] & 0x0f) == 0x05);
    380         }
    381 
    382         boolean isMCOrgLocal() {
    383             return ((ipaddress[0] & 0xff) == 0xff
    384                     && (ipaddress[1] & 0x0f) == 0x08);
    385         }
    386     }
    387 
    388     private final transient Inet6AddressHolder holder6;
    389 
    390     private static final long serialVersionUID = 6880410070516793377L;
    391 
    392     // BEGIN Android-removed: Android doesn't need to call native init
    393     /*
    394     // Perform native initialization
    395     static { init(); }
    396     // END Android-removed: Android doesn't need to call native init
    397     */
    398 
    399     Inet6Address() {
    400         super();
    401         holder.init(null, AF_INET6);
    402         holder6 = new Inet6AddressHolder();
    403     }
    404 
    405     /* checking of value for scope_id should be done by caller
    406      * scope_id must be >= 0, or -1 to indicate not being set
    407      */
    408     Inet6Address(String hostName, byte addr[], int scope_id) {
    409         holder.init(hostName, AF_INET6);
    410         holder6 = new Inet6AddressHolder();
    411         holder6.init(addr, scope_id);
    412     }
    413 
    414     Inet6Address(String hostName, byte addr[]) {
    415         holder6 = new Inet6AddressHolder();
    416         try {
    417             initif (hostName, addr, null);
    418         } catch (UnknownHostException e) {} /* cant happen if ifname is null */
    419     }
    420 
    421     Inet6Address (String hostName, byte addr[], NetworkInterface nif)
    422         throws UnknownHostException
    423     {
    424         holder6 = new Inet6AddressHolder();
    425         initif (hostName, addr, nif);
    426     }
    427 
    428     Inet6Address (String hostName, byte addr[], String ifname)
    429         throws UnknownHostException
    430     {
    431         holder6 = new Inet6AddressHolder();
    432         initstr (hostName, addr, ifname);
    433     }
    434 
    435     /**
    436      * Create an Inet6Address in the exact manner of {@link
    437      * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is
    438      * set to the value corresponding to the given interface for the address
    439      * type specified in {@code addr}. The call will fail with an
    440      * UnknownHostException if the given interface does not have a numeric
    441      * scope_id assigned for the given address type (eg. link-local or site-local).
    442      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
    443      * scoped addresses.
    444      *
    445      * @param host the specified host
    446      * @param addr the raw IP address in network byte order
    447      * @param nif an interface this address must be associated with.
    448      * @return  an Inet6Address object created from the raw IP address.
    449      * @throws  UnknownHostException
    450      *          if IP address is of illegal length, or if the interface does not
    451      *          have a numeric scope_id assigned for the given address type.
    452      *
    453      * @since 1.5
    454      */
    455     public static Inet6Address getByAddress(String host, byte[] addr,
    456                                             NetworkInterface nif)
    457         throws UnknownHostException
    458     {
    459         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
    460             if (host.charAt(host.length()-1) == ']') {
    461                 host = host.substring(1, host.length() -1);
    462             }
    463         }
    464         if (addr != null) {
    465             if (addr.length == Inet6Address.INADDRSZ) {
    466                 return new Inet6Address(host, addr, nif);
    467             }
    468         }
    469         throw new UnknownHostException("addr is of illegal length");
    470     }
    471 
    472     /**
    473      * Create an Inet6Address in the exact manner of {@link
    474      * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is
    475      * set to the given numeric value. The scope_id is not checked to determine
    476      * if it corresponds to any interface on the system.
    477      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
    478      * scoped addresses.
    479      *
    480      * @param host the specified host
    481      * @param addr the raw IP address in network byte order
    482      * @param scope_id the numeric scope_id for the address.
    483      * @return  an Inet6Address object created from the raw IP address.
    484      * @throws  UnknownHostException  if IP address is of illegal length.
    485      *
    486      * @since 1.5
    487      */
    488     public static Inet6Address getByAddress(String host, byte[] addr,
    489                                             int scope_id)
    490         throws UnknownHostException
    491     {
    492         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
    493             if (host.charAt(host.length()-1) == ']') {
    494                 host = host.substring(1, host.length() -1);
    495             }
    496         }
    497         if (addr != null) {
    498             if (addr.length == Inet6Address.INADDRSZ) {
    499                 return new Inet6Address(host, addr, scope_id);
    500             }
    501         }
    502         throw new UnknownHostException("addr is of illegal length");
    503     }
    504 
    505     private void initstr(String hostName, byte addr[], String ifname)
    506         throws UnknownHostException
    507     {
    508         try {
    509             NetworkInterface nif = NetworkInterface.getByName (ifname);
    510             if (nif == null) {
    511                 throw new UnknownHostException ("no such interface " + ifname);
    512             }
    513             initif (hostName, addr, nif);
    514         } catch (SocketException e) {
    515             throw new UnknownHostException ("SocketException thrown" + ifname);
    516         }
    517     }
    518 
    519     private void initif(String hostName, byte addr[], NetworkInterface nif)
    520         throws UnknownHostException
    521     {
    522         int family = -1;
    523         holder6.init(addr, nif);
    524 
    525         if (addr.length == INADDRSZ) { // normal IPv6 address
    526             family = AF_INET6;
    527         }
    528         holder.init(hostName, family);
    529     }
    530 
    531     /* check the two Ipv6 addresses and return false if they are both
    532      * non global address types, but not the same.
    533      * (ie. one is sitelocal and the other linklocal)
    534      * return true otherwise.
    535      */
    536 
    537     private static boolean isDifferentLocalAddressType(
    538         byte[] thisAddr, byte[] otherAddr) {
    539 
    540         if (Inet6Address.isLinkLocalAddress(thisAddr) &&
    541                 !Inet6Address.isLinkLocalAddress(otherAddr)) {
    542             return false;
    543         }
    544         if (Inet6Address.isSiteLocalAddress(thisAddr) &&
    545                 !Inet6Address.isSiteLocalAddress(otherAddr)) {
    546             return false;
    547         }
    548         return true;
    549     }
    550 
    551     private static int deriveNumericScope (byte[] thisAddr, NetworkInterface ifc) throws UnknownHostException {
    552         Enumeration<InetAddress> addresses = ifc.getInetAddresses();
    553         while (addresses.hasMoreElements()) {
    554             InetAddress addr = addresses.nextElement();
    555             if (!(addr instanceof Inet6Address)) {
    556                 continue;
    557             }
    558             Inet6Address ia6_addr = (Inet6Address)addr;
    559             /* check if site or link local prefixes match */
    560             if (!isDifferentLocalAddressType(thisAddr, ia6_addr.getAddress())){
    561                 /* type not the same, so carry on searching */
    562                 continue;
    563             }
    564             /* found a matching address - return its scope_id */
    565             return ia6_addr.getScopeId();
    566         }
    567         throw new UnknownHostException ("no scope_id found");
    568     }
    569 
    570     private int deriveNumericScope (String ifname) throws UnknownHostException {
    571         Enumeration<NetworkInterface> en;
    572         try {
    573             en = NetworkInterface.getNetworkInterfaces();
    574         } catch (SocketException e) {
    575             throw new UnknownHostException ("could not enumerate local network interfaces");
    576         }
    577         while (en.hasMoreElements()) {
    578             NetworkInterface ifc = en.nextElement();
    579             if (ifc.getName().equals (ifname)) {
    580                 return deriveNumericScope(holder6.ipaddress, ifc);
    581             }
    582         }
    583         throw new UnknownHostException ("No matching address found for interface : " +ifname);
    584     }
    585 
    586     /**
    587      * @serialField ipaddress byte[]
    588      * @serialField scope_id int
    589      * @serialField scope_id_set boolean
    590      * @serialField scope_ifname_set boolean
    591      * @serialField ifname String
    592      */
    593 
    594     private static final ObjectStreamField[] serialPersistentFields = {
    595          new ObjectStreamField("ipaddress", byte[].class),
    596          new ObjectStreamField("scope_id", int.class),
    597          new ObjectStreamField("scope_id_set", boolean.class),
    598          new ObjectStreamField("scope_ifname_set", boolean.class),
    599          new ObjectStreamField("ifname", String.class)
    600     };
    601 
    602     private static final long FIELDS_OFFSET;
    603     private static final sun.misc.Unsafe UNSAFE;
    604 
    605     static {
    606         try {
    607             sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
    608             FIELDS_OFFSET = unsafe.objectFieldOffset(
    609                     Inet6Address.class.getDeclaredField("holder6"));
    610             UNSAFE = unsafe;
    611         } catch (ReflectiveOperationException e) {
    612             throw new Error(e);
    613         }
    614     }
    615 
    616     /**
    617      * restore the state of this object from stream
    618      * including the scope information, only if the
    619      * scoped interface name is valid on this system
    620      */
    621     private void readObject(ObjectInputStream s)
    622         throws IOException, ClassNotFoundException {
    623         NetworkInterface scope_ifname = null;
    624 
    625         // Android-changed: was getClass().getClassLoader() != null
    626         if (getClass().getClassLoader() != Class.class.getClassLoader()) {
    627             throw new SecurityException ("invalid address type");
    628         }
    629 
    630         ObjectInputStream.GetField gf = s.readFields();
    631         byte[] ipaddress = (byte[])gf.get("ipaddress", null);
    632         int scope_id = (int)gf.get("scope_id", -1);
    633         boolean scope_id_set = (boolean)gf.get("scope_id_set", false);
    634         boolean scope_ifname_set = (boolean)gf.get("scope_ifname_set", false);
    635         String ifname = (String)gf.get("ifname", null);
    636 
    637         if (ifname != null && !"".equals (ifname)) {
    638             try {
    639                 scope_ifname = NetworkInterface.getByName(ifname);
    640                 if (scope_ifname == null) {
    641                     /* the interface does not exist on this system, so we clear
    642                      * the scope information completely */
    643                     scope_id_set = false;
    644                     scope_ifname_set = false;
    645                     scope_id = 0;
    646                 } else {
    647                     scope_ifname_set = true;
    648                     try {
    649                         scope_id = deriveNumericScope (ipaddress, scope_ifname);
    650                     } catch (UnknownHostException e) {
    651                         // typically should not happen, but it may be that
    652                         // the machine being used for deserialization has
    653                         // the same interface name but without IPv6 configured.
    654                     }
    655                 }
    656             } catch (SocketException e) {}
    657         }
    658 
    659         /* if ifname was not supplied, then the numeric info is used */
    660 
    661         ipaddress = ipaddress.clone();
    662 
    663         // Check that our invariants are satisfied
    664         if (ipaddress.length != INADDRSZ) {
    665             throw new InvalidObjectException("invalid address length: "+
    666                                              ipaddress.length);
    667         }
    668 
    669         if (holder().getFamily() != AF_INET6) {
    670             throw new InvalidObjectException("invalid address family type");
    671         }
    672 
    673         Inet6AddressHolder h = new Inet6AddressHolder(
    674             ipaddress, scope_id, scope_id_set, scope_ifname, scope_ifname_set
    675         );
    676 
    677         UNSAFE.putObject(this, FIELDS_OFFSET, h);
    678     }
    679 
    680     /**
    681      * default behavior is overridden in order to write the
    682      * scope_ifname field as a String, rather than a NetworkInterface
    683      * which is not serializable
    684      */
    685     private synchronized void writeObject(ObjectOutputStream s)
    686         throws IOException
    687     {
    688             String ifname = null;
    689 
    690         if (holder6.scope_ifname != null) {
    691             ifname = holder6.scope_ifname.getName();
    692             holder6.scope_ifname_set = true;
    693         }
    694         ObjectOutputStream.PutField pfields = s.putFields();
    695         pfields.put("ipaddress", holder6.ipaddress);
    696         pfields.put("scope_id", holder6.scope_id);
    697         pfields.put("scope_id_set", holder6.scope_id_set);
    698         pfields.put("scope_ifname_set", holder6.scope_ifname_set);
    699         pfields.put("ifname", ifname);
    700         s.writeFields();
    701     }
    702 
    703     /**
    704      * Utility routine to check if the InetAddress is an IP multicast
    705      * address. 11111111 at the start of the address identifies the
    706      * address as being a multicast address.
    707      *
    708      * @return a {@code boolean} indicating if the InetAddress is an IP
    709      *         multicast address
    710      *
    711      * @since JDK1.1
    712      */
    713     @Override
    714     public boolean isMulticastAddress() {
    715         return holder6.isMulticastAddress();
    716     }
    717 
    718     /**
    719      * Utility routine to check if the InetAddress in a wildcard address.
    720      *
    721      * @return a {@code boolean} indicating if the Inetaddress is
    722      *         a wildcard address.
    723      *
    724      * @since 1.4
    725      */
    726     @Override
    727     public boolean isAnyLocalAddress() {
    728         return holder6.isAnyLocalAddress();
    729     }
    730 
    731     /**
    732      * Utility routine to check if the InetAddress is a loopback address.
    733      *
    734      * @return a {@code boolean} indicating if the InetAddress is a loopback
    735      *         address; or false otherwise.
    736      *
    737      * @since 1.4
    738      */
    739     @Override
    740     public boolean isLoopbackAddress() {
    741         return holder6.isLoopbackAddress();
    742     }
    743 
    744     /**
    745      * Utility routine to check if the InetAddress is an link local address.
    746      *
    747      * @return a {@code boolean} indicating if the InetAddress is a link local
    748      *         address; or false if address is not a link local unicast address.
    749      *
    750      * @since 1.4
    751      */
    752     @Override
    753     public boolean isLinkLocalAddress() {
    754         return holder6.isLinkLocalAddress();
    755     }
    756 
    757     /* static version of above */
    758     static boolean isLinkLocalAddress(byte[] ipaddress) {
    759         return ((ipaddress[0] & 0xff) == 0xfe
    760                 && (ipaddress[1] & 0xc0) == 0x80);
    761     }
    762 
    763     /**
    764      * Utility routine to check if the InetAddress is a site local address.
    765      *
    766      * @return a {@code boolean} indicating if the InetAddress is a site local
    767      *         address; or false if address is not a site local unicast address.
    768      *
    769      * @since 1.4
    770      */
    771     @Override
    772     public boolean isSiteLocalAddress() {
    773         return holder6.isSiteLocalAddress();
    774     }
    775 
    776     /* static version of above */
    777     static boolean isSiteLocalAddress(byte[] ipaddress) {
    778         return ((ipaddress[0] & 0xff) == 0xfe
    779                 && (ipaddress[1] & 0xc0) == 0xc0);
    780     }
    781 
    782     /**
    783      * Utility routine to check if the multicast address has global scope.
    784      *
    785      * @return a {@code boolean} indicating if the address has is a multicast
    786      *         address of global scope, false if it is not of global scope or
    787      *         it is not a multicast address
    788      *
    789      * @since 1.4
    790      */
    791     @Override
    792     public boolean isMCGlobal() {
    793         return holder6.isMCGlobal();
    794     }
    795 
    796     /**
    797      * Utility routine to check if the multicast address has node scope.
    798      *
    799      * @return a {@code boolean} indicating if the address has is a multicast
    800      *         address of node-local scope, false if it is not of node-local
    801      *         scope or it is not a multicast address
    802      *
    803      * @since 1.4
    804      */
    805     @Override
    806     public boolean isMCNodeLocal() {
    807         return holder6.isMCNodeLocal();
    808     }
    809 
    810     /**
    811      * Utility routine to check if the multicast address has link scope.
    812      *
    813      * @return a {@code boolean} indicating if the address has is a multicast
    814      *         address of link-local scope, false if it is not of link-local
    815      *         scope or it is not a multicast address
    816      *
    817      * @since 1.4
    818      */
    819     @Override
    820     public boolean isMCLinkLocal() {
    821         return holder6.isMCLinkLocal();
    822     }
    823 
    824     /**
    825      * Utility routine to check if the multicast address has site scope.
    826      *
    827      * @return a {@code boolean} indicating if the address has is a multicast
    828      *         address of site-local scope, false if it is not  of site-local
    829      *         scope or it is not a multicast address
    830      *
    831      * @since 1.4
    832      */
    833     @Override
    834     public boolean isMCSiteLocal() {
    835         return holder6.isMCSiteLocal();
    836     }
    837 
    838     /**
    839      * Utility routine to check if the multicast address has organization scope.
    840      *
    841      * @return a {@code boolean} indicating if the address has is a multicast
    842      *         address of organization-local scope, false if it is not of
    843      *         organization-local scope or it is not a multicast address
    844      *
    845      * @since 1.4
    846      */
    847     @Override
    848     public boolean isMCOrgLocal() {
    849         return holder6.isMCOrgLocal();
    850     }
    851     /**
    852      * Returns the raw IP address of this {@code InetAddress} object. The result
    853      * is in network byte order: the highest order byte of the address is in
    854      * {@code getAddress()[0]}.
    855      *
    856      * @return  the raw IP address of this object.
    857      */
    858     @Override
    859     public byte[] getAddress() {
    860         return holder6.ipaddress.clone();
    861     }
    862 
    863     /**
    864      * Returns the numeric scopeId, if this instance is associated with
    865      * an interface. If no scoped_id is set, the returned value is zero.
    866      *
    867      * @return the scopeId, or zero if not set.
    868      *
    869      * @since 1.5
    870      */
    871      public int getScopeId() {
    872         return holder6.scope_id;
    873      }
    874 
    875     /**
    876      * Returns the scoped interface, if this instance was created with
    877      * with a scoped interface.
    878      *
    879      * @return the scoped interface, or null if not set.
    880      * @since 1.5
    881      */
    882      public NetworkInterface getScopedInterface() {
    883         return holder6.scope_ifname;
    884      }
    885 
    886     /**
    887      * Returns the IP address string in textual presentation. If the instance
    888      * was created specifying a scope identifier then the scope id is appended
    889      * to the IP address preceded by a "%" (per-cent) character. This can be
    890      * either a numeric value or a string, depending on which was used to create
    891      * the instance.
    892      *
    893      * @return  the raw IP address in a string format.
    894      */
    895     @Override
    896     public String getHostAddress() {
    897         // Android-changed: getnameinfo returns smarter representations than getHostAddress()
    898         // return holder6.getHostAddress();
    899         return Libcore.os.getnameinfo(this, NI_NUMERICHOST); // Can't throw.
    900     }
    901 
    902     /**
    903      * Returns a hashcode for this IP address.
    904      *
    905      * @return  a hash code value for this IP address.
    906      */
    907     @Override
    908     public int hashCode() {
    909         return holder6.hashCode();
    910     }
    911 
    912     /**
    913      * Compares this object against the specified object. The result is {@code
    914      * true} if and only if the argument is not {@code null} and it represents
    915      * the same IP address as this object.
    916      *
    917      * <p> Two instances of {@code InetAddress} represent the same IP address
    918      * if the length of the byte arrays returned by {@code getAddress} is the
    919      * same for both, and each of the array components is the same for the byte
    920      * arrays.
    921      *
    922      * @param   obj   the object to compare against.
    923      *
    924      * @return  {@code true} if the objects are the same; {@code false} otherwise.
    925      *
    926      * @see     java.net.InetAddress#getAddress()
    927      */
    928     @Override
    929     public boolean equals(Object obj) {
    930         if (obj == null || !(obj instanceof Inet6Address))
    931             return false;
    932 
    933         Inet6Address inetAddr = (Inet6Address)obj;
    934 
    935         return holder6.equals(inetAddr.holder6);
    936     }
    937 
    938     /**
    939      * Utility routine to check if the InetAddress is an
    940      * IPv4 compatible IPv6 address.
    941      *
    942      * @return a {@code boolean} indicating if the InetAddress is an IPv4
    943      *         compatible IPv6 address; or false if address is IPv4 address.
    944      *
    945      * @since 1.4
    946      */
    947     public boolean isIPv4CompatibleAddress() {
    948         return holder6.isIPv4CompatibleAddress();
    949     }
    950 
    951     // Utilities
    952     private final static int INT16SZ = 2;
    953 
    954     /*
    955      * Convert IPv6 binary address into presentation (printable) format.
    956      *
    957      * @param src a byte array representing the IPv6 numeric address
    958      * @return a String representing an IPv6 address in
    959      *         textual representation format
    960      * @since 1.4
    961      */
    962     static String numericToTextFormat(byte[] src) {
    963         StringBuilder sb = new StringBuilder(39);
    964         for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
    965             sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
    966                                           | (src[(i<<1)+1] & 0xff)));
    967             if (i < (INADDRSZ / INT16SZ) -1 ) {
    968                sb.append(":");
    969             }
    970         }
    971         return sb.toString();
    972     }
    973 
    974     // BEGIN Android-removed: Android doesn't need to call native init
    975     /*
    976      * Perform class load-time initializations.
    977      *
    978     private static native void init();
    979     */
    980     // END Android-removed: Android doesn't need to call native init
    981 }
    982