session server ch UPDATE ch threads creation
diff --git a/doc/libnetconf.doc b/doc/libnetconf.doc
index 9a788a4..83ebe78 100644
--- a/doc/libnetconf.doc
+++ b/doc/libnetconf.doc
@@ -245,7 +245,6 @@
*
* Available in __nc_client.h__.
*
- * - ::nc_client_ssh_ch_set_auth_hostkey_check_clb()
* - ::nc_client_ssh_ch_set_auth_password_clb()
* - ::nc_client_ssh_ch_set_auth_interactive_clb()
* - ::nc_client_ssh_ch_set_auth_privkey_passphrase_clb()
@@ -485,10 +484,16 @@
*
* _Call Home_ works with endpoints just like standard sessions, but
* the options are organized a bit differently and endpoints are added
- * for CH clients. However, one important difference is that
- * once all the mandatory options are set, _libnetconf2_ __will not__
- * immediately start connecting to a client. It will do so only after
- * calling ::nc_connect_ch_client_dispatch() in a separate thread.
+ * for CH clients.
+ * You may choose one of two approaches for creating a new Call Home
+ * session (or in other words making a server connect to a client).
+ * The first is to set all the required callbacks
+ * by calling ::nc_server_ch_set_dispatch_data(). By setting the callbacks,
+ * the server will automatically start connecting to a client, whenever
+ * a new Call Home client is created.
+ * The second approach is to create the Call Home thread manually.
+ * To do this, you need to call ::nc_connect_ch_client_dispatch(),
+ * which then creates a new thread and the server will start to connect.
* Unix socket _Call Home_ sessions are not supported.
*
* Lastly, monitoring of these sessions is up to the application.
diff --git a/src/server_config.c b/src/server_config.c
index ce67f57..d31c191 100644
--- a/src/server_config.c
+++ b/src/server_config.c
@@ -3745,6 +3745,18 @@
if (ret) {
goto cleanup;
}
+
+ if (server_opts.ch_dispatch_data.acquire_ctx_cb && server_opts.ch_dispatch_data.release_ctx_cb &&
+ server_opts.ch_dispatch_data.new_session_cb) {
+ /* we have all we need for dispatching a new call home thread */
+ ret = nc_connect_ch_client_dispatch(lyd_get_value(lyd_child(node)), server_opts.ch_dispatch_data.acquire_ctx_cb,
+ server_opts.ch_dispatch_data.release_ctx_cb, server_opts.ch_dispatch_data.ctx_cb_data,
+ server_opts.ch_dispatch_data.new_session_cb, server_opts.ch_dispatch_data.new_session_cb_data);
+ if (ret) {
+ ERR(NULL, "Dispatching a new Call Home thread failed for Call Home client \"%s\".", lyd_get_value(lyd_child(node)));
+ goto cleanup;
+ }
+ }
} else if (op == NC_OP_DELETE) {
if (nc_server_config_get_ch_client(node, &ch_client)) {
ret = 1;
diff --git a/src/session_p.h b/src/session_p.h
index 2f06a5c..5d2ccb1 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -27,6 +27,7 @@
#include "compat.h"
#include "config.h"
#include "session_client.h"
+#include "session_server_ch.h"
/**
* Enumeration of diff operation types.
@@ -513,6 +514,14 @@
uint16_t ch_client_count;
pthread_rwlock_t ch_client_lock;
+ struct nc_ch_dispatch_data {
+ nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb;
+ nc_server_ch_session_release_ctx_cb release_ctx_cb;
+ void *ctx_cb_data;
+ nc_server_ch_new_session_cb new_session_cb;
+ void *new_session_cb_data;
+ } ch_dispatch_data;
+
/* Atomic IDs */
ATOMIC_T new_session_id;
ATOMIC_T new_client_id;
diff --git a/src/session_server.c b/src/session_server.c
index 30db040..1fd84fb 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -277,6 +277,20 @@
return ret;
}
+API void
+nc_server_ch_set_dispatch_data(nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb,
+ nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb,
+ void *new_session_cb_data)
+{
+ NC_CHECK_ARG_RET(NULL, acquire_ctx_cb, release_ctx_cb, new_session_cb, );
+
+ server_opts.ch_dispatch_data.acquire_ctx_cb = acquire_ctx_cb;
+ server_opts.ch_dispatch_data.release_ctx_cb = release_ctx_cb;
+ server_opts.ch_dispatch_data.ctx_cb_data = ctx_cb_data;
+ server_opts.ch_dispatch_data.new_session_cb = new_session_cb;
+ server_opts.ch_dispatch_data.new_session_cb_data = new_session_cb_data;
+}
+
int
nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka)
{
diff --git a/src/session_server_ch.h b/src/session_server_ch.h
index a465c09..6b5ff19 100644
--- a/src/session_server_ch.h
+++ b/src/session_server_ch.h
@@ -85,6 +85,7 @@
*
* @param[in] client_name Name of the CH client which established the session.
* @param[in] new_session New established CH session, the pointer is internally discarded afterwards.
+ * @param[in] user_data Arbitrary new session callback data.
* @return 0 on success;
* @return non-zero on error and @p new_session is freed.
*/
@@ -105,6 +106,22 @@
nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb,
void *new_session_cb_data);
+/**
+ * @brief Set callbacks and their data for Call Home threads.
+ *
+ * If set, Call Home threads will be dispatched automatically upon creation of a new Call Home clients.
+ * Calling this will replace all the previously set callbacks and their data.
+ *
+ * @param[in] acquire_ctx_cb Callback for acquiring new session context.
+ * @param[in] release_ctx_cb Callback for releasing session context.
+ * @param[in] ctx_cb_data Arbitrary user data passed to @p acquire_ctx_cb and @p release_ctx_cb.
+ * @param[in] new_session_cb Callback called for every established session on the client.
+ * @param[in] new_session_cb_data Arbitrary user data passed to @p new_session_cb.
+ */
+void nc_server_ch_set_dispatch_data(nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb,
+ nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb,
+ void *new_session_cb_data);
+
/** @} Server-side Call Home Functions */
#endif /* NC_ENABLED_SSH_TLS */