From a9390874c76d30f655c31be9a4a4ac7c646f1d7e Mon Sep 17 00:00:00 2001 From: Andy Green Date: Wed, 18 Apr 2018 18:45:27 +0800 Subject: [PATCH] tcp keepalive: user TCP_USER_TIMEOUT on linux if extant https://github.com/warmcat/libwebsockets/issues/1223 --- CMakeLists.txt | 5 +++++ cmake/lws_config_private.h.in | 2 ++ lib/plat/lws-plat-unix.c | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdc1a09a..e12e1889 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1365,6 +1365,11 @@ endif() CHECK_C_SOURCE_COMPILES("#define _GNU_SOURCE\n#include \nint main(void) {int fd[2];\n return pipe2(fd, 0);\n}\n" LWS_HAVE_PIPE2) +# tcp keepalive needs this on linux to work practically... but it only exists +# after kernel 2.6.37 + +CHECK_C_SOURCE_COMPILES("#include \nint main(void) { return TCP_USER_TIMEOUT; }\n" LWS_HAVE_TCP_USER_TIMEOUT) + set(CMAKE_REQUIRED_LIBRARIES ${temp}) # Generate the lws_config.h that includes all the public compilation settings. configure_file( diff --git a/cmake/lws_config_private.h.in b/cmake/lws_config_private.h.in index 267a3484..1578aea3 100644 --- a/cmake/lws_config_private.h.in +++ b/cmake/lws_config_private.h.in @@ -81,6 +81,8 @@ /* Define to 1 if you have the header file. */ #cmakedefine LWS_HAVE_UNISTD_H +#cmakedefine LWS_HAVE_TCP_USER_TIMEOUT + /* Define to 1 if you have the `vfork' function. */ #cmakedefine LWS_HAVE_VFORK diff --git a/lib/plat/lws-plat-unix.c b/lib/plat/lws-plat-unix.c index 55d26d41..c5c0ce61 100644 --- a/lib/plat/lws-plat-unix.c +++ b/lib/plat/lws-plat-unix.c @@ -348,6 +348,14 @@ lws_plat_set_socket_options(struct lws_vhost *vhost, int fd) */ #else /* set the keepalive conditions we want on it too */ + +#if defined(LWS_HAVE_TCP_USER_TIMEOUT) + optval = 1000 * (vhost->ka_time + + (vhost->ka_interval * vhost->ka_probes)); + if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, + (const void *)&optval, optlen) < 0) + return 1; +#endif optval = vhost->ka_time; if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, (const void *)&optval, optlen) < 0)