From 1e52d207aca21c8fa39f99fdf3ce559af6370ae2 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Fri, 15 May 2020 10:07:14 -0400 Subject: Enable TCP keepalive with half-hour idle Since we swallow IRC PINGs, a client connection can go hours idle on a quiet network. On my home internet, at least, these connections seem to get silently dropped. --- compat.h | 4 ++++ local.c | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/compat.h b/compat.h index d471a8f..01ef10c 100644 --- a/compat.h +++ b/compat.h @@ -35,3 +35,7 @@ uint32_t arc4random_uniform(uint32_t upper_bound); #ifndef SIGINFO #define SIGINFO SIGUSR2 #endif + +#ifdef __APPLE__ +#define TCP_KEEPIDLE TCP_KEEPALIVE +#endif diff --git a/local.c b/local.c index 0a140e6..50e49d5 100644 --- a/local.c +++ b/local.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -218,6 +219,13 @@ struct tls *localAccept(int *fd, int bind) { int error = setsockopt(*fd, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes)); if (error) err(EX_OSERR, "setsockopt"); + error = setsockopt(*fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)); + if (error) err(EX_OSERR, "setsockopt"); + + int idle = 15 * 60; + error = setsockopt(*fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)); + if (error) err(EX_OSERR, "setsockopt"); + struct tls *client; error = tls_accept_socket(server, &client, *fd); if (error) errx(EX_SOFTWARE, "tls_accept_socket: %s", tls_error(server)); -- cgit 1.4.1