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