Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
      4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      5  *
      6  * This code is free software; you can redistribute it and/or modify it
      7  * under the terms of the GNU General Public License version 2 only, as
      8  * published by the Free Software Foundation.  Oracle designates this
      9  * particular file as subject to the "Classpath" exception as provided
     10  * by Oracle in the LICENSE file that accompanied this code.
     11  *
     12  * This code is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  * version 2 for more details (a copy is included in the LICENSE file that
     16  * accompanied this code).
     17  *
     18  * You should have received a copy of the GNU General Public License version
     19  * 2 along with this work; if not, write to the Free Software Foundation,
     20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     21  *
     22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     23  * or visit www.oracle.com if you need additional information or have any
     24  * questions.
     25  */
     26 
     27 package java.net;
     28 
     29 import java.io.ObjectStreamException;
     30 import static android.system.OsConstants.*;
     31 
     32 /**
     33  * This class represents an Internet Protocol version 4 (IPv4) address.
     34  * Defined by <a href="http://www.ietf.org/rfc/rfc790.txt">
     35  * <i>RFC&nbsp;790: Assigned Numbers</i></a>,
     36  * <a href="http://www.ietf.org/rfc/rfc1918.txt">
     37  * <i>RFC&nbsp;1918: Address Allocation for Private Internets</i></a>,
     38  * and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
     39  * Administratively Scoped IP Multicast</i></a>
     40  *
     41  * <h3> <A NAME="format">Textual representation of IP addresses</a> </h3>
     42  *
     43  * Textual representation of IPv4 address used as input to methods
     44  * takes one of the following forms:
     45  *
     46  * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
     47  * <tr><td>{@code d.d.d.d}</td></tr>
     48  * <tr><td>{@code d.d.d}</td></tr>
     49  * <tr><td>{@code d.d}</td></tr>
     50  * <tr><td>{@code d}</td></tr>
     51  * </table></blockquote>
     52  *
     53  * <p> When four parts are specified, each is interpreted as a byte of
     54  * data and assigned, from left to right, to the four bytes of an IPv4
     55  * address.
     56 
     57  * <p> When a three part address is specified, the last part is
     58  * interpreted as a 16-bit quantity and placed in the right most two
     59  * bytes of the network address. This makes the three part address
     60  * format convenient for specifying Class B net- work addresses as
     61  * 128.net.host.
     62  *
     63  * <p> When a two part address is supplied, the last part is
     64  * interpreted as a 24-bit quantity and placed in the right most three
     65  * bytes of the network address. This makes the two part address
     66  * format convenient for specifying Class A network addresses as
     67  * net.host.
     68  *
     69  * <p> When only one part is given, the value is stored directly in
     70  * the network address without any byte rearrangement.
     71  *
     72  * <p> For methods that return a textual representation as output
     73  * value, the first form, i.e. a dotted-quad string, is used.
     74  *
     75  * <h4> The Scope of a Multicast Address </h4>
     76  *
     77  * Historically the IPv4 TTL field in the IP header has doubled as a
     78  * multicast scope field: a TTL of 0 means node-local, 1 means
     79  * link-local, up through 32 means site-local, up through 64 means
     80  * region-local, up through 128 means continent-local, and up through
     81  * 255 are global. However, the administrative scoping is preferred.
     82  * Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt">
     83  * <i>RFC&nbsp;2365: Administratively Scoped IP Multicast</i></a>
     84  * @since 1.4
     85  */
     86 
     87 public final
     88 class Inet4Address extends InetAddress {
     89     final static int INADDRSZ = 4;
     90 
     91     /** use serialVersionUID from InetAddress, but Inet4Address instance
     92      *  is always replaced by an InetAddress instance before being
     93      *  serialized */
     94     private static final long serialVersionUID = 3286316764910316507L;
     95 
     96     // BEGIN Android-added: Define special-purpose IPv4 address
     97     /** @hide */
     98     public static final InetAddress ANY = new Inet4Address(null, new byte[] { 0, 0, 0, 0 });
     99 
    100     /** @hide */
    101     public static final InetAddress ALL =
    102             new Inet4Address(null, new byte[] { (byte) 255, (byte) 255,
    103                   (byte) 255, (byte) 255 });
    104     /** @hide */
    105     public static final InetAddress LOOPBACK =
    106             new Inet4Address("localhost", new byte[] { 127, 0, 0, 1 });
    107     // END Android-added: Define special-purpose IPv4 address
    108 
    109 
    110     // BEGIN Android-removed: Android doesn't need to call native init
    111     /*
    112      * Perform initializations.
    113      *
    114     static {
    115         init();
    116     }
    117     */
    118     // END Android-removed: Android doesn't need to call native init
    119     Inet4Address() {
    120         super();
    121         holder().hostName = null;
    122         holder().address = 0;
    123         holder().family = AF_INET;
    124     }
    125 
    126     Inet4Address(String hostName, byte addr[]) {
    127         holder().hostName = hostName;
    128         holder().family = AF_INET;
    129         if (addr != null) {
    130             if (addr.length == INADDRSZ) {
    131                 int address  = addr[3] & 0xFF;
    132                 address |= ((addr[2] << 8) & 0xFF00);
    133                 address |= ((addr[1] << 16) & 0xFF0000);
    134                 address |= ((addr[0] << 24) & 0xFF000000);
    135                 holder().address = address;
    136             }
    137         }
    138         holder().originalHostName = hostName;
    139     }
    140     Inet4Address(String hostName, int address) {
    141         holder().hostName = hostName;
    142         holder().family = AF_INET;
    143         holder().address = address;
    144         holder().originalHostName = hostName;
    145     }
    146 
    147     /**
    148      * Replaces the object to be serialized with an InetAddress object.
    149      *
    150      * @return the alternate object to be serialized.
    151      *
    152      * @throws ObjectStreamException if a new object replacing this
    153      * object could not be created
    154      */
    155     private Object writeReplace() throws ObjectStreamException {
    156         // will replace the to be serialized 'this' object
    157         InetAddress inet = new InetAddress();
    158         inet.holder().hostName = holder().getHostName();
    159         inet.holder().address = holder().getAddress();
    160 
    161         /**
    162          * Prior to 1.4 an InetAddress was created with a family
    163          * based on the platform AF_INET value (usually 2).
    164          * For compatibility reasons we must therefore write the
    165          * the InetAddress with this family.
    166          */
    167         inet.holder().family = 2;
    168 
    169         return inet;
    170     }
    171 
    172     /**
    173      * Utility routine to check if the InetAddress is an
    174      * IP multicast address. IP multicast address is a Class D
    175      * address i.e first four bits of the address are 1110.
    176      * @return a {@code boolean} indicating if the InetAddress is
    177      * an IP multicast address
    178      * @since   JDK1.1
    179      */
    180     public boolean isMulticastAddress() {
    181         return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
    182     }
    183 
    184     /**
    185      * Utility routine to check if the InetAddress in a wildcard address.
    186      * @return a {@code boolean} indicating if the Inetaddress is
    187      *         a wildcard address.
    188      * @since 1.4
    189      */
    190     public boolean isAnyLocalAddress() {
    191         return holder().getAddress() == 0;
    192     }
    193 
    194     /**
    195      * Utility routine to check if the InetAddress is a loopback address.
    196      *
    197      * @return a {@code boolean} indicating if the InetAddress is
    198      * a loopback address; or false otherwise.
    199      * @since 1.4
    200      */
    201     public boolean isLoopbackAddress() {
    202         /* 127.x.x.x */
    203         byte[] byteAddr = getAddress();
    204         return byteAddr[0] == 127;
    205     }
    206 
    207     /**
    208      * Utility routine to check if the InetAddress is an link local address.
    209      *
    210      * @return a {@code boolean} indicating if the InetAddress is
    211      * a link local address; or false if address is not a link local unicast address.
    212      * @since 1.4
    213      */
    214     public boolean isLinkLocalAddress() {
    215         // link-local unicast in IPv4 (169.254.0.0/16)
    216         // defined in "Documenting Special Use IPv4 Address Blocks
    217         // that have been Registered with IANA" by Bill Manning
    218         // draft-manning-dsua-06.txt
    219         int address = holder().getAddress();
    220         return (((address >>> 24) & 0xFF) == 169)
    221             && (((address >>> 16) & 0xFF) == 254);
    222     }
    223 
    224     /**
    225      * Utility routine to check if the InetAddress is a site local address.
    226      *
    227      * @return a {@code boolean} indicating if the InetAddress is
    228      * a site local address; or false if address is not a site local unicast address.
    229      * @since 1.4
    230      */
    231     public boolean isSiteLocalAddress() {
    232         // refer to RFC 1918
    233         // 10/8 prefix
    234         // 172.16/12 prefix
    235         // 192.168/16 prefix
    236         int address = holder().getAddress();
    237         return (((address >>> 24) & 0xFF) == 10)
    238             || ((((address >>> 24) & 0xFF) == 172)
    239                 && (((address >>> 16) & 0xF0) == 16))
    240             || ((((address >>> 24) & 0xFF) == 192)
    241                 && (((address >>> 16) & 0xFF) == 168));
    242     }
    243 
    244     /**
    245      * Utility routine to check if the multicast address has global scope.
    246      *
    247      * @return a {@code boolean} indicating if the address has
    248      *         is a multicast address of global scope, false if it is not
    249      *         of global scope or it is not a multicast address
    250      * @since 1.4
    251      */
    252     public boolean isMCGlobal() {
    253         // 224.0.1.0 to 238.255.255.255
    254         byte[] byteAddr = getAddress();
    255         return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
    256             !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
    257               byteAddr[2] == 0);
    258     }
    259 
    260     /**
    261      * Utility routine to check if the multicast address has node scope.
    262      *
    263      * @return a {@code boolean} indicating if the address has
    264      *         is a multicast address of node-local scope, false if it is not
    265      *         of node-local scope or it is not a multicast address
    266      * @since 1.4
    267      */
    268     public boolean isMCNodeLocal() {
    269         // unless ttl == 0
    270         return false;
    271     }
    272 
    273     /**
    274      * Utility routine to check if the multicast address has link scope.
    275      *
    276      * @return a {@code boolean} indicating if the address has
    277      *         is a multicast address of link-local scope, false if it is not
    278      *         of link-local scope or it is not a multicast address
    279      * @since 1.4
    280      */
    281     public boolean isMCLinkLocal() {
    282         // 224.0.0/24 prefix and ttl == 1
    283         int address = holder().getAddress();
    284         return (((address >>> 24) & 0xFF) == 224)
    285             && (((address >>> 16) & 0xFF) == 0)
    286             && (((address >>> 8) & 0xFF) == 0);
    287     }
    288 
    289     /**
    290      * Utility routine to check if the multicast address has site scope.
    291      *
    292      * @return a {@code boolean} indicating if the address has
    293      *         is a multicast address of site-local scope, false if it is not
    294      *         of site-local scope or it is not a multicast address
    295      * @since 1.4
    296      */
    297     public boolean isMCSiteLocal() {
    298         // 239.255/16 prefix or ttl < 32
    299         int address = holder().getAddress();
    300         return (((address >>> 24) & 0xFF) == 239)
    301             && (((address >>> 16) & 0xFF) == 255);
    302     }
    303 
    304     /**
    305      * Utility routine to check if the multicast address has organization scope.
    306      *
    307      * @return a {@code boolean} indicating if the address has
    308      *         is a multicast address of organization-local scope,
    309      *         false if it is not of organization-local scope
    310      *         or it is not a multicast address
    311      * @since 1.4
    312      */
    313     public boolean isMCOrgLocal() {
    314         // 239.192 - 239.195
    315         int address = holder().getAddress();
    316         return (((address >>> 24) & 0xFF) == 239)
    317             && (((address >>> 16) & 0xFF) >= 192)
    318             && (((address >>> 16) & 0xFF) <= 195);
    319     }
    320 
    321     /**
    322      * Returns the raw IP address of this {@code InetAddress}
    323      * object. The result is in network byte order: the highest order
    324      * byte of the address is in {@code getAddress()[0]}.
    325      *
    326      * @return  the raw IP address of this object.
    327      */
    328     public byte[] getAddress() {
    329         int address = holder().getAddress();
    330         byte[] addr = new byte[INADDRSZ];
    331 
    332         addr[0] = (byte) ((address >>> 24) & 0xFF);
    333         addr[1] = (byte) ((address >>> 16) & 0xFF);
    334         addr[2] = (byte) ((address >>> 8) & 0xFF);
    335         addr[3] = (byte) (address & 0xFF);
    336         return addr;
    337     }
    338 
    339     /**
    340      * Returns the IP address string in textual presentation form.
    341      *
    342      * @return  the raw IP address in a string format.
    343      * @since   JDK1.0.2
    344      */
    345     public String getHostAddress() {
    346         return numericToTextFormat(getAddress());
    347     }
    348 
    349     /**
    350      * Returns a hashcode for this IP address.
    351      *
    352      * @return  a hash code value for this IP address.
    353      */
    354     public int hashCode() {
    355         return holder().getAddress();
    356     }
    357 
    358     /**
    359      * Compares this object against the specified object.
    360      * The result is {@code true} if and only if the argument is
    361      * not {@code null} and it represents the same IP address as
    362      * this object.
    363      * <p>
    364      * Two instances of {@code InetAddress} represent the same IP
    365      * address if the length of the byte arrays returned by
    366      * {@code getAddress} is the same for both, and each of the
    367      * array components is the same for the byte arrays.
    368      *
    369      * @param   obj   the object to compare against.
    370      * @return  {@code true} if the objects are the same;
    371      *          {@code false} otherwise.
    372      * @see     java.net.InetAddress#getAddress()
    373      */
    374     public boolean equals(Object obj) {
    375         return (obj != null) && (obj instanceof Inet4Address) &&
    376             (((InetAddress)obj).holder().getAddress() == holder().getAddress());
    377     }
    378 
    379     // Utilities
    380     /*
    381      * Converts IPv4 binary address into a string suitable for presentation.
    382      *
    383      * @param src a byte array representing an IPv4 numeric address
    384      * @return a String representing the IPv4 address in
    385      *         textual representation format
    386      * @since 1.4
    387      */
    388 
    389     static String numericToTextFormat(byte[] src)
    390     {
    391         return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
    392     }
    393 
    394     // BEGIN Android-removed: Android doesn't need to call native init
    395     /*
    396      * Perform class load-time initializations.
    397      *
    398     private static native void init();
    399     */
    400     // END Android-removed: Android doesn't need to call native init
    401 }
    402