Skip to content

Commit

Permalink
rpcap: reduce timeout on socket connect
Browse files Browse the repository at this point in the history
Allow to set a custom timeout to the connect to avoid waiting
minutes for nothing should the connect() call fail.

Signed-off-by: Kevin Boulain <[email protected]>
Signed-off-by: Gabriel Ganne <[email protected]>
  • Loading branch information
GabrielGanne committed Feb 14, 2022
1 parent 5c3d71d commit 38f59f7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pcap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ struct pcap {
get_airpcap_handle_op_t get_airpcap_handle_op;
#endif
cleanup_op_t cleanup_op;

unsigned int sock_open_timeout;
};

/*
Expand Down
10 changes: 6 additions & 4 deletions pcap-rpcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,8 @@ static int pcap_startcapture_remote(pcap_t *fp)
goto error_nodiscard;

if ((sockdata = sock_open(addrinfo, SOCKOPEN_SERVER,
1 /* max 1 connection in queue */, fp->errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
1 /* max 1 connection in queue */, fp->errbuf, PCAP_ERRBUF_SIZE,
fp->sock_open_timeout)) == INVALID_SOCKET)
goto error_nodiscard;

/* addrinfo is no longer used */
Expand Down Expand Up @@ -1321,7 +1322,8 @@ static int pcap_startcapture_remote(pcap_t *fp)
if (sock_initaddress(host, portstring, &hints, &addrinfo, fp->errbuf, PCAP_ERRBUF_SIZE) == -1)
goto error;

if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE,
fp->sock_open_timeout)) == INVALID_SOCKET)
goto error;

/* addrinfo is no longer used */
Expand Down Expand Up @@ -2273,7 +2275,7 @@ rpcap_setup_session(const char *source, struct pcap_rmtauth *auth,
}

if ((*sockctrlp = sock_open(addrinfo, SOCKOPEN_CLIENT, 0,
errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
{
freeaddrinfo(addrinfo);
return -1;
Expand Down Expand Up @@ -2880,7 +2882,7 @@ SOCKET pcap_remoteact_accept_ex(const char *address, const char *port, const cha
}


if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
{
freeaddrinfo(addrinfo);
return (SOCKET)-2;
Expand Down
4 changes: 2 additions & 2 deletions rpcapd/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ daemon_msg_startcap_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
if (sock_initaddress(peerhost, portdata, &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
goto error;

if ((session->sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
if ((session->sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errmsgbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
goto error;
}
else // Data connection is opened by the client toward the server
Expand All @@ -2089,7 +2089,7 @@ daemon_msg_startcap_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
if (sock_initaddress(NULL, "0", &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
goto error;

if ((session->sockdata = sock_open(addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
if ((session->sockdata = sock_open(addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errmsgbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
goto error;

// get the complete sockaddr structure used in the data connection
Expand Down
4 changes: 2 additions & 2 deletions rpcapd/rpcapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void main_startup(void)
SOCKET sock;
struct listen_sock *sock_info;

if ((sock = sock_open(tempaddrinfo, SOCKOPEN_SERVER, SOCKET_MAXCONN, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
if ((sock = sock_open(tempaddrinfo, SOCKOPEN_SERVER, SOCKET_MAXCONN, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
{
switch (tempaddrinfo->ai_family)
{
Expand Down Expand Up @@ -1358,7 +1358,7 @@ main_active(void *ptr)
{
int activeclose;

if ((sockctrl = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
if ((sockctrl = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
{
rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);

Expand Down
21 changes: 20 additions & 1 deletion sockutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ static int sock_ismcastaddr(const struct sockaddr *saddr)
* if everything is fine, INVALID_SOCKET if some errors occurred. The error message is returned
* in the 'errbuf' variable.
*/
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen)
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen, unsigned int timeout_sec)
{
SOCKET sock;
#if defined(SO_NOSIGPIPE) || defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY)
Expand Down Expand Up @@ -422,6 +422,25 @@ SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf,
}
else /* we're the client */
{
/* Customize some timeouts to avoid minutes of waiting for nothing.
* Keep the defaults if unset. */
if (timeout_sec != 0) {
struct timeval timeout = { .tv_sec = timeout_sec };
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
sock_geterror("socket(): ", errbuf, errbuflen);
closesocket(sock);
return INVALID_SOCKET;
}
/* needed for the first syn */
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
sock_geterror("socket(): ", errbuf, errbuflen);
closesocket(sock);
return INVALID_SOCKET;
}
}

struct addrinfo *tempaddrinfo;
char *errbufptr;
size_t bufspaceleft;
Expand Down
2 changes: 1 addition & 1 deletion sockutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int sock_recv(SOCKET sock, SSL *, void *buffer, size_t size, int receiveall,
char *errbuf, int errbuflen);
int sock_recv_dgram(SOCKET sock, SSL *, void *buffer, size_t size,
char *errbuf, int errbuflen);
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen);
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen, unsigned int timeout_sec);
int sock_close(SOCKET sock, char *errbuf, int errbuflen);

int sock_send(SOCKET sock, SSL *, const char *buffer, size_t size,
Expand Down

0 comments on commit 38f59f7

Please sign in to comment.