jdk/src/java.base/share/classes/java/net/Inet4Address.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
child 33301 9ee33abf5b4c
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 16870
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 16870
diff changeset
    39
 * <h3> <A NAME="format">Textual representation of IP addresses</a> </h3>
2
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">
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
    45
 * <tr><td>{@code d.d.d.d}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
    46
 * <tr><td>{@code d.d.d}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
    47
 * <tr><td>{@code d.d}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
    48
 * <tr><td>{@code d}</td></tr>
2
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 {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    87
    static final int INADDRSZ = 4;
2
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();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   103
        holder().hostName = null;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   104
        holder().address = 0;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   105
        holder().family = IPv4;
2
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[]) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   109
        holder().hostName = hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   110
        holder().family = IPv4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (addr.length == INADDRSZ) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   113
                int address  = addr[3] & 0xFF;
2
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);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   117
                holder().address = address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    Inet4Address(String hostName, int address) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   122
        holder().hostName = hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   123
        holder().family = IPv4;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   124
        holder().address = address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Replaces the object to be serialized with an InetAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return the alternate object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws ObjectStreamException if a new object replacing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * object could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // will replace the to be serialized 'this' object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        InetAddress inet = new InetAddress();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   138
        inet.holder().hostName = holder().getHostName();
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   139
        inet.holder().address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Prior to 1.4 an InetAddress was created with a family
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * based on the platform AF_INET value (usually 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * For compatibility reasons we must therefore write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * the InetAddress with this family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         */
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   147
        inet.holder().family = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return inet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * IP multicast address. IP multicast address is a Class D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * address i.e first four bits of the address are 1110.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   156
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * an IP multicast address
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19069
diff changeset
   158
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public boolean isMulticastAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   161
        return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Utility routine to check if the InetAddress in a wildcard address.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   166
     * @return a {@code boolean} indicating if the Inetaddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public boolean isAnyLocalAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   171
        return holder().getAddress() == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   177
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * a loopback address; or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
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
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   190
     * @return a {@code boolean} indicating if the InetAddress is
2
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
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   199
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return (((address >>> 24) & 0xFF) == 169)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            && (((address >>> 16) & 0xFF) == 254);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   207
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * a site local address; or false if address is not a site local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public boolean isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // refer to RFC 1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // 10/8 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // 172.16/12 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // 192.168/16 prefix
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   216
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return (((address >>> 24) & 0xFF) == 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            || ((((address >>> 24) & 0xFF) == 172)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                && (((address >>> 16) & 0xF0) == 16))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            || ((((address >>> 24) & 0xFF) == 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                && (((address >>> 16) & 0xFF) == 168));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   227
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *         of global scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public boolean isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // 224.0.1.0 to 238.255.255.255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        byte[] byteAddr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
              byteAddr[2] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Utility routine to check if the multicast address has node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   243
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         is a multicast address of node-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         of node-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // unless ttl == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return false;
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
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   256
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // 224.0.0/24 prefix and ttl == 1
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   263
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return (((address >>> 24) & 0xFF) == 224)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            && (((address >>> 16) & 0xFF) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            && (((address >>> 8) & 0xFF) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   272
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // 239.255/16 prefix or ttl < 32
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   279
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            && (((address >>> 16) & 0xFF) == 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   287
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // 239.192 - 239.195
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   295
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            && (((address >>> 16) & 0xFF) >= 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            && (((address >>> 16) & 0xFF) <= 195);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   302
     * Returns the raw IP address of this {@code InetAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * object. The result is in network byte order: the highest order
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   304
     * byte of the address is in {@code getAddress()[0]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public byte[] getAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   309
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        byte[] addr = new byte[INADDRSZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        addr[0] = (byte) ((address >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        addr[1] = (byte) ((address >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        addr[2] = (byte) ((address >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        addr[3] = (byte) (address & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Returns the IP address string in textual presentation form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return  the raw IP address in a string format.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19069
diff changeset
   323
     * @since   1.0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return numericToTextFormat(getAddress());
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
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public int hashCode() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   335
        return holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   340
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   341
     * not {@code null} and it represents the same IP address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   344
     * Two instances of {@code InetAddress} represent the same IP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * address if the length of the byte arrays returned by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   346
     * {@code getAddress} is the same for both, and each of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   350
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   351
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return (obj != null) && (obj instanceof Inet4Address) &&
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   356
            (((InetAddress)obj).holder().getAddress() == holder().getAddress());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    // Utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Converts IPv4 binary address into a string suitable for presentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param src a byte array representing an IPv4 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return a String representing the IPv4 address in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         textual representation format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    static String numericToTextFormat(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}