8286781: Replace the deprecated/obsolete gethostbyname and inet_addr calls
Co-authored-by: Daniel Jeliński <djelinski@openjdk.org> Reviewed-by: kbarrett, djelinski
This commit is contained in:
parent
5f1108f8f0
commit
d7298245d6
@ -476,8 +476,7 @@ AC_DEFUN([FLAGS_SETUP_CFLAGS_HELPER],
|
||||
ALWAYS_DEFINES_JDK="-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0602 \
|
||||
-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -DWIN32 -DIAL"
|
||||
ALWAYS_DEFINES_JVM="-DNOMINMAX -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0602 \
|
||||
-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE \
|
||||
-D_WINSOCK_DEPRECATED_NO_WARNINGS"
|
||||
-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE"
|
||||
fi
|
||||
|
||||
###############################################################################
|
||||
|
@ -171,7 +171,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
|
||||
LIB_SETUP_JVM_LIBS(BUILD)
|
||||
LIB_SETUP_JVM_LIBS(TARGET)
|
||||
|
@ -824,10 +824,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) {
|
||||
ALLOW_C_FUNCTION(::exit, ::exit(num);)
|
||||
}
|
||||
|
@ -5533,10 +5533,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);
|
||||
}
|
||||
|
@ -878,7 +878,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
|
||||
static void initialize_jdk_signal_support(TRAPS);
|
||||
|
@ -1089,7 +1089,7 @@ bufferedStream::~bufferedStream() {
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#elif defined(_WINDOWS)
|
||||
#include <winsock2.h>
|
||||
#include <Ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
// Network access
|
||||
@ -1130,25 +1130,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 != nullptr) {
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user