This allows to move some low-level code out of reds.c Signed-off-by: Christophe Fergeau <cfergeau@xxxxxxxxxx> Acked-by: Frediano Ziglio <fziglio@xxxxxxxxxx> --- server/net-utils.c | 32 ++++++++++++++++++++++++++++++++ server/net-utils.h | 1 + server/reds.c | 25 +------------------------ 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/server/net-utils.c b/server/net-utils.c index b017daa..ae7fa91 100644 --- a/server/net-utils.c +++ b/server/net-utils.c @@ -32,6 +32,38 @@ #include "net-utils.h" /** + * red_socket_set_keepalive: + * @fd: a socket file descriptor + * @keepalive: whether to enable keepalives on @fd + * + * Returns: #true if the operation succeeded, #false otherwise. + */ +bool red_socket_set_keepalive(int fd, bool enable, int timeout) +{ + int keepalive = !!enable; + + if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)) == -1) { + if (errno != ENOTSUP) { + spice_printerr("setsockopt for keepalive failed, %s", strerror(errno)); + return false; + } + } + + if (!enable) { + return true; + } + + if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout)) == -1) { + if (errno != ENOTSUP) { + spice_printerr("setsockopt for keepalive timeout failed, %s", strerror(errno)); + return false; + } + } + + return true; +} + +/** * red_socket_set_no_delay: * @fd: a socket file descriptor * @no_delay: whether to enable TCP_NODELAY on @fd diff --git a/server/net-utils.h b/server/net-utils.h index 023bc6b..d56b479 100644 --- a/server/net-utils.h +++ b/server/net-utils.h @@ -20,6 +20,7 @@ #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); bool red_socket_set_non_blocking(int fd, bool non_blocking); diff --git a/server/reds.c b/server/reds.c index 2f2dad8..d741a10 100644 --- a/server/reds.c +++ b/server/reds.c @@ -2378,29 +2378,6 @@ static void reds_handle_ssl_accept(int fd, int event, void *data) #define KEEPALIVE_TIMEOUT (10*60) -static bool reds_init_keepalive(int socket) -{ - int keepalive = 1; - int keepalive_timeout = KEEPALIVE_TIMEOUT; - - if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)) == -1) { - if (errno != ENOTSUP) { - spice_printerr("setsockopt for keepalive failed, %s", strerror(errno)); - return false; - } - } - - if (setsockopt(socket, SOL_TCP, TCP_KEEPIDLE, - &keepalive_timeout, sizeof(keepalive_timeout)) == -1) { - if (errno != ENOTSUP) { - spice_printerr("setsockopt for keepalive timeout failed, %s", strerror(errno)); - return false; - } - } - - return true; -} - static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket) { RedLinkInfo *link; @@ -2413,7 +2390,7 @@ static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket) goto error; } - reds_init_keepalive(socket); + red_socket_set_keepalive(socket, TRUE, KEEPALIVE_TIMEOUT); link = spice_new0(RedLinkInfo, 1); link->reds = reds; -- 2.9.3 _______________________________________________ Spice-devel mailing list Spice-devel@xxxxxxxxxxxxxxxxxxxxx https://lists.freedesktop.org/mailman/listinfo/spice-devel