From 7dd83b8284b7594874af4370040b9db80b4c7915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jere=20Lepp=C3=A4nen?= Date: Thu, 31 Mar 2022 15:25:04 +0300 Subject: [PATCH] Call regular callback regardless of client input Regular callback is called only if there's no input from the client during the regular interval. Fix by calling the callback when the interval expires, regardless of input from the client. --- libcli.c | 17 +++++++++++------ libcli.h | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/libcli.c b/libcli.c index 67c779e..294ff28 100644 --- a/libcli.c +++ b/libcli.c @@ -1114,6 +1114,7 @@ int cli_loop(struct cli_def *cli, int sockfd) { // Set the last action now so we don't time immediately if (cli->idle_timeout) time(&cli->last_action); + if (cli->regular_callback) time(&cli->last_regular); // Start off in unprivileged mode cli_set_privilege(cli, PRIVILEGE_UNPRIVILEGED); @@ -1186,6 +1187,16 @@ int cli_loop(struct cli_def *cli, int sockfd) { cli->showprompt = 0; } + if (cli->regular_callback) { + if (time(NULL) - cli->last_regular >= cli->timeout_tm.tv_sec) { + if (cli->regular_callback(cli) != CLI_OK) { + l = -1; + break; + } + time(&cli->last_regular); + } + } + if ((sr = cli_socket_wait(sockfd, &tm)) < 0) { if (errno == EINTR) continue; perror(CLI_SOCKET_WAIT_PERROR); @@ -1194,12 +1205,6 @@ int cli_loop(struct cli_def *cli, int sockfd) { } if (sr == 0) { - // Timeout every second - if (cli->regular_callback && cli->regular_callback(cli) != CLI_OK) { - l = -1; - break; - } - if (cli->idle_timeout) { if (time(NULL) - cli->last_action >= cli->idle_timeout) { if (cli->idle_timeout_callback) { diff --git a/libcli.h b/libcli.h index c10c79e..bf0c3f2 100644 --- a/libcli.h +++ b/libcli.h @@ -78,6 +78,7 @@ struct cli_def { time_t idle_timeout; int (*idle_timeout_callback)(struct cli_def *); time_t last_action; + time_t last_regular; int telnet_protocol; void *user_context; struct cli_optarg_pair *found_optargs;