blob: c429b99260c36e0f193fb59d4056ce0cca2cc205 [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>
19#include <pthread.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26#include <arpa/inet.h>
27#include <poll.h>
28
29#include <libyang/libyang.h>
30
Michal Vasko086311b2016-01-08 09:53:11 +010031#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010032#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010033#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010034
Michal Vasko80ef5d22016-01-18 09:21:02 +010035static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
36
Michal Vaskodaf9a092016-02-09 10:42:05 +010037struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010038
39API int
Michal Vasko3031aae2016-01-27 16:07:18 +010040nc_client_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010041{
Michal Vasko3031aae2016-01-27 16:07:18 +010042 if (client_opts.schema_searchpath) {
43 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010044 }
Michal Vasko086311b2016-01-08 09:53:11 +010045
Michal Vasko7f1c78b2016-01-19 09:52:14 +010046 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010047 client_opts.schema_searchpath = strdup(path);
48 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010049 ERRMEM;
50 return 1;
51 }
52 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010053 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010054 }
55
56 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010057}
58
Michal Vasko3031aae2016-01-27 16:07:18 +010059/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010060static int
61ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
62{
63 const struct lys_module *module;
64 char *ptr, *ptr2;
65 char *model_name, *revision = NULL, *features = NULL;
66
67 /* parse module */
68 ptr = strstr(cpblt, "module=");
69 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010070 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
71 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010072 }
73 ptr += 7;
74 ptr2 = strchr(ptr, '&');
75 if (!ptr2) {
76 ptr2 = ptr + strlen(ptr);
77 }
78 model_name = strndup(ptr, ptr2 - ptr);
79
80 /* parse revision */
81 ptr = strstr(cpblt, "revision=");
82 if (ptr) {
83 ptr += 9;
84 ptr2 = strchr(ptr, '&');
85 if (!ptr2) {
86 ptr2 = ptr + strlen(ptr);
87 }
88 revision = strndup(ptr, ptr2 - ptr);
89 }
90
91 /* load module if needed */
92 module = ly_ctx_get_module(session->ctx, model_name, revision);
93 if (!module) {
94 module = ly_ctx_load_module(session->ctx, model_name, revision);
95 }
96
Michal Vasko086311b2016-01-08 09:53:11 +010097 free(revision);
98 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +010099 WRN("Failed to load model \"%s\".", model_name);
100 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100101 return 1;
102 }
Michal Vaskoef578332016-01-25 13:20:09 +0100103 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100104
105 /* parse features */
106 ptr = strstr(cpblt, "features=");
107 if (ptr) {
108 ptr += 9;
109 ptr2 = strchr(ptr, '&');
110 if (!ptr2) {
111 ptr2 = ptr + strlen(ptr);
112 }
113 features = strndup(ptr, ptr2 - ptr);
114 }
115
116 /* enable features */
117 if (features) {
118 /* basically manual strtok_r (to avoid macro) */
119 ptr2 = features;
120 for (ptr = features; *ptr; ++ptr) {
121 if (*ptr == ',') {
122 *ptr = '\0';
123 /* remember last feature */
124 ptr2 = ptr + 1;
125 }
126 }
127
128 ptr = features;
129 lys_features_enable(module, ptr);
130 while (ptr != ptr2) {
131 ptr += strlen(ptr) + 1;
132 lys_features_enable(module, ptr);
133 }
134
135 free(features);
136 }
137
138 return 0;
139}
140
Michal Vasko1aaa6602016-02-09 11:04:33 +0100141/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100142static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100143ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100144{
145 int i;
146 const struct lys_module *ietfnc;
147
148 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
149 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100150 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
151 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100152 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100153 }
154 }
155 if (!ietfnc) {
156 ERR("Loading base NETCONF schema failed.");
157 return 1;
158 }
159
160 /* set supported capabilities from ietf-netconf */
161 for (i = 0; cpblts[i]; ++i) {
162 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
163 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
164 lys_features_enable(ietfnc, "writable-running");
165 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
166 lys_features_enable(ietfnc, "candidate");
167 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
168 lys_features_enable(ietfnc, "confirmed-commit");
169 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
170 lys_features_enable(ietfnc, "rollback-on-error");
171 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
172 lys_features_enable(ietfnc, "validate");
173 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
174 lys_features_enable(ietfnc, "startup");
175 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
176 lys_features_enable(ietfnc, "url");
177 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
178 lys_features_enable(ietfnc, "xpath");
179 }
180 }
181 }
182
183 return 0;
184}
185
186static char *
187libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
188 void (**free_model_data)(char *model_data))
189{
190 struct nc_session *session = (struct nc_session *)user_data;
191 struct nc_rpc *rpc;
192 struct nc_reply *reply;
193 struct nc_reply_data *data_rpl;
194 NC_MSG_TYPE msg;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100195 char *model_data = NULL, *ptr, *ptr2, *anyxml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100196 uint64_t msgid;
197
198 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100199 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100200 *format = LYS_IN_YIN;
201
202 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
203 usleep(1000);
204 }
205 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100206 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100207 nc_rpc_free(rpc);
208 return NULL;
209 }
210
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100211 msg = nc_recv_reply(session, rpc, msgid, 250, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100212 nc_rpc_free(rpc);
213 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100214 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100215 return NULL;
216 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100217 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100218 return NULL;
219 }
220
Michal Vasko05ba9df2016-01-13 14:40:27 +0100221 if (reply->type != NC_RPL_DATA) {
222 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100223 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100224 nc_reply_free(reply);
225 return NULL;
226 }
227
Michal Vasko086311b2016-01-08 09:53:11 +0100228 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100229 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data_rpl->data)->value, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100230 nc_reply_free(reply);
231 *free_model_data = NULL;
232
233 /* it's with the data root node, remove it */
234 if (anyxml) {
235 ptr = strchr(anyxml, '>');
236 ++ptr;
237
238 ptr2 = strrchr(anyxml, '<');
239
240 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
241 free(anyxml);
242 }
243
244 return model_data;
245}
246
Michal Vaskoef578332016-01-25 13:20:09 +0100247/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100248int
249nc_ctx_check_and_fill(struct nc_session *session)
250{
Michal Vaskoef578332016-01-25 13:20:09 +0100251 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 ly_module_clb old_clb = NULL;
253 void *old_data = NULL;
254
255 assert(session->cpblts && session->ctx);
256
257 /* check if get-schema is supported */
258 for (i = 0; session->cpblts[i]; ++i) {
259 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
260 get_schema_support = 1;
261 break;
262 }
263 }
264
265 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
266 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
267 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
268 /* set module retrieval using <get-schema> */
269 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
270 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
271 } else {
272 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
273 }
274 }
275
276 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100277 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100278 if (old_clb) {
279 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
280 }
Michal Vaskoef578332016-01-25 13:20:09 +0100281 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100282 }
283
284 /* load all other models */
285 for (i = 0; session->cpblts[i]; ++i) {
286 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
287 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
288 continue;
289 }
290
Michal Vaskoef578332016-01-25 13:20:09 +0100291 r = ctx_check_and_load_model(session, session->cpblts[i]);
292 if (r == -1) {
293 ret = -1;
294 break;
295 }
296
297 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
298 * if it was using get-schema */
299 if (r == 1) {
300 if (get_schema_support) {
301 VRB("Trying to load the schema from a different source.");
302 /* works even if old_clb is NULL */
303 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
304 r = ctx_check_and_load_model(session, session->cpblts[i]);
305 }
306
307 /* fail again (or no other way to try), too bad */
308 if (r) {
309 ret = 1;
310 }
311
312 /* set get-schema callback back */
313 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
314 }
Michal Vasko086311b2016-01-08 09:53:11 +0100315 }
316
317 if (old_clb) {
318 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
319 }
Michal Vaskoef578332016-01-25 13:20:09 +0100320 if (ret == 1) {
321 WRN("Some models failed to be loaded, any data from these models will be ignored.");
322 }
323 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100324}
325
326API struct nc_session *
327nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
328{
Michal Vaskod083db62016-01-19 10:31:29 +0100329 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100330
Michal Vaskod083db62016-01-19 10:31:29 +0100331 if ((fdin < 0) || (fdout < 0)) {
332 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100333 return NULL;
334 }
335
336 /* prepare session structure */
337 session = calloc(1, sizeof *session);
338 if (!session) {
339 ERRMEM;
340 return NULL;
341 }
342 session->status = NC_STATUS_STARTING;
343 session->side = NC_CLIENT;
344
345 /* transport specific data */
346 session->ti_type = NC_TI_FD;
347 session->ti.fd.in = fdin;
348 session->ti.fd.out = fdout;
349
350 /* assign context (dicionary needed for handshake) */
351 if (!ctx) {
352 ctx = ly_ctx_new(SCHEMAS_DIR);
353 } else {
354 session->flags |= NC_SESSION_SHAREDCTX;
355 }
356 session->ctx = ctx;
357
358 /* NETCONF handshake */
359 if (nc_handshake(session)) {
360 goto fail;
361 }
362 session->status = NC_STATUS_RUNNING;
363
Michal Vaskoef578332016-01-25 13:20:09 +0100364 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100365 goto fail;
366 }
367
368 return session;
369
370fail:
371 nc_session_free(session);
372 return NULL;
373}
374
375int
Michal Vaskof05562c2016-01-20 12:06:43 +0100376nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100377{
378 int i, sock = -1;
379 struct addrinfo hints, *res_list, *res;
380 char port_s[6]; /* length of string representation of short int */
381
382 snprintf(port_s, 6, "%u", port);
383
384 /* Connect to a server */
385 memset(&hints, 0, sizeof hints);
386 hints.ai_family = AF_UNSPEC;
387 hints.ai_socktype = SOCK_STREAM;
388 hints.ai_protocol = IPPROTO_TCP;
389 i = getaddrinfo(host, port_s, &hints, &res_list);
390 if (i != 0) {
391 ERR("Unable to translate the host address (%s).", gai_strerror(i));
392 return -1;
393 }
394
395 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
396 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
397 if (sock == -1) {
398 /* socket was not created, try another resource */
399 i = errno;
400 goto errloop;
401 }
402
403 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
404 /* network connection failed, try another resource */
405 i = errno;
406 close(sock);
407 sock = -1;
408 goto errloop;
409 }
410
411 /* we're done, network connection established */
412 break;
413errloop:
414 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
415 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
416 continue;
417 }
418
419 if (sock == -1) {
420 ERR("Unable to connect to %s:%s.", host, port_s);
421 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100422 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 +0100423 }
424 freeaddrinfo(res_list);
425
426 return sock;
427}
428
Michal Vasko086311b2016-01-08 09:53:11 +0100429static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100430get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100431{
Michal Vasko2518b6b2016-01-28 13:24:53 +0100432 int r, elapsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100433 char *ptr;
434 const char *str_msgid;
435 uint64_t cur_msgid;
436 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100437 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100438 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
439
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100440 r = nc_timedlock(session->ti_lock, timeout, &elapsed);
441 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100442 /* error */
443 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100444 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100445 /* timeout */
446 return NC_MSG_WOULDBLOCK;
447 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100448 if (timeout > 0) {
449 timeout -= elapsed;
450 }
Michal Vasko086311b2016-01-08 09:53:11 +0100451
452 /* try to get notification from the session's queue */
453 if (!msgid && session->notifs) {
454 cont = session->notifs;
455 session->notifs = cont->next;
456
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100457 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100458
459 *msg = cont->msg;
460 free(cont);
461
462 return NC_MSG_NOTIF;
463 }
464
465 /* try to get rpc-reply from the session's queue */
466 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100467 while (session->replies) {
468 cont = session->replies;
469 session->replies = cont->next;
470
Michal Vasko086311b2016-01-08 09:53:11 +0100471 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
472 cur_msgid = strtoul(str_msgid, &ptr, 10);
473
474 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100475 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100476 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100477
478 *msg = cont->msg;
479 free(cont);
480
481 return NC_MSG_REPLY;
482 }
483
Michal Vasko2518b6b2016-01-28 13:24:53 +0100484 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
485 lyxml_free(session->ctx, cont->msg);
Michal Vaskob2d91072016-02-01 13:25:20 +0100486 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100487 }
488 }
489
490 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100491 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100492
493 /* we read rpc-reply, want a notif */
494 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100495 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100496 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
497 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100498 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100499 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100500 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100501 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100502 }
Michal Vasko086311b2016-01-08 09:53:11 +0100503
504 cont_ptr = &session->replies;
505 while (*cont_ptr) {
506 cont_ptr = &((*cont_ptr)->next);
507 }
508 *cont_ptr = malloc(sizeof **cont_ptr);
509 (*cont_ptr)->msg = xml;
510 (*cont_ptr)->next = NULL;
511 }
512
513 /* we read notif, want a rpc-reply */
514 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100515 /* TODO check whether the session is even subscribed */
516 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100517 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100518 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100519 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100520 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100521 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100522
523 cont_ptr = &session->notifs;
524 while (*cont_ptr) {
525 cont_ptr = &((*cont_ptr)->next);
526 }
527 *cont_ptr = malloc(sizeof **cont_ptr);
528 (*cont_ptr)->msg = xml;
529 (*cont_ptr)->next = NULL;
530 }
531
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100532 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100533
534 switch (msgtype) {
535 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100536 if (!msgid) {
537 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100538 }
Michal Vasko086311b2016-01-08 09:53:11 +0100539 break;
540
541 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100542 if (msgid) {
543 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100544 }
Michal Vasko086311b2016-01-08 09:53:11 +0100545 break;
546
547 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100548 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100549 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100550 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100551
552 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100553 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100554 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100555 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100556
557 default:
558 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
559 * NC_MSG_NONE is not returned by nc_read_msg()
560 */
561 break;
562 }
563
564 return msgtype;
565}
566
567/* cannot strictly fail, but does not need to fill any error parameter at all */
568static void
569parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
570{
571 struct lyxml_elem *iter, *next, *info;
572
573 LY_TREE_FOR(xml->child, iter) {
574 if (!iter->ns) {
575 if (iter->content) {
576 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
577 } else {
578 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
579 }
580 continue;
581 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
582 if (iter->content) {
583 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
584 iter->name, iter->content, iter->ns->value);
585 } else {
586 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
587 }
588 continue;
589 }
590
591 if (!strcmp(iter->name, "error-type")) {
592 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
593 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
594 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
595 } else if (err->type) {
596 WRN("<rpc-error> <error-type> duplicated.");
597 } else {
598 err->type = lydict_insert(ctx, iter->content, 0);
599 }
600 } else if (!strcmp(iter->name, "error-tag")) {
601 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
602 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
603 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
604 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
605 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
606 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
607 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
608 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
609 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
610 && strcmp(iter->content, "malformed-message"))) {
611 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
612 } else if (err->tag) {
613 WRN("<rpc-error> <error-tag> duplicated.");
614 } else {
615 err->tag = lydict_insert(ctx, iter->content, 0);
616 }
617 } else if (!strcmp(iter->name, "error-severity")) {
618 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
619 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
620 } else if (err->severity) {
621 WRN("<rpc-error> <error-severity> duplicated.");
622 } else {
623 err->severity = lydict_insert(ctx, iter->content, 0);
624 }
625 } else if (!strcmp(iter->name, "error-app-tag")) {
626 if (err->apptag) {
627 WRN("<rpc-error> <error-app-tag> duplicated.");
628 } else {
629 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
630 }
631 } else if (!strcmp(iter->name, "error-path")) {
632 if (err->path) {
633 WRN("<rpc-error> <error-path> duplicated.");
634 } else {
635 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
636 }
637 } else if (!strcmp(iter->name, "error-message")) {
638 if (err->message) {
639 WRN("<rpc-error> <error-message> duplicated.");
640 } else {
641 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
642 if (!err->message_lang) {
643 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
644 }
645 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
646 }
647 } else if (!strcmp(iter->name, "error-info")) {
648 LY_TREE_FOR_SAFE(iter->child, next, info) {
649 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
650 if (!strcmp(info->name, "session-id")) {
651 if (err->sid) {
652 WRN("<rpc-error> <error-info> <session-id> duplicated.");
653 } else {
654 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
655 }
656 } else if (!strcmp(info->name, "bad-attr")) {
657 ++err->attr_count;
658 err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr);
659 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
660 } else if (!strcmp(info->name, "bad-element")) {
661 ++err->elem_count;
662 err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem);
663 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
664 } else if (!strcmp(info->name, "bad-namespace")) {
665 ++err->ns_count;
666 err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns);
667 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
668 } else {
669 if (info->content) {
670 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
671 info->name, info->content);
672 } else {
673 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
674 }
675 }
676 } else {
677 lyxml_unlink(ctx, info);
678 ++err->other_count;
679 err->other = realloc(err->other, err->other_count * sizeof *err->other);
680 err->other[err->other_count - 1] = info;
681 }
682 }
683 } else {
684 if (iter->content) {
685 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
686 } else {
687 WRN("<rpc-error> unknown child \"%s\".", iter->name);
688 }
689 }
690 }
691}
692
693static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100694parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100695{
696 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100697 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100698 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100699 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100700 struct nc_reply_data *data_rpl;
701 struct nc_reply *reply = NULL;
702 struct nc_rpc_generic *rpc_gen;
703 int i;
704
705 if (!xml->child) {
706 ERR("An empty <rpc-reply>.");
707 return NULL;
708 }
709
710 /* rpc-error */
711 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
712 /* count and check elements */
713 i = 0;
714 LY_TREE_FOR(xml->child, iter) {
715 if (strcmp(iter->name, "rpc-error")) {
716 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
717 return NULL;
718 } else if (!iter->ns) {
719 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
720 return NULL;
721 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
722 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
723 return NULL;
724 }
725 ++i;
726 }
727
728 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100729 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100730 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100731 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100732 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100733 reply = (struct nc_reply *)error_rpl;
734
735 i = 0;
736 LY_TREE_FOR(xml->child, iter) {
737 parse_rpc_error(ctx, iter, error_rpl->err + i);
738 ++i;
739 }
740
741 /* ok */
742 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
743 if (xml->child->next) {
744 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
745 return NULL;
746 }
747 reply = malloc(sizeof *reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100748 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100749
750 /* some RPC output */
751 } else {
752 switch (rpc->type) {
753 case NC_RPC_GENERIC:
754 rpc_gen = (struct nc_rpc_generic *)rpc;
755
756 if (rpc_gen->has_data) {
757 schema = rpc_gen->content.data->schema;
758 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100759 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100760 if (!data) {
761 ERR("Failed to parse a generic RPC XML.");
762 return NULL;
763 }
764 schema = data->schema;
765 lyd_free(data);
766 data = NULL;
767 }
768 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100769 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100770 return NULL;
771 }
772 break;
773
774 case NC_RPC_GETCONFIG:
775 case NC_RPC_GET:
776 /* special treatment */
777 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100778 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100779 if (!data) {
780 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
781 return NULL;
782 }
783 break;
784
785 case NC_RPC_GETSCHEMA:
Michal Vaskofea54dc2016-02-17 13:12:16 +0100786 schema = ly_ctx_get_node(ctx, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100787 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100788 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100789 return NULL;
790 }
791 break;
792
793 case NC_RPC_EDIT:
794 case NC_RPC_COPY:
795 case NC_RPC_DELETE:
796 case NC_RPC_LOCK:
797 case NC_RPC_UNLOCK:
798 case NC_RPC_KILL:
799 case NC_RPC_COMMIT:
800 case NC_RPC_DISCARD:
801 case NC_RPC_CANCEL:
802 case NC_RPC_VALIDATE:
803 case NC_RPC_SUBSCRIBE:
804 /* there is no output defined */
805 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
806 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100807 default:
808 ERRINT;
809 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100810 }
811
812 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100813 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100814 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100815 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100816 } else {
817 /* <get>, <get-config> */
818 data_rpl->data = data;
819 }
820 if (!data_rpl->data) {
821 ERR("Failed to parse <rpc-reply>.");
822 free(data_rpl);
823 return NULL;
824 }
825 reply = (struct nc_reply *)data_rpl;
826 }
827
828 return reply;
829}
830
Radek Krejci53691be2016-02-22 13:58:37 +0100831#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100832
Michal Vasko3031aae2016-01-27 16:07:18 +0100833int
834nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
835{
836 int sock;
837
838 if (!address || !port) {
839 ERRARG;
840 return -1;
841 }
842
843 sock = nc_sock_listen(address, port);
844 if (sock == -1) {
845 return -1;
846 }
847
848 ++client_opts.ch_bind_count;
849 client_opts.ch_binds = realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
850
851 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
852 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
853 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
854 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
855
856 return 0;
857}
858
859int
860nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
861{
862 uint32_t i;
863 int ret = -1;
864
865 if (!address && !port && !ti) {
866 for (i = 0; i < client_opts.ch_bind_count; ++i) {
867 close(client_opts.ch_binds[i].sock);
868 free((char *)client_opts.ch_binds[i].address);
869
870 ret = 0;
871 }
872 free(client_opts.ch_binds);
873 client_opts.ch_binds = NULL;
874 client_opts.ch_bind_count = 0;
875 } else {
876 for (i = 0; i < client_opts.ch_bind_count; ++i) {
877 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
878 && (!port || (client_opts.ch_binds[i].port == port))
879 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
880 close(client_opts.ch_binds[i].sock);
881 free((char *)client_opts.ch_binds[i].address);
882
883 --client_opts.ch_bind_count;
884 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
885
886 ret = 0;
887 }
888 }
889 }
890
891 return ret;
892}
893
894API int
895nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
896{
897 int sock;
898 char *host = NULL;
899 uint16_t port, idx;
900
901 if (!client_opts.ch_binds || !session) {
902 ERRARG;
903 return -1;
904 }
905
906 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
907
Michal Vasko50456e82016-02-02 12:16:08 +0100908 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100909 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100910 return sock;
911 }
912
Radek Krejci53691be2016-02-22 13:58:37 +0100913#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100914 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100915 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100916 } else
917#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100918#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +0100919 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100920 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100921 } else
922#endif
923 {
Michal Vaskofee717c2016-02-01 13:25:43 +0100924 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100925 *session = NULL;
926 }
927
928 free(host);
929
930 if (!(*session)) {
931 return -1;
932 }
933
934 return 1;
935}
936
Radek Krejci53691be2016-02-22 13:58:37 +0100937#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +0100938
Michal Vasko086311b2016-01-08 09:53:11 +0100939API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100940nc_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 +0100941{
942 struct lyxml_elem *xml;
943 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
944
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100945 if (!session || !rpc || !reply || (parseroptions & LYD_OPT_TYPEMASK)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100946 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100947 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100948 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100949 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100950 return NC_MSG_ERROR;
951 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100952 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +0100953 *reply = NULL;
954
955 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100956
957 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100958 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100959 lyxml_free(session->ctx, xml);
960 if (!(*reply)) {
961 return NC_MSG_ERROR;
962 }
963 }
964
965 return msgtype;
966}
967
968API NC_MSG_TYPE
969nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
970{
971 struct lyxml_elem *xml, *ev_time;
972 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
973
974 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100975 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100976 return NC_MSG_ERROR;
977 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100978 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100979 return NC_MSG_ERROR;
980 }
981
982 msgtype = get_msg(session, timeout, 0, &xml);
983
984 if (msgtype == NC_MSG_NOTIF) {
985 *notif = calloc(1, sizeof **notif);
986
987 /* eventTime */
988 LY_TREE_FOR(xml->child, ev_time) {
989 if (!strcmp(ev_time->name, "eventTime")) {
990 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
991 /* lyd_parse does not know this element */
992 lyxml_free(session->ctx, ev_time);
993 break;
994 }
995 }
996 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +0100997 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100998 goto fail;
999 }
1000
1001 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001002 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001003 lyxml_free(session->ctx, xml);
1004 xml = NULL;
1005 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001006 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001007 goto fail;
1008 }
1009 }
1010
1011 return msgtype;
1012
1013fail:
1014 lydict_remove(session->ctx, (*notif)->datetime);
1015 lyd_free((*notif)->tree);
1016 free(*notif);
1017 *notif = NULL;
1018 lyxml_free(session->ctx, xml);
1019
1020 return NC_MSG_ERROR;
1021}
1022
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001023static void *
1024nc_recv_notif_thread(void *arg)
1025{
1026 struct nc_ntf_thread_arg *ntarg;
1027 struct nc_session *session;
1028 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1029 struct nc_notif *notif;
1030 NC_MSG_TYPE msgtype;
1031
1032 ntarg = (struct nc_ntf_thread_arg *)arg;
1033 session = ntarg->session;
1034 notif_clb = ntarg->notif_clb;
1035 free(ntarg);
1036
1037 while (session->ntf_tid) {
1038 msgtype = nc_recv_notif(session, 0, &notif);
1039 if (msgtype == NC_MSG_NOTIF) {
1040 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001041 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1042 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1043 nc_notif_free(notif);
1044 break;
1045 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001046 nc_notif_free(notif);
1047 }
1048
1049 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1050 }
1051
1052 return NULL;
1053}
1054
1055API int
1056nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1057{
1058 struct nc_ntf_thread_arg *ntarg;
1059 int ret;
1060
1061 if (!session || !notif_clb) {
1062 ERRARG;
1063 return -1;
1064 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1065 ERR("Session %u: invalid session to receive Notifications.", session->id);
1066 return -1;
1067 } else if (session->ntf_tid) {
1068 ERR("Session %u: separate notification thread is already running.", session->id);
1069 return -1;
1070 }
1071
1072 ntarg = malloc(sizeof *ntarg);
1073 ntarg->session = session;
1074 ntarg->notif_clb = notif_clb;
1075
1076 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1077 session->ntf_tid = malloc(sizeof *session->ntf_tid);
1078
1079 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1080 if (ret) {
1081 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1082 free(ntarg);
1083 free((pthread_t *)session->ntf_tid);
1084 session->ntf_tid = NULL;
1085 return -1;
1086 }
1087
1088 return 0;
1089}
1090
Michal Vasko086311b2016-01-08 09:53:11 +01001091API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001092nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001093{
1094 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001095 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001096 struct nc_rpc_generic *rpc_gen;
1097 struct nc_rpc_getconfig *rpc_gc;
1098 struct nc_rpc_edit *rpc_e;
1099 struct nc_rpc_copy *rpc_cp;
1100 struct nc_rpc_delete *rpc_del;
1101 struct nc_rpc_lock *rpc_lock;
1102 struct nc_rpc_get *rpc_g;
1103 struct nc_rpc_kill *rpc_k;
1104 struct nc_rpc_commit *rpc_com;
1105 struct nc_rpc_cancel *rpc_can;
1106 struct nc_rpc_validate *rpc_val;
1107 struct nc_rpc_getschema *rpc_gs;
1108 struct nc_rpc_subscribe *rpc_sub;
1109 struct lyd_node *data, *node;
1110 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
1111 char str[11];
1112 uint64_t cur_msgid;
1113
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001114 if (!session || !rpc || !msgid) {
1115 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001116 return NC_MSG_ERROR;
1117 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001118 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001119 return NC_MSG_ERROR;
1120 }
1121
1122 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1123 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1124 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001125 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001126 return NC_MSG_ERROR;
1127 }
1128 }
1129
1130 switch (rpc->type) {
1131 case NC_RPC_GENERIC:
1132 rpc_gen = (struct nc_rpc_generic *)rpc;
1133
1134 if (rpc_gen->has_data) {
1135 data = rpc_gen->content.data;
1136 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001137 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001138 }
1139 break;
1140
1141 case NC_RPC_GETCONFIG:
1142 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1143
1144 data = lyd_new(NULL, ietfnc, "get-config");
1145 node = lyd_new(data, ietfnc, "source");
1146 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1147 if (!node) {
1148 lyd_free(data);
1149 return NC_MSG_ERROR;
1150 }
1151 if (rpc_gc->filter) {
1152 if (rpc_gc->filter[0] == '<') {
1153 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1154 lyd_insert_attr(node, "type", "subtree");
1155 } else {
1156 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1157 lyd_insert_attr(node, "type", "xpath");
1158 lyd_insert_attr(node, "select", rpc_gc->filter);
1159 }
1160 if (!node) {
1161 lyd_free(data);
1162 return NC_MSG_ERROR;
1163 }
1164 }
1165
1166 if (rpc_gc->wd_mode) {
1167 if (!ietfncwd) {
1168 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1169 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001170 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001171 return NC_MSG_ERROR;
1172 }
1173 }
1174 switch (rpc_gc->wd_mode) {
1175 case NC_WD_UNKNOWN:
1176 /* cannot get here */
1177 break;
1178 case NC_WD_ALL:
1179 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1180 break;
1181 case NC_WD_ALL_TAG:
1182 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1183 break;
1184 case NC_WD_TRIM:
1185 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1186 break;
1187 case NC_WD_EXPLICIT:
1188 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1189 break;
1190 }
1191 if (!node) {
1192 lyd_free(data);
1193 return NC_MSG_ERROR;
1194 }
1195 }
1196 break;
1197
1198 case NC_RPC_EDIT:
1199 rpc_e = (struct nc_rpc_edit *)rpc;
1200
1201 data = lyd_new(NULL, ietfnc, "edit-config");
1202 node = lyd_new(data, ietfnc, "target");
1203 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1204 if (!node) {
1205 lyd_free(data);
1206 return NC_MSG_ERROR;
1207 }
1208
1209 if (rpc_e->default_op) {
1210 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1211 if (!node) {
1212 lyd_free(data);
1213 return NC_MSG_ERROR;
1214 }
1215 }
1216
1217 if (rpc_e->test_opt) {
1218 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1219 if (!node) {
1220 lyd_free(data);
1221 return NC_MSG_ERROR;
1222 }
1223 }
1224
1225 if (rpc_e->error_opt) {
1226 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1227 if (!node) {
1228 lyd_free(data);
1229 return NC_MSG_ERROR;
1230 }
1231 }
1232
1233 if (rpc_e->edit_cont[0] == '<') {
1234 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1235 } else {
1236 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1237 }
1238 if (!node) {
1239 lyd_free(data);
1240 return NC_MSG_ERROR;
1241 }
1242 break;
1243
1244 case NC_RPC_COPY:
1245 rpc_cp = (struct nc_rpc_copy *)rpc;
1246
1247 data = lyd_new(NULL, ietfnc, "copy-config");
1248 node = lyd_new(data, ietfnc, "target");
1249 if (rpc_cp->url_trg) {
1250 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1251 } else {
1252 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1253 }
1254 if (!node) {
1255 lyd_free(data);
1256 return NC_MSG_ERROR;
1257 }
1258
1259 node = lyd_new(data, ietfnc, "source");
1260 if (rpc_cp->url_config_src) {
1261 if (rpc_cp->url_config_src[0] == '<') {
1262 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1263 } else {
1264 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1265 }
1266 } else {
1267 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1268 }
1269 if (!node) {
1270 lyd_free(data);
1271 return NC_MSG_ERROR;
1272 }
1273
1274 if (rpc_cp->wd_mode) {
1275 if (!ietfncwd) {
1276 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1277 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001278 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001279 return NC_MSG_ERROR;
1280 }
1281 }
1282 switch (rpc_cp->wd_mode) {
1283 case NC_WD_UNKNOWN:
1284 /* cannot get here */
1285 break;
1286 case NC_WD_ALL:
1287 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1288 break;
1289 case NC_WD_ALL_TAG:
1290 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1291 break;
1292 case NC_WD_TRIM:
1293 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1294 break;
1295 case NC_WD_EXPLICIT:
1296 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1297 break;
1298 }
1299 if (!node) {
1300 lyd_free(data);
1301 return NC_MSG_ERROR;
1302 }
1303 }
1304 break;
1305
1306 case NC_RPC_DELETE:
1307 rpc_del = (struct nc_rpc_delete *)rpc;
1308
1309 data = lyd_new(NULL, ietfnc, "delete-config");
1310 node = lyd_new(data, ietfnc, "target");
1311 if (rpc_del->url) {
1312 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1313 } else {
1314 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1315 }
1316 if (!node) {
1317 lyd_free(data);
1318 return NC_MSG_ERROR;
1319 }
1320 break;
1321
1322 case NC_RPC_LOCK:
1323 rpc_lock = (struct nc_rpc_lock *)rpc;
1324
1325 data = lyd_new(NULL, ietfnc, "lock");
1326 node = lyd_new(data, ietfnc, "target");
1327 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1328 if (!node) {
1329 lyd_free(data);
1330 return NC_MSG_ERROR;
1331 }
1332 break;
1333
1334 case NC_RPC_UNLOCK:
1335 rpc_lock = (struct nc_rpc_lock *)rpc;
1336
1337 data = lyd_new(NULL, ietfnc, "unlock");
1338 node = lyd_new(data, ietfnc, "target");
1339 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1340 if (!node) {
1341 lyd_free(data);
1342 return NC_MSG_ERROR;
1343 }
1344 break;
1345
1346 case NC_RPC_GET:
1347 rpc_g = (struct nc_rpc_get *)rpc;
1348
1349 data = lyd_new(NULL, ietfnc, "get");
1350 if (rpc_g->filter) {
1351 if (rpc_g->filter[0] == '<') {
1352 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1353 lyd_insert_attr(node, "type", "subtree");
1354 } else {
1355 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1356 lyd_insert_attr(node, "type", "xpath");
1357 lyd_insert_attr(node, "select", rpc_g->filter);
1358 }
1359 if (!node) {
1360 lyd_free(data);
1361 return NC_MSG_ERROR;
1362 }
1363 }
1364
1365 if (rpc_g->wd_mode) {
1366 if (!ietfncwd) {
1367 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1368 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001369 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001370 return NC_MSG_ERROR;
1371 }
1372 }
1373 switch (rpc_g->wd_mode) {
1374 case NC_WD_UNKNOWN:
1375 /* cannot get here */
1376 break;
1377 case NC_WD_ALL:
1378 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1379 break;
1380 case NC_WD_ALL_TAG:
1381 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1382 break;
1383 case NC_WD_TRIM:
1384 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1385 break;
1386 case NC_WD_EXPLICIT:
1387 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1388 break;
1389 }
1390 if (!node) {
1391 lyd_free(data);
1392 return NC_MSG_ERROR;
1393 }
1394 }
1395 break;
1396
1397 case NC_RPC_KILL:
1398 rpc_k = (struct nc_rpc_kill *)rpc;
1399
1400 data = lyd_new(NULL, ietfnc, "kill-session");
1401 sprintf(str, "%u", rpc_k->sid);
1402 lyd_new_leaf(data, ietfnc, "session-id", str);
1403 break;
1404
1405 case NC_RPC_COMMIT:
1406 rpc_com = (struct nc_rpc_commit *)rpc;
1407
1408 data = lyd_new(NULL, ietfnc, "commit");
1409 if (rpc_com->confirmed) {
1410 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1411 }
1412
1413 if (rpc_com->confirm_timeout) {
1414 sprintf(str, "%u", rpc_com->confirm_timeout);
1415 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1416 }
1417
1418 if (rpc_com->persist) {
1419 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1420 if (!node) {
1421 lyd_free(data);
1422 return NC_MSG_ERROR;
1423 }
1424 }
1425
1426 if (rpc_com->persist_id) {
1427 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1428 if (!node) {
1429 lyd_free(data);
1430 return NC_MSG_ERROR;
1431 }
1432 }
1433 break;
1434
1435 case NC_RPC_DISCARD:
1436 data = lyd_new(NULL, ietfnc, "discard-changes");
1437 break;
1438
1439 case NC_RPC_CANCEL:
1440 rpc_can = (struct nc_rpc_cancel *)rpc;
1441
1442 data = lyd_new(NULL, ietfnc, "cancel-commit");
1443 if (rpc_can->persist_id) {
1444 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1445 if (!node) {
1446 lyd_free(data);
1447 return NC_MSG_ERROR;
1448 }
1449 }
1450 break;
1451
1452 case NC_RPC_VALIDATE:
1453 rpc_val = (struct nc_rpc_validate *)rpc;
1454
1455 data = lyd_new(NULL, ietfnc, "validate");
1456 node = lyd_new(data, ietfnc, "source");
1457 if (rpc_val->url_config_src) {
1458 if (rpc_val->url_config_src[0] == '<') {
1459 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1460 } else {
1461 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1462 }
1463 } else {
1464 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1465 }
1466 if (!node) {
1467 lyd_free(data);
1468 return NC_MSG_ERROR;
1469 }
1470 break;
1471
1472 case NC_RPC_GETSCHEMA:
1473 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1474 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001475 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001476 return NC_MSG_ERROR;
1477 }
1478
1479 rpc_gs = (struct nc_rpc_getschema *)rpc;
1480
1481 data = lyd_new(NULL, ietfncmon, "get-schema");
1482 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1483 if (!node) {
1484 lyd_free(data);
1485 return NC_MSG_ERROR;
1486 }
1487 if (rpc_gs->version) {
1488 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1489 if (!node) {
1490 lyd_free(data);
1491 return NC_MSG_ERROR;
1492 }
1493 }
1494 if (rpc_gs->format) {
1495 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1496 if (!node) {
1497 lyd_free(data);
1498 return NC_MSG_ERROR;
1499 }
1500 }
1501 break;
1502
1503 case NC_RPC_SUBSCRIBE:
1504 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1505 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001506 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001507 return NC_MSG_ERROR;
1508 }
1509
1510 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1511
1512 data = lyd_new(NULL, notifs, "create-subscription");
1513 if (rpc_sub->stream) {
1514 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1515 if (!node) {
1516 lyd_free(data);
1517 return NC_MSG_ERROR;
1518 }
1519 }
1520
1521 if (rpc_sub->filter) {
1522 if (rpc_sub->filter[0] == '<') {
1523 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1524 lyd_insert_attr(node, "type", "subtree");
1525 } else {
1526 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1527 lyd_insert_attr(node, "type", "xpath");
1528 lyd_insert_attr(node, "select", rpc_sub->filter);
1529 }
1530 if (!node) {
1531 lyd_free(data);
1532 return NC_MSG_ERROR;
1533 }
1534 }
1535
1536 if (rpc_sub->start) {
1537 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1538 if (!node) {
1539 lyd_free(data);
1540 return NC_MSG_ERROR;
1541 }
1542 }
1543
1544 if (rpc_sub->stop) {
1545 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1546 if (!node) {
1547 lyd_free(data);
1548 return NC_MSG_ERROR;
1549 }
1550 }
1551 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001552 default:
1553 ERRINT;
1554 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001555 }
1556
1557 if (lyd_validate(data, LYD_OPT_STRICT)) {
1558 lyd_free(data);
1559 return NC_MSG_ERROR;
1560 }
1561
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001562 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1563 if (ret == -1) {
1564 /* error */
1565 r = NC_MSG_ERROR;
1566 } else if (!ret) {
1567 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001568 r = NC_MSG_WOULDBLOCK;
1569 } else {
1570 /* send RPC, store its message ID */
1571 r = nc_send_msg(session, data);
1572 cur_msgid = session->msgid;
1573 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001574 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001575
1576 lyd_free(data);
1577
1578 if (r != NC_MSG_RPC) {
1579 return r;
1580 }
1581
1582 *msgid = cur_msgid;
1583 return NC_MSG_RPC;
1584}