blob: b53dd8592288d77a9403ce6454a478eae4cb84a7 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
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 Krejci9b81f5b2016-02-24 13:14:49 +01008 * 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 Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020026#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#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 Vasko086311b2016-01-08 09:53:11 +010034#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010036#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko80ef5d22016-01-18 09:21:02 +010038static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
39
Radek Krejci62aa0642017-05-25 16:33:49 +020040#ifdef NC_ENABLED_SSH
41int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
42char *sshauth_password(const char *username, const char *hostname, void *priv);
43char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
44char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
45#endif /* NC_ENABLED_SSH */
46
47static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
48static pthread_key_t nc_client_context_key;
49#ifdef __linux__
50static 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 Krejci5cebc6b2017-05-26 13:24:38 +020066 },
Radek Krejci62aa0642017-05-25 16:33:49 +020067#endif /* NC_ENABLED_SSH */
68 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020069 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020070};
71#endif
72
73static void
74nc_client_context_free(void *ptr)
75{
76 struct nc_client_context *c = (struct nc_client_context *)ptr;
77
Radek Krejci5cebc6b2017-05-26 13:24:38 +020078 if (--(c->refcount)) {
79 /* still used */
80 return;
81 }
82
Radek Krejci62aa0642017-05-25 16:33:49 +020083#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 Krejci5cebc6b2017-05-26 13:24:38 +020091 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020092 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
106static void
107nc_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
116struct nc_client_context *
117nc_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 Krejci5cebc6b2017-05-26 13:24:38 +0200134 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200135#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 Vasko086311b2016-01-08 09:53:11 +0100161
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162API void *
163nc_client_get_thread_context(void)
164{
165 return nc_client_context_location();
166}
167
168API void
169nc_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 Krejcifd5b6682017-06-13 15:52:53 +0200191int
192nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
193{
194 /* assign context (dicionary needed for handshake) */
195 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200196 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200197 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 Vaskoff7e3562018-02-15 13:41:22 +0100205 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200206
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 Vasko086311b2016-01-08 09:53:11 +0100218API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100219nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100220{
Michal Vasko3031aae2016-01-27 16:07:18 +0100221 if (client_opts.schema_searchpath) {
222 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 }
Michal Vasko086311b2016-01-08 09:53:11 +0100224
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100225 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100226 client_opts.schema_searchpath = strdup(path);
227 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100228 ERRMEM;
229 return 1;
230 }
231 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100233 }
234
235 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100236}
237
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100238API const char *
239nc_client_get_schema_searchpath(void)
240{
241 return client_opts.schema_searchpath;
242}
243
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244API int
245nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100246{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 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 Vasko086311b2016-01-08 09:53:11 +0100252 }
253
254 return 0;
255}
256
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257API ly_module_imp_clb
258nc_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 Vaskoff7e3562018-02-15 13:41:22 +0100266/* NC_SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100267static int
Michal Vasko1b2ddc92017-05-24 08:59:49 +0200268ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100269{
270 int i;
271 const struct lys_module *ietfnc;
272
Radek Krejci3222b7d2017-09-21 16:04:30 +0200273 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100274 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100275 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
276 if (!ietfnc) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100277 ietfnc = lys_parse_path(ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100278 }
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
311static char *
Radek Krejcifd5b6682017-06-13 15:52:53 +0200312getschema_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200313 void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100314{
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 Vasko998ba412016-09-16 12:00:07 +0200319 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200320 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100321 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200322 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100323 uint64_t msgid;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200324 char *filename = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200325 FILE *output;
Michal Vasko086311b2016-01-08 09:53:11 +0100326
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200327 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200328 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200329 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200330 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200331 }
Michal Vasko086311b2016-01-08 09:53:11 +0100332
333 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
334 usleep(1000);
335 }
336 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100337 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100338 nc_rpc_free(rpc);
339 return NULL;
340 }
341
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200342 do {
Michal Vaskof471fa02017-02-15 10:48:12 +0100343 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200344 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100345 nc_rpc_free(rpc);
346 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100347 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100348 return NULL;
349 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100350 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100351 return NULL;
352 }
353
Michal Vasko998ba412016-09-16 12:00:07 +0200354 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 Vasko05ba9df2016-01-13 14:40:27 +0100374 nc_reply_free(reply);
375 return NULL;
376 }
377
Michal Vasko086311b2016-01-08 09:53:11 +0100378 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200379 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 Krejci539efb62016-08-24 15:05:16 +0200385 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 Vaskob2583f12016-05-12 11:40:23 +0200389 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200390 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 Krejci03438ec2016-09-21 14:12:26 +0200397 case LYD_ANYDATA_JSON:
398 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200399 case LYD_ANYDATA_SXML:
400 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200401 ERRINT;
402 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200403 }
Michal Vasko086311b2016-01-08 09:53:11 +0100404 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100405 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200406 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100407
Radek Krejcifd5b6682017-06-13 15:52:53 +0200408 /* try to store the model_data into local schema repository */
Michal Vasko206ec2d2018-02-23 13:43:48 +0100409 if (model_data && client_opts.schema_searchpath) {
410 if (asprintf(&filename, "%s/%s%s%s.yang", client_opts.schema_searchpath, mod_name,
Michal Vaskof945da52018-02-15 08:45:13 +0100411 mod_rev ? "@" : "", mod_rev ? mod_rev : "") == -1) {
412 ERRMEM;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200413 } else {
Michal Vaskof945da52018-02-15 08:45:13 +0100414 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 Krejcifd5b6682017-06-13 15:52:53 +0200423 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200424 }
425
Michal Vasko086311b2016-01-08 09:53:11 +0100426 return model_data;
427}
428
Michal Vaskoceae0152018-02-14 16:03:59 +0100429static int
430nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, int implement,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200431 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100432{
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 Krejci9aa1e352018-05-16 16:05:42 +0200462 if (has_get_schema && !(*mod)) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100463 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
464 *mod = ly_ctx_load_module(session->ctx, name, revision);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100465 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 Vaskoceae0152018-02-14 16:03:59 +0100473 }
474
475 /* unset callback back to use searchpath */
476 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
477
Michal Vaskodbf7c272018-02-19 09:07:59 +0100478 /* restore logging options, then print errors on definite failure */
479 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100480 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 Vaskodbf7c272018-02-19 09:07:59 +0100487 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100488 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100489 }
490
491 return ret;
492}
493
Michal Vaskoff7e3562018-02-15 13:41:22 +0100494/* NC_SCHEMAS_DIR not used (implicitly) */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200495static int
Radek Krejci9aa1e352018-05-16 16:05:42 +0200496nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200497{
498 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200499 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 Vasko78cd61f2018-02-22 08:32:24 +0100533 /* we can continue even if it fails */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200534 nc_ctx_load_module(session, name, revision, 1, user_clb, user_data, has_get_schema, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200535
536 if (!mod) {
Michal Vasko60f66602017-10-17 13:52:18 +0200537 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 Krejcifd5b6682017-06-13 15:52:53 +0200544 /* 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 Krejcifd5b6682017-06-13 15:52:53 +0200547 } 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
586cleanup:
587 free(name);
588 free(revision);
589 free(features);
590
591 return ret;
592}
593
594static int
Radek Krejci9aa1e352018-05-16 16:05:42 +0200595nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200596{
597 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200598 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 Vaskoac0356e2018-06-22 13:24:29 +0200612 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200613 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 Vasko60f66602017-10-17 13:52:18 +0200621 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200622 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 Vasko09319322017-10-02 09:26:54 +0200659 if (!data_rpl->data || strcmp(data_rpl->data->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200660 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
661 goto cleanup;
662 }
663
Michal Vaskof4c8b792017-07-28 14:57:22 +0200664 modules = lyd_find_path(data_rpl->data, "/ietf-yang-library:modules-state/module");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200665 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
673parse:
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 Vasko689202b2018-04-23 11:26:01 +0200692 ly_set_add(features, (void *)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200693 }
694 }
695
Michal Vasko78cd61f2018-02-22 08:32:24 +0100696 /* continue even on fail */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200697 nc_ctx_load_module(session, name, revision, implemented, user_clb, user_data, has_get_schema, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200698
Michal Vasko663b3d62018-04-23 11:26:18 +0200699 if (!mod && !implemented) { /* will be loaded automatically, but remember to set features in the end */
Michal Vasko3c556372017-08-09 10:17:34 +0200700 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 Krejcifd5b6682017-06-13 15:52:53 +0200706 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 Krejcifd5b6682017-06-13 15:52:53 +0200714 } 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
736cleanup:
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 Vasko60f66602017-10-17 13:52:18 +0200745 if (session->status != NC_STATUS_RUNNING) {
746 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
747 ret = -1;
748 }
749
Radek Krejcifd5b6682017-06-13 15:52:53 +0200750 return ret;
751}
752
Michal Vasko086311b2016-01-08 09:53:11 +0100753int
754nc_ctx_check_and_fill(struct nc_session *session)
755{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200756 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100757 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100758 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200759 const struct lys_module *mod = NULL;
760 char *revision;
Michal Vasko086311b2016-01-08 09:53:11 +0100761
Michal Vasko2e6defd2016-10-07 15:48:15 +0200762 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100763
Radek Krejcifd5b6682017-06-13 15:52:53 +0200764 /* 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 Vasko086311b2016-01-08 09:53:11 +0100769 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200770 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200771 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200772 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200773 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 Krejcib1d250e2018-04-12 13:54:18 +0200777 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200778 if (get_schema_support) {
779 break;
780 }
Michal Vasko086311b2016-01-08 09:53:11 +0100781 }
782 }
Michal Vasko086311b2016-01-08 09:53:11 +0100783 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200784 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100785 if (!lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100786 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200787 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100788 }
789 }
790
791 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200792 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100794 }
795
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796 if (yanglib_support && get_schema_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200797 /* 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 Krejci9aa1e352018-05-16 16:05:42 +0200805 if (nc_ctx_load_module(session, "ietf-yang-library", revision, 1, old_clb, old_data, 1, &mod)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200806 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 Krejci9aa1e352018-05-16 16:05:42 +0200813 if (yanglib_support) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200814 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200815 r = nc_ctx_fill_yl(session, old_clb, old_data, get_schema_support);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200816 if (r == -1) {
817 goto cleanup;
818 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200819 VRB("Session %d: trying to use capabilities instead of ietf-yang-library data.", nc_session_get_id(session));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200820 /* try to use standard capabilities */
821 goto capabilities;
822 }
823 } else {
824capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100825
Radek Krejci9aa1e352018-05-16 16:05:42 +0200826 if (nc_ctx_fill_cpblts(session, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200827 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100828 }
Michal Vasko086311b2016-01-08 09:53:11 +0100829 }
830
Radek Krejcifd5b6682017-06-13 15:52:53 +0200831 /* succsess */
832 ret = 0;
833
Michal Vaskoeee99412016-11-21 10:19:43 +0100834 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 Vaskoef578332016-01-25 13:20:09 +0100836 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200837
838cleanup:
839 /* set user callback back */
840 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
841
Michal Vaskoef578332016-01-25 13:20:09 +0100842 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100843}
844
845API struct nc_session *
846nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
847{
Michal Vaskod083db62016-01-19 10:31:29 +0100848 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100849
Michal Vasko45e53ae2016-04-07 11:46:03 +0200850 if (fdin < 0) {
851 ERRARG("fdin");
852 return NULL;
853 } else if (fdout < 0) {
854 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100855 return NULL;
856 }
857
858 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200859 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100860 if (!session) {
861 ERRMEM;
862 return NULL;
863 }
864 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +0100865
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 Vaskoff7e3562018-02-15 13:41:22 +0100873 ctx = ly_ctx_new(NC_SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100874 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200875 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100876 /* that's just it */
877 goto fail;
878 }
Michal Vasko086311b2016-01-08 09:53:11 +0100879 } else {
880 session->flags |= NC_SESSION_SHAREDCTX;
881 }
882 session->ctx = ctx;
883
884 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200885 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100886 goto fail;
887 }
888 session->status = NC_STATUS_RUNNING;
889
Michal Vaskoef578332016-01-25 13:20:09 +0100890 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100891 goto fail;
892 }
893
894 return session;
895
896fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100897 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100898 return NULL;
899}
900
901int
Michal Vaskof05562c2016-01-20 12:06:43 +0100902nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100903{
Michal Vasko0190bc32016-03-02 15:47:49 +0100904 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100905 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 Vasko1f0b2ac2016-10-13 10:33:50 +0200921 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100922 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
923 if (sock == -1) {
924 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200925 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100926 }
927
928 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
929 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100930 close(sock);
931 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200932 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100933 }
934
Michal Vasko0190bc32016-03-02 15:47:49 +0100935 /* 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 Vasko06c860d2018-07-09 16:08:52 +0200938 goto error;
Michal Vasko7d965dc2018-03-12 14:39:23 +0100939 }
940
Michal Vasko086311b2016-01-08 09:53:11 +0100941 /* we're done, network connection established */
942 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100943 }
944
Michal Vasko29af44b2016-10-13 10:59:55 +0200945 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100946 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko086311b2016-01-08 09:53:11 +0100947 }
948 freeaddrinfo(res_list);
949
950 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +0200951
952error:
953 if (sock > -1) {
954 close(sock);
955 }
956 freeaddrinfo(res_list);
957 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100958}
959
Michal Vasko086311b2016-01-08 09:53:11 +0100960static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100961get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100962{
Michal Vasko086311b2016-01-08 09:53:11 +0100963 char *ptr;
964 const char *str_msgid;
965 uint64_t cur_msgid;
966 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100967 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100968 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
969
Michal Vasko086311b2016-01-08 09:53:11 +0100970 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200971 if (!msgid && session->opts.client.notifs) {
972 cont = session->opts.client.notifs;
973 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100974
Michal Vasko71ba2da2016-05-04 10:53:16 +0200975 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100976 free(cont);
977
Michal Vasko71ba2da2016-05-04 10:53:16 +0200978 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100979 }
980
981 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200982 if (msgid && session->opts.client.replies) {
983 cont = session->opts.client.replies;
984 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100985
Michal Vasko71ba2da2016-05-04 10:53:16 +0200986 xml = cont->msg;
987 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100988
Michal Vasko71ba2da2016-05-04 10:53:16 +0200989 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100990 }
991
Michal Vasko71ba2da2016-05-04 10:53:16 +0200992 if (!msgtype) {
993 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +0200994 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200995 }
Michal Vasko086311b2016-01-08 09:53:11 +0100996
997 /* we read rpc-reply, want a notif */
998 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200999 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001000 while (*cont_ptr) {
1001 cont_ptr = &((*cont_ptr)->next);
1002 }
1003 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001004 if (!*cont_ptr) {
1005 ERRMEM;
1006 lyxml_free(session->ctx, xml);
1007 return NC_MSG_ERROR;
1008 }
Michal Vasko086311b2016-01-08 09:53:11 +01001009 (*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 Vasko3486a7c2017-03-03 13:28:07 +01001015 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001016 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001017 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001018 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001019 }
Michal Vasko086311b2016-01-08 09:53:11 +01001020
Michal Vasko2e6defd2016-10-07 15:48:15 +02001021 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001022 while (*cont_ptr) {
1023 cont_ptr = &((*cont_ptr)->next);
1024 }
1025 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001026 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001027 ERRMEM;
1028 lyxml_free(session->ctx, xml);
1029 return NC_MSG_ERROR;
1030 }
Michal Vasko086311b2016-01-08 09:53:11 +01001031 (*cont_ptr)->msg = xml;
1032 (*cont_ptr)->next = NULL;
1033 }
1034
Michal Vasko086311b2016-01-08 09:53:11 +01001035 switch (msgtype) {
1036 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001037 if (!msgid) {
1038 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001039 }
Michal Vasko086311b2016-01-08 09:53:11 +01001040 break;
1041
1042 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001043 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001044 /* check message-id */
1045 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1046 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001047 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001048 } 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 Vasko2518b6b2016-01-28 13:24:53 +01001056 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001057 }
Michal Vasko086311b2016-01-08 09:53:11 +01001058 break;
1059
1060 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001061 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001062 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001063 msgtype = NC_MSG_ERROR;
1064 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001065
1066 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001067 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001068 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001069 msgtype = NC_MSG_ERROR;
1070 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001071
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 */
1083static void
1084parse_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 Vaskob0222c42018-01-03 15:17:12 +01001157 if (err->message_lang) {
1158 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1159 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001160 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 Vasko630485f2018-01-03 14:28:32 +01001173 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001174 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001175 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1176 if (!err->attr) {
1177 ERRMEM;
1178 return;
1179 }
Michal Vasko086311b2016-01-08 09:53:11 +01001180 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 Vasko4eb3c312016-03-01 14:09:37 +01001183 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1184 if (!err->elem) {
1185 ERRMEM;
1186 return;
1187 }
Michal Vasko086311b2016-01-08 09:53:11 +01001188 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 Vasko4eb3c312016-03-01 14:09:37 +01001191 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1192 if (!err->ns) {
1193 ERRMEM;
1194 return;
1195 }
Michal Vasko086311b2016-01-08 09:53:11 +01001196 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 Vasko4eb3c312016-03-01 14:09:37 +01001208 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1209 if (!err->other) {
1210 ERRMEM;
1211 return;
1212 }
Michal Vasko086311b2016-01-08 09:53:11 +01001213 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
1226static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001227parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001228{
1229 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001230 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001231 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001232 struct nc_reply_data *data_rpl;
1233 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001234 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001235 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 Vasko4eb3c312016-03-01 14:09:37 +01001261 if (!error_rpl) {
1262 ERRMEM;
1263 return NULL;
1264 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001265 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001266 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001267 if (!error_rpl->err) {
1268 ERRMEM;
1269 free(error_rpl);
1270 return NULL;
1271 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001272 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001273 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001274 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 Vasko4eb3c312016-03-01 14:09:37 +01001289 if (!reply) {
1290 ERRMEM;
1291 return NULL;
1292 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001293 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001294
1295 /* some RPC output */
1296 } else {
1297 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001298 case NC_RPC_ACT_GENERIC:
1299 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001300
1301 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001302 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 Vasko086311b2016-01-08 09:53:11 +01001307 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001308 rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vaskoe1708602016-10-18 12:17:22 +02001309 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001310 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001311 return NULL;
1312 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001313 }
Michal Vasko086311b2016-01-08 09:53:11 +01001314 break;
1315
1316 case NC_RPC_GETCONFIG:
1317 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001318 if (!xml->child->child) {
1319 /* we did not receive any data */
1320 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001321 if (!data_rpl) {
1322 ERRMEM;
1323 return NULL;
1324 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001325 data_rpl->type = NC_RPL_DATA;
1326 data_rpl->data = NULL;
1327 return (struct nc_reply *)data_rpl;
1328 }
1329
Michal Vasko086311b2016-01-08 09:53:11 +01001330 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001331 data = lyd_parse_xml(ctx, &xml->child->child,
1332 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001333 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001334 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 Krejci3222b7d2017-09-21 16:04:30 +02001341 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001342 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001343 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001344 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 Vasko7f1c78b2016-01-19 09:52:14 +01001362 default:
1363 ERRINT;
1364 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001365 }
1366
1367 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001368 if (!data_rpl) {
1369 ERRMEM;
1370 return NULL;
1371 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001372 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001373 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001374 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001375 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001376 } else {
1377 /* <get>, <get-config> */
1378 data_rpl->data = data;
1379 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001380 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001381 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 Krejci53691be2016-02-22 13:58:37 +01001392#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001393
Michal Vasko3031aae2016-01-27 16:07:18 +01001394int
1395nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1396{
1397 int sock;
1398
Michal Vasko45e53ae2016-04-07 11:46:03 +02001399 if (!address) {
1400 ERRARG("address");
1401 return -1;
1402 } else if (!port) {
1403 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001404 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 Vasko4eb3c312016-03-01 14:09:37 +01001413 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 Vasko0f74da52016-03-03 08:52:52 +01001416 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001417 return -1;
1418 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001419
Michal Vasko2e6defd2016-10-07 15:48:15 +02001420 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 Vasko3031aae2016-01-27 16:07:18 +01001428 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001429 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1430 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001431 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001432 return -1;
1433 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001434 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 Vasko0a3f3752016-10-13 14:58:38 +02001436 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001437
1438 return 0;
1439}
1440
1441int
1442nc_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 Vasko2e6defd2016-10-07 15:48:15 +02001461 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001462 close(client_opts.ch_binds[i].sock);
1463 free((char *)client_opts.ch_binds[i].address);
1464
1465 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001466 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 Vasko3031aae2016-01-27 16:07:18 +01001473
1474 ret = 0;
1475 }
1476 }
1477 }
1478
1479 return ret;
1480}
1481
1482API int
1483nc_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 Vasko45e53ae2016-04-07 11:46:03 +02001489 if (!client_opts.ch_binds) {
1490 ERRINIT;
1491 return -1;
1492 } else if (!session) {
1493 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001494 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 Vasko50456e82016-02-02 12:16:08 +01001499 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001500 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001501 return sock;
1502 }
1503
Radek Krejci53691be2016-02-22 13:58:37 +01001504#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001505 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001506 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001507 } else
1508#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001509#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001510 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001511 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001512 } else
1513#endif
1514 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001515 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001516 *session = NULL;
1517 }
1518
1519 free(host);
1520
1521 if (!(*session)) {
1522 return -1;
1523 }
1524
1525 return 1;
1526}
1527
Radek Krejci53691be2016-02-22 13:58:37 +01001528#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001529
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001530API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001531nc_session_get_cpblts(const struct nc_session *session)
1532{
1533 if (!session) {
1534 ERRARG("session");
1535 return NULL;
1536 }
1537
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001538 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001539}
1540
1541API const char *
1542nc_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 Vasko2e6defd2016-10-07 15:48:15 +02001555 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 Vaskobdfb5242016-05-24 09:11:01 +02001558 }
1559 }
1560
1561 return NULL;
1562}
1563
Michal Vasko9cd26a82016-05-31 08:58:48 +02001564API int
1565nc_session_ntf_thread_running(const struct nc_session *session)
1566{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001567 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001568 ERRARG("session");
1569 return 0;
1570 }
1571
Michal Vasko2e6defd2016-10-07 15:48:15 +02001572 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001573}
1574
Michal Vaskob7558c52016-02-26 15:04:19 +01001575API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001576nc_client_init(void)
1577{
1578 nc_init();
1579}
1580
1581API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001582nc_client_destroy(void)
1583{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001584 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 Vaskoa7b8ca52016-03-01 12:09:29 +01001594 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001595}
1596
Michal Vasko086311b2016-01-08 09:53:11 +01001597API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001598nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, int parseroptions, struct nc_reply **reply)
Michal Vasko086311b2016-01-08 09:53:11 +01001599{
1600 struct lyxml_elem *xml;
1601 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1602
Michal Vasko45e53ae2016-04-07 11:46:03 +02001603 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 Vasko086311b2016-01-08 09:53:11 +01001614 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001615 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001616 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001617 return NC_MSG_ERROR;
1618 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001619 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001620 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1621 parseroptions &= LYD_OPT_STRICT;
1622 }
Michal Vasko41adf392017-01-17 10:39:04 +01001623 /* no mechanism to check external dependencies is provided */
1624 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001625 *reply = NULL;
1626
1627 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001628
Michal Vasko71ba2da2016-05-04 10:53:16 +02001629 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001630 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001631 lyxml_free(session->ctx, xml);
1632 if (!(*reply)) {
1633 return NC_MSG_ERROR;
1634 }
1635 }
1636
1637 return msgtype;
1638}
1639
1640API NC_MSG_TYPE
1641nc_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 Vasko45e53ae2016-04-07 11:46:03 +02001646 if (!session) {
1647 ERRARG("session");
1648 return NC_MSG_ERROR;
1649 } else if (!notif) {
1650 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001651 return NC_MSG_ERROR;
1652 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001653 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001654 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 Vasko4eb3c312016-03-01 14:09:37 +01001661 if (!*notif) {
1662 ERRMEM;
1663 lyxml_free(session->ctx, xml);
1664 return NC_MSG_ERROR;
1665 }
Michal Vasko086311b2016-01-08 09:53:11 +01001666
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 Vaskod083db62016-01-19 10:31:29 +01001677 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001678 goto fail;
1679 }
1680
1681 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001682 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01001683 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001684 lyxml_free(session->ctx, xml);
1685 xml = NULL;
1686 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001687 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001688 goto fail;
1689 }
1690 }
1691
1692 return msgtype;
1693
1694fail:
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 Vaskoa8ad4482016-01-28 14:25:54 +01001704static void *
1705nc_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 Vasko131120a2018-05-29 15:44:02 +02001712 pthread_t *ntf_tid;
1713
1714 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001715
1716 ntarg = (struct nc_ntf_thread_arg *)arg;
1717 session = ntarg->session;
1718 notif_clb = ntarg->notif_clb;
1719 free(ntarg);
1720
Michal Vasko131120a2018-05-29 15:44:02 +02001721 /* remember our allocated tid, we will be freeing it */
1722 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
1723
Michal Vasko2e6defd2016-10-07 15:48:15 +02001724 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001725 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001726 if (msgtype == NC_MSG_NOTIF) {
1727 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001728 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 Vaskoa8ad4482016-01-28 14:25:54 +01001733 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001734 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001735 /* quit this thread once the session is broken */
1736 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001737 }
1738
1739 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1740 }
1741
Michal Vasko0651c902016-05-19 15:55:42 +02001742 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001743 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02001744 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001745 return NULL;
1746}
1747
1748API int
1749nc_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 Vasko45e53ae2016-04-07 11:46:03 +02001754 if (!session) {
1755 ERRARG("session");
1756 return -1;
1757 } else if (!notif_clb) {
1758 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001759 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 Vasko2e6defd2016-10-07 15:48:15 +02001763 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001764 ERR("Session %u: separate notification thread is already running.", session->id);
1765 return -1;
1766 }
1767
1768 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001769 if (!ntarg) {
1770 ERRMEM;
1771 return -1;
1772 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001773 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 Vasko2e6defd2016-10-07 15:48:15 +02001777 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1778 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001779 ERRMEM;
1780 free(ntarg);
1781 return -1;
1782 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001783
Michal Vasko2e6defd2016-10-07 15:48:15 +02001784 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001785 if (ret) {
1786 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1787 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001788 free((pthread_t *)session->opts.client.ntf_tid);
1789 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001790 return -1;
1791 }
1792
1793 return 0;
1794}
1795
Michal Vasko086311b2016-01-08 09:53:11 +01001796API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001797nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001798{
1799 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02001800 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02001801 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001802 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 Vasko9d8bee62016-03-03 10:58:24 +01001815 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001816 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001817 uint64_t cur_msgid;
1818
Michal Vasko45e53ae2016-04-07 11:46:03 +02001819 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 Vasko086311b2016-01-08 09:53:11 +01001827 return NC_MSG_ERROR;
1828 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001829 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001830 return NC_MSG_ERROR;
1831 }
1832
Michal Vasko90e8e692016-07-13 12:27:57 +02001833 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001834 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001835 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001836 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001837 return NC_MSG_ERROR;
1838 }
1839 }
1840
1841 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001842 case NC_RPC_ACT_GENERIC:
1843 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001844
1845 if (rpc_gen->has_data) {
1846 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01001847 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001848 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001849 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01001850 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001851 if (!data) {
1852 return NC_MSG_ERROR;
1853 }
Michal Vasko086311b2016-01-08 09:53:11 +01001854 }
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 Vaskof3c647b2016-03-08 12:17:33 +01001868 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001869 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001870 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001871 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001872 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001873 lyd_insert_attr(node, NULL, "type", "xpath");
1874 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001875 }
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 Krejci3222b7d2017-09-21 16:04:30 +02001884 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001885 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001886 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001887 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 Vasko7793bc62016-09-16 11:58:41 +02001949 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001950 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001951 } 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 Vasko7793bc62016-09-16 11:58:41 +02001977 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001978 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001979 } 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 Krejci3222b7d2017-09-21 16:04:30 +02001992 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001993 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001994 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001995 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 Vaskof3c647b2016-03-08 12:17:33 +01002067 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002068 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002069 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002070 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002071 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002072 lyd_insert_attr(node, NULL, "type", "xpath");
2073 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002074 }
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 Krejci3222b7d2017-09-21 16:04:30 +02002083 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002084 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002085 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002086 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002087 return NC_MSG_ERROR;
2088 }
2089 }
2090 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002091 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 Vasko2260f552018-06-06 10:06:12 +02002103 default:
2104 /* cannot get here */
2105 node = NULL;
2106 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002107 }
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 Vasko7793bc62016-09-16 11:58:41 +02002176 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002177 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002178 } 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 Krejci3222b7d2017-09-21 16:04:30 +02002191 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002192 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002193 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002194 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 Krejci3222b7d2017-09-21 16:04:30 +02002222 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002223 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002224 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002225 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 Vaskof3c647b2016-03-08 12:17:33 +01002240 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002241 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002242 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002243 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002244 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002245 lyd_insert_attr(node, NULL, "type", "xpath");
2246 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002247 }
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 Vasko7f1c78b2016-01-19 09:52:14 +01002270 default:
2271 ERRINT;
2272 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002273 }
2274
Michal Vasko131120a2018-05-29 15:44:02 +02002275 if (!data) {
2276 /* error was already printed */
2277 return NC_MSG_ERROR;
2278 }
2279
Michal Vasko41adf392017-01-17 10:39:04 +01002280 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2281 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002282 if (dofree) {
2283 lyd_free(data);
2284 }
Michal Vasko086311b2016-01-08 09:53:11 +01002285 return NC_MSG_ERROR;
2286 }
2287
Michal Vasko131120a2018-05-29 15:44:02 +02002288 /* send RPC, store its message ID */
2289 r = nc_send_msg_io(session, timeout, data);
2290 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002291
Radek Krejcib4b19062018-02-07 16:33:06 +01002292 if (dofree) {
2293 lyd_free(data);
2294 }
Michal Vasko086311b2016-01-08 09:53:11 +01002295
Michal Vasko131120a2018-05-29 15:44:02 +02002296 if (r == NC_MSG_RPC) {
2297 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002298 }
Michal Vasko131120a2018-05-29 15:44:02 +02002299 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002300}
Michal Vaskode2946c2017-01-12 12:19:26 +01002301
2302API void
2303nc_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}