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