jdk/src/share/classes/java/net/Inet6Address.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 902 bbfb1e2369b2
child 5506 202f599c92aa
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 902
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InvalidObjectException;
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <table cellspacing=2 summary="Description of IPv4-mapped address"> <tr><th valign=top><i>IPv4-mapped address</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *         <td>Of the form::ffff:w.x.y.z, this IPv6 address is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *         represent an IPv4 address. It allows the native program to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *         use the same address data structure and also the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *         socket when communicating with both IPv4 and IPv6 nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *         <p>In InetAddress and Inet6Address, it is used for internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *         representation; it has no functional role. Java will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *         return an IPv4-mapped address.  These classes can take an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *         IPv4-mapped address as input, both in byte array and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *         representation. However, it will be converted into an IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *         address.</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <h4> <A NAME="scoped">Textual representation of IPv6 scoped addresses</a> </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * The textual representation of IPv6 addresses as described above can be extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * to specify IPv6 scoped addresses. This extension to the basic addressing architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * is described in [draft-ietf-ipngwg-scoping-arch-04.txt].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * Because link-local and site-local addresses are non-global, it is possible that different hosts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * may have the same destination address and may be reachable through different interfaces on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * same originating system. In this case, the originating system is said to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * to multiple zones of the same scope. In order to disambiguate which is the intended destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * The general format for specifying the <i>scope_id</i> is the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <p> The IPv6-address is a literal IPv6 address as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * The <i>scope_id</i> refers to an interface on the local system, and it can be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * in two ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer that identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * particular interface and scope as understood by the system. Usually, the numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * values can be determined through administration tools on the system. Each interface may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * have multiple values, one for each scope. If the scope is unspecified, then the default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * used is zero.</li><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <li><i>As a string.</i> This must be the exact string that is returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * {@link java.net.NetworkInterface#getName()} for the particular interface in question.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * When an Inet6Address is created in this way, the numeric scope-id is determined at the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * the object is created by querying the relevant NetworkInterface.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * </ol><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Note also, that the numeric <i>scope_id</i> can be retrieved from Inet6Address instances returned from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * NetworkInterface class. This can be used to find out the current scope ids configured on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
class Inet6Address extends InetAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    final static int INADDRSZ = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * cached scope_id - for link-local address use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private transient int cached_scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Holds a 128-bit (16 bytes) IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    byte[] ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * scope_id. The scope specified when the object is created. If the object is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * with an interface name, then the scope_id is not determined until the time it is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private int scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This will be set to true when the scope_id field contains a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * integer scope_id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private boolean scope_id_set = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * scoped interface. scope_id is derived from this as the scope_id of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * address whose scope is the same as this address for the named interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private transient NetworkInterface scope_ifname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * set if the object is constructed with a scoped interface instead of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * numeric scope id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private boolean scope_ifname_set = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private static final long serialVersionUID = 6880410070516793377L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Perform initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    Inet6Address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        hostName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        ipaddress = new byte[INADDRSZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        family = IPv6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /* checking of value for scope_id should be done by caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * scope_id must be >= 0, or -1 to indicate not being set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    Inet6Address(String hostName, byte addr[], int scope_id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        this.hostName = hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (addr.length == INADDRSZ) { // normal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            family = IPv6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            ipaddress = addr.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (scope_id >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            this.scope_id = scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            scope_id_set = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    Inet6Address(String hostName, byte addr[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            initif (hostName, addr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (UnknownHostException e) {} /* cant happen if ifname is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        initif (hostName, addr, nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        initstr (hostName, addr, ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * except that the IPv6 scope_id is set to the value corresponding to the given interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * for the address type specified in <code>addr</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * The call will fail with an UnknownHostException if the given interface does not have a numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * scope_id assigned for the given address type (eg. link-local or site-local).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * scoped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param nif an interface this address must be associated with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return  an Inet6Address object created from the raw IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @exception  UnknownHostException  if IP address is of illegal length, or if the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          does not have a numeric scope_id assigned for the given address type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (addr.length == Inet6Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return new Inet6Address(host, addr, nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        throw new UnknownHostException("addr is of illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * except that the IPv6 scope_id is set to the given numeric value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * The scope_id is not checked to determine if it corresponds to any interface on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * scoped addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param addr the raw IP address in network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param scope_id the numeric scope_id for the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return  an Inet6Address object created from the raw IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception  UnknownHostException  if IP address is of illegal length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static Inet6Address getByAddress(String host, byte[] addr, int scope_id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (host != null && host.length() > 0 && host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (host.charAt(host.length()-1) == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                host = host.substring(1, host.length() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (addr.length == Inet6Address.INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return new Inet6Address(host, addr, scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        throw new UnknownHostException("addr is of illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private void initstr (String hostName, byte addr[], String ifname) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            NetworkInterface nif = NetworkInterface.getByName (ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (nif == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                throw new UnknownHostException ("no such interface " + ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            initif (hostName, addr, nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new UnknownHostException ("SocketException thrown" + ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        this.hostName = hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (addr.length == INADDRSZ) { // normal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            family = IPv6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            ipaddress = addr.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (nif != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            this.scope_ifname = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            scope_ifname_set = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            scope_id = deriveNumericScope (nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            scope_id_set = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /* check the two Ipv6 addresses and return false if they are both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * non global address types, but not the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * (ie. one is sitelocal and the other linklocal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * return true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private boolean differentLocalAddressTypes(Inet6Address other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (isLinkLocalAddress() && !other.isLinkLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (isSiteLocalAddress() && !other.isSiteLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   358
        Enumeration<InetAddress> addresses = ifc.getInetAddresses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        while (addresses.hasMoreElements()) {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   360
            InetAddress addr = addresses.nextElement();
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   361
            if (!(addr instanceof Inet6Address)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   364
            Inet6Address ia6_addr = (Inet6Address)addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            /* check if site or link local prefixes match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (!differentLocalAddressTypes(ia6_addr)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                /* type not the same, so carry on searching */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            /* found a matching address - return its scope_id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return ia6_addr.scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        throw new UnknownHostException ("no scope_id found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private int deriveNumericScope (String ifname) throws UnknownHostException {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   377
        Enumeration<NetworkInterface> en;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            en = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new UnknownHostException ("could not enumerate local network interfaces");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        while (en.hasMoreElements()) {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   384
            NetworkInterface ifc = en.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (ifc.getName().equals (ifname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                Enumeration addresses = ifc.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                while (addresses.hasMoreElements()) {
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   388
                    InetAddress addr = (InetAddress)addresses.nextElement();
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   389
                    if (!(addr instanceof Inet6Address)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   392
                    Inet6Address ia6_addr = (Inet6Address)addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    /* check if site or link local prefixes match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    if (!differentLocalAddressTypes(ia6_addr)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        /* type not the same, so carry on searching */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    /* found a matching address - return its scope_id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    return ia6_addr.scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        throw new UnknownHostException ("No matching address found for interface : " +ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * restore the state of this object from stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * including the scope information, only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * scoped interface name is valid on this system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        scope_ifname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        scope_ifname_set = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (ifname != null && !"".equals (ifname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                scope_ifname = NetworkInterface.getByName(ifname);
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   420
                if (scope_ifname == null) {
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   421
                    /* the interface does not exist on this system, so we clear
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   422
                     * the scope information completely */
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   423
                    scope_id_set = false;
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   424
                    scope_ifname_set = false;
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   425
                    scope_id = 0;
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   426
                } else {
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   427
                    try {
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   428
                        scope_id = deriveNumericScope (scope_ifname);
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   429
                    } catch (UnknownHostException e) {
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   430
                        // should not happen
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   431
                        assert false;
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   432
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } catch (SocketException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        /* if ifname was not supplied, then the numeric info is used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        ipaddress = ipaddress.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // Check that our invariants are satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (ipaddress.length != INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new InvalidObjectException("invalid address length: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                             ipaddress.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (family != IPv6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new InvalidObjectException("invalid address family type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Utility routine to check if the InetAddress is an IP multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * address. 11111111 at the start of the address identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * address as being a multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * an IP multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   461
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public boolean isMulticastAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return ((ipaddress[0] & 0xff) == 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Utility routine to check if the InetAddress in a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return a <code>boolean</code> indicating if the Inetaddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *         a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   472
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public boolean isAnyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        byte test = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        for (int i = 0; i < INADDRSZ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            test |= ipaddress[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return (test == 0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * a loopback address; or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   488
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        byte test = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        for (int i = 0; i < 15; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            test |= ipaddress[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return (test == 0x00) && (ipaddress[15] == 0x01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * a link local address; or false if address is not a link local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   504
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public boolean isLinkLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return ((ipaddress[0] & 0xff) == 0xfe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                && (ipaddress[1] & 0xc0) == 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * a site local address; or false if address is not a site local unicast address.
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 isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return ((ipaddress[0] & 0xff) == 0xfe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                && (ipaddress[1] & 0xc0) == 0xc0);
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 multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *         of global scope or it is not a multicast address
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 isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                && (ipaddress[1] & 0x0f) == 0x0e);
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 node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *         is a multicast address of node-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *         of node-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   545
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                && (ipaddress[1] & 0x0f) == 0x01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   559
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                && (ipaddress[1] & 0x0f) == 0x02);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   573
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                && (ipaddress[1] & 0x0f) == 0x05);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   588
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return ((ipaddress[0] & 0xff) == 0xff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                && (ipaddress[1] & 0x0f) == 0x08);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * Returns the raw IP address of this <code>InetAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * object. The result is in network byte order: the highest order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * byte of the address is in <code>getAddress()[0]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   601
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public byte[] getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return ipaddress.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Returns the numeric scopeId, if this instance is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * an interface. If no scoped_id is set, the returned value is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @return the scopeId, or zero if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     public int getScopeId () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Returns the scoped interface, if this instance was created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * with a scoped interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return the scoped interface, or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     public NetworkInterface getScopedInterface () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return scope_ifname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Returns the IP address string in textual presentation. If the instance was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * specifying a scope identifier then the scope id is appended to the IP address preceded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * a "%" (per-cent) character. This can be either a numeric value or a string, depending on which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * was used to createthe instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return  the raw IP address in a string format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   636
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        String s = numericToTextFormat(ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (scope_ifname_set) { /* must check this first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            s = s + "%" + scope_ifname.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } else if (scope_id_set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            s = s + "%" + scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   652
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (ipaddress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            while (i<INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                int component=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                while (j<4 && i<INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    component = (component << 8) + ipaddress[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                hash += component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * not <code>null</code> and it represents the same IP address as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Two instances of <code>InetAddress</code> represent the same IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * address if the length of the byte arrays returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <code>getAddress</code> is the same for both, and each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
902
bbfb1e2369b2 6656849: NullPointerException thrown while de-serializing IPV6 Address.
jccollet
parents: 2
diff changeset
   691
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (obj == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            !(obj instanceof Inet6Address))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        Inet6Address inetAddr = (Inet6Address)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        for (int i = 0; i < INADDRSZ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (ipaddress[i] != inetAddr.ipaddress[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * IPv4 compatible IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * an IPv4 compatible IPv6 address; or false if address is IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public boolean isIPv4CompatibleAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00))  {
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
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    // Utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    private final static int INT16SZ = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Convert IPv6 binary address into presentation (printable) format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @param src a byte array representing the IPv6 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @return a String representing an IPv6 address in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *         textual representation format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    static String numericToTextFormat(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        StringBuffer sb = new StringBuffer(39);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                          | (src[(i<<1)+1] & 0xff)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if (i < (INADDRSZ / INT16SZ) -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
               sb.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Following field is only used during (de)/serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private String ifname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * default behavior is overridden in order to write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * scope_ifname field as a String, rather than a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * which is not serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (scope_ifname_set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            ifname = scope_ifname.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
}