Patch: Serialization for java.net.InetAddress

Warren Levy warrenl@cygnus.com
Thu Nov 2 23:42:00 GMT 2000


Folks,
Here's the patch another special case class for serialization.  I had to
change the name of one of the internal fields to avoid a conflict with the
same named field listed in the Serialized Form doc for this class.  I also
added code to recognize and save off the family in use for the address
(i.e. IPv4 or IPv6).

Otherwise, it's pretty straight forward.  Enjoy!
--warrenl


2000-11-02  Warren Levy  <warrenl@cygnus.com>

	* 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'.


Index: java/net/InetAddress.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/net/InetAddress.java,v
retrieving revision 1.5
diff -u -p -r1.5 InetAddress.java
--- InetAddress.java	2000/06/27 05:10:02	1.5
+++ InetAddress.java	2000/11/03 07:28:30
@@ -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
 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 implement
   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 implement
 	  {
 	    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 implement
       }
     for ( ;  ; )
       {
-	sbuf.append(address[i] & 0xFF);
+	sbuf.append(addr[i] & 0xFF);
 	i++;
 	if (i == len)
 	  break;
@@ -130,10 +175,10 @@ public final class InetAddress implement
     // 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 implement
     // 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 implement
     // 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;
   }
Index: java/net/natInetAddress.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/net/natInetAddress.cc,v
retrieving revision 1.15
diff -u -p -r1.15 natInetAddress.cc
--- natInetAddress.cc	2000/09/05 21:57:23	1.15
+++ natInetAddress.cc	2000/11/03 07:28:30
@@ -66,6 +66,12 @@ java::net::InetAddress::aton (jstring)
   return NULL;
 }
 
+jint
+java::net::InetAddress::getFamily (jbyteArray bytes)
+{
+  return 0;
+}
+
 JArray<java::net::InetAddress*> *
 java::net::InetAddress::lookup (jstring, java::net::InetAddress *, jboolean)
 {
@@ -127,6 +133,20 @@ java::net::InetAddress::aton (jstring ho
   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*> *
 java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
@@ -196,7 +216,7 @@ java::net::InetAddress::lookup (jstring 
     }
   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 
       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 
       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 
     {
       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;
Index: java/net/natPlainDatagramSocketImpl.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/net/natPlainDatagramSocketImpl.cc,v
retrieving revision 1.25
diff -u -p -r1.25 natPlainDatagramSocketImpl.cc
--- natPlainDatagramSocketImpl.cc	2000/10/06 01:49:32	1.25
+++ natPlainDatagramSocketImpl.cc	2000/11/03 07:28:30
@@ -184,7 +184,7 @@ java::net::PlainDatagramSocketImpl::bind
   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
   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
   // 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::mcas
 					      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::setO
 	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)
Index: java/net/natPlainSocketImpl.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/net/natPlainSocketImpl.cc,v
retrieving revision 1.22
diff -u -p -r1.22 natPlainSocketImpl.cc
--- natPlainSocketImpl.cc	2000/10/06 01:49:32	1.22
+++ natPlainSocketImpl.cc	2000/11/03 07:28:30
@@ -134,7 +134,7 @@ java::net::PlainSocketImpl::bind (java::
 {
   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 (jav
 {
   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;





More information about the Java-patches mailing list