8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl

Co-authored-by: Michael McMahon <michael.x.mcmahon@oracle.com>
Reviewed-by: chegar
This commit is contained in:
Alan Bateman 2019-03-16 19:44:12 +00:00
parent c306e3f059
commit 8743be63c4
12 changed files with 1525 additions and 188 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2019, 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
@ -23,6 +23,7 @@
* questions.
*/
package java.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -36,11 +37,9 @@ import sun.net.www.ParseUtil;
/**
* SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
* This is a subclass of PlainSocketImpl.
* Note this class should <b>NOT</b> be public.
*/
class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
private String server = null;
private int serverPort = DEFAULT_PORT;
private InetSocketAddress external_address;
@ -49,11 +48,12 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
private InputStream cmdIn = null;
private OutputStream cmdOut = null;
SocksSocketImpl() {
// Nothing needed
SocksSocketImpl(SocketImpl delegate) {
super(delegate);
}
SocksSocketImpl(Proxy proxy) {
SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
super(delegate);
SocketAddress a = proxy.address();
if (a instanceof InetSocketAddress) {
InetSocketAddress ad = (InetSocketAddress) a;
@ -75,7 +75,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
private synchronized void privilegedConnect(final String host,
final int port,
final int timeout)
throws IOException
throws IOException
{
try {
AccessController.doPrivileged(
@ -94,7 +94,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
private void superConnectServer(String host, int port,
int timeout) throws IOException {
super.connect(new InetSocketAddress(host, port), timeout);
delegate.connect(new InetSocketAddress(host, port), timeout);
}
private static int remainingMillis(long deadlineMillis) throws IOException {
@ -111,16 +111,23 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
private int readSocksReply(InputStream in, byte[] data, long deadlineMillis) throws IOException {
int len = data.length;
int received = 0;
while (received < len) {
int count;
try {
count = ((SocketInputStream)in).read(data, received, len - received, remainingMillis(deadlineMillis));
} catch (SocketTimeoutException e) {
throw new SocketTimeoutException("Connect timed out");
int originalTimeout = (int) getOption(SocketOptions.SO_TIMEOUT);
try {
while (received < len) {
int count;
int remaining = remainingMillis(deadlineMillis);
setOption(SocketOptions.SO_TIMEOUT, remaining);
try {
count = in.read(data, received, len - received);
} catch (SocketTimeoutException e) {
throw new SocketTimeoutException("Connect timed out");
}
if (count < 0)
throw new SocketException("Malformed reply from SOCKS server");
received += count;
}
if (count < 0)
throw new SocketException("Malformed reply from SOCKS server");
received += count;
} finally {
setOption(SocketOptions.SO_TIMEOUT, originalTimeout);
}
return received;
}
@ -239,6 +246,27 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
}
}
@Override
protected void connect(String host, int port) throws IOException {
connect(new InetSocketAddress(host, port), 0);
}
@Override
protected void connect(InetAddress address, int port) throws IOException {
connect(new InetSocketAddress(address, port), 0);
}
@Override
void setSocket(Socket soc) {
delegate.socket = soc;
super.setSocket(soc);
}
@Override
void setServerSocket(ServerSocket soc) {
throw new InternalError("should not get here");
}
/**
* Connects the Socks Socket to the specified endpoint. It will first
* connect to the SOCKS proxy and negotiate the access. If the proxy
@ -290,7 +318,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
/*
* No default proxySelector --> direct connection
*/
super.connect(epoint, remainingMillis(deadlineMillis));
delegate.connect(epoint, remainingMillis(deadlineMillis));
return;
}
URI uri;
@ -313,13 +341,13 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
java.util.Iterator<Proxy> iProxy = null;
iProxy = sel.select(uri).iterator();
if (iProxy == null || !(iProxy.hasNext())) {
super.connect(epoint, remainingMillis(deadlineMillis));
delegate.connect(epoint, remainingMillis(deadlineMillis));
return;
}
while (iProxy.hasNext()) {
p = iProxy.next();
if (p == null || p.type() != Proxy.Type.SOCKS) {
super.connect(epoint, remainingMillis(deadlineMillis));
delegate.connect(epoint, remainingMillis(deadlineMillis));
return;
}
@ -509,7 +537,15 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
external_address = epoint;
}
@Override
protected void listen(int backlog) {
throw new InternalError("should not get here");
}
@Override
protected void accept(SocketImpl s) {
throw new InternalError("should not get here");
}
/**
* Returns the value of this socket's {@code address} field.
@ -522,7 +558,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
if (external_address != null)
return external_address.getAddress();
else
return super.getInetAddress();
return delegate.getInetAddress();
}
/**
@ -536,7 +572,7 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
if (external_address != null)
return external_address.getPort();
else
return super.getPort();
return delegate.getPort();
}
@Override
@ -544,10 +580,15 @@ class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
if (cmdsock != null)
cmdsock.close();
cmdsock = null;
super.close();
delegate.close();
}
private String getUserName() {
return StaticProperty.userName();
}
@Override
void reset() {
throw new InternalError("should not get here");
}
}