X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fgallium%2Fauxiliary%2Futil%2Fu_network.c;h=203205dc62150ebd216099c6c5d5f4761554dd32;hb=6321b1bd4005fcd05b2f1e6d16a974dba97f9415;hp=6269c72e121335bf03f36d89aae9247cfbb99be7;hpb=0d4a05445c6b47b93269a3829afbe509ffec4817;p=mesa.git diff --git a/src/gallium/auxiliary/util/u_network.c b/src/gallium/auxiliary/util/u_network.c index 6269c72e121..203205dc621 100644 --- a/src/gallium/auxiliary/util/u_network.c +++ b/src/gallium/auxiliary/util/u_network.c @@ -2,11 +2,15 @@ #include "pipe/p_compiler.h" #include "util/u_network.h" #include "util/u_debug.h" +#include "util/u_string.h" +#include #if defined(PIPE_SUBSYSTEM_WINDOWS_USER) # include # include -#elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_BSD) +# include +#elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || \ + defined(PIPE_OS_APPLE) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) # include # include # include @@ -54,7 +58,8 @@ u_socket_close(int s) if (s < 0) return; -#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_BSD) +#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) \ + || defined(PIPE_OS_APPLE) || defined(PIPE_OS_SOLARIS) shutdown(s, SHUT_RDWR); close(s); #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) @@ -108,28 +113,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,host->h_length); - sa.sin_family= host->h_addrtype; - sa.sin_port = htons(port); + util_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); @@ -169,7 +181,8 @@ u_socket_listen_on_port(uint16_t portnum) void u_socket_block(int s, boolean block) { -#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_BSD) +#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) \ + || defined(PIPE_OS_APPLE) || defined(PIPE_OS_SOLARIS) int old = fcntl(s, F_GETFL, 0); if (old == -1) return;