From f92ec18438489d490da3f6c6e283fa61e0584b0f Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 4 Oct 2024 15:03:21 +0200 Subject: [PATCH 1/2] cli UPDATE add client monitoring support --- CMakeLists.txt | 4 +-- cli/commands.c | 64 ++++++++++++++++++++++++++++++++++++++++- cli/doc/netopeer2-cli.1 | 10 +++++++ cli/main.c | 4 +++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c727f2c..6de32ccf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,8 +49,8 @@ set(LIBYANG_DEP_SOVERSION 3.0.0) set(LIBYANG_DEP_SOVERSION_MAJOR 3) # libnetconf2 required version -set(LIBNETCONF2_DEP_VERSION 3.5.0) -set(LIBNETCONF2_DEP_SOVERSION 4.4.0) +set(LIBNETCONF2_DEP_VERSION 3.5.2) +set(LIBNETCONF2_DEP_SOVERSION 4.4.2) set(LIBNETCONF2_DEP_SOVERSION_MAJOR 4) # sysrepo required version diff --git a/cli/commands.c b/cli/commands.c index 99f30d8a..f26fc242 100644 --- a/cli/commands.c +++ b/cli/commands.c @@ -70,7 +70,7 @@ COMMAND commands[]; extern int done; struct nc_session *session; volatile int interleave; -int timed; +int timed, monitor; static int cmd_disconnect(const char *arg, char **tmp_config_file); @@ -1264,6 +1264,12 @@ cmd_timed_help(void) printf("timed [--help] [on | off]\n"); } +static void +cmd_monitor_help(void) +{ + printf("monitor [--help] [on | off]\n"); +} + #ifdef NC_ENABLED_SSH_TLS static void @@ -2557,6 +2563,61 @@ cmd_disconnect(const char *UNUSED(arg), char **UNUSED(tmp_config_file)) return EXIT_SUCCESS; } +void +monitoring_clb(struct nc_session *sess, void *user_data) +{ + int was_rawmode = 0; + + (void)sess; + (void)user_data; + + /* needed for the case that the user is typing a command */ + if (lss.rawmode) { + was_rawmode = 1; + linenoiseDisableRawMode(lss.ifd); + printf("\n"); + } + + fprintf(stdout, "Connection reset by peer.\n"); + fflush(stdout); + + /* set the global session variable to NULL */ + session = NULL; + + if (was_rawmode) { + linenoiseEnableRawMode(lss.ifd); + linenoiseRefreshLine(); + } +} + +static int +cmd_monitor(const char *arg, char **UNUSED(tmp_config_file)) +{ + char *args = strdupa(arg); + char *cmd = NULL; + + strtok(args, " "); + if ((cmd = strtok(NULL, " ")) == NULL) { + fprintf(stdout, "Connection state will %sbe monitored.\n", monitor ? "" : "not "); + } else { + if (!strcmp(cmd, "on")) { + if (nc_client_monitoring_thread_start(monitoring_clb, NULL, NULL)) { + ERROR(__func__, "Monitoring thread failed to start."); + return 1; + } + monitor = 1; + } else if (!strcmp(cmd, "off")) { + monitor = 0; + nc_client_monitoring_thread_stop(); + } else { + ERROR(__func__, "Unknown option %s.", cmd); + cmd_monitor_help(); + } + } + + return 0; +} + static int cmd_status(const char *UNUSED(arg), char **UNUSED(tmp_config_file)) { @@ -6575,6 +6636,7 @@ COMMAND commands[] = { "ietf-subscribed-notifications operation with ietf-yang-push augments" }, {"modify-sub", cmd_modifysub, cmd_modifysub_help, "ietf-subscribed-notifications operation"}, + {"monitor", cmd_monitor, cmd_monitor_help, "Monitor client connection status"}, {"outputformat", cmd_outputformat, cmd_outputformat_help, "Set the output format of all the data"}, {"resync-sub", cmd_resyncsub, cmd_resyncsub_help, "ietf-yang-push operation"}, {"searchpath", cmd_searchpath, cmd_searchpath_help, "Set the search path for models"}, diff --git a/cli/doc/netopeer2-cli.1 b/cli/doc/netopeer2-cli.1 index a6ab1a73..f635f503 100644 --- a/cli/doc/netopeer2-cli.1 +++ b/cli/doc/netopeer2-cli.1 @@ -1149,6 +1149,16 @@ Current time minus the given number of seconds. .PP +.SS monitor +Monitor the client connection status. +.PP + +.B monitor +[\-\-help] [on | off] +.RE +.RE + + .SS outputformat Set the format for all the output data. XML is the default. .PP diff --git a/cli/main.c b/cli/main.c index dc4abcee..c68f1854 100644 --- a/cli/main.c +++ b/cli/main.c @@ -39,6 +39,7 @@ int done; extern struct nc_session *session; +extern int monitor; static void lnc2_print_clb(const struct nc_session *UNUSED(session), NC_VERB_LEVEL level, const char *msg) @@ -220,6 +221,9 @@ main(void) if (session) { nc_session_free(session, NULL); } + if (monitor) { + nc_client_monitoring_thread_stop(); + } nc_client_destroy(); return 0; From fda79adc0628ee8374f41c042d5a4b645946e8fd Mon Sep 17 00:00:00 2001 From: roman Date: Fri, 4 Oct 2024 15:15:41 +0200 Subject: [PATCH 2/2] VERSION bump to version 2.2.32 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6de32ccf..ee61c789 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ endif() # Generic version of not only the library. Major version is reserved for really big changes of the project, # minor version changes with added functionality (new tool, functionality of the tool or library, ...) and # micro version is changed with a set of small changes or bugfixes anywhere in the project. -set(NP2SRV_VERSION 2.2.31) +set(NP2SRV_VERSION 2.2.32) # libyang required version set(LIBYANG_DEP_VERSION 2.2.0)