diff options
-rw-r--r-- | native/jni/java-net/java_net_InetAddress.c | 166 | ||||
-rw-r--r-- | native/jni/java-net/java_net_NetworkInterface.c | 16 | ||||
-rw-r--r-- | native/jni/java-net/java_net_PlainDatagramSocketImpl.c | 257 | ||||
-rw-r--r-- | native/jni/java-net/java_net_PlainSocketImpl.c | 105 | ||||
-rw-r--r-- | native/jni/java-net/javanet.c | 651 |
5 files changed, 769 insertions, 426 deletions
diff --git a/native/jni/java-net/java_net_InetAddress.c b/native/jni/java-net/java_net_InetAddress.c index da8d53e24..71ca3c886 100644 --- a/native/jni/java-net/java_net_InetAddress.c +++ b/native/jni/java-net/java_net_InetAddress.c @@ -35,22 +35,26 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ +/* do not move; needed here because of some macro definitions */ +#include <config.h> -#include <stdio.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> -#include <unistd.h> -#include <netdb.h> -#include <sys/socket.h> -#include <netinet/in.h> +#include <assert.h> #include <jni.h> #include <jcl.h> -#include "java_net_InetAddress.h" - #include "javanet.h" +#include "target_native.h" +#ifndef WITHOUT_NETWORK + #include "target_native_network.h" +#endif /* WITHOUT_NETWORK */ + +#include "java_net_InetAddress.h" + /*************************************************************************/ /* @@ -59,13 +63,24 @@ exception statement from your version. */ JNIEXPORT jstring JNICALL Java_java_net_InetAddress_getLocalHostName(JNIEnv *env, jclass class) { - char buf[255]; + char hostname[256]; + int result; jstring retval; - if (gethostname(buf, sizeof(buf) - 1) == -1) - strcpy(buf, "localhost"); + assert(env!=NULL); + assert((*env)!=NULL); - retval = (*env)->NewStringUTF(env, buf); +#ifndef WITHOUT_NETWORK + TARGET_NATIVE_NETWORK_GET_HOSTNAME(hostname,sizeof(hostname),result); + if (result != TARGET_NATIVE_OK) + { + strcpy(hostname,"localhost"); + } +#else /* not WITHOUT_NETWORK */ + strcpy(hostname, "localhost"); +#endif /* not WITHOUT_NETWORK */ + + retval = (*env)->NewStringUTF(env, hostname); return(retval); } @@ -78,28 +93,39 @@ Java_java_net_InetAddress_getLocalHostName(JNIEnv *env, jclass class) JNIEXPORT jarray JNICALL Java_java_net_InetAddress_lookupInaddrAny(JNIEnv *env, jclass class) { - jarray arr; - jbyte *octets; + jarray IParray; + jbyte *octets; + + assert(env!=NULL); + assert((*env)!=NULL); /* Allocate an array for the IP address */ - arr = (*env)->NewByteArray(env, 4); - if (!arr) + IParray = (*env)->NewByteArray(env, 4); + if (IParray == NULL) { JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); return (jarray)NULL; } /* Copy in the values */ - octets = (*env)->GetByteArrayElements(env, arr, 0); - - octets[0] = (INADDR_ANY & 0xFF000000) >> 24; - octets[1] = (INADDR_ANY & 0x00FF0000) >> 16; - octets[2] = (INADDR_ANY & 0x0000FF00) >> 8; - octets[3] = (INADDR_ANY & 0x000000FF); - - (*env)->ReleaseByteArrayElements(env, arr, octets, 0); - - return(arr); + octets = (*env)->GetByteArrayElements(env, IParray, 0); + +#ifndef WITHOUT_NETWORK + TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(INADDR_ANY, + octets[0], + octets[1], + octets[2], + octets[3] + ); + (*env)->ReleaseByteArrayElements(env, IParray, octets, 0); +#else /* not WITHOUT_NETWORK */ + octets[0]=0; + octets[1]=0; + octets[2]=0; + octets[3]=0; +#endif /* not WITHOUT_NETWORK */ + + return(IParray); } /*************************************************************************/ @@ -111,12 +137,17 @@ Java_java_net_InetAddress_lookupInaddrAny(JNIEnv *env, jclass class) JNIEXPORT jstring JNICALL Java_java_net_InetAddress_getHostByAddr(JNIEnv *env, jclass class, jarray arr) { - jbyte *octets; - jsize len; - int addr; - struct hostent *hp; +#ifndef WITHOUT_NETWORK + jbyte *octets; + jsize len; + int addr; + char hostname[255]; + int result; jstring retval; + assert(env!=NULL); + assert((*env)!=NULL); + /* Grab the byte[] array with the IP out of the input data */ len = (*env)->GetArrayLength(env, arr); if (len != 4) @@ -133,23 +164,30 @@ Java_java_net_InetAddress_getHostByAddr(JNIEnv *env, jclass class, jarray arr) } /* Convert it to a 32 bit address */ - addr = (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3]; - addr = htonl(addr); + TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT(octets[0], + octets[1], + octets[2], + octets[3], + addr + ); /* Release some memory */ (*env)->ReleaseByteArrayElements(env, arr, octets, 0); /* Resolve the address and return the name */ - hp = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET); - if (!hp) + TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS(addr,hostname,sizeof(hostname),result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address"); + JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP address"); return (jstring)NULL; } - retval = (*env)->NewStringUTF(env, hp->h_name); + retval = (*env)->NewStringUTF(env, hostname); return(retval); +#else /* not WITHOUT_NETWORK */ + return (jstring)NULL; +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -157,14 +195,20 @@ Java_java_net_InetAddress_getHostByAddr(JNIEnv *env, jclass class, jarray arr) JNIEXPORT jobjectArray JNICALL Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host) { - const char *hostname; - struct hostent *hp; - int i, ip; - jbyte *octets; - jsize num_addrs; - jclass arr_class; - jobjectArray addrs; - jarray ret_octets; +#ifndef WITHOUT_NETWORK + const char *hostname; +/* FIXME: limitation of max. 64 addresses - how to make it more flexibale? */ + int addresses[64]; + jsize addresses_count; + int result; + jclass arr_class; + jobjectArray addrs; + int i; + jbyte *octets; + jarray ret_octets; + + assert(env!=NULL); + assert((*env)!=NULL); /* Grab the hostname string */ hostname = (*env)->GetStringUTFChars(env, host, 0); @@ -175,18 +219,19 @@ Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host) } /* Look up the host */ - hp = gethostbyname(hostname); - if (!hp) + TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME(hostname, + addresses, + sizeof(addresses)/sizeof(addresses[0]), + addresses_count, + result + ); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, hostname); + JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, (char*)hostname); return (jobjectArray)NULL; } (*env)->ReleaseStringUTFChars(env, host, hostname); - /* Figure out how many addresses there are and allocate a return array */ - for (num_addrs = 0, i = 0; hp->h_addr_list[i] ; i++) - ++num_addrs; - arr_class = (*env)->FindClass(env,"[B"); if (!arr_class) { @@ -194,7 +239,7 @@ Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host) return (jobjectArray)NULL; } - addrs = (*env)->NewObjectArray(env, num_addrs, arr_class, 0); + addrs = (*env)->NewObjectArray(env, addresses_count, arr_class, 0); if (!addrs) { JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); @@ -202,7 +247,7 @@ Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host) } /* Now loop and copy in each address */ - for (i = 0; i < num_addrs; i++) + for (i = 0; i < addresses_count; i++) { ret_octets = (*env)->NewByteArray(env, 4); if (!ret_octets) @@ -213,17 +258,22 @@ Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host) octets = (*env)->GetByteArrayElements(env, ret_octets, 0); - ip = ntohl(*(int*)(hp->h_addr_list[i])); - octets[0] = (ip & 0xFF000000) >> 24; - octets[1] = (ip & 0x00FF0000) >> 16; - octets[2] = (ip & 0x0000FF00) >> 8; - octets[3] = (ip & 0x000000FF); + TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(addresses[i], + octets[0], + octets[1], + octets[2], + octets[3] + ); (*env)->ReleaseByteArrayElements(env, ret_octets, octets, 0); + (*env)->SetObjectArrayElement(env, addrs, i, ret_octets); } return(addrs); +#else /* not WITHOUT_NETWORK */ + return (jobjectArray)NULL; +#endif /* not WITHOUT_NETWORK */ } - +/* end of file */ diff --git a/native/jni/java-net/java_net_NetworkInterface.c b/native/jni/java-net/java_net_NetworkInterface.c index e2571f424..6c51f872f 100644 --- a/native/jni/java-net/java_net_NetworkInterface.c +++ b/native/jni/java-net/java_net_NetworkInterface.c @@ -35,19 +35,14 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ +/* do not move; needed here because of some macro definitions */ #include <config.h> -#include <errno.h> - -#ifdef HAVE_SYS_IOCTL_H -#include <sys/ioctl.h> -#endif - -#ifdef HAVE_ASM_IOCTLS_H -#include <asm/ioctls.h> -#endif +#include <stdlib.h> +#include <stdio.h> #include <string.h> - +#include <assert.h> + #include <jni.h> #include <jcl.h> @@ -65,3 +60,4 @@ Java_java_net_NetworkInterface_getRealNetworkInterfaces (JNIEnv* env, jclass cla return 0; } +/* end of file */ diff --git a/native/jni/java-net/java_net_PlainDatagramSocketImpl.c b/native/jni/java-net/java_net_PlainDatagramSocketImpl.c index ca4660603..167495281 100644 --- a/native/jni/java-net/java_net_PlainDatagramSocketImpl.c +++ b/native/jni/java-net/java_net_PlainDatagramSocketImpl.c @@ -35,22 +35,26 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ +/* do not move; needed here because of some macro definitions */ +#include <config.h> -#include <stdio.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> +#include <assert.h> #include <jni.h> #include <jcl.h> -#include "java_net_PlainDatagramSocketImpl.h" - #include "javanet.h" +#include "target_native.h" +#ifndef WITHOUT_NETWORK + #include "target_native_network.h" +#endif /* WITHOUT_NETWORK */ + +#include "java_net_PlainDatagramSocketImpl.h" + /* * Note that most of the functions in this module simply redirect to another * internal function. Why? Because many of these functions are shared @@ -65,7 +69,13 @@ exception statement from your version. */ JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_create(JNIEnv *env, jobject this) { + assert(env!=NULL); + assert((*env)!=NULL); + +#ifndef WITHOUT_NETWORK _javanet_create(env, this, 0); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -76,7 +86,13 @@ Java_java_net_PlainDatagramSocketImpl_create(JNIEnv *env, jobject this) JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_close(JNIEnv *env, jobject this) { + assert(env!=NULL); + assert((*env)!=NULL); + +#ifndef WITHOUT_NETWORK _javanet_close(env, this, 0); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -90,7 +106,13 @@ JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_bind(JNIEnv *env, jobject this, jint port, jobject addr) { + assert(env!=NULL); + assert((*env)!=NULL); + +#ifndef WITHOUT_NETWORK _javanet_bind(env, this, addr, port, 0); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -102,7 +124,13 @@ JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_setOption(JNIEnv *env, jobject this, jint option_id, jobject val) { + assert(env!=NULL); + assert((*env)!=NULL); + +#ifndef WITHOUT_NETWORK _javanet_set_option(env, this, option_id, val); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -114,7 +142,13 @@ JNIEXPORT jobject JNICALL Java_java_net_PlainDatagramSocketImpl_getOption(JNIEnv *env, jobject this, jint option_id) { + assert(env!=NULL); + assert((*env)!=NULL); + +#ifndef WITHOUT_NETWORK return(_javanet_get_option(env, this, option_id)); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -126,38 +160,65 @@ JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_receive(JNIEnv *env, jobject this, jobject packet) { - unsigned int addr = 0, port = 0, len = 0, bytes_read = 0; - jclass cls, addr_cls; - jmethodID mid; - jarray arr; - jbyte *buf; - char ip_str[16]; - jobject ip_str_obj, addr_obj; +#ifndef WITHOUT_NETWORK + unsigned int addr, port, len, bytes_read; + jclass cls, addr_cls; + jmethodID mid; + jarray arr; + unsigned char octets[4]; + char ip_str[16]; + jobject ip_str_obj, addr_obj; + + assert(env!=NULL); + assert((*env)!=NULL); + + addr = 0; + port = 0; + len = 0; + bytes_read = 0; if(packet == NULL) - { JCL_ThrowException(env, "java/lang/NullPointerException", "Null datagram packet"); return; } + { + JCL_ThrowException(env, "java/lang/NullPointerException", "Null datagram packet"); + return; + } /* Get the buffer from the packet */ cls = (*env)->GetObjectClass(env, packet); if (cls == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); + return; + } mid = (*env)->GetMethodID(env, cls, "getData", "()[B"); if (mid == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: getData"); + return; + } arr = (*env)->CallObjectMethod(env, packet, mid); if ((arr == NULL) || (*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call getData"); + return; + } /* Now get the length from the packet */ mid = (*env)->GetMethodID(env, cls, "getLength", "()I"); if (mid == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: getLength"); + return; + } len = (*env)->CallIntMethod(env, packet, mid); if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call getLength"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Got the length\n"); @@ -165,42 +226,71 @@ Java_java_net_PlainDatagramSocketImpl_receive(JNIEnv *env, jobject this, /* should we try some sort of validation on the length? */ bytes_read = _javanet_recvfrom(env, this, arr, 0, len, &addr, &port); if ((bytes_read == -1) || (*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: receive"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Received packet\n"); /* Store the address */ - addr = ntohl(addr); - sprintf(ip_str, "%d.%d.%d.%d", (addr & 0xFF000000) >> 24, - (addr & 0x00FF0000) >> 16, (addr & 0x0000FF00) >> 8, - (addr & 0x000000FF)); + TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(addr, + octets[0], + octets[1], + octets[2], + octets[3] + ); + sprintf(ip_str, "%d.%d.%d.%d", + octets[0], + octets[1], + octets[2], + octets[3] + ); ip_str_obj = (*env)->NewStringUTF(env, ip_str); if (ip_str_obj == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: new string"); + return; + } addr_cls = (*env)->FindClass(env, "java/net/InetAddress"); if (addr_cls == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: InetAddress class"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Found InetAddress class\n"); mid = (*env)->GetStaticMethodID(env, addr_cls, "getByName", "(Ljava/lang/String;)Ljava/net/InetAddress;"); if (mid == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Found InetAddress.getByName method\n"); addr_obj = (*env)->CallStaticObjectMethod(env, addr_cls, mid, ip_str_obj); if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call getByName"); + return; + } mid = (*env)->GetMethodID(env, cls, "setAddress", "(Ljava/net/InetAddress;)V"); if (mid == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: setAddress"); + return; + } (*env)->CallVoidMethod(env, packet, mid, addr_obj); if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call setAddress"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Stored the address\n"); @@ -209,26 +299,38 @@ Java_java_net_PlainDatagramSocketImpl_receive(JNIEnv *env, jobject this, mid = (*env)->GetMethodID(env, cls, "setPort", "(I)V"); if (mid == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: setPort"); + return; + } (*env)->CallVoidMethod(env, packet, mid, port); if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call setPort"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Stored the port\n"); /* Store back the length */ mid = (*env)->GetMethodID(env, cls, "setLength", "(I)V"); if (mid == NULL) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: setLength"); + return; + } (*env)->CallVoidMethod(env, packet, mid, bytes_read); if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call setLength"); + return; + } DBG("PlainDatagramSocketImpl.receive(): Stored the length\n"); - - return; +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -241,22 +343,27 @@ Java_java_net_PlainDatagramSocketImpl_sendto(JNIEnv *env, jobject this, jobject addr, jint port, jarray buf, jint len) { - struct ip_mreq ipm; - memset(&ipm, 0, sizeof(ipm)); - ipm.imr_multiaddr.s_addr = _javanet_get_netaddr(env, addr); +#ifndef WITHOUT_NETWORK + jint netAddress; + + assert(env!=NULL); + assert((*env)!=NULL); + + netAddress = _javanet_get_netaddr(env, addr); if ((*env)->ExceptionOccurred(env)) { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } DBG("PlainDatagramSocketImpl.sendto(): have addr\n"); - _javanet_sendto(env, this, buf, 0, len, ipm.imr_multiaddr.s_addr, - htons(((unsigned short)port))); + _javanet_sendto(env, this, buf, 0, len, netAddress, port); if ((*env)->ExceptionOccurred(env)) { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } DBG("PlainDatagramSocketImpl.sendto(): finished\n"); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -268,28 +375,41 @@ JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_join(JNIEnv *env, jobject this, jobject addr) { - int rc, fd; - struct ip_mreq ipm; +#ifndef WITHOUT_NETWORK + jint netAddress; + int fd; + int result; - memset(&ipm, 0, sizeof(ipm)); - ipm.imr_multiaddr.s_addr = _javanet_get_netaddr(env, addr); - if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + assert(env!=NULL); + assert((*env)!=NULL); - ipm.imr_interface.s_addr = INADDR_ANY; + netAddress = _javanet_get_netaddr(env, addr); + if ((*env)->ExceptionOccurred(env)) + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); + return; + } fd = _javanet_get_int_field(env, this, "native_fd"); if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); + return; + } DBG("PlainDatagramSocketImpl.join(): have native fd\n"); - rc = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &ipm, sizeof(ipm)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_ADD_MEMBERSHIP(fd,netAddress,result); - if (rc == -1) - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } DBG("PlainDatagramSocketImpl.join(): finished\n"); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -301,15 +421,20 @@ JNIEXPORT void JNICALL Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this, jobject addr) { - int rc, fd; - struct ip_mreq ipm; +#ifndef WITHOUT_NETWORK + jint netAddress; + int fd; + int result; - memset(&ipm, 0, sizeof(ipm)); - ipm.imr_multiaddr.s_addr = _javanet_get_netaddr(env, addr); - if ((*env)->ExceptionOccurred(env)) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); return; } + assert(env!=NULL); + assert((*env)!=NULL); - ipm.imr_interface.s_addr = INADDR_ANY; + netAddress = _javanet_get_netaddr(env, addr); + if ((*env)->ExceptionOccurred(env)) + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error"); + return; + } fd = _javanet_get_int_field(env, this, "native_fd"); if ((*env)->ExceptionOccurred(env)) @@ -317,10 +442,14 @@ Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this, DBG("PlainDatagramSocketImpl.leave(): have native fd\n"); - rc = setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &ipm, sizeof(ipm)); - - if (rc == -1) - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_DROP_MEMBERSHIP(fd,netAddress,result); + if (result!=TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } DBG("PlainDatagramSocketImpl.leave(): finished\n"); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } diff --git a/native/jni/java-net/java_net_PlainSocketImpl.c b/native/jni/java-net/java_net_PlainSocketImpl.c index f3c096af7..982755511 100644 --- a/native/jni/java-net/java_net_PlainSocketImpl.c +++ b/native/jni/java-net/java_net_PlainSocketImpl.c @@ -35,26 +35,26 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ +/* do not move; needed here because of some macro definitions */ #include <config.h> -#include <errno.h> - -#ifdef HAVE_SYS_IOCTL_H -#include <sys/ioctl.h> -#endif - -#ifdef HAVE_ASM_IOCTLS_H -#include <asm/ioctls.h> -#endif +#include <stdlib.h> +#include <stdio.h> #include <string.h> +#include <assert.h> #include <jni.h> #include <jcl.h> -#include "java_net_PlainSocketImpl.h" - #include "javanet.h" +#include "target_native.h" +#ifndef WITHOUT_NETWORK + #include "target_native_network.h" +#endif /* WITHOUT_NETWORK */ + +#include "java_net_PlainSocketImpl.h" + /* * Note that the functions in this module simply redirect to another * internal function. Why? Because many of these functions are shared @@ -70,7 +70,13 @@ exception statement from your version. */ JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_create(JNIEnv *env, jobject this, jboolean stream) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_create(env, this, stream); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -82,7 +88,13 @@ Java_java_net_PlainSocketImpl_create(JNIEnv *env, jobject this, jboolean stream) JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_close(JNIEnv *env, jobject this) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_close(env, this, 1); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -94,7 +106,13 @@ JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_connect(JNIEnv *env, jobject this, jobject addr, jint port) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_connect(env, this, addr, port); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -108,7 +126,13 @@ JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_bind(JNIEnv *env, jobject this, jobject addr, jint port) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_bind(env, this, addr, port, 1); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -120,7 +144,13 @@ Java_java_net_PlainSocketImpl_bind(JNIEnv *env, jobject this, jobject addr, JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_listen(JNIEnv *env, jobject this, jint queuelen) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_listen(env, this, queuelen); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -132,7 +162,13 @@ Java_java_net_PlainSocketImpl_listen(JNIEnv *env, jobject this, jint queuelen) JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_accept(JNIEnv *env, jobject this, jobject impl) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_accept(env, this, impl); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -140,11 +176,16 @@ Java_java_net_PlainSocketImpl_accept(JNIEnv *env, jobject this, jobject impl) JNIEXPORT jint JNICALL Java_java_net_PlainSocketImpl_available(JNIEnv *env, jobject this) { - int fd; - int count = 0; - jclass cls; +#ifndef WITHOUT_NETWORK + jclass cls; jfieldID fid; + int fd; + int bytesAvailable; + int result; + assert(env!=NULL); + assert((*env)!=NULL); + cls = (*env)->GetObjectClass(env, this); if (cls == 0) { @@ -161,13 +202,16 @@ Java_java_net_PlainSocketImpl_available(JNIEnv *env, jobject this) fd = (*env)->GetIntField(env, this, fid); - if (ioctl(fd, FIONREAD, &count) == -1) + TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE(fd,bytesAvailable,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return 0; } - else - return count; + + return bytesAvailable; +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -179,7 +223,13 @@ JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_setOption(JNIEnv *env, jobject this, jint option_id, jobject val) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_set_option(env, this, option_id, val); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -191,7 +241,13 @@ JNIEXPORT jobject JNICALL Java_java_net_PlainSocketImpl_getOption(JNIEnv *env, jobject this, jint option_id) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + return(_javanet_get_option(env, this, option_id)); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -203,7 +259,13 @@ JNIEXPORT jint JNICALL Java_java_net_PlainSocketImpl_read(JNIEnv *env, jobject this, jarray buf, jint offset, jint len) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + return(_javanet_recvfrom(env, this, buf, offset, len, 0, 0)); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -215,6 +277,13 @@ JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_write(JNIEnv *env, jobject this, jarray buf, jint offset, jint len) { +#ifndef WITHOUT_NETWORK + assert(env!=NULL); + assert((*env)!=NULL); + _javanet_sendto(env, this, buf, offset, len, 0, 0); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } +/* end of file */ diff --git a/native/jni/java-net/javanet.c b/native/jni/java-net/javanet.c index 540061dbe..c86305ce6 100644 --- a/native/jni/java-net/javanet.c +++ b/native/jni/java-net/javanet.c @@ -35,22 +35,25 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ +/* do not move; needed here because of some macro definitions */ +#include <config.h> -#include <stdio.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> -#include <errno.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netinet/tcp.h> +#include <assert.h> #include <jni.h> #include <jcl.h> #include "javanet.h" +#include "target_native.h" +#ifndef WITHOUT_NETWORK + #include "target_native_network.h" +#endif /* WITHOUT_NETWORK */ + +#ifndef WITHOUT_NETWORK /* Need to have some value for SO_TIMEOUT */ #ifndef SO_TIMEOUT #ifndef SO_RCVTIMEO @@ -60,6 +63,7 @@ exception statement from your version. */ #define SO_TIMEOUT SO_RCVTIMEO #endif /* not SO_RCVTIMEO */ #endif /* not SO_TIMEOUT */ +#endif /* WITHOUT_NETWORK */ /*************************************************************************/ @@ -70,9 +74,12 @@ static void _javanet_set_int_field(JNIEnv *env, jobject obj, char *class, char *field, int val) { - jclass cls; + jclass cls; jfieldID fid; + assert(env!=NULL); + assert((*env)!=NULL); + cls = (*env)->FindClass(env, class); if (cls == NULL) return; @@ -99,6 +106,9 @@ _javanet_get_int_field(JNIEnv *env, jobject obj, const char *field) jfieldID fid; int fd; + assert(env!=NULL); + assert((*env)!=NULL); + DBG("_javanet_get_int_field(): Entered _javanet_get_int_field\n"); cls = (*env)->GetObjectClass(env, obj); @@ -125,10 +135,13 @@ _javanet_get_int_field(JNIEnv *env, jobject obj, const char *field) static void _javanet_create_localfd(JNIEnv *env, jobject this) { - jclass this_cls, fd_cls; - jfieldID fid; + jclass this_cls, fd_cls; + jfieldID fid; jmethodID mid; - jobject fd_obj; + jobject fd_obj; + + assert(env!=NULL); + assert((*env)!=NULL); DBG("_javanet_create_localfd(): Entered _javanet_create_localfd\n"); @@ -177,9 +190,12 @@ _javanet_create_localfd(JNIEnv *env, jobject this) static jobject _javanet_create_boolean(JNIEnv *env, jboolean val) { - jclass cls; + jclass cls; jmethodID mid; - jobject obj; + jobject obj; + + assert(env!=NULL); + assert((*env)!=NULL); cls = (*env)->FindClass(env, "java/lang/Boolean"); if (cls == NULL) @@ -208,6 +224,9 @@ _javanet_create_integer(JNIEnv *env, jint val) jmethodID mid; jobject obj; + assert(env!=NULL); + assert((*env)!=NULL); + cls = (*env)->FindClass(env, "java/lang/Integer"); if (cls == NULL) return NULL; @@ -231,48 +250,95 @@ _javanet_create_integer(JNIEnv *env, jint val) static jobject _javanet_create_inetaddress(JNIEnv *env, int netaddr) { - char buf[16]; - jclass ia_cls; - jmethodID mid; - jstring ip_str; - jobject ia; +#ifndef WITHOUT_NETWORK + unsigned char octets[4]; + char buf[16]; + jclass ia_cls; + jmethodID mid; + jstring ip_str; + jobject ia; + + assert(env!=NULL); + assert((*env)!=NULL); /* Build a string IP address */ - sprintf(buf, "%d.%d.%d.%d", ((netaddr & 0xFF000000) >> 24), - ((netaddr & 0x00FF0000) >> 16), ((netaddr &0x0000FF00) >> 8), - (netaddr & 0x000000FF)); + 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] + ); DBG("_javanet_create_inetaddress(): Created ip addr string\n"); /* Get an InetAddress object for this IP */ ia_cls = (*env)->FindClass(env, "java/net/InetAddress"); if (ia_cls == NULL) - return NULL; + { + return NULL; + } DBG("_javanet_create_inetaddress(): Found InetAddress class\n"); mid = (*env)->GetStaticMethodID(env, ia_cls, "getByName", "(Ljava/lang/String;)Ljava/net/InetAddress;"); if (mid == NULL) - return NULL; + { + return NULL; + } DBG("_javanet_create_inetaddress(): Found getByName method\n"); ip_str = (*env)->NewStringUTF(env, buf); if (ip_str == NULL) - return NULL; + { + return NULL; + } ia = (*env)->CallStaticObjectMethod(env, ia_cls, mid, ip_str); if (ia == NULL) - return NULL; + { + return NULL; + } DBG("_javanet_create_inetaddress(): Called getByName method\n"); return ia; +#else /* not WITHOUT_NETWORK */ + return NULL; +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ -static void _javanet_set_remhost_addr(JNIEnv*, jobject, jobject); +static void +_javanet_set_remhost_addr(JNIEnv *env, jobject this, jobject ia) +{ + jclass this_cls; + jfieldID fid; + + assert(env!=NULL); + assert((*env)!=NULL); + + /* Set the variable in the object */ + this_cls = (*env)->FindClass(env, "java/net/SocketImpl"); + if (this_cls == NULL) + return; + + fid = (*env)->GetFieldID(env, this_cls, "address", "Ljava/net/InetAddress;"); + if (fid == NULL) + return; + + DBG("_javanet_set_remhost_addr(): Found address field\n"); + + (*env)->SetObjectField(env, this, fid, ia); + DBG("_javanet_set_remhost_addr(): Set field\n"); +} /* * Set's the value of the "addr" field in PlainSocketImpl with a new @@ -283,6 +349,9 @@ _javanet_set_remhost(JNIEnv *env, jobject this, int netaddr) { jobject ia; + assert(env!=NULL); + assert((*env)!=NULL); + DBG("_javanet_set_remhost(): Entered _javanet_set_remhost\n"); /* Get an InetAddress object */ @@ -293,26 +362,6 @@ _javanet_set_remhost(JNIEnv *env, jobject this, int netaddr) _javanet_set_remhost_addr(env, this, ia); } -static void -_javanet_set_remhost_addr(JNIEnv *env, jobject this, jobject ia) -{ - jclass this_cls; - jfieldID fid; - - /* Set the variable in the object */ - this_cls = (*env)->FindClass(env, "java/net/SocketImpl"); - if (this_cls == NULL) - return; - - fid = (*env)->GetFieldID(env, this_cls, "address", "Ljava/net/InetAddress;"); - if (fid == NULL) - return; - - DBG("_javanet_set_remhost_addr(): Found address field\n"); - - (*env)->SetObjectField(env, this, fid, ia); - DBG("_javanet_set_remhost_addr(): Set field\n"); -} /*************************************************************************/ @@ -322,11 +371,15 @@ _javanet_set_remhost_addr(JNIEnv *env, jobject this, jobject ia) int _javanet_get_netaddr(JNIEnv *env, jobject addr) { - jclass cls = 0; +#ifndef WITHOUT_NETWORK + jclass cls = 0; jmethodID mid; - jarray arr = 0; - jbyte *octets; - int netaddr, len; + jarray arr = 0; + jbyte *octets; + int netaddr, len; + + assert(env!=NULL); + assert((*env)!=NULL); DBG("_javanet_get_netaddr(): Entered _javanet_get_netaddr\n"); @@ -362,17 +415,19 @@ _javanet_get_netaddr(JNIEnv *env, jobject addr) DBG("_javanet_get_netaddr(): Grabbed bytes\n"); - netaddr = (((unsigned char)octets[0]) << 24) + - (((unsigned char)octets[1]) << 16) + - (((unsigned char)octets[2]) << 8) + - ((unsigned char)octets[3]); - - netaddr = htonl(netaddr); + TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT(octets[0], + octets[1], + octets[2], + octets[3], + netaddr + ); (*env)->ReleaseByteArrayElements(env, arr, octets, 0); DBG("_javanet_get_netaddr(): Done getting addr\n"); return netaddr; +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -383,15 +438,23 @@ _javanet_get_netaddr(JNIEnv *env, jobject addr) void _javanet_create(JNIEnv *env, jobject this, jboolean stream) { +#ifndef WITHOUT_NETWORK int fd; + int result; + + assert(env!=NULL); + assert((*env)!=NULL); if (stream) - fd = socket(AF_INET, SOCK_STREAM, 0); + TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM(fd,result); else - fd = socket(AF_INET, SOCK_DGRAM, 0); + TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM(fd,result); - if (fd == -1) - { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; } + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } if (stream) _javanet_set_int_field(env, this, "java/net/PlainSocketImpl", @@ -399,6 +462,8 @@ _javanet_create(JNIEnv *env, jobject this, jboolean stream) else _javanet_set_int_field(env, this, "java/net/PlainDatagramSocketImpl", "native_fd", fd); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -410,13 +475,18 @@ _javanet_create(JNIEnv *env, jobject this, jboolean stream) void _javanet_close(JNIEnv *env, jobject this, int stream) { - int fd = -1; +#ifndef WITHOUT_NETWORK + int fd; + int result; + + assert(env!=NULL); + assert((*env)!=NULL); fd = _javanet_get_int_field(env, this, "native_fd"); if (fd == -1) return; - close(fd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); if (stream) _javanet_set_int_field(env, this, "java/net/PlainSocketImpl", @@ -424,6 +494,8 @@ _javanet_close(JNIEnv *env, jobject this, int stream) else _javanet_set_int_field(env, this, "java/net/PlainDatagramSocketImpl", "native_fd", -1); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -434,8 +506,14 @@ _javanet_close(JNIEnv *env, jobject this, int stream) void _javanet_connect(JNIEnv *env, jobject this, jobject addr, jint port) { - int netaddr, fd = -1, rc, addrlen; - struct sockaddr_in si; +#ifndef WITHOUT_NETWORK + int netaddr, fd; + int result; + int local_address,local_port; + int remote_address,remote_port; + + assert(env!=NULL); + assert((*env)!=NULL); DBG("_javanet_connect(): Entered _javanet_connect\n"); @@ -459,68 +537,66 @@ _javanet_connect(JNIEnv *env, jobject this, jobject addr, jint port) DBG("_javanet_connect(): Got native fd\n"); /* Connect up */ - memset(&si, 0, sizeof(struct sockaddr_in)); - si.sin_family = AF_INET; - si.sin_addr.s_addr = netaddr; - si.sin_port = htons(((short)port)); - - rc = connect(fd, (struct sockaddr *) &si, sizeof(struct sockaddr_in)); - if (rc == -1) - { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; } + TARGET_NATIVE_NETWORK_SOCKET_CONNECT(fd,netaddr,port,result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } DBG("_javanet_connect(): Connected successfully\n"); /* Populate instance variables */ - addrlen = sizeof(struct sockaddr_in); - rc = getsockname(fd, (struct sockaddr *) &si, &addrlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO(fd,local_address,local_port,result); + if (result != TARGET_NATIVE_OK) { - close(fd); - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return; } _javanet_create_localfd(env, this); if ((*env)->ExceptionOccurred(env)) { - close(fd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); return; } DBG("_javanet_connect(): Created fd\n"); _javanet_set_int_field(env, this, "java/net/SocketImpl", "localport", - ntohs(si.sin_port)); + local_port); if ((*env)->ExceptionOccurred(env)) { - close(fd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); return; } DBG("_javanet_connect(): Set the local port\n"); - addrlen = sizeof(struct sockaddr_in); - rc = getpeername(fd, (struct sockaddr *) &si, &addrlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO(fd,remote_address,remote_port,result); + if (result != TARGET_NATIVE_OK) { - close(fd); - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return; } - _javanet_set_remhost_addr(env, this, addr); + _javanet_set_remhost(env, this, remote_address); if ((*env)->ExceptionOccurred(env)) { - close(fd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); return; } DBG("_javanet_connect(): Set the remote host\n"); _javanet_set_int_field(env, this, "java/net/SocketImpl", "port", - ntohs(si.sin_port)); + remote_port); if ((*env)->ExceptionOccurred(env)) { - close(fd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(fd,result); return; } DBG("_javanet_connect(): Set the remote port\n"); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -533,13 +609,18 @@ _javanet_connect(JNIEnv *env, jobject this, jobject addr, jint port) void _javanet_bind(JNIEnv *env, jobject this, jobject addr, jint port, int stream) { - jclass cls; - jmethodID mid; +#ifndef WITHOUT_NETWORK + jclass cls; + jmethodID mid; jbyteArray arr = 0; - jbyte *octets; - jint fd; - struct sockaddr_in si; - int namelen; + jbyte *octets; + jint fd; + int tmpaddr; + int result; + int local_address,local_port; + + assert(env!=NULL); + assert((*env)!=NULL); DBG("_javanet_bind(): Entering native bind()\n"); @@ -577,39 +658,48 @@ _javanet_bind(JNIEnv *env, jobject this, jobject addr, jint port, int stream) } DBG("_javanet_bind(): Past native_fd lookup\n"); +// NYI ??? _javanet_set_option (env, this, SOCKOPT_SO_REUSEADDR, _javanet_create_boolean (env, JNI_TRUE)); /* Bind the socket */ - memset(&si, 0, sizeof(struct sockaddr_in)); - - si.sin_family = AF_INET; - si.sin_addr.s_addr = *(int *)octets; /* Already in network byte order */ - if (port == -1) - si.sin_port = 0; - else - si.sin_port = htons(port); + 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); (*env)->ReleaseByteArrayElements(env, arr, octets, 0); - if (bind(fd, (struct sockaddr *) &si, sizeof(struct sockaddr_in)) == -1) - { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; } + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } DBG("_javanet_bind(): Past bind\n"); /* Update instance variables, specifically the local port number */ - namelen = sizeof(struct sockaddr_in); - getsockname(fd, (struct sockaddr *) &si, &namelen); + TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO(fd,local_address,local_port,result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } if (stream) _javanet_set_int_field(env, this, "java/net/SocketImpl", - "localport", ntohs(si.sin_port)); + "localport", local_port); else _javanet_set_int_field(env, this, "java/net/DatagramSocketImpl", - "localPort", ntohs(si.sin_port)); + "localPort", local_port); DBG("_javanet_bind(): Past update port number\n"); return; +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -621,7 +711,12 @@ _javanet_bind(JNIEnv *env, jobject this, jobject addr, jint port, int stream) void _javanet_listen(JNIEnv *env, jobject this, jint queuelen) { - int fd = -1, rc; +#ifndef WITHOUT_NETWORK + int fd; + int result; + + assert(env!=NULL); + assert((*env)!=NULL); /* Get the real file descriptor */ fd = _javanet_get_int_field(env, this, "native_fd"); @@ -633,11 +728,14 @@ _javanet_listen(JNIEnv *env, jobject this, jint queuelen) } /* Start listening */ - rc = listen(fd, queuelen); - if (rc == -1) - { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; } - - return; + TARGET_NATIVE_NETWORK_SOCKET_LISTEN(fd,queuelen,result); + if (result!=TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -649,8 +747,14 @@ _javanet_listen(JNIEnv *env, jobject this, jint queuelen) void _javanet_accept(JNIEnv *env, jobject this, jobject impl) { - int fd = -1, newfd, addrlen, rc; - struct sockaddr_in si; +#ifndef WITHOUT_NETWORK + int fd, newfd; + int result; + int local_address,local_port; + int remote_address,remote_port; + + assert(env!=NULL); + assert((*env)!=NULL); /* Get the real file descriptor */ fd = _javanet_get_int_field(env, this, "native_fd"); @@ -662,13 +766,13 @@ _javanet_accept(JNIEnv *env, jobject this, jobject impl) } /* Accept the connection */ - addrlen = sizeof(struct sockaddr_in); - memset(&si, 0, addrlen); - /******* Do we need to look for EINTR? */ - newfd = accept(fd, (struct sockaddr *) &si, &addrlen); - if (newfd == -1) - { JCL_ThrowException(env, IO_EXCEPTION, "Internal error: _javanet_accept(): "); return; } + TARGET_NATIVE_NETWORK_SOCKET_ACCEPT(fd,newfd,result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: _javanet_accept(): "); + return; + } /* Populate instance variables */ _javanet_set_int_field(env, impl, "java/net/PlainSocketImpl", "native_fd", @@ -676,56 +780,57 @@ _javanet_accept(JNIEnv *env, jobject this, jobject impl) if ((*env)->ExceptionOccurred(env)) { - close(newfd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); return; } - rc = getsockname(newfd, (struct sockaddr *) &si, &addrlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO(newfd,local_address,local_port,result); + if (result!=TARGET_NATIVE_OK) { - close(newfd); - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return; } _javanet_create_localfd(env, impl); if ((*env)->ExceptionOccurred(env)) { - close(newfd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); return; } _javanet_set_int_field(env, impl, "java/net/SocketImpl", "localport", - ntohs(si.sin_port)); + local_port); if ((*env)->ExceptionOccurred(env)) { - close(newfd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); return; } - addrlen = sizeof(struct sockaddr_in); - rc = getpeername(newfd, (struct sockaddr *) &si, &addrlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO(newfd,remote_address,remote_port,result); + if (result!=TARGET_NATIVE_OK) { - close(newfd); - JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return; } - _javanet_set_remhost(env, impl, ntohl(si.sin_addr.s_addr)); + _javanet_set_remhost(env, impl, remote_address); if ((*env)->ExceptionOccurred(env)) { - close(newfd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); return; } _javanet_set_int_field(env, impl, "java/net/SocketImpl", "port", - ntohs(si.sin_port)); + remote_port); if ((*env)->ExceptionOccurred(env)) { - close(newfd); + TARGET_NATIVE_NETWORK_SOCKET_CLOSE(newfd,result); return; } +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -748,9 +853,14 @@ int _javanet_recvfrom(JNIEnv *env, jobject this, jarray buf, int offset, int len, int *addr, int *port) { - int fd, rc, si_len; +#ifndef WITHOUT_NETWORK + int fd; jbyte *p; - struct sockaddr_in si; + int from_address,from_port; + int received_bytes; + + assert(env!=NULL); + assert((*env)!=NULL); DBG("_javanet_recvfrom(): Entered _javanet_recvfrom\n"); @@ -772,37 +882,40 @@ _javanet_recvfrom(JNIEnv *env, jobject this, jarray buf, int offset, int len, DBG("_javanet_recvfrom(): Got buffer\n"); /* Read the data */ - for (;;) - { - if (addr == NULL) - rc = recv(fd, p + offset, len, 0); - else - { - memset(&si, 0, sizeof(struct sockaddr_in)); - si_len = sizeof(struct sockaddr_in); - rc = recvfrom(fd, p + offset, len, 0, (struct sockaddr *) &si, &si_len); - } - - if ((rc == -1) && (errno == EINTR)) - continue; - - break; - } + from_address = 0; + from_port = 0; + do { + if (addr != NULL) + { + TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT(fd,p+offset,len,from_address,from_port,received_bytes); + } + else + { + TARGET_NATIVE_NETWORK_SOCKET_RECEIVE(fd,p+offset,len,received_bytes); + } + } while ((received_bytes == -1) && + (TARGET_NATIVE_LAST_ERROR() == TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL) + ); (*env)->ReleaseByteArrayElements(env, buf, p, 0); - if (rc == -1) - { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return 0; } + if (received_bytes == -1) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return 0; + } /* Handle return addr case */ - if (addr) + if (addr != NULL) { - *addr = si.sin_addr.s_addr; - if (port) - *port = si.sin_port; + (*addr) = from_address; + if (port != NULL) + (*port) = from_port; } - return(rc); + return(received_bytes); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -820,9 +933,13 @@ void _javanet_sendto(JNIEnv *env, jobject this, jarray buf, int offset, int len, int addr, int port) { - int fd, rc; +#ifndef WITHOUT_NETWORK + int fd; jbyte *p; - struct sockaddr_in si; + int bytes_sent; + + assert(env!=NULL); + assert((*env)!=NULL); /* Get the real file descriptor */ fd = _javanet_get_int_field(env, this, "native_fd"); @@ -842,26 +959,24 @@ _javanet_sendto(JNIEnv *env, jobject this, jarray buf, int offset, int len, if (addr == 0) { DBG("_javanet_sendto(): Sending....\n"); - rc = send(fd, p + offset, len, 0); + TARGET_NATIVE_NETWORK_SOCKET_SEND(fd,p+offset,len,bytes_sent); } else { - memset(&si, 0, sizeof(struct sockaddr_in)); - si.sin_family = AF_INET; - si.sin_addr.s_addr = addr; - si.sin_port = (unsigned short)port; - DBG("_javanet_sendto(): Sending....\n"); - rc = sendto(fd, p + offset, len, 0, (struct sockaddr *) &si, sizeof(struct sockaddr_in)); + TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT(fd,p+offset,len,addr,port,bytes_sent); } (*env)->ReleaseByteArrayElements(env, buf, p, 0); /***** Do we need to check EINTR? */ - if (rc == -1) - { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; } - - return; + if (bytes_sent<0) + { + JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -872,12 +987,16 @@ _javanet_sendto(JNIEnv *env, jobject this, jarray buf, int offset, int len, void _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) { - int fd = -1, rc; - int optval, sockopt; - jclass cls; +#ifndef WITHOUT_NETWORK + int fd; + int optval; + jclass cls; jmethodID mid; - struct linger linger; - struct sockaddr_in si; + int address; + int result; + + assert(env!=NULL); + assert((*env)!=NULL); /* Get the real file descriptor */ fd = _javanet_get_int_field(env, this, "native_fd"); @@ -894,6 +1013,7 @@ _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 */ @@ -908,20 +1028,18 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) if ((*env)->ExceptionOccurred(env)) return; - rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY(fd,optval,result); break; /* SO_LINGER case. If val is a boolean, then it will always be set to false indicating disable linger, otherwise it will be an integer that contains the linger value */ case SOCKOPT_SO_LINGER: - memset(&linger, 0, sizeof(struct linger)); - mid = (*env)->GetMethodID(env, cls, "booleanValue", "()Z"); if (mid) { /* We are disabling linger */ - linger.l_onoff = 0; + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER(fd,1,0,result); } else { @@ -935,14 +1053,12 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) { JCL_ThrowException(env, IO_EXCEPTION, "Internal error: _javanet_set_option()"); return; } - linger.l_linger = (*env)->CallIntMethod(env, val, mid); + optval= (*env)->CallIntMethod(env, val, mid); if ((*env)->ExceptionOccurred(env)) return; - - linger.l_onoff = 1; + + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER(fd,0,optval,result); } - rc = setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, - sizeof(struct linger)); break; /* SO_TIMEOUT case. Val will be an integer with the new value */ @@ -958,7 +1074,7 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) if ((*env)->ExceptionOccurred(env)) return; - rc = setsockopt(fd, SOL_SOCKET, SO_TIMEOUT, &optval, sizeof(int)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_TIMEOUT(fd,optval,result); #endif return; // ignore errors and do not throw an exception break; @@ -975,11 +1091,9 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) return; if (option_id == SOCKOPT_SO_SNDBUF) - sockopt = SO_SNDBUF; + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_SNDBUF(fd,optval,result); else - sockopt = SO_RCVBUF; - - rc = setsockopt(fd, SOL_SOCKET, sockopt, &optval, sizeof(int)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_RCDBUF(fd,optval,result); break; /* TTL case. Val with be an Integer with the new time to live value */ @@ -993,20 +1107,17 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) if ((*env)->ExceptionOccurred(env)) return; - rc = setsockopt(fd, IPPROTO_IP, IP_TTL, &optval, sizeof(int)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_TTL(fd,optval,result); break; /* Multicast Interface case - val is InetAddress object */ case SOCKOPT_IP_MULTICAST_IF: - memset(&si, 0, sizeof(struct sockaddr_in)); - si.sin_family = AF_INET; - si.sin_addr.s_addr = _javanet_get_netaddr(env, val); + address = _javanet_get_netaddr(env, val); if ((*env)->ExceptionOccurred(env)) return; - rc = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &si, - sizeof(struct sockaddr_in)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_MULTICAST_IF(fd,address,result); break; case SOCKOPT_SO_REUSEADDR: @@ -1020,8 +1131,7 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) if ((*env)->ExceptionOccurred(env)) return; - rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, - sizeof(int)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_REUSE_ADDRESS(fd,optval,result); break; case SOCKOPT_SO_KEEPALIVE: @@ -1035,8 +1145,7 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) if ((*env)->ExceptionOccurred(env)) return; - rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, - sizeof(int)); + TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_KEEP_ALIVE(fd,optval,result); break; case SOCKOPT_SO_BINDADDR: @@ -1049,10 +1158,13 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) } /* Check to see if above operations succeeded */ - if (rc == -1) - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); - - return; + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } /*************************************************************************/ @@ -1063,10 +1175,14 @@ _javanet_set_option(JNIEnv *env, jobject this, jint option_id, jobject val) jobject _javanet_get_option(JNIEnv *env, jobject this, jint option_id) { - int fd = -1, rc; - int optval, optlen, sockopt; - struct linger linger; - struct sockaddr_in si; +#ifndef WITHOUT_NETWORK + int fd; + int flag,optval; + int address; + int result; + + assert(env!=NULL); + assert((*env)!=NULL); /* Get the real file descriptor */ fd = _javanet_get_int_field(env, this, "native_fd"); @@ -1082,11 +1198,10 @@ _javanet_get_option(JNIEnv *env, jobject this, jint option_id) { /* TCP_NODELAY case. Return a Boolean indicating on or off */ case SOCKOPT_TCP_NODELAY: - optlen = sizeof(optval); - rc = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, &optlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_TCP_NODELAY(fd,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } @@ -1100,18 +1215,15 @@ _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: - memset(&linger, 0, sizeof(struct linger)); - optlen = sizeof(struct linger); - - rc = getsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, &optlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_LINGER(fd,flag,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } - if (linger.l_onoff) - return(_javanet_create_integer(env, linger.l_linger)); + if (optval) + return(_javanet_create_integer(env, JNI_TRUE)); else return(_javanet_create_boolean(env, JNI_FALSE)); @@ -1120,37 +1232,30 @@ _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 - optlen = sizeof(int); - - rc = getsockopt(fd, SOL_SOCKET, SO_TIMEOUT, &optval, &optlen); -#else - JCL_ThrowException(env, SOCKET_EXCEPTION, - "SO_TIMEOUT not supported on this platform"); - return(0); -#endif /* not SO_TIMEOUT */ - - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_TIMEOUT(fd,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); 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: - optlen = sizeof(int); if (option_id == SOCKOPT_SO_SNDBUF) - sockopt = SO_SNDBUF; + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_SNDBUF(fd,optval,result); else - sockopt = SO_RCVBUF; - - rc = getsockopt(fd, SOL_SOCKET, sockopt, &optval, &optlen); - - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_RCDBUF(fd,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } @@ -1159,12 +1264,10 @@ _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: - optlen = sizeof(int); - - rc = getsockopt(fd, IPPROTO_IP, IP_TTL, &optval, &optlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_TTL(fd,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } @@ -1173,38 +1276,32 @@ _javanet_get_option(JNIEnv *env, jobject this, jint option_id) /* Multicast interface case */ case SOCKOPT_IP_MULTICAST_IF: - memset(&si, 0, sizeof(struct sockaddr_in)); - optlen = sizeof(struct sockaddr_in); - - rc = getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &si, &optlen); - if (rc == -1) - { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); - return(0); - } + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_MULTICAST_IF(fd,address,result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); + return(0); + } - return(_javanet_create_inetaddress(env, ntohl(si.sin_addr.s_addr))); - break; + return(_javanet_create_inetaddress(env, address)); + break; case SOCKOPT_SO_BINDADDR: - memset(&si, 0, sizeof(struct sockaddr_in)); - optlen = sizeof(struct sockaddr_in); - rc = getsockname(fd, (struct sockaddr *) &si, &optlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BIND_ADDRESS(fd,address,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } - return(_javanet_create_inetaddress(env, ntohl(si.sin_addr.s_addr))); + return(_javanet_create_inetaddress(env, address)); break; case SOCKOPT_SO_REUSEADDR: - optlen = sizeof(int); - rc = getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, &optlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_REUSE_ADDRESS(fd,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } @@ -1216,11 +1313,10 @@ _javanet_get_option(JNIEnv *env, jobject this, jint option_id) break; case SOCKOPT_SO_KEEPALIVE: - optlen = sizeof(int); - rc = getsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, &optlen); - if (rc == -1) + TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_KEEP_ALIVE(fd,optval,result); + if (result != TARGET_NATIVE_OK) { - JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno)); + JCL_ThrowException(env, SOCKET_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING()); return(0); } @@ -1237,5 +1333,8 @@ _javanet_get_option(JNIEnv *env, jobject this, jint option_id) } return(0); +#else /* not WITHOUT_NETWORK */ +#endif /* not WITHOUT_NETWORK */ } +/* end of file */ |