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