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