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