gallium: add PIPE_CAP_RESOURCE_FROM_USER_MEMORY_COMPUTE_ONLY
[mesa.git] / src / gallium / auxiliary / util / u_network.c
index bc4b758406772d4b83e643482cb478f270612a8e..31139f2983fb939fd8022e9a6012ee4f356f92b8 100644 (file)
@@ -2,11 +2,14 @@
 #include "pipe/p_compiler.h"
 #include "util/u_network.h"
 #include "util/u_debug.h"
+#include "util/u_string.h"
 
-#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
+#include <stdio.h>
+#if defined(PIPE_OS_WINDOWS)
 #  include <winsock2.h>
 #  include <windows.h>
-#elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU)
+#  include <ws2tcpip.h>
+#elif defined(PIPE_OS_UNIX)
 #  include <sys/socket.h>
 #  include <netinet/in.h>
 #  include <unistd.h>
@@ -17,9 +20,9 @@
 #endif
 
 boolean
-u_socket_init()
+u_socket_init(void)
 {
-#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
+#if defined(PIPE_OS_WINDOWS)
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
@@ -41,9 +44,9 @@ u_socket_init()
 }
 
 void
-u_socket_stop()
+u_socket_stop(void)
 {
-#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
+#if defined(PIPE_OS_WINDOWS)
    WSACleanup();
 #endif
 }
@@ -54,10 +57,10 @@ u_socket_close(int s)
    if (s < 0)
       return;
 
-#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU)
+#if defined(PIPE_OS_UNIX)
    shutdown(s, SHUT_RDWR);
    close(s);
-#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
+#elif defined(PIPE_OS_WINDOWS)
    shutdown(s, SD_BOTH);
    closesocket(s);
 #else
@@ -108,28 +111,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);
+   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);
@@ -157,7 +167,7 @@ u_socket_listen_on_port(uint16_t portnum)
       return -1;
    }
 
-   listen(s, 0);
+   listen(s, 1);
 
    return s;
 #else
@@ -169,7 +179,7 @@ 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)
+#if defined(PIPE_OS_UNIX)
    int old = fcntl(s, F_GETFL, 0);
    if (old == -1)
       return;
@@ -179,7 +189,7 @@ u_socket_block(int s, boolean block)
       fcntl(s, F_SETFL, old & ~O_NONBLOCK);
    else
       fcntl(s, F_SETFL, old | O_NONBLOCK);
-#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
+#elif defined(PIPE_OS_WINDOWS)
    u_long iMode = block ? 0 : 1;
    ioctlsocket(s, FIONBIO, &iMode);
 #else