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