8260428: Drop support for pre JDK 1.4 DatagramSocketImpl implementations

Reviewed-by: alanb, dfuchs, vtewari
This commit is contained in:
Patrick Concannon 2021-11-01 17:11:20 +00:00
parent e265f83858
commit 977154400b
3 changed files with 212 additions and 93 deletions

View file

@ -27,6 +27,7 @@ package java.net;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Objects;
import java.util.Set;
@ -99,19 +100,30 @@ public abstract class DatagramSocketImpl implements SocketOptions {
* packet has been received for that address, then a subsequent call to
* send or receive may throw a PortUnreachableException.
* Note, there is no guarantee that the exception will be thrown.
*
* @implSpec The default implementation of this method throws {@code SocketException}.
*
* @param address the remote InetAddress to connect to
* @param port the remote port number
* @throws SocketException may be thrown if the socket cannot be
* connected to the remote destination
* @since 1.4
*/
protected void connect(InetAddress address, int port) throws SocketException {}
protected void connect(InetAddress address, int port) throws SocketException {
throw new SocketException("connect not implemented");
}
/**
* Disconnects a datagram socket from its remote destination.
*
* @implSpec The default implementation of this method throws {@code UncheckedIOException}.
*
* @throws UncheckedIOException if disconnect fails or no implementation is provided
* @since 1.4
*/
protected void disconnect() {}
protected void disconnect() {
throw new UncheckedIOException(new SocketException("disconnect not implemented"));
}
/**
* Peek at the packet to see who it is from. Updates the specified {@code InetAddress}

View file

@ -56,11 +56,6 @@ final class NetMulticastSocket extends MulticastSocket {
*/
private final DatagramSocketImpl impl;
/**
* Are we using an older DatagramSocketImpl?
*/
private final boolean oldImpl;
/**
* Set when a socket is ST_CONNECTED until we are certain
* that any packets which might have been received prior
@ -76,11 +71,9 @@ final class NetMulticastSocket extends MulticastSocket {
* Connection state:
* ST_NOT_CONNECTED = socket not connected
* ST_CONNECTED = socket connected
* ST_CONNECTED_NO_IMPL = socket connected but not at impl level
*/
static final int ST_NOT_CONNECTED = 0;
static final int ST_CONNECTED = 1;
static final int ST_CONNECTED_NO_IMPL = 2;
int connectState = ST_NOT_CONNECTED;
@ -97,7 +90,6 @@ final class NetMulticastSocket extends MulticastSocket {
NetMulticastSocket(DatagramSocketImpl impl) {
super((MulticastSocket) null);
this.impl = Objects.requireNonNull(impl);
this.oldImpl = checkOldImpl(impl);
}
/**
@ -135,59 +127,24 @@ final class NetMulticastSocket extends MulticastSocket {
if (!isBound())
bind(new InetSocketAddress(0));
// old impls do not support connect/disconnect
if (oldImpl) {
connectState = ST_CONNECTED_NO_IMPL;
} else {
try {
getImpl().connect(address, port);
getImpl().connect(address, port);
// socket is now connected by the impl
connectState = ST_CONNECTED;
// Do we need to filter some packets?
int avail = getImpl().dataAvailable();
if (avail == -1) {
throw new SocketException();
}
explicitFilter = avail > 0;
if (explicitFilter) {
bytesLeftToFilter = getReceiveBufferSize();
}
} catch (SocketException se) {
// connection will be emulated by DatagramSocket
connectState = ST_CONNECTED_NO_IMPL;
}
// socket is now connected by the impl
connectState = ST_CONNECTED;
// Do we need to filter some packets?
int avail = getImpl().dataAvailable();
if (avail == -1) {
throw new SocketException();
}
explicitFilter = avail > 0;
if (explicitFilter) {
bytesLeftToFilter = getReceiveBufferSize();
}
connectedAddress = address;
connectedPort = port;
}
/**
* Return true if the given DatagramSocketImpl is an "old" impl. An old impl
* is one that doesn't implement the abstract methods added in Java SE 1.4.
*/
@SuppressWarnings("removal")
private static boolean checkOldImpl(DatagramSocketImpl impl) {
// DatagramSocketImpl.peekData() is a protected method, therefore we need to use
// getDeclaredMethod, therefore we need permission to access the member
try {
AccessController.doPrivileged(
new PrivilegedExceptionAction<>() {
public Void run() throws NoSuchMethodException {
Class<?>[] cl = new Class<?>[1];
cl[0] = DatagramPacket.class;
impl.getClass().getDeclaredMethod("peekData", cl);
return null;
}
});
return false;
} catch (java.security.PrivilegedActionException e) {
return true;
}
}
/**
* Return the {@code DatagramSocketImpl} attached to this socket,
* creating the socket if not already created.
@ -382,19 +339,11 @@ final class NetMulticastSocket extends MulticastSocket {
SecurityManager security = System.getSecurityManager();
if (security != null) {
while (true) {
String peekAd = null;
int peekPort = 0;
// peek at the packet to see who it is from.
if (!oldImpl) {
// We can use the new peekData() API
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
peekPort = getImpl().peekData(peekPacket);
peekAd = peekPacket.getAddress().getHostAddress();
} else {
InetAddress adr = new InetAddress();
peekPort = getImpl().peek(adr);
peekAd = adr.getHostAddress();
}
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
peekPort = getImpl().peekData(peekPacket);
String peekAd = peekPacket.getAddress().getHostAddress();
try {
security.checkAccept(peekAd, peekPort);
// security check succeeded - so now break
@ -418,7 +367,7 @@ final class NetMulticastSocket extends MulticastSocket {
}
}
DatagramPacket tmp = null;
if ((connectState == ST_CONNECTED_NO_IMPL) || explicitFilter) {
if (explicitFilter) {
// We have to do the filtering the old fashioned way since
// the native impl doesn't support connect or the connect
// via the impl failed, or .. "explicitFilter" may be set when
@ -426,21 +375,11 @@ final class NetMulticastSocket extends MulticastSocket {
// when packets from other sources might be queued on socket.
boolean stop = false;
while (!stop) {
InetAddress peekAddress = null;
int peekPort = -1;
// peek at the packet to see who it is from.
if (!oldImpl) {
// We can use the new peekData() API
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
peekPort = getImpl().peekData(peekPacket);
peekAddress = peekPacket.getAddress();
} else {
// this api only works for IPv4
peekAddress = new InetAddress();
peekPort = getImpl().peek(peekAddress);
}
if ((!connectedAddress.equals(peekAddress)) ||
(connectedPort != peekPort)) {
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
int peekPort = getImpl().peekData(peekPacket);
InetAddress peekAddress = peekPacket.getAddress();
if ((!connectedAddress.equals(peekAddress)) || (connectedPort != peekPort)) {
// throw the packet away and silently continue
tmp = new DatagramPacket(
new byte[1024], 1024);
@ -578,11 +517,7 @@ final class NetMulticastSocket extends MulticastSocket {
public synchronized void setReuseAddress(boolean on) throws SocketException {
if (isClosed())
throw new SocketException("Socket is closed");
// Integer instead of Boolean for compatibility with older DatagramSocketImpl
if (oldImpl)
getImpl().setOption(SocketOptions.SO_REUSEADDR, on ? -1 : 0);
else
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
}
@Override
@ -809,9 +744,6 @@ final class NetMulticastSocket extends MulticastSocket {
if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type");
if (oldImpl)
throw new UnsupportedOperationException();
checkAddress(addr.getAddress(), "joinGroup");
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
@ -835,9 +767,6 @@ final class NetMulticastSocket extends MulticastSocket {
if (!(mcastaddr instanceof InetSocketAddress addr))
throw new IllegalArgumentException("Unsupported address type");
if (oldImpl)
throw new UnsupportedOperationException();
checkAddress(addr.getAddress(), "leaveGroup");
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();