blob: 08658011433aed9a73ad14f63d5b7928ea744c06 [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 Vasko7f1c0ef2016-03-11 11:13:06 +010041nc_client_set_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 Vasko7f1c0ef2016-03-11 11:13:06 +010060API const char *
61nc_client_get_schema_searchpath(void)
62{
63 return client_opts.schema_searchpath;
64}
65
Michal Vasko3031aae2016-01-27 16:07:18 +010066/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010067static int
68ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
69{
70 const struct lys_module *module;
71 char *ptr, *ptr2;
72 char *model_name, *revision = NULL, *features = NULL;
73
74 /* parse module */
75 ptr = strstr(cpblt, "module=");
76 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010077 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
78 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010079 }
80 ptr += 7;
81 ptr2 = strchr(ptr, '&');
82 if (!ptr2) {
83 ptr2 = ptr + strlen(ptr);
84 }
85 model_name = strndup(ptr, ptr2 - ptr);
86
87 /* parse revision */
88 ptr = strstr(cpblt, "revision=");
89 if (ptr) {
90 ptr += 9;
91 ptr2 = strchr(ptr, '&');
92 if (!ptr2) {
93 ptr2 = ptr + strlen(ptr);
94 }
95 revision = strndup(ptr, ptr2 - ptr);
96 }
97
98 /* load module if needed */
99 module = ly_ctx_get_module(session->ctx, model_name, revision);
100 if (!module) {
101 module = ly_ctx_load_module(session->ctx, model_name, revision);
102 }
103
Michal Vasko086311b2016-01-08 09:53:11 +0100104 free(revision);
105 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100106 WRN("Failed to load model \"%s\".", model_name);
107 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100108 return 1;
109 }
Michal Vaskoef578332016-01-25 13:20:09 +0100110 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100111
112 /* parse features */
113 ptr = strstr(cpblt, "features=");
114 if (ptr) {
115 ptr += 9;
116 ptr2 = strchr(ptr, '&');
117 if (!ptr2) {
118 ptr2 = ptr + strlen(ptr);
119 }
120 features = strndup(ptr, ptr2 - ptr);
121 }
122
123 /* enable features */
124 if (features) {
125 /* basically manual strtok_r (to avoid macro) */
126 ptr2 = features;
127 for (ptr = features; *ptr; ++ptr) {
128 if (*ptr == ',') {
129 *ptr = '\0';
130 /* remember last feature */
131 ptr2 = ptr + 1;
132 }
133 }
134
135 ptr = features;
136 lys_features_enable(module, ptr);
137 while (ptr != ptr2) {
138 ptr += strlen(ptr) + 1;
139 lys_features_enable(module, ptr);
140 }
141
142 free(features);
143 }
144
145 return 0;
146}
147
Michal Vasko1aaa6602016-02-09 11:04:33 +0100148/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100149static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100150ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100151{
152 int i;
153 const struct lys_module *ietfnc;
154
155 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
156 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100157 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
158 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100159 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100160 }
161 }
162 if (!ietfnc) {
163 ERR("Loading base NETCONF schema failed.");
164 return 1;
165 }
166
167 /* set supported capabilities from ietf-netconf */
168 for (i = 0; cpblts[i]; ++i) {
169 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
170 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
171 lys_features_enable(ietfnc, "writable-running");
172 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
173 lys_features_enable(ietfnc, "candidate");
174 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
175 lys_features_enable(ietfnc, "confirmed-commit");
176 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
177 lys_features_enable(ietfnc, "rollback-on-error");
178 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
179 lys_features_enable(ietfnc, "validate");
180 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
181 lys_features_enable(ietfnc, "startup");
182 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
183 lys_features_enable(ietfnc, "url");
184 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
185 lys_features_enable(ietfnc, "xpath");
186 }
187 }
188 }
189
190 return 0;
191}
192
193static char *
194libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
Michal Vaskoca4ad152016-03-03 15:50:45 +0100195 void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100196{
197 struct nc_session *session = (struct nc_session *)user_data;
198 struct nc_rpc *rpc;
199 struct nc_reply *reply;
200 struct nc_reply_data *data_rpl;
201 NC_MSG_TYPE msg;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100202 char *model_data = NULL, *ptr, *ptr2, *anyxml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100203 uint64_t msgid;
204
205 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100206 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100207 *format = LYS_IN_YIN;
208
209 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
210 usleep(1000);
211 }
212 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100213 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100214 nc_rpc_free(rpc);
215 return NULL;
216 }
217
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100218 msg = nc_recv_reply(session, rpc, msgid, 250, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100219 nc_rpc_free(rpc);
220 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100221 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100222 return NULL;
223 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100225 return NULL;
226 }
227
Michal Vasko05ba9df2016-01-13 14:40:27 +0100228 if (reply->type != NC_RPL_DATA) {
229 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100230 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100231 nc_reply_free(reply);
232 return NULL;
233 }
234
Michal Vasko086311b2016-01-08 09:53:11 +0100235 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100236 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data_rpl->data)->value, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100237 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100238 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100239
240 /* it's with the data root node, remove it */
241 if (anyxml) {
242 ptr = strchr(anyxml, '>');
243 ++ptr;
244
245 ptr2 = strrchr(anyxml, '<');
246
247 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
248 free(anyxml);
249 }
250
251 return model_data;
252}
253
Michal Vaskoef578332016-01-25 13:20:09 +0100254/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100255int
256nc_ctx_check_and_fill(struct nc_session *session)
257{
Michal Vaskoef578332016-01-25 13:20:09 +0100258 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100259 ly_module_clb old_clb = NULL;
260 void *old_data = NULL;
261
262 assert(session->cpblts && session->ctx);
263
264 /* check if get-schema is supported */
265 for (i = 0; session->cpblts[i]; ++i) {
266 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
267 get_schema_support = 1;
268 break;
269 }
270 }
271
272 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
273 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
274 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
275 /* set module retrieval using <get-schema> */
276 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100277 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100278 } else {
279 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
280 }
281 }
282
283 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100284 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100285 if (old_clb) {
286 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
287 }
Michal Vaskoef578332016-01-25 13:20:09 +0100288 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100289 }
290
291 /* load all other models */
292 for (i = 0; session->cpblts[i]; ++i) {
293 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
294 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
295 continue;
296 }
297
Michal Vaskoef578332016-01-25 13:20:09 +0100298 r = ctx_check_and_load_model(session, session->cpblts[i]);
299 if (r == -1) {
300 ret = -1;
301 break;
302 }
303
304 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
305 * if it was using get-schema */
306 if (r == 1) {
307 if (get_schema_support) {
308 VRB("Trying to load the schema from a different source.");
309 /* works even if old_clb is NULL */
310 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
311 r = ctx_check_and_load_model(session, session->cpblts[i]);
312 }
313
314 /* fail again (or no other way to try), too bad */
315 if (r) {
316 ret = 1;
317 }
318
319 /* set get-schema callback back */
320 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
321 }
Michal Vasko086311b2016-01-08 09:53:11 +0100322 }
323
324 if (old_clb) {
325 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
326 }
Michal Vaskoef578332016-01-25 13:20:09 +0100327 if (ret == 1) {
328 WRN("Some models failed to be loaded, any data from these models will be ignored.");
329 }
330 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100331}
332
333API struct nc_session *
334nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
335{
Michal Vaskod083db62016-01-19 10:31:29 +0100336 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100337
Michal Vaskod083db62016-01-19 10:31:29 +0100338 if ((fdin < 0) || (fdout < 0)) {
339 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100340 return NULL;
341 }
342
343 /* prepare session structure */
344 session = calloc(1, sizeof *session);
345 if (!session) {
346 ERRMEM;
347 return NULL;
348 }
349 session->status = NC_STATUS_STARTING;
350 session->side = NC_CLIENT;
351
352 /* transport specific data */
353 session->ti_type = NC_TI_FD;
354 session->ti.fd.in = fdin;
355 session->ti.fd.out = fdout;
356
357 /* assign context (dicionary needed for handshake) */
358 if (!ctx) {
359 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100360 /* definitely should not happen, but be ready */
361 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
362 /* that's just it */
363 goto fail;
364 }
Michal Vasko086311b2016-01-08 09:53:11 +0100365 } else {
366 session->flags |= NC_SESSION_SHAREDCTX;
367 }
368 session->ctx = ctx;
369
370 /* NETCONF handshake */
371 if (nc_handshake(session)) {
372 goto fail;
373 }
374 session->status = NC_STATUS_RUNNING;
375
Michal Vaskoef578332016-01-25 13:20:09 +0100376 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100377 goto fail;
378 }
379
380 return session;
381
382fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100383 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100384 return NULL;
385}
386
387int
Michal Vaskof05562c2016-01-20 12:06:43 +0100388nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100389{
Michal Vasko0190bc32016-03-02 15:47:49 +0100390 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100391 struct addrinfo hints, *res_list, *res;
392 char port_s[6]; /* length of string representation of short int */
393
394 snprintf(port_s, 6, "%u", port);
395
396 /* Connect to a server */
397 memset(&hints, 0, sizeof hints);
398 hints.ai_family = AF_UNSPEC;
399 hints.ai_socktype = SOCK_STREAM;
400 hints.ai_protocol = IPPROTO_TCP;
401 i = getaddrinfo(host, port_s, &hints, &res_list);
402 if (i != 0) {
403 ERR("Unable to translate the host address (%s).", gai_strerror(i));
404 return -1;
405 }
406
407 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
408 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
409 if (sock == -1) {
410 /* socket was not created, try another resource */
411 i = errno;
412 goto errloop;
413 }
414
415 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
416 /* network connection failed, try another resource */
417 i = errno;
418 close(sock);
419 sock = -1;
420 goto errloop;
421 }
422
Michal Vasko0190bc32016-03-02 15:47:49 +0100423 /* make the socket non-blocking */
424 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
425 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100426 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100427 return -1;
428 }
429
Michal Vasko086311b2016-01-08 09:53:11 +0100430 /* we're done, network connection established */
431 break;
432errloop:
433 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
434 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
435 continue;
436 }
437
438 if (sock == -1) {
439 ERR("Unable to connect to %s:%s.", host, port_s);
440 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100441 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 +0100442 }
443 freeaddrinfo(res_list);
444
445 return sock;
446}
447
Michal Vasko086311b2016-01-08 09:53:11 +0100448static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100449get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100450{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100451 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100452 char *ptr;
453 const char *str_msgid;
454 uint64_t cur_msgid;
455 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100456 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100457 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
458
Michal Vasko62be1ce2016-03-03 13:24:52 +0100459 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100460 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100461 /* error */
462 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100463 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100464 /* timeout */
465 return NC_MSG_WOULDBLOCK;
466 }
467
468 /* try to get notification from the session's queue */
469 if (!msgid && session->notifs) {
470 cont = session->notifs;
471 session->notifs = cont->next;
472
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100473 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100474
475 *msg = cont->msg;
476 free(cont);
477
478 return NC_MSG_NOTIF;
479 }
480
481 /* try to get rpc-reply from the session's queue */
482 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100483 while (session->replies) {
484 cont = session->replies;
485 session->replies = cont->next;
486
Michal Vasko086311b2016-01-08 09:53:11 +0100487 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
488 cur_msgid = strtoul(str_msgid, &ptr, 10);
489
490 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100491 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100492 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100493
494 *msg = cont->msg;
495 free(cont);
496
497 return NC_MSG_REPLY;
498 }
499
Michal Vasko2518b6b2016-01-28 13:24:53 +0100500 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
501 lyxml_free(session->ctx, cont->msg);
Michal Vaskob2d91072016-02-01 13:25:20 +0100502 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100503 }
504 }
505
506 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100507 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100508
509 /* we read rpc-reply, want a notif */
510 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100511 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100512 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
513 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100514 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100515 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100516 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100517 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100518 }
Michal Vasko086311b2016-01-08 09:53:11 +0100519
520 cont_ptr = &session->replies;
521 while (*cont_ptr) {
522 cont_ptr = &((*cont_ptr)->next);
523 }
524 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100525 if (!*cont_ptr) {
526 ERRMEM;
527 lyxml_free(session->ctx, xml);
528 return NC_MSG_ERROR;
529 }
Michal Vasko086311b2016-01-08 09:53:11 +0100530 (*cont_ptr)->msg = xml;
531 (*cont_ptr)->next = NULL;
532 }
533
534 /* we read notif, want a rpc-reply */
535 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100536 /* TODO check whether the session is even subscribed */
537 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100538 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100539 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100540 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100541 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100542 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100543
544 cont_ptr = &session->notifs;
545 while (*cont_ptr) {
546 cont_ptr = &((*cont_ptr)->next);
547 }
548 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100549 if (!cont_ptr) {
550 ERRMEM;
551 lyxml_free(session->ctx, xml);
552 return NC_MSG_ERROR;
553 }
Michal Vasko086311b2016-01-08 09:53:11 +0100554 (*cont_ptr)->msg = xml;
555 (*cont_ptr)->next = NULL;
556 }
557
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100558 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100559
560 switch (msgtype) {
561 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100562 if (!msgid) {
563 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100564 }
Michal Vasko086311b2016-01-08 09:53:11 +0100565 break;
566
567 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100568 if (msgid) {
569 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100570 }
Michal Vasko086311b2016-01-08 09:53:11 +0100571 break;
572
573 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100574 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100575 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100576 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100577
578 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100579 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100580 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100581 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100582
583 default:
584 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
585 * NC_MSG_NONE is not returned by nc_read_msg()
586 */
587 break;
588 }
589
590 return msgtype;
591}
592
593/* cannot strictly fail, but does not need to fill any error parameter at all */
594static void
595parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
596{
597 struct lyxml_elem *iter, *next, *info;
598
599 LY_TREE_FOR(xml->child, iter) {
600 if (!iter->ns) {
601 if (iter->content) {
602 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
603 } else {
604 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
605 }
606 continue;
607 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
608 if (iter->content) {
609 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
610 iter->name, iter->content, iter->ns->value);
611 } else {
612 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
613 }
614 continue;
615 }
616
617 if (!strcmp(iter->name, "error-type")) {
618 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
619 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
620 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
621 } else if (err->type) {
622 WRN("<rpc-error> <error-type> duplicated.");
623 } else {
624 err->type = lydict_insert(ctx, iter->content, 0);
625 }
626 } else if (!strcmp(iter->name, "error-tag")) {
627 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
628 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
629 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
630 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
631 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
632 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
633 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
634 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
635 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
636 && strcmp(iter->content, "malformed-message"))) {
637 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
638 } else if (err->tag) {
639 WRN("<rpc-error> <error-tag> duplicated.");
640 } else {
641 err->tag = lydict_insert(ctx, iter->content, 0);
642 }
643 } else if (!strcmp(iter->name, "error-severity")) {
644 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
645 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
646 } else if (err->severity) {
647 WRN("<rpc-error> <error-severity> duplicated.");
648 } else {
649 err->severity = lydict_insert(ctx, iter->content, 0);
650 }
651 } else if (!strcmp(iter->name, "error-app-tag")) {
652 if (err->apptag) {
653 WRN("<rpc-error> <error-app-tag> duplicated.");
654 } else {
655 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
656 }
657 } else if (!strcmp(iter->name, "error-path")) {
658 if (err->path) {
659 WRN("<rpc-error> <error-path> duplicated.");
660 } else {
661 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
662 }
663 } else if (!strcmp(iter->name, "error-message")) {
664 if (err->message) {
665 WRN("<rpc-error> <error-message> duplicated.");
666 } else {
667 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
668 if (!err->message_lang) {
669 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
670 }
671 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
672 }
673 } else if (!strcmp(iter->name, "error-info")) {
674 LY_TREE_FOR_SAFE(iter->child, next, info) {
675 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
676 if (!strcmp(info->name, "session-id")) {
677 if (err->sid) {
678 WRN("<rpc-error> <error-info> <session-id> duplicated.");
679 } else {
680 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
681 }
682 } else if (!strcmp(info->name, "bad-attr")) {
683 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100684 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
685 if (!err->attr) {
686 ERRMEM;
687 return;
688 }
Michal Vasko086311b2016-01-08 09:53:11 +0100689 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
690 } else if (!strcmp(info->name, "bad-element")) {
691 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100692 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
693 if (!err->elem) {
694 ERRMEM;
695 return;
696 }
Michal Vasko086311b2016-01-08 09:53:11 +0100697 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
698 } else if (!strcmp(info->name, "bad-namespace")) {
699 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100700 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
701 if (!err->ns) {
702 ERRMEM;
703 return;
704 }
Michal Vasko086311b2016-01-08 09:53:11 +0100705 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
706 } else {
707 if (info->content) {
708 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
709 info->name, info->content);
710 } else {
711 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
712 }
713 }
714 } else {
715 lyxml_unlink(ctx, info);
716 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100717 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
718 if (!err->other) {
719 ERRMEM;
720 return;
721 }
Michal Vasko086311b2016-01-08 09:53:11 +0100722 err->other[err->other_count - 1] = info;
723 }
724 }
725 } else {
726 if (iter->content) {
727 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
728 } else {
729 WRN("<rpc-error> unknown child \"%s\".", iter->name);
730 }
731 }
732 }
733}
734
735static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100736parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100737{
738 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100739 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100740 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100741 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100742 struct nc_reply_data *data_rpl;
743 struct nc_reply *reply = NULL;
744 struct nc_rpc_generic *rpc_gen;
745 int i;
746
747 if (!xml->child) {
748 ERR("An empty <rpc-reply>.");
749 return NULL;
750 }
751
752 /* rpc-error */
753 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
754 /* count and check elements */
755 i = 0;
756 LY_TREE_FOR(xml->child, iter) {
757 if (strcmp(iter->name, "rpc-error")) {
758 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
759 return NULL;
760 } else if (!iter->ns) {
761 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
762 return NULL;
763 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
764 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
765 return NULL;
766 }
767 ++i;
768 }
769
770 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100771 if (!error_rpl) {
772 ERRMEM;
773 return NULL;
774 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100775 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100776 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100777 if (!error_rpl->err) {
778 ERRMEM;
779 free(error_rpl);
780 return NULL;
781 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100782 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100783 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100784 reply = (struct nc_reply *)error_rpl;
785
786 i = 0;
787 LY_TREE_FOR(xml->child, iter) {
788 parse_rpc_error(ctx, iter, error_rpl->err + i);
789 ++i;
790 }
791
792 /* ok */
793 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
794 if (xml->child->next) {
795 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
796 return NULL;
797 }
798 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100799 if (!reply) {
800 ERRMEM;
801 return NULL;
802 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100803 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100804
805 /* some RPC output */
806 } else {
807 switch (rpc->type) {
808 case NC_RPC_GENERIC:
809 rpc_gen = (struct nc_rpc_generic *)rpc;
810
811 if (rpc_gen->has_data) {
812 schema = rpc_gen->content.data->schema;
813 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100814 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100815 if (!data) {
816 ERR("Failed to parse a generic RPC XML.");
817 return NULL;
818 }
819 schema = data->schema;
820 lyd_free(data);
821 data = NULL;
822 }
823 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100824 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100825 return NULL;
826 }
827 break;
828
829 case NC_RPC_GETCONFIG:
830 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100831 if (!xml->child->child) {
832 /* we did not receive any data */
833 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100834 if (!data_rpl) {
835 ERRMEM;
836 return NULL;
837 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100838 data_rpl->type = NC_RPL_DATA;
839 data_rpl->data = NULL;
840 return (struct nc_reply *)data_rpl;
841 }
842
Michal Vasko086311b2016-01-08 09:53:11 +0100843 /* special treatment */
844 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100845 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100846 if (!data) {
847 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
848 return NULL;
849 }
850 break;
851
852 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100853 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100854 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100855 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100856 return NULL;
857 }
858 break;
859
860 case NC_RPC_EDIT:
861 case NC_RPC_COPY:
862 case NC_RPC_DELETE:
863 case NC_RPC_LOCK:
864 case NC_RPC_UNLOCK:
865 case NC_RPC_KILL:
866 case NC_RPC_COMMIT:
867 case NC_RPC_DISCARD:
868 case NC_RPC_CANCEL:
869 case NC_RPC_VALIDATE:
870 case NC_RPC_SUBSCRIBE:
871 /* there is no output defined */
872 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
873 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100874 default:
875 ERRINT;
876 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100877 }
878
879 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100880 if (!data_rpl) {
881 ERRMEM;
882 return NULL;
883 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100884 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100885 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100886 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100887 } else {
888 /* <get>, <get-config> */
889 data_rpl->data = data;
890 }
891 if (!data_rpl->data) {
892 ERR("Failed to parse <rpc-reply>.");
893 free(data_rpl);
894 return NULL;
895 }
896 reply = (struct nc_reply *)data_rpl;
897 }
898
899 return reply;
900}
901
Radek Krejci53691be2016-02-22 13:58:37 +0100902#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100903
Michal Vasko3031aae2016-01-27 16:07:18 +0100904int
905nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
906{
907 int sock;
908
909 if (!address || !port) {
910 ERRARG;
911 return -1;
912 }
913
914 sock = nc_sock_listen(address, port);
915 if (sock == -1) {
916 return -1;
917 }
918
919 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100920 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
921 if (!client_opts.ch_binds) {
922 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100923 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100924 return -1;
925 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100926
927 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100928 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
929 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100930 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100931 return -1;
932 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100933 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
934 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
935 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
936
937 return 0;
938}
939
940int
941nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
942{
943 uint32_t i;
944 int ret = -1;
945
946 if (!address && !port && !ti) {
947 for (i = 0; i < client_opts.ch_bind_count; ++i) {
948 close(client_opts.ch_binds[i].sock);
949 free((char *)client_opts.ch_binds[i].address);
950
951 ret = 0;
952 }
953 free(client_opts.ch_binds);
954 client_opts.ch_binds = NULL;
955 client_opts.ch_bind_count = 0;
956 } else {
957 for (i = 0; i < client_opts.ch_bind_count; ++i) {
958 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
959 && (!port || (client_opts.ch_binds[i].port == port))
960 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
961 close(client_opts.ch_binds[i].sock);
962 free((char *)client_opts.ch_binds[i].address);
963
964 --client_opts.ch_bind_count;
965 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
966
967 ret = 0;
968 }
969 }
970 }
971
972 return ret;
973}
974
975API int
976nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
977{
978 int sock;
979 char *host = NULL;
980 uint16_t port, idx;
981
982 if (!client_opts.ch_binds || !session) {
983 ERRARG;
984 return -1;
985 }
986
987 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
988
Michal Vasko50456e82016-02-02 12:16:08 +0100989 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100990 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100991 return sock;
992 }
993
Radek Krejci53691be2016-02-22 13:58:37 +0100994#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100995 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100996 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100997 } else
998#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100999#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001000 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001001 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001002 } else
1003#endif
1004 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001005 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001006 *session = NULL;
1007 }
1008
1009 free(host);
1010
1011 if (!(*session)) {
1012 return -1;
1013 }
1014
1015 return 1;
1016}
1017
Radek Krejci53691be2016-02-22 13:58:37 +01001018#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001019
Michal Vaskob7558c52016-02-26 15:04:19 +01001020API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001021nc_client_init(void)
1022{
1023 nc_init();
1024}
1025
1026API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001027nc_client_destroy(void)
1028{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001029 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001030#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1031 nc_client_ch_del_bind(NULL, 0, 0);
1032#endif
1033#ifdef NC_ENABLED_SSH
1034 nc_client_ssh_destroy_opts();
1035#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001036#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001037 nc_client_tls_destroy_opts();
1038#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001039 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001040}
1041
Michal Vasko086311b2016-01-08 09:53:11 +01001042API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001043nc_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 +01001044{
1045 struct lyxml_elem *xml;
1046 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1047
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001048 if (!session || !rpc || !reply || (parseroptions & LYD_OPT_TYPEMASK)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001049 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001050 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001051 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001052 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001053 return NC_MSG_ERROR;
1054 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001055 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001056 *reply = NULL;
1057
1058 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001059
1060 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001061 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001062 lyxml_free(session->ctx, xml);
1063 if (!(*reply)) {
1064 return NC_MSG_ERROR;
1065 }
1066 }
1067
1068 return msgtype;
1069}
1070
1071API NC_MSG_TYPE
1072nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1073{
1074 struct lyxml_elem *xml, *ev_time;
1075 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1076
1077 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +01001078 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001079 return NC_MSG_ERROR;
1080 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001081 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001082 return NC_MSG_ERROR;
1083 }
1084
1085 msgtype = get_msg(session, timeout, 0, &xml);
1086
1087 if (msgtype == NC_MSG_NOTIF) {
1088 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001089 if (!*notif) {
1090 ERRMEM;
1091 lyxml_free(session->ctx, xml);
1092 return NC_MSG_ERROR;
1093 }
Michal Vasko086311b2016-01-08 09:53:11 +01001094
1095 /* eventTime */
1096 LY_TREE_FOR(xml->child, ev_time) {
1097 if (!strcmp(ev_time->name, "eventTime")) {
1098 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1099 /* lyd_parse does not know this element */
1100 lyxml_free(session->ctx, ev_time);
1101 break;
1102 }
1103 }
1104 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001105 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001106 goto fail;
1107 }
1108
1109 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001110 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001111 lyxml_free(session->ctx, xml);
1112 xml = NULL;
1113 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001114 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001115 goto fail;
1116 }
1117 }
1118
1119 return msgtype;
1120
1121fail:
1122 lydict_remove(session->ctx, (*notif)->datetime);
1123 lyd_free((*notif)->tree);
1124 free(*notif);
1125 *notif = NULL;
1126 lyxml_free(session->ctx, xml);
1127
1128 return NC_MSG_ERROR;
1129}
1130
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001131static void *
1132nc_recv_notif_thread(void *arg)
1133{
1134 struct nc_ntf_thread_arg *ntarg;
1135 struct nc_session *session;
1136 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1137 struct nc_notif *notif;
1138 NC_MSG_TYPE msgtype;
1139
1140 ntarg = (struct nc_ntf_thread_arg *)arg;
1141 session = ntarg->session;
1142 notif_clb = ntarg->notif_clb;
1143 free(ntarg);
1144
1145 while (session->ntf_tid) {
1146 msgtype = nc_recv_notif(session, 0, &notif);
1147 if (msgtype == NC_MSG_NOTIF) {
1148 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001149 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1150 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1151 nc_notif_free(notif);
1152 break;
1153 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001154 nc_notif_free(notif);
1155 }
1156
1157 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1158 }
1159
1160 return NULL;
1161}
1162
1163API int
1164nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1165{
1166 struct nc_ntf_thread_arg *ntarg;
1167 int ret;
1168
1169 if (!session || !notif_clb) {
1170 ERRARG;
1171 return -1;
1172 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1173 ERR("Session %u: invalid session to receive Notifications.", session->id);
1174 return -1;
1175 } else if (session->ntf_tid) {
1176 ERR("Session %u: separate notification thread is already running.", session->id);
1177 return -1;
1178 }
1179
1180 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001181 if (!ntarg) {
1182 ERRMEM;
1183 return -1;
1184 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001185 ntarg->session = session;
1186 ntarg->notif_clb = notif_clb;
1187
1188 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1189 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001190 if (!session->ntf_tid) {
1191 ERRMEM;
1192 free(ntarg);
1193 return -1;
1194 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001195
1196 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1197 if (ret) {
1198 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1199 free(ntarg);
1200 free((pthread_t *)session->ntf_tid);
1201 session->ntf_tid = NULL;
1202 return -1;
1203 }
1204
1205 return 0;
1206}
1207
Michal Vasko086311b2016-01-08 09:53:11 +01001208API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001209nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001210{
1211 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001212 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001213 struct nc_rpc_generic *rpc_gen;
1214 struct nc_rpc_getconfig *rpc_gc;
1215 struct nc_rpc_edit *rpc_e;
1216 struct nc_rpc_copy *rpc_cp;
1217 struct nc_rpc_delete *rpc_del;
1218 struct nc_rpc_lock *rpc_lock;
1219 struct nc_rpc_get *rpc_g;
1220 struct nc_rpc_kill *rpc_k;
1221 struct nc_rpc_commit *rpc_com;
1222 struct nc_rpc_cancel *rpc_can;
1223 struct nc_rpc_validate *rpc_val;
1224 struct nc_rpc_getschema *rpc_gs;
1225 struct nc_rpc_subscribe *rpc_sub;
1226 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001227 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001228 char str[11];
1229 uint64_t cur_msgid;
1230
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001231 if (!session || !rpc || !msgid) {
1232 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001233 return NC_MSG_ERROR;
1234 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001235 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001236 return NC_MSG_ERROR;
1237 }
1238
1239 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1240 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1241 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001242 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001243 return NC_MSG_ERROR;
1244 }
1245 }
1246
1247 switch (rpc->type) {
1248 case NC_RPC_GENERIC:
1249 rpc_gen = (struct nc_rpc_generic *)rpc;
1250
1251 if (rpc_gen->has_data) {
1252 data = rpc_gen->content.data;
1253 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001254 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001255 }
1256 break;
1257
1258 case NC_RPC_GETCONFIG:
1259 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1260
1261 data = lyd_new(NULL, ietfnc, "get-config");
1262 node = lyd_new(data, ietfnc, "source");
1263 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1264 if (!node) {
1265 lyd_free(data);
1266 return NC_MSG_ERROR;
1267 }
1268 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001269 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001270 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001271 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001272 } else {
1273 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001274 lyd_insert_attr(node, NULL, "type", "xpath");
1275 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001276 }
1277 if (!node) {
1278 lyd_free(data);
1279 return NC_MSG_ERROR;
1280 }
1281 }
1282
1283 if (rpc_gc->wd_mode) {
1284 if (!ietfncwd) {
1285 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1286 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001287 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001288 return NC_MSG_ERROR;
1289 }
1290 }
1291 switch (rpc_gc->wd_mode) {
1292 case NC_WD_UNKNOWN:
1293 /* cannot get here */
1294 break;
1295 case NC_WD_ALL:
1296 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1297 break;
1298 case NC_WD_ALL_TAG:
1299 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1300 break;
1301 case NC_WD_TRIM:
1302 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1303 break;
1304 case NC_WD_EXPLICIT:
1305 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1306 break;
1307 }
1308 if (!node) {
1309 lyd_free(data);
1310 return NC_MSG_ERROR;
1311 }
1312 }
1313 break;
1314
1315 case NC_RPC_EDIT:
1316 rpc_e = (struct nc_rpc_edit *)rpc;
1317
1318 data = lyd_new(NULL, ietfnc, "edit-config");
1319 node = lyd_new(data, ietfnc, "target");
1320 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1321 if (!node) {
1322 lyd_free(data);
1323 return NC_MSG_ERROR;
1324 }
1325
1326 if (rpc_e->default_op) {
1327 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1328 if (!node) {
1329 lyd_free(data);
1330 return NC_MSG_ERROR;
1331 }
1332 }
1333
1334 if (rpc_e->test_opt) {
1335 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1336 if (!node) {
1337 lyd_free(data);
1338 return NC_MSG_ERROR;
1339 }
1340 }
1341
1342 if (rpc_e->error_opt) {
1343 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1344 if (!node) {
1345 lyd_free(data);
1346 return NC_MSG_ERROR;
1347 }
1348 }
1349
1350 if (rpc_e->edit_cont[0] == '<') {
1351 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1352 } else {
1353 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1354 }
1355 if (!node) {
1356 lyd_free(data);
1357 return NC_MSG_ERROR;
1358 }
1359 break;
1360
1361 case NC_RPC_COPY:
1362 rpc_cp = (struct nc_rpc_copy *)rpc;
1363
1364 data = lyd_new(NULL, ietfnc, "copy-config");
1365 node = lyd_new(data, ietfnc, "target");
1366 if (rpc_cp->url_trg) {
1367 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1368 } else {
1369 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1370 }
1371 if (!node) {
1372 lyd_free(data);
1373 return NC_MSG_ERROR;
1374 }
1375
1376 node = lyd_new(data, ietfnc, "source");
1377 if (rpc_cp->url_config_src) {
1378 if (rpc_cp->url_config_src[0] == '<') {
1379 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1380 } else {
1381 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1382 }
1383 } else {
1384 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1385 }
1386 if (!node) {
1387 lyd_free(data);
1388 return NC_MSG_ERROR;
1389 }
1390
1391 if (rpc_cp->wd_mode) {
1392 if (!ietfncwd) {
1393 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1394 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001395 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001396 return NC_MSG_ERROR;
1397 }
1398 }
1399 switch (rpc_cp->wd_mode) {
1400 case NC_WD_UNKNOWN:
1401 /* cannot get here */
1402 break;
1403 case NC_WD_ALL:
1404 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1405 break;
1406 case NC_WD_ALL_TAG:
1407 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1408 break;
1409 case NC_WD_TRIM:
1410 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1411 break;
1412 case NC_WD_EXPLICIT:
1413 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1414 break;
1415 }
1416 if (!node) {
1417 lyd_free(data);
1418 return NC_MSG_ERROR;
1419 }
1420 }
1421 break;
1422
1423 case NC_RPC_DELETE:
1424 rpc_del = (struct nc_rpc_delete *)rpc;
1425
1426 data = lyd_new(NULL, ietfnc, "delete-config");
1427 node = lyd_new(data, ietfnc, "target");
1428 if (rpc_del->url) {
1429 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1430 } else {
1431 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1432 }
1433 if (!node) {
1434 lyd_free(data);
1435 return NC_MSG_ERROR;
1436 }
1437 break;
1438
1439 case NC_RPC_LOCK:
1440 rpc_lock = (struct nc_rpc_lock *)rpc;
1441
1442 data = lyd_new(NULL, ietfnc, "lock");
1443 node = lyd_new(data, ietfnc, "target");
1444 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1445 if (!node) {
1446 lyd_free(data);
1447 return NC_MSG_ERROR;
1448 }
1449 break;
1450
1451 case NC_RPC_UNLOCK:
1452 rpc_lock = (struct nc_rpc_lock *)rpc;
1453
1454 data = lyd_new(NULL, ietfnc, "unlock");
1455 node = lyd_new(data, ietfnc, "target");
1456 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1457 if (!node) {
1458 lyd_free(data);
1459 return NC_MSG_ERROR;
1460 }
1461 break;
1462
1463 case NC_RPC_GET:
1464 rpc_g = (struct nc_rpc_get *)rpc;
1465
1466 data = lyd_new(NULL, ietfnc, "get");
1467 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001468 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001469 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001470 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001471 } else {
1472 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001473 lyd_insert_attr(node, NULL, "type", "xpath");
1474 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001475 }
1476 if (!node) {
1477 lyd_free(data);
1478 return NC_MSG_ERROR;
1479 }
1480 }
1481
1482 if (rpc_g->wd_mode) {
1483 if (!ietfncwd) {
1484 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1485 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001486 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001487 return NC_MSG_ERROR;
1488 }
1489 }
1490 switch (rpc_g->wd_mode) {
1491 case NC_WD_UNKNOWN:
1492 /* cannot get here */
1493 break;
1494 case NC_WD_ALL:
1495 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1496 break;
1497 case NC_WD_ALL_TAG:
1498 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1499 break;
1500 case NC_WD_TRIM:
1501 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1502 break;
1503 case NC_WD_EXPLICIT:
1504 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1505 break;
1506 }
1507 if (!node) {
1508 lyd_free(data);
1509 return NC_MSG_ERROR;
1510 }
1511 }
1512 break;
1513
1514 case NC_RPC_KILL:
1515 rpc_k = (struct nc_rpc_kill *)rpc;
1516
1517 data = lyd_new(NULL, ietfnc, "kill-session");
1518 sprintf(str, "%u", rpc_k->sid);
1519 lyd_new_leaf(data, ietfnc, "session-id", str);
1520 break;
1521
1522 case NC_RPC_COMMIT:
1523 rpc_com = (struct nc_rpc_commit *)rpc;
1524
1525 data = lyd_new(NULL, ietfnc, "commit");
1526 if (rpc_com->confirmed) {
1527 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1528 }
1529
1530 if (rpc_com->confirm_timeout) {
1531 sprintf(str, "%u", rpc_com->confirm_timeout);
1532 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1533 }
1534
1535 if (rpc_com->persist) {
1536 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1537 if (!node) {
1538 lyd_free(data);
1539 return NC_MSG_ERROR;
1540 }
1541 }
1542
1543 if (rpc_com->persist_id) {
1544 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1545 if (!node) {
1546 lyd_free(data);
1547 return NC_MSG_ERROR;
1548 }
1549 }
1550 break;
1551
1552 case NC_RPC_DISCARD:
1553 data = lyd_new(NULL, ietfnc, "discard-changes");
1554 break;
1555
1556 case NC_RPC_CANCEL:
1557 rpc_can = (struct nc_rpc_cancel *)rpc;
1558
1559 data = lyd_new(NULL, ietfnc, "cancel-commit");
1560 if (rpc_can->persist_id) {
1561 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1562 if (!node) {
1563 lyd_free(data);
1564 return NC_MSG_ERROR;
1565 }
1566 }
1567 break;
1568
1569 case NC_RPC_VALIDATE:
1570 rpc_val = (struct nc_rpc_validate *)rpc;
1571
1572 data = lyd_new(NULL, ietfnc, "validate");
1573 node = lyd_new(data, ietfnc, "source");
1574 if (rpc_val->url_config_src) {
1575 if (rpc_val->url_config_src[0] == '<') {
1576 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1577 } else {
1578 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1579 }
1580 } else {
1581 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1582 }
1583 if (!node) {
1584 lyd_free(data);
1585 return NC_MSG_ERROR;
1586 }
1587 break;
1588
1589 case NC_RPC_GETSCHEMA:
1590 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1591 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001592 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001593 return NC_MSG_ERROR;
1594 }
1595
1596 rpc_gs = (struct nc_rpc_getschema *)rpc;
1597
1598 data = lyd_new(NULL, ietfncmon, "get-schema");
1599 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1600 if (!node) {
1601 lyd_free(data);
1602 return NC_MSG_ERROR;
1603 }
1604 if (rpc_gs->version) {
1605 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1606 if (!node) {
1607 lyd_free(data);
1608 return NC_MSG_ERROR;
1609 }
1610 }
1611 if (rpc_gs->format) {
1612 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1613 if (!node) {
1614 lyd_free(data);
1615 return NC_MSG_ERROR;
1616 }
1617 }
1618 break;
1619
1620 case NC_RPC_SUBSCRIBE:
1621 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1622 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001623 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001624 return NC_MSG_ERROR;
1625 }
1626
1627 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1628
1629 data = lyd_new(NULL, notifs, "create-subscription");
1630 if (rpc_sub->stream) {
1631 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1632 if (!node) {
1633 lyd_free(data);
1634 return NC_MSG_ERROR;
1635 }
1636 }
1637
1638 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001639 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001640 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001641 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001642 } else {
1643 node = lyd_new_anyxml(data, notifs, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001644 lyd_insert_attr(node, NULL, "type", "xpath");
1645 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001646 }
1647 if (!node) {
1648 lyd_free(data);
1649 return NC_MSG_ERROR;
1650 }
1651 }
1652
1653 if (rpc_sub->start) {
1654 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1655 if (!node) {
1656 lyd_free(data);
1657 return NC_MSG_ERROR;
1658 }
1659 }
1660
1661 if (rpc_sub->stop) {
1662 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1663 if (!node) {
1664 lyd_free(data);
1665 return NC_MSG_ERROR;
1666 }
1667 }
1668 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001669 default:
1670 ERRINT;
1671 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001672 }
1673
Michal Vasko303245c2016-03-24 15:20:03 +01001674 if (lyd_validate(&data, LYD_OPT_STRICT)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001675 lyd_free(data);
1676 return NC_MSG_ERROR;
1677 }
1678
Michal Vasko62be1ce2016-03-03 13:24:52 +01001679 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001680 if (ret == -1) {
1681 /* error */
1682 r = NC_MSG_ERROR;
1683 } else if (!ret) {
1684 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001685 r = NC_MSG_WOULDBLOCK;
1686 } else {
1687 /* send RPC, store its message ID */
1688 r = nc_send_msg(session, data);
1689 cur_msgid = session->msgid;
1690 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001691 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001692
1693 lyd_free(data);
1694
1695 if (r != NC_MSG_RPC) {
1696 return r;
1697 }
1698
1699 *msgid = cur_msgid;
1700 return NC_MSG_RPC;
1701}