6905552: libnet/nio portability issues
authorchegar
Wed, 27 Jan 2010 16:11:53 +0000
changeset 4814 dbf72872f8d2
parent 4813 654ff751c112
child 4815 228652bbd0b9
6905552: libnet/nio portability issues Reviewed-by: alanb
jdk/src/share/native/java/net/net_util.c
jdk/src/solaris/native/java/net/Inet4AddressImpl.c
jdk/src/solaris/native/java/net/Inet6AddressImpl.c
jdk/src/solaris/native/java/net/NetworkInterface.c
jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c
jdk/src/solaris/native/java/net/net_util_md.c
jdk/src/solaris/native/java/net/net_util_md.h
jdk/src/solaris/native/sun/net/spi/SdpProvider.c
jdk/src/solaris/native/sun/nio/ch/Net.c
jdk/src/solaris/native/sun/nio/ch/SctpNet.c
--- a/jdk/src/share/native/java/net/net_util.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/share/native/java/net/net_util.c	Wed Jan 27 16:11:53 2010 +0000
@@ -162,10 +162,11 @@
 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;
--- a/jdk/src/solaris/native/java/net/Inet4AddressImpl.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/java/net/Inet4AddressImpl.c	Wed Jan 27 16:11:53 2010 +0000
@@ -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
  */
--- a/jdk/src/solaris/native/java/net/Inet6AddressImpl.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/java/net/Inet6AddressImpl.c	Wed Jan 27 16:11:53 2010 +0000
@@ -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 @@
         }
     }
 
-#endif /* AF_INET6 */
-
 cleanupAndReturn:
     {
         struct addrinfo *iterator, *tmp;
@@ -374,7 +368,6 @@
         JNU_ReleaseStringPlatformChars(env, host, hostname);
     }
 
-#ifdef AF_INET6
     if (NET_addrtransAvailable())
         (*freeaddrinfo_ptr)(res);
 #endif /* AF_INET6 */
--- a/jdk/src/solaris/native/java/net/NetworkInterface.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/java/net/NetworkInterface.c	Wed Jan 27 16:11:53 2010 +0000
@@ -253,8 +253,12 @@
     (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 @@
     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 @@
                                      "IOCTL failed");
       }
     }
+#else
+    } else {
+        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
+                                     "IOCTL failed");
+    }
+#endif
 #endif
     close(sock);
   }
--- a/jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c	Wed Jan 27 16:11:53 2010 +0000
@@ -605,8 +605,12 @@
     }
 
     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 @@
     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 @@
  * 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 @@
     }
 #endif
 }
+#endif /* AF_INET6 */
 
 /*
  * Set outgoing multicast interface designated by an InetAddress.
@@ -1251,6 +1257,7 @@
  * 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 @@
 
     mcast_set_if_by_if_v6(env, this, fd, value);
 }
+#endif
 
 /*
  * Sets the multicast interface.
@@ -1307,6 +1315,7 @@
         /*
          * 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 @@
             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 @@
             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 @@
 /*
  * 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 @@
     }
 #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 @@
         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 @@
     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 @@
 /*
  * 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 @@
                        "Error setting socket option");
     }
 }
+#endif
 
 /*
  * Class:     java_net_PlainDatagramSocketImpl
@@ -1971,6 +1995,7 @@
         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 @@
             (*env)->SetIntField(env, this, pdsi_ttlID, ttl);
         }
     }
-#endif
+#endif  // __linux__
+#else
+    setTTL(env, fd, ttl);
+#endif  /* AF_INET6 */
 }
 
 /*
--- a/jdk/src/solaris/native/java/net/net_util_md.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/java/net/net_util_md.c	Wed Jan 27 16:11:53 2010 +0000
@@ -319,8 +319,6 @@
 
 #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 @@
 
     close(fd);
     return JNI_TRUE;
+#endif /* AF_INET6 */
 }
 
 void
--- a/jdk/src/solaris/native/java/net/net_util_md.h	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/java/net/net_util_md.h	Wed Jan 27 16:11:53 2010 +0000
@@ -133,7 +133,7 @@
 
 #else
 
-#define SOCKADDR        union { struct sockaddr_in him4 }
+#define SOCKADDR        union { struct sockaddr_in him4; }
 #define SOCKADDR_LEN    sizeof(SOCKADDR)
 
 #endif
--- a/jdk/src/solaris/native/sun/net/spi/SdpProvider.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/sun/net/spi/SdpProvider.c	Wed Jan 27 16:11:53 2010 +0000
@@ -44,7 +44,11 @@
 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");
--- a/jdk/src/solaris/native/sun/nio/ch/Net.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/sun/nio/ch/Net.c	Wed Jan 27 16:11:53 2010 +0000
@@ -124,6 +124,7 @@
  * 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 @@
     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 @@
 {
     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 @@
             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 @@
 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 @@
         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 @@
         handleSocketError(env, errno);
     }
     return 0;
+#else
+    JNU_ThrowInternalError(env, "Should not get here");
+    return IOS_THROWN;
+#endif
 }
 
 JNIEXPORT void JNICALL
--- a/jdk/src/solaris/native/sun/nio/ch/SctpNet.c	Tue Jan 26 11:39:29 2010 +0100
+++ b/jdk/src/solaris/native/sun/nio/ch/SctpNet.c	Wed Jan 27 16:11:53 2010 +0000
@@ -168,14 +168,18 @@
   (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);