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