blob: c4872cd833fc307ef12b79fc438e0fcc49d437c5 [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 Vaskoff8ddc02016-10-03 14:13:29 +0200223 do {
224 msg = nc_recv_reply(session, rpc, msgid, 1000, 0, &reply);
225 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100226 nc_rpc_free(rpc);
227 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100228 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100229 return NULL;
230 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100231 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100232 return NULL;
233 }
234
Michal Vasko998ba412016-09-16 12:00:07 +0200235 switch (reply->type) {
236 case NC_RPL_OK:
237 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
238 nc_reply_free(reply);
239 return NULL;
240 case NC_RPL_DATA:
241 /* fine */
242 break;
243 case NC_RPL_ERROR:
244 error_rpl = (struct nc_reply_error *)reply;
245 if (error_rpl->count) {
246 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
247 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
248 } else {
249 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
250 }
251 nc_reply_free(reply);
252 return NULL;
253 case NC_RPL_NOTIF:
254 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100255 nc_reply_free(reply);
256 return NULL;
257 }
258
Michal Vasko086311b2016-01-08 09:53:11 +0100259 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200260 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
261 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
262 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
263 nc_reply_free(reply);
264 return NULL;
265 }
Radek Krejci539efb62016-08-24 15:05:16 +0200266 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
267 switch (get_schema_data->value_type) {
268 case LYD_ANYDATA_CONSTSTRING:
269 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200270 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200271 break;
272 case LYD_ANYDATA_DATATREE:
273 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
274 break;
275 case LYD_ANYDATA_XML:
276 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
277 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200278 case LYD_ANYDATA_JSON:
279 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200280 case LYD_ANYDATA_SXML:
281 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200282 ERRINT;
283 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200284 }
Michal Vasko086311b2016-01-08 09:53:11 +0100285 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100286 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200287 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100288
Michal Vasko086311b2016-01-08 09:53:11 +0100289 return model_data;
290}
291
Michal Vaskoef578332016-01-25 13:20:09 +0100292/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100293int
294nc_ctx_check_and_fill(struct nc_session *session)
295{
Michal Vaskoef578332016-01-25 13:20:09 +0100296 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100297 ly_module_clb old_clb = NULL;
298 void *old_data = NULL;
299
300 assert(session->cpblts && session->ctx);
301
302 /* check if get-schema is supported */
303 for (i = 0; session->cpblts[i]; ++i) {
304 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
305 get_schema_support = 1;
306 break;
307 }
308 }
309
310 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
311 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
312 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
313 /* set module retrieval using <get-schema> */
314 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100315 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100316 } else {
317 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
318 }
319 }
320
321 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100322 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100323 if (old_clb) {
324 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
325 }
Michal Vaskoef578332016-01-25 13:20:09 +0100326 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100327 }
328
329 /* load all other models */
330 for (i = 0; session->cpblts[i]; ++i) {
331 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
332 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
333 continue;
334 }
335
Michal Vaskoef578332016-01-25 13:20:09 +0100336 r = ctx_check_and_load_model(session, session->cpblts[i]);
337 if (r == -1) {
338 ret = -1;
339 break;
340 }
341
342 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
343 * if it was using get-schema */
344 if (r == 1) {
345 if (get_schema_support) {
346 VRB("Trying to load the schema from a different source.");
347 /* works even if old_clb is NULL */
348 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
349 r = ctx_check_and_load_model(session, session->cpblts[i]);
350 }
351
352 /* fail again (or no other way to try), too bad */
353 if (r) {
354 ret = 1;
355 }
356
357 /* set get-schema callback back */
358 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
359 }
Michal Vasko086311b2016-01-08 09:53:11 +0100360 }
361
362 if (old_clb) {
363 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
364 }
Michal Vaskoef578332016-01-25 13:20:09 +0100365 if (ret == 1) {
366 WRN("Some models failed to be loaded, any data from these models will be ignored.");
367 }
368 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100369}
370
371API struct nc_session *
372nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
373{
Michal Vaskod083db62016-01-19 10:31:29 +0100374 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100375
Michal Vasko45e53ae2016-04-07 11:46:03 +0200376 if (fdin < 0) {
377 ERRARG("fdin");
378 return NULL;
379 } else if (fdout < 0) {
380 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100381 return NULL;
382 }
383
384 /* prepare session structure */
385 session = calloc(1, sizeof *session);
386 if (!session) {
387 ERRMEM;
388 return NULL;
389 }
390 session->status = NC_STATUS_STARTING;
391 session->side = NC_CLIENT;
392
393 /* transport specific data */
394 session->ti_type = NC_TI_FD;
395 session->ti.fd.in = fdin;
396 session->ti.fd.out = fdout;
397
398 /* assign context (dicionary needed for handshake) */
399 if (!ctx) {
400 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100401 /* definitely should not happen, but be ready */
402 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
403 /* that's just it */
404 goto fail;
405 }
Michal Vasko086311b2016-01-08 09:53:11 +0100406 } else {
407 session->flags |= NC_SESSION_SHAREDCTX;
408 }
409 session->ctx = ctx;
410
411 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200412 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100413 goto fail;
414 }
415 session->status = NC_STATUS_RUNNING;
416
Michal Vaskoef578332016-01-25 13:20:09 +0100417 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100418 goto fail;
419 }
420
421 return session;
422
423fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100424 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100425 return NULL;
426}
427
428int
Michal Vaskof05562c2016-01-20 12:06:43 +0100429nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100430{
Michal Vasko0190bc32016-03-02 15:47:49 +0100431 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100432 struct addrinfo hints, *res_list, *res;
433 char port_s[6]; /* length of string representation of short int */
434
435 snprintf(port_s, 6, "%u", port);
436
437 /* Connect to a server */
438 memset(&hints, 0, sizeof hints);
439 hints.ai_family = AF_UNSPEC;
440 hints.ai_socktype = SOCK_STREAM;
441 hints.ai_protocol = IPPROTO_TCP;
442 i = getaddrinfo(host, port_s, &hints, &res_list);
443 if (i != 0) {
444 ERR("Unable to translate the host address (%s).", gai_strerror(i));
445 return -1;
446 }
447
448 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
449 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
450 if (sock == -1) {
451 /* socket was not created, try another resource */
452 i = errno;
453 goto errloop;
454 }
455
456 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
457 /* network connection failed, try another resource */
458 i = errno;
459 close(sock);
460 sock = -1;
461 goto errloop;
462 }
463
Michal Vasko0190bc32016-03-02 15:47:49 +0100464 /* make the socket non-blocking */
465 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
466 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100467 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100468 return -1;
469 }
470
Michal Vasko086311b2016-01-08 09:53:11 +0100471 /* we're done, network connection established */
472 break;
473errloop:
474 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
475 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
476 continue;
477 }
478
479 if (sock == -1) {
480 ERR("Unable to connect to %s:%s.", host, port_s);
481 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100482 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 +0100483 }
484 freeaddrinfo(res_list);
485
486 return sock;
487}
488
Michal Vasko086311b2016-01-08 09:53:11 +0100489static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100490get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100491{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100492 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100493 char *ptr;
494 const char *str_msgid;
495 uint64_t cur_msgid;
496 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100497 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100498 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
499
Michal Vasko62be1ce2016-03-03 13:24:52 +0100500 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100502 /* error */
503 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100504 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100505 /* timeout */
506 return NC_MSG_WOULDBLOCK;
507 }
508
509 /* try to get notification from the session's queue */
510 if (!msgid && session->notifs) {
511 cont = session->notifs;
512 session->notifs = cont->next;
513
Michal Vasko71ba2da2016-05-04 10:53:16 +0200514 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100515 free(cont);
516
Michal Vasko71ba2da2016-05-04 10:53:16 +0200517 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100518 }
519
520 /* try to get rpc-reply from the session's queue */
521 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200522 cont = session->replies;
523 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100524
Michal Vasko71ba2da2016-05-04 10:53:16 +0200525 xml = cont->msg;
526 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100527
Michal Vasko71ba2da2016-05-04 10:53:16 +0200528 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100529 }
530
Michal Vasko71ba2da2016-05-04 10:53:16 +0200531 if (!msgtype) {
532 /* read message from wire */
533 msgtype = nc_read_msg_poll(session, timeout, &xml);
534 }
Michal Vasko086311b2016-01-08 09:53:11 +0100535
536 /* we read rpc-reply, want a notif */
537 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100538 cont_ptr = &session->replies;
539 while (*cont_ptr) {
540 cont_ptr = &((*cont_ptr)->next);
541 }
542 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100543 if (!*cont_ptr) {
544 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200545 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100546 lyxml_free(session->ctx, xml);
547 return NC_MSG_ERROR;
548 }
Michal Vasko086311b2016-01-08 09:53:11 +0100549 (*cont_ptr)->msg = xml;
550 (*cont_ptr)->next = NULL;
551 }
552
553 /* we read notif, want a rpc-reply */
554 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100555 /* TODO check whether the session is even subscribed */
556 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100557 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100558 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100560 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100561 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100562
563 cont_ptr = &session->notifs;
564 while (*cont_ptr) {
565 cont_ptr = &((*cont_ptr)->next);
566 }
567 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100568 if (!cont_ptr) {
569 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200570 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100571 lyxml_free(session->ctx, xml);
572 return NC_MSG_ERROR;
573 }
Michal Vasko086311b2016-01-08 09:53:11 +0100574 (*cont_ptr)->msg = xml;
575 (*cont_ptr)->next = NULL;
576 }
577
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100578 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100579
580 switch (msgtype) {
581 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100582 if (!msgid) {
583 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100584 }
Michal Vasko086311b2016-01-08 09:53:11 +0100585 break;
586
587 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100588 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200589 /* check message-id */
590 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
591 if (!str_msgid) {
592 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
593 msgtype = NC_MSG_REPLY_ERR_MSGID;
594 } else {
595 cur_msgid = strtoul(str_msgid, &ptr, 10);
596 if (cur_msgid != msgid) {
597 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
598 session->id, str_msgid);
599 msgtype = NC_MSG_REPLY_ERR_MSGID;
600 }
601 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100602 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100603 }
Michal Vasko086311b2016-01-08 09:53:11 +0100604 break;
605
606 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100607 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100608 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200609 msgtype = NC_MSG_ERROR;
610 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100611
612 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100613 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100614 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200615 msgtype = NC_MSG_ERROR;
616 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100617
618 default:
619 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
620 * NC_MSG_NONE is not returned by nc_read_msg()
621 */
622 break;
623 }
624
625 return msgtype;
626}
627
628/* cannot strictly fail, but does not need to fill any error parameter at all */
629static void
630parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
631{
632 struct lyxml_elem *iter, *next, *info;
633
634 LY_TREE_FOR(xml->child, iter) {
635 if (!iter->ns) {
636 if (iter->content) {
637 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
638 } else {
639 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
640 }
641 continue;
642 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
643 if (iter->content) {
644 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
645 iter->name, iter->content, iter->ns->value);
646 } else {
647 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
648 }
649 continue;
650 }
651
652 if (!strcmp(iter->name, "error-type")) {
653 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
654 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
655 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
656 } else if (err->type) {
657 WRN("<rpc-error> <error-type> duplicated.");
658 } else {
659 err->type = lydict_insert(ctx, iter->content, 0);
660 }
661 } else if (!strcmp(iter->name, "error-tag")) {
662 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
663 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
664 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
665 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
666 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
667 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
668 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
669 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
670 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
671 && strcmp(iter->content, "malformed-message"))) {
672 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
673 } else if (err->tag) {
674 WRN("<rpc-error> <error-tag> duplicated.");
675 } else {
676 err->tag = lydict_insert(ctx, iter->content, 0);
677 }
678 } else if (!strcmp(iter->name, "error-severity")) {
679 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
680 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
681 } else if (err->severity) {
682 WRN("<rpc-error> <error-severity> duplicated.");
683 } else {
684 err->severity = lydict_insert(ctx, iter->content, 0);
685 }
686 } else if (!strcmp(iter->name, "error-app-tag")) {
687 if (err->apptag) {
688 WRN("<rpc-error> <error-app-tag> duplicated.");
689 } else {
690 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
691 }
692 } else if (!strcmp(iter->name, "error-path")) {
693 if (err->path) {
694 WRN("<rpc-error> <error-path> duplicated.");
695 } else {
696 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
697 }
698 } else if (!strcmp(iter->name, "error-message")) {
699 if (err->message) {
700 WRN("<rpc-error> <error-message> duplicated.");
701 } else {
702 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
703 if (!err->message_lang) {
704 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
705 }
706 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
707 }
708 } else if (!strcmp(iter->name, "error-info")) {
709 LY_TREE_FOR_SAFE(iter->child, next, info) {
710 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
711 if (!strcmp(info->name, "session-id")) {
712 if (err->sid) {
713 WRN("<rpc-error> <error-info> <session-id> duplicated.");
714 } else {
715 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
716 }
717 } else if (!strcmp(info->name, "bad-attr")) {
718 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100719 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
720 if (!err->attr) {
721 ERRMEM;
722 return;
723 }
Michal Vasko086311b2016-01-08 09:53:11 +0100724 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
725 } else if (!strcmp(info->name, "bad-element")) {
726 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100727 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
728 if (!err->elem) {
729 ERRMEM;
730 return;
731 }
Michal Vasko086311b2016-01-08 09:53:11 +0100732 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
733 } else if (!strcmp(info->name, "bad-namespace")) {
734 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100735 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
736 if (!err->ns) {
737 ERRMEM;
738 return;
739 }
Michal Vasko086311b2016-01-08 09:53:11 +0100740 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
741 } else {
742 if (info->content) {
743 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
744 info->name, info->content);
745 } else {
746 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
747 }
748 }
749 } else {
750 lyxml_unlink(ctx, info);
751 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100752 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
753 if (!err->other) {
754 ERRMEM;
755 return;
756 }
Michal Vasko086311b2016-01-08 09:53:11 +0100757 err->other[err->other_count - 1] = info;
758 }
759 }
760 } else {
761 if (iter->content) {
762 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
763 } else {
764 WRN("<rpc-error> unknown child \"%s\".", iter->name);
765 }
766 }
767 }
768}
769
770static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100771parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100772{
773 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100774 const struct lys_node *schema = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200775 struct lyd_node *data = NULL, *next, *elem;
Michal Vasko1a38c862016-01-15 15:50:07 +0100776 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100777 struct nc_reply_data *data_rpl;
778 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200779 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100780 int i;
781
782 if (!xml->child) {
783 ERR("An empty <rpc-reply>.");
784 return NULL;
785 }
786
787 /* rpc-error */
788 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
789 /* count and check elements */
790 i = 0;
791 LY_TREE_FOR(xml->child, iter) {
792 if (strcmp(iter->name, "rpc-error")) {
793 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
794 return NULL;
795 } else if (!iter->ns) {
796 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
797 return NULL;
798 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
799 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
800 return NULL;
801 }
802 ++i;
803 }
804
805 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100806 if (!error_rpl) {
807 ERRMEM;
808 return NULL;
809 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100810 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100811 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100812 if (!error_rpl->err) {
813 ERRMEM;
814 free(error_rpl);
815 return NULL;
816 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100817 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100818 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100819 reply = (struct nc_reply *)error_rpl;
820
821 i = 0;
822 LY_TREE_FOR(xml->child, iter) {
823 parse_rpc_error(ctx, iter, error_rpl->err + i);
824 ++i;
825 }
826
827 /* ok */
828 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
829 if (xml->child->next) {
830 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
831 return NULL;
832 }
833 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100834 if (!reply) {
835 ERRMEM;
836 return NULL;
837 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100838 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100839
840 /* some RPC output */
841 } else {
842 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200843 case NC_RPC_ACT_GENERIC:
844 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100845
846 if (rpc_gen->has_data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200847 data = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100848 } else {
Michal Vasko68b3f292016-09-16 12:00:32 +0200849 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100850 if (!data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200851 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100852 return NULL;
853 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200854 }
855 if (data->schema->nodetype == LYS_RPC) {
856 /* RPC */
Michal Vasko086311b2016-01-08 09:53:11 +0100857 schema = data->schema;
Michal Vasko90e8e692016-07-13 12:27:57 +0200858 } else {
859 /* action */
860 LY_TREE_DFS_BEGIN(data, next, elem) {
861 if (elem->schema->nodetype == LYS_ACTION) {
862 schema = elem->schema;
863 break;
864 }
865 LY_TREE_DFS_END(data, next, elem);
866 }
867 }
868
869 /* cleanup */
870 if (data != rpc_gen->content.data) {
Michal Vasko086311b2016-01-08 09:53:11 +0100871 lyd_free(data);
872 data = NULL;
873 }
874 if (!schema) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200875 /* only with action, if there is no action, it should not have gotten this far */
Michal Vasko9e036d52016-01-08 10:49:26 +0100876 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100877 return NULL;
878 }
879 break;
880
881 case NC_RPC_GETCONFIG:
882 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100883 if (!xml->child->child) {
884 /* we did not receive any data */
885 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100886 if (!data_rpl) {
887 ERRMEM;
888 return NULL;
889 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100890 data_rpl->type = NC_RPL_DATA;
891 data_rpl->data = NULL;
892 return (struct nc_reply *)data_rpl;
893 }
894
Michal Vasko086311b2016-01-08 09:53:11 +0100895 /* special treatment */
896 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100897 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100898 if (!data) {
899 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
900 return NULL;
901 }
902 break;
903
904 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100905 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100906 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100907 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100908 return NULL;
909 }
910 break;
911
912 case NC_RPC_EDIT:
913 case NC_RPC_COPY:
914 case NC_RPC_DELETE:
915 case NC_RPC_LOCK:
916 case NC_RPC_UNLOCK:
917 case NC_RPC_KILL:
918 case NC_RPC_COMMIT:
919 case NC_RPC_DISCARD:
920 case NC_RPC_CANCEL:
921 case NC_RPC_VALIDATE:
922 case NC_RPC_SUBSCRIBE:
923 /* there is no output defined */
924 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
925 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100926 default:
927 ERRINT;
928 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100929 }
930
931 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100932 if (!data_rpl) {
933 ERRMEM;
934 return NULL;
935 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100936 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100937 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +0200938 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
939 schema, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100940 } else {
941 /* <get>, <get-config> */
942 data_rpl->data = data;
943 }
944 if (!data_rpl->data) {
945 ERR("Failed to parse <rpc-reply>.");
946 free(data_rpl);
947 return NULL;
948 }
949 reply = (struct nc_reply *)data_rpl;
950 }
951
952 return reply;
953}
954
Radek Krejci53691be2016-02-22 13:58:37 +0100955#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100956
Michal Vasko3031aae2016-01-27 16:07:18 +0100957int
958nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
959{
960 int sock;
961
Michal Vasko45e53ae2016-04-07 11:46:03 +0200962 if (!address) {
963 ERRARG("address");
964 return -1;
965 } else if (!port) {
966 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100967 return -1;
968 }
969
970 sock = nc_sock_listen(address, port);
971 if (sock == -1) {
972 return -1;
973 }
974
975 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100976 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
977 if (!client_opts.ch_binds) {
978 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100979 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100980 return -1;
981 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100982
983 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100984 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
985 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100986 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100987 return -1;
988 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100989 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
990 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
991 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
992
993 return 0;
994}
995
996int
997nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
998{
999 uint32_t i;
1000 int ret = -1;
1001
1002 if (!address && !port && !ti) {
1003 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1004 close(client_opts.ch_binds[i].sock);
1005 free((char *)client_opts.ch_binds[i].address);
1006
1007 ret = 0;
1008 }
1009 free(client_opts.ch_binds);
1010 client_opts.ch_binds = NULL;
1011 client_opts.ch_bind_count = 0;
1012 } else {
1013 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1014 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1015 && (!port || (client_opts.ch_binds[i].port == port))
1016 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
1017 close(client_opts.ch_binds[i].sock);
1018 free((char *)client_opts.ch_binds[i].address);
1019
1020 --client_opts.ch_bind_count;
1021 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1022
1023 ret = 0;
1024 }
1025 }
1026 }
1027
1028 return ret;
1029}
1030
1031API int
1032nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1033{
1034 int sock;
1035 char *host = NULL;
1036 uint16_t port, idx;
1037
Michal Vasko45e53ae2016-04-07 11:46:03 +02001038 if (!client_opts.ch_binds) {
1039 ERRINIT;
1040 return -1;
1041 } else if (!session) {
1042 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001043 return -1;
1044 }
1045
1046 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1047
Michal Vasko50456e82016-02-02 12:16:08 +01001048 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001049 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001050 return sock;
1051 }
1052
Radek Krejci53691be2016-02-22 13:58:37 +01001053#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001054 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001055 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001056 } else
1057#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001058#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001059 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001060 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001061 } else
1062#endif
1063 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001064 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001065 *session = NULL;
1066 }
1067
1068 free(host);
1069
1070 if (!(*session)) {
1071 return -1;
1072 }
1073
1074 return 1;
1075}
1076
Radek Krejci53691be2016-02-22 13:58:37 +01001077#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001078
Michal Vaskobdfb5242016-05-24 09:11:01 +02001079API const char **
1080nc_session_get_cpblts(const struct nc_session *session)
1081{
1082 if (!session) {
1083 ERRARG("session");
1084 return NULL;
1085 }
1086
1087 return session->cpblts;
1088}
1089
1090API const char *
1091nc_session_cpblt(const struct nc_session *session, const char *capab)
1092{
1093 int i, len;
1094
1095 if (!session) {
1096 ERRARG("session");
1097 return NULL;
1098 } else if (!capab) {
1099 ERRARG("capab");
1100 return NULL;
1101 }
1102
1103 len = strlen(capab);
1104 for (i = 0; session->cpblts[i]; ++i) {
1105 if (!strncmp(session->cpblts[i], capab, len)) {
1106 return session->cpblts[i];
1107 }
1108 }
1109
1110 return NULL;
1111}
1112
Michal Vasko9cd26a82016-05-31 08:58:48 +02001113API int
1114nc_session_ntf_thread_running(const struct nc_session *session)
1115{
1116 if (!session) {
1117 ERRARG("session");
1118 return 0;
1119 }
1120
1121 return session->ntf_tid ? 1 : 0;
1122}
1123
Michal Vaskob7558c52016-02-26 15:04:19 +01001124API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001125nc_client_init(void)
1126{
1127 nc_init();
1128}
1129
1130API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001131nc_client_destroy(void)
1132{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001133 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001134#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1135 nc_client_ch_del_bind(NULL, 0, 0);
1136#endif
1137#ifdef NC_ENABLED_SSH
1138 nc_client_ssh_destroy_opts();
1139#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001140#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001141 nc_client_tls_destroy_opts();
1142#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001143 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001144}
1145
Michal Vasko086311b2016-01-08 09:53:11 +01001146API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001147nc_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 +01001148{
1149 struct lyxml_elem *xml;
1150 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1151
Michal Vasko45e53ae2016-04-07 11:46:03 +02001152 if (!session) {
1153 ERRARG("session");
1154 return NC_MSG_ERROR;
1155 } else if (!rpc) {
1156 ERRARG("rpc");
1157 return NC_MSG_ERROR;
1158 } else if (!reply) {
1159 ERRARG("reply");
1160 return NC_MSG_ERROR;
1161 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1162 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001163 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001164 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001165 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001166 return NC_MSG_ERROR;
1167 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001168 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001169 *reply = NULL;
1170
1171 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001172
Michal Vasko71ba2da2016-05-04 10:53:16 +02001173 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001174 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001175 lyxml_free(session->ctx, xml);
1176 if (!(*reply)) {
1177 return NC_MSG_ERROR;
1178 }
1179 }
1180
1181 return msgtype;
1182}
1183
1184API NC_MSG_TYPE
1185nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1186{
1187 struct lyxml_elem *xml, *ev_time;
1188 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1189
Michal Vasko45e53ae2016-04-07 11:46:03 +02001190 if (!session) {
1191 ERRARG("session");
1192 return NC_MSG_ERROR;
1193 } else if (!notif) {
1194 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001195 return NC_MSG_ERROR;
1196 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001197 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001198 return NC_MSG_ERROR;
1199 }
1200
1201 msgtype = get_msg(session, timeout, 0, &xml);
1202
1203 if (msgtype == NC_MSG_NOTIF) {
1204 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001205 if (!*notif) {
1206 ERRMEM;
1207 lyxml_free(session->ctx, xml);
1208 return NC_MSG_ERROR;
1209 }
Michal Vasko086311b2016-01-08 09:53:11 +01001210
1211 /* eventTime */
1212 LY_TREE_FOR(xml->child, ev_time) {
1213 if (!strcmp(ev_time->name, "eventTime")) {
1214 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1215 /* lyd_parse does not know this element */
1216 lyxml_free(session->ctx, ev_time);
1217 break;
1218 }
1219 }
1220 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001221 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001222 goto fail;
1223 }
1224
1225 /* notification body */
Michal Vasko68b3f292016-09-16 12:00:32 +02001226 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001227 lyxml_free(session->ctx, xml);
1228 xml = NULL;
1229 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001230 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001231 goto fail;
1232 }
1233 }
1234
1235 return msgtype;
1236
1237fail:
1238 lydict_remove(session->ctx, (*notif)->datetime);
1239 lyd_free((*notif)->tree);
1240 free(*notif);
1241 *notif = NULL;
1242 lyxml_free(session->ctx, xml);
1243
1244 return NC_MSG_ERROR;
1245}
1246
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001247static void *
1248nc_recv_notif_thread(void *arg)
1249{
1250 struct nc_ntf_thread_arg *ntarg;
1251 struct nc_session *session;
1252 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1253 struct nc_notif *notif;
1254 NC_MSG_TYPE msgtype;
1255
1256 ntarg = (struct nc_ntf_thread_arg *)arg;
1257 session = ntarg->session;
1258 notif_clb = ntarg->notif_clb;
1259 free(ntarg);
1260
1261 while (session->ntf_tid) {
1262 msgtype = nc_recv_notif(session, 0, &notif);
1263 if (msgtype == NC_MSG_NOTIF) {
1264 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001265 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1266 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1267 nc_notif_free(notif);
1268 break;
1269 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001270 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001271 } else if (msgtype == NC_MSG_ERROR) {
1272 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001273 }
1274
1275 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1276 }
1277
Michal Vasko0651c902016-05-19 15:55:42 +02001278 VRB("Session %u: notification thread exit.", session->id);
1279 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001280 return NULL;
1281}
1282
1283API int
1284nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1285{
1286 struct nc_ntf_thread_arg *ntarg;
1287 int ret;
1288
Michal Vasko45e53ae2016-04-07 11:46:03 +02001289 if (!session) {
1290 ERRARG("session");
1291 return -1;
1292 } else if (!notif_clb) {
1293 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001294 return -1;
1295 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1296 ERR("Session %u: invalid session to receive Notifications.", session->id);
1297 return -1;
1298 } else if (session->ntf_tid) {
1299 ERR("Session %u: separate notification thread is already running.", session->id);
1300 return -1;
1301 }
1302
1303 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001304 if (!ntarg) {
1305 ERRMEM;
1306 return -1;
1307 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001308 ntarg->session = session;
1309 ntarg->notif_clb = notif_clb;
1310
1311 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1312 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001313 if (!session->ntf_tid) {
1314 ERRMEM;
1315 free(ntarg);
1316 return -1;
1317 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001318
1319 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1320 if (ret) {
1321 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1322 free(ntarg);
1323 free((pthread_t *)session->ntf_tid);
1324 session->ntf_tid = NULL;
1325 return -1;
1326 }
1327
1328 return 0;
1329}
1330
Michal Vasko086311b2016-01-08 09:53:11 +01001331API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001332nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001333{
1334 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001335 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001336 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001337 struct nc_rpc_getconfig *rpc_gc;
1338 struct nc_rpc_edit *rpc_e;
1339 struct nc_rpc_copy *rpc_cp;
1340 struct nc_rpc_delete *rpc_del;
1341 struct nc_rpc_lock *rpc_lock;
1342 struct nc_rpc_get *rpc_g;
1343 struct nc_rpc_kill *rpc_k;
1344 struct nc_rpc_commit *rpc_com;
1345 struct nc_rpc_cancel *rpc_can;
1346 struct nc_rpc_validate *rpc_val;
1347 struct nc_rpc_getschema *rpc_gs;
1348 struct nc_rpc_subscribe *rpc_sub;
1349 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001350 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001351 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001352 uint64_t cur_msgid;
1353
Michal Vasko45e53ae2016-04-07 11:46:03 +02001354 if (!session) {
1355 ERRARG("session");
1356 return NC_MSG_ERROR;
1357 } else if (!rpc) {
1358 ERRARG("rpc");
1359 return NC_MSG_ERROR;
1360 } else if (!msgid) {
1361 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001362 return NC_MSG_ERROR;
1363 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001364 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001365 return NC_MSG_ERROR;
1366 }
1367
Michal Vasko90e8e692016-07-13 12:27:57 +02001368 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001369 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1370 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001371 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001372 return NC_MSG_ERROR;
1373 }
1374 }
1375
1376 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001377 case NC_RPC_ACT_GENERIC:
1378 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001379
1380 if (rpc_gen->has_data) {
1381 data = rpc_gen->content.data;
1382 } else {
Michal Vasko68b3f292016-09-16 12:00:32 +02001383 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 +01001384 }
1385 break;
1386
1387 case NC_RPC_GETCONFIG:
1388 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1389
1390 data = lyd_new(NULL, ietfnc, "get-config");
1391 node = lyd_new(data, ietfnc, "source");
1392 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1393 if (!node) {
1394 lyd_free(data);
1395 return NC_MSG_ERROR;
1396 }
1397 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001398 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001399 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001400 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001401 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001402 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001403 lyd_insert_attr(node, NULL, "type", "xpath");
1404 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001405 }
1406 if (!node) {
1407 lyd_free(data);
1408 return NC_MSG_ERROR;
1409 }
1410 }
1411
1412 if (rpc_gc->wd_mode) {
1413 if (!ietfncwd) {
1414 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1415 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001416 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001417 return NC_MSG_ERROR;
1418 }
1419 }
1420 switch (rpc_gc->wd_mode) {
1421 case NC_WD_UNKNOWN:
1422 /* cannot get here */
1423 break;
1424 case NC_WD_ALL:
1425 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1426 break;
1427 case NC_WD_ALL_TAG:
1428 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1429 break;
1430 case NC_WD_TRIM:
1431 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1432 break;
1433 case NC_WD_EXPLICIT:
1434 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1435 break;
1436 }
1437 if (!node) {
1438 lyd_free(data);
1439 return NC_MSG_ERROR;
1440 }
1441 }
1442 break;
1443
1444 case NC_RPC_EDIT:
1445 rpc_e = (struct nc_rpc_edit *)rpc;
1446
1447 data = lyd_new(NULL, ietfnc, "edit-config");
1448 node = lyd_new(data, ietfnc, "target");
1449 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1450 if (!node) {
1451 lyd_free(data);
1452 return NC_MSG_ERROR;
1453 }
1454
1455 if (rpc_e->default_op) {
1456 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1457 if (!node) {
1458 lyd_free(data);
1459 return NC_MSG_ERROR;
1460 }
1461 }
1462
1463 if (rpc_e->test_opt) {
1464 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1465 if (!node) {
1466 lyd_free(data);
1467 return NC_MSG_ERROR;
1468 }
1469 }
1470
1471 if (rpc_e->error_opt) {
1472 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1473 if (!node) {
1474 lyd_free(data);
1475 return NC_MSG_ERROR;
1476 }
1477 }
1478
Michal Vasko7793bc62016-09-16 11:58:41 +02001479 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001480 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001481 } else {
1482 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1483 }
1484 if (!node) {
1485 lyd_free(data);
1486 return NC_MSG_ERROR;
1487 }
1488 break;
1489
1490 case NC_RPC_COPY:
1491 rpc_cp = (struct nc_rpc_copy *)rpc;
1492
1493 data = lyd_new(NULL, ietfnc, "copy-config");
1494 node = lyd_new(data, ietfnc, "target");
1495 if (rpc_cp->url_trg) {
1496 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1497 } else {
1498 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1499 }
1500 if (!node) {
1501 lyd_free(data);
1502 return NC_MSG_ERROR;
1503 }
1504
1505 node = lyd_new(data, ietfnc, "source");
1506 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001507 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001508 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001509 } else {
1510 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1511 }
1512 } else {
1513 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1514 }
1515 if (!node) {
1516 lyd_free(data);
1517 return NC_MSG_ERROR;
1518 }
1519
1520 if (rpc_cp->wd_mode) {
1521 if (!ietfncwd) {
1522 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1523 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001524 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001525 return NC_MSG_ERROR;
1526 }
1527 }
1528 switch (rpc_cp->wd_mode) {
1529 case NC_WD_UNKNOWN:
1530 /* cannot get here */
1531 break;
1532 case NC_WD_ALL:
1533 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1534 break;
1535 case NC_WD_ALL_TAG:
1536 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1537 break;
1538 case NC_WD_TRIM:
1539 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1540 break;
1541 case NC_WD_EXPLICIT:
1542 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1543 break;
1544 }
1545 if (!node) {
1546 lyd_free(data);
1547 return NC_MSG_ERROR;
1548 }
1549 }
1550 break;
1551
1552 case NC_RPC_DELETE:
1553 rpc_del = (struct nc_rpc_delete *)rpc;
1554
1555 data = lyd_new(NULL, ietfnc, "delete-config");
1556 node = lyd_new(data, ietfnc, "target");
1557 if (rpc_del->url) {
1558 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1559 } else {
1560 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1561 }
1562 if (!node) {
1563 lyd_free(data);
1564 return NC_MSG_ERROR;
1565 }
1566 break;
1567
1568 case NC_RPC_LOCK:
1569 rpc_lock = (struct nc_rpc_lock *)rpc;
1570
1571 data = lyd_new(NULL, ietfnc, "lock");
1572 node = lyd_new(data, ietfnc, "target");
1573 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1574 if (!node) {
1575 lyd_free(data);
1576 return NC_MSG_ERROR;
1577 }
1578 break;
1579
1580 case NC_RPC_UNLOCK:
1581 rpc_lock = (struct nc_rpc_lock *)rpc;
1582
1583 data = lyd_new(NULL, ietfnc, "unlock");
1584 node = lyd_new(data, ietfnc, "target");
1585 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1586 if (!node) {
1587 lyd_free(data);
1588 return NC_MSG_ERROR;
1589 }
1590 break;
1591
1592 case NC_RPC_GET:
1593 rpc_g = (struct nc_rpc_get *)rpc;
1594
1595 data = lyd_new(NULL, ietfnc, "get");
1596 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001597 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001598 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001599 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001600 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001601 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001602 lyd_insert_attr(node, NULL, "type", "xpath");
1603 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001604 }
1605 if (!node) {
1606 lyd_free(data);
1607 return NC_MSG_ERROR;
1608 }
1609 }
1610
1611 if (rpc_g->wd_mode) {
1612 if (!ietfncwd) {
1613 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1614 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001615 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001616 return NC_MSG_ERROR;
1617 }
1618 }
1619 switch (rpc_g->wd_mode) {
1620 case NC_WD_UNKNOWN:
1621 /* cannot get here */
1622 break;
1623 case NC_WD_ALL:
1624 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1625 break;
1626 case NC_WD_ALL_TAG:
1627 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1628 break;
1629 case NC_WD_TRIM:
1630 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1631 break;
1632 case NC_WD_EXPLICIT:
1633 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1634 break;
1635 }
1636 if (!node) {
1637 lyd_free(data);
1638 return NC_MSG_ERROR;
1639 }
1640 }
1641 break;
1642
1643 case NC_RPC_KILL:
1644 rpc_k = (struct nc_rpc_kill *)rpc;
1645
1646 data = lyd_new(NULL, ietfnc, "kill-session");
1647 sprintf(str, "%u", rpc_k->sid);
1648 lyd_new_leaf(data, ietfnc, "session-id", str);
1649 break;
1650
1651 case NC_RPC_COMMIT:
1652 rpc_com = (struct nc_rpc_commit *)rpc;
1653
1654 data = lyd_new(NULL, ietfnc, "commit");
1655 if (rpc_com->confirmed) {
1656 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1657 }
1658
1659 if (rpc_com->confirm_timeout) {
1660 sprintf(str, "%u", rpc_com->confirm_timeout);
1661 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1662 }
1663
1664 if (rpc_com->persist) {
1665 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1666 if (!node) {
1667 lyd_free(data);
1668 return NC_MSG_ERROR;
1669 }
1670 }
1671
1672 if (rpc_com->persist_id) {
1673 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1674 if (!node) {
1675 lyd_free(data);
1676 return NC_MSG_ERROR;
1677 }
1678 }
1679 break;
1680
1681 case NC_RPC_DISCARD:
1682 data = lyd_new(NULL, ietfnc, "discard-changes");
1683 break;
1684
1685 case NC_RPC_CANCEL:
1686 rpc_can = (struct nc_rpc_cancel *)rpc;
1687
1688 data = lyd_new(NULL, ietfnc, "cancel-commit");
1689 if (rpc_can->persist_id) {
1690 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1691 if (!node) {
1692 lyd_free(data);
1693 return NC_MSG_ERROR;
1694 }
1695 }
1696 break;
1697
1698 case NC_RPC_VALIDATE:
1699 rpc_val = (struct nc_rpc_validate *)rpc;
1700
1701 data = lyd_new(NULL, ietfnc, "validate");
1702 node = lyd_new(data, ietfnc, "source");
1703 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001704 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001705 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001706 } else {
1707 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1708 }
1709 } else {
1710 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1711 }
1712 if (!node) {
1713 lyd_free(data);
1714 return NC_MSG_ERROR;
1715 }
1716 break;
1717
1718 case NC_RPC_GETSCHEMA:
1719 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1720 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001721 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001722 return NC_MSG_ERROR;
1723 }
1724
1725 rpc_gs = (struct nc_rpc_getschema *)rpc;
1726
1727 data = lyd_new(NULL, ietfncmon, "get-schema");
1728 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1729 if (!node) {
1730 lyd_free(data);
1731 return NC_MSG_ERROR;
1732 }
1733 if (rpc_gs->version) {
1734 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1735 if (!node) {
1736 lyd_free(data);
1737 return NC_MSG_ERROR;
1738 }
1739 }
1740 if (rpc_gs->format) {
1741 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1742 if (!node) {
1743 lyd_free(data);
1744 return NC_MSG_ERROR;
1745 }
1746 }
1747 break;
1748
1749 case NC_RPC_SUBSCRIBE:
1750 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1751 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001752 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001753 return NC_MSG_ERROR;
1754 }
1755
1756 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1757
1758 data = lyd_new(NULL, notifs, "create-subscription");
1759 if (rpc_sub->stream) {
1760 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1761 if (!node) {
1762 lyd_free(data);
1763 return NC_MSG_ERROR;
1764 }
1765 }
1766
1767 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001768 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001769 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001770 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001771 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001772 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001773 lyd_insert_attr(node, NULL, "type", "xpath");
1774 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001775 }
1776 if (!node) {
1777 lyd_free(data);
1778 return NC_MSG_ERROR;
1779 }
1780 }
1781
1782 if (rpc_sub->start) {
1783 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1784 if (!node) {
1785 lyd_free(data);
1786 return NC_MSG_ERROR;
1787 }
1788 }
1789
1790 if (rpc_sub->stop) {
1791 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1792 if (!node) {
1793 lyd_free(data);
1794 return NC_MSG_ERROR;
1795 }
1796 }
1797 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001798 default:
1799 ERRINT;
1800 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001801 }
1802
Michal Vasko49a0cf92016-09-14 09:15:32 +02001803 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT, NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001804 lyd_free(data);
1805 return NC_MSG_ERROR;
1806 }
1807
Michal Vasko62be1ce2016-03-03 13:24:52 +01001808 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001809 if (ret == -1) {
1810 /* error */
1811 r = NC_MSG_ERROR;
1812 } else if (!ret) {
1813 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001814 r = NC_MSG_WOULDBLOCK;
1815 } else {
1816 /* send RPC, store its message ID */
1817 r = nc_send_msg(session, data);
1818 cur_msgid = session->msgid;
1819 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001820 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001821
1822 lyd_free(data);
1823
1824 if (r != NC_MSG_RPC) {
1825 return r;
1826 }
1827
1828 *msgid = cur_msgid;
1829 return NC_MSG_RPC;
1830}