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