Skip to content
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

Remove static field from c code base #11894

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 18 additions & 24 deletions transport-native-unix-common/src/main/c/netty_unix_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ static jmethodID datagramSocketAddrMethodId = NULL;
static jmethodID domainDatagramSocketAddrMethodId = NULL;
static jmethodID inetSocketAddrMethodId = NULL;
static jclass inetSocketAddressClass = NULL;
static int socketType = AF_INET;
static const unsigned char wildcardAddress[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char ipv4MappedWildcardAddress[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char ipv4MappedAddress[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
Expand Down Expand Up @@ -209,31 +208,27 @@ jbyteArray netty_unix_socket_createInetSocketAddressArray(JNIEnv* env, const str
return bArray;
}

static void netty_unix_socket_initialize(JNIEnv* env, jclass clazz, jboolean ipv4Preferred) {
static jboolean netty_unix_socket_isIPv6Preferred0(JNIEnv* env, jclass clazz, jboolean ipv4Preferred) {
if (ipv4Preferred) {
// User asked to use ipv4 explicitly.
socketType = AF_INET;
} else {
int fd = nettyNonBlockingSocket(AF_INET6, SOCK_STREAM, 0);
if (fd == -1) {
socketType = errno == EAFNOSUPPORT ? AF_INET : AF_INET6;
} else {
// Explicitly try to bind to ::1 to ensure IPV6 can really be used.
// See https://github.com/netty/netty/issues/7021.
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_addr.s6_addr[15] = 1; /* [::1]:0 */
int res = bind(fd, (struct sockaddr *)&addr, sizeof(addr));

close(fd);
socketType = res == 0 ? AF_INET6 : AF_INET;
}
return JNI_FALSE;
}
}

static jboolean netty_unix_socket_isIPv6Preferred(JNIEnv* env, jclass clazz) {
return socketType == AF_INET6;
int fd = nettyNonBlockingSocket(AF_INET6, SOCK_STREAM, 0);
if (fd == -1) {
return errno == EAFNOSUPPORT ? JNI_FALSE : JNI_TRUE;
}

// Explicitly try to bind to ::1 to ensure IPV6 can really be used.
// See https://github.com/netty/netty/issues/7021.
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_addr.s6_addr[15] = 1; /* [::1]:0 */
int res = bind(fd, (struct sockaddr *)&addr, sizeof(addr));

close(fd);
return res == 0 ? JNI_TRUE : JNI_FALSE;
}


Expand Down Expand Up @@ -1110,8 +1105,7 @@ static const JNINativeMethod fixed_method_table[] = {
{ "getSoLinger", "(I)I", (void *) netty_unix_socket_getSoLinger },
{ "getTrafficClass", "(IZ)I", (void *) netty_unix_socket_getTrafficClass },
{ "getSoError", "(I)I", (void *) netty_unix_socket_getSoError },
{ "initialize", "(Z)V", (void *) netty_unix_socket_initialize },
{ "isIPv6Preferred", "()Z", (void *) netty_unix_socket_isIPv6Preferred },
{ "isIPv6Preferred0", "(Z)Z", (void *) netty_unix_socket_isIPv6Preferred0 },
{ "isIPv6", "(I)Z", (void *) netty_unix_socket_isIPv6 },
{ "msgFastopen", "()I", (void *) netty_unit_socket_msgFastopen }
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.concurrent.atomic.AtomicBoolean;

import static io.netty.channel.unix.Errors.ERRNO_EAGAIN_NEGATIVE;
import static io.netty.channel.unix.Errors.ERRNO_EINPROGRESS_NEGATIVE;
Expand All @@ -45,6 +44,8 @@
*/
public class Socket extends FileDescriptor {

private static volatile boolean isIpv6Preferred;

@Deprecated
public static final int UDS_SUN_PATH_SIZE = 100;

Expand All @@ -54,7 +55,6 @@ public Socket(int fd) {
super(fd);
ipv6 = isIPv6(fd);
}

/**
* Returns {@code true} if we should use IPv6 internally, {@code false} otherwise.
*/
Expand Down Expand Up @@ -458,7 +458,11 @@ public final void setTrafficClass(int trafficClass) throws IOException {
setTrafficClass(fd, ipv6, trafficClass);
}

public static native boolean isIPv6Preferred();
public static boolean isIPv6Preferred() {
return isIpv6Preferred;
}

private static native boolean isIPv6Preferred0(boolean ipv4Preferred);

private static native boolean isIPv6(int fd);

Expand All @@ -469,8 +473,6 @@ public String toString() {
'}';
}

private static final AtomicBoolean INITIALIZED = new AtomicBoolean();

public static Socket newSocketStream() {
return new Socket(newSocketStream0());
}
Expand All @@ -488,9 +490,7 @@ public static Socket newSocketDomainDgram() {
}

public static void initialize() {
if (INITIALIZED.compareAndSet(false, true)) {
initialize(NetUtil.isIpV4StackPreferred());
}
isIpv6Preferred = isIPv6Preferred0(NetUtil.isIpV4StackPreferred());
}

protected static int newSocketStream0() {
Expand Down Expand Up @@ -599,5 +599,4 @@ private static native DomainDatagramSocketAddress recvFromAddressDomainSocket(
private static native void setSoLinger(int fd, int soLinger) throws IOException;
private static native void setBroadcast(int fd, int broadcast) throws IOException;
private static native void setTrafficClass(int fd, boolean ipv6, int trafficClass) throws IOException;
private static native void initialize(boolean ipv4Preferred);
}