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