blob: 68c81964a55b1b995a3163a769725fa970aff728 [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 Vaskod91f6e62016-04-05 11:34:22 +0200202 char *model_data = 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 Vasko36b17ba2016-05-03 11:54:21 +0200218 msg = nc_recv_reply(session, rpc, msgid, 1000, 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 Vaskod91f6e62016-04-05 11:34:22 +0200236 if (((struct lyd_node_anyxml *)data_rpl->data)->xml_struct) {
237 lyxml_print_mem(&model_data, ((struct lyd_node_anyxml *)data_rpl->data)->value.xml, LYXML_PRINT_SIBLINGS);
238 } else {
239 model_data = strdup(((struct lyd_node_anyxml *)data_rpl->data)->value.str);
240 }
Michal Vasko086311b2016-01-08 09:53:11 +0100241 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100242 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100243
Michal Vasko086311b2016-01-08 09:53:11 +0100244 return model_data;
245}
246
Michal Vaskoef578332016-01-25 13:20:09 +0100247/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100248int
249nc_ctx_check_and_fill(struct nc_session *session)
250{
Michal Vaskoef578332016-01-25 13:20:09 +0100251 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100252 ly_module_clb old_clb = NULL;
253 void *old_data = NULL;
254
255 assert(session->cpblts && session->ctx);
256
257 /* check if get-schema is supported */
258 for (i = 0; session->cpblts[i]; ++i) {
259 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
260 get_schema_support = 1;
261 break;
262 }
263 }
264
265 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
266 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
267 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
268 /* set module retrieval using <get-schema> */
269 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100270 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100271 } else {
272 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
273 }
274 }
275
276 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100277 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100278 if (old_clb) {
279 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
280 }
Michal Vaskoef578332016-01-25 13:20:09 +0100281 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100282 }
283
284 /* load all other models */
285 for (i = 0; session->cpblts[i]; ++i) {
286 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
287 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
288 continue;
289 }
290
Michal Vaskoef578332016-01-25 13:20:09 +0100291 r = ctx_check_and_load_model(session, session->cpblts[i]);
292 if (r == -1) {
293 ret = -1;
294 break;
295 }
296
297 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
298 * if it was using get-schema */
299 if (r == 1) {
300 if (get_schema_support) {
301 VRB("Trying to load the schema from a different source.");
302 /* works even if old_clb is NULL */
303 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
304 r = ctx_check_and_load_model(session, session->cpblts[i]);
305 }
306
307 /* fail again (or no other way to try), too bad */
308 if (r) {
309 ret = 1;
310 }
311
312 /* set get-schema callback back */
313 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
314 }
Michal Vasko086311b2016-01-08 09:53:11 +0100315 }
316
317 if (old_clb) {
318 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
319 }
Michal Vaskoef578332016-01-25 13:20:09 +0100320 if (ret == 1) {
321 WRN("Some models failed to be loaded, any data from these models will be ignored.");
322 }
323 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100324}
325
326API struct nc_session *
327nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
328{
Michal Vaskod083db62016-01-19 10:31:29 +0100329 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100330
Michal Vasko45e53ae2016-04-07 11:46:03 +0200331 if (fdin < 0) {
332 ERRARG("fdin");
333 return NULL;
334 } else if (fdout < 0) {
335 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100336 return NULL;
337 }
338
339 /* prepare session structure */
340 session = calloc(1, sizeof *session);
341 if (!session) {
342 ERRMEM;
343 return NULL;
344 }
345 session->status = NC_STATUS_STARTING;
346 session->side = NC_CLIENT;
347
348 /* transport specific data */
349 session->ti_type = NC_TI_FD;
350 session->ti.fd.in = fdin;
351 session->ti.fd.out = fdout;
352
353 /* assign context (dicionary needed for handshake) */
354 if (!ctx) {
355 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100356 /* definitely should not happen, but be ready */
357 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
358 /* that's just it */
359 goto fail;
360 }
Michal Vasko086311b2016-01-08 09:53:11 +0100361 } else {
362 session->flags |= NC_SESSION_SHAREDCTX;
363 }
364 session->ctx = ctx;
365
366 /* NETCONF handshake */
367 if (nc_handshake(session)) {
368 goto fail;
369 }
370 session->status = NC_STATUS_RUNNING;
371
Michal Vaskoef578332016-01-25 13:20:09 +0100372 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100373 goto fail;
374 }
375
376 return session;
377
378fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100379 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100380 return NULL;
381}
382
383int
Michal Vaskof05562c2016-01-20 12:06:43 +0100384nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100385{
Michal Vasko0190bc32016-03-02 15:47:49 +0100386 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100387 struct addrinfo hints, *res_list, *res;
388 char port_s[6]; /* length of string representation of short int */
389
390 snprintf(port_s, 6, "%u", port);
391
392 /* Connect to a server */
393 memset(&hints, 0, sizeof hints);
394 hints.ai_family = AF_UNSPEC;
395 hints.ai_socktype = SOCK_STREAM;
396 hints.ai_protocol = IPPROTO_TCP;
397 i = getaddrinfo(host, port_s, &hints, &res_list);
398 if (i != 0) {
399 ERR("Unable to translate the host address (%s).", gai_strerror(i));
400 return -1;
401 }
402
403 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
404 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
405 if (sock == -1) {
406 /* socket was not created, try another resource */
407 i = errno;
408 goto errloop;
409 }
410
411 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
412 /* network connection failed, try another resource */
413 i = errno;
414 close(sock);
415 sock = -1;
416 goto errloop;
417 }
418
Michal Vasko0190bc32016-03-02 15:47:49 +0100419 /* make the socket non-blocking */
420 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
421 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100422 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100423 return -1;
424 }
425
Michal Vasko086311b2016-01-08 09:53:11 +0100426 /* we're done, network connection established */
427 break;
428errloop:
429 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
430 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
431 continue;
432 }
433
434 if (sock == -1) {
435 ERR("Unable to connect to %s:%s.", host, port_s);
436 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100437 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 +0100438 }
439 freeaddrinfo(res_list);
440
441 return sock;
442}
443
Michal Vasko086311b2016-01-08 09:53:11 +0100444static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100445get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100446{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100447 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100448 char *ptr;
449 const char *str_msgid;
450 uint64_t cur_msgid;
451 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100452 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100453 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
454
Michal Vasko62be1ce2016-03-03 13:24:52 +0100455 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100456 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100457 /* error */
458 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100459 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100460 /* timeout */
461 return NC_MSG_WOULDBLOCK;
462 }
463
464 /* try to get notification from the session's queue */
465 if (!msgid && session->notifs) {
466 cont = session->notifs;
467 session->notifs = cont->next;
468
Michal Vasko71ba2da2016-05-04 10:53:16 +0200469 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100470 free(cont);
471
Michal Vasko71ba2da2016-05-04 10:53:16 +0200472 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100473 }
474
475 /* try to get rpc-reply from the session's queue */
476 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200477 cont = session->replies;
478 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100479
Michal Vasko71ba2da2016-05-04 10:53:16 +0200480 xml = cont->msg;
481 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100482
Michal Vasko71ba2da2016-05-04 10:53:16 +0200483 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100484 }
485
Michal Vasko71ba2da2016-05-04 10:53:16 +0200486 if (!msgtype) {
487 /* read message from wire */
488 msgtype = nc_read_msg_poll(session, timeout, &xml);
489 }
Michal Vasko086311b2016-01-08 09:53:11 +0100490
491 /* we read rpc-reply, want a notif */
492 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100493 cont_ptr = &session->replies;
494 while (*cont_ptr) {
495 cont_ptr = &((*cont_ptr)->next);
496 }
497 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100498 if (!*cont_ptr) {
499 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200500 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100501 lyxml_free(session->ctx, xml);
502 return NC_MSG_ERROR;
503 }
Michal Vasko086311b2016-01-08 09:53:11 +0100504 (*cont_ptr)->msg = xml;
505 (*cont_ptr)->next = NULL;
506 }
507
508 /* we read notif, want a rpc-reply */
509 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100510 /* TODO check whether the session is even subscribed */
511 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100512 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100513 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100514 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100515 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100516 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100517
518 cont_ptr = &session->notifs;
519 while (*cont_ptr) {
520 cont_ptr = &((*cont_ptr)->next);
521 }
522 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100523 if (!cont_ptr) {
524 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200525 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100526 lyxml_free(session->ctx, xml);
527 return NC_MSG_ERROR;
528 }
Michal Vasko086311b2016-01-08 09:53:11 +0100529 (*cont_ptr)->msg = xml;
530 (*cont_ptr)->next = NULL;
531 }
532
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100533 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100534
535 switch (msgtype) {
536 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100537 if (!msgid) {
538 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100539 }
Michal Vasko086311b2016-01-08 09:53:11 +0100540 break;
541
542 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100543 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200544 /* check message-id */
545 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
546 if (!str_msgid) {
547 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
548 msgtype = NC_MSG_REPLY_ERR_MSGID;
549 } else {
550 cur_msgid = strtoul(str_msgid, &ptr, 10);
551 if (cur_msgid != msgid) {
552 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
553 session->id, str_msgid);
554 msgtype = NC_MSG_REPLY_ERR_MSGID;
555 }
556 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100557 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100558 }
Michal Vasko086311b2016-01-08 09:53:11 +0100559 break;
560
561 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100562 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200564 msgtype = NC_MSG_ERROR;
565 break;
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 Vasko71ba2da2016-05-04 10:53:16 +0200570 msgtype = NC_MSG_ERROR;
571 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100572
573 default:
574 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
575 * NC_MSG_NONE is not returned by nc_read_msg()
576 */
577 break;
578 }
579
580 return msgtype;
581}
582
583/* cannot strictly fail, but does not need to fill any error parameter at all */
584static void
585parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
586{
587 struct lyxml_elem *iter, *next, *info;
588
589 LY_TREE_FOR(xml->child, iter) {
590 if (!iter->ns) {
591 if (iter->content) {
592 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
593 } else {
594 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
595 }
596 continue;
597 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
598 if (iter->content) {
599 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
600 iter->name, iter->content, iter->ns->value);
601 } else {
602 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
603 }
604 continue;
605 }
606
607 if (!strcmp(iter->name, "error-type")) {
608 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
609 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
610 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
611 } else if (err->type) {
612 WRN("<rpc-error> <error-type> duplicated.");
613 } else {
614 err->type = lydict_insert(ctx, iter->content, 0);
615 }
616 } else if (!strcmp(iter->name, "error-tag")) {
617 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
618 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
619 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
620 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
621 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
622 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
623 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
624 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
625 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
626 && strcmp(iter->content, "malformed-message"))) {
627 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
628 } else if (err->tag) {
629 WRN("<rpc-error> <error-tag> duplicated.");
630 } else {
631 err->tag = lydict_insert(ctx, iter->content, 0);
632 }
633 } else if (!strcmp(iter->name, "error-severity")) {
634 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
635 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
636 } else if (err->severity) {
637 WRN("<rpc-error> <error-severity> duplicated.");
638 } else {
639 err->severity = lydict_insert(ctx, iter->content, 0);
640 }
641 } else if (!strcmp(iter->name, "error-app-tag")) {
642 if (err->apptag) {
643 WRN("<rpc-error> <error-app-tag> duplicated.");
644 } else {
645 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
646 }
647 } else if (!strcmp(iter->name, "error-path")) {
648 if (err->path) {
649 WRN("<rpc-error> <error-path> duplicated.");
650 } else {
651 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
652 }
653 } else if (!strcmp(iter->name, "error-message")) {
654 if (err->message) {
655 WRN("<rpc-error> <error-message> duplicated.");
656 } else {
657 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
658 if (!err->message_lang) {
659 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
660 }
661 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
662 }
663 } else if (!strcmp(iter->name, "error-info")) {
664 LY_TREE_FOR_SAFE(iter->child, next, info) {
665 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
666 if (!strcmp(info->name, "session-id")) {
667 if (err->sid) {
668 WRN("<rpc-error> <error-info> <session-id> duplicated.");
669 } else {
670 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
671 }
672 } else if (!strcmp(info->name, "bad-attr")) {
673 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100674 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
675 if (!err->attr) {
676 ERRMEM;
677 return;
678 }
Michal Vasko086311b2016-01-08 09:53:11 +0100679 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
680 } else if (!strcmp(info->name, "bad-element")) {
681 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100682 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
683 if (!err->elem) {
684 ERRMEM;
685 return;
686 }
Michal Vasko086311b2016-01-08 09:53:11 +0100687 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
688 } else if (!strcmp(info->name, "bad-namespace")) {
689 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100690 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
691 if (!err->ns) {
692 ERRMEM;
693 return;
694 }
Michal Vasko086311b2016-01-08 09:53:11 +0100695 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
696 } else {
697 if (info->content) {
698 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
699 info->name, info->content);
700 } else {
701 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
702 }
703 }
704 } else {
705 lyxml_unlink(ctx, info);
706 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100707 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
708 if (!err->other) {
709 ERRMEM;
710 return;
711 }
Michal Vasko086311b2016-01-08 09:53:11 +0100712 err->other[err->other_count - 1] = info;
713 }
714 }
715 } else {
716 if (iter->content) {
717 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
718 } else {
719 WRN("<rpc-error> unknown child \"%s\".", iter->name);
720 }
721 }
722 }
723}
724
725static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100726parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100727{
728 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100729 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100730 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100731 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100732 struct nc_reply_data *data_rpl;
733 struct nc_reply *reply = NULL;
734 struct nc_rpc_generic *rpc_gen;
735 int i;
736
737 if (!xml->child) {
738 ERR("An empty <rpc-reply>.");
739 return NULL;
740 }
741
742 /* rpc-error */
743 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
744 /* count and check elements */
745 i = 0;
746 LY_TREE_FOR(xml->child, iter) {
747 if (strcmp(iter->name, "rpc-error")) {
748 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
749 return NULL;
750 } else if (!iter->ns) {
751 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
752 return NULL;
753 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
754 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
755 return NULL;
756 }
757 ++i;
758 }
759
760 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100761 if (!error_rpl) {
762 ERRMEM;
763 return NULL;
764 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100765 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100766 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100767 if (!error_rpl->err) {
768 ERRMEM;
769 free(error_rpl);
770 return NULL;
771 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100772 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100773 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100774 reply = (struct nc_reply *)error_rpl;
775
776 i = 0;
777 LY_TREE_FOR(xml->child, iter) {
778 parse_rpc_error(ctx, iter, error_rpl->err + i);
779 ++i;
780 }
781
782 /* ok */
783 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
784 if (xml->child->next) {
785 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
786 return NULL;
787 }
788 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100789 if (!reply) {
790 ERRMEM;
791 return NULL;
792 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100793 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100794
795 /* some RPC output */
796 } else {
797 switch (rpc->type) {
798 case NC_RPC_GENERIC:
799 rpc_gen = (struct nc_rpc_generic *)rpc;
800
801 if (rpc_gen->has_data) {
802 schema = rpc_gen->content.data->schema;
803 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100804 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100805 if (!data) {
806 ERR("Failed to parse a generic RPC XML.");
807 return NULL;
808 }
809 schema = data->schema;
810 lyd_free(data);
811 data = NULL;
812 }
813 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100814 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100815 return NULL;
816 }
817 break;
818
819 case NC_RPC_GETCONFIG:
820 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100821 if (!xml->child->child) {
822 /* we did not receive any data */
823 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100824 if (!data_rpl) {
825 ERRMEM;
826 return NULL;
827 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100828 data_rpl->type = NC_RPL_DATA;
829 data_rpl->data = NULL;
830 return (struct nc_reply *)data_rpl;
831 }
832
Michal Vasko086311b2016-01-08 09:53:11 +0100833 /* special treatment */
834 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100835 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100836 if (!data) {
837 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
838 return NULL;
839 }
840 break;
841
842 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100843 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100844 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100845 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100846 return NULL;
847 }
848 break;
849
850 case NC_RPC_EDIT:
851 case NC_RPC_COPY:
852 case NC_RPC_DELETE:
853 case NC_RPC_LOCK:
854 case NC_RPC_UNLOCK:
855 case NC_RPC_KILL:
856 case NC_RPC_COMMIT:
857 case NC_RPC_DISCARD:
858 case NC_RPC_CANCEL:
859 case NC_RPC_VALIDATE:
860 case NC_RPC_SUBSCRIBE:
861 /* there is no output defined */
862 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
863 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100864 default:
865 ERRINT;
866 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100867 }
868
869 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100870 if (!data_rpl) {
871 ERRMEM;
872 return NULL;
873 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100874 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100875 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100876 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100877 } else {
878 /* <get>, <get-config> */
879 data_rpl->data = data;
880 }
881 if (!data_rpl->data) {
882 ERR("Failed to parse <rpc-reply>.");
883 free(data_rpl);
884 return NULL;
885 }
886 reply = (struct nc_reply *)data_rpl;
887 }
888
889 return reply;
890}
891
Radek Krejci53691be2016-02-22 13:58:37 +0100892#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100893
Michal Vasko3031aae2016-01-27 16:07:18 +0100894int
895nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
896{
897 int sock;
898
Michal Vasko45e53ae2016-04-07 11:46:03 +0200899 if (!address) {
900 ERRARG("address");
901 return -1;
902 } else if (!port) {
903 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100904 return -1;
905 }
906
907 sock = nc_sock_listen(address, port);
908 if (sock == -1) {
909 return -1;
910 }
911
912 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100913 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
914 if (!client_opts.ch_binds) {
915 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100916 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100917 return -1;
918 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100919
920 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100921 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
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 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
927 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
928 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
929
930 return 0;
931}
932
933int
934nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
935{
936 uint32_t i;
937 int ret = -1;
938
939 if (!address && !port && !ti) {
940 for (i = 0; i < client_opts.ch_bind_count; ++i) {
941 close(client_opts.ch_binds[i].sock);
942 free((char *)client_opts.ch_binds[i].address);
943
944 ret = 0;
945 }
946 free(client_opts.ch_binds);
947 client_opts.ch_binds = NULL;
948 client_opts.ch_bind_count = 0;
949 } else {
950 for (i = 0; i < client_opts.ch_bind_count; ++i) {
951 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
952 && (!port || (client_opts.ch_binds[i].port == port))
953 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
954 close(client_opts.ch_binds[i].sock);
955 free((char *)client_opts.ch_binds[i].address);
956
957 --client_opts.ch_bind_count;
958 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
959
960 ret = 0;
961 }
962 }
963 }
964
965 return ret;
966}
967
968API int
969nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
970{
971 int sock;
972 char *host = NULL;
973 uint16_t port, idx;
974
Michal Vasko45e53ae2016-04-07 11:46:03 +0200975 if (!client_opts.ch_binds) {
976 ERRINIT;
977 return -1;
978 } else if (!session) {
979 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +0100980 return -1;
981 }
982
983 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
984
Michal Vasko50456e82016-02-02 12:16:08 +0100985 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100986 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100987 return sock;
988 }
989
Radek Krejci53691be2016-02-22 13:58:37 +0100990#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100991 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100992 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100993 } else
994#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100995#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +0100996 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100997 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100998 } else
999#endif
1000 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001001 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001002 *session = NULL;
1003 }
1004
1005 free(host);
1006
1007 if (!(*session)) {
1008 return -1;
1009 }
1010
1011 return 1;
1012}
1013
Radek Krejci53691be2016-02-22 13:58:37 +01001014#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001015
Michal Vaskob7558c52016-02-26 15:04:19 +01001016API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001017nc_client_init(void)
1018{
1019 nc_init();
1020}
1021
1022API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001023nc_client_destroy(void)
1024{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001025 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001026#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1027 nc_client_ch_del_bind(NULL, 0, 0);
1028#endif
1029#ifdef NC_ENABLED_SSH
1030 nc_client_ssh_destroy_opts();
1031#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001032#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001033 nc_client_tls_destroy_opts();
1034#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001035 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001036}
1037
Michal Vasko086311b2016-01-08 09:53:11 +01001038API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001039nc_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 +01001040{
1041 struct lyxml_elem *xml;
1042 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1043
Michal Vasko45e53ae2016-04-07 11:46:03 +02001044 if (!session) {
1045 ERRARG("session");
1046 return NC_MSG_ERROR;
1047 } else if (!rpc) {
1048 ERRARG("rpc");
1049 return NC_MSG_ERROR;
1050 } else if (!reply) {
1051 ERRARG("reply");
1052 return NC_MSG_ERROR;
1053 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1054 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001055 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001056 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001057 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001058 return NC_MSG_ERROR;
1059 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001060 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001061 *reply = NULL;
1062
1063 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001064
Michal Vasko71ba2da2016-05-04 10:53:16 +02001065 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001066 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001067 lyxml_free(session->ctx, xml);
1068 if (!(*reply)) {
1069 return NC_MSG_ERROR;
1070 }
1071 }
1072
1073 return msgtype;
1074}
1075
1076API NC_MSG_TYPE
1077nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1078{
1079 struct lyxml_elem *xml, *ev_time;
1080 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1081
Michal Vasko45e53ae2016-04-07 11:46:03 +02001082 if (!session) {
1083 ERRARG("session");
1084 return NC_MSG_ERROR;
1085 } else if (!notif) {
1086 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001087 return NC_MSG_ERROR;
1088 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001089 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001090 return NC_MSG_ERROR;
1091 }
1092
1093 msgtype = get_msg(session, timeout, 0, &xml);
1094
1095 if (msgtype == NC_MSG_NOTIF) {
1096 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001097 if (!*notif) {
1098 ERRMEM;
1099 lyxml_free(session->ctx, xml);
1100 return NC_MSG_ERROR;
1101 }
Michal Vasko086311b2016-01-08 09:53:11 +01001102
1103 /* eventTime */
1104 LY_TREE_FOR(xml->child, ev_time) {
1105 if (!strcmp(ev_time->name, "eventTime")) {
1106 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1107 /* lyd_parse does not know this element */
1108 lyxml_free(session->ctx, ev_time);
1109 break;
1110 }
1111 }
1112 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001113 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001114 goto fail;
1115 }
1116
1117 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001118 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001119 lyxml_free(session->ctx, xml);
1120 xml = NULL;
1121 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001122 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001123 goto fail;
1124 }
1125 }
1126
1127 return msgtype;
1128
1129fail:
1130 lydict_remove(session->ctx, (*notif)->datetime);
1131 lyd_free((*notif)->tree);
1132 free(*notif);
1133 *notif = NULL;
1134 lyxml_free(session->ctx, xml);
1135
1136 return NC_MSG_ERROR;
1137}
1138
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001139static void *
1140nc_recv_notif_thread(void *arg)
1141{
1142 struct nc_ntf_thread_arg *ntarg;
1143 struct nc_session *session;
1144 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1145 struct nc_notif *notif;
1146 NC_MSG_TYPE msgtype;
1147
1148 ntarg = (struct nc_ntf_thread_arg *)arg;
1149 session = ntarg->session;
1150 notif_clb = ntarg->notif_clb;
1151 free(ntarg);
1152
1153 while (session->ntf_tid) {
1154 msgtype = nc_recv_notif(session, 0, &notif);
1155 if (msgtype == NC_MSG_NOTIF) {
1156 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001157 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1158 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1159 nc_notif_free(notif);
1160 break;
1161 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001162 nc_notif_free(notif);
1163 }
1164
1165 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1166 }
1167
1168 return NULL;
1169}
1170
1171API int
1172nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1173{
1174 struct nc_ntf_thread_arg *ntarg;
1175 int ret;
1176
Michal Vasko45e53ae2016-04-07 11:46:03 +02001177 if (!session) {
1178 ERRARG("session");
1179 return -1;
1180 } else if (!notif_clb) {
1181 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001182 return -1;
1183 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1184 ERR("Session %u: invalid session to receive Notifications.", session->id);
1185 return -1;
1186 } else if (session->ntf_tid) {
1187 ERR("Session %u: separate notification thread is already running.", session->id);
1188 return -1;
1189 }
1190
1191 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001192 if (!ntarg) {
1193 ERRMEM;
1194 return -1;
1195 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001196 ntarg->session = session;
1197 ntarg->notif_clb = notif_clb;
1198
1199 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1200 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001201 if (!session->ntf_tid) {
1202 ERRMEM;
1203 free(ntarg);
1204 return -1;
1205 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001206
1207 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1208 if (ret) {
1209 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1210 free(ntarg);
1211 free((pthread_t *)session->ntf_tid);
1212 session->ntf_tid = NULL;
1213 return -1;
1214 }
1215
1216 return 0;
1217}
1218
Michal Vasko086311b2016-01-08 09:53:11 +01001219API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001220nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001221{
1222 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001223 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001224 struct nc_rpc_generic *rpc_gen;
1225 struct nc_rpc_getconfig *rpc_gc;
1226 struct nc_rpc_edit *rpc_e;
1227 struct nc_rpc_copy *rpc_cp;
1228 struct nc_rpc_delete *rpc_del;
1229 struct nc_rpc_lock *rpc_lock;
1230 struct nc_rpc_get *rpc_g;
1231 struct nc_rpc_kill *rpc_k;
1232 struct nc_rpc_commit *rpc_com;
1233 struct nc_rpc_cancel *rpc_can;
1234 struct nc_rpc_validate *rpc_val;
1235 struct nc_rpc_getschema *rpc_gs;
1236 struct nc_rpc_subscribe *rpc_sub;
1237 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001238 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vaskod91f6e62016-04-05 11:34:22 +02001239 char str[11], *filter;
Michal Vasko086311b2016-01-08 09:53:11 +01001240 uint64_t cur_msgid;
1241
Michal Vasko45e53ae2016-04-07 11:46:03 +02001242 if (!session) {
1243 ERRARG("session");
1244 return NC_MSG_ERROR;
1245 } else if (!rpc) {
1246 ERRARG("rpc");
1247 return NC_MSG_ERROR;
1248 } else if (!msgid) {
1249 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001250 return NC_MSG_ERROR;
1251 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001252 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001253 return NC_MSG_ERROR;
1254 }
1255
1256 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1257 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1258 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001259 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001260 return NC_MSG_ERROR;
1261 }
1262 }
1263
1264 switch (rpc->type) {
1265 case NC_RPC_GENERIC:
1266 rpc_gen = (struct nc_rpc_generic *)rpc;
1267
1268 if (rpc_gen->has_data) {
1269 data = rpc_gen->content.data;
1270 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001271 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001272 }
1273 break;
1274
1275 case NC_RPC_GETCONFIG:
1276 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1277
1278 data = lyd_new(NULL, ietfnc, "get-config");
1279 node = lyd_new(data, ietfnc, "source");
1280 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1281 if (!node) {
1282 lyd_free(data);
1283 return NC_MSG_ERROR;
1284 }
1285 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001286 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001287 /* we need a copy of the filter */
1288 filter = strdup(rpc_gc->filter);
1289 if (!filter) {
1290 ERRMEM;
1291 lyd_free(data);
1292 return NC_MSG_ERROR;
1293 }
1294
1295 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001296 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001297 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001298 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001299 lyd_insert_attr(node, NULL, "type", "xpath");
1300 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001301 }
1302 if (!node) {
1303 lyd_free(data);
1304 return NC_MSG_ERROR;
1305 }
1306 }
1307
1308 if (rpc_gc->wd_mode) {
1309 if (!ietfncwd) {
1310 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1311 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001312 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001313 return NC_MSG_ERROR;
1314 }
1315 }
1316 switch (rpc_gc->wd_mode) {
1317 case NC_WD_UNKNOWN:
1318 /* cannot get here */
1319 break;
1320 case NC_WD_ALL:
1321 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1322 break;
1323 case NC_WD_ALL_TAG:
1324 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1325 break;
1326 case NC_WD_TRIM:
1327 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1328 break;
1329 case NC_WD_EXPLICIT:
1330 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1331 break;
1332 }
1333 if (!node) {
1334 lyd_free(data);
1335 return NC_MSG_ERROR;
1336 }
1337 }
1338 break;
1339
1340 case NC_RPC_EDIT:
1341 rpc_e = (struct nc_rpc_edit *)rpc;
1342
1343 data = lyd_new(NULL, ietfnc, "edit-config");
1344 node = lyd_new(data, ietfnc, "target");
1345 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1346 if (!node) {
1347 lyd_free(data);
1348 return NC_MSG_ERROR;
1349 }
1350
1351 if (rpc_e->default_op) {
1352 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1353 if (!node) {
1354 lyd_free(data);
1355 return NC_MSG_ERROR;
1356 }
1357 }
1358
1359 if (rpc_e->test_opt) {
1360 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1361 if (!node) {
1362 lyd_free(data);
1363 return NC_MSG_ERROR;
1364 }
1365 }
1366
1367 if (rpc_e->error_opt) {
1368 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1369 if (!node) {
1370 lyd_free(data);
1371 return NC_MSG_ERROR;
1372 }
1373 }
1374
1375 if (rpc_e->edit_cont[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001376 /* we need a copy of the content */
1377 filter = strdup(rpc_e->edit_cont);
1378 if (!filter) {
1379 ERRMEM;
1380 lyd_free(data);
1381 return NC_MSG_ERROR;
1382 }
1383
1384 node = lyd_new_anyxml_str(data, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001385 } else {
1386 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1387 }
1388 if (!node) {
1389 lyd_free(data);
1390 return NC_MSG_ERROR;
1391 }
1392 break;
1393
1394 case NC_RPC_COPY:
1395 rpc_cp = (struct nc_rpc_copy *)rpc;
1396
1397 data = lyd_new(NULL, ietfnc, "copy-config");
1398 node = lyd_new(data, ietfnc, "target");
1399 if (rpc_cp->url_trg) {
1400 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1401 } else {
1402 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1403 }
1404 if (!node) {
1405 lyd_free(data);
1406 return NC_MSG_ERROR;
1407 }
1408
1409 node = lyd_new(data, ietfnc, "source");
1410 if (rpc_cp->url_config_src) {
1411 if (rpc_cp->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001412 /* we need a copy of the content */
1413 filter = strdup(rpc_cp->url_config_src);
1414 if (!filter) {
1415 ERRMEM;
1416 lyd_free(data);
1417 return NC_MSG_ERROR;
1418 }
1419
1420 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001421 } else {
1422 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1423 }
1424 } else {
1425 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1426 }
1427 if (!node) {
1428 lyd_free(data);
1429 return NC_MSG_ERROR;
1430 }
1431
1432 if (rpc_cp->wd_mode) {
1433 if (!ietfncwd) {
1434 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1435 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001436 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001437 return NC_MSG_ERROR;
1438 }
1439 }
1440 switch (rpc_cp->wd_mode) {
1441 case NC_WD_UNKNOWN:
1442 /* cannot get here */
1443 break;
1444 case NC_WD_ALL:
1445 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1446 break;
1447 case NC_WD_ALL_TAG:
1448 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1449 break;
1450 case NC_WD_TRIM:
1451 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1452 break;
1453 case NC_WD_EXPLICIT:
1454 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1455 break;
1456 }
1457 if (!node) {
1458 lyd_free(data);
1459 return NC_MSG_ERROR;
1460 }
1461 }
1462 break;
1463
1464 case NC_RPC_DELETE:
1465 rpc_del = (struct nc_rpc_delete *)rpc;
1466
1467 data = lyd_new(NULL, ietfnc, "delete-config");
1468 node = lyd_new(data, ietfnc, "target");
1469 if (rpc_del->url) {
1470 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1471 } else {
1472 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1473 }
1474 if (!node) {
1475 lyd_free(data);
1476 return NC_MSG_ERROR;
1477 }
1478 break;
1479
1480 case NC_RPC_LOCK:
1481 rpc_lock = (struct nc_rpc_lock *)rpc;
1482
1483 data = lyd_new(NULL, ietfnc, "lock");
1484 node = lyd_new(data, ietfnc, "target");
1485 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1486 if (!node) {
1487 lyd_free(data);
1488 return NC_MSG_ERROR;
1489 }
1490 break;
1491
1492 case NC_RPC_UNLOCK:
1493 rpc_lock = (struct nc_rpc_lock *)rpc;
1494
1495 data = lyd_new(NULL, ietfnc, "unlock");
1496 node = lyd_new(data, ietfnc, "target");
1497 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1498 if (!node) {
1499 lyd_free(data);
1500 return NC_MSG_ERROR;
1501 }
1502 break;
1503
1504 case NC_RPC_GET:
1505 rpc_g = (struct nc_rpc_get *)rpc;
1506
1507 data = lyd_new(NULL, ietfnc, "get");
1508 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001509 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001510 /* we need a copy of the filter */
1511 filter = strdup(rpc_g->filter);
1512 if (!filter) {
1513 ERRMEM;
1514 lyd_free(data);
1515 return NC_MSG_ERROR;
1516 }
1517
1518 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001519 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001520 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001521 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001522 lyd_insert_attr(node, NULL, "type", "xpath");
1523 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001524 }
1525 if (!node) {
1526 lyd_free(data);
1527 return NC_MSG_ERROR;
1528 }
1529 }
1530
1531 if (rpc_g->wd_mode) {
1532 if (!ietfncwd) {
1533 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1534 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001535 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001536 return NC_MSG_ERROR;
1537 }
1538 }
1539 switch (rpc_g->wd_mode) {
1540 case NC_WD_UNKNOWN:
1541 /* cannot get here */
1542 break;
1543 case NC_WD_ALL:
1544 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1545 break;
1546 case NC_WD_ALL_TAG:
1547 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1548 break;
1549 case NC_WD_TRIM:
1550 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1551 break;
1552 case NC_WD_EXPLICIT:
1553 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1554 break;
1555 }
1556 if (!node) {
1557 lyd_free(data);
1558 return NC_MSG_ERROR;
1559 }
1560 }
1561 break;
1562
1563 case NC_RPC_KILL:
1564 rpc_k = (struct nc_rpc_kill *)rpc;
1565
1566 data = lyd_new(NULL, ietfnc, "kill-session");
1567 sprintf(str, "%u", rpc_k->sid);
1568 lyd_new_leaf(data, ietfnc, "session-id", str);
1569 break;
1570
1571 case NC_RPC_COMMIT:
1572 rpc_com = (struct nc_rpc_commit *)rpc;
1573
1574 data = lyd_new(NULL, ietfnc, "commit");
1575 if (rpc_com->confirmed) {
1576 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1577 }
1578
1579 if (rpc_com->confirm_timeout) {
1580 sprintf(str, "%u", rpc_com->confirm_timeout);
1581 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1582 }
1583
1584 if (rpc_com->persist) {
1585 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1586 if (!node) {
1587 lyd_free(data);
1588 return NC_MSG_ERROR;
1589 }
1590 }
1591
1592 if (rpc_com->persist_id) {
1593 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1594 if (!node) {
1595 lyd_free(data);
1596 return NC_MSG_ERROR;
1597 }
1598 }
1599 break;
1600
1601 case NC_RPC_DISCARD:
1602 data = lyd_new(NULL, ietfnc, "discard-changes");
1603 break;
1604
1605 case NC_RPC_CANCEL:
1606 rpc_can = (struct nc_rpc_cancel *)rpc;
1607
1608 data = lyd_new(NULL, ietfnc, "cancel-commit");
1609 if (rpc_can->persist_id) {
1610 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1611 if (!node) {
1612 lyd_free(data);
1613 return NC_MSG_ERROR;
1614 }
1615 }
1616 break;
1617
1618 case NC_RPC_VALIDATE:
1619 rpc_val = (struct nc_rpc_validate *)rpc;
1620
1621 data = lyd_new(NULL, ietfnc, "validate");
1622 node = lyd_new(data, ietfnc, "source");
1623 if (rpc_val->url_config_src) {
1624 if (rpc_val->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001625 /* we need a copy of the config */
1626 filter = strdup(rpc_val->url_config_src);
1627 if (!filter) {
1628 ERRMEM;
1629 lyd_free(data);
1630 return NC_MSG_ERROR;
1631 }
1632
1633 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001634 } else {
1635 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1636 }
1637 } else {
1638 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1639 }
1640 if (!node) {
1641 lyd_free(data);
1642 return NC_MSG_ERROR;
1643 }
1644 break;
1645
1646 case NC_RPC_GETSCHEMA:
1647 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1648 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001649 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001650 return NC_MSG_ERROR;
1651 }
1652
1653 rpc_gs = (struct nc_rpc_getschema *)rpc;
1654
1655 data = lyd_new(NULL, ietfncmon, "get-schema");
1656 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1657 if (!node) {
1658 lyd_free(data);
1659 return NC_MSG_ERROR;
1660 }
1661 if (rpc_gs->version) {
1662 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1663 if (!node) {
1664 lyd_free(data);
1665 return NC_MSG_ERROR;
1666 }
1667 }
1668 if (rpc_gs->format) {
1669 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1670 if (!node) {
1671 lyd_free(data);
1672 return NC_MSG_ERROR;
1673 }
1674 }
1675 break;
1676
1677 case NC_RPC_SUBSCRIBE:
1678 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1679 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001680 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001681 return NC_MSG_ERROR;
1682 }
1683
1684 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1685
1686 data = lyd_new(NULL, notifs, "create-subscription");
1687 if (rpc_sub->stream) {
1688 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1689 if (!node) {
1690 lyd_free(data);
1691 return NC_MSG_ERROR;
1692 }
1693 }
1694
1695 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001696 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001697 /* we need a copy of the filter */
1698 filter = strdup(rpc_sub->filter);
1699 if (!filter) {
1700 ERRMEM;
1701 lyd_free(data);
1702 return NC_MSG_ERROR;
1703 }
1704
1705 node = lyd_new_anyxml_str(data, notifs, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001706 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001707 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001708 node = lyd_new_anyxml_str(data, notifs, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001709 lyd_insert_attr(node, NULL, "type", "xpath");
1710 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001711 }
1712 if (!node) {
1713 lyd_free(data);
1714 return NC_MSG_ERROR;
1715 }
1716 }
1717
1718 if (rpc_sub->start) {
1719 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1720 if (!node) {
1721 lyd_free(data);
1722 return NC_MSG_ERROR;
1723 }
1724 }
1725
1726 if (rpc_sub->stop) {
1727 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1728 if (!node) {
1729 lyd_free(data);
1730 return NC_MSG_ERROR;
1731 }
1732 }
1733 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001734 default:
1735 ERRINT;
1736 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001737 }
1738
Michal Vasko1d8212b2016-04-13 14:24:52 +02001739 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001740 lyd_free(data);
1741 return NC_MSG_ERROR;
1742 }
1743
Michal Vasko62be1ce2016-03-03 13:24:52 +01001744 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001745 if (ret == -1) {
1746 /* error */
1747 r = NC_MSG_ERROR;
1748 } else if (!ret) {
1749 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001750 r = NC_MSG_WOULDBLOCK;
1751 } else {
1752 /* send RPC, store its message ID */
1753 r = nc_send_msg(session, data);
1754 cur_msgid = session->msgid;
1755 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001756 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001757
1758 lyd_free(data);
1759
1760 if (r != NC_MSG_RPC) {
1761 return r;
1762 }
1763
1764 *msgid = cur_msgid;
1765 return NC_MSG_RPC;
1766}