Path: blob/master/src/java.base/share/classes/java/net/InterfaceAddress.java
41152 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.util.Objects;2829/**30* This class represents a Network Interface address. In short it's an31* IP address, a subnet mask and a broadcast address when the address is32* an IPv4 one. An IP address and a network prefix length in the case33* of IPv6 address.34*35* @see java.net.NetworkInterface36* @since 1.637*/38public class InterfaceAddress {39private InetAddress address = null;40private Inet4Address broadcast = null;41private short maskLength = 0;4243/*44* Package private constructor. Can't be built directly, instances are45* obtained through the NetworkInterface class.46*/47InterfaceAddress() {48}4950/**51* Returns an {@code InetAddress} for this address.52*53* @return the {@code InetAddress} for this address.54*/55public InetAddress getAddress() {56return address;57}5859/**60* Returns an {@code InetAddress} for the broadcast address61* for this InterfaceAddress.62* <p>63* Only IPv4 networks have broadcast address therefore, in the case64* of an IPv6 network, {@code null} will be returned.65*66* @return the {@code InetAddress} representing the broadcast67* address or {@code null} if there is no broadcast address.68*/69public InetAddress getBroadcast() {70return broadcast;71}7273/**74* Returns the network prefix length for this address. This is also known75* as the subnet mask in the context of IPv4 addresses.76* Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)77* or 24 (255.255.255.0). <p>78* Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)79*80* @return a {@code short} representing the prefix length for the81* subnet of that address.82*/83public short getNetworkPrefixLength() {84return maskLength;85}8687/**88* Compares this object against the specified object.89* The result is {@code true} if and only if the argument is90* not {@code null} and it represents the same interface address as91* this object.92* <p>93* Two instances of {@code InterfaceAddress} represent the same94* address if the InetAddress, the prefix length and the broadcast are95* the same for both.96*97* @param obj the object to compare against.98* @return {@code true} if the objects are the same;99* {@code false} otherwise.100* @see java.net.InterfaceAddress#hashCode()101*/102public boolean equals(Object obj) {103return obj instanceof InterfaceAddress cmp &&104Objects.equals(address, cmp.address) &&105Objects.equals(broadcast, cmp.broadcast) &&106maskLength == cmp.maskLength;107}108109/**110* Returns a hashcode for this Interface address.111*112* @return a hash code value for this Interface address.113*/114public int hashCode() {115return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;116}117118/**119* Converts this Interface address to a {@code String}. The120* string returned is of the form: InetAddress / prefix length [ broadcast address ].121*122* @return a string representation of this Interface address.123*/124public String toString() {125return address + "/" + maskLength + " [" + broadcast + "]";126}127128}129130131