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