Skip to content

Commit

Permalink
Merge pull request #80 from nokia/dev/regular-cb-improve
Browse files Browse the repository at this point in the history
Regular callback improvements
  • Loading branch information
RobSanders authored Dec 2, 2022
2 parents 0acad90 + 7dd83b8 commit 589c718
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
17 changes: 11 additions & 6 deletions libcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions libcli.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 589c718

Please sign in to comment.