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     /** @hide */
97     public static final InetAddress ANY = new Inet4Address(null, new byte[] { 0, 0, 0, 0 });
98 
99     /** @hide */
100     public static final InetAddress ALL =
101             new Inet4Address(null, new byte[] { (byte) 255, (byte) 255,
102                   (byte) 255, (byte) 255 });
103     /** @hide */
104     public static final InetAddress LOOPBACK =
105             new Inet4Address("localhost", new byte[] { 127, 0, 0, 1 });
106 
Inet4Address()107     Inet4Address() {
108         super();
109         holder().hostName = null;
110         holder().address = 0;
111         holder().family = AF_INET;
112     }
113 
Inet4Address(String hostName, byte addr[])114     Inet4Address(String hostName, byte addr[]) {
115         holder().hostName = hostName;
116         holder().family = AF_INET;
117         if (addr != null) {
118             if (addr.length == INADDRSZ) {
119                 int address  = addr[3] & 0xFF;
120                 address |= ((addr[2] << 8) & 0xFF00);
121                 address |= ((addr[1] << 16) & 0xFF0000);
122                 address |= ((addr[0] << 24) & 0xFF000000);
123                 holder().address = address;
124             }
125         }
126     }
Inet4Address(String hostName, int address)127     Inet4Address(String hostName, int address) {
128         holder().hostName = hostName;
129         holder().family = AF_INET;
130         holder().address = address;
131     }
132 
133     /**
134      * Replaces the object to be serialized with an InetAddress object.
135      *
136      * @return the alternate object to be serialized.
137      *
138      * @throws ObjectStreamException if a new object replacing this
139      * object could not be created
140      */
writeReplace()141     private Object writeReplace() throws ObjectStreamException {
142         // will replace the to be serialized 'this' object
143         InetAddress inet = new InetAddress();
144         inet.holder().hostName = holder().getHostName();
145         inet.holder().address = holder().getAddress();
146 
147         /**
148          * Prior to 1.4 an InetAddress was created with a family
149          * based on the platform AF_INET value (usually 2).
150          * For compatibility reasons we must therefore write the
151          * the InetAddress with this family.
152          */
153         inet.holder().family = 2;
154 
155         return inet;
156     }
157 
158     /**
159      * Utility routine to check if the InetAddress is an
160      * IP multicast address. IP multicast address is a Class D
161      * address i.e first four bits of the address are 1110.
162      * @return a {@code boolean} indicating if the InetAddress is
163      * an IP multicast address
164      * @since   JDK1.1
165      */
isMulticastAddress()166     public boolean isMulticastAddress() {
167         return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
168     }
169 
170     /**
171      * Utility routine to check if the InetAddress in a wildcard address.
172      * @return a {@code boolean} indicating if the Inetaddress is
173      *         a wildcard address.
174      * @since 1.4
175      */
isAnyLocalAddress()176     public boolean isAnyLocalAddress() {
177         return holder().getAddress() == 0;
178     }
179 
180     /**
181      * Utility routine to check if the InetAddress is a loopback address.
182      *
183      * @return a {@code boolean} indicating if the InetAddress is
184      * a loopback address; or false otherwise.
185      * @since 1.4
186      */
isLoopbackAddress()187     public boolean isLoopbackAddress() {
188         /* 127.x.x.x */
189         byte[] byteAddr = getAddress();
190         return byteAddr[0] == 127;
191     }
192 
193     /**
194      * Utility routine to check if the InetAddress is an link local address.
195      *
196      * @return a {@code boolean} indicating if the InetAddress is
197      * a link local address; or false if address is not a link local unicast address.
198      * @since 1.4
199      */
isLinkLocalAddress()200     public boolean isLinkLocalAddress() {
201         // link-local unicast in IPv4 (169.254.0.0/16)
202         // defined in "Documenting Special Use IPv4 Address Blocks
203         // that have been Registered with IANA" by Bill Manning
204         // draft-manning-dsua-06.txt
205         int address = holder().getAddress();
206         return (((address >>> 24) & 0xFF) == 169)
207             && (((address >>> 16) & 0xFF) == 254);
208     }
209 
210     /**
211      * Utility routine to check if the InetAddress is a site local address.
212      *
213      * @return a {@code boolean} indicating if the InetAddress is
214      * a site local address; or false if address is not a site local unicast address.
215      * @since 1.4
216      */
isSiteLocalAddress()217     public boolean isSiteLocalAddress() {
218         // refer to RFC 1918
219         // 10/8 prefix
220         // 172.16/12 prefix
221         // 192.168/16 prefix
222         int address = holder().getAddress();
223         return (((address >>> 24) & 0xFF) == 10)
224             || ((((address >>> 24) & 0xFF) == 172)
225                 && (((address >>> 16) & 0xF0) == 16))
226             || ((((address >>> 24) & 0xFF) == 192)
227                 && (((address >>> 16) & 0xFF) == 168));
228     }
229 
230     /**
231      * Utility routine to check if the multicast address has global scope.
232      *
233      * @return a {@code boolean} indicating if the address has
234      *         is a multicast address of global scope, false if it is not
235      *         of global scope or it is not a multicast address
236      * @since 1.4
237      */
isMCGlobal()238     public boolean isMCGlobal() {
239         // 224.0.1.0 to 238.255.255.255
240         byte[] byteAddr = getAddress();
241         return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
242             !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
243               byteAddr[2] == 0);
244     }
245 
246     /**
247      * Utility routine to check if the multicast address has node scope.
248      *
249      * @return a {@code boolean} indicating if the address has
250      *         is a multicast address of node-local scope, false if it is not
251      *         of node-local scope or it is not a multicast address
252      * @since 1.4
253      */
isMCNodeLocal()254     public boolean isMCNodeLocal() {
255         // unless ttl == 0
256         return false;
257     }
258 
259     /**
260      * Utility routine to check if the multicast address has link scope.
261      *
262      * @return a {@code boolean} indicating if the address has
263      *         is a multicast address of link-local scope, false if it is not
264      *         of link-local scope or it is not a multicast address
265      * @since 1.4
266      */
isMCLinkLocal()267     public boolean isMCLinkLocal() {
268         // 224.0.0/24 prefix and ttl == 1
269         int address = holder().getAddress();
270         return (((address >>> 24) & 0xFF) == 224)
271             && (((address >>> 16) & 0xFF) == 0)
272             && (((address >>> 8) & 0xFF) == 0);
273     }
274 
275     /**
276      * Utility routine to check if the multicast address has site scope.
277      *
278      * @return a {@code boolean} indicating if the address has
279      *         is a multicast address of site-local scope, false if it is not
280      *         of site-local scope or it is not a multicast address
281      * @since 1.4
282      */
isMCSiteLocal()283     public boolean isMCSiteLocal() {
284         // 239.255/16 prefix or ttl < 32
285         int address = holder().getAddress();
286         return (((address >>> 24) & 0xFF) == 239)
287             && (((address >>> 16) & 0xFF) == 255);
288     }
289 
290     /**
291      * Utility routine to check if the multicast address has organization scope.
292      *
293      * @return a {@code boolean} indicating if the address has
294      *         is a multicast address of organization-local scope,
295      *         false if it is not of organization-local scope
296      *         or it is not a multicast address
297      * @since 1.4
298      */
isMCOrgLocal()299     public boolean isMCOrgLocal() {
300         // 239.192 - 239.195
301         int address = holder().getAddress();
302         return (((address >>> 24) & 0xFF) == 239)
303             && (((address >>> 16) & 0xFF) >= 192)
304             && (((address >>> 16) & 0xFF) <= 195);
305     }
306 
307     /**
308      * Returns the raw IP address of this {@code InetAddress}
309      * object. The result is in network byte order: the highest order
310      * byte of the address is in {@code getAddress()[0]}.
311      *
312      * @return  the raw IP address of this object.
313      */
getAddress()314     public byte[] getAddress() {
315         int address = holder().getAddress();
316         byte[] addr = new byte[INADDRSZ];
317 
318         addr[0] = (byte) ((address >>> 24) & 0xFF);
319         addr[1] = (byte) ((address >>> 16) & 0xFF);
320         addr[2] = (byte) ((address >>> 8) & 0xFF);
321         addr[3] = (byte) (address & 0xFF);
322         return addr;
323     }
324 
325     /**
326      * Returns the IP address string in textual presentation form.
327      *
328      * @return  the raw IP address in a string format.
329      * @since   JDK1.0.2
330      */
getHostAddress()331     public String getHostAddress() {
332         return numericToTextFormat(getAddress());
333     }
334 
335     /**
336      * Returns a hashcode for this IP address.
337      *
338      * @return  a hash code value for this IP address.
339      */
hashCode()340     public int hashCode() {
341         return holder().getAddress();
342     }
343 
344     /**
345      * Compares this object against the specified object.
346      * The result is {@code true} if and only if the argument is
347      * not {@code null} and it represents the same IP address as
348      * this object.
349      * <p>
350      * Two instances of {@code InetAddress} represent the same IP
351      * address if the length of the byte arrays returned by
352      * {@code getAddress} is the same for both, and each of the
353      * array components is the same for the byte arrays.
354      *
355      * @param   obj   the object to compare against.
356      * @return  {@code true} if the objects are the same;
357      *          {@code false} otherwise.
358      * @see     java.net.InetAddress#getAddress()
359      */
equals(Object obj)360     public boolean equals(Object obj) {
361         return (obj != null) && (obj instanceof Inet4Address) &&
362             (((InetAddress)obj).holder().getAddress() == holder().getAddress());
363     }
364 
365     // Utilities
366     /*
367      * Converts IPv4 binary address into a string suitable for presentation.
368      *
369      * @param src a byte array representing an IPv4 numeric address
370      * @return a String representing the IPv4 address in
371      *         textual representation format
372      * @since 1.4
373      */
374 
numericToTextFormat(byte[] src)375     static String numericToTextFormat(byte[] src)
376     {
377         return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
378     }
379 }
380