Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/Inet4Address.java
38829 views
/*1* Copyright (c) 2000, 2013, 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* <h3> <A NAME="format">Textual representation of IP addresses</a> </h3>39*40* Textual representation of IPv4 address used as input to methods41* takes one of the following forms:42*43* <blockquote><table cellpadding=0 cellspacing=0 summary="layout">44* <tr><td>{@code d.d.d.d}</td></tr>45* <tr><td>{@code d.d.d}</td></tr>46* <tr><td>{@code d.d}</td></tr>47* <tr><td>{@code d}</td></tr>48* </table></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.5354* <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* <h4> The Scope of a Multicast Address </h4>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 {86final static int INADDRSZ = 4;8788/** use serialVersionUID from InetAddress, but Inet4Address instance89* is always replaced by an InetAddress instance before being90* serialized */91private static final long serialVersionUID = 3286316764910316507L;9293/*94* Perform initializations.95*/96static {97init();98}99100Inet4Address() {101super();102holder().hostName = null;103holder().address = 0;104holder().family = IPv4;105}106107Inet4Address(String hostName, byte addr[]) {108holder().hostName = hostName;109holder().family = IPv4;110if (addr != null) {111if (addr.length == INADDRSZ) {112int address = addr[3] & 0xFF;113address |= ((addr[2] << 8) & 0xFF00);114address |= ((addr[1] << 16) & 0xFF0000);115address |= ((addr[0] << 24) & 0xFF000000);116holder().address = address;117}118}119holder().originalHostName = hostName;120}121Inet4Address(String hostName, int address) {122holder().hostName = hostName;123holder().family = IPv4;124holder().address = address;125holder().originalHostName = hostName;126}127128/**129* Replaces the object to be serialized with an InetAddress object.130*131* @return the alternate object to be serialized.132*133* @throws ObjectStreamException if a new object replacing this134* object could not be created135*/136private Object writeReplace() throws ObjectStreamException {137// will replace the to be serialized 'this' object138InetAddress inet = new InetAddress();139inet.holder().hostName = holder().getHostName();140inet.holder().address = holder().getAddress();141142/**143* Prior to 1.4 an InetAddress was created with a family144* based on the platform AF_INET value (usually 2).145* For compatibility reasons we must therefore write the146* the InetAddress with this family.147*/148inet.holder().family = 2;149150return inet;151}152153/**154* Utility routine to check if the InetAddress is an155* IP multicast address. IP multicast address is a Class D156* address i.e first four bits of the address are 1110.157* @return a {@code boolean} indicating if the InetAddress is158* an IP multicast address159* @since JDK1.1160*/161public boolean isMulticastAddress() {162return ((holder().getAddress() & 0xf0000000) == 0xe0000000);163}164165/**166* Utility routine to check if the InetAddress in a wildcard address.167* @return a {@code boolean} indicating if the Inetaddress is168* a wildcard address.169* @since 1.4170*/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* @since 1.4181*/182public boolean isLoopbackAddress() {183/* 127.x.x.x */184byte[] byteAddr = getAddress();185return byteAddr[0] == 127;186}187188/**189* Utility routine to check if the InetAddress is an link local address.190*191* @return a {@code boolean} indicating if the InetAddress is192* a link local address; or false if address is not a link local unicast address.193* @since 1.4194*/195public boolean isLinkLocalAddress() {196// link-local unicast in IPv4 (169.254.0.0/16)197// defined in "Documenting Special Use IPv4 Address Blocks198// that have been Registered with IANA" by Bill Manning199// draft-manning-dsua-06.txt200int address = holder().getAddress();201return (((address >>> 24) & 0xFF) == 169)202&& (((address >>> 16) & 0xFF) == 254);203}204205/**206* Utility routine to check if the InetAddress is a site local address.207*208* @return a {@code boolean} indicating if the InetAddress is209* a site local address; or false if address is not a site local unicast address.210* @since 1.4211*/212public boolean isSiteLocalAddress() {213// refer to RFC 1918214// 10/8 prefix215// 172.16/12 prefix216// 192.168/16 prefix217int address = holder().getAddress();218return (((address >>> 24) & 0xFF) == 10)219|| ((((address >>> 24) & 0xFF) == 172)220&& (((address >>> 16) & 0xF0) == 16))221|| ((((address >>> 24) & 0xFF) == 192)222&& (((address >>> 16) & 0xFF) == 168));223}224225/**226* Utility routine to check if the multicast address has global scope.227*228* @return a {@code boolean} indicating if the address has229* is a multicast address of global scope, false if it is not230* of global scope or it is not a multicast address231* @since 1.4232*/233public boolean isMCGlobal() {234// 224.0.1.0 to 238.255.255.255235byte[] byteAddr = getAddress();236return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&237!((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&238byteAddr[2] == 0);239}240241/**242* Utility routine to check if the multicast address has node scope.243*244* @return a {@code boolean} indicating if the address has245* is a multicast address of node-local scope, false if it is not246* of node-local scope or it is not a multicast address247* @since 1.4248*/249public boolean isMCNodeLocal() {250// unless ttl == 0251return false;252}253254/**255* Utility routine to check if the multicast address has link scope.256*257* @return a {@code boolean} indicating if the address has258* is a multicast address of link-local scope, false if it is not259* of link-local scope or it is not a multicast address260* @since 1.4261*/262public boolean isMCLinkLocal() {263// 224.0.0/24 prefix and ttl == 1264int address = holder().getAddress();265return (((address >>> 24) & 0xFF) == 224)266&& (((address >>> 16) & 0xFF) == 0)267&& (((address >>> 8) & 0xFF) == 0);268}269270/**271* Utility routine to check if the multicast address has site scope.272*273* @return a {@code boolean} indicating if the address has274* is a multicast address of site-local scope, false if it is not275* of site-local scope or it is not a multicast address276* @since 1.4277*/278public boolean isMCSiteLocal() {279// 239.255/16 prefix or ttl < 32280int address = holder().getAddress();281return (((address >>> 24) & 0xFF) == 239)282&& (((address >>> 16) & 0xFF) == 255);283}284285/**286* Utility routine to check if the multicast address has organization scope.287*288* @return a {@code boolean} indicating if the address has289* is a multicast address of organization-local scope,290* false if it is not of organization-local scope291* or it is not a multicast address292* @since 1.4293*/294public boolean isMCOrgLocal() {295// 239.192 - 239.195296int address = holder().getAddress();297return (((address >>> 24) & 0xFF) == 239)298&& (((address >>> 16) & 0xFF) >= 192)299&& (((address >>> 16) & 0xFF) <= 195);300}301302/**303* Returns the raw IP address of this {@code InetAddress}304* object. The result is in network byte order: the highest order305* byte of the address is in {@code getAddress()[0]}.306*307* @return the raw IP address of this object.308*/309public byte[] getAddress() {310int address = holder().getAddress();311byte[] addr = new byte[INADDRSZ];312313addr[0] = (byte) ((address >>> 24) & 0xFF);314addr[1] = (byte) ((address >>> 16) & 0xFF);315addr[2] = (byte) ((address >>> 8) & 0xFF);316addr[3] = (byte) (address & 0xFF);317return addr;318}319320/**321* Returns the IP address string in textual presentation form.322*323* @return the raw IP address in a string format.324* @since JDK1.0.2325*/326public String getHostAddress() {327return numericToTextFormat(getAddress());328}329330/**331* Returns a hashcode for this IP address.332*333* @return a hash code value for this IP address.334*/335public int hashCode() {336return holder().getAddress();337}338339/**340* Compares this object against the specified object.341* The result is {@code true} if and only if the argument is342* not {@code null} and it represents the same IP address as343* this object.344* <p>345* Two instances of {@code InetAddress} represent the same IP346* address if the length of the byte arrays returned by347* {@code getAddress} is the same for both, and each of the348* array components is the same for the byte arrays.349*350* @param obj the object to compare against.351* @return {@code true} if the objects are the same;352* {@code false} otherwise.353* @see java.net.InetAddress#getAddress()354*/355public boolean equals(Object obj) {356return (obj != null) && (obj instanceof Inet4Address) &&357(((InetAddress)obj).holder().getAddress() == holder().getAddress());358}359360// Utilities361/*362* Converts IPv4 binary address into a string suitable for presentation.363*364* @param src a byte array representing an IPv4 numeric address365* @return a String representing the IPv4 address in366* textual representation format367* @since 1.4368*/369370static String numericToTextFormat(byte[] src)371{372return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);373}374375/**376* Perform class load-time initializations.377*/378private static native void init();379}380381382