Skip to content

Commit

Permalink
darwin: make TCP_KEEPINTVL and TCP_KEEPCNT available (libuv#3908)
Browse files Browse the repository at this point in the history
In earlier versions, macOS only defined TCP_KEEPALIVE, but since macOS
10.8 (Mountain Lion), it has supported TCP_KEEPINTVL and TCP_KEEPCNT.

https://lists.apple.com/archives/macnetworkprog/2012/Jul/msg00005.html
  • Loading branch information
caobug authored Aug 14, 2023
1 parent 1b4bd92 commit 65541f7
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/unix/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,28 +424,39 @@ int uv__tcp_nodelay(int fd, int on) {


int uv__tcp_keepalive(int fd, int on, unsigned int delay) {
int intvl;
int cnt;

(void) &intvl;
(void) &cnt;

if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)))
return UV__ERR(errno);

if (!on)
return 0;

#ifdef TCP_KEEPIDLE
if (on) {
int intvl = 1; /* 1 second; same as default on Win32 */
int cnt = 10; /* 10 retries; same as hardcoded on Win32 */
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)))
return UV__ERR(errno);
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)))
return UV__ERR(errno);
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)))
return UV__ERR(errno);
}
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)))
return UV__ERR(errno);
/* Solaris/SmartOS, if you don't support keep-alive,
* then don't advertise it in your system headers...
*/
/* FIXME(bnoordhuis) That's possibly because sizeof(delay) should be 1. */
#elif defined(TCP_KEEPALIVE) && !defined(__sun)
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)))
return UV__ERR(errno);
#endif

/* Solaris/SmartOS, if you don't support keep-alive,
* then don't advertise it in your system headers...
*/
/* FIXME(bnoordhuis) That's possibly because sizeof(delay) should be 1. */
#if defined(TCP_KEEPALIVE) && !defined(__sun)
if (on && setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)))
#ifdef TCP_KEEPINTVL
intvl = 1; /* 1 second; same as default on Win32 */
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)))
return UV__ERR(errno);
#endif

#ifdef TCP_KEEPCNT
cnt = 10; /* 10 retries; same as hardcoded on Win32 */
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)))
return UV__ERR(errno);
#endif

Expand Down

0 comments on commit 65541f7

Please sign in to comment.