Skip to content

Commit df077cb

Browse files
committed
session server UPDATE new callback for CH new session fail
1 parent f74dce5 commit df077cb

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/session_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ struct nc_server_ch_thread_arg {
670670
void *ctx_cb_data; /**< acq/rel cb data */
671671
int (*new_session_cb)(const char *client_name, struct nc_session *new_session, void *user_data); /**< creating new session cb */
672672
void *new_session_cb_data; /**< new session cb data */
673+
void (*new_session_fail_cb)(const char *client_name, const char *endpt_name, uint8_t max_attempts,
674+
uint8_t cur_attempt, void *user_data); /**< failed to create a new session cb */
675+
void *new_session_fail_cb_data; /**< new session fail cb data */
673676

674677
int thread_running; /**< A boolean value that is truthy while the underlying Call Home thread is running */
675678
pthread_mutex_t cond_lock; /**< Condition's lock used for signalling the thread to terminate */
@@ -793,6 +796,8 @@ struct nc_server_opts {
793796
void *ctx_cb_data; /**< Data passed to the callbacks above. */
794797
nc_server_ch_new_session_cb new_session_cb; /**< New session callback. */
795798
void *new_session_cb_data; /**< Data passed to the new_session_cb callback. */
799+
nc_server_ch_new_session_fail_cb new_session_fail_cb; /**< New session fail callback, */
800+
void *new_session_fail_cb_data; /**< Data passed to the new_session_fail_cb callback. */
796801
} ch_dispatch_data;
797802

798803
struct {

src/session_server.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,22 @@ nc_server_ch_set_dispatch_data(nc_server_ch_session_acquire_ctx_cb acquire_ctx_c
255255
nc_rwlock_unlock(&server_opts.config_lock, __func__);
256256
}
257257

258+
API void
259+
nc_server_ch_set_new_session_fail_cb(nc_server_ch_new_session_fail_cb new_session_fail_cb,
260+
void *new_session_fail_cb_data)
261+
{
262+
/* CONFIG WRITE LOCK */
263+
if (nc_rwlock_lock(&server_opts.config_lock, NC_RWLOCK_WRITE, NC_CONFIG_LOCK_TIMEOUT, __func__) != 1) {
264+
return;
265+
}
266+
267+
server_opts.ch_dispatch_data.new_session_fail_cb = new_session_fail_cb;
268+
server_opts.ch_dispatch_data.new_session_fail_cb_data = new_session_fail_cb_data;
269+
270+
/* CONFIG WRITE UNLOCK */
271+
nc_rwlock_unlock(&server_opts.config_lock, __func__);
272+
}
273+
258274
#endif
259275

260276
int
@@ -3291,7 +3307,7 @@ nc_ch_client_thread(void *arg)
32913307
{
32923308
struct nc_server_ch_thread_arg *data = arg;
32933309
NC_MSG_TYPE msgtype;
3294-
uint8_t cur_attempts = 0;
3310+
uint8_t cur_attempts = 0, max_attempts;
32953311
uint16_t next_endpt_index, max_wait;
32963312
char *cur_endpt_name = NULL;
32973313
struct nc_ch_endpt *cur_endpt;
@@ -3416,11 +3432,19 @@ nc_ch_client_thread(void *arg)
34163432
}
34173433
} else {
34183434
/* session was not created, wait a little bit and try again */
3435+
++cur_attempts;
34193436
max_wait = client->max_wait;
3437+
max_attempts = client->max_attempts;
34203438

34213439
/* CONFIG READ UNLOCK */
34223440
nc_rwlock_unlock(&server_opts.config_lock, __func__);
34233441

3442+
/* failed connection attempt */
3443+
if (data->new_session_fail_cb) {
3444+
data->new_session_fail_cb(data->client_name, cur_endpt_name, max_attempts, cur_attempts,
3445+
data->new_session_fail_cb_data);
3446+
}
3447+
34243448
/* wait for max_wait seconds */
34253449
if (!nc_server_ch_client_thread_is_running_wait(session, data, max_wait)) {
34263450
/* thread should stop running */
@@ -3439,8 +3463,6 @@ nc_ch_client_thread(void *arg)
34393463
goto cleanup_unlock;
34403464
}
34413465

3442-
++cur_attempts;
3443-
34443466
/* try to find our endpoint again */
34453467
LY_ARRAY_FOR(client->ch_endpts, next_endpt_index) {
34463468
if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) {
@@ -3555,6 +3577,8 @@ _nc_connect_ch_client_dispatch(const struct nc_ch_client *ch_client, nc_server_c
35553577
arg->ctx_cb_data = ctx_cb_data;
35563578
arg->new_session_cb = new_session_cb;
35573579
arg->new_session_cb_data = new_session_cb_data;
3580+
arg->new_session_fail_cb = server_opts.ch_dispatch_data.new_session_fail_cb;
3581+
arg->new_session_fail_cb_data = server_opts.ch_dispatch_data.new_session_fail_cb_data;
35583582
pthread_cond_init(&arg->cond, NULL);
35593583
pthread_mutex_init(&arg->cond_lock, NULL);
35603584

src/session_server_ch.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ typedef void (*nc_server_ch_session_release_ctx_cb)(void *cb_data);
9191
*/
9292
typedef int (*nc_server_ch_new_session_cb)(const char *client_name, struct nc_session *new_session, void *user_data);
9393

94+
/**
95+
* @brief Callback for failures when creating new Call Home sessions.
96+
*
97+
* @param[in] client_name Name of the CH client that failed to create the session.
98+
* @param[in] endpt_name Name of the CH client endpoint that failed to create the session.
99+
* @param[in] max_attemps Maximum connection attempts for the endpoints.
100+
* @param[in] cur_attempt Current failed connection attempt.
101+
* @param[in] user_data Arbitrary new session callback data.
102+
*/
103+
typedef void (*nc_server_ch_new_session_fail_cb)(const char *client_name, const char *endpt_name, uint8_t max_attempts,
104+
uint8_t cur_attempt, void *user_data);
105+
94106
/**
95107
* @brief Dispatch a thread connecting to a listening NETCONF client and creating Call Home sessions.
96108
*
@@ -121,6 +133,15 @@ void nc_server_ch_set_dispatch_data(nc_server_ch_session_acquire_ctx_cb acquire_
121133
nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb,
122134
void *new_session_cb_data);
123135

136+
/**
137+
* @brief Set callback for failure of a new CH session creation.
138+
*
139+
* @param[in] new_session_fail_cb Callback to call for every failed session creation.
140+
* @param[in] new_session_fail_cb_data Arbitrary user data passed to @p new_session_fail_cb.
141+
*/
142+
void nc_server_ch_set_new_session_fail_cb(nc_server_ch_new_session_fail_cb new_session_fail_cb,
143+
void *new_session_fail_cb_data);
144+
124145
/** @} Server-side Call Home Functions */
125146

126147
#endif /* NC_ENABLED_SSH_TLS */

0 commit comments

Comments
 (0)