jdk/src/share/classes/java/net/Inet4Address.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
child 14014 da3648e13e67
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class represents an Internet Protocol version 4 (IPv4) address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Defined by <a href="http://www.ietf.org/rfc/rfc790.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <i>RFC&nbsp;790: Assigned Numbers</i></a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <a href="http://www.ietf.org/rfc/rfc1918.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <i>RFC&nbsp;1918: Address Allocation for Private Internets</i></a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Administratively Scoped IP Multicast</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <h4> <A NAME="format">Textual representation of IP addresses</a> </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Textual representation of IPv4 address used as input to methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * takes one of the following forms:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tr><td><tt>d.d.d.d</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <tr><td><tt>d.d.d</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <tr><td><tt>d.d</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <tr><td><tt>d</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> When four parts are specified, each is interpreted as a byte of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * data and assigned, from left to right, to the four bytes of an IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> When a three part address is specified, the last part is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * interpreted as a 16-bit quantity and placed in the right most two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * bytes of the network address. This makes the three part address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * format convenient for specifying Class B net- work addresses as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * 128.net.host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> When a two part address is supplied, the last part is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * interpreted as a 24-bit quantity and placed in the right most three
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * bytes of the network address. This makes the two part address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * format convenient for specifying Class A network addresses as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * net.host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p> When only one part is given, the value is stored directly in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * the network address without any byte rearrangement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p> For methods that return a textual representation as output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * value, the first form, i.e. a dotted-quad string, is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <h4> The Scope of a Multicast Address </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Historically the IPv4 TTL field in the IP header has doubled as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * multicast scope field: a TTL of 0 means node-local, 1 means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * link-local, up through 32 means site-local, up through 64 means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * region-local, up through 128 means continent-local, and up through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * 255 are global. However, the administrative scoping is preferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <i>RFC&nbsp;2365: Administratively Scoped IP Multicast</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
class Inet4Address extends InetAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    final static int INADDRSZ = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** use serialVersionUID from InetAddress, but Inet4Address instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *  is always replaced by an InetAddress instance before being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *  serialized */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final long serialVersionUID = 3286316764910316507L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Perform initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    Inet4Address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        hostName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        address = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        family = IPv4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    Inet4Address(String hostName, byte addr[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.hostName = hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.family = IPv4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (addr.length == INADDRSZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                address  = addr[3] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                address |= ((addr[2] << 8) & 0xFF00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                address |= ((addr[1] << 16) & 0xFF0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                address |= ((addr[0] << 24) & 0xFF000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    Inet4Address(String hostName, int address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.hostName = hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.family = IPv4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Replaces the object to be serialized with an InetAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the alternate object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws ObjectStreamException if a new object replacing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * object could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // will replace the to be serialized 'this' object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        InetAddress inet = new InetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        inet.hostName = this.hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        inet.address = this.address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * Prior to 1.4 an InetAddress was created with a family
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * based on the platform AF_INET value (usually 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * For compatibility reasons we must therefore write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * the InetAddress with this family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        inet.family = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return inet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * IP multicast address. IP multicast address is a Class D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * address i.e first four bits of the address are 1110.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * an IP multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean isMulticastAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return ((address & 0xf0000000) == 0xe0000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Utility routine to check if the InetAddress in a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return a <code>boolean</code> indicating if the Inetaddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public boolean isAnyLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return address == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * a loopback address; or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final int loopback = 2130706433; /* 127.0.0.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /* 127.x.x.x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        byte[] byteAddr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return byteAddr[0] == 127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * a link local address; or false if address is not a link local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public boolean isLinkLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // link-local unicast in IPv4 (169.254.0.0/16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // defined in "Documenting Special Use IPv4 Address Blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // that have been Registered with IANA" by Bill Manning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // draft-manning-dsua-06.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return (((address >>> 24) & 0xFF) == 169)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            && (((address >>> 16) & 0xFF) == 254);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return a <code>boolean</code> indicating if the InetAddress is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * a site local address; or false if address is not a site local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public boolean isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // refer to RFC 1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // 10/8 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // 172.16/12 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // 192.168/16 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return (((address >>> 24) & 0xFF) == 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            || ((((address >>> 24) & 0xFF) == 172)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                && (((address >>> 16) & 0xF0) == 16))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            || ((((address >>> 24) & 0xFF) == 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                && (((address >>> 16) & 0xFF) == 168));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         of global scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public boolean isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // 224.0.1.0 to 238.255.255.255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        byte[] byteAddr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
              byteAddr[2] == 0);
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
     * Utility routine to check if the multicast address has node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *         is a multicast address of node-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *         of node-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // unless ttl == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // 224.0.0/24 prefix and ttl == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return (((address >>> 24) & 0xFF) == 224)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            && (((address >>> 16) & 0xFF) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            && (((address >>> 8) & 0xFF) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // 239.255/16 prefix or ttl < 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            && (((address >>> 16) & 0xFF) == 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return a <code>boolean</code> indicating if the address has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // 239.192 - 239.195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            && (((address >>> 16) & 0xFF) >= 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            && (((address >>> 16) & 0xFF) <= 195);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns the raw IP address of this <code>InetAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * object. The result is in network byte order: the highest order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * byte of the address is in <code>getAddress()[0]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public byte[] getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        byte[] addr = new byte[INADDRSZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        addr[0] = (byte) ((address >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        addr[1] = (byte) ((address >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        addr[2] = (byte) ((address >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        addr[3] = (byte) (address & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns the IP address string in textual presentation form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return  the raw IP address in a string format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @since   JDK1.0.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return numericToTextFormat(getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * not <code>null</code> and it represents the same IP address as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Two instances of <code>InetAddress</code> represent the same IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * address if the length of the byte arrays returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <code>getAddress</code> is the same for both, and each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return (obj != null) && (obj instanceof Inet4Address) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            (((InetAddress)obj).address == address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    // Utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Converts IPv4 binary address into a string suitable for presentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param src a byte array representing an IPv4 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return a String representing the IPv4 address in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *         textual representation format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    static String numericToTextFormat(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
}