Skip to content

Commit

Permalink
8286781: Replace the deprecated/obsolete gethostbyname and inet_addr …
Browse files Browse the repository at this point in the history
…calls

Reviewed-by: andrew
Backport-of: d7298245d6759f62e253b5cf0df975db17fdbf82
  • Loading branch information
fitzsim authored and gnu-andrew committed May 23, 2024
1 parent a640001 commit 4071b8c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion make/autoconf/libraries.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions src/hotspot/os/posix/os_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 0 additions & 4 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 22 additions & 16 deletions src/hotspot/share/utilities/ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

1 comment on commit 4071b8c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.