src/java.base/share/classes/java/net/Inet4Address.java
author chegar
Thu, 17 Oct 2019 21:15:33 +0100
branchdatagramsocketimpl-branch
changeset 58680 7e9e2f10a050
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
datagramsocketimpl-branch: initial code changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 52499
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 52499
diff changeset
    39
 * <h2> <a id="format">Textual representation of IP addresses</a> </h2>
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
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 52499
diff changeset
    73
 * <h3> The Scope of a Multicast Address </h3>
2
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 */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
    92
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final long serialVersionUID = 3286316764910316507L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Perform initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    Inet4Address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        super();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   104
        holder().hostName = null;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   105
        holder().address = 0;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   106
        holder().family = IPv4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    Inet4Address(String hostName, byte addr[]) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   110
        holder().hostName = hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   111
        holder().family = IPv4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (addr.length == INADDRSZ) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   114
                int address  = addr[3] & 0xFF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                address |= ((addr[2] << 8) & 0xFF00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                address |= ((addr[1] << 16) & 0xFF0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                address |= ((addr[0] << 24) & 0xFF000000);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   118
                holder().address = address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents: 32649
diff changeset
   121
        holder().originalHostName = hostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    Inet4Address(String hostName, int address) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   124
        holder().hostName = hostName;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   125
        holder().family = IPv4;
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   126
        holder().address = address;
33301
9ee33abf5b4c 8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents: 32649
diff changeset
   127
        holder().originalHostName = hostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Replaces the object to be serialized with an InetAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return the alternate object to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @throws ObjectStreamException if a new object replacing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * object could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   138
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // will replace the to be serialized 'this' object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        InetAddress inet = new InetAddress();
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   142
        inet.holder().hostName = holder().getHostName();
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   143
        inet.holder().address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * Prior to 1.4 an InetAddress was created with a family
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * based on the platform AF_INET value (usually 2).
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   148
         * For compatibility reasons we must therefore write
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * the InetAddress with this family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         */
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   151
        inet.holder().family = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return inet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Utility routine to check if the InetAddress is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * IP multicast address. IP multicast address is a Class D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * address i.e first four bits of the address are 1110.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   160
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * an IP multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public boolean isMulticastAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   164
        return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
40118
f63c761ec2d2 8162819: fix minor Javadoc issues and remove warnings in java.net.Socket
clanger
parents: 33301
diff changeset
   168
     * Utility routine to check if the InetAddress is a wildcard address.
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 47478
diff changeset
   169
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         a wildcard address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public boolean isAnyLocalAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   173
        return holder().getAddress() == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Utility routine to check if the InetAddress is a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   179
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * a loopback address; or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public boolean isLoopbackAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /* 127.x.x.x */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        byte[] byteAddr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return byteAddr[0] == 127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Utility routine to check if the InetAddress is an link local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   191
     * @return a {@code boolean} indicating if the InetAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * a link local address; or false if address is not a link local unicast address.
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
     */
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
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   215
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return (((address >>> 24) & 0xFF) == 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            || ((((address >>> 24) & 0xFF) == 172)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                && (((address >>> 16) & 0xF0) == 16))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            || ((((address >>> 24) & 0xFF) == 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                && (((address >>> 16) & 0xFF) == 168));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Utility routine to check if the multicast address has global scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   226
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         is a multicast address of global scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         of global scope or it is not a multicast address
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
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   241
     * @return a {@code boolean} indicating if the address has
2
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public boolean isMCNodeLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // unless ttl == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Utility routine to check if the multicast address has link scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   253
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *         is a multicast address of link-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         of link-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public boolean isMCLinkLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // 224.0.0/24 prefix and ttl == 1
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   259
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return (((address >>> 24) & 0xFF) == 224)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            && (((address >>> 16) & 0xFF) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            && (((address >>> 8) & 0xFF) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Utility routine to check if the multicast address has site scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   268
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *         is a multicast address of site-local scope, false if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *         of site-local scope or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public boolean isMCSiteLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // 239.255/16 prefix or ttl < 32
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   274
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            && (((address >>> 16) & 0xFF) == 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Utility routine to check if the multicast address has organization scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   282
     * @return a {@code boolean} indicating if the address has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *         is a multicast address of organization-local scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         false if it is not of organization-local scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         or it is not a multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public boolean isMCOrgLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // 239.192 - 239.195
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   289
        int address = holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return (((address >>> 24) & 0xFF) == 239)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            && (((address >>> 16) & 0xFF) >= 192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            && (((address >>> 16) & 0xFF) <= 195);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   296
     * Returns the raw IP address of this {@code InetAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * object. The result is in network byte order: the highest order
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   298
     * byte of the address is in {@code getAddress()[0]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return  the raw IP address of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public byte[] getAddress() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   303
        int address = holder().getAddress();
2
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public String getHostAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return numericToTextFormat(getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns a hashcode for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return  a hash code value for this IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public int hashCode() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   328
        return holder().getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   333
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   334
     * not {@code null} and it represents the same IP address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   337
     * Two instances of {@code InetAddress} represent the same IP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * address if the length of the byte arrays returned by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   339
     * {@code getAddress} is the same for both, and each of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * array components is the same for the byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   343
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18800
diff changeset
   344
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @see     java.net.InetAddress#getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return (obj != null) && (obj instanceof Inet4Address) &&
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   349
            (((InetAddress)obj).holder().getAddress() == holder().getAddress());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    // Utilities
42474
6b0beb616950 8164057: Fix @since for java.net.Inet[46]Address
clanger
parents: 40118
diff changeset
   353
6b0beb616950 8164057: Fix @since for java.net.Inet[46]Address
clanger
parents: 40118
diff changeset
   354
    /**
2
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    static String numericToTextFormat(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}