[Spice-devel] [PATCH spice-server v3 09/32] reds: Use socket compatibility layer

Frediano Ziglio fziglio at redhat.com
Mon Jan 7 18:01:52 UTC 2019


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/red-common.h   |  4 +-
 server/reds-private.h |  4 +-
 server/reds.c         | 94 +++++++++++++++++++++++--------------------
 server/reds.h         |  2 +-
 4 files changed, 56 insertions(+), 48 deletions(-)

diff --git a/server/red-common.h b/server/red-common.h
index 181ed283..eb2fef96 100644
--- a/server/red-common.h
+++ b/server/red-common.h
@@ -35,6 +35,7 @@
 
 #include "spice.h"
 #include "utils.h"
+#include "sys-socket.h"
 
 #define SPICE_UPCAST(type, ptr) \
     (verify_expr(SPICE_OFFSETOF(type, base) == 0,SPICE_CONTAINEROF(ptr, type, base)))
@@ -47,7 +48,8 @@ struct SpiceCoreInterfaceInternal {
     void (*timer_cancel)(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer);
     void (*timer_remove)(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer);
 
-    SpiceWatch *(*watch_add)(const SpiceCoreInterfaceInternal *iface, int fd, int event_mask, SpiceWatchFunc func, void *opaque);
+    SpiceWatch *(*watch_add)(const SpiceCoreInterfaceInternal *iface, socket_t fd, int event_mask,
+                             SpiceWatchFunc func, void *opaque);
     void (*watch_update_mask)(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch, int event_mask);
     void (*watch_remove)(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch);
 
diff --git a/server/reds-private.h b/server/reds-private.h
index 920edc5c..17e45caf 100644
--- a/server/reds-private.h
+++ b/server/reds-private.h
@@ -75,8 +75,8 @@ typedef struct RedServerConfig RedServerConfig;
 
 struct RedsState {
     RedServerConfig *config;
-    int listen_socket;
-    int secure_listen_socket;
+    socket_t listen_socket;
+    socket_t secure_listen_socket;
     SpiceWatch *listen_watch;
     SpiceWatch *secure_listen_watch;
     RedCharDeviceVDIPort *agent_dev;
diff --git a/server/reds.c b/server/reds.c
index ec7fbc38..0421fd76 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -106,9 +106,10 @@ static void adapter_timer_remove(const SpiceCoreInterfaceInternal *iface, SpiceT
 }
 
 static SpiceWatch *adapter_watch_add(const SpiceCoreInterfaceInternal *iface,
-                                     int fd, int event_mask, SpiceWatchFunc func, void *opaque)
+                                     socket_t fd, int event_mask, SpiceWatchFunc func, void *opaque)
 {
-    return iface->public_interface->watch_add(fd, event_mask, func, opaque);
+    // note: Qemu API is fine having a SOCKET on Windows
+    return iface->public_interface->watch_add(socket_get_raw(fd), event_mask, func, opaque);
 }
 
 static void adapter_watch_update_mask(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch, int event_mask)
@@ -168,7 +169,7 @@ struct RedServerConfig {
 
     int spice_port;
     int spice_secure_port;
-    int spice_listen_socket_fd;
+    socket_t spice_listen_socket_fd;
     char spice_addr[256];
     int spice_family;
     TicketAuthentication taTicket;
@@ -2383,7 +2384,7 @@ static void reds_handle_ssl_accept(int fd, int event, void *data)
 
 #define KEEPALIVE_TIMEOUT (10*60)
 
-static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
+static RedLinkInfo *reds_init_client_connection(RedsState *reds, socket_t socket)
 {
     RedLinkInfo *link;
 
@@ -2414,7 +2415,7 @@ error:
 }
 
 
-static RedLinkInfo *reds_init_client_ssl_connection(RedsState *reds, int socket)
+static RedLinkInfo *reds_init_client_ssl_connection(RedsState *reds, socket_t socket)
 {
     RedLinkInfo *link;
     RedStreamSslStatus ssl_status;
@@ -2447,7 +2448,7 @@ static RedLinkInfo *reds_init_client_ssl_connection(RedsState *reds, int socket)
 error:
     /* close the stream but do not close the socket, this API is
      * supposed to not close it if it fails */
-    link->stream->socket = -1;
+    link->stream->socket = SOCKET_INVALID;
     reds_link_free(link);
     return NULL;
 }
@@ -2459,11 +2460,12 @@ static void reds_accept_ssl_connection(int fd, int event, void *data)
     int socket;
 
     if ((socket = accept(fd, NULL, 0)) == -1) {
+        socket_win32_set_errno();
         spice_warning("accept failed, %s", strerror(errno));
         return;
     }
 
-    if (!(link = reds_init_client_ssl_connection(reds, socket))) {
+    if (!(link = reds_init_client_ssl_connection(reds, SOCKET_FROM_INT(socket)))) {
         close(socket);
         return;
     }
@@ -2476,6 +2478,7 @@ static void reds_accept(int fd, int event, void *data)
     int socket;
 
     if ((socket = accept(fd, NULL, 0)) == -1) {
+        socket_win32_set_errno();
         spice_warning("accept failed, %s", strerror(errno));
         return;
     }
@@ -2489,7 +2492,7 @@ SPICE_GNUC_VISIBLE int spice_server_add_client(SpiceServer *reds, int socket, in
 {
     RedLinkInfo *link;
 
-    if (!(link = reds_init_client_connection(reds, socket))) {
+    if (!(link = reds_init_client_connection(reds, SOCKET_FROM_INT(socket)))) {
         spice_warning("accept failed");
         return -1;
     }
@@ -2505,7 +2508,7 @@ SPICE_GNUC_VISIBLE int spice_server_add_ssl_client(SpiceServer *reds, int socket
 {
     RedLinkInfo *link;
 
-    if (!(link = reds_init_client_ssl_connection(reds, socket))) {
+    if (!(link = reds_init_client_ssl_connection(reds, SOCKET_FROM_INT(socket)))) {
         return -1;
     }
 
@@ -2514,29 +2517,32 @@ SPICE_GNUC_VISIBLE int spice_server_add_ssl_client(SpiceServer *reds, int socket
 }
 
 
-static int reds_init_socket(const char *addr, int portnr, int family)
+static socket_t reds_init_socket(const char *addr, int portnr, int family)
 {
     static const int on=1, off=0;
     struct addrinfo ai,*res,*e;
     char port[33];
-    int slisten, rc, len;
+    int rc;
+    socket_t slisten;
 
     if (family == AF_UNIX) {
+        int len;
         struct sockaddr_un local = { 0, };
 
-        if ((slisten = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+        slisten = socket_new(AF_UNIX, SOCK_STREAM, 0);
+        if (!socket_is_valid(slisten)) {
             perror("socket");
-            return -1;
+            return SOCKET_INVALID;
         }
 
         local.sun_family = AF_UNIX;
         g_strlcpy(local.sun_path, addr, sizeof(local.sun_path));
         unlink(local.sun_path);
         len = SUN_LEN(&local);
-        if (bind(slisten, (struct sockaddr *)&local, len) == -1) {
+        if (socket_bind(slisten, (struct sockaddr *)&local, len) == -1) {
             perror("bind");
-            close(slisten);
-            return -1;
+            socket_close(slisten);
+            return SOCKET_INVALID;
         }
 
         goto listen;
@@ -2552,24 +2558,23 @@ static int reds_init_socket(const char *addr, int portnr, int family)
     if (rc != 0) {
         spice_warning("getaddrinfo(%s,%s): %s", addr, port,
                       gai_strerror(rc));
-        return -1;
+        return SOCKET_INVALID;
     }
 
     for (e = res; e != NULL; e = e->ai_next) {
-        slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
-        if (slisten < 0) {
+        slisten = socket_new(e->ai_family, e->ai_socktype, e->ai_protocol);
+        if (!socket_is_valid(slisten)) {
             continue;
         }
 
-        setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
+        socket_setopt(slisten, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on));
 #ifdef IPV6_V6ONLY
         if (e->ai_family == PF_INET6) {
             /* listen on both ipv4 and ipv6 */
-            setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
-                       sizeof(off));
+            socket_setopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&off, sizeof(off));
         }
 #endif
-        if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
+        if (socket_bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
             char uaddr[INET6_ADDRSTRLEN+1];
             char uport[33];
             rc = getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
@@ -2583,17 +2588,17 @@ static int reds_init_socket(const char *addr, int portnr, int family)
             freeaddrinfo(res);
             goto listen;
         }
-        close(slisten);
+        socket_close(slisten);
     }
     spice_warning("binding socket to %s:%d failed", addr, portnr);
     freeaddrinfo(res);
-    return -1;
+    return SOCKET_INVALID;
 
 listen:
-    if (listen(slisten, SOMAXCONN) != 0) {
+    if (socket_listen(slisten, SOMAXCONN) != 0) {
         spice_warning("listen: %s", strerror(errno));
-        close(slisten);
-        return -1;
+        socket_close(slisten);
+        return SOCKET_INVALID;
     }
     return slisten;
 }
@@ -2627,19 +2632,20 @@ void reds_set_client_mm_time_latency(RedsState *reds, RedClient *client, uint32_
 
 static void reds_cleanup_net(SpiceServer *reds)
 {
-    if (reds->listen_socket != -1) {
+    if (socket_is_valid(reds->listen_socket)) {
        reds_core_watch_remove(reds, reds->listen_watch);
-       if (reds->config->spice_listen_socket_fd != reds->listen_socket) {
-          close(reds->listen_socket);
+       if (socket_get_raw(reds->config->spice_listen_socket_fd) !=
+           socket_get_raw(reds->listen_socket)) {
+          socket_close(reds->listen_socket);
        }
        reds->listen_watch = NULL;
-       reds->listen_socket = -1;
+       reds->listen_socket = SOCKET_INVALID;
     }
-    if (reds->secure_listen_socket != -1) {
+    if (socket_is_valid(reds->secure_listen_socket)) {
        reds_core_watch_remove(reds, reds->secure_listen_watch);
-       close(reds->secure_listen_socket);
+       socket_close(reds->secure_listen_socket);
        reds->secure_listen_watch = NULL;
-       reds->secure_listen_socket = -1;
+       reds->secure_listen_socket = SOCKET_INVALID;
     }
 }
 
@@ -2647,7 +2653,7 @@ static int reds_init_net(RedsState *reds)
 {
     if (reds->config->spice_port != -1 || reds->config->spice_family == AF_UNIX) {
         reds->listen_socket = reds_init_socket(reds->config->spice_addr, reds->config->spice_port, reds->config->spice_family);
-        if (-1 == reds->listen_socket) {
+        if (!socket_is_valid(reds->listen_socket)) {
             return -1;
         }
         reds->listen_watch = reds_core_watch_add(reds, reds->listen_socket,
@@ -2661,7 +2667,7 @@ static int reds_init_net(RedsState *reds)
     if (reds->config->spice_secure_port != -1) {
         reds->secure_listen_socket = reds_init_socket(reds->config->spice_addr, reds->config->spice_secure_port,
                                                       reds->config->spice_family);
-        if (-1 == reds->secure_listen_socket) {
+        if (!socket_is_valid(reds->secure_listen_socket)) {
             return -1;
         }
         reds->secure_listen_watch = reds_core_watch_add(reds, reds->secure_listen_socket,
@@ -2672,7 +2678,7 @@ static int reds_init_net(RedsState *reds)
         }
     }
 
-    if (reds->config->spice_listen_socket_fd != -1 ) {
+    if (socket_is_valid(reds->config->spice_listen_socket_fd)) {
         reds->listen_socket = reds->config->spice_listen_socket_fd;
         reds->listen_watch = reds_core_watch_add(reds, reds->listen_socket,
                                                  SPICE_WATCH_EVENT_READ,
@@ -3400,7 +3406,7 @@ static int do_spice_init(RedsState *reds, SpiceCoreInterface *core_interface)
         spice_warning("Failed to open SPICE sockets");
         goto err;
     }
-    if (reds->secure_listen_socket != -1) {
+    if (socket_is_valid(reds->secure_listen_socket)) {
         if (reds_init_ssl(reds) < 0) {
             goto err;
         }
@@ -3456,7 +3462,7 @@ SPICE_GNUC_VISIBLE SpiceServer *spice_server_new(void)
     reds->config->renderers = g_array_sized_new(FALSE, TRUE, sizeof(uint32_t), RED_RENDERER_LAST);
     reds->config->spice_port = -1;
     reds->config->spice_secure_port = -1;
-    reds->config->spice_listen_socket_fd = -1;
+    reds->config->spice_listen_socket_fd = SOCKET_INVALID;
     reds->config->spice_family = PF_UNSPEC;
     reds->config->sasl_enabled = 0; // sasl disabled by default
 #if HAVE_SASL
@@ -3482,8 +3488,8 @@ SPICE_GNUC_VISIBLE SpiceServer *spice_server_new(void)
      */
     stat_file_add_node(reds->stat_file, INVALID_STAT_REF, "default_channel", TRUE);
 #endif
-    reds->listen_socket = -1;
-    reds->secure_listen_socket = -1;
+    reds->listen_socket = SOCKET_INVALID;
+    reds->secure_listen_socket = SOCKET_INVALID;
 
     /* This environment was in red-worker so the "WORKER" in it.
      * For compatibility reason we maintain the old name */
@@ -3770,7 +3776,7 @@ SPICE_GNUC_VISIBLE void spice_server_set_addr(SpiceServer *reds, const char *add
 
 SPICE_GNUC_VISIBLE int spice_server_set_listen_socket_fd(SpiceServer *s, int listen_fd)
 {
-    s->config->spice_listen_socket_fd = listen_fd;
+    s->config->spice_listen_socket_fd = SOCKET_FROM_INT(listen_fd);
     return 0;
 }
 
@@ -4236,7 +4242,7 @@ SpiceCoreInterfaceInternal* reds_get_core_interface(RedsState *reds)
 }
 
 SpiceWatch *reds_core_watch_add(RedsState *reds,
-                                int fd, int event_mask,
+                                socket_t fd, int event_mask,
                                 SpiceWatchFunc func,
                                 void *opaque)
 {
diff --git a/server/reds.h b/server/reds.h
index 106310eb..8481d7c6 100644
--- a/server/reds.h
+++ b/server/reds.h
@@ -105,7 +105,7 @@ struct RedRecord *reds_get_record(RedsState *reds);
 
 /* fd watches/timers */
 SpiceWatch *reds_core_watch_add(RedsState *reds,
-                                int fd, int event_mask,
+                                socket_t fd, int event_mask,
                                 SpiceWatchFunc func,
                                 void *opaque);
 void reds_core_watch_update_mask(RedsState *reds,
-- 
2.20.1



More information about the Spice-devel mailing list