From cb081e2ee06665d24b789505bbc9532af07b184d Mon Sep 17 00:00:00 2001 From: zhangshuai39 Date: Mon, 23 Sep 2024 14:20:50 +0800 Subject: [PATCH] net/tcp: Fix TCP keepalive time unit misuse problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: The conn->keeptimer units is decisecond,but its unit is treated as half-second in the tcp_timer & tcp_get_timeout function. Therefore conn>keeptimer needs to be divided by 5(DSEC_PER_HSEC) to match half-second units. Signed-off-by: zhangshuai39 --- net/tcp/tcp_timer.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/net/tcp/tcp_timer.c b/net/tcp/tcp_timer.c index 1bfacfbb02428..568743e251308 100644 --- a/net/tcp/tcp_timer.c +++ b/net/tcp/tcp_timer.c @@ -107,11 +107,15 @@ static int tcp_get_timeout(FAR struct tcp_conn_s *conn) #ifdef CONFIG_NET_TCP_KEEPALIVE if (timeout == 0) { - timeout = conn->keeptimer; + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; } - else if (conn->keeptimer > 0 && timeout > conn->keeptimer) + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) { - timeout = conn->keeptimer; + timeout = conn->keeptimer / DSEC_PER_HSEC; } #endif @@ -699,11 +703,11 @@ void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) * received from the remote peer? */ - if (conn->keeptimer > hsec) + if (conn->keeptimer > hsec * DSEC_PER_HSEC) { /* Will not yet decrement to zero */ - conn->keeptimer -= hsec; + conn->keeptimer -= hsec * DSEC_PER_HSEC; } else {