blob: 165f33629aa3722c444afaba745f7fbc39fb173c [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
15#include <assert.h>
16#include <errno.h>
17#include <fcntl.h>
18#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010019#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020025#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010026#include <sys/types.h>
27#include <unistd.h>
28#include <arpa/inet.h>
29#include <poll.h>
30
31#include <libyang/libyang.h>
32
Michal Vasko086311b2016-01-08 09:53:11 +010033#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010034#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010035#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010036
Michal Vasko80ef5d22016-01-18 09:21:02 +010037static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
38
Radek Krejci62aa0642017-05-25 16:33:49 +020039#ifdef NC_ENABLED_SSH
40int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
41char *sshauth_password(const char *username, const char *hostname, void *priv);
42char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
43char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
44#endif /* NC_ENABLED_SSH */
45
46static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
47static pthread_key_t nc_client_context_key;
48#ifdef __linux__
49static struct nc_client_context context_main = {
50 /* .opts zeroed */
51#ifdef NC_ENABLED_SSH
52 .ssh_opts = {
53 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
54 .auth_hostkey_check = sshauth_hostkey_check,
55 .auth_password = sshauth_password,
56 .auth_interactive = sshauth_interactive,
57 .auth_privkey_passphrase = sshauth_privkey_passphrase
58 },
59 .ssh_ch_opts = {
60 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
61 .auth_hostkey_check = sshauth_hostkey_check,
62 .auth_password = sshauth_password,
63 .auth_interactive = sshauth_interactive,
64 .auth_privkey_passphrase = sshauth_privkey_passphrase
65 }
66#endif /* NC_ENABLED_SSH */
67 /* .tls_ structures zeroed */
68};
69#endif
70
71static void
72nc_client_context_free(void *ptr)
73{
74 struct nc_client_context *c = (struct nc_client_context *)ptr;
75
76#ifdef __linux__
77 /* in __linux__ we use static memory in the main thread,
78 * so this check is for programs terminating the main()
79 * function by pthread_exit() :)
80 */
81 if (c != &context_main)
82#endif
83 {
84 nc_client_set_schema_searchpath(NULL);
85#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
86 nc_client_ch_del_bind(NULL, 0, 0);
87#endif
88#ifdef NC_ENABLED_SSH
89 nc_client_ssh_destroy_opts();
90#endif
91#ifdef NC_ENABLED_TLS
92 nc_client_tls_destroy_opts();
93#endif
94 free(c);
95 }
96}
97
98static void
99nc_client_context_createkey(void)
100{
101 int r;
102
103 /* initiate */
104 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
105 pthread_setspecific(nc_client_context_key, NULL);
106}
107
108struct nc_client_context *
109nc_client_context_location(void)
110{
111 struct nc_client_context *e;
112
113 pthread_once(&nc_client_context_once, nc_client_context_createkey);
114 e = pthread_getspecific(nc_client_context_key);
115 if (!e) {
116 /* prepare ly_err storage */
117#ifdef __linux__
118 if (getpid() == syscall(SYS_gettid)) {
119 /* main thread - use global variable instead of thread-specific variable. */
120 e = &context_main;
121 } else
122#endif /* __linux__ */
123 {
124 e = calloc(1, sizeof *e);
125 /* set default values */
126#ifdef NC_ENABLED_SSH
127 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
128 e->ssh_opts.auth_pref[0].value = 3;
129 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
130 e->ssh_opts.auth_pref[1].value = 2;
131 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
132 e->ssh_opts.auth_pref[2].value = 1;
133 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
134 e->ssh_opts.auth_password = sshauth_password;
135 e->ssh_opts.auth_interactive = sshauth_interactive;
136 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
137
138 /* callhome settings are the same except the inverted auth methods preferences */
139 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
140 e->ssh_ch_opts.auth_pref[0].value = 1;
141 e->ssh_ch_opts.auth_pref[1].value = 2;
142 e->ssh_ch_opts.auth_pref[2].value = 3;
143#endif /* NC_ENABLED_SSH */
144 }
145 pthread_setspecific(nc_client_context_key, e);
146 }
147
148 return e;
149}
150
151#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100152
153API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100154nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100155{
Michal Vasko3031aae2016-01-27 16:07:18 +0100156 if (client_opts.schema_searchpath) {
157 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100158 }
Michal Vasko086311b2016-01-08 09:53:11 +0100159
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100160 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100161 client_opts.schema_searchpath = strdup(path);
162 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100163 ERRMEM;
164 return 1;
165 }
166 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100167 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100168 }
169
170 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100171}
172
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100173API const char *
174nc_client_get_schema_searchpath(void)
175{
176 return client_opts.schema_searchpath;
177}
178
Michal Vasko3031aae2016-01-27 16:07:18 +0100179/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +0100180static int
Michal Vaskoeee99412016-11-21 10:19:43 +0100181ctx_check_and_load_model(struct nc_session *session, const char *module_cpblt)
Michal Vasko086311b2016-01-08 09:53:11 +0100182{
183 const struct lys_module *module;
184 char *ptr, *ptr2;
185 char *model_name, *revision = NULL, *features = NULL;
186
Michal Vaskoeee99412016-11-21 10:19:43 +0100187 assert(!strncmp(module_cpblt, "module=", 7));
188
189 ptr = (char *)module_cpblt + 7;
Michal Vasko086311b2016-01-08 09:53:11 +0100190 ptr2 = strchr(ptr, '&');
191 if (!ptr2) {
192 ptr2 = ptr + strlen(ptr);
193 }
194 model_name = strndup(ptr, ptr2 - ptr);
195
196 /* parse revision */
Michal Vaskoeee99412016-11-21 10:19:43 +0100197 ptr = strstr(module_cpblt, "revision=");
Michal Vasko086311b2016-01-08 09:53:11 +0100198 if (ptr) {
199 ptr += 9;
200 ptr2 = strchr(ptr, '&');
201 if (!ptr2) {
202 ptr2 = ptr + strlen(ptr);
203 }
204 revision = strndup(ptr, ptr2 - ptr);
205 }
206
207 /* load module if needed */
208 module = ly_ctx_get_module(session->ctx, model_name, revision);
209 if (!module) {
210 module = ly_ctx_load_module(session->ctx, model_name, revision);
211 }
212
Michal Vasko086311b2016-01-08 09:53:11 +0100213 free(revision);
214 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100215 WRN("Failed to load model \"%s\".", model_name);
216 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100217 return 1;
218 }
Michal Vaskoef578332016-01-25 13:20:09 +0100219 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100220
Michal Vasko2a54a6d2017-04-26 14:26:41 +0200221 /* make it implemented */
222 if (!module->implemented && lys_set_implemented(module)) {
223 WRN("Failed to implement model \"%s\".", module->name);
224 return 1;
225 }
226
Michal Vasko086311b2016-01-08 09:53:11 +0100227 /* parse features */
Michal Vaskoeee99412016-11-21 10:19:43 +0100228 ptr = strstr(module_cpblt, "features=");
Michal Vasko086311b2016-01-08 09:53:11 +0100229 if (ptr) {
230 ptr += 9;
231 ptr2 = strchr(ptr, '&');
232 if (!ptr2) {
233 ptr2 = ptr + strlen(ptr);
234 }
235 features = strndup(ptr, ptr2 - ptr);
236 }
237
238 /* enable features */
239 if (features) {
240 /* basically manual strtok_r (to avoid macro) */
241 ptr2 = features;
242 for (ptr = features; *ptr; ++ptr) {
243 if (*ptr == ',') {
244 *ptr = '\0';
245 /* remember last feature */
246 ptr2 = ptr + 1;
247 }
248 }
249
250 ptr = features;
251 lys_features_enable(module, ptr);
252 while (ptr != ptr2) {
253 ptr += strlen(ptr) + 1;
254 lys_features_enable(module, ptr);
255 }
256
257 free(features);
258 }
259
260 return 0;
261}
262
Michal Vasko1aaa6602016-02-09 11:04:33 +0100263/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100264static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100265ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100266{
267 int i;
268 const struct lys_module *ietfnc;
269
270 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
271 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100272 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
273 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100274 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100275 }
276 }
277 if (!ietfnc) {
278 ERR("Loading base NETCONF schema failed.");
279 return 1;
280 }
281
282 /* set supported capabilities from ietf-netconf */
283 for (i = 0; cpblts[i]; ++i) {
284 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
285 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
286 lys_features_enable(ietfnc, "writable-running");
287 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
288 lys_features_enable(ietfnc, "candidate");
289 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
290 lys_features_enable(ietfnc, "confirmed-commit");
291 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
292 lys_features_enable(ietfnc, "rollback-on-error");
293 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
294 lys_features_enable(ietfnc, "validate");
295 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
296 lys_features_enable(ietfnc, "startup");
297 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
298 lys_features_enable(ietfnc, "url");
299 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
300 lys_features_enable(ietfnc, "xpath");
301 }
302 }
303 }
304
305 return 0;
306}
307
308static char *
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200309libyang_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
310 void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100311{
312 struct nc_session *session = (struct nc_session *)user_data;
313 struct nc_rpc *rpc;
314 struct nc_reply *reply;
315 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200316 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200317 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100318 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200319 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100320 uint64_t msgid;
321
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200322 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200323 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200324 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200325 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200326 }
Michal Vasko086311b2016-01-08 09:53:11 +0100327
328 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
329 usleep(1000);
330 }
331 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100332 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100333 nc_rpc_free(rpc);
334 return NULL;
335 }
336
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200337 do {
Michal Vaskof471fa02017-02-15 10:48:12 +0100338 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200339 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100340 nc_rpc_free(rpc);
341 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100342 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100343 return NULL;
344 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100345 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100346 return NULL;
347 }
348
Michal Vasko998ba412016-09-16 12:00:07 +0200349 switch (reply->type) {
350 case NC_RPL_OK:
351 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
352 nc_reply_free(reply);
353 return NULL;
354 case NC_RPL_DATA:
355 /* fine */
356 break;
357 case NC_RPL_ERROR:
358 error_rpl = (struct nc_reply_error *)reply;
359 if (error_rpl->count) {
360 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
361 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
362 } else {
363 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
364 }
365 nc_reply_free(reply);
366 return NULL;
367 case NC_RPL_NOTIF:
368 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100369 nc_reply_free(reply);
370 return NULL;
371 }
372
Michal Vasko086311b2016-01-08 09:53:11 +0100373 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200374 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
375 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
376 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
377 nc_reply_free(reply);
378 return NULL;
379 }
Radek Krejci539efb62016-08-24 15:05:16 +0200380 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
381 switch (get_schema_data->value_type) {
382 case LYD_ANYDATA_CONSTSTRING:
383 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200384 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200385 break;
386 case LYD_ANYDATA_DATATREE:
387 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
388 break;
389 case LYD_ANYDATA_XML:
390 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
391 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200392 case LYD_ANYDATA_JSON:
393 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200394 case LYD_ANYDATA_SXML:
395 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200396 ERRINT;
397 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200398 }
Michal Vasko086311b2016-01-08 09:53:11 +0100399 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100400 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200401 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100402
Michal Vasko086311b2016-01-08 09:53:11 +0100403 return model_data;
404}
405
406int
407nc_ctx_check_and_fill(struct nc_session *session)
408{
Michal Vaskoeee99412016-11-21 10:19:43 +0100409 const char *module_cpblt;
Michal Vaskoef578332016-01-25 13:20:09 +0100410 int i, get_schema_support = 0, ret = 0, r;
Michal Vaskocdeee432017-01-13 13:51:01 +0100411 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100412 void *old_data = NULL;
413
Michal Vasko2e6defd2016-10-07 15:48:15 +0200414 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100415
416 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200417 for (i = 0; session->opts.client.cpblts[i]; ++i) {
418 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100419 get_schema_support = 1;
420 break;
421 }
422 }
423
424 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
425 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
426 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
427 /* set module retrieval using <get-schema> */
Michal Vaskocdeee432017-01-13 13:51:01 +0100428 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
429 ly_ctx_set_module_imp_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100430 } else {
431 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
432 }
433 }
434
435 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200436 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100437 if (old_clb) {
Michal Vaskocdeee432017-01-13 13:51:01 +0100438 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko086311b2016-01-08 09:53:11 +0100439 }
Michal Vaskoef578332016-01-25 13:20:09 +0100440 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100441 }
442
443 /* load all other models */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200444 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Michal Vaskoeee99412016-11-21 10:19:43 +0100445 module_cpblt = strstr(session->opts.client.cpblts[i], "module=");
446 /* this capability requires a module */
447 if (module_cpblt) {
448 r = ctx_check_and_load_model(session, module_cpblt);
449 if (r == -1) {
450 ret = -1;
451 break;
Michal Vaskoef578332016-01-25 13:20:09 +0100452 }
453
Michal Vaskoeee99412016-11-21 10:19:43 +0100454 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
455 * if it was using get-schema */
456 if (r == 1) {
457 if (get_schema_support) {
458 VRB("Trying to load the schema from a different source.");
459 /* works even if old_clb is NULL */
Michal Vaskocdeee432017-01-13 13:51:01 +0100460 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vaskoeee99412016-11-21 10:19:43 +0100461 r = ctx_check_and_load_model(session, module_cpblt);
462 }
Michal Vaskoef578332016-01-25 13:20:09 +0100463
Michal Vaskoeee99412016-11-21 10:19:43 +0100464 /* fail again (or no other way to try), too bad */
465 if (r) {
466 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
467 }
468
469 /* set get-schema callback back */
Michal Vaskocdeee432017-01-13 13:51:01 +0100470 ly_ctx_set_module_imp_clb(session->ctx, &libyang_module_clb, session);
Michal Vaskoeee99412016-11-21 10:19:43 +0100471 }
Michal Vaskoef578332016-01-25 13:20:09 +0100472 }
Michal Vasko086311b2016-01-08 09:53:11 +0100473 }
474
475 if (old_clb) {
Michal Vaskocdeee432017-01-13 13:51:01 +0100476 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko086311b2016-01-08 09:53:11 +0100477 }
Michal Vaskoeee99412016-11-21 10:19:43 +0100478 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
479 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 +0100480 }
481 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100482}
483
484API struct nc_session *
485nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
486{
Michal Vaskod083db62016-01-19 10:31:29 +0100487 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100488
Michal Vasko45e53ae2016-04-07 11:46:03 +0200489 if (fdin < 0) {
490 ERRARG("fdin");
491 return NULL;
492 } else if (fdout < 0) {
493 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100494 return NULL;
495 }
496
497 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100498 session = nc_new_session(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100499 if (!session) {
500 ERRMEM;
501 return NULL;
502 }
503 session->status = NC_STATUS_STARTING;
504 session->side = NC_CLIENT;
505
506 /* transport specific data */
507 session->ti_type = NC_TI_FD;
Michal Vaskoade892d2017-02-22 13:40:35 +0100508 pthread_mutex_init(session->ti_lock, NULL);
509 pthread_cond_init(session->ti_cond, NULL);
510 *session->ti_inuse = 0;
511
Michal Vasko086311b2016-01-08 09:53:11 +0100512 session->ti.fd.in = fdin;
513 session->ti.fd.out = fdout;
514
515 /* assign context (dicionary needed for handshake) */
516 if (!ctx) {
517 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100518 /* definitely should not happen, but be ready */
519 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
520 /* that's just it */
521 goto fail;
522 }
Michal Vasko086311b2016-01-08 09:53:11 +0100523 } else {
524 session->flags |= NC_SESSION_SHAREDCTX;
525 }
526 session->ctx = ctx;
527
528 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200529 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100530 goto fail;
531 }
532 session->status = NC_STATUS_RUNNING;
533
Michal Vaskoef578332016-01-25 13:20:09 +0100534 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100535 goto fail;
536 }
537
538 return session;
539
540fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100541 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100542 return NULL;
543}
544
545int
Michal Vaskof05562c2016-01-20 12:06:43 +0100546nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100547{
Michal Vasko0190bc32016-03-02 15:47:49 +0100548 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100549 struct addrinfo hints, *res_list, *res;
550 char port_s[6]; /* length of string representation of short int */
551
552 snprintf(port_s, 6, "%u", port);
553
554 /* Connect to a server */
555 memset(&hints, 0, sizeof hints);
556 hints.ai_family = AF_UNSPEC;
557 hints.ai_socktype = SOCK_STREAM;
558 hints.ai_protocol = IPPROTO_TCP;
559 i = getaddrinfo(host, port_s, &hints, &res_list);
560 if (i != 0) {
561 ERR("Unable to translate the host address (%s).", gai_strerror(i));
562 return -1;
563 }
564
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200565 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100566 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
567 if (sock == -1) {
568 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200569 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100570 }
571
572 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
573 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100574 close(sock);
575 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200576 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100577 }
578
Michal Vasko0190bc32016-03-02 15:47:49 +0100579 /* make the socket non-blocking */
580 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
581 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100582 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200583 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100584 return -1;
585 }
586
Michal Vasko086311b2016-01-08 09:53:11 +0100587 /* we're done, network connection established */
588 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100589 }
590
Michal Vasko29af44b2016-10-13 10:59:55 +0200591 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100592 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 +0100593 }
594 freeaddrinfo(res_list);
595
596 return sock;
597}
598
Michal Vasko086311b2016-01-08 09:53:11 +0100599static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100600get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100601{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100602 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100603 char *ptr;
604 const char *str_msgid;
605 uint64_t cur_msgid;
606 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100607 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100608 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
609
Michal Vaskoade892d2017-02-22 13:40:35 +0100610 r = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100611 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100612 /* error */
613 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100614 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100615 /* timeout */
616 return NC_MSG_WOULDBLOCK;
617 }
618
619 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200620 if (!msgid && session->opts.client.notifs) {
621 cont = session->opts.client.notifs;
622 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100623
Michal Vasko71ba2da2016-05-04 10:53:16 +0200624 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100625 free(cont);
626
Michal Vasko71ba2da2016-05-04 10:53:16 +0200627 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100628 }
629
630 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200631 if (msgid && session->opts.client.replies) {
632 cont = session->opts.client.replies;
633 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100634
Michal Vasko71ba2da2016-05-04 10:53:16 +0200635 xml = cont->msg;
636 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100637
Michal Vasko71ba2da2016-05-04 10:53:16 +0200638 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100639 }
640
Michal Vasko71ba2da2016-05-04 10:53:16 +0200641 if (!msgtype) {
642 /* read message from wire */
643 msgtype = nc_read_msg_poll(session, timeout, &xml);
644 }
Michal Vasko086311b2016-01-08 09:53:11 +0100645
646 /* we read rpc-reply, want a notif */
647 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200648 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +0100649 while (*cont_ptr) {
650 cont_ptr = &((*cont_ptr)->next);
651 }
652 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100653 if (!*cont_ptr) {
654 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +0100655 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100656 lyxml_free(session->ctx, xml);
657 return NC_MSG_ERROR;
658 }
Michal Vasko086311b2016-01-08 09:53:11 +0100659 (*cont_ptr)->msg = xml;
660 (*cont_ptr)->next = NULL;
661 }
662
663 /* we read notif, want a rpc-reply */
664 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +0100665 if (!session->opts.client.ntf_tid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100666 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100667 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100668 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100669 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +0100670 }
Michal Vasko086311b2016-01-08 09:53:11 +0100671
Michal Vasko2e6defd2016-10-07 15:48:15 +0200672 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +0100673 while (*cont_ptr) {
674 cont_ptr = &((*cont_ptr)->next);
675 }
676 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100677 if (!cont_ptr) {
678 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +0100679 nc_session_unlock(session, timeout, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100680 lyxml_free(session->ctx, xml);
681 return NC_MSG_ERROR;
682 }
Michal Vasko086311b2016-01-08 09:53:11 +0100683 (*cont_ptr)->msg = xml;
684 (*cont_ptr)->next = NULL;
685 }
686
Michal Vaskoade892d2017-02-22 13:40:35 +0100687 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +0100688
689 switch (msgtype) {
690 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100691 if (!msgid) {
692 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100693 }
Michal Vasko086311b2016-01-08 09:53:11 +0100694 break;
695
696 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100697 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200698 /* check message-id */
699 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
700 if (!str_msgid) {
701 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
702 msgtype = NC_MSG_REPLY_ERR_MSGID;
703 } else {
704 cur_msgid = strtoul(str_msgid, &ptr, 10);
705 if (cur_msgid != msgid) {
706 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
707 session->id, str_msgid);
708 msgtype = NC_MSG_REPLY_ERR_MSGID;
709 }
710 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100711 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100712 }
Michal Vasko086311b2016-01-08 09:53:11 +0100713 break;
714
715 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100716 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100717 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200718 msgtype = NC_MSG_ERROR;
719 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100720
721 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100722 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100723 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200724 msgtype = NC_MSG_ERROR;
725 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100726
727 default:
728 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
729 * NC_MSG_NONE is not returned by nc_read_msg()
730 */
731 break;
732 }
733
734 return msgtype;
735}
736
737/* cannot strictly fail, but does not need to fill any error parameter at all */
738static void
739parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
740{
741 struct lyxml_elem *iter, *next, *info;
742
743 LY_TREE_FOR(xml->child, iter) {
744 if (!iter->ns) {
745 if (iter->content) {
746 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
747 } else {
748 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
749 }
750 continue;
751 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
752 if (iter->content) {
753 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
754 iter->name, iter->content, iter->ns->value);
755 } else {
756 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
757 }
758 continue;
759 }
760
761 if (!strcmp(iter->name, "error-type")) {
762 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
763 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
764 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
765 } else if (err->type) {
766 WRN("<rpc-error> <error-type> duplicated.");
767 } else {
768 err->type = lydict_insert(ctx, iter->content, 0);
769 }
770 } else if (!strcmp(iter->name, "error-tag")) {
771 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
772 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
773 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
774 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
775 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
776 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
777 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
778 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
779 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
780 && strcmp(iter->content, "malformed-message"))) {
781 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
782 } else if (err->tag) {
783 WRN("<rpc-error> <error-tag> duplicated.");
784 } else {
785 err->tag = lydict_insert(ctx, iter->content, 0);
786 }
787 } else if (!strcmp(iter->name, "error-severity")) {
788 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
789 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
790 } else if (err->severity) {
791 WRN("<rpc-error> <error-severity> duplicated.");
792 } else {
793 err->severity = lydict_insert(ctx, iter->content, 0);
794 }
795 } else if (!strcmp(iter->name, "error-app-tag")) {
796 if (err->apptag) {
797 WRN("<rpc-error> <error-app-tag> duplicated.");
798 } else {
799 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
800 }
801 } else if (!strcmp(iter->name, "error-path")) {
802 if (err->path) {
803 WRN("<rpc-error> <error-path> duplicated.");
804 } else {
805 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
806 }
807 } else if (!strcmp(iter->name, "error-message")) {
808 if (err->message) {
809 WRN("<rpc-error> <error-message> duplicated.");
810 } else {
811 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
812 if (!err->message_lang) {
813 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
814 }
815 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
816 }
817 } else if (!strcmp(iter->name, "error-info")) {
818 LY_TREE_FOR_SAFE(iter->child, next, info) {
819 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
820 if (!strcmp(info->name, "session-id")) {
821 if (err->sid) {
822 WRN("<rpc-error> <error-info> <session-id> duplicated.");
823 } else {
824 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
825 }
826 } else if (!strcmp(info->name, "bad-attr")) {
827 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100828 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
829 if (!err->attr) {
830 ERRMEM;
831 return;
832 }
Michal Vasko086311b2016-01-08 09:53:11 +0100833 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
834 } else if (!strcmp(info->name, "bad-element")) {
835 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100836 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
837 if (!err->elem) {
838 ERRMEM;
839 return;
840 }
Michal Vasko086311b2016-01-08 09:53:11 +0100841 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
842 } else if (!strcmp(info->name, "bad-namespace")) {
843 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100844 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
845 if (!err->ns) {
846 ERRMEM;
847 return;
848 }
Michal Vasko086311b2016-01-08 09:53:11 +0100849 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
850 } else {
851 if (info->content) {
852 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
853 info->name, info->content);
854 } else {
855 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
856 }
857 }
858 } else {
859 lyxml_unlink(ctx, info);
860 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100861 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
862 if (!err->other) {
863 ERRMEM;
864 return;
865 }
Michal Vasko086311b2016-01-08 09:53:11 +0100866 err->other[err->other_count - 1] = info;
867 }
868 }
869 } else {
870 if (iter->content) {
871 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
872 } else {
873 WRN("<rpc-error> unknown child \"%s\".", iter->name);
874 }
875 }
876 }
877}
878
879static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100880parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100881{
882 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +0200883 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100884 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100885 struct nc_reply_data *data_rpl;
886 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200887 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100888 int i;
889
890 if (!xml->child) {
891 ERR("An empty <rpc-reply>.");
892 return NULL;
893 }
894
895 /* rpc-error */
896 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
897 /* count and check elements */
898 i = 0;
899 LY_TREE_FOR(xml->child, iter) {
900 if (strcmp(iter->name, "rpc-error")) {
901 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
902 return NULL;
903 } else if (!iter->ns) {
904 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
905 return NULL;
906 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
907 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
908 return NULL;
909 }
910 ++i;
911 }
912
913 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100914 if (!error_rpl) {
915 ERRMEM;
916 return NULL;
917 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100918 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100919 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100920 if (!error_rpl->err) {
921 ERRMEM;
922 free(error_rpl);
923 return NULL;
924 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100925 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100926 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100927 reply = (struct nc_reply *)error_rpl;
928
929 i = 0;
930 LY_TREE_FOR(xml->child, iter) {
931 parse_rpc_error(ctx, iter, error_rpl->err + i);
932 ++i;
933 }
934
935 /* ok */
936 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
937 if (xml->child->next) {
938 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
939 return NULL;
940 }
941 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100942 if (!reply) {
943 ERRMEM;
944 return NULL;
945 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100946 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100947
948 /* some RPC output */
949 } else {
950 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200951 case NC_RPC_ACT_GENERIC:
952 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100953
954 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +0200955 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100956 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +0100957 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 +0200958 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200959 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100960 return NULL;
961 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200962 }
Michal Vasko086311b2016-01-08 09:53:11 +0100963 break;
964
965 case NC_RPC_GETCONFIG:
966 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100967 if (!xml->child->child) {
968 /* we did not receive any data */
969 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100970 if (!data_rpl) {
971 ERRMEM;
972 return NULL;
973 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100974 data_rpl->type = NC_RPL_DATA;
975 data_rpl->data = NULL;
976 return (struct nc_reply *)data_rpl;
977 }
978
Michal Vasko086311b2016-01-08 09:53:11 +0100979 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +0100980 data = lyd_parse_xml(ctx, &xml->child->child,
981 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +0100982 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100983 if (!data) {
984 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
985 return NULL;
986 }
987 break;
988
989 case NC_RPC_GETSCHEMA:
Michal Vaskoe1708602016-10-18 12:17:22 +0200990 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL), "get-schema");
991 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100992 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100993 return NULL;
994 }
995 break;
996
997 case NC_RPC_EDIT:
998 case NC_RPC_COPY:
999 case NC_RPC_DELETE:
1000 case NC_RPC_LOCK:
1001 case NC_RPC_UNLOCK:
1002 case NC_RPC_KILL:
1003 case NC_RPC_COMMIT:
1004 case NC_RPC_DISCARD:
1005 case NC_RPC_CANCEL:
1006 case NC_RPC_VALIDATE:
1007 case NC_RPC_SUBSCRIBE:
1008 /* there is no output defined */
1009 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1010 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001011 default:
1012 ERRINT;
1013 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001014 }
1015
1016 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001017 if (!data_rpl) {
1018 ERRMEM;
1019 return NULL;
1020 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001021 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +01001022 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001023 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001024 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001025 } else {
1026 /* <get>, <get-config> */
1027 data_rpl->data = data;
1028 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001029 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +01001030 if (!data_rpl->data) {
1031 ERR("Failed to parse <rpc-reply>.");
1032 free(data_rpl);
1033 return NULL;
1034 }
1035 reply = (struct nc_reply *)data_rpl;
1036 }
1037
1038 return reply;
1039}
1040
Radek Krejci53691be2016-02-22 13:58:37 +01001041#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001042
Michal Vasko3031aae2016-01-27 16:07:18 +01001043int
1044nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1045{
1046 int sock;
1047
Michal Vasko45e53ae2016-04-07 11:46:03 +02001048 if (!address) {
1049 ERRARG("address");
1050 return -1;
1051 } else if (!port) {
1052 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001053 return -1;
1054 }
1055
1056 sock = nc_sock_listen(address, port);
1057 if (sock == -1) {
1058 return -1;
1059 }
1060
1061 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001062 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1063 if (!client_opts.ch_binds) {
1064 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001065 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001066 return -1;
1067 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001068
Michal Vasko2e6defd2016-10-07 15:48:15 +02001069 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1070 if (!client_opts.ch_bind_ti) {
1071 ERRMEM;
1072 close(sock);
1073 return -1;
1074 }
1075 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1076
Michal Vasko3031aae2016-01-27 16:07:18 +01001077 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001078 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1079 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001080 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001081 return -1;
1082 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001083 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1084 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001085 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001086
1087 return 0;
1088}
1089
1090int
1091nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1092{
1093 uint32_t i;
1094 int ret = -1;
1095
1096 if (!address && !port && !ti) {
1097 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1098 close(client_opts.ch_binds[i].sock);
1099 free((char *)client_opts.ch_binds[i].address);
1100
1101 ret = 0;
1102 }
1103 free(client_opts.ch_binds);
1104 client_opts.ch_binds = NULL;
1105 client_opts.ch_bind_count = 0;
1106 } else {
1107 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1108 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1109 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001110 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001111 close(client_opts.ch_binds[i].sock);
1112 free((char *)client_opts.ch_binds[i].address);
1113
1114 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001115 if (!client_opts.ch_bind_count) {
1116 free(client_opts.ch_binds);
1117 client_opts.ch_binds = NULL;
1118 } else if (i < client_opts.ch_bind_count) {
1119 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1120 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1121 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001122
1123 ret = 0;
1124 }
1125 }
1126 }
1127
1128 return ret;
1129}
1130
1131API int
1132nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1133{
1134 int sock;
1135 char *host = NULL;
1136 uint16_t port, idx;
1137
Michal Vasko45e53ae2016-04-07 11:46:03 +02001138 if (!client_opts.ch_binds) {
1139 ERRINIT;
1140 return -1;
1141 } else if (!session) {
1142 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001143 return -1;
1144 }
1145
1146 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1147
Michal Vasko50456e82016-02-02 12:16:08 +01001148 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001149 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001150 return sock;
1151 }
1152
Radek Krejci53691be2016-02-22 13:58:37 +01001153#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001154 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001155 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001156 } else
1157#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001158#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001159 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001160 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001161 } else
1162#endif
1163 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001164 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001165 *session = NULL;
1166 }
1167
1168 free(host);
1169
1170 if (!(*session)) {
1171 return -1;
1172 }
1173
1174 return 1;
1175}
1176
Radek Krejci53691be2016-02-22 13:58:37 +01001177#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001178
Michal Vaskobdfb5242016-05-24 09:11:01 +02001179API const char **
1180nc_session_get_cpblts(const struct nc_session *session)
1181{
1182 if (!session) {
1183 ERRARG("session");
1184 return NULL;
1185 }
1186
Michal Vasko2e6defd2016-10-07 15:48:15 +02001187 return session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001188}
1189
1190API const char *
1191nc_session_cpblt(const struct nc_session *session, const char *capab)
1192{
1193 int i, len;
1194
1195 if (!session) {
1196 ERRARG("session");
1197 return NULL;
1198 } else if (!capab) {
1199 ERRARG("capab");
1200 return NULL;
1201 }
1202
1203 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001204 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1205 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1206 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001207 }
1208 }
1209
1210 return NULL;
1211}
1212
Michal Vasko9cd26a82016-05-31 08:58:48 +02001213API int
1214nc_session_ntf_thread_running(const struct nc_session *session)
1215{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001216 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001217 ERRARG("session");
1218 return 0;
1219 }
1220
Michal Vasko2e6defd2016-10-07 15:48:15 +02001221 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001222}
1223
Michal Vaskob7558c52016-02-26 15:04:19 +01001224API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001225nc_client_init(void)
1226{
1227 nc_init();
1228}
1229
1230API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001231nc_client_destroy(void)
1232{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001233 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001234}
1235
Michal Vasko086311b2016-01-08 09:53:11 +01001236API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001237nc_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 +01001238{
1239 struct lyxml_elem *xml;
1240 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1241
Michal Vasko45e53ae2016-04-07 11:46:03 +02001242 if (!session) {
1243 ERRARG("session");
1244 return NC_MSG_ERROR;
1245 } else if (!rpc) {
1246 ERRARG("rpc");
1247 return NC_MSG_ERROR;
1248 } else if (!reply) {
1249 ERRARG("reply");
1250 return NC_MSG_ERROR;
1251 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1252 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001253 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001254 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001255 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001256 return NC_MSG_ERROR;
1257 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001258 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001259 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1260 parseroptions &= LYD_OPT_STRICT;
1261 }
Michal Vasko41adf392017-01-17 10:39:04 +01001262 /* no mechanism to check external dependencies is provided */
1263 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01001264 *reply = NULL;
1265
1266 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001267
Michal Vasko71ba2da2016-05-04 10:53:16 +02001268 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001269 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001270 lyxml_free(session->ctx, xml);
1271 if (!(*reply)) {
1272 return NC_MSG_ERROR;
1273 }
1274 }
1275
1276 return msgtype;
1277}
1278
1279API NC_MSG_TYPE
1280nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1281{
1282 struct lyxml_elem *xml, *ev_time;
1283 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1284
Michal Vasko45e53ae2016-04-07 11:46:03 +02001285 if (!session) {
1286 ERRARG("session");
1287 return NC_MSG_ERROR;
1288 } else if (!notif) {
1289 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001290 return NC_MSG_ERROR;
1291 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001292 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001293 return NC_MSG_ERROR;
1294 }
1295
1296 msgtype = get_msg(session, timeout, 0, &xml);
1297
1298 if (msgtype == NC_MSG_NOTIF) {
1299 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001300 if (!*notif) {
1301 ERRMEM;
1302 lyxml_free(session->ctx, xml);
1303 return NC_MSG_ERROR;
1304 }
Michal Vasko086311b2016-01-08 09:53:11 +01001305
1306 /* eventTime */
1307 LY_TREE_FOR(xml->child, ev_time) {
1308 if (!strcmp(ev_time->name, "eventTime")) {
1309 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1310 /* lyd_parse does not know this element */
1311 lyxml_free(session->ctx, ev_time);
1312 break;
1313 }
1314 }
1315 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001316 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001317 goto fail;
1318 }
1319
1320 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01001321 (*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 +01001322 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001323 lyxml_free(session->ctx, xml);
1324 xml = NULL;
1325 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001326 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001327 goto fail;
1328 }
1329 }
1330
1331 return msgtype;
1332
1333fail:
1334 lydict_remove(session->ctx, (*notif)->datetime);
1335 lyd_free((*notif)->tree);
1336 free(*notif);
1337 *notif = NULL;
1338 lyxml_free(session->ctx, xml);
1339
1340 return NC_MSG_ERROR;
1341}
1342
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001343static void *
1344nc_recv_notif_thread(void *arg)
1345{
1346 struct nc_ntf_thread_arg *ntarg;
1347 struct nc_session *session;
1348 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1349 struct nc_notif *notif;
1350 NC_MSG_TYPE msgtype;
1351
1352 ntarg = (struct nc_ntf_thread_arg *)arg;
1353 session = ntarg->session;
1354 notif_clb = ntarg->notif_clb;
1355 free(ntarg);
1356
Michal Vasko2e6defd2016-10-07 15:48:15 +02001357 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001358 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001359 if (msgtype == NC_MSG_NOTIF) {
1360 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001361 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1362 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1363 nc_notif_free(notif);
1364 break;
1365 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001366 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001367 } else if (msgtype == NC_MSG_ERROR) {
1368 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001369 }
1370
1371 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1372 }
1373
Michal Vasko0651c902016-05-19 15:55:42 +02001374 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001375 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001376 return NULL;
1377}
1378
1379API int
1380nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1381{
1382 struct nc_ntf_thread_arg *ntarg;
1383 int ret;
1384
Michal Vasko45e53ae2016-04-07 11:46:03 +02001385 if (!session) {
1386 ERRARG("session");
1387 return -1;
1388 } else if (!notif_clb) {
1389 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001390 return -1;
1391 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1392 ERR("Session %u: invalid session to receive Notifications.", session->id);
1393 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001394 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001395 ERR("Session %u: separate notification thread is already running.", session->id);
1396 return -1;
1397 }
1398
1399 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001400 if (!ntarg) {
1401 ERRMEM;
1402 return -1;
1403 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001404 ntarg->session = session;
1405 ntarg->notif_clb = notif_clb;
1406
1407 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001408 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1409 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001410 ERRMEM;
1411 free(ntarg);
1412 return -1;
1413 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001414
Michal Vasko2e6defd2016-10-07 15:48:15 +02001415 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001416 if (ret) {
1417 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1418 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001419 free((pthread_t *)session->opts.client.ntf_tid);
1420 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001421 return -1;
1422 }
1423
1424 return 0;
1425}
1426
Michal Vasko086311b2016-01-08 09:53:11 +01001427API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001428nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001429{
1430 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001431 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001432 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001433 struct nc_rpc_getconfig *rpc_gc;
1434 struct nc_rpc_edit *rpc_e;
1435 struct nc_rpc_copy *rpc_cp;
1436 struct nc_rpc_delete *rpc_del;
1437 struct nc_rpc_lock *rpc_lock;
1438 struct nc_rpc_get *rpc_g;
1439 struct nc_rpc_kill *rpc_k;
1440 struct nc_rpc_commit *rpc_com;
1441 struct nc_rpc_cancel *rpc_can;
1442 struct nc_rpc_validate *rpc_val;
1443 struct nc_rpc_getschema *rpc_gs;
1444 struct nc_rpc_subscribe *rpc_sub;
1445 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001446 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001447 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001448 uint64_t cur_msgid;
1449
Michal Vasko45e53ae2016-04-07 11:46:03 +02001450 if (!session) {
1451 ERRARG("session");
1452 return NC_MSG_ERROR;
1453 } else if (!rpc) {
1454 ERRARG("rpc");
1455 return NC_MSG_ERROR;
1456 } else if (!msgid) {
1457 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001458 return NC_MSG_ERROR;
1459 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001460 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001461 return NC_MSG_ERROR;
1462 }
1463
Michal Vasko90e8e692016-07-13 12:27:57 +02001464 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001465 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1466 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01001467 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001468 return NC_MSG_ERROR;
1469 }
1470 }
1471
1472 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001473 case NC_RPC_ACT_GENERIC:
1474 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001475
1476 if (rpc_gen->has_data) {
1477 data = rpc_gen->content.data;
1478 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01001479 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 +01001480 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001481 }
1482 break;
1483
1484 case NC_RPC_GETCONFIG:
1485 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1486
1487 data = lyd_new(NULL, ietfnc, "get-config");
1488 node = lyd_new(data, ietfnc, "source");
1489 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1490 if (!node) {
1491 lyd_free(data);
1492 return NC_MSG_ERROR;
1493 }
1494 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001495 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001496 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001497 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001498 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001499 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001500 lyd_insert_attr(node, NULL, "type", "xpath");
1501 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001502 }
1503 if (!node) {
1504 lyd_free(data);
1505 return NC_MSG_ERROR;
1506 }
1507 }
1508
1509 if (rpc_gc->wd_mode) {
1510 if (!ietfncwd) {
1511 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1512 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001513 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001514 return NC_MSG_ERROR;
1515 }
1516 }
1517 switch (rpc_gc->wd_mode) {
1518 case NC_WD_UNKNOWN:
1519 /* cannot get here */
1520 break;
1521 case NC_WD_ALL:
1522 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1523 break;
1524 case NC_WD_ALL_TAG:
1525 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1526 break;
1527 case NC_WD_TRIM:
1528 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1529 break;
1530 case NC_WD_EXPLICIT:
1531 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1532 break;
1533 }
1534 if (!node) {
1535 lyd_free(data);
1536 return NC_MSG_ERROR;
1537 }
1538 }
1539 break;
1540
1541 case NC_RPC_EDIT:
1542 rpc_e = (struct nc_rpc_edit *)rpc;
1543
1544 data = lyd_new(NULL, ietfnc, "edit-config");
1545 node = lyd_new(data, ietfnc, "target");
1546 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1547 if (!node) {
1548 lyd_free(data);
1549 return NC_MSG_ERROR;
1550 }
1551
1552 if (rpc_e->default_op) {
1553 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1554 if (!node) {
1555 lyd_free(data);
1556 return NC_MSG_ERROR;
1557 }
1558 }
1559
1560 if (rpc_e->test_opt) {
1561 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1562 if (!node) {
1563 lyd_free(data);
1564 return NC_MSG_ERROR;
1565 }
1566 }
1567
1568 if (rpc_e->error_opt) {
1569 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1570 if (!node) {
1571 lyd_free(data);
1572 return NC_MSG_ERROR;
1573 }
1574 }
1575
Michal Vasko7793bc62016-09-16 11:58:41 +02001576 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001577 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001578 } else {
1579 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1580 }
1581 if (!node) {
1582 lyd_free(data);
1583 return NC_MSG_ERROR;
1584 }
1585 break;
1586
1587 case NC_RPC_COPY:
1588 rpc_cp = (struct nc_rpc_copy *)rpc;
1589
1590 data = lyd_new(NULL, ietfnc, "copy-config");
1591 node = lyd_new(data, ietfnc, "target");
1592 if (rpc_cp->url_trg) {
1593 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1594 } else {
1595 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1596 }
1597 if (!node) {
1598 lyd_free(data);
1599 return NC_MSG_ERROR;
1600 }
1601
1602 node = lyd_new(data, ietfnc, "source");
1603 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001604 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001605 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001606 } else {
1607 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1608 }
1609 } else {
1610 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1611 }
1612 if (!node) {
1613 lyd_free(data);
1614 return NC_MSG_ERROR;
1615 }
1616
1617 if (rpc_cp->wd_mode) {
1618 if (!ietfncwd) {
1619 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1620 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001621 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001622 return NC_MSG_ERROR;
1623 }
1624 }
1625 switch (rpc_cp->wd_mode) {
1626 case NC_WD_UNKNOWN:
1627 /* cannot get here */
1628 break;
1629 case NC_WD_ALL:
1630 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1631 break;
1632 case NC_WD_ALL_TAG:
1633 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1634 break;
1635 case NC_WD_TRIM:
1636 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1637 break;
1638 case NC_WD_EXPLICIT:
1639 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1640 break;
1641 }
1642 if (!node) {
1643 lyd_free(data);
1644 return NC_MSG_ERROR;
1645 }
1646 }
1647 break;
1648
1649 case NC_RPC_DELETE:
1650 rpc_del = (struct nc_rpc_delete *)rpc;
1651
1652 data = lyd_new(NULL, ietfnc, "delete-config");
1653 node = lyd_new(data, ietfnc, "target");
1654 if (rpc_del->url) {
1655 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1656 } else {
1657 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1658 }
1659 if (!node) {
1660 lyd_free(data);
1661 return NC_MSG_ERROR;
1662 }
1663 break;
1664
1665 case NC_RPC_LOCK:
1666 rpc_lock = (struct nc_rpc_lock *)rpc;
1667
1668 data = lyd_new(NULL, ietfnc, "lock");
1669 node = lyd_new(data, ietfnc, "target");
1670 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1671 if (!node) {
1672 lyd_free(data);
1673 return NC_MSG_ERROR;
1674 }
1675 break;
1676
1677 case NC_RPC_UNLOCK:
1678 rpc_lock = (struct nc_rpc_lock *)rpc;
1679
1680 data = lyd_new(NULL, ietfnc, "unlock");
1681 node = lyd_new(data, ietfnc, "target");
1682 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1683 if (!node) {
1684 lyd_free(data);
1685 return NC_MSG_ERROR;
1686 }
1687 break;
1688
1689 case NC_RPC_GET:
1690 rpc_g = (struct nc_rpc_get *)rpc;
1691
1692 data = lyd_new(NULL, ietfnc, "get");
1693 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001694 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001695 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001696 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001697 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001698 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001699 lyd_insert_attr(node, NULL, "type", "xpath");
1700 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001701 }
1702 if (!node) {
1703 lyd_free(data);
1704 return NC_MSG_ERROR;
1705 }
1706 }
1707
1708 if (rpc_g->wd_mode) {
1709 if (!ietfncwd) {
1710 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1711 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01001712 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001713 return NC_MSG_ERROR;
1714 }
1715 }
1716 switch (rpc_g->wd_mode) {
1717 case NC_WD_UNKNOWN:
1718 /* cannot get here */
1719 break;
1720 case NC_WD_ALL:
1721 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1722 break;
1723 case NC_WD_ALL_TAG:
1724 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1725 break;
1726 case NC_WD_TRIM:
1727 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1728 break;
1729 case NC_WD_EXPLICIT:
1730 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1731 break;
1732 }
1733 if (!node) {
1734 lyd_free(data);
1735 return NC_MSG_ERROR;
1736 }
1737 }
1738 break;
1739
1740 case NC_RPC_KILL:
1741 rpc_k = (struct nc_rpc_kill *)rpc;
1742
1743 data = lyd_new(NULL, ietfnc, "kill-session");
1744 sprintf(str, "%u", rpc_k->sid);
1745 lyd_new_leaf(data, ietfnc, "session-id", str);
1746 break;
1747
1748 case NC_RPC_COMMIT:
1749 rpc_com = (struct nc_rpc_commit *)rpc;
1750
1751 data = lyd_new(NULL, ietfnc, "commit");
1752 if (rpc_com->confirmed) {
1753 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1754 }
1755
1756 if (rpc_com->confirm_timeout) {
1757 sprintf(str, "%u", rpc_com->confirm_timeout);
1758 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1759 }
1760
1761 if (rpc_com->persist) {
1762 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1763 if (!node) {
1764 lyd_free(data);
1765 return NC_MSG_ERROR;
1766 }
1767 }
1768
1769 if (rpc_com->persist_id) {
1770 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1771 if (!node) {
1772 lyd_free(data);
1773 return NC_MSG_ERROR;
1774 }
1775 }
1776 break;
1777
1778 case NC_RPC_DISCARD:
1779 data = lyd_new(NULL, ietfnc, "discard-changes");
1780 break;
1781
1782 case NC_RPC_CANCEL:
1783 rpc_can = (struct nc_rpc_cancel *)rpc;
1784
1785 data = lyd_new(NULL, ietfnc, "cancel-commit");
1786 if (rpc_can->persist_id) {
1787 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1788 if (!node) {
1789 lyd_free(data);
1790 return NC_MSG_ERROR;
1791 }
1792 }
1793 break;
1794
1795 case NC_RPC_VALIDATE:
1796 rpc_val = (struct nc_rpc_validate *)rpc;
1797
1798 data = lyd_new(NULL, ietfnc, "validate");
1799 node = lyd_new(data, ietfnc, "source");
1800 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001801 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001802 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001803 } else {
1804 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1805 }
1806 } else {
1807 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1808 }
1809 if (!node) {
1810 lyd_free(data);
1811 return NC_MSG_ERROR;
1812 }
1813 break;
1814
1815 case NC_RPC_GETSCHEMA:
1816 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1817 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01001818 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001819 return NC_MSG_ERROR;
1820 }
1821
1822 rpc_gs = (struct nc_rpc_getschema *)rpc;
1823
1824 data = lyd_new(NULL, ietfncmon, "get-schema");
1825 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1826 if (!node) {
1827 lyd_free(data);
1828 return NC_MSG_ERROR;
1829 }
1830 if (rpc_gs->version) {
1831 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1832 if (!node) {
1833 lyd_free(data);
1834 return NC_MSG_ERROR;
1835 }
1836 }
1837 if (rpc_gs->format) {
1838 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1839 if (!node) {
1840 lyd_free(data);
1841 return NC_MSG_ERROR;
1842 }
1843 }
1844 break;
1845
1846 case NC_RPC_SUBSCRIBE:
1847 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1848 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01001849 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001850 return NC_MSG_ERROR;
1851 }
1852
1853 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1854
1855 data = lyd_new(NULL, notifs, "create-subscription");
1856 if (rpc_sub->stream) {
1857 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1858 if (!node) {
1859 lyd_free(data);
1860 return NC_MSG_ERROR;
1861 }
1862 }
1863
1864 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001865 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001866 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001867 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001868 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001869 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001870 lyd_insert_attr(node, NULL, "type", "xpath");
1871 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001872 }
1873 if (!node) {
1874 lyd_free(data);
1875 return NC_MSG_ERROR;
1876 }
1877 }
1878
1879 if (rpc_sub->start) {
1880 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1881 if (!node) {
1882 lyd_free(data);
1883 return NC_MSG_ERROR;
1884 }
1885 }
1886
1887 if (rpc_sub->stop) {
1888 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1889 if (!node) {
1890 lyd_free(data);
1891 return NC_MSG_ERROR;
1892 }
1893 }
1894 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001895 default:
1896 ERRINT;
1897 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001898 }
1899
Michal Vasko41adf392017-01-17 10:39:04 +01001900 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
1901 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001902 lyd_free(data);
1903 return NC_MSG_ERROR;
1904 }
1905
Michal Vaskoade892d2017-02-22 13:40:35 +01001906 ret = nc_session_lock(session, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001907 if (ret == -1) {
1908 /* error */
1909 r = NC_MSG_ERROR;
1910 } else if (!ret) {
1911 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001912 r = NC_MSG_WOULDBLOCK;
1913 } else {
1914 /* send RPC, store its message ID */
1915 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001916 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01001917 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001918 nc_session_unlock(session, timeout, __func__);
Michal Vasko086311b2016-01-08 09:53:11 +01001919
1920 lyd_free(data);
1921
1922 if (r != NC_MSG_RPC) {
1923 return r;
1924 }
1925
1926 *msgid = cur_msgid;
1927 return NC_MSG_RPC;
1928}
Michal Vaskode2946c2017-01-12 12:19:26 +01001929
1930API void
1931nc_client_session_set_not_strict(struct nc_session *session)
1932{
1933 if (session->side != NC_CLIENT) {
1934 ERRARG("session");
1935 return;
1936 }
1937
1938 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
1939}