-
Notifications
You must be signed in to change notification settings - Fork 315
Use the JDK's built-in support for Unix Domain Sockets on Java 16+ #8197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,37 @@ | ||
| apply from: "$rootDir/gradle/java.gradle" | ||
| apply plugin: "idea" | ||
|
|
||
| sourceSets { | ||
| main_java17 { | ||
| java.srcDirs "${project.projectDir}/src/main/java17" | ||
| } | ||
| } | ||
|
|
||
| compileMain_java17Java.configure { | ||
| setJavaVersion(it, 17) | ||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly sourceSets.main_java17.output | ||
|
|
||
| implementation libs.slf4j | ||
| implementation project(':internal-api') | ||
|
|
||
| implementation group: 'com.github.jnr', name: 'jnr-unixsocket', version: libs.versions.jnr.unixsocket.get() | ||
| } | ||
|
|
||
| jar { | ||
| from sourceSets.main_java17.output | ||
| } | ||
|
|
||
| forbiddenApisMain_java17 { | ||
| failOnMissingClasses = false | ||
| } | ||
|
|
||
| idea { | ||
| module { | ||
| jdkName = '17' | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
utils/socket-utils/src/main/java17/datadog/common/socket/TunnelingJdkSocket.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| package datadog.common.socket; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.Socket; | ||
| import java.net.SocketAddress; | ||
| import java.net.SocketException; | ||
| import java.net.UnixDomainSocketAddress; | ||
| import java.nio.channels.Channels; | ||
| import java.nio.channels.SocketChannel; | ||
| import java.nio.file.Path; | ||
|
|
||
| /** | ||
| * Subtype UNIX socket for a higher-fidelity impersonation of TCP sockets. This is named "tunneling" | ||
| * because it assumes the ultimate destination has a hostname and port. | ||
| * | ||
| * <p>Bsed on {@link TunnelingUnixSocket}; adapted to use the built-in UDS support added in Java 16. | ||
| */ | ||
| final class TunnelingJdkSocket extends Socket { | ||
| private final SocketAddress unixSocketAddress; | ||
| private InetSocketAddress inetSocketAddress; | ||
|
|
||
| private SocketChannel unixSocketChannel; | ||
|
|
||
| private int timeout; | ||
| private boolean shutIn; | ||
| private boolean shutOut; | ||
| private boolean closed; | ||
|
|
||
| TunnelingJdkSocket(final Path path) { | ||
| this.unixSocketAddress = UnixDomainSocketAddress.of(path); | ||
| } | ||
|
|
||
| TunnelingJdkSocket(final Path path, final InetSocketAddress address) { | ||
| this(path); | ||
| inetSocketAddress = address; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isConnected() { | ||
| return null != unixSocketChannel; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isInputShutdown() { | ||
| return shutIn; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isOutputShutdown() { | ||
| return shutOut; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isClosed() { | ||
| return closed; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void setSoTimeout(int timeout) throws SocketException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (timeout < 0) { | ||
| throw new IllegalArgumentException("Socket timeout can't be negative"); | ||
| } | ||
| this.timeout = timeout; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized int getSoTimeout() throws SocketException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| return timeout; | ||
| } | ||
|
|
||
| @Override | ||
| public void connect(final SocketAddress endpoint) throws IOException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (isConnected()) { | ||
| throw new SocketException("Socket is already connected"); | ||
| } | ||
| inetSocketAddress = (InetSocketAddress) endpoint; | ||
| unixSocketChannel = SocketChannel.open(unixSocketAddress); | ||
| } | ||
|
|
||
| @Override | ||
| public void connect(final SocketAddress endpoint, final int timeout) throws IOException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (isConnected()) { | ||
| throw new SocketException("Socket is already connected"); | ||
| } | ||
| inetSocketAddress = (InetSocketAddress) endpoint; | ||
| unixSocketChannel = SocketChannel.open(unixSocketAddress); | ||
| } | ||
|
|
||
| @Override | ||
| public SocketChannel getChannel() { | ||
| return unixSocketChannel; | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream getInputStream() throws IOException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (!isConnected()) { | ||
| throw new SocketException("Socket is not connected"); | ||
| } | ||
| if (isInputShutdown()) { | ||
| throw new SocketException("Socket input is shutdown"); | ||
| } | ||
| return Channels.newInputStream(unixSocketChannel); | ||
| } | ||
|
|
||
| @Override | ||
| public OutputStream getOutputStream() throws IOException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (!isConnected()) { | ||
| throw new SocketException("Socket is not connected"); | ||
| } | ||
| if (isInputShutdown()) { | ||
| throw new SocketException("Socket output is shutdown"); | ||
| } | ||
| return Channels.newOutputStream(unixSocketChannel); | ||
| } | ||
|
|
||
| @Override | ||
| public void shutdownInput() throws IOException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (!isConnected()) { | ||
| throw new SocketException("Socket is not connected"); | ||
| } | ||
| if (isInputShutdown()) { | ||
| throw new SocketException("Socket input is already shutdown"); | ||
| } | ||
| unixSocketChannel.shutdownInput(); | ||
| shutIn = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void shutdownOutput() throws IOException { | ||
| if (isClosed()) { | ||
| throw new SocketException("Socket is closed"); | ||
| } | ||
| if (!isConnected()) { | ||
| throw new SocketException("Socket is not connected"); | ||
| } | ||
| if (isOutputShutdown()) { | ||
| throw new SocketException("Socket output is already shutdown"); | ||
| } | ||
| unixSocketChannel.shutdownOutput(); | ||
| shutOut = true; | ||
| } | ||
|
|
||
| @Override | ||
| public InetAddress getInetAddress() { | ||
| return inetSocketAddress.getAddress(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| if (isClosed()) { | ||
| return; | ||
| } | ||
| if (null != unixSocketChannel) { | ||
| unixSocketChannel.close(); | ||
| } | ||
| closed = true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.