diff --git a/redir.c b/redir.c index fd18956..cf7924c 100644 --- a/redir.c +++ b/redir.c @@ -38,6 +38,7 @@ #include #include #include +#include #ifdef USE_TCP_WRAPPERS #include @@ -977,6 +978,19 @@ static int server_socket(char *addr, int port, int fail) exit(1); } + /* Enable TCP keepalive to detect broken connections */ + int keepalive = 1; + rc = setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)); + if (rc != 0) { + if (fail) { + close(sd); + return -1; + } + + ERR("Failed setting socket option SO_KEEPALIVE: %s", strerror(errno)); + exit(1); + } + DBG("TCP keepalive enabled using system defaults"); /* * Try to bind the address to the socket. */