From d7298245d6759f62e253b5cf0df975db17fdbf82 Mon Sep 17 00:00:00 2001 From: David Holmes Date: Tue, 7 Mar 2023 22:56:33 +0000 Subject: [PATCH] 8286781: Replace the deprecated/obsolete gethostbyname and inet_addr calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel JeliƄski Reviewed-by: kbarrett, djelinski --- make/autoconf/flags-cflags.m4 | 3 +- make/autoconf/libraries.m4 | 2 +- src/hotspot/os/posix/os_posix.cpp | 4 --- src/hotspot/os/windows/os_windows.cpp | 4 --- src/hotspot/share/runtime/os.hpp | 1 - src/hotspot/share/utilities/ostream.cpp | 38 ++++++++++++++----------- 6 files changed, 24 insertions(+), 28 deletions(-) diff --git a/make/autoconf/flags-cflags.m4 b/make/autoconf/flags-cflags.m4 index 1fdd0143adf..4b524c2b42c 100644 --- a/make/autoconf/flags-cflags.m4 +++ b/make/autoconf/flags-cflags.m4 @@ -476,8 +476,7 @@ AC_DEFUN([FLAGS_SETUP_CFLAGS_HELPER], ALWAYS_DEFINES_JDK="-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0602 \ -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -DWIN32 -DIAL" ALWAYS_DEFINES_JVM="-DNOMINMAX -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0602 \ - -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE \ - -D_WINSOCK_DEPRECATED_NO_WARNINGS" + -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE" fi ############################################################################### diff --git a/make/autoconf/libraries.m4 b/make/autoconf/libraries.m4 index a50ce573181..9e746b470c9 100644 --- a/make/autoconf/libraries.m4 +++ b/make/autoconf/libraries.m4 @@ -171,7 +171,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES], if test "x$OPENJDK_TARGET_OS" = xwindows; then BASIC_JVM_LIBS="$BASIC_JVM_LIBS kernel32.lib user32.lib gdi32.lib winspool.lib \ comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib powrprof.lib uuid.lib \ - wsock32.lib winmm.lib version.lib psapi.lib" + ws2_32.lib winmm.lib version.lib psapi.lib" fi LIB_SETUP_JVM_LIBS(BUILD) LIB_SETUP_JVM_LIBS(TARGET) diff --git a/src/hotspot/os/posix/os_posix.cpp b/src/hotspot/os/posix/os_posix.cpp index 966aa37b23d..9bd67526600 100644 --- a/src/hotspot/os/posix/os_posix.cpp +++ b/src/hotspot/os/posix/os_posix.cpp @@ -824,10 +824,6 @@ int os::connect(int fd, struct sockaddr* him, socklen_t len) { RESTARTABLE_RETURN_INT(::connect(fd, him, len)); } -struct hostent* os::get_host_by_name(char* name) { - return ::gethostbyname(name); -} - void os::exit(int num) { ALLOW_C_FUNCTION(::exit, ::exit(num);) } diff --git a/src/hotspot/os/windows/os_windows.cpp b/src/hotspot/os/windows/os_windows.cpp index 62e25584137..686e27add56 100644 --- a/src/hotspot/os/windows/os_windows.cpp +++ b/src/hotspot/os/windows/os_windows.cpp @@ -5533,10 +5533,6 @@ static jint initSock() { return JNI_OK; } -struct hostent* os::get_host_by_name(char* name) { - return (struct hostent*)gethostbyname(name); -} - int os::socket_close(int fd) { return ::closesocket(fd); } diff --git a/src/hotspot/share/runtime/os.hpp b/src/hotspot/share/runtime/os.hpp index 44747b704d4..c7870009cc7 100644 --- a/src/hotspot/share/runtime/os.hpp +++ b/src/hotspot/share/runtime/os.hpp @@ -878,7 +878,6 @@ class os: AllStatic { static int send(int fd, char* buf, size_t nBytes, uint flags); static int raw_send(int fd, char* buf, size_t nBytes, uint flags); static int connect(int fd, struct sockaddr* him, socklen_t len); - static struct hostent* get_host_by_name(char* name); // Support for signals static void initialize_jdk_signal_support(TRAPS); diff --git a/src/hotspot/share/utilities/ostream.cpp b/src/hotspot/share/utilities/ostream.cpp index b9a37f7ca61..5b2b8e2c2be 100644 --- a/src/hotspot/share/utilities/ostream.cpp +++ b/src/hotspot/share/utilities/ostream.cpp @@ -1089,7 +1089,7 @@ bufferedStream::~bufferedStream() { #include #include #elif defined(_WINDOWS) -#include +#include #endif // Network access @@ -1130,25 +1130,31 @@ void networkStream::close() { } } -bool networkStream::connect(const char *ip, short port) { +// host could be IP address, or a host name +bool networkStream::connect(const char *host, short port) { - struct sockaddr_in server; - server.sin_family = AF_INET; - server.sin_port = htons(port); + char s_port[6]; // 5 digits max plus terminator + int ret = os::snprintf(s_port, sizeof(s_port), "%hu", (unsigned short) port); + assert(ret > 0, "snprintf failed: %d", ret); - server.sin_addr.s_addr = inet_addr(ip); - if (server.sin_addr.s_addr == (uint32_t)-1) { - struct hostent* host = os::get_host_by_name((char*)ip); - if (host != nullptr) { - memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length); - } else { - return false; - } + struct addrinfo* addr_info = nullptr; + struct addrinfo hints; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // Allow IPv4 only + hints.ai_socktype = SOCK_STREAM; // TCP only + + // getaddrinfo can resolve both an IP address and a host name + ret = getaddrinfo(host, s_port, &hints, &addr_info); + if (ret != 0) { + warning("networkStream::connect getaddrinfo for host %s and port %s failed: %s", + host, s_port, gai_strerror(ret)); + return false; } - - int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in)); - return (result >= 0); + ret = os::connect(_socket, addr_info->ai_addr, (socklen_t)addr_info->ai_addrlen); + freeaddrinfo(addr_info); + return (ret >= 0); } #endif