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