Skip to content

Commit 4071b8c

Browse files
fitzsimgnu-andrew
authored andcommitted
8286781: Replace the deprecated/obsolete gethostbyname and inet_addr calls
Reviewed-by: andrew Backport-of: d7298245d6759f62e253b5cf0df975db17fdbf82
1 parent a640001 commit 4071b8c

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

make/autoconf/libraries.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES],
154154
if test "x$OPENJDK_TARGET_OS" = xwindows; then
155155
BASIC_JVM_LIBS="$BASIC_JVM_LIBS kernel32.lib user32.lib gdi32.lib winspool.lib \
156156
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib powrprof.lib uuid.lib \
157-
wsock32.lib winmm.lib version.lib psapi.lib"
157+
ws2_32.lib winmm.lib version.lib psapi.lib"
158158
fi
159159
160160
JDKLIB_LIBS="$BASIC_JDKLIB_LIBS"

src/hotspot/os/posix/os_posix.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,6 @@ int os::connect(int fd, struct sockaddr* him, socklen_t len) {
800800
RESTARTABLE_RETURN_INT(::connect(fd, him, len));
801801
}
802802

803-
struct hostent* os::get_host_by_name(char* name) {
804-
return ::gethostbyname(name);
805-
}
806-
807803
void os::exit(int num) {
808804
::exit(num);
809805
}

src/hotspot/os/windows/os_windows.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5797,10 +5797,6 @@ static jint initSock() {
57975797
return JNI_OK;
57985798
}
57995799

5800-
struct hostent* os::get_host_by_name(char* name) {
5801-
return (struct hostent*)gethostbyname(name);
5802-
}
5803-
58045800
int os::socket_close(int fd) {
58055801
return ::closesocket(fd);
58065802
}

src/hotspot/share/runtime/os.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ class os: AllStatic {
808808
static int send(int fd, char* buf, size_t nBytes, uint flags);
809809
static int raw_send(int fd, char* buf, size_t nBytes, uint flags);
810810
static int connect(int fd, struct sockaddr* him, socklen_t len);
811-
static struct hostent* get_host_by_name(char* name);
812811

813812
// Support for signals (see JVM_RaiseSignal, JVM_RegisterSignal)
814813
static void initialize_jdk_signal_support(TRAPS);

src/hotspot/share/utilities/ostream.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ bufferedStream::~bufferedStream() {
10711071
#include <netinet/in.h>
10721072
#include <arpa/inet.h>
10731073
#elif defined(_WINDOWS)
1074-
#include <winsock2.h>
1074+
#include <Ws2tcpip.h>
10751075
#endif
10761076

10771077
// Network access
@@ -1112,25 +1112,31 @@ void networkStream::close() {
11121112
}
11131113
}
11141114

1115-
bool networkStream::connect(const char *ip, short port) {
1115+
// host could be IP address, or a host name
1116+
bool networkStream::connect(const char *host, short port) {
11161117

1117-
struct sockaddr_in server;
1118-
server.sin_family = AF_INET;
1119-
server.sin_port = htons(port);
1118+
char s_port[6]; // 5 digits max plus terminator
1119+
int ret = os::snprintf(s_port, sizeof(s_port), "%hu", (unsigned short) port);
1120+
assert(ret > 0, "snprintf failed: %d", ret);
11201121

1121-
server.sin_addr.s_addr = inet_addr(ip);
1122-
if (server.sin_addr.s_addr == (uint32_t)-1) {
1123-
struct hostent* host = os::get_host_by_name((char*)ip);
1124-
if (host != NULL) {
1125-
memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length);
1126-
} else {
1127-
return false;
1128-
}
1129-
}
1122+
struct addrinfo* addr_info = nullptr;
1123+
struct addrinfo hints;
11301124

1125+
memset(&hints, 0, sizeof(hints));
1126+
hints.ai_family = AF_INET; // Allow IPv4 only
1127+
hints.ai_socktype = SOCK_STREAM; // TCP only
1128+
1129+
// getaddrinfo can resolve both an IP address and a host name
1130+
ret = getaddrinfo(host, s_port, &hints, &addr_info);
1131+
if (ret != 0) {
1132+
warning("networkStream::connect getaddrinfo for host %s and port %s failed: %s",
1133+
host, s_port, gai_strerror(ret));
1134+
return false;
1135+
}
11311136

1132-
int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
1133-
return (result >= 0);
1137+
ret = os::connect(_socket, addr_info->ai_addr, (socklen_t)addr_info->ai_addrlen);
1138+
freeaddrinfo(addr_info);
1139+
return (ret >= 0);
11341140
}
11351141

11361142
#endif

0 commit comments

Comments
 (0)