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