From baec395c8ca3634bd502aa20b3610468aa9178b3 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sun, 28 Feb 2016 20:43:43 +0100 Subject: [PATCH] Added support for TCP Keepalive. --- CMakeLists.txt | 2 ++ cmake/config.hpp.cmake | 1 + src/vmime/platforms/posix/posixSocket.cpp | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 25ad376c..153e1c7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -975,6 +975,8 @@ CHECK_FUNCTION_EXISTS(gettid VMIME_HAVE_GETTID) CHECK_FUNCTION_EXISTS(syscall VMIME_HAVE_SYSCALL) CHECK_SYMBOL_EXISTS(SYS_gettid sys/syscall.h VMIME_HAVE_SYSCALL_GETTID) +CHECK_SYMBOL_EXISTS(SO_KEEPALIVE sys/socket.h VMIME_HAVE_SO_KEEPALIVE) + FIND_PACKAGE(Threads) IF(VMIME_BUILD_SHARED_LIBRARY) diff --git a/cmake/config.hpp.cmake b/cmake/config.hpp.cmake index cdd128c8..a79268cd 100644 --- a/cmake/config.hpp.cmake +++ b/cmake/config.hpp.cmake @@ -84,6 +84,7 @@ typedef unsigned @VMIME_64BIT_TYPE@ vmime_uint64; #cmakedefine01 VMIME_HAVE_LOCALTIME_S #cmakedefine01 VMIME_HAVE_LOCALTIME_R #cmakedefine01 VMIME_HAVE_MLANG +#cmakedefine01 VMIME_HAVE_SO_KEEPALIVE #cmakedefine01 VMIME_SHARED_PTR_USE_CXX #cmakedefine01 VMIME_SHARED_PTR_USE_BOOST diff --git a/src/vmime/platforms/posix/posixSocket.cpp b/src/vmime/platforms/posix/posixSocket.cpp index e13f6523..6ebd2766 100644 --- a/src/vmime/platforms/posix/posixSocket.cpp +++ b/src/vmime/platforms/posix/posixSocket.cpp @@ -138,6 +138,16 @@ void posixSocket::connect(const vmime::string& address, const vmime::port_t port continue; // try next } +#if VMIME_HAVE_SO_KEEPALIVE + + // Enable TCP Keepalive + int keepAlive_optval = 1; + socklen_t keepAlive_optlen = sizeof(keepAlive_optval); + + ::setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive_optval, keepAlive_optlen); + +#endif // VMIME_HAVE_SO_KEEPALIVE + if (m_timeoutHandler != NULL) { ::fcntl(sock, F_SETFL, ::fcntl(sock, F_GETFL) | O_NONBLOCK);