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