Allow setTrafficClass on streams socket niosocketimpl-branch
authoralanb
Sat, 16 Feb 2019 19:53:43 +0000
branchniosocketimpl-branch
changeset 57187 056911ad3ee7
parent 57186 997178749c87
child 57188 1f2101ee432d
Allow setTrafficClass on streams socket
src/java.base/share/classes/java/net/Socket.java
src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
src/java.base/unix/native/libnio/ch/Net.c
--- a/src/java.base/share/classes/java/net/Socket.java	Fri Feb 15 18:07:17 2019 +0000
+++ b/src/java.base/share/classes/java/net/Socket.java	Sat Feb 16 19:53:43 2019 +0000
@@ -1423,9 +1423,8 @@
         try {
             getImpl().setOption(SocketOptions.IP_TOS, tc);
         } catch (SocketException se) {
-            // not supported if socket already connected
-            // Solaris returns error in such cases
-            if(!isConnected())
+            // may not be supported to change when socket is connected
+            if (!isConnected())
                 throw se;
         }
     }
@@ -1799,7 +1798,14 @@
      * @since 9
      */
     public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
-        getImpl().setOption(name, value);
+        try {
+            getImpl().setOption(name, value);
+        } catch (SocketException se) {
+            // may not be supported to change when socket is connected
+            if (name != StandardSocketOptions.IP_TOS || !isConnected()) {
+                throw se;
+            }
+        }
         return this;
     }
 
--- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Fri Feb 15 18:07:17 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Sat Feb 16 19:53:43 2019 +0000
@@ -118,6 +118,9 @@
     // used when SO_REUSEADDR is emulated
     private boolean isReuseAddress;
 
+    // cached value of IPV6_TCLASS or IP_TOS socket option
+    private int trafficClass;
+
     // read or accept timeout in millis
     private volatile int timeout;
 
@@ -987,11 +990,9 @@
                 }
                 case IP_TOS: {
                     int i = intValue(value, "IP_TOS");
-                    if (stream && Net.isIPv6Available()) {
-                        // IP_TOS is not specified for stream sockets when IPv6 enabled
-                    } else {
-                        Net.setSocketOption(fd, family(), StandardSocketOptions.IP_TOS, i);
-                    }
+                    var IP_TOS = StandardSocketOptions.IP_TOS;
+                    Net.setSocketOption(fd, family(), IP_TOS, i);
+                    trafficClass = i;
                     break;
                 }
                 case TCP_NODELAY: {
@@ -1001,11 +1002,15 @@
                 }
                 case SO_SNDBUF: {
                     int i = intValue(value, "SO_SNDBUF");
+                    if (i <= 0)
+                        throw new IllegalArgumentException("SO_SNDBUF < 0");
                     Net.setSocketOption(fd, StandardSocketOptions.SO_SNDBUF, i);
                     break;
                 }
                 case SO_RCVBUF: {
                     int i = intValue(value, "SO_RCVBUF");
+                    if (i <= 0)
+                        throw new IllegalArgumentException("SO_RCVBUF < 0");
                     Net.setSocketOption(fd, StandardSocketOptions.SO_RCVBUF, i);
                     break;
                 }
@@ -1078,12 +1083,7 @@
                 case SO_RCVBUF:
                     return Net.getSocketOption(fd, StandardSocketOptions.SO_RCVBUF);
                 case IP_TOS:
-                    if (stream && Net.isIPv6Available()) {
-                        // IP_TOS is not specified for stream sockets when IPv6 enabled
-                        return 0;
-                    } else {
-                        return Net.getSocketOption(fd, family(), StandardSocketOptions.IP_TOS);
-                    }
+                    return trafficClass;
                 case SO_KEEPALIVE:
                     return Net.getSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE);
                 case SO_REUSEPORT:
--- a/src/java.base/unix/native/libnio/ch/Net.c	Fri Feb 15 18:07:17 2019 +0000
+++ b/src/java.base/unix/native/libnio/ch/Net.c	Sat Feb 16 19:53:43 2019 +0000
@@ -830,6 +830,7 @@
             break;
         case EADDRINUSE:  /* Fall through */
         case EADDRNOTAVAIL:
+        case EACCES:
             xn = JNU_JAVANETPKG "BindException";
             break;
         default: