Skip to content

Commit

Permalink
8308593: Add KEEPALIVE Extended Socket Options Support for Windows
Browse files Browse the repository at this point in the history
Reviewed-by: djelinski, phh
Backport-of: f3ade38
  • Loading branch information
tkyc authored and Paul Hohensee committed Oct 30, 2023
1 parent 4a638cb commit 57bf412
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 32 deletions.
7 changes: 4 additions & 3 deletions make/modules/jdk.net/Lib.gmk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2016, 2023 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
Expand Down Expand Up @@ -27,16 +27,17 @@ include LibCommon.gmk

################################################################################

ifeq ($(call isTargetOs, linux macosx), true)
ifeq ($(call isTargetOs, linux macosx windows), true)

$(eval $(call SetupJdkLibrary, BUILD_LIBEXTNET, \
NAME := extnet, \
OPTIMIZATION := LOW, \
CFLAGS := $(CFLAGS_JDKLIB), \
LDFLAGS := $(LDFLAGS_JDKLIB) \
$(call SET_SHARED_LIBRARY_ORIGIN), \
LIBS := -ljava, \
LIBS_unix := -ljava, \
LIBS_linux := -ljvm, \
LIBS_windows := jvm.lib ws2_32.lib $(WIN_JAVA_LIB), \
))

$(BUILD_LIBEXTNET): $(call FindLib, java.base, java)
Expand Down
12 changes: 6 additions & 6 deletions src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ boolean peerCredentialsSupported() {
}

@Override
void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
setTcpkeepAliveProbes0(fd, value);
void setTcpKeepAliveProbes(int fd, final int value) throws SocketException {
setTcpKeepAliveProbes0(fd, value);
}

@Override
Expand All @@ -78,8 +78,8 @@ void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
}

@Override
int getTcpkeepAliveProbes(int fd) throws SocketException {
return getTcpkeepAliveProbes0(fd);
int getTcpKeepAliveProbes(int fd) throws SocketException {
return getTcpKeepAliveProbes0(fd);
}

@Override
Expand Down Expand Up @@ -112,10 +112,10 @@ UnixDomainPrincipal getSoPeerCred(int fd) throws SocketException {
return new UnixDomainPrincipal(user, group);
}

private static native void setTcpkeepAliveProbes0(int fd, int value) throws SocketException;
private static native void setTcpKeepAliveProbes0(int fd, int value) throws SocketException;
private static native void setTcpKeepAliveTime0(int fd, int value) throws SocketException;
private static native void setTcpKeepAliveIntvl0(int fd, int value) throws SocketException;
private static native int getTcpkeepAliveProbes0(int fd) throws SocketException;
private static native int getTcpKeepAliveProbes0(int fd) throws SocketException;
private static native int getTcpKeepAliveTime0(int fd) throws SocketException;
private static native int getTcpKeepAliveIntvl0(int fd) throws SocketException;
private static native void setQuickAck0(int fd, boolean on) throws SocketException;
Expand Down
8 changes: 4 additions & 4 deletions src/jdk.net/linux/native/libextnet/LinuxSocketOptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_keepAliveOptionsSuppo

/*
* Class: jdk_net_LinuxSocketOptions
* Method: setTcpkeepAliveProbes0
* Method: setTcpKeepAliveProbes0
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpkeepAliveProbes0
JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpKeepAliveProbes0
(JNIEnv *env, jobject unused, jint fd, jint optval) {
jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &optval, sizeof (optval));
handleError(env, rv, "set option TCP_KEEPCNT failed");
Expand Down Expand Up @@ -180,10 +180,10 @@ JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpKeepAliveIntvl0

/*
* Class: jdk_net_LinuxSocketOptions
* Method: getTcpkeepAliveProbes0
* Method: getTcpKeepAliveProbes0
* Signature: (I)I;
*/
JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpkeepAliveProbes0
JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpKeepAliveProbes0
(JNIEnv *env, jobject unused, jint fd) {
jint optval, rv;
socklen_t sz = sizeof (optval);
Expand Down
12 changes: 6 additions & 6 deletions src/jdk.net/macosx/classes/jdk/net/MacOSXSocketOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ boolean keepAliveOptionsSupported() {
}

@Override
void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
setTcpkeepAliveProbes0(fd, value);
void setTcpKeepAliveProbes(int fd, final int value) throws SocketException {
setTcpKeepAliveProbes0(fd, value);
}

@Override
Expand All @@ -64,8 +64,8 @@ void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
}

@Override
int getTcpkeepAliveProbes(int fd) throws SocketException {
return getTcpkeepAliveProbes0(fd);
int getTcpKeepAliveProbes(int fd) throws SocketException {
return getTcpKeepAliveProbes0(fd);
}

@Override
Expand All @@ -88,10 +88,10 @@ UnixDomainPrincipal getSoPeerCred(int fd) throws SocketException {
return new UnixDomainPrincipal(user, group);
}

private static native void setTcpkeepAliveProbes0(int fd, int value) throws SocketException;
private static native void setTcpKeepAliveProbes0(int fd, int value) throws SocketException;
private static native void setTcpKeepAliveTime0(int fd, int value) throws SocketException;
private static native void setTcpKeepAliveIntvl0(int fd, int value) throws SocketException;
private static native int getTcpkeepAliveProbes0(int fd) throws SocketException;
private static native int getTcpKeepAliveProbes0(int fd) throws SocketException;
private static native int getTcpKeepAliveTime0(int fd) throws SocketException;
private static native int getTcpKeepAliveIntvl0(int fd) throws SocketException;
private static native long getSoPeerCred0(int fd) throws SocketException;
Expand Down
8 changes: 4 additions & 4 deletions src/jdk.net/macosx/native/libextnet/MacOSXSocketOptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ JNIEXPORT jboolean JNICALL Java_jdk_net_MacOSXSocketOptions_keepAliveOptionsSupp

/*
* Class: jdk_net_MacOSXSocketOptions
* Method: setTcpkeepAliveProbes0
* Method: setTcpKeepAliveProbes0
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_jdk_net_MacOSXSocketOptions_setTcpkeepAliveProbes0
JNIEXPORT void JNICALL Java_jdk_net_MacOSXSocketOptions_setTcpKeepAliveProbes0
(JNIEnv *env, jobject unused, jint fd, jint optval) {
jint rv = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof (optval));
handleError(env, rv, "set option TCP_KEEPCNT failed");
Expand Down Expand Up @@ -112,10 +112,10 @@ JNIEXPORT void JNICALL Java_jdk_net_MacOSXSocketOptions_setTcpKeepAliveIntvl0

/*
* Class: jdk_net_MacOSXSocketOptions
* Method: getTcpkeepAliveProbes0
* Method: getTcpKeepAliveProbes0
* Signature: (I)I;
*/
JNIEXPORT jint JNICALL Java_jdk_net_MacOSXSocketOptions_getTcpkeepAliveProbes0
JNIEXPORT jint JNICALL Java_jdk_net_MacOSXSocketOptions_getTcpKeepAliveProbes0
(JNIEnv *env, jobject unused, jint fd) {
jint optval, rv;
socklen_t sz = sizeof (optval);
Expand Down
20 changes: 11 additions & 9 deletions src/jdk.net/share/classes/jdk/net/ExtendedSocketOptions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023 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
Expand Down Expand Up @@ -247,7 +247,7 @@ public void setOption(FileDescriptor fd,
if (option == TCP_QUICKACK) {
setQuickAckOption(fd, (boolean) value);
} else if (option == TCP_KEEPCOUNT) {
setTcpkeepAliveProbes(fd, (Integer) value);
setTcpKeepAliveProbes(fd, (Integer) value);
} else if (option == TCP_KEEPIDLE) {
setTcpKeepAliveTime(fd, (Integer) value);
} else if (option == TCP_KEEPINTERVAL) {
Expand Down Expand Up @@ -276,7 +276,7 @@ public Object getOption(FileDescriptor fd,
if (option == TCP_QUICKACK) {
return getQuickAckOption(fd);
} else if (option == TCP_KEEPCOUNT) {
return getTcpkeepAliveProbes(fd);
return getTcpKeepAliveProbes(fd);
} else if (option == TCP_KEEPIDLE) {
return getTcpKeepAliveTime(fd);
} else if (option == TCP_KEEPINTERVAL) {
Expand Down Expand Up @@ -310,9 +310,9 @@ private static Object getQuickAckOption(FileDescriptor fd)
return platformSocketOptions.getQuickAck(fdAccess.get(fd));
}

private static void setTcpkeepAliveProbes(FileDescriptor fd, int value)
private static void setTcpKeepAliveProbes(FileDescriptor fd, int value)
throws SocketException {
platformSocketOptions.setTcpkeepAliveProbes(fdAccess.get(fd), value);
platformSocketOptions.setTcpKeepAliveProbes(fdAccess.get(fd), value);
}

private static void setTcpKeepAliveTime(FileDescriptor fd, int value)
Expand All @@ -325,8 +325,8 @@ private static void setTcpKeepAliveIntvl(FileDescriptor fd, int value)
platformSocketOptions.setTcpKeepAliveIntvl(fdAccess.get(fd), value);
}

private static int getTcpkeepAliveProbes(FileDescriptor fd) throws SocketException {
return platformSocketOptions.getTcpkeepAliveProbes(fdAccess.get(fd));
private static int getTcpKeepAliveProbes(FileDescriptor fd) throws SocketException {
return platformSocketOptions.getTcpKeepAliveProbes(fdAccess.get(fd));
}

private static int getTcpKeepAliveTime(FileDescriptor fd) throws SocketException {
Expand Down Expand Up @@ -368,6 +368,8 @@ public String run() {
return newInstance("jdk.net.LinuxSocketOptions");
} else if (osname.startsWith("Mac")) {
return newInstance("jdk.net.MacOSXSocketOptions");
} else if (osname.startsWith("Windows")) {
return newInstance("jdk.net.WindowsSocketOptions");
} else {
return new PlatformSocketOptions();
}
Expand Down Expand Up @@ -399,7 +401,7 @@ boolean keepAliveOptionsSupported() {
return false;
}

void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
void setTcpKeepAliveProbes(int fd, final int value) throws SocketException {
throw new UnsupportedOperationException("unsupported TCP_KEEPCNT option");
}

Expand All @@ -415,7 +417,7 @@ void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
throw new UnsupportedOperationException("unsupported TCP_KEEPINTVL option");
}

int getTcpkeepAliveProbes(int fd) throws SocketException {
int getTcpKeepAliveProbes(int fd) throws SocketException {
throw new UnsupportedOperationException("unsupported TCP_KEEPCNT option");
}

Expand Down
92 changes: 92 additions & 0 deletions src/jdk.net/windows/classes/jdk/net/WindowsSocketOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2023, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.net;

import java.net.SocketException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import jdk.net.ExtendedSocketOptions.PlatformSocketOptions;


@SuppressWarnings("removal")
class WindowsSocketOptions extends PlatformSocketOptions {

public WindowsSocketOptions() {
}

@Override
boolean keepAliveOptionsSupported() {
return keepAliveOptionsSupported0();
}

@Override
void setTcpKeepAliveProbes(int fd, final int value) throws SocketException {
setTcpKeepAliveProbes0(fd, value);
}

@Override
int getTcpKeepAliveProbes(int fd) throws SocketException {
return getTcpKeepAliveProbes0(fd);
}

@Override
void setTcpKeepAliveTime(int fd, final int value) throws SocketException {
setTcpKeepAliveTime0(fd, value);
}

@Override
int getTcpKeepAliveTime(int fd) throws SocketException {
return getTcpKeepAliveTime0(fd);
}

@Override
void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
setTcpKeepAliveIntvl0(fd, value);
}

@Override
int getTcpKeepAliveIntvl(int fd) throws SocketException {
return getTcpKeepAliveIntvl0(fd);
}

private static native boolean keepAliveOptionsSupported0();
private static native void setTcpKeepAliveProbes0(int fd, int value) throws SocketException;
private static native int getTcpKeepAliveProbes0(int fd) throws SocketException;
private static native void setTcpKeepAliveTime0(int fd, int value) throws SocketException;
private static native int getTcpKeepAliveTime0(int fd) throws SocketException;
private static native void setTcpKeepAliveIntvl0(int fd, int value) throws SocketException;
private static native int getTcpKeepAliveIntvl0(int fd) throws SocketException;

static {
if (System.getSecurityManager() == null) {
System.loadLibrary("extnet");
} else {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
System.loadLibrary("extnet");
return null;
});
}
}
}
Loading

0 comments on commit 57bf412

Please sign in to comment.