diff options
Diffstat (limited to 'openjdk/sun/nio/ch/Net.java')
-rw-r--r-- | openjdk/sun/nio/ch/Net.java | 107 |
1 files changed, 104 insertions, 3 deletions
diff --git a/openjdk/sun/nio/ch/Net.java b/openjdk/sun/nio/ch/Net.java index 71ae5436..d9ab0f0f 100644 --- a/openjdk/sun/nio/ch/Net.java +++ b/openjdk/sun/nio/ch/Net.java @@ -31,6 +31,7 @@ import java.nio.channels.*; import java.util.*; import java.security.AccessController; import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; class Net { // package-private @@ -44,6 +45,40 @@ class Net { // package-private } }; + // Value of jdk.net.revealLocalAddress + private static boolean revealLocalAddress; + + // True if jdk.net.revealLocalAddress had been read + private static volatile boolean propRevealLocalAddress; + + // 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 +96,13 @@ class Net { // package-private } /** + * Returns true if exclusive binding is on + */ + static boolean useExclusiveBind() { + return exclusiveBind; + } + + /** * Tells whether IPv6 sockets can join IPv4 multicast groups */ static boolean canIPv6SocketJoinIPv4Group() { @@ -148,6 +190,58 @@ class Net { // package-private } /** + * Returns the local address after performing a SecurityManager#checkConnect. + */ + static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) { + SecurityManager sm = System.getSecurityManager(); + if (addr == null || sm == null) + return addr; + + if (!getRevealLocalAddress()) { + // Return loopback address only if security check fails + try{ + sm.checkConnect(addr.getAddress().getHostAddress(), -1); + //Security check passed + } catch (SecurityException e) { + //Return loopback address + addr = getLoopbackAddress(addr.getPort()); + } + } + return addr; + } + + static String getRevealedLocalAddressAsString(InetSocketAddress addr) { + if (!getRevealLocalAddress() && System.getSecurityManager() != null) + addr = getLoopbackAddress(addr.getPort()); + return addr.toString(); + } + + private static boolean getRevealLocalAddress() { + if (!propRevealLocalAddress) { + try { + revealLocalAddress = Boolean.parseBoolean( + AccessController.doPrivileged( + new PrivilegedExceptionAction<String>() { + public String run() { + return System.getProperty( + "jdk.net.revealLocalAddress"); + } + })); + + } catch (Exception e) { + // revealLocalAddress is false + } + propRevealLocalAddress = true; + } + return revealLocalAddress; + } + + private static InetSocketAddress getLoopbackAddress(int port) { + return new InetSocketAddress(InetAddress.getLoopbackAddress(), + port); + } + + /** * Returns any IPv4 address of the given network interface, or * null if the interface does not have any IPv4 addresses. */ @@ -308,6 +402,12 @@ class Net { // package-private 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 +441,12 @@ class Net { // package-private { 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; |