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