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