/* InetAddress.java -- Class to model an Internet address Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.net; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.IOException; import java.io.Serializable; import java.io.ObjectStreamException; /* * Written using on-line Java Platform 1.2 API Specification, as well * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). * (The latter turns out to have some errors ...) * Status: Believed complete and correct. */ /** * This class models an Internet address. It does not have a public * constructor. Instead, new instances of this objects are created * using the static methods getLocalHost(), getByName(), and * getAllByName(). *
* This class fulfills the function of the C style functions gethostname(),
* gethostbyname(), and gethostbyaddr(). It resolves Internet DNS names
* into their corresponding numeric addresses and vice versa.
*
* @author Aaron M. Renn InetAddress
as string
*/
public String toString ()
{
String result;
String address = getHostAddress ();
if (hostName != null)
result = hostName + "/" + address;
else
result = address;
return result;
}
/**
* Returns an InetAddress object given the raw IP address.
*
* The argument is in network byte order: the highest order byte of the
* address is in getAddress()[0].
*
* @param addr The IP address to create the InetAddress object from
*
* @exception UnknownHostException If IP address has illegal length
*
* @since 1.4
*/
public static InetAddress getByAddress (byte[] addr)
throws UnknownHostException
{
return getByAddress (null, addr);
}
/**
* Creates an InetAddress based on the provided host name and IP address.
* No name service is checked for the validity of the address.
*
* @param host The hostname of the InetAddress object to create
* @param addr The IP address to create the InetAddress object from
*
* @exception UnknownHostException If IP address is of illegal length
*
* @since 1.4
*/
public static InetAddress getByAddress (String host, byte[] addr)
throws UnknownHostException
{
if (addr.length == 4)
return new Inet4Address (addr, host);
if (addr.length == 16)
return new Inet6Address (addr, host);
throw new UnknownHostException ("IP address has illegal length");
}
/**
* If host is a valid numeric IP address, return the numeric address.
* Otherwise, return null.
*/
private static native byte[] aton (String host);
private static native InetAddress[] lookup (String hostname,
InetAddress addr, boolean all);
/**
* Determines the IP address of a host, given the host's name.
*
* @exception UnknownHostException If no IP address for the host could
* be found
* @exception SecurityException If a security manager exists and its
* checkConnect method doesn't allow the operation
*/
public static InetAddress getByName (String hostname)
throws UnknownHostException
{
SecurityManager s = System.getSecurityManager ();
if (s != null)
s.checkConnect (hostname, -1);
// Default to current host if necessary
if (hostname == null)
return getLocalHost ();
// Assume that the host string is an IP address
byte[] address = aton (hostname);
if (address != null)
{
if (address.length == 4)
return new Inet4Address (address, null);
else if (address.length == 16)
{
if ((address [10] == 0xFF) && (address [11] == 0xFF))
{
byte[] ip4addr = new byte [4];
ip4addr [0] = address [12];
ip4addr [1] = address [13];
ip4addr [2] = address [14];
ip4addr [3] = address [15];
return new Inet4Address (ip4addr, null);
}
return new Inet6Address (address, null);
}
else
throw new UnknownHostException ("Address has invalid length");
}
// Try to resolve the host by DNS
InetAddress[] addresses = getAllByName (hostname);
return addresses [0];
}
/**
* Given the name of a host, returns an array of its IP addresses,
* based on the configured name service on the system.
*
* @exception UnknownHostException If no IP address for the host could
* be found
* @exception SecurityException If a security manager exists and its
* checkConnect method doesn't allow the operation
*/
public static InetAddress[] getAllByName (String hostname)
throws UnknownHostException
{
SecurityManager s = System.getSecurityManager ();
if (s != null)
s.checkConnect (hostname, -1);
// Check if hostname is an IP address
byte[] address = aton (hostname);
if (address != null)
{
InetAddress[] result = new InetAddress [1];
result [0] = new InetAddress (address, null);
return result;
}
// Try to resolve the hostname by DNS
return lookup (hostname, null, true);
}
static final byte[] zeros = { 0, 0, 0, 0 };
/* dummy InetAddress, used to bind socket to any (all) network interfaces */
static final InetAddress ANY_IF = new InetAddress (zeros, null);
private static final byte[] localhostAddress = { 127, 0, 0, 1 };
private static native String getLocalHostname ();
private static InetAddress localhost = null;
/**
* Returns the local host
*
* @exception UnknownHostException If no IP address for the host could
* be found
*/
public static InetAddress getLocalHost () throws UnknownHostException
{
SecurityManager s = System.getSecurityManager ();
// Experimentation shows that JDK1.2 does cache the result.
// However, if there is a security manager, and the cached result
// is other than "localhost", we need to check again.
if (localhost == null
|| (s != null && localhost.addr != localhostAddress))
getLocalHost (s);
return localhost;
}
private static synchronized void getLocalHost (SecurityManager s)
throws UnknownHostException
{
// Check the localhost cache again, now that we've synchronized.
if (s == null && localhost != null)
return;
String hostname = getLocalHostname ();
if (s != null)
{
// "The Java Class Libraries" suggests that if the security
// manager disallows getting the local host name, then
// we use the loopback host.
// However, the JDK 1.2 API claims to throw SecurityException,
// which seems to suggest SecurityException is *not* caught.
// In this case, experimentation shows that former is correct.
try
{
// This is wrong, if the name returned from getLocalHostname()
// is not a fully qualified name. FIXME.
s.checkConnect (hostname, -1);
}
catch (SecurityException ex)
{
hostname = null;
}
}
if (hostname != null)
{
try
{
localhost = new InetAddress (null, null);
lookup (hostname, localhost, false);
}
catch (Exception ex)
{
}
}
if (localhost == null)
localhost = new InetAddress (localhostAddress, "localhost");
}
}