jdk/src/share/classes/sun/nio/ch/Net.java
changeset 18192 fa6bd0992104
parent 14342 8435a30053c1
child 18212 22f8c33b0690
--- a/jdk/src/share/classes/sun/nio/ch/Net.java	Thu Mar 14 14:45:55 2013 +0100
+++ b/jdk/src/share/classes/sun/nio/ch/Net.java	Thu Mar 14 13:46:15 2013 -0700
@@ -44,6 +44,34 @@
         }
     };
 
+    // set to true if exclusive binding is on for Windows
+    private static final boolean exclusiveBind;
+
+    static {
+        int availLevel = isExclusiveBindAvailable();
+        if (availLevel >= 0) {
+            String exclBindProp =
+                java.security.AccessController.doPrivileged(
+                    new PrivilegedAction<String>() {
+                        @Override
+                        public String run() {
+                            return System.getProperty(
+                                    "sun.net.useExclusiveBind");
+                        }
+                    });
+            if (exclBindProp != null) {
+                exclusiveBind = exclBindProp.length() == 0 ?
+                        true : Boolean.parseBoolean(exclBindProp);
+            } else if (availLevel == 1) {
+                exclusiveBind = true;
+            } else {
+                exclusiveBind = false;
+            }
+        } else {
+            exclusiveBind = false;
+        }
+    }
+
     // -- Miscellaneous utilities --
 
     private static volatile boolean checkedIPv6 = false;
@@ -61,6 +89,13 @@
     }
 
     /**
+     * Returns true if exclusive binding is on
+     */
+    static boolean useExclusiveBind() {
+        return exclusiveBind;
+    }
+
+    /**
      * Tells whether IPv6 sockets can join IPv4 multicast groups
      */
     static boolean canIPv6SocketJoinIPv4Group() {
@@ -308,6 +343,12 @@
 
     private static native boolean isIPv6Available0();
 
+    /*
+     * Returns 1 for Windows versions that support exclusive binding by default, 0
+     * for those that do not, and -1 for Solaris/Linux/Mac OS
+     */
+    private static native int isExclusiveBindAvailable();
+
     private static native boolean canIPv6SocketJoinIPv4Group0();
 
     private static native boolean canJoin6WithIPv4Group0();
@@ -341,11 +382,12 @@
     {
         boolean preferIPv6 = isIPv6Available() &&
             (family != StandardProtocolFamily.INET);
-        bind0(preferIPv6, fd, addr, port);
+        bind0(fd, preferIPv6, exclusiveBind, addr, port);
     }
 
-    private static native void bind0(boolean preferIPv6, FileDescriptor fd,
-                                     InetAddress addr, int port)
+    private static native void bind0(FileDescriptor fd, boolean preferIPv6,
+                                     boolean useExclBind, InetAddress addr,
+                                     int port)
         throws IOException;
 
     static native void listen(FileDescriptor fd, int backlog) throws IOException;