blob: 4d677711c4172b8faab423b562da00efea85a2b2 [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,
431 ly_module_imp_clb user_clb, void *user_data, const struct lys_module **mod)
432{
433 int ret = 0;
434 struct ly_err_item *eitem;
435
436 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
437 if (*mod) {
438 if (implement && !(*mod)->implemented) {
439 /* make the present module implemented */
440 if (lys_set_implemented(*mod)) {
441 ERR("Failed to implement model \"%s\".", (*mod)->name);
442 ret = -1;
443 }
444 }
445 } else if (!(*mod) && implement) {
446 /* missing implemented module, load it ... */
447
448 /* clear all the errors and just collect them for now */
449 ly_err_clean(session->ctx, NULL);
450 ly_log_options(LY_LOSTORE);
451
452 /* 1) using only searchpaths */
453 *mod = ly_ctx_load_module(session->ctx, name, revision);
454
455 /* 2) using user callback */
456 if (!(*mod) && user_clb) {
457 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
458 *mod = ly_ctx_load_module(session->ctx, name, revision);
459 }
460
461 /* 3) using get-schema callback */
462 if (!(*mod)) {
463 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
464 *mod = ly_ctx_load_module(session->ctx, name, revision);
Michal 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
496nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
497{
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 */
534 nc_ctx_load_module(session, name, revision, 1, user_clb, user_data, &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
595nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data)
596{
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 */
612 rpc = nc_rpc_get("/ietf-yang-library:*//.", 0, NC_PARAMTYPE_CONST);
613 if (!rpc) {
614 goto cleanup;
615 }
616
617 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
618 usleep(1000);
619 }
620 if (msg == NC_MSG_ERROR) {
Michal 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")) {
692 ly_set_add(features, (void*)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
693 }
694 }
695
Michal Vasko78cd61f2018-02-22 08:32:24 +0100696 /* continue even on fail */
697 nc_ctx_load_module(session, name, revision, implemented, user_clb, user_data, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200698
Michal Vaskoceae0152018-02-14 16:03:59 +0100699 if (!mod) { /* !mod && !implemented - will be loaded automatically, but remember to set features in the end */
700 assert(!implemented);
Michal Vasko3c556372017-08-09 10:17:34 +0200701 if (imports_flag) {
702 ERR("Module \"%s@%s\" is supposed to be imported, but no other module imports it.",
703 name, revision ? revision : "<latest>");
704 ret = -1;
705 goto cleanup;
706 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200707 ly_set_add(imports, modules->set.d[u], LY_SET_OPT_USEASLIST);
708 continue;
709 }
710
711 if (!mod) {
712 /* all loading ways failed, the schema will be ignored in the received data */
713 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
714 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200715 } else {
716 /* set features - first disable all to enable specified then */
717 lys_features_disable(mod, "*");
718 for (v = 0; v < features->number; v++) {
719 lys_features_enable(mod, (const char*)features->set.g[v]);
720 }
721 }
722 }
723
724 if (!imports_flag && imports->number) {
725 /* even imported modules should be now loaded as dependency, so just go through
726 * the parsing again and just set the features */
727 ly_set_free(modules);
728 modules = imports;
729 imports = NULL;
730 imports_flag = 1;
731 goto parse;
732 }
733
734 /* done */
735 ret = 0;
736
737cleanup:
738 nc_rpc_free(rpc);
739 nc_reply_free(reply);
740 lyd_free_withsiblings(data);
741
742 ly_set_free(modules);
743 ly_set_free(imports);
744 ly_set_free(features);
745
Michal Vasko60f66602017-10-17 13:52:18 +0200746 if (session->status != NC_STATUS_RUNNING) {
747 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
748 ret = -1;
749 }
750
Radek Krejcifd5b6682017-06-13 15:52:53 +0200751 return ret;
752}
753
Michal Vasko086311b2016-01-08 09:53:11 +0100754int
755nc_ctx_check_and_fill(struct nc_session *session)
756{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200757 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100758 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100759 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200760 const struct lys_module *mod = NULL;
761 char *revision;
Michal Vasko086311b2016-01-08 09:53:11 +0100762
Michal Vasko2e6defd2016-10-07 15:48:15 +0200763 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100764
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 /* store the original user's callback, here we will be switching between searchpath, user callback
766 * and get-schema callback */
767 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
768 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); /* unset callback, so we prefer local searchpath */
769
Michal Vasko086311b2016-01-08 09:53:11 +0100770 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200771 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200772 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200773 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200774 if (yanglib_support) {
775 break;
776 }
777 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200778 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200779 if (get_schema_support) {
780 break;
781 }
Michal Vasko086311b2016-01-08 09:53:11 +0100782 }
783 }
Michal Vasko086311b2016-01-08 09:53:11 +0100784 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200785 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100786 if (!lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100787 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200788 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100789 }
790 }
791
792 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200793 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200794 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100795 }
796
Radek Krejcifd5b6682017-06-13 15:52:53 +0200797 if (yanglib_support && get_schema_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200798 /* use get schema to get server's ietf-yang-library */
799 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
800 if (!revision) {
801 WRN("Loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.");
802 WRN("Unable to automatically use <get-schema>.");
803 yanglib_support = 0;
804 } else {
805 revision = strndup(&revision[9], 10);
806 if (nc_ctx_load_module(session, "ietf-yang-library", revision, 1, old_clb, old_data, &mod)) {
807 WRN("Loading NETCONF ietf-yang-library schema failed, unable to automatically use <get-schema>.");
808 yanglib_support = 0;
809 }
810 free(revision);
811 }
812 }
813
814 if (yanglib_support && get_schema_support) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200815 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
816 r = nc_ctx_fill_yl(session, old_clb, old_data);
817 if (r == -1) {
818 goto cleanup;
819 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200820 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 +0200821 /* try to use standard capabilities */
822 goto capabilities;
823 }
824 } else {
825capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100826
Michal Vasko60f66602017-10-17 13:52:18 +0200827 if (nc_ctx_fill_cpblts(session, old_clb, old_data)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200828 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100829 }
Michal Vasko086311b2016-01-08 09:53:11 +0100830 }
831
Radek Krejcifd5b6682017-06-13 15:52:53 +0200832 /* succsess */
833 ret = 0;
834
Michal Vaskoeee99412016-11-21 10:19:43 +0100835 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
836 WRN("Some models failed to be loaded, any data from these models (and any other unknown) will be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +0100837 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200838
839cleanup:
840 /* set user callback back */
841 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
842
Michal Vaskoef578332016-01-25 13:20:09 +0100843 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100844}
845
846API struct nc_session *
847nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
848{
Michal Vaskod083db62016-01-19 10:31:29 +0100849 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100850
Michal Vasko45e53ae2016-04-07 11:46:03 +0200851 if (fdin < 0) {
852 ERRARG("fdin");
853 return NULL;
854 } else if (fdout < 0) {
855 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100856 return NULL;
857 }
858
859 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100860 session = nc_new_session(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100861 if (!session) {
862 ERRMEM;
863 return NULL;
864 }
865 session->status = NC_STATUS_STARTING;
866 session->side = NC_CLIENT;
867
868 /* transport specific data */
869 session->ti_type = NC_TI_FD;
Michal Vaskoade892d2017-02-22 13:40:35 +0100870 pthread_mutex_init(session->ti_lock, NULL);
871 pthread_cond_init(session->ti_cond, NULL);
872 *session->ti_inuse = 0;
873
Michal Vasko086311b2016-01-08 09:53:11 +0100874 session->ti.fd.in = fdin;
875 session->ti.fd.out = fdout;
876
877 /* assign context (dicionary needed for handshake) */
878 if (!ctx) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100879 ctx = ly_ctx_new(NC_SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100880 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200881 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100882 /* that's just it */
883 goto fail;
884 }
Michal Vasko086311b2016-01-08 09:53:11 +0100885 } else {
886 session->flags |= NC_SESSION_SHAREDCTX;
887 }
888 session->ctx = ctx;
889
890 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200891 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100892 goto fail;
893 }
894 session->status = NC_STATUS_RUNNING;
895
Michal Vaskoef578332016-01-25 13:20:09 +0100896 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100897 goto fail;
898 }
899
900 return session;
901
902fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100903 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100904 return NULL;
905}
906
907int
Michal Vaskof05562c2016-01-20 12:06:43 +0100908nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100909{
Michal Vasko0190bc32016-03-02 15:47:49 +0100910 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100911 struct addrinfo hints, *res_list, *res;
912 char port_s[6]; /* length of string representation of short int */
913
914 snprintf(port_s, 6, "%u", port);
915
916 /* Connect to a server */
917 memset(&hints, 0, sizeof hints);
918 hints.ai_family = AF_UNSPEC;
919 hints.ai_socktype = SOCK_STREAM;
920 hints.ai_protocol = IPPROTO_TCP;
921 i = getaddrinfo(host, port_s, &hints, &res_list);
922 if (i != 0) {
923 ERR("Unable to translate the host address (%s).", gai_strerror(i));
924 return -1;
925 }
926
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200927 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100928 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
929 if (sock == -1) {
930 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200931 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100932 }
933
934 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
935 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100936 close(sock);
937 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200938 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100939 }
940
Michal Vasko0190bc32016-03-02 15:47:49 +0100941 /* make the socket non-blocking */
942 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
943 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100944 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200945 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100946 return -1;
947 }
948
Michal Vasko7d965dc2018-03-12 14:39:23 +0100949 /* enable keep-alive */
950 i = 1;
951 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof i) == -1) {
952 ERR("Setsockopt failed (%s).", strerror(errno));
953 close(sock);
954 freeaddrinfo(res_list);
955 return -1;
956 }
957
Michal Vasko086311b2016-01-08 09:53:11 +0100958 /* we're done, network connection established */
959 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100960 }
961
Michal Vasko29af44b2016-10-13 10:59:55 +0200962 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100963 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 +0100964 }
965 freeaddrinfo(res_list);
966
967 return sock;
968}
969
Michal Vasko086311b2016-01-08 09:53:11 +0100970static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100971get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100972{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100973 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100974 char *ptr;
975 const char *str_msgid;
976 uint64_t cur_msgid;
977 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100978 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100979 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
980
Michal Vaskoade892d2017-02-22 13:40:35 +0100981 r = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100982 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100983 /* error */
984 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100985 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100986 /* timeout */
987 return NC_MSG_WOULDBLOCK;
988 }
989
990 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200991 if (!msgid && session->opts.client.notifs) {
992 cont = session->opts.client.notifs;
993 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100994
Michal Vasko71ba2da2016-05-04 10:53:16 +0200995 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100996 free(cont);
997
Michal Vasko71ba2da2016-05-04 10:53:16 +0200998 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100999 }
1000
1001 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001002 if (msgid && session->opts.client.replies) {
1003 cont = session->opts.client.replies;
1004 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001005
Michal Vasko71ba2da2016-05-04 10:53:16 +02001006 xml = cont->msg;
1007 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001008
Michal Vasko71ba2da2016-05-04 10:53:16 +02001009 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001010 }
1011
Michal Vasko71ba2da2016-05-04 10:53:16 +02001012 if (!msgtype) {
1013 /* read message from wire */
1014 msgtype = nc_read_msg_poll(session, timeout, &xml);
1015 }
Michal Vasko086311b2016-01-08 09:53:11 +01001016
1017 /* we read rpc-reply, want a notif */
1018 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001019 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001020 while (*cont_ptr) {
1021 cont_ptr = &((*cont_ptr)->next);
1022 }
1023 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001024 if (!*cont_ptr) {
1025 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001026 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001027 lyxml_free(session->ctx, xml);
1028 return NC_MSG_ERROR;
1029 }
Michal Vasko086311b2016-01-08 09:53:11 +01001030 (*cont_ptr)->msg = xml;
1031 (*cont_ptr)->next = NULL;
1032 }
1033
1034 /* we read notif, want a rpc-reply */
1035 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001036 if (!session->opts.client.ntf_tid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001037 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +01001038 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001039 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001040 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001041 }
Michal Vasko086311b2016-01-08 09:53:11 +01001042
Michal Vasko2e6defd2016-10-07 15:48:15 +02001043 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001044 while (*cont_ptr) {
1045 cont_ptr = &((*cont_ptr)->next);
1046 }
1047 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001048 if (!cont_ptr) {
1049 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001050 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001051 lyxml_free(session->ctx, xml);
1052 return NC_MSG_ERROR;
1053 }
Michal Vasko086311b2016-01-08 09:53:11 +01001054 (*cont_ptr)->msg = xml;
1055 (*cont_ptr)->next = NULL;
1056 }
1057
Michal Vaskoade892d2017-02-22 13:40:35 +01001058 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01001059
1060 switch (msgtype) {
1061 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001062 if (!msgid) {
1063 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001064 }
Michal Vasko086311b2016-01-08 09:53:11 +01001065 break;
1066
1067 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001068 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001069 /* check message-id */
1070 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1071 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001072 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001073 } else {
1074 cur_msgid = strtoul(str_msgid, &ptr, 10);
1075 if (cur_msgid != msgid) {
1076 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1077 session->id, str_msgid);
1078 msgtype = NC_MSG_REPLY_ERR_MSGID;
1079 }
1080 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001081 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001082 }
Michal Vasko086311b2016-01-08 09:53:11 +01001083 break;
1084
1085 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001086 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001087 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001088 msgtype = NC_MSG_ERROR;
1089 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001090
1091 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001092 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001093 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001094 msgtype = NC_MSG_ERROR;
1095 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001096
1097 default:
1098 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1099 * NC_MSG_NONE is not returned by nc_read_msg()
1100 */
1101 break;
1102 }
1103
1104 return msgtype;
1105}
1106
1107/* cannot strictly fail, but does not need to fill any error parameter at all */
1108static void
1109parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1110{
1111 struct lyxml_elem *iter, *next, *info;
1112
1113 LY_TREE_FOR(xml->child, iter) {
1114 if (!iter->ns) {
1115 if (iter->content) {
1116 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1117 } else {
1118 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1119 }
1120 continue;
1121 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1122 if (iter->content) {
1123 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1124 iter->name, iter->content, iter->ns->value);
1125 } else {
1126 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1127 }
1128 continue;
1129 }
1130
1131 if (!strcmp(iter->name, "error-type")) {
1132 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1133 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1134 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1135 } else if (err->type) {
1136 WRN("<rpc-error> <error-type> duplicated.");
1137 } else {
1138 err->type = lydict_insert(ctx, iter->content, 0);
1139 }
1140 } else if (!strcmp(iter->name, "error-tag")) {
1141 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1142 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1143 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1144 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1145 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1146 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1147 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1148 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1149 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1150 && strcmp(iter->content, "malformed-message"))) {
1151 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1152 } else if (err->tag) {
1153 WRN("<rpc-error> <error-tag> duplicated.");
1154 } else {
1155 err->tag = lydict_insert(ctx, iter->content, 0);
1156 }
1157 } else if (!strcmp(iter->name, "error-severity")) {
1158 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1159 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1160 } else if (err->severity) {
1161 WRN("<rpc-error> <error-severity> duplicated.");
1162 } else {
1163 err->severity = lydict_insert(ctx, iter->content, 0);
1164 }
1165 } else if (!strcmp(iter->name, "error-app-tag")) {
1166 if (err->apptag) {
1167 WRN("<rpc-error> <error-app-tag> duplicated.");
1168 } else {
1169 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1170 }
1171 } else if (!strcmp(iter->name, "error-path")) {
1172 if (err->path) {
1173 WRN("<rpc-error> <error-path> duplicated.");
1174 } else {
1175 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1176 }
1177 } else if (!strcmp(iter->name, "error-message")) {
1178 if (err->message) {
1179 WRN("<rpc-error> <error-message> duplicated.");
1180 } else {
1181 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001182 if (err->message_lang) {
1183 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1184 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001185 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1186 }
1187 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1188 }
1189 } else if (!strcmp(iter->name, "error-info")) {
1190 LY_TREE_FOR_SAFE(iter->child, next, info) {
1191 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1192 if (!strcmp(info->name, "session-id")) {
1193 if (err->sid) {
1194 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1195 } else {
1196 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1197 }
Michal Vasko630485f2018-01-03 14:28:32 +01001198 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001199 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001200 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1201 if (!err->attr) {
1202 ERRMEM;
1203 return;
1204 }
Michal Vasko086311b2016-01-08 09:53:11 +01001205 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1206 } else if (!strcmp(info->name, "bad-element")) {
1207 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001208 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1209 if (!err->elem) {
1210 ERRMEM;
1211 return;
1212 }
Michal Vasko086311b2016-01-08 09:53:11 +01001213 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1214 } else if (!strcmp(info->name, "bad-namespace")) {
1215 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001216 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1217 if (!err->ns) {
1218 ERRMEM;
1219 return;
1220 }
Michal Vasko086311b2016-01-08 09:53:11 +01001221 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1222 } else {
1223 if (info->content) {
1224 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1225 info->name, info->content);
1226 } else {
1227 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1228 }
1229 }
1230 } else {
1231 lyxml_unlink(ctx, info);
1232 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001233 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1234 if (!err->other) {
1235 ERRMEM;
1236 return;
1237 }
Michal Vasko086311b2016-01-08 09:53:11 +01001238 err->other[err->other_count - 1] = info;
1239 }
1240 }
1241 } else {
1242 if (iter->content) {
1243 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1244 } else {
1245 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1246 }
1247 }
1248 }
1249}
1250
1251static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001252parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001253{
1254 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001255 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001256 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001257 struct nc_reply_data *data_rpl;
1258 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001259 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001260 int i;
1261
1262 if (!xml->child) {
1263 ERR("An empty <rpc-reply>.");
1264 return NULL;
1265 }
1266
1267 /* rpc-error */
1268 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1269 /* count and check elements */
1270 i = 0;
1271 LY_TREE_FOR(xml->child, iter) {
1272 if (strcmp(iter->name, "rpc-error")) {
1273 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1274 return NULL;
1275 } else if (!iter->ns) {
1276 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1277 return NULL;
1278 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1279 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1280 return NULL;
1281 }
1282 ++i;
1283 }
1284
1285 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001286 if (!error_rpl) {
1287 ERRMEM;
1288 return NULL;
1289 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001290 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001291 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001292 if (!error_rpl->err) {
1293 ERRMEM;
1294 free(error_rpl);
1295 return NULL;
1296 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001297 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001298 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001299 reply = (struct nc_reply *)error_rpl;
1300
1301 i = 0;
1302 LY_TREE_FOR(xml->child, iter) {
1303 parse_rpc_error(ctx, iter, error_rpl->err + i);
1304 ++i;
1305 }
1306
1307 /* ok */
1308 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1309 if (xml->child->next) {
1310 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1311 return NULL;
1312 }
1313 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001314 if (!reply) {
1315 ERRMEM;
1316 return NULL;
1317 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001318 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001319
1320 /* some RPC output */
1321 } else {
1322 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001323 case NC_RPC_ACT_GENERIC:
1324 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001325
1326 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +02001327 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +01001328 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001329 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 +02001330 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001331 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001332 return NULL;
1333 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001334 }
Michal Vasko086311b2016-01-08 09:53:11 +01001335 break;
1336
1337 case NC_RPC_GETCONFIG:
1338 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001339 if (!xml->child->child) {
1340 /* we did not receive any data */
1341 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001342 if (!data_rpl) {
1343 ERRMEM;
1344 return NULL;
1345 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001346 data_rpl->type = NC_RPL_DATA;
1347 data_rpl->data = NULL;
1348 return (struct nc_reply *)data_rpl;
1349 }
1350
Michal Vasko086311b2016-01-08 09:53:11 +01001351 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001352 data = lyd_parse_xml(ctx, &xml->child->child,
1353 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001354 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001355 if (!data) {
1356 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1357 return NULL;
1358 }
1359 break;
1360
1361 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001362 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001363 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001364 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001365 return NULL;
1366 }
1367 break;
1368
1369 case NC_RPC_EDIT:
1370 case NC_RPC_COPY:
1371 case NC_RPC_DELETE:
1372 case NC_RPC_LOCK:
1373 case NC_RPC_UNLOCK:
1374 case NC_RPC_KILL:
1375 case NC_RPC_COMMIT:
1376 case NC_RPC_DISCARD:
1377 case NC_RPC_CANCEL:
1378 case NC_RPC_VALIDATE:
1379 case NC_RPC_SUBSCRIBE:
1380 /* there is no output defined */
1381 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1382 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001383 default:
1384 ERRINT;
1385 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001386 }
1387
1388 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001389 if (!data_rpl) {
1390 ERRMEM;
1391 return NULL;
1392 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001393 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001394 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001395 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001396 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001397 } else {
1398 /* <get>, <get-config> */
1399 data_rpl->data = data;
1400 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001401 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001402 if (!data_rpl->data) {
1403 ERR("Failed to parse <rpc-reply>.");
1404 free(data_rpl);
1405 return NULL;
1406 }
1407 reply = (struct nc_reply *)data_rpl;
1408 }
1409
1410 return reply;
1411}
1412
Radek Krejci53691be2016-02-22 13:58:37 +01001413#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001414
Michal Vasko3031aae2016-01-27 16:07:18 +01001415int
1416nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1417{
1418 int sock;
1419
Michal Vasko45e53ae2016-04-07 11:46:03 +02001420 if (!address) {
1421 ERRARG("address");
1422 return -1;
1423 } else if (!port) {
1424 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001425 return -1;
1426 }
1427
1428 sock = nc_sock_listen(address, port);
1429 if (sock == -1) {
1430 return -1;
1431 }
1432
1433 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001434 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1435 if (!client_opts.ch_binds) {
1436 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001437 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001438 return -1;
1439 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001440
Michal Vasko2e6defd2016-10-07 15:48:15 +02001441 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1442 if (!client_opts.ch_bind_ti) {
1443 ERRMEM;
1444 close(sock);
1445 return -1;
1446 }
1447 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1448
Michal Vasko3031aae2016-01-27 16:07:18 +01001449 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001450 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1451 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001452 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001453 return -1;
1454 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001455 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1456 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001457 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001458
1459 return 0;
1460}
1461
1462int
1463nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1464{
1465 uint32_t i;
1466 int ret = -1;
1467
1468 if (!address && !port && !ti) {
1469 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1470 close(client_opts.ch_binds[i].sock);
1471 free((char *)client_opts.ch_binds[i].address);
1472
1473 ret = 0;
1474 }
1475 free(client_opts.ch_binds);
1476 client_opts.ch_binds = NULL;
1477 client_opts.ch_bind_count = 0;
1478 } else {
1479 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1480 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1481 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001482 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001483 close(client_opts.ch_binds[i].sock);
1484 free((char *)client_opts.ch_binds[i].address);
1485
1486 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001487 if (!client_opts.ch_bind_count) {
1488 free(client_opts.ch_binds);
1489 client_opts.ch_binds = NULL;
1490 } else if (i < client_opts.ch_bind_count) {
1491 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1492 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1493 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001494
1495 ret = 0;
1496 }
1497 }
1498 }
1499
1500 return ret;
1501}
1502
1503API int
1504nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1505{
1506 int sock;
1507 char *host = NULL;
1508 uint16_t port, idx;
1509
Michal Vasko45e53ae2016-04-07 11:46:03 +02001510 if (!client_opts.ch_binds) {
1511 ERRINIT;
1512 return -1;
1513 } else if (!session) {
1514 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001515 return -1;
1516 }
1517
1518 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1519
Michal Vasko50456e82016-02-02 12:16:08 +01001520 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001521 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001522 return sock;
1523 }
1524
Radek Krejci53691be2016-02-22 13:58:37 +01001525#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001526 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001527 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001528 } else
1529#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001530#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001531 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001532 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001533 } else
1534#endif
1535 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001536 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001537 *session = NULL;
1538 }
1539
1540 free(host);
1541
1542 if (!(*session)) {
1543 return -1;
1544 }
1545
1546 return 1;
1547}
1548
Radek Krejci53691be2016-02-22 13:58:37 +01001549#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001550
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001551API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001552nc_session_get_cpblts(const struct nc_session *session)
1553{
1554 if (!session) {
1555 ERRARG("session");
1556 return NULL;
1557 }
1558
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001559 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001560}
1561
1562API const char *
1563nc_session_cpblt(const struct nc_session *session, const char *capab)
1564{
1565 int i, len;
1566
1567 if (!session) {
1568 ERRARG("session");
1569 return NULL;
1570 } else if (!capab) {
1571 ERRARG("capab");
1572 return NULL;
1573 }
1574
1575 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001576 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1577 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1578 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001579 }
1580 }
1581
1582 return NULL;
1583}
1584
Michal Vasko9cd26a82016-05-31 08:58:48 +02001585API int
1586nc_session_ntf_thread_running(const struct nc_session *session)
1587{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001588 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001589 ERRARG("session");
1590 return 0;
1591 }
1592
Michal Vasko2e6defd2016-10-07 15:48:15 +02001593 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001594}
1595
Michal Vaskob7558c52016-02-26 15:04:19 +01001596API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001597nc_client_init(void)
1598{
1599 nc_init();
1600}
1601
1602API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001603nc_client_destroy(void)
1604{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001605 nc_client_set_schema_searchpath(NULL);
1606#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1607 nc_client_ch_del_bind(NULL, 0, 0);
1608#endif
1609#ifdef NC_ENABLED_SSH
1610 nc_client_ssh_destroy_opts();
1611#endif
1612#ifdef NC_ENABLED_TLS
1613 nc_client_tls_destroy_opts();
1614#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001615 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001616}
1617
Michal Vasko086311b2016-01-08 09:53:11 +01001618API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001619nc_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 +01001620{
1621 struct lyxml_elem *xml;
1622 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1623
Michal Vasko45e53ae2016-04-07 11:46:03 +02001624 if (!session) {
1625 ERRARG("session");
1626 return NC_MSG_ERROR;
1627 } else if (!rpc) {
1628 ERRARG("rpc");
1629 return NC_MSG_ERROR;
1630 } else if (!reply) {
1631 ERRARG("reply");
1632 return NC_MSG_ERROR;
1633 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1634 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001635 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001636 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001637 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001638 return NC_MSG_ERROR;
1639 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001640 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001641 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1642 parseroptions &= LYD_OPT_STRICT;
1643 }
Michal Vasko41adf392017-01-17 10:39:04 +01001644 /* no mechanism to check external dependencies is provided */
1645 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001646 *reply = NULL;
1647
1648 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001649
Michal Vasko71ba2da2016-05-04 10:53:16 +02001650 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001651 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001652 lyxml_free(session->ctx, xml);
1653 if (!(*reply)) {
1654 return NC_MSG_ERROR;
1655 }
1656 }
1657
1658 return msgtype;
1659}
1660
1661API NC_MSG_TYPE
1662nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1663{
1664 struct lyxml_elem *xml, *ev_time;
1665 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1666
Michal Vasko45e53ae2016-04-07 11:46:03 +02001667 if (!session) {
1668 ERRARG("session");
1669 return NC_MSG_ERROR;
1670 } else if (!notif) {
1671 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001672 return NC_MSG_ERROR;
1673 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001674 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001675 return NC_MSG_ERROR;
1676 }
1677
1678 msgtype = get_msg(session, timeout, 0, &xml);
1679
1680 if (msgtype == NC_MSG_NOTIF) {
1681 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001682 if (!*notif) {
1683 ERRMEM;
1684 lyxml_free(session->ctx, xml);
1685 return NC_MSG_ERROR;
1686 }
Michal Vasko086311b2016-01-08 09:53:11 +01001687
1688 /* eventTime */
1689 LY_TREE_FOR(xml->child, ev_time) {
1690 if (!strcmp(ev_time->name, "eventTime")) {
1691 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1692 /* lyd_parse does not know this element */
1693 lyxml_free(session->ctx, ev_time);
1694 break;
1695 }
1696 }
1697 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001698 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001699 goto fail;
1700 }
1701
1702 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001703 (*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 +01001704 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001705 lyxml_free(session->ctx, xml);
1706 xml = NULL;
1707 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001708 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001709 goto fail;
1710 }
1711 }
1712
1713 return msgtype;
1714
1715fail:
1716 lydict_remove(session->ctx, (*notif)->datetime);
1717 lyd_free((*notif)->tree);
1718 free(*notif);
1719 *notif = NULL;
1720 lyxml_free(session->ctx, xml);
1721
1722 return NC_MSG_ERROR;
1723}
1724
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001725static void *
1726nc_recv_notif_thread(void *arg)
1727{
1728 struct nc_ntf_thread_arg *ntarg;
1729 struct nc_session *session;
1730 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1731 struct nc_notif *notif;
1732 NC_MSG_TYPE msgtype;
1733
1734 ntarg = (struct nc_ntf_thread_arg *)arg;
1735 session = ntarg->session;
1736 notif_clb = ntarg->notif_clb;
1737 free(ntarg);
1738
Michal Vasko2e6defd2016-10-07 15:48:15 +02001739 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001740 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001741 if (msgtype == NC_MSG_NOTIF) {
1742 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001743 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1744 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1745 nc_notif_free(notif);
1746 break;
1747 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001748 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001749 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001750 /* quit this thread once the session is broken */
1751 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001752 }
1753
1754 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1755 }
1756
Michal Vasko0651c902016-05-19 15:55:42 +02001757 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001758 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001759 return NULL;
1760}
1761
1762API int
1763nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1764{
1765 struct nc_ntf_thread_arg *ntarg;
1766 int ret;
1767
Michal Vasko45e53ae2016-04-07 11:46:03 +02001768 if (!session) {
1769 ERRARG("session");
1770 return -1;
1771 } else if (!notif_clb) {
1772 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001773 return -1;
1774 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1775 ERR("Session %u: invalid session to receive Notifications.", session->id);
1776 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001777 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001778 ERR("Session %u: separate notification thread is already running.", session->id);
1779 return -1;
1780 }
1781
1782 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001783 if (!ntarg) {
1784 ERRMEM;
1785 return -1;
1786 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001787 ntarg->session = session;
1788 ntarg->notif_clb = notif_clb;
1789
1790 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001791 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1792 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001793 ERRMEM;
1794 free(ntarg);
1795 return -1;
1796 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001797
Michal Vasko2e6defd2016-10-07 15:48:15 +02001798 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001799 if (ret) {
1800 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1801 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001802 free((pthread_t *)session->opts.client.ntf_tid);
1803 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001804 return -1;
1805 }
1806
1807 return 0;
1808}
1809
Michal Vasko086311b2016-01-08 09:53:11 +01001810API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001811nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001812{
1813 NC_MSG_TYPE r;
Radek Krejcib4b19062018-02-07 16:33:06 +01001814 int ret, dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02001815 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001816 struct nc_rpc_getconfig *rpc_gc;
1817 struct nc_rpc_edit *rpc_e;
1818 struct nc_rpc_copy *rpc_cp;
1819 struct nc_rpc_delete *rpc_del;
1820 struct nc_rpc_lock *rpc_lock;
1821 struct nc_rpc_get *rpc_g;
1822 struct nc_rpc_kill *rpc_k;
1823 struct nc_rpc_commit *rpc_com;
1824 struct nc_rpc_cancel *rpc_can;
1825 struct nc_rpc_validate *rpc_val;
1826 struct nc_rpc_getschema *rpc_gs;
1827 struct nc_rpc_subscribe *rpc_sub;
1828 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001829 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001830 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001831 uint64_t cur_msgid;
1832
Michal Vasko45e53ae2016-04-07 11:46:03 +02001833 if (!session) {
1834 ERRARG("session");
1835 return NC_MSG_ERROR;
1836 } else if (!rpc) {
1837 ERRARG("rpc");
1838 return NC_MSG_ERROR;
1839 } else if (!msgid) {
1840 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001841 return NC_MSG_ERROR;
1842 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001843 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001844 return NC_MSG_ERROR;
1845 }
1846
Michal Vasko90e8e692016-07-13 12:27:57 +02001847 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001848 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001849 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001850 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001851 return NC_MSG_ERROR;
1852 }
1853 }
1854
1855 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001856 case NC_RPC_ACT_GENERIC:
1857 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001858
1859 if (rpc_gen->has_data) {
1860 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01001861 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001862 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001863 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 +01001864 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001865 if (!data) {
1866 return NC_MSG_ERROR;
1867 }
Michal Vasko086311b2016-01-08 09:53:11 +01001868 }
1869 break;
1870
1871 case NC_RPC_GETCONFIG:
1872 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1873
1874 data = lyd_new(NULL, ietfnc, "get-config");
1875 node = lyd_new(data, ietfnc, "source");
1876 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1877 if (!node) {
1878 lyd_free(data);
1879 return NC_MSG_ERROR;
1880 }
1881 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001882 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001883 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001884 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001885 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001886 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001887 lyd_insert_attr(node, NULL, "type", "xpath");
1888 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001889 }
1890 if (!node) {
1891 lyd_free(data);
1892 return NC_MSG_ERROR;
1893 }
1894 }
1895
1896 if (rpc_gc->wd_mode) {
1897 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001898 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001899 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001900 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001901 return NC_MSG_ERROR;
1902 }
1903 }
1904 switch (rpc_gc->wd_mode) {
1905 case NC_WD_UNKNOWN:
1906 /* cannot get here */
1907 break;
1908 case NC_WD_ALL:
1909 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1910 break;
1911 case NC_WD_ALL_TAG:
1912 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1913 break;
1914 case NC_WD_TRIM:
1915 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1916 break;
1917 case NC_WD_EXPLICIT:
1918 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1919 break;
1920 }
1921 if (!node) {
1922 lyd_free(data);
1923 return NC_MSG_ERROR;
1924 }
1925 }
1926 break;
1927
1928 case NC_RPC_EDIT:
1929 rpc_e = (struct nc_rpc_edit *)rpc;
1930
1931 data = lyd_new(NULL, ietfnc, "edit-config");
1932 node = lyd_new(data, ietfnc, "target");
1933 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1934 if (!node) {
1935 lyd_free(data);
1936 return NC_MSG_ERROR;
1937 }
1938
1939 if (rpc_e->default_op) {
1940 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1941 if (!node) {
1942 lyd_free(data);
1943 return NC_MSG_ERROR;
1944 }
1945 }
1946
1947 if (rpc_e->test_opt) {
1948 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1949 if (!node) {
1950 lyd_free(data);
1951 return NC_MSG_ERROR;
1952 }
1953 }
1954
1955 if (rpc_e->error_opt) {
1956 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1957 if (!node) {
1958 lyd_free(data);
1959 return NC_MSG_ERROR;
1960 }
1961 }
1962
Michal Vasko7793bc62016-09-16 11:58:41 +02001963 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001964 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001965 } else {
1966 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1967 }
1968 if (!node) {
1969 lyd_free(data);
1970 return NC_MSG_ERROR;
1971 }
1972 break;
1973
1974 case NC_RPC_COPY:
1975 rpc_cp = (struct nc_rpc_copy *)rpc;
1976
1977 data = lyd_new(NULL, ietfnc, "copy-config");
1978 node = lyd_new(data, ietfnc, "target");
1979 if (rpc_cp->url_trg) {
1980 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1981 } else {
1982 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1983 }
1984 if (!node) {
1985 lyd_free(data);
1986 return NC_MSG_ERROR;
1987 }
1988
1989 node = lyd_new(data, ietfnc, "source");
1990 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001991 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001992 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001993 } else {
1994 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1995 }
1996 } else {
1997 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1998 }
1999 if (!node) {
2000 lyd_free(data);
2001 return NC_MSG_ERROR;
2002 }
2003
2004 if (rpc_cp->wd_mode) {
2005 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002006 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002007 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002008 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002009 return NC_MSG_ERROR;
2010 }
2011 }
2012 switch (rpc_cp->wd_mode) {
2013 case NC_WD_UNKNOWN:
2014 /* cannot get here */
2015 break;
2016 case NC_WD_ALL:
2017 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2018 break;
2019 case NC_WD_ALL_TAG:
2020 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2021 break;
2022 case NC_WD_TRIM:
2023 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2024 break;
2025 case NC_WD_EXPLICIT:
2026 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2027 break;
2028 }
2029 if (!node) {
2030 lyd_free(data);
2031 return NC_MSG_ERROR;
2032 }
2033 }
2034 break;
2035
2036 case NC_RPC_DELETE:
2037 rpc_del = (struct nc_rpc_delete *)rpc;
2038
2039 data = lyd_new(NULL, ietfnc, "delete-config");
2040 node = lyd_new(data, ietfnc, "target");
2041 if (rpc_del->url) {
2042 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2043 } else {
2044 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2045 }
2046 if (!node) {
2047 lyd_free(data);
2048 return NC_MSG_ERROR;
2049 }
2050 break;
2051
2052 case NC_RPC_LOCK:
2053 rpc_lock = (struct nc_rpc_lock *)rpc;
2054
2055 data = lyd_new(NULL, ietfnc, "lock");
2056 node = lyd_new(data, ietfnc, "target");
2057 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2058 if (!node) {
2059 lyd_free(data);
2060 return NC_MSG_ERROR;
2061 }
2062 break;
2063
2064 case NC_RPC_UNLOCK:
2065 rpc_lock = (struct nc_rpc_lock *)rpc;
2066
2067 data = lyd_new(NULL, ietfnc, "unlock");
2068 node = lyd_new(data, ietfnc, "target");
2069 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2070 if (!node) {
2071 lyd_free(data);
2072 return NC_MSG_ERROR;
2073 }
2074 break;
2075
2076 case NC_RPC_GET:
2077 rpc_g = (struct nc_rpc_get *)rpc;
2078
2079 data = lyd_new(NULL, ietfnc, "get");
2080 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002081 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002082 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002083 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002084 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002085 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002086 lyd_insert_attr(node, NULL, "type", "xpath");
2087 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002088 }
2089 if (!node) {
2090 lyd_free(data);
2091 return NC_MSG_ERROR;
2092 }
2093 }
2094
2095 if (rpc_g->wd_mode) {
2096 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002097 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002098 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002099 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002100 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002101 return NC_MSG_ERROR;
2102 }
2103 }
2104 switch (rpc_g->wd_mode) {
2105 case NC_WD_UNKNOWN:
2106 /* cannot get here */
2107 break;
2108 case NC_WD_ALL:
2109 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2110 break;
2111 case NC_WD_ALL_TAG:
2112 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2113 break;
2114 case NC_WD_TRIM:
2115 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2116 break;
2117 case NC_WD_EXPLICIT:
2118 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2119 break;
2120 }
2121 if (!node) {
2122 lyd_free(data);
2123 return NC_MSG_ERROR;
2124 }
2125 }
2126 break;
2127
2128 case NC_RPC_KILL:
2129 rpc_k = (struct nc_rpc_kill *)rpc;
2130
2131 data = lyd_new(NULL, ietfnc, "kill-session");
2132 sprintf(str, "%u", rpc_k->sid);
2133 lyd_new_leaf(data, ietfnc, "session-id", str);
2134 break;
2135
2136 case NC_RPC_COMMIT:
2137 rpc_com = (struct nc_rpc_commit *)rpc;
2138
2139 data = lyd_new(NULL, ietfnc, "commit");
2140 if (rpc_com->confirmed) {
2141 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2142 }
2143
2144 if (rpc_com->confirm_timeout) {
2145 sprintf(str, "%u", rpc_com->confirm_timeout);
2146 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2147 }
2148
2149 if (rpc_com->persist) {
2150 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2151 if (!node) {
2152 lyd_free(data);
2153 return NC_MSG_ERROR;
2154 }
2155 }
2156
2157 if (rpc_com->persist_id) {
2158 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2159 if (!node) {
2160 lyd_free(data);
2161 return NC_MSG_ERROR;
2162 }
2163 }
2164 break;
2165
2166 case NC_RPC_DISCARD:
2167 data = lyd_new(NULL, ietfnc, "discard-changes");
2168 break;
2169
2170 case NC_RPC_CANCEL:
2171 rpc_can = (struct nc_rpc_cancel *)rpc;
2172
2173 data = lyd_new(NULL, ietfnc, "cancel-commit");
2174 if (rpc_can->persist_id) {
2175 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2176 if (!node) {
2177 lyd_free(data);
2178 return NC_MSG_ERROR;
2179 }
2180 }
2181 break;
2182
2183 case NC_RPC_VALIDATE:
2184 rpc_val = (struct nc_rpc_validate *)rpc;
2185
2186 data = lyd_new(NULL, ietfnc, "validate");
2187 node = lyd_new(data, ietfnc, "source");
2188 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002189 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002190 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002191 } else {
2192 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2193 }
2194 } else {
2195 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2196 }
2197 if (!node) {
2198 lyd_free(data);
2199 return NC_MSG_ERROR;
2200 }
2201 break;
2202
2203 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002204 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002205 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002206 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002207 return NC_MSG_ERROR;
2208 }
2209
2210 rpc_gs = (struct nc_rpc_getschema *)rpc;
2211
2212 data = lyd_new(NULL, ietfncmon, "get-schema");
2213 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2214 if (!node) {
2215 lyd_free(data);
2216 return NC_MSG_ERROR;
2217 }
2218 if (rpc_gs->version) {
2219 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2220 if (!node) {
2221 lyd_free(data);
2222 return NC_MSG_ERROR;
2223 }
2224 }
2225 if (rpc_gs->format) {
2226 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2227 if (!node) {
2228 lyd_free(data);
2229 return NC_MSG_ERROR;
2230 }
2231 }
2232 break;
2233
2234 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002235 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002236 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002237 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002238 return NC_MSG_ERROR;
2239 }
2240
2241 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2242
2243 data = lyd_new(NULL, notifs, "create-subscription");
2244 if (rpc_sub->stream) {
2245 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2246 if (!node) {
2247 lyd_free(data);
2248 return NC_MSG_ERROR;
2249 }
2250 }
2251
2252 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002253 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002254 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002255 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002256 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002257 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002258 lyd_insert_attr(node, NULL, "type", "xpath");
2259 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002260 }
2261 if (!node) {
2262 lyd_free(data);
2263 return NC_MSG_ERROR;
2264 }
2265 }
2266
2267 if (rpc_sub->start) {
2268 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2269 if (!node) {
2270 lyd_free(data);
2271 return NC_MSG_ERROR;
2272 }
2273 }
2274
2275 if (rpc_sub->stop) {
2276 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2277 if (!node) {
2278 lyd_free(data);
2279 return NC_MSG_ERROR;
2280 }
2281 }
2282 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002283 default:
2284 ERRINT;
2285 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002286 }
2287
Michal Vasko41adf392017-01-17 10:39:04 +01002288 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2289 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002290 if (dofree) {
2291 lyd_free(data);
2292 }
Michal Vasko086311b2016-01-08 09:53:11 +01002293 return NC_MSG_ERROR;
2294 }
2295
Michal Vaskoade892d2017-02-22 13:40:35 +01002296 ret = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002297 if (ret == -1) {
2298 /* error */
2299 r = NC_MSG_ERROR;
2300 } else if (!ret) {
2301 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01002302 r = NC_MSG_WOULDBLOCK;
2303 } else {
2304 /* send RPC, store its message ID */
2305 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002306 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002307 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002308 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01002309
Radek Krejcib4b19062018-02-07 16:33:06 +01002310 if (dofree) {
2311 lyd_free(data);
2312 }
Michal Vasko086311b2016-01-08 09:53:11 +01002313
2314 if (r != NC_MSG_RPC) {
2315 return r;
2316 }
2317
2318 *msgid = cur_msgid;
2319 return NC_MSG_RPC;
2320}
Michal Vaskode2946c2017-01-12 12:19:26 +01002321
2322API void
2323nc_client_session_set_not_strict(struct nc_session *session)
2324{
2325 if (session->side != NC_CLIENT) {
2326 ERRARG("session");
2327 return;
2328 }
2329
2330 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2331}