8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
authorgadams
Thu, 08 Feb 2018 09:24:44 -0500
changeset 48767 0c6ce8fdb50a
parent 48766 2c4d1993b5f9
child 48769 266539dc2776
8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() Reviewed-by: erikj, clanger, chegar
make/autoconf/flags.m4
src/java.base/windows/native/libnet/net_util_md.c
src/java.base/windows/native/libnio/ch/SocketDispatcher.c
src/jdk.jdwp.agent/share/native/libdt_socket/socketTransport.c
src/jdk.jdwp.agent/share/native/libdt_socket/sysSocket.h
src/jdk.jdwp.agent/unix/native/libdt_socket/socket_md.c
src/jdk.jdwp.agent/windows/native/libdt_socket/socket_md.c
--- a/make/autoconf/flags.m4	Thu Feb 08 12:01:32 2018 -0800
+++ b/make/autoconf/flags.m4	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -918,7 +918,6 @@
         -MD -Zc:wchar_t- -W3 -wd4800 \
         -DWIN32_LEAN_AND_MEAN \
         -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE \
-        -D_WINSOCK_DEPRECATED_NO_WARNINGS \
         -DWIN32 -DIAL"
     if test "x$OPENJDK_$1_CPU" = xx86_64; then
       $2COMMON_CCXXFLAGS_JDK="[$]$2COMMON_CCXXFLAGS_JDK -D_AMD64_ -Damd64"
--- a/src/java.base/windows/native/libnet/net_util_md.c	Thu Feb 08 12:01:32 2018 -0800
+++ b/src/java.base/windows/native/libnet/net_util_md.c	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -525,7 +525,7 @@
     int len = sizeof (l);
     if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) {
         if (l.l_onoff == 0) {
-            WSASendDisconnect(fd, NULL);
+            shutdown(fd, SD_SEND);
         }
     }
     ret = closesocket (fd);
--- a/src/java.base/windows/native/libnio/ch/SocketDispatcher.c	Thu Feb 08 12:01:32 2018 -0800
+++ b/src/java.base/windows/native/libnio/ch/SocketDispatcher.c	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -272,7 +272,7 @@
     int len = sizeof(l);
     if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) {
         if (l.l_onoff == 0) {
-            WSASendDisconnect(fd, NULL);
+            shutdown(fd, SD_SEND);
         }
     }
 }
--- a/src/jdk.jdwp.agent/share/native/libdt_socket/socketTransport.c	Thu Feb 08 12:01:32 2018 -0800
+++ b/src/jdk.jdwp.agent/share/native/libdt_socket/socketTransport.c	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -228,6 +228,7 @@
     // it looks up "localhost" and returns 127.0.0.1 if lookup
     // fails.
     struct addrinfo hints, *res = NULL;
+    uint32_t addr;
     int err;
 
     // Use portable way to initialize the structure
@@ -241,7 +242,9 @@
 
     // getaddrinfo might return more than one address
     // but we are using first one only
-    return ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
+    addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
+    freeaddrinfo(res);
+    return addr;
 }
 
 static int
@@ -300,7 +303,7 @@
         char *buf;
         char *hostname;
         uint32_t addr;
-
+        int ai;
         buf = (*callback->alloc)((int)strlen(address) + 1);
         if (buf == NULL) {
             RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
@@ -315,16 +318,25 @@
          */
         addr = dbgsysInetAddr(hostname);
         if (addr == 0xffffffff) {
-            struct hostent *hp = dbgsysGetHostByName(hostname);
-            if (hp == NULL) {
+            struct addrinfo hints;
+            struct addrinfo *results = NULL;
+            memset (&hints, 0, sizeof(hints));
+            hints.ai_family = AF_INET;
+            hints.ai_socktype = SOCK_STREAM;
+            hints.ai_protocol = IPPROTO_TCP;
+
+            ai = dbgsysGetAddrInfo(hostname, NULL, &hints, &results);
+
+            if (ai != 0) {
                 /* don't use RETURN_IO_ERROR as unknown host is normal */
-                setLastError(0, "gethostbyname: unknown host");
+                setLastError(0, "getaddrinfo: unknown host");
                 (*callback->free)(buf);
                 return JDWPTRANSPORT_ERROR_IO_ERROR;
             }
 
             /* lookup was successful */
-            memcpy(&(sa->sin_addr), hp->h_addr_list[0], hp->h_length);
+            sa->sin_addr =  ((struct sockaddr_in *)results->ai_addr)->sin_addr;
+            freeaddrinfo(results);
         } else {
             sa->sin_addr.s_addr = addr;
         }
--- a/src/jdk.jdwp.agent/share/native/libdt_socket/sysSocket.h	Thu Feb 08 12:01:32 2018 -0800
+++ b/src/jdk.jdwp.agent/share/native/libdt_socket/sysSocket.h	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
 int dbgsysListen(int fd, int backlog);
 int dbgsysRecv(int fd, char *buf, size_t nBytes, int flags);
 int dbgsysSend(int fd, char *buf, size_t nBytes, int flags);
-struct hostent *dbgsysGetHostByName(char *hostname);
+int dbgsysGetAddrInfo(char *hostname, char *service, struct addrinfo *hints, struct addrinfo **results);
 int dbgsysSocket(int domain, int type, int protocol);
 int dbgsysBind(int fd, struct sockaddr *name, socklen_t namelen);
 int dbgsysSetSocketOption(int fd, jint cmd, jboolean on, jvalue value);
--- a/src/jdk.jdwp.agent/unix/native/libdt_socket/socket_md.c	Thu Feb 08 12:01:32 2018 -0800
+++ b/src/jdk.jdwp.agent/unix/native/libdt_socket/socket_md.c	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -126,9 +126,11 @@
     return rv;
 }
 
-struct hostent *
-dbgsysGetHostByName(char *hostname) {
-    return gethostbyname(hostname);
+int
+dbgsysGetAddrInfo(char *hostname, char *service,
+                  struct addrinfo *hints,
+                  struct addrinfo **results) {
+    return getaddrinfo(hostname, service, hints, results);
 }
 
 unsigned short
--- a/src/jdk.jdwp.agent/windows/native/libdt_socket/socket_md.c	Thu Feb 08 12:01:32 2018 -0800
+++ b/src/jdk.jdwp.agent/windows/native/libdt_socket/socket_md.c	Thu Feb 08 09:24:44 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
  */
 #include <windows.h>
 #include <winsock2.h>
+#include <ws2tcpip.h>
 
 #include "sysSocket.h"
 #include "socketTransport.h"
@@ -197,9 +198,11 @@
     return send(fd, buf, (int)nBytes, flags);
 }
 
-struct hostent *
-dbgsysGetHostByName(char *hostname) {
-    return gethostbyname(hostname);
+int
+dbgsysGetAddrInfo(char *hostname, char *service,
+                  struct addrinfo *hints,
+                  struct addrinfo **result) {
+  return getaddrinfo(hostname, service, hints, result);
 }
 
 unsigned short
@@ -223,7 +226,7 @@
 
     if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) {
         if (l.l_onoff == 0) {
-            WSASendDisconnect(fd, NULL);
+            shutdown(fd, SD_SEND);
         }
     }
     return closesocket(fd);
@@ -239,7 +242,11 @@
 
 uint32_t
 dbgsysInetAddr(const char* cp) {
-    return (uint32_t)inet_addr(cp);
+    uint32_t addr;
+    if (inet_pton(AF_INET, cp, &addr) < 1) {
+      return -1;
+    }
+    return addr;
 }
 
 uint32_t