sock: refactore tcp keepalive setting

- Leave default number of keepalive probes rather than hardcoded value
- QNX and OSX use TCP_KEEPALIVE instead of TCP_KEEPIDLE
- QNX doesn't have TCP_KEEPINTVL
This commit is contained in:
Dmitry Podgorny
2016-04-08 17:59:08 +00:00
parent 6c1c2a86f8
commit 5518a9e10a
2 changed files with 14 additions and 9 deletions

View File

@@ -115,6 +115,8 @@ int sock_set_keepalive(const sock_t sock, int timeout, int interval)
int ret;
int optval = (timeout && interval) ? 1 : 0;
/* This function doesn't change maximum number of keepalive probes */
#ifdef _WIN32
struct tcp_keepalive ka;
DWORD dw = 0;
@@ -129,20 +131,22 @@ int sock_set_keepalive(const sock_t sock, int timeout, int interval)
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));
#ifdef TCP_KEEPIDLE
ret = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout));
#else
/* QNX receives `struct timeval' as argument, but it seems OSX does int */
ret = setsockopt(sock, IPPROTO_TCP, TCP_KEEPALIVE, &timeout, sizeof(timeout));
#endif /* TCP_KEEPIDLE */
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));
#ifdef TCP_KEEPINTVL
ret = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
if (ret < 0)
return ret;
#endif /* TCP_KEEPINTVL */
}
#endif
#endif /* _WIN32 */
return ret;
}