summaryrefslogtreecommitdiffstats
path: root/src/gallium
diff options
context:
space:
mode:
authorBrian Paul <[email protected]>2017-10-17 13:11:03 -0600
committerBrian Paul <[email protected]>2017-10-23 15:10:01 -0600
commit623077393631d8c42d5e4aafeee446ca165e0b7e (patch)
tree33985953aea86eb6b293ea2f7ed3e1f32dc3a435 /src/gallium
parentfee9d05e2136b2b7c5a1ad2be7180b99f733f539 (diff)
gallium/util: replace gethostbyname() with getaddrinfo()
Compiling with MSVC options /we4995 /we4996 (a subset of /sdl) generates a warning that the gethostbyname() function is deprecated in favor of getaddrinfo() or GetAddrInfoW(). Replace the call with getaddrinfo(). Untested. There are no callers to u_socket_connect() in Gallium. Reviewed-by: Nicolai Hähnle <[email protected]>
Diffstat (limited to 'src/gallium')
-rw-r--r--src/gallium/auxiliary/util/u_network.c35
1 files changed, 22 insertions, 13 deletions
diff --git a/src/gallium/auxiliary/util/u_network.c b/src/gallium/auxiliary/util/u_network.c
index 45b3691f901..a7a4d28abcd 100644
--- a/src/gallium/auxiliary/util/u_network.c
+++ b/src/gallium/auxiliary/util/u_network.c
@@ -3,9 +3,11 @@
#include "util/u_network.h"
#include "util/u_debug.h"
+#include <stdio.h>
#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
# include <winsock2.h>
# include <windows.h>
+# include <ws2tcpip.h>
#elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || \
defined(PIPE_OS_APPLE) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS)
# include <sys/socket.h>
@@ -110,28 +112,35 @@ int
u_socket_connect(const char *hostname, uint16_t port)
{
#if defined(PIPE_HAVE_SOCKETS)
- int s;
- struct sockaddr_in sa;
- struct hostent *host = NULL;
+ int s, r;
+ struct addrinfo hints, *addr;
+ char portString[20];
- memset(&sa, 0, sizeof(struct sockaddr_in));
- host = gethostbyname(hostname);
- if (!host)
- return -1;
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
+ hints.ai_socktype = SOCK_STREAM;
- memcpy((char *)&sa.sin_addr,host->h_addr_list[0],host->h_length);
- sa.sin_family= host->h_addrtype;
- sa.sin_port = htons(port);
+ snprintf(portString, sizeof(portString), "%d", port);
- s = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP);
- if (s < 0)
+ r = getaddrinfo(hostname, portString, NULL, &addr);
+ if (r != 0) {
return -1;
+ }
- if (connect(s, (struct sockaddr *)&sa, sizeof(sa))) {
+ s = socket(addr->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ if (s < 0) {
+ freeaddrinfo(addr);
+ return -1;
+ }
+
+ if (connect(s, addr->ai_addr, (int) addr->ai_addrlen)) {
u_socket_close(s);
+ freeaddrinfo(addr);
return -1;
}
+ freeaddrinfo(addr);
+
return s;
#else
assert(0);