Skip to content

Commit da802f0

Browse files
committed
rpcap: reduce timeout on socket connect
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]>
1 parent 5b963e9 commit da802f0

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

pcap-int.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ struct pcap {
362362
get_airpcap_handle_op_t get_airpcap_handle_op;
363363
#endif
364364
cleanup_op_t cleanup_op;
365+
366+
unsigned int sock_open_timeout;
365367
};
366368

367369
/*

pcap-rpcap.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,8 @@ static int pcap_startcapture_remote(pcap_t *fp)
11931193
goto error_nodiscard;
11941194

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

11991200
/* addrinfo is no longer used */
@@ -1316,7 +1317,8 @@ static int pcap_startcapture_remote(pcap_t *fp)
13161317
if (sock_initaddress(host, portstring, &hints, &addrinfo, fp->errbuf, PCAP_ERRBUF_SIZE) == -1)
13171318
goto error;
13181319

1319-
if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
1320+
if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE,
1321+
fp->sock_open_timeout)) == INVALID_SOCKET)
13201322
goto error;
13211323

13221324
/* addrinfo is no longer used */
@@ -2268,7 +2270,7 @@ rpcap_setup_session(const char *source, struct pcap_rmtauth *auth,
22682270
}
22692271

22702272
if ((*sockctrlp = sock_open(addrinfo, SOCKOPEN_CLIENT, 0,
2271-
errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2273+
errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
22722274
{
22732275
freeaddrinfo(addrinfo);
22742276
return -1;
@@ -2875,7 +2877,7 @@ SOCKET pcap_remoteact_accept_ex(const char *address, const char *port, const cha
28752877
}
28762878

28772879

2878-
if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2880+
if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
28792881
{
28802882
freeaddrinfo(addrinfo);
28812883
return (SOCKET)-2;

rpcapd/daemon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,7 @@ daemon_msg_startcap_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
20782078
if (sock_initaddress(peerhost, portdata, &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
20792079
goto error;
20802080

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

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

20952095
// get the complete sockaddr structure used in the data connection

rpcapd/rpcapd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ void main_startup(void)
623623
SOCKET sock;
624624
struct listen_sock *sock_info;
625625

626-
if ((sock = sock_open(tempaddrinfo, SOCKOPEN_SERVER, SOCKET_MAXCONN, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
626+
if ((sock = sock_open(tempaddrinfo, SOCKOPEN_SERVER, SOCKET_MAXCONN, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
627627
{
628628
switch (tempaddrinfo->ai_family)
629629
{
@@ -1358,7 +1358,7 @@ main_active(void *ptr)
13581358
{
13591359
int activeclose;
13601360

1361-
if ((sockctrl = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
1361+
if ((sockctrl = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
13621362
{
13631363
rpcapd_log(LOGPRIO_DEBUG, "%s", errbuf);
13641364

sockutils.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static int sock_ismcastaddr(const struct sockaddr *saddr)
312312
* if everything is fine, INVALID_SOCKET if some errors occurred. The error message is returned
313313
* in the 'errbuf' variable.
314314
*/
315-
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen)
315+
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen, unsigned int timeout_sec)
316316
{
317317
SOCKET sock;
318318
#if defined(SO_NOSIGPIPE) || defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY)
@@ -422,6 +422,26 @@ SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf,
422422
}
423423
else /* we're the client */
424424
{
425+
/* Customize some timeouts to avoid minutes of waiting for nothing.
426+
* Keep the defaults if unset. */
427+
if (timeout_sec != 0) {
428+
struct timeval timeout = { 0 };
429+
timeout.tv_sec = timeout_sec;
430+
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
431+
{
432+
sock_geterror("socket(): ", errbuf, errbuflen);
433+
closesocket(sock);
434+
return INVALID_SOCKET;
435+
}
436+
/* needed for the first syn */
437+
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
438+
{
439+
sock_geterror("socket(): ", errbuf, errbuflen);
440+
closesocket(sock);
441+
return INVALID_SOCKET;
442+
}
443+
}
444+
425445
struct addrinfo *tempaddrinfo;
426446
char *errbufptr;
427447
size_t bufspaceleft;

sockutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int sock_recv(SOCKET sock, SSL *, void *buffer, size_t size, int receiveall,
136136
char *errbuf, int errbuflen);
137137
int sock_recv_dgram(SOCKET sock, SSL *, void *buffer, size_t size,
138138
char *errbuf, int errbuflen);
139-
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen);
139+
SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen, unsigned int timeout_sec);
140140
int sock_close(SOCKET sock, char *errbuf, int errbuflen);
141141

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

0 commit comments

Comments
 (0)