[Spice-devel] [PATCH spice-server 09/33] red-stream: Use socket compatibility layer

Frediano Ziglio fziglio at redhat.com
Fri Dec 21 12:02:57 UTC 2018


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/red-stream.c | 32 +++++++++++++++++---------------
 server/red-stream.h |  4 ++--
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/server/red-stream.c b/server/red-stream.c
index 57034a64..33086d99 100644
--- a/server/red-stream.c
+++ b/server/red-stream.c
@@ -106,15 +106,15 @@ struct RedStreamPrivate {
  * Set TCP_CORK on socket
  */
 /* NOTE: enabled must be int */
-static int socket_set_cork(int socket, int enabled)
+static int socket_set_cork(socket_t socket, int enabled)
 {
     SPICE_VERIFY(sizeof(enabled) == sizeof(int));
-    return setsockopt(socket, IPPROTO_TCP, TCP_CORK, &enabled, sizeof(enabled));
+    return socket_setopt(socket, IPPROTO_TCP, TCP_CORK, &enabled, sizeof(enabled));
 }
 
 static ssize_t stream_write_cb(RedStream *s, const void *buf, size_t size)
 {
-    return write(s->socket, buf, size);
+    return socket_write(s->socket, buf, size);
 }
 
 static ssize_t stream_writev_cb(RedStream *s, const struct iovec *iov, int iovcnt)
@@ -132,7 +132,7 @@ static ssize_t stream_writev_cb(RedStream *s, const struct iovec *iov, int iovcn
         for (i = 0; i < tosend; i++) {
             expected += iov[i].iov_len;
         }
-        n = writev(s->socket, iov, tosend);
+        n = socket_writev(s->socket, iov, tosend);
         if (n <= expected) {
             if (n > 0)
                 ret += n;
@@ -148,7 +148,7 @@ static ssize_t stream_writev_cb(RedStream *s, const struct iovec *iov, int iovcn
 
 static ssize_t stream_read_cb(RedStream *s, void *buf, size_t size)
 {
-    return read(s->socket, buf, size);
+    return socket_read(s->socket, buf, size);
 }
 
 static ssize_t stream_ssl_write_cb(RedStream *s, const void *buf, size_t size)
@@ -274,7 +274,7 @@ int red_stream_get_family(const RedStream *s)
 {
     spice_return_val_if_fail(s != NULL, -1);
 
-    if (s->socket == -1)
+    if (!socket_is_valid(s->socket))
         return -1;
 
     return s->priv->info->laddr_ext.ss_family;
@@ -355,7 +355,7 @@ int red_stream_send_msgfd(RedStream *stream, int fd)
     }
 
     do {
-        r = sendmsg(stream->socket, &msgh, MSG_NOSIGNAL);
+        r = sendmsg(socket_get_raw(stream->socket), &msgh, MSG_NOSIGNAL);
     } while (r < 0 && (errno == EINTR || errno == EAGAIN));
 
     return r;
@@ -404,7 +404,7 @@ void red_stream_free(RedStream *s)
     }
 
     red_stream_remove_watch(s);
-    close(s->socket);
+    socket_close(s->socket);
 
     g_free(s);
 }
@@ -416,21 +416,23 @@ void red_stream_push_channel_event(RedStream *s, int event)
     main_dispatcher_channel_event(md, event, s->priv->info);
 }
 
-static void red_stream_set_socket(RedStream *stream, int socket)
+static void red_stream_set_socket(RedStream *stream, socket_t socket)
 {
     stream->socket = socket;
     /* deprecated fields. Filling them for backward compatibility */
     stream->priv->info->llen = sizeof(stream->priv->info->laddr);
     stream->priv->info->plen = sizeof(stream->priv->info->paddr);
-    getsockname(stream->socket, (struct sockaddr*)(&stream->priv->info->laddr), &stream->priv->info->llen);
-    getpeername(stream->socket, (struct sockaddr*)(&stream->priv->info->paddr), &stream->priv->info->plen);
+    socket_getsockname(stream->socket, (struct sockaddr*)(&stream->priv->info->laddr),
+                       &stream->priv->info->llen);
+    socket_getpeername(stream->socket, (struct sockaddr*)(&stream->priv->info->paddr),
+                       &stream->priv->info->plen);
 
     stream->priv->info->flags |= SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT;
     stream->priv->info->llen_ext = sizeof(stream->priv->info->laddr_ext);
     stream->priv->info->plen_ext = sizeof(stream->priv->info->paddr_ext);
-    getsockname(stream->socket, (struct sockaddr*)(&stream->priv->info->laddr_ext),
+    socket_getsockname(stream->socket, (struct sockaddr*)(&stream->priv->info->laddr_ext),
                 &stream->priv->info->llen_ext);
-    getpeername(stream->socket, (struct sockaddr*)(&stream->priv->info->paddr_ext),
+    socket_getpeername(stream->socket, (struct sockaddr*)(&stream->priv->info->paddr_ext),
                 &stream->priv->info->plen_ext);
 }
 
@@ -446,7 +448,7 @@ void red_stream_set_channel(RedStream *stream, int connection_id,
     }
 }
 
-RedStream *red_stream_new(RedsState *reds, int socket)
+RedStream *red_stream_new(RedsState *reds, socket_t socket)
 {
     RedStream *stream;
 
@@ -513,7 +515,7 @@ RedStreamSslStatus red_stream_enable_ssl(RedStream *stream, SSL_CTX *ctx)
     BIO *sbio;
 
     // Handle SSL handshaking
-    if (!(sbio = BIO_new_socket(stream->socket, BIO_NOCLOSE))) {
+    if (!(sbio = BIO_new_socket(socket_get_raw(stream->socket), BIO_NOCLOSE))) {
         spice_warning("could not allocate ssl bio socket");
         return RED_STREAM_SSL_STATUS_ERROR;
     }
diff --git a/server/red-stream.h b/server/red-stream.h
index 9a7cc617..52082f68 100644
--- a/server/red-stream.h
+++ b/server/red-stream.h
@@ -30,7 +30,7 @@ typedef struct RedStream RedStream;
 typedef struct RedStreamPrivate RedStreamPrivate;
 
 struct RedStream {
-    int socket;
+    socket_t socket;
     SpiceWatch *watch;
 
     RedStreamPrivate *priv;
@@ -58,7 +58,7 @@ void red_stream_push_channel_event(RedStream *s, int event);
 void red_stream_remove_watch(RedStream* s);
 void red_stream_set_channel(RedStream *stream, int connection_id,
                             int channel_type, int channel_id);
-RedStream *red_stream_new(RedsState *reds, int socket);
+RedStream *red_stream_new(RedsState *reds, socket_t socket);
 void red_stream_set_core_interface(RedStream *stream, SpiceCoreInterfaceInternal *core);
 bool red_stream_is_ssl(RedStream *stream);
 RedStreamSslStatus red_stream_ssl_accept(RedStream *stream);
-- 
2.17.2



More information about the Spice-devel mailing list