src/java.base/share/classes/java/net/Inet4Address.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45124 jdk/src/java.base/share/classes/java/net/Inet4Address.java@144479e89cdb
child 47478 438e0c9f2f17
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
     2
 * Copyright (c) 2000, 2017, 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
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 42474
diff changeset
    39
 * <h3> <a id="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
 *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    44
 * <blockquote><ul style="list-style-type:none">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    45
 * <li>{@code d.d.d.d}</li>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    46
 * <li>{@code d.d.d}</li>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    47
 * <li>{@code d.d}</li>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    48
 * <li>{@code d}</li>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44844
diff changeset
    49
 * </ul></blockquote>
2
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
        }
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents: 32649
diff changeset
   120
        holder().originalHostName = hostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    Inet4Address(String hostName, int address) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   123
        holder().hostName = hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   124
        holder().family = IPv4;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   125
        holder().address = address;
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents: 32649
diff changeset
   126
        holder().originalHostName = hostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Replaces the object to be serialized with an InetAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the alternate object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws ObjectStreamException if a new object replacing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * object could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // will replace the to be serialized 'this' object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        InetAddress inet = new InetAddress();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   140
        inet.holder().hostName = holder().getHostName();
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   141
        inet.holder().address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * Prior to 1.4 an InetAddress was created with a family
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * based on the platform AF_INET value (usually 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * For compatibility reasons we must therefore write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * the InetAddress with this family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         */
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   149
        inet.holder().family = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return inet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * IP multicast address. IP multicast address is a Class D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * address i.e first four bits of the address are 1110.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   158
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * an IP multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public boolean isMulticastAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   162
        return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
40118
f63c761ec2d2 8162819: fix minor Javadoc issues and remove warnings in java.net.Socket
clanger
parents: 33301
diff changeset
   166
     * Utility routine to check if the InetAddress is a wildcard address.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   167
     * @return a {@code boolean} indicating if the Inetaddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *         a wildcard address.
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        /* 127.x.x.x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        byte[] byteAddr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return byteAddr[0] == 127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   189
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * a link local address; or false if address is not a link local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public boolean isLinkLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // link-local unicast in IPv4 (169.254.0.0/16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // defined in "Documenting Special Use IPv4 Address Blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // that have been Registered with IANA" by Bill Manning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // draft-manning-dsua-06.txt
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   197
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return (((address >>> 24) & 0xFF) == 169)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            && (((address >>> 16) & 0xFF) == 254);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Utility routine to check if the InetAddress is a site local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   205
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * a site local address; or false if address is not a site local unicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public boolean isSiteLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // refer to RFC 1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // 10/8 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // 172.16/12 prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // 192.168/16 prefix
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   213
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return (((address >>> 24) & 0xFF) == 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            || ((((address >>> 24) & 0xFF) == 172)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                && (((address >>> 16) & 0xF0) == 16))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            || ((((address >>> 24) & 0xFF) == 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                && (((address >>> 16) & 0xFF) == 168));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   224
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         of global scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public boolean isMCGlobal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // 224.0.1.0 to 238.255.255.255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        byte[] byteAddr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
              byteAddr[2] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Utility routine to check if the multicast address has node scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   239
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *         is a multicast address of node-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         of node-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // unless ttl == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   251
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // 224.0.0/24 prefix and ttl == 1
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   257
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return (((address >>> 24) & 0xFF) == 224)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            && (((address >>> 16) & 0xFF) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            && (((address >>> 8) & 0xFF) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   266
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // 239.255/16 prefix or ttl < 32
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   272
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            && (((address >>> 16) & 0xFF) == 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   280
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // 239.192 - 239.195
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   287
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            && (((address >>> 16) & 0xFF) >= 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            && (((address >>> 16) & 0xFF) <= 195);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   294
     * Returns the raw IP address of this {@code InetAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * object. The result is in network byte order: the highest order
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   296
     * byte of the address is in {@code getAddress()[0]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public byte[] getAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   301
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        byte[] addr = new byte[INADDRSZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        addr[0] = (byte) ((address >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        addr[1] = (byte) ((address >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        addr[2] = (byte) ((address >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        addr[3] = (byte) (address & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns the IP address string in textual presentation form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return  the raw IP address in a string format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return numericToTextFormat(getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public int hashCode() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   326
        return holder().getAddress();
2
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
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   331
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   332
     * not {@code null} and it represents the same IP address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   335
     * Two instances of {@code InetAddress} represent the same IP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * address if the length of the byte arrays returned by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   337
     * {@code getAddress} is the same for both, and each of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   341
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   342
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return (obj != null) && (obj instanceof Inet4Address) &&
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   347
            (((InetAddress)obj).holder().getAddress() == holder().getAddress());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    // Utilities
42474
6b0beb616950 8164057: Fix @since for java.net.Inet[46]Address
clanger
parents: 40118
diff changeset
   351
6b0beb616950 8164057: Fix @since for java.net.Inet[46]Address
clanger
parents: 40118
diff changeset
   352
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Converts IPv4 binary address into a string suitable for presentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param src a byte array representing an IPv4 numeric address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return a String representing the IPv4 address in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         textual representation format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    static String numericToTextFormat(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
}