8245194: Unix domain socket channel implementation

Reviewed-by: erikj, dfuchs, alanb, chegar
This commit is contained in:
Michael McMahon 2020-10-28 17:26:26 +00:00
parent 8bde2f4e3d
commit 6bb7e45e8e
73 changed files with 5434 additions and 1116 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020, 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
@ -48,6 +48,7 @@ public abstract class ExtendedSocketOptions {
private final Set<SocketOption<?>> datagramOptions;
private final Set<SocketOption<?>> clientStreamOptions;
private final Set<SocketOption<?>> serverStreamOptions;
private final Set<SocketOption<?>> unixDomainClientOptions;
/** Tells whether or not the option is supported. */
public final boolean isOptionSupported(SocketOption<?> option) {
@ -73,6 +74,19 @@ public abstract class ExtendedSocketOptions {
return getInstance().options0(SOCK_STREAM, false);
}
/**
* Return the, possibly empty, set of extended socket options available for
* Unix domain client sockets. Note, there are no extended
* Unix domain server options.
*/
private final Set<SocketOption<?>> unixDomainClientOptions() {
return unixDomainClientOptions;
}
public static Set<SocketOption<?>> unixDomainSocketOptions() {
return getInstance().unixDomainClientOptions();
}
/**
* Returns the (possibly empty) set of extended socket options for
* datagram-oriented sockets.
@ -82,14 +96,22 @@ public abstract class ExtendedSocketOptions {
}
private static boolean isDatagramOption(SocketOption<?> option) {
return !option.name().startsWith("TCP_");
if (option.name().startsWith("TCP_") || isUnixDomainOption(option)) {
return false;
} else {
return true;
}
}
private static boolean isUnixDomainOption(SocketOption<?> option) {
return option.name().equals("SO_PEERCRED");
}
private static boolean isStreamOption(SocketOption<?> option, boolean server) {
if (server && "SO_FLOW_SLA".equals(option.name())) {
if (option.name().startsWith("UDP_") || isUnixDomainOption(option)) {
return false;
} else {
return !option.name().startsWith("UDP_");
return true;
}
}
@ -122,6 +144,7 @@ public abstract class ExtendedSocketOptions {
var datagramOptions = new HashSet<SocketOption<?>>();
var serverStreamOptions = new HashSet<SocketOption<?>>();
var clientStreamOptions = new HashSet<SocketOption<?>>();
var unixDomainClientOptions = new HashSet<SocketOption<?>>();
for (var option : options) {
if (isDatagramOption(option)) {
datagramOptions.add(option);
@ -132,10 +155,14 @@ public abstract class ExtendedSocketOptions {
if (isStreamOption(option, false)) {
clientStreamOptions.add(option);
}
if (isUnixDomainOption(option)) {
unixDomainClientOptions.add(option);
}
}
this.datagramOptions = Set.copyOf(datagramOptions);
this.serverStreamOptions = Set.copyOf(serverStreamOptions);
this.clientStreamOptions = Set.copyOf(clientStreamOptions);
this.unixDomainClientOptions = Set.copyOf(unixDomainClientOptions);
}
private static volatile ExtendedSocketOptions instance;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, 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
@ -28,6 +28,8 @@ package sun.net.util;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.InetSocketAddress;
import java.net.UnixDomainSocketAddress;
import java.net.SocketAddress;
import java.security.AccessController;
import java.security.PrivilegedAction;
@ -51,11 +53,22 @@ public final class SocketExceptions {
*
* Only specific IOException subtypes are supported.
*/
public static IOException of(IOException e, InetSocketAddress address) {
if (!enhancedExceptionText || address == null)
public static IOException of(IOException e, SocketAddress addr) {
if (!enhancedExceptionText || addr == null) {
return e;
int port = address.getPort();
String host = address.getHostString();
}
if (addr instanceof UnixDomainSocketAddress) {
return ofUnixDomain(e, (UnixDomainSocketAddress)addr);
} else if (addr instanceof InetSocketAddress) {
return ofInet(e, (InetSocketAddress)addr);
} else {
return e;
}
}
private static IOException ofInet(IOException e, InetSocketAddress addr) {
int port = addr.getPort();
String host = addr.getHostString();
StringBuilder sb = new StringBuilder();
sb.append(e.getMessage());
sb.append(": ");
@ -66,6 +79,16 @@ public final class SocketExceptions {
return create(e, enhancedMsg);
}
private static IOException ofUnixDomain(IOException e, UnixDomainSocketAddress addr) {
String path = addr.getPath().toString();
StringBuilder sb = new StringBuilder();
sb.append(e.getMessage());
sb.append(": ");
sb.append(path);
String enhancedMsg = sb.toString();
return create(e, enhancedMsg);
}
// return a new instance of the same type with the given detail
// msg, or if the type doesn't support detail msgs, return given
// instance.