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