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