Path: blob/master/src/java.base/share/classes/java/net/Inet4Address.java
41152 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.ObjectStreamException;2829/**30* This class represents an Internet Protocol version 4 (IPv4) address.31* Defined by <a href="http://www.ietf.org/rfc/rfc790.txt">32* <i>RFC 790: Assigned Numbers</i></a>,33* <a href="http://www.ietf.org/rfc/rfc1918.txt">34* <i>RFC 1918: Address Allocation for Private Internets</i></a>,35* and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC 2365:36* Administratively Scoped IP Multicast</i></a>37*38* <h2> <a id="format">Textual representation of IP addresses</a> </h2>39*40* Textual representation of IPv4 address used as input to methods41* takes one of the following forms:42*43* <blockquote><ul style="list-style-type:none">44* <li>{@code d.d.d.d}</li>45* <li>{@code d.d.d}</li>46* <li>{@code d.d}</li>47* <li>{@code d}</li>48* </ul></blockquote>49*50* <p> When four parts are specified, each is interpreted as a byte of51* data and assigned, from left to right, to the four bytes of an IPv452* address.53*54* <p> When a three part address is specified, the last part is55* interpreted as a 16-bit quantity and placed in the right most two56* bytes of the network address. This makes the three part address57* format convenient for specifying Class B net- work addresses as58* 128.net.host.59*60* <p> When a two part address is supplied, the last part is61* interpreted as a 24-bit quantity and placed in the right most three62* bytes of the network address. This makes the two part address63* format convenient for specifying Class A network addresses as64* net.host.65*66* <p> When only one part is given, the value is stored directly in67* the network address without any byte rearrangement.68*69* <p> For methods that return a textual representation as output70* value, the first form, i.e. a dotted-quad string, is used.71*72* <h3> The Scope of a Multicast Address </h3>73*74* Historically the IPv4 TTL field in the IP header has doubled as a75* multicast scope field: a TTL of 0 means node-local, 1 means76* link-local, up through 32 means site-local, up through 64 means77* region-local, up through 128 means continent-local, and up through78* 255 are global. However, the administrative scoping is preferred.79* Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt">80* <i>RFC 2365: Administratively Scoped IP Multicast</i></a>81* @since 1.482*/8384public final85class Inet4Address extends InetAddress {86static final int INADDRSZ = 4;8788/** use serialVersionUID from InetAddress, but Inet4Address instance89* is always replaced by an InetAddress instance before being90* serialized */91@java.io.Serial92private static final long serialVersionUID = 3286316764910316507L;9394/*95* Perform initializations.96*/97static {98init();99}100101Inet4Address() {102super();103holder().hostName = null;104holder().address = 0;105holder().family = IPv4;106}107108Inet4Address(String hostName, byte addr[]) {109holder().hostName = hostName;110holder().family = IPv4;111if (addr != null) {112if (addr.length == INADDRSZ) {113int address = addr[3] & 0xFF;114address |= ((addr[2] << 8) & 0xFF00);115address |= ((addr[1] << 16) & 0xFF0000);116address |= ((addr[0] << 24) & 0xFF000000);117holder().address = address;118}119}120holder().originalHostName = hostName;121}122Inet4Address(String hostName, int address) {123holder().hostName = hostName;124holder().family = IPv4;125holder().address = address;126holder().originalHostName = hostName;127}128129/**130* Replaces the object to be serialized with an InetAddress object.131*132* @return the alternate object to be serialized.133*134* @throws ObjectStreamException if a new object replacing this135* object could not be created136*/137@java.io.Serial138private Object writeReplace() throws ObjectStreamException {139// will replace the to be serialized 'this' object140InetAddress inet = new InetAddress();141inet.holder().hostName = holder().getHostName();142inet.holder().address = holder().getAddress();143144/**145* Prior to 1.4 an InetAddress was created with a family146* based on the platform AF_INET value (usually 2).147* For compatibility reasons we must therefore write148* the InetAddress with this family.149*/150inet.holder().family = 2;151152return inet;153}154155/**156* Utility routine to check if the InetAddress is an157* IP multicast address. IP multicast address is a Class D158* address i.e first four bits of the address are 1110.159* @return a {@code boolean} indicating if the InetAddress is160* an IP multicast address161*/162public boolean isMulticastAddress() {163return ((holder().getAddress() & 0xf0000000) == 0xe0000000);164}165166/**167* Utility routine to check if the InetAddress is a wildcard address.168* @return a {@code boolean} indicating if the InetAddress is169* a wildcard address.170*/171public boolean isAnyLocalAddress() {172return holder().getAddress() == 0;173}174175/**176* Utility routine to check if the InetAddress is a loopback address.177*178* @return a {@code boolean} indicating if the InetAddress is179* a loopback address; or false otherwise.180*/181public boolean isLoopbackAddress() {182/* 127.x.x.x */183byte[] byteAddr = getAddress();184return byteAddr[0] == 127;185}186187/**188* Utility routine to check if the InetAddress is an link local address.189*190* @return a {@code boolean} indicating if the InetAddress is191* a link local address; or false if address is not a link local unicast address.192*/193public boolean isLinkLocalAddress() {194// link-local unicast in IPv4 (169.254.0.0/16)195// defined in "Documenting Special Use IPv4 Address Blocks196// that have been Registered with IANA" by Bill Manning197// draft-manning-dsua-06.txt198int address = holder().getAddress();199return (((address >>> 24) & 0xFF) == 169)200&& (((address >>> 16) & 0xFF) == 254);201}202203/**204* Utility routine to check if the InetAddress is a site local address.205*206* @return a {@code boolean} indicating if the InetAddress is207* a site local address; or false if address is not a site local unicast address.208*/209public boolean isSiteLocalAddress() {210// refer to RFC 1918211// 10/8 prefix212// 172.16/12 prefix213// 192.168/16 prefix214int address = holder().getAddress();215return (((address >>> 24) & 0xFF) == 10)216|| ((((address >>> 24) & 0xFF) == 172)217&& (((address >>> 16) & 0xF0) == 16))218|| ((((address >>> 24) & 0xFF) == 192)219&& (((address >>> 16) & 0xFF) == 168));220}221222/**223* Utility routine to check if the multicast address has global scope.224*225* @return a {@code boolean} indicating if the address has226* is a multicast address of global scope, false if it is not227* of global scope or it is not a multicast address228*/229public boolean isMCGlobal() {230// 224.0.1.0 to 238.255.255.255231byte[] byteAddr = getAddress();232return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&233!((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&234byteAddr[2] == 0);235}236237/**238* Utility routine to check if the multicast address has node scope.239*240* @return a {@code boolean} indicating if the address has241* is a multicast address of node-local scope, false if it is not242* of node-local scope or it is not a multicast address243*/244public boolean isMCNodeLocal() {245// unless ttl == 0246return false;247}248249/**250* Utility routine to check if the multicast address has link scope.251*252* @return a {@code boolean} indicating if the address has253* is a multicast address of link-local scope, false if it is not254* of link-local scope or it is not a multicast address255*/256public boolean isMCLinkLocal() {257// 224.0.0/24 prefix and ttl == 1258int address = holder().getAddress();259return (((address >>> 24) & 0xFF) == 224)260&& (((address >>> 16) & 0xFF) == 0)261&& (((address >>> 8) & 0xFF) == 0);262}263264/**265* Utility routine to check if the multicast address has site scope.266*267* @return a {@code boolean} indicating if the address has268* is a multicast address of site-local scope, false if it is not269* of site-local scope or it is not a multicast address270*/271public boolean isMCSiteLocal() {272// 239.255/16 prefix or ttl < 32273int address = holder().getAddress();274return (((address >>> 24) & 0xFF) == 239)275&& (((address >>> 16) & 0xFF) == 255);276}277278/**279* Utility routine to check if the multicast address has organization scope.280*281* @return a {@code boolean} indicating if the address has282* is a multicast address of organization-local scope,283* false if it is not of organization-local scope284* or it is not a multicast address285*/286public boolean isMCOrgLocal() {287// 239.192 - 239.195288int address = holder().getAddress();289return (((address >>> 24) & 0xFF) == 239)290&& (((address >>> 16) & 0xFF) >= 192)291&& (((address >>> 16) & 0xFF) <= 195);292}293294/**295* Returns the raw IP address of this {@code InetAddress}296* object. The result is in network byte order: the highest order297* byte of the address is in {@code getAddress()[0]}.298*299* @return the raw IP address of this object.300*/301public byte[] getAddress() {302int address = holder().getAddress();303byte[] addr = new byte[INADDRSZ];304305addr[0] = (byte) ((address >>> 24) & 0xFF);306addr[1] = (byte) ((address >>> 16) & 0xFF);307addr[2] = (byte) ((address >>> 8) & 0xFF);308addr[3] = (byte) (address & 0xFF);309return addr;310}311312/**313* Returns the 32-bit IPv4 address.314*/315int addressValue() {316return holder().getAddress();317}318319/**320* Returns the IP address string in textual presentation form.321*322* @return the raw IP address in a string format.323*/324public String getHostAddress() {325return numericToTextFormat(getAddress());326}327328/**329* Returns a hashcode for this IP address.330*331* @return a hash code value for this IP address.332*/333public int hashCode() {334return holder().getAddress();335}336337/**338* Compares this object against the specified object.339* The result is {@code true} if and only if the argument is340* not {@code null} and it represents the same IP address as341* this object.342* <p>343* Two instances of {@code InetAddress} represent the same IP344* address if the length of the byte arrays returned by345* {@code getAddress} is the same for both, and each of the346* array components is the same for the byte arrays.347*348* @param obj the object to compare against.349* @return {@code true} if the objects are the same;350* {@code false} otherwise.351* @see java.net.InetAddress#getAddress()352*/353public boolean equals(Object obj) {354return (obj instanceof Inet4Address inet4Address) &&355inet4Address.holder().getAddress() == holder().getAddress();356}357358// Utilities359360/**361* Converts IPv4 binary address into a string suitable for presentation.362*363* @param src a byte array representing an IPv4 numeric address364* @return a String representing the IPv4 address in365* textual representation format366*/367static String numericToTextFormat(byte[] src)368{369return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);370}371372/**373* Perform class load-time initializations.374*/375private static native void init();376}377378379