Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_client.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 session client functions |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
| 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
Michal Vasko | bbf52c8 | 2020-04-07 13:08:50 +0200 | [diff] [blame] | 17 | |
| 18 | #ifdef __linux__ |
| 19 | # include <sys/syscall.h> |
| 20 | #endif |
| 21 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 23 | #include <assert.h> |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 24 | #include <ctype.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <netdb.h> |
Radek Krejci | 4cf58ec | 2016-02-26 15:04:52 +0100 | [diff] [blame] | 28 | #include <netinet/in.h> |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 29 | #include <netinet/tcp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 30 | #include <poll.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 31 | #include <pthread.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 32 | #include <pwd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 35 | #include <sys/select.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 39 | #include <sys/un.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 40 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 41 | |
| 42 | #include <libyang/libyang.h> |
| 43 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 44 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 45 | #include "config.h" |
| 46 | #include "log_p.h" |
| 47 | #include "messages_p.h" |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 48 | #include "session_client.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 49 | #include "session_client_ch.h" |
| 50 | #include "session_p.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 51 | |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 52 | #include "../modules/ietf_netconf@2013-09-29_yang.h" |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 53 | #include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h" |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 54 | |
Michal Vasko | 80ef5d2 | 2016-01-18 09:21:02 +0100 | [diff] [blame] | 55 | static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"}; |
| 56 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 57 | #ifdef NC_ENABLED_SSH_TLS |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 58 | char *sshauth_password(const char *username, const char *hostname, void *priv); |
| 59 | char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 60 | char *sshauth_privkey_passphrase(const char *privkey_path, void *priv); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 61 | #endif /* NC_ENABLED_SSH_TLS */ |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 62 | |
| 63 | static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT; |
| 64 | static pthread_key_t nc_client_context_key; |
| 65 | #ifdef __linux__ |
| 66 | static struct nc_client_context context_main = { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 67 | .opts.ka = { |
| 68 | .enabled = 1, |
| 69 | .idle_time = 1, |
| 70 | .max_probes = 10, |
| 71 | .probe_interval = 5 |
| 72 | }, |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 73 | #ifdef NC_ENABLED_SSH_TLS |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 74 | .ssh_opts = { |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 75 | .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}}, |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 76 | .auth_password = sshauth_password, |
| 77 | .auth_interactive = sshauth_interactive, |
| 78 | .auth_privkey_passphrase = sshauth_privkey_passphrase |
| 79 | }, |
| 80 | .ssh_ch_opts = { |
| 81 | .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}}, |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 82 | .auth_password = sshauth_password, |
| 83 | .auth_interactive = sshauth_interactive, |
| 84 | .auth_privkey_passphrase = sshauth_privkey_passphrase |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 85 | }, |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 86 | #endif /* NC_ENABLED_SSH_TLS */ |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 87 | /* .tls_ structures zeroed */ |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 88 | .refcount = 0 |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 89 | }; |
| 90 | #endif |
| 91 | |
| 92 | static void |
| 93 | nc_client_context_free(void *ptr) |
| 94 | { |
| 95 | struct nc_client_context *c = (struct nc_client_context *)ptr; |
| 96 | |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 97 | if (--(c->refcount)) { |
| 98 | /* still used */ |
| 99 | return; |
| 100 | } |
| 101 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 102 | #ifdef __linux__ |
| 103 | /* in __linux__ we use static memory in the main thread, |
| 104 | * so this check is for programs terminating the main() |
| 105 | * function by pthread_exit() :) |
| 106 | */ |
| 107 | if (c != &context_main) |
| 108 | #endif |
| 109 | { |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 110 | /* for the main thread the same is done in nc_client_destroy() */ |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 111 | free(c->opts.schema_searchpath); |
| 112 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 113 | #ifdef NC_ENABLED_SSH_TLS |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 114 | int i; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 115 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 116 | for (i = 0; i < c->opts.ch_bind_count; ++i) { |
| 117 | close(c->opts.ch_binds[i].sock); |
| 118 | free((char *)c->opts.ch_binds[i].address); |
| 119 | } |
| 120 | free(c->opts.ch_binds); |
| 121 | c->opts.ch_binds = NULL; |
| 122 | c->opts.ch_bind_count = 0; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 123 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 124 | _nc_client_ssh_destroy_opts(&c->ssh_opts); |
| 125 | _nc_client_ssh_destroy_opts(&c->ssh_ch_opts); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 126 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 127 | _nc_client_tls_destroy_opts(&c->tls_opts); |
| 128 | _nc_client_tls_destroy_opts(&c->tls_ch_opts); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 129 | #endif /* NC_ENABLED_SSH_TLS */ |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 130 | free(c); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | nc_client_context_createkey(void) |
| 136 | { |
| 137 | int r; |
| 138 | |
| 139 | /* initiate */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 140 | while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {} |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 141 | pthread_setspecific(nc_client_context_key, NULL); |
| 142 | } |
| 143 | |
| 144 | struct nc_client_context * |
| 145 | nc_client_context_location(void) |
| 146 | { |
| 147 | struct nc_client_context *e; |
| 148 | |
| 149 | pthread_once(&nc_client_context_once, nc_client_context_createkey); |
| 150 | e = pthread_getspecific(nc_client_context_key); |
| 151 | if (!e) { |
| 152 | /* prepare ly_err storage */ |
| 153 | #ifdef __linux__ |
| 154 | if (getpid() == syscall(SYS_gettid)) { |
| 155 | /* main thread - use global variable instead of thread-specific variable. */ |
| 156 | e = &context_main; |
| 157 | } else |
| 158 | #endif /* __linux__ */ |
| 159 | { |
| 160 | e = calloc(1, sizeof *e); |
| 161 | /* set default values */ |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 162 | e->refcount = 1; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 163 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 487ab80 | 2024-04-26 12:20:29 +0200 | [diff] [blame] | 164 | # ifdef HAVE_TERMIOS |
| 165 | e->ssh_opts.knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK; |
| 166 | # else |
| 167 | e->ssh_opts.knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT; |
| 168 | # endif |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 169 | e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 170 | e->ssh_opts.auth_pref[0].value = 1; |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 171 | e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD; |
| 172 | e->ssh_opts.auth_pref[1].value = 2; |
| 173 | e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 174 | e->ssh_opts.auth_pref[2].value = 3; |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 175 | e->ssh_opts.auth_password = sshauth_password; |
| 176 | e->ssh_opts.auth_interactive = sshauth_interactive; |
| 177 | e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase; |
| 178 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 179 | /* callhome settings are the same */ |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 180 | memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts); |
| 181 | e->ssh_ch_opts.auth_pref[0].value = 1; |
| 182 | e->ssh_ch_opts.auth_pref[1].value = 2; |
| 183 | e->ssh_ch_opts.auth_pref[2].value = 3; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 184 | #endif /* NC_ENABLED_SSH_TLS */ |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 185 | } |
| 186 | pthread_setspecific(nc_client_context_key, e); |
| 187 | } |
| 188 | |
| 189 | return e; |
| 190 | } |
| 191 | |
| 192 | #define client_opts nc_client_context_location()->opts |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 193 | |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 194 | API void * |
| 195 | nc_client_get_thread_context(void) |
| 196 | { |
| 197 | return nc_client_context_location(); |
| 198 | } |
| 199 | |
| 200 | API void |
| 201 | nc_client_set_thread_context(void *context) |
| 202 | { |
| 203 | struct nc_client_context *old, *new; |
| 204 | |
| 205 | if (!context) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 206 | ERRARG(NULL, "context"); |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
| 210 | new = (struct nc_client_context *)context; |
| 211 | old = nc_client_context_location(); |
| 212 | if (old == new) { |
| 213 | /* nothing to change */ |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | /* replace old by new, increase reference counter in the newly set context */ |
| 218 | nc_client_context_free(old); |
| 219 | new->refcount++; |
| 220 | pthread_setspecific(nc_client_context_key, new); |
| 221 | } |
| 222 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 223 | /** |
| 224 | * @brief Ext data callback for a context to provide schema mount data. |
| 225 | */ |
| 226 | static LY_ERR |
| 227 | nc_ly_ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free) |
| 228 | { |
| 229 | struct nc_session *session = user_data; |
| 230 | |
| 231 | if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) { |
| 232 | return LY_EINVAL; |
| 233 | } |
| 234 | |
| 235 | if (!session->opts.client.ext_data) { |
| 236 | ERR(session, "Unable to parse mounted data, no operational schema-mounts data received from the server."); |
| 237 | return LY_ENOTFOUND; |
| 238 | } |
| 239 | |
| 240 | /* return ext data */ |
| 241 | *ext_data = session->opts.client.ext_data; |
| 242 | *ext_data_free = 0; |
| 243 | |
| 244 | return LY_SUCCESS; |
| 245 | } |
| 246 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 247 | int |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 248 | nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx) |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 249 | { |
| 250 | /* assign context (dicionary needed for handshake) */ |
| 251 | if (!ctx) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 252 | if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 253 | return EXIT_FAILURE; |
| 254 | } |
| 255 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 256 | /* user path must be first, the first path is used to store modules retreived via get-schema */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 257 | if (client_opts.schema_searchpath) { |
| 258 | ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath); |
Michal Vasko | 5d0cdc3 | 2022-12-12 07:38:21 +0100 | [diff] [blame] | 259 | } |
Michal Vasko | 8022722 | 2022-12-12 09:05:24 +0100 | [diff] [blame] | 260 | if (!access(NC_CLIENT_SEARCH_DIR, F_OK)) { |
| 261 | ly_ctx_set_searchdir(ctx, NC_CLIENT_SEARCH_DIR); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 262 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 263 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 264 | /* set callback for getting modules, if provided */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 265 | ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data); |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 266 | |
| 267 | /* set ext data callback to avoid errors that no callback is set, the data are stored later, if any */ |
| 268 | ly_ctx_set_ext_data_clb(ctx, nc_ly_ext_data_clb, session); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 269 | } else { |
| 270 | session->flags |= NC_SESSION_SHAREDCTX; |
| 271 | } |
| 272 | |
| 273 | session->ctx = ctx; |
| 274 | |
| 275 | return EXIT_SUCCESS; |
| 276 | } |
| 277 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 278 | API int |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 279 | nc_client_set_schema_searchpath(const char *path) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 280 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 281 | if (client_opts.schema_searchpath) { |
| 282 | free(client_opts.schema_searchpath); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 283 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 284 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 285 | if (path) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 286 | client_opts.schema_searchpath = strdup(path); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 287 | NC_CHECK_ERRMEM_RET(!client_opts.schema_searchpath, 1); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 288 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 289 | client_opts.schema_searchpath = NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 295 | API const char * |
| 296 | nc_client_get_schema_searchpath(void) |
| 297 | { |
| 298 | return client_opts.schema_searchpath; |
| 299 | } |
| 300 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 301 | API int |
| 302 | nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 303 | { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 304 | client_opts.schema_clb = clb; |
| 305 | if (clb) { |
| 306 | client_opts.schema_clb_data = user_data; |
| 307 | } else { |
| 308 | client_opts.schema_clb_data = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 314 | API ly_module_imp_clb |
| 315 | nc_client_get_schema_callback(void **user_data) |
| 316 | { |
| 317 | if (user_data) { |
| 318 | (*user_data) = client_opts.schema_clb_data; |
| 319 | } |
| 320 | return client_opts.schema_clb; |
| 321 | } |
| 322 | |
Michal Vasko | a272e09 | 2023-07-10 14:34:03 +0200 | [diff] [blame] | 323 | API void |
| 324 | nc_client_set_new_session_context_autofill(int enabled) |
| 325 | { |
| 326 | client_opts.auto_context_fill_disabled = !enabled; |
| 327 | } |
| 328 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 329 | struct module_info { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 330 | char *name; |
| 331 | char *revision; |
Michal Vasko | 6ce8e82 | 2022-08-02 15:09:17 +0200 | [diff] [blame] | 332 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 333 | struct { |
| 334 | char *name; |
| 335 | char *revision; |
| 336 | } *submodules; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 337 | char **features; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 338 | int implemented; |
| 339 | }; |
| 340 | |
| 341 | struct clb_data_s { |
| 342 | void *user_data; |
| 343 | ly_module_imp_clb user_clb; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 344 | struct module_info *modules; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 345 | struct nc_session *session; |
| 346 | int has_get_schema; |
| 347 | }; |
| 348 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 349 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 350 | * @brief Retrieve YANG module content from a local file. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 351 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 352 | * @param[in] name Module name. |
| 353 | * @param[in] rev Module revision. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 354 | * @param[in] clb_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 355 | * @param[out] format Module format. |
| 356 | * @return Module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 357 | */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 358 | static char * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 359 | retrieve_module_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 360 | LYS_INFORMAT *format) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 361 | { |
Michal Vasko | b13a224 | 2023-12-05 13:29:07 +0100 | [diff] [blame] | 362 | char *localfile = NULL, *model_data = NULL; |
| 363 | const char *ptr; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 364 | FILE *f; |
| 365 | long length, l; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 366 | |
Radek Krejci | 3b60e5c | 2018-08-17 10:10:16 +0200 | [diff] [blame] | 367 | if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 368 | !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD), |
| 369 | name, rev, &localfile, format)) { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 370 | return NULL; |
| 371 | } |
Michal Vasko | b13a224 | 2023-12-05 13:29:07 +0100 | [diff] [blame] | 372 | if (localfile && rev) { |
| 373 | ptr = strrchr(localfile, '/'); |
| 374 | if (!strchr(ptr, '@')) { |
| 375 | /* we do not know the revision of the module and we require a specific one, so ignore this module */ |
| 376 | localfile = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 377 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Michal Vasko | b13a224 | 2023-12-05 13:29:07 +0100 | [diff] [blame] | 380 | if (!localfile) { |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | VRB(clb_data->session, "Reading module \"%s@%s\" from local file \"%s\".", name, rev ? rev : "<latest>", |
| 385 | localfile); |
| 386 | f = fopen(localfile, "r"); |
| 387 | if (!f) { |
| 388 | ERR(clb_data->session, "Unable to open file \"%s\" (%s).", localfile, strerror(errno)); |
| 389 | free(localfile); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | fseek(f, 0, SEEK_END); |
| 394 | length = ftell(f); |
| 395 | if (length < 0) { |
| 396 | ERR(clb_data->session, "Unable to get the size of module file \"%s\".", localfile); |
| 397 | free(localfile); |
| 398 | fclose(f); |
| 399 | return NULL; |
| 400 | } |
| 401 | fseek(f, 0, SEEK_SET); |
| 402 | |
| 403 | model_data = malloc(length + 1); |
| 404 | if (!model_data) { |
| 405 | ERRMEM; |
| 406 | } else if ((l = fread(model_data, 1, length, f)) != length) { |
| 407 | ERR(clb_data->session, "Reading module from \"%s\" failed (%d bytes read, but %d expected).", localfile, l, |
| 408 | length); |
| 409 | free(model_data); |
| 410 | model_data = NULL; |
| 411 | } else { |
| 412 | /* terminating NULL byte */ |
| 413 | model_data[length] = '\0'; |
| 414 | } |
| 415 | fclose(f); |
| 416 | free(localfile); |
| 417 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 418 | return model_data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 421 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 422 | * @brief Retrieve YANG module content from a reply to get-schema RPC. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 423 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 424 | * @param[in] name Module name. |
| 425 | * @param[in] rev Module revision. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 426 | * @param[in] clb_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 427 | * @param[out] format Module format. |
| 428 | * @return Module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 429 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 430 | static char * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 431 | retrieve_module_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 432 | LYS_INFORMAT *format) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 433 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 434 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 435 | struct lyd_node *envp = NULL, *op = NULL; |
| 436 | struct lyd_node_any *get_schema_data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 437 | NC_MSG_TYPE msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 438 | uint64_t msgid; |
Michal Vasko | 7502b60 | 2022-08-26 11:51:17 +0200 | [diff] [blame] | 439 | char *localfile = NULL, *envp_str = NULL, *model_data = NULL; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 440 | FILE *f; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 441 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 442 | VRB(clb_data->session, "Reading module \"%s@%s\" from server via get-schema.", name, rev ? rev : "<latest>"); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 443 | rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 444 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 445 | while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 446 | usleep(1000); |
| 447 | } |
| 448 | if (msg == NC_MSG_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 449 | ERR(clb_data->session, "Failed to send the <get-schema> RPC."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 450 | nc_rpc_free(rpc); |
| 451 | return NULL; |
| 452 | } |
| 453 | |
Michal Vasko | ff8ddc0 | 2016-10-03 14:13:29 +0200 | [diff] [blame] | 454 | do { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 455 | msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op); |
Robin Jarry | 52ddb95 | 2019-10-15 16:23:01 +0200 | [diff] [blame] | 456 | } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 457 | nc_rpc_free(rpc); |
| 458 | if (msg == NC_MSG_WOULDBLOCK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 459 | ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 460 | goto cleanup; |
Michal Vasko | 8d0f3b3 | 2022-01-12 08:29:14 +0100 | [diff] [blame] | 461 | } else if (msg == NC_MSG_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 462 | ERR(clb_data->session, "Failed to receive a reply to <get-schema>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 463 | goto cleanup; |
Michal Vasko | 8d0f3b3 | 2022-01-12 08:29:14 +0100 | [diff] [blame] | 464 | } else if (!op) { |
Michal Vasko | 7502b60 | 2022-08-26 11:51:17 +0200 | [diff] [blame] | 465 | assert(envp); |
| 466 | lyd_print_mem(&envp_str, envp, LYD_XML, 0); |
| 467 | WRN(clb_data->session, "Received an unexpected reply to <get-schema>:\n%s", envp_str); |
| 468 | free(envp_str); |
Michal Vasko | 8d0f3b3 | 2022-01-12 08:29:14 +0100 | [diff] [blame] | 469 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 472 | if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 473 | ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 474 | goto cleanup; |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 475 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 476 | get_schema_data = (struct lyd_node_any *)lyd_child(op); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 477 | switch (get_schema_data->value_type) { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 478 | case LYD_ANYDATA_STRING: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 479 | case LYD_ANYDATA_XML: |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 480 | model_data = strdup(get_schema_data->value.str); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 481 | break; |
| 482 | case LYD_ANYDATA_DATATREE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 483 | lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 484 | break; |
Radek Krejci | 03438ec | 2016-09-21 14:12:26 +0200 | [diff] [blame] | 485 | case LYD_ANYDATA_JSON: |
Michal Vasko | d838d29 | 2018-08-15 11:39:05 +0200 | [diff] [blame] | 486 | case LYD_ANYDATA_LYB: |
Radek Krejci | 03438ec | 2016-09-21 14:12:26 +0200 | [diff] [blame] | 487 | ERRINT; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 488 | break; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 489 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 490 | |
Radek Krejci | 488b070 | 2018-08-29 14:47:15 +0200 | [diff] [blame] | 491 | if (model_data && !model_data[0]) { |
| 492 | /* empty data */ |
| 493 | free(model_data); |
| 494 | model_data = NULL; |
| 495 | } |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 496 | if (!model_data) { |
| 497 | goto cleanup; |
| 498 | } |
| 499 | |
| 500 | /* set format */ |
| 501 | *format = LYS_IN_YANG; |
Radek Krejci | 488b070 | 2018-08-29 14:47:15 +0200 | [diff] [blame] | 502 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 503 | /* try to store the model_data into local module repository */ |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 504 | lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), 0, name, rev, &localfile, NULL); |
| 505 | if (client_opts.schema_searchpath && !localfile) { |
| 506 | if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "", |
| 507 | rev ? rev : "") == -1) { |
| 508 | ERRMEM; |
| 509 | } else { |
| 510 | f = fopen(localfile, "w"); |
| 511 | if (!f) { |
| 512 | WRN(clb_data->session, "Unable to store \"%s\" as a local copy of module retrieved via <get-schema> (%s).", |
| 513 | localfile, strerror(errno)); |
Michal Vasko | f945da5 | 2018-02-15 08:45:13 +0100 | [diff] [blame] | 514 | } else { |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 515 | fputs(model_data, f); |
| 516 | fclose(f); |
Michal Vasko | f945da5 | 2018-02-15 08:45:13 +0100 | [diff] [blame] | 517 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 518 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 519 | } |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 520 | free(localfile); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 521 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 522 | cleanup: |
| 523 | lyd_free_tree(envp); |
| 524 | lyd_free_tree(op); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 525 | return model_data; |
| 526 | } |
| 527 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 528 | static void |
| 529 | free_with_user_data(void *data, void *user_data) |
Jan Kundrát | 35972df | 2018-09-06 19:00:01 +0200 | [diff] [blame] | 530 | { |
| 531 | free(data); |
| 532 | (void)user_data; |
| 533 | } |
| 534 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 535 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 536 | * @brief Retrieve YANG module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 537 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 538 | * @param[in] mod_name Module name. |
| 539 | * @param[in] mod_rev Module revision. |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 540 | * @param[in] user_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 541 | * @param[out] format Module format. |
| 542 | * @param[out] module_data Module content. |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 543 | * @param[out] free_module_data Callback for freeing @p module_data. |
| 544 | * @return LY_ERR value. |
| 545 | */ |
| 546 | static LY_ERR |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 547 | retrieve_module_data(const char *mod_name, const char *mod_rev, void *user_data, LYS_INFORMAT *format, |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 548 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 549 | { |
| 550 | struct clb_data_s *clb_data = (struct clb_data_s *)user_data; |
| 551 | char *model_data = NULL; |
| 552 | |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 553 | /* 1. try to get data locally */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 554 | model_data = retrieve_module_data_localfile(mod_name, mod_rev, clb_data, format); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 555 | |
| 556 | /* 2. try to use <get-schema> */ |
| 557 | if (!model_data && clb_data->has_get_schema) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 558 | model_data = retrieve_module_data_getschema(mod_name, mod_rev, clb_data, format); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* 3. try to use user callback */ |
| 562 | if (!model_data && clb_data->user_clb) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 563 | VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", mod_name, mod_rev ? mod_rev : "<latest>"); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 564 | clb_data->user_clb(mod_name, mod_rev, NULL, NULL, clb_data->user_data, format, (const char **)&model_data, |
| 565 | free_module_data); |
| 566 | } |
| 567 | |
| 568 | *free_module_data = free_with_user_data; |
| 569 | *module_data = model_data; |
| 570 | return *module_data ? LY_SUCCESS : LY_ENOTFOUND; |
| 571 | } |
| 572 | |
| 573 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 574 | * @brief Retrieve YANG import module content. |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 575 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 576 | * @param[in] mod_name Module name. |
| 577 | * @param[in] mod_rev Module revision. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 578 | * @param[in] submod_name Optional submodule name. |
| 579 | * @param[in] sub_rev Submodule revision. |
| 580 | * @param[in] user_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 581 | * @param[out] format Module format. |
| 582 | * @param[out] module_data Module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 583 | * @param[out] free_module_data Callback for freeing @p module_data. |
| 584 | * @return LY_ERR value. |
| 585 | */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 586 | static LY_ERR |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 587 | retrieve_module_data_imp(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 588 | void *user_data, LYS_INFORMAT *format, const char **module_data, |
| 589 | void (**free_module_data)(void *model_data, void *user_data)) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 590 | { |
| 591 | struct clb_data_s *clb_data = (struct clb_data_s *)user_data; |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 592 | uint32_t u, v, match = 1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 593 | const char *name = NULL, *rev = NULL; |
| 594 | char *model_data = NULL; |
| 595 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 596 | /* get and check the final name and revision of the module to be retrieved */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 597 | if (!mod_rev || !mod_rev[0]) { |
| 598 | /* newest revision requested - get the newest revision from the list of available modules on server */ |
| 599 | match = 0; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 600 | for (u = 0; clb_data->modules[u].name; ++u) { |
| 601 | if (strcmp(mod_name, clb_data->modules[u].name)) { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 602 | continue; |
| 603 | } |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 604 | if (!match || (strcmp(mod_rev, clb_data->modules[u].revision) > 0)) { |
| 605 | mod_rev = clb_data->modules[u].revision; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 606 | } |
| 607 | match = u + 1; |
| 608 | } |
| 609 | if (!match) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 610 | /* valid situation if we are retrieving YANG 1.1 module and have only capabilities for now |
Michal Vasko | 1d2665c | 2021-02-12 09:28:44 +0100 | [diff] [blame] | 611 | * (when loading ietf-datastore for ietf-yang-library) */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 612 | VRB(clb_data->session, "Unable to identify revision of the import module \"%s\" from " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 613 | "the available server side information.", mod_name); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | if (submod_name) { |
| 617 | name = submod_name; |
| 618 | if (sub_rev) { |
| 619 | rev = sub_rev; |
Radek Krejci | 6455eb1 | 2018-08-17 09:26:29 +0200 | [diff] [blame] | 620 | } else if (match) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 621 | if (!clb_data->modules[match - 1].submodules) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 622 | VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", " |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 623 | "in import module \"%s\", from the available server side information.", submod_name, mod_name); |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 624 | } else { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 625 | for (v = 0; clb_data->modules[match - 1].submodules[v].name; ++v) { |
| 626 | if (!strcmp(submod_name, clb_data->modules[match - 1].submodules[v].name)) { |
| 627 | rev = sub_rev = clb_data->modules[match - 1].submodules[v].revision; |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 628 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 629 | } |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 630 | if (!rev) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 631 | ERR(clb_data->session, "Requested submodule \"%s\" is not found in import module \"%s\" on server side.", |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 632 | submod_name, mod_name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 633 | return LY_ENOTFOUND; |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 634 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } else { |
| 638 | name = mod_name; |
| 639 | rev = mod_rev; |
| 640 | } |
| 641 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 642 | if (match) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 643 | /* we have enough information to avoid communication with server and try to get the module locally */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 644 | |
| 645 | /* 1. try to get data locally */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 646 | model_data = retrieve_module_data_localfile(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 647 | |
| 648 | /* 2. try to use <get-schema> */ |
| 649 | if (!model_data && clb_data->has_get_schema) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 650 | model_data = retrieve_module_data_getschema(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 651 | } |
| 652 | } else { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 653 | /* we are unsure which revision of the module we should load, so first try to get |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 654 | * the newest revision from the server via get-schema and only if the server does not |
| 655 | * implement get-schema, try to load the newest revision locally. This is imperfect |
| 656 | * solution, but there are situation when a client does not know what revision is |
| 657 | * actually implemented by the server. */ |
| 658 | |
| 659 | /* 1. try to use <get-schema> */ |
| 660 | if (clb_data->has_get_schema) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 661 | model_data = retrieve_module_data_getschema(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* 2. try to get data locally */ |
| 665 | if (!model_data) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 666 | model_data = retrieve_module_data_localfile(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | /* 3. try to use user callback */ |
| 671 | if (!model_data && clb_data->user_clb) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 672 | VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", name, rev ? rev : "<latest>"); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 673 | clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, |
| 674 | (const char **)&model_data, free_module_data); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Jan Kundrát | 35972df | 2018-09-06 19:00:01 +0200 | [diff] [blame] | 677 | *free_module_data = free_with_user_data; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 678 | *module_data = model_data; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 679 | return *module_data ? LY_SUCCESS : LY_ENOTFOUND; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 682 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 683 | * @brief Load a YANG module into context. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 684 | * |
| 685 | * @param[in] session NC session. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 686 | * @param[in] name Module name. |
| 687 | * @param[in] revision Module revision. |
| 688 | * @param[in] features Enabled module features. |
| 689 | * @param[in] modules Server module info built from capabilities. |
| 690 | * @param[in] user_clb User callback for retrieving module data. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 691 | * @param[in] user_data User data for @p user_clb. |
| 692 | * @param[in] has_get_schema Whether the server supports get-schema. |
| 693 | * @param[out] mod Loaded module. |
| 694 | * @return 0 on success. |
| 695 | * @return -1 on error. |
| 696 | */ |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 697 | static int |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 698 | nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, const char **features, |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 699 | struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod) |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 700 | { |
| 701 | int ret = 0; |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 702 | const struct ly_err_item *eitem; |
Jan Kundrát | 6d7c396 | 2018-09-06 19:01:07 +0200 | [diff] [blame] | 703 | const char *module_data = NULL; |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 704 | struct ly_in *in; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 705 | LYS_INFORMAT format; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 706 | |
| 707 | void (*free_module_data)(void *, void *) = NULL; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 708 | struct clb_data_s clb_data; |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 709 | |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 710 | /* try to use a module from the context */ |
Michal Vasko | c3e402f | 2022-09-14 13:34:55 +0200 | [diff] [blame] | 711 | *mod = ly_ctx_get_module_implemented(session->ctx, name); |
| 712 | if (!*mod) { |
| 713 | if (revision) { |
| 714 | *mod = ly_ctx_get_module(session->ctx, name, revision); |
| 715 | } else { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 716 | *mod = ly_ctx_get_module_latest(session->ctx, name); |
| 717 | } |
Michal Vasko | c3e402f | 2022-09-14 13:34:55 +0200 | [diff] [blame] | 718 | } else if (revision && (!(*mod)->revision || strcmp((*mod)->revision, revision))) { |
| 719 | WRN(session, "Server implements module \"%s\" in revision \"%s\" but revision \"%s\" is already implemented" |
| 720 | " and will be used instead.", name, revision, (*mod)->revision ? (*mod)->revision : "<none>"); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 721 | } |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 722 | |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 723 | if (*mod) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 724 | /* make the present module implemented and/or enable all its features */ |
| 725 | if (lys_set_implemented(*mod, features)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 726 | ERR(session, "Failed to implement module \"%s\".", (*mod)->name); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 727 | ret = -1; |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 728 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 729 | } else { |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 730 | /* missing implemented module, load it ... */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 731 | clb_data.has_get_schema = has_get_schema; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 732 | clb_data.modules = modules; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 733 | clb_data.session = session; |
| 734 | clb_data.user_clb = user_clb; |
| 735 | clb_data.user_data = user_data; |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 736 | |
| 737 | /* clear all the errors and just collect them for now */ |
| 738 | ly_err_clean(session->ctx, NULL); |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 739 | ly_log_options(LY_LOSTORE); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 740 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 741 | /* get module data */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 742 | retrieve_module_data(name, revision, &clb_data, &format, &module_data, &free_module_data); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 743 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 744 | if (module_data) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 745 | /* set import callback */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 746 | ly_ctx_set_module_imp_clb(session->ctx, retrieve_module_data_imp, &clb_data); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 747 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 748 | /* parse the module */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 749 | ly_in_new_memory(module_data, &in); |
| 750 | lys_parse(session->ctx, in, format, features, mod); |
| 751 | ly_in_free(in, 0); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 752 | if (*free_module_data) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 753 | (*free_module_data)((char *)module_data, user_data); |
Michal Vasko | dbf7c27 | 2018-02-19 09:07:59 +0100 | [diff] [blame] | 754 | } |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 755 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 756 | ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); |
| 757 | } |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 758 | |
Michal Vasko | dbf7c27 | 2018-02-19 09:07:59 +0100 | [diff] [blame] | 759 | /* restore logging options, then print errors on definite failure */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 760 | ly_log_options(LY_LOLOG | LY_LOSTORE_LAST); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 761 | if (!(*mod)) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 762 | for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 763 | ly_err_print(session->ctx, eitem); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 764 | } |
| 765 | ret = -1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 766 | } else { |
| 767 | /* print only warnings */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 768 | for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 769 | if (eitem->level == LY_LLWRN) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 770 | ly_err_print(session->ctx, eitem); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 771 | } |
| 772 | } |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Michal Vasko | dbf7c27 | 2018-02-19 09:07:59 +0100 | [diff] [blame] | 775 | /* clean the errors */ |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 776 | ly_err_clean(session->ctx, NULL); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | return ret; |
| 780 | } |
| 781 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 782 | static void |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 783 | free_module_info(struct module_info *list) |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 784 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 785 | uint32_t u, v; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 786 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 787 | if (!list) { |
| 788 | return; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 791 | for (u = 0; list[u].name; ++u) { |
| 792 | free(list[u].name); |
| 793 | free(list[u].revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 794 | if (list[u].features) { |
| 795 | for (v = 0; list[u].features[v]; ++v) { |
| 796 | free(list[u].features[v]); |
| 797 | } |
| 798 | free(list[u].features); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 799 | } |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 800 | if (list[u].submodules) { |
| 801 | for (v = 0; list[u].submodules[v].name; ++v) { |
| 802 | free(list[u].submodules[v].name); |
| 803 | free(list[u].submodules[v].revision); |
| 804 | } |
| 805 | free(list[u].submodules); |
| 806 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 807 | } |
| 808 | free(list); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 811 | /** |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 812 | * @brief Retrieve yang-library and schema-mounts operational data from the server. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 813 | * |
| 814 | * @param[in] session NC session. |
| 815 | * @param[in] has_get_data Whether get-data RPC is available or only get. |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 816 | * @param[in] filter Filter to use. |
| 817 | * @param[out] oper_data Received data. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 818 | * @return 0 on success. |
| 819 | * @return -1 on error. |
| 820 | */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 821 | static int |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 822 | get_oper_data(struct nc_session *session, int has_get_data, const char *filter, struct lyd_node **oper_data) |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 823 | { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 824 | struct nc_rpc *rpc = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 825 | struct lyd_node *op = NULL, *envp = NULL; |
| 826 | struct lyd_node_any *data; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 827 | NC_MSG_TYPE msg; |
| 828 | uint64_t msgid; |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 829 | int ret = 0; |
| 830 | const char *rpc_name; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 831 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 832 | /* get data from the server */ |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 833 | if (has_get_data) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 834 | rpc_name = "<get-data>"; |
| 835 | rpc = nc_rpc_getdata("ietf-datastores:operational", filter, "false", NULL, 0, 0, 0, 0, 0, NC_PARAMTYPE_CONST); |
Radek Krejci | 261737f | 2018-08-21 09:22:34 +0200 | [diff] [blame] | 836 | } else { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 837 | rpc_name = "<get>"; |
| 838 | rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST); |
Radek Krejci | 261737f | 2018-08-21 09:22:34 +0200 | [diff] [blame] | 839 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 840 | if (!rpc) { |
| 841 | goto cleanup; |
| 842 | } |
| 843 | |
| 844 | while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) { |
| 845 | usleep(1000); |
| 846 | } |
| 847 | if (msg == NC_MSG_ERROR) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 848 | WRN(session, "Failed to send %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 849 | goto cleanup; |
| 850 | } |
| 851 | |
| 852 | do { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 853 | lyd_free_tree(envp); |
| 854 | lyd_free_tree(op); |
| 855 | |
| 856 | msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op); |
Robin Jarry | 52ddb95 | 2019-10-15 16:23:01 +0200 | [diff] [blame] | 857 | } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 858 | if (msg == NC_MSG_WOULDBLOCK) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 859 | WRN(session, "Timeout for receiving reply to a %s RPC expired.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 860 | goto cleanup; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 861 | } else if (msg == NC_MSG_ERROR) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 862 | WRN(session, "Failed to receive a reply to %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 863 | goto cleanup; |
Jan Kundrát | 1d73a5a | 2022-03-30 16:54:49 +0200 | [diff] [blame] | 864 | } else if (!op || !lyd_child(op) || !lyd_child(op)->schema || strcmp(lyd_child(op)->schema->name, "data")) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 865 | WRN(session, "Unexpected reply without data to a %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 866 | goto cleanup; |
| 867 | } |
| 868 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 869 | data = (struct lyd_node_any *)lyd_child(op); |
| 870 | if (data->value_type != LYD_ANYDATA_DATATREE) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 871 | WRN(session, "Unexpected data in reply to a %s RPC.", rpc_name); |
Michal Vasko | 4052875 | 2021-06-21 15:41:51 +0200 | [diff] [blame] | 872 | goto cleanup; |
| 873 | } else if (!data->value.tree) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 874 | WRN(session, "No data in reply to a %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 875 | goto cleanup; |
| 876 | } |
| 877 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 878 | *oper_data = data->value.tree; |
| 879 | data->value.tree = NULL; |
| 880 | |
| 881 | cleanup: |
| 882 | nc_rpc_free(rpc); |
| 883 | lyd_free_tree(envp); |
| 884 | lyd_free_tree(op); |
| 885 | |
| 886 | if (session->status != NC_STATUS_RUNNING) { |
| 887 | /* something bad happened, discard the session */ |
| 888 | ERR(session, "Invalid session, discarding."); |
| 889 | ret = -1; |
| 890 | } |
| 891 | |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * @brief Build server module info from ietf-yang-library data. |
| 897 | * |
| 898 | * @param[in] session NC session. |
| 899 | * @param[in] get_data_sup Whether get-data RPC is available or only get. |
| 900 | * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter. |
| 901 | * @param[out] result Server modules. |
| 902 | * @return 0 on success. |
| 903 | * @return -1 on error. |
| 904 | */ |
| 905 | static int |
| 906 | build_module_info_yl(struct nc_session *session, int get_data_sup, int xpath_sup, struct module_info **result) |
| 907 | { |
| 908 | struct ly_set *modules = NULL; |
| 909 | uint32_t u, v, submodules_count, feature_count; |
| 910 | struct lyd_node *iter, *child, *oper_data = NULL; |
| 911 | struct lys_module *mod; |
| 912 | int ret = 0; |
| 913 | |
| 914 | /* get yang-library operational data */ |
| 915 | if (xpath_sup) { |
| 916 | if (get_oper_data(session, get_data_sup, "/ietf-yang-library:*", &oper_data)) { |
| 917 | goto cleanup; |
| 918 | } |
| 919 | } else { |
| 920 | if (get_oper_data(session, get_data_sup, |
| 921 | "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", &oper_data)) { |
| 922 | goto cleanup; |
| 923 | } |
| 924 | } |
| 925 | if (!oper_data) { |
| 926 | goto cleanup; |
| 927 | } |
| 928 | |
| 929 | if (lyd_find_xpath(oper_data, "/ietf-yang-library:modules-state/module", &modules)) { |
| 930 | WRN(NULL, "No yang-library module information found."); |
Radek Krejci | 2d08883 | 2018-08-21 13:40:14 +0200 | [diff] [blame] | 931 | goto cleanup; |
Radek Krejci | ac91952 | 2018-08-17 11:00:17 +0200 | [diff] [blame] | 932 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 933 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 934 | (*result) = calloc(modules->count + 1, sizeof **result); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 935 | NC_CHECK_ERRMEM_GOTO(!(*result), ret = -1, cleanup); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 936 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 937 | for (u = 0; u < modules->count; ++u) { |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 938 | submodules_count = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 939 | feature_count = 0; |
| 940 | mod = ((struct lyd_node *)modules->dnodes[u])->schema->module; |
| 941 | LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) { |
Michal Vasko | 5d9d597 | 2021-06-09 14:17:01 +0200 | [diff] [blame] | 942 | if (!iter->schema || (iter->schema->module != mod)) { |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 943 | /* ignore node from other schemas (augments) */ |
| 944 | continue; |
| 945 | } |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 946 | if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 947 | /* ignore empty nodes */ |
| 948 | continue; |
| 949 | } |
| 950 | if (!strcmp(iter->schema->name, "name")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 951 | (*result)[u].name = strdup(lyd_get_value(iter)); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 952 | } else if (!strcmp(iter->schema->name, "revision")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 953 | (*result)[u].revision = strdup(lyd_get_value(iter)); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 954 | } else if (!strcmp(iter->schema->name, "conformance-type")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 955 | (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement"); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 956 | } else if (!strcmp(iter->schema->name, "feature")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 957 | (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 958 | NC_CHECK_ERRMEM_GOTO(!(*result)[u].features, free_module_info(*result); *result = NULL; ret = -1, cleanup); |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 959 | (*result)[u].features[feature_count] = strdup(lyd_get_value(iter)); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 960 | (*result)[u].features[feature_count + 1] = NULL; |
| 961 | ++feature_count; |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 962 | } else if (!strcmp(iter->schema->name, "submodule")) { |
| 963 | submodules_count++; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if (submodules_count) { |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 968 | (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 969 | NC_CHECK_ERRMEM_GOTO(!(*result)[u].submodules, free_module_info(*result); *result = NULL; ret = -1, cleanup); |
| 970 | v = 0; |
| 971 | LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) { |
| 972 | mod = modules->dnodes[u]->schema->module; |
| 973 | if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) { |
| 974 | LY_LIST_FOR(lyd_child(iter), child) { |
| 975 | if (mod != child->schema->module) { |
| 976 | continue; |
| 977 | } else if (!strcmp(child->schema->name, "name")) { |
| 978 | (*result)[u].submodules[v].name = strdup(lyd_get_value(child)); |
| 979 | } else if (!strcmp(child->schema->name, "revision")) { |
| 980 | (*result)[u].submodules[v].revision = strdup(lyd_get_value(child)); |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 988 | cleanup: |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 989 | lyd_free_siblings(oper_data); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 990 | ly_set_free(modules, NULL); |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 991 | return ret; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 994 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 995 | * @brief Build server module info from received capabilities. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 996 | * |
| 997 | * @param[in] cpblts Server capabilities. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 998 | * @param[out] result Server modules. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 999 | * @return 0 on success. |
| 1000 | * @return -1 on error. |
| 1001 | */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1002 | static int |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1003 | build_module_info_cpblts(char **cpblts, struct module_info **result) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1004 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1005 | uint32_t u, v, feature_count; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1006 | char *module_cpblt, *ptr, *ptr2; |
| 1007 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1008 | for (u = 0; cpblts[u]; ++u) {} |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1009 | (*result) = calloc(u + 1, sizeof **result); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1010 | NC_CHECK_ERRMEM_RET(!(*result), -1); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1011 | |
| 1012 | for (u = v = 0; cpblts[u]; ++u) { |
| 1013 | module_cpblt = strstr(cpblts[u], "module="); |
| 1014 | /* this capability requires a module */ |
| 1015 | if (!module_cpblt) { |
| 1016 | continue; |
| 1017 | } |
| 1018 | |
| 1019 | /* get module's name */ |
| 1020 | ptr = (char *)module_cpblt + 7; |
| 1021 | ptr2 = strchr(ptr, '&'); |
| 1022 | if (!ptr2) { |
| 1023 | ptr2 = ptr + strlen(ptr); |
| 1024 | } |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1025 | (*result)[v].name = strndup(ptr, ptr2 - ptr); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* get module's revision */ |
| 1028 | ptr = strstr(module_cpblt, "revision="); |
| 1029 | if (ptr) { |
| 1030 | ptr += 9; |
| 1031 | ptr2 = strchr(ptr, '&'); |
| 1032 | if (!ptr2) { |
| 1033 | ptr2 = ptr + strlen(ptr); |
| 1034 | } |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1035 | (*result)[v].revision = strndup(ptr, ptr2 - ptr); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /* all are implemented since there is no better information in capabilities list */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1039 | (*result)[v].implemented = 1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1040 | |
| 1041 | /* get module's features */ |
| 1042 | ptr = strstr(module_cpblt, "features="); |
| 1043 | if (ptr) { |
| 1044 | ptr += 9; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1045 | feature_count = 0; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1046 | for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) { |
| 1047 | if (*ptr == ',') { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1048 | (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features); |
| 1049 | (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2); |
| 1050 | (*result)[v].features[feature_count + 1] = NULL; |
| 1051 | ++feature_count; |
| 1052 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1053 | ptr2 = ptr + 1; |
| 1054 | } |
| 1055 | } |
| 1056 | /* the last one */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1057 | (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features); |
| 1058 | (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2); |
| 1059 | (*result)[v].features[feature_count + 1] = NULL; |
| 1060 | ++feature_count; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1061 | } |
| 1062 | ++v; |
| 1063 | } |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1064 | |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1065 | return 0; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1068 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1069 | * @brief Fill client context based on server modules info. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1070 | * |
| 1071 | * @param[in] session NC session with the context to modify. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1072 | * @param[in] modules Server modules info. |
| 1073 | * @param[in] user_clb User callback for retrieving specific modules. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1074 | * @param[in] user_data User data for @p user_clb. |
| 1075 | * @param[in] has_get_schema Whether server supports get-schema RPC. |
| 1076 | * @return 0 on success. |
| 1077 | * @return -1 on error. |
| 1078 | */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1079 | static int |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1080 | nc_ctx_fill(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1081 | int has_get_schema) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1082 | { |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1083 | int ret = -1; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1084 | struct lys_module *mod; |
| 1085 | uint32_t u; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1086 | |
| 1087 | for (u = 0; modules[u].name; ++u) { |
Michal Vasko | 488c7f5 | 2018-09-19 11:19:44 +0200 | [diff] [blame] | 1088 | /* skip import-only modules */ |
| 1089 | if (!modules[u].implemented) { |
| 1090 | continue; |
| 1091 | } |
| 1092 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1093 | /* we can continue even if it fails */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1094 | nc_ctx_load_module(session, modules[u].name, modules[u].revision, (const char **)modules[u].features, modules, |
| 1095 | user_clb, user_data, has_get_schema, &mod); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1096 | |
| 1097 | if (!mod) { |
| 1098 | if (session->status != NC_STATUS_RUNNING) { |
| 1099 | /* something bad heppened, discard the session */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1100 | ERR(session, "Invalid session, discarding."); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1101 | goto cleanup; |
| 1102 | } |
| 1103 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1104 | /* all loading ways failed, the module will be ignored in the received data */ |
| 1105 | WRN(session, "Failed to load module \"%s@%s\".", modules[u].name, modules[u].revision ? |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1106 | modules[u].revision : "<latest>"); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1107 | session->flags |= NC_SESSION_CLIENT_NOT_STRICT; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1108 | } |
Michal Vasko | 60f6660 | 2017-10-17 13:52:18 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1111 | /* success */ |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1112 | ret = 0; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1113 | |
| 1114 | cleanup: |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1115 | return ret; |
| 1116 | } |
| 1117 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1118 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1119 | * @brief Fill client context with ietf-netconf module. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1120 | * |
| 1121 | * @param[in] session NC session with the context to modify. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1122 | * @param[in] modules Server module info. |
| 1123 | * @param[in] user_clb User callback for retrieving specific modules. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1124 | * @param[in] user_data User data for @p user_clb. |
| 1125 | * @param[in] has_get_schema Whether server supports get-schema RPC. |
| 1126 | * @return 0 on success. |
| 1127 | * @return -1 on error. |
| 1128 | */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1129 | static int |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1130 | nc_ctx_fill_ietf_netconf(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1131 | void *user_data, int has_get_schema) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1132 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1133 | uint32_t u; |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1134 | const char **features = NULL; |
| 1135 | struct ly_in *in; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1136 | struct lys_module *ietfnc; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1137 | |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1138 | /* find supported features (capabilities) in ietf-netconf */ |
| 1139 | for (u = 0; modules[u].name; ++u) { |
| 1140 | if (!strcmp(modules[u].name, "ietf-netconf")) { |
| 1141 | assert(modules[u].implemented); |
| 1142 | features = (const char **)modules[u].features; |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | if (!modules[u].name) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1147 | ERR(session, "Base NETCONF module not supported by the server."); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1148 | return -1; |
| 1149 | } |
| 1150 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1151 | ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf"); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1152 | if (ietfnc) { |
| 1153 | /* make sure to enable all the features if already loaded */ |
| 1154 | lys_set_implemented(ietfnc, features); |
| 1155 | } else { |
| 1156 | /* load the module */ |
| 1157 | nc_ctx_load_module(session, "ietf-netconf", NULL, features, modules, user_clb, user_data, has_get_schema, &ietfnc); |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 1158 | if (!ietfnc) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1159 | ly_in_new_memory(ietf_netconf_2013_09_29_yang, &in); |
| 1160 | lys_parse(session->ctx, in, LYS_IN_YANG, features, &ietfnc); |
| 1161 | ly_in_free(in, 0); |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 1162 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1163 | } |
| 1164 | if (!ietfnc) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1165 | ERR(session, "Loading base NETCONF module failed."); |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1166 | return -1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1172 | /** |
| 1173 | * @brief Set client session context to support schema-mount if possible. |
| 1174 | * |
| 1175 | * @param[in] session NC session with the context to modify. |
| 1176 | * @param[in] get_data_sup Whether get-data RPC is available or only get. |
| 1177 | * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter. |
| 1178 | * @return 0 on success. |
| 1179 | * @return -1 on error. |
| 1180 | */ |
| 1181 | static int |
| 1182 | nc_ctx_schema_mount(struct nc_session *session, int get_data_sup, int xpath_sup) |
| 1183 | { |
| 1184 | int rc = 0; |
| 1185 | struct lyd_node *oper_data = NULL; |
| 1186 | |
| 1187 | if (session->flags & NC_SESSION_SHAREDCTX) { |
| 1188 | /* context is already fully set up */ |
| 1189 | goto cleanup; |
| 1190 | } |
| 1191 | |
| 1192 | /* get yang-library and schema-mounts operational data */ |
| 1193 | if (xpath_sup) { |
| 1194 | if ((rc = get_oper_data(session, get_data_sup, "/ietf-yang-library:* | /ietf-yang-schema-mount:*", &oper_data))) { |
| 1195 | goto cleanup; |
| 1196 | } |
| 1197 | } else { |
| 1198 | if ((rc = get_oper_data(session, get_data_sup, |
| 1199 | "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>" |
| 1200 | "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\"/>", &oper_data))) { |
| 1201 | goto cleanup; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | if (!oper_data || lyd_find_path(oper_data, "/ietf-yang-schema-mount:schema-mounts", 0, NULL)) { |
| 1206 | /* no schema-mounts operational data */ |
| 1207 | goto cleanup; |
| 1208 | } |
| 1209 | |
| 1210 | /* validate the data for the parent reference prefixes to be resolved */ |
| 1211 | if (lyd_validate_all(&oper_data, NULL, LYD_VALIDATE_PRESENT, NULL)) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 1212 | ERR(session, "Invalid operational data received from the server (%s).", ly_err_last(LYD_CTX(oper_data))->msg); |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1213 | rc = -1; |
| 1214 | goto cleanup; |
| 1215 | } |
| 1216 | |
| 1217 | /* store the data in the session */ |
| 1218 | session->opts.client.ext_data = oper_data; |
| 1219 | oper_data = NULL; |
| 1220 | |
| 1221 | cleanup: |
| 1222 | lyd_free_siblings(oper_data); |
| 1223 | return rc; |
| 1224 | } |
| 1225 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1226 | int |
| 1227 | nc_ctx_check_and_fill(struct nc_session *session) |
| 1228 | { |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1229 | int i, get_schema_support = 0, yanglib_support = 0, xpath_support = 0, nmda_support = 0, ret = -1; |
Michal Vasko | cdeee43 | 2017-01-13 13:51:01 +0100 | [diff] [blame] | 1230 | ly_module_imp_clb old_clb = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1231 | void *old_data = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1232 | struct lys_module *mod = NULL; |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1233 | char *revision; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1234 | struct module_info *server_modules = NULL, *sm = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1235 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1236 | assert(session->opts.client.cpblts && session->ctx); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1237 | |
Michal Vasko | a272e09 | 2023-07-10 14:34:03 +0200 | [diff] [blame] | 1238 | if (client_opts.auto_context_fill_disabled) { |
| 1239 | VRB(session, "Context of the new session is left only with the default YANG modules."); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1243 | /* store the original user's callback, we will be switching between local search, get-schema and user callback */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1244 | old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1245 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1246 | /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1247 | * modules only to those present on the server side */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1248 | ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1249 | |
| 1250 | /* our callback is set later with appropriate data */ |
| 1251 | ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); |
| 1252 | |
| 1253 | /* check if get-schema and yang-library is supported */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1254 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1255 | if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) { |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1256 | get_schema_support = 1 + i; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1257 | } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) { |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1258 | yanglib_support = 1 + i; |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1259 | } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:xpath:1.0", 44)) { |
| 1260 | xpath_support = 1 + i; |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1261 | } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-nmda", 45)) { |
| 1262 | nmda_support = 1 + i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1263 | } |
| 1264 | } |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1265 | VRB(session, "Capability for <get-schema> support%s found.", get_schema_support ? "" : " not"); |
| 1266 | VRB(session, "Capability for yang-library support%s found.", yanglib_support ? "" : " not"); |
| 1267 | VRB(session, "Capability for XPath filter support%s found.", xpath_support ? "" : " not"); |
| 1268 | VRB(session, "Capability for NMDA RPCs support%s found.", nmda_support ? "" : " not"); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1269 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1270 | /* get information about server's modules from capabilities list until we will have yang-library */ |
| 1271 | if (build_module_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) { |
| 1272 | ERR(session, "Unable to get server module information from the <hello>'s capabilities."); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1273 | goto cleanup; |
| 1274 | } |
| 1275 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1276 | /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1277 | if (get_schema_support && lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_IN_YANG, NULL)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1278 | WRN(session, "Loading NETCONF monitoring module failed, cannot use <get-schema>."); |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 1279 | get_schema_support = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1283 | if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1284 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1287 | /* get correct version of ietf-yang-library into context */ |
| 1288 | if (yanglib_support) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1289 | /* use get-schema to get server's ietf-yang-library */ |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1290 | revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision="); |
| 1291 | if (!revision) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1292 | WRN(session, "Loading NETCONF ietf-yang-library module failed, missing revision in NETCONF <hello> message."); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1293 | WRN(session, "Unable to automatically use <get-schema>."); |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1294 | yanglib_support = 0; |
| 1295 | } else { |
| 1296 | revision = strndup(&revision[9], 10); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1297 | if (nc_ctx_load_module(session, "ietf-yang-library", revision, NULL, server_modules, old_clb, old_data, |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1298 | get_schema_support, &mod)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1299 | WRN(session, "Loading NETCONF ietf-yang-library module failed, unable to use it to learn all " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1300 | "the supported modules."); |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1301 | yanglib_support = 0; |
| 1302 | } |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1303 | if (strcmp(revision, "2019-01-04") >= 0) { |
| 1304 | /* we also need ietf-datastores to be implemented */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1305 | if (nc_ctx_load_module(session, "ietf-datastores", NULL, NULL, server_modules, old_clb, old_data, |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1306 | get_schema_support, &mod)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1307 | WRN(session, "Loading NETCONF ietf-datastores module failed, unable to use yang-library " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1308 | "to learn all the supported modules."); |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1309 | yanglib_support = 0; |
| 1310 | } |
| 1311 | } |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1312 | free(revision); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1316 | /* ietf-netconf-nmda is needed to issue get-data */ |
| 1317 | if (nmda_support && nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, NULL, server_modules, old_clb, old_data, |
| 1318 | get_schema_support, &mod)) { |
| 1319 | WRN(session, "Loading NMDA module failed, unable to use <get-data>."); |
| 1320 | nmda_support = 0; |
| 1321 | } |
| 1322 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1323 | /* prepare structured information about server's modules */ |
Radek Krejci | 9aa1e35 | 2018-05-16 16:05:42 +0200 | [diff] [blame] | 1324 | if (yanglib_support) { |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1325 | if (build_module_info_yl(session, nmda_support, xpath_support, &sm)) { |
Radek Krejci | f063379 | 2018-08-20 15:12:09 +0200 | [diff] [blame] | 1326 | goto cleanup; |
| 1327 | } else if (!sm) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1328 | VRB(session, "Trying to use capabilities instead of ietf-yang-library data."); |
Radek Krejci | f063379 | 2018-08-20 15:12:09 +0200 | [diff] [blame] | 1329 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1330 | /* prefer yang-library information, currently we have it from capabilities used for getting correct |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1331 | * yang-library module */ |
| 1332 | free_module_info(server_modules); |
Radek Krejci | f063379 | 2018-08-20 15:12:09 +0200 | [diff] [blame] | 1333 | server_modules = sm; |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1334 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1335 | } |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1336 | |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1337 | /* compile all modules at once to avoid invalid errors or warnings */ |
| 1338 | ly_ctx_set_options(session->ctx, LY_CTX_EXPLICIT_COMPILE); |
| 1339 | |
| 1340 | /* fill the context */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1341 | if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) { |
| 1342 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1343 | } |
| 1344 | |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1345 | /* compile it */ |
| 1346 | if (ly_ctx_compile(session->ctx)) { |
| 1347 | goto cleanup; |
| 1348 | } |
| 1349 | |
Michal Vasko | 59762c3 | 2024-04-12 12:13:12 +0200 | [diff] [blame] | 1350 | /* set support for schema-mount, if possible (requires ietf-yang-library support) */ |
| 1351 | if (yanglib_support && nc_ctx_schema_mount(session, nmda_support, xpath_support)) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1352 | goto cleanup; |
| 1353 | } |
| 1354 | |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1355 | /* success */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1356 | ret = 0; |
| 1357 | |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 1358 | if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1359 | WRN(session, "Some modules failed to be loaded, any data from these modules (and any other unknown) will " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1360 | "be ignored."); |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1361 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1362 | |
| 1363 | cleanup: |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1364 | free_module_info(server_modules); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1365 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1366 | /* set user callback back */ |
| 1367 | ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1368 | ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS); |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1369 | ly_ctx_unset_options(session->ctx, LY_CTX_EXPLICIT_COMPILE); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1370 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1371 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | API struct nc_session * |
| 1375 | nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx) |
| 1376 | { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1377 | struct nc_session *session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1378 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1379 | if (fdin < 0) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1380 | ERRARG(NULL, "fdin"); |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1381 | return NULL; |
| 1382 | } else if (fdout < 0) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1383 | ERRARG(NULL, "fdout"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1384 | return NULL; |
| 1385 | } |
| 1386 | |
| 1387 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1388 | session = nc_new_session(NC_CLIENT, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1389 | NC_CHECK_ERRMEM_RET(!session, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1390 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1391 | |
| 1392 | /* transport specific data */ |
| 1393 | session->ti_type = NC_TI_FD; |
| 1394 | session->ti.fd.in = fdin; |
| 1395 | session->ti.fd.out = fdout; |
| 1396 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1397 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1398 | goto fail; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1399 | } |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1400 | ctx = session->ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1401 | |
| 1402 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1403 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1404 | goto fail; |
| 1405 | } |
| 1406 | session->status = NC_STATUS_RUNNING; |
| 1407 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1408 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1409 | goto fail; |
| 1410 | } |
| 1411 | |
| 1412 | return session; |
| 1413 | |
| 1414 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1415 | nc_session_free(session, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1416 | return NULL; |
| 1417 | } |
| 1418 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1419 | API struct nc_session * |
| 1420 | nc_connect_unix(const char *address, struct ly_ctx *ctx) |
| 1421 | { |
| 1422 | struct nc_session *session = NULL; |
| 1423 | struct sockaddr_un sun; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1424 | struct passwd *pw, pw_buf; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1425 | char *username; |
| 1426 | int sock = -1; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1427 | char *buf = NULL; |
| 1428 | size_t buf_size = 0; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1429 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1430 | NC_CHECK_ARG_RET(NULL, address, NULL); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1431 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1432 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1433 | if (sock < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1434 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1435 | goto fail; |
| 1436 | } |
| 1437 | |
| 1438 | memset(&sun, 0, sizeof(sun)); |
| 1439 | sun.sun_family = AF_UNIX; |
| 1440 | snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address); |
| 1441 | |
| 1442 | if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1443 | ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1444 | goto fail; |
| 1445 | } |
| 1446 | |
| 1447 | if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1448 | ERR(NULL, "fcntl failed (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1449 | goto fail; |
| 1450 | } |
| 1451 | |
| 1452 | /* prepare session structure */ |
| 1453 | session = nc_new_session(NC_CLIENT, 0); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1454 | NC_CHECK_ERRMEM_GOTO(!session, , fail); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1455 | session->status = NC_STATUS_STARTING; |
| 1456 | |
| 1457 | /* transport specific data */ |
| 1458 | session->ti_type = NC_TI_UNIX; |
| 1459 | session->ti.unixsock.sock = sock; |
| 1460 | sock = -1; /* do not close sock in fail label anymore */ |
| 1461 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1462 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1463 | goto fail; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1464 | } |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1465 | ctx = session->ctx; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1466 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1467 | session->path = strdup(address); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1468 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1469 | pw = nc_getpw(geteuid(), NULL, &pw_buf, &buf, &buf_size); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1470 | if (!pw) { |
| 1471 | ERR(NULL, "Failed to find username for UID %u.", (unsigned int)geteuid()); |
| 1472 | goto fail; |
| 1473 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1474 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1475 | free(buf); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1476 | NC_CHECK_ERRMEM_GOTO(!username, , fail); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1477 | session->username = username; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1478 | |
| 1479 | /* NETCONF handshake */ |
| 1480 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
| 1481 | goto fail; |
| 1482 | } |
| 1483 | session->status = NC_STATUS_RUNNING; |
| 1484 | |
| 1485 | if (nc_ctx_check_and_fill(session) == -1) { |
| 1486 | goto fail; |
| 1487 | } |
| 1488 | |
| 1489 | return session; |
| 1490 | |
| 1491 | fail: |
| 1492 | nc_session_free(session, NULL); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1493 | if (sock >= 0) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1494 | close(sock); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1495 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1496 | return NULL; |
| 1497 | } |
| 1498 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1499 | /** |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1500 | * @brief Convert socket IP address to string. |
| 1501 | * |
| 1502 | * @param[in] saddr Sockaddr to convert. |
| 1503 | * @param[out] str_ip String IP address. |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1504 | * @param[out] port Optional port. |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1505 | * @return 0 on success. |
| 1506 | * @return -1 on error. |
| 1507 | */ |
| 1508 | static int |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1509 | nc_saddr2str(const struct sockaddr *saddr, char **str_ip, uint16_t *port) |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1510 | { |
| 1511 | void *addr; |
| 1512 | socklen_t str_len; |
| 1513 | |
| 1514 | assert((saddr->sa_family == AF_INET) || (saddr->sa_family == AF_INET6)); |
| 1515 | |
| 1516 | str_len = (saddr->sa_family == AF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN; |
| 1517 | *str_ip = malloc(str_len); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1518 | NC_CHECK_ERRMEM_RET(!(*str_ip), -1); |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1519 | |
| 1520 | if (saddr->sa_family == AF_INET) { |
Michal Vasko | c0f85ca | 2023-03-27 10:19:00 +0200 | [diff] [blame] | 1521 | addr = &((struct sockaddr_in *)saddr)->sin_addr; |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1522 | if (port) { |
| 1523 | *port = ntohs(((struct sockaddr_in *)saddr)->sin_port); |
| 1524 | } |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1525 | } else { |
Michal Vasko | c0f85ca | 2023-03-27 10:19:00 +0200 | [diff] [blame] | 1526 | addr = &((struct sockaddr_in6 *)saddr)->sin6_addr; |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1527 | if (port) { |
| 1528 | *port = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); |
| 1529 | } |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1530 | } |
| 1531 | if (!inet_ntop(saddr->sa_family, addr, *str_ip, str_len)) { |
| 1532 | ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno)); |
| 1533 | free(*str_ip); |
| 1534 | return -1; |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | /** |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1541 | * @brief Try to connect a socket, optionally a pending one from a previous attempt. |
| 1542 | * |
| 1543 | * @param[in] timeout_ms Timeout in ms to wait for the connection to be fully established, -1 to block. |
| 1544 | * @param[in,out] sock_pending Optional previously created socked that was not fully connected yet. If provided and |
| 1545 | * connected, is set to -1. |
| 1546 | * @param[in] res Addrinfo resource to use when creating a new socket. |
| 1547 | * @param[in] ka Keepalives to set. |
| 1548 | * @return Connected socket or -1 on error. |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1549 | */ |
| 1550 | static int |
Michal Vasko | c455038 | 2024-05-03 12:02:38 +0200 | [diff] [blame] | 1551 | sock_connect(int timeout_ms, int *sock_pending, struct addrinfo *res, const struct nc_keepalives *ka) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1552 | { |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1553 | int flags, ret, error; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1554 | int sock = -1; |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1555 | struct pollfd fds = {0}; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1556 | socklen_t len = sizeof(int); |
Michal Vasko | 7e7f99d | 2019-09-12 13:49:46 +0200 | [diff] [blame] | 1557 | uint16_t port; |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1558 | char *str; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1559 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1560 | if (sock_pending && (*sock_pending != -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1561 | VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1562 | sock = *sock_pending; |
| 1563 | } else { |
| 1564 | assert(res); |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1565 | if (nc_saddr2str(res->ai_addr, &str, &port)) { |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1566 | return -1; |
| 1567 | } |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1568 | VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port); |
| 1569 | free(str); |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1570 | |
| 1571 | /* connect to a server */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1572 | sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 1573 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1574 | ERR(NULL, "Socket could not be created (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1575 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1576 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1577 | /* make the socket non-blocking */ |
| 1578 | if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1579 | ERR(NULL, "fcntl() failed (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1580 | goto cleanup; |
Michal Vasko | 7d965dc | 2018-03-12 14:39:23 +0100 | [diff] [blame] | 1581 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1582 | /* non-blocking connect! */ |
| 1583 | if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { |
| 1584 | if (errno != EINPROGRESS) { |
| 1585 | /* network connection failed, try another resource */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1586 | ERR(NULL, "connect() failed (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1587 | goto cleanup; |
| 1588 | } |
| 1589 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1590 | } |
Michal Vasko | 7d965dc | 2018-03-12 14:39:23 +0100 | [diff] [blame] | 1591 | |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1592 | fds.fd = sock; |
| 1593 | fds.events = POLLOUT; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1594 | |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1595 | /* wait until we can write data to the socket */ |
| 1596 | ret = poll(&fds, 1, timeout_ms); |
| 1597 | if (ret == -1) { |
| 1598 | /* error */ |
| 1599 | ERR(NULL, "poll() failed (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1600 | goto cleanup; |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1601 | } else if (ret == 0) { |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1602 | /* there was a timeout */ |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1603 | VRB(NULL, "Timed out after %d ms (%s).", timeout_ms, strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1604 | if (sock_pending) { |
| 1605 | /* no sock-close, we'll try it again */ |
| 1606 | *sock_pending = sock; |
| 1607 | } else { |
| 1608 | close(sock); |
| 1609 | } |
| 1610 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1611 | } |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1612 | |
| 1613 | /* check the usability of the socket */ |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1614 | error = 0; |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1615 | if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1616 | ERR(NULL, "getsockopt() failed (%s).", strerror(errno)); |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1617 | goto cleanup; |
| 1618 | } |
Michal Vasko | e27ab4e | 2020-07-27 11:10:58 +0200 | [diff] [blame] | 1619 | if (error) { |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1620 | /* network connection failed, try another resource */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1621 | VRB(NULL, "getsockopt() error (%s).", strerror(error)); |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1622 | errno = error; |
| 1623 | goto cleanup; |
| 1624 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 1625 | |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 1626 | /* configure keepalives */ |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame^] | 1627 | if (nc_sock_configure_ka(sock, ka)) { |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 1628 | goto cleanup; |
| 1629 | } |
| 1630 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1631 | /* connected */ |
| 1632 | if (sock_pending) { |
| 1633 | *sock_pending = -1; |
| 1634 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1635 | return sock; |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 1636 | |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1637 | cleanup: |
| 1638 | if (sock_pending) { |
| 1639 | *sock_pending = -1; |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 1640 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1641 | close(sock); |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 1642 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1645 | int |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1646 | nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepalives *ka, int *sock_pending, char **ip_host) |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1647 | { |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1648 | int i, opt; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1649 | int sock = sock_pending ? *sock_pending : -1; |
Michal Vasko | 0be8569 | 2021-03-02 08:04:57 +0100 | [diff] [blame] | 1650 | struct addrinfo hints, *res_list = NULL, *res; |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1651 | char port_s[6]; /* length of string representation of short int */ |
| 1652 | struct sockaddr_storage saddr; |
| 1653 | socklen_t addr_len = sizeof saddr; |
| 1654 | |
| 1655 | *ip_host = NULL; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1656 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1657 | DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout_ms, sock); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1658 | |
| 1659 | /* no pending socket */ |
| 1660 | if (sock == -1) { |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1661 | /* connect to a server */ |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1662 | snprintf(port_s, 6, "%u", port); |
| 1663 | memset(&hints, 0, sizeof hints); |
| 1664 | hints.ai_family = AF_UNSPEC; |
| 1665 | hints.ai_socktype = SOCK_STREAM; |
| 1666 | hints.ai_protocol = IPPROTO_TCP; |
| 1667 | i = getaddrinfo(host, port_s, &hints, &res_list); |
| 1668 | if (i != 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1669 | ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i)); |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1670 | goto error; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | for (res = res_list; res != NULL; res = res->ai_next) { |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1674 | sock = sock_connect(timeout_ms, sock_pending, res, ka); |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1675 | if (sock == -1) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1676 | if (!sock_pending || (*sock_pending == -1)) { |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1677 | /* try the next resource */ |
| 1678 | continue; |
| 1679 | } else { |
| 1680 | /* timeout, keep pending socket */ |
Michal Vasko | 0be8569 | 2021-03-02 08:04:57 +0100 | [diff] [blame] | 1681 | break; |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1682 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1683 | } |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1684 | VRB(NULL, "Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4"); |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1685 | |
| 1686 | opt = 1; |
| 1687 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1688 | ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1689 | goto error; |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1692 | if (nc_saddr2str(res->ai_addr, ip_host, NULL)) { |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1693 | goto error; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1694 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1695 | break; |
| 1696 | } |
| 1697 | freeaddrinfo(res_list); |
| 1698 | |
| 1699 | } else { |
| 1700 | /* try to get a connection with the pending socket */ |
| 1701 | assert(sock_pending); |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1702 | sock = sock_connect(timeout_ms, sock_pending, NULL, ka); |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1703 | |
| 1704 | if (sock > 0) { |
Barbaros Tokaoglu | 96da591 | 2023-06-19 18:57:05 +0300 | [diff] [blame] | 1705 | if (getpeername(sock, (struct sockaddr *)&saddr, &addr_len)) { |
| 1706 | ERR(NULL, "getpeername failed (%s).", strerror(errno)); |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1707 | goto error; |
| 1708 | } |
| 1709 | |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1710 | if (nc_saddr2str((struct sockaddr *)&saddr, ip_host, NULL)) { |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1711 | goto error; |
| 1712 | } |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1713 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | return sock; |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1717 | |
| 1718 | error: |
Michal Vasko | 0be8569 | 2021-03-02 08:04:57 +0100 | [diff] [blame] | 1719 | if (res_list) { |
| 1720 | freeaddrinfo(res_list); |
| 1721 | } |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1722 | if (sock != -1) { |
| 1723 | close(sock); |
| 1724 | } |
| 1725 | if (sock_pending) { |
| 1726 | *sock_pending = -1; |
| 1727 | } |
| 1728 | return -1; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1731 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1732 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1733 | int |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1734 | nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hostname, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1735 | { |
| 1736 | int sock; |
| 1737 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1738 | NC_CHECK_ARG_RET(NULL, address, port, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1739 | |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame^] | 1740 | sock = nc_sock_listen_inet(address, port); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1741 | if (sock == -1) { |
| 1742 | return -1; |
| 1743 | } |
| 1744 | |
| 1745 | ++client_opts.ch_bind_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1746 | client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds); |
| 1747 | if (!client_opts.ch_binds) { |
| 1748 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 1749 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1750 | return -1; |
| 1751 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1752 | |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1753 | client_opts.ch_binds_aux = nc_realloc(client_opts.ch_binds_aux, client_opts.ch_bind_count * sizeof *client_opts.ch_binds_aux); |
| 1754 | if (!client_opts.ch_binds_aux) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1755 | ERRMEM; |
| 1756 | close(sock); |
| 1757 | return -1; |
| 1758 | } |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1759 | client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].ti = ti; |
| 1760 | client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].hostname = hostname ? strdup(hostname) : NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1761 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1762 | client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address); |
| 1763 | client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port; |
| 1764 | client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 1765 | client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1766 | |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
| 1770 | int |
| 1771 | nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 1772 | { |
| 1773 | uint32_t i; |
| 1774 | int ret = -1; |
| 1775 | |
| 1776 | if (!address && !port && !ti) { |
| 1777 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 1778 | close(client_opts.ch_binds[i].sock); |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1779 | free(client_opts.ch_binds[i].address); |
| 1780 | |
| 1781 | free(client_opts.ch_binds_aux[i].hostname); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1782 | |
| 1783 | ret = 0; |
| 1784 | } |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1785 | client_opts.ch_bind_count = 0; |
| 1786 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1787 | free(client_opts.ch_binds); |
| 1788 | client_opts.ch_binds = NULL; |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1789 | |
| 1790 | free(client_opts.ch_binds_aux); |
| 1791 | client_opts.ch_binds_aux = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1792 | } else { |
| 1793 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1794 | if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) && |
| 1795 | (!port || (client_opts.ch_binds[i].port == port)) && |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1796 | (!ti || (client_opts.ch_binds_aux[i].ti == ti))) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1797 | close(client_opts.ch_binds[i].sock); |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1798 | free(client_opts.ch_binds[i].address); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1799 | |
| 1800 | --client_opts.ch_bind_count; |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1801 | if (!client_opts.ch_bind_count) { |
| 1802 | free(client_opts.ch_binds); |
| 1803 | client_opts.ch_binds = NULL; |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1804 | |
| 1805 | free(client_opts.ch_binds_aux); |
| 1806 | client_opts.ch_binds_aux = NULL; |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1807 | } else if (i < client_opts.ch_bind_count) { |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1808 | memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], |
| 1809 | sizeof *client_opts.ch_binds); |
| 1810 | |
| 1811 | memcpy(&client_opts.ch_binds_aux[i], &client_opts.ch_binds_aux[client_opts.ch_bind_count], |
| 1812 | sizeof *client_opts.ch_binds_aux); |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1813 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1814 | |
| 1815 | ret = 0; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
| 1823 | API int |
| 1824 | nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session) |
| 1825 | { |
| 1826 | int sock; |
| 1827 | char *host = NULL; |
| 1828 | uint16_t port, idx; |
| 1829 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1830 | NC_CHECK_ARG_RET(NULL, session, -1); |
| 1831 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1832 | if (!client_opts.ch_binds) { |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 1833 | ERR(NULL, "Call-Home binds not set."); |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1834 | return -1; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
Michal Vasko | 5a2c5b9 | 2024-02-02 09:11:41 +0100 | [diff] [blame] | 1837 | sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout, |
| 1838 | &host, &port, &idx); |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 1839 | if (sock < 1) { |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1840 | free(host); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1841 | return sock; |
| 1842 | } |
| 1843 | |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame^] | 1844 | /* configure keepalives */ |
| 1845 | if (nc_sock_configure_ka(sock, &client_opts.ka)) { |
| 1846 | free(host); |
| 1847 | close(sock); |
| 1848 | return -1; |
| 1849 | } |
| 1850 | |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1851 | if (client_opts.ch_binds_aux[idx].ti == NC_TI_LIBSSH) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1852 | *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1853 | } else if (client_opts.ch_binds_aux[idx].ti == NC_TI_OPENSSL) { |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1854 | *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT, |
| 1855 | client_opts.ch_binds_aux[idx].hostname); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1856 | } else { |
Michal Vasko | fee717c | 2016-02-01 13:25:43 +0100 | [diff] [blame] | 1857 | close(sock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1858 | *session = NULL; |
| 1859 | } |
| 1860 | |
| 1861 | free(host); |
| 1862 | |
| 1863 | if (!(*session)) { |
| 1864 | return -1; |
| 1865 | } |
| 1866 | |
| 1867 | return 1; |
| 1868 | } |
| 1869 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1870 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1871 | |
Michal Vasko | 1b2ddc9 | 2017-05-24 08:59:49 +0200 | [diff] [blame] | 1872 | API const char * const * |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1873 | nc_session_get_cpblts(const struct nc_session *session) |
| 1874 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1875 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1876 | |
Michal Vasko | 1b2ddc9 | 2017-05-24 08:59:49 +0200 | [diff] [blame] | 1877 | return (const char * const *)session->opts.client.cpblts; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | API const char * |
| 1881 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 1882 | { |
| 1883 | int i, len; |
| 1884 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1885 | NC_CHECK_ARG_RET(session, session, capab, NULL); |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1886 | |
| 1887 | len = strlen(capab); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1888 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
| 1889 | if (!strncmp(session->opts.client.cpblts[i], capab, len)) { |
| 1890 | return session->opts.client.cpblts[i]; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | return NULL; |
| 1895 | } |
| 1896 | |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1897 | API int |
| 1898 | nc_session_ntf_thread_running(const struct nc_session *session) |
| 1899 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1900 | NC_CHECK_ARG_RET(session, session, 0); |
| 1901 | |
| 1902 | if (session->side != NC_CLIENT) { |
| 1903 | ERRARG(NULL, "session"); |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1904 | return 0; |
| 1905 | } |
| 1906 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 1907 | return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running); |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1908 | } |
| 1909 | |
roman | 9075eab | 2023-09-14 10:07:26 +0200 | [diff] [blame] | 1910 | API int |
| 1911 | nc_client_init(void) |
| 1912 | { |
| 1913 | int r; |
| 1914 | |
| 1915 | if ((r = pthread_mutex_init(&client_opts.ch_bind_lock, NULL))) { |
| 1916 | ERR(NULL, "%s: failed to init bind lock(%s).", __func__, strerror(r)); |
| 1917 | return -1; |
| 1918 | } |
| 1919 | |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1923 | API void |
| 1924 | nc_client_destroy(void) |
| 1925 | { |
roman | 9075eab | 2023-09-14 10:07:26 +0200 | [diff] [blame] | 1926 | pthread_mutex_destroy(&client_opts.ch_bind_lock); |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 1927 | nc_client_set_schema_searchpath(NULL); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1928 | #ifdef NC_ENABLED_SSH_TLS |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 1929 | nc_client_ch_del_bind(NULL, 0, 0); |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 1930 | nc_client_ssh_destroy_opts(); |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 1931 | nc_client_tls_destroy_opts(); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1932 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1935 | static NC_MSG_TYPE |
| 1936 | recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1937 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1938 | char *ptr; |
| 1939 | struct lyd_attr *attr; |
| 1940 | uint64_t cur_msgid; |
| 1941 | |
| 1942 | assert(envp && !envp->schema); |
| 1943 | |
| 1944 | /* find the message-id attribute */ |
| 1945 | LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) { |
| 1946 | if (!strcmp(attr->name.name, "message-id")) { |
| 1947 | break; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | if (!attr) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1952 | ERR(session, "Received a <rpc-reply> without a message-id."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1953 | return NC_MSG_REPLY_ERR_MSGID; |
| 1954 | } |
| 1955 | |
| 1956 | cur_msgid = strtoul(attr->value, &ptr, 10); |
| 1957 | if (cur_msgid != msgid) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1958 | ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").", |
| 1959 | cur_msgid, msgid); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1960 | return NC_MSG_REPLY_ERR_MSGID; |
| 1961 | } |
| 1962 | |
| 1963 | return NC_MSG_REPLY; |
| 1964 | } |
| 1965 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 1966 | /** |
| 1967 | * @brief Used to roughly estimate the type of the message, does not actually parse or verify it. |
| 1968 | * |
| 1969 | * @param[in] session NETCONF session used to send error messages. |
| 1970 | * @param[in] msg Message to check for type. |
| 1971 | * @return NC_MSG_REPLY If format roughly matches a rpc-reply; |
| 1972 | * @return NC_MSG_NOTIF If format roughly matches a notification; |
| 1973 | * @return NC_MSG_ERROR If format is malformed or unrecognized. |
| 1974 | */ |
| 1975 | static NC_MSG_TYPE |
| 1976 | get_msg_type(struct nc_session *session, struct ly_in *msg) |
| 1977 | { |
| 1978 | const char *str, *end; |
| 1979 | |
| 1980 | str = ly_in_memory(msg, NULL); |
| 1981 | |
| 1982 | while (*str) { |
| 1983 | /* Skip whitespaces */ |
| 1984 | while (isspace(*str)) { |
| 1985 | str++; |
| 1986 | } |
| 1987 | |
| 1988 | if (*str == '<') { |
| 1989 | str++; |
| 1990 | if (!strncmp(str, "!--", 3)) { |
| 1991 | /* Skip comments */ |
| 1992 | end = "-->"; |
| 1993 | str = strstr(str, end); |
| 1994 | } else if (!strncmp(str, "?xml", 4)) { |
| 1995 | /* Skip xml declaration */ |
| 1996 | end = "?>"; |
| 1997 | str = strstr(str, end); |
| 1998 | } else if (!strncmp(str, "rpc-reply", 9)) { |
| 1999 | return NC_MSG_REPLY; |
| 2000 | } else if (!strncmp(str, "notification", 12)) { |
| 2001 | return NC_MSG_NOTIF; |
| 2002 | } else { |
| 2003 | ERR(session, "Unknown xml element '%.10s'.", str); |
| 2004 | return NC_MSG_ERROR; |
| 2005 | } |
| 2006 | if (!str) { |
| 2007 | /* No matching ending tag found */ |
| 2008 | ERR(session, "No matching ending tag '%s' found in xml message.", end); |
| 2009 | return NC_MSG_ERROR; |
| 2010 | } |
| 2011 | str += strlen(end); |
| 2012 | } else { |
| 2013 | /* Not a valid xml */ |
| 2014 | ERR(session, "Unexpected character '%c' in xml message.", *str); |
| 2015 | return NC_MSG_ERROR; |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | /* Unexpected end of message */ |
| 2020 | ERR(session, "Unexpected end of xml message."); |
| 2021 | return NC_MSG_ERROR; |
| 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * @brief Function to receive either replies or notifications. |
| 2026 | * |
| 2027 | * @param[in] session NETCONF session from which this function receives messages. |
| 2028 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite. |
| 2029 | * @param[in] expected Type of the message the caller desired. |
| 2030 | * @param[out] message If receiving a message succeeded this is the message, NULL otherwise. |
| 2031 | * @return NC_MSG_REPLY If a rpc-reply was received; |
| 2032 | * @return NC_MSG_NOTIF If a notification was received; |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 2033 | * @return NC_MSG_ERROR If any error occurred; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2034 | * @return NC_MSG_WOULDBLOCK If the timeout was reached. |
| 2035 | */ |
| 2036 | static NC_MSG_TYPE |
| 2037 | recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message) |
| 2038 | { |
| 2039 | struct nc_msg_cont **cont_ptr; |
| 2040 | struct ly_in *msg = NULL; |
| 2041 | struct nc_msg_cont *cont, *prev; |
| 2042 | NC_MSG_TYPE ret = NC_MSG_ERROR; |
| 2043 | int r; |
| 2044 | |
| 2045 | *message = NULL; |
| 2046 | |
| 2047 | /* MSGS LOCK */ |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2048 | r = nc_session_client_msgs_lock(session, &timeout, __func__); |
| 2049 | if (!r) { |
| 2050 | ret = NC_MSG_WOULDBLOCK; |
| 2051 | goto cleanup; |
| 2052 | } else if (r == -1) { |
| 2053 | ret = NC_MSG_ERROR; |
| 2054 | goto cleanup; |
| 2055 | } |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2056 | |
| 2057 | /* Find the expected message in the buffer */ |
| 2058 | prev = NULL; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2059 | for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) { |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2060 | prev = cont; |
| 2061 | } |
| 2062 | |
| 2063 | if (cont) { |
| 2064 | /* Remove found message from buffer */ |
| 2065 | if (prev) { |
| 2066 | prev->next = cont->next; |
| 2067 | } else { |
| 2068 | session->opts.client.msgs = cont->next; |
| 2069 | } |
| 2070 | |
| 2071 | /* Use the buffer message */ |
| 2072 | ret = cont->type; |
| 2073 | msg = cont->msg; |
| 2074 | free(cont); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2075 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /* Read a message from the wire */ |
| 2079 | r = nc_read_msg_poll_io(session, timeout, &msg); |
| 2080 | if (!r) { |
| 2081 | ret = NC_MSG_WOULDBLOCK; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2082 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2083 | } else if (r == -1) { |
| 2084 | ret = NC_MSG_ERROR; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2085 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* Basic check to determine message type */ |
| 2089 | ret = get_msg_type(session, msg); |
| 2090 | if (ret == NC_MSG_ERROR) { |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2091 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* If received a message of different type store it in the buffer */ |
| 2095 | if (ret != expected) { |
| 2096 | cont_ptr = &session->opts.client.msgs; |
| 2097 | while (*cont_ptr) { |
| 2098 | cont_ptr = &((*cont_ptr)->next); |
| 2099 | } |
| 2100 | *cont_ptr = malloc(sizeof **cont_ptr); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2101 | NC_CHECK_ERRMEM_GOTO(!*cont_ptr, ret = NC_MSG_ERROR, cleanup_unlock); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2102 | (*cont_ptr)->msg = msg; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2103 | msg = NULL; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2104 | (*cont_ptr)->type = ret; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2105 | (*cont_ptr)->next = NULL; |
| 2106 | } |
| 2107 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2108 | cleanup_unlock: |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2109 | /* MSGS UNLOCK */ |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2110 | nc_session_client_msgs_unlock(session, __func__); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2111 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2112 | cleanup: |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2113 | if (ret == expected) { |
| 2114 | *message = msg; |
| 2115 | } else { |
| 2116 | ly_in_free(msg, 1); |
| 2117 | } |
| 2118 | return ret; |
| 2119 | } |
| 2120 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2121 | static NC_MSG_TYPE |
| 2122 | recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp) |
| 2123 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2124 | LY_ERR lyrc; |
| 2125 | struct ly_in *msg = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2126 | NC_MSG_TYPE ret = NC_MSG_ERROR; |
Michal Vasko | b25c56f | 2024-03-27 15:45:56 +0100 | [diff] [blame] | 2127 | uint32_t temp_lo = LY_LOSTORE, *prev_lo; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2128 | |
| 2129 | assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION))); |
| 2130 | |
| 2131 | *envp = NULL; |
| 2132 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2133 | /* Receive messages until a rpc-reply is found or a timeout or error reached */ |
| 2134 | ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg); |
| 2135 | if (ret != NC_MSG_REPLY) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2136 | goto cleanup; |
| 2137 | } |
| 2138 | |
| 2139 | /* parse */ |
Michal Vasko | b25c56f | 2024-03-27 15:45:56 +0100 | [diff] [blame] | 2140 | prev_lo = ly_temp_log_options(&temp_lo); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2141 | lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL); |
Michal Vasko | b25c56f | 2024-03-27 15:45:56 +0100 | [diff] [blame] | 2142 | ly_temp_log_options(prev_lo); |
| 2143 | |
| 2144 | if (*envp) { |
| 2145 | /* if the envelopes were parsed, check the message-id, even on error */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2146 | ret = recv_reply_check_msgid(session, *envp, msgid); |
| 2147 | goto cleanup; |
Michal Vasko | b25c56f | 2024-03-27 15:45:56 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | if (lyrc) { |
| 2151 | /* parsing error */ |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2152 | ERR(session, "Received an invalid message (%s).", ly_err_last(LYD_CTX(op))->msg); |
Michal Vasko | 9a10805 | 2022-04-01 12:06:54 +0200 | [diff] [blame] | 2153 | lyd_free_tree(*envp); |
| 2154 | *envp = NULL; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2155 | ret = NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2156 | goto cleanup; |
| 2157 | } |
| 2158 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2159 | cleanup: |
| 2160 | ly_in_free(msg, 1); |
| 2161 | return ret; |
| 2162 | } |
| 2163 | |
| 2164 | static int |
| 2165 | recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op) |
| 2166 | { |
Michal Vasko | 143aa14 | 2021-10-01 15:31:48 +0200 | [diff] [blame] | 2167 | LY_ERR lyrc = LY_SUCCESS; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2168 | struct nc_rpc_act_generic *rpc_gen; |
| 2169 | struct ly_in *in; |
| 2170 | struct lyd_node *tree, *op2; |
| 2171 | const struct lys_module *mod; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2172 | const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2173 | |
| 2174 | switch (rpc->type) { |
| 2175 | case NC_RPC_ACT_GENERIC: |
| 2176 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
| 2177 | if (rpc_gen->has_data) { |
| 2178 | tree = rpc_gen->content.data; |
| 2179 | |
| 2180 | /* find the operation node */ |
| 2181 | lyrc = LY_EINVAL; |
| 2182 | LYD_TREE_DFS_BEGIN(tree, op2) { |
| 2183 | if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 2184 | lyrc = lyd_dup_single(op2, NULL, 0, op); |
| 2185 | break; |
| 2186 | } |
| 2187 | LYD_TREE_DFS_END(tree, op2); |
| 2188 | } |
| 2189 | } else { |
| 2190 | ly_in_new_memory(rpc_gen->content.xml_str, &in); |
| 2191 | lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2); |
| 2192 | ly_in_free(in, 0); |
| 2193 | if (lyrc) { |
Michal Vasko | 9a10805 | 2022-04-01 12:06:54 +0200 | [diff] [blame] | 2194 | lyd_free_tree(tree); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2195 | return -1; |
| 2196 | } |
| 2197 | |
| 2198 | /* we want just the operation node */ |
| 2199 | lyrc = lyd_dup_single(op2, NULL, 0, op); |
| 2200 | |
| 2201 | lyd_free_tree(tree); |
| 2202 | } |
| 2203 | break; |
| 2204 | case NC_RPC_GETCONFIG: |
| 2205 | module_name = "ietf-netconf"; |
| 2206 | rpc_name = "get-config"; |
| 2207 | break; |
| 2208 | case NC_RPC_EDIT: |
| 2209 | module_name = "ietf-netconf"; |
| 2210 | rpc_name = "edit-config"; |
| 2211 | break; |
| 2212 | case NC_RPC_COPY: |
| 2213 | module_name = "ietf-netconf"; |
| 2214 | rpc_name = "copy-config"; |
| 2215 | break; |
| 2216 | case NC_RPC_DELETE: |
| 2217 | module_name = "ietf-netconf"; |
| 2218 | rpc_name = "delete-config"; |
| 2219 | break; |
| 2220 | case NC_RPC_LOCK: |
| 2221 | module_name = "ietf-netconf"; |
| 2222 | rpc_name = "lock"; |
| 2223 | break; |
| 2224 | case NC_RPC_UNLOCK: |
| 2225 | module_name = "ietf-netconf"; |
| 2226 | rpc_name = "unlock"; |
| 2227 | break; |
| 2228 | case NC_RPC_GET: |
| 2229 | module_name = "ietf-netconf"; |
| 2230 | rpc_name = "get"; |
| 2231 | break; |
| 2232 | case NC_RPC_KILL: |
| 2233 | module_name = "ietf-netconf"; |
| 2234 | rpc_name = "kill-session"; |
| 2235 | break; |
| 2236 | case NC_RPC_COMMIT: |
| 2237 | module_name = "ietf-netconf"; |
| 2238 | rpc_name = "commit"; |
| 2239 | break; |
| 2240 | case NC_RPC_DISCARD: |
| 2241 | module_name = "ietf-netconf"; |
| 2242 | rpc_name = "discard-changes"; |
| 2243 | break; |
| 2244 | case NC_RPC_CANCEL: |
| 2245 | module_name = "ietf-netconf"; |
| 2246 | rpc_name = "cancel-commit"; |
| 2247 | break; |
| 2248 | case NC_RPC_VALIDATE: |
| 2249 | module_name = "ietf-netconf"; |
| 2250 | rpc_name = "validate"; |
| 2251 | break; |
| 2252 | case NC_RPC_GETSCHEMA: |
| 2253 | module_name = "ietf-netconf-monitoring"; |
| 2254 | rpc_name = "get-schema"; |
| 2255 | break; |
| 2256 | case NC_RPC_SUBSCRIBE: |
| 2257 | module_name = "notifications"; |
Michal Vasko | eed893d | 2021-05-25 17:11:08 +0200 | [diff] [blame] | 2258 | rpc_name = "create-subscription"; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2259 | break; |
| 2260 | case NC_RPC_GETDATA: |
| 2261 | module_name = "ietf-netconf-nmda"; |
| 2262 | rpc_name = "get-data"; |
| 2263 | break; |
| 2264 | case NC_RPC_EDITDATA: |
| 2265 | module_name = "ietf-netconf-nmda"; |
| 2266 | rpc_name = "edit-data"; |
| 2267 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2268 | case NC_RPC_ESTABLISHSUB: |
| 2269 | module_name = "ietf-subscribed-notifications"; |
| 2270 | rpc_name = "establish-subscription"; |
| 2271 | break; |
| 2272 | case NC_RPC_MODIFYSUB: |
| 2273 | module_name = "ietf-subscribed-notifications"; |
| 2274 | rpc_name = "modify-subscription"; |
| 2275 | break; |
| 2276 | case NC_RPC_DELETESUB: |
| 2277 | module_name = "ietf-subscribed-notifications"; |
| 2278 | rpc_name = "delete-subscription"; |
| 2279 | break; |
| 2280 | case NC_RPC_KILLSUB: |
| 2281 | module_name = "ietf-subscribed-notifications"; |
| 2282 | rpc_name = "kill-subscription"; |
| 2283 | break; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2284 | case NC_RPC_ESTABLISHPUSH: |
| 2285 | module_name = "ietf-subscribed-notifications"; |
| 2286 | rpc_name = "establish-subscription"; |
| 2287 | module_check = "ietf-yang-push"; |
| 2288 | break; |
| 2289 | case NC_RPC_MODIFYPUSH: |
| 2290 | module_name = "ietf-subscribed-notifications"; |
| 2291 | rpc_name = "modify-subscription"; |
| 2292 | module_check = "ietf-yang-push"; |
| 2293 | break; |
| 2294 | case NC_RPC_RESYNCSUB: |
| 2295 | module_name = "ietf-yang-push"; |
| 2296 | rpc_name = "resync-subscription"; |
| 2297 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2298 | case NC_RPC_UNKNOWN: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2299 | lyrc = LY_EINT; |
| 2300 | break; |
| 2301 | } |
| 2302 | |
| 2303 | if (module_name && rpc_name) { |
| 2304 | mod = ly_ctx_get_module_implemented(session->ctx, module_name); |
| 2305 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2306 | ERR(session, "Missing \"%s\" module in the context.", module_name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2307 | return -1; |
| 2308 | } |
| 2309 | |
| 2310 | /* create the operation node */ |
| 2311 | lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op); |
| 2312 | } |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2313 | if (module_check) { |
| 2314 | if (!ly_ctx_get_module_implemented(session->ctx, module_check)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2315 | ERR(session, "Missing \"%s\" module in the context.", module_check); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2316 | return -1; |
| 2317 | } |
| 2318 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2319 | |
| 2320 | if (lyrc) { |
| 2321 | return -1; |
| 2322 | } |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | API NC_MSG_TYPE |
| 2327 | nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp, |
| 2328 | struct lyd_node **op) |
| 2329 | { |
| 2330 | NC_MSG_TYPE ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2331 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 2332 | NC_CHECK_ARG_RET(session, session, rpc, envp, op, NC_MSG_ERROR); |
| 2333 | |
| 2334 | if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2335 | ERR(session, "Invalid session to receive RPC replies."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2336 | return NC_MSG_ERROR; |
| 2337 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2338 | |
| 2339 | /* get a duplicate of the RPC node to append reply to */ |
| 2340 | if (recv_reply_dup_rpc(session, rpc, op)) { |
| 2341 | return NC_MSG_ERROR; |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 2342 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2343 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2344 | /* receive a reply */ |
| 2345 | ret = recv_reply(session, timeout, *op, msgid, envp); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2346 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2347 | /* do not return the RPC copy on error or if the reply includes no data */ |
| 2348 | if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) { |
| 2349 | lyd_free_tree(*op); |
| 2350 | *op = NULL; |
| 2351 | } |
| 2352 | return ret; |
| 2353 | } |
| 2354 | |
| 2355 | static NC_MSG_TYPE |
| 2356 | recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op) |
| 2357 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2358 | LY_ERR lyrc; |
| 2359 | struct ly_in *msg = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2360 | NC_MSG_TYPE ret = NC_MSG_ERROR; |
| 2361 | |
| 2362 | *op = NULL; |
| 2363 | *envp = NULL; |
| 2364 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2365 | /* Receive messages until a notification is found or a timeout or error reached */ |
| 2366 | ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg); |
| 2367 | if (ret != NC_MSG_NOTIF) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2368 | goto cleanup; |
| 2369 | } |
| 2370 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2371 | /* Parse */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2372 | lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op); |
| 2373 | if (!lyrc) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2374 | goto cleanup; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2375 | } else { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2376 | ERR(session, "Received an invalid message (%s).", ly_err_last(session->ctx)->msg); |
Michal Vasko | 9a10805 | 2022-04-01 12:06:54 +0200 | [diff] [blame] | 2377 | lyd_free_tree(*envp); |
| 2378 | *envp = NULL; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2379 | ret = NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2380 | goto cleanup; |
| 2381 | } |
| 2382 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2383 | cleanup: |
| 2384 | ly_in_free(msg, 1); |
| 2385 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | API NC_MSG_TYPE |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2389 | nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2390 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 2391 | NC_CHECK_ARG_RET(session, session, envp, op, NC_MSG_ERROR); |
| 2392 | |
| 2393 | if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2394 | ERR(session, "Invalid session to receive Notifications."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2395 | return NC_MSG_ERROR; |
| 2396 | } |
| 2397 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2398 | /* receive a notification */ |
| 2399 | return recv_notif(session, timeout, envp, op); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2400 | } |
| 2401 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2402 | static void * |
| 2403 | nc_recv_notif_thread(void *arg) |
| 2404 | { |
| 2405 | struct nc_ntf_thread_arg *ntarg; |
| 2406 | struct nc_session *session; |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2407 | nc_notif_dispatch_clb notif_clb; |
| 2408 | void *user_data; |
Michal Vasko | 743ff9f | 2022-10-20 10:03:13 +0200 | [diff] [blame] | 2409 | |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2410 | void (*free_data)(void *); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2411 | struct lyd_node *envp, *op; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2412 | NC_MSG_TYPE msgtype; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2413 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2414 | /* detach ourselves */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2415 | pthread_detach(pthread_self()); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2416 | |
| 2417 | ntarg = (struct nc_ntf_thread_arg *)arg; |
| 2418 | session = ntarg->session; |
| 2419 | notif_clb = ntarg->notif_clb; |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2420 | user_data = ntarg->user_data; |
| 2421 | free_data = ntarg->free_data; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2422 | free(ntarg); |
| 2423 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2424 | while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2425 | msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2426 | if (msgtype == NC_MSG_NOTIF) { |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2427 | notif_clb(session, envp, op, user_data); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2428 | if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) { |
| 2429 | lyd_free_tree(envp); |
Michal Vasko | 0c0239c | 2023-02-01 14:31:06 +0100 | [diff] [blame] | 2430 | lyd_free_all(op); |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 2431 | break; |
| 2432 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2433 | lyd_free_tree(envp); |
Michal Vasko | 0c0239c | 2023-02-01 14:31:06 +0100 | [diff] [blame] | 2434 | lyd_free_all(op); |
Michal Vasko | ce32605 | 2018-01-04 10:32:03 +0100 | [diff] [blame] | 2435 | } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) { |
Michal Vasko | 132f7f4 | 2017-09-14 13:40:18 +0200 | [diff] [blame] | 2436 | /* quit this thread once the session is broken */ |
| 2437 | break; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | usleep(NC_CLIENT_NOTIF_THREAD_SLEEP); |
| 2441 | } |
| 2442 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2443 | VRB(session, "Notification thread exit."); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2444 | ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count); |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2445 | if (free_data) { |
| 2446 | free_data(user_data); |
| 2447 | } |
| 2448 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2449 | return NULL; |
| 2450 | } |
| 2451 | |
| 2452 | API int |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2453 | nc_recv_notif_dispatch(struct nc_session *session, nc_notif_dispatch_clb notif_clb) |
| 2454 | { |
| 2455 | return nc_recv_notif_dispatch_data(session, notif_clb, NULL, NULL); |
| 2456 | } |
| 2457 | |
| 2458 | API int |
| 2459 | nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb notif_clb, void *user_data, |
| 2460 | void (*free_data)(void *)) |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2461 | { |
| 2462 | struct nc_ntf_thread_arg *ntarg; |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2463 | pthread_t tid; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2464 | int ret; |
| 2465 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 2466 | NC_CHECK_ARG_RET(session, session, notif_clb, -1); |
| 2467 | |
| 2468 | if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2469 | ERR(session, "Invalid session to receive Notifications."); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2470 | return -1; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | ntarg = malloc(sizeof *ntarg); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2474 | NC_CHECK_ERRMEM_RET(!ntarg, -1); |
| 2475 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2476 | ntarg->session = session; |
| 2477 | ntarg->notif_clb = notif_clb; |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2478 | ntarg->user_data = user_data; |
| 2479 | ntarg->free_data = free_data; |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2480 | ATOMIC_INC_RELAXED(session->opts.client.ntf_thread_count); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2481 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2482 | /* just so that nc_recv_notif_thread() does not immediately exit */ |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2483 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 1); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2484 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2485 | ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2486 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2487 | ERR(session, "Failed to create a new thread (%s).", strerror(errno)); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2488 | free(ntarg); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2489 | if (ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count) == 1) { |
| 2490 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0); |
| 2491 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2492 | return -1; |
| 2493 | } |
| 2494 | |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2498 | static const char * |
| 2499 | nc_wd2str(NC_WD_MODE wd) |
| 2500 | { |
| 2501 | switch (wd) { |
| 2502 | case NC_WD_ALL: |
| 2503 | return "report-all"; |
| 2504 | case NC_WD_ALL_TAG: |
| 2505 | return "report-all-tagged"; |
| 2506 | case NC_WD_TRIM: |
| 2507 | return "trim"; |
| 2508 | case NC_WD_EXPLICIT: |
| 2509 | return "explicit"; |
| 2510 | default: |
| 2511 | break; |
| 2512 | } |
| 2513 | |
| 2514 | return NULL; |
| 2515 | } |
| 2516 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2517 | API NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 2518 | nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2519 | { |
| 2520 | NC_MSG_TYPE r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2521 | int dofree = 1; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2522 | struct ly_in *in; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 2523 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2524 | struct nc_rpc_getconfig *rpc_gc; |
| 2525 | struct nc_rpc_edit *rpc_e; |
| 2526 | struct nc_rpc_copy *rpc_cp; |
| 2527 | struct nc_rpc_delete *rpc_del; |
| 2528 | struct nc_rpc_lock *rpc_lock; |
| 2529 | struct nc_rpc_get *rpc_g; |
| 2530 | struct nc_rpc_kill *rpc_k; |
| 2531 | struct nc_rpc_commit *rpc_com; |
| 2532 | struct nc_rpc_cancel *rpc_can; |
| 2533 | struct nc_rpc_validate *rpc_val; |
| 2534 | struct nc_rpc_getschema *rpc_gs; |
| 2535 | struct nc_rpc_subscribe *rpc_sub; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2536 | struct nc_rpc_getdata *rpc_getd; |
| 2537 | struct nc_rpc_editdata *rpc_editd; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2538 | struct nc_rpc_establishsub *rpc_estsub; |
| 2539 | struct nc_rpc_modifysub *rpc_modsub; |
| 2540 | struct nc_rpc_deletesub *rpc_delsub; |
| 2541 | struct nc_rpc_killsub *rpc_killsub; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2542 | struct nc_rpc_establishpush *rpc_estpush; |
| 2543 | struct nc_rpc_modifypush *rpc_modpush; |
| 2544 | struct nc_rpc_resyncsub *rpc_resyncsub; |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2545 | struct lyd_node *data = NULL, *node, *cont; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2546 | const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd; |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2547 | LY_ERR lyrc = 0; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2548 | int i; |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 2549 | char str[11]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2550 | uint64_t cur_msgid; |
| 2551 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 2552 | NC_CHECK_ARG_RET(session, session, rpc, msgid, NC_MSG_ERROR); |
| 2553 | |
| 2554 | if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2555 | ERR(session, "Invalid session to send RPCs."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2556 | return NC_MSG_ERROR; |
| 2557 | } |
| 2558 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2559 | switch (rpc->type) { |
| 2560 | case NC_RPC_ACT_GENERIC: |
| 2561 | /* checked when parsing */ |
| 2562 | break; |
| 2563 | case NC_RPC_GETCONFIG: |
| 2564 | case NC_RPC_EDIT: |
| 2565 | case NC_RPC_COPY: |
| 2566 | case NC_RPC_DELETE: |
| 2567 | case NC_RPC_LOCK: |
| 2568 | case NC_RPC_UNLOCK: |
| 2569 | case NC_RPC_GET: |
| 2570 | case NC_RPC_KILL: |
| 2571 | case NC_RPC_COMMIT: |
| 2572 | case NC_RPC_DISCARD: |
| 2573 | case NC_RPC_CANCEL: |
| 2574 | case NC_RPC_VALIDATE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2575 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2576 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2577 | ERR(session, "Missing \"ietf-netconf\" module in the context."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2578 | return NC_MSG_ERROR; |
| 2579 | } |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2580 | break; |
| 2581 | case NC_RPC_GETSCHEMA: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2582 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2583 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2584 | ERR(session, "Missing \"ietf-netconf-monitoring\" module in the context."); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2585 | return NC_MSG_ERROR; |
| 2586 | } |
| 2587 | break; |
| 2588 | case NC_RPC_SUBSCRIBE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2589 | mod = ly_ctx_get_module_implemented(session->ctx, "notifications"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2590 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2591 | ERR(session, "Missing \"notifications\" module in the context."); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2592 | return NC_MSG_ERROR; |
| 2593 | } |
| 2594 | break; |
| 2595 | case NC_RPC_GETDATA: |
| 2596 | case NC_RPC_EDITDATA: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2597 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2598 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2599 | ERR(session, "Missing \"ietf-netconf-nmda\" module in the context."); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2600 | return NC_MSG_ERROR; |
| 2601 | } |
| 2602 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2603 | case NC_RPC_ESTABLISHSUB: |
| 2604 | case NC_RPC_MODIFYSUB: |
| 2605 | case NC_RPC_DELETESUB: |
| 2606 | case NC_RPC_KILLSUB: |
| 2607 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications"); |
| 2608 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2609 | ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context."); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2610 | return NC_MSG_ERROR; |
| 2611 | } |
| 2612 | break; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2613 | case NC_RPC_ESTABLISHPUSH: |
| 2614 | case NC_RPC_MODIFYPUSH: |
| 2615 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications"); |
| 2616 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2617 | ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context."); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2618 | return NC_MSG_ERROR; |
| 2619 | } |
| 2620 | mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push"); |
| 2621 | if (!mod2) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2622 | ERR(session, "Missing \"ietf-yang-push\" module in the context."); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2623 | return NC_MSG_ERROR; |
| 2624 | } |
| 2625 | break; |
| 2626 | case NC_RPC_RESYNCSUB: |
| 2627 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push"); |
| 2628 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2629 | ERR(session, "Missing \"ietf-yang-push\" module in the context."); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2630 | return NC_MSG_ERROR; |
| 2631 | } |
| 2632 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2633 | case NC_RPC_UNKNOWN: |
| 2634 | ERRINT; |
| 2635 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2638 | #define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break; |
| 2639 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2640 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 2641 | case NC_RPC_ACT_GENERIC: |
| 2642 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2643 | |
| 2644 | if (rpc_gen->has_data) { |
| 2645 | data = rpc_gen->content.data; |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 2646 | dofree = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2647 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2648 | ly_in_new_memory(rpc_gen->content.xml_str, &in); |
| 2649 | lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL); |
| 2650 | ly_in_free(in, 0); |
| 2651 | if (lyrc) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2652 | break; |
Michal Vasko | eec410f | 2017-11-24 09:14:55 +0100 | [diff] [blame] | 2653 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2654 | } |
| 2655 | break; |
| 2656 | |
| 2657 | case NC_RPC_GETCONFIG: |
| 2658 | rpc_gc = (struct nc_rpc_getconfig *)rpc; |
| 2659 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2660 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data)); |
| 2661 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont)); |
| 2662 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2663 | if (rpc_gc->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2664 | if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2665 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, LYD_ANYDATA_XML, 0, &node)); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2666 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2667 | } else { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2668 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node)); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2669 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL)); |
| 2670 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | if (rpc_gc->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2675 | ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2676 | if (!ietfncwd) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 2677 | ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2678 | lyrc = LY_ENOTFOUND; |
| 2679 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2680 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2681 | CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_gc->wd_mode), 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2682 | } |
| 2683 | break; |
| 2684 | |
| 2685 | case NC_RPC_EDIT: |
| 2686 | rpc_e = (struct nc_rpc_edit *)rpc; |
| 2687 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2688 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data)); |
| 2689 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
| 2690 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2691 | |
| 2692 | if (rpc_e->default_op) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2693 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op], 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2694 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2695 | if (rpc_e->test_opt) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2696 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt], 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2697 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2698 | if (rpc_e->error_opt) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2699 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt], 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2700 | } |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2701 | if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2702 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_e->edit_cont, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2703 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2704 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2705 | } |
| 2706 | break; |
| 2707 | |
| 2708 | case NC_RPC_COPY: |
| 2709 | rpc_cp = (struct nc_rpc_copy *)rpc; |
| 2710 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2711 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data)); |
| 2712 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2713 | if (rpc_cp->url_trg) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2714 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2715 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2716 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2719 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2720 | if (rpc_cp->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2721 | if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2722 | CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_cp->url_config_src, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2723 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2724 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2725 | } |
| 2726 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2727 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | if (rpc_cp->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2731 | ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2732 | if (!ietfncwd) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 2733 | ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2734 | lyrc = LY_ENOTFOUND; |
| 2735 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2736 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2737 | CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_cp->wd_mode), 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2738 | } |
| 2739 | break; |
| 2740 | |
| 2741 | case NC_RPC_DELETE: |
| 2742 | rpc_del = (struct nc_rpc_delete *)rpc; |
| 2743 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2744 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data)); |
| 2745 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2746 | if (rpc_del->url) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2747 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2748 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2749 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2750 | } |
| 2751 | break; |
| 2752 | |
| 2753 | case NC_RPC_LOCK: |
| 2754 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 2755 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2756 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data)); |
| 2757 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
| 2758 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2759 | break; |
| 2760 | |
| 2761 | case NC_RPC_UNLOCK: |
| 2762 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 2763 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2764 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data)); |
| 2765 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
| 2766 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2767 | break; |
| 2768 | |
| 2769 | case NC_RPC_GET: |
| 2770 | rpc_g = (struct nc_rpc_get *)rpc; |
| 2771 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2772 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2773 | if (rpc_g->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2774 | if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2775 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, LYD_ANYDATA_XML, 0, &node)); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2776 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2777 | } else { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2778 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node)); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2779 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL)); |
| 2780 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | if (rpc_g->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2785 | ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2786 | if (!ietfncwd) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 2787 | ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2788 | lyrc = LY_ENOTFOUND; |
| 2789 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2790 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2791 | CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_g->wd_mode), 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2792 | } |
| 2793 | break; |
| 2794 | |
| 2795 | case NC_RPC_KILL: |
| 2796 | rpc_k = (struct nc_rpc_kill *)rpc; |
| 2797 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2798 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data)); |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 2799 | sprintf(str, "%" PRIu32, rpc_k->sid); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2800 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2801 | break; |
| 2802 | |
| 2803 | case NC_RPC_COMMIT: |
| 2804 | rpc_com = (struct nc_rpc_commit *)rpc; |
| 2805 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2806 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2807 | if (rpc_com->confirmed) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2808 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | if (rpc_com->confirm_timeout) { |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 2812 | sprintf(str, "%" PRIu32, rpc_com->confirm_timeout); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2813 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2814 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2815 | if (rpc_com->persist) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2816 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2817 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2818 | if (rpc_com->persist_id) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2819 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2820 | } |
| 2821 | break; |
| 2822 | |
| 2823 | case NC_RPC_DISCARD: |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2824 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2825 | break; |
| 2826 | |
| 2827 | case NC_RPC_CANCEL: |
| 2828 | rpc_can = (struct nc_rpc_cancel *)rpc; |
| 2829 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2830 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2831 | if (rpc_can->persist_id) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2832 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2833 | } |
| 2834 | break; |
| 2835 | |
| 2836 | case NC_RPC_VALIDATE: |
| 2837 | rpc_val = (struct nc_rpc_validate *)rpc; |
| 2838 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2839 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data)); |
| 2840 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2841 | if (rpc_val->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2842 | if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2843 | CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_val->url_config_src, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2844 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2845 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2846 | } |
| 2847 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2848 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2849 | } |
| 2850 | break; |
| 2851 | |
| 2852 | case NC_RPC_GETSCHEMA: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2853 | rpc_gs = (struct nc_rpc_getschema *)rpc; |
| 2854 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2855 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data)); |
| 2856 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2857 | if (rpc_gs->version) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2858 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2859 | } |
| 2860 | if (rpc_gs->format) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2861 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2862 | } |
| 2863 | break; |
| 2864 | |
| 2865 | case NC_RPC_SUBSCRIBE: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2866 | rpc_sub = (struct nc_rpc_subscribe *)rpc; |
| 2867 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2868 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2869 | if (rpc_sub->stream) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2870 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | if (rpc_sub->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2874 | if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2875 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, LYD_ANYDATA_XML, 0, &node)); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2876 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2877 | } else { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2878 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, LYD_ANYDATA_STRING, 0, &node)); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2879 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL)); |
| 2880 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2881 | } |
| 2882 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2883 | if (rpc_sub->start) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2884 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2885 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2886 | if (rpc_sub->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2887 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2888 | } |
| 2889 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2890 | |
| 2891 | case NC_RPC_GETDATA: |
| 2892 | rpc_getd = (struct nc_rpc_getdata *)rpc; |
| 2893 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2894 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data)); |
| 2895 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL)); |
| 2896 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2897 | if (rpc_getd->filter) { |
| 2898 | if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2899 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2900 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2901 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | if (rpc_getd->config_filter) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2905 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2906 | } |
| 2907 | for (i = 0; i < rpc_getd->origin_filter_count; ++i) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2908 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : |
| 2909 | "origin-filter", rpc_getd->origin_filter[i], 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2910 | } |
| 2911 | if (rpc_getd->max_depth) { |
| 2912 | sprintf(str, "%u", rpc_getd->max_depth); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2913 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2914 | } |
| 2915 | if (rpc_getd->with_origin) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2916 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | if (rpc_getd->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2920 | /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */ |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2921 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-defaults", nc_wd2str(rpc_getd->wd_mode), 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2922 | } |
| 2923 | break; |
| 2924 | |
| 2925 | case NC_RPC_EDITDATA: |
| 2926 | rpc_editd = (struct nc_rpc_editdata *)rpc; |
| 2927 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2928 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data)); |
| 2929 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2930 | |
| 2931 | if (rpc_editd->default_op) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2932 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0, |
| 2933 | NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2934 | } |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2935 | if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2936 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2937 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2938 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2939 | } |
| 2940 | break; |
| 2941 | |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2942 | case NC_RPC_ESTABLISHSUB: |
| 2943 | rpc_estsub = (struct nc_rpc_establishsub *)rpc; |
| 2944 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2945 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2946 | |
| 2947 | if (rpc_estsub->filter) { |
| 2948 | if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2949 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, LYD_ANYDATA_XML, |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2950 | 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2951 | } else if (rpc_estsub->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2952 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2953 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2954 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2955 | } |
| 2956 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2957 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2958 | |
| 2959 | if (rpc_estsub->start) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2960 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2961 | } |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2962 | if (rpc_estsub->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2963 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2964 | } |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2965 | if (rpc_estsub->encoding) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2966 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2967 | } |
| 2968 | break; |
| 2969 | |
| 2970 | case NC_RPC_MODIFYSUB: |
| 2971 | rpc_modsub = (struct nc_rpc_modifysub *)rpc; |
| 2972 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2973 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2974 | |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 2975 | sprintf(str, "%" PRIu32, rpc_modsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2976 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2977 | |
| 2978 | if (rpc_modsub->filter) { |
| 2979 | if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 2980 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, LYD_ANYDATA_XML, |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2981 | 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2982 | } else if (rpc_modsub->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2983 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2984 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2985 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2986 | } |
| 2987 | } |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2988 | if (rpc_modsub->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2989 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2990 | } |
| 2991 | break; |
| 2992 | |
| 2993 | case NC_RPC_DELETESUB: |
| 2994 | rpc_delsub = (struct nc_rpc_deletesub *)rpc; |
| 2995 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2996 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2997 | |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 2998 | sprintf(str, "%" PRIu32, rpc_delsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2999 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3000 | break; |
| 3001 | |
| 3002 | case NC_RPC_KILLSUB: |
| 3003 | rpc_killsub = (struct nc_rpc_killsub *)rpc; |
| 3004 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3005 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3006 | |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 3007 | sprintf(str, "%" PRIu32, rpc_killsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3008 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3009 | break; |
| 3010 | |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3011 | case NC_RPC_ESTABLISHPUSH: |
| 3012 | rpc_estpush = (struct nc_rpc_establishpush *)rpc; |
| 3013 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3014 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data)); |
| 3015 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3016 | |
| 3017 | if (rpc_estpush->filter) { |
| 3018 | if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 3019 | CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter, |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3020 | LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3021 | } else if (rpc_estpush->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3022 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3023 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3024 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | if (rpc_estpush->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3029 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3030 | } |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3031 | if (rpc_estpush->encoding) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3032 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | if (rpc_estpush->periodic) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3036 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3037 | sprintf(str, "%" PRIu32, rpc_estpush->period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3038 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3039 | if (rpc_estpush->anchor_time) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3040 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3041 | } |
| 3042 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3043 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3044 | if (rpc_estpush->dampening_period) { |
| 3045 | sprintf(str, "%" PRIu32, rpc_estpush->dampening_period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3046 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3047 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3048 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0, |
| 3049 | NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3050 | if (rpc_estpush->excluded_change) { |
| 3051 | for (i = 0; rpc_estpush->excluded_change[i]; ++i) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3052 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0, |
| 3053 | NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | break; |
| 3058 | |
| 3059 | case NC_RPC_MODIFYPUSH: |
| 3060 | rpc_modpush = (struct nc_rpc_modifypush *)rpc; |
| 3061 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3062 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3063 | |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 3064 | sprintf(str, "%" PRIu32, rpc_modpush->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3065 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
| 3066 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3067 | |
| 3068 | if (rpc_modpush->filter) { |
| 3069 | if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) { |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 3070 | CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter, |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3071 | LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3072 | } else if (rpc_modpush->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3073 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3074 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3075 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3076 | } |
| 3077 | } |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3078 | if (rpc_modpush->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3079 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | if (rpc_modpush->periodic) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3083 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3084 | sprintf(str, "%" PRIu32, rpc_modpush->period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3085 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3086 | if (rpc_modpush->anchor_time) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3087 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3088 | } |
| 3089 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3090 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3091 | if (rpc_modpush->dampening_period) { |
| 3092 | sprintf(str, "%" PRIu32, rpc_modpush->dampening_period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3093 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | break; |
| 3097 | |
| 3098 | case NC_RPC_RESYNCSUB: |
| 3099 | rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc; |
| 3100 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3101 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data)); |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 3102 | sprintf(str, "%" PRIu32, rpc_resyncsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3103 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3104 | break; |
| 3105 | |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3106 | case NC_RPC_UNKNOWN: |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 3107 | ERRINT; |
| 3108 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3111 | #undef CHECK_LYRC_BREAK |
| 3112 | |
| 3113 | if (lyrc) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3114 | ERR(session, "Failed to create RPC, perhaps a required feature is disabled."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3115 | lyd_free_tree(data); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3116 | return NC_MSG_ERROR; |
| 3117 | } |
| 3118 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3119 | /* send RPC, store its message ID */ |
| 3120 | r = nc_send_msg_io(session, timeout, data); |
| 3121 | cur_msgid = session->opts.client.msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3122 | |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 3123 | if (dofree) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 3124 | lyd_free_tree(data); |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 3125 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3126 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3127 | if (r == NC_MSG_RPC) { |
| 3128 | *msgid = cur_msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3129 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3130 | return r; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3131 | } |
Michal Vasko | de2946c | 2017-01-12 12:19:26 +0100 | [diff] [blame] | 3132 | |
| 3133 | API void |
| 3134 | nc_client_session_set_not_strict(struct nc_session *session) |
| 3135 | { |
| 3136 | if (session->side != NC_CLIENT) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 3137 | ERRARG(NULL, "session"); |
Michal Vasko | de2946c | 2017-01-12 12:19:26 +0100 | [diff] [blame] | 3138 | return; |
| 3139 | } |
| 3140 | |
| 3141 | session->flags |= NC_SESSION_CLIENT_NOT_STRICT; |
| 3142 | } |