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