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