Question

I'm trying to set tcp keepalive but in doing so I'm seeing the error

"Protocol not available"

int rc = setsockopt(s, SOL_SOCKET, TCP_KEEPIDLE, &keepalive_idle, sizeof(keepalive_idle));
if (rc < 0)
    printf("error setting keepalive_idle: %s\n", strerror(errno));

I'm able to turn on keepalive, set keepalive interval and count but keepalive idle which is keepalive time is throwing that error and I never see any keepalive packets being transmitted/received either with wireshark and the filter tcp.analysis.keep_alive or with tcpdump

sudo tcpdump -vv "tcp[tcpflags] == tcp-ack and less 1"

Is there a kernel module that needs to be loaded or something? Or are you no longer able to override the global KEEPIDLE time.

By the way the output of

matt@devpc:~/ sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_probes net.ipv4.tcp_keepalive_intvl
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75
Était-ce utile?

La solution

In an application that I coded, the following works:

setsockopt(*sfd, SOL_SOCKET, SO_KEEPALIVE,(char *)&enable_keepalive, sizeof(enable_keepalive));
setsockopt(*sfd, IPPROTO_TCP, TCP_KEEPCNT, (char *)&num_keepalive_strobes, sizeof(num_keepalive_strobes));
setsockopt(*sfd, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepalive_idle_time_secs, sizeof(keepalive_idle_time_secs));
setsockopt(*sfd, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepalive_strobe_interval_secs, sizeof(keepalive_strobe_interval_secs));

Try changing SOL_SOCKET to IPPROTO_TCP for TCPKEEPIDLE.

Autres conseils

There a very handy lib that can help you, it's called libkeepalive : http://libkeepalive.sourceforge.net/

It can be used with LD_PRELOAD in order to enable and control keep-alive on all TCP sockets. You can also override keep-alive settings with environment variables.

I tried to run a tcp server with it:

KEEPIDLE=5 KEEPINTVL=5 KEEPCNT=100 LD_PRELOAD=/usr/lib/libkeepalive.so nc -l -p 4242

Then I connected a client:

nc 127.0.0.1 4242

And I visualized the traffic with Wireshark: the keep-alive packets began exactly after 5 seconds of inactivity (my system wide setting is 75). Therefore it means that it's possible to override the system settings.

Here is how libkeepalive sets TCP_KEEPIDLE:

  if((env = getenv("KEEPIDLE")) && ((optval = atoi(env)) >= 0)) {
    setsockopt(s, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
  }

Looks like they use SOL_TCP instead of SOL_SOCKET.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top