[Spice-devel] [PATCHv2 1/3] reds: add Unix socket support

Marc-André Lureau marcandre.lureau at gmail.com
Mon Jan 12 08:35:28 PST 2015


Learn to listen on a Unix address. In this case, the connection is plain
only (non-tls).
---
 server/reds.c         | 36 ++++++++++++++++++++++++++++++------
 server/spice-server.h |  1 +
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/server/reds.c b/server/reds.c
index e34433b..91eb208 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -44,6 +44,7 @@
 #endif
 
 #include <glib.h>
+#include <sys/un.h>
 
 #include <spice/protocol.h>
 #include <spice/vd_agent.h>
@@ -2351,7 +2352,27 @@ static int 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;
+    int slisten, rc, len;
+
+    if (family == AF_UNIX) {
+        struct sockaddr_un local = { 0, };
+
+        if ((slisten = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+            perror("socket");
+            return -1;
+        }
+
+        local.sun_family = AF_UNIX;
+        strncpy(local.sun_path, addr, sizeof(local.sun_path) -1);
+        unlink(local.sun_path);
+        len = SUN_LEN(&local);
+        if (bind(slisten, (struct sockaddr *)&local, len) == -1) {
+            perror("bind");
+            return -1;
+        }
+
+        goto listen;
+    }
 
     memset(&ai,0, sizeof(ai));
     ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
@@ -2391,6 +2412,7 @@ static int reds_init_socket(const char *addr, int portnr, int family)
             } else {
                 spice_info("cannot resolve address spice-server is bound to");
             }
+            freeaddrinfo(res);
             goto listen;
         }
         close(slisten);
@@ -2401,7 +2423,6 @@ static int reds_init_socket(const char *addr, int portnr, int family)
     return -1;
 
 listen:
-    freeaddrinfo(res);
     if (listen(slisten,1) != 0) {
         spice_warning("listen: %s", strerror(errno));
         close(slisten);
@@ -2439,7 +2460,7 @@ void reds_set_client_mm_time_latency(RedClient *client, uint32_t latency)
 
 static int reds_init_net(void)
 {
-    if (spice_port != -1) {
+    if (spice_port != -1 || spice_family == AF_UNIX) {
         reds->listen_socket = reds_init_socket(spice_addr, spice_port, spice_family);
         if (-1 == reds->listen_socket) {
             return -1;
@@ -3361,11 +3382,14 @@ SPICE_GNUC_VISIBLE void spice_server_set_addr(SpiceServer *s, const char *addr,
 {
     spice_assert(reds == s);
     g_strlcpy(spice_addr, addr, sizeof(spice_addr));
-    if (flags & SPICE_ADDR_FLAG_IPV4_ONLY) {
+    if (flags == SPICE_ADDR_FLAG_IPV4_ONLY) {
         spice_family = PF_INET;
-    }
-    if (flags & SPICE_ADDR_FLAG_IPV6_ONLY) {
+    } else if (flags == SPICE_ADDR_FLAG_IPV6_ONLY) {
         spice_family = PF_INET6;
+    } else if (flags == SPICE_ADDR_FLAG_UNIX_ONLY) {
+        spice_family = AF_UNIX;
+    } else {
+        spice_warning("unknown address flag: 0x%X", flags);
     }
 }
 
diff --git a/server/spice-server.h b/server/spice-server.h
index c97b221..bca0da6 100644
--- a/server/spice-server.h
+++ b/server/spice-server.h
@@ -42,6 +42,7 @@ void spice_server_destroy(SpiceServer *s);
 
 #define SPICE_ADDR_FLAG_IPV4_ONLY (1 << 0)
 #define SPICE_ADDR_FLAG_IPV6_ONLY (1 << 1)
+#define SPICE_ADDR_FLAG_UNIX_ONLY (1 << 2)
 
 int spice_server_set_compat_version(SpiceServer *s,
                                     spice_compat_version_t version);
-- 
2.1.0



More information about the Spice-devel mailing list