blob: e430c87a339776de8e3dde41c77127df7b5fc125 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#include <assert.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <netdb.h>
27#include <pthread.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/socket.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <unistd.h>
34#include <arpa/inet.h>
35#include <poll.h>
36
37#include <libyang/libyang.h>
38
Michal Vasko086311b2016-01-08 09:53:11 +010039#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010040#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010041#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010042
Michal Vasko80ef5d22016-01-18 09:21:02 +010043static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
44
Michal Vaskodaf9a092016-02-09 10:42:05 +010045struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010046
47API int
Michal Vasko3031aae2016-01-27 16:07:18 +010048nc_client_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010049{
Michal Vasko3031aae2016-01-27 16:07:18 +010050 if (client_opts.schema_searchpath) {
51 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010052 }
Michal Vasko086311b2016-01-08 09:53:11 +010053
Michal Vasko7f1c78b2016-01-19 09:52:14 +010054 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010055 client_opts.schema_searchpath = strdup(path);
56 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010057 ERRMEM;
58 return 1;
59 }
60 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010061 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010062 }
63
64 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010065}
66
Michal Vasko3031aae2016-01-27 16:07:18 +010067/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010068static int
69ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
70{
71 const struct lys_module *module;
72 char *ptr, *ptr2;
73 char *model_name, *revision = NULL, *features = NULL;
74
75 /* parse module */
76 ptr = strstr(cpblt, "module=");
77 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010078 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
79 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010080 }
81 ptr += 7;
82 ptr2 = strchr(ptr, '&');
83 if (!ptr2) {
84 ptr2 = ptr + strlen(ptr);
85 }
86 model_name = strndup(ptr, ptr2 - ptr);
87
88 /* parse revision */
89 ptr = strstr(cpblt, "revision=");
90 if (ptr) {
91 ptr += 9;
92 ptr2 = strchr(ptr, '&');
93 if (!ptr2) {
94 ptr2 = ptr + strlen(ptr);
95 }
96 revision = strndup(ptr, ptr2 - ptr);
97 }
98
99 /* load module if needed */
100 module = ly_ctx_get_module(session->ctx, model_name, revision);
101 if (!module) {
102 module = ly_ctx_load_module(session->ctx, model_name, revision);
103 }
104
Michal Vasko086311b2016-01-08 09:53:11 +0100105 free(revision);
106 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100107 WRN("Failed to load model \"%s\".", model_name);
108 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100109 return 1;
110 }
Michal Vaskoef578332016-01-25 13:20:09 +0100111 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100112
113 /* parse features */
114 ptr = strstr(cpblt, "features=");
115 if (ptr) {
116 ptr += 9;
117 ptr2 = strchr(ptr, '&');
118 if (!ptr2) {
119 ptr2 = ptr + strlen(ptr);
120 }
121 features = strndup(ptr, ptr2 - ptr);
122 }
123
124 /* enable features */
125 if (features) {
126 /* basically manual strtok_r (to avoid macro) */
127 ptr2 = features;
128 for (ptr = features; *ptr; ++ptr) {
129 if (*ptr == ',') {
130 *ptr = '\0';
131 /* remember last feature */
132 ptr2 = ptr + 1;
133 }
134 }
135
136 ptr = features;
137 lys_features_enable(module, ptr);
138 while (ptr != ptr2) {
139 ptr += strlen(ptr) + 1;
140 lys_features_enable(module, ptr);
141 }
142
143 free(features);
144 }
145
146 return 0;
147}
148
Michal Vasko1aaa6602016-02-09 11:04:33 +0100149/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100150static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100151ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100152{
153 int i;
154 const struct lys_module *ietfnc;
155
156 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
157 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100158 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
159 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100160 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100161 }
162 }
163 if (!ietfnc) {
164 ERR("Loading base NETCONF schema failed.");
165 return 1;
166 }
167
168 /* set supported capabilities from ietf-netconf */
169 for (i = 0; cpblts[i]; ++i) {
170 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
171 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
172 lys_features_enable(ietfnc, "writable-running");
173 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
174 lys_features_enable(ietfnc, "candidate");
175 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
176 lys_features_enable(ietfnc, "confirmed-commit");
177 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
178 lys_features_enable(ietfnc, "rollback-on-error");
179 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
180 lys_features_enable(ietfnc, "validate");
181 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
182 lys_features_enable(ietfnc, "startup");
183 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
184 lys_features_enable(ietfnc, "url");
185 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
186 lys_features_enable(ietfnc, "xpath");
187 }
188 }
189 }
190
191 return 0;
192}
193
194static char *
195libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
196 void (**free_model_data)(char *model_data))
197{
198 struct nc_session *session = (struct nc_session *)user_data;
199 struct nc_rpc *rpc;
200 struct nc_reply *reply;
201 struct nc_reply_data *data_rpl;
202 NC_MSG_TYPE msg;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100203 char *model_data = NULL, *ptr, *ptr2, *anyxml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100204 uint64_t msgid;
205
206 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100207 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100208 *format = LYS_IN_YIN;
209
210 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
211 usleep(1000);
212 }
213 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100214 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100215 nc_rpc_free(rpc);
216 return NULL;
217 }
218
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100219 msg = nc_recv_reply(session, rpc, msgid, 250, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100220 nc_rpc_free(rpc);
221 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100222 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 return NULL;
224 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100225 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100226 return NULL;
227 }
228
Michal Vasko05ba9df2016-01-13 14:40:27 +0100229 if (reply->type != NC_RPL_DATA) {
230 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100231 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100232 nc_reply_free(reply);
233 return NULL;
234 }
235
Michal Vasko086311b2016-01-08 09:53:11 +0100236 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100237 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data_rpl->data)->value, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100238 nc_reply_free(reply);
239 *free_model_data = NULL;
240
241 /* it's with the data root node, remove it */
242 if (anyxml) {
243 ptr = strchr(anyxml, '>');
244 ++ptr;
245
246 ptr2 = strrchr(anyxml, '<');
247
248 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
249 free(anyxml);
250 }
251
252 return model_data;
253}
254
Michal Vaskoef578332016-01-25 13:20:09 +0100255/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100256int
257nc_ctx_check_and_fill(struct nc_session *session)
258{
Michal Vaskoef578332016-01-25 13:20:09 +0100259 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100260 ly_module_clb old_clb = NULL;
261 void *old_data = NULL;
262
263 assert(session->cpblts && session->ctx);
264
265 /* check if get-schema is supported */
266 for (i = 0; session->cpblts[i]; ++i) {
267 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
268 get_schema_support = 1;
269 break;
270 }
271 }
272
273 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
274 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
275 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
276 /* set module retrieval using <get-schema> */
277 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
278 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
279 } else {
280 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
281 }
282 }
283
284 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100285 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100286 if (old_clb) {
287 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
288 }
Michal Vaskoef578332016-01-25 13:20:09 +0100289 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100290 }
291
292 /* load all other models */
293 for (i = 0; session->cpblts[i]; ++i) {
294 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
295 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
296 continue;
297 }
298
Michal Vaskoef578332016-01-25 13:20:09 +0100299 r = ctx_check_and_load_model(session, session->cpblts[i]);
300 if (r == -1) {
301 ret = -1;
302 break;
303 }
304
305 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
306 * if it was using get-schema */
307 if (r == 1) {
308 if (get_schema_support) {
309 VRB("Trying to load the schema from a different source.");
310 /* works even if old_clb is NULL */
311 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
312 r = ctx_check_and_load_model(session, session->cpblts[i]);
313 }
314
315 /* fail again (or no other way to try), too bad */
316 if (r) {
317 ret = 1;
318 }
319
320 /* set get-schema callback back */
321 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
322 }
Michal Vasko086311b2016-01-08 09:53:11 +0100323 }
324
325 if (old_clb) {
326 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
327 }
Michal Vaskoef578332016-01-25 13:20:09 +0100328 if (ret == 1) {
329 WRN("Some models failed to be loaded, any data from these models will be ignored.");
330 }
331 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100332}
333
334API struct nc_session *
335nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
336{
Michal Vaskod083db62016-01-19 10:31:29 +0100337 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100338
Michal Vaskod083db62016-01-19 10:31:29 +0100339 if ((fdin < 0) || (fdout < 0)) {
340 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100341 return NULL;
342 }
343
344 /* prepare session structure */
345 session = calloc(1, sizeof *session);
346 if (!session) {
347 ERRMEM;
348 return NULL;
349 }
350 session->status = NC_STATUS_STARTING;
351 session->side = NC_CLIENT;
352
353 /* transport specific data */
354 session->ti_type = NC_TI_FD;
355 session->ti.fd.in = fdin;
356 session->ti.fd.out = fdout;
357
358 /* assign context (dicionary needed for handshake) */
359 if (!ctx) {
360 ctx = ly_ctx_new(SCHEMAS_DIR);
361 } else {
362 session->flags |= NC_SESSION_SHAREDCTX;
363 }
364 session->ctx = ctx;
365
366 /* NETCONF handshake */
367 if (nc_handshake(session)) {
368 goto fail;
369 }
370 session->status = NC_STATUS_RUNNING;
371
Michal Vaskoef578332016-01-25 13:20:09 +0100372 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100373 goto fail;
374 }
375
376 return session;
377
378fail:
379 nc_session_free(session);
380 return NULL;
381}
382
383int
Michal Vaskof05562c2016-01-20 12:06:43 +0100384nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100385{
386 int i, sock = -1;
387 struct addrinfo hints, *res_list, *res;
388 char port_s[6]; /* length of string representation of short int */
389
390 snprintf(port_s, 6, "%u", port);
391
392 /* Connect to a server */
393 memset(&hints, 0, sizeof hints);
394 hints.ai_family = AF_UNSPEC;
395 hints.ai_socktype = SOCK_STREAM;
396 hints.ai_protocol = IPPROTO_TCP;
397 i = getaddrinfo(host, port_s, &hints, &res_list);
398 if (i != 0) {
399 ERR("Unable to translate the host address (%s).", gai_strerror(i));
400 return -1;
401 }
402
403 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
404 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
405 if (sock == -1) {
406 /* socket was not created, try another resource */
407 i = errno;
408 goto errloop;
409 }
410
411 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
412 /* network connection failed, try another resource */
413 i = errno;
414 close(sock);
415 sock = -1;
416 goto errloop;
417 }
418
419 /* we're done, network connection established */
420 break;
421errloop:
422 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
423 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
424 continue;
425 }
426
427 if (sock == -1) {
428 ERR("Unable to connect to %s:%s.", host, port_s);
429 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100430 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 +0100431 }
432 freeaddrinfo(res_list);
433
434 return sock;
435}
436
Michal Vasko086311b2016-01-08 09:53:11 +0100437static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100438get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100439{
Michal Vasko2518b6b2016-01-28 13:24:53 +0100440 int r, elapsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100441 char *ptr;
442 const char *str_msgid;
443 uint64_t cur_msgid;
444 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100445 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100446 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
447
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100448 r = nc_timedlock(session->ti_lock, timeout, &elapsed);
449 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100450 /* error */
451 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100452 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100453 /* timeout */
454 return NC_MSG_WOULDBLOCK;
455 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100456 if (timeout > 0) {
457 timeout -= elapsed;
458 }
Michal Vasko086311b2016-01-08 09:53:11 +0100459
460 /* try to get notification from the session's queue */
461 if (!msgid && session->notifs) {
462 cont = session->notifs;
463 session->notifs = cont->next;
464
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100465 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100466
467 *msg = cont->msg;
468 free(cont);
469
470 return NC_MSG_NOTIF;
471 }
472
473 /* try to get rpc-reply from the session's queue */
474 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100475 while (session->replies) {
476 cont = session->replies;
477 session->replies = cont->next;
478
Michal Vasko086311b2016-01-08 09:53:11 +0100479 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
480 cur_msgid = strtoul(str_msgid, &ptr, 10);
481
482 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100483 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100484 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100485
486 *msg = cont->msg;
487 free(cont);
488
489 return NC_MSG_REPLY;
490 }
491
Michal Vasko2518b6b2016-01-28 13:24:53 +0100492 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
493 lyxml_free(session->ctx, cont->msg);
Michal Vaskob2d91072016-02-01 13:25:20 +0100494 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100495 }
496 }
497
498 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100499 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100500
501 /* we read rpc-reply, want a notif */
502 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100503 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100504 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
505 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100506 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100507 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100508 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100509 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100510 }
Michal Vasko086311b2016-01-08 09:53:11 +0100511
512 cont_ptr = &session->replies;
513 while (*cont_ptr) {
514 cont_ptr = &((*cont_ptr)->next);
515 }
516 *cont_ptr = malloc(sizeof **cont_ptr);
517 (*cont_ptr)->msg = xml;
518 (*cont_ptr)->next = NULL;
519 }
520
521 /* we read notif, want a rpc-reply */
522 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100523 /* TODO check whether the session is even subscribed */
524 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100525 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100526 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100527 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100528 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100529 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100530
531 cont_ptr = &session->notifs;
532 while (*cont_ptr) {
533 cont_ptr = &((*cont_ptr)->next);
534 }
535 *cont_ptr = malloc(sizeof **cont_ptr);
536 (*cont_ptr)->msg = xml;
537 (*cont_ptr)->next = NULL;
538 }
539
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100540 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100541
542 switch (msgtype) {
543 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100544 if (!msgid) {
545 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100546 }
Michal Vasko086311b2016-01-08 09:53:11 +0100547 break;
548
549 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100550 if (msgid) {
551 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100552 }
Michal Vasko086311b2016-01-08 09:53:11 +0100553 break;
554
555 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100556 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100557 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100558 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100559
560 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100561 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100562 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100563 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100564
565 default:
566 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
567 * NC_MSG_NONE is not returned by nc_read_msg()
568 */
569 break;
570 }
571
572 return msgtype;
573}
574
575/* cannot strictly fail, but does not need to fill any error parameter at all */
576static void
577parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
578{
579 struct lyxml_elem *iter, *next, *info;
580
581 LY_TREE_FOR(xml->child, iter) {
582 if (!iter->ns) {
583 if (iter->content) {
584 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
585 } else {
586 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
587 }
588 continue;
589 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
590 if (iter->content) {
591 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
592 iter->name, iter->content, iter->ns->value);
593 } else {
594 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
595 }
596 continue;
597 }
598
599 if (!strcmp(iter->name, "error-type")) {
600 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
601 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
602 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
603 } else if (err->type) {
604 WRN("<rpc-error> <error-type> duplicated.");
605 } else {
606 err->type = lydict_insert(ctx, iter->content, 0);
607 }
608 } else if (!strcmp(iter->name, "error-tag")) {
609 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
610 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
611 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
612 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
613 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
614 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
615 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
616 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
617 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
618 && strcmp(iter->content, "malformed-message"))) {
619 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
620 } else if (err->tag) {
621 WRN("<rpc-error> <error-tag> duplicated.");
622 } else {
623 err->tag = lydict_insert(ctx, iter->content, 0);
624 }
625 } else if (!strcmp(iter->name, "error-severity")) {
626 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
627 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
628 } else if (err->severity) {
629 WRN("<rpc-error> <error-severity> duplicated.");
630 } else {
631 err->severity = lydict_insert(ctx, iter->content, 0);
632 }
633 } else if (!strcmp(iter->name, "error-app-tag")) {
634 if (err->apptag) {
635 WRN("<rpc-error> <error-app-tag> duplicated.");
636 } else {
637 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
638 }
639 } else if (!strcmp(iter->name, "error-path")) {
640 if (err->path) {
641 WRN("<rpc-error> <error-path> duplicated.");
642 } else {
643 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
644 }
645 } else if (!strcmp(iter->name, "error-message")) {
646 if (err->message) {
647 WRN("<rpc-error> <error-message> duplicated.");
648 } else {
649 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
650 if (!err->message_lang) {
651 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
652 }
653 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
654 }
655 } else if (!strcmp(iter->name, "error-info")) {
656 LY_TREE_FOR_SAFE(iter->child, next, info) {
657 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
658 if (!strcmp(info->name, "session-id")) {
659 if (err->sid) {
660 WRN("<rpc-error> <error-info> <session-id> duplicated.");
661 } else {
662 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
663 }
664 } else if (!strcmp(info->name, "bad-attr")) {
665 ++err->attr_count;
666 err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr);
667 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
668 } else if (!strcmp(info->name, "bad-element")) {
669 ++err->elem_count;
670 err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem);
671 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
672 } else if (!strcmp(info->name, "bad-namespace")) {
673 ++err->ns_count;
674 err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns);
675 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
676 } else {
677 if (info->content) {
678 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
679 info->name, info->content);
680 } else {
681 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
682 }
683 }
684 } else {
685 lyxml_unlink(ctx, info);
686 ++err->other_count;
687 err->other = realloc(err->other, err->other_count * sizeof *err->other);
688 err->other[err->other_count - 1] = info;
689 }
690 }
691 } else {
692 if (iter->content) {
693 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
694 } else {
695 WRN("<rpc-error> unknown child \"%s\".", iter->name);
696 }
697 }
698 }
699}
700
701static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100702parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100703{
704 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100705 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100706 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100707 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100708 struct nc_reply_data *data_rpl;
709 struct nc_reply *reply = NULL;
710 struct nc_rpc_generic *rpc_gen;
711 int i;
712
713 if (!xml->child) {
714 ERR("An empty <rpc-reply>.");
715 return NULL;
716 }
717
718 /* rpc-error */
719 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
720 /* count and check elements */
721 i = 0;
722 LY_TREE_FOR(xml->child, iter) {
723 if (strcmp(iter->name, "rpc-error")) {
724 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
725 return NULL;
726 } else if (!iter->ns) {
727 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
728 return NULL;
729 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
730 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
731 return NULL;
732 }
733 ++i;
734 }
735
736 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100737 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100738 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100739 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100740 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100741 reply = (struct nc_reply *)error_rpl;
742
743 i = 0;
744 LY_TREE_FOR(xml->child, iter) {
745 parse_rpc_error(ctx, iter, error_rpl->err + i);
746 ++i;
747 }
748
749 /* ok */
750 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
751 if (xml->child->next) {
752 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
753 return NULL;
754 }
755 reply = malloc(sizeof *reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100756 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100757
758 /* some RPC output */
759 } else {
760 switch (rpc->type) {
761 case NC_RPC_GENERIC:
762 rpc_gen = (struct nc_rpc_generic *)rpc;
763
764 if (rpc_gen->has_data) {
765 schema = rpc_gen->content.data->schema;
766 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100767 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100768 if (!data) {
769 ERR("Failed to parse a generic RPC XML.");
770 return NULL;
771 }
772 schema = data->schema;
773 lyd_free(data);
774 data = NULL;
775 }
776 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100777 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100778 return NULL;
779 }
780 break;
781
782 case NC_RPC_GETCONFIG:
783 case NC_RPC_GET:
784 /* special treatment */
785 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100786 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100787 if (!data) {
788 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
789 return NULL;
790 }
791 break;
792
793 case NC_RPC_GETSCHEMA:
Michal Vaskofea54dc2016-02-17 13:12:16 +0100794 schema = ly_ctx_get_node(ctx, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100795 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100796 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100797 return NULL;
798 }
799 break;
800
801 case NC_RPC_EDIT:
802 case NC_RPC_COPY:
803 case NC_RPC_DELETE:
804 case NC_RPC_LOCK:
805 case NC_RPC_UNLOCK:
806 case NC_RPC_KILL:
807 case NC_RPC_COMMIT:
808 case NC_RPC_DISCARD:
809 case NC_RPC_CANCEL:
810 case NC_RPC_VALIDATE:
811 case NC_RPC_SUBSCRIBE:
812 /* there is no output defined */
813 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
814 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100815 default:
816 ERRINT;
817 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100818 }
819
820 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100821 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100822 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100823 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100824 } else {
825 /* <get>, <get-config> */
826 data_rpl->data = data;
827 }
828 if (!data_rpl->data) {
829 ERR("Failed to parse <rpc-reply>.");
830 free(data_rpl);
831 return NULL;
832 }
833 reply = (struct nc_reply *)data_rpl;
834 }
835
836 return reply;
837}
838
Radek Krejci53691be2016-02-22 13:58:37 +0100839#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100840
Michal Vasko3031aae2016-01-27 16:07:18 +0100841int
842nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
843{
844 int sock;
845
846 if (!address || !port) {
847 ERRARG;
848 return -1;
849 }
850
851 sock = nc_sock_listen(address, port);
852 if (sock == -1) {
853 return -1;
854 }
855
856 ++client_opts.ch_bind_count;
857 client_opts.ch_binds = realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
858
859 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
860 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
861 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
862 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
863
864 return 0;
865}
866
867int
868nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
869{
870 uint32_t i;
871 int ret = -1;
872
873 if (!address && !port && !ti) {
874 for (i = 0; i < client_opts.ch_bind_count; ++i) {
875 close(client_opts.ch_binds[i].sock);
876 free((char *)client_opts.ch_binds[i].address);
877
878 ret = 0;
879 }
880 free(client_opts.ch_binds);
881 client_opts.ch_binds = NULL;
882 client_opts.ch_bind_count = 0;
883 } else {
884 for (i = 0; i < client_opts.ch_bind_count; ++i) {
885 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
886 && (!port || (client_opts.ch_binds[i].port == port))
887 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
888 close(client_opts.ch_binds[i].sock);
889 free((char *)client_opts.ch_binds[i].address);
890
891 --client_opts.ch_bind_count;
892 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
893
894 ret = 0;
895 }
896 }
897 }
898
899 return ret;
900}
901
902API int
903nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
904{
905 int sock;
906 char *host = NULL;
907 uint16_t port, idx;
908
909 if (!client_opts.ch_binds || !session) {
910 ERRARG;
911 return -1;
912 }
913
914 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
915
Michal Vasko50456e82016-02-02 12:16:08 +0100916 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100917 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100918 return sock;
919 }
920
Radek Krejci53691be2016-02-22 13:58:37 +0100921#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100922 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100923 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100924 } else
925#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100926#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +0100927 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100928 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100929 } else
930#endif
931 {
Michal Vaskofee717c2016-02-01 13:25:43 +0100932 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100933 *session = NULL;
934 }
935
936 free(host);
937
938 if (!(*session)) {
939 return -1;
940 }
941
942 return 1;
943}
944
Radek Krejci53691be2016-02-22 13:58:37 +0100945#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +0100946
Michal Vasko086311b2016-01-08 09:53:11 +0100947API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100948nc_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 +0100949{
950 struct lyxml_elem *xml;
951 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
952
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100953 if (!session || !rpc || !reply || (parseroptions & LYD_OPT_TYPEMASK)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100954 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100955 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100956 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100957 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100958 return NC_MSG_ERROR;
959 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100960 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +0100961 *reply = NULL;
962
963 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100964
965 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100966 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100967 lyxml_free(session->ctx, xml);
968 if (!(*reply)) {
969 return NC_MSG_ERROR;
970 }
971 }
972
973 return msgtype;
974}
975
976API NC_MSG_TYPE
977nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
978{
979 struct lyxml_elem *xml, *ev_time;
980 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
981
982 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100983 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100984 return NC_MSG_ERROR;
985 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100986 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100987 return NC_MSG_ERROR;
988 }
989
990 msgtype = get_msg(session, timeout, 0, &xml);
991
992 if (msgtype == NC_MSG_NOTIF) {
993 *notif = calloc(1, sizeof **notif);
994
995 /* eventTime */
996 LY_TREE_FOR(xml->child, ev_time) {
997 if (!strcmp(ev_time->name, "eventTime")) {
998 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
999 /* lyd_parse does not know this element */
1000 lyxml_free(session->ctx, ev_time);
1001 break;
1002 }
1003 }
1004 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001005 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001006 goto fail;
1007 }
1008
1009 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001010 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001011 lyxml_free(session->ctx, xml);
1012 xml = NULL;
1013 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001014 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001015 goto fail;
1016 }
1017 }
1018
1019 return msgtype;
1020
1021fail:
1022 lydict_remove(session->ctx, (*notif)->datetime);
1023 lyd_free((*notif)->tree);
1024 free(*notif);
1025 *notif = NULL;
1026 lyxml_free(session->ctx, xml);
1027
1028 return NC_MSG_ERROR;
1029}
1030
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001031static void *
1032nc_recv_notif_thread(void *arg)
1033{
1034 struct nc_ntf_thread_arg *ntarg;
1035 struct nc_session *session;
1036 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1037 struct nc_notif *notif;
1038 NC_MSG_TYPE msgtype;
1039
1040 ntarg = (struct nc_ntf_thread_arg *)arg;
1041 session = ntarg->session;
1042 notif_clb = ntarg->notif_clb;
1043 free(ntarg);
1044
1045 while (session->ntf_tid) {
1046 msgtype = nc_recv_notif(session, 0, &notif);
1047 if (msgtype == NC_MSG_NOTIF) {
1048 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001049 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1050 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1051 nc_notif_free(notif);
1052 break;
1053 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001054 nc_notif_free(notif);
1055 }
1056
1057 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1058 }
1059
1060 return NULL;
1061}
1062
1063API int
1064nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1065{
1066 struct nc_ntf_thread_arg *ntarg;
1067 int ret;
1068
1069 if (!session || !notif_clb) {
1070 ERRARG;
1071 return -1;
1072 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1073 ERR("Session %u: invalid session to receive Notifications.", session->id);
1074 return -1;
1075 } else if (session->ntf_tid) {
1076 ERR("Session %u: separate notification thread is already running.", session->id);
1077 return -1;
1078 }
1079
1080 ntarg = malloc(sizeof *ntarg);
1081 ntarg->session = session;
1082 ntarg->notif_clb = notif_clb;
1083
1084 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1085 session->ntf_tid = malloc(sizeof *session->ntf_tid);
1086
1087 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1088 if (ret) {
1089 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1090 free(ntarg);
1091 free((pthread_t *)session->ntf_tid);
1092 session->ntf_tid = NULL;
1093 return -1;
1094 }
1095
1096 return 0;
1097}
1098
Michal Vasko086311b2016-01-08 09:53:11 +01001099API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001100nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001101{
1102 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001103 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001104 struct nc_rpc_generic *rpc_gen;
1105 struct nc_rpc_getconfig *rpc_gc;
1106 struct nc_rpc_edit *rpc_e;
1107 struct nc_rpc_copy *rpc_cp;
1108 struct nc_rpc_delete *rpc_del;
1109 struct nc_rpc_lock *rpc_lock;
1110 struct nc_rpc_get *rpc_g;
1111 struct nc_rpc_kill *rpc_k;
1112 struct nc_rpc_commit *rpc_com;
1113 struct nc_rpc_cancel *rpc_can;
1114 struct nc_rpc_validate *rpc_val;
1115 struct nc_rpc_getschema *rpc_gs;
1116 struct nc_rpc_subscribe *rpc_sub;
1117 struct lyd_node *data, *node;
1118 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
1119 char str[11];
1120 uint64_t cur_msgid;
1121
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001122 if (!session || !rpc || !msgid) {
1123 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001124 return NC_MSG_ERROR;
1125 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001126 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001127 return NC_MSG_ERROR;
1128 }
1129
1130 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1131 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1132 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001133 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001134 return NC_MSG_ERROR;
1135 }
1136 }
1137
1138 switch (rpc->type) {
1139 case NC_RPC_GENERIC:
1140 rpc_gen = (struct nc_rpc_generic *)rpc;
1141
1142 if (rpc_gen->has_data) {
1143 data = rpc_gen->content.data;
1144 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001145 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001146 }
1147 break;
1148
1149 case NC_RPC_GETCONFIG:
1150 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1151
1152 data = lyd_new(NULL, ietfnc, "get-config");
1153 node = lyd_new(data, ietfnc, "source");
1154 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1155 if (!node) {
1156 lyd_free(data);
1157 return NC_MSG_ERROR;
1158 }
1159 if (rpc_gc->filter) {
1160 if (rpc_gc->filter[0] == '<') {
1161 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1162 lyd_insert_attr(node, "type", "subtree");
1163 } else {
1164 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1165 lyd_insert_attr(node, "type", "xpath");
1166 lyd_insert_attr(node, "select", rpc_gc->filter);
1167 }
1168 if (!node) {
1169 lyd_free(data);
1170 return NC_MSG_ERROR;
1171 }
1172 }
1173
1174 if (rpc_gc->wd_mode) {
1175 if (!ietfncwd) {
1176 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1177 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001178 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001179 return NC_MSG_ERROR;
1180 }
1181 }
1182 switch (rpc_gc->wd_mode) {
1183 case NC_WD_UNKNOWN:
1184 /* cannot get here */
1185 break;
1186 case NC_WD_ALL:
1187 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1188 break;
1189 case NC_WD_ALL_TAG:
1190 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1191 break;
1192 case NC_WD_TRIM:
1193 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1194 break;
1195 case NC_WD_EXPLICIT:
1196 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1197 break;
1198 }
1199 if (!node) {
1200 lyd_free(data);
1201 return NC_MSG_ERROR;
1202 }
1203 }
1204 break;
1205
1206 case NC_RPC_EDIT:
1207 rpc_e = (struct nc_rpc_edit *)rpc;
1208
1209 data = lyd_new(NULL, ietfnc, "edit-config");
1210 node = lyd_new(data, ietfnc, "target");
1211 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1212 if (!node) {
1213 lyd_free(data);
1214 return NC_MSG_ERROR;
1215 }
1216
1217 if (rpc_e->default_op) {
1218 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1219 if (!node) {
1220 lyd_free(data);
1221 return NC_MSG_ERROR;
1222 }
1223 }
1224
1225 if (rpc_e->test_opt) {
1226 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1227 if (!node) {
1228 lyd_free(data);
1229 return NC_MSG_ERROR;
1230 }
1231 }
1232
1233 if (rpc_e->error_opt) {
1234 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1235 if (!node) {
1236 lyd_free(data);
1237 return NC_MSG_ERROR;
1238 }
1239 }
1240
1241 if (rpc_e->edit_cont[0] == '<') {
1242 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1243 } else {
1244 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1245 }
1246 if (!node) {
1247 lyd_free(data);
1248 return NC_MSG_ERROR;
1249 }
1250 break;
1251
1252 case NC_RPC_COPY:
1253 rpc_cp = (struct nc_rpc_copy *)rpc;
1254
1255 data = lyd_new(NULL, ietfnc, "copy-config");
1256 node = lyd_new(data, ietfnc, "target");
1257 if (rpc_cp->url_trg) {
1258 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1259 } else {
1260 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1261 }
1262 if (!node) {
1263 lyd_free(data);
1264 return NC_MSG_ERROR;
1265 }
1266
1267 node = lyd_new(data, ietfnc, "source");
1268 if (rpc_cp->url_config_src) {
1269 if (rpc_cp->url_config_src[0] == '<') {
1270 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1271 } else {
1272 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1273 }
1274 } else {
1275 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1276 }
1277 if (!node) {
1278 lyd_free(data);
1279 return NC_MSG_ERROR;
1280 }
1281
1282 if (rpc_cp->wd_mode) {
1283 if (!ietfncwd) {
1284 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1285 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001286 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001287 return NC_MSG_ERROR;
1288 }
1289 }
1290 switch (rpc_cp->wd_mode) {
1291 case NC_WD_UNKNOWN:
1292 /* cannot get here */
1293 break;
1294 case NC_WD_ALL:
1295 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1296 break;
1297 case NC_WD_ALL_TAG:
1298 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1299 break;
1300 case NC_WD_TRIM:
1301 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1302 break;
1303 case NC_WD_EXPLICIT:
1304 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1305 break;
1306 }
1307 if (!node) {
1308 lyd_free(data);
1309 return NC_MSG_ERROR;
1310 }
1311 }
1312 break;
1313
1314 case NC_RPC_DELETE:
1315 rpc_del = (struct nc_rpc_delete *)rpc;
1316
1317 data = lyd_new(NULL, ietfnc, "delete-config");
1318 node = lyd_new(data, ietfnc, "target");
1319 if (rpc_del->url) {
1320 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1321 } else {
1322 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1323 }
1324 if (!node) {
1325 lyd_free(data);
1326 return NC_MSG_ERROR;
1327 }
1328 break;
1329
1330 case NC_RPC_LOCK:
1331 rpc_lock = (struct nc_rpc_lock *)rpc;
1332
1333 data = lyd_new(NULL, ietfnc, "lock");
1334 node = lyd_new(data, ietfnc, "target");
1335 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1336 if (!node) {
1337 lyd_free(data);
1338 return NC_MSG_ERROR;
1339 }
1340 break;
1341
1342 case NC_RPC_UNLOCK:
1343 rpc_lock = (struct nc_rpc_lock *)rpc;
1344
1345 data = lyd_new(NULL, ietfnc, "unlock");
1346 node = lyd_new(data, ietfnc, "target");
1347 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1348 if (!node) {
1349 lyd_free(data);
1350 return NC_MSG_ERROR;
1351 }
1352 break;
1353
1354 case NC_RPC_GET:
1355 rpc_g = (struct nc_rpc_get *)rpc;
1356
1357 data = lyd_new(NULL, ietfnc, "get");
1358 if (rpc_g->filter) {
1359 if (rpc_g->filter[0] == '<') {
1360 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1361 lyd_insert_attr(node, "type", "subtree");
1362 } else {
1363 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1364 lyd_insert_attr(node, "type", "xpath");
1365 lyd_insert_attr(node, "select", rpc_g->filter);
1366 }
1367 if (!node) {
1368 lyd_free(data);
1369 return NC_MSG_ERROR;
1370 }
1371 }
1372
1373 if (rpc_g->wd_mode) {
1374 if (!ietfncwd) {
1375 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1376 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001377 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001378 return NC_MSG_ERROR;
1379 }
1380 }
1381 switch (rpc_g->wd_mode) {
1382 case NC_WD_UNKNOWN:
1383 /* cannot get here */
1384 break;
1385 case NC_WD_ALL:
1386 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1387 break;
1388 case NC_WD_ALL_TAG:
1389 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1390 break;
1391 case NC_WD_TRIM:
1392 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1393 break;
1394 case NC_WD_EXPLICIT:
1395 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1396 break;
1397 }
1398 if (!node) {
1399 lyd_free(data);
1400 return NC_MSG_ERROR;
1401 }
1402 }
1403 break;
1404
1405 case NC_RPC_KILL:
1406 rpc_k = (struct nc_rpc_kill *)rpc;
1407
1408 data = lyd_new(NULL, ietfnc, "kill-session");
1409 sprintf(str, "%u", rpc_k->sid);
1410 lyd_new_leaf(data, ietfnc, "session-id", str);
1411 break;
1412
1413 case NC_RPC_COMMIT:
1414 rpc_com = (struct nc_rpc_commit *)rpc;
1415
1416 data = lyd_new(NULL, ietfnc, "commit");
1417 if (rpc_com->confirmed) {
1418 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1419 }
1420
1421 if (rpc_com->confirm_timeout) {
1422 sprintf(str, "%u", rpc_com->confirm_timeout);
1423 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1424 }
1425
1426 if (rpc_com->persist) {
1427 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1428 if (!node) {
1429 lyd_free(data);
1430 return NC_MSG_ERROR;
1431 }
1432 }
1433
1434 if (rpc_com->persist_id) {
1435 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1436 if (!node) {
1437 lyd_free(data);
1438 return NC_MSG_ERROR;
1439 }
1440 }
1441 break;
1442
1443 case NC_RPC_DISCARD:
1444 data = lyd_new(NULL, ietfnc, "discard-changes");
1445 break;
1446
1447 case NC_RPC_CANCEL:
1448 rpc_can = (struct nc_rpc_cancel *)rpc;
1449
1450 data = lyd_new(NULL, ietfnc, "cancel-commit");
1451 if (rpc_can->persist_id) {
1452 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1453 if (!node) {
1454 lyd_free(data);
1455 return NC_MSG_ERROR;
1456 }
1457 }
1458 break;
1459
1460 case NC_RPC_VALIDATE:
1461 rpc_val = (struct nc_rpc_validate *)rpc;
1462
1463 data = lyd_new(NULL, ietfnc, "validate");
1464 node = lyd_new(data, ietfnc, "source");
1465 if (rpc_val->url_config_src) {
1466 if (rpc_val->url_config_src[0] == '<') {
1467 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1468 } else {
1469 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1470 }
1471 } else {
1472 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1473 }
1474 if (!node) {
1475 lyd_free(data);
1476 return NC_MSG_ERROR;
1477 }
1478 break;
1479
1480 case NC_RPC_GETSCHEMA:
1481 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1482 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001483 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001484 return NC_MSG_ERROR;
1485 }
1486
1487 rpc_gs = (struct nc_rpc_getschema *)rpc;
1488
1489 data = lyd_new(NULL, ietfncmon, "get-schema");
1490 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1491 if (!node) {
1492 lyd_free(data);
1493 return NC_MSG_ERROR;
1494 }
1495 if (rpc_gs->version) {
1496 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1497 if (!node) {
1498 lyd_free(data);
1499 return NC_MSG_ERROR;
1500 }
1501 }
1502 if (rpc_gs->format) {
1503 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1504 if (!node) {
1505 lyd_free(data);
1506 return NC_MSG_ERROR;
1507 }
1508 }
1509 break;
1510
1511 case NC_RPC_SUBSCRIBE:
1512 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1513 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001514 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001515 return NC_MSG_ERROR;
1516 }
1517
1518 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1519
1520 data = lyd_new(NULL, notifs, "create-subscription");
1521 if (rpc_sub->stream) {
1522 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1523 if (!node) {
1524 lyd_free(data);
1525 return NC_MSG_ERROR;
1526 }
1527 }
1528
1529 if (rpc_sub->filter) {
1530 if (rpc_sub->filter[0] == '<') {
1531 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1532 lyd_insert_attr(node, "type", "subtree");
1533 } else {
1534 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1535 lyd_insert_attr(node, "type", "xpath");
1536 lyd_insert_attr(node, "select", rpc_sub->filter);
1537 }
1538 if (!node) {
1539 lyd_free(data);
1540 return NC_MSG_ERROR;
1541 }
1542 }
1543
1544 if (rpc_sub->start) {
1545 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1546 if (!node) {
1547 lyd_free(data);
1548 return NC_MSG_ERROR;
1549 }
1550 }
1551
1552 if (rpc_sub->stop) {
1553 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1554 if (!node) {
1555 lyd_free(data);
1556 return NC_MSG_ERROR;
1557 }
1558 }
1559 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001560 default:
1561 ERRINT;
1562 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001563 }
1564
1565 if (lyd_validate(data, LYD_OPT_STRICT)) {
1566 lyd_free(data);
1567 return NC_MSG_ERROR;
1568 }
1569
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001570 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1571 if (ret == -1) {
1572 /* error */
1573 r = NC_MSG_ERROR;
1574 } else if (!ret) {
1575 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001576 r = NC_MSG_WOULDBLOCK;
1577 } else {
1578 /* send RPC, store its message ID */
1579 r = nc_send_msg(session, data);
1580 cur_msgid = session->msgid;
1581 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001582 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001583
1584 lyd_free(data);
1585
1586 if (r != NC_MSG_RPC) {
1587 return r;
1588 }
1589
1590 *msgid = cur_msgid;
1591 return NC_MSG_RPC;
1592}