8286781: Replace the deprecated/obsolete gethostbyname and inet_addr calls

Reviewed-by: andrew
Backport-of: d7298245d6759f62e253b5cf0df975db17fdbf82
This commit is contained in:
Thomas Fitzsimmons 2024-05-23 21:17:41 +00:00 committed by Andrew John Hughes
parent a64000108d
commit 4071b8c9be
5 changed files with 23 additions and 26 deletions

View File

@ -154,7 +154,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
JDKLIB_LIBS="$BASIC_JDKLIB_LIBS"

View File

@ -800,10 +800,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) {
::exit(num);
}

View File

@ -5797,10 +5797,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);
}

View File

@ -808,7 +808,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 (see JVM_RaiseSignal, JVM_RegisterSignal)
static void initialize_jdk_signal_support(TRAPS);

View File

@ -1071,7 +1071,7 @@ bufferedStream::~bufferedStream() {
#include <netinet/in.h>
#include <arpa/inet.h>
#elif defined(_WINDOWS)
#include <winsock2.h>
#include <Ws2tcpip.h>
#endif
// Network access
@ -1112,25 +1112,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 != NULL) {
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