diff --git a/src/java.base/share/native/libnet/net_util.c b/src/java.base/share/native/libnet/net_util.c index d5e94f594fc5c..a198152563a09 100644 --- a/src/java.base/share/native/libnet/net_util.c +++ b/src/java.base/share/native/libnet/net_util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024, 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 @@ -63,6 +63,10 @@ DEF_JNI_OnLoad(JavaVM *vm, void *reserved) return JNI_EVERSION; /* JNI version not supported */ } + if (NET_PlatformInit() != 0) { + return JNI_ERR; + } + iCls = (*env)->FindClass(env, "java/lang/Boolean"); CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2); mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z"); diff --git a/src/java.base/share/native/libnet/net_util.h b/src/java.base/share/native/libnet/net_util.h index 06cf448a01d8a..a1fa9571821ca 100644 --- a/src/java.base/share/native/libnet/net_util.h +++ b/src/java.base/share/native/libnet/net_util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, 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 @@ -155,6 +155,8 @@ int NET_IsEqual(jbyte* caddr1, jbyte* caddr2); int NET_IsZeroAddr(jbyte* caddr); +int NET_PlatformInit(); + /* Socket operations * * These work just like the system calls, except that they may do some diff --git a/src/java.base/unix/native/libnet/net_util_md.c b/src/java.base/unix/native/libnet/net_util_md.c index 426a3de284502..1713d0a8ccdbd 100644 --- a/src/java.base/unix/native/libnet/net_util_md.c +++ b/src/java.base/unix/native/libnet/net_util_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, 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 @@ -50,6 +50,15 @@ #define IPV6_FLOWINFO_SEND 33 #endif +/* Perform platform specific initialization. + * Returns 0 on success, non-0 on failure */ +int +NET_PlatformInit() +{ + // Not needed on unix + return 0; +} + void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, const char *defaultDetail) { diff --git a/src/java.base/windows/native/libnet/net_util_md.c b/src/java.base/windows/native/libnet/net_util_md.c index 36927beff3cb9..410d141e7909d 100644 --- a/src/java.base/windows/native/libnet/net_util_md.c +++ b/src/java.base/windows/native/libnet/net_util_md.c @@ -101,29 +101,21 @@ static struct { { WSA_OPERATION_ABORTED, 0, "Overlapped operation aborted" }, }; -/* - * Initialize Windows Sockets API support - */ -BOOL WINAPI -DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) +static void at_exit_callback(void) { - WSADATA wsadata; + WSACleanup(); +} - switch (reason) { - case DLL_PROCESS_ATTACH: - if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) { - return FALSE; - } - break; +/* Perform platform specific initialization. + * Returns 0 on success, non-0 on failure */ +int +NET_PlatformInit() +{ + WSADATA wsadata; - case DLL_PROCESS_DETACH: - WSACleanup(); - break; + atexit(at_exit_callback); - default: - break; - } - return TRUE; + return WSAStartup(MAKEWORD(2,2), &wsadata); } /*