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 790: Assigned Numbers</i></a>, 36 * <a href="http://www.ietf.org/rfc/rfc1918.txt"> 37 * <i>RFC 1918: Address Allocation for Private Internets</i></a>, 38 * and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC 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 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 /** 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 * @since JDK1.1 192 */ isMulticastAddress()193 public boolean isMulticastAddress() { 194 return ((holder().getAddress() & 0xf0000000) == 0xe0000000); 195 } 196 197 /** 198 * Utility routine to check if the InetAddress in a wildcard address. 199 * @return a {@code boolean} indicating if the Inetaddress is 200 * a wildcard address. 201 * @since 1.4 202 */ isAnyLocalAddress()203 public boolean isAnyLocalAddress() { 204 return holder().getAddress() == 0; 205 } 206 207 /** 208 * Utility routine to check if the InetAddress is a loopback address. 209 * 210 * @return a {@code boolean} indicating if the InetAddress is 211 * a loopback address; or false otherwise. 212 * @since 1.4 213 */ isLoopbackAddress()214 public boolean isLoopbackAddress() { 215 /* 127.x.x.x */ 216 byte[] byteAddr = getAddress(); 217 return byteAddr[0] == 127; 218 } 219 220 /** 221 * Utility routine to check if the InetAddress is an link local address. 222 * 223 * @return a {@code boolean} indicating if the InetAddress is 224 * a link local address; or false if address is not a link local unicast address. 225 * @since 1.4 226 */ isLinkLocalAddress()227 public boolean isLinkLocalAddress() { 228 // link-local unicast in IPv4 (169.254.0.0/16) 229 // defined in "Documenting Special Use IPv4 Address Blocks 230 // that have been Registered with IANA" by Bill Manning 231 // draft-manning-dsua-06.txt 232 int address = holder().getAddress(); 233 return (((address >>> 24) & 0xFF) == 169) 234 && (((address >>> 16) & 0xFF) == 254); 235 } 236 237 /** 238 * Utility routine to check if the InetAddress is a site local address. 239 * 240 * @return a {@code boolean} indicating if the InetAddress is 241 * a site local address; or false if address is not a site local unicast address. 242 * @since 1.4 243 */ isSiteLocalAddress()244 public boolean isSiteLocalAddress() { 245 // refer to RFC 1918 246 // 10/8 prefix 247 // 172.16/12 prefix 248 // 192.168/16 prefix 249 int address = holder().getAddress(); 250 return (((address >>> 24) & 0xFF) == 10) 251 || ((((address >>> 24) & 0xFF) == 172) 252 && (((address >>> 16) & 0xF0) == 16)) 253 || ((((address >>> 24) & 0xFF) == 192) 254 && (((address >>> 16) & 0xFF) == 168)); 255 } 256 257 /** 258 * Utility routine to check if the multicast address has global scope. 259 * 260 * @return a {@code boolean} indicating if the address has 261 * is a multicast address of global scope, false if it is not 262 * of global scope or it is not a multicast address 263 * @since 1.4 264 */ isMCGlobal()265 public boolean isMCGlobal() { 266 // 224.0.1.0 to 238.255.255.255 267 byte[] byteAddr = getAddress(); 268 return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) && 269 !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 && 270 byteAddr[2] == 0); 271 } 272 273 /** 274 * Utility routine to check if the multicast address has node scope. 275 * 276 * @return a {@code boolean} indicating if the address has 277 * is a multicast address of node-local scope, false if it is not 278 * of node-local scope or it is not a multicast address 279 * @since 1.4 280 */ isMCNodeLocal()281 public boolean isMCNodeLocal() { 282 // unless ttl == 0 283 return false; 284 } 285 286 /** 287 * Utility routine to check if the multicast address has link scope. 288 * 289 * @return a {@code boolean} indicating if the address has 290 * is a multicast address of link-local scope, false if it is not 291 * of link-local scope or it is not a multicast address 292 * @since 1.4 293 */ isMCLinkLocal()294 public boolean isMCLinkLocal() { 295 // 224.0.0/24 prefix and ttl == 1 296 int address = holder().getAddress(); 297 return (((address >>> 24) & 0xFF) == 224) 298 && (((address >>> 16) & 0xFF) == 0) 299 && (((address >>> 8) & 0xFF) == 0); 300 } 301 302 /** 303 * Utility routine to check if the multicast address has site scope. 304 * 305 * @return a {@code boolean} indicating if the address has 306 * is a multicast address of site-local scope, false if it is not 307 * of site-local scope or it is not a multicast address 308 * @since 1.4 309 */ isMCSiteLocal()310 public boolean isMCSiteLocal() { 311 // 239.255/16 prefix or ttl < 32 312 int address = holder().getAddress(); 313 return (((address >>> 24) & 0xFF) == 239) 314 && (((address >>> 16) & 0xFF) == 255); 315 } 316 317 /** 318 * Utility routine to check if the multicast address has organization scope. 319 * 320 * @return a {@code boolean} indicating if the address has 321 * is a multicast address of organization-local scope, 322 * false if it is not of organization-local scope 323 * or it is not a multicast address 324 * @since 1.4 325 */ isMCOrgLocal()326 public boolean isMCOrgLocal() { 327 // 239.192 - 239.195 328 int address = holder().getAddress(); 329 return (((address >>> 24) & 0xFF) == 239) 330 && (((address >>> 16) & 0xFF) >= 192) 331 && (((address >>> 16) & 0xFF) <= 195); 332 } 333 334 /** 335 * Returns the raw IP address of this {@code InetAddress} 336 * object. The result is in network byte order: the highest order 337 * byte of the address is in {@code getAddress()[0]}. 338 * 339 * @return the raw IP address of this object. 340 */ getAddress()341 public byte[] getAddress() { 342 int address = holder().getAddress(); 343 byte[] addr = new byte[INADDRSZ]; 344 345 addr[0] = (byte) ((address >>> 24) & 0xFF); 346 addr[1] = (byte) ((address >>> 16) & 0xFF); 347 addr[2] = (byte) ((address >>> 8) & 0xFF); 348 addr[3] = (byte) (address & 0xFF); 349 return addr; 350 } 351 352 /** 353 * Returns the IP address string in textual presentation form. 354 * 355 * @return the raw IP address in a string format. 356 * @since JDK1.0.2 357 */ getHostAddress()358 public String getHostAddress() { 359 return numericToTextFormat(getAddress()); 360 } 361 362 /** 363 * Returns a hashcode for this IP address. 364 * 365 * @return a hash code value for this IP address. 366 */ hashCode()367 public int hashCode() { 368 return holder().getAddress(); 369 } 370 371 /** 372 * Compares this object against the specified object. 373 * The result is {@code true} if and only if the argument is 374 * not {@code null} and it represents the same IP address as 375 * this object. 376 * <p> 377 * Two instances of {@code InetAddress} represent the same IP 378 * address if the length of the byte arrays returned by 379 * {@code getAddress} is the same for both, and each of the 380 * array components is the same for the byte arrays. 381 * 382 * @param obj the object to compare against. 383 * @return {@code true} if the objects are the same; 384 * {@code false} otherwise. 385 * @see java.net.InetAddress#getAddress() 386 */ equals(Object obj)387 public boolean equals(Object obj) { 388 return (obj != null) && (obj instanceof Inet4Address) && 389 (((InetAddress)obj).holder().getAddress() == holder().getAddress()); 390 } 391 392 // Utilities 393 /* 394 * Converts IPv4 binary address into a string suitable for presentation. 395 * 396 * @param src a byte array representing an IPv4 numeric address 397 * @return a String representing the IPv4 address in 398 * textual representation format 399 * @since 1.4 400 */ 401 numericToTextFormat(byte[] src)402 static String numericToTextFormat(byte[] src) 403 { 404 return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff); 405 } 406 407 // BEGIN Android-removed: Android doesn't need to call native init. 408 /* 409 * Perform class load-time initializations. 410 * 411 private static native void init(); 412 */ 413 // END Android-removed: Android doesn't need to call native init. 414 } 415