From 3b1f2c293f5f71468dc7c5cfd876e2a53f869d46 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 22 Apr 2021 12:20:48 +0200 Subject: #980 Broken TCP connection times out after 120 seconds through keepalive Enable TCP keepalive to detect if the ogClient is gone (hard reset). If no reply after 120 seconds, then release the connection to the client. --- src/core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/core.c') diff --git a/src/core.c b/src/core.c index f61f089..fef00ea 100644 --- a/src/core.c +++ b/src/core.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -313,10 +314,16 @@ static void og_agent_send_refresh(struct og_client *cli) /* Agent client operation might take longer, shut down after 30 seconds. */ #define OG_AGENT_CLIENT_TIMEOUT 30 +#define OG_TCP_KEEPALIVE_IDLE 60 +#define OG_TCP_KEEPALIVE_INTL 30 +#define OG_TCP_KEEPALIVE_CNT 4 + int socket_rest, socket_agent_rest; void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) { + int intl = OG_TCP_KEEPALIVE_INTL, cnt = OG_TCP_KEEPALIVE_CNT; + int on = 1, idle = OG_TCP_KEEPALIVE_IDLE; struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); struct og_client *cli; @@ -331,6 +338,11 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) return; } + setsockopt(client_sd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(int)); + setsockopt(client_sd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(int)); + setsockopt(client_sd, IPPROTO_TCP, TCP_KEEPINTVL, &intl, sizeof(int)); + setsockopt(client_sd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(int)); + cli = (struct og_client *)calloc(1, sizeof(struct og_client)); if (!cli) { close(client_sd); -- cgit v1.2.3-18-g5258