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