[Spice-devel] [PATCH spice-server v3 08/32] net-utils: Use socket compatibility layer

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


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/net-utils.c | 33 +++++++++++++++++++++------------
 server/net-utils.h | 10 ++++++----
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/server/net-utils.c b/server/net-utils.c
index 802509a4..8a5b62cb 100644
--- a/server/net-utils.c
+++ b/server/net-utils.c
@@ -43,11 +43,11 @@
  *
  * Returns: #true if the operation succeeded, #false otherwise.
  */
-bool red_socket_set_keepalive(int fd, bool enable, int timeout)
+bool red_socket_set_keepalive(socket_t sock, bool enable, int timeout)
 {
     int keepalive = !!enable;
 
-    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)) == -1) {
+    if (socket_setopt(sock, SOL_SOCKET, SO_KEEPALIVE,&keepalive, sizeof(keepalive)) == -1) {
         if (errno != ENOTSUP) {
             g_warning("setsockopt for keepalive failed, %s", strerror(errno));
             return false;
@@ -59,7 +59,7 @@ bool red_socket_set_keepalive(int fd, bool enable, int timeout)
     }
 
 #ifdef HAVE_TCP_KEEPIDLE
-    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout)) == -1) {
+    if (socket_setopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout)) == -1) {
         if (errno != ENOTSUP) {
             g_warning("setsockopt for keepalive timeout failed, %s", strerror(errno));
             return false;
@@ -77,12 +77,11 @@ bool red_socket_set_keepalive(int fd, bool enable, int timeout)
  *
  * Returns: #true if the operation succeeded, #false otherwise.
  */
-bool red_socket_set_no_delay(int fd, bool no_delay)
+bool red_socket_set_no_delay(socket_t sock, bool no_delay)
 {
     int optval = no_delay;
 
-    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
-                   &optval, sizeof(optval)) != 0) {
+    if (socket_setopt(sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval)) != 0) {
         if (errno != ENOTSUP && errno != ENOPROTOOPT) {
             spice_warning("setsockopt failed, %s", strerror(errno));
             return false;
@@ -99,9 +98,11 @@ bool red_socket_set_no_delay(int fd, bool no_delay)
  *
  * Returns: #true if the operation succeeded, #false otherwise.
  */
-bool red_socket_set_non_blocking(int fd, bool non_blocking)
+bool red_socket_set_non_blocking(socket_t sock, bool non_blocking)
 {
+#ifndef _WIN32
     int flags;
+    int fd = socket_get_raw(sock);
 
     if ((flags = fcntl(fd, F_GETFL)) == -1) {
         spice_warning("fnctl(F_GETFL) failed, %s", strerror(errno));
@@ -120,6 +121,15 @@ bool red_socket_set_non_blocking(int fd, bool non_blocking)
     }
 
     return true;
+#else
+    u_long ioctl_nonblocking = 1;
+
+    if (ioctlsocket(socket_get_raw(sock), FIONBIO, &ioctl_nonblocking) != 0) {
+        spice_warning("ioctlsocket(FIONBIO) failed, %d", WSAGetLastError());
+        return false;
+    }
+    return true;
+#endif
 }
 
 /**
@@ -128,15 +138,14 @@ bool red_socket_set_non_blocking(int fd, bool non_blocking)
  *
  * Returns: The current value of TCP_NODELAY for @fd, -1 if an error occurred
  */
-int red_socket_get_no_delay(int fd)
+int red_socket_get_no_delay(socket_t sock)
 {
     int delay_val;
     socklen_t opt_size = sizeof(delay_val);
 
-    if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &delay_val,
-                   &opt_size) == -1) {
-            spice_warning("getsockopt failed, %s", strerror(errno));
-            return -1;
+    if (socket_getopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*) &delay_val, &opt_size) == -1) {
+        spice_warning("getsockopt failed, %s", strerror(errno));
+        return -1;
     }
 
     return delay_val;
diff --git a/server/net-utils.h b/server/net-utils.h
index f95d689a..89e48ea8 100644
--- a/server/net-utils.h
+++ b/server/net-utils.h
@@ -20,9 +20,11 @@
 
 #include <stdbool.h>
 
-bool red_socket_set_keepalive(int fd, bool enable, int timeout);
-bool red_socket_set_no_delay(int fd, bool no_delay);
-int red_socket_get_no_delay(int fd);
-bool red_socket_set_non_blocking(int fd, bool non_blocking);
+#include "sys-socket.h"
+
+bool red_socket_set_keepalive(socket_t fd, bool enable, int timeout);
+bool red_socket_set_no_delay(socket_t fd, bool no_delay);
+int red_socket_get_no_delay(socket_t fd);
+bool red_socket_set_non_blocking(socket_t fd, bool non_blocking);
 
 #endif /* RED_NET_UTILS_H_ */
-- 
2.20.1



More information about the Spice-devel mailing list