Skip to content

Commit

Permalink
rpcap: set keepalives on rpcap's control socket
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Boulain <[email protected]>
Signed-off-by: Gabriel Ganne <[email protected]>
  • Loading branch information
GabrielGanne committed Feb 14, 2022
1 parent 5b963e9 commit b3d6978
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pcap-rpcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
#include <stdarg.h> /* for functions with variable number of arguments */
#include <errno.h> /* for the errno variable */
#include <limits.h> /* for INT_MAX */

#ifndef _WIN32
#include <netinet/tcp.h> /* for TCP_KEEP* */
#endif

#include "sockutils.h"
#include "pcap-int.h"
#include "rpcap-protocol.h"
Expand Down Expand Up @@ -3536,3 +3541,46 @@ static int rpcap_read_packet_msg(struct pcap_rpcap const *rp, pcap_t *p, size_t
p->cc = cc;
return 0;
}

/*
* Set the keepalives parameters on the control socket.
* An rpcap-based application may detect more rapidly a network error.
*
* It may not be necessary to set them on the data socket as it may use UDP.
* See pcap_read_nocb_remote for the select logic that will take into
* account the error on the control socket.
*/
int
pcap_set_control_keepalive(pcap_t *p, int enable, int keepcnt, int keepidle, int keepintvl)
{
struct pcap_rpcap *pr = p->priv; /* structure used when doing a remote live capture */

if (setsockopt(pr->rmt_sockctrl, SOL_SOCKET, SO_KEEPALIVE, (char *)&enable, sizeof(enable)) < 0)
{
sock_geterror("setsockopt(): ", p->errbuf, PCAP_ERRBUF_SIZE);
return -1;
}

/* when SO_KEEPALIVE isn't active, the following options aren't used */
if (!enable)
return 0;

#if defined(TCP_KEEPCNT) && defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
if (setsockopt(pr->rmt_sockctrl, IPPROTO_TCP, TCP_KEEPCNT, (char *)&keepcnt, sizeof(keepcnt)) < 0 ||
setsockopt(pr->rmt_sockctrl, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepidle, sizeof(keepidle)) < 0 ||
setsockopt(pr->rmt_sockctrl, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepintvl, sizeof(keepintvl)) < 0)
{
sock_geterror("setsockopt(): ", p->errbuf, PCAP_ERRBUF_SIZE);
return -1;
}
#else
if (keepcnt || keepidle || keepintvl)
{
snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"TCP_KEEPCNT, TCP_KEEPIDLE or TCP_KEEPINTVL not supported on this platform");
return -1;
}
#endif

return 0;
}
1 change: 1 addition & 0 deletions pcap/pcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ PCAP_API int pcap_get_tstamp_precision(pcap_t *);

PCAP_AVAILABLE_1_0
PCAP_API int pcap_activate(pcap_t *);
PCAP_API int pcap_set_control_keepalive(pcap_t *, int, int, int, int);

PCAP_AVAILABLE_1_2
PCAP_API int pcap_list_tstamp_types(pcap_t *, int **);
Expand Down

0 comments on commit b3d6978

Please sign in to comment.