blob: 4475dc131861280345343b8ac485e7806fc8d67e [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;
Michal Vasko998ba412016-09-16 12:00:07 +0200201 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200202 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100203 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200204 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100205 uint64_t msgid;
206
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200207 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200208 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200209 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200210 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200211 }
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 Vasko998ba412016-09-16 12:00:07 +0200233 switch (reply->type) {
234 case NC_RPL_OK:
235 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
236 nc_reply_free(reply);
237 return NULL;
238 case NC_RPL_DATA:
239 /* fine */
240 break;
241 case NC_RPL_ERROR:
242 error_rpl = (struct nc_reply_error *)reply;
243 if (error_rpl->count) {
244 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
245 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
246 } else {
247 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
248 }
249 nc_reply_free(reply);
250 return NULL;
251 case NC_RPL_NOTIF:
252 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100253 nc_reply_free(reply);
254 return NULL;
255 }
256
Michal Vasko086311b2016-01-08 09:53:11 +0100257 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200258 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
259 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
260 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
261 nc_reply_free(reply);
262 return NULL;
263 }
Radek Krejci539efb62016-08-24 15:05:16 +0200264 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
265 switch (get_schema_data->value_type) {
266 case LYD_ANYDATA_CONSTSTRING:
267 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200268 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200269 break;
270 case LYD_ANYDATA_DATATREE:
271 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
272 break;
273 case LYD_ANYDATA_XML:
274 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
275 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200276 case LYD_ANYDATA_JSON:
277 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200278 case LYD_ANYDATA_SXML:
279 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200280 ERRINT;
281 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200282 }
Michal Vasko086311b2016-01-08 09:53:11 +0100283 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100284 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200285 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100286
Michal Vasko086311b2016-01-08 09:53:11 +0100287 return model_data;
288}
289
Michal Vaskoef578332016-01-25 13:20:09 +0100290/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100291int
292nc_ctx_check_and_fill(struct nc_session *session)
293{
Michal Vaskoef578332016-01-25 13:20:09 +0100294 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100295 ly_module_clb old_clb = NULL;
296 void *old_data = NULL;
297
298 assert(session->cpblts && session->ctx);
299
300 /* check if get-schema is supported */
301 for (i = 0; session->cpblts[i]; ++i) {
302 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
303 get_schema_support = 1;
304 break;
305 }
306 }
307
308 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
309 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
310 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
311 /* set module retrieval using <get-schema> */
312 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100313 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100314 } else {
315 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
316 }
317 }
318
319 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100320 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100321 if (old_clb) {
322 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
323 }
Michal Vaskoef578332016-01-25 13:20:09 +0100324 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100325 }
326
327 /* load all other models */
328 for (i = 0; session->cpblts[i]; ++i) {
329 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
330 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
331 continue;
332 }
333
Michal Vaskoef578332016-01-25 13:20:09 +0100334 r = ctx_check_and_load_model(session, session->cpblts[i]);
335 if (r == -1) {
336 ret = -1;
337 break;
338 }
339
340 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
341 * if it was using get-schema */
342 if (r == 1) {
343 if (get_schema_support) {
344 VRB("Trying to load the schema from a different source.");
345 /* works even if old_clb is NULL */
346 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
347 r = ctx_check_and_load_model(session, session->cpblts[i]);
348 }
349
350 /* fail again (or no other way to try), too bad */
351 if (r) {
352 ret = 1;
353 }
354
355 /* set get-schema callback back */
356 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
357 }
Michal Vasko086311b2016-01-08 09:53:11 +0100358 }
359
360 if (old_clb) {
361 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
362 }
Michal Vaskoef578332016-01-25 13:20:09 +0100363 if (ret == 1) {
364 WRN("Some models failed to be loaded, any data from these models will be ignored.");
365 }
366 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100367}
368
369API struct nc_session *
370nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
371{
Michal Vaskod083db62016-01-19 10:31:29 +0100372 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100373
Michal Vasko45e53ae2016-04-07 11:46:03 +0200374 if (fdin < 0) {
375 ERRARG("fdin");
376 return NULL;
377 } else if (fdout < 0) {
378 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100379 return NULL;
380 }
381
382 /* prepare session structure */
383 session = calloc(1, sizeof *session);
384 if (!session) {
385 ERRMEM;
386 return NULL;
387 }
388 session->status = NC_STATUS_STARTING;
389 session->side = NC_CLIENT;
390
391 /* transport specific data */
392 session->ti_type = NC_TI_FD;
393 session->ti.fd.in = fdin;
394 session->ti.fd.out = fdout;
395
396 /* assign context (dicionary needed for handshake) */
397 if (!ctx) {
398 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100399 /* definitely should not happen, but be ready */
400 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
401 /* that's just it */
402 goto fail;
403 }
Michal Vasko086311b2016-01-08 09:53:11 +0100404 } else {
405 session->flags |= NC_SESSION_SHAREDCTX;
406 }
407 session->ctx = ctx;
408
409 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200410 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100411 goto fail;
412 }
413 session->status = NC_STATUS_RUNNING;
414
Michal Vaskoef578332016-01-25 13:20:09 +0100415 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100416 goto fail;
417 }
418
419 return session;
420
421fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100422 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100423 return NULL;
424}
425
426int
Michal Vaskof05562c2016-01-20 12:06:43 +0100427nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100428{
Michal Vasko0190bc32016-03-02 15:47:49 +0100429 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100430 struct addrinfo hints, *res_list, *res;
431 char port_s[6]; /* length of string representation of short int */
432
433 snprintf(port_s, 6, "%u", port);
434
435 /* Connect to a server */
436 memset(&hints, 0, sizeof hints);
437 hints.ai_family = AF_UNSPEC;
438 hints.ai_socktype = SOCK_STREAM;
439 hints.ai_protocol = IPPROTO_TCP;
440 i = getaddrinfo(host, port_s, &hints, &res_list);
441 if (i != 0) {
442 ERR("Unable to translate the host address (%s).", gai_strerror(i));
443 return -1;
444 }
445
446 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
447 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
448 if (sock == -1) {
449 /* socket was not created, try another resource */
450 i = errno;
451 goto errloop;
452 }
453
454 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
455 /* network connection failed, try another resource */
456 i = errno;
457 close(sock);
458 sock = -1;
459 goto errloop;
460 }
461
Michal Vasko0190bc32016-03-02 15:47:49 +0100462 /* make the socket non-blocking */
463 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
464 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100465 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100466 return -1;
467 }
468
Michal Vasko086311b2016-01-08 09:53:11 +0100469 /* we're done, network connection established */
470 break;
471errloop:
472 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
473 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
474 continue;
475 }
476
477 if (sock == -1) {
478 ERR("Unable to connect to %s:%s.", host, port_s);
479 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100480 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 +0100481 }
482 freeaddrinfo(res_list);
483
484 return sock;
485}
486
Michal Vasko086311b2016-01-08 09:53:11 +0100487static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100488get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100489{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100490 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100491 char *ptr;
492 const char *str_msgid;
493 uint64_t cur_msgid;
494 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100495 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100496 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
497
Michal Vasko62be1ce2016-03-03 13:24:52 +0100498 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100500 /* error */
501 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100502 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100503 /* timeout */
504 return NC_MSG_WOULDBLOCK;
505 }
506
507 /* try to get notification from the session's queue */
508 if (!msgid && session->notifs) {
509 cont = session->notifs;
510 session->notifs = cont->next;
511
Michal Vasko71ba2da2016-05-04 10:53:16 +0200512 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100513 free(cont);
514
Michal Vasko71ba2da2016-05-04 10:53:16 +0200515 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100516 }
517
518 /* try to get rpc-reply from the session's queue */
519 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200520 cont = session->replies;
521 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100522
Michal Vasko71ba2da2016-05-04 10:53:16 +0200523 xml = cont->msg;
524 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100525
Michal Vasko71ba2da2016-05-04 10:53:16 +0200526 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100527 }
528
Michal Vasko71ba2da2016-05-04 10:53:16 +0200529 if (!msgtype) {
530 /* read message from wire */
531 msgtype = nc_read_msg_poll(session, timeout, &xml);
532 }
Michal Vasko086311b2016-01-08 09:53:11 +0100533
534 /* we read rpc-reply, want a notif */
535 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100536 cont_ptr = &session->replies;
537 while (*cont_ptr) {
538 cont_ptr = &((*cont_ptr)->next);
539 }
540 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100541 if (!*cont_ptr) {
542 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200543 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100544 lyxml_free(session->ctx, xml);
545 return NC_MSG_ERROR;
546 }
Michal Vasko086311b2016-01-08 09:53:11 +0100547 (*cont_ptr)->msg = xml;
548 (*cont_ptr)->next = NULL;
549 }
550
551 /* we read notif, want a rpc-reply */
552 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100553 /* TODO check whether the session is even subscribed */
554 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100555 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100556 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100557 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100558 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100559 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100560
561 cont_ptr = &session->notifs;
562 while (*cont_ptr) {
563 cont_ptr = &((*cont_ptr)->next);
564 }
565 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100566 if (!cont_ptr) {
567 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200568 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100569 lyxml_free(session->ctx, xml);
570 return NC_MSG_ERROR;
571 }
Michal Vasko086311b2016-01-08 09:53:11 +0100572 (*cont_ptr)->msg = xml;
573 (*cont_ptr)->next = NULL;
574 }
575
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100576 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100577
578 switch (msgtype) {
579 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100580 if (!msgid) {
581 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100582 }
Michal Vasko086311b2016-01-08 09:53:11 +0100583 break;
584
585 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100586 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200587 /* check message-id */
588 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
589 if (!str_msgid) {
590 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
591 msgtype = NC_MSG_REPLY_ERR_MSGID;
592 } else {
593 cur_msgid = strtoul(str_msgid, &ptr, 10);
594 if (cur_msgid != msgid) {
595 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
596 session->id, str_msgid);
597 msgtype = NC_MSG_REPLY_ERR_MSGID;
598 }
599 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100600 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100601 }
Michal Vasko086311b2016-01-08 09:53:11 +0100602 break;
603
604 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100605 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100606 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200607 msgtype = NC_MSG_ERROR;
608 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100609
610 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100611 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100612 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200613 msgtype = NC_MSG_ERROR;
614 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100615
616 default:
617 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
618 * NC_MSG_NONE is not returned by nc_read_msg()
619 */
620 break;
621 }
622
623 return msgtype;
624}
625
626/* cannot strictly fail, but does not need to fill any error parameter at all */
627static void
628parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
629{
630 struct lyxml_elem *iter, *next, *info;
631
632 LY_TREE_FOR(xml->child, iter) {
633 if (!iter->ns) {
634 if (iter->content) {
635 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
636 } else {
637 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
638 }
639 continue;
640 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
641 if (iter->content) {
642 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
643 iter->name, iter->content, iter->ns->value);
644 } else {
645 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
646 }
647 continue;
648 }
649
650 if (!strcmp(iter->name, "error-type")) {
651 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
652 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
653 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
654 } else if (err->type) {
655 WRN("<rpc-error> <error-type> duplicated.");
656 } else {
657 err->type = lydict_insert(ctx, iter->content, 0);
658 }
659 } else if (!strcmp(iter->name, "error-tag")) {
660 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
661 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
662 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
663 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
664 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
665 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
666 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
667 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
668 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
669 && strcmp(iter->content, "malformed-message"))) {
670 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
671 } else if (err->tag) {
672 WRN("<rpc-error> <error-tag> duplicated.");
673 } else {
674 err->tag = lydict_insert(ctx, iter->content, 0);
675 }
676 } else if (!strcmp(iter->name, "error-severity")) {
677 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
678 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
679 } else if (err->severity) {
680 WRN("<rpc-error> <error-severity> duplicated.");
681 } else {
682 err->severity = lydict_insert(ctx, iter->content, 0);
683 }
684 } else if (!strcmp(iter->name, "error-app-tag")) {
685 if (err->apptag) {
686 WRN("<rpc-error> <error-app-tag> duplicated.");
687 } else {
688 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
689 }
690 } else if (!strcmp(iter->name, "error-path")) {
691 if (err->path) {
692 WRN("<rpc-error> <error-path> duplicated.");
693 } else {
694 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
695 }
696 } else if (!strcmp(iter->name, "error-message")) {
697 if (err->message) {
698 WRN("<rpc-error> <error-message> duplicated.");
699 } else {
700 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
701 if (!err->message_lang) {
702 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
703 }
704 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
705 }
706 } else if (!strcmp(iter->name, "error-info")) {
707 LY_TREE_FOR_SAFE(iter->child, next, info) {
708 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
709 if (!strcmp(info->name, "session-id")) {
710 if (err->sid) {
711 WRN("<rpc-error> <error-info> <session-id> duplicated.");
712 } else {
713 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
714 }
715 } else if (!strcmp(info->name, "bad-attr")) {
716 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100717 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
718 if (!err->attr) {
719 ERRMEM;
720 return;
721 }
Michal Vasko086311b2016-01-08 09:53:11 +0100722 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
723 } else if (!strcmp(info->name, "bad-element")) {
724 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100725 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
726 if (!err->elem) {
727 ERRMEM;
728 return;
729 }
Michal Vasko086311b2016-01-08 09:53:11 +0100730 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
731 } else if (!strcmp(info->name, "bad-namespace")) {
732 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100733 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
734 if (!err->ns) {
735 ERRMEM;
736 return;
737 }
Michal Vasko086311b2016-01-08 09:53:11 +0100738 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
739 } else {
740 if (info->content) {
741 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
742 info->name, info->content);
743 } else {
744 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
745 }
746 }
747 } else {
748 lyxml_unlink(ctx, info);
749 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100750 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
751 if (!err->other) {
752 ERRMEM;
753 return;
754 }
Michal Vasko086311b2016-01-08 09:53:11 +0100755 err->other[err->other_count - 1] = info;
756 }
757 }
758 } else {
759 if (iter->content) {
760 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
761 } else {
762 WRN("<rpc-error> unknown child \"%s\".", iter->name);
763 }
764 }
765 }
766}
767
768static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100769parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100770{
771 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100772 const struct lys_node *schema = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200773 struct lyd_node *data = NULL, *next, *elem;
Michal Vasko1a38c862016-01-15 15:50:07 +0100774 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100775 struct nc_reply_data *data_rpl;
776 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200777 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100778 int i;
779
780 if (!xml->child) {
781 ERR("An empty <rpc-reply>.");
782 return NULL;
783 }
784
785 /* rpc-error */
786 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
787 /* count and check elements */
788 i = 0;
789 LY_TREE_FOR(xml->child, iter) {
790 if (strcmp(iter->name, "rpc-error")) {
791 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
792 return NULL;
793 } else if (!iter->ns) {
794 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
795 return NULL;
796 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
797 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
798 return NULL;
799 }
800 ++i;
801 }
802
803 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100804 if (!error_rpl) {
805 ERRMEM;
806 return NULL;
807 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100808 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100809 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100810 if (!error_rpl->err) {
811 ERRMEM;
812 free(error_rpl);
813 return NULL;
814 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100815 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100816 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100817 reply = (struct nc_reply *)error_rpl;
818
819 i = 0;
820 LY_TREE_FOR(xml->child, iter) {
821 parse_rpc_error(ctx, iter, error_rpl->err + i);
822 ++i;
823 }
824
825 /* ok */
826 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
827 if (xml->child->next) {
828 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
829 return NULL;
830 }
831 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100832 if (!reply) {
833 ERRMEM;
834 return NULL;
835 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100836 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100837
838 /* some RPC output */
839 } else {
840 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200841 case NC_RPC_ACT_GENERIC:
842 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100843
844 if (rpc_gen->has_data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200845 data = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100846 } else {
Michal Vasko68b3f292016-09-16 12:00:32 +0200847 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100848 if (!data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200849 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100850 return NULL;
851 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200852 }
853 if (data->schema->nodetype == LYS_RPC) {
854 /* RPC */
Michal Vasko086311b2016-01-08 09:53:11 +0100855 schema = data->schema;
Michal Vasko90e8e692016-07-13 12:27:57 +0200856 } else {
857 /* action */
858 LY_TREE_DFS_BEGIN(data, next, elem) {
859 if (elem->schema->nodetype == LYS_ACTION) {
860 schema = elem->schema;
861 break;
862 }
863 LY_TREE_DFS_END(data, next, elem);
864 }
865 }
866
867 /* cleanup */
868 if (data != rpc_gen->content.data) {
Michal Vasko086311b2016-01-08 09:53:11 +0100869 lyd_free(data);
870 data = NULL;
871 }
872 if (!schema) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200873 /* only with action, if there is no action, it should not have gotten this far */
Michal Vasko9e036d52016-01-08 10:49:26 +0100874 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100875 return NULL;
876 }
877 break;
878
879 case NC_RPC_GETCONFIG:
880 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100881 if (!xml->child->child) {
882 /* we did not receive any data */
883 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100884 if (!data_rpl) {
885 ERRMEM;
886 return NULL;
887 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100888 data_rpl->type = NC_RPL_DATA;
889 data_rpl->data = NULL;
890 return (struct nc_reply *)data_rpl;
891 }
892
Michal Vasko086311b2016-01-08 09:53:11 +0100893 /* special treatment */
894 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100895 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100896 if (!data) {
897 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
898 return NULL;
899 }
900 break;
901
902 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100903 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100904 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100905 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100906 return NULL;
907 }
908 break;
909
910 case NC_RPC_EDIT:
911 case NC_RPC_COPY:
912 case NC_RPC_DELETE:
913 case NC_RPC_LOCK:
914 case NC_RPC_UNLOCK:
915 case NC_RPC_KILL:
916 case NC_RPC_COMMIT:
917 case NC_RPC_DISCARD:
918 case NC_RPC_CANCEL:
919 case NC_RPC_VALIDATE:
920 case NC_RPC_SUBSCRIBE:
921 /* there is no output defined */
922 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
923 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100924 default:
925 ERRINT;
926 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100927 }
928
929 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100930 if (!data_rpl) {
931 ERRMEM;
932 return NULL;
933 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100934 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100935 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +0200936 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
937 schema, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100938 } else {
939 /* <get>, <get-config> */
940 data_rpl->data = data;
941 }
942 if (!data_rpl->data) {
943 ERR("Failed to parse <rpc-reply>.");
944 free(data_rpl);
945 return NULL;
946 }
947 reply = (struct nc_reply *)data_rpl;
948 }
949
950 return reply;
951}
952
Radek Krejci53691be2016-02-22 13:58:37 +0100953#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100954
Michal Vasko3031aae2016-01-27 16:07:18 +0100955int
956nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
957{
958 int sock;
959
Michal Vasko45e53ae2016-04-07 11:46:03 +0200960 if (!address) {
961 ERRARG("address");
962 return -1;
963 } else if (!port) {
964 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100965 return -1;
966 }
967
968 sock = nc_sock_listen(address, port);
969 if (sock == -1) {
970 return -1;
971 }
972
973 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100974 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
975 if (!client_opts.ch_binds) {
976 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100977 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100978 return -1;
979 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100980
981 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100982 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
983 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100984 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100985 return -1;
986 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100987 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
988 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
989 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
990
991 return 0;
992}
993
994int
995nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
996{
997 uint32_t i;
998 int ret = -1;
999
1000 if (!address && !port && !ti) {
1001 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1002 close(client_opts.ch_binds[i].sock);
1003 free((char *)client_opts.ch_binds[i].address);
1004
1005 ret = 0;
1006 }
1007 free(client_opts.ch_binds);
1008 client_opts.ch_binds = NULL;
1009 client_opts.ch_bind_count = 0;
1010 } else {
1011 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1012 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1013 && (!port || (client_opts.ch_binds[i].port == port))
1014 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
1015 close(client_opts.ch_binds[i].sock);
1016 free((char *)client_opts.ch_binds[i].address);
1017
1018 --client_opts.ch_bind_count;
1019 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1020
1021 ret = 0;
1022 }
1023 }
1024 }
1025
1026 return ret;
1027}
1028
1029API int
1030nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1031{
1032 int sock;
1033 char *host = NULL;
1034 uint16_t port, idx;
1035
Michal Vasko45e53ae2016-04-07 11:46:03 +02001036 if (!client_opts.ch_binds) {
1037 ERRINIT;
1038 return -1;
1039 } else if (!session) {
1040 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001041 return -1;
1042 }
1043
1044 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1045
Michal Vasko50456e82016-02-02 12:16:08 +01001046 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001047 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001048 return sock;
1049 }
1050
Radek Krejci53691be2016-02-22 13:58:37 +01001051#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001052 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001053 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001054 } else
1055#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001056#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001057 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001058 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001059 } else
1060#endif
1061 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001062 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001063 *session = NULL;
1064 }
1065
1066 free(host);
1067
1068 if (!(*session)) {
1069 return -1;
1070 }
1071
1072 return 1;
1073}
1074
Radek Krejci53691be2016-02-22 13:58:37 +01001075#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001076
Michal Vaskobdfb5242016-05-24 09:11:01 +02001077API const char **
1078nc_session_get_cpblts(const struct nc_session *session)
1079{
1080 if (!session) {
1081 ERRARG("session");
1082 return NULL;
1083 }
1084
1085 return session->cpblts;
1086}
1087
1088API const char *
1089nc_session_cpblt(const struct nc_session *session, const char *capab)
1090{
1091 int i, len;
1092
1093 if (!session) {
1094 ERRARG("session");
1095 return NULL;
1096 } else if (!capab) {
1097 ERRARG("capab");
1098 return NULL;
1099 }
1100
1101 len = strlen(capab);
1102 for (i = 0; session->cpblts[i]; ++i) {
1103 if (!strncmp(session->cpblts[i], capab, len)) {
1104 return session->cpblts[i];
1105 }
1106 }
1107
1108 return NULL;
1109}
1110
Michal Vasko9cd26a82016-05-31 08:58:48 +02001111API int
1112nc_session_ntf_thread_running(const struct nc_session *session)
1113{
1114 if (!session) {
1115 ERRARG("session");
1116 return 0;
1117 }
1118
1119 return session->ntf_tid ? 1 : 0;
1120}
1121
Michal Vaskob7558c52016-02-26 15:04:19 +01001122API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001123nc_client_init(void)
1124{
1125 nc_init();
1126}
1127
1128API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001129nc_client_destroy(void)
1130{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001131 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001132#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1133 nc_client_ch_del_bind(NULL, 0, 0);
1134#endif
1135#ifdef NC_ENABLED_SSH
1136 nc_client_ssh_destroy_opts();
1137#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001138#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001139 nc_client_tls_destroy_opts();
1140#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001141 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001142}
1143
Michal Vasko086311b2016-01-08 09:53:11 +01001144API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001145nc_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 +01001146{
1147 struct lyxml_elem *xml;
1148 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1149
Michal Vasko45e53ae2016-04-07 11:46:03 +02001150 if (!session) {
1151 ERRARG("session");
1152 return NC_MSG_ERROR;
1153 } else if (!rpc) {
1154 ERRARG("rpc");
1155 return NC_MSG_ERROR;
1156 } else if (!reply) {
1157 ERRARG("reply");
1158 return NC_MSG_ERROR;
1159 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1160 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001161 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001162 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001163 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001164 return NC_MSG_ERROR;
1165 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001166 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001167 *reply = NULL;
1168
1169 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001170
Michal Vasko71ba2da2016-05-04 10:53:16 +02001171 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001172 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001173 lyxml_free(session->ctx, xml);
1174 if (!(*reply)) {
1175 return NC_MSG_ERROR;
1176 }
1177 }
1178
1179 return msgtype;
1180}
1181
1182API NC_MSG_TYPE
1183nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1184{
1185 struct lyxml_elem *xml, *ev_time;
1186 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1187
Michal Vasko45e53ae2016-04-07 11:46:03 +02001188 if (!session) {
1189 ERRARG("session");
1190 return NC_MSG_ERROR;
1191 } else if (!notif) {
1192 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001193 return NC_MSG_ERROR;
1194 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001195 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001196 return NC_MSG_ERROR;
1197 }
1198
1199 msgtype = get_msg(session, timeout, 0, &xml);
1200
1201 if (msgtype == NC_MSG_NOTIF) {
1202 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001203 if (!*notif) {
1204 ERRMEM;
1205 lyxml_free(session->ctx, xml);
1206 return NC_MSG_ERROR;
1207 }
Michal Vasko086311b2016-01-08 09:53:11 +01001208
1209 /* eventTime */
1210 LY_TREE_FOR(xml->child, ev_time) {
1211 if (!strcmp(ev_time->name, "eventTime")) {
1212 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1213 /* lyd_parse does not know this element */
1214 lyxml_free(session->ctx, ev_time);
1215 break;
1216 }
1217 }
1218 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001219 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001220 goto fail;
1221 }
1222
1223 /* notification body */
Michal Vasko68b3f292016-09-16 12:00:32 +02001224 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001225 lyxml_free(session->ctx, xml);
1226 xml = NULL;
1227 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001228 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001229 goto fail;
1230 }
1231 }
1232
1233 return msgtype;
1234
1235fail:
1236 lydict_remove(session->ctx, (*notif)->datetime);
1237 lyd_free((*notif)->tree);
1238 free(*notif);
1239 *notif = NULL;
1240 lyxml_free(session->ctx, xml);
1241
1242 return NC_MSG_ERROR;
1243}
1244
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001245static void *
1246nc_recv_notif_thread(void *arg)
1247{
1248 struct nc_ntf_thread_arg *ntarg;
1249 struct nc_session *session;
1250 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1251 struct nc_notif *notif;
1252 NC_MSG_TYPE msgtype;
1253
1254 ntarg = (struct nc_ntf_thread_arg *)arg;
1255 session = ntarg->session;
1256 notif_clb = ntarg->notif_clb;
1257 free(ntarg);
1258
1259 while (session->ntf_tid) {
1260 msgtype = nc_recv_notif(session, 0, &notif);
1261 if (msgtype == NC_MSG_NOTIF) {
1262 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001263 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1264 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1265 nc_notif_free(notif);
1266 break;
1267 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001268 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001269 } else if (msgtype == NC_MSG_ERROR) {
1270 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001271 }
1272
1273 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1274 }
1275
Michal Vasko0651c902016-05-19 15:55:42 +02001276 VRB("Session %u: notification thread exit.", session->id);
1277 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001278 return NULL;
1279}
1280
1281API int
1282nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1283{
1284 struct nc_ntf_thread_arg *ntarg;
1285 int ret;
1286
Michal Vasko45e53ae2016-04-07 11:46:03 +02001287 if (!session) {
1288 ERRARG("session");
1289 return -1;
1290 } else if (!notif_clb) {
1291 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001292 return -1;
1293 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1294 ERR("Session %u: invalid session to receive Notifications.", session->id);
1295 return -1;
1296 } else if (session->ntf_tid) {
1297 ERR("Session %u: separate notification thread is already running.", session->id);
1298 return -1;
1299 }
1300
1301 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001302 if (!ntarg) {
1303 ERRMEM;
1304 return -1;
1305 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001306 ntarg->session = session;
1307 ntarg->notif_clb = notif_clb;
1308
1309 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1310 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001311 if (!session->ntf_tid) {
1312 ERRMEM;
1313 free(ntarg);
1314 return -1;
1315 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001316
1317 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1318 if (ret) {
1319 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1320 free(ntarg);
1321 free((pthread_t *)session->ntf_tid);
1322 session->ntf_tid = NULL;
1323 return -1;
1324 }
1325
1326 return 0;
1327}
1328
Michal Vasko086311b2016-01-08 09:53:11 +01001329API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001330nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001331{
1332 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001333 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001334 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001335 struct nc_rpc_getconfig *rpc_gc;
1336 struct nc_rpc_edit *rpc_e;
1337 struct nc_rpc_copy *rpc_cp;
1338 struct nc_rpc_delete *rpc_del;
1339 struct nc_rpc_lock *rpc_lock;
1340 struct nc_rpc_get *rpc_g;
1341 struct nc_rpc_kill *rpc_k;
1342 struct nc_rpc_commit *rpc_com;
1343 struct nc_rpc_cancel *rpc_can;
1344 struct nc_rpc_validate *rpc_val;
1345 struct nc_rpc_getschema *rpc_gs;
1346 struct nc_rpc_subscribe *rpc_sub;
1347 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001348 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001349 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001350 uint64_t cur_msgid;
1351
Michal Vasko45e53ae2016-04-07 11:46:03 +02001352 if (!session) {
1353 ERRARG("session");
1354 return NC_MSG_ERROR;
1355 } else if (!rpc) {
1356 ERRARG("rpc");
1357 return NC_MSG_ERROR;
1358 } else if (!msgid) {
1359 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001360 return NC_MSG_ERROR;
1361 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001362 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001363 return NC_MSG_ERROR;
1364 }
1365
Michal Vasko90e8e692016-07-13 12:27:57 +02001366 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001367 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1368 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001369 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001370 return NC_MSG_ERROR;
1371 }
1372 }
1373
1374 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001375 case NC_RPC_ACT_GENERIC:
1376 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001377
1378 if (rpc_gen->has_data) {
1379 data = rpc_gen->content.data;
1380 } else {
Michal Vasko68b3f292016-09-16 12:00:32 +02001381 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_STRICT, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001382 }
1383 break;
1384
1385 case NC_RPC_GETCONFIG:
1386 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1387
1388 data = lyd_new(NULL, ietfnc, "get-config");
1389 node = lyd_new(data, ietfnc, "source");
1390 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1391 if (!node) {
1392 lyd_free(data);
1393 return NC_MSG_ERROR;
1394 }
1395 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001396 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001397 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001398 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001399 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001400 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001401 lyd_insert_attr(node, NULL, "type", "xpath");
1402 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001403 }
1404 if (!node) {
1405 lyd_free(data);
1406 return NC_MSG_ERROR;
1407 }
1408 }
1409
1410 if (rpc_gc->wd_mode) {
1411 if (!ietfncwd) {
1412 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1413 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001414 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001415 return NC_MSG_ERROR;
1416 }
1417 }
1418 switch (rpc_gc->wd_mode) {
1419 case NC_WD_UNKNOWN:
1420 /* cannot get here */
1421 break;
1422 case NC_WD_ALL:
1423 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1424 break;
1425 case NC_WD_ALL_TAG:
1426 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1427 break;
1428 case NC_WD_TRIM:
1429 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1430 break;
1431 case NC_WD_EXPLICIT:
1432 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1433 break;
1434 }
1435 if (!node) {
1436 lyd_free(data);
1437 return NC_MSG_ERROR;
1438 }
1439 }
1440 break;
1441
1442 case NC_RPC_EDIT:
1443 rpc_e = (struct nc_rpc_edit *)rpc;
1444
1445 data = lyd_new(NULL, ietfnc, "edit-config");
1446 node = lyd_new(data, ietfnc, "target");
1447 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1448 if (!node) {
1449 lyd_free(data);
1450 return NC_MSG_ERROR;
1451 }
1452
1453 if (rpc_e->default_op) {
1454 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1455 if (!node) {
1456 lyd_free(data);
1457 return NC_MSG_ERROR;
1458 }
1459 }
1460
1461 if (rpc_e->test_opt) {
1462 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1463 if (!node) {
1464 lyd_free(data);
1465 return NC_MSG_ERROR;
1466 }
1467 }
1468
1469 if (rpc_e->error_opt) {
1470 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1471 if (!node) {
1472 lyd_free(data);
1473 return NC_MSG_ERROR;
1474 }
1475 }
1476
Michal Vasko7793bc62016-09-16 11:58:41 +02001477 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001478 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001479 } else {
1480 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1481 }
1482 if (!node) {
1483 lyd_free(data);
1484 return NC_MSG_ERROR;
1485 }
1486 break;
1487
1488 case NC_RPC_COPY:
1489 rpc_cp = (struct nc_rpc_copy *)rpc;
1490
1491 data = lyd_new(NULL, ietfnc, "copy-config");
1492 node = lyd_new(data, ietfnc, "target");
1493 if (rpc_cp->url_trg) {
1494 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1495 } else {
1496 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1497 }
1498 if (!node) {
1499 lyd_free(data);
1500 return NC_MSG_ERROR;
1501 }
1502
1503 node = lyd_new(data, ietfnc, "source");
1504 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001505 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001506 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001507 } else {
1508 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1509 }
1510 } else {
1511 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1512 }
1513 if (!node) {
1514 lyd_free(data);
1515 return NC_MSG_ERROR;
1516 }
1517
1518 if (rpc_cp->wd_mode) {
1519 if (!ietfncwd) {
1520 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1521 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001522 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001523 return NC_MSG_ERROR;
1524 }
1525 }
1526 switch (rpc_cp->wd_mode) {
1527 case NC_WD_UNKNOWN:
1528 /* cannot get here */
1529 break;
1530 case NC_WD_ALL:
1531 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1532 break;
1533 case NC_WD_ALL_TAG:
1534 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1535 break;
1536 case NC_WD_TRIM:
1537 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1538 break;
1539 case NC_WD_EXPLICIT:
1540 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1541 break;
1542 }
1543 if (!node) {
1544 lyd_free(data);
1545 return NC_MSG_ERROR;
1546 }
1547 }
1548 break;
1549
1550 case NC_RPC_DELETE:
1551 rpc_del = (struct nc_rpc_delete *)rpc;
1552
1553 data = lyd_new(NULL, ietfnc, "delete-config");
1554 node = lyd_new(data, ietfnc, "target");
1555 if (rpc_del->url) {
1556 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1557 } else {
1558 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1559 }
1560 if (!node) {
1561 lyd_free(data);
1562 return NC_MSG_ERROR;
1563 }
1564 break;
1565
1566 case NC_RPC_LOCK:
1567 rpc_lock = (struct nc_rpc_lock *)rpc;
1568
1569 data = lyd_new(NULL, ietfnc, "lock");
1570 node = lyd_new(data, ietfnc, "target");
1571 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1572 if (!node) {
1573 lyd_free(data);
1574 return NC_MSG_ERROR;
1575 }
1576 break;
1577
1578 case NC_RPC_UNLOCK:
1579 rpc_lock = (struct nc_rpc_lock *)rpc;
1580
1581 data = lyd_new(NULL, ietfnc, "unlock");
1582 node = lyd_new(data, ietfnc, "target");
1583 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1584 if (!node) {
1585 lyd_free(data);
1586 return NC_MSG_ERROR;
1587 }
1588 break;
1589
1590 case NC_RPC_GET:
1591 rpc_g = (struct nc_rpc_get *)rpc;
1592
1593 data = lyd_new(NULL, ietfnc, "get");
1594 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001595 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001596 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001597 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001598 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001599 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001600 lyd_insert_attr(node, NULL, "type", "xpath");
1601 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001602 }
1603 if (!node) {
1604 lyd_free(data);
1605 return NC_MSG_ERROR;
1606 }
1607 }
1608
1609 if (rpc_g->wd_mode) {
1610 if (!ietfncwd) {
1611 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1612 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001613 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001614 return NC_MSG_ERROR;
1615 }
1616 }
1617 switch (rpc_g->wd_mode) {
1618 case NC_WD_UNKNOWN:
1619 /* cannot get here */
1620 break;
1621 case NC_WD_ALL:
1622 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1623 break;
1624 case NC_WD_ALL_TAG:
1625 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1626 break;
1627 case NC_WD_TRIM:
1628 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1629 break;
1630 case NC_WD_EXPLICIT:
1631 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1632 break;
1633 }
1634 if (!node) {
1635 lyd_free(data);
1636 return NC_MSG_ERROR;
1637 }
1638 }
1639 break;
1640
1641 case NC_RPC_KILL:
1642 rpc_k = (struct nc_rpc_kill *)rpc;
1643
1644 data = lyd_new(NULL, ietfnc, "kill-session");
1645 sprintf(str, "%u", rpc_k->sid);
1646 lyd_new_leaf(data, ietfnc, "session-id", str);
1647 break;
1648
1649 case NC_RPC_COMMIT:
1650 rpc_com = (struct nc_rpc_commit *)rpc;
1651
1652 data = lyd_new(NULL, ietfnc, "commit");
1653 if (rpc_com->confirmed) {
1654 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1655 }
1656
1657 if (rpc_com->confirm_timeout) {
1658 sprintf(str, "%u", rpc_com->confirm_timeout);
1659 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1660 }
1661
1662 if (rpc_com->persist) {
1663 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1664 if (!node) {
1665 lyd_free(data);
1666 return NC_MSG_ERROR;
1667 }
1668 }
1669
1670 if (rpc_com->persist_id) {
1671 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1672 if (!node) {
1673 lyd_free(data);
1674 return NC_MSG_ERROR;
1675 }
1676 }
1677 break;
1678
1679 case NC_RPC_DISCARD:
1680 data = lyd_new(NULL, ietfnc, "discard-changes");
1681 break;
1682
1683 case NC_RPC_CANCEL:
1684 rpc_can = (struct nc_rpc_cancel *)rpc;
1685
1686 data = lyd_new(NULL, ietfnc, "cancel-commit");
1687 if (rpc_can->persist_id) {
1688 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1689 if (!node) {
1690 lyd_free(data);
1691 return NC_MSG_ERROR;
1692 }
1693 }
1694 break;
1695
1696 case NC_RPC_VALIDATE:
1697 rpc_val = (struct nc_rpc_validate *)rpc;
1698
1699 data = lyd_new(NULL, ietfnc, "validate");
1700 node = lyd_new(data, ietfnc, "source");
1701 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001702 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001703 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001704 } else {
1705 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1706 }
1707 } else {
1708 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1709 }
1710 if (!node) {
1711 lyd_free(data);
1712 return NC_MSG_ERROR;
1713 }
1714 break;
1715
1716 case NC_RPC_GETSCHEMA:
1717 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1718 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001719 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001720 return NC_MSG_ERROR;
1721 }
1722
1723 rpc_gs = (struct nc_rpc_getschema *)rpc;
1724
1725 data = lyd_new(NULL, ietfncmon, "get-schema");
1726 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1727 if (!node) {
1728 lyd_free(data);
1729 return NC_MSG_ERROR;
1730 }
1731 if (rpc_gs->version) {
1732 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1733 if (!node) {
1734 lyd_free(data);
1735 return NC_MSG_ERROR;
1736 }
1737 }
1738 if (rpc_gs->format) {
1739 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1740 if (!node) {
1741 lyd_free(data);
1742 return NC_MSG_ERROR;
1743 }
1744 }
1745 break;
1746
1747 case NC_RPC_SUBSCRIBE:
1748 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1749 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001750 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001751 return NC_MSG_ERROR;
1752 }
1753
1754 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1755
1756 data = lyd_new(NULL, notifs, "create-subscription");
1757 if (rpc_sub->stream) {
1758 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1759 if (!node) {
1760 lyd_free(data);
1761 return NC_MSG_ERROR;
1762 }
1763 }
1764
1765 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001766 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001767 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001768 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001769 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001770 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001771 lyd_insert_attr(node, NULL, "type", "xpath");
1772 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001773 }
1774 if (!node) {
1775 lyd_free(data);
1776 return NC_MSG_ERROR;
1777 }
1778 }
1779
1780 if (rpc_sub->start) {
1781 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1782 if (!node) {
1783 lyd_free(data);
1784 return NC_MSG_ERROR;
1785 }
1786 }
1787
1788 if (rpc_sub->stop) {
1789 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1790 if (!node) {
1791 lyd_free(data);
1792 return NC_MSG_ERROR;
1793 }
1794 }
1795 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001796 default:
1797 ERRINT;
1798 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001799 }
1800
Michal Vasko49a0cf92016-09-14 09:15:32 +02001801 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT, NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001802 lyd_free(data);
1803 return NC_MSG_ERROR;
1804 }
1805
Michal Vasko62be1ce2016-03-03 13:24:52 +01001806 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001807 if (ret == -1) {
1808 /* error */
1809 r = NC_MSG_ERROR;
1810 } else if (!ret) {
1811 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001812 r = NC_MSG_WOULDBLOCK;
1813 } else {
1814 /* send RPC, store its message ID */
1815 r = nc_send_msg(session, data);
1816 cur_msgid = session->msgid;
1817 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001818 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001819
1820 lyd_free(data);
1821
1822 if (r != NC_MSG_RPC) {
1823 return r;
1824 }
1825
1826 *msgid = cur_msgid;
1827 return NC_MSG_RPC;
1828}