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