package sun.nio.ch;
import java.lang.reflect.Constructor;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.ProtocolFamily;
import java.net.SocketAddress;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;
import static java.net.StandardProtocolFamily.INET6;
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.UNIX;
class InheritedChannel {
private static final int UNKNOWN = -1;
private static final int SOCK_STREAM = 1;
private static final int SOCK_DGRAM = 2;
static final int AF_UNKNOWN = -1;
static final int AF_INET = 1;
static final int AF_INET6 = 2;
static final int AF_UNIX = 3;
private static final int O_RDONLY = 0;
private static final int O_WRONLY = 1;
private static final int O_RDWR = 2;
private static int devnull = -1;
private static void detachIOStreams() {
try {
dup2(devnull, 0);
dup2(devnull, 1);
dup2(devnull, 2);
} catch (IOException ioe) {
throw new InternalError(ioe);
}
}
static ProtocolFamily protocolFamily(SocketAddress sa) {
if (sa instanceof UnixDomainSocketAddress) {
return UNIX;
} else {
InetSocketAddress isa = (InetSocketAddress) sa;
return (isa.getAddress() instanceof Inet6Address) ? INET6 : INET;
}
}
static ProtocolFamily protocolFamily(int family) {
return switch (family) {
case AF_INET -> INET;
case AF_INET6 -> INET6;
case AF_UNIX -> UNIX;
default -> throw new IllegalArgumentException();
};
}
public static class InheritedSocketChannelImpl extends SocketChannelImpl {
InheritedSocketChannelImpl(SelectorProvider sp,
FileDescriptor fd,
SocketAddress remote)
throws IOException
{
super(sp, protocolFamily(remote), fd, remote);
}
protected void implCloseSelectableChannel() throws IOException {
super.implCloseSelectableChannel();
detachIOStreams();
}
}
public static class InheritedServerSocketChannelImpl extends ServerSocketChannelImpl {
InheritedServerSocketChannelImpl(SelectorProvider sp,
ProtocolFamily family,
FileDescriptor fd)
throws IOException
{
super(sp, family, fd, true);
}
@Override
protected void implCloseSelectableChannel() throws IOException {
super.implCloseSelectableChannel();
detachIOStreams();
}
}
public static class InheritedDatagramChannelImpl extends DatagramChannelImpl {
InheritedDatagramChannelImpl(SelectorProvider sp,
FileDescriptor fd)
throws IOException
{
super(sp, fd);
}
protected void implCloseSelectableChannel() throws IOException {
super.implCloseSelectableChannel();
detachIOStreams();
}
}
private static void checkAccess() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("inheritedChannel"));
}
}
private static Channel createChannel() throws IOException {
int fdVal = dup(0);
int st;
st = soType0(fdVal);
if (st != SOCK_STREAM && st != SOCK_DGRAM) {
close0(fdVal);
return null;
}
Class<?> paramTypes[] = { int.class };
Constructor<?> ctr = Reflect.lookupConstructor("java.io.FileDescriptor",
paramTypes);
Object args[] = { Integer.valueOf(fdVal) };
FileDescriptor fd = (FileDescriptor)Reflect.invoke(ctr, args);
SelectorProvider provider = SelectorProvider.provider();
assert provider instanceof sun.nio.ch.SelectorProviderImpl;
Channel c;
if (st == SOCK_STREAM) {
int family = addressFamily(fdVal);
if (family == AF_UNKNOWN)
return null;
ProtocolFamily pfamily = protocolFamily(family);
if (family == AF_UNIX) {
if (isConnected(fdVal)) {
var sa = UnixDomainSocketAddress.of(unixPeerAddress(fdVal));
return new InheritedSocketChannelImpl(provider, fd, sa);
} else {
return new InheritedServerSocketChannelImpl(provider, pfamily, fd);
}
}
InetAddress ia = inetPeerAddress0(fdVal);
if (ia == null) {
c = new InheritedServerSocketChannelImpl(provider, pfamily, fd);
} else {
int port = peerPort0(fdVal);
assert port > 0;
InetSocketAddress isa = new InetSocketAddress(ia, port);
c = new InheritedSocketChannelImpl(provider, fd, isa);
}
} else {
c = new InheritedDatagramChannelImpl(provider, fd);
}
return c;
}
private static boolean haveChannel = false;
private static Channel channel = null;
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
if (channel != null) {
checkAccess();
}
return channel;
}
private static String unixPeerAddress(int fd) throws IOException {
byte[] bytes = unixPeerAddress0(fd);
return new String(bytes);
}
private static native void initIDs();
private static native int dup(int fd) throws IOException;
private static native void dup2(int fd, int fd2) throws IOException;
private static native int open0(String path, int oflag) throws IOException;
private static native void close0(int fd) throws IOException;
private static native int soType0(int fd);
private static native int addressFamily(int fd);
private static native InetAddress inetPeerAddress0(int fd);
private static native byte[] unixPeerAddress0(int fd);
private static native int peerPort0(int fd);
private static native boolean isConnected(int fd);
static {
IOUtil.load();
initIDs();
}
}