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