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