From 75fe3383d87ba0a14c725537c9063f55d722cee3 Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Fri, 1 Nov 2002 06:35:14 +0000 Subject: [PATCH] 2002-11-01 Michael Koch * java/net/InetAddress.java: (isAnyLocalAddress): Implemented. (isLoopbackAddress): Implemented, comment added. (isLinkLocalAddress): Implemented, documentation added. (isSiteLocalAddress): Implemented, documentation added. (isMCGlobal): Implemented, documentation added. (isMCNodeLocal): Implemented, documentation added. (isMCLinkLocal): Implemented, documentation added. (isMCSiteLocal): Implemented, documentation added. (isMCOrgLocal): Implemented, documentation added. (getHostName): Documentation added. (getCanonicalHostName): Implemented, documentation added. (getAddress): Documentation added. (hashCode): Documentation added. (equals): Documentation added. (toString): Fixed implementation. (getByAddress): Use Inet4Address and Inet6Address. (lookup): New linewrap. (getByName): SecurityManager check added, support Inet4Address and Inet6address, comments added. (getAllByName): SecurityManager check added, comments added. * java/net/Inet6Address.java: (Inet6Address): Initialize parent class with addr instead of null. * java/net/URL.java (equals): Documentation added. (getFile): Documentation added. (hashCode): Documentation added. * java/net/natInetAddress.cc: (aton): Fix IPv6 support. * java/net/natPlainDatagramSocketImpl.cc: (peek): Throw PortUnreachableException when suitable. (peekData): Throw PortUnreachableException when suitable. (send): Throw PortUnreachableException when suitable. (receive): Throw PortUnreachableException when suitable. From-SVN: r58704 --- libjava/ChangeLog | 37 ++++ libjava/java/net/Inet6Address.java | 2 +- libjava/java/net/InetAddress.java | 186 +++++++++++++++--- libjava/java/net/URL.java | 9 + libjava/java/net/natInetAddress.cc | 2 +- .../java/net/natPlainDatagramSocketImpl.cc | 9 + 6 files changed, 215 insertions(+), 30 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index b5b0f3eeebe..8d04a293ba9 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,40 @@ +2002-11-01 Michael Koch + + * java/net/InetAddress.java: + (isAnyLocalAddress): Implemented. + (isLoopbackAddress): Implemented, comment added. + (isLinkLocalAddress): Implemented, documentation added. + (isSiteLocalAddress): Implemented, documentation added. + (isMCGlobal): Implemented, documentation added. + (isMCNodeLocal): Implemented, documentation added. + (isMCLinkLocal): Implemented, documentation added. + (isMCSiteLocal): Implemented, documentation added. + (isMCOrgLocal): Implemented, documentation added. + (getHostName): Documentation added. + (getCanonicalHostName): Implemented, documentation added. + (getAddress): Documentation added. + (hashCode): Documentation added. + (equals): Documentation added. + (toString): Fixed implementation. + (getByAddress): Use Inet4Address and Inet6Address. + (lookup): New linewrap. + (getByName): SecurityManager check added, support Inet4Address and + Inet6address, comments added. + (getAllByName): SecurityManager check added, comments added. + * java/net/Inet6Address.java: + (Inet6Address): Initialize parent class with addr instead of null. + * java/net/URL.java + (equals): Documentation added. + (getFile): Documentation added. + (hashCode): Documentation added. + * java/net/natInetAddress.cc: + (aton): Fix IPv6 support. + * java/net/natPlainDatagramSocketImpl.cc: + (peek): Throw PortUnreachableException when suitable. + (peekData): Throw PortUnreachableException when suitable. + (send): Throw PortUnreachableException when suitable. + (receive): Throw PortUnreachableException when suitable. + 2002-10-27 Mark Wielaard * java/util/zip/ZipFile.java (readLeShort): Take and use DataInput as diff --git a/libjava/java/net/Inet6Address.java b/libjava/java/net/Inet6Address.java index de496b148ee..8d3c0c3af44 100644 --- a/libjava/java/net/Inet6Address.java +++ b/libjava/java/net/Inet6Address.java @@ -67,7 +67,7 @@ public final class Inet6Address extends InetAddress */ protected Inet6Address (byte[] addr, String host) { - super (null, host); + super (addr, host); this.ipaddress = addr; } diff --git a/libjava/java/net/InetAddress.java b/libjava/java/net/InetAddress.java index c4ea6efd0d9..c15c0f6dc41 100644 --- a/libjava/java/net/InetAddress.java +++ b/libjava/java/net/InetAddress.java @@ -113,8 +113,9 @@ public class InetAddress implements Serializable */ public boolean isAnyLocalAddress () { - // FIXME: implement this - return false; + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + return addr == zeros; } /** @@ -124,89 +125,169 @@ public class InetAddress implements Serializable */ public boolean isLoopbackAddress () { - // FIXME: implement this - return addr [0] == 0x7F; + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + return addr[0] == 0x7F; } /** + * Utility routine to check if InetAddress is a link local address + * * @since 1.4 */ public boolean isLinkLocalAddress () { - // FIXME: implement this + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + // XXX: This seems to not exist with IPv4 addresses return false; } /** + * Utility routine to check if InetAddress is a site local address + * * @since 1.4 */ public boolean isSiteLocalAddress () { - // FIXME: implement this + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + // 10.0.0.0/8 + if (addr[0] == 0x0A) + return true; + + // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here: + // it says 172.16.0.0 - 172.255.255.255 are site local addresses + + // 172.16.0.0/12 + if (addr[0] == 0xAC && (addr[1] & 0xF0) == 0x01) + return true; + + // 192.168.0.0/16 + if (addr[0] == 0xC0 && addr[1] == 0xA8) + return true; + + // XXX: Do we need to check more addresses here ? return false; } /** + * Utility routine to check if InetAddress is a global multicast address + * * @since 1.4 */ public boolean isMCGlobal () { - // FIXME: implement this + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + // XXX: This seems to not exist with IPv4 addresses return false; } /** + * Utility reoutine to check if InetAddress is a node local multicast address + * * @since 1.4 */ public boolean isMCNodeLocal () { - // FIXME: implement this + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + // XXX: This seems to not exist with IPv4 addresses return false; } /** + * Utility reoutine to check if InetAddress is a link local multicast address + * * @since 1.4 */ public boolean isMCLinkLocal () { - // FIXME: implement this - return false; + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + if (!isMulticastAddress ()) + return false; + + return (addr[0] == 0xE0 + && addr[1] == 0x00 + && addr[2] == 0x00); } /** + * Utility reoutine to check if InetAddress is a site local multicast address + * * @since 1.4 */ public boolean isMCSiteLocal () { - // FIXME: implement this + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + // XXX: This seems to not exist with IPv4 addresses return false; } /** + * Utility reoutine to check if InetAddress is a organization local + * multicast address + * * @since 1.4 */ public boolean isMCOrgLocal () { - // FIXME: implement this + // This is the IPv4 implementation. + // Any class derived from InetAddress should override this. + + // XXX: This seems to not exist with IPv4 addresses return false; } + /** + * Returns the hostname represented by this InetAddress + */ public String getHostName () { if (hostName == null) lookup (null, this, false); + return hostName; } /** + * Returns the canonical hostname represented by this InetAddress + * * @since 1.4 */ public String getCanonicalHostName () { - // FIXME: implement this - return ""; + 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 InetAddress (getAddress (), null); + return address.getHostName (); } + /** + * Returns the IP address of this InetAddress as array of bytes + */ public byte[] getAddress () { // An experiment shows that JDK1.2 returns a different byte array each @@ -284,9 +365,13 @@ public class InetAddress implements Serializable break; sbuf.append('.'); } + return sbuf.toString(); } + /** + * Returns a hashcode of the InetAddress + */ public int hashCode() { // There hashing algorithm is not specified, but a simple experiment @@ -299,6 +384,9 @@ public class InetAddress implements Serializable return hash; } + /** + * Compares the InetAddress object with another one. + */ public boolean equals (Object obj) { if (obj == null || ! (obj instanceof InetAddress)) @@ -325,7 +413,12 @@ public class InetAddress implements Serializable */ public String toString() { - return getHostName()+'/'+getHostAddress(); + String hostname = getHostName (); + + if (hostname == "") + hostname = getHostAddress (); + + return hostname + '/' + getHostAddress (); } /** @@ -346,7 +439,10 @@ public class InetAddress implements Serializable if (addr.length != 4 && addr.length != 16) throw new UnknownHostException ("IP address has illegal length"); - return new InetAddress (addr, ""); + if (addr.length == 4) + return new Inet4Address (addr, null); + + return new Inet6Address (addr, null); } /** @@ -376,8 +472,8 @@ public class InetAddress implements Serializable * Otherwise, return null. */ private static native byte[] aton (String host); - private static native InetAddress[] lookup - (String hostname, InetAddress addr, boolean all); + private static native InetAddress[] lookup (String hostname, + InetAddress addr, boolean all); /** * Determines the IP address of a host, given the host's name. @@ -387,17 +483,43 @@ public class InetAddress implements Serializable * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ - public static InetAddress getByName (String host) + public static InetAddress getByName (String hostname) throws UnknownHostException { - if (host == null) + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.checkConnect (hostname, -1); + + // Default to current host if necessary + if (hostname == null) return getLocalHost(); - byte[] address = aton(host); + + // Assume that the host string is an IP address + byte[] address = aton (hostname); if (address != null) - return new InetAddress(address, null); - InetAddress iaddr = new InetAddress(null, host); - lookup(host, iaddr, false); - return iaddr; + { + 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]; } /** @@ -409,20 +531,28 @@ public class InetAddress implements Serializable * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ - public static InetAddress[] getAllByName (String host) + public static InetAddress[] getAllByName (String hostname) throws UnknownHostException { - byte[] address = aton(host); + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.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; } - return lookup(host, null, true); + + // 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); diff --git a/libjava/java/net/URL.java b/libjava/java/net/URL.java index b485c51231d..00dad2c7871 100644 --- a/libjava/java/net/URL.java +++ b/libjava/java/net/URL.java @@ -254,6 +254,9 @@ public final class URL implements Serializable hashCode = hashCode(); // Used for serialization. } + /** + * Checks if two URLs are equal + */ public boolean equals(Object obj) { if (obj == null || ! (obj instanceof URL)) @@ -287,6 +290,9 @@ public final class URL implements Serializable return getContent(); } + /** + * Returns the file of the URL + */ public String getFile() { return file; @@ -367,6 +373,9 @@ public final class URL implements Serializable return query; } + /** + * Returns a hashcode computed by the URLStreamHandler of this URL + */ public int hashCode() { // JCL book says this is computed using (only) the hashcodes of the diff --git a/libjava/java/net/natInetAddress.cc b/libjava/java/net/natInetAddress.cc index 15e5db732a2..c0083714287 100644 --- a/libjava/java/net/natInetAddress.cc +++ b/libjava/java/net/natInetAddress.cc @@ -121,7 +121,7 @@ java::net::InetAddress::aton (jstring host) #endif #if defined (HAVE_INET_PTON) && defined (HAVE_INET6) char inet6_addr[16]; - if (len == 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0) + if (len != 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0) { bytes = inet6_addr; blen = 16; diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc index 84de798d944..60b25d22bfa 100644 --- a/libjava/java/net/natPlainDatagramSocketImpl.cc +++ b/libjava/java/net/natPlainDatagramSocketImpl.cc @@ -65,6 +65,7 @@ _Jv_bind (int fd, struct sockaddr *addr, int addrlen) #include #include #include +#include #include #include #include @@ -328,6 +329,8 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i) return rport; error: char* strerr = strerror (errno); + if (errno == ECONNREFUSED) + throw new PortUnreachableException (JvNewStringUTF (strerr)); throw new java::io::IOException (JvNewStringUTF (strerr)); } @@ -390,6 +393,8 @@ java::net::PlainDatagramSocketImpl::peekData(java::net::DatagramPacket *p) return rport; error: char* strerr = strerror (errno); + if (errno == ECONNREFUSED) + throw new PortUnreachableException (JvNewStringUTF (strerr)); throw new java::io::IOException (JvNewStringUTF (strerr)); } @@ -441,6 +446,8 @@ java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p) return; char* strerr = strerror (errno); + if (errno == ECONNREFUSED) + throw new PortUnreachableException (JvNewStringUTF (strerr)); throw new java::io::IOException (JvNewStringUTF (strerr)); } @@ -503,6 +510,8 @@ java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *p) return; error: char* strerr = strerror (errno); + if (errno == ECONNREFUSED) + throw new PortUnreachableException (JvNewStringUTF (strerr)); throw new java::io::IOException (JvNewStringUTF (strerr)); } -- 2.30.2