blob: 84f44c4755ed12134718016d4dad114cbbd3cd28 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020026#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <sys/types.h>
28#include <unistd.h>
29#include <arpa/inet.h>
30#include <poll.h>
31
32#include <libyang/libyang.h>
33
Michal Vasko086311b2016-01-08 09:53:11 +010034#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010036#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko80ef5d22016-01-18 09:21:02 +010038static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
39
Radek Krejci62aa0642017-05-25 16:33:49 +020040#ifdef NC_ENABLED_SSH
41int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
42char *sshauth_password(const char *username, const char *hostname, void *priv);
43char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
44char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
45#endif /* NC_ENABLED_SSH */
46
47static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
48static pthread_key_t nc_client_context_key;
49#ifdef __linux__
50static struct nc_client_context context_main = {
51 /* .opts zeroed */
52#ifdef NC_ENABLED_SSH
53 .ssh_opts = {
54 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
55 .auth_hostkey_check = sshauth_hostkey_check,
56 .auth_password = sshauth_password,
57 .auth_interactive = sshauth_interactive,
58 .auth_privkey_passphrase = sshauth_privkey_passphrase
59 },
60 .ssh_ch_opts = {
61 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
62 .auth_hostkey_check = sshauth_hostkey_check,
63 .auth_password = sshauth_password,
64 .auth_interactive = sshauth_interactive,
65 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020066 },
Radek Krejci62aa0642017-05-25 16:33:49 +020067#endif /* NC_ENABLED_SSH */
68 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020069 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020070};
71#endif
72
73static void
74nc_client_context_free(void *ptr)
75{
76 struct nc_client_context *c = (struct nc_client_context *)ptr;
77
Radek Krejci5cebc6b2017-05-26 13:24:38 +020078 if (--(c->refcount)) {
79 /* still used */
80 return;
81 }
82
Radek Krejci62aa0642017-05-25 16:33:49 +020083#ifdef __linux__
84 /* in __linux__ we use static memory in the main thread,
85 * so this check is for programs terminating the main()
86 * function by pthread_exit() :)
87 */
88 if (c != &context_main)
89#endif
90 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020091 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020092 nc_client_set_schema_searchpath(NULL);
93#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
94 nc_client_ch_del_bind(NULL, 0, 0);
95#endif
96#ifdef NC_ENABLED_SSH
97 nc_client_ssh_destroy_opts();
98#endif
99#ifdef NC_ENABLED_TLS
100 nc_client_tls_destroy_opts();
101#endif
102 free(c);
103 }
104}
105
106static void
107nc_client_context_createkey(void)
108{
109 int r;
110
111 /* initiate */
112 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
113 pthread_setspecific(nc_client_context_key, NULL);
114}
115
116struct nc_client_context *
117nc_client_context_location(void)
118{
119 struct nc_client_context *e;
120
121 pthread_once(&nc_client_context_once, nc_client_context_createkey);
122 e = pthread_getspecific(nc_client_context_key);
123 if (!e) {
124 /* prepare ly_err storage */
125#ifdef __linux__
126 if (getpid() == syscall(SYS_gettid)) {
127 /* main thread - use global variable instead of thread-specific variable. */
128 e = &context_main;
129 } else
130#endif /* __linux__ */
131 {
132 e = calloc(1, sizeof *e);
133 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200134 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200135#ifdef NC_ENABLED_SSH
136 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
137 e->ssh_opts.auth_pref[0].value = 3;
138 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
139 e->ssh_opts.auth_pref[1].value = 2;
140 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
141 e->ssh_opts.auth_pref[2].value = 1;
142 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
143 e->ssh_opts.auth_password = sshauth_password;
144 e->ssh_opts.auth_interactive = sshauth_interactive;
145 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
146
147 /* callhome settings are the same except the inverted auth methods preferences */
148 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
149 e->ssh_ch_opts.auth_pref[0].value = 1;
150 e->ssh_ch_opts.auth_pref[1].value = 2;
151 e->ssh_ch_opts.auth_pref[2].value = 3;
152#endif /* NC_ENABLED_SSH */
153 }
154 pthread_setspecific(nc_client_context_key, e);
155 }
156
157 return e;
158}
159
160#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100161
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200162API void *
163nc_client_get_thread_context(void)
164{
165 return nc_client_context_location();
166}
167
168API void
169nc_client_set_thread_context(void *context)
170{
171 struct nc_client_context *old, *new;
172
173 if (!context) {
174 ERRARG(context);
175 return;
176 }
177
178 new = (struct nc_client_context *)context;
179 old = nc_client_context_location();
180 if (old == new) {
181 /* nothing to change */
182 return;
183 }
184
185 /* replace old by new, increase reference counter in the newly set context */
186 nc_client_context_free(old);
187 new->refcount++;
188 pthread_setspecific(nc_client_context_key, new);
189}
190
Radek Krejcifd5b6682017-06-13 15:52:53 +0200191int
192nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
193{
194 /* assign context (dicionary needed for handshake) */
195 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200196 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200197 if (!ctx) {
198 return EXIT_FAILURE;
199 }
200
201 /* user path must be first, the first path is used to store schemas retreived via get-schema */
202 if (client_opts.schema_searchpath) {
203 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
204 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100205 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200206
207 /* set callback for getting schemas, if provided */
208 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
209 } else {
210 session->flags |= NC_SESSION_SHAREDCTX;
211 }
212
213 session->ctx = ctx;
214
215 return EXIT_SUCCESS;
216}
217
Michal Vasko086311b2016-01-08 09:53:11 +0100218API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100219nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100220{
Michal Vasko3031aae2016-01-27 16:07:18 +0100221 if (client_opts.schema_searchpath) {
222 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 }
Michal Vasko086311b2016-01-08 09:53:11 +0100224
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100225 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100226 client_opts.schema_searchpath = strdup(path);
227 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100228 ERRMEM;
229 return 1;
230 }
231 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100233 }
234
235 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100236}
237
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100238API const char *
239nc_client_get_schema_searchpath(void)
240{
241 return client_opts.schema_searchpath;
242}
243
Radek Krejcifd5b6682017-06-13 15:52:53 +0200244API int
245nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100246{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 client_opts.schema_clb = clb;
248 if (clb) {
249 client_opts.schema_clb_data = user_data;
250 } else {
251 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 }
253
254 return 0;
255}
256
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257API ly_module_imp_clb
258nc_client_get_schema_callback(void **user_data)
259{
260 if (user_data) {
261 (*user_data) = client_opts.schema_clb_data;
262 }
263 return client_opts.schema_clb;
264}
265
Michal Vaskoff7e3562018-02-15 13:41:22 +0100266/* NC_SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100267static int
Michal Vasko1b2ddc92017-05-24 08:59:49 +0200268ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100269{
270 int i;
271 const struct lys_module *ietfnc;
272
Radek Krejci3222b7d2017-09-21 16:04:30 +0200273 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100274 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100275 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
276 if (!ietfnc) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100277 ietfnc = lys_parse_path(ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100278 }
279 }
280 if (!ietfnc) {
281 ERR("Loading base NETCONF schema failed.");
282 return 1;
283 }
284
285 /* set supported capabilities from ietf-netconf */
286 for (i = 0; cpblts[i]; ++i) {
287 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
288 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
289 lys_features_enable(ietfnc, "writable-running");
290 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
291 lys_features_enable(ietfnc, "candidate");
292 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
293 lys_features_enable(ietfnc, "confirmed-commit");
294 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
295 lys_features_enable(ietfnc, "rollback-on-error");
296 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
297 lys_features_enable(ietfnc, "validate");
298 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
299 lys_features_enable(ietfnc, "startup");
300 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
301 lys_features_enable(ietfnc, "url");
302 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
303 lys_features_enable(ietfnc, "xpath");
304 }
305 }
306 }
307
308 return 0;
309}
310
311static char *
Radek Krejcifd5b6682017-06-13 15:52:53 +0200312getschema_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200313 void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100314{
315 struct nc_session *session = (struct nc_session *)user_data;
316 struct nc_rpc *rpc;
317 struct nc_reply *reply;
318 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200319 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200320 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100321 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200322 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100323 uint64_t msgid;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200324 char *filename = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200325 FILE *output;
Michal Vasko086311b2016-01-08 09:53:11 +0100326
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200327 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200328 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200329 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200330 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200331 }
Michal Vasko086311b2016-01-08 09:53:11 +0100332
333 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
334 usleep(1000);
335 }
336 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100337 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100338 nc_rpc_free(rpc);
339 return NULL;
340 }
341
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200342 do {
Michal Vaskof471fa02017-02-15 10:48:12 +0100343 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200344 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100345 nc_rpc_free(rpc);
346 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100347 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100348 return NULL;
349 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100350 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100351 return NULL;
352 }
353
Michal Vasko998ba412016-09-16 12:00:07 +0200354 switch (reply->type) {
355 case NC_RPL_OK:
356 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
357 nc_reply_free(reply);
358 return NULL;
359 case NC_RPL_DATA:
360 /* fine */
361 break;
362 case NC_RPL_ERROR:
363 error_rpl = (struct nc_reply_error *)reply;
364 if (error_rpl->count) {
365 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
366 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
367 } else {
368 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
369 }
370 nc_reply_free(reply);
371 return NULL;
372 case NC_RPL_NOTIF:
373 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100374 nc_reply_free(reply);
375 return NULL;
376 }
377
Michal Vasko086311b2016-01-08 09:53:11 +0100378 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200379 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
380 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
381 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
382 nc_reply_free(reply);
383 return NULL;
384 }
Radek Krejci539efb62016-08-24 15:05:16 +0200385 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
386 switch (get_schema_data->value_type) {
387 case LYD_ANYDATA_CONSTSTRING:
388 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200389 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200390 break;
391 case LYD_ANYDATA_DATATREE:
392 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
393 break;
394 case LYD_ANYDATA_XML:
395 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
396 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200397 case LYD_ANYDATA_JSON:
398 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200399 case LYD_ANYDATA_SXML:
400 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200401 ERRINT;
402 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200403 }
Michal Vasko086311b2016-01-08 09:53:11 +0100404 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100405 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200406 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100407
Radek Krejcifd5b6682017-06-13 15:52:53 +0200408 /* try to store the model_data into local schema repository */
Michal Vasko206ec2d2018-02-23 13:43:48 +0100409 if (model_data && client_opts.schema_searchpath) {
410 if (asprintf(&filename, "%s/%s%s%s.yang", client_opts.schema_searchpath, mod_name,
Michal Vaskof945da52018-02-15 08:45:13 +0100411 mod_rev ? "@" : "", mod_rev ? mod_rev : "") == -1) {
412 ERRMEM;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200413 } else {
Michal Vaskof945da52018-02-15 08:45:13 +0100414 output = fopen(filename, "w");
415 if (!output) {
416 WRN("Unable to store \"%s\" as a local copy of schema retreived via <get-schema> (%s).",
417 filename, strerror(errno));
418 } else {
419 fputs(model_data, output);
420 fclose(output);
421 }
422 free(filename);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200423 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200424 }
425
Michal Vasko086311b2016-01-08 09:53:11 +0100426 return model_data;
427}
428
Michal Vaskoceae0152018-02-14 16:03:59 +0100429static int
430nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, int implement,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200431 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100432{
433 int ret = 0;
434 struct ly_err_item *eitem;
435
436 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
437 if (*mod) {
438 if (implement && !(*mod)->implemented) {
439 /* make the present module implemented */
440 if (lys_set_implemented(*mod)) {
441 ERR("Failed to implement model \"%s\".", (*mod)->name);
442 ret = -1;
443 }
444 }
445 } else if (!(*mod) && implement) {
446 /* missing implemented module, load it ... */
447
448 /* clear all the errors and just collect them for now */
449 ly_err_clean(session->ctx, NULL);
450 ly_log_options(LY_LOSTORE);
451
452 /* 1) using only searchpaths */
453 *mod = ly_ctx_load_module(session->ctx, name, revision);
454
455 /* 2) using user callback */
456 if (!(*mod) && user_clb) {
457 ly_ctx_set_module_imp_clb(session->ctx, user_clb, user_data);
458 *mod = ly_ctx_load_module(session->ctx, name, revision);
459 }
460
461 /* 3) using get-schema callback */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200462 if (has_get_schema && !(*mod)) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100463 ly_ctx_set_module_imp_clb(session->ctx, &getschema_module_clb, session);
464 *mod = ly_ctx_load_module(session->ctx, name, revision);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100465 if (*mod) {
466 /* print get-schema warning */
467 eitem = ly_err_first(session->ctx);
468 if (eitem && (eitem->prev->level == LY_LLWRN)) {
469 ly_log_options(LY_LOLOG);
470 ly_err_print(eitem->prev);
471 }
472 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100473 }
474
475 /* unset callback back to use searchpath */
476 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
477
Michal Vaskodbf7c272018-02-19 09:07:59 +0100478 /* restore logging options, then print errors on definite failure */
479 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100480 if (!(*mod)) {
481 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
482 ly_err_print(eitem);
483 }
484 ret = -1;
485 }
486
Michal Vaskodbf7c272018-02-19 09:07:59 +0100487 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100488 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100489 }
490
491 return ret;
492}
493
Michal Vaskoff7e3562018-02-15 13:41:22 +0100494/* NC_SCHEMAS_DIR not used (implicitly) */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200495static int
Radek Krejci9aa1e352018-05-16 16:05:42 +0200496nc_ctx_fill_cpblts(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200497{
498 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200499 const struct lys_module *mod;
500 char *ptr, *ptr2;
501 const char *module_cpblt;
502 char *name = NULL, *revision = NULL, *features = NULL;
503 unsigned int u;
504
505 for (u = 0; session->opts.client.cpblts[u]; ++u) {
506 module_cpblt = strstr(session->opts.client.cpblts[u], "module=");
507 /* this capability requires a module */
508 if (!module_cpblt) {
509 continue;
510 }
511
512 /* get module name */
513 ptr = (char *)module_cpblt + 7;
514 ptr2 = strchr(ptr, '&');
515 if (!ptr2) {
516 ptr2 = ptr + strlen(ptr);
517 }
518 free(name);
519 name = strndup(ptr, ptr2 - ptr);
520
521 /* get module revision */
522 free(revision); revision = NULL;
523 ptr = strstr(module_cpblt, "revision=");
524 if (ptr) {
525 ptr += 9;
526 ptr2 = strchr(ptr, '&');
527 if (!ptr2) {
528 ptr2 = ptr + strlen(ptr);
529 }
530 revision = strndup(ptr, ptr2 - ptr);
531 }
532
Michal Vasko78cd61f2018-02-22 08:32:24 +0100533 /* we can continue even if it fails */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200534 nc_ctx_load_module(session, name, revision, 1, user_clb, user_data, has_get_schema, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200535
536 if (!mod) {
Michal Vasko60f66602017-10-17 13:52:18 +0200537 if (session->status != NC_STATUS_RUNNING) {
538 /* something bad heppened, discard the session */
539 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
540 ret = 1;
541 goto cleanup;
542 }
543
Radek Krejcifd5b6682017-06-13 15:52:53 +0200544 /* all loading ways failed, the schema will be ignored in the received data */
545 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
546 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200547 } else {
548 /* set features - first disable all to enable specified then */
549 lys_features_disable(mod, "*");
550
551 ptr = strstr(module_cpblt, "features=");
552 if (ptr) {
553 ptr += 9;
554 ptr2 = strchr(ptr, '&');
555 if (!ptr2) {
556 ptr2 = ptr + strlen(ptr);
557 }
558 free(features);
559 features = strndup(ptr, ptr2 - ptr);
560
561 /* basically manual strtok_r (to avoid macro) */
562 ptr2 = features;
563 for (ptr = features; *ptr; ++ptr) {
564 if (*ptr == ',') {
565 *ptr = '\0';
566 /* remember last feature */
567 ptr2 = ptr + 1;
568 }
569 }
570
571 ptr = features;
572 while (1) {
573 lys_features_enable(mod, ptr);
574 if (ptr != ptr2) {
575 ptr += strlen(ptr) + 1;
576 } else {
577 break;
578 }
579 }
580 }
581 }
582 }
583
584 ret = 0;
585
586cleanup:
587 free(name);
588 free(revision);
589 free(features);
590
591 return ret;
592}
593
594static int
Radek Krejci9aa1e352018-05-16 16:05:42 +0200595nc_ctx_fill_yl(struct nc_session *session, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200596{
597 int ret = 1;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200598 struct nc_rpc *rpc = NULL;
599 struct nc_reply *reply = NULL;
600 struct nc_reply_error *error_rpl;
601 struct nc_reply_data *data_rpl;
602 NC_MSG_TYPE msg;
603 uint64_t msgid;
604 struct lyd_node *data = NULL, *iter;
605 struct ly_set *modules = NULL, *imports = NULL, *features = NULL;
606 unsigned int u, v;
607 const char *name, *revision;
608 int implemented, imports_flag = 0;
609 const struct lys_module *mod;
610
611 /* get yang-library data from the server */
Michal Vaskoac0356e2018-06-22 13:24:29 +0200612 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200613 if (!rpc) {
614 goto cleanup;
615 }
616
617 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
618 usleep(1000);
619 }
620 if (msg == NC_MSG_ERROR) {
Michal Vasko60f66602017-10-17 13:52:18 +0200621 ERR("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200622 session->id);
623 goto cleanup;
624 }
625
626 do {
627 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
628 } while (msg == NC_MSG_NOTIF);
629 if (msg == NC_MSG_WOULDBLOCK) {
630 ERR("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
631 goto cleanup;
632 } else if (msg == NC_MSG_ERROR) {
633 ERR("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
634 goto cleanup;
635 }
636
637 switch (reply->type) {
638 case NC_RPL_OK:
639 ERR("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
640 goto cleanup;
641 case NC_RPL_DATA:
642 /* fine */
643 break;
644 case NC_RPL_ERROR:
645 error_rpl = (struct nc_reply_error *)reply;
646 if (error_rpl->count) {
647 ERR("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
648 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
649 } else {
650 ERR("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
651 }
652 goto cleanup;
653 case NC_RPL_NOTIF:
654 ERR("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
655 goto cleanup;
656 }
657
658 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko09319322017-10-02 09:26:54 +0200659 if (!data_rpl->data || strcmp(data_rpl->data->schema->module->name, "ietf-yang-library")) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200660 ERR("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
661 goto cleanup;
662 }
663
Michal Vaskof4c8b792017-07-28 14:57:22 +0200664 modules = lyd_find_path(data_rpl->data, "/ietf-yang-library:modules-state/module");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200665 if (!modules || !modules->number) {
666 ERR("No yang-library modules information for session %u.", session->id);
667 goto cleanup;
668 }
669
670 features = ly_set_new();
671 imports = ly_set_new();
672
673parse:
674 for (u = modules->number - 1; u < modules->number; u--) {
675 name = revision = NULL;
676 ly_set_clean(features);
677 implemented = 0;
678
679 /* store the data */
680 LY_TREE_FOR(modules->set.d[u]->child, iter) {
681 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
682 /* ignore empty nodes */
683 continue;
684 }
685 if (!strcmp(iter->schema->name, "name")) {
686 name = ((struct lyd_node_leaf_list *)iter)->value_str;
687 } else if (!strcmp(iter->schema->name, "revision")) {
688 revision = ((struct lyd_node_leaf_list *)iter)->value_str;
689 } else if (!strcmp(iter->schema->name, "conformance-type")) {
690 implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
691 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko689202b2018-04-23 11:26:01 +0200692 ly_set_add(features, (void *)((struct lyd_node_leaf_list *)iter)->value_str, LY_SET_OPT_USEASLIST);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200693 }
694 }
695
Michal Vasko78cd61f2018-02-22 08:32:24 +0100696 /* continue even on fail */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200697 nc_ctx_load_module(session, name, revision, implemented, user_clb, user_data, has_get_schema, &mod);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200698
Michal Vasko663b3d62018-04-23 11:26:18 +0200699 if (!mod && !implemented) { /* will be loaded automatically, but remember to set features in the end */
Michal Vasko3c556372017-08-09 10:17:34 +0200700 if (imports_flag) {
701 ERR("Module \"%s@%s\" is supposed to be imported, but no other module imports it.",
702 name, revision ? revision : "<latest>");
703 ret = -1;
704 goto cleanup;
705 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200706 ly_set_add(imports, modules->set.d[u], LY_SET_OPT_USEASLIST);
707 continue;
708 }
709
710 if (!mod) {
711 /* all loading ways failed, the schema will be ignored in the received data */
712 WRN("Failed to load schema \"%s@%s\".", name, revision ? revision : "<latest>");
713 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200714 } else {
715 /* set features - first disable all to enable specified then */
716 lys_features_disable(mod, "*");
717 for (v = 0; v < features->number; v++) {
718 lys_features_enable(mod, (const char*)features->set.g[v]);
719 }
720 }
721 }
722
723 if (!imports_flag && imports->number) {
724 /* even imported modules should be now loaded as dependency, so just go through
725 * the parsing again and just set the features */
726 ly_set_free(modules);
727 modules = imports;
728 imports = NULL;
729 imports_flag = 1;
730 goto parse;
731 }
732
733 /* done */
734 ret = 0;
735
736cleanup:
737 nc_rpc_free(rpc);
738 nc_reply_free(reply);
739 lyd_free_withsiblings(data);
740
741 ly_set_free(modules);
742 ly_set_free(imports);
743 ly_set_free(features);
744
Michal Vasko60f66602017-10-17 13:52:18 +0200745 if (session->status != NC_STATUS_RUNNING) {
746 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
747 ret = -1;
748 }
749
Radek Krejcifd5b6682017-06-13 15:52:53 +0200750 return ret;
751}
752
Michal Vasko086311b2016-01-08 09:53:11 +0100753int
754nc_ctx_check_and_fill(struct nc_session *session)
755{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200756 int i, get_schema_support = 0, yanglib_support = 0, ret = -1, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100757 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100758 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200759 const struct lys_module *mod = NULL;
760 char *revision;
Michal Vasko086311b2016-01-08 09:53:11 +0100761
Michal Vasko2e6defd2016-10-07 15:48:15 +0200762 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100763
Radek Krejcifd5b6682017-06-13 15:52:53 +0200764 /* store the original user's callback, here we will be switching between searchpath, user callback
765 * and get-schema callback */
766 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
767 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); /* unset callback, so we prefer local searchpath */
768
Michal Vasko086311b2016-01-08 09:53:11 +0100769 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200770 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200771 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200772 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200773 if (yanglib_support) {
774 break;
775 }
776 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-yang-library?", 46)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200777 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200778 if (get_schema_support) {
779 break;
780 }
Michal Vasko086311b2016-01-08 09:53:11 +0100781 }
782 }
Michal Vasko086311b2016-01-08 09:53:11 +0100783 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200784 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100785 if (!lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100786 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200787 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100788 }
789 }
790
791 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200792 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200793 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100794 }
795
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796 if (yanglib_support && get_schema_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200797 /* use get schema to get server's ietf-yang-library */
798 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
799 if (!revision) {
800 WRN("Loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.");
801 WRN("Unable to automatically use <get-schema>.");
802 yanglib_support = 0;
803 } else {
804 revision = strndup(&revision[9], 10);
Radek Krejci9aa1e352018-05-16 16:05:42 +0200805 if (nc_ctx_load_module(session, "ietf-yang-library", revision, 1, old_clb, old_data, 1, &mod)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200806 WRN("Loading NETCONF ietf-yang-library schema failed, unable to automatically use <get-schema>.");
807 yanglib_support = 0;
808 }
809 free(revision);
810 }
811 }
812
Radek Krejci9aa1e352018-05-16 16:05:42 +0200813 if (yanglib_support) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200814 /* load schemas according to the ietf-yang-library data, which are more precise than capabilities list */
Radek Krejci9aa1e352018-05-16 16:05:42 +0200815 r = nc_ctx_fill_yl(session, old_clb, old_data, get_schema_support);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200816 if (r == -1) {
817 goto cleanup;
818 } else if (r == 1) {
Michal Vasko60f66602017-10-17 13:52:18 +0200819 VRB("Session %d: trying to use capabilities instead of ietf-yang-library data.", nc_session_get_id(session));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200820 /* try to use standard capabilities */
821 goto capabilities;
822 }
823 } else {
824capabilities:
Michal Vaskoef578332016-01-25 13:20:09 +0100825
Radek Krejci9aa1e352018-05-16 16:05:42 +0200826 if (nc_ctx_fill_cpblts(session, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200827 goto cleanup;
Michal Vaskoef578332016-01-25 13:20:09 +0100828 }
Michal Vasko086311b2016-01-08 09:53:11 +0100829 }
830
Radek Krejcifd5b6682017-06-13 15:52:53 +0200831 /* succsess */
832 ret = 0;
833
Michal Vaskoeee99412016-11-21 10:19:43 +0100834 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
835 WRN("Some models failed to be loaded, any data from these models (and any other unknown) will be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +0100836 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200837
838cleanup:
839 /* set user callback back */
840 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
841
Michal Vaskoef578332016-01-25 13:20:09 +0100842 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100843}
844
845API struct nc_session *
846nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
847{
Michal Vaskod083db62016-01-19 10:31:29 +0100848 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100849
Michal Vasko45e53ae2016-04-07 11:46:03 +0200850 if (fdin < 0) {
851 ERRARG("fdin");
852 return NULL;
853 } else if (fdout < 0) {
854 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100855 return NULL;
856 }
857
858 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200859 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100860 if (!session) {
861 ERRMEM;
862 return NULL;
863 }
864 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +0100865
866 /* transport specific data */
867 session->ti_type = NC_TI_FD;
868 session->ti.fd.in = fdin;
869 session->ti.fd.out = fdout;
870
871 /* assign context (dicionary needed for handshake) */
872 if (!ctx) {
Michal Vaskoff7e3562018-02-15 13:41:22 +0100873 ctx = ly_ctx_new(NC_SCHEMAS_DIR, 0);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100874 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200875 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100876 /* that's just it */
877 goto fail;
878 }
Michal Vasko086311b2016-01-08 09:53:11 +0100879 } else {
880 session->flags |= NC_SESSION_SHAREDCTX;
881 }
882 session->ctx = ctx;
883
884 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200885 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100886 goto fail;
887 }
888 session->status = NC_STATUS_RUNNING;
889
Michal Vaskoef578332016-01-25 13:20:09 +0100890 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100891 goto fail;
892 }
893
894 return session;
895
896fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100897 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100898 return NULL;
899}
900
901int
Michal Vaskof05562c2016-01-20 12:06:43 +0100902nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100903{
Michal Vasko0190bc32016-03-02 15:47:49 +0100904 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100905 struct addrinfo hints, *res_list, *res;
906 char port_s[6]; /* length of string representation of short int */
907
908 snprintf(port_s, 6, "%u", port);
909
910 /* Connect to a server */
911 memset(&hints, 0, sizeof hints);
912 hints.ai_family = AF_UNSPEC;
913 hints.ai_socktype = SOCK_STREAM;
914 hints.ai_protocol = IPPROTO_TCP;
915 i = getaddrinfo(host, port_s, &hints, &res_list);
916 if (i != 0) {
917 ERR("Unable to translate the host address (%s).", gai_strerror(i));
918 return -1;
919 }
920
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200921 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100922 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
923 if (sock == -1) {
924 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200925 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100926 }
927
928 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
929 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100930 close(sock);
931 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200932 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100933 }
934
Michal Vasko0190bc32016-03-02 15:47:49 +0100935 /* make the socket non-blocking */
936 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
937 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100938 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200939 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100940 return -1;
941 }
942
Michal Vasko7d965dc2018-03-12 14:39:23 +0100943 /* enable keep-alive */
944 i = 1;
945 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof i) == -1) {
946 ERR("Setsockopt failed (%s).", strerror(errno));
947 close(sock);
948 freeaddrinfo(res_list);
949 return -1;
950 }
951
Michal Vasko086311b2016-01-08 09:53:11 +0100952 /* we're done, network connection established */
953 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100954 }
955
Michal Vasko29af44b2016-10-13 10:59:55 +0200956 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100957 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 +0100958 }
959 freeaddrinfo(res_list);
960
961 return sock;
962}
963
Michal Vasko086311b2016-01-08 09:53:11 +0100964static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100965get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100966{
Michal Vasko086311b2016-01-08 09:53:11 +0100967 char *ptr;
968 const char *str_msgid;
969 uint64_t cur_msgid;
970 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100971 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100972 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
973
Michal Vasko086311b2016-01-08 09:53:11 +0100974 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200975 if (!msgid && session->opts.client.notifs) {
976 cont = session->opts.client.notifs;
977 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100978
Michal Vasko71ba2da2016-05-04 10:53:16 +0200979 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100980 free(cont);
981
Michal Vasko71ba2da2016-05-04 10:53:16 +0200982 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100983 }
984
985 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200986 if (msgid && session->opts.client.replies) {
987 cont = session->opts.client.replies;
988 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100989
Michal Vasko71ba2da2016-05-04 10:53:16 +0200990 xml = cont->msg;
991 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100992
Michal Vasko71ba2da2016-05-04 10:53:16 +0200993 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100994 }
995
Michal Vasko71ba2da2016-05-04 10:53:16 +0200996 if (!msgtype) {
997 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +0200998 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200999 }
Michal Vasko086311b2016-01-08 09:53:11 +01001000
1001 /* we read rpc-reply, want a notif */
1002 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001003 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001004 while (*cont_ptr) {
1005 cont_ptr = &((*cont_ptr)->next);
1006 }
1007 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001008 if (!*cont_ptr) {
1009 ERRMEM;
1010 lyxml_free(session->ctx, xml);
1011 return NC_MSG_ERROR;
1012 }
Michal Vasko086311b2016-01-08 09:53:11 +01001013 (*cont_ptr)->msg = xml;
1014 (*cont_ptr)->next = NULL;
1015 }
1016
1017 /* we read notif, want a rpc-reply */
1018 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001019 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001020 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001021 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001022 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001023 }
Michal Vasko086311b2016-01-08 09:53:11 +01001024
Michal Vasko2e6defd2016-10-07 15:48:15 +02001025 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001026 while (*cont_ptr) {
1027 cont_ptr = &((*cont_ptr)->next);
1028 }
1029 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001030 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001031 ERRMEM;
1032 lyxml_free(session->ctx, xml);
1033 return NC_MSG_ERROR;
1034 }
Michal Vasko086311b2016-01-08 09:53:11 +01001035 (*cont_ptr)->msg = xml;
1036 (*cont_ptr)->next = NULL;
1037 }
1038
Michal Vasko086311b2016-01-08 09:53:11 +01001039 switch (msgtype) {
1040 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001041 if (!msgid) {
1042 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001043 }
Michal Vasko086311b2016-01-08 09:53:11 +01001044 break;
1045
1046 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001047 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001048 /* check message-id */
1049 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1050 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001051 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001052 } else {
1053 cur_msgid = strtoul(str_msgid, &ptr, 10);
1054 if (cur_msgid != msgid) {
1055 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1056 session->id, str_msgid);
1057 msgtype = NC_MSG_REPLY_ERR_MSGID;
1058 }
1059 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001060 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001061 }
Michal Vasko086311b2016-01-08 09:53:11 +01001062 break;
1063
1064 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001065 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001066 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001067 msgtype = NC_MSG_ERROR;
1068 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001069
1070 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001071 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001072 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001073 msgtype = NC_MSG_ERROR;
1074 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001075
1076 default:
1077 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1078 * NC_MSG_NONE is not returned by nc_read_msg()
1079 */
1080 break;
1081 }
1082
1083 return msgtype;
1084}
1085
1086/* cannot strictly fail, but does not need to fill any error parameter at all */
1087static void
1088parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1089{
1090 struct lyxml_elem *iter, *next, *info;
1091
1092 LY_TREE_FOR(xml->child, iter) {
1093 if (!iter->ns) {
1094 if (iter->content) {
1095 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1096 } else {
1097 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1098 }
1099 continue;
1100 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1101 if (iter->content) {
1102 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1103 iter->name, iter->content, iter->ns->value);
1104 } else {
1105 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1106 }
1107 continue;
1108 }
1109
1110 if (!strcmp(iter->name, "error-type")) {
1111 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1112 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1113 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1114 } else if (err->type) {
1115 WRN("<rpc-error> <error-type> duplicated.");
1116 } else {
1117 err->type = lydict_insert(ctx, iter->content, 0);
1118 }
1119 } else if (!strcmp(iter->name, "error-tag")) {
1120 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1121 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1122 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1123 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1124 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1125 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1126 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1127 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1128 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1129 && strcmp(iter->content, "malformed-message"))) {
1130 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1131 } else if (err->tag) {
1132 WRN("<rpc-error> <error-tag> duplicated.");
1133 } else {
1134 err->tag = lydict_insert(ctx, iter->content, 0);
1135 }
1136 } else if (!strcmp(iter->name, "error-severity")) {
1137 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1138 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1139 } else if (err->severity) {
1140 WRN("<rpc-error> <error-severity> duplicated.");
1141 } else {
1142 err->severity = lydict_insert(ctx, iter->content, 0);
1143 }
1144 } else if (!strcmp(iter->name, "error-app-tag")) {
1145 if (err->apptag) {
1146 WRN("<rpc-error> <error-app-tag> duplicated.");
1147 } else {
1148 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1149 }
1150 } else if (!strcmp(iter->name, "error-path")) {
1151 if (err->path) {
1152 WRN("<rpc-error> <error-path> duplicated.");
1153 } else {
1154 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1155 }
1156 } else if (!strcmp(iter->name, "error-message")) {
1157 if (err->message) {
1158 WRN("<rpc-error> <error-message> duplicated.");
1159 } else {
1160 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001161 if (err->message_lang) {
1162 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1163 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001164 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1165 }
1166 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1167 }
1168 } else if (!strcmp(iter->name, "error-info")) {
1169 LY_TREE_FOR_SAFE(iter->child, next, info) {
1170 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1171 if (!strcmp(info->name, "session-id")) {
1172 if (err->sid) {
1173 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1174 } else {
1175 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1176 }
Michal Vasko630485f2018-01-03 14:28:32 +01001177 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001178 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001179 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1180 if (!err->attr) {
1181 ERRMEM;
1182 return;
1183 }
Michal Vasko086311b2016-01-08 09:53:11 +01001184 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1185 } else if (!strcmp(info->name, "bad-element")) {
1186 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001187 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1188 if (!err->elem) {
1189 ERRMEM;
1190 return;
1191 }
Michal Vasko086311b2016-01-08 09:53:11 +01001192 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1193 } else if (!strcmp(info->name, "bad-namespace")) {
1194 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001195 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1196 if (!err->ns) {
1197 ERRMEM;
1198 return;
1199 }
Michal Vasko086311b2016-01-08 09:53:11 +01001200 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1201 } else {
1202 if (info->content) {
1203 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1204 info->name, info->content);
1205 } else {
1206 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1207 }
1208 }
1209 } else {
1210 lyxml_unlink(ctx, info);
1211 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001212 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1213 if (!err->other) {
1214 ERRMEM;
1215 return;
1216 }
Michal Vasko086311b2016-01-08 09:53:11 +01001217 err->other[err->other_count - 1] = info;
1218 }
1219 }
1220 } else {
1221 if (iter->content) {
1222 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1223 } else {
1224 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1225 }
1226 }
1227 }
1228}
1229
1230static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001231parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001232{
1233 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001234 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001235 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001236 struct nc_reply_data *data_rpl;
1237 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001238 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001239 int i;
1240
1241 if (!xml->child) {
1242 ERR("An empty <rpc-reply>.");
1243 return NULL;
1244 }
1245
1246 /* rpc-error */
1247 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1248 /* count and check elements */
1249 i = 0;
1250 LY_TREE_FOR(xml->child, iter) {
1251 if (strcmp(iter->name, "rpc-error")) {
1252 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1253 return NULL;
1254 } else if (!iter->ns) {
1255 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1256 return NULL;
1257 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1258 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1259 return NULL;
1260 }
1261 ++i;
1262 }
1263
1264 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001265 if (!error_rpl) {
1266 ERRMEM;
1267 return NULL;
1268 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001269 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001270 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001271 if (!error_rpl->err) {
1272 ERRMEM;
1273 free(error_rpl);
1274 return NULL;
1275 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001276 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001277 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001278 reply = (struct nc_reply *)error_rpl;
1279
1280 i = 0;
1281 LY_TREE_FOR(xml->child, iter) {
1282 parse_rpc_error(ctx, iter, error_rpl->err + i);
1283 ++i;
1284 }
1285
1286 /* ok */
1287 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1288 if (xml->child->next) {
1289 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1290 return NULL;
1291 }
1292 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001293 if (!reply) {
1294 ERRMEM;
1295 return NULL;
1296 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001297 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001298
1299 /* some RPC output */
1300 } else {
1301 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001302 case NC_RPC_ACT_GENERIC:
1303 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001304
1305 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +02001306 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +01001307 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001308 rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vaskoe1708602016-10-18 12:17:22 +02001309 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001310 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001311 return NULL;
1312 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001313 }
Michal Vasko086311b2016-01-08 09:53:11 +01001314 break;
1315
1316 case NC_RPC_GETCONFIG:
1317 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001318 if (!xml->child->child) {
1319 /* we did not receive any data */
1320 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001321 if (!data_rpl) {
1322 ERRMEM;
1323 return NULL;
1324 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001325 data_rpl->type = NC_RPL_DATA;
1326 data_rpl->data = NULL;
1327 return (struct nc_reply *)data_rpl;
1328 }
1329
Michal Vasko086311b2016-01-08 09:53:11 +01001330 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001331 data = lyd_parse_xml(ctx, &xml->child->child,
1332 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001333 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001334 if (!data) {
1335 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1336 return NULL;
1337 }
1338 break;
1339
1340 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001341 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001342 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001343 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001344 return NULL;
1345 }
1346 break;
1347
1348 case NC_RPC_EDIT:
1349 case NC_RPC_COPY:
1350 case NC_RPC_DELETE:
1351 case NC_RPC_LOCK:
1352 case NC_RPC_UNLOCK:
1353 case NC_RPC_KILL:
1354 case NC_RPC_COMMIT:
1355 case NC_RPC_DISCARD:
1356 case NC_RPC_CANCEL:
1357 case NC_RPC_VALIDATE:
1358 case NC_RPC_SUBSCRIBE:
1359 /* there is no output defined */
1360 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1361 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001362 default:
1363 ERRINT;
1364 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001365 }
1366
1367 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001368 if (!data_rpl) {
1369 ERRMEM;
1370 return NULL;
1371 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001372 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001373 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001374 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001375 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001376 } else {
1377 /* <get>, <get-config> */
1378 data_rpl->data = data;
1379 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001380 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001381 if (!data_rpl->data) {
1382 ERR("Failed to parse <rpc-reply>.");
1383 free(data_rpl);
1384 return NULL;
1385 }
1386 reply = (struct nc_reply *)data_rpl;
1387 }
1388
1389 return reply;
1390}
1391
Radek Krejci53691be2016-02-22 13:58:37 +01001392#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001393
Michal Vasko3031aae2016-01-27 16:07:18 +01001394int
1395nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1396{
1397 int sock;
1398
Michal Vasko45e53ae2016-04-07 11:46:03 +02001399 if (!address) {
1400 ERRARG("address");
1401 return -1;
1402 } else if (!port) {
1403 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001404 return -1;
1405 }
1406
1407 sock = nc_sock_listen(address, port);
1408 if (sock == -1) {
1409 return -1;
1410 }
1411
1412 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001413 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1414 if (!client_opts.ch_binds) {
1415 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001416 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001417 return -1;
1418 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001419
Michal Vasko2e6defd2016-10-07 15:48:15 +02001420 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1421 if (!client_opts.ch_bind_ti) {
1422 ERRMEM;
1423 close(sock);
1424 return -1;
1425 }
1426 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1427
Michal Vasko3031aae2016-01-27 16:07:18 +01001428 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001429 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1430 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001431 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001432 return -1;
1433 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001434 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1435 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001436 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001437
1438 return 0;
1439}
1440
1441int
1442nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1443{
1444 uint32_t i;
1445 int ret = -1;
1446
1447 if (!address && !port && !ti) {
1448 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1449 close(client_opts.ch_binds[i].sock);
1450 free((char *)client_opts.ch_binds[i].address);
1451
1452 ret = 0;
1453 }
1454 free(client_opts.ch_binds);
1455 client_opts.ch_binds = NULL;
1456 client_opts.ch_bind_count = 0;
1457 } else {
1458 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1459 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1460 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001461 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001462 close(client_opts.ch_binds[i].sock);
1463 free((char *)client_opts.ch_binds[i].address);
1464
1465 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001466 if (!client_opts.ch_bind_count) {
1467 free(client_opts.ch_binds);
1468 client_opts.ch_binds = NULL;
1469 } else if (i < client_opts.ch_bind_count) {
1470 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1471 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1472 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001473
1474 ret = 0;
1475 }
1476 }
1477 }
1478
1479 return ret;
1480}
1481
1482API int
1483nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1484{
1485 int sock;
1486 char *host = NULL;
1487 uint16_t port, idx;
1488
Michal Vasko45e53ae2016-04-07 11:46:03 +02001489 if (!client_opts.ch_binds) {
1490 ERRINIT;
1491 return -1;
1492 } else if (!session) {
1493 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001494 return -1;
1495 }
1496
1497 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1498
Michal Vasko50456e82016-02-02 12:16:08 +01001499 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001500 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001501 return sock;
1502 }
1503
Radek Krejci53691be2016-02-22 13:58:37 +01001504#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001505 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001506 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001507 } else
1508#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001509#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001510 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001511 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001512 } else
1513#endif
1514 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001515 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001516 *session = NULL;
1517 }
1518
1519 free(host);
1520
1521 if (!(*session)) {
1522 return -1;
1523 }
1524
1525 return 1;
1526}
1527
Radek Krejci53691be2016-02-22 13:58:37 +01001528#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001529
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001530API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001531nc_session_get_cpblts(const struct nc_session *session)
1532{
1533 if (!session) {
1534 ERRARG("session");
1535 return NULL;
1536 }
1537
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001538 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001539}
1540
1541API const char *
1542nc_session_cpblt(const struct nc_session *session, const char *capab)
1543{
1544 int i, len;
1545
1546 if (!session) {
1547 ERRARG("session");
1548 return NULL;
1549 } else if (!capab) {
1550 ERRARG("capab");
1551 return NULL;
1552 }
1553
1554 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001555 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1556 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1557 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001558 }
1559 }
1560
1561 return NULL;
1562}
1563
Michal Vasko9cd26a82016-05-31 08:58:48 +02001564API int
1565nc_session_ntf_thread_running(const struct nc_session *session)
1566{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001567 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001568 ERRARG("session");
1569 return 0;
1570 }
1571
Michal Vasko2e6defd2016-10-07 15:48:15 +02001572 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001573}
1574
Michal Vaskob7558c52016-02-26 15:04:19 +01001575API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001576nc_client_init(void)
1577{
1578 nc_init();
1579}
1580
1581API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001582nc_client_destroy(void)
1583{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001584 nc_client_set_schema_searchpath(NULL);
1585#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1586 nc_client_ch_del_bind(NULL, 0, 0);
1587#endif
1588#ifdef NC_ENABLED_SSH
1589 nc_client_ssh_destroy_opts();
1590#endif
1591#ifdef NC_ENABLED_TLS
1592 nc_client_tls_destroy_opts();
1593#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001594 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001595}
1596
Michal Vasko086311b2016-01-08 09:53:11 +01001597API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001598nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, int parseroptions, struct nc_reply **reply)
Michal Vasko086311b2016-01-08 09:53:11 +01001599{
1600 struct lyxml_elem *xml;
1601 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1602
Michal Vasko45e53ae2016-04-07 11:46:03 +02001603 if (!session) {
1604 ERRARG("session");
1605 return NC_MSG_ERROR;
1606 } else if (!rpc) {
1607 ERRARG("rpc");
1608 return NC_MSG_ERROR;
1609 } else if (!reply) {
1610 ERRARG("reply");
1611 return NC_MSG_ERROR;
1612 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1613 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001614 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001615 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001616 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001617 return NC_MSG_ERROR;
1618 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001619 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001620 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1621 parseroptions &= LYD_OPT_STRICT;
1622 }
Michal Vasko41adf392017-01-17 10:39:04 +01001623 /* no mechanism to check external dependencies is provided */
1624 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001625 *reply = NULL;
1626
1627 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001628
Michal Vasko71ba2da2016-05-04 10:53:16 +02001629 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001630 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001631 lyxml_free(session->ctx, xml);
1632 if (!(*reply)) {
1633 return NC_MSG_ERROR;
1634 }
1635 }
1636
1637 return msgtype;
1638}
1639
1640API NC_MSG_TYPE
1641nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1642{
1643 struct lyxml_elem *xml, *ev_time;
1644 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1645
Michal Vasko45e53ae2016-04-07 11:46:03 +02001646 if (!session) {
1647 ERRARG("session");
1648 return NC_MSG_ERROR;
1649 } else if (!notif) {
1650 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001651 return NC_MSG_ERROR;
1652 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001653 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001654 return NC_MSG_ERROR;
1655 }
1656
1657 msgtype = get_msg(session, timeout, 0, &xml);
1658
1659 if (msgtype == NC_MSG_NOTIF) {
1660 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001661 if (!*notif) {
1662 ERRMEM;
1663 lyxml_free(session->ctx, xml);
1664 return NC_MSG_ERROR;
1665 }
Michal Vasko086311b2016-01-08 09:53:11 +01001666
1667 /* eventTime */
1668 LY_TREE_FOR(xml->child, ev_time) {
1669 if (!strcmp(ev_time->name, "eventTime")) {
1670 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1671 /* lyd_parse does not know this element */
1672 lyxml_free(session->ctx, ev_time);
1673 break;
1674 }
1675 }
1676 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001677 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001678 goto fail;
1679 }
1680
1681 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001682 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01001683 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001684 lyxml_free(session->ctx, xml);
1685 xml = NULL;
1686 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001687 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001688 goto fail;
1689 }
1690 }
1691
1692 return msgtype;
1693
1694fail:
1695 lydict_remove(session->ctx, (*notif)->datetime);
1696 lyd_free((*notif)->tree);
1697 free(*notif);
1698 *notif = NULL;
1699 lyxml_free(session->ctx, xml);
1700
1701 return NC_MSG_ERROR;
1702}
1703
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001704static void *
1705nc_recv_notif_thread(void *arg)
1706{
1707 struct nc_ntf_thread_arg *ntarg;
1708 struct nc_session *session;
1709 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1710 struct nc_notif *notif;
1711 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02001712 pthread_t *ntf_tid;
1713
1714 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001715
1716 ntarg = (struct nc_ntf_thread_arg *)arg;
1717 session = ntarg->session;
1718 notif_clb = ntarg->notif_clb;
1719 free(ntarg);
1720
Michal Vasko131120a2018-05-29 15:44:02 +02001721 /* remember our allocated tid, we will be freeing it */
1722 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
1723
Michal Vasko2e6defd2016-10-07 15:48:15 +02001724 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001725 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001726 if (msgtype == NC_MSG_NOTIF) {
1727 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001728 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1729 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1730 nc_notif_free(notif);
1731 break;
1732 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001733 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01001734 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02001735 /* quit this thread once the session is broken */
1736 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001737 }
1738
1739 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1740 }
1741
Michal Vasko0651c902016-05-19 15:55:42 +02001742 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001743 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02001744 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001745 return NULL;
1746}
1747
1748API int
1749nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1750{
1751 struct nc_ntf_thread_arg *ntarg;
1752 int ret;
1753
Michal Vasko45e53ae2016-04-07 11:46:03 +02001754 if (!session) {
1755 ERRARG("session");
1756 return -1;
1757 } else if (!notif_clb) {
1758 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001759 return -1;
1760 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1761 ERR("Session %u: invalid session to receive Notifications.", session->id);
1762 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001763 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001764 ERR("Session %u: separate notification thread is already running.", session->id);
1765 return -1;
1766 }
1767
1768 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001769 if (!ntarg) {
1770 ERRMEM;
1771 return -1;
1772 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001773 ntarg->session = session;
1774 ntarg->notif_clb = notif_clb;
1775
1776 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001777 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1778 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001779 ERRMEM;
1780 free(ntarg);
1781 return -1;
1782 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001783
Michal Vasko2e6defd2016-10-07 15:48:15 +02001784 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001785 if (ret) {
1786 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1787 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001788 free((pthread_t *)session->opts.client.ntf_tid);
1789 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001790 return -1;
1791 }
1792
1793 return 0;
1794}
1795
Michal Vasko086311b2016-01-08 09:53:11 +01001796API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001797nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001798{
1799 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02001800 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02001801 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001802 struct nc_rpc_getconfig *rpc_gc;
1803 struct nc_rpc_edit *rpc_e;
1804 struct nc_rpc_copy *rpc_cp;
1805 struct nc_rpc_delete *rpc_del;
1806 struct nc_rpc_lock *rpc_lock;
1807 struct nc_rpc_get *rpc_g;
1808 struct nc_rpc_kill *rpc_k;
1809 struct nc_rpc_commit *rpc_com;
1810 struct nc_rpc_cancel *rpc_can;
1811 struct nc_rpc_validate *rpc_val;
1812 struct nc_rpc_getschema *rpc_gs;
1813 struct nc_rpc_subscribe *rpc_sub;
1814 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001815 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001816 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001817 uint64_t cur_msgid;
1818
Michal Vasko45e53ae2016-04-07 11:46:03 +02001819 if (!session) {
1820 ERRARG("session");
1821 return NC_MSG_ERROR;
1822 } else if (!rpc) {
1823 ERRARG("rpc");
1824 return NC_MSG_ERROR;
1825 } else if (!msgid) {
1826 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001827 return NC_MSG_ERROR;
1828 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001829 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001830 return NC_MSG_ERROR;
1831 }
1832
Michal Vasko90e8e692016-07-13 12:27:57 +02001833 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001834 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001835 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001836 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001837 return NC_MSG_ERROR;
1838 }
1839 }
1840
1841 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001842 case NC_RPC_ACT_GENERIC:
1843 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001844
1845 if (rpc_gen->has_data) {
1846 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01001847 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001848 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001849 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01001850 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01001851 if (!data) {
1852 return NC_MSG_ERROR;
1853 }
Michal Vasko086311b2016-01-08 09:53:11 +01001854 }
1855 break;
1856
1857 case NC_RPC_GETCONFIG:
1858 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1859
1860 data = lyd_new(NULL, ietfnc, "get-config");
1861 node = lyd_new(data, ietfnc, "source");
1862 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1863 if (!node) {
1864 lyd_free(data);
1865 return NC_MSG_ERROR;
1866 }
1867 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001868 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001869 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001870 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001871 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001872 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001873 lyd_insert_attr(node, NULL, "type", "xpath");
1874 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001875 }
1876 if (!node) {
1877 lyd_free(data);
1878 return NC_MSG_ERROR;
1879 }
1880 }
1881
1882 if (rpc_gc->wd_mode) {
1883 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001884 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001885 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001886 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001887 return NC_MSG_ERROR;
1888 }
1889 }
1890 switch (rpc_gc->wd_mode) {
1891 case NC_WD_UNKNOWN:
1892 /* cannot get here */
1893 break;
1894 case NC_WD_ALL:
1895 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1896 break;
1897 case NC_WD_ALL_TAG:
1898 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1899 break;
1900 case NC_WD_TRIM:
1901 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1902 break;
1903 case NC_WD_EXPLICIT:
1904 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1905 break;
1906 }
1907 if (!node) {
1908 lyd_free(data);
1909 return NC_MSG_ERROR;
1910 }
1911 }
1912 break;
1913
1914 case NC_RPC_EDIT:
1915 rpc_e = (struct nc_rpc_edit *)rpc;
1916
1917 data = lyd_new(NULL, ietfnc, "edit-config");
1918 node = lyd_new(data, ietfnc, "target");
1919 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1920 if (!node) {
1921 lyd_free(data);
1922 return NC_MSG_ERROR;
1923 }
1924
1925 if (rpc_e->default_op) {
1926 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1927 if (!node) {
1928 lyd_free(data);
1929 return NC_MSG_ERROR;
1930 }
1931 }
1932
1933 if (rpc_e->test_opt) {
1934 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1935 if (!node) {
1936 lyd_free(data);
1937 return NC_MSG_ERROR;
1938 }
1939 }
1940
1941 if (rpc_e->error_opt) {
1942 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1943 if (!node) {
1944 lyd_free(data);
1945 return NC_MSG_ERROR;
1946 }
1947 }
1948
Michal Vasko7793bc62016-09-16 11:58:41 +02001949 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001950 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001951 } else {
1952 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1953 }
1954 if (!node) {
1955 lyd_free(data);
1956 return NC_MSG_ERROR;
1957 }
1958 break;
1959
1960 case NC_RPC_COPY:
1961 rpc_cp = (struct nc_rpc_copy *)rpc;
1962
1963 data = lyd_new(NULL, ietfnc, "copy-config");
1964 node = lyd_new(data, ietfnc, "target");
1965 if (rpc_cp->url_trg) {
1966 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1967 } else {
1968 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1969 }
1970 if (!node) {
1971 lyd_free(data);
1972 return NC_MSG_ERROR;
1973 }
1974
1975 node = lyd_new(data, ietfnc, "source");
1976 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001977 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001978 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001979 } else {
1980 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1981 }
1982 } else {
1983 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1984 }
1985 if (!node) {
1986 lyd_free(data);
1987 return NC_MSG_ERROR;
1988 }
1989
1990 if (rpc_cp->wd_mode) {
1991 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02001992 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01001993 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001994 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001995 return NC_MSG_ERROR;
1996 }
1997 }
1998 switch (rpc_cp->wd_mode) {
1999 case NC_WD_UNKNOWN:
2000 /* cannot get here */
2001 break;
2002 case NC_WD_ALL:
2003 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2004 break;
2005 case NC_WD_ALL_TAG:
2006 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2007 break;
2008 case NC_WD_TRIM:
2009 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2010 break;
2011 case NC_WD_EXPLICIT:
2012 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2013 break;
2014 }
2015 if (!node) {
2016 lyd_free(data);
2017 return NC_MSG_ERROR;
2018 }
2019 }
2020 break;
2021
2022 case NC_RPC_DELETE:
2023 rpc_del = (struct nc_rpc_delete *)rpc;
2024
2025 data = lyd_new(NULL, ietfnc, "delete-config");
2026 node = lyd_new(data, ietfnc, "target");
2027 if (rpc_del->url) {
2028 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2029 } else {
2030 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2031 }
2032 if (!node) {
2033 lyd_free(data);
2034 return NC_MSG_ERROR;
2035 }
2036 break;
2037
2038 case NC_RPC_LOCK:
2039 rpc_lock = (struct nc_rpc_lock *)rpc;
2040
2041 data = lyd_new(NULL, ietfnc, "lock");
2042 node = lyd_new(data, ietfnc, "target");
2043 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2044 if (!node) {
2045 lyd_free(data);
2046 return NC_MSG_ERROR;
2047 }
2048 break;
2049
2050 case NC_RPC_UNLOCK:
2051 rpc_lock = (struct nc_rpc_lock *)rpc;
2052
2053 data = lyd_new(NULL, ietfnc, "unlock");
2054 node = lyd_new(data, ietfnc, "target");
2055 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2056 if (!node) {
2057 lyd_free(data);
2058 return NC_MSG_ERROR;
2059 }
2060 break;
2061
2062 case NC_RPC_GET:
2063 rpc_g = (struct nc_rpc_get *)rpc;
2064
2065 data = lyd_new(NULL, ietfnc, "get");
2066 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002067 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002068 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002069 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002070 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002071 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002072 lyd_insert_attr(node, NULL, "type", "xpath");
2073 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002074 }
2075 if (!node) {
2076 lyd_free(data);
2077 return NC_MSG_ERROR;
2078 }
2079 }
2080
2081 if (rpc_g->wd_mode) {
2082 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002083 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002084 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002085 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002086 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002087 return NC_MSG_ERROR;
2088 }
2089 }
2090 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002091 case NC_WD_ALL:
2092 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2093 break;
2094 case NC_WD_ALL_TAG:
2095 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2096 break;
2097 case NC_WD_TRIM:
2098 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2099 break;
2100 case NC_WD_EXPLICIT:
2101 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2102 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002103 default:
2104 /* cannot get here */
2105 node = NULL;
2106 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002107 }
2108 if (!node) {
2109 lyd_free(data);
2110 return NC_MSG_ERROR;
2111 }
2112 }
2113 break;
2114
2115 case NC_RPC_KILL:
2116 rpc_k = (struct nc_rpc_kill *)rpc;
2117
2118 data = lyd_new(NULL, ietfnc, "kill-session");
2119 sprintf(str, "%u", rpc_k->sid);
2120 lyd_new_leaf(data, ietfnc, "session-id", str);
2121 break;
2122
2123 case NC_RPC_COMMIT:
2124 rpc_com = (struct nc_rpc_commit *)rpc;
2125
2126 data = lyd_new(NULL, ietfnc, "commit");
2127 if (rpc_com->confirmed) {
2128 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2129 }
2130
2131 if (rpc_com->confirm_timeout) {
2132 sprintf(str, "%u", rpc_com->confirm_timeout);
2133 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2134 }
2135
2136 if (rpc_com->persist) {
2137 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2138 if (!node) {
2139 lyd_free(data);
2140 return NC_MSG_ERROR;
2141 }
2142 }
2143
2144 if (rpc_com->persist_id) {
2145 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2146 if (!node) {
2147 lyd_free(data);
2148 return NC_MSG_ERROR;
2149 }
2150 }
2151 break;
2152
2153 case NC_RPC_DISCARD:
2154 data = lyd_new(NULL, ietfnc, "discard-changes");
2155 break;
2156
2157 case NC_RPC_CANCEL:
2158 rpc_can = (struct nc_rpc_cancel *)rpc;
2159
2160 data = lyd_new(NULL, ietfnc, "cancel-commit");
2161 if (rpc_can->persist_id) {
2162 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2163 if (!node) {
2164 lyd_free(data);
2165 return NC_MSG_ERROR;
2166 }
2167 }
2168 break;
2169
2170 case NC_RPC_VALIDATE:
2171 rpc_val = (struct nc_rpc_validate *)rpc;
2172
2173 data = lyd_new(NULL, ietfnc, "validate");
2174 node = lyd_new(data, ietfnc, "source");
2175 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002176 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002177 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002178 } else {
2179 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2180 }
2181 } else {
2182 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2183 }
2184 if (!node) {
2185 lyd_free(data);
2186 return NC_MSG_ERROR;
2187 }
2188 break;
2189
2190 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002191 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002192 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002193 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002194 return NC_MSG_ERROR;
2195 }
2196
2197 rpc_gs = (struct nc_rpc_getschema *)rpc;
2198
2199 data = lyd_new(NULL, ietfncmon, "get-schema");
2200 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2201 if (!node) {
2202 lyd_free(data);
2203 return NC_MSG_ERROR;
2204 }
2205 if (rpc_gs->version) {
2206 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2207 if (!node) {
2208 lyd_free(data);
2209 return NC_MSG_ERROR;
2210 }
2211 }
2212 if (rpc_gs->format) {
2213 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2214 if (!node) {
2215 lyd_free(data);
2216 return NC_MSG_ERROR;
2217 }
2218 }
2219 break;
2220
2221 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002222 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002223 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002224 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002225 return NC_MSG_ERROR;
2226 }
2227
2228 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2229
2230 data = lyd_new(NULL, notifs, "create-subscription");
2231 if (rpc_sub->stream) {
2232 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2233 if (!node) {
2234 lyd_free(data);
2235 return NC_MSG_ERROR;
2236 }
2237 }
2238
2239 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002240 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002241 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002242 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002243 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002244 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002245 lyd_insert_attr(node, NULL, "type", "xpath");
2246 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002247 }
2248 if (!node) {
2249 lyd_free(data);
2250 return NC_MSG_ERROR;
2251 }
2252 }
2253
2254 if (rpc_sub->start) {
2255 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2256 if (!node) {
2257 lyd_free(data);
2258 return NC_MSG_ERROR;
2259 }
2260 }
2261
2262 if (rpc_sub->stop) {
2263 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2264 if (!node) {
2265 lyd_free(data);
2266 return NC_MSG_ERROR;
2267 }
2268 }
2269 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002270 default:
2271 ERRINT;
2272 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002273 }
2274
Michal Vasko131120a2018-05-29 15:44:02 +02002275 if (!data) {
2276 /* error was already printed */
2277 return NC_MSG_ERROR;
2278 }
2279
Michal Vasko41adf392017-01-17 10:39:04 +01002280 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2281 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002282 if (dofree) {
2283 lyd_free(data);
2284 }
Michal Vasko086311b2016-01-08 09:53:11 +01002285 return NC_MSG_ERROR;
2286 }
2287
Michal Vasko131120a2018-05-29 15:44:02 +02002288 /* send RPC, store its message ID */
2289 r = nc_send_msg_io(session, timeout, data);
2290 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002291
Radek Krejcib4b19062018-02-07 16:33:06 +01002292 if (dofree) {
2293 lyd_free(data);
2294 }
Michal Vasko086311b2016-01-08 09:53:11 +01002295
Michal Vasko131120a2018-05-29 15:44:02 +02002296 if (r == NC_MSG_RPC) {
2297 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002298 }
Michal Vasko131120a2018-05-29 15:44:02 +02002299 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002300}
Michal Vaskode2946c2017-01-12 12:19:26 +01002301
2302API void
2303nc_client_session_set_not_strict(struct nc_session *session)
2304{
2305 if (session->side != NC_CLIENT) {
2306 ERRARG("session");
2307 return;
2308 }
2309
2310 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2311}