blob: 6f2a320ec147dd136bc43fe380e7398748de9908 [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 *
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200194libyang_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
195 void *user_data, LYS_INFORMAT *format, 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;
Radek Krejci539efb62016-08-24 15:05:16 +0200201 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100202 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200203 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100204 uint64_t msgid;
205
206 /* TODO later replace with yang to reduce model size? */
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200207 if (submod_name) {
208 rpc = nc_rpc_getschema(submod_name, submod_rev, "yin", NC_PARAMTYPE_CONST);
209 } else {
210 rpc = nc_rpc_getschema(mod_name, mod_rev, "yin", NC_PARAMTYPE_CONST);
211 }
Michal Vasko086311b2016-01-08 09:53:11 +0100212 *format = LYS_IN_YIN;
213
214 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
215 usleep(1000);
216 }
217 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100218 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100219 nc_rpc_free(rpc);
220 return NULL;
221 }
222
Michal Vasko36b17ba2016-05-03 11:54:21 +0200223 msg = nc_recv_reply(session, rpc, msgid, 1000, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100224 nc_rpc_free(rpc);
225 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100226 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100227 return NULL;
228 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100229 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100230 return NULL;
231 }
232
Michal Vasko05ba9df2016-01-13 14:40:27 +0100233 if (reply->type != NC_RPL_DATA) {
234 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100235 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100236 nc_reply_free(reply);
237 return NULL;
238 }
239
Michal Vasko086311b2016-01-08 09:53:11 +0100240 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200241 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
242 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
243 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
244 nc_reply_free(reply);
245 return NULL;
246 }
Radek Krejci539efb62016-08-24 15:05:16 +0200247 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
248 switch (get_schema_data->value_type) {
249 case LYD_ANYDATA_CONSTSTRING:
250 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200251 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200252 break;
253 case LYD_ANYDATA_DATATREE:
254 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
255 break;
256 case LYD_ANYDATA_XML:
257 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
258 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200259 }
Michal Vasko086311b2016-01-08 09:53:11 +0100260 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100261 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100262
Michal Vasko086311b2016-01-08 09:53:11 +0100263 return model_data;
264}
265
Michal Vaskoef578332016-01-25 13:20:09 +0100266/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100267int
268nc_ctx_check_and_fill(struct nc_session *session)
269{
Michal Vaskoef578332016-01-25 13:20:09 +0100270 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100271 ly_module_clb old_clb = NULL;
272 void *old_data = NULL;
273
274 assert(session->cpblts && session->ctx);
275
276 /* check if get-schema is supported */
277 for (i = 0; session->cpblts[i]; ++i) {
278 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
279 get_schema_support = 1;
280 break;
281 }
282 }
283
284 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
285 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
286 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
287 /* set module retrieval using <get-schema> */
288 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100289 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100290 } else {
291 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
292 }
293 }
294
295 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100296 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100297 if (old_clb) {
298 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
299 }
Michal Vaskoef578332016-01-25 13:20:09 +0100300 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100301 }
302
303 /* load all other models */
304 for (i = 0; session->cpblts[i]; ++i) {
305 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
306 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
307 continue;
308 }
309
Michal Vaskoef578332016-01-25 13:20:09 +0100310 r = ctx_check_and_load_model(session, session->cpblts[i]);
311 if (r == -1) {
312 ret = -1;
313 break;
314 }
315
316 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
317 * if it was using get-schema */
318 if (r == 1) {
319 if (get_schema_support) {
320 VRB("Trying to load the schema from a different source.");
321 /* works even if old_clb is NULL */
322 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
323 r = ctx_check_and_load_model(session, session->cpblts[i]);
324 }
325
326 /* fail again (or no other way to try), too bad */
327 if (r) {
328 ret = 1;
329 }
330
331 /* set get-schema callback back */
332 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
333 }
Michal Vasko086311b2016-01-08 09:53:11 +0100334 }
335
336 if (old_clb) {
337 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
338 }
Michal Vaskoef578332016-01-25 13:20:09 +0100339 if (ret == 1) {
340 WRN("Some models failed to be loaded, any data from these models will be ignored.");
341 }
342 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100343}
344
345API struct nc_session *
346nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
347{
Michal Vaskod083db62016-01-19 10:31:29 +0100348 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100349
Michal Vasko45e53ae2016-04-07 11:46:03 +0200350 if (fdin < 0) {
351 ERRARG("fdin");
352 return NULL;
353 } else if (fdout < 0) {
354 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100355 return NULL;
356 }
357
358 /* prepare session structure */
359 session = calloc(1, sizeof *session);
360 if (!session) {
361 ERRMEM;
362 return NULL;
363 }
364 session->status = NC_STATUS_STARTING;
365 session->side = NC_CLIENT;
366
367 /* transport specific data */
368 session->ti_type = NC_TI_FD;
369 session->ti.fd.in = fdin;
370 session->ti.fd.out = fdout;
371
372 /* assign context (dicionary needed for handshake) */
373 if (!ctx) {
374 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100375 /* definitely should not happen, but be ready */
376 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
377 /* that's just it */
378 goto fail;
379 }
Michal Vasko086311b2016-01-08 09:53:11 +0100380 } else {
381 session->flags |= NC_SESSION_SHAREDCTX;
382 }
383 session->ctx = ctx;
384
385 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200386 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100387 goto fail;
388 }
389 session->status = NC_STATUS_RUNNING;
390
Michal Vaskoef578332016-01-25 13:20:09 +0100391 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100392 goto fail;
393 }
394
395 return session;
396
397fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100398 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100399 return NULL;
400}
401
402int
Michal Vaskof05562c2016-01-20 12:06:43 +0100403nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100404{
Michal Vasko0190bc32016-03-02 15:47:49 +0100405 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100406 struct addrinfo hints, *res_list, *res;
407 char port_s[6]; /* length of string representation of short int */
408
409 snprintf(port_s, 6, "%u", port);
410
411 /* Connect to a server */
412 memset(&hints, 0, sizeof hints);
413 hints.ai_family = AF_UNSPEC;
414 hints.ai_socktype = SOCK_STREAM;
415 hints.ai_protocol = IPPROTO_TCP;
416 i = getaddrinfo(host, port_s, &hints, &res_list);
417 if (i != 0) {
418 ERR("Unable to translate the host address (%s).", gai_strerror(i));
419 return -1;
420 }
421
422 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
423 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
424 if (sock == -1) {
425 /* socket was not created, try another resource */
426 i = errno;
427 goto errloop;
428 }
429
430 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
431 /* network connection failed, try another resource */
432 i = errno;
433 close(sock);
434 sock = -1;
435 goto errloop;
436 }
437
Michal Vasko0190bc32016-03-02 15:47:49 +0100438 /* make the socket non-blocking */
439 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
440 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100441 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100442 return -1;
443 }
444
Michal Vasko086311b2016-01-08 09:53:11 +0100445 /* we're done, network connection established */
446 break;
447errloop:
448 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
449 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
450 continue;
451 }
452
453 if (sock == -1) {
454 ERR("Unable to connect to %s:%s.", host, port_s);
455 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100456 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 +0100457 }
458 freeaddrinfo(res_list);
459
460 return sock;
461}
462
Michal Vasko086311b2016-01-08 09:53:11 +0100463static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100464get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100465{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100466 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100467 char *ptr;
468 const char *str_msgid;
469 uint64_t cur_msgid;
470 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100471 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100472 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
473
Michal Vasko62be1ce2016-03-03 13:24:52 +0100474 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100475 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100476 /* error */
477 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100478 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100479 /* timeout */
480 return NC_MSG_WOULDBLOCK;
481 }
482
483 /* try to get notification from the session's queue */
484 if (!msgid && session->notifs) {
485 cont = session->notifs;
486 session->notifs = cont->next;
487
Michal Vasko71ba2da2016-05-04 10:53:16 +0200488 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100489 free(cont);
490
Michal Vasko71ba2da2016-05-04 10:53:16 +0200491 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100492 }
493
494 /* try to get rpc-reply from the session's queue */
495 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200496 cont = session->replies;
497 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100498
Michal Vasko71ba2da2016-05-04 10:53:16 +0200499 xml = cont->msg;
500 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100501
Michal Vasko71ba2da2016-05-04 10:53:16 +0200502 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100503 }
504
Michal Vasko71ba2da2016-05-04 10:53:16 +0200505 if (!msgtype) {
506 /* read message from wire */
507 msgtype = nc_read_msg_poll(session, timeout, &xml);
508 }
Michal Vasko086311b2016-01-08 09:53:11 +0100509
510 /* we read rpc-reply, want a notif */
511 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100512 cont_ptr = &session->replies;
513 while (*cont_ptr) {
514 cont_ptr = &((*cont_ptr)->next);
515 }
516 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100517 if (!*cont_ptr) {
518 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200519 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100520 lyxml_free(session->ctx, xml);
521 return NC_MSG_ERROR;
522 }
Michal Vasko086311b2016-01-08 09:53:11 +0100523 (*cont_ptr)->msg = xml;
524 (*cont_ptr)->next = NULL;
525 }
526
527 /* we read notif, want a rpc-reply */
528 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100529 /* TODO check whether the session is even subscribed */
530 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100531 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100532 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100533 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100534 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100535 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100536
537 cont_ptr = &session->notifs;
538 while (*cont_ptr) {
539 cont_ptr = &((*cont_ptr)->next);
540 }
541 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100542 if (!cont_ptr) {
543 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200544 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100545 lyxml_free(session->ctx, xml);
546 return NC_MSG_ERROR;
547 }
Michal Vasko086311b2016-01-08 09:53:11 +0100548 (*cont_ptr)->msg = xml;
549 (*cont_ptr)->next = NULL;
550 }
551
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100552 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100553
554 switch (msgtype) {
555 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100556 if (!msgid) {
557 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100558 }
Michal Vasko086311b2016-01-08 09:53:11 +0100559 break;
560
561 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100562 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200563 /* check message-id */
564 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
565 if (!str_msgid) {
566 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
567 msgtype = NC_MSG_REPLY_ERR_MSGID;
568 } else {
569 cur_msgid = strtoul(str_msgid, &ptr, 10);
570 if (cur_msgid != msgid) {
571 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
572 session->id, str_msgid);
573 msgtype = NC_MSG_REPLY_ERR_MSGID;
574 }
575 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100576 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100577 }
Michal Vasko086311b2016-01-08 09:53:11 +0100578 break;
579
580 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100581 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100582 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200583 msgtype = NC_MSG_ERROR;
584 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100585
586 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100587 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100588 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200589 msgtype = NC_MSG_ERROR;
590 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100591
592 default:
593 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
594 * NC_MSG_NONE is not returned by nc_read_msg()
595 */
596 break;
597 }
598
599 return msgtype;
600}
601
602/* cannot strictly fail, but does not need to fill any error parameter at all */
603static void
604parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
605{
606 struct lyxml_elem *iter, *next, *info;
607
608 LY_TREE_FOR(xml->child, iter) {
609 if (!iter->ns) {
610 if (iter->content) {
611 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
612 } else {
613 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
614 }
615 continue;
616 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
617 if (iter->content) {
618 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
619 iter->name, iter->content, iter->ns->value);
620 } else {
621 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
622 }
623 continue;
624 }
625
626 if (!strcmp(iter->name, "error-type")) {
627 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
628 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
629 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
630 } else if (err->type) {
631 WRN("<rpc-error> <error-type> duplicated.");
632 } else {
633 err->type = lydict_insert(ctx, iter->content, 0);
634 }
635 } else if (!strcmp(iter->name, "error-tag")) {
636 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
637 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
638 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
639 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
640 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
641 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
642 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
643 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
644 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
645 && strcmp(iter->content, "malformed-message"))) {
646 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
647 } else if (err->tag) {
648 WRN("<rpc-error> <error-tag> duplicated.");
649 } else {
650 err->tag = lydict_insert(ctx, iter->content, 0);
651 }
652 } else if (!strcmp(iter->name, "error-severity")) {
653 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
654 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
655 } else if (err->severity) {
656 WRN("<rpc-error> <error-severity> duplicated.");
657 } else {
658 err->severity = lydict_insert(ctx, iter->content, 0);
659 }
660 } else if (!strcmp(iter->name, "error-app-tag")) {
661 if (err->apptag) {
662 WRN("<rpc-error> <error-app-tag> duplicated.");
663 } else {
664 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
665 }
666 } else if (!strcmp(iter->name, "error-path")) {
667 if (err->path) {
668 WRN("<rpc-error> <error-path> duplicated.");
669 } else {
670 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
671 }
672 } else if (!strcmp(iter->name, "error-message")) {
673 if (err->message) {
674 WRN("<rpc-error> <error-message> duplicated.");
675 } else {
676 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
677 if (!err->message_lang) {
678 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
679 }
680 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
681 }
682 } else if (!strcmp(iter->name, "error-info")) {
683 LY_TREE_FOR_SAFE(iter->child, next, info) {
684 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
685 if (!strcmp(info->name, "session-id")) {
686 if (err->sid) {
687 WRN("<rpc-error> <error-info> <session-id> duplicated.");
688 } else {
689 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
690 }
691 } else if (!strcmp(info->name, "bad-attr")) {
692 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100693 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
694 if (!err->attr) {
695 ERRMEM;
696 return;
697 }
Michal Vasko086311b2016-01-08 09:53:11 +0100698 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
699 } else if (!strcmp(info->name, "bad-element")) {
700 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100701 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
702 if (!err->elem) {
703 ERRMEM;
704 return;
705 }
Michal Vasko086311b2016-01-08 09:53:11 +0100706 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
707 } else if (!strcmp(info->name, "bad-namespace")) {
708 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100709 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
710 if (!err->ns) {
711 ERRMEM;
712 return;
713 }
Michal Vasko086311b2016-01-08 09:53:11 +0100714 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
715 } else {
716 if (info->content) {
717 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
718 info->name, info->content);
719 } else {
720 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
721 }
722 }
723 } else {
724 lyxml_unlink(ctx, info);
725 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100726 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
727 if (!err->other) {
728 ERRMEM;
729 return;
730 }
Michal Vasko086311b2016-01-08 09:53:11 +0100731 err->other[err->other_count - 1] = info;
732 }
733 }
734 } else {
735 if (iter->content) {
736 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
737 } else {
738 WRN("<rpc-error> unknown child \"%s\".", iter->name);
739 }
740 }
741 }
742}
743
744static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100745parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100746{
747 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100748 const struct lys_node *schema = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200749 struct lyd_node *data = NULL, *next, *elem;
Michal Vasko1a38c862016-01-15 15:50:07 +0100750 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100751 struct nc_reply_data *data_rpl;
752 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200753 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100754 int i;
755
756 if (!xml->child) {
757 ERR("An empty <rpc-reply>.");
758 return NULL;
759 }
760
761 /* rpc-error */
762 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
763 /* count and check elements */
764 i = 0;
765 LY_TREE_FOR(xml->child, iter) {
766 if (strcmp(iter->name, "rpc-error")) {
767 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
768 return NULL;
769 } else if (!iter->ns) {
770 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
771 return NULL;
772 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
773 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
774 return NULL;
775 }
776 ++i;
777 }
778
779 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100780 if (!error_rpl) {
781 ERRMEM;
782 return NULL;
783 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100784 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100785 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100786 if (!error_rpl->err) {
787 ERRMEM;
788 free(error_rpl);
789 return NULL;
790 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100791 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100792 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100793 reply = (struct nc_reply *)error_rpl;
794
795 i = 0;
796 LY_TREE_FOR(xml->child, iter) {
797 parse_rpc_error(ctx, iter, error_rpl->err + i);
798 ++i;
799 }
800
801 /* ok */
802 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
803 if (xml->child->next) {
804 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
805 return NULL;
806 }
807 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100808 if (!reply) {
809 ERRMEM;
810 return NULL;
811 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100812 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100813
814 /* some RPC output */
815 } else {
816 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200817 case NC_RPC_ACT_GENERIC:
818 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100819
820 if (rpc_gen->has_data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200821 data = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100822 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100823 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100824 if (!data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200825 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100826 return NULL;
827 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200828 }
829 if (data->schema->nodetype == LYS_RPC) {
830 /* RPC */
Michal Vasko086311b2016-01-08 09:53:11 +0100831 schema = data->schema;
Michal Vasko90e8e692016-07-13 12:27:57 +0200832 } else {
833 /* action */
834 LY_TREE_DFS_BEGIN(data, next, elem) {
835 if (elem->schema->nodetype == LYS_ACTION) {
836 schema = elem->schema;
837 break;
838 }
839 LY_TREE_DFS_END(data, next, elem);
840 }
841 }
842
843 /* cleanup */
844 if (data != rpc_gen->content.data) {
Michal Vasko086311b2016-01-08 09:53:11 +0100845 lyd_free(data);
846 data = NULL;
847 }
848 if (!schema) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200849 /* only with action, if there is no action, it should not have gotten this far */
Michal Vasko9e036d52016-01-08 10:49:26 +0100850 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100851 return NULL;
852 }
853 break;
854
855 case NC_RPC_GETCONFIG:
856 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100857 if (!xml->child->child) {
858 /* we did not receive any data */
859 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100860 if (!data_rpl) {
861 ERRMEM;
862 return NULL;
863 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100864 data_rpl->type = NC_RPL_DATA;
865 data_rpl->data = NULL;
866 return (struct nc_reply *)data_rpl;
867 }
868
Michal Vasko086311b2016-01-08 09:53:11 +0100869 /* special treatment */
870 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100871 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100872 if (!data) {
873 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
874 return NULL;
875 }
876 break;
877
878 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100879 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100880 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100881 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100882 return NULL;
883 }
884 break;
885
886 case NC_RPC_EDIT:
887 case NC_RPC_COPY:
888 case NC_RPC_DELETE:
889 case NC_RPC_LOCK:
890 case NC_RPC_UNLOCK:
891 case NC_RPC_KILL:
892 case NC_RPC_COMMIT:
893 case NC_RPC_DISCARD:
894 case NC_RPC_CANCEL:
895 case NC_RPC_VALIDATE:
896 case NC_RPC_SUBSCRIBE:
897 /* there is no output defined */
898 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
899 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100900 default:
901 ERRINT;
902 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100903 }
904
905 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100906 if (!data_rpl) {
907 ERRMEM;
908 return NULL;
909 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100910 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100911 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100912 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100913 } else {
914 /* <get>, <get-config> */
915 data_rpl->data = data;
916 }
917 if (!data_rpl->data) {
918 ERR("Failed to parse <rpc-reply>.");
919 free(data_rpl);
920 return NULL;
921 }
922 reply = (struct nc_reply *)data_rpl;
923 }
924
925 return reply;
926}
927
Radek Krejci53691be2016-02-22 13:58:37 +0100928#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100929
Michal Vasko3031aae2016-01-27 16:07:18 +0100930int
931nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
932{
933 int sock;
934
Michal Vasko45e53ae2016-04-07 11:46:03 +0200935 if (!address) {
936 ERRARG("address");
937 return -1;
938 } else if (!port) {
939 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100940 return -1;
941 }
942
943 sock = nc_sock_listen(address, port);
944 if (sock == -1) {
945 return -1;
946 }
947
948 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100949 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
950 if (!client_opts.ch_binds) {
951 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100952 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100953 return -1;
954 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100955
956 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100957 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
958 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100959 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100960 return -1;
961 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100962 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
963 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
964 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
965
966 return 0;
967}
968
969int
970nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
971{
972 uint32_t i;
973 int ret = -1;
974
975 if (!address && !port && !ti) {
976 for (i = 0; i < client_opts.ch_bind_count; ++i) {
977 close(client_opts.ch_binds[i].sock);
978 free((char *)client_opts.ch_binds[i].address);
979
980 ret = 0;
981 }
982 free(client_opts.ch_binds);
983 client_opts.ch_binds = NULL;
984 client_opts.ch_bind_count = 0;
985 } else {
986 for (i = 0; i < client_opts.ch_bind_count; ++i) {
987 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
988 && (!port || (client_opts.ch_binds[i].port == port))
989 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
990 close(client_opts.ch_binds[i].sock);
991 free((char *)client_opts.ch_binds[i].address);
992
993 --client_opts.ch_bind_count;
994 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
995
996 ret = 0;
997 }
998 }
999 }
1000
1001 return ret;
1002}
1003
1004API int
1005nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1006{
1007 int sock;
1008 char *host = NULL;
1009 uint16_t port, idx;
1010
Michal Vasko45e53ae2016-04-07 11:46:03 +02001011 if (!client_opts.ch_binds) {
1012 ERRINIT;
1013 return -1;
1014 } else if (!session) {
1015 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001016 return -1;
1017 }
1018
1019 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1020
Michal Vasko50456e82016-02-02 12:16:08 +01001021 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001022 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001023 return sock;
1024 }
1025
Radek Krejci53691be2016-02-22 13:58:37 +01001026#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001027 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001028 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001029 } else
1030#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001031#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001032 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001033 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001034 } else
1035#endif
1036 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001037 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001038 *session = NULL;
1039 }
1040
1041 free(host);
1042
1043 if (!(*session)) {
1044 return -1;
1045 }
1046
1047 return 1;
1048}
1049
Radek Krejci53691be2016-02-22 13:58:37 +01001050#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001051
Michal Vaskobdfb5242016-05-24 09:11:01 +02001052API const char **
1053nc_session_get_cpblts(const struct nc_session *session)
1054{
1055 if (!session) {
1056 ERRARG("session");
1057 return NULL;
1058 }
1059
1060 return session->cpblts;
1061}
1062
1063API const char *
1064nc_session_cpblt(const struct nc_session *session, const char *capab)
1065{
1066 int i, len;
1067
1068 if (!session) {
1069 ERRARG("session");
1070 return NULL;
1071 } else if (!capab) {
1072 ERRARG("capab");
1073 return NULL;
1074 }
1075
1076 len = strlen(capab);
1077 for (i = 0; session->cpblts[i]; ++i) {
1078 if (!strncmp(session->cpblts[i], capab, len)) {
1079 return session->cpblts[i];
1080 }
1081 }
1082
1083 return NULL;
1084}
1085
Michal Vasko9cd26a82016-05-31 08:58:48 +02001086API int
1087nc_session_ntf_thread_running(const struct nc_session *session)
1088{
1089 if (!session) {
1090 ERRARG("session");
1091 return 0;
1092 }
1093
1094 return session->ntf_tid ? 1 : 0;
1095}
1096
Michal Vaskob7558c52016-02-26 15:04:19 +01001097API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001098nc_client_init(void)
1099{
1100 nc_init();
1101}
1102
1103API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001104nc_client_destroy(void)
1105{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001106 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001107#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1108 nc_client_ch_del_bind(NULL, 0, 0);
1109#endif
1110#ifdef NC_ENABLED_SSH
1111 nc_client_ssh_destroy_opts();
1112#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001113#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001114 nc_client_tls_destroy_opts();
1115#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001116 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001117}
1118
Michal Vasko086311b2016-01-08 09:53:11 +01001119API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001120nc_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 +01001121{
1122 struct lyxml_elem *xml;
1123 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1124
Michal Vasko45e53ae2016-04-07 11:46:03 +02001125 if (!session) {
1126 ERRARG("session");
1127 return NC_MSG_ERROR;
1128 } else if (!rpc) {
1129 ERRARG("rpc");
1130 return NC_MSG_ERROR;
1131 } else if (!reply) {
1132 ERRARG("reply");
1133 return NC_MSG_ERROR;
1134 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1135 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001136 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001137 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001138 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001139 return NC_MSG_ERROR;
1140 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001141 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001142 *reply = NULL;
1143
1144 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001145
Michal Vasko71ba2da2016-05-04 10:53:16 +02001146 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001147 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001148 lyxml_free(session->ctx, xml);
1149 if (!(*reply)) {
1150 return NC_MSG_ERROR;
1151 }
1152 }
1153
1154 return msgtype;
1155}
1156
1157API NC_MSG_TYPE
1158nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1159{
1160 struct lyxml_elem *xml, *ev_time;
1161 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1162
Michal Vasko45e53ae2016-04-07 11:46:03 +02001163 if (!session) {
1164 ERRARG("session");
1165 return NC_MSG_ERROR;
1166 } else if (!notif) {
1167 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001168 return NC_MSG_ERROR;
1169 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001170 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001171 return NC_MSG_ERROR;
1172 }
1173
1174 msgtype = get_msg(session, timeout, 0, &xml);
1175
1176 if (msgtype == NC_MSG_NOTIF) {
1177 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001178 if (!*notif) {
1179 ERRMEM;
1180 lyxml_free(session->ctx, xml);
1181 return NC_MSG_ERROR;
1182 }
Michal Vasko086311b2016-01-08 09:53:11 +01001183
1184 /* eventTime */
1185 LY_TREE_FOR(xml->child, ev_time) {
1186 if (!strcmp(ev_time->name, "eventTime")) {
1187 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1188 /* lyd_parse does not know this element */
1189 lyxml_free(session->ctx, ev_time);
1190 break;
1191 }
1192 }
1193 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001194 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001195 goto fail;
1196 }
1197
1198 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001199 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001200 lyxml_free(session->ctx, xml);
1201 xml = NULL;
1202 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001203 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001204 goto fail;
1205 }
1206 }
1207
1208 return msgtype;
1209
1210fail:
1211 lydict_remove(session->ctx, (*notif)->datetime);
1212 lyd_free((*notif)->tree);
1213 free(*notif);
1214 *notif = NULL;
1215 lyxml_free(session->ctx, xml);
1216
1217 return NC_MSG_ERROR;
1218}
1219
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001220static void *
1221nc_recv_notif_thread(void *arg)
1222{
1223 struct nc_ntf_thread_arg *ntarg;
1224 struct nc_session *session;
1225 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1226 struct nc_notif *notif;
1227 NC_MSG_TYPE msgtype;
1228
1229 ntarg = (struct nc_ntf_thread_arg *)arg;
1230 session = ntarg->session;
1231 notif_clb = ntarg->notif_clb;
1232 free(ntarg);
1233
1234 while (session->ntf_tid) {
1235 msgtype = nc_recv_notif(session, 0, &notif);
1236 if (msgtype == NC_MSG_NOTIF) {
1237 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001238 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1239 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1240 nc_notif_free(notif);
1241 break;
1242 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001243 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001244 } else if (msgtype == NC_MSG_ERROR) {
1245 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001246 }
1247
1248 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1249 }
1250
Michal Vasko0651c902016-05-19 15:55:42 +02001251 VRB("Session %u: notification thread exit.", session->id);
1252 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001253 return NULL;
1254}
1255
1256API int
1257nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1258{
1259 struct nc_ntf_thread_arg *ntarg;
1260 int ret;
1261
Michal Vasko45e53ae2016-04-07 11:46:03 +02001262 if (!session) {
1263 ERRARG("session");
1264 return -1;
1265 } else if (!notif_clb) {
1266 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001267 return -1;
1268 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1269 ERR("Session %u: invalid session to receive Notifications.", session->id);
1270 return -1;
1271 } else if (session->ntf_tid) {
1272 ERR("Session %u: separate notification thread is already running.", session->id);
1273 return -1;
1274 }
1275
1276 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001277 if (!ntarg) {
1278 ERRMEM;
1279 return -1;
1280 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001281 ntarg->session = session;
1282 ntarg->notif_clb = notif_clb;
1283
1284 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1285 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001286 if (!session->ntf_tid) {
1287 ERRMEM;
1288 free(ntarg);
1289 return -1;
1290 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001291
1292 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1293 if (ret) {
1294 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1295 free(ntarg);
1296 free((pthread_t *)session->ntf_tid);
1297 session->ntf_tid = NULL;
1298 return -1;
1299 }
1300
1301 return 0;
1302}
1303
Michal Vasko086311b2016-01-08 09:53:11 +01001304API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001305nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001306{
1307 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001308 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001309 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001310 struct nc_rpc_getconfig *rpc_gc;
1311 struct nc_rpc_edit *rpc_e;
1312 struct nc_rpc_copy *rpc_cp;
1313 struct nc_rpc_delete *rpc_del;
1314 struct nc_rpc_lock *rpc_lock;
1315 struct nc_rpc_get *rpc_g;
1316 struct nc_rpc_kill *rpc_k;
1317 struct nc_rpc_commit *rpc_com;
1318 struct nc_rpc_cancel *rpc_can;
1319 struct nc_rpc_validate *rpc_val;
1320 struct nc_rpc_getschema *rpc_gs;
1321 struct nc_rpc_subscribe *rpc_sub;
1322 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001323 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001324 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001325 uint64_t cur_msgid;
1326
Michal Vasko45e53ae2016-04-07 11:46:03 +02001327 if (!session) {
1328 ERRARG("session");
1329 return NC_MSG_ERROR;
1330 } else if (!rpc) {
1331 ERRARG("rpc");
1332 return NC_MSG_ERROR;
1333 } else if (!msgid) {
1334 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001335 return NC_MSG_ERROR;
1336 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001337 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001338 return NC_MSG_ERROR;
1339 }
1340
Michal Vasko90e8e692016-07-13 12:27:57 +02001341 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001342 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1343 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001344 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001345 return NC_MSG_ERROR;
1346 }
1347 }
1348
1349 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001350 case NC_RPC_ACT_GENERIC:
1351 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001352
1353 if (rpc_gen->has_data) {
1354 data = rpc_gen->content.data;
1355 } else {
Michal Vasko03df9e32016-05-17 10:22:53 +02001356 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001357 }
1358 break;
1359
1360 case NC_RPC_GETCONFIG:
1361 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1362
1363 data = lyd_new(NULL, ietfnc, "get-config");
1364 node = lyd_new(data, ietfnc, "source");
1365 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1366 if (!node) {
1367 lyd_free(data);
1368 return NC_MSG_ERROR;
1369 }
1370 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001371 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001372 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001373 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001374 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001375 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001376 lyd_insert_attr(node, NULL, "type", "xpath");
1377 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001378 }
1379 if (!node) {
1380 lyd_free(data);
1381 return NC_MSG_ERROR;
1382 }
1383 }
1384
1385 if (rpc_gc->wd_mode) {
1386 if (!ietfncwd) {
1387 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1388 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001389 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001390 return NC_MSG_ERROR;
1391 }
1392 }
1393 switch (rpc_gc->wd_mode) {
1394 case NC_WD_UNKNOWN:
1395 /* cannot get here */
1396 break;
1397 case NC_WD_ALL:
1398 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1399 break;
1400 case NC_WD_ALL_TAG:
1401 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1402 break;
1403 case NC_WD_TRIM:
1404 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1405 break;
1406 case NC_WD_EXPLICIT:
1407 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1408 break;
1409 }
1410 if (!node) {
1411 lyd_free(data);
1412 return NC_MSG_ERROR;
1413 }
1414 }
1415 break;
1416
1417 case NC_RPC_EDIT:
1418 rpc_e = (struct nc_rpc_edit *)rpc;
1419
1420 data = lyd_new(NULL, ietfnc, "edit-config");
1421 node = lyd_new(data, ietfnc, "target");
1422 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1423 if (!node) {
1424 lyd_free(data);
1425 return NC_MSG_ERROR;
1426 }
1427
1428 if (rpc_e->default_op) {
1429 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1430 if (!node) {
1431 lyd_free(data);
1432 return NC_MSG_ERROR;
1433 }
1434 }
1435
1436 if (rpc_e->test_opt) {
1437 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1438 if (!node) {
1439 lyd_free(data);
1440 return NC_MSG_ERROR;
1441 }
1442 }
1443
1444 if (rpc_e->error_opt) {
1445 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1446 if (!node) {
1447 lyd_free(data);
1448 return NC_MSG_ERROR;
1449 }
1450 }
1451
Michal Vasko7793bc62016-09-16 11:58:41 +02001452 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001453 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001454 } else {
1455 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1456 }
1457 if (!node) {
1458 lyd_free(data);
1459 return NC_MSG_ERROR;
1460 }
1461 break;
1462
1463 case NC_RPC_COPY:
1464 rpc_cp = (struct nc_rpc_copy *)rpc;
1465
1466 data = lyd_new(NULL, ietfnc, "copy-config");
1467 node = lyd_new(data, ietfnc, "target");
1468 if (rpc_cp->url_trg) {
1469 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1470 } else {
1471 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1472 }
1473 if (!node) {
1474 lyd_free(data);
1475 return NC_MSG_ERROR;
1476 }
1477
1478 node = lyd_new(data, ietfnc, "source");
1479 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001480 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001481 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001482 } else {
1483 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1484 }
1485 } else {
1486 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1487 }
1488 if (!node) {
1489 lyd_free(data);
1490 return NC_MSG_ERROR;
1491 }
1492
1493 if (rpc_cp->wd_mode) {
1494 if (!ietfncwd) {
1495 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1496 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001497 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001498 return NC_MSG_ERROR;
1499 }
1500 }
1501 switch (rpc_cp->wd_mode) {
1502 case NC_WD_UNKNOWN:
1503 /* cannot get here */
1504 break;
1505 case NC_WD_ALL:
1506 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1507 break;
1508 case NC_WD_ALL_TAG:
1509 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1510 break;
1511 case NC_WD_TRIM:
1512 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1513 break;
1514 case NC_WD_EXPLICIT:
1515 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1516 break;
1517 }
1518 if (!node) {
1519 lyd_free(data);
1520 return NC_MSG_ERROR;
1521 }
1522 }
1523 break;
1524
1525 case NC_RPC_DELETE:
1526 rpc_del = (struct nc_rpc_delete *)rpc;
1527
1528 data = lyd_new(NULL, ietfnc, "delete-config");
1529 node = lyd_new(data, ietfnc, "target");
1530 if (rpc_del->url) {
1531 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1532 } else {
1533 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1534 }
1535 if (!node) {
1536 lyd_free(data);
1537 return NC_MSG_ERROR;
1538 }
1539 break;
1540
1541 case NC_RPC_LOCK:
1542 rpc_lock = (struct nc_rpc_lock *)rpc;
1543
1544 data = lyd_new(NULL, ietfnc, "lock");
1545 node = lyd_new(data, ietfnc, "target");
1546 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1547 if (!node) {
1548 lyd_free(data);
1549 return NC_MSG_ERROR;
1550 }
1551 break;
1552
1553 case NC_RPC_UNLOCK:
1554 rpc_lock = (struct nc_rpc_lock *)rpc;
1555
1556 data = lyd_new(NULL, ietfnc, "unlock");
1557 node = lyd_new(data, ietfnc, "target");
1558 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1559 if (!node) {
1560 lyd_free(data);
1561 return NC_MSG_ERROR;
1562 }
1563 break;
1564
1565 case NC_RPC_GET:
1566 rpc_g = (struct nc_rpc_get *)rpc;
1567
1568 data = lyd_new(NULL, ietfnc, "get");
1569 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001570 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001571 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001572 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001573 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001574 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001575 lyd_insert_attr(node, NULL, "type", "xpath");
1576 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001577 }
1578 if (!node) {
1579 lyd_free(data);
1580 return NC_MSG_ERROR;
1581 }
1582 }
1583
1584 if (rpc_g->wd_mode) {
1585 if (!ietfncwd) {
1586 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1587 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001588 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001589 return NC_MSG_ERROR;
1590 }
1591 }
1592 switch (rpc_g->wd_mode) {
1593 case NC_WD_UNKNOWN:
1594 /* cannot get here */
1595 break;
1596 case NC_WD_ALL:
1597 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1598 break;
1599 case NC_WD_ALL_TAG:
1600 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1601 break;
1602 case NC_WD_TRIM:
1603 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1604 break;
1605 case NC_WD_EXPLICIT:
1606 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1607 break;
1608 }
1609 if (!node) {
1610 lyd_free(data);
1611 return NC_MSG_ERROR;
1612 }
1613 }
1614 break;
1615
1616 case NC_RPC_KILL:
1617 rpc_k = (struct nc_rpc_kill *)rpc;
1618
1619 data = lyd_new(NULL, ietfnc, "kill-session");
1620 sprintf(str, "%u", rpc_k->sid);
1621 lyd_new_leaf(data, ietfnc, "session-id", str);
1622 break;
1623
1624 case NC_RPC_COMMIT:
1625 rpc_com = (struct nc_rpc_commit *)rpc;
1626
1627 data = lyd_new(NULL, ietfnc, "commit");
1628 if (rpc_com->confirmed) {
1629 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1630 }
1631
1632 if (rpc_com->confirm_timeout) {
1633 sprintf(str, "%u", rpc_com->confirm_timeout);
1634 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1635 }
1636
1637 if (rpc_com->persist) {
1638 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1639 if (!node) {
1640 lyd_free(data);
1641 return NC_MSG_ERROR;
1642 }
1643 }
1644
1645 if (rpc_com->persist_id) {
1646 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1647 if (!node) {
1648 lyd_free(data);
1649 return NC_MSG_ERROR;
1650 }
1651 }
1652 break;
1653
1654 case NC_RPC_DISCARD:
1655 data = lyd_new(NULL, ietfnc, "discard-changes");
1656 break;
1657
1658 case NC_RPC_CANCEL:
1659 rpc_can = (struct nc_rpc_cancel *)rpc;
1660
1661 data = lyd_new(NULL, ietfnc, "cancel-commit");
1662 if (rpc_can->persist_id) {
1663 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1664 if (!node) {
1665 lyd_free(data);
1666 return NC_MSG_ERROR;
1667 }
1668 }
1669 break;
1670
1671 case NC_RPC_VALIDATE:
1672 rpc_val = (struct nc_rpc_validate *)rpc;
1673
1674 data = lyd_new(NULL, ietfnc, "validate");
1675 node = lyd_new(data, ietfnc, "source");
1676 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001677 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001678 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001679 } else {
1680 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1681 }
1682 } else {
1683 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1684 }
1685 if (!node) {
1686 lyd_free(data);
1687 return NC_MSG_ERROR;
1688 }
1689 break;
1690
1691 case NC_RPC_GETSCHEMA:
1692 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1693 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001694 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001695 return NC_MSG_ERROR;
1696 }
1697
1698 rpc_gs = (struct nc_rpc_getschema *)rpc;
1699
1700 data = lyd_new(NULL, ietfncmon, "get-schema");
1701 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1702 if (!node) {
1703 lyd_free(data);
1704 return NC_MSG_ERROR;
1705 }
1706 if (rpc_gs->version) {
1707 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1708 if (!node) {
1709 lyd_free(data);
1710 return NC_MSG_ERROR;
1711 }
1712 }
1713 if (rpc_gs->format) {
1714 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1715 if (!node) {
1716 lyd_free(data);
1717 return NC_MSG_ERROR;
1718 }
1719 }
1720 break;
1721
1722 case NC_RPC_SUBSCRIBE:
1723 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1724 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001725 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001726 return NC_MSG_ERROR;
1727 }
1728
1729 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1730
1731 data = lyd_new(NULL, notifs, "create-subscription");
1732 if (rpc_sub->stream) {
1733 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1734 if (!node) {
1735 lyd_free(data);
1736 return NC_MSG_ERROR;
1737 }
1738 }
1739
1740 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001741 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001742 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001743 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001744 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001745 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001746 lyd_insert_attr(node, NULL, "type", "xpath");
1747 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001748 }
1749 if (!node) {
1750 lyd_free(data);
1751 return NC_MSG_ERROR;
1752 }
1753 }
1754
1755 if (rpc_sub->start) {
1756 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1757 if (!node) {
1758 lyd_free(data);
1759 return NC_MSG_ERROR;
1760 }
1761 }
1762
1763 if (rpc_sub->stop) {
1764 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1765 if (!node) {
1766 lyd_free(data);
1767 return NC_MSG_ERROR;
1768 }
1769 }
1770 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001771 default:
1772 ERRINT;
1773 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001774 }
1775
Michal Vasko49a0cf92016-09-14 09:15:32 +02001776 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT, NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001777 lyd_free(data);
1778 return NC_MSG_ERROR;
1779 }
1780
Michal Vasko62be1ce2016-03-03 13:24:52 +01001781 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001782 if (ret == -1) {
1783 /* error */
1784 r = NC_MSG_ERROR;
1785 } else if (!ret) {
1786 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001787 r = NC_MSG_WOULDBLOCK;
1788 } else {
1789 /* send RPC, store its message ID */
1790 r = nc_send_msg(session, data);
1791 cur_msgid = session->msgid;
1792 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001793 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001794
1795 lyd_free(data);
1796
1797 if (r != NC_MSG_RPC) {
1798 return r;
1799 }
1800
1801 *msgid = cur_msgid;
1802 return NC_MSG_RPC;
1803}