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