--- a/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java Sun Feb 10 17:37:38 2019 +0000
+++ b/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java Sun Feb 10 19:51:43 2019 +0000
@@ -40,6 +40,7 @@
import sun.net.ConnectionResetException;
import sun.net.NetHooks;
import sun.net.ResourceManager;
+import sun.net.TrustedSocketImpl;
import sun.net.util.SocketExceptions;
/**
@@ -49,7 +50,7 @@
*
* @author Steven B. Byrne
*/
-abstract class AbstractPlainSocketImpl extends SocketImpl {
+abstract class AbstractPlainSocketImpl extends SocketImpl implements TrustedSocketImpl {
/* instance variable for SO_TIMEOUT */
int timeout; // timeout in millisec
// traffic class
@@ -731,11 +732,23 @@
socketClose0(false);
}
- /**
- * For use by ServerSocket to copy the state from this connected SocketImpl
- * to a target SocketImpl.
- */
- void copyTo(SocketImpl si) {
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S extends SocketImpl & TrustedSocketImpl> S newInstance(boolean server) {
+ return (S) new PlainSocketImpl();
+ }
+
+ @Override
+ public void postCustomAccept() {
+ assert fd.valid() && localport != 0 && address != null && port != 0;
+ stream = true;
+
+ // assume the custom SocketImpl didn't register a cleaner
+ SocketCleanable.register(fd);
+ }
+
+ @Override
+ public void copyTo(SocketImpl si) {
if (si instanceof AbstractPlainSocketImpl) {
try {
si.close();
--- a/src/java.base/share/classes/java/net/ServerSocket.java Sun Feb 10 17:37:38 2019 +0000
+++ b/src/java.base/share/classes/java/net/ServerSocket.java Sun Feb 10 19:51:43 2019 +0000
@@ -37,6 +37,8 @@
import java.security.PrivilegedExceptionAction;
import java.util.Set;
import java.util.Collections;
+
+import sun.net.TrustedSocketImpl;
import sun.nio.ch.NioSocketImpl;
/**
@@ -550,9 +552,9 @@
si = Socket.createImpl();
impl.accept(si);
try {
- // a custom impl has accepted the connection with a NIO SocketImpl
- if (!(impl instanceof NioSocketImpl) && (si instanceof NioSocketImpl)) {
- ((NioSocketImpl) si).postCustomAccept();
+ // a custom impl has accepted the connection with a trusted SocketImpl
+ if (!(impl instanceof TrustedSocketImpl) && (si instanceof TrustedSocketImpl)) {
+ ((TrustedSocketImpl) si).postCustomAccept();
}
} finally {
securityCheckAccept(si); // closes si if permission check fails
@@ -567,12 +569,14 @@
if (si instanceof DelegatingSocketImpl)
si = ((DelegatingSocketImpl) si).delegate();
- // ServerSocket or Socket is using NIO SocketImpl
- if (impl instanceof NioSocketImpl || si instanceof NioSocketImpl) {
- // accept connection via new SocketImpl
- NioSocketImpl nsi = new NioSocketImpl(false);
+ // ServerSocket or Socket is using a trusted SocketImpl
+ if (impl instanceof TrustedSocketImpl || si instanceof TrustedSocketImpl) {
+ // accept connection with new SocketImpl
+ var nsi = (impl instanceof TrustedSocketImpl)
+ ? ((TrustedSocketImpl) impl).newInstance(false)
+ : ((TrustedSocketImpl) si).newInstance(false);
impl.accept(nsi);
- securityCheckAccept(nsi); // closes si if permission check fails
+ securityCheckAccept(nsi); // closes nsi if permission check fails
// copy state to the existing SocketImpl and update socket state
nsi.copyTo(si);
@@ -580,19 +584,6 @@
return;
}
- // ServerSocket or Socket is using PlainSocketImpl
- if (impl instanceof PlainSocketImpl || si instanceof PlainSocketImpl) {
- // accept connection via new SocketImpl
- PlainSocketImpl psi = new PlainSocketImpl();
- impl.accept(psi);
- securityCheckAccept(psi); // closes si if permission check fails
-
- // copy state to the existing SocketImpl and update socket state
- psi.copyTo(si);
- s.postAccept();
- return;
- }
-
// ServerSocket and Socket bound to custom SocketImpls
s.impl = null; // break connection to impl
boolean completed = false;
--- a/src/java.base/share/classes/java/net/SocketImpl.java Sun Feb 10 17:37:38 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocketImpl.java Sun Feb 10 19:51:43 2019 +0000
@@ -29,8 +29,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.util.Set;
+import sun.net.NetProperties;
import sun.nio.ch.NioSocketImpl;
import sun.security.action.GetPropertyAction;
@@ -49,13 +52,19 @@
private static final boolean USE_PLAINSOCKETIMPL = usePlainSocketImpl();
private static boolean usePlainSocketImpl() {
- String s = GetPropertyAction.privilegedGetProperty("jdk.net.socketimpl.default");
- return "classic".equalsIgnoreCase(s);
+ String s = GetPropertyAction.privilegedGetProperty("jdk.net.usePlainSocketImpl");
+ if (s != null && !"false".equalsIgnoreCase(s))
+ return true;
+
+ PrivilegedAction<String> pa = () -> NetProperties.get("jdk.net.socketimpl.default");
+ s = AccessController.doPrivileged(pa);
+ return (s != null) && "classic".equalsIgnoreCase(s);
}
/**
* Creates a instance of platform's SocketImpl
*/
+ @SuppressWarnings("unchecked")
static SocketImpl createSocketImpl(boolean server) {
if (USE_PLAINSOCKETIMPL) {
return new PlainSocketImpl();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/sun/net/TrustedSocketImpl.java Sun Feb 10 19:51:43 2019 +0000
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package sun.net;
+
+import java.io.IOException;
+import java.net.SocketImpl;
+
+/**
+ * Implemented by the platform's SocketImpl implementations.
+ */
+
+public interface TrustedSocketImpl {
+
+ /**
+ * Creates a new instance of this SocketImpl.
+ */
+ <S extends SocketImpl & TrustedSocketImpl> S newInstance(boolean server);
+
+ /**
+ * Invoked by ServerSocket to fix up the SocketImpl state after a connection
+ * is accepted by a custom SocketImpl
+ */
+ void postCustomAccept() throws IOException;
+
+ /**
+ * Copy the state from this connected SocketImpl to a target SocketImpl. If
+ * the target SocketImpl is not a newly created SocketImpl then it is first
+ * closed to release any resources. The target SocketImpl becomes the owner
+ * of the file descriptor, this SocketImpl is marked as closed and should
+ * be discarded.
+ */
+ void copyTo(SocketImpl si);
+}
--- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java Sun Feb 10 17:37:38 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java Sun Feb 10 19:51:43 2019 +0000
@@ -58,6 +58,7 @@
import jdk.internal.ref.CleanerFactory;
import sun.net.NetHooks;
import sun.net.ResourceManager;
+import sun.net.TrustedSocketImpl;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.SocketExceptions;
@@ -80,7 +81,7 @@
* an application continues to call read or available after a reset.
*/
-public final class NioSocketImpl extends SocketImpl {
+public final class NioSocketImpl extends SocketImpl implements TrustedSocketImpl {
private static final NativeDispatcher nd = new SocketDispatcher();
// The maximum number of bytes to read/write per syscall to avoid needing
@@ -393,10 +394,20 @@
}
/**
+ * For use by ServerSocket to create a new instance of this SocketImpl.
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S extends SocketImpl & TrustedSocketImpl> S newInstance(boolean server) {
+ return (S) new NioSocketImpl(server);
+ }
+
+ /**
* For use by ServerSocket to set the state and other fields after a
* connection is accepted by a ServerSocket using a custom SocketImpl.
* The protected fields defined by SocketImpl should be set.
*/
+ @Override
public void postCustomAccept() throws IOException {
synchronized (stateLock) {
assert state == ST_NEW;
@@ -415,6 +426,7 @@
* SocketImpl becomes the owner of the file descriptor, this SocketImpl
* is marked as closed and should be discarded.
*/
+ @Override
public void copyTo(SocketImpl si) {
if (si instanceof NioSocketImpl) {
NioSocketImpl nsi = (NioSocketImpl) si;