src/java.base/share/classes/java/net/InterfaceAddress.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 57895 82a71d82e326
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
    28
import java.util.Objects;
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class represents a Network Interface address. In short it's an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * IP address, a subnet mask and a broadcast address when the address is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * an IPv4 one. An IP address and a network prefix length in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * of IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see java.net.NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class InterfaceAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private InetAddress address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private Inet4Address broadcast = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private short        maskLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Package private constructor. Can't be built directly, instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * obtained through the NetworkInterface class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    InterfaceAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    52
     * Returns an {@code InetAddress} for this address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    54
     * @return the {@code InetAddress} for this address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public InetAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
    61
     * Returns an {@code InetAddress} for the broadcast address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * for this InterfaceAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Only IPv4 networks have broadcast address therefore, in the case
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    65
     * of an IPv6 network, {@code null} will be returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    67
     * @return the {@code InetAddress} representing the broadcast
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    68
     *         address or {@code null} if there is no broadcast address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public InetAddress getBroadcast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return broadcast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the network prefix length for this address. This is also known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * as the subnet mask in the context of IPv4 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * or 24 (255.255.255.0). <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    81
     * @return a {@code short} representing the prefix length for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *         subnet of that address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     public short getNetworkPrefixLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return maskLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    90
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    91
     * not {@code null} and it represents the same interface address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    94
     * Two instances of {@code InterfaceAddress} represent the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * address if the InetAddress, the prefix length and the broadcast are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the same for both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    99
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   100
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see     java.net.InterfaceAddress#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public boolean equals(Object obj) {
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   104
        if (obj instanceof InterfaceAddress) {
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   105
            InterfaceAddress cmp = (InterfaceAddress) obj;
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   106
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   107
            if (Objects.equals(address, cmp.address) &&
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   108
                Objects.equals(broadcast, cmp.broadcast) &&
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   109
                maskLength == cmp.maskLength)
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   110
            {
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   111
                return true;
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   112
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 47216
diff changeset
   114
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Returns a hashcode for this Interface address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return  a hash code value for this Interface address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   127
     * Converts this Interface address to a {@code String}. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * string returned is of the form: InetAddress / prefix length [ broadcast address ].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return  a string representation of this Interface address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return address + "/" + maskLength + " [" + broadcast + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}