Index: java/net/Inet4Address.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/net/Inet4Address.java,v retrieving revision 1.11 diff -u -r1.11 Inet4Address.java --- java/net/Inet4Address.java 6 Sep 2004 17:00:08 -0000 1.11 +++ java/net/Inet4Address.java 13 Oct 2004 07:17:05 -0000 @@ -1,5 +1,5 @@ -/* Inet4Address.java - Copyright (C) 2002, 2003 Free Software Foundation, Inc. +/* Inet4Address.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -35,23 +35,20 @@ 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.ObjectStreamException; import java.util.Arrays; - -/** - * @author Michael Koch - * @date August 3, 2002. - */ - /* * Written using on-line Java Platform 1.4 API Specification and * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt), * RFC 1918 (http://www.ietf.org/rfc/rfc1918.txt), * RFC 2365 (http://www.ietf.org/rfc/rfc2365.txt) - * Status: Believed complete and correct. + * + * @author Michael Koch + * @status Believed complete and correct. */ public final class Inet4Address extends InetAddress { @@ -65,47 +62,22 @@ */ private Object writeReplace() throws ObjectStreamException { - return new InetAddress(addr, hostName, null); + return new InetAddress(addr, hostName); } /** * Initializes this object's addr instance variable from the passed in - * byte array. Note that this constructor is package-private and is called - * only by static methods in InetAddress. - * - * @param addr - */ - Inet4Address(byte[] addr) - { - this(addr, null, null); - } - - /** - * Creates a Inet4Address - * - * @param addr The IP address - * @param host The Hostname - */ - Inet4Address(byte[] addr, String host) - { - this(addr, host, null); - } - - /** - * Initializes this object's addr instance variable from the passed in * byte array. Note that this constructor is protected and is called * only by static methods in this class. * * @param addr The IP number of this address as an array of bytes * @param hostname The hostname of this IP address. - * @param hostname_alias A backup hostname to use if hostname is null to - * prevent reverse lookup failures */ - Inet4Address(byte[] addr, String hostname, String hostname_alias) + Inet4Address(byte[] addr, String host) { - super(addr, hostname, hostname_alias); + super(addr, host); } - + /** * Checks if the address is a multicast address * Index: java/net/Inet6Address.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/net/Inet6Address.java,v retrieving revision 1.7 diff -u -r1.7 Inet6Address.java --- java/net/Inet6Address.java 29 Apr 2004 21:16:42 -0000 1.7 +++ java/net/Inet6Address.java 13 Oct 2004 07:17:05 -0000 @@ -1,5 +1,5 @@ -/* Inet6Address.java - Copyright (C) 2002, 2003 Free Software Foundation, Inc. +/* Inet6Address.java -- + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -35,20 +35,17 @@ obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.net; import java.util.Arrays; - -/** - * @author Michael Koch - * @date August 3, 2002. - */ - /* * Written using on-line Java Platform 1.4 API Specification and * RFC 1884 (http://www.ietf.org/rfc/rfc1884.txt) - * Status: Believed complete and correct. + * + * @author Michael Koch + * @status Believed complete and correct. */ public final class Inet6Address extends InetAddress { @@ -67,7 +64,7 @@ */ Inet6Address(byte[] addr, String host) { - super(addr, host, null); + super(addr, host); this.ipaddress = addr; } Index: java/net/InetAddress.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/net/InetAddress.java,v retrieving revision 1.32 diff -u -r1.32 InetAddress.java --- java/net/InetAddress.java 30 Sep 2004 15:09:47 -0000 1.32 +++ java/net/InetAddress.java 13 Oct 2004 07:17:06 -0000 @@ -94,6 +94,11 @@ static InetAddress ANY_IF; /** + * Stores static localhost address object. + */ + static InetAddress LOCALHOST; + + /** * The size of the cache. */ private static int cache_size = 0; @@ -145,12 +150,15 @@ try { ANY_IF = getInaddrAny(); + + byte[] ip_localhost = { 127, 0, 0, 1 }; + LOCALHOST = new Inet4Address(ip_localhost, "localhost"); } catch (UnknownHostException uhe) { // Hmmm, make one up and hope that it works. byte[] zeros = { 0, 0, 0, 0 }; - ANY_IF = new Inet4Address(zeros); + ANY_IF = new Inet4Address(zeros, "0.0.0.0"); } } @@ -172,11 +180,6 @@ String hostName; /** - * Backup hostname alias for this address. - */ - transient String hostname_alias; - - /** * The time this address was looked up. */ transient long lookup_time; @@ -197,10 +200,8 @@ * * @param ipaddr The IP number of this address as an array of bytes * @param hostname The hostname of this IP address. - * @param hostname_alias A backup hostname to use if hostname is null to - * prevent reverse lookup failures */ - InetAddress(byte[] ipaddr, String hostname, String hostname_alias) + InetAddress(byte[] ipaddr, String hostname) { addr = new byte[ipaddr.length]; @@ -208,7 +209,6 @@ addr[i] = ipaddr[i]; this.hostName = hostname; - this.hostname_alias = hostname_alias; lookup_time = System.currentTimeMillis(); family = 2; /* AF_INET */ @@ -386,11 +386,33 @@ } catch (UnknownHostException e) { - if (hostname_alias != null) - return hostname_alias; - else - return getHostAddress(); + return getHostAddress(); + } + } + + /** + * Returns the canonical hostname represented by this InetAddress + * + * @since 1.4 + */ + public String getCanonicalHostName() + { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + { + try + { + sm.checkConnect(hostName, -1); + } + catch (SecurityException e) + { + return getHostAddress(); + } } + + // Try to find the FDQN now + InetAddress address = new Inet4Address(getAddress(), null); + return address.getHostName(); } /** @@ -501,8 +523,6 @@ if (hostName != null) host = hostName; - else if (hostname_alias != null) - host = hostname_alias; else host = address; @@ -608,20 +628,6 @@ 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 || hostname.length() == 0) - return getLocalHost(); - - // Assume that the host string is an IP address - byte[] address = aton(hostname); - if (address != null) - return new Inet4Address(address); - - // Try to resolve the host by DNS InetAddress[] addresses = getAllByName(hostname); return addresses[0]; } @@ -650,15 +656,18 @@ if (s != null) s.checkConnect(hostname, -1); + InetAddress[] addresses; + // Default to current host if necessary if (hostname == null) { - InetAddress local = getLocalHost(); - return getAllByName(local.getHostName()); + addresses = new InetAddress[1]; + addresses[0] = LOCALHOST; + return addresses; } // Check the cache for this host before doing a lookup - InetAddress[] addresses = checkCacheFor(hostname); + addresses = checkCacheFor(hostname); if (addresses != null) return addresses; @@ -676,11 +685,7 @@ if (iplist[i].length != 4) throw new UnknownHostException(hostname); - // Don't store the hostname in order to force resolution of the - // canonical names of these ip's when the user asks for the hostname - // But do specify the host alias so if the IP returned won't - // reverse lookup we don't throw an exception. - addresses[i] = new Inet4Address(iplist[i], null, hostname); + addresses[i] = new Inet4Address(iplist[i], hostname); } addToCache(hostname, addresses); @@ -760,7 +765,7 @@ if (inaddr_any == null) { byte[] tmp = lookupInaddrAny(); - inaddr_any = new Inet4Address(tmp); + inaddr_any = new Inet4Address(tmp, null); } return inaddr_any;