0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.io.ObjectStreamException;
0N/Aimport sun.security.action.*;
0N/A
0N/A/**
0N/A * This class represents an Internet Protocol version 4 (IPv4) address.
0N/A * Defined by <a href="http://www.ietf.org/rfc/rfc790.txt">
0N/A * <i>RFC&nbsp;790: Assigned Numbers</i></a>,
0N/A * <a href="http://www.ietf.org/rfc/rfc1918.txt">
0N/A * <i>RFC&nbsp;1918: Address Allocation for Private Internets</i></a>,
0N/A * and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC&nbsp;2365:
0N/A * Administratively Scoped IP Multicast</i></a>
0N/A *
0N/A * <h4> <A NAME="format">Textual representation of IP addresses</a> </h4>
0N/A *
0N/A * Textual representation of IPv4 address used as input to methods
0N/A * takes one of the following forms:
0N/A *
0N/A * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
0N/A * <tr><td><tt>d.d.d.d</tt></td></tr>
0N/A * <tr><td><tt>d.d.d</tt></td></tr>
0N/A * <tr><td><tt>d.d</tt></td></tr>
0N/A * <tr><td><tt>d</tt></td></tr>
0N/A * </table></blockquote>
0N/A *
0N/A * <p> When four parts are specified, each is interpreted as a byte of
0N/A * data and assigned, from left to right, to the four bytes of an IPv4
0N/A * address.
0N/A
0N/A * <p> When a three part address is specified, the last part is
0N/A * interpreted as a 16-bit quantity and placed in the right most two
0N/A * bytes of the network address. This makes the three part address
0N/A * format convenient for specifying Class B net- work addresses as
0N/A * 128.net.host.
0N/A *
0N/A * <p> When a two part address is supplied, the last part is
0N/A * interpreted as a 24-bit quantity and placed in the right most three
0N/A * bytes of the network address. This makes the two part address
0N/A * format convenient for specifying Class A network addresses as
0N/A * net.host.
0N/A *
0N/A * <p> When only one part is given, the value is stored directly in
0N/A * the network address without any byte rearrangement.
0N/A *
0N/A * <p> For methods that return a textual representation as output
0N/A * value, the first form, i.e. a dotted-quad string, is used.
0N/A *
0N/A * <h4> The Scope of a Multicast Address </h4>
0N/A *
0N/A * Historically the IPv4 TTL field in the IP header has doubled as a
0N/A * multicast scope field: a TTL of 0 means node-local, 1 means
0N/A * link-local, up through 32 means site-local, up through 64 means
0N/A * region-local, up through 128 means continent-local, and up through
0N/A * 255 are global. However, the administrative scoping is preferred.
0N/A * Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt">
0N/A * <i>RFC&nbsp;2365: Administratively Scoped IP Multicast</i></a>
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic final
0N/Aclass Inet4Address extends InetAddress {
0N/A final static int INADDRSZ = 4;
0N/A
0N/A /** use serialVersionUID from InetAddress, but Inet4Address instance
0N/A * is always replaced by an InetAddress instance before being
0N/A * serialized */
0N/A private static final long serialVersionUID = 3286316764910316507L;
0N/A
0N/A /*
0N/A * Perform initializations.
0N/A */
0N/A static {
0N/A init();
0N/A }
0N/A
0N/A Inet4Address() {
0N/A super();
5888N/A holder().hostName = null;
5888N/A holder().address = 0;
5888N/A holder().family = IPv4;
0N/A }
0N/A
0N/A Inet4Address(String hostName, byte addr[]) {
5888N/A holder().hostName = hostName;
5888N/A holder().family = IPv4;
0N/A if (addr != null) {
0N/A if (addr.length == INADDRSZ) {
5888N/A int address = addr[3] & 0xFF;
0N/A address |= ((addr[2] << 8) & 0xFF00);
0N/A address |= ((addr[1] << 16) & 0xFF0000);
0N/A address |= ((addr[0] << 24) & 0xFF000000);
5888N/A holder().address = address;
0N/A }
0N/A }
0N/A }
0N/A Inet4Address(String hostName, int address) {
5888N/A holder().hostName = hostName;
5888N/A holder().family = IPv4;
5888N/A holder().address = address;
0N/A }
0N/A
0N/A /**
0N/A * Replaces the object to be serialized with an InetAddress object.
0N/A *
0N/A * @return the alternate object to be serialized.
0N/A *
0N/A * @throws ObjectStreamException if a new object replacing this
0N/A * object could not be created
0N/A */
0N/A private Object writeReplace() throws ObjectStreamException {
0N/A // will replace the to be serialized 'this' object
0N/A InetAddress inet = new InetAddress();
5888N/A inet.holder().hostName = holder().getHostName();
5888N/A inet.holder().address = holder().getAddress();
0N/A
0N/A /**
0N/A * Prior to 1.4 an InetAddress was created with a family
0N/A * based on the platform AF_INET value (usually 2).
0N/A * For compatibility reasons we must therefore write the
0N/A * the InetAddress with this family.
0N/A */
5888N/A inet.holder().family = 2;
0N/A
0N/A return inet;
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the InetAddress is an
0N/A * IP multicast address. IP multicast address is a Class D
0N/A * address i.e first four bits of the address are 1110.
0N/A * @return a <code>boolean</code> indicating if the InetAddress is
0N/A * an IP multicast address
0N/A * @since JDK1.1
0N/A */
0N/A public boolean isMulticastAddress() {
5888N/A return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the InetAddress in a wildcard address.
0N/A * @return a <code>boolean</code> indicating if the Inetaddress is
0N/A * a wildcard address.
0N/A * @since 1.4
0N/A */
0N/A public boolean isAnyLocalAddress() {
5888N/A return holder().getAddress() == 0;
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the InetAddress is a loopback address.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the InetAddress is
0N/A * a loopback address; or false otherwise.
0N/A * @since 1.4
0N/A */
0N/A private static final int loopback = 2130706433; /* 127.0.0.1 */
0N/A public boolean isLoopbackAddress() {
0N/A /* 127.x.x.x */
0N/A byte[] byteAddr = getAddress();
0N/A return byteAddr[0] == 127;
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the InetAddress is an link local address.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the InetAddress is
0N/A * a link local address; or false if address is not a link local unicast address.
0N/A * @since 1.4
0N/A */
0N/A public boolean isLinkLocalAddress() {
0N/A // link-local unicast in IPv4 (169.254.0.0/16)
0N/A // defined in "Documenting Special Use IPv4 Address Blocks
0N/A // that have been Registered with IANA" by Bill Manning
0N/A // draft-manning-dsua-06.txt
5888N/A int address = holder().getAddress();
0N/A return (((address >>> 24) & 0xFF) == 169)
0N/A && (((address >>> 16) & 0xFF) == 254);
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the InetAddress is a site local address.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the InetAddress is
0N/A * a site local address; or false if address is not a site local unicast address.
0N/A * @since 1.4
0N/A */
0N/A public boolean isSiteLocalAddress() {
0N/A // refer to RFC 1918
0N/A // 10/8 prefix
0N/A // 172.16/12 prefix
0N/A // 192.168/16 prefix
5888N/A int address = holder().getAddress();
0N/A return (((address >>> 24) & 0xFF) == 10)
0N/A || ((((address >>> 24) & 0xFF) == 172)
0N/A && (((address >>> 16) & 0xF0) == 16))
0N/A || ((((address >>> 24) & 0xFF) == 192)
0N/A && (((address >>> 16) & 0xFF) == 168));
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the multicast address has global scope.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the address has
0N/A * is a multicast address of global scope, false if it is not
0N/A * of global scope or it is not a multicast address
0N/A * @since 1.4
0N/A */
0N/A public boolean isMCGlobal() {
0N/A // 224.0.1.0 to 238.255.255.255
0N/A byte[] byteAddr = getAddress();
0N/A return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
0N/A !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 &&
0N/A byteAddr[2] == 0);
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the multicast address has node scope.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the address has
0N/A * is a multicast address of node-local scope, false if it is not
0N/A * of node-local scope or it is not a multicast address
0N/A * @since 1.4
0N/A */
0N/A public boolean isMCNodeLocal() {
0N/A // unless ttl == 0
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the multicast address has link scope.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the address has
0N/A * is a multicast address of link-local scope, false if it is not
0N/A * of link-local scope or it is not a multicast address
0N/A * @since 1.4
0N/A */
0N/A public boolean isMCLinkLocal() {
0N/A // 224.0.0/24 prefix and ttl == 1
5888N/A int address = holder().getAddress();
0N/A return (((address >>> 24) & 0xFF) == 224)
0N/A && (((address >>> 16) & 0xFF) == 0)
0N/A && (((address >>> 8) & 0xFF) == 0);
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the multicast address has site scope.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the address has
0N/A * is a multicast address of site-local scope, false if it is not
0N/A * of site-local scope or it is not a multicast address
0N/A * @since 1.4
0N/A */
0N/A public boolean isMCSiteLocal() {
0N/A // 239.255/16 prefix or ttl < 32
5888N/A int address = holder().getAddress();
0N/A return (((address >>> 24) & 0xFF) == 239)
0N/A && (((address >>> 16) & 0xFF) == 255);
0N/A }
0N/A
0N/A /**
0N/A * Utility routine to check if the multicast address has organization scope.
0N/A *
0N/A * @return a <code>boolean</code> indicating if the address has
0N/A * is a multicast address of organization-local scope,
0N/A * false if it is not of organization-local scope
0N/A * or it is not a multicast address
0N/A * @since 1.4
0N/A */
0N/A public boolean isMCOrgLocal() {
0N/A // 239.192 - 239.195
5888N/A int address = holder().getAddress();
0N/A return (((address >>> 24) & 0xFF) == 239)
0N/A && (((address >>> 16) & 0xFF) >= 192)
0N/A && (((address >>> 16) & 0xFF) <= 195);
0N/A }
0N/A
0N/A /**
0N/A * Returns the raw IP address of this <code>InetAddress</code>
0N/A * object. The result is in network byte order: the highest order
0N/A * byte of the address is in <code>getAddress()[0]</code>.
0N/A *
0N/A * @return the raw IP address of this object.
0N/A */
0N/A public byte[] getAddress() {
5888N/A int address = holder().getAddress();
0N/A byte[] addr = new byte[INADDRSZ];
0N/A
0N/A addr[0] = (byte) ((address >>> 24) & 0xFF);
0N/A addr[1] = (byte) ((address >>> 16) & 0xFF);
0N/A addr[2] = (byte) ((address >>> 8) & 0xFF);
0N/A addr[3] = (byte) (address & 0xFF);
0N/A return addr;
0N/A }
0N/A
0N/A /**
0N/A * Returns the IP address string in textual presentation form.
0N/A *
0N/A * @return the raw IP address in a string format.
0N/A * @since JDK1.0.2
0N/A */
0N/A public String getHostAddress() {
0N/A return numericToTextFormat(getAddress());
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this IP address.
0N/A *
0N/A * @return a hash code value for this IP address.
0N/A */
0N/A public int hashCode() {
5888N/A return holder().getAddress();
0N/A }
0N/A
0N/A /**
0N/A * Compares this object against the specified object.
0N/A * The result is <code>true</code> if and only if the argument is
0N/A * not <code>null</code> and it represents the same IP address as
0N/A * this object.
0N/A * <p>
0N/A * Two instances of <code>InetAddress</code> represent the same IP
0N/A * address if the length of the byte arrays returned by
0N/A * <code>getAddress</code> is the same for both, and each of the
0N/A * array components is the same for the byte arrays.
0N/A *
0N/A * @param obj the object to compare against.
0N/A * @return <code>true</code> if the objects are the same;
0N/A * <code>false</code> otherwise.
0N/A * @see java.net.InetAddress#getAddress()
0N/A */
0N/A public boolean equals(Object obj) {
0N/A return (obj != null) && (obj instanceof Inet4Address) &&
5888N/A (((InetAddress)obj).holder().getAddress() == holder().getAddress());
0N/A }
0N/A
0N/A // Utilities
0N/A /*
0N/A * Converts IPv4 binary address into a string suitable for presentation.
0N/A *
0N/A * @param src a byte array representing an IPv4 numeric address
0N/A * @return a String representing the IPv4 address in
0N/A * textual representation format
0N/A * @since 1.4
0N/A */
0N/A
0N/A static String numericToTextFormat(byte[] src)
0N/A {
0N/A return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
0N/A }
0N/A
0N/A /**
0N/A * Perform class load-time initializations.
0N/A */
0N/A private static native void init();
0N/A}