Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 package java.net;
     27 
     28 /**
     29  * This class represents a Network Interface address. In short it's an
     30  * IP address, a subnet mask and a broadcast address when the address is
     31  * an IPv4 one. An IP address and a network prefix length in the case
     32  * of IPv6 address.
     33  *
     34  * @see java.net.NetworkInterface
     35  * @since 1.6
     36  */
     37 public class InterfaceAddress {
     38     private InetAddress address = null;
     39     private Inet4Address broadcast = null;
     40     private short        maskLength = 0;
     41 
     42     /*
     43      * Package private constructor. Can't be built directly, instances are
     44      * obtained through the NetworkInterface class.
     45      */
     46     InterfaceAddress() {
     47     }
     48 
     49     InterfaceAddress(InetAddress address, Inet4Address broadcast, InetAddress netmask) {
     50         this.address = address;
     51         this.broadcast = broadcast;
     52         this.maskLength = countPrefixLength(netmask);
     53     }
     54 
     55     /**
     56      * Counts the prefix length for the netmask address.
     57      *
     58      * A valid netmask address must start with a continuous sequence of 1, followed by a continuous
     59      * sequence of 0.
     60      */
     61     private short countPrefixLength(InetAddress netmask) {
     62         short count = 0;
     63         for (byte b : netmask.getAddress()) {
     64             for (; b != 0; b <<= 1, ++count);
     65         }
     66         return count;
     67     }
     68 
     69     /**
     70      * Returns an {@code InetAddress} for this address.
     71      *
     72      * @return the {@code InetAddress} for this address.
     73      */
     74     public InetAddress getAddress() {
     75         return address;
     76     }
     77 
     78     /**
     79      * Returns an {@code InetAddress} for the broadcast address
     80      * for this InterfaceAddress.
     81      * <p>
     82      * Only IPv4 networks have broadcast address therefore, in the case
     83      * of an IPv6 network, {@code null} will be returned.
     84      *
     85      * @return the {@code InetAddress} representing the broadcast
     86      *         address or {@code null} if there is no broadcast address.
     87      */
     88     public InetAddress getBroadcast() {
     89         return broadcast;
     90     }
     91 
     92     /**
     93      * Returns the network prefix length for this address. This is also known
     94      * as the subnet mask in the context of IPv4 addresses.
     95      * Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)
     96      * or 24 (255.255.255.0). <p>
     97      * Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)
     98      *
     99      * @return a {@code short} representing the prefix length for the
    100      *         subnet of that address.
    101      */
    102      public short getNetworkPrefixLength() {
    103         return maskLength;
    104     }
    105 
    106     /**
    107      * Compares this object against the specified object.
    108      * The result is {@code true} if and only if the argument is
    109      * not {@code null} and it represents the same interface address as
    110      * this object.
    111      * <p>
    112      * Two instances of {@code InterfaceAddress} represent the same
    113      * address if the InetAddress, the prefix length and the broadcast are
    114      * the same for both.
    115      *
    116      * @param   obj   the object to compare against.
    117      * @return  {@code true} if the objects are the same;
    118      *          {@code false} otherwise.
    119      * @see     java.net.InterfaceAddress#hashCode()
    120      */
    121     public boolean equals(Object obj) {
    122         if (!(obj instanceof InterfaceAddress)) {
    123             return false;
    124         }
    125         InterfaceAddress cmp = (InterfaceAddress) obj;
    126         if ( !(address == null ? cmp.address == null : address.equals(cmp.address)) )
    127             return false;
    128         if ( !(broadcast  == null ? cmp.broadcast == null : broadcast.equals(cmp.broadcast)) )
    129             return false;
    130         if (maskLength != cmp.maskLength)
    131             return false;
    132         return true;
    133     }
    134 
    135     /**
    136      * Returns a hashcode for this Interface address.
    137      *
    138      * @return  a hash code value for this Interface address.
    139      */
    140     public int hashCode() {
    141         return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;
    142     }
    143 
    144     /**
    145      * Converts this Interface address to a {@code String}. The
    146      * string returned is of the form: InetAddress / prefix length [ broadcast address ].
    147      *
    148      * @return  a string representation of this Interface address.
    149      */
    150     public String toString() {
    151         return address + "/" + maskLength + " [" + broadcast + "]";
    152     }
    153 
    154 }
    155