From d4681b69208654cb72698bef47d0ea1d000a55f3 Mon Sep 17 00:00:00 2001 From: chegar Date: Wed, 27 Jan 2010 16:11:53 +0000 Subject: [PATCH] 6905552: libnet/nio portability issues Reviewed-by: alanb --- src/share/native/java/net/net_util.c | 5 ++- .../native/java/net/Inet4AddressImpl.c | 5 --- .../native/java/net/Inet6AddressImpl.c | 7 ---- .../native/java/net/NetworkInterface.c | 11 ++++++ .../native/java/net/PlainDatagramSocketImpl.c | 38 ++++++++++++++++--- src/solaris/native/java/net/net_util_md.c | 3 +- src/solaris/native/java/net/net_util_md.h | 2 +- src/solaris/native/sun/net/spi/SdpProvider.c | 4 ++ src/solaris/native/sun/nio/ch/Net.c | 19 +++++++++- src/solaris/native/sun/nio/ch/SctpNet.c | 8 +++- 10 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/share/native/java/net/net_util.c b/src/share/native/java/net/net_util.c index 5634e736a..c2464b241 100644 --- a/src/share/native/java/net/net_util.c +++ b/src/share/native/java/net/net_util.c @@ -162,10 +162,11 @@ NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) { JNIEXPORT jint JNICALL NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj) { - jint family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? - AF_INET : AF_INET6; + jint family = AF_INET; #ifdef AF_INET6 + family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? + AF_INET : AF_INET6; if (him->sa_family == AF_INET6) { #ifdef WIN32 struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; diff --git a/src/solaris/native/java/net/Inet4AddressImpl.c b/src/solaris/native/java/net/Inet4AddressImpl.c index 5621961f3..ab6c7ae2d 100644 --- a/src/solaris/native/java/net/Inet4AddressImpl.c +++ b/src/solaris/native/java/net/Inet4AddressImpl.c @@ -46,11 +46,6 @@ #define HENT_BUF_SIZE 1024 #define BIG_HENT_BUF_SIZE 10240 /* a jumbo-sized one */ -#ifndef __GLIBC__ -/* gethostname() is in libc.so but I can't find a header file for it */ -extern int gethostname(char *buf, int buf_len); -#endif - /************************************************************************ * Inet4AddressImpl */ diff --git a/src/solaris/native/java/net/Inet6AddressImpl.c b/src/solaris/native/java/net/Inet6AddressImpl.c index f9dcfef4f..2d5fff291 100644 --- a/src/solaris/native/java/net/Inet6AddressImpl.c +++ b/src/solaris/native/java/net/Inet6AddressImpl.c @@ -49,10 +49,6 @@ #define NI_MAXHOST 1025 #endif -#ifndef __GLIBC__ -/* gethostname() is in libc.so but I can't find a header file for it */ -extern int gethostname(char *buf, int buf_len); -#endif /************************************************************************ * Inet6AddressImpl @@ -360,8 +356,6 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, } } -#endif /* AF_INET6 */ - cleanupAndReturn: { struct addrinfo *iterator, *tmp; @@ -374,7 +368,6 @@ cleanupAndReturn: JNU_ReleaseStringPlatformChars(env, host, hostname); } -#ifdef AF_INET6 if (NET_addrtransAvailable()) (*freeaddrinfo_ptr)(res); #endif /* AF_INET6 */ diff --git a/src/solaris/native/java/net/NetworkInterface.c b/src/solaris/native/java/net/NetworkInterface.c index 9580c8164..dc2bccc7f 100644 --- a/src/solaris/native/java/net/NetworkInterface.c +++ b/src/solaris/native/java/net/NetworkInterface.c @@ -253,8 +253,12 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0 (JNIEnv *env, jclass cls, jobject iaObj) { netif *ifs, *curr; +#ifdef AF_INET6 int family = (*env)->GetIntField(env, iaObj, ni_iafamilyID) == IPv4? AF_INET : AF_INET6; +#else + int family = AF_INET; +#endif jobject obj = NULL; jboolean match = JNI_FALSE; @@ -1528,6 +1532,7 @@ JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclas strcpy((caddr_t)&(lifr.lifr_name), name_utf); if (ioctl(sock, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) { ret = lifr.lifr_mtu; +#ifdef AF_INET6 } else { /* Try wIth an IPv6 socket in case the interface has only IPv6 addresses assigned to it */ close(sock); @@ -1547,6 +1552,12 @@ JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclas "IOCTL failed"); } } +#else + } else { + NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", + "IOCTL failed"); + } +#endif #endif close(sock); } diff --git a/src/solaris/native/java/net/PlainDatagramSocketImpl.c b/src/solaris/native/java/net/PlainDatagramSocketImpl.c index dc804be45..0fb06a9f7 100644 --- a/src/solaris/native/java/net/PlainDatagramSocketImpl.c +++ b/src/solaris/native/java/net/PlainDatagramSocketImpl.c @@ -605,8 +605,12 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this, } iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); +#ifdef AF_INET6 family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? AF_INET : AF_INET6; +#else + family = AF_INET; +#endif if (family == AF_INET) { /* this api can't handle IPV6 addresses */ int address = (*env)->GetIntField(env, iaObj, ia_addressID); (*env)->SetIntField(env, addressObj, ia_addressID, address); @@ -812,9 +816,9 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this, jboolean retry; #ifdef __linux__ jboolean connected = JNI_FALSE; - jobject connectedAddress; - jint connectedPort; - jlong prevTime; + jobject connectedAddress = NULL; + jint connectedPort = 0; + jlong prevTime = 0; #endif if (IS_NULL(fdObj)) { @@ -1186,6 +1190,7 @@ static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject val * Set outgoing multicast interface designated by a NetworkInterface. * Throw exception if failed. */ +#ifdef AF_INET6 static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) { static jfieldID ni_indexID; int index; @@ -1222,6 +1227,7 @@ static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject val } #endif } +#endif /* AF_INET6 */ /* * Set outgoing multicast interface designated by an InetAddress. @@ -1251,6 +1257,7 @@ static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject v * Set outgoing multicast interface designated by an InetAddress. * Throw exception if failed. */ +#ifdef AF_INET6 static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) { static jclass ni_class; if (ni_class == NULL) { @@ -1272,6 +1279,7 @@ static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject v mcast_set_if_by_if_v6(env, this, fd, value); } +#endif /* * Sets the multicast interface. @@ -1307,6 +1315,7 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, /* * value is an InetAddress. */ +#ifdef AF_INET6 #ifdef __solaris__ if (ipv6_available()) { mcast_set_if_by_addr_v6(env, this, fd, value); @@ -1320,12 +1329,16 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, mcast_set_if_by_addr_v6(env, this, fd, value); } #endif +#else + mcast_set_if_by_addr_v4(env, this, fd, value); +#endif /* AF_INET6 */ } if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) { /* * value is a NetworkInterface. */ +#ifdef AF_INET6 #ifdef __solaris__ if (ipv6_available()) { mcast_set_if_by_if_v6(env, this, fd, value); @@ -1339,6 +1352,9 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, mcast_set_if_by_if_v6(env, this, fd, value); } #endif +#else + mcast_set_if_by_if_v4(env, this, fd, value); +#endif /* AF_INET6 */ } } @@ -1368,6 +1384,7 @@ static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value) /* * Enable/disable local loopback of multicast datagrams. */ +#ifdef AF_INET6 static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) { jclass cls; jfieldID fid; @@ -1397,12 +1414,14 @@ static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) } #endif } +#endif /* AF_INET6 */ /* * Sets the multicast loopback mode. */ static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd, jint opt, jobject value) { +#ifdef AF_INET6 #ifdef __solaris__ if (ipv6_available()) { mcast_set_loop_v6(env, this, fd, value); @@ -1416,6 +1435,9 @@ static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd, mcast_set_loop_v6(env, this, fd, value); } #endif +#else + mcast_set_loop_v4(env, this, fd, value); +#endif /* AF_INET6 */ } /* @@ -1838,7 +1860,7 @@ Java_java_net_PlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this, if (opt == java_net_SocketOptions_SO_BINDADDR) { /* find out local IP address */ SOCKADDR him; - int len = 0; + socklen_t len = 0; int port; jobject iaObj; @@ -1941,6 +1963,7 @@ static void setTTL(JNIEnv *env, int fd, jint ttl) { /* * Set hops limit for a socket. Throw exception if failed. */ +#ifdef AF_INET6 static void setHopLimit(JNIEnv *env, int fd, jint ttl) { int ittl = (int)ttl; if (JVM_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, @@ -1949,6 +1972,7 @@ static void setHopLimit(JNIEnv *env, int fd, jint ttl) { "Error setting socket option"); } } +#endif /* * Class: java_net_PlainDatagramSocketImpl @@ -1971,6 +1995,7 @@ Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this, fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); } /* setsockopt to be correct ttl */ +#ifdef AF_INET6 #ifdef __solaris__ if (ipv6_available()) { setHopLimit(env, fd, ttl); @@ -1986,7 +2011,10 @@ Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this, (*env)->SetIntField(env, this, pdsi_ttlID, ttl); } } -#endif +#endif // __linux__ +#else + setTTL(env, fd, ttl); +#endif /* AF_INET6 */ } /* diff --git a/src/solaris/native/java/net/net_util_md.c b/src/solaris/native/java/net/net_util_md.c index cfc86a884..620a70d63 100644 --- a/src/solaris/native/java/net/net_util_md.c +++ b/src/solaris/native/java/net/net_util_md.c @@ -319,8 +319,6 @@ jint IPv6_supported() #endif /* __solaris */ -#endif /* AF_INET6 */ - /* * OK we may have the stack available in the kernel, * we should also check if the APIs are available. @@ -354,6 +352,7 @@ jint IPv6_supported() close(fd); return JNI_TRUE; +#endif /* AF_INET6 */ } void diff --git a/src/solaris/native/java/net/net_util_md.h b/src/solaris/native/java/net/net_util_md.h index 3e581781a..1614c7ae3 100644 --- a/src/solaris/native/java/net/net_util_md.h +++ b/src/solaris/native/java/net/net_util_md.h @@ -133,7 +133,7 @@ extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout); #else -#define SOCKADDR union { struct sockaddr_in him4 } +#define SOCKADDR union { struct sockaddr_in him4; } #define SOCKADDR_LEN sizeof(SOCKADDR) #endif diff --git a/src/solaris/native/sun/net/spi/SdpProvider.c b/src/solaris/native/sun/net/spi/SdpProvider.c index 00d7f4ba6..26b5f16f2 100644 --- a/src/solaris/native/sun/net/spi/SdpProvider.c +++ b/src/solaris/native/sun/net/spi/SdpProvider.c @@ -44,7 +44,11 @@ JNIEXPORT void JNICALL Java_sun_net_spi_SdpProvider_convert(JNIEnv *env, jclass cls, jint fd) { #ifdef PROTO_SDP +#ifdef AF_INET6 int domain = ipv6_available() ? AF_INET6 : AF_INET; +#else + int domain = AF_INET; +#endif int s = socket(domain, SOCK_STREAM, PROTO_SDP); if (s < 0) { JNU_ThrowIOExceptionWithLastError(env, "socket"); diff --git a/src/solaris/native/sun/nio/ch/Net.c b/src/solaris/native/sun/nio/ch/Net.c index 79031de6b..a386c1024 100644 --- a/src/solaris/native/sun/nio/ch/Net.c +++ b/src/solaris/native/sun/nio/ch/Net.c @@ -124,6 +124,7 @@ struct my_group_source_req { * Copy IPv6 group, interface index, and IPv6 source address * into group_source_req structure. */ +#ifdef AF_INET6 static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index, jbyteArray source, struct my_group_source_req* req) { @@ -139,7 +140,7 @@ static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index, sin6->sin6_family = AF_INET6; COPY_INET6_ADDRESS(env, source, (jbyte*)&(sin6->sin6_addr)); } - +#endif JNIEXPORT void JNICALL Java_sun_nio_ch_Net_initIDs(JNIEnv *env, jclass clazz) @@ -159,7 +160,11 @@ Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6, { int fd; int type = (stream ? SOCK_STREAM : SOCK_DGRAM); +#ifdef AF_INET6 int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET; +#else + int domain = AF_INET; +#endif fd = socket(domain, type, 0); if (fd < 0) { @@ -176,7 +181,7 @@ Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6, return -1; } } -#ifdef __linux__ +#if defined(__linux__) && defined(AF_INET6) /* By default, Linux uses the route default */ if (domain == AF_INET6 && type == SOCK_DGRAM) { int arg = 1; @@ -424,6 +429,7 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo, jbyteArray group, jint index, jbyteArray source) { +#ifdef AF_INET6 struct ipv6_mreq mreq6; struct my_group_source_req req; int opt, n, optlen; @@ -454,12 +460,17 @@ Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobjec handleSocketError(env, errno); } return 0; +#else + JNU_ThrowInternalError(env, "Should not get here"); + return IOS_THROWN; +#endif /* AF_INET6 */ } JNIEXPORT jint JNICALL Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo, jbyteArray group, jint index, jbyteArray source) { +#ifdef AF_INET6 struct my_group_source_req req; int n; int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE; @@ -474,6 +485,10 @@ Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, j handleSocketError(env, errno); } return 0; +#else + JNU_ThrowInternalError(env, "Should not get here"); + return IOS_THROWN; +#endif } JNIEXPORT void JNICALL diff --git a/src/solaris/native/sun/nio/ch/SctpNet.c b/src/solaris/native/sun/nio/ch/SctpNet.c index 61d7764d2..3f61c56e3 100644 --- a/src/solaris/native/sun/nio/ch/SctpNet.c +++ b/src/solaris/native/sun/nio/ch/SctpNet.c @@ -168,14 +168,18 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_SctpNet_socket0 (JNIEnv *env, jclass klass, jboolean oneToOne) { int fd; struct sctp_event_subscribe event; +#ifdef AF_INET6 + int domain = ipv6_available() ? AF_INET6 : AF_INET; +#else + int domain = AF_INET; +#endif /* Try to load the socket API extension functions */ if (!funcsLoaded && !loadSocketExtensionFuncs(env)) { return 0; } - fd = socket(ipv6_available() ? AF_INET6 : AF_INET, - (oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP); + fd = socket(domain, (oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP); if (fd < 0) { return handleSocketError(env, errno); -- GitLab