diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 24e84fa5e75..025d08e98d0 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,20 @@ +2000-11-02 Warren Levy + + * java/net/InetAddress.java (addr): Renamed from 'address'. + (address): New field to match Serialized Form doc. + (hostName): Renamed from 'hostname' to match Serialized Form doc. + (family): New serialization field. + (serialVersionUID): New field. + (readObject): New method. + (writeObject): New method. + (getFamily): New native method. + (InetAddress): Set family. + * java/net/natInetAddress.cc (getFamily): New method. + (addr): Renamed from 'address'. + (hostName): Renamed from 'hostname' to match Serialized Form doc. + * java/net/natPlainDatagramSocketImpl.cc (addr): Renamed from 'address'. + * java/net/natPlainSocketImpl.cc (addr): Renamed from 'address'. + 2000-11-03 Bryce McKinlay * java/util/AbstractList.java (SubList): Make it a top-level private diff --git a/libjava/java/net/InetAddress.java b/libjava/java/net/InetAddress.java index 303a45b3cf2..f1a12354be8 100644 --- a/libjava/java/net/InetAddress.java +++ b/libjava/java/net/InetAddress.java @@ -1,6 +1,6 @@ // INetAddress.java -- An Internet Protocol (IP) address. -/* Copyright (C) 1998, 1999 Free Software Foundation +/* Copyright (C) 1998, 1999, 2000 Free Software Foundation This file is part of libgcj. @@ -9,6 +9,9 @@ Libgcj License. Please consult the file "LIBGCJ_LICENSE" for details. */ package java.net; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.IOException; /** * @author Per Bothner @@ -24,37 +27,79 @@ package java.net; public final class InetAddress implements java.io.Serializable { - byte[] address; - String hostname; + // The Serialized Form specifies that an int 'address' is saved/restored. + // This class uses a byte array internally so we'll just do the conversion + // at serialization time and leave the rest of the algorithm as is. + private int address; + transient byte[] addr; + String hostName; + // The field 'family' seems to be the AF_ value. + // FIXME: Much of the code in the other java.net classes does not make + // use of this family field. A better implementation would be to make + // use of getaddrinfo() and have other methods just check the family + // field rather than examining the length of the address each time. + int family; + private static final long serialVersionUID = 3286316764910316507L; + + private void readObject(ObjectInputStream ois) + throws IOException, ClassNotFoundException + { + ois.defaultReadObject(); + addr = new byte[4]; + addr[3] = (byte) address; + for (int i = 2; i >= 0; --i) + addr[i] = (byte) (address >>= 8); + // Ignore family from serialized data. Since the saved address is 32 bits + // the deserialized object will have an IPv4 address i.e. AF_INET family. + // FIXME: An alternative is to call the aton method on the deserialized + // hostname to get a new address. The Serialized Form doc is silent + // on how these fields are used. + family = getFamily (addr); + } + + private void writeObject(ObjectOutputStream oos) throws IOException + { + // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address + // or a 16 byte IPv6 address. + int len = addr.length; + int i = len - 4; + for (; i < len; i++) + address = address << 8 | (((int) addr[i]) & 0xFF); + oos.defaultWriteObject(); + } + + private static native int getFamily (byte[] address); InetAddress (byte[] address, String hostname) { - this.address = address; - this.hostname = hostname; + addr = address; + hostName = hostname; + if (address != null) + family = getFamily (address); } public boolean isMulticastAddress () { - int len = address.length; + int len = addr.length; if (len == 4) - return (address[0] & 0xF0) == 0xE0; + return (addr[0] & 0xF0) == 0xE0; if (len == 16) - return address[0] == (byte) 0xFF; + return addr[0] == (byte) 0xFF; return false; } public String getHostName () { - if (hostname == null) + if (hostName == null) lookup (null, this, false); - return hostname; + return hostName; } public byte[] getAddress () { // An experiment shows that JDK1.2 returns a different byte array each // time. This makes sense, in terms of security. - return (byte[]) address.clone(); + return (byte[]) addr.clone(); } /* Helper function due to a CNI limitation. */ @@ -83,7 +128,7 @@ public final class InetAddress implements java.io.Serializable public String getHostAddress () { StringBuffer sbuf = new StringBuffer(40); - int len = address.length; + int len = addr.length; int i = 0; if (len == 16) { // An IPv6 address. @@ -91,7 +136,7 @@ public final class InetAddress implements java.io.Serializable { if (i >= 16) return sbuf.toString(); - int x = ((address[i] & 0xFF) << 8) | (address[i+1] & 0xFF); + int x = ((addr[i] & 0xFF) << 8) | (addr[i+1] & 0xFF); boolean empty = sbuf.length() == 0; if (empty) { @@ -116,7 +161,7 @@ public final class InetAddress implements java.io.Serializable } for ( ; ; ) { - sbuf.append(address[i] & 0xFF); + sbuf.append(addr[i] & 0xFF); i++; if (i == len) break; @@ -130,10 +175,10 @@ public final class InetAddress implements java.io.Serializable // There hashing algorithm is not specified, but a simple experiment // shows that it is equal to the address, as a 32-bit big-endian integer. int hash = 0; - int len = address.length; + int len = addr.length; int i = len > 4 ? len - 4 : 0; for ( ; i < len; i++) - hash = (hash << 8) | (address[i] & 0xFF); + hash = (hash << 8) | (addr[i] & 0xFF); return hash; } @@ -147,8 +192,8 @@ public final class InetAddress implements java.io.Serializable // different host names." This violates the description in the // JDK 1.2 API documentation. A little experiementation // shows that the latter is correct. - byte[] addr1 = address; - byte[] addr2 = ((InetAddress) obj).address; + byte[] addr1 = addr; + byte[] addr2 = ((InetAddress) obj).addr; if (addr1.length != addr2.length) return false; for (int i = addr1.length; --i >= 0; ) @@ -208,7 +253,7 @@ public final class InetAddress implements java.io.Serializable // 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.address != localhostAddress)) + || (s != null && localhost.addr != localhostAddress)) getLocalHost(s); return localhost; } diff --git a/libjava/java/net/natInetAddress.cc b/libjava/java/net/natInetAddress.cc index d8db576f506..68a0b411828 100644 --- a/libjava/java/net/natInetAddress.cc +++ b/libjava/java/net/natInetAddress.cc @@ -66,6 +66,12 @@ java::net::InetAddress::aton (jstring) return NULL; } +jint +java::net::InetAddress::getFamily (jbyteArray bytes) +{ + return 0; +} + JArray * java::net::InetAddress::lookup (jstring, java::net::InetAddress *, jboolean) { @@ -127,6 +133,20 @@ java::net::InetAddress::aton (jstring host) return result; } +jint +java::net::InetAddress::getFamily (jbyteArray bytes) +{ + int len = bytes->length; + if (len == 4) + return AF_INET; +#ifdef HAVE_INET6 + else if (len == 16) + return AF_INET6; +#endif /* HAVE_INET6 */ + else + JvFail ("unrecognized size"); +} + JArray * java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, @@ -196,7 +216,7 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, } else { - jbyteArray bytes = iaddr->address; + jbyteArray bytes = iaddr->addr; char *chars = (char*) elements (bytes); int len = bytes->length; int type; @@ -204,13 +224,13 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, if (len == 4) { val = chars; - type = AF_INET; + type = iaddr->family = AF_INET; } #ifdef HAVE_INET6 else if (len == 16) { val = (char *) &chars; - type = AF_INET6; + type = iaddr->family = AF_INET6; } #endif /* HAVE_INET6 */ else @@ -255,16 +275,16 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, java::lang::SecurityException *ex = checkConnect (host); if (ex != NULL) { - if (iaddr == NULL || iaddr->address == NULL) + if (iaddr == NULL || iaddr->addr == NULL) throw ex; hptr = NULL; } } if (hptr == NULL) { - if (iaddr != NULL && iaddr->address != NULL) + if (iaddr != NULL && iaddr->addr != NULL) { - iaddr->hostname = iaddr->getHostAddress(); + iaddr->hostName = iaddr->getHostAddress(); return NULL; } else @@ -296,13 +316,14 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, { if (iaddrs[i] == NULL) iaddrs[i] = new java::net::InetAddress (NULL, NULL); - if (iaddrs[i]->hostname == NULL) - iaddrs[i]->hostname = host; - if (iaddrs[i]->address == NULL) + if (iaddrs[i]->hostName == NULL) + iaddrs[i]->hostName = host; + if (iaddrs[i]->addr == NULL) { char *bytes = hptr->h_addr_list[i]; - iaddrs[i]->address = JvNewByteArray (hptr->h_length); - memcpy (elements (iaddrs[i]->address), bytes, hptr->h_length); + iaddrs[i]->addr = JvNewByteArray (hptr->h_length); + iaddrs[i]->family = getFamily (iaddrs[i]->addr); + memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length); } } return result; diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc index 6934f090b82..a90d2a8e8e0 100644 --- a/libjava/java/net/natPlainDatagramSocketImpl.cc +++ b/libjava/java/net/natPlainDatagramSocketImpl.cc @@ -184,7 +184,7 @@ java::net::PlainDatagramSocketImpl::bind (jint lport, union SockAddr u; struct sockaddr *ptr = (struct sockaddr *) &u.address; // FIXME: Use getaddrinfo() to get actual protocol instead of assuming ipv4. - jbyteArray haddress = host->address; + jbyteArray haddress = host->addr; jbyte *bytes = elements (haddress); int len = haddress->length; @@ -257,7 +257,7 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i) else throw new java::net::SocketException (JvNewStringUTF ("invalid family")); - i->address = raddr; + i->addr = raddr; return rport; error: char* strerr = strerror (errno); @@ -270,7 +270,7 @@ java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p) // FIXME: Deal with Multicast and if the socket is connected. jint rport = p->getPort(); union SockAddr u; - jbyteArray haddress = p->getAddress()->address; + jbyteArray haddress = p->getAddress()->addr; jbyte *bytes = elements (haddress); int len = haddress->length; struct sockaddr *ptr = (struct sockaddr *) &u.address; @@ -391,7 +391,7 @@ java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *inetaddr, jboolean join) { union McastReq u; - jbyteArray haddress = inetaddr->address; + jbyteArray haddress = inetaddr->addr; jbyte *bytes = elements (haddress); int len = haddress->length; int level, opname; @@ -499,7 +499,7 @@ java::net::PlainDatagramSocketImpl::setOption (jint optID, int level, opname; const char *ptr; - haddress = ((java::net::InetAddress *) value)->address; + haddress = ((java::net::InetAddress *) value)->addr; bytes = elements (haddress); len = haddress->length; if (len == 4) diff --git a/libjava/java/net/natPlainSocketImpl.cc b/libjava/java/net/natPlainSocketImpl.cc index 3c6bc393429..751b79821e2 100644 --- a/libjava/java/net/natPlainSocketImpl.cc +++ b/libjava/java/net/natPlainSocketImpl.cc @@ -134,7 +134,7 @@ java::net::PlainSocketImpl::bind (java::net::InetAddress *host, jint lport) { union SockAddr u; struct sockaddr *ptr = (struct sockaddr *) &u.address; - jbyteArray haddress = host->address; + jbyteArray haddress = host->addr; jbyte *bytes = elements (haddress); int len = haddress->length; int i = 1; @@ -186,7 +186,7 @@ java::net::PlainSocketImpl::connect (java::net::InetAddress *host, jint rport) { union SockAddr u; socklen_t addrlen = sizeof(u); - jbyteArray haddress = host->address; + jbyteArray haddress = host->addr; jbyte *bytes = elements (haddress); int len = haddress->length; struct sockaddr *ptr = (struct sockaddr *) &u.address;