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