From 304e94bc6ff67b6e9a71263aebba00eb1be32858 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Sun, 19 Apr 2020 09:43:47 +0800 Subject: [PATCH] fast-ping: force send RST after ping finish. --- src/fast_ping.c | 5 +++++ src/util.c | 14 ++++++++++++++ src/util.h | 2 ++ 3 files changed, 21 insertions(+) diff --git a/src/fast_ping.c b/src/fast_ping.c index 1bdf67f..413c3c9 100644 --- a/src/fast_ping.c +++ b/src/fast_ping.c @@ -573,6 +573,11 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); + set_sock_keepalive(fd, 0, 0, 0); + /* Set the socket lingering so we will RST connections instead of wasting + * bandwidth with the four-step close + */ + set_sock_lingertime(fd, 0); ping_host->seq++; if (connect(fd, (struct sockaddr *)&ping_host->addr, ping_host->addr_len) != 0) { diff --git a/src/util.c b/src/util.c index a7f5983..8d0c39a 100644 --- a/src/util.c +++ b/src/util.c @@ -969,5 +969,19 @@ int set_sock_keepalive(int fd, int keepidle, int keepinterval, int keepcnt) setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepinterval, sizeof(keepinterval)); setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepcnt, sizeof(keepcnt)); + return 0; +} + +int set_sock_lingertime(int fd, int time) +{ + struct linger l; + + l.l_onoff = 1; + l.l_linger = 0; + + if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (const char *)&l, sizeof(l)) != 0) { + return -1; + } + return 0; } \ No newline at end of file diff --git a/src/util.h b/src/util.h index b5c4390..1c581ae 100644 --- a/src/util.h +++ b/src/util.h @@ -102,6 +102,8 @@ int has_network_raw_cap(void); int set_sock_keepalive(int fd, int keepidle, int keepinterval, int keepcnt); +int set_sock_lingertime(int fd, int time); + #ifdef __cplusplus } #endif /*__cplusplus */