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); |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1383 | if (sock == -1) { |
| 1384 | if (!sock_pending || *sock_pending == -1) { |
| 1385 | /* try the next resource */ |
| 1386 | continue; |
| 1387 | } else { |
| 1388 | /* timeout, keep pending socket */ |
| 1389 | return -1; |
| 1390 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1391 | } |
| 1392 | 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] | 1393 | |
| 1394 | opt = 1; |
| 1395 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
| 1396 | ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1397 | goto error; |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1398 | } |
| 1399 | |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1400 | if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) { |
| 1401 | buf = malloc(INET6_ADDRSTRLEN); |
| 1402 | if (!buf) { |
| 1403 | ERRMEM; |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1404 | goto error; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1405 | } |
| 1406 | if (res->ai_family == AF_INET) { |
| 1407 | addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; |
| 1408 | } else { |
| 1409 | addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; |
| 1410 | } |
| 1411 | if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) { |
| 1412 | ERR("Converting host to IP address failed (%s).", strerror(errno)); |
| 1413 | free(buf); |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1414 | goto error; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | *ip_host = buf; |
| 1418 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1419 | break; |
| 1420 | } |
| 1421 | freeaddrinfo(res_list); |
| 1422 | |
| 1423 | } else { |
| 1424 | /* try to get a connection with the pending socket */ |
| 1425 | assert(sock_pending); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1426 | sock = _non_blocking_connect(timeout, sock_pending, NULL, ka); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | return sock; |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1430 | |
| 1431 | error: |
| 1432 | if (sock != -1) { |
| 1433 | close(sock); |
| 1434 | } |
| 1435 | if (sock_pending) { |
| 1436 | *sock_pending = -1; |
| 1437 | } |
| 1438 | return -1; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1441 | static NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1442 | 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] | 1443 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1444 | char *ptr; |
| 1445 | const char *str_msgid; |
| 1446 | uint64_t cur_msgid; |
| 1447 | struct lyxml_elem *xml; |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 1448 | struct nc_msg_cont *cont, **cont_ptr; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1449 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 1450 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1451 | /* try to get notification from the session's queue */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1452 | if (!msgid && session->opts.client.notifs) { |
| 1453 | cont = session->opts.client.notifs; |
| 1454 | session->opts.client.notifs = cont->next; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1455 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1456 | xml = cont->msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1457 | free(cont); |
| 1458 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1459 | msgtype = NC_MSG_NOTIF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* try to get rpc-reply from the session's queue */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1463 | if (msgid && session->opts.client.replies) { |
| 1464 | cont = session->opts.client.replies; |
| 1465 | session->opts.client.replies = cont->next; |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 1466 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1467 | xml = cont->msg; |
| 1468 | free(cont); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1469 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1470 | msgtype = NC_MSG_REPLY; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1473 | if (!msgtype) { |
| 1474 | /* read message from wire */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1475 | msgtype = nc_read_msg_poll_io(session, timeout, &xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1476 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1477 | |
| 1478 | /* we read rpc-reply, want a notif */ |
| 1479 | if (!msgid && (msgtype == NC_MSG_REPLY)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1480 | cont_ptr = &session->opts.client.replies; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1481 | while (*cont_ptr) { |
| 1482 | cont_ptr = &((*cont_ptr)->next); |
| 1483 | } |
| 1484 | *cont_ptr = malloc(sizeof **cont_ptr); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1485 | if (!*cont_ptr) { |
| 1486 | ERRMEM; |
| 1487 | lyxml_free(session->ctx, xml); |
| 1488 | return NC_MSG_ERROR; |
| 1489 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1490 | (*cont_ptr)->msg = xml; |
| 1491 | (*cont_ptr)->next = NULL; |
| 1492 | } |
| 1493 | |
| 1494 | /* we read notif, want a rpc-reply */ |
| 1495 | if (msgid && (msgtype == NC_MSG_NOTIF)) { |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1496 | if (!session->opts.client.ntf_tid) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1497 | 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] | 1498 | lyxml_free(session->ctx, xml); |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 1499 | return NC_MSG_ERROR; |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1500 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1501 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1502 | cont_ptr = &session->opts.client.notifs; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1503 | while (*cont_ptr) { |
| 1504 | cont_ptr = &((*cont_ptr)->next); |
| 1505 | } |
| 1506 | *cont_ptr = malloc(sizeof **cont_ptr); |
Michal Vasko | 140eecd | 2018-05-30 09:55:56 +0200 | [diff] [blame] | 1507 | if (!*cont_ptr) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1508 | ERRMEM; |
| 1509 | lyxml_free(session->ctx, xml); |
| 1510 | return NC_MSG_ERROR; |
| 1511 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1512 | (*cont_ptr)->msg = xml; |
| 1513 | (*cont_ptr)->next = NULL; |
| 1514 | } |
| 1515 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1516 | switch (msgtype) { |
| 1517 | case NC_MSG_NOTIF: |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 1518 | if (!msgid) { |
| 1519 | *msg = xml; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1520 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1521 | break; |
| 1522 | |
| 1523 | case NC_MSG_REPLY: |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 1524 | if (msgid) { |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1525 | /* check message-id */ |
| 1526 | str_msgid = lyxml_get_attr(xml, "message-id", NULL); |
| 1527 | if (!str_msgid) { |
Michal Vasko | 14fdbb6 | 2018-01-18 09:13:20 +0100 | [diff] [blame] | 1528 | 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] | 1529 | } else { |
| 1530 | cur_msgid = strtoul(str_msgid, &ptr, 10); |
| 1531 | if (cur_msgid != msgid) { |
| 1532 | ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".", |
| 1533 | session->id, str_msgid); |
| 1534 | msgtype = NC_MSG_REPLY_ERR_MSGID; |
| 1535 | } |
| 1536 | } |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 1537 | *msg = xml; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1538 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1539 | break; |
| 1540 | |
| 1541 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1542 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1543 | lyxml_free(session->ctx, xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1544 | msgtype = NC_MSG_ERROR; |
| 1545 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1546 | |
| 1547 | case NC_MSG_RPC: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1548 | ERR("Session %u: received <rpc> from a NETCONF server.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1549 | lyxml_free(session->ctx, xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1550 | msgtype = NC_MSG_ERROR; |
| 1551 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1552 | |
| 1553 | default: |
| 1554 | /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out; |
| 1555 | * NC_MSG_NONE is not returned by nc_read_msg() |
| 1556 | */ |
| 1557 | break; |
| 1558 | } |
| 1559 | |
| 1560 | return msgtype; |
| 1561 | } |
| 1562 | |
| 1563 | /* cannot strictly fail, but does not need to fill any error parameter at all */ |
| 1564 | static void |
| 1565 | parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err) |
| 1566 | { |
| 1567 | struct lyxml_elem *iter, *next, *info; |
| 1568 | |
| 1569 | LY_TREE_FOR(xml->child, iter) { |
| 1570 | if (!iter->ns) { |
| 1571 | if (iter->content) { |
| 1572 | WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content); |
| 1573 | } else { |
| 1574 | WRN("<rpc-error> child \"%s\" without namespace.", iter->name); |
| 1575 | } |
| 1576 | continue; |
| 1577 | } else if (strcmp(iter->ns->value, NC_NS_BASE)) { |
| 1578 | if (iter->content) { |
| 1579 | WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".", |
| 1580 | iter->name, iter->content, iter->ns->value); |
| 1581 | } else { |
| 1582 | WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value); |
| 1583 | } |
| 1584 | continue; |
| 1585 | } |
| 1586 | |
| 1587 | if (!strcmp(iter->name, "error-type")) { |
| 1588 | if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc") |
| 1589 | && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) { |
| 1590 | WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 1591 | } else if (err->type) { |
| 1592 | WRN("<rpc-error> <error-type> duplicated."); |
| 1593 | } else { |
| 1594 | err->type = lydict_insert(ctx, iter->content, 0); |
| 1595 | } |
| 1596 | } else if (!strcmp(iter->name, "error-tag")) { |
| 1597 | if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value") |
| 1598 | && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute") |
| 1599 | && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute") |
| 1600 | && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element") |
| 1601 | && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace") |
| 1602 | && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied") |
| 1603 | && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed") |
| 1604 | && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing") |
| 1605 | && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed") |
| 1606 | && strcmp(iter->content, "malformed-message"))) { |
| 1607 | WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 1608 | } else if (err->tag) { |
| 1609 | WRN("<rpc-error> <error-tag> duplicated."); |
| 1610 | } else { |
| 1611 | err->tag = lydict_insert(ctx, iter->content, 0); |
| 1612 | } |
| 1613 | } else if (!strcmp(iter->name, "error-severity")) { |
| 1614 | if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) { |
| 1615 | WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 1616 | } else if (err->severity) { |
| 1617 | WRN("<rpc-error> <error-severity> duplicated."); |
| 1618 | } else { |
| 1619 | err->severity = lydict_insert(ctx, iter->content, 0); |
| 1620 | } |
| 1621 | } else if (!strcmp(iter->name, "error-app-tag")) { |
| 1622 | if (err->apptag) { |
| 1623 | WRN("<rpc-error> <error-app-tag> duplicated."); |
| 1624 | } else { |
| 1625 | err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 1626 | } |
| 1627 | } else if (!strcmp(iter->name, "error-path")) { |
| 1628 | if (err->path) { |
| 1629 | WRN("<rpc-error> <error-path> duplicated."); |
| 1630 | } else { |
| 1631 | err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 1632 | } |
| 1633 | } else if (!strcmp(iter->name, "error-message")) { |
| 1634 | if (err->message) { |
| 1635 | WRN("<rpc-error> <error-message> duplicated."); |
| 1636 | } else { |
| 1637 | err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL); |
Michal Vasko | b0222c4 | 2018-01-03 15:17:12 +0100 | [diff] [blame] | 1638 | if (err->message_lang) { |
| 1639 | err->message_lang = lydict_insert(ctx, err->message_lang, 0); |
| 1640 | } else { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1641 | VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute."); |
| 1642 | } |
| 1643 | err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 1644 | } |
| 1645 | } else if (!strcmp(iter->name, "error-info")) { |
| 1646 | LY_TREE_FOR_SAFE(iter->child, next, info) { |
| 1647 | if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) { |
| 1648 | if (!strcmp(info->name, "session-id")) { |
| 1649 | if (err->sid) { |
| 1650 | WRN("<rpc-error> <error-info> <session-id> duplicated."); |
| 1651 | } else { |
| 1652 | err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 1653 | } |
Michal Vasko | 630485f | 2018-01-03 14:28:32 +0100 | [diff] [blame] | 1654 | } else if (!strcmp(info->name, "bad-attribute")) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1655 | ++err->attr_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1656 | err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr); |
| 1657 | if (!err->attr) { |
| 1658 | ERRMEM; |
| 1659 | return; |
| 1660 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1661 | err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 1662 | } else if (!strcmp(info->name, "bad-element")) { |
| 1663 | ++err->elem_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1664 | err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem); |
| 1665 | if (!err->elem) { |
| 1666 | ERRMEM; |
| 1667 | return; |
| 1668 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1669 | err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 1670 | } else if (!strcmp(info->name, "bad-namespace")) { |
| 1671 | ++err->ns_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1672 | err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns); |
| 1673 | if (!err->ns) { |
| 1674 | ERRMEM; |
| 1675 | return; |
| 1676 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1677 | err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 1678 | } else { |
| 1679 | if (info->content) { |
| 1680 | WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".", |
| 1681 | info->name, info->content); |
| 1682 | } else { |
| 1683 | WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name); |
| 1684 | } |
| 1685 | } |
| 1686 | } else { |
| 1687 | lyxml_unlink(ctx, info); |
| 1688 | ++err->other_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1689 | err->other = nc_realloc(err->other, err->other_count * sizeof *err->other); |
| 1690 | if (!err->other) { |
| 1691 | ERRMEM; |
| 1692 | return; |
| 1693 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1694 | err->other[err->other_count - 1] = info; |
| 1695 | } |
| 1696 | } |
| 1697 | } else { |
| 1698 | if (iter->content) { |
| 1699 | WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content); |
| 1700 | } else { |
| 1701 | WRN("<rpc-error> unknown child \"%s\".", iter->name); |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | static struct nc_reply * |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 1708 | 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] | 1709 | { |
| 1710 | struct lyxml_elem *iter; |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 1711 | struct lyd_node *data = NULL, *rpc_act = NULL; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1712 | struct nc_client_reply_error *error_rpl; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1713 | struct nc_reply_data *data_rpl; |
| 1714 | struct nc_reply *reply = NULL; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1715 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | f45e0c1 | 2018-11-28 08:38:23 +0100 | [diff] [blame] | 1716 | int i, data_parsed = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1717 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1718 | /* rpc-error */ |
Michal Vasko | 6b281ff | 2019-02-14 10:15:19 +0100 | [diff] [blame] | 1719 | 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] | 1720 | /* count and check elements */ |
| 1721 | i = 0; |
| 1722 | LY_TREE_FOR(xml->child, iter) { |
| 1723 | if (strcmp(iter->name, "rpc-error")) { |
| 1724 | ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name); |
| 1725 | return NULL; |
| 1726 | } else if (!iter->ns) { |
| 1727 | ERR("<rpc-reply> content mismatch (<rpc-error> without namespace)."); |
| 1728 | return NULL; |
| 1729 | } else if (strcmp(iter->ns->value, NC_NS_BASE)) { |
| 1730 | ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value); |
| 1731 | return NULL; |
| 1732 | } |
| 1733 | ++i; |
| 1734 | } |
| 1735 | |
| 1736 | error_rpl = malloc(sizeof *error_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1737 | if (!error_rpl) { |
| 1738 | ERRMEM; |
| 1739 | return NULL; |
| 1740 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1741 | error_rpl->type = NC_RPL_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1742 | error_rpl->err = calloc(i, sizeof *error_rpl->err); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1743 | if (!error_rpl->err) { |
| 1744 | ERRMEM; |
| 1745 | free(error_rpl); |
| 1746 | return NULL; |
| 1747 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1748 | error_rpl->count = i; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1749 | error_rpl->ctx = ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1750 | reply = (struct nc_reply *)error_rpl; |
| 1751 | |
| 1752 | i = 0; |
| 1753 | LY_TREE_FOR(xml->child, iter) { |
| 1754 | parse_rpc_error(ctx, iter, error_rpl->err + i); |
| 1755 | ++i; |
| 1756 | } |
| 1757 | |
| 1758 | /* ok */ |
Michal Vasko | 6b281ff | 2019-02-14 10:15:19 +0100 | [diff] [blame] | 1759 | } 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] | 1760 | if (xml->child->next) { |
| 1761 | ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name); |
| 1762 | return NULL; |
| 1763 | } |
| 1764 | reply = malloc(sizeof *reply); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1765 | if (!reply) { |
| 1766 | ERRMEM; |
| 1767 | return NULL; |
| 1768 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1769 | reply->type = NC_RPL_OK; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1770 | |
| 1771 | /* some RPC output */ |
| 1772 | } else { |
| 1773 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1774 | case NC_RPC_ACT_GENERIC: |
| 1775 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1776 | |
| 1777 | if (rpc_gen->has_data) { |
Michal Vasko | dd3e314 | 2018-07-10 08:30:20 +0200 | [diff] [blame] | 1778 | rpc_act = lyd_dup(rpc_gen->content.data, 1); |
| 1779 | if (!rpc_act) { |
| 1780 | ERR("Failed to duplicate a generic RPC/action."); |
| 1781 | return NULL; |
| 1782 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1783 | } else { |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 1784 | 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] | 1785 | if (!rpc_act) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1786 | ERR("Failed to parse a generic RPC/action XML."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1787 | return NULL; |
| 1788 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1789 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1790 | break; |
| 1791 | |
| 1792 | case NC_RPC_GETCONFIG: |
| 1793 | case NC_RPC_GET: |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 1794 | case NC_RPC_GETDATA: |
Michal Vasko | 6b281ff | 2019-02-14 10:15:19 +0100 | [diff] [blame] | 1795 | /* we should definitely have received at least an empty "data" element even on empty reply, but fine */ |
| 1796 | if (!xml->child || !xml->child->child) { |
Michal Vasko | 13ed294 | 2016-02-29 16:21:00 +0100 | [diff] [blame] | 1797 | /* we did not receive any data */ |
| 1798 | data_rpl = malloc(sizeof *data_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1799 | if (!data_rpl) { |
| 1800 | ERRMEM; |
| 1801 | return NULL; |
| 1802 | } |
Michal Vasko | 13ed294 | 2016-02-29 16:21:00 +0100 | [diff] [blame] | 1803 | data_rpl->type = NC_RPL_DATA; |
| 1804 | data_rpl->data = NULL; |
| 1805 | return (struct nc_reply *)data_rpl; |
| 1806 | } |
| 1807 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1808 | /* special treatment */ |
Michal Vasko | f45e0c1 | 2018-11-28 08:38:23 +0100 | [diff] [blame] | 1809 | ly_errno = 0; |
Michal Vasko | 0a5ae9a | 2016-11-15 11:54:47 +0100 | [diff] [blame] | 1810 | data = lyd_parse_xml(ctx, &xml->child->child, |
| 1811 | 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] | 1812 | | parseroptions); |
Michal Vasko | f45e0c1 | 2018-11-28 08:38:23 +0100 | [diff] [blame] | 1813 | if (ly_errno) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1814 | ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get")); |
| 1815 | return NULL; |
| 1816 | } |
Michal Vasko | f45e0c1 | 2018-11-28 08:38:23 +0100 | [diff] [blame] | 1817 | data_parsed = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1818 | break; |
| 1819 | |
| 1820 | case NC_RPC_GETSCHEMA: |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 1821 | 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] | 1822 | if (!rpc_act) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1823 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1824 | return NULL; |
| 1825 | } |
| 1826 | break; |
| 1827 | |
| 1828 | case NC_RPC_EDIT: |
| 1829 | case NC_RPC_COPY: |
| 1830 | case NC_RPC_DELETE: |
| 1831 | case NC_RPC_LOCK: |
| 1832 | case NC_RPC_UNLOCK: |
| 1833 | case NC_RPC_KILL: |
| 1834 | case NC_RPC_COMMIT: |
| 1835 | case NC_RPC_DISCARD: |
| 1836 | case NC_RPC_CANCEL: |
| 1837 | case NC_RPC_VALIDATE: |
| 1838 | case NC_RPC_SUBSCRIBE: |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 1839 | case NC_RPC_EDITDATA: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1840 | /* there is no output defined */ |
Michal Vasko | 6b281ff | 2019-02-14 10:15:19 +0100 | [diff] [blame] | 1841 | 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] | 1842 | return NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1843 | default: |
| 1844 | ERRINT; |
| 1845 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | data_rpl = malloc(sizeof *data_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1849 | if (!data_rpl) { |
| 1850 | ERRMEM; |
| 1851 | return NULL; |
| 1852 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1853 | data_rpl->type = NC_RPL_DATA; |
Michal Vasko | f45e0c1 | 2018-11-28 08:38:23 +0100 | [diff] [blame] | 1854 | |
| 1855 | ly_errno = 0; |
| 1856 | if (!data_parsed) { |
Michal Vasko | 68b3f29 | 2016-09-16 12:00:32 +0200 | [diff] [blame] | 1857 | 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] | 1858 | rpc_act, NULL); |
Michal Vasko | 6b281ff | 2019-02-14 10:15:19 +0100 | [diff] [blame] | 1859 | if (!ly_errno && !data_rpl->data->child) { |
| 1860 | ERR("An empty data <rpc-reply>."); |
| 1861 | lyd_free_withsiblings(rpc_act); |
| 1862 | lyd_free(data_rpl->data); |
| 1863 | free(data_rpl); |
| 1864 | return NULL; |
| 1865 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1866 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 1867 | /* <get>, <get-config>, <get-data> */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1868 | data_rpl->data = data; |
| 1869 | } |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 1870 | lyd_free_withsiblings(rpc_act); |
Michal Vasko | f45e0c1 | 2018-11-28 08:38:23 +0100 | [diff] [blame] | 1871 | if (ly_errno) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1872 | ERR("Failed to parse <rpc-reply>."); |
| 1873 | free(data_rpl); |
| 1874 | return NULL; |
| 1875 | } |
| 1876 | reply = (struct nc_reply *)data_rpl; |
| 1877 | } |
| 1878 | |
| 1879 | return reply; |
| 1880 | } |
| 1881 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1882 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1883 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1884 | int |
| 1885 | nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 1886 | { |
| 1887 | int sock; |
| 1888 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1889 | if (!address) { |
| 1890 | ERRARG("address"); |
| 1891 | return -1; |
| 1892 | } else if (!port) { |
| 1893 | ERRARG("port"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1894 | return -1; |
| 1895 | } |
| 1896 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1897 | sock = nc_sock_listen_inet(address, port, &client_opts.ka); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1898 | if (sock == -1) { |
| 1899 | return -1; |
| 1900 | } |
| 1901 | |
| 1902 | ++client_opts.ch_bind_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1903 | client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds); |
| 1904 | if (!client_opts.ch_binds) { |
| 1905 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 1906 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1907 | return -1; |
| 1908 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1909 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1910 | client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti); |
| 1911 | if (!client_opts.ch_bind_ti) { |
| 1912 | ERRMEM; |
| 1913 | close(sock); |
| 1914 | return -1; |
| 1915 | } |
| 1916 | client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti; |
| 1917 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1918 | 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] | 1919 | if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) { |
| 1920 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 1921 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1922 | return -1; |
| 1923 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1924 | client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port; |
| 1925 | client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 1926 | client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | int |
| 1932 | nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 1933 | { |
| 1934 | uint32_t i; |
| 1935 | int ret = -1; |
| 1936 | |
| 1937 | if (!address && !port && !ti) { |
| 1938 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 1939 | close(client_opts.ch_binds[i].sock); |
| 1940 | free((char *)client_opts.ch_binds[i].address); |
| 1941 | |
| 1942 | ret = 0; |
| 1943 | } |
| 1944 | free(client_opts.ch_binds); |
| 1945 | client_opts.ch_binds = NULL; |
| 1946 | client_opts.ch_bind_count = 0; |
| 1947 | } else { |
| 1948 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 1949 | if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) |
| 1950 | && (!port || (client_opts.ch_binds[i].port == port)) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1951 | && (!ti || (client_opts.ch_bind_ti[i] == ti))) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1952 | close(client_opts.ch_binds[i].sock); |
| 1953 | free((char *)client_opts.ch_binds[i].address); |
| 1954 | |
| 1955 | --client_opts.ch_bind_count; |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1956 | if (!client_opts.ch_bind_count) { |
| 1957 | free(client_opts.ch_binds); |
| 1958 | client_opts.ch_binds = NULL; |
| 1959 | } else if (i < client_opts.ch_bind_count) { |
| 1960 | memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds); |
| 1961 | client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count]; |
| 1962 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1963 | |
| 1964 | ret = 0; |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | return ret; |
| 1970 | } |
| 1971 | |
| 1972 | API int |
| 1973 | nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session) |
| 1974 | { |
| 1975 | int sock; |
| 1976 | char *host = NULL; |
| 1977 | uint16_t port, idx; |
| 1978 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1979 | if (!client_opts.ch_binds) { |
| 1980 | ERRINIT; |
| 1981 | return -1; |
| 1982 | } else if (!session) { |
| 1983 | ERRARG("session"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1984 | return -1; |
| 1985 | } |
| 1986 | |
| 1987 | sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx); |
| 1988 | |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 1989 | if (sock < 1) { |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1990 | free(host); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1991 | return sock; |
| 1992 | } |
| 1993 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1994 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1995 | if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1996 | *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] | 1997 | } else |
| 1998 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1999 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2000 | if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2001 | *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] | 2002 | } else |
| 2003 | #endif |
| 2004 | { |
Michal Vasko | fee717c | 2016-02-01 13:25:43 +0100 | [diff] [blame] | 2005 | close(sock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2006 | *session = NULL; |
| 2007 | } |
| 2008 | |
| 2009 | free(host); |
| 2010 | |
| 2011 | if (!(*session)) { |
| 2012 | return -1; |
| 2013 | } |
| 2014 | |
| 2015 | return 1; |
| 2016 | } |
| 2017 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2018 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2019 | |
Michal Vasko | 1b2ddc9 | 2017-05-24 08:59:49 +0200 | [diff] [blame] | 2020 | API const char * const * |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 2021 | nc_session_get_cpblts(const struct nc_session *session) |
| 2022 | { |
| 2023 | if (!session) { |
| 2024 | ERRARG("session"); |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | |
Michal Vasko | 1b2ddc9 | 2017-05-24 08:59:49 +0200 | [diff] [blame] | 2028 | return (const char * const *)session->opts.client.cpblts; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | API const char * |
| 2032 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 2033 | { |
| 2034 | int i, len; |
| 2035 | |
| 2036 | if (!session) { |
| 2037 | ERRARG("session"); |
| 2038 | return NULL; |
| 2039 | } else if (!capab) { |
| 2040 | ERRARG("capab"); |
| 2041 | return NULL; |
| 2042 | } |
| 2043 | |
| 2044 | len = strlen(capab); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2045 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
| 2046 | if (!strncmp(session->opts.client.cpblts[i], capab, len)) { |
| 2047 | return session->opts.client.cpblts[i]; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | return NULL; |
| 2052 | } |
| 2053 | |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 2054 | API int |
| 2055 | nc_session_ntf_thread_running(const struct nc_session *session) |
| 2056 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2057 | if (!session || (session->side != NC_CLIENT)) { |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 2058 | ERRARG("session"); |
| 2059 | return 0; |
| 2060 | } |
| 2061 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2062 | return session->opts.client.ntf_tid ? 1 : 0; |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 2063 | } |
| 2064 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 2065 | API void |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 2066 | nc_client_init(void) |
| 2067 | { |
| 2068 | nc_init(); |
| 2069 | } |
| 2070 | |
| 2071 | API void |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 2072 | nc_client_destroy(void) |
| 2073 | { |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 2074 | nc_client_set_schema_searchpath(NULL); |
| 2075 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 2076 | nc_client_ch_del_bind(NULL, 0, 0); |
| 2077 | #endif |
| 2078 | #ifdef NC_ENABLED_SSH |
| 2079 | nc_client_ssh_destroy_opts(); |
| 2080 | #endif |
| 2081 | #ifdef NC_ENABLED_TLS |
| 2082 | nc_client_tls_destroy_opts(); |
| 2083 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 2084 | nc_destroy(); |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2087 | API NC_MSG_TYPE |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 2088 | 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] | 2089 | { |
Michal Vasko | 5a2c716 | 2020-01-30 11:24:17 +0100 | [diff] [blame] | 2090 | struct lyxml_elem *xml = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2091 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 2092 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2093 | if (!session) { |
| 2094 | ERRARG("session"); |
| 2095 | return NC_MSG_ERROR; |
| 2096 | } else if (!rpc) { |
| 2097 | ERRARG("rpc"); |
| 2098 | return NC_MSG_ERROR; |
Michal Vasko | 5a2c716 | 2020-01-30 11:24:17 +0100 | [diff] [blame] | 2099 | } else if (!msgid) { |
| 2100 | ERRARG("msgid"); |
| 2101 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2102 | } else if (!reply) { |
| 2103 | ERRARG("reply"); |
| 2104 | return NC_MSG_ERROR; |
| 2105 | } else if (parseroptions & LYD_OPT_TYPEMASK) { |
| 2106 | ERRARG("parseroptions"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2107 | return NC_MSG_ERROR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 2108 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 2109 | ERR("Session %u: invalid session to receive RPC replies.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2110 | return NC_MSG_ERROR; |
| 2111 | } |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 2112 | parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS); |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 2113 | if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) { |
| 2114 | parseroptions &= LYD_OPT_STRICT; |
| 2115 | } |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 2116 | /* no mechanism to check external dependencies is provided */ |
| 2117 | parseroptions|= LYD_OPT_NOEXTDEPS; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2118 | *reply = NULL; |
| 2119 | |
| 2120 | msgtype = get_msg(session, timeout, msgid, &xml); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2121 | |
Michal Vasko | 5a2c716 | 2020-01-30 11:24:17 +0100 | [diff] [blame] | 2122 | if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) { |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 2123 | *reply = parse_reply(session->ctx, xml, rpc, parseroptions); |
Michal Vasko | 5a2c716 | 2020-01-30 11:24:17 +0100 | [diff] [blame] | 2124 | lyxml_free_withsiblings(session->ctx, xml); |
| 2125 | xml = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2126 | if (!(*reply)) { |
| 2127 | return NC_MSG_ERROR; |
| 2128 | } |
| 2129 | } |
Michal Vasko | 5a2c716 | 2020-01-30 11:24:17 +0100 | [diff] [blame] | 2130 | assert(!xml); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2131 | |
| 2132 | return msgtype; |
| 2133 | } |
| 2134 | |
| 2135 | API NC_MSG_TYPE |
| 2136 | nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif) |
| 2137 | { |
| 2138 | struct lyxml_elem *xml, *ev_time; |
| 2139 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 2140 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2141 | if (!session) { |
| 2142 | ERRARG("session"); |
| 2143 | return NC_MSG_ERROR; |
| 2144 | } else if (!notif) { |
| 2145 | ERRARG("notif"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2146 | return NC_MSG_ERROR; |
| 2147 | } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 2148 | ERR("Session %u: invalid session to receive Notifications.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2149 | return NC_MSG_ERROR; |
| 2150 | } |
| 2151 | |
| 2152 | msgtype = get_msg(session, timeout, 0, &xml); |
| 2153 | |
| 2154 | if (msgtype == NC_MSG_NOTIF) { |
| 2155 | *notif = calloc(1, sizeof **notif); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2156 | if (!*notif) { |
| 2157 | ERRMEM; |
| 2158 | lyxml_free(session->ctx, xml); |
| 2159 | return NC_MSG_ERROR; |
| 2160 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2161 | |
| 2162 | /* eventTime */ |
| 2163 | LY_TREE_FOR(xml->child, ev_time) { |
| 2164 | if (!strcmp(ev_time->name, "eventTime")) { |
| 2165 | (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0); |
| 2166 | /* lyd_parse does not know this element */ |
| 2167 | lyxml_free(session->ctx, ev_time); |
| 2168 | break; |
| 2169 | } |
| 2170 | } |
| 2171 | if (!(*notif)->datetime) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 2172 | ERR("Session %u: notification is missing the \"eventTime\" element.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2173 | goto fail; |
| 2174 | } |
| 2175 | |
| 2176 | /* notification body */ |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 2177 | (*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] | 2178 | | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2179 | lyxml_free(session->ctx, xml); |
| 2180 | xml = NULL; |
| 2181 | if (!(*notif)->tree) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 2182 | ERR("Session %u: failed to parse a new notification.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2183 | goto fail; |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | return msgtype; |
| 2188 | |
| 2189 | fail: |
| 2190 | lydict_remove(session->ctx, (*notif)->datetime); |
| 2191 | lyd_free((*notif)->tree); |
| 2192 | free(*notif); |
| 2193 | *notif = NULL; |
| 2194 | lyxml_free(session->ctx, xml); |
| 2195 | |
| 2196 | return NC_MSG_ERROR; |
| 2197 | } |
| 2198 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2199 | static void * |
| 2200 | nc_recv_notif_thread(void *arg) |
| 2201 | { |
| 2202 | struct nc_ntf_thread_arg *ntarg; |
| 2203 | struct nc_session *session; |
| 2204 | void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif); |
| 2205 | struct nc_notif *notif; |
| 2206 | NC_MSG_TYPE msgtype; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2207 | pthread_t *ntf_tid; |
| 2208 | |
| 2209 | pthread_detach(pthread_self()); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2210 | |
| 2211 | ntarg = (struct nc_ntf_thread_arg *)arg; |
| 2212 | session = ntarg->session; |
| 2213 | notif_clb = ntarg->notif_clb; |
| 2214 | free(ntarg); |
| 2215 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2216 | /* remember our allocated tid, we will be freeing it */ |
| 2217 | ntf_tid = (pthread_t *)session->opts.client.ntf_tid; |
| 2218 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2219 | while (session->opts.client.ntf_tid) { |
Michal vasko | 4e1a36c | 2016-10-05 13:04:37 +0200 | [diff] [blame] | 2220 | msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, ¬if); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2221 | if (msgtype == NC_MSG_NOTIF) { |
| 2222 | notif_clb(session, notif); |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 2223 | if (!strcmp(notif->tree->schema->name, "notificationComplete") |
| 2224 | && !strcmp(notif->tree->schema->module->name, "nc-notifications")) { |
| 2225 | nc_notif_free(notif); |
| 2226 | break; |
| 2227 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2228 | nc_notif_free(notif); |
Michal Vasko | ce32605 | 2018-01-04 10:32:03 +0100 | [diff] [blame] | 2229 | } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) { |
Michal Vasko | 132f7f4 | 2017-09-14 13:40:18 +0200 | [diff] [blame] | 2230 | /* quit this thread once the session is broken */ |
| 2231 | break; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | usleep(NC_CLIENT_NOTIF_THREAD_SLEEP); |
| 2235 | } |
| 2236 | |
Michal Vasko | 0651c90 | 2016-05-19 15:55:42 +0200 | [diff] [blame] | 2237 | VRB("Session %u: notification thread exit.", session->id); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2238 | session->opts.client.ntf_tid = NULL; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2239 | free(ntf_tid); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2240 | return NULL; |
| 2241 | } |
| 2242 | |
| 2243 | API int |
| 2244 | nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif)) |
| 2245 | { |
| 2246 | struct nc_ntf_thread_arg *ntarg; |
| 2247 | int ret; |
| 2248 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2249 | if (!session) { |
| 2250 | ERRARG("session"); |
| 2251 | return -1; |
| 2252 | } else if (!notif_clb) { |
| 2253 | ERRARG("notif_clb"); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2254 | return -1; |
| 2255 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
| 2256 | ERR("Session %u: invalid session to receive Notifications.", session->id); |
| 2257 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2258 | } else if (session->opts.client.ntf_tid) { |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2259 | ERR("Session %u: separate notification thread is already running.", session->id); |
| 2260 | return -1; |
| 2261 | } |
| 2262 | |
| 2263 | ntarg = malloc(sizeof *ntarg); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2264 | if (!ntarg) { |
| 2265 | ERRMEM; |
| 2266 | return -1; |
| 2267 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2268 | ntarg->session = session; |
| 2269 | ntarg->notif_clb = notif_clb; |
| 2270 | |
| 2271 | /* 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] | 2272 | session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid); |
| 2273 | if (!session->opts.client.ntf_tid) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2274 | ERRMEM; |
| 2275 | free(ntarg); |
| 2276 | return -1; |
| 2277 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2278 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2279 | 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] | 2280 | if (ret) { |
| 2281 | ERR("Session %u: failed to create a new thread (%s).", strerror(errno)); |
| 2282 | free(ntarg); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2283 | free((pthread_t *)session->opts.client.ntf_tid); |
| 2284 | session->opts.client.ntf_tid = NULL; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2285 | return -1; |
| 2286 | } |
| 2287 | |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2291 | API NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 2292 | 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] | 2293 | { |
| 2294 | NC_MSG_TYPE r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2295 | int dofree = 1; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 2296 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2297 | struct nc_rpc_getconfig *rpc_gc; |
| 2298 | struct nc_rpc_edit *rpc_e; |
| 2299 | struct nc_rpc_copy *rpc_cp; |
| 2300 | struct nc_rpc_delete *rpc_del; |
| 2301 | struct nc_rpc_lock *rpc_lock; |
| 2302 | struct nc_rpc_get *rpc_g; |
| 2303 | struct nc_rpc_kill *rpc_k; |
| 2304 | struct nc_rpc_commit *rpc_com; |
| 2305 | struct nc_rpc_cancel *rpc_can; |
| 2306 | struct nc_rpc_validate *rpc_val; |
| 2307 | struct nc_rpc_getschema *rpc_gs; |
| 2308 | struct nc_rpc_subscribe *rpc_sub; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2309 | struct nc_rpc_getdata *rpc_getd; |
| 2310 | struct nc_rpc_editdata *rpc_editd; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2311 | struct lyd_node *data, *node; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2312 | const struct lys_module *mod = NULL, *ietfncwd; |
| 2313 | int i; |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 2314 | char str[11]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2315 | uint64_t cur_msgid; |
| 2316 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2317 | if (!session) { |
| 2318 | ERRARG("session"); |
| 2319 | return NC_MSG_ERROR; |
| 2320 | } else if (!rpc) { |
| 2321 | ERRARG("rpc"); |
| 2322 | return NC_MSG_ERROR; |
| 2323 | } else if (!msgid) { |
| 2324 | ERRARG("msgid"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2325 | return NC_MSG_ERROR; |
| 2326 | } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 2327 | ERR("Session %u: invalid session to send RPCs.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2328 | return NC_MSG_ERROR; |
| 2329 | } |
| 2330 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2331 | switch (rpc->type) { |
| 2332 | case NC_RPC_ACT_GENERIC: |
| 2333 | /* checked when parsing */ |
| 2334 | break; |
| 2335 | case NC_RPC_GETCONFIG: |
| 2336 | case NC_RPC_EDIT: |
| 2337 | case NC_RPC_COPY: |
| 2338 | case NC_RPC_DELETE: |
| 2339 | case NC_RPC_LOCK: |
| 2340 | case NC_RPC_UNLOCK: |
| 2341 | case NC_RPC_GET: |
| 2342 | case NC_RPC_KILL: |
| 2343 | case NC_RPC_COMMIT: |
| 2344 | case NC_RPC_DISCARD: |
| 2345 | case NC_RPC_CANCEL: |
| 2346 | case NC_RPC_VALIDATE: |
| 2347 | mod = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1); |
| 2348 | if (!mod) { |
Michal Vasko | 086cd57 | 2017-01-12 12:19:05 +0100 | [diff] [blame] | 2349 | ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2350 | return NC_MSG_ERROR; |
| 2351 | } |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2352 | break; |
| 2353 | case NC_RPC_GETSCHEMA: |
| 2354 | mod = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1); |
| 2355 | if (!mod) { |
| 2356 | ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id); |
| 2357 | return NC_MSG_ERROR; |
| 2358 | } |
| 2359 | break; |
| 2360 | case NC_RPC_SUBSCRIBE: |
| 2361 | mod = ly_ctx_get_module(session->ctx, "notifications", NULL, 1); |
| 2362 | if (!mod) { |
| 2363 | ERR("Session %u: missing \"notifications\" schema in the context.", session->id); |
| 2364 | return NC_MSG_ERROR; |
| 2365 | } |
| 2366 | break; |
| 2367 | case NC_RPC_GETDATA: |
| 2368 | case NC_RPC_EDITDATA: |
| 2369 | mod = ly_ctx_get_module(session->ctx, "ietf-netconf-nmda", NULL, 1); |
| 2370 | if (!mod) { |
| 2371 | ERR("Session %u: missing \"ietf-netconf-nmda\" schema in the context.", session->id); |
| 2372 | return NC_MSG_ERROR; |
| 2373 | } |
| 2374 | break; |
| 2375 | case NC_RPC_UNKNOWN: |
| 2376 | ERRINT; |
| 2377 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 2381 | case NC_RPC_ACT_GENERIC: |
| 2382 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2383 | |
| 2384 | if (rpc_gen->has_data) { |
| 2385 | data = rpc_gen->content.data; |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 2386 | dofree = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2387 | } else { |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 2388 | 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] | 2389 | | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL); |
Michal Vasko | eec410f | 2017-11-24 09:14:55 +0100 | [diff] [blame] | 2390 | if (!data) { |
| 2391 | return NC_MSG_ERROR; |
| 2392 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2393 | } |
| 2394 | break; |
| 2395 | |
| 2396 | case NC_RPC_GETCONFIG: |
| 2397 | rpc_gc = (struct nc_rpc_getconfig *)rpc; |
| 2398 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2399 | data = lyd_new(NULL, mod, "get-config"); |
| 2400 | node = lyd_new(data, mod, "source"); |
| 2401 | node = lyd_new_leaf(node, mod, ncds2str[rpc_gc->source], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2402 | if (!node) { |
| 2403 | lyd_free(data); |
| 2404 | return NC_MSG_ERROR; |
| 2405 | } |
| 2406 | if (rpc_gc->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2407 | if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2408 | 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] | 2409 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2410 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2411 | node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 2412 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 2413 | lyd_insert_attr(node, NULL, "select", rpc_gc->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2414 | } |
| 2415 | if (!node) { |
| 2416 | lyd_free(data); |
| 2417 | return NC_MSG_ERROR; |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | if (rpc_gc->wd_mode) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2422 | 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] | 2423 | if (!ietfncwd) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2424 | ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id); |
| 2425 | lyd_free(data); |
| 2426 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2427 | } |
| 2428 | switch (rpc_gc->wd_mode) { |
| 2429 | case NC_WD_UNKNOWN: |
| 2430 | /* cannot get here */ |
| 2431 | break; |
| 2432 | case NC_WD_ALL: |
| 2433 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 2434 | break; |
| 2435 | case NC_WD_ALL_TAG: |
| 2436 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 2437 | break; |
| 2438 | case NC_WD_TRIM: |
| 2439 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 2440 | break; |
| 2441 | case NC_WD_EXPLICIT: |
| 2442 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 2443 | break; |
| 2444 | } |
| 2445 | if (!node) { |
| 2446 | lyd_free(data); |
| 2447 | return NC_MSG_ERROR; |
| 2448 | } |
| 2449 | } |
| 2450 | break; |
| 2451 | |
| 2452 | case NC_RPC_EDIT: |
| 2453 | rpc_e = (struct nc_rpc_edit *)rpc; |
| 2454 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2455 | data = lyd_new(NULL, mod, "edit-config"); |
| 2456 | node = lyd_new(data, mod, "target"); |
| 2457 | node = lyd_new_leaf(node, mod, ncds2str[rpc_e->target], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2458 | if (!node) { |
| 2459 | lyd_free(data); |
| 2460 | return NC_MSG_ERROR; |
| 2461 | } |
| 2462 | |
| 2463 | if (rpc_e->default_op) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2464 | 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] | 2465 | if (!node) { |
| 2466 | lyd_free(data); |
| 2467 | return NC_MSG_ERROR; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | if (rpc_e->test_opt) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2472 | 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] | 2473 | if (!node) { |
| 2474 | lyd_free(data); |
| 2475 | return NC_MSG_ERROR; |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | if (rpc_e->error_opt) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2480 | 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] | 2481 | if (!node) { |
| 2482 | lyd_free(data); |
| 2483 | return NC_MSG_ERROR; |
| 2484 | } |
| 2485 | } |
| 2486 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2487 | if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2488 | 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] | 2489 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2490 | node = lyd_new_leaf(data, mod, "url", rpc_e->edit_cont); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2491 | } |
| 2492 | if (!node) { |
| 2493 | lyd_free(data); |
| 2494 | return NC_MSG_ERROR; |
| 2495 | } |
| 2496 | break; |
| 2497 | |
| 2498 | case NC_RPC_COPY: |
| 2499 | rpc_cp = (struct nc_rpc_copy *)rpc; |
| 2500 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2501 | data = lyd_new(NULL, mod, "copy-config"); |
| 2502 | node = lyd_new(data, mod, "target"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2503 | if (rpc_cp->url_trg) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2504 | node = lyd_new_leaf(node, mod, "url", rpc_cp->url_trg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2505 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2506 | node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->target], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2507 | } |
| 2508 | if (!node) { |
| 2509 | lyd_free(data); |
| 2510 | return NC_MSG_ERROR; |
| 2511 | } |
| 2512 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2513 | node = lyd_new(data, mod, "source"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2514 | if (rpc_cp->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2515 | 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] | 2516 | 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] | 2517 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2518 | node = lyd_new_leaf(node, mod, "url", rpc_cp->url_config_src); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2519 | } |
| 2520 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2521 | node = lyd_new_leaf(node, mod, ncds2str[rpc_cp->source], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2522 | } |
| 2523 | if (!node) { |
| 2524 | lyd_free(data); |
| 2525 | return NC_MSG_ERROR; |
| 2526 | } |
| 2527 | |
| 2528 | if (rpc_cp->wd_mode) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2529 | 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] | 2530 | if (!ietfncwd) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2531 | ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id); |
| 2532 | lyd_free(data); |
| 2533 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2534 | } |
| 2535 | switch (rpc_cp->wd_mode) { |
| 2536 | case NC_WD_UNKNOWN: |
| 2537 | /* cannot get here */ |
| 2538 | break; |
| 2539 | case NC_WD_ALL: |
| 2540 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 2541 | break; |
| 2542 | case NC_WD_ALL_TAG: |
| 2543 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 2544 | break; |
| 2545 | case NC_WD_TRIM: |
| 2546 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 2547 | break; |
| 2548 | case NC_WD_EXPLICIT: |
| 2549 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 2550 | break; |
| 2551 | } |
| 2552 | if (!node) { |
| 2553 | lyd_free(data); |
| 2554 | return NC_MSG_ERROR; |
| 2555 | } |
| 2556 | } |
| 2557 | break; |
| 2558 | |
| 2559 | case NC_RPC_DELETE: |
| 2560 | rpc_del = (struct nc_rpc_delete *)rpc; |
| 2561 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2562 | data = lyd_new(NULL, mod, "delete-config"); |
| 2563 | node = lyd_new(data, mod, "target"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2564 | if (rpc_del->url) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2565 | node = lyd_new_leaf(node, mod, "url", rpc_del->url); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2566 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2567 | node = lyd_new_leaf(node, mod, ncds2str[rpc_del->target], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2568 | } |
| 2569 | if (!node) { |
| 2570 | lyd_free(data); |
| 2571 | return NC_MSG_ERROR; |
| 2572 | } |
| 2573 | break; |
| 2574 | |
| 2575 | case NC_RPC_LOCK: |
| 2576 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 2577 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2578 | data = lyd_new(NULL, mod, "lock"); |
| 2579 | node = lyd_new(data, mod, "target"); |
| 2580 | node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2581 | if (!node) { |
| 2582 | lyd_free(data); |
| 2583 | return NC_MSG_ERROR; |
| 2584 | } |
| 2585 | break; |
| 2586 | |
| 2587 | case NC_RPC_UNLOCK: |
| 2588 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 2589 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2590 | data = lyd_new(NULL, mod, "unlock"); |
| 2591 | node = lyd_new(data, mod, "target"); |
| 2592 | node = lyd_new_leaf(node, mod, ncds2str[rpc_lock->target], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2593 | if (!node) { |
| 2594 | lyd_free(data); |
| 2595 | return NC_MSG_ERROR; |
| 2596 | } |
| 2597 | break; |
| 2598 | |
| 2599 | case NC_RPC_GET: |
| 2600 | rpc_g = (struct nc_rpc_get *)rpc; |
| 2601 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2602 | data = lyd_new(NULL, mod, "get"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2603 | if (rpc_g->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2604 | if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2605 | 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] | 2606 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2607 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2608 | node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 2609 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 2610 | lyd_insert_attr(node, NULL, "select", rpc_g->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2611 | } |
| 2612 | if (!node) { |
| 2613 | lyd_free(data); |
| 2614 | return NC_MSG_ERROR; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | if (rpc_g->wd_mode) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2619 | 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] | 2620 | if (!ietfncwd) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2621 | ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id); |
| 2622 | lyd_free(data); |
| 2623 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2624 | } |
| 2625 | switch (rpc_g->wd_mode) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2626 | case NC_WD_ALL: |
| 2627 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 2628 | break; |
| 2629 | case NC_WD_ALL_TAG: |
| 2630 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 2631 | break; |
| 2632 | case NC_WD_TRIM: |
| 2633 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 2634 | break; |
| 2635 | case NC_WD_EXPLICIT: |
| 2636 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 2637 | break; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 2638 | default: |
| 2639 | /* cannot get here */ |
| 2640 | node = NULL; |
| 2641 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2642 | } |
| 2643 | if (!node) { |
| 2644 | lyd_free(data); |
| 2645 | return NC_MSG_ERROR; |
| 2646 | } |
| 2647 | } |
| 2648 | break; |
| 2649 | |
| 2650 | case NC_RPC_KILL: |
| 2651 | rpc_k = (struct nc_rpc_kill *)rpc; |
| 2652 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2653 | data = lyd_new(NULL, mod, "kill-session"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2654 | sprintf(str, "%u", rpc_k->sid); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2655 | lyd_new_leaf(data, mod, "session-id", str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2656 | break; |
| 2657 | |
| 2658 | case NC_RPC_COMMIT: |
| 2659 | rpc_com = (struct nc_rpc_commit *)rpc; |
| 2660 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2661 | data = lyd_new(NULL, mod, "commit"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2662 | if (rpc_com->confirmed) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2663 | lyd_new_leaf(data, mod, "confirmed", NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | if (rpc_com->confirm_timeout) { |
| 2667 | sprintf(str, "%u", rpc_com->confirm_timeout); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2668 | lyd_new_leaf(data, mod, "confirm-timeout", str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | if (rpc_com->persist) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2672 | node = lyd_new_leaf(data, mod, "persist", rpc_com->persist); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2673 | if (!node) { |
| 2674 | lyd_free(data); |
| 2675 | return NC_MSG_ERROR; |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | if (rpc_com->persist_id) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2680 | node = lyd_new_leaf(data, mod, "persist-id", rpc_com->persist_id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2681 | if (!node) { |
| 2682 | lyd_free(data); |
| 2683 | return NC_MSG_ERROR; |
| 2684 | } |
| 2685 | } |
| 2686 | break; |
| 2687 | |
| 2688 | case NC_RPC_DISCARD: |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2689 | data = lyd_new(NULL, mod, "discard-changes"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2690 | break; |
| 2691 | |
| 2692 | case NC_RPC_CANCEL: |
| 2693 | rpc_can = (struct nc_rpc_cancel *)rpc; |
| 2694 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2695 | data = lyd_new(NULL, mod, "cancel-commit"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2696 | if (rpc_can->persist_id) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2697 | node = lyd_new_leaf(data, mod, "persist-id", rpc_can->persist_id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2698 | if (!node) { |
| 2699 | lyd_free(data); |
| 2700 | return NC_MSG_ERROR; |
| 2701 | } |
| 2702 | } |
| 2703 | break; |
| 2704 | |
| 2705 | case NC_RPC_VALIDATE: |
| 2706 | rpc_val = (struct nc_rpc_validate *)rpc; |
| 2707 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2708 | data = lyd_new(NULL, mod, "validate"); |
| 2709 | node = lyd_new(data, mod, "source"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2710 | if (rpc_val->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2711 | 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] | 2712 | 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] | 2713 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2714 | node = lyd_new_leaf(node, mod, "url", rpc_val->url_config_src); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2715 | } |
| 2716 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2717 | node = lyd_new_leaf(node, mod, ncds2str[rpc_val->source], NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2718 | } |
| 2719 | if (!node) { |
| 2720 | lyd_free(data); |
| 2721 | return NC_MSG_ERROR; |
| 2722 | } |
| 2723 | break; |
| 2724 | |
| 2725 | case NC_RPC_GETSCHEMA: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2726 | rpc_gs = (struct nc_rpc_getschema *)rpc; |
| 2727 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2728 | data = lyd_new(NULL, mod, "get-schema"); |
| 2729 | node = lyd_new_leaf(data, mod, "identifier", rpc_gs->identifier); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2730 | if (!node) { |
| 2731 | lyd_free(data); |
| 2732 | return NC_MSG_ERROR; |
| 2733 | } |
| 2734 | if (rpc_gs->version) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2735 | node = lyd_new_leaf(data, mod, "version", rpc_gs->version); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2736 | if (!node) { |
| 2737 | lyd_free(data); |
| 2738 | return NC_MSG_ERROR; |
| 2739 | } |
| 2740 | } |
| 2741 | if (rpc_gs->format) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2742 | node = lyd_new_leaf(data, mod, "format", rpc_gs->format); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2743 | if (!node) { |
| 2744 | lyd_free(data); |
| 2745 | return NC_MSG_ERROR; |
| 2746 | } |
| 2747 | } |
| 2748 | break; |
| 2749 | |
| 2750 | case NC_RPC_SUBSCRIBE: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2751 | rpc_sub = (struct nc_rpc_subscribe *)rpc; |
| 2752 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2753 | data = lyd_new(NULL, mod, "create-subscription"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2754 | if (rpc_sub->stream) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2755 | node = lyd_new_leaf(data, mod, "stream", rpc_sub->stream); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2756 | if (!node) { |
| 2757 | lyd_free(data); |
| 2758 | return NC_MSG_ERROR; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | if (rpc_sub->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2763 | if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2764 | 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] | 2765 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2766 | } else { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2767 | node = lyd_new_anydata(data, mod, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 2768 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 2769 | lyd_insert_attr(node, NULL, "select", rpc_sub->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2770 | } |
| 2771 | if (!node) { |
| 2772 | lyd_free(data); |
| 2773 | return NC_MSG_ERROR; |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | if (rpc_sub->start) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2778 | node = lyd_new_leaf(data, mod, "startTime", rpc_sub->start); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2779 | if (!node) { |
| 2780 | lyd_free(data); |
| 2781 | return NC_MSG_ERROR; |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | if (rpc_sub->stop) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2786 | node = lyd_new_leaf(data, mod, "stopTime", rpc_sub->stop); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2787 | if (!node) { |
| 2788 | lyd_free(data); |
| 2789 | return NC_MSG_ERROR; |
| 2790 | } |
| 2791 | } |
| 2792 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2793 | |
| 2794 | case NC_RPC_GETDATA: |
| 2795 | rpc_getd = (struct nc_rpc_getdata *)rpc; |
| 2796 | |
| 2797 | data = lyd_new(NULL, mod, "get-data"); |
| 2798 | node = lyd_new_leaf(data, mod, "datastore", rpc_getd->datastore); |
| 2799 | if (!node) { |
| 2800 | lyd_free(data); |
| 2801 | return NC_MSG_ERROR; |
| 2802 | } |
| 2803 | if (rpc_getd->filter) { |
| 2804 | if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) { |
| 2805 | node = lyd_new_anydata(data, mod, "subtree-filter", rpc_getd->filter, LYD_ANYDATA_SXML); |
| 2806 | } else { |
| 2807 | node = lyd_new_leaf(data, mod, "xpath-filter", rpc_getd->filter); |
| 2808 | } |
| 2809 | if (!node) { |
| 2810 | lyd_free(data); |
| 2811 | return NC_MSG_ERROR; |
| 2812 | } |
| 2813 | } |
| 2814 | if (rpc_getd->config_filter) { |
| 2815 | node = lyd_new_leaf(data, mod, "config-filter", rpc_getd->config_filter); |
| 2816 | if (!node) { |
| 2817 | lyd_free(data); |
| 2818 | return NC_MSG_ERROR; |
| 2819 | } |
| 2820 | } |
| 2821 | for (i = 0; i < rpc_getd->origin_filter_count; ++i) { |
| 2822 | node = lyd_new_leaf(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : "origin-filter", |
| 2823 | rpc_getd->origin_filter[i]); |
| 2824 | if (!node) { |
| 2825 | lyd_free(data); |
| 2826 | return NC_MSG_ERROR; |
| 2827 | } |
| 2828 | } |
| 2829 | if (rpc_getd->max_depth) { |
| 2830 | sprintf(str, "%u", rpc_getd->max_depth); |
| 2831 | node = lyd_new_leaf(data, mod, "max-depth", str); |
| 2832 | if (!node) { |
| 2833 | lyd_free(data); |
| 2834 | return NC_MSG_ERROR; |
| 2835 | } |
| 2836 | } |
| 2837 | if (rpc_getd->with_origin) { |
| 2838 | node = lyd_new_leaf(data, mod, "with-origin", NULL); |
| 2839 | if (!node) { |
| 2840 | lyd_free(data); |
| 2841 | return NC_MSG_ERROR; |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | if (rpc_getd->wd_mode) { |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2846 | switch (rpc_getd->wd_mode) { |
| 2847 | case NC_WD_UNKNOWN: |
| 2848 | /* cannot get here */ |
| 2849 | break; |
| 2850 | case NC_WD_ALL: |
Michal Vasko | c5fa07b | 2020-03-04 08:37:46 +0100 | [diff] [blame] | 2851 | /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */ |
| 2852 | node = lyd_new_leaf(data, mod, "with-defaults", "report-all"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2853 | break; |
| 2854 | case NC_WD_ALL_TAG: |
Michal Vasko | c5fa07b | 2020-03-04 08:37:46 +0100 | [diff] [blame] | 2855 | node = lyd_new_leaf(data, mod, "with-defaults", "report-all-tagged"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2856 | break; |
| 2857 | case NC_WD_TRIM: |
Michal Vasko | c5fa07b | 2020-03-04 08:37:46 +0100 | [diff] [blame] | 2858 | node = lyd_new_leaf(data, mod, "with-defaults", "trim"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2859 | break; |
| 2860 | case NC_WD_EXPLICIT: |
Michal Vasko | c5fa07b | 2020-03-04 08:37:46 +0100 | [diff] [blame] | 2861 | node = lyd_new_leaf(data, mod, "with-defaults", "explicit"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2862 | break; |
| 2863 | } |
| 2864 | if (!node) { |
| 2865 | lyd_free(data); |
| 2866 | return NC_MSG_ERROR; |
| 2867 | } |
| 2868 | } |
| 2869 | break; |
| 2870 | |
| 2871 | case NC_RPC_EDITDATA: |
| 2872 | rpc_editd = (struct nc_rpc_editdata *)rpc; |
| 2873 | |
| 2874 | data = lyd_new(NULL, mod, "edit-data"); |
| 2875 | node = lyd_new_leaf(data, mod, "datastore", rpc_editd->datastore); |
| 2876 | if (!node) { |
| 2877 | lyd_free(data); |
| 2878 | return NC_MSG_ERROR; |
| 2879 | } |
| 2880 | |
| 2881 | if (rpc_editd->default_op) { |
| 2882 | node = lyd_new_leaf(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op]); |
| 2883 | if (!node) { |
| 2884 | lyd_free(data); |
| 2885 | return NC_MSG_ERROR; |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) { |
| 2890 | node = lyd_new_anydata(data, mod, "config", rpc_editd->edit_cont, LYD_ANYDATA_SXML); |
| 2891 | } else { |
| 2892 | node = lyd_new_leaf(data, mod, "url", rpc_editd->edit_cont); |
| 2893 | } |
| 2894 | if (!node) { |
| 2895 | lyd_free(data); |
| 2896 | return NC_MSG_ERROR; |
| 2897 | } |
| 2898 | break; |
| 2899 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 2900 | default: |
| 2901 | ERRINT; |
| 2902 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2903 | } |
| 2904 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2905 | if (!data) { |
| 2906 | /* error was already printed */ |
| 2907 | return NC_MSG_ERROR; |
| 2908 | } |
| 2909 | |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 2910 | if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS |
| 2911 | | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) { |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 2912 | if (dofree) { |
| 2913 | lyd_free(data); |
| 2914 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2915 | return NC_MSG_ERROR; |
| 2916 | } |
| 2917 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2918 | /* send RPC, store its message ID */ |
| 2919 | r = nc_send_msg_io(session, timeout, data); |
| 2920 | cur_msgid = session->opts.client.msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2921 | |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 2922 | if (dofree) { |
| 2923 | lyd_free(data); |
| 2924 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2925 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2926 | if (r == NC_MSG_RPC) { |
| 2927 | *msgid = cur_msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2928 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2929 | return r; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2930 | } |
Michal Vasko | de2946c | 2017-01-12 12:19:26 +0100 | [diff] [blame] | 2931 | |
| 2932 | API void |
| 2933 | nc_client_session_set_not_strict(struct nc_session *session) |
| 2934 | { |
| 2935 | if (session->side != NC_CLIENT) { |
| 2936 | ERRARG("session"); |
| 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | session->flags |= NC_SESSION_CLIENT_NOT_STRICT; |
| 2941 | } |