jdk/src/share/classes/java/net/Inet6Address.java
author michaelm
Wed, 13 Feb 2013 10:40:31 +0000
changeset 16870 f35b2bd19761
parent 14783 2ecc993a8e67
child 17198 32db080a2a70
permissions -rw-r--r--
8000724: Improve networking serialization Summary: delegate InetAddress fields to a holder object Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
     2
 * Copyright (c) 2000, 2011, 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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class represents an Internet Protocol version 6 (IPv6) address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Defined by <a href="http://www.ietf.org/rfc/rfc2373.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <i>RFC&nbsp;2373: IP Version 6 Addressing Architecture</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <h4> <A NAME="format">Textual representation of IP addresses</a> </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Textual representation of IPv6 address used as input to methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * takes one of the following forms:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   <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
    45
 *   where the 'x's are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   the hexadecimal values of the eight 16-bit pieces of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   address. This is the full form.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   <tr><td><tt>1080:0:0:0:8:800:200C:417A</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   <p> Note that it is not necessary to write the leading zeros in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   an individual field. However, there must be at least one numeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   in every field, except as described below.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   <li><p> Due to some methods of allocating certain styles of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   addresses, it will be common for addresses to contain long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   strings of zero bits. In order to make writing addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   containing zero bits easier, a special syntax is available to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   compress the zeros. The use of "::" indicates multiple groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   of 16-bits of zeros. The "::" can only appear once in an address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   The "::" can also be used to compress the leading and/or trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   zeros in an address. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   <tr><td><tt>1080::8:800:200C:417A</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   <li><p> An alternative form that is sometimes more convenient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   when dealing with a mixed environment of IPv4 and IPv6 nodes is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   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
    73
 *   of the six high-order 16-bit pieces of the address, and the 'd's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   are the decimal values of the four low-order 8-bit pieces of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   standard IPv4 representation address, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   <tr><td><tt>::FFFF:129.144.52.38</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   <tr><td><tt>::129.144.52.38</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   <p> where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   general forms of an IPv4-mapped IPv6 address and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   IPv4-compatible IPv6 address. Note that the IPv4 portion must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   in the "d.d.d.d" form. The following forms are invalid:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   <tr><td><tt>::FFFF:d.d.d</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   <tr><td><tt>::FFFF:d.d</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   <tr><td><tt>::d.d.d</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <tr><td><tt>::d.d</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   <p> The following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   <tr><td><tt>::FFFF:d</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <p> is valid, however it is an unconventional representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   the IPv4-compatible IPv6 address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   <tr><td><tt>::255.255.0.d</tt><td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   <p> while "::d" corresponds to the general IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   "0:0:0:0:0:0:0:d".</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p> For methods that return a textual representation as output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * value, the full form is used. Inet6Address will return the full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * form because it is unambiguous when used in combination with other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * textual data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <h4> Special IPv6 address </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <blockquote>
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   119
 * <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
   120
 * <tr><th valign=top><i>IPv4-mapped address</i></th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *         <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *         represent an IPv4 address. It allows the native program to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *         use the same address data structure and also the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *         socket when communicating with both IPv4 and IPv6 nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *         <p>In InetAddress and Inet6Address, it is used for internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *         representation; it has no functional role. Java will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *         return an IPv4-mapped address.  These classes can take an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *         IPv4-mapped address as input, both in byte array and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *         representation. However, it will be converted into an IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *         address.</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <p>
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   134
 * <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
   135
 *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   136
 * <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
   137
 * 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
   138
 * 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
   139
 *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   140
 * <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
   141
 * 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
   142
 * 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
   143
 * 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
   144
 * 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
   145
 * 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
   146
 * IPv6 address.
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   147
 *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   148
 * <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
   149
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <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
   152
 * 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
   153
 * specified in two ways.
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   154
 * <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
   155
 * 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
   156
 * 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
   157
 * 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
   158
 * 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
   159
 * <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
   160
 * {@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
   161
 * 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
   162
 * 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
   163
 * NetworkInterface.</li></ol>
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   164
 *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   165
 * <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
   166
 * 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
   167
 * used to find out the current scope ids configured on the system.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
class Inet6Address extends InetAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    final static int INADDRSZ = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * cached scope_id - for link-local address use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   178
    private transient int cached_scope_id;  // 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Holds a 128-bit (16 bytes) IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    byte[] ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   188
     * scope_id. The scope specified when the object is created. If the object
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   189
     * is created with an interface name, then the scope_id is not determined
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   190
     * until the time it is needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   192
    private int scope_id;  // 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * This will be set to true when the scope_id field contains a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * integer scope_id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   198
    private boolean scope_id_set;  // false
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * scoped interface. scope_id is derived from this as the scope_id of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * address whose scope is the same as this address for the named interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   204
    private transient NetworkInterface scope_ifname;  // null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private static final long serialVersionUID = 6880410070516793377L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   208
    // Perform native initialization
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   209
    static { init(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    Inet6Address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        super();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   213
        holder().hostName = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        ipaddress = new byte[INADDRSZ];
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   215
        holder().family = IPv6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /* checking of value for scope_id should be done by caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * scope_id must be >= 0, or -1 to indicate not being set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    Inet6Address(String hostName, byte addr[], int scope_id) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   222
        holder().hostName = hostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (addr.length == INADDRSZ) { // normal IPv6 address
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   224
            holder().family = IPv6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            ipaddress = addr.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (scope_id >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            this.scope_id = scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            scope_id_set = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    Inet6Address(String hostName, byte addr[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            initif (hostName, addr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (UnknownHostException e) {} /* cant happen if ifname is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   239
    Inet6Address (String hostName, byte addr[], NetworkInterface nif)
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   240
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   241
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        initif (hostName, addr, nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   245
    Inet6Address (String hostName, byte addr[], String ifname)
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   246
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   247
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        initstr (hostName, addr, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   252
     * 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
   253
     * 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
   254
     * 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
   255
     * 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
   256
     * UnknownHostException if the given interface does not have a numeric
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * scope_id assigned for the given address type (eg. link-local or site-local).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * scoped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param nif an interface this address must be associated with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @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
   265
     * @throws  UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   266
     *          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
   267
     *          have a numeric scope_id assigned for the given address type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   271
    public static Inet6Address getByAddress(String host, byte[] addr,
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   272
                                            NetworkInterface nif)
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   273
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   274
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (addr.length == Inet6Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                return new Inet6Address(host, addr, nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        throw new UnknownHostException("addr is of illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   289
     * 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
   290
     * 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
   291
     * 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
   292
     * if it corresponds to any interface on the system.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * scoped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param scope_id the numeric scope_id for the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @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
   300
     * @throws  UnknownHostException  if IP address is of illegal length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   304
    public static Inet6Address getByAddress(String host, byte[] addr,
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   305
                                            int scope_id)
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   306
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   307
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (addr.length == Inet6Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return new Inet6Address(host, addr, scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        throw new UnknownHostException("addr is of illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   321
    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
   322
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   323
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            NetworkInterface nif = NetworkInterface.getByName (ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (nif == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                throw new UnknownHostException ("no such interface " + ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            initif (hostName, addr, nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new UnknownHostException ("SocketException thrown" + ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   335
    private void initif(String hostName, byte addr[],NetworkInterface nif)
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   336
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   337
    {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   338
        holder().hostName = hostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (addr.length == INADDRSZ) { // normal IPv6 address
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   340
            holder().family = IPv6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            ipaddress = addr.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (nif != null) {
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   344
            scope_ifname = nif;
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   345
            scope_id = deriveNumericScope(nif);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            scope_id_set = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /* check the two Ipv6 addresses and return false if they are both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * non global address types, but not the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * (ie. one is sitelocal and the other linklocal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * return true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private boolean differentLocalAddressTypes(Inet6Address other) {
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   356
        if (isLinkLocalAddress() && !other.isLinkLocalAddress())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return false;
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   358
        if (isSiteLocalAddress() && !other.isSiteLocalAddress())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   363
    private int deriveNumericScope(NetworkInterface ifc)
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   364
        throws UnknownHostException
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   365
    {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   366
        Enumeration<InetAddress> addresses = ifc.getInetAddresses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        while (addresses.hasMoreElements()) {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   368
            InetAddress addr = addresses.nextElement();
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   369
            if (!(addr instanceof Inet6Address)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   372
            Inet6Address ia6_addr = (Inet6Address)addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            /* check if site or link local prefixes match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (!differentLocalAddressTypes(ia6_addr)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                /* type not the same, so carry on searching */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            /* found a matching address - return its scope_id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return ia6_addr.scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        throw new UnknownHostException ("no scope_id found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   384
    private int deriveNumericScope(String ifname) throws UnknownHostException {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   385
        Enumeration<NetworkInterface> en;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            en = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } catch (SocketException e) {
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   389
            throw new UnknownHostException(
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   390
                    "could not enumerate local network interfaces");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        while (en.hasMoreElements()) {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   393
            NetworkInterface ifc = en.nextElement();
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   394
            if (ifc.getName().equals(ifname)) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   395
                Enumeration<InetAddress> addresses = ifc.getInetAddresses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                while (addresses.hasMoreElements()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   397
                    InetAddress addr = addresses.nextElement();
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   398
                    if (!(addr instanceof Inet6Address)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    }
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   401
                    Inet6Address ia6_addr = (Inet6Address)addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    /* check if site or link local prefixes match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    if (!differentLocalAddressTypes(ia6_addr)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        /* type not the same, so carry on searching */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    /* found a matching address - return its scope_id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    return ia6_addr.scope_id;
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
        throw new UnknownHostException(
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   413
                "No matching address found for interface : " +ifname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * restore the state of this object from stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * including the scope information, only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * scoped interface name is valid on this system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        throws IOException, ClassNotFoundException {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   423
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   424
        if (getClass().getClassLoader() != null) {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   425
            throw new SecurityException ("invalid address type");
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   426
        }
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   427
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   430
        if (ifname != null && !ifname.equals("")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                scope_ifname = NetworkInterface.getByName(ifname);
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   433
                if (scope_ifname != null) {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   434
                    try {
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   435
                        scope_id = deriveNumericScope(scope_ifname);
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   436
                    } catch (UnknownHostException e) {
6004
f88fd15b64b9 6966846: Incorrect assertion in java.net.Inet6Address.readObject
chegar
parents: 5506
diff changeset
   437
                        // typically should not happen, but it may be that
f88fd15b64b9 6966846: Incorrect assertion in java.net.Inet6Address.readObject
chegar
parents: 5506
diff changeset
   438
                        // the machine being used for deserialization has
f88fd15b64b9 6966846: Incorrect assertion in java.net.Inet6Address.readObject
chegar
parents: 5506
diff changeset
   439
                        // the same interface name but without IPv6 configured.
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   440
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } catch (SocketException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        /* if ifname was not supplied, then the numeric info is used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        ipaddress = ipaddress.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // Check that our invariants are satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (ipaddress.length != INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new InvalidObjectException("invalid address length: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                             ipaddress.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14783
diff changeset
   455
        if (holder().getFamily() != IPv6) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            throw new InvalidObjectException("invalid address family type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Utility routine to check if the InetAddress is an IP multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * address. 11111111 at the start of the address identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * address as being a multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   465
     * @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
   466
     *         multicast address
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
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   470
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public boolean isMulticastAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return ((ipaddress[0] & 0xff) == 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * 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
   477
     *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   478
     * @return a {@code boolean} indicating if the Inetaddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *         a wildcard address.
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   480
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   483
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public boolean isAnyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        byte test = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        for (int i = 0; i < INADDRSZ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            test |= ipaddress[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return (test == 0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   495
     * @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
   496
     *         address; or false otherwise.
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   497
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   500
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        byte test = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        for (int i = 0; i < 15; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            test |= ipaddress[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return (test == 0x00) && (ipaddress[15] == 0x01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   512
     * @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
   513
     *         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
   514
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   517
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public boolean isLinkLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return ((ipaddress[0] & 0xff) == 0xfe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                && (ipaddress[1] & 0xc0) == 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   526
     * @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
   527
     *         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
   528
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   531
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public boolean isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return ((ipaddress[0] & 0xff) == 0xfe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                && (ipaddress[1] & 0xc0) == 0xc0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   540
     * @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
   541
     *         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
   542
     *         it is not a multicast address
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   543
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   546
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public boolean isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                && (ipaddress[1] & 0x0f) == 0x0e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Utility routine to check if the multicast address has node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   555
     * @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
   556
     *         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
   557
     *         scope or it is not a multicast address
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   558
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   561
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                && (ipaddress[1] & 0x0f) == 0x01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   570
     * @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
   571
     *         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
   572
     *         scope or it is not a multicast address
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   573
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   576
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                && (ipaddress[1] & 0x0f) == 0x02);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   585
     * @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
   586
     *         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
   587
     *         scope or it is not a multicast address
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   588
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   591
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                && (ipaddress[1] & 0x0f) == 0x05);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   600
     * @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
   601
     *         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
   602
     *         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
   603
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   606
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                && (ipaddress[1] & 0x0f) == 0x08);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   613
     * 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
   614
     * 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
   615
     * {@code getAddress()[0]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   619
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public byte[] getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return ipaddress.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Returns the numeric scopeId, if this instance is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * an interface. If no scoped_id is set, the returned value is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @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
   629
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   632
     public int getScopeId() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Returns the scoped interface, if this instance was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * with a scoped interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @return the scoped interface, or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   643
     public NetworkInterface getScopedInterface() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return scope_ifname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   648
     * 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
   649
     * 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
   650
     * 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
   651
     * 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
   652
     * the instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return  the raw IP address in a string format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   656
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        String s = numericToTextFormat(ipaddress);
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   659
        if (scope_ifname != null) { /* must check this first */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            s = s + "%" + scope_ifname.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        } else if (scope_id_set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            s = s + "%" + scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   672
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (ipaddress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            while (i<INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                int component=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                while (j<4 && i<INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    component = (component << 8) + ipaddress[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                hash += component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   696
     * 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
   697
     * 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
   698
     * the same IP address as this object.
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   699
     *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   700
     * <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
   701
     * 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
   702
     * 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
   703
     * arrays.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @param   obj   the object to compare against.
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   706
     *
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   707
     * @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
   708
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   711
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public boolean equals(Object obj) {
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   713
        if (obj == null || !(obj instanceof Inet6Address))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        Inet6Address inetAddr = (Inet6Address)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        for (int i = 0; i < INADDRSZ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (ipaddress[i] != inetAddr.ipaddress[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * IPv4 compatible IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   729
     * @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
   730
     *         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
   731
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public boolean isIPv4CompatibleAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    // Utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    private final static int INT16SZ = 2;
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   748
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Convert IPv6 binary address into presentation (printable) format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param src a byte array representing the IPv6 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @return a String representing an IPv6 address in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *         textual representation format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   757
    static String numericToTextFormat(byte[] src) {
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   758
        StringBuilder sb = new StringBuilder(39);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                          | (src[(i<<1)+1] & 0xff)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (i < (INADDRSZ / INT16SZ) -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
               sb.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Following field is only used during (de)/serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    private String ifname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * default behavior is overridden in order to write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * scope_ifname field as a String, rather than a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * which is not serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    {
14783
2ecc993a8e67 8004675: Inet6Address.getHostAddress should use string scope identifier where available
chegar
parents: 10596
diff changeset
   787
        if (scope_ifname != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            ifname = scope_ifname.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
}