Inet6Address.java revision f74285406bce55f409becf867e96142abeb9b58f
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.net;
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.ObjectInputStream;
2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.IOException;
3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.InvalidObjectException;
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.util.Enumeration;
3293c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniakimport libcore.io.Libcore;
3393c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniakimport static android.system.OsConstants.*;
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class represents an Internet Protocol version 6 (IPv6) address.
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt">
3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>RFC&nbsp;2373: IP Version 6 Addressing Architecture</i></a>.
3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> <A NAME="format">Textual representation of IP addresses</a> </h4>
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Textual representation of IPv6 address used as input to methods
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * takes one of the following forms:
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ol>
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <li><p> <A NAME="lform">The preferred form</a> is x:x:x:x:x:x:x:x,
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   where the 'x's are
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   the hexadecimal values of the eight 16-bit pieces of the
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   address. This is the full form.  For example,
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>1080:0:0:0:8:800:200C:417A</tt><td></tr>
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   </table></blockquote>
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <p> Note that it is not necessary to write the leading zeros in
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   an individual field. However, there must be at least one numeral
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   in every field, except as described below.</li>
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <li><p> Due to some methods of allocating certain styles of IPv6
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   addresses, it will be common for addresses to contain long
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   strings of zero bits. In order to make writing addresses
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   containing zero bits easier, a special syntax is available to
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   compress the zeros. The use of "::" indicates multiple groups
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   of 16-bits of zeros. The "::" can only appear once in an address.
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   The "::" can also be used to compress the leading and/or trailing
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   zeros in an address. For example,
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>1080::8:800:200C:417A</tt><td></tr>
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   </table></blockquote>
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <li><p> An alternative form that is sometimes more convenient
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   when dealing with a mixed environment of IPv4 and IPv6 nodes is
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   of the six high-order 16-bit pieces of the address, and the 'd's
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   are the decimal values of the four low-order 8-bit pieces of the
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   standard IPv4 representation address, for example,
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::FFFF:129.144.52.38</tt><td></tr>
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::129.144.52.38</tt><td></tr>
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   </table></blockquote>
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <p> where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   general forms of an IPv4-mapped IPv6 address and an
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   IPv4-compatible IPv6 address. Note that the IPv4 portion must be
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   in the "d.d.d.d" form. The following forms are invalid:
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::FFFF:d.d.d</tt><td></tr>
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::FFFF:d.d</tt><td></tr>
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::d.d.d</tt><td></tr>
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::d.d</tt><td></tr>
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   </table></blockquote>
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <p> The following form:
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::FFFF:d</tt><td></tr>
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   </table></blockquote>
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <p> is valid, however it is an unconventional representation of
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   the IPv4-compatible IPv6 address,
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <tr><td><tt>::255.255.0.d</tt><td></tr>
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   </table></blockquote>
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   <p> while "::d" corresponds to the general IPv6 address
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *   "0:0:0:0:0:0:0:d".</li>
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ol>
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> For methods that return a textual representation as output
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value, the full form is used. Inet6Address will return the full
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * form because it is unambiguous when used in combination with other
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * textual data.
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> Special IPv6 address </h4>
11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote>
12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <table cellspacing=2 summary="Description of IPv4-mapped address"> <tr><th valign=top><i>IPv4-mapped address</i></th>
12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to
12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         represent an IPv4 address. It allows the native program to
12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         use the same address data structure and also the same
12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         socket when communicating with both IPv4 and IPv6 nodes.
12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         <p>In InetAddress and Inet6Address, it is used for internal
12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         representation; it has no functional role. Java will never
12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         return an IPv4-mapped address.  These classes can take an
13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         IPv4-mapped address as input, both in byte array and text
13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         representation. However, it will be converted into an IPv4
13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *         address.</td></tr>
13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </table></blockquote>
13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <h4> <A NAME="scoped">Textual representation of IPv6 scoped addresses</a> </h4>
13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The textual representation of IPv6 addresses as described above can be extended
13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to specify IPv6 scoped addresses. This extension to the basic addressing architecture
13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is described in [draft-ietf-ipngwg-scoping-arch-04.txt].
14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Because link-local and site-local addresses are non-global, it is possible that different hosts
14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may have the same destination address and may be reachable through different interfaces on the
14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * same originating system. In this case, the originating system is said to be connected
14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to multiple zones of the same scope. In order to disambiguate which is the intended destination
14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an IPv6 address.
14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>
14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The general format for specifying the <i>scope_id</i> is the following:
14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote>
14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The IPv6-address is a literal IPv6 address as described above.
15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The <i>scope_id</i> refers to an interface on the local system, and it can be specified
15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in two ways.
15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer that identifies the
15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular interface and scope as understood by the system. Usually, the numeric
15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values can be determined through administration tools on the system. Each interface may
15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * have multiple values, one for each scope. If the scope is unspecified, then the default value
15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * used is zero.</li><p>
15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li><i>As a string.</i> This must be the exact string that is returned by
15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link java.net.NetworkInterface#getName()} for the particular interface in question.
15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * When an Inet6Address is created in this way, the numeric scope-id is determined at the time
16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the object is created by querying the relevant NetworkInterface.</li>
16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ol><p>
16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note also, that the numeric <i>scope_id</i> can be retrieved from Inet6Address instances returned from the
16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * NetworkInterface class. This can be used to find out the current scope ids configured on the system.
16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4
16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic final
16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiclass Inet6Address extends InetAddress {
16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    final static int INADDRSZ = 16;
17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
171f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    /* @hide */
172f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static final InetAddress ANY =
173f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            new Inet6Address(null, new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0);
174f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
175f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    /* @hide */
176f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public static final InetAddress LOOPBACK = new Inet6Address("localhost",
177f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski            new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 0);
178f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
179f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * cached scope_id - for link-local address use only.
18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private transient int cached_scope_id = 0;
18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Holds a 128-bit (16 bytes) IPv6 address.
18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serial
18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    byte[] ipaddress;
19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scope_id. The scope specified when the object is created. If the object is created
19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * with an interface name, then the scope_id is not determined until the time it is needed.
19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private int scope_id = 0;
19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * This will be set to true when the scope_id field contains a valid
20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * integer scope_id.
20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private boolean scope_id_set = false;
20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scoped interface. scope_id is derived from this as the scope_id of the first
20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address whose scope is the same as this address for the named interface.
20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private transient NetworkInterface scope_ifname = null;
20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * set if the object is constructed with a scoped interface instead of a
21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * numeric scope id.
21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private boolean scope_ifname_set = false;
21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static final long serialVersionUID = 6880410070516793377L;
21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Perform initializations.
22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static {
22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        init();
22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    Inet6Address() {
22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        super();
22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        holder().hostName = null;
22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        ipaddress = new byte[INADDRSZ];
22993c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        holder().family = AF_INET6;
23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /* checking of value for scope_id should be done by caller
23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scope_id must be >= 0, or -1 to indicate not being set
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    Inet6Address(String hostName, byte addr[], int scope_id) {
23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        holder().hostName = hostName;
23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (addr.length == INADDRSZ) { // normal IPv6 address
23893c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak            holder().family = AF_INET6;
23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ipaddress = addr.clone();
24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
24193c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        // Android change, was >= 0.
24293c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        if (scope_id > 0) {
24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            this.scope_id = scope_id;
24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            scope_id_set = true;
24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    Inet6Address(String hostName, byte addr[]) {
24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        try {
25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            initif (hostName, addr, null);
25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } catch (UnknownHostException e) {} /* cant happen if ifname is null */
25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException {
25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        initif (hostName, addr, nif);
25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException {
25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        initstr (hostName, addr, ifname);
26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * except that the IPv6 scope_id is set to the value corresponding to the given interface
26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for the address type specified in <code>addr</code>.
26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The call will fail with an UnknownHostException if the given interface does not have a numeric
26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scope_id assigned for the given address type (eg. link-local or site-local).
26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scoped addresses.
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param host the specified host
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param addr the raw IP address in network byte order
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param nif an interface this address must be associated with.
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  an Inet6Address object created from the raw IP address.
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if IP address is of illegal length, or if the interface
27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          does not have a numeric scope_id assigned for the given address type.
27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif)
28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws UnknownHostException {
28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (host.charAt(host.length()-1) == ']') {
28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                host = host.substring(1, host.length() -1);
28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (addr != null) {
28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (addr.length == Inet6Address.INADDRSZ) {
29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return new Inet6Address(host, addr, nif);
29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throw new UnknownHostException("addr is of illegal length");
29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * except that the IPv6 scope_id is set to the given numeric value.
29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The scope_id is not checked to determine if it corresponds to any interface on the system.
30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scoped addresses.
30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param host the specified host
30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param addr the raw IP address in network byte order
30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param scope_id the numeric scope_id for the address.
30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  an Inet6Address object created from the raw IP address.
30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @exception  UnknownHostException  if IP address is of illegal length.
30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static Inet6Address getByAddress(String host, byte[] addr, int scope_id)
31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws UnknownHostException {
31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (host.charAt(host.length()-1) == ']') {
31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                host = host.substring(1, host.length() -1);
31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (addr != null) {
32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (addr.length == Inet6Address.INADDRSZ) {
32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return new Inet6Address(host, addr, scope_id);
32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throw new UnknownHostException("addr is of illegal length");
32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private void initstr (String hostName, byte addr[], String ifname) throws UnknownHostException {
32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        try {
32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            NetworkInterface nif = NetworkInterface.getByName (ifname);
33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (nif == null) {
33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                throw new UnknownHostException ("no such interface " + ifname);
33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            initif (hostName, addr, nif);
33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } catch (SocketException e) {
33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new UnknownHostException ("SocketException thrown" + ifname);
33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException {
34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        holder().hostName = hostName;
34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (addr.length == INADDRSZ) { // normal IPv6 address
34293c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak            holder().family = AF_INET6;
34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ipaddress = addr.clone();
34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (nif != null) {
34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            this.scope_ifname = nif;
34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            scope_ifname_set = true;
34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            scope_id = deriveNumericScope (nif);
34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            scope_id_set = true;
35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /* check the two Ipv6 addresses and return false if they are both
35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * non global address types, but not the same.
35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (ie. one is sitelocal and the other linklocal)
35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * return true otherwise.
35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private boolean differentLocalAddressTypes(Inet6Address other) {
35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (isLinkLocalAddress() && !other.isLinkLocalAddress()) {
36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (isSiteLocalAddress() && !other.isSiteLocalAddress()) {
36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException {
37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Enumeration<InetAddress> addresses = ifc.getInetAddresses();
37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (addresses.hasMoreElements()) {
37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            InetAddress addr = addresses.nextElement();
37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!(addr instanceof Inet6Address)) {
37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                continue;
37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Inet6Address ia6_addr = (Inet6Address)addr;
37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            /* check if site or link local prefixes match */
37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!differentLocalAddressTypes(ia6_addr)){
37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                /* type not the same, so carry on searching */
38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                continue;
38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            /* found a matching address - return its scope_id */
38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return ia6_addr.scope_id;
38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throw new UnknownHostException ("no scope_id found");
38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private int deriveNumericScope (String ifname) throws UnknownHostException {
38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Enumeration<NetworkInterface> en;
39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        try {
39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            en = NetworkInterface.getNetworkInterfaces();
39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } catch (SocketException e) {
39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new UnknownHostException ("could not enumerate local network interfaces");
39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (en.hasMoreElements()) {
39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            NetworkInterface ifc = en.nextElement();
39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (ifc.getName().equals (ifname)) {
39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                Enumeration addresses = ifc.getInetAddresses();
39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                while (addresses.hasMoreElements()) {
40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    InetAddress addr = (InetAddress)addresses.nextElement();
40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (!(addr instanceof Inet6Address)) {
40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        continue;
40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    }
40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    Inet6Address ia6_addr = (Inet6Address)addr;
40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    /* check if site or link local prefixes match */
40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (!differentLocalAddressTypes(ia6_addr)){
40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        /* type not the same, so carry on searching */
40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        continue;
40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    }
41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    /* found a matching address - return its scope_id */
41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return ia6_addr.scope_id;
41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throw new UnknownHostException ("No matching address found for interface : " +ifname);
41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * restore the state of this object from stream
42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * including the scope information, only if the
42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scoped interface name is valid on this system
42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private void readObject(ObjectInputStream s)
42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws IOException, ClassNotFoundException {
42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        scope_ifname = null;
42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        scope_ifname_set = false;
42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
428f74285406bce55f409becf867e96142abeb9b58fNarayan Kamath        if (getClass().getClassLoader() != Class.class.getClassLoader()) {
42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new SecurityException ("invalid address type");
43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        s.defaultReadObject();
43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (ifname != null && !"".equals (ifname)) {
43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            try {
43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                scope_ifname = NetworkInterface.getByName(ifname);
43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (scope_ifname == null) {
43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    /* the interface does not exist on this system, so we clear
43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                     * the scope information completely */
44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    scope_id_set = false;
44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    scope_ifname_set = false;
44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    scope_id = 0;
44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                } else {
44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    try {
44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        scope_id = deriveNumericScope (scope_ifname);
44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    } catch (UnknownHostException e) {
44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        // typically should not happen, but it may be that
44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        // the machine being used for deserialization has
44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        // the same interface name but without IPv6 configured.
45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    }
45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } catch (SocketException e) {}
45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        /* if ifname was not supplied, then the numeric info is used */
45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        ipaddress = ipaddress.clone();
45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Check that our invariants are satisfied
46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (ipaddress.length != INADDRSZ) {
46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new InvalidObjectException("invalid address length: "+
46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                             ipaddress.length);
46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
46593c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        if (holder().getFamily() != AF_INET6) {
46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new InvalidObjectException("invalid address family type");
46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is an IP multicast
47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address. 11111111 at the start of the address identifies the
47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address as being a multicast address.
47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the InetAddress is
47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * an IP multicast address
47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since JDK1.1
47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMulticastAddress() {
48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xff);
48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress in a wildcard address.
48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the Inetaddress is
48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         a wildcard address.
48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isAnyLocalAddress() {
49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte test = 0x00;
49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < INADDRSZ; i++) {
49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            test |= ipaddress[i];
49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (test == 0x00);
49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is a loopback address.
50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the InetAddress is
50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a loopback address; or false otherwise.
50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isLoopbackAddress() {
50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte test = 0x00;
50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < 15; i++) {
51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            test |= ipaddress[i];
51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (test == 0x00) && (ipaddress[15] == 0x01);
51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is an link local address.
51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the InetAddress is
51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a link local address; or false if address is not a link local unicast address.
52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isLinkLocalAddress() {
52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xfe
52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0xc0) == 0x80);
52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is a site local address.
53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the InetAddress is
53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a site local address; or false if address is not a site local unicast address.
53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isSiteLocalAddress() {
53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xfe
53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0xc0) == 0xc0);
53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has global scope.
54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the address has
54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of global scope, false if it is not
54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of global scope or it is not a multicast address
54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCGlobal() {
55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xff
55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0x0f) == 0x0e);
55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has node scope.
55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the address has
55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of node-local scope, false if it is not
56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of node-local scope or it is not a multicast address
56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCNodeLocal() {
56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xff
56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0x0f) == 0x01);
56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has link scope.
57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the address has
57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of link-local scope, false if it is not
57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of link-local scope or it is not a multicast address
57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCLinkLocal() {
57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xff
58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0x0f) == 0x02);
58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has site scope.
58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the address has
58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of site-local scope, false if it is not
58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         of site-local scope or it is not a multicast address
58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCSiteLocal() {
59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xff
59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0x0f) == 0x05);
59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the multicast address has organization scope.
59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the address has
60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         is a multicast address of organization-local scope,
60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         false if it is not of organization-local scope
60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or it is not a multicast address
60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isMCOrgLocal() {
60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ((ipaddress[0] & 0xff) == 0xff
60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                && (ipaddress[1] & 0x0f) == 0x08);
61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the raw IP address of this <code>InetAddress</code>
61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * object. The result is in network byte order: the highest order
61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * byte of the address is in <code>getAddress()[0]</code>.
61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the raw IP address of this object.
61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public byte[] getAddress() {
62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return ipaddress.clone();
62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
624f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    public byte[] getAddressInternal() {
625f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski        return ipaddress;
626f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski    }
627f7ab2bc37debba91864bfec6572a3e7bbe994c58Piotr Jastrzebski
62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the numeric scopeId, if this instance is associated with
63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * an interface. If no scoped_id is set, the returned value is zero.
63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the scopeId, or zero if not set.
63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     public int getScopeId () {
63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return scope_id;
63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     }
63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the scoped interface, if this instance was created with
64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * with a scoped interface.
64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the scoped interface, or null if not set.
64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     public NetworkInterface getScopedInterface () {
64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return scope_ifname;
64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     }
64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the IP address string in textual presentation. If the instance was created
65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * specifying a scope identifier then the scope id is appended to the IP address preceded by
65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * a "%" (per-cent) character. This can be either a numeric value or a string, depending on which
65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * was used to createthe instance.
65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  the raw IP address in a string format.
65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
65951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public String getHostAddress() {
66093c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak
66193c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        /* ----- BEGIN android -----
66293c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak           getnameinfo returns smarter representations
66351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        String s = numericToTextFormat(ipaddress);
66493c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        if (scope_ifname_set) {
66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            s = s + "%" + scope_ifname.getName();
66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } else if (scope_id_set) {
66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            s = s + "%" + scope_id;
66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
66993c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        return s;*/
67093c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak
67193c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        return Libcore.os.getnameinfo(this, NI_NUMERICHOST); // Can't throw.
67293c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak        // ----- END android -----
67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hashcode for this IP address.
67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  a hash code value for this IP address.
67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public int hashCode() {
68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (ipaddress != null) {
68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int hash = 0;
68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int i=0;
68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            while (i<INADDRSZ) {
68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int j=0;
68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int component=0;
68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                while (j<4 && i<INADDRSZ) {
69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    component = (component << 8) + ipaddress[i];
69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    j++;
69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    i++;
69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                hash += component;
69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return hash;
69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        } else {
69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Compares this object against the specified object.
70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The result is <code>true</code> if and only if the argument is
70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not <code>null</code> and it represents the same IP address as
70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this object.
70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two instances of <code>InetAddress</code> represent the same IP
71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * address if the length of the byte arrays returned by
71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <code>getAddress</code> is the same for both, and each of the
71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array components is the same for the byte arrays.
71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param   obj   the object to compare against.
71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  <code>true</code> if the objects are the same;
71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          <code>false</code> otherwise.
71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see     java.net.InetAddress#getAddress()
71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @Override
72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean equals(Object obj) {
72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (obj == null ||
72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            !(obj instanceof Inet6Address))
72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Inet6Address inetAddr = (Inet6Address)obj;
72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < INADDRSZ; i++) {
72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (ipaddress[i] != inetAddr.ipaddress[i])
72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Utility routine to check if the InetAddress is an
73751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * IPv4 compatible IPv6 address.
73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a <code>boolean</code> indicating if the InetAddress is
74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * an IPv4 compatible IPv6 address; or false if address is IPv4 address.
74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public boolean isIPv4CompatibleAddress() {
74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&
74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&
74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&
74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00))  {
75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return false;
75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Utilities
75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private final static int INT16SZ = 2;
75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Convert IPv6 binary address into presentation (printable) format.
75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param src a byte array representing the IPv6 numeric address
76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a String representing an IPv6 address in
76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         textual representation format
76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.4
76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static String numericToTextFormat(byte[] src)
76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuffer sb = new StringBuffer(39);
76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                          | (src[(i<<1)+1] & 0xff)));
77193c98aac54848043518d85216782a0801e79ffe2Przemyslaw Szczepaniak
77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i < (INADDRSZ / INT16SZ) -1 ) {
77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski               sb.append(":");
77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return sb.toString();
77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Perform class load-time initializations.
78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static native void init();
78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Following field is only used during (de)/serialization
78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private String ifname;
78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * default behavior is overridden in order to write the
79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * scope_ifname field as a String, rather than a NetworkInterface
79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * which is not serializable
79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private synchronized void writeObject(java.io.ObjectOutputStream s)
79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        throws IOException
79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (scope_ifname_set) {
79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ifname = scope_ifname.getName();
79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        s.defaultWriteObject();
80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
803