Skip to content

Commit 7728304

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 b3d6978 commit 7728304

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
@@ -1198,7 +1198,8 @@ static int pcap_startcapture_remote(pcap_t *fp)
11981198
goto error_nodiscard;
11991199

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

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

1324-
if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
1325+
if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, fp->errbuf, PCAP_ERRBUF_SIZE,
1326+
fp->sock_open_timeout)) == INVALID_SOCKET)
13251327
goto error;
13261328

13271329
/* addrinfo is no longer used */
@@ -2273,7 +2275,7 @@ rpcap_setup_session(const char *source, struct pcap_rmtauth *auth,
22732275
}
22742276

22752277
if ((*sockctrlp = sock_open(addrinfo, SOCKOPEN_CLIENT, 0,
2276-
errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2278+
errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
22772279
{
22782280
freeaddrinfo(addrinfo);
22792281
return -1;
@@ -2880,7 +2882,7 @@ SOCKET pcap_remoteact_accept_ex(const char *address, const char *port, const cha
28802882
}
28812883

28822884

2883-
if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
2885+
if ((sockmain = sock_open(addrinfo, SOCKOPEN_SERVER, 1, errbuf, PCAP_ERRBUF_SIZE, 0)) == INVALID_SOCKET)
28842886
{
28852887
freeaddrinfo(addrinfo);
28862888
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)