From 97b8365cafc3a344a22d3980b8ed885f5c6d8357 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey@gcc.gnu.org>
Date: Tue, 9 Jan 2007 19:58:05 +0000
Subject: Merged gcj-eclipse branch to trunk.

From-SVN: r120621
---
 libjava/classpath/native/jni/java-net/javanet.c | 551 +++++++++++-------------
 1 file changed, 255 insertions(+), 296 deletions(-)

(limited to 'libjava/classpath/native/jni/java-net/javanet.c')

diff --git a/libjava/classpath/native/jni/java-net/javanet.c b/libjava/classpath/native/jni/java-net/javanet.c
index e500c60..1d2f7202 100644
--- a/libjava/classpath/native/jni/java-net/javanet.c
+++ b/libjava/classpath/native/jni/java-net/javanet.c
@@ -45,12 +45,10 @@ exception statement from your version. */
 #include <jni.h>
 #include <jcl.h>
 
-#include "javanet.h"
+#include "cpnative.h"
+#include "cpnet.h"
 
-#include "target_native.h"
-#ifndef WITHOUT_NETWORK
-#include "target_native_network.h"
-#endif /* WITHOUT_NETWORK */
+#include "javanet.h"
 
 #ifndef WITHOUT_NETWORK
 /* Need to have some value for SO_TIMEOUT */
@@ -234,23 +232,20 @@ _javanet_create_integer (JNIEnv * env, jint val)
 /*
  * Builds an InetAddress object from a 32 bit address in host byte order
  */
-static jobject
-_javanet_create_inetaddress (JNIEnv * env, int netaddr)
+jobject
+_javanet_create_inetaddress (JNIEnv * env, cpnet_address *netaddr)
 {
 #ifndef WITHOUT_NETWORK
-  unsigned char octets[4];
-  char buf[16];
+  jbyte octets[4];
+  char buf[64];
   jclass ia_cls;
   jmethodID mid;
   jstring ip_str;
   jobject ia;
 
   /* Build a string IP address */
-  TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES (netaddr,
-						octets[0],
-						octets[1],
-						octets[2], octets[3]);
-  sprintf (buf, "%d.%d.%d.%d", octets[0], octets[1], octets[2], octets[3]);
+  cpnet_IPV4AddressToBytes(netaddr, octets);
+  sprintf (buf, "%d.%d.%d.%d", (int) (unsigned char)octets[0], (int)(unsigned char)octets[1], (int)(unsigned char)octets[2], (int)(unsigned char)octets[3]);
   DBG ("_javanet_create_inetaddress(): Created ip addr string\n");
 
   /* Get an InetAddress object for this IP */
@@ -320,7 +315,7 @@ _javanet_set_remhost_addr (JNIEnv * env, jobject this, jobject ia)
  * InetAddress for the specified addr
  */
 static void
-_javanet_set_remhost (JNIEnv * env, jobject this, int netaddr)
+_javanet_set_remhost (JNIEnv * env, jobject this, cpnet_address *netaddr)
 {
   jobject ia;
 
@@ -338,19 +333,20 @@ _javanet_set_remhost (JNIEnv * env, jobject this, int netaddr)
 /*************************************************************************/
 
 /*
- * Returns a 32 bit Internet address for the passed in InetAddress object
+ * Returns an Internet address for the passed in InetAddress object
  */
-int
-_javanet_get_netaddr (JNIEnv * env, jobject addr)
+cpnet_address *
+_javanet_get_ip_netaddr (JNIEnv * env, jobject addr)
 {
 #ifndef WITHOUT_NETWORK
   jclass cls = 0;
   jmethodID mid;
   jarray arr = 0;
   jbyte *octets;
-  int netaddr, len;
+  cpnet_address *netaddr;
+  jint len;
 
-  DBG ("_javanet_get_netaddr(): Entered _javanet_get_netaddr\n");
+  DBG ("_javanet_get_ip_netaddr(): Entered _javanet_get_ip_netaddr\n");
 
   if (addr == NULL)
     {
@@ -368,36 +364,51 @@ _javanet_get_netaddr (JNIEnv * env, jobject addr)
   if (mid == NULL)
     return 0;
 
-  DBG ("_javanet_get_netaddr(): Got getAddress method\n");
+  DBG ("_javanet_get_ip_netaddr(): Got getAddress method\n");
 
   arr = (*env)->CallObjectMethod (env, addr, mid);
   if (arr == NULL)
     return 0;
 
-  DBG ("_javanet_get_netaddr(): Got the address\n");
+  DBG ("_javanet_get_ip_netaddr(): Got the address\n");
 
-  /* Turn the IP address into a 32 bit Internet address in network byte order */
+  /* Turn the IP address into a system cpnet address.
+   * If the length is 4 then it is an IPV4 address, if it
+   * is 16 then it is an IPV6 address else it is an InternError. */
   len = (*env)->GetArrayLength (env, arr);
-  if (len != 4)
+  if (len != 4 && len != 16)
     {
       JCL_ThrowException (env, IO_EXCEPTION, "Internal Error");
       return 0;
     }
-  DBG ("_javanet_get_netaddr(): Length ok\n");
+  DBG ("_javanet_get_ip_netaddr(): Length ok\n");
 
   octets = (*env)->GetByteArrayElements (env, arr, 0);
   if (octets == NULL)
     return 0;
 
-  DBG ("_javanet_get_netaddr(): Grabbed bytes\n");
+  DBG ("_javanet_get_ip_netaddr(): Grabbed bytes\n");
 
-  TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets[0],
-						octets[1],
-						octets[2],
-						octets[3], netaddr);
+  switch (len)
+    {
+    case 4:      
+      netaddr = cpnet_newIPV4Address(env);
+      cpnet_bytesToIPV4Address(netaddr, octets);
+      break;
+    case 16:
+      netaddr = cpnet_newIPV6Address(env);
+      cpnet_bytesToIPV6Address(netaddr, octets);
+      break;
+    default:
+      /* This should not happen as we have checked before.
+       * But that way we shut the compiler warnings */
+      JCL_ThrowException (env, IO_EXCEPTION, "Internal Error");
+      return 0;
+      
+    }
 
   (*env)->ReleaseByteArrayElements (env, arr, octets, 0);
-  DBG ("_javanet_get_netaddr(): Done getting addr\n");
+  DBG ("_javanet_get_ip_netaddr(): Done getting addr\n");
 
   return netaddr;
 #else /* not WITHOUT_NETWORK */
@@ -419,29 +430,29 @@ _javanet_create (JNIEnv * env, jobject this, jboolean stream)
   if (stream)
     {
       /* create a stream socket */
-      TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM (fd, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_openSocketStream(env, &fd, AF_INET);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, IO_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return;
 	}
     }
   else
     {
       /* create a datagram socket, set broadcast option */
-      TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM (fd, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_openSocketDatagram (env, &fd, AF_INET);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, IO_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return;
 	}
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_BROADCAST (fd, 1, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_setBroadcast(env, fd, 1);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, IO_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return;
 	}
     }
@@ -458,13 +469,11 @@ _javanet_create (JNIEnv * env, jobject this, jboolean stream)
       /* Try to make sure we close the socket since close() won't work. */
       do
 	{
-	  TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
-	  if (result != TARGET_NATIVE_OK
-	      && (TARGET_NATIVE_LAST_ERROR ()
-		  != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+	  result = cpnet_close(env, fd);
+	  if (result != CPNATIVE_OK && result != CPNATIVE_EINTR)
 	    return;
 	}
-      while (result != TARGET_NATIVE_OK);
+      while (result != CPNATIVE_OK);
       return;
     }
 
@@ -498,18 +507,16 @@ _javanet_close (JNIEnv * env, jobject this, int stream)
 			    "native_fd", -1);
   do
     {
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_close (env, fd);
+      if (result != CPNATIVE_OK)
 	{
 	  /* Only throw an error when a "real" error occurs. */
-	  error = TARGET_NATIVE_LAST_ERROR ();
-	  if (error != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL
-	      && error != ENOTCONN && error != ECONNRESET && error != EBADF)
+	  if (result != CPNATIVE_EINTR && result != ENOTCONN && result != ECONNRESET && result != EBADF)
 	    JCL_ThrowException (env, IO_EXCEPTION,
-				TARGET_NATIVE_LAST_ERROR_STRING ());
+				cpnative_getErrorString (result));
 	}
     }
-  while (error == TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL);
+  while (error == CPNATIVE_EINTR);
 
 #else /* not WITHOUT_NETWORK */
 #endif /* not WITHOUT_NETWORK */
@@ -525,20 +532,24 @@ _javanet_connect (JNIEnv * env, jobject this, jobject addr, jint port,
 		  jboolean stream)
 {
 #ifndef WITHOUT_NETWORK
-  int netaddr, fd;
+  cpnet_address *netaddr;
+  int fd;
   int result;
-  int local_address, local_port;
-  int remote_address, remote_port;
+  cpnet_address *local_addr;
+  cpnet_address *remote_addr;
 
   DBG ("_javanet_connect(): Entered _javanet_connect\n");
 
   /* Pre-process input variables */
-  netaddr = _javanet_get_netaddr (env, addr);
+  netaddr = _javanet_get_ip_netaddr (env, addr);
   if ((*env)->ExceptionOccurred (env))
     return;
 
   if (port == -1)
     port = 0;
+
+  cpnet_addressSetPort(netaddr, port);
+
   DBG ("_javanet_connect(): Got network address\n");
 
   /* Grab the real socket file descriptor */
@@ -554,29 +565,27 @@ _javanet_connect (JNIEnv * env, jobject this, jobject addr, jint port,
   /* Connect up */
   do
     {
-      TARGET_NATIVE_NETWORK_SOCKET_CONNECT (fd, netaddr, port, result);
-      if (result != TARGET_NATIVE_OK
-	  && (TARGET_NATIVE_LAST_ERROR ()
-	      != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = cpnet_connect (env, fd, netaddr);
+      if (result != CPNATIVE_OK && result != CPNATIVE_EINTR)
 	{
 	  JCL_ThrowException (env, CONNECT_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return;
 	}
     }
-  while (result != TARGET_NATIVE_OK);
-
+  while (result != CPNATIVE_OK);
+  
   DBG ("_javanet_connect(): Connected successfully\n");
 
   /* Populate instance variables */
-  TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO (fd, local_address, local_port,
-					       result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpnet_getLocalAddr (env, fd, &local_addr);
+  if (result != CPNATIVE_OK)
     {
+      cpnet_freeAddress(env, netaddr);
       JCL_ThrowException (env, IO_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
+      cpnet_close (env, fd);
       return;
     }
 
@@ -584,63 +593,72 @@ _javanet_connect (JNIEnv * env, jobject this, jobject addr, jint port,
   if ((*env)->ExceptionOccurred (env))
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
+      cpnet_freeAddress(env, netaddr);
+      cpnet_freeAddress(env, local_addr);
+      cpnet_close (env, fd);
       return;
     }
   DBG ("_javanet_connect(): Created fd\n");
 
   if (stream)
     _javanet_set_int_field (env, this, "java/net/SocketImpl", "localport",
-			    local_port);
+			    cpnet_addressGetPort(local_addr));
   else
     _javanet_set_int_field (env, this, "java/net/DatagramSocketImpl",
-			    "localPort", local_port);
+			    "localPort", cpnet_addressGetPort(local_addr));
 
+  cpnet_freeAddress (env, local_addr);
   if ((*env)->ExceptionOccurred (env))
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
+      cpnet_freeAddress(env, netaddr);
+      cpnet_close (env, fd);
       return;
     }
   DBG ("_javanet_connect(): Set the local port\n");
 
-  TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO (fd, remote_address,
-						remote_port, result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpnet_getRemoteAddr (env, fd, &remote_addr);
+  if (result != CPNATIVE_OK)
     {
+      cpnet_freeAddress(env, netaddr);
       JCL_ThrowException (env, IO_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
+      cpnet_close (env, fd);
       return;
     }
 
   if (stream)
     {
-      if (remote_address == netaddr)
+      if (cpnet_isAddressEqual(remote_addr, netaddr))
 	{
 	  _javanet_set_remhost_addr (env, this, addr);
 	}
       else
 	{
-	  _javanet_set_remhost (env, this, remote_address);
+	  _javanet_set_remhost (env, this, remote_addr);
 	}
+      cpnet_freeAddress(env, netaddr);
+
       if ((*env)->ExceptionOccurred (env))
 	{
 	  /* We don't care whether this succeeds. close() will cleanup later.
 	   */
-	  TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
+	  cpnet_freeAddress (env, remote_addr);
+	  cpnet_close (env, fd);
 	  return;
 	}
       DBG ("_javanet_connect(): Set the remote host\n");
 
       _javanet_set_int_field (env, this, "java/net/SocketImpl", "port",
-			      remote_port);
+			      cpnet_addressGetPort(remote_addr));
+      cpnet_freeAddress (env, remote_addr);
+
       if ((*env)->ExceptionOccurred (env))
 	{
 	  /* We don't care whether this succeeds. close() will cleanup later.
 	   */
-	  TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);
+	  cpnet_close (env, fd);
 	  return;
 	}
       DBG ("_javanet_connect(): Set the remote port\n");
@@ -661,98 +679,59 @@ _javanet_bind (JNIEnv * env, jobject this, jobject addr, jint port,
 	       int stream)
 {
 #ifndef WITHOUT_NETWORK
-  jclass cls;
-  jmethodID mid;
-  jbyteArray arr = 0;
-  jbyte *octets;
   jint fd;
-  int tmpaddr;
+  cpnet_address *tmpaddr;
+  cpnet_address *local_addr;
   int result;
-  int local_address, local_port;
 
   DBG ("_javanet_bind(): Entering native bind()\n");
-
-  /* Get the address to connect to */
-  cls = (*env)->GetObjectClass (env, addr);
-  if (cls == NULL)
-    return;
-
-  mid = (*env)->GetMethodID (env, cls, "getAddress", "()[B");
-  if (mid == NULL)
-    return;
-
-  DBG ("_javanet_bind(): Past getAddress method id\n");
-
-  arr = (*env)->CallObjectMethod (env, addr, mid);
-  if ((arr == NULL) || (*env)->ExceptionOccurred (env))
-    {
-      JCL_ThrowException (env, IO_EXCEPTION,
-			  "Internal error: _javanet_bind()");
-      return;
-    }
-
-  DBG ("_javanet_bind(): Past call object method\n");
-
-  octets = (*env)->GetByteArrayElements (env, arr, 0);
-  if (octets == NULL)
-    return;
-
-  DBG ("_javanet_bind(): Past grab array\n");
-
-  /* Get the native socket file descriptor */
+ 
+ /* Grab the real socket file descriptor */
   fd = _javanet_get_int_field (env, this, "native_fd");
   if (fd == -1)
     {
-      (*env)->ReleaseByteArrayElements (env, arr, octets, 0);
       JCL_ThrowException (env, IO_EXCEPTION,
-			  "Internal error: _javanet_bind(): no native file descriptor");
+			  "Internal error: _javanet_connect(): no native file descriptor");
       return;
     }
-  DBG ("_javanet_bind(): Past native_fd lookup\n");
 
-  /* XXX NYI ??? */
-  _javanet_set_option (env, this, SOCKOPT_SO_REUSEADDR,
-		       _javanet_create_boolean (env, JNI_TRUE));
+  cpnet_setReuseAddress (env, fd, 1);
 
+  /* Get the address to connect to */
+  tmpaddr = _javanet_get_ip_netaddr (env, addr);
+  if ((*env)->ExceptionOccurred (env))
+    return;
 
-  /* Bind the socket */
-  TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets[0],
-						octets[1],
-						octets[2],
-						octets[3], tmpaddr);
-  TARGET_NATIVE_NETWORK_SOCKET_BIND (fd, tmpaddr, port, result);
-
-  if (result != TARGET_NATIVE_OK)
+  cpnet_addressSetPort (tmpaddr, port);
+  result = cpnet_bind(env, fd, tmpaddr);
+  cpnet_freeAddress (env, tmpaddr);
+  if (result != CPNATIVE_OK)
     {
-      char *errorstr = TARGET_NATIVE_LAST_ERROR_STRING ();
-      (*env)->ReleaseByteArrayElements (env, arr, octets, 0);
-
       JCL_ThrowException (env, BIND_EXCEPTION,
-			  errorstr);
+			  cpnative_getErrorString (result));
       return;
     }
   DBG ("_javanet_bind(): Past bind\n");
 
-  (*env)->ReleaseByteArrayElements (env, arr, octets, 0);
-
   /* Update instance variables, specifically the local port number */
-  TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO (fd, local_address, local_port,
-					       result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpnet_getLocalAddr (env, fd, &local_addr);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       return;
     }
 
   if (stream)
     _javanet_set_int_field (env, this, "java/net/SocketImpl",
-			    "localport", local_port);
+			    "localport", cpnet_addressGetPort (local_addr));
   else
     _javanet_set_int_field (env, this, "java/net/DatagramSocketImpl",
-			    "localPort", local_port);
+			    "localPort", cpnet_addressGetPort (local_addr));
   DBG ("_javanet_bind(): Past update port number\n");
 
+  cpnet_freeAddress (env, local_addr);
+
   return;
 #else /* not WITHOUT_NETWORK */
 #endif /* not WITHOUT_NETWORK */
@@ -781,11 +760,11 @@ _javanet_listen (JNIEnv * env, jobject this, jint queuelen)
     }
 
   /* Start listening */
-  TARGET_NATIVE_NETWORK_SOCKET_LISTEN (fd, queuelen, result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpnet_listen (env, fd, queuelen);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       return;
     }
 #else /* not WITHOUT_NETWORK */
@@ -804,8 +783,7 @@ _javanet_accept (JNIEnv * env, jobject this, jobject impl)
 #ifndef WITHOUT_NETWORK
   int fd, newfd;
   int result;
-  int local_address, local_port;
-  int remote_address, remote_port;
+  cpnet_address *remote_addr, *local_addr;
 
   /* Get the real file descriptor */
   fd = _javanet_get_int_field (env, this, "native_fd");
@@ -819,24 +797,22 @@ _javanet_accept (JNIEnv * env, jobject this, jobject impl)
   /* Accept the connection */
   do
     {
-      TARGET_NATIVE_NETWORK_SOCKET_ACCEPT (fd, newfd, result);
-      if (result != TARGET_NATIVE_OK
-	  && (TARGET_NATIVE_LAST_ERROR ()
-	      != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = cpnet_accept (env, fd, &newfd);
+      if (result != CPNATIVE_OK && result != CPNATIVE_EINTR)
 	{
-	  if (TARGET_NATIVE_LAST_ERROR () == EAGAIN)
+	  if (result == ETIMEDOUT || result == EAGAIN)
 	    JCL_ThrowException (env, "java/net/SocketTimeoutException",
-				"Timeout");
+				"Accept operation timed out");
 	  else
 	    JCL_ThrowException (env, IO_EXCEPTION,
-				TARGET_NATIVE_LAST_ERROR_STRING ());
+				cpnative_getErrorString (result));
 	  return;
 	}
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != CPNATIVE_OK);
 
   /* Reset the inherited timeout. */
-  TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_TIMEOUT (newfd, 0, result);
+  cpnet_setSocketTimeout (env, newfd, 0);
 
   /* Populate instance variables */
   _javanet_set_int_field (env, impl, "gnu/java/net/PlainSocketImpl",
@@ -847,24 +823,21 @@ _javanet_accept (JNIEnv * env, jobject this, jobject impl)
       /* Try to make sure we close the socket since close() won't work. */
       do
 	{
-	  TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
-	  if (result != TARGET_NATIVE_OK
-	      && (TARGET_NATIVE_LAST_ERROR ()
-		  != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+	  result = cpnet_close (env, newfd);
+	  if (result != CPNATIVE_OK && result != CPNATIVE_EINTR)
 	    return;
 	}
-      while (result != TARGET_NATIVE_OK);
+      while (result != CPNATIVE_OK);
       return;
     }
 
-  TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO (newfd, local_address,
-					       local_port, result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpnet_getLocalAddr (env, newfd, &local_addr);
+  if (result != CPNATIVE_OK)
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
+      cpnet_close (env, newfd);
       JCL_ThrowException (env, IO_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       return;
     }
 
@@ -872,44 +845,47 @@ _javanet_accept (JNIEnv * env, jobject this, jobject impl)
   if ((*env)->ExceptionOccurred (env))
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
+      cpnet_freeAddress (env, local_addr);
+      cpnet_close (env, newfd);
       return;
     }
 
   _javanet_set_int_field (env, impl, "java/net/SocketImpl", "localport",
-			  local_port);
+			  cpnet_addressGetPort (local_addr));
+  cpnet_freeAddress (env, local_addr);
   if ((*env)->ExceptionOccurred (env))
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
+      cpnet_close (env, newfd);
       return;
     }
 
-  TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO (newfd, remote_address,
-						remote_port, result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpnet_getRemoteAddr (env, newfd, &remote_addr);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
+      cpnet_close (env, newfd);
       return;
     }
 
-  _javanet_set_remhost (env, impl, remote_address);
+  _javanet_set_remhost (env, impl, remote_addr);
   if ((*env)->ExceptionOccurred (env))
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
+      cpnet_close (env, newfd);
+      cpnet_freeAddress (env, remote_addr);
       return;
     }
 
   _javanet_set_int_field (env, impl, "java/net/SocketImpl", "port",
-			  remote_port);
+			  cpnet_addressGetPort (remote_addr));
+  cpnet_freeAddress (env, remote_addr);
   if ((*env)->ExceptionOccurred (env))
     {
       /* We don't care whether this succeeds. close() will cleanup later. */
-      TARGET_NATIVE_NETWORK_SOCKET_CLOSE (newfd, result);
+      cpnet_close (env, newfd);
       return;
     }
 #else /* not WITHOUT_NETWORK */
@@ -934,13 +910,14 @@ _javanet_accept (JNIEnv * env, jobject this, jobject impl)
  */
 int
 _javanet_recvfrom (JNIEnv * env, jobject this, jarray buf, int offset,
-		   int len, int *addr, int *port)
+		   int len, cpnet_address **addr)
 {
 #ifndef WITHOUT_NETWORK
   int fd;
   jbyte *p;
-  int from_address, from_port;
-  int received_bytes;
+  cpnet_address *from_addr;
+  jint received_bytes;
+  int result;
 
   DBG ("_javanet_recvfrom(): Entered _javanet_recvfrom\n");
 
@@ -962,36 +939,26 @@ _javanet_recvfrom (JNIEnv * env, jobject this, jarray buf, int offset,
   DBG ("_javanet_recvfrom(): Got buffer\n");
 
   /* Read the data */
-  from_address = 0;
-  from_port = 0;
+  from_addr = NULL;
   do
     {
       if (addr != NULL)
 	{
-	  TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT (fd,
-								  p + offset,
-								  len,
-								  from_address,
-								  from_port,
-								  received_bytes);
+	  result = cpnet_recvFrom (env, fd, p + offset, len, &from_addr, &received_bytes);
 	}
       else
 	{
-	  TARGET_NATIVE_NETWORK_SOCKET_RECEIVE (fd, p + offset, len,
-						received_bytes);
+	  result = cpnet_recv (env, fd, p + offset, len, &received_bytes);
 	}
     }
-  while ((received_bytes == -1) &&
-	 (TARGET_NATIVE_LAST_ERROR () ==
-	  TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL));
-
-  if (received_bytes == -1)
+  while (result == CPNATIVE_EINTR);
+  if (result != 0)
     {
-      if (TARGET_NATIVE_LAST_ERROR () == EAGAIN)
-	JCL_ThrowException (env, "java/net/SocketTimeoutException", "Timeout");
+      if (result == EAGAIN || result == ETIMEDOUT)
+	JCL_ThrowException (env, "java/net/SocketTimeoutException", "Receive operation timed out");
       else
 	JCL_ThrowException (env, IO_EXCEPTION,
-			    TARGET_NATIVE_LAST_ERROR_STRING ());
+			    cpnative_getErrorString (result));
  
       /* Cleanup and return. */
       (*env)->ReleaseByteArrayElements (env, buf, p, 0);
@@ -1003,9 +970,7 @@ _javanet_recvfrom (JNIEnv * env, jobject this, jarray buf, int offset,
   /* Handle return addr case */
   if (addr != NULL)
     {
-      (*addr) = from_address;
-      if (port != NULL)
-	(*port) = from_port;
+      (*addr) = from_addr;
     }
 
   /* zero bytes received means recv() noticed the other side orderly
@@ -1013,7 +978,7 @@ _javanet_recvfrom (JNIEnv * env, jobject this, jarray buf, int offset,
   if (received_bytes == 0)
     received_bytes = -1;
 
-  return (received_bytes);
+  return received_bytes;
 #else /* not WITHOUT_NETWORK */
 #endif /* not WITHOUT_NETWORK */
 }
@@ -1031,12 +996,13 @@ _javanet_recvfrom (JNIEnv * env, jobject this, jarray buf, int offset,
  */
 void
 _javanet_sendto (JNIEnv * env, jobject this, jarray buf, int offset, int len,
-		 int addr, int port)
+		 cpnet_address *addr)
 {
 #ifndef WITHOUT_NETWORK
   int fd;
   jbyte *p;
-  int bytes_sent;
+  jint bytes_sent;
+  int result;
 
   /* Get the real file descriptor */
   fd = _javanet_get_int_field (env, this, "native_fd");
@@ -1056,26 +1022,30 @@ _javanet_sendto (JNIEnv * env, jobject this, jarray buf, int offset, int len,
   while (len > 0)
     {
       /* Send the data */
-      if (addr == 0)
+      if (addr == NULL)
 	{
 	  DBG ("_javanet_sendto(): Sending....\n");
-	  TARGET_NATIVE_NETWORK_SOCKET_SEND (fd, p + offset, len, bytes_sent);
+	  result = cpnet_send (env, fd, p + offset, len, &bytes_sent);
 	}
       else
 	{
 	  DBG ("_javanet_sendto(): Sending....\n");
-	  TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT (fd, p + offset,
-							       len, addr, port,
-							       bytes_sent);
+	  result = cpnet_sendTo (env, fd, p + offset, len, addr, &bytes_sent);
 	}
 
+      if (result == EDESTADDRREQ)
+	{
+	  JCL_ThrowException (env, NULL_EXCEPTION,
+			      "Socket is not connected and no address is given");
+	  break;
+	}
+	
       if (bytes_sent < 0)
 	{
-	  if (TARGET_NATIVE_LAST_ERROR ()
-	      != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL)
+	  if (result != CPNATIVE_EINTR)
 	    {
 	      JCL_ThrowException (env, IO_EXCEPTION,
-				  TARGET_NATIVE_LAST_ERROR_STRING ());
+				  cpnative_getErrorString (result));
 	      break;
 	    }
 	}
@@ -1105,8 +1075,8 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
   int optval;
   jclass cls;
   jmethodID mid;
-  int address;
-  int result;
+  cpnet_address * address;
+  int result = CPNATIVE_OK;
 
   /* Get the real file descriptor */
   fd = _javanet_get_int_field (env, this, "native_fd");
@@ -1123,7 +1093,6 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
     return;
 
   /* Process the option request */
-  result = TARGET_NATIVE_ERROR;
   switch (option_id)
     {
       /* TCP_NODELAY case.  val is a Boolean that tells us what to do */
@@ -1141,8 +1110,7 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
       if ((*env)->ExceptionOccurred (env))
 	return;
 
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY (fd, optval,
-							   result);
+      result = cpnet_setSocketTCPNoDelay (env, fd, optval);
       break;
 
       /* SO_LINGER case.  If val is a boolean, then it will always be set
@@ -1153,8 +1121,7 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
       if (mid)
 	{
 	  /* We are disabling linger */
-	  TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER (fd, 1, 0,
-							     result);
+	  result = cpnet_setLinger (env, fd, JNI_FALSE, 0);
 	}
       else
 	{
@@ -1175,15 +1142,13 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
 	  if ((*env)->ExceptionOccurred (env))
 	    return;
 
-	  TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER (fd, 0, optval,
-							     result);
+	  result = cpnet_setLinger(env, fd, JNI_TRUE, optval);
 	}
       break;
 
       /* SO_TIMEOUT case. Val will be an integer with the new value */
       /* Not writable on Linux */
     case SOCKOPT_SO_TIMEOUT:
-#ifdef SO_TIMEOUT
       mid = (*env)->GetMethodID (env, cls, "intValue", "()I");
       if (mid == NULL)
 	{
@@ -1196,10 +1161,7 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
       if ((*env)->ExceptionOccurred (env))
 	return;
 
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_TIMEOUT (fd, optval, result);
-#else
-      result = TARGET_NATIVE_OK;
-#endif
+      result = cpnet_setSocketTimeout (env, fd, optval);
       break;
 
     case SOCKOPT_SO_SNDBUF:
@@ -1218,11 +1180,9 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
 	return;
 
       if (option_id == SOCKOPT_SO_SNDBUF)
-	TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_SNDBUF (fd, optval,
-							   result);
+	result = cpnet_setSendBuf (env, fd, optval);
       else
-	TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_RCDBUF (fd, optval,
-							   result);
+	result = cpnet_setRecvBuf (env, fd, optval);
       break;
 
       /* TTL case.  Val with be an Integer with the new time to live value */
@@ -1239,18 +1199,18 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
       if ((*env)->ExceptionOccurred (env))
 	return;
 
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_TTL (fd, optval, result);
+      result = cpnet_setTTL (env, fd, optval);
       break;
 
       /* Multicast Interface case - val is InetAddress object */
     case SOCKOPT_IP_MULTICAST_IF:
-      address = _javanet_get_netaddr (env, val);
+      address = _javanet_get_ip_netaddr (env, val);
 
       if ((*env)->ExceptionOccurred (env))
 	return;
-
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_MULTICAST_IF (fd, address,
-							       result);
+      
+      result = cpnet_setMulticastIF (env, fd, address);
+      cpnet_freeAddress (env, address);
       break;
 
     case SOCKOPT_SO_REUSEADDR:
@@ -1267,8 +1227,7 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
       if ((*env)->ExceptionOccurred (env))
 	return;
 
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_REUSE_ADDRESS (fd, optval,
-							     result);
+      result = cpnet_setReuseAddress (env, fd, optval);
       break;
 
     case SOCKOPT_SO_KEEPALIVE:
@@ -1285,7 +1244,7 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
       if ((*env)->ExceptionOccurred (env))
 	return;
 
-      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_KEEP_ALIVE (fd, optval, result);
+      result = cpnet_setKeepAlive (env, fd, optval);
       break;
 
     case SOCKOPT_SO_BINDADDR:
@@ -1298,10 +1257,10 @@ _javanet_set_option (JNIEnv * env, jobject this, jint option_id, jobject val)
     }
 
   /* Check to see if above operations succeeded */
-  if (result != TARGET_NATIVE_OK)
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, SOCKET_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING ());
+			  cpnative_getErrorString (result));
       return;
     }
 #else /* not WITHOUT_NETWORK */
@@ -1319,8 +1278,9 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
 #ifndef WITHOUT_NETWORK
   int fd;
   int flag, optval;
-  int address;
+  cpnet_address *address;
   int result;
+  jobject obj;
 
   /* Get the real file descriptor */
   fd = _javanet_get_int_field (env, this, "native_fd");
@@ -1336,12 +1296,11 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
     {
       /* TCP_NODELAY case.  Return a Boolean indicating on or off */
     case SOCKOPT_TCP_NODELAY:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_TCP_NODELAY (fd, optval,
-							   result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getSocketTCPNoDelay (env, fd, &optval);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
@@ -1355,17 +1314,17 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
       /* SO_LINGER case.  If disabled, return a Boolean object that represents
          false, else return an Integer that is the value of SO_LINGER */
     case SOCKOPT_SO_LINGER:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_LINGER (fd, flag, optval,
-							 result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getLinger (env, fd, &flag, &optval);
+
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
-      if (optval)
-	return (_javanet_create_integer (env, JNI_TRUE));
+      if (flag)
+	return (_javanet_create_integer (env, optval));
       else
 	return (_javanet_create_boolean (env, JNI_FALSE));
 
@@ -1373,34 +1332,27 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
 
       /* SO_TIMEOUT case. Return an Integer object with the timeout value */
     case SOCKOPT_SO_TIMEOUT:
-#ifdef SO_TIMEOUT
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_TIMEOUT (fd, optval, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getSocketTimeout (env, fd, &optval);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
       return (_javanet_create_integer (env, optval));
-#else
-      JCL_ThrowException (env, SOCKET_EXCEPTION,
-			  "SO_TIMEOUT not supported on this platform");
-      return (0);
-#endif /* not SO_TIMEOUT */
       break;
 
     case SOCKOPT_SO_SNDBUF:
     case SOCKOPT_SO_RCVBUF:
       if (option_id == SOCKOPT_SO_SNDBUF)
-	TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_SNDBUF (fd, optval,
-							   result);
+	result = cpnet_getSendBuf (env, fd, &optval);
       else
-	TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_RCDBUF (fd, optval,
-							   result);
-      if (result != TARGET_NATIVE_OK)
+	result = cpnet_getRecvBuf (env, fd, &optval);
+
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
@@ -1409,11 +1361,11 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
 
       /* The TTL case.  Return an Integer with the Time to Live value */
     case SOCKOPT_IP_TTL:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_TTL (fd, optval, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getTTL (env, fd, &optval);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
@@ -1422,61 +1374,64 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
 
       /* Multicast interface case */
     case SOCKOPT_IP_MULTICAST_IF:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_MULTICAST_IF (fd, address,
-							       result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getMulticastIF (env, fd, &address);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
-      return (_javanet_create_inetaddress (env, address));
+      obj = _javanet_create_inetaddress (env, address);
+      cpnet_freeAddress (env, address);
+
+      return obj;
       break;
 
     case SOCKOPT_SO_BINDADDR:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BIND_ADDRESS (fd, address,
-							    result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getLocalAddr (env, fd, &address);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
-      return (_javanet_create_inetaddress (env, address));
+      obj = _javanet_create_inetaddress (env, address);
+      cpnet_freeAddress (env, address);
+
+      return obj;
       break;
 
     case SOCKOPT_SO_REUSEADDR:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_REUSE_ADDRESS (fd, optval,
-							     result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getReuseAddress (env, fd, &optval);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
       if (optval)
-	return (_javanet_create_boolean (env, JNI_TRUE));
+	return _javanet_create_boolean (env, JNI_TRUE);
       else
-	return (_javanet_create_boolean (env, JNI_FALSE));
+	return _javanet_create_boolean (env, JNI_FALSE);
 
       break;
 
     case SOCKOPT_SO_KEEPALIVE:
-      TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_KEEP_ALIVE (fd, optval, result);
-      if (result != TARGET_NATIVE_OK)
+      result = cpnet_getKeepAlive (env, fd, &optval);
+      if (result != CPNATIVE_OK)
 	{
 	  JCL_ThrowException (env, SOCKET_EXCEPTION,
-			      TARGET_NATIVE_LAST_ERROR_STRING ());
+			      cpnative_getErrorString (result));
 	  return (0);
 	}
 
       if (optval)
-	return (_javanet_create_boolean (env, JNI_TRUE));
+	return _javanet_create_boolean (env, JNI_TRUE);
       else
-	return (_javanet_create_boolean (env, JNI_FALSE));
+	return _javanet_create_boolean (env, JNI_FALSE);
 
       break;
 
@@ -1493,6 +1448,7 @@ _javanet_get_option (JNIEnv * env, jobject this, jint option_id)
 void
 _javanet_shutdownInput (JNIEnv * env, jobject this)
 {
+  int result;
   int fd;
 
   /* Get the real file descriptor. */
@@ -1505,10 +1461,11 @@ _javanet_shutdownInput (JNIEnv * env, jobject this)
     }
 
   /* Shutdown input stream of socket. */
-  if (shutdown (fd, SHUT_RD) == -1)
+  result = cpnet_shutdown (env, fd, CPNET_SHUTDOWN_READ);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, SOCKET_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING());
+			  cpnative_getErrorString (result));
       return;
     }
 }
@@ -1517,6 +1474,7 @@ void
 _javanet_shutdownOutput (JNIEnv * env, jobject this)
 {
   int fd;
+  int result;
 
   /* Get the real file descriptor. */
   fd = _javanet_get_int_field (env, this, "native_fd");
@@ -1528,10 +1486,11 @@ _javanet_shutdownOutput (JNIEnv * env, jobject this)
     }
 
   /* Shutdown output stream of socket. */
-  if (shutdown (fd, SHUT_WR) == -1)
+  result = cpnet_shutdown (env, fd, CPNET_SHUTDOWN_WRITE);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, SOCKET_EXCEPTION,
-			  TARGET_NATIVE_LAST_ERROR_STRING());
+			  cpnative_getErrorString (result));
       return;
     }
 }
-- 
cgit v1.1