blob: cd49325de4e628dd8b980aef9b1f4b2ec8934928 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <assert.h>
16#include <errno.h>
17#include <fcntl.h>
18#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010019#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <arpa/inet.h>
28#include <poll.h>
29
30#include <libyang/libyang.h>
31
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010033#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010034#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010035
Michal Vasko80ef5d22016-01-18 09:21:02 +010036static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
37
Michal Vaskodaf9a092016-02-09 10:42:05 +010038struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010039
40API int
Michal Vasko3031aae2016-01-27 16:07:18 +010041nc_client_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010042{
Michal Vasko3031aae2016-01-27 16:07:18 +010043 if (client_opts.schema_searchpath) {
44 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010045 }
Michal Vasko086311b2016-01-08 09:53:11 +010046
Michal Vasko7f1c78b2016-01-19 09:52:14 +010047 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010048 client_opts.schema_searchpath = strdup(path);
49 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010050 ERRMEM;
51 return 1;
52 }
53 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010054 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010055 }
56
57 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010058}
59
Michal Vasko3031aae2016-01-27 16:07:18 +010060/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010061static int
62ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
63{
64 const struct lys_module *module;
65 char *ptr, *ptr2;
66 char *model_name, *revision = NULL, *features = NULL;
67
68 /* parse module */
69 ptr = strstr(cpblt, "module=");
70 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010071 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
72 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010073 }
74 ptr += 7;
75 ptr2 = strchr(ptr, '&');
76 if (!ptr2) {
77 ptr2 = ptr + strlen(ptr);
78 }
79 model_name = strndup(ptr, ptr2 - ptr);
80
81 /* parse revision */
82 ptr = strstr(cpblt, "revision=");
83 if (ptr) {
84 ptr += 9;
85 ptr2 = strchr(ptr, '&');
86 if (!ptr2) {
87 ptr2 = ptr + strlen(ptr);
88 }
89 revision = strndup(ptr, ptr2 - ptr);
90 }
91
92 /* load module if needed */
93 module = ly_ctx_get_module(session->ctx, model_name, revision);
94 if (!module) {
95 module = ly_ctx_load_module(session->ctx, model_name, revision);
96 }
97
Michal Vasko086311b2016-01-08 09:53:11 +010098 free(revision);
99 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100100 WRN("Failed to load model \"%s\".", model_name);
101 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100102 return 1;
103 }
Michal Vaskoef578332016-01-25 13:20:09 +0100104 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100105
106 /* parse features */
107 ptr = strstr(cpblt, "features=");
108 if (ptr) {
109 ptr += 9;
110 ptr2 = strchr(ptr, '&');
111 if (!ptr2) {
112 ptr2 = ptr + strlen(ptr);
113 }
114 features = strndup(ptr, ptr2 - ptr);
115 }
116
117 /* enable features */
118 if (features) {
119 /* basically manual strtok_r (to avoid macro) */
120 ptr2 = features;
121 for (ptr = features; *ptr; ++ptr) {
122 if (*ptr == ',') {
123 *ptr = '\0';
124 /* remember last feature */
125 ptr2 = ptr + 1;
126 }
127 }
128
129 ptr = features;
130 lys_features_enable(module, ptr);
131 while (ptr != ptr2) {
132 ptr += strlen(ptr) + 1;
133 lys_features_enable(module, ptr);
134 }
135
136 free(features);
137 }
138
139 return 0;
140}
141
Michal Vasko1aaa6602016-02-09 11:04:33 +0100142/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100143static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100144ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100145{
146 int i;
147 const struct lys_module *ietfnc;
148
149 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
150 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100151 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
152 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100153 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100154 }
155 }
156 if (!ietfnc) {
157 ERR("Loading base NETCONF schema failed.");
158 return 1;
159 }
160
161 /* set supported capabilities from ietf-netconf */
162 for (i = 0; cpblts[i]; ++i) {
163 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
164 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
165 lys_features_enable(ietfnc, "writable-running");
166 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
167 lys_features_enable(ietfnc, "candidate");
168 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
169 lys_features_enable(ietfnc, "confirmed-commit");
170 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
171 lys_features_enable(ietfnc, "rollback-on-error");
172 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
173 lys_features_enable(ietfnc, "validate");
174 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
175 lys_features_enable(ietfnc, "startup");
176 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
177 lys_features_enable(ietfnc, "url");
178 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
179 lys_features_enable(ietfnc, "xpath");
180 }
181 }
182 }
183
184 return 0;
185}
186
187static char *
188libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
Michal Vaskoca4ad152016-03-03 15:50:45 +0100189 void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100190{
191 struct nc_session *session = (struct nc_session *)user_data;
192 struct nc_rpc *rpc;
193 struct nc_reply *reply;
194 struct nc_reply_data *data_rpl;
195 NC_MSG_TYPE msg;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100196 char *model_data = NULL, *ptr, *ptr2, *anyxml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100197 uint64_t msgid;
198
199 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100200 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100201 *format = LYS_IN_YIN;
202
203 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
204 usleep(1000);
205 }
206 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100207 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100208 nc_rpc_free(rpc);
209 return NULL;
210 }
211
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100212 msg = nc_recv_reply(session, rpc, msgid, 250, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100213 nc_rpc_free(rpc);
214 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100215 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100216 return NULL;
217 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100218 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100219 return NULL;
220 }
221
Michal Vasko05ba9df2016-01-13 14:40:27 +0100222 if (reply->type != NC_RPL_DATA) {
223 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100225 nc_reply_free(reply);
226 return NULL;
227 }
228
Michal Vasko086311b2016-01-08 09:53:11 +0100229 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100230 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data_rpl->data)->value, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100231 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100232 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100233
234 /* it's with the data root node, remove it */
235 if (anyxml) {
236 ptr = strchr(anyxml, '>');
237 ++ptr;
238
239 ptr2 = strrchr(anyxml, '<');
240
241 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
242 free(anyxml);
243 }
244
245 return model_data;
246}
247
Michal Vaskoef578332016-01-25 13:20:09 +0100248/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100249int
250nc_ctx_check_and_fill(struct nc_session *session)
251{
Michal Vaskoef578332016-01-25 13:20:09 +0100252 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100253 ly_module_clb old_clb = NULL;
254 void *old_data = NULL;
255
256 assert(session->cpblts && session->ctx);
257
258 /* check if get-schema is supported */
259 for (i = 0; session->cpblts[i]; ++i) {
260 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
261 get_schema_support = 1;
262 break;
263 }
264 }
265
266 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
267 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
268 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
269 /* set module retrieval using <get-schema> */
270 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100271 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100272 } else {
273 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
274 }
275 }
276
277 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100278 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100279 if (old_clb) {
280 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
281 }
Michal Vaskoef578332016-01-25 13:20:09 +0100282 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100283 }
284
285 /* load all other models */
286 for (i = 0; session->cpblts[i]; ++i) {
287 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
288 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
289 continue;
290 }
291
Michal Vaskoef578332016-01-25 13:20:09 +0100292 r = ctx_check_and_load_model(session, session->cpblts[i]);
293 if (r == -1) {
294 ret = -1;
295 break;
296 }
297
298 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
299 * if it was using get-schema */
300 if (r == 1) {
301 if (get_schema_support) {
302 VRB("Trying to load the schema from a different source.");
303 /* works even if old_clb is NULL */
304 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
305 r = ctx_check_and_load_model(session, session->cpblts[i]);
306 }
307
308 /* fail again (or no other way to try), too bad */
309 if (r) {
310 ret = 1;
311 }
312
313 /* set get-schema callback back */
314 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
315 }
Michal Vasko086311b2016-01-08 09:53:11 +0100316 }
317
318 if (old_clb) {
319 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
320 }
Michal Vaskoef578332016-01-25 13:20:09 +0100321 if (ret == 1) {
322 WRN("Some models failed to be loaded, any data from these models will be ignored.");
323 }
324 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100325}
326
327API struct nc_session *
328nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
329{
Michal Vaskod083db62016-01-19 10:31:29 +0100330 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100331
Michal Vaskod083db62016-01-19 10:31:29 +0100332 if ((fdin < 0) || (fdout < 0)) {
333 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100334 return NULL;
335 }
336
337 /* prepare session structure */
338 session = calloc(1, sizeof *session);
339 if (!session) {
340 ERRMEM;
341 return NULL;
342 }
343 session->status = NC_STATUS_STARTING;
344 session->side = NC_CLIENT;
345
346 /* transport specific data */
347 session->ti_type = NC_TI_FD;
348 session->ti.fd.in = fdin;
349 session->ti.fd.out = fdout;
350
351 /* assign context (dicionary needed for handshake) */
352 if (!ctx) {
353 ctx = ly_ctx_new(SCHEMAS_DIR);
354 } else {
355 session->flags |= NC_SESSION_SHAREDCTX;
356 }
357 session->ctx = ctx;
358
359 /* NETCONF handshake */
360 if (nc_handshake(session)) {
361 goto fail;
362 }
363 session->status = NC_STATUS_RUNNING;
364
Michal Vaskoef578332016-01-25 13:20:09 +0100365 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100366 goto fail;
367 }
368
369 return session;
370
371fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100372 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100373 return NULL;
374}
375
376int
Michal Vaskof05562c2016-01-20 12:06:43 +0100377nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100378{
Michal Vasko0190bc32016-03-02 15:47:49 +0100379 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100380 struct addrinfo hints, *res_list, *res;
381 char port_s[6]; /* length of string representation of short int */
382
383 snprintf(port_s, 6, "%u", port);
384
385 /* Connect to a server */
386 memset(&hints, 0, sizeof hints);
387 hints.ai_family = AF_UNSPEC;
388 hints.ai_socktype = SOCK_STREAM;
389 hints.ai_protocol = IPPROTO_TCP;
390 i = getaddrinfo(host, port_s, &hints, &res_list);
391 if (i != 0) {
392 ERR("Unable to translate the host address (%s).", gai_strerror(i));
393 return -1;
394 }
395
396 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
397 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
398 if (sock == -1) {
399 /* socket was not created, try another resource */
400 i = errno;
401 goto errloop;
402 }
403
404 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
405 /* network connection failed, try another resource */
406 i = errno;
407 close(sock);
408 sock = -1;
409 goto errloop;
410 }
411
Michal Vasko0190bc32016-03-02 15:47:49 +0100412 /* make the socket non-blocking */
413 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
414 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100415 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100416 return -1;
417 }
418
Michal Vasko086311b2016-01-08 09:53:11 +0100419 /* 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 Vasko62be1ce2016-03-03 13:24:52 +0100440 int r;
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 Vasko62be1ce2016-03-03 13:24:52 +0100448 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100449 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 }
456
457 /* try to get notification from the session's queue */
458 if (!msgid && session->notifs) {
459 cont = session->notifs;
460 session->notifs = cont->next;
461
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100462 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100463
464 *msg = cont->msg;
465 free(cont);
466
467 return NC_MSG_NOTIF;
468 }
469
470 /* try to get rpc-reply from the session's queue */
471 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100472 while (session->replies) {
473 cont = session->replies;
474 session->replies = cont->next;
475
Michal Vasko086311b2016-01-08 09:53:11 +0100476 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
477 cur_msgid = strtoul(str_msgid, &ptr, 10);
478
479 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100480 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100481 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100482
483 *msg = cont->msg;
484 free(cont);
485
486 return NC_MSG_REPLY;
487 }
488
Michal Vasko2518b6b2016-01-28 13:24:53 +0100489 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
490 lyxml_free(session->ctx, cont->msg);
Michal Vaskob2d91072016-02-01 13:25:20 +0100491 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100492 }
493 }
494
495 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100496 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100497
498 /* we read rpc-reply, want a notif */
499 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100500 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100501 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
502 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100503 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100504 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100505 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100506 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100507 }
Michal Vasko086311b2016-01-08 09:53:11 +0100508
509 cont_ptr = &session->replies;
510 while (*cont_ptr) {
511 cont_ptr = &((*cont_ptr)->next);
512 }
513 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100514 if (!*cont_ptr) {
515 ERRMEM;
516 lyxml_free(session->ctx, xml);
517 return NC_MSG_ERROR;
518 }
Michal Vasko086311b2016-01-08 09:53:11 +0100519 (*cont_ptr)->msg = xml;
520 (*cont_ptr)->next = NULL;
521 }
522
523 /* we read notif, want a rpc-reply */
524 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100525 /* TODO check whether the session is even subscribed */
526 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100527 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100528 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100529 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100530 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100531 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100532
533 cont_ptr = &session->notifs;
534 while (*cont_ptr) {
535 cont_ptr = &((*cont_ptr)->next);
536 }
537 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100538 if (!cont_ptr) {
539 ERRMEM;
540 lyxml_free(session->ctx, xml);
541 return NC_MSG_ERROR;
542 }
Michal Vasko086311b2016-01-08 09:53:11 +0100543 (*cont_ptr)->msg = xml;
544 (*cont_ptr)->next = NULL;
545 }
546
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100547 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100548
549 switch (msgtype) {
550 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100551 if (!msgid) {
552 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100553 }
Michal Vasko086311b2016-01-08 09:53:11 +0100554 break;
555
556 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100557 if (msgid) {
558 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100559 }
Michal Vasko086311b2016-01-08 09:53:11 +0100560 break;
561
562 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100563 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100564 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100565 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100566
567 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100568 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100569 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100570 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100571
572 default:
573 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
574 * NC_MSG_NONE is not returned by nc_read_msg()
575 */
576 break;
577 }
578
579 return msgtype;
580}
581
582/* cannot strictly fail, but does not need to fill any error parameter at all */
583static void
584parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
585{
586 struct lyxml_elem *iter, *next, *info;
587
588 LY_TREE_FOR(xml->child, iter) {
589 if (!iter->ns) {
590 if (iter->content) {
591 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
592 } else {
593 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
594 }
595 continue;
596 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
597 if (iter->content) {
598 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
599 iter->name, iter->content, iter->ns->value);
600 } else {
601 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
602 }
603 continue;
604 }
605
606 if (!strcmp(iter->name, "error-type")) {
607 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
608 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
609 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
610 } else if (err->type) {
611 WRN("<rpc-error> <error-type> duplicated.");
612 } else {
613 err->type = lydict_insert(ctx, iter->content, 0);
614 }
615 } else if (!strcmp(iter->name, "error-tag")) {
616 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
617 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
618 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
619 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
620 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
621 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
622 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
623 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
624 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
625 && strcmp(iter->content, "malformed-message"))) {
626 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
627 } else if (err->tag) {
628 WRN("<rpc-error> <error-tag> duplicated.");
629 } else {
630 err->tag = lydict_insert(ctx, iter->content, 0);
631 }
632 } else if (!strcmp(iter->name, "error-severity")) {
633 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
634 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
635 } else if (err->severity) {
636 WRN("<rpc-error> <error-severity> duplicated.");
637 } else {
638 err->severity = lydict_insert(ctx, iter->content, 0);
639 }
640 } else if (!strcmp(iter->name, "error-app-tag")) {
641 if (err->apptag) {
642 WRN("<rpc-error> <error-app-tag> duplicated.");
643 } else {
644 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
645 }
646 } else if (!strcmp(iter->name, "error-path")) {
647 if (err->path) {
648 WRN("<rpc-error> <error-path> duplicated.");
649 } else {
650 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
651 }
652 } else if (!strcmp(iter->name, "error-message")) {
653 if (err->message) {
654 WRN("<rpc-error> <error-message> duplicated.");
655 } else {
656 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
657 if (!err->message_lang) {
658 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
659 }
660 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
661 }
662 } else if (!strcmp(iter->name, "error-info")) {
663 LY_TREE_FOR_SAFE(iter->child, next, info) {
664 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
665 if (!strcmp(info->name, "session-id")) {
666 if (err->sid) {
667 WRN("<rpc-error> <error-info> <session-id> duplicated.");
668 } else {
669 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
670 }
671 } else if (!strcmp(info->name, "bad-attr")) {
672 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100673 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
674 if (!err->attr) {
675 ERRMEM;
676 return;
677 }
Michal Vasko086311b2016-01-08 09:53:11 +0100678 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
679 } else if (!strcmp(info->name, "bad-element")) {
680 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100681 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
682 if (!err->elem) {
683 ERRMEM;
684 return;
685 }
Michal Vasko086311b2016-01-08 09:53:11 +0100686 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
687 } else if (!strcmp(info->name, "bad-namespace")) {
688 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100689 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
690 if (!err->ns) {
691 ERRMEM;
692 return;
693 }
Michal Vasko086311b2016-01-08 09:53:11 +0100694 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
695 } else {
696 if (info->content) {
697 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
698 info->name, info->content);
699 } else {
700 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
701 }
702 }
703 } else {
704 lyxml_unlink(ctx, info);
705 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100706 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
707 if (!err->other) {
708 ERRMEM;
709 return;
710 }
Michal Vasko086311b2016-01-08 09:53:11 +0100711 err->other[err->other_count - 1] = info;
712 }
713 }
714 } else {
715 if (iter->content) {
716 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
717 } else {
718 WRN("<rpc-error> unknown child \"%s\".", iter->name);
719 }
720 }
721 }
722}
723
724static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100725parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100726{
727 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100728 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100729 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100730 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100731 struct nc_reply_data *data_rpl;
732 struct nc_reply *reply = NULL;
733 struct nc_rpc_generic *rpc_gen;
734 int i;
735
736 if (!xml->child) {
737 ERR("An empty <rpc-reply>.");
738 return NULL;
739 }
740
741 /* rpc-error */
742 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
743 /* count and check elements */
744 i = 0;
745 LY_TREE_FOR(xml->child, iter) {
746 if (strcmp(iter->name, "rpc-error")) {
747 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
748 return NULL;
749 } else if (!iter->ns) {
750 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
751 return NULL;
752 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
753 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
754 return NULL;
755 }
756 ++i;
757 }
758
759 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100760 if (!error_rpl) {
761 ERRMEM;
762 return NULL;
763 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100764 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100765 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100766 if (!error_rpl->err) {
767 ERRMEM;
768 free(error_rpl);
769 return NULL;
770 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100771 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100772 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100773 reply = (struct nc_reply *)error_rpl;
774
775 i = 0;
776 LY_TREE_FOR(xml->child, iter) {
777 parse_rpc_error(ctx, iter, error_rpl->err + i);
778 ++i;
779 }
780
781 /* ok */
782 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
783 if (xml->child->next) {
784 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
785 return NULL;
786 }
787 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100788 if (!reply) {
789 ERRMEM;
790 return NULL;
791 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100792 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100793
794 /* some RPC output */
795 } else {
796 switch (rpc->type) {
797 case NC_RPC_GENERIC:
798 rpc_gen = (struct nc_rpc_generic *)rpc;
799
800 if (rpc_gen->has_data) {
801 schema = rpc_gen->content.data->schema;
802 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100803 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100804 if (!data) {
805 ERR("Failed to parse a generic RPC XML.");
806 return NULL;
807 }
808 schema = data->schema;
809 lyd_free(data);
810 data = NULL;
811 }
812 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100813 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100814 return NULL;
815 }
816 break;
817
818 case NC_RPC_GETCONFIG:
819 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100820 if (!xml->child->child) {
821 /* we did not receive any data */
822 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100823 if (!data_rpl) {
824 ERRMEM;
825 return NULL;
826 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100827 data_rpl->type = NC_RPL_DATA;
828 data_rpl->data = NULL;
829 return (struct nc_reply *)data_rpl;
830 }
831
Michal Vasko086311b2016-01-08 09:53:11 +0100832 /* special treatment */
833 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100834 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100835 if (!data) {
836 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
837 return NULL;
838 }
839 break;
840
841 case NC_RPC_GETSCHEMA:
Michal Vaskofea54dc2016-02-17 13:12:16 +0100842 schema = ly_ctx_get_node(ctx, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100843 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100844 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100845 return NULL;
846 }
847 break;
848
849 case NC_RPC_EDIT:
850 case NC_RPC_COPY:
851 case NC_RPC_DELETE:
852 case NC_RPC_LOCK:
853 case NC_RPC_UNLOCK:
854 case NC_RPC_KILL:
855 case NC_RPC_COMMIT:
856 case NC_RPC_DISCARD:
857 case NC_RPC_CANCEL:
858 case NC_RPC_VALIDATE:
859 case NC_RPC_SUBSCRIBE:
860 /* there is no output defined */
861 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
862 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100863 default:
864 ERRINT;
865 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100866 }
867
868 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100869 if (!data_rpl) {
870 ERRMEM;
871 return NULL;
872 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100873 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100874 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100875 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100876 } else {
877 /* <get>, <get-config> */
878 data_rpl->data = data;
879 }
880 if (!data_rpl->data) {
881 ERR("Failed to parse <rpc-reply>.");
882 free(data_rpl);
883 return NULL;
884 }
885 reply = (struct nc_reply *)data_rpl;
886 }
887
888 return reply;
889}
890
Radek Krejci53691be2016-02-22 13:58:37 +0100891#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100892
Michal Vasko3031aae2016-01-27 16:07:18 +0100893int
894nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
895{
896 int sock;
897
898 if (!address || !port) {
899 ERRARG;
900 return -1;
901 }
902
903 sock = nc_sock_listen(address, port);
904 if (sock == -1) {
905 return -1;
906 }
907
908 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100909 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
910 if (!client_opts.ch_binds) {
911 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100912 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100913 return -1;
914 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100915
916 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100917 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
918 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100919 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100920 return -1;
921 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100922 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
923 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
924 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
925
926 return 0;
927}
928
929int
930nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
931{
932 uint32_t i;
933 int ret = -1;
934
935 if (!address && !port && !ti) {
936 for (i = 0; i < client_opts.ch_bind_count; ++i) {
937 close(client_opts.ch_binds[i].sock);
938 free((char *)client_opts.ch_binds[i].address);
939
940 ret = 0;
941 }
942 free(client_opts.ch_binds);
943 client_opts.ch_binds = NULL;
944 client_opts.ch_bind_count = 0;
945 } else {
946 for (i = 0; i < client_opts.ch_bind_count; ++i) {
947 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
948 && (!port || (client_opts.ch_binds[i].port == port))
949 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
950 close(client_opts.ch_binds[i].sock);
951 free((char *)client_opts.ch_binds[i].address);
952
953 --client_opts.ch_bind_count;
954 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
955
956 ret = 0;
957 }
958 }
959 }
960
961 return ret;
962}
963
964API int
965nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
966{
967 int sock;
968 char *host = NULL;
969 uint16_t port, idx;
970
971 if (!client_opts.ch_binds || !session) {
972 ERRARG;
973 return -1;
974 }
975
976 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
977
Michal Vasko50456e82016-02-02 12:16:08 +0100978 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100979 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100980 return sock;
981 }
982
Radek Krejci53691be2016-02-22 13:58:37 +0100983#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100984 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100985 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100986 } else
987#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100988#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +0100989 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100990 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100991 } else
992#endif
993 {
Michal Vaskofee717c2016-02-01 13:25:43 +0100994 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100995 *session = NULL;
996 }
997
998 free(host);
999
1000 if (!(*session)) {
1001 return -1;
1002 }
1003
1004 return 1;
1005}
1006
Radek Krejci53691be2016-02-22 13:58:37 +01001007#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001008
Michal Vaskob7558c52016-02-26 15:04:19 +01001009API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001010nc_client_init(void)
1011{
1012 nc_init();
1013}
1014
1015API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001016nc_client_destroy(void)
1017{
1018 nc_client_schema_searchpath(NULL);
1019#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1020 nc_client_ch_del_bind(NULL, 0, 0);
1021#endif
1022#ifdef NC_ENABLED_SSH
1023 nc_client_ssh_destroy_opts();
1024#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001025#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001026 nc_client_tls_destroy_opts();
1027#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001028 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001029}
1030
Michal Vasko086311b2016-01-08 09:53:11 +01001031API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001032nc_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 +01001033{
1034 struct lyxml_elem *xml;
1035 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1036
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001037 if (!session || !rpc || !reply || (parseroptions & LYD_OPT_TYPEMASK)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001038 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001039 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001040 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001041 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001042 return NC_MSG_ERROR;
1043 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001044 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001045 *reply = NULL;
1046
1047 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001048
1049 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001050 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001051 lyxml_free(session->ctx, xml);
1052 if (!(*reply)) {
1053 return NC_MSG_ERROR;
1054 }
1055 }
1056
1057 return msgtype;
1058}
1059
1060API NC_MSG_TYPE
1061nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1062{
1063 struct lyxml_elem *xml, *ev_time;
1064 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1065
1066 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +01001067 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001068 return NC_MSG_ERROR;
1069 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001070 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001071 return NC_MSG_ERROR;
1072 }
1073
1074 msgtype = get_msg(session, timeout, 0, &xml);
1075
1076 if (msgtype == NC_MSG_NOTIF) {
1077 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001078 if (!*notif) {
1079 ERRMEM;
1080 lyxml_free(session->ctx, xml);
1081 return NC_MSG_ERROR;
1082 }
Michal Vasko086311b2016-01-08 09:53:11 +01001083
1084 /* eventTime */
1085 LY_TREE_FOR(xml->child, ev_time) {
1086 if (!strcmp(ev_time->name, "eventTime")) {
1087 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1088 /* lyd_parse does not know this element */
1089 lyxml_free(session->ctx, ev_time);
1090 break;
1091 }
1092 }
1093 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001094 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001095 goto fail;
1096 }
1097
1098 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001099 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001100 lyxml_free(session->ctx, xml);
1101 xml = NULL;
1102 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001103 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001104 goto fail;
1105 }
1106 }
1107
1108 return msgtype;
1109
1110fail:
1111 lydict_remove(session->ctx, (*notif)->datetime);
1112 lyd_free((*notif)->tree);
1113 free(*notif);
1114 *notif = NULL;
1115 lyxml_free(session->ctx, xml);
1116
1117 return NC_MSG_ERROR;
1118}
1119
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001120static void *
1121nc_recv_notif_thread(void *arg)
1122{
1123 struct nc_ntf_thread_arg *ntarg;
1124 struct nc_session *session;
1125 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1126 struct nc_notif *notif;
1127 NC_MSG_TYPE msgtype;
1128
1129 ntarg = (struct nc_ntf_thread_arg *)arg;
1130 session = ntarg->session;
1131 notif_clb = ntarg->notif_clb;
1132 free(ntarg);
1133
1134 while (session->ntf_tid) {
1135 msgtype = nc_recv_notif(session, 0, &notif);
1136 if (msgtype == NC_MSG_NOTIF) {
1137 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001138 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1139 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1140 nc_notif_free(notif);
1141 break;
1142 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001143 nc_notif_free(notif);
1144 }
1145
1146 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1147 }
1148
1149 return NULL;
1150}
1151
1152API int
1153nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1154{
1155 struct nc_ntf_thread_arg *ntarg;
1156 int ret;
1157
1158 if (!session || !notif_clb) {
1159 ERRARG;
1160 return -1;
1161 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1162 ERR("Session %u: invalid session to receive Notifications.", session->id);
1163 return -1;
1164 } else if (session->ntf_tid) {
1165 ERR("Session %u: separate notification thread is already running.", session->id);
1166 return -1;
1167 }
1168
1169 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001170 if (!ntarg) {
1171 ERRMEM;
1172 return -1;
1173 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001174 ntarg->session = session;
1175 ntarg->notif_clb = notif_clb;
1176
1177 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1178 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001179 if (!session->ntf_tid) {
1180 ERRMEM;
1181 free(ntarg);
1182 return -1;
1183 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001184
1185 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1186 if (ret) {
1187 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1188 free(ntarg);
1189 free((pthread_t *)session->ntf_tid);
1190 session->ntf_tid = NULL;
1191 return -1;
1192 }
1193
1194 return 0;
1195}
1196
Michal Vasko086311b2016-01-08 09:53:11 +01001197API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001198nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001199{
1200 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001201 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001202 struct nc_rpc_generic *rpc_gen;
1203 struct nc_rpc_getconfig *rpc_gc;
1204 struct nc_rpc_edit *rpc_e;
1205 struct nc_rpc_copy *rpc_cp;
1206 struct nc_rpc_delete *rpc_del;
1207 struct nc_rpc_lock *rpc_lock;
1208 struct nc_rpc_get *rpc_g;
1209 struct nc_rpc_kill *rpc_k;
1210 struct nc_rpc_commit *rpc_com;
1211 struct nc_rpc_cancel *rpc_can;
1212 struct nc_rpc_validate *rpc_val;
1213 struct nc_rpc_getschema *rpc_gs;
1214 struct nc_rpc_subscribe *rpc_sub;
1215 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001216 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001217 char str[11];
1218 uint64_t cur_msgid;
1219
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001220 if (!session || !rpc || !msgid) {
1221 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001222 return NC_MSG_ERROR;
1223 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001224 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001225 return NC_MSG_ERROR;
1226 }
1227
1228 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1229 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1230 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001231 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001232 return NC_MSG_ERROR;
1233 }
1234 }
1235
1236 switch (rpc->type) {
1237 case NC_RPC_GENERIC:
1238 rpc_gen = (struct nc_rpc_generic *)rpc;
1239
1240 if (rpc_gen->has_data) {
1241 data = rpc_gen->content.data;
1242 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001243 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001244 }
1245 break;
1246
1247 case NC_RPC_GETCONFIG:
1248 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1249
1250 data = lyd_new(NULL, ietfnc, "get-config");
1251 node = lyd_new(data, ietfnc, "source");
1252 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1253 if (!node) {
1254 lyd_free(data);
1255 return NC_MSG_ERROR;
1256 }
1257 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001258 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001259 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1260 lyd_insert_attr(node, "type", "subtree");
1261 } else {
1262 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1263 lyd_insert_attr(node, "type", "xpath");
1264 lyd_insert_attr(node, "select", rpc_gc->filter);
1265 }
1266 if (!node) {
1267 lyd_free(data);
1268 return NC_MSG_ERROR;
1269 }
1270 }
1271
1272 if (rpc_gc->wd_mode) {
1273 if (!ietfncwd) {
1274 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1275 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001276 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001277 return NC_MSG_ERROR;
1278 }
1279 }
1280 switch (rpc_gc->wd_mode) {
1281 case NC_WD_UNKNOWN:
1282 /* cannot get here */
1283 break;
1284 case NC_WD_ALL:
1285 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1286 break;
1287 case NC_WD_ALL_TAG:
1288 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1289 break;
1290 case NC_WD_TRIM:
1291 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1292 break;
1293 case NC_WD_EXPLICIT:
1294 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1295 break;
1296 }
1297 if (!node) {
1298 lyd_free(data);
1299 return NC_MSG_ERROR;
1300 }
1301 }
1302 break;
1303
1304 case NC_RPC_EDIT:
1305 rpc_e = (struct nc_rpc_edit *)rpc;
1306
1307 data = lyd_new(NULL, ietfnc, "edit-config");
1308 node = lyd_new(data, ietfnc, "target");
1309 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1310 if (!node) {
1311 lyd_free(data);
1312 return NC_MSG_ERROR;
1313 }
1314
1315 if (rpc_e->default_op) {
1316 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1317 if (!node) {
1318 lyd_free(data);
1319 return NC_MSG_ERROR;
1320 }
1321 }
1322
1323 if (rpc_e->test_opt) {
1324 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1325 if (!node) {
1326 lyd_free(data);
1327 return NC_MSG_ERROR;
1328 }
1329 }
1330
1331 if (rpc_e->error_opt) {
1332 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1333 if (!node) {
1334 lyd_free(data);
1335 return NC_MSG_ERROR;
1336 }
1337 }
1338
1339 if (rpc_e->edit_cont[0] == '<') {
1340 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1341 } else {
1342 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1343 }
1344 if (!node) {
1345 lyd_free(data);
1346 return NC_MSG_ERROR;
1347 }
1348 break;
1349
1350 case NC_RPC_COPY:
1351 rpc_cp = (struct nc_rpc_copy *)rpc;
1352
1353 data = lyd_new(NULL, ietfnc, "copy-config");
1354 node = lyd_new(data, ietfnc, "target");
1355 if (rpc_cp->url_trg) {
1356 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1357 } else {
1358 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1359 }
1360 if (!node) {
1361 lyd_free(data);
1362 return NC_MSG_ERROR;
1363 }
1364
1365 node = lyd_new(data, ietfnc, "source");
1366 if (rpc_cp->url_config_src) {
1367 if (rpc_cp->url_config_src[0] == '<') {
1368 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1369 } else {
1370 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1371 }
1372 } else {
1373 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1374 }
1375 if (!node) {
1376 lyd_free(data);
1377 return NC_MSG_ERROR;
1378 }
1379
1380 if (rpc_cp->wd_mode) {
1381 if (!ietfncwd) {
1382 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1383 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001384 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001385 return NC_MSG_ERROR;
1386 }
1387 }
1388 switch (rpc_cp->wd_mode) {
1389 case NC_WD_UNKNOWN:
1390 /* cannot get here */
1391 break;
1392 case NC_WD_ALL:
1393 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1394 break;
1395 case NC_WD_ALL_TAG:
1396 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1397 break;
1398 case NC_WD_TRIM:
1399 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1400 break;
1401 case NC_WD_EXPLICIT:
1402 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1403 break;
1404 }
1405 if (!node) {
1406 lyd_free(data);
1407 return NC_MSG_ERROR;
1408 }
1409 }
1410 break;
1411
1412 case NC_RPC_DELETE:
1413 rpc_del = (struct nc_rpc_delete *)rpc;
1414
1415 data = lyd_new(NULL, ietfnc, "delete-config");
1416 node = lyd_new(data, ietfnc, "target");
1417 if (rpc_del->url) {
1418 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1419 } else {
1420 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1421 }
1422 if (!node) {
1423 lyd_free(data);
1424 return NC_MSG_ERROR;
1425 }
1426 break;
1427
1428 case NC_RPC_LOCK:
1429 rpc_lock = (struct nc_rpc_lock *)rpc;
1430
1431 data = lyd_new(NULL, ietfnc, "lock");
1432 node = lyd_new(data, ietfnc, "target");
1433 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1434 if (!node) {
1435 lyd_free(data);
1436 return NC_MSG_ERROR;
1437 }
1438 break;
1439
1440 case NC_RPC_UNLOCK:
1441 rpc_lock = (struct nc_rpc_lock *)rpc;
1442
1443 data = lyd_new(NULL, ietfnc, "unlock");
1444 node = lyd_new(data, ietfnc, "target");
1445 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1446 if (!node) {
1447 lyd_free(data);
1448 return NC_MSG_ERROR;
1449 }
1450 break;
1451
1452 case NC_RPC_GET:
1453 rpc_g = (struct nc_rpc_get *)rpc;
1454
1455 data = lyd_new(NULL, ietfnc, "get");
1456 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001457 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001458 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1459 lyd_insert_attr(node, "type", "subtree");
1460 } else {
1461 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1462 lyd_insert_attr(node, "type", "xpath");
1463 lyd_insert_attr(node, "select", rpc_g->filter);
1464 }
1465 if (!node) {
1466 lyd_free(data);
1467 return NC_MSG_ERROR;
1468 }
1469 }
1470
1471 if (rpc_g->wd_mode) {
1472 if (!ietfncwd) {
1473 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1474 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001475 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001476 return NC_MSG_ERROR;
1477 }
1478 }
1479 switch (rpc_g->wd_mode) {
1480 case NC_WD_UNKNOWN:
1481 /* cannot get here */
1482 break;
1483 case NC_WD_ALL:
1484 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1485 break;
1486 case NC_WD_ALL_TAG:
1487 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1488 break;
1489 case NC_WD_TRIM:
1490 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1491 break;
1492 case NC_WD_EXPLICIT:
1493 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1494 break;
1495 }
1496 if (!node) {
1497 lyd_free(data);
1498 return NC_MSG_ERROR;
1499 }
1500 }
1501 break;
1502
1503 case NC_RPC_KILL:
1504 rpc_k = (struct nc_rpc_kill *)rpc;
1505
1506 data = lyd_new(NULL, ietfnc, "kill-session");
1507 sprintf(str, "%u", rpc_k->sid);
1508 lyd_new_leaf(data, ietfnc, "session-id", str);
1509 break;
1510
1511 case NC_RPC_COMMIT:
1512 rpc_com = (struct nc_rpc_commit *)rpc;
1513
1514 data = lyd_new(NULL, ietfnc, "commit");
1515 if (rpc_com->confirmed) {
1516 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1517 }
1518
1519 if (rpc_com->confirm_timeout) {
1520 sprintf(str, "%u", rpc_com->confirm_timeout);
1521 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1522 }
1523
1524 if (rpc_com->persist) {
1525 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1526 if (!node) {
1527 lyd_free(data);
1528 return NC_MSG_ERROR;
1529 }
1530 }
1531
1532 if (rpc_com->persist_id) {
1533 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1534 if (!node) {
1535 lyd_free(data);
1536 return NC_MSG_ERROR;
1537 }
1538 }
1539 break;
1540
1541 case NC_RPC_DISCARD:
1542 data = lyd_new(NULL, ietfnc, "discard-changes");
1543 break;
1544
1545 case NC_RPC_CANCEL:
1546 rpc_can = (struct nc_rpc_cancel *)rpc;
1547
1548 data = lyd_new(NULL, ietfnc, "cancel-commit");
1549 if (rpc_can->persist_id) {
1550 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1551 if (!node) {
1552 lyd_free(data);
1553 return NC_MSG_ERROR;
1554 }
1555 }
1556 break;
1557
1558 case NC_RPC_VALIDATE:
1559 rpc_val = (struct nc_rpc_validate *)rpc;
1560
1561 data = lyd_new(NULL, ietfnc, "validate");
1562 node = lyd_new(data, ietfnc, "source");
1563 if (rpc_val->url_config_src) {
1564 if (rpc_val->url_config_src[0] == '<') {
1565 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1566 } else {
1567 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1568 }
1569 } else {
1570 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1571 }
1572 if (!node) {
1573 lyd_free(data);
1574 return NC_MSG_ERROR;
1575 }
1576 break;
1577
1578 case NC_RPC_GETSCHEMA:
1579 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1580 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001581 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001582 return NC_MSG_ERROR;
1583 }
1584
1585 rpc_gs = (struct nc_rpc_getschema *)rpc;
1586
1587 data = lyd_new(NULL, ietfncmon, "get-schema");
1588 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1589 if (!node) {
1590 lyd_free(data);
1591 return NC_MSG_ERROR;
1592 }
1593 if (rpc_gs->version) {
1594 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1595 if (!node) {
1596 lyd_free(data);
1597 return NC_MSG_ERROR;
1598 }
1599 }
1600 if (rpc_gs->format) {
1601 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1602 if (!node) {
1603 lyd_free(data);
1604 return NC_MSG_ERROR;
1605 }
1606 }
1607 break;
1608
1609 case NC_RPC_SUBSCRIBE:
1610 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1611 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001612 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001613 return NC_MSG_ERROR;
1614 }
1615
1616 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1617
1618 data = lyd_new(NULL, notifs, "create-subscription");
1619 if (rpc_sub->stream) {
1620 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1621 if (!node) {
1622 lyd_free(data);
1623 return NC_MSG_ERROR;
1624 }
1625 }
1626
1627 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001628 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001629 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1630 lyd_insert_attr(node, "type", "subtree");
1631 } else {
1632 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1633 lyd_insert_attr(node, "type", "xpath");
1634 lyd_insert_attr(node, "select", rpc_sub->filter);
1635 }
1636 if (!node) {
1637 lyd_free(data);
1638 return NC_MSG_ERROR;
1639 }
1640 }
1641
1642 if (rpc_sub->start) {
1643 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1644 if (!node) {
1645 lyd_free(data);
1646 return NC_MSG_ERROR;
1647 }
1648 }
1649
1650 if (rpc_sub->stop) {
1651 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1652 if (!node) {
1653 lyd_free(data);
1654 return NC_MSG_ERROR;
1655 }
1656 }
1657 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001658 default:
1659 ERRINT;
1660 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001661 }
1662
1663 if (lyd_validate(data, LYD_OPT_STRICT)) {
1664 lyd_free(data);
1665 return NC_MSG_ERROR;
1666 }
1667
Michal Vasko62be1ce2016-03-03 13:24:52 +01001668 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001669 if (ret == -1) {
1670 /* error */
1671 r = NC_MSG_ERROR;
1672 } else if (!ret) {
1673 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001674 r = NC_MSG_WOULDBLOCK;
1675 } else {
1676 /* send RPC, store its message ID */
1677 r = nc_send_msg(session, data);
1678 cur_msgid = session->msgid;
1679 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001680 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001681
1682 lyd_free(data);
1683
1684 if (r != NC_MSG_RPC) {
1685 return r;
1686 }
1687
1688 *msgid = cur_msgid;
1689 return NC_MSG_RPC;
1690}