From 71815ea13d6197556967377fba200d58fb62a3eb Mon Sep 17 00:00:00 2001 From: Mrx Date: Mon, 21 Sep 2026 02:39:59 +0530 Subject: [PATCH] Honor -i idle timeout in Ncat HTTP proxy mode ncat_http_server() passed a NULL timeout to fselect(), so -i/--idle-timeout was silently ignored: the accept loop blocked forever (leaving the 'Idle timeout expired' branch as dead code) and the post-CONNECT relay loop in handle_connect() never timed out idle connections either. Pass a timeval derived from o.idletimeout (milliseconds) to both selects, re-armed each iteration since select() may modify it. When -i is unset the timeout pointer stays NULL, preserving existing behavior. Relay timeout closes via the existing end label; error paths are unchanged. Fixes #824. --- ncat/ncat_proxy.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ncat/ncat_proxy.c b/ncat/ncat_proxy.c index 327dcf309..e1e8b4324 100644 --- a/ncat/ncat_proxy.c +++ b/ncat/ncat_proxy.c @@ -190,7 +190,16 @@ int ncat_http_server(void) logdebug("selecting, fdmax %d\n", listen_fdlist.fdmax); read_fds = listen_fds; - int fds_ready = fselect(listen_fdlist.fdmax + 1, &read_fds, NULL, NULL, NULL); + /* Arm the idle timer, if requested. select() may modify the + timeout, so it is re-initialized on every iteration. */ + struct timeval tv, *tvp = NULL; + if (o.idletimeout > 0) { + tvp = &tv; + tv.tv_sec = o.idletimeout / 1000; + tv.tv_usec = (o.idletimeout % 1000) * 1000; + } + + int fds_ready = fselect(listen_fdlist.fdmax + 1, &read_fds, NULL, NULL, tvp); if (o.debug > 1) logdebug("select returned %d fds ready\n", fds_ready); @@ -473,7 +482,19 @@ static int handle_connect(struct socket_buffer *client_sock, r = m; - fselect(maxfd + 1, &r, NULL, NULL, NULL); + /* Enforce the idle timeout on the relayed connection, if + requested. select() may modify the timeout, so it is + re-initialized on every iteration. */ + struct timeval tv, *tvp = NULL; + if (o.idletimeout > 0) { + tvp = &tv; + tv.tv_sec = o.idletimeout / 1000; + tv.tv_usec = (o.idletimeout % 1000) * 1000; + } + + rc = fselect(maxfd + 1, &r, NULL, NULL, tvp); + if (rc == 0) + goto end; zmem(buf, sizeof(buf));