TCP keepalive setting support

This commit is contained in:
Andrey (Stanson) Kozin
2016-02-08 08:28:41 +03:00
committed by Dmitry Podgorny
parent cb79f5c10b
commit 6c1c2a86f8
4 changed files with 60 additions and 0 deletions

View File

@@ -175,6 +175,26 @@ xmpp_conn_t *xmpp_conn_clone(xmpp_conn_t * const conn)
return conn;
}
/** Set TCP keepalive parameters
* Turn on TCP keepalive and set timeout and interval. Zero timeout
* disables TCP keepalives.
*
* @param conn a Strophe connection object
* @param timeout TCP keepalive timeout in seconds
* @param interval TCP keepalive interval in seconds
*
* @ingroup Connections
*/
void xmpp_conn_set_keepalive(xmpp_conn_t * const conn, int timeout, int interval)
{
int ret;
ret = sock_set_keepalive(conn->sock, timeout, interval);
if (ret < 0) {
xmpp_error(conn->ctx, "xmpp", "Setting TCP keepalive (%d,%d) error: %d",
timeout, interval, sock_error());
}
}
/** Release a Strophe connection object.
* Decrement the reference count by one for a connection, freeing the
* connection object if the count reaches 0.

View File

@@ -22,11 +22,13 @@
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <Mstcpip.h> /* tcp_keepalive */
#else
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <fcntl.h>
#endif
@@ -108,6 +110,42 @@ sock_t sock_connect(const char * const host, const unsigned short port)
return sock;
}
int sock_set_keepalive(const sock_t sock, int timeout, int interval)
{
int ret;
int optval = (timeout && interval) ? 1 : 0;
#ifdef _WIN32
struct tcp_keepalive ka;
DWORD dw = 0;
ka.onoff = optval;
ka.keepalivetime = timeout * 1000;
ka.keepaliveinterval = interval * 1000;
ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &ka, sizeof(ka), NULL, 0, &dw, NULL, NULL);
#else
ret = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
if (ret < 0)
return ret;
if (optval) {
/* it's not possible to set keepalive count in Windows, so just use some
* acceptable value for UNIX to keep things work the same */
optval = 5;
ret = setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &optval, sizeof(optval));
if (ret < 0)
return ret;
ret = setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout));
if (ret < 0)
return ret;
ret = setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
if (ret < 0)
return ret;
}
#endif
return ret;
}
int sock_close(const sock_t sock)
{
#ifdef _WIN32

View File

@@ -40,5 +40,6 @@ int sock_write(const sock_t sock, const void * const buff, const size_t len);
int sock_is_recoverable(const int error);
/* checks for an error after connect, return 0 if connect successful */
int sock_connect_error(const sock_t sock);
int sock_set_keepalive(const sock_t sock, int timeout, int interval);
#endif /* __LIBSTROPHE_SOCK_H__ */

View File

@@ -222,6 +222,7 @@ void xmpp_conn_set_pass(xmpp_conn_t * const conn, const char * const pass);
xmpp_ctx_t* xmpp_conn_get_context(xmpp_conn_t * const conn);
void xmpp_conn_disable_tls(xmpp_conn_t * const conn);
int xmpp_conn_is_secured(xmpp_conn_t * const conn);
void xmpp_conn_set_keepalive(xmpp_conn_t * const conn, int timeout, int interval);
int xmpp_connect_client(xmpp_conn_t * const conn,
const char * const altdomain,