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