blob: 2236dd7e94b29bec643122351ad2a54fe73e3279 [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>
25#include <sys/types.h>
26#include <unistd.h>
27#include <arpa/inet.h>
28#include <poll.h>
29
30#include <libyang/libyang.h>
31
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010033#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010034#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010035
Michal Vasko80ef5d22016-01-18 09:21:02 +010036static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
37
Michal Vaskodaf9a092016-02-09 10:42:05 +010038struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010039
40API int
Michal Vasko3031aae2016-01-27 16:07:18 +010041nc_client_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010042{
Michal Vasko3031aae2016-01-27 16:07:18 +010043 if (client_opts.schema_searchpath) {
44 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010045 }
Michal Vasko086311b2016-01-08 09:53:11 +010046
Michal Vasko7f1c78b2016-01-19 09:52:14 +010047 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010048 client_opts.schema_searchpath = strdup(path);
49 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010050 ERRMEM;
51 return 1;
52 }
53 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010054 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010055 }
56
57 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010058}
59
Michal Vasko3031aae2016-01-27 16:07:18 +010060/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010061static int
62ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
63{
64 const struct lys_module *module;
65 char *ptr, *ptr2;
66 char *model_name, *revision = NULL, *features = NULL;
67
68 /* parse module */
69 ptr = strstr(cpblt, "module=");
70 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010071 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
72 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010073 }
74 ptr += 7;
75 ptr2 = strchr(ptr, '&');
76 if (!ptr2) {
77 ptr2 = ptr + strlen(ptr);
78 }
79 model_name = strndup(ptr, ptr2 - ptr);
80
81 /* parse revision */
82 ptr = strstr(cpblt, "revision=");
83 if (ptr) {
84 ptr += 9;
85 ptr2 = strchr(ptr, '&');
86 if (!ptr2) {
87 ptr2 = ptr + strlen(ptr);
88 }
89 revision = strndup(ptr, ptr2 - ptr);
90 }
91
92 /* load module if needed */
93 module = ly_ctx_get_module(session->ctx, model_name, revision);
94 if (!module) {
95 module = ly_ctx_load_module(session->ctx, model_name, revision);
96 }
97
Michal Vasko086311b2016-01-08 09:53:11 +010098 free(revision);
99 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100100 WRN("Failed to load model \"%s\".", model_name);
101 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100102 return 1;
103 }
Michal Vaskoef578332016-01-25 13:20:09 +0100104 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100105
106 /* parse features */
107 ptr = strstr(cpblt, "features=");
108 if (ptr) {
109 ptr += 9;
110 ptr2 = strchr(ptr, '&');
111 if (!ptr2) {
112 ptr2 = ptr + strlen(ptr);
113 }
114 features = strndup(ptr, ptr2 - ptr);
115 }
116
117 /* enable features */
118 if (features) {
119 /* basically manual strtok_r (to avoid macro) */
120 ptr2 = features;
121 for (ptr = features; *ptr; ++ptr) {
122 if (*ptr == ',') {
123 *ptr = '\0';
124 /* remember last feature */
125 ptr2 = ptr + 1;
126 }
127 }
128
129 ptr = features;
130 lys_features_enable(module, ptr);
131 while (ptr != ptr2) {
132 ptr += strlen(ptr) + 1;
133 lys_features_enable(module, ptr);
134 }
135
136 free(features);
137 }
138
139 return 0;
140}
141
Michal Vasko1aaa6602016-02-09 11:04:33 +0100142/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100143static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100144ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100145{
146 int i;
147 const struct lys_module *ietfnc;
148
149 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
150 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100151 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
152 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100153 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100154 }
155 }
156 if (!ietfnc) {
157 ERR("Loading base NETCONF schema failed.");
158 return 1;
159 }
160
161 /* set supported capabilities from ietf-netconf */
162 for (i = 0; cpblts[i]; ++i) {
163 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
164 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
165 lys_features_enable(ietfnc, "writable-running");
166 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
167 lys_features_enable(ietfnc, "candidate");
168 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
169 lys_features_enable(ietfnc, "confirmed-commit");
170 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
171 lys_features_enable(ietfnc, "rollback-on-error");
172 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
173 lys_features_enable(ietfnc, "validate");
174 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
175 lys_features_enable(ietfnc, "startup");
176 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
177 lys_features_enable(ietfnc, "url");
178 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
179 lys_features_enable(ietfnc, "xpath");
180 }
181 }
182 }
183
184 return 0;
185}
186
187static char *
188libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
189 void (**free_model_data)(char *model_data))
190{
191 struct nc_session *session = (struct nc_session *)user_data;
192 struct nc_rpc *rpc;
193 struct nc_reply *reply;
194 struct nc_reply_data *data_rpl;
195 NC_MSG_TYPE msg;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100196 char *model_data = NULL, *ptr, *ptr2, *anyxml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100197 uint64_t msgid;
198
199 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100200 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100201 *format = LYS_IN_YIN;
202
203 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
204 usleep(1000);
205 }
206 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100207 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100208 nc_rpc_free(rpc);
209 return NULL;
210 }
211
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100212 msg = nc_recv_reply(session, rpc, msgid, 250, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100213 nc_rpc_free(rpc);
214 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100215 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100216 return NULL;
217 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100218 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100219 return NULL;
220 }
221
Michal Vasko05ba9df2016-01-13 14:40:27 +0100222 if (reply->type != NC_RPL_DATA) {
223 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100225 nc_reply_free(reply);
226 return NULL;
227 }
228
Michal Vasko086311b2016-01-08 09:53:11 +0100229 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100230 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data_rpl->data)->value, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100231 nc_reply_free(reply);
232 *free_model_data = NULL;
233
234 /* it's with the data root node, remove it */
235 if (anyxml) {
236 ptr = strchr(anyxml, '>');
237 ++ptr;
238
239 ptr2 = strrchr(anyxml, '<');
240
241 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
242 free(anyxml);
243 }
244
245 return model_data;
246}
247
Michal Vaskoef578332016-01-25 13:20:09 +0100248/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100249int
250nc_ctx_check_and_fill(struct nc_session *session)
251{
Michal Vaskoef578332016-01-25 13:20:09 +0100252 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100253 ly_module_clb old_clb = NULL;
254 void *old_data = NULL;
255
256 assert(session->cpblts && session->ctx);
257
258 /* check if get-schema is supported */
259 for (i = 0; session->cpblts[i]; ++i) {
260 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
261 get_schema_support = 1;
262 break;
263 }
264 }
265
266 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
267 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
268 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
269 /* set module retrieval using <get-schema> */
270 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
271 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
272 } else {
273 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
274 }
275 }
276
277 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100278 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100279 if (old_clb) {
280 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
281 }
Michal Vaskoef578332016-01-25 13:20:09 +0100282 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100283 }
284
285 /* load all other models */
286 for (i = 0; session->cpblts[i]; ++i) {
287 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
288 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
289 continue;
290 }
291
Michal Vaskoef578332016-01-25 13:20:09 +0100292 r = ctx_check_and_load_model(session, session->cpblts[i]);
293 if (r == -1) {
294 ret = -1;
295 break;
296 }
297
298 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
299 * if it was using get-schema */
300 if (r == 1) {
301 if (get_schema_support) {
302 VRB("Trying to load the schema from a different source.");
303 /* works even if old_clb is NULL */
304 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
305 r = ctx_check_and_load_model(session, session->cpblts[i]);
306 }
307
308 /* fail again (or no other way to try), too bad */
309 if (r) {
310 ret = 1;
311 }
312
313 /* set get-schema callback back */
314 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
315 }
Michal Vasko086311b2016-01-08 09:53:11 +0100316 }
317
318 if (old_clb) {
319 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
320 }
Michal Vaskoef578332016-01-25 13:20:09 +0100321 if (ret == 1) {
322 WRN("Some models failed to be loaded, any data from these models will be ignored.");
323 }
324 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100325}
326
327API struct nc_session *
328nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
329{
Michal Vaskod083db62016-01-19 10:31:29 +0100330 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100331
Michal Vaskod083db62016-01-19 10:31:29 +0100332 if ((fdin < 0) || (fdout < 0)) {
333 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100334 return NULL;
335 }
336
337 /* prepare session structure */
338 session = calloc(1, sizeof *session);
339 if (!session) {
340 ERRMEM;
341 return NULL;
342 }
343 session->status = NC_STATUS_STARTING;
344 session->side = NC_CLIENT;
345
346 /* transport specific data */
347 session->ti_type = NC_TI_FD;
348 session->ti.fd.in = fdin;
349 session->ti.fd.out = fdout;
350
351 /* assign context (dicionary needed for handshake) */
352 if (!ctx) {
353 ctx = ly_ctx_new(SCHEMAS_DIR);
354 } else {
355 session->flags |= NC_SESSION_SHAREDCTX;
356 }
357 session->ctx = ctx;
358
359 /* NETCONF handshake */
360 if (nc_handshake(session)) {
361 goto fail;
362 }
363 session->status = NC_STATUS_RUNNING;
364
Michal Vaskoef578332016-01-25 13:20:09 +0100365 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100366 goto fail;
367 }
368
369 return session;
370
371fail:
372 nc_session_free(session);
373 return NULL;
374}
375
376int
Michal Vaskof05562c2016-01-20 12:06:43 +0100377nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100378{
379 int i, sock = -1;
380 struct addrinfo hints, *res_list, *res;
381 char port_s[6]; /* length of string representation of short int */
382
383 snprintf(port_s, 6, "%u", port);
384
385 /* Connect to a server */
386 memset(&hints, 0, sizeof hints);
387 hints.ai_family = AF_UNSPEC;
388 hints.ai_socktype = SOCK_STREAM;
389 hints.ai_protocol = IPPROTO_TCP;
390 i = getaddrinfo(host, port_s, &hints, &res_list);
391 if (i != 0) {
392 ERR("Unable to translate the host address (%s).", gai_strerror(i));
393 return -1;
394 }
395
396 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
397 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
398 if (sock == -1) {
399 /* socket was not created, try another resource */
400 i = errno;
401 goto errloop;
402 }
403
404 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
405 /* network connection failed, try another resource */
406 i = errno;
407 close(sock);
408 sock = -1;
409 goto errloop;
410 }
411
412 /* we're done, network connection established */
413 break;
414errloop:
415 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
416 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
417 continue;
418 }
419
420 if (sock == -1) {
421 ERR("Unable to connect to %s:%s.", host, port_s);
422 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100423 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 +0100424 }
425 freeaddrinfo(res_list);
426
427 return sock;
428}
429
Michal Vasko086311b2016-01-08 09:53:11 +0100430static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100431get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100432{
Michal Vasko2518b6b2016-01-28 13:24:53 +0100433 int r, elapsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100434 char *ptr;
435 const char *str_msgid;
436 uint64_t cur_msgid;
437 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100438 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100439 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
440
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100441 r = nc_timedlock(session->ti_lock, timeout, &elapsed);
442 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100443 /* error */
444 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100445 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100446 /* timeout */
447 return NC_MSG_WOULDBLOCK;
448 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100449 if (timeout > 0) {
450 timeout -= elapsed;
451 }
Michal Vasko086311b2016-01-08 09:53:11 +0100452
453 /* try to get notification from the session's queue */
454 if (!msgid && session->notifs) {
455 cont = session->notifs;
456 session->notifs = cont->next;
457
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100458 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100459
460 *msg = cont->msg;
461 free(cont);
462
463 return NC_MSG_NOTIF;
464 }
465
466 /* try to get rpc-reply from the session's queue */
467 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100468 while (session->replies) {
469 cont = session->replies;
470 session->replies = cont->next;
471
Michal Vasko086311b2016-01-08 09:53:11 +0100472 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
473 cur_msgid = strtoul(str_msgid, &ptr, 10);
474
475 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100476 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100477 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100478
479 *msg = cont->msg;
480 free(cont);
481
482 return NC_MSG_REPLY;
483 }
484
Michal Vasko2518b6b2016-01-28 13:24:53 +0100485 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
486 lyxml_free(session->ctx, cont->msg);
Michal Vaskob2d91072016-02-01 13:25:20 +0100487 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100488 }
489 }
490
491 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100492 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100493
494 /* we read rpc-reply, want a notif */
495 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100496 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100497 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
498 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100500 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100501 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100502 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100503 }
Michal Vasko086311b2016-01-08 09:53:11 +0100504
505 cont_ptr = &session->replies;
506 while (*cont_ptr) {
507 cont_ptr = &((*cont_ptr)->next);
508 }
509 *cont_ptr = malloc(sizeof **cont_ptr);
510 (*cont_ptr)->msg = xml;
511 (*cont_ptr)->next = NULL;
512 }
513
514 /* we read notif, want a rpc-reply */
515 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100516 /* TODO check whether the session is even subscribed */
517 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100518 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100519 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100520 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100521 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100522 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100523
524 cont_ptr = &session->notifs;
525 while (*cont_ptr) {
526 cont_ptr = &((*cont_ptr)->next);
527 }
528 *cont_ptr = malloc(sizeof **cont_ptr);
529 (*cont_ptr)->msg = xml;
530 (*cont_ptr)->next = NULL;
531 }
532
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100533 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100534
535 switch (msgtype) {
536 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100537 if (!msgid) {
538 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100539 }
Michal Vasko086311b2016-01-08 09:53:11 +0100540 break;
541
542 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100543 if (msgid) {
544 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100545 }
Michal Vasko086311b2016-01-08 09:53:11 +0100546 break;
547
548 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100549 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100550 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100551 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100552
553 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100554 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100555 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100556 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100557
558 default:
559 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
560 * NC_MSG_NONE is not returned by nc_read_msg()
561 */
562 break;
563 }
564
565 return msgtype;
566}
567
568/* cannot strictly fail, but does not need to fill any error parameter at all */
569static void
570parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
571{
572 struct lyxml_elem *iter, *next, *info;
573
574 LY_TREE_FOR(xml->child, iter) {
575 if (!iter->ns) {
576 if (iter->content) {
577 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
578 } else {
579 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
580 }
581 continue;
582 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
583 if (iter->content) {
584 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
585 iter->name, iter->content, iter->ns->value);
586 } else {
587 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
588 }
589 continue;
590 }
591
592 if (!strcmp(iter->name, "error-type")) {
593 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
594 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
595 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
596 } else if (err->type) {
597 WRN("<rpc-error> <error-type> duplicated.");
598 } else {
599 err->type = lydict_insert(ctx, iter->content, 0);
600 }
601 } else if (!strcmp(iter->name, "error-tag")) {
602 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
603 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
604 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
605 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
606 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
607 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
608 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
609 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
610 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
611 && strcmp(iter->content, "malformed-message"))) {
612 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
613 } else if (err->tag) {
614 WRN("<rpc-error> <error-tag> duplicated.");
615 } else {
616 err->tag = lydict_insert(ctx, iter->content, 0);
617 }
618 } else if (!strcmp(iter->name, "error-severity")) {
619 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
620 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
621 } else if (err->severity) {
622 WRN("<rpc-error> <error-severity> duplicated.");
623 } else {
624 err->severity = lydict_insert(ctx, iter->content, 0);
625 }
626 } else if (!strcmp(iter->name, "error-app-tag")) {
627 if (err->apptag) {
628 WRN("<rpc-error> <error-app-tag> duplicated.");
629 } else {
630 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
631 }
632 } else if (!strcmp(iter->name, "error-path")) {
633 if (err->path) {
634 WRN("<rpc-error> <error-path> duplicated.");
635 } else {
636 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
637 }
638 } else if (!strcmp(iter->name, "error-message")) {
639 if (err->message) {
640 WRN("<rpc-error> <error-message> duplicated.");
641 } else {
642 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
643 if (!err->message_lang) {
644 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
645 }
646 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
647 }
648 } else if (!strcmp(iter->name, "error-info")) {
649 LY_TREE_FOR_SAFE(iter->child, next, info) {
650 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
651 if (!strcmp(info->name, "session-id")) {
652 if (err->sid) {
653 WRN("<rpc-error> <error-info> <session-id> duplicated.");
654 } else {
655 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
656 }
657 } else if (!strcmp(info->name, "bad-attr")) {
658 ++err->attr_count;
659 err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr);
660 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
661 } else if (!strcmp(info->name, "bad-element")) {
662 ++err->elem_count;
663 err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem);
664 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
665 } else if (!strcmp(info->name, "bad-namespace")) {
666 ++err->ns_count;
667 err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns);
668 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
669 } else {
670 if (info->content) {
671 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
672 info->name, info->content);
673 } else {
674 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
675 }
676 }
677 } else {
678 lyxml_unlink(ctx, info);
679 ++err->other_count;
680 err->other = realloc(err->other, err->other_count * sizeof *err->other);
681 err->other[err->other_count - 1] = info;
682 }
683 }
684 } else {
685 if (iter->content) {
686 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
687 } else {
688 WRN("<rpc-error> unknown child \"%s\".", iter->name);
689 }
690 }
691 }
692}
693
694static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100695parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100696{
697 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100698 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100699 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100700 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100701 struct nc_reply_data *data_rpl;
702 struct nc_reply *reply = NULL;
703 struct nc_rpc_generic *rpc_gen;
704 int i;
705
706 if (!xml->child) {
707 ERR("An empty <rpc-reply>.");
708 return NULL;
709 }
710
711 /* rpc-error */
712 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
713 /* count and check elements */
714 i = 0;
715 LY_TREE_FOR(xml->child, iter) {
716 if (strcmp(iter->name, "rpc-error")) {
717 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
718 return NULL;
719 } else if (!iter->ns) {
720 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
721 return NULL;
722 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
723 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
724 return NULL;
725 }
726 ++i;
727 }
728
729 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100730 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100731 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100732 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100733 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100734 reply = (struct nc_reply *)error_rpl;
735
736 i = 0;
737 LY_TREE_FOR(xml->child, iter) {
738 parse_rpc_error(ctx, iter, error_rpl->err + i);
739 ++i;
740 }
741
742 /* ok */
743 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
744 if (xml->child->next) {
745 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
746 return NULL;
747 }
748 reply = malloc(sizeof *reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100749 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100750
751 /* some RPC output */
752 } else {
753 switch (rpc->type) {
754 case NC_RPC_GENERIC:
755 rpc_gen = (struct nc_rpc_generic *)rpc;
756
757 if (rpc_gen->has_data) {
758 schema = rpc_gen->content.data->schema;
759 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100760 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100761 if (!data) {
762 ERR("Failed to parse a generic RPC XML.");
763 return NULL;
764 }
765 schema = data->schema;
766 lyd_free(data);
767 data = NULL;
768 }
769 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100770 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100771 return NULL;
772 }
773 break;
774
775 case NC_RPC_GETCONFIG:
776 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100777 if (!xml->child->child) {
778 /* we did not receive any data */
779 data_rpl = malloc(sizeof *data_rpl);
780 data_rpl->type = NC_RPL_DATA;
781 data_rpl->data = NULL;
782 return (struct nc_reply *)data_rpl;
783 }
784
Michal Vasko086311b2016-01-08 09:53:11 +0100785 /* special treatment */
786 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100787 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100788 if (!data) {
789 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
790 return NULL;
791 }
792 break;
793
794 case NC_RPC_GETSCHEMA:
Michal Vaskofea54dc2016-02-17 13:12:16 +0100795 schema = ly_ctx_get_node(ctx, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100796 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100797 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100798 return NULL;
799 }
800 break;
801
802 case NC_RPC_EDIT:
803 case NC_RPC_COPY:
804 case NC_RPC_DELETE:
805 case NC_RPC_LOCK:
806 case NC_RPC_UNLOCK:
807 case NC_RPC_KILL:
808 case NC_RPC_COMMIT:
809 case NC_RPC_DISCARD:
810 case NC_RPC_CANCEL:
811 case NC_RPC_VALIDATE:
812 case NC_RPC_SUBSCRIBE:
813 /* there is no output defined */
814 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
815 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100816 default:
817 ERRINT;
818 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100819 }
820
821 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100822 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100823 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100824 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100825 } else {
826 /* <get>, <get-config> */
827 data_rpl->data = data;
828 }
829 if (!data_rpl->data) {
830 ERR("Failed to parse <rpc-reply>.");
831 free(data_rpl);
832 return NULL;
833 }
834 reply = (struct nc_reply *)data_rpl;
835 }
836
837 return reply;
838}
839
Radek Krejci53691be2016-02-22 13:58:37 +0100840#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100841
Michal Vasko3031aae2016-01-27 16:07:18 +0100842int
843nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
844{
845 int sock;
846
847 if (!address || !port) {
848 ERRARG;
849 return -1;
850 }
851
852 sock = nc_sock_listen(address, port);
853 if (sock == -1) {
854 return -1;
855 }
856
857 ++client_opts.ch_bind_count;
858 client_opts.ch_binds = realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
859
860 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
861 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
862 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
863 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
864
865 return 0;
866}
867
868int
869nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
870{
871 uint32_t i;
872 int ret = -1;
873
874 if (!address && !port && !ti) {
875 for (i = 0; i < client_opts.ch_bind_count; ++i) {
876 close(client_opts.ch_binds[i].sock);
877 free((char *)client_opts.ch_binds[i].address);
878
879 ret = 0;
880 }
881 free(client_opts.ch_binds);
882 client_opts.ch_binds = NULL;
883 client_opts.ch_bind_count = 0;
884 } else {
885 for (i = 0; i < client_opts.ch_bind_count; ++i) {
886 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
887 && (!port || (client_opts.ch_binds[i].port == port))
888 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
889 close(client_opts.ch_binds[i].sock);
890 free((char *)client_opts.ch_binds[i].address);
891
892 --client_opts.ch_bind_count;
893 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
894
895 ret = 0;
896 }
897 }
898 }
899
900 return ret;
901}
902
903API int
904nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
905{
906 int sock;
907 char *host = NULL;
908 uint16_t port, idx;
909
910 if (!client_opts.ch_binds || !session) {
911 ERRARG;
912 return -1;
913 }
914
915 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
916
Michal Vasko50456e82016-02-02 12:16:08 +0100917 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100918 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100919 return sock;
920 }
921
Radek Krejci53691be2016-02-22 13:58:37 +0100922#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100923 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100924 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100925 } else
926#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100927#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +0100928 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100929 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100930 } else
931#endif
932 {
Michal Vaskofee717c2016-02-01 13:25:43 +0100933 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100934 *session = NULL;
935 }
936
937 free(host);
938
939 if (!(*session)) {
940 return -1;
941 }
942
943 return 1;
944}
945
Radek Krejci53691be2016-02-22 13:58:37 +0100946#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +0100947
Michal Vaskob7558c52016-02-26 15:04:19 +0100948API void
949nc_client_destroy(void)
950{
951 nc_client_schema_searchpath(NULL);
952#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
953 nc_client_ch_del_bind(NULL, 0, 0);
954#endif
955#ifdef NC_ENABLED_SSH
956 nc_client_ssh_destroy_opts();
957#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +0100958#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +0100959 nc_client_tls_destroy_opts();
960#endif
961}
962
Michal Vasko086311b2016-01-08 09:53:11 +0100963API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100964nc_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 +0100965{
966 struct lyxml_elem *xml;
967 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
968
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100969 if (!session || !rpc || !reply || (parseroptions & LYD_OPT_TYPEMASK)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100970 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100971 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100972 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100973 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100974 return NC_MSG_ERROR;
975 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100976 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +0100977 *reply = NULL;
978
979 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100980
981 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100982 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100983 lyxml_free(session->ctx, xml);
984 if (!(*reply)) {
985 return NC_MSG_ERROR;
986 }
987 }
988
989 return msgtype;
990}
991
992API NC_MSG_TYPE
993nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
994{
995 struct lyxml_elem *xml, *ev_time;
996 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
997
998 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100999 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001000 return NC_MSG_ERROR;
1001 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001002 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001003 return NC_MSG_ERROR;
1004 }
1005
1006 msgtype = get_msg(session, timeout, 0, &xml);
1007
1008 if (msgtype == NC_MSG_NOTIF) {
1009 *notif = calloc(1, sizeof **notif);
1010
1011 /* eventTime */
1012 LY_TREE_FOR(xml->child, ev_time) {
1013 if (!strcmp(ev_time->name, "eventTime")) {
1014 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1015 /* lyd_parse does not know this element */
1016 lyxml_free(session->ctx, ev_time);
1017 break;
1018 }
1019 }
1020 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001021 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001022 goto fail;
1023 }
1024
1025 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001026 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001027 lyxml_free(session->ctx, xml);
1028 xml = NULL;
1029 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001030 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001031 goto fail;
1032 }
1033 }
1034
1035 return msgtype;
1036
1037fail:
1038 lydict_remove(session->ctx, (*notif)->datetime);
1039 lyd_free((*notif)->tree);
1040 free(*notif);
1041 *notif = NULL;
1042 lyxml_free(session->ctx, xml);
1043
1044 return NC_MSG_ERROR;
1045}
1046
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001047static void *
1048nc_recv_notif_thread(void *arg)
1049{
1050 struct nc_ntf_thread_arg *ntarg;
1051 struct nc_session *session;
1052 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1053 struct nc_notif *notif;
1054 NC_MSG_TYPE msgtype;
1055
1056 ntarg = (struct nc_ntf_thread_arg *)arg;
1057 session = ntarg->session;
1058 notif_clb = ntarg->notif_clb;
1059 free(ntarg);
1060
1061 while (session->ntf_tid) {
1062 msgtype = nc_recv_notif(session, 0, &notif);
1063 if (msgtype == NC_MSG_NOTIF) {
1064 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001065 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1066 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1067 nc_notif_free(notif);
1068 break;
1069 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001070 nc_notif_free(notif);
1071 }
1072
1073 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1074 }
1075
1076 return NULL;
1077}
1078
1079API int
1080nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1081{
1082 struct nc_ntf_thread_arg *ntarg;
1083 int ret;
1084
1085 if (!session || !notif_clb) {
1086 ERRARG;
1087 return -1;
1088 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1089 ERR("Session %u: invalid session to receive Notifications.", session->id);
1090 return -1;
1091 } else if (session->ntf_tid) {
1092 ERR("Session %u: separate notification thread is already running.", session->id);
1093 return -1;
1094 }
1095
1096 ntarg = malloc(sizeof *ntarg);
1097 ntarg->session = session;
1098 ntarg->notif_clb = notif_clb;
1099
1100 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1101 session->ntf_tid = malloc(sizeof *session->ntf_tid);
1102
1103 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1104 if (ret) {
1105 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1106 free(ntarg);
1107 free((pthread_t *)session->ntf_tid);
1108 session->ntf_tid = NULL;
1109 return -1;
1110 }
1111
1112 return 0;
1113}
1114
Michal Vasko086311b2016-01-08 09:53:11 +01001115API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001116nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001117{
1118 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001119 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001120 struct nc_rpc_generic *rpc_gen;
1121 struct nc_rpc_getconfig *rpc_gc;
1122 struct nc_rpc_edit *rpc_e;
1123 struct nc_rpc_copy *rpc_cp;
1124 struct nc_rpc_delete *rpc_del;
1125 struct nc_rpc_lock *rpc_lock;
1126 struct nc_rpc_get *rpc_g;
1127 struct nc_rpc_kill *rpc_k;
1128 struct nc_rpc_commit *rpc_com;
1129 struct nc_rpc_cancel *rpc_can;
1130 struct nc_rpc_validate *rpc_val;
1131 struct nc_rpc_getschema *rpc_gs;
1132 struct nc_rpc_subscribe *rpc_sub;
1133 struct lyd_node *data, *node;
1134 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
1135 char str[11];
1136 uint64_t cur_msgid;
1137
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001138 if (!session || !rpc || !msgid) {
1139 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001140 return NC_MSG_ERROR;
1141 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001142 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001143 return NC_MSG_ERROR;
1144 }
1145
1146 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1147 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1148 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001149 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001150 return NC_MSG_ERROR;
1151 }
1152 }
1153
1154 switch (rpc->type) {
1155 case NC_RPC_GENERIC:
1156 rpc_gen = (struct nc_rpc_generic *)rpc;
1157
1158 if (rpc_gen->has_data) {
1159 data = rpc_gen->content.data;
1160 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001161 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001162 }
1163 break;
1164
1165 case NC_RPC_GETCONFIG:
1166 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1167
1168 data = lyd_new(NULL, ietfnc, "get-config");
1169 node = lyd_new(data, ietfnc, "source");
1170 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1171 if (!node) {
1172 lyd_free(data);
1173 return NC_MSG_ERROR;
1174 }
1175 if (rpc_gc->filter) {
1176 if (rpc_gc->filter[0] == '<') {
1177 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1178 lyd_insert_attr(node, "type", "subtree");
1179 } else {
1180 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1181 lyd_insert_attr(node, "type", "xpath");
1182 lyd_insert_attr(node, "select", rpc_gc->filter);
1183 }
1184 if (!node) {
1185 lyd_free(data);
1186 return NC_MSG_ERROR;
1187 }
1188 }
1189
1190 if (rpc_gc->wd_mode) {
1191 if (!ietfncwd) {
1192 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1193 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001194 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001195 return NC_MSG_ERROR;
1196 }
1197 }
1198 switch (rpc_gc->wd_mode) {
1199 case NC_WD_UNKNOWN:
1200 /* cannot get here */
1201 break;
1202 case NC_WD_ALL:
1203 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1204 break;
1205 case NC_WD_ALL_TAG:
1206 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1207 break;
1208 case NC_WD_TRIM:
1209 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1210 break;
1211 case NC_WD_EXPLICIT:
1212 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1213 break;
1214 }
1215 if (!node) {
1216 lyd_free(data);
1217 return NC_MSG_ERROR;
1218 }
1219 }
1220 break;
1221
1222 case NC_RPC_EDIT:
1223 rpc_e = (struct nc_rpc_edit *)rpc;
1224
1225 data = lyd_new(NULL, ietfnc, "edit-config");
1226 node = lyd_new(data, ietfnc, "target");
1227 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1228 if (!node) {
1229 lyd_free(data);
1230 return NC_MSG_ERROR;
1231 }
1232
1233 if (rpc_e->default_op) {
1234 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1235 if (!node) {
1236 lyd_free(data);
1237 return NC_MSG_ERROR;
1238 }
1239 }
1240
1241 if (rpc_e->test_opt) {
1242 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1243 if (!node) {
1244 lyd_free(data);
1245 return NC_MSG_ERROR;
1246 }
1247 }
1248
1249 if (rpc_e->error_opt) {
1250 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1251 if (!node) {
1252 lyd_free(data);
1253 return NC_MSG_ERROR;
1254 }
1255 }
1256
1257 if (rpc_e->edit_cont[0] == '<') {
1258 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1259 } else {
1260 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1261 }
1262 if (!node) {
1263 lyd_free(data);
1264 return NC_MSG_ERROR;
1265 }
1266 break;
1267
1268 case NC_RPC_COPY:
1269 rpc_cp = (struct nc_rpc_copy *)rpc;
1270
1271 data = lyd_new(NULL, ietfnc, "copy-config");
1272 node = lyd_new(data, ietfnc, "target");
1273 if (rpc_cp->url_trg) {
1274 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1275 } else {
1276 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1277 }
1278 if (!node) {
1279 lyd_free(data);
1280 return NC_MSG_ERROR;
1281 }
1282
1283 node = lyd_new(data, ietfnc, "source");
1284 if (rpc_cp->url_config_src) {
1285 if (rpc_cp->url_config_src[0] == '<') {
1286 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1287 } else {
1288 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1289 }
1290 } else {
1291 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1292 }
1293 if (!node) {
1294 lyd_free(data);
1295 return NC_MSG_ERROR;
1296 }
1297
1298 if (rpc_cp->wd_mode) {
1299 if (!ietfncwd) {
1300 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1301 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001302 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001303 return NC_MSG_ERROR;
1304 }
1305 }
1306 switch (rpc_cp->wd_mode) {
1307 case NC_WD_UNKNOWN:
1308 /* cannot get here */
1309 break;
1310 case NC_WD_ALL:
1311 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1312 break;
1313 case NC_WD_ALL_TAG:
1314 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1315 break;
1316 case NC_WD_TRIM:
1317 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1318 break;
1319 case NC_WD_EXPLICIT:
1320 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1321 break;
1322 }
1323 if (!node) {
1324 lyd_free(data);
1325 return NC_MSG_ERROR;
1326 }
1327 }
1328 break;
1329
1330 case NC_RPC_DELETE:
1331 rpc_del = (struct nc_rpc_delete *)rpc;
1332
1333 data = lyd_new(NULL, ietfnc, "delete-config");
1334 node = lyd_new(data, ietfnc, "target");
1335 if (rpc_del->url) {
1336 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1337 } else {
1338 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1339 }
1340 if (!node) {
1341 lyd_free(data);
1342 return NC_MSG_ERROR;
1343 }
1344 break;
1345
1346 case NC_RPC_LOCK:
1347 rpc_lock = (struct nc_rpc_lock *)rpc;
1348
1349 data = lyd_new(NULL, ietfnc, "lock");
1350 node = lyd_new(data, ietfnc, "target");
1351 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1352 if (!node) {
1353 lyd_free(data);
1354 return NC_MSG_ERROR;
1355 }
1356 break;
1357
1358 case NC_RPC_UNLOCK:
1359 rpc_lock = (struct nc_rpc_lock *)rpc;
1360
1361 data = lyd_new(NULL, ietfnc, "unlock");
1362 node = lyd_new(data, ietfnc, "target");
1363 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1364 if (!node) {
1365 lyd_free(data);
1366 return NC_MSG_ERROR;
1367 }
1368 break;
1369
1370 case NC_RPC_GET:
1371 rpc_g = (struct nc_rpc_get *)rpc;
1372
1373 data = lyd_new(NULL, ietfnc, "get");
1374 if (rpc_g->filter) {
1375 if (rpc_g->filter[0] == '<') {
1376 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1377 lyd_insert_attr(node, "type", "subtree");
1378 } else {
1379 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1380 lyd_insert_attr(node, "type", "xpath");
1381 lyd_insert_attr(node, "select", rpc_g->filter);
1382 }
1383 if (!node) {
1384 lyd_free(data);
1385 return NC_MSG_ERROR;
1386 }
1387 }
1388
1389 if (rpc_g->wd_mode) {
1390 if (!ietfncwd) {
1391 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1392 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001393 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001394 return NC_MSG_ERROR;
1395 }
1396 }
1397 switch (rpc_g->wd_mode) {
1398 case NC_WD_UNKNOWN:
1399 /* cannot get here */
1400 break;
1401 case NC_WD_ALL:
1402 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1403 break;
1404 case NC_WD_ALL_TAG:
1405 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1406 break;
1407 case NC_WD_TRIM:
1408 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1409 break;
1410 case NC_WD_EXPLICIT:
1411 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1412 break;
1413 }
1414 if (!node) {
1415 lyd_free(data);
1416 return NC_MSG_ERROR;
1417 }
1418 }
1419 break;
1420
1421 case NC_RPC_KILL:
1422 rpc_k = (struct nc_rpc_kill *)rpc;
1423
1424 data = lyd_new(NULL, ietfnc, "kill-session");
1425 sprintf(str, "%u", rpc_k->sid);
1426 lyd_new_leaf(data, ietfnc, "session-id", str);
1427 break;
1428
1429 case NC_RPC_COMMIT:
1430 rpc_com = (struct nc_rpc_commit *)rpc;
1431
1432 data = lyd_new(NULL, ietfnc, "commit");
1433 if (rpc_com->confirmed) {
1434 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1435 }
1436
1437 if (rpc_com->confirm_timeout) {
1438 sprintf(str, "%u", rpc_com->confirm_timeout);
1439 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1440 }
1441
1442 if (rpc_com->persist) {
1443 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1444 if (!node) {
1445 lyd_free(data);
1446 return NC_MSG_ERROR;
1447 }
1448 }
1449
1450 if (rpc_com->persist_id) {
1451 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1452 if (!node) {
1453 lyd_free(data);
1454 return NC_MSG_ERROR;
1455 }
1456 }
1457 break;
1458
1459 case NC_RPC_DISCARD:
1460 data = lyd_new(NULL, ietfnc, "discard-changes");
1461 break;
1462
1463 case NC_RPC_CANCEL:
1464 rpc_can = (struct nc_rpc_cancel *)rpc;
1465
1466 data = lyd_new(NULL, ietfnc, "cancel-commit");
1467 if (rpc_can->persist_id) {
1468 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1469 if (!node) {
1470 lyd_free(data);
1471 return NC_MSG_ERROR;
1472 }
1473 }
1474 break;
1475
1476 case NC_RPC_VALIDATE:
1477 rpc_val = (struct nc_rpc_validate *)rpc;
1478
1479 data = lyd_new(NULL, ietfnc, "validate");
1480 node = lyd_new(data, ietfnc, "source");
1481 if (rpc_val->url_config_src) {
1482 if (rpc_val->url_config_src[0] == '<') {
1483 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1484 } else {
1485 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1486 }
1487 } else {
1488 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1489 }
1490 if (!node) {
1491 lyd_free(data);
1492 return NC_MSG_ERROR;
1493 }
1494 break;
1495
1496 case NC_RPC_GETSCHEMA:
1497 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1498 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001499 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001500 return NC_MSG_ERROR;
1501 }
1502
1503 rpc_gs = (struct nc_rpc_getschema *)rpc;
1504
1505 data = lyd_new(NULL, ietfncmon, "get-schema");
1506 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1507 if (!node) {
1508 lyd_free(data);
1509 return NC_MSG_ERROR;
1510 }
1511 if (rpc_gs->version) {
1512 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1513 if (!node) {
1514 lyd_free(data);
1515 return NC_MSG_ERROR;
1516 }
1517 }
1518 if (rpc_gs->format) {
1519 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1520 if (!node) {
1521 lyd_free(data);
1522 return NC_MSG_ERROR;
1523 }
1524 }
1525 break;
1526
1527 case NC_RPC_SUBSCRIBE:
1528 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1529 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001530 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001531 return NC_MSG_ERROR;
1532 }
1533
1534 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1535
1536 data = lyd_new(NULL, notifs, "create-subscription");
1537 if (rpc_sub->stream) {
1538 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1539 if (!node) {
1540 lyd_free(data);
1541 return NC_MSG_ERROR;
1542 }
1543 }
1544
1545 if (rpc_sub->filter) {
1546 if (rpc_sub->filter[0] == '<') {
1547 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1548 lyd_insert_attr(node, "type", "subtree");
1549 } else {
1550 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1551 lyd_insert_attr(node, "type", "xpath");
1552 lyd_insert_attr(node, "select", rpc_sub->filter);
1553 }
1554 if (!node) {
1555 lyd_free(data);
1556 return NC_MSG_ERROR;
1557 }
1558 }
1559
1560 if (rpc_sub->start) {
1561 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1562 if (!node) {
1563 lyd_free(data);
1564 return NC_MSG_ERROR;
1565 }
1566 }
1567
1568 if (rpc_sub->stop) {
1569 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1570 if (!node) {
1571 lyd_free(data);
1572 return NC_MSG_ERROR;
1573 }
1574 }
1575 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001576 default:
1577 ERRINT;
1578 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001579 }
1580
1581 if (lyd_validate(data, LYD_OPT_STRICT)) {
1582 lyd_free(data);
1583 return NC_MSG_ERROR;
1584 }
1585
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001586 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1587 if (ret == -1) {
1588 /* error */
1589 r = NC_MSG_ERROR;
1590 } else if (!ret) {
1591 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001592 r = NC_MSG_WOULDBLOCK;
1593 } else {
1594 /* send RPC, store its message ID */
1595 r = nc_send_msg(session, data);
1596 cur_msgid = session->msgid;
1597 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001598 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001599
1600 lyd_free(data);
1601
1602 if (r != NC_MSG_RPC) {
1603 return r;
1604 }
1605
1606 *msgid = cur_msgid;
1607 return NC_MSG_RPC;
1608}