2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import java.io.IOException;
41 import java.io.ObjectStreamException;
44 * @author Michael Koch
45 * @date August 3, 2002.
49 * Written using on-line Java Platform 1.4 API Specification and
50 * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt),
51 * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt),
52 * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt)
53 * Status: Believed complete and correct.
56 public final class Inet4Address extends InetAddress
58 static final long serialVersionUID = 7615067291688066509L;
61 * needed for serialization
63 private Object writeReplace () throws ObjectStreamException
65 return new InetAddress (addr, hostName);
69 * Creates a Inet4Address
71 * @param addr The IP address
72 * @param host The Hostname
74 protected Inet4Address(byte[] addr, String host)
80 * Checks if the address is a multicast address
84 public boolean isMulticastAddress ()
86 return (addr [0] & 0xF0) == 0xE0;
90 * Checks if this address is a loopback address
92 public boolean isLoopbackAddress ()
94 return addr [0] == 0x7F;
98 * Checks if this address is a wildcard address
102 public boolean isAnyLocalAddress ()
104 byte[] anylocal = { 0, 0, 0, 0 };
106 return addr == anylocal;
110 * Checks if this address is a link local address
114 public boolean isLinkLocalAddress ()
116 // XXX: This seems to not exist with IPv4 addresses
121 * Checks if this address is a site local address
125 public boolean isSiteLocalAddress ()
128 if (addr [0] == 0x0A)
131 // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here:
132 // it says 172.16.0.0 - 172.255.255.255 are site local addresses
135 if (addr [0] == 0xAC && (addr [1] & 0xF0) == 0x01)
139 if (addr [0] == 0xC0 && addr [1] == 0xA8)
142 // XXX: Do we need to check more addresses here ?
147 * Checks if this multicast address has global scope
151 public boolean isMCGlobal ()
153 // XXX: This seems to net exist with IPv4 addresses
158 * Checks if this multicast address has node scope
162 public boolean isMCNodeLocal ()
164 // XXX: This seems to net exist with IPv4 addresses
169 * Checks if this multicast address has link scope
173 public boolean isMCLinkLocal ()
175 if (!isMulticastAddress ())
178 return (addr [0] == 0xE0)
179 && (addr [1] == 0x00)
180 && (addr [2] == 0x00);
184 * Checks if this multicast address has site scope
188 public boolean isMCSiteLocal ()
190 // XXX: This seems to net exist with IPv4 addresses
195 * Checks if this multicast address has organization scope
199 public boolean isMCOrgLocal ()
201 // XXX: This seems to net exist with IPv4 addresses
206 * Returns the address of the current instance
208 public byte[] getAddress ()
214 * Returns the address as string
218 public String getHostAddress ()
220 StringBuffer sbuf = new StringBuffer (40);
221 int len = addr.length;
226 sbuf.append (addr [i] & 0xFF);
235 return sbuf.toString ();
239 * Computes the hashcode of the instance
241 public int hashCode ()
244 int len = addr.length;
245 int i = len > 4 ? len - 4 : 0;
247 for ( ; i < len; i++)
248 hash = (hash << 8) | (addr [i] & 0xFF);
254 * Compare the current Inet4Address instance with obj
256 * @param obj Object to compare with
258 public boolean equals (Object obj)
260 if (obj == null || ! (obj instanceof InetAddress))
264 byte[] addr2 = ((InetAddress) obj).addr;
266 if (addr1.length != addr2.length)
269 for (int i = addr1.length; --i >= 0; )
270 if (addr1 [i] != addr2 [i])
275 } // class Inet4Address