Skip to content

Commit d729824

Browse files
David Holmesdjelinski
andcommitted
8286781: Replace the deprecated/obsolete gethostbyname and inet_addr calls
Co-authored-by: Daniel Jeliński <djelinski@openjdk.org> Reviewed-by: kbarrett, djelinski
1 parent 5f1108f commit d729824

File tree

6 files changed

+24
-28
lines changed

6 files changed

+24
-28
lines changed

make/autoconf/flags-cflags.m4

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,7 @@ AC_DEFUN([FLAGS_SETUP_CFLAGS_HELPER],
476476
ALWAYS_DEFINES_JDK="-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0602 \
477477
-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -DWIN32 -DIAL"
478478
ALWAYS_DEFINES_JVM="-DNOMINMAX -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0602 \
479-
-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE \
480-
-D_WINSOCK_DEPRECATED_NO_WARNINGS"
479+
-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE"
481480
fi
482481
483482
###############################################################################

make/autoconf/libraries.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES],
171171
if test "x$OPENJDK_TARGET_OS" = xwindows; then
172172
BASIC_JVM_LIBS="$BASIC_JVM_LIBS kernel32.lib user32.lib gdi32.lib winspool.lib \
173173
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib powrprof.lib uuid.lib \
174-
wsock32.lib winmm.lib version.lib psapi.lib"
174+
ws2_32.lib winmm.lib version.lib psapi.lib"
175175
fi
176176
LIB_SETUP_JVM_LIBS(BUILD)
177177
LIB_SETUP_JVM_LIBS(TARGET)

src/hotspot/os/posix/os_posix.cpp

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

827-
struct hostent* os::get_host_by_name(char* name) {
828-
return ::gethostbyname(name);
829-
}
830-
831827
void os::exit(int num) {
832828
ALLOW_C_FUNCTION(::exit, ::exit(num);)
833829
}

src/hotspot/os/windows/os_windows.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5533,10 +5533,6 @@ static jint initSock() {
55335533
return JNI_OK;
55345534
}
55355535

5536-
struct hostent* os::get_host_by_name(char* name) {
5537-
return (struct hostent*)gethostbyname(name);
5538-
}
5539-
55405536
int os::socket_close(int fd) {
55415537
return ::closesocket(fd);
55425538
}

src/hotspot/share/runtime/os.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,6 @@ class os: AllStatic {
878878
static int send(int fd, char* buf, size_t nBytes, uint flags);
879879
static int raw_send(int fd, char* buf, size_t nBytes, uint flags);
880880
static int connect(int fd, struct sockaddr* him, socklen_t len);
881-
static struct hostent* get_host_by_name(char* name);
882881

883882
// Support for signals
884883
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
@@ -1089,7 +1089,7 @@ bufferedStream::~bufferedStream() {
10891089
#include <netdb.h>
10901090
#include <arpa/inet.h>
10911091
#elif defined(_WINDOWS)
1092-
#include <winsock2.h>
1092+
#include <Ws2tcpip.h>
10931093
#endif
10941094

10951095
// Network access
@@ -1130,25 +1130,31 @@ void networkStream::close() {
11301130
}
11311131
}
11321132

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

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

1139-
server.sin_addr.s_addr = inet_addr(ip);
1140-
if (server.sin_addr.s_addr == (uint32_t)-1) {
1141-
struct hostent* host = os::get_host_by_name((char*)ip);
1142-
if (host != nullptr) {
1143-
memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length);
1144-
} else {
1145-
return false;
1146-
}
1147-
}
1140+
struct addrinfo* addr_info = nullptr;
1141+
struct addrinfo hints;
11481142

1143+
memset(&hints, 0, sizeof(hints));
1144+
hints.ai_family = AF_INET; // Allow IPv4 only
1145+
hints.ai_socktype = SOCK_STREAM; // TCP only
1146+
1147+
// getaddrinfo can resolve both an IP address and a host name
1148+
ret = getaddrinfo(host, s_port, &hints, &addr_info);
1149+
if (ret != 0) {
1150+
warning("networkStream::connect getaddrinfo for host %s and port %s failed: %s",
1151+
host, s_port, gai_strerror(ret));
1152+
return false;
1153+
}
11491154

1150-
int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
1151-
return (result >= 0);
1155+
ret = os::connect(_socket, addr_info->ai_addr, (socklen_t)addr_info->ai_addrlen);
1156+
freeaddrinfo(addr_info);
1157+
return (ret >= 0);
11521158
}
11531159

11541160
#endif

0 commit comments

Comments
 (0)