unixdomainchannels: finx to local address handling unixdomainchannels
authormichaelm
Fri, 15 Nov 2019 16:30:39 +0000
branchunixdomainchannels
changeset 59111 31b8ffbfe1b9
parent 59090 fb91b01624be
child 59123 ddb8977e44da
unixdomainchannels: finx to local address handling
src/java.base/unix/native/libnio/ch/Net.c
src/java.base/windows/native/libnio/ch/Net.c
--- a/src/java.base/unix/native/libnio/ch/Net.c	Thu Nov 14 17:43:07 2019 +0000
+++ b/src/java.base/unix/native/libnio/ch/Net.c	Fri Nov 15 16:30:39 2019 +0000
@@ -27,6 +27,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <string.h>
+#include <stddef.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <limits.h>
@@ -51,7 +52,16 @@
 extern jmethodID udsa_ctorID;
 extern jfieldID udsa_pathID;
 
-#define PATHLEN(len) (len - offsetof(struct sockaddr_un, sun_path))
+/* Subtle platform differences in how unnamed sockets (empty path)
+ * are returned from getsockname()
+ */
+#if defined(__solaris__)
+  #define ZERO_PATHLEN(len) (len == 0)
+#elif defined(MACOSX)
+  #define ZERO_PATHLEN(len) (JNI_FALSE)
+#else
+  #define ZERO_PATHLEN(len) (len == offsetof(struct sockaddr_un, sun_path))
+#endif
 
 JNIEXPORT jobject JNICALL
 NET_SockaddrToUnixAddress(JNIEnv *env, struct sockaddr_un *sa, socklen_t len) {
@@ -59,7 +69,7 @@
     if (sa->sun_family == AF_UNIX) {
         char *name;
 
-        if (PATHLEN(len) == 0) {
+        if (ZERO_PATHLEN(len)) {
             name = "";
         } else {
             name = sa->sun_path;
@@ -76,10 +86,6 @@
     jstring path;
     memset(sa, 0, sizeof(struct sockaddr_un));
     sa->sun_family = AF_UNIX;
-    if (uaddr == NULL) {
-        *len = (int)(offsetof(struct sockaddr_un, sun_path));
-        return 0;
-    }
     path = (*env)->GetObjectField(env, uaddr, udsa_pathID);
     jboolean isCopy;
     int ret;
@@ -123,7 +129,7 @@
     int rv = 0;
 
     if (uaddr == NULL)
-        return; /* Rely on implicit bind */
+        return; /* Rely on implicit bind: Unix */
 
     if (NET_UnixSocketAddressToSockaddr(env, uaddr, &sa, &sa_len) != 0)
         return;
--- a/src/java.base/windows/native/libnio/ch/Net.c	Thu Nov 14 17:43:07 2019 +0000
+++ b/src/java.base/windows/native/libnio/ch/Net.c	Fri Nov 15 16:30:39 2019 +0000
@@ -90,20 +90,12 @@
 extern jmethodID udsa_ctorID;
 extern jfieldID udsa_pathID;
 
-#define PATHLEN_UN(len) (len - offsetof(struct sockaddr_un, sun_path))
 
 JNIEXPORT jobject JNICALL
 NET_SockaddrToUnixAddress(JNIEnv *env, struct sockaddr_un *sa, socklen_t len) {
 
     if (sa->sun_family == AF_UNIX) {
-        char *name;
-
-        if (PATHLEN_UN(len) == 0) {
-            name = "";
-        } else {
-            name = sa->sun_path;
-        }
-        jstring nstr = JNU_NewStringPlatform(env, name);
+        jstring nstr = JNU_NewStringPlatform(env, sa->sun_path);
         return (*env)->NewObject(env, udsa_class, udsa_ctorID, nstr);
     }
     return NULL;
@@ -116,6 +108,7 @@
     memset(sa, 0, sizeof(struct sockaddr_un));
     sa->sun_family = AF_UNIX;
     if (uaddr == NULL) {
+ 	/* Do explicit bind on Windows */
         *len = (int)(offsetof(struct sockaddr_un, sun_path));
         return 0;
     }
@@ -159,6 +152,10 @@
     return (int)fd;
 }
 
+/**
+ * Windows does not support auto bind. So, the windows version of NET_UnixSocketAddressToSockaddr
+ * looks out for a null 'uaddr' and handles it specially
+ */
 JNIEXPORT void JNICALL
 Java_sun_nio_ch_Net_unixDomainBind(JNIEnv *env, jclass clazz, jobject fdo, jobject uaddr)
 {
@@ -166,11 +163,6 @@
     int sa_len = 0;
     int rv = 0;
 
-/*
-    if (uaddr == NULL)
-        return;
-*/
-
     if (NET_UnixSocketAddressToSockaddr(env, uaddr, &sa, &sa_len) != 0)
         return;