Skip to content

Commit e4fb228

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

File tree

8 files changed

+23
-38
lines changed

8 files changed

+23
-38
lines changed

make/autoconf/libraries.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES],
144144
if test "x$OPENJDK_TARGET_OS" = xwindows; then
145145
BASIC_JVM_LIBS="$BASIC_JVM_LIBS kernel32.lib user32.lib gdi32.lib winspool.lib \
146146
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib \
147-
wsock32.lib winmm.lib version.lib psapi.lib"
147+
ws2_32.lib winmm.lib version.lib psapi.lib"
148148
fi
149149
150150
JDKLIB_LIBS="$BASIC_JDKLIB_LIBS"

src/hotspot/os/aix/os_aix.inline.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ inline int os::connect(int fd, struct sockaddr *him, socklen_t len) {
142142
RESTARTABLE_RETURN_INT(::connect(fd, him, len));
143143
}
144144

145-
inline struct hostent* os::get_host_by_name(char* name) {
146-
return ::gethostbyname(name);
147-
}
148-
149145
inline bool os::supports_monotonic_clock() {
150146
// mread_real_time() is monotonic on AIX (see os::javaTimeNanos() comments)
151147
return true;

src/hotspot/os/bsd/os_bsd.inline.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
144144
RESTARTABLE_RETURN_INT(::connect(fd, him, len));
145145
}
146146

147-
inline struct hostent* os::get_host_by_name(char* name) {
148-
return ::gethostbyname(name);
149-
}
150-
151147
inline bool os::supports_monotonic_clock() {
152148
#ifdef __APPLE__
153149
return true;

src/hotspot/os/linux/os_linux.inline.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
136136
RESTARTABLE_RETURN_INT(::connect(fd, him, len));
137137
}
138138

139-
inline struct hostent* os::get_host_by_name(char* name) {
140-
return ::gethostbyname(name);
141-
}
142-
143139
inline bool os::supports_monotonic_clock() {
144140
return Linux::_clock_gettime != NULL;
145141
}

src/hotspot/os/solaris/os_solaris.inline.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ inline int os::socket(int domain, int type, int protocol) {
9292
return ::socket(domain, type, protocol);
9393
}
9494

95-
inline struct hostent* os::get_host_by_name(char* name) {
96-
return ::gethostbyname(name);
97-
}
98-
9995
inline bool os::supports_monotonic_clock() {
10096
// javaTimeNanos() is monotonic on Solaris, see getTimeNanos() comments
10197
return true;

src/hotspot/os/windows/os_windows.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5565,10 +5565,6 @@ static jint initSock() {
55655565
return JNI_OK;
55665566
}
55675567

5568-
struct hostent* os::get_host_by_name(char* name) {
5569-
return (struct hostent*)gethostbyname(name);
5570-
}
5571-
55725568
int os::socket_close(int fd) {
55735569
return ::closesocket(fd);
55745570
}

src/hotspot/share/runtime/os.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,6 @@ class os: AllStatic {
819819
static int send(int fd, char* buf, size_t nBytes, uint flags);
820820
static int raw_send(int fd, char* buf, size_t nBytes, uint flags);
821821
static int connect(int fd, struct sockaddr* him, socklen_t len);
822-
static struct hostent* get_host_by_name(char* name);
823822

824823
// Support for signals (see JVM_RaiseSignal, JVM_RegisterSignal)
825824
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
@@ -1072,7 +1072,7 @@ bufferedStream::~bufferedStream() {
10721072
#include <netinet/in.h>
10731073
#include <arpa/inet.h>
10741074
#elif defined(_WINDOWS)
1075-
#include <winsock2.h>
1075+
#include <Ws2tcpip.h>
10761076
#endif
10771077

10781078
// Network access
@@ -1113,25 +1113,31 @@ void networkStream::close() {
11131113
}
11141114
}
11151115

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

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

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

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

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

11371143
#endif

0 commit comments

Comments
 (0)