blob: 5286e722d8613bf409fddd3b64893a654f7d94d6 [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:
278 ERRINT;
279 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200280 }
Michal Vasko086311b2016-01-08 09:53:11 +0100281 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100282 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200283 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100284
Michal Vasko086311b2016-01-08 09:53:11 +0100285 return model_data;
286}
287
Michal Vaskoef578332016-01-25 13:20:09 +0100288/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100289int
290nc_ctx_check_and_fill(struct nc_session *session)
291{
Michal Vaskoef578332016-01-25 13:20:09 +0100292 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100293 ly_module_clb old_clb = NULL;
294 void *old_data = NULL;
295
296 assert(session->cpblts && session->ctx);
297
298 /* check if get-schema is supported */
299 for (i = 0; session->cpblts[i]; ++i) {
300 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
301 get_schema_support = 1;
302 break;
303 }
304 }
305
306 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
307 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
308 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
309 /* set module retrieval using <get-schema> */
310 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100311 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100312 } else {
313 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
314 }
315 }
316
317 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100318 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100319 if (old_clb) {
320 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
321 }
Michal Vaskoef578332016-01-25 13:20:09 +0100322 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100323 }
324
325 /* load all other models */
326 for (i = 0; session->cpblts[i]; ++i) {
327 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
328 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
329 continue;
330 }
331
Michal Vaskoef578332016-01-25 13:20:09 +0100332 r = ctx_check_and_load_model(session, session->cpblts[i]);
333 if (r == -1) {
334 ret = -1;
335 break;
336 }
337
338 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
339 * if it was using get-schema */
340 if (r == 1) {
341 if (get_schema_support) {
342 VRB("Trying to load the schema from a different source.");
343 /* works even if old_clb is NULL */
344 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
345 r = ctx_check_and_load_model(session, session->cpblts[i]);
346 }
347
348 /* fail again (or no other way to try), too bad */
349 if (r) {
350 ret = 1;
351 }
352
353 /* set get-schema callback back */
354 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
355 }
Michal Vasko086311b2016-01-08 09:53:11 +0100356 }
357
358 if (old_clb) {
359 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
360 }
Michal Vaskoef578332016-01-25 13:20:09 +0100361 if (ret == 1) {
362 WRN("Some models failed to be loaded, any data from these models will be ignored.");
363 }
364 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100365}
366
367API struct nc_session *
368nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
369{
Michal Vaskod083db62016-01-19 10:31:29 +0100370 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100371
Michal Vasko45e53ae2016-04-07 11:46:03 +0200372 if (fdin < 0) {
373 ERRARG("fdin");
374 return NULL;
375 } else if (fdout < 0) {
376 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100377 return NULL;
378 }
379
380 /* prepare session structure */
381 session = calloc(1, sizeof *session);
382 if (!session) {
383 ERRMEM;
384 return NULL;
385 }
386 session->status = NC_STATUS_STARTING;
387 session->side = NC_CLIENT;
388
389 /* transport specific data */
390 session->ti_type = NC_TI_FD;
391 session->ti.fd.in = fdin;
392 session->ti.fd.out = fdout;
393
394 /* assign context (dicionary needed for handshake) */
395 if (!ctx) {
396 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100397 /* definitely should not happen, but be ready */
398 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
399 /* that's just it */
400 goto fail;
401 }
Michal Vasko086311b2016-01-08 09:53:11 +0100402 } else {
403 session->flags |= NC_SESSION_SHAREDCTX;
404 }
405 session->ctx = ctx;
406
407 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200408 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100409 goto fail;
410 }
411 session->status = NC_STATUS_RUNNING;
412
Michal Vaskoef578332016-01-25 13:20:09 +0100413 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100414 goto fail;
415 }
416
417 return session;
418
419fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100420 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100421 return NULL;
422}
423
424int
Michal Vaskof05562c2016-01-20 12:06:43 +0100425nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100426{
Michal Vasko0190bc32016-03-02 15:47:49 +0100427 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100428 struct addrinfo hints, *res_list, *res;
429 char port_s[6]; /* length of string representation of short int */
430
431 snprintf(port_s, 6, "%u", port);
432
433 /* Connect to a server */
434 memset(&hints, 0, sizeof hints);
435 hints.ai_family = AF_UNSPEC;
436 hints.ai_socktype = SOCK_STREAM;
437 hints.ai_protocol = IPPROTO_TCP;
438 i = getaddrinfo(host, port_s, &hints, &res_list);
439 if (i != 0) {
440 ERR("Unable to translate the host address (%s).", gai_strerror(i));
441 return -1;
442 }
443
444 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
445 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
446 if (sock == -1) {
447 /* socket was not created, try another resource */
448 i = errno;
449 goto errloop;
450 }
451
452 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
453 /* network connection failed, try another resource */
454 i = errno;
455 close(sock);
456 sock = -1;
457 goto errloop;
458 }
459
Michal Vasko0190bc32016-03-02 15:47:49 +0100460 /* make the socket non-blocking */
461 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
462 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100463 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100464 return -1;
465 }
466
Michal Vasko086311b2016-01-08 09:53:11 +0100467 /* we're done, network connection established */
468 break;
469errloop:
470 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
471 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
472 continue;
473 }
474
475 if (sock == -1) {
476 ERR("Unable to connect to %s:%s.", host, port_s);
477 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100478 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 +0100479 }
480 freeaddrinfo(res_list);
481
482 return sock;
483}
484
Michal Vasko086311b2016-01-08 09:53:11 +0100485static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100486get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100487{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100488 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100489 char *ptr;
490 const char *str_msgid;
491 uint64_t cur_msgid;
492 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100493 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100494 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
495
Michal Vasko62be1ce2016-03-03 13:24:52 +0100496 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100497 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100498 /* error */
499 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100500 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100501 /* timeout */
502 return NC_MSG_WOULDBLOCK;
503 }
504
505 /* try to get notification from the session's queue */
506 if (!msgid && session->notifs) {
507 cont = session->notifs;
508 session->notifs = cont->next;
509
Michal Vasko71ba2da2016-05-04 10:53:16 +0200510 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100511 free(cont);
512
Michal Vasko71ba2da2016-05-04 10:53:16 +0200513 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100514 }
515
516 /* try to get rpc-reply from the session's queue */
517 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200518 cont = session->replies;
519 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100520
Michal Vasko71ba2da2016-05-04 10:53:16 +0200521 xml = cont->msg;
522 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100523
Michal Vasko71ba2da2016-05-04 10:53:16 +0200524 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100525 }
526
Michal Vasko71ba2da2016-05-04 10:53:16 +0200527 if (!msgtype) {
528 /* read message from wire */
529 msgtype = nc_read_msg_poll(session, timeout, &xml);
530 }
Michal Vasko086311b2016-01-08 09:53:11 +0100531
532 /* we read rpc-reply, want a notif */
533 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100534 cont_ptr = &session->replies;
535 while (*cont_ptr) {
536 cont_ptr = &((*cont_ptr)->next);
537 }
538 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100539 if (!*cont_ptr) {
540 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200541 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100542 lyxml_free(session->ctx, xml);
543 return NC_MSG_ERROR;
544 }
Michal Vasko086311b2016-01-08 09:53:11 +0100545 (*cont_ptr)->msg = xml;
546 (*cont_ptr)->next = NULL;
547 }
548
549 /* we read notif, want a rpc-reply */
550 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100551 /* TODO check whether the session is even subscribed */
552 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100553 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100554 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100555 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100556 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100557 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100558
559 cont_ptr = &session->notifs;
560 while (*cont_ptr) {
561 cont_ptr = &((*cont_ptr)->next);
562 }
563 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100564 if (!cont_ptr) {
565 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200566 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100567 lyxml_free(session->ctx, xml);
568 return NC_MSG_ERROR;
569 }
Michal Vasko086311b2016-01-08 09:53:11 +0100570 (*cont_ptr)->msg = xml;
571 (*cont_ptr)->next = NULL;
572 }
573
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100574 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100575
576 switch (msgtype) {
577 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100578 if (!msgid) {
579 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100580 }
Michal Vasko086311b2016-01-08 09:53:11 +0100581 break;
582
583 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100584 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200585 /* check message-id */
586 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
587 if (!str_msgid) {
588 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
589 msgtype = NC_MSG_REPLY_ERR_MSGID;
590 } else {
591 cur_msgid = strtoul(str_msgid, &ptr, 10);
592 if (cur_msgid != msgid) {
593 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
594 session->id, str_msgid);
595 msgtype = NC_MSG_REPLY_ERR_MSGID;
596 }
597 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100598 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100599 }
Michal Vasko086311b2016-01-08 09:53:11 +0100600 break;
601
602 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100603 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100604 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200605 msgtype = NC_MSG_ERROR;
606 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100607
608 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100609 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100610 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200611 msgtype = NC_MSG_ERROR;
612 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100613
614 default:
615 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
616 * NC_MSG_NONE is not returned by nc_read_msg()
617 */
618 break;
619 }
620
621 return msgtype;
622}
623
624/* cannot strictly fail, but does not need to fill any error parameter at all */
625static void
626parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
627{
628 struct lyxml_elem *iter, *next, *info;
629
630 LY_TREE_FOR(xml->child, iter) {
631 if (!iter->ns) {
632 if (iter->content) {
633 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
634 } else {
635 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
636 }
637 continue;
638 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
639 if (iter->content) {
640 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
641 iter->name, iter->content, iter->ns->value);
642 } else {
643 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
644 }
645 continue;
646 }
647
648 if (!strcmp(iter->name, "error-type")) {
649 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
650 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
651 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
652 } else if (err->type) {
653 WRN("<rpc-error> <error-type> duplicated.");
654 } else {
655 err->type = lydict_insert(ctx, iter->content, 0);
656 }
657 } else if (!strcmp(iter->name, "error-tag")) {
658 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
659 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
660 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
661 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
662 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
663 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
664 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
665 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
666 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
667 && strcmp(iter->content, "malformed-message"))) {
668 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
669 } else if (err->tag) {
670 WRN("<rpc-error> <error-tag> duplicated.");
671 } else {
672 err->tag = lydict_insert(ctx, iter->content, 0);
673 }
674 } else if (!strcmp(iter->name, "error-severity")) {
675 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
676 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
677 } else if (err->severity) {
678 WRN("<rpc-error> <error-severity> duplicated.");
679 } else {
680 err->severity = lydict_insert(ctx, iter->content, 0);
681 }
682 } else if (!strcmp(iter->name, "error-app-tag")) {
683 if (err->apptag) {
684 WRN("<rpc-error> <error-app-tag> duplicated.");
685 } else {
686 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
687 }
688 } else if (!strcmp(iter->name, "error-path")) {
689 if (err->path) {
690 WRN("<rpc-error> <error-path> duplicated.");
691 } else {
692 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
693 }
694 } else if (!strcmp(iter->name, "error-message")) {
695 if (err->message) {
696 WRN("<rpc-error> <error-message> duplicated.");
697 } else {
698 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
699 if (!err->message_lang) {
700 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
701 }
702 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
703 }
704 } else if (!strcmp(iter->name, "error-info")) {
705 LY_TREE_FOR_SAFE(iter->child, next, info) {
706 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
707 if (!strcmp(info->name, "session-id")) {
708 if (err->sid) {
709 WRN("<rpc-error> <error-info> <session-id> duplicated.");
710 } else {
711 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
712 }
713 } else if (!strcmp(info->name, "bad-attr")) {
714 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100715 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
716 if (!err->attr) {
717 ERRMEM;
718 return;
719 }
Michal Vasko086311b2016-01-08 09:53:11 +0100720 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
721 } else if (!strcmp(info->name, "bad-element")) {
722 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100723 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
724 if (!err->elem) {
725 ERRMEM;
726 return;
727 }
Michal Vasko086311b2016-01-08 09:53:11 +0100728 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
729 } else if (!strcmp(info->name, "bad-namespace")) {
730 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100731 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
732 if (!err->ns) {
733 ERRMEM;
734 return;
735 }
Michal Vasko086311b2016-01-08 09:53:11 +0100736 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
737 } else {
738 if (info->content) {
739 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
740 info->name, info->content);
741 } else {
742 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
743 }
744 }
745 } else {
746 lyxml_unlink(ctx, info);
747 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100748 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
749 if (!err->other) {
750 ERRMEM;
751 return;
752 }
Michal Vasko086311b2016-01-08 09:53:11 +0100753 err->other[err->other_count - 1] = info;
754 }
755 }
756 } else {
757 if (iter->content) {
758 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
759 } else {
760 WRN("<rpc-error> unknown child \"%s\".", iter->name);
761 }
762 }
763 }
764}
765
766static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100767parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100768{
769 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100770 const struct lys_node *schema = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200771 struct lyd_node *data = NULL, *next, *elem;
Michal Vasko1a38c862016-01-15 15:50:07 +0100772 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100773 struct nc_reply_data *data_rpl;
774 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200775 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100776 int i;
777
778 if (!xml->child) {
779 ERR("An empty <rpc-reply>.");
780 return NULL;
781 }
782
783 /* rpc-error */
784 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
785 /* count and check elements */
786 i = 0;
787 LY_TREE_FOR(xml->child, iter) {
788 if (strcmp(iter->name, "rpc-error")) {
789 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
790 return NULL;
791 } else if (!iter->ns) {
792 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
793 return NULL;
794 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
795 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
796 return NULL;
797 }
798 ++i;
799 }
800
801 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100802 if (!error_rpl) {
803 ERRMEM;
804 return NULL;
805 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100806 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100807 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100808 if (!error_rpl->err) {
809 ERRMEM;
810 free(error_rpl);
811 return NULL;
812 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100813 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100814 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100815 reply = (struct nc_reply *)error_rpl;
816
817 i = 0;
818 LY_TREE_FOR(xml->child, iter) {
819 parse_rpc_error(ctx, iter, error_rpl->err + i);
820 ++i;
821 }
822
823 /* ok */
824 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
825 if (xml->child->next) {
826 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
827 return NULL;
828 }
829 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100830 if (!reply) {
831 ERRMEM;
832 return NULL;
833 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100834 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100835
836 /* some RPC output */
837 } else {
838 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200839 case NC_RPC_ACT_GENERIC:
840 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100841
842 if (rpc_gen->has_data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200843 data = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100844 } else {
Michal Vasko68b3f292016-09-16 12:00:32 +0200845 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100846 if (!data) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200847 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100848 return NULL;
849 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200850 }
851 if (data->schema->nodetype == LYS_RPC) {
852 /* RPC */
Michal Vasko086311b2016-01-08 09:53:11 +0100853 schema = data->schema;
Michal Vasko90e8e692016-07-13 12:27:57 +0200854 } else {
855 /* action */
856 LY_TREE_DFS_BEGIN(data, next, elem) {
857 if (elem->schema->nodetype == LYS_ACTION) {
858 schema = elem->schema;
859 break;
860 }
861 LY_TREE_DFS_END(data, next, elem);
862 }
863 }
864
865 /* cleanup */
866 if (data != rpc_gen->content.data) {
Michal Vasko086311b2016-01-08 09:53:11 +0100867 lyd_free(data);
868 data = NULL;
869 }
870 if (!schema) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200871 /* only with action, if there is no action, it should not have gotten this far */
Michal Vasko9e036d52016-01-08 10:49:26 +0100872 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100873 return NULL;
874 }
875 break;
876
877 case NC_RPC_GETCONFIG:
878 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100879 if (!xml->child->child) {
880 /* we did not receive any data */
881 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100882 if (!data_rpl) {
883 ERRMEM;
884 return NULL;
885 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100886 data_rpl->type = NC_RPL_DATA;
887 data_rpl->data = NULL;
888 return (struct nc_reply *)data_rpl;
889 }
890
Michal Vasko086311b2016-01-08 09:53:11 +0100891 /* special treatment */
892 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100893 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100894 if (!data) {
895 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
896 return NULL;
897 }
898 break;
899
900 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100901 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100902 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100903 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100904 return NULL;
905 }
906 break;
907
908 case NC_RPC_EDIT:
909 case NC_RPC_COPY:
910 case NC_RPC_DELETE:
911 case NC_RPC_LOCK:
912 case NC_RPC_UNLOCK:
913 case NC_RPC_KILL:
914 case NC_RPC_COMMIT:
915 case NC_RPC_DISCARD:
916 case NC_RPC_CANCEL:
917 case NC_RPC_VALIDATE:
918 case NC_RPC_SUBSCRIBE:
919 /* there is no output defined */
920 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
921 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100922 default:
923 ERRINT;
924 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100925 }
926
927 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100928 if (!data_rpl) {
929 ERRMEM;
930 return NULL;
931 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100932 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100933 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +0200934 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
935 schema, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100936 } else {
937 /* <get>, <get-config> */
938 data_rpl->data = data;
939 }
940 if (!data_rpl->data) {
941 ERR("Failed to parse <rpc-reply>.");
942 free(data_rpl);
943 return NULL;
944 }
945 reply = (struct nc_reply *)data_rpl;
946 }
947
948 return reply;
949}
950
Radek Krejci53691be2016-02-22 13:58:37 +0100951#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100952
Michal Vasko3031aae2016-01-27 16:07:18 +0100953int
954nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
955{
956 int sock;
957
Michal Vasko45e53ae2016-04-07 11:46:03 +0200958 if (!address) {
959 ERRARG("address");
960 return -1;
961 } else if (!port) {
962 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100963 return -1;
964 }
965
966 sock = nc_sock_listen(address, port);
967 if (sock == -1) {
968 return -1;
969 }
970
971 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100972 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
973 if (!client_opts.ch_binds) {
974 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100975 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100976 return -1;
977 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100978
979 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100980 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
981 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100982 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100983 return -1;
984 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100985 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
986 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
987 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
988
989 return 0;
990}
991
992int
993nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
994{
995 uint32_t i;
996 int ret = -1;
997
998 if (!address && !port && !ti) {
999 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1000 close(client_opts.ch_binds[i].sock);
1001 free((char *)client_opts.ch_binds[i].address);
1002
1003 ret = 0;
1004 }
1005 free(client_opts.ch_binds);
1006 client_opts.ch_binds = NULL;
1007 client_opts.ch_bind_count = 0;
1008 } else {
1009 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1010 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1011 && (!port || (client_opts.ch_binds[i].port == port))
1012 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
1013 close(client_opts.ch_binds[i].sock);
1014 free((char *)client_opts.ch_binds[i].address);
1015
1016 --client_opts.ch_bind_count;
1017 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1018
1019 ret = 0;
1020 }
1021 }
1022 }
1023
1024 return ret;
1025}
1026
1027API int
1028nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1029{
1030 int sock;
1031 char *host = NULL;
1032 uint16_t port, idx;
1033
Michal Vasko45e53ae2016-04-07 11:46:03 +02001034 if (!client_opts.ch_binds) {
1035 ERRINIT;
1036 return -1;
1037 } else if (!session) {
1038 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001039 return -1;
1040 }
1041
1042 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1043
Michal Vasko50456e82016-02-02 12:16:08 +01001044 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001045 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001046 return sock;
1047 }
1048
Radek Krejci53691be2016-02-22 13:58:37 +01001049#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001050 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001051 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001052 } else
1053#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001054#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001055 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001056 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001057 } else
1058#endif
1059 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001060 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001061 *session = NULL;
1062 }
1063
1064 free(host);
1065
1066 if (!(*session)) {
1067 return -1;
1068 }
1069
1070 return 1;
1071}
1072
Radek Krejci53691be2016-02-22 13:58:37 +01001073#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001074
Michal Vaskobdfb5242016-05-24 09:11:01 +02001075API const char **
1076nc_session_get_cpblts(const struct nc_session *session)
1077{
1078 if (!session) {
1079 ERRARG("session");
1080 return NULL;
1081 }
1082
1083 return session->cpblts;
1084}
1085
1086API const char *
1087nc_session_cpblt(const struct nc_session *session, const char *capab)
1088{
1089 int i, len;
1090
1091 if (!session) {
1092 ERRARG("session");
1093 return NULL;
1094 } else if (!capab) {
1095 ERRARG("capab");
1096 return NULL;
1097 }
1098
1099 len = strlen(capab);
1100 for (i = 0; session->cpblts[i]; ++i) {
1101 if (!strncmp(session->cpblts[i], capab, len)) {
1102 return session->cpblts[i];
1103 }
1104 }
1105
1106 return NULL;
1107}
1108
Michal Vasko9cd26a82016-05-31 08:58:48 +02001109API int
1110nc_session_ntf_thread_running(const struct nc_session *session)
1111{
1112 if (!session) {
1113 ERRARG("session");
1114 return 0;
1115 }
1116
1117 return session->ntf_tid ? 1 : 0;
1118}
1119
Michal Vaskob7558c52016-02-26 15:04:19 +01001120API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001121nc_client_init(void)
1122{
1123 nc_init();
1124}
1125
1126API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001127nc_client_destroy(void)
1128{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001129 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001130#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1131 nc_client_ch_del_bind(NULL, 0, 0);
1132#endif
1133#ifdef NC_ENABLED_SSH
1134 nc_client_ssh_destroy_opts();
1135#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001136#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001137 nc_client_tls_destroy_opts();
1138#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001139 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001140}
1141
Michal Vasko086311b2016-01-08 09:53:11 +01001142API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001143nc_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 +01001144{
1145 struct lyxml_elem *xml;
1146 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1147
Michal Vasko45e53ae2016-04-07 11:46:03 +02001148 if (!session) {
1149 ERRARG("session");
1150 return NC_MSG_ERROR;
1151 } else if (!rpc) {
1152 ERRARG("rpc");
1153 return NC_MSG_ERROR;
1154 } else if (!reply) {
1155 ERRARG("reply");
1156 return NC_MSG_ERROR;
1157 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1158 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001159 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001160 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001161 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001162 return NC_MSG_ERROR;
1163 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001164 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001165 *reply = NULL;
1166
1167 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001168
Michal Vasko71ba2da2016-05-04 10:53:16 +02001169 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001170 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001171 lyxml_free(session->ctx, xml);
1172 if (!(*reply)) {
1173 return NC_MSG_ERROR;
1174 }
1175 }
1176
1177 return msgtype;
1178}
1179
1180API NC_MSG_TYPE
1181nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1182{
1183 struct lyxml_elem *xml, *ev_time;
1184 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1185
Michal Vasko45e53ae2016-04-07 11:46:03 +02001186 if (!session) {
1187 ERRARG("session");
1188 return NC_MSG_ERROR;
1189 } else if (!notif) {
1190 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001191 return NC_MSG_ERROR;
1192 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001193 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001194 return NC_MSG_ERROR;
1195 }
1196
1197 msgtype = get_msg(session, timeout, 0, &xml);
1198
1199 if (msgtype == NC_MSG_NOTIF) {
1200 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001201 if (!*notif) {
1202 ERRMEM;
1203 lyxml_free(session->ctx, xml);
1204 return NC_MSG_ERROR;
1205 }
Michal Vasko086311b2016-01-08 09:53:11 +01001206
1207 /* eventTime */
1208 LY_TREE_FOR(xml->child, ev_time) {
1209 if (!strcmp(ev_time->name, "eventTime")) {
1210 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1211 /* lyd_parse does not know this element */
1212 lyxml_free(session->ctx, ev_time);
1213 break;
1214 }
1215 }
1216 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001217 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001218 goto fail;
1219 }
1220
1221 /* notification body */
Michal Vasko68b3f292016-09-16 12:00:32 +02001222 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001223 lyxml_free(session->ctx, xml);
1224 xml = NULL;
1225 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001226 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001227 goto fail;
1228 }
1229 }
1230
1231 return msgtype;
1232
1233fail:
1234 lydict_remove(session->ctx, (*notif)->datetime);
1235 lyd_free((*notif)->tree);
1236 free(*notif);
1237 *notif = NULL;
1238 lyxml_free(session->ctx, xml);
1239
1240 return NC_MSG_ERROR;
1241}
1242
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001243static void *
1244nc_recv_notif_thread(void *arg)
1245{
1246 struct nc_ntf_thread_arg *ntarg;
1247 struct nc_session *session;
1248 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1249 struct nc_notif *notif;
1250 NC_MSG_TYPE msgtype;
1251
1252 ntarg = (struct nc_ntf_thread_arg *)arg;
1253 session = ntarg->session;
1254 notif_clb = ntarg->notif_clb;
1255 free(ntarg);
1256
1257 while (session->ntf_tid) {
1258 msgtype = nc_recv_notif(session, 0, &notif);
1259 if (msgtype == NC_MSG_NOTIF) {
1260 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001261 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1262 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1263 nc_notif_free(notif);
1264 break;
1265 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001266 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001267 } else if (msgtype == NC_MSG_ERROR) {
1268 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001269 }
1270
1271 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1272 }
1273
Michal Vasko0651c902016-05-19 15:55:42 +02001274 VRB("Session %u: notification thread exit.", session->id);
1275 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001276 return NULL;
1277}
1278
1279API int
1280nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1281{
1282 struct nc_ntf_thread_arg *ntarg;
1283 int ret;
1284
Michal Vasko45e53ae2016-04-07 11:46:03 +02001285 if (!session) {
1286 ERRARG("session");
1287 return -1;
1288 } else if (!notif_clb) {
1289 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001290 return -1;
1291 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1292 ERR("Session %u: invalid session to receive Notifications.", session->id);
1293 return -1;
1294 } else if (session->ntf_tid) {
1295 ERR("Session %u: separate notification thread is already running.", session->id);
1296 return -1;
1297 }
1298
1299 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001300 if (!ntarg) {
1301 ERRMEM;
1302 return -1;
1303 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001304 ntarg->session = session;
1305 ntarg->notif_clb = notif_clb;
1306
1307 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1308 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001309 if (!session->ntf_tid) {
1310 ERRMEM;
1311 free(ntarg);
1312 return -1;
1313 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001314
1315 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1316 if (ret) {
1317 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1318 free(ntarg);
1319 free((pthread_t *)session->ntf_tid);
1320 session->ntf_tid = NULL;
1321 return -1;
1322 }
1323
1324 return 0;
1325}
1326
Michal Vasko086311b2016-01-08 09:53:11 +01001327API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001328nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001329{
1330 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001331 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001332 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001333 struct nc_rpc_getconfig *rpc_gc;
1334 struct nc_rpc_edit *rpc_e;
1335 struct nc_rpc_copy *rpc_cp;
1336 struct nc_rpc_delete *rpc_del;
1337 struct nc_rpc_lock *rpc_lock;
1338 struct nc_rpc_get *rpc_g;
1339 struct nc_rpc_kill *rpc_k;
1340 struct nc_rpc_commit *rpc_com;
1341 struct nc_rpc_cancel *rpc_can;
1342 struct nc_rpc_validate *rpc_val;
1343 struct nc_rpc_getschema *rpc_gs;
1344 struct nc_rpc_subscribe *rpc_sub;
1345 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001346 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001347 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001348 uint64_t cur_msgid;
1349
Michal Vasko45e53ae2016-04-07 11:46:03 +02001350 if (!session) {
1351 ERRARG("session");
1352 return NC_MSG_ERROR;
1353 } else if (!rpc) {
1354 ERRARG("rpc");
1355 return NC_MSG_ERROR;
1356 } else if (!msgid) {
1357 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001358 return NC_MSG_ERROR;
1359 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001360 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001361 return NC_MSG_ERROR;
1362 }
1363
Michal Vasko90e8e692016-07-13 12:27:57 +02001364 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001365 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1366 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001367 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001368 return NC_MSG_ERROR;
1369 }
1370 }
1371
1372 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001373 case NC_RPC_ACT_GENERIC:
1374 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001375
1376 if (rpc_gen->has_data) {
1377 data = rpc_gen->content.data;
1378 } else {
Michal Vasko68b3f292016-09-16 12:00:32 +02001379 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 +01001380 }
1381 break;
1382
1383 case NC_RPC_GETCONFIG:
1384 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1385
1386 data = lyd_new(NULL, ietfnc, "get-config");
1387 node = lyd_new(data, ietfnc, "source");
1388 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1389 if (!node) {
1390 lyd_free(data);
1391 return NC_MSG_ERROR;
1392 }
1393 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001394 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001395 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001396 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001397 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001398 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001399 lyd_insert_attr(node, NULL, "type", "xpath");
1400 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001401 }
1402 if (!node) {
1403 lyd_free(data);
1404 return NC_MSG_ERROR;
1405 }
1406 }
1407
1408 if (rpc_gc->wd_mode) {
1409 if (!ietfncwd) {
1410 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1411 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001412 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001413 return NC_MSG_ERROR;
1414 }
1415 }
1416 switch (rpc_gc->wd_mode) {
1417 case NC_WD_UNKNOWN:
1418 /* cannot get here */
1419 break;
1420 case NC_WD_ALL:
1421 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1422 break;
1423 case NC_WD_ALL_TAG:
1424 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1425 break;
1426 case NC_WD_TRIM:
1427 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1428 break;
1429 case NC_WD_EXPLICIT:
1430 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1431 break;
1432 }
1433 if (!node) {
1434 lyd_free(data);
1435 return NC_MSG_ERROR;
1436 }
1437 }
1438 break;
1439
1440 case NC_RPC_EDIT:
1441 rpc_e = (struct nc_rpc_edit *)rpc;
1442
1443 data = lyd_new(NULL, ietfnc, "edit-config");
1444 node = lyd_new(data, ietfnc, "target");
1445 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1446 if (!node) {
1447 lyd_free(data);
1448 return NC_MSG_ERROR;
1449 }
1450
1451 if (rpc_e->default_op) {
1452 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1453 if (!node) {
1454 lyd_free(data);
1455 return NC_MSG_ERROR;
1456 }
1457 }
1458
1459 if (rpc_e->test_opt) {
1460 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1461 if (!node) {
1462 lyd_free(data);
1463 return NC_MSG_ERROR;
1464 }
1465 }
1466
1467 if (rpc_e->error_opt) {
1468 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1469 if (!node) {
1470 lyd_free(data);
1471 return NC_MSG_ERROR;
1472 }
1473 }
1474
Michal Vasko7793bc62016-09-16 11:58:41 +02001475 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001476 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001477 } else {
1478 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1479 }
1480 if (!node) {
1481 lyd_free(data);
1482 return NC_MSG_ERROR;
1483 }
1484 break;
1485
1486 case NC_RPC_COPY:
1487 rpc_cp = (struct nc_rpc_copy *)rpc;
1488
1489 data = lyd_new(NULL, ietfnc, "copy-config");
1490 node = lyd_new(data, ietfnc, "target");
1491 if (rpc_cp->url_trg) {
1492 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1493 } else {
1494 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1495 }
1496 if (!node) {
1497 lyd_free(data);
1498 return NC_MSG_ERROR;
1499 }
1500
1501 node = lyd_new(data, ietfnc, "source");
1502 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001503 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001504 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001505 } else {
1506 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1507 }
1508 } else {
1509 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1510 }
1511 if (!node) {
1512 lyd_free(data);
1513 return NC_MSG_ERROR;
1514 }
1515
1516 if (rpc_cp->wd_mode) {
1517 if (!ietfncwd) {
1518 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1519 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001520 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001521 return NC_MSG_ERROR;
1522 }
1523 }
1524 switch (rpc_cp->wd_mode) {
1525 case NC_WD_UNKNOWN:
1526 /* cannot get here */
1527 break;
1528 case NC_WD_ALL:
1529 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1530 break;
1531 case NC_WD_ALL_TAG:
1532 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1533 break;
1534 case NC_WD_TRIM:
1535 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1536 break;
1537 case NC_WD_EXPLICIT:
1538 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1539 break;
1540 }
1541 if (!node) {
1542 lyd_free(data);
1543 return NC_MSG_ERROR;
1544 }
1545 }
1546 break;
1547
1548 case NC_RPC_DELETE:
1549 rpc_del = (struct nc_rpc_delete *)rpc;
1550
1551 data = lyd_new(NULL, ietfnc, "delete-config");
1552 node = lyd_new(data, ietfnc, "target");
1553 if (rpc_del->url) {
1554 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1555 } else {
1556 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1557 }
1558 if (!node) {
1559 lyd_free(data);
1560 return NC_MSG_ERROR;
1561 }
1562 break;
1563
1564 case NC_RPC_LOCK:
1565 rpc_lock = (struct nc_rpc_lock *)rpc;
1566
1567 data = lyd_new(NULL, ietfnc, "lock");
1568 node = lyd_new(data, ietfnc, "target");
1569 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1570 if (!node) {
1571 lyd_free(data);
1572 return NC_MSG_ERROR;
1573 }
1574 break;
1575
1576 case NC_RPC_UNLOCK:
1577 rpc_lock = (struct nc_rpc_lock *)rpc;
1578
1579 data = lyd_new(NULL, ietfnc, "unlock");
1580 node = lyd_new(data, ietfnc, "target");
1581 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1582 if (!node) {
1583 lyd_free(data);
1584 return NC_MSG_ERROR;
1585 }
1586 break;
1587
1588 case NC_RPC_GET:
1589 rpc_g = (struct nc_rpc_get *)rpc;
1590
1591 data = lyd_new(NULL, ietfnc, "get");
1592 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001593 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001594 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001595 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001596 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001597 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001598 lyd_insert_attr(node, NULL, "type", "xpath");
1599 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001600 }
1601 if (!node) {
1602 lyd_free(data);
1603 return NC_MSG_ERROR;
1604 }
1605 }
1606
1607 if (rpc_g->wd_mode) {
1608 if (!ietfncwd) {
1609 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1610 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001611 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001612 return NC_MSG_ERROR;
1613 }
1614 }
1615 switch (rpc_g->wd_mode) {
1616 case NC_WD_UNKNOWN:
1617 /* cannot get here */
1618 break;
1619 case NC_WD_ALL:
1620 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1621 break;
1622 case NC_WD_ALL_TAG:
1623 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1624 break;
1625 case NC_WD_TRIM:
1626 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1627 break;
1628 case NC_WD_EXPLICIT:
1629 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1630 break;
1631 }
1632 if (!node) {
1633 lyd_free(data);
1634 return NC_MSG_ERROR;
1635 }
1636 }
1637 break;
1638
1639 case NC_RPC_KILL:
1640 rpc_k = (struct nc_rpc_kill *)rpc;
1641
1642 data = lyd_new(NULL, ietfnc, "kill-session");
1643 sprintf(str, "%u", rpc_k->sid);
1644 lyd_new_leaf(data, ietfnc, "session-id", str);
1645 break;
1646
1647 case NC_RPC_COMMIT:
1648 rpc_com = (struct nc_rpc_commit *)rpc;
1649
1650 data = lyd_new(NULL, ietfnc, "commit");
1651 if (rpc_com->confirmed) {
1652 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1653 }
1654
1655 if (rpc_com->confirm_timeout) {
1656 sprintf(str, "%u", rpc_com->confirm_timeout);
1657 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1658 }
1659
1660 if (rpc_com->persist) {
1661 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1662 if (!node) {
1663 lyd_free(data);
1664 return NC_MSG_ERROR;
1665 }
1666 }
1667
1668 if (rpc_com->persist_id) {
1669 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1670 if (!node) {
1671 lyd_free(data);
1672 return NC_MSG_ERROR;
1673 }
1674 }
1675 break;
1676
1677 case NC_RPC_DISCARD:
1678 data = lyd_new(NULL, ietfnc, "discard-changes");
1679 break;
1680
1681 case NC_RPC_CANCEL:
1682 rpc_can = (struct nc_rpc_cancel *)rpc;
1683
1684 data = lyd_new(NULL, ietfnc, "cancel-commit");
1685 if (rpc_can->persist_id) {
1686 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1687 if (!node) {
1688 lyd_free(data);
1689 return NC_MSG_ERROR;
1690 }
1691 }
1692 break;
1693
1694 case NC_RPC_VALIDATE:
1695 rpc_val = (struct nc_rpc_validate *)rpc;
1696
1697 data = lyd_new(NULL, ietfnc, "validate");
1698 node = lyd_new(data, ietfnc, "source");
1699 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001700 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001701 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_CONSTSTRING);
Michal Vasko086311b2016-01-08 09:53:11 +01001702 } else {
1703 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1704 }
1705 } else {
1706 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1707 }
1708 if (!node) {
1709 lyd_free(data);
1710 return NC_MSG_ERROR;
1711 }
1712 break;
1713
1714 case NC_RPC_GETSCHEMA:
1715 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1716 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001717 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001718 return NC_MSG_ERROR;
1719 }
1720
1721 rpc_gs = (struct nc_rpc_getschema *)rpc;
1722
1723 data = lyd_new(NULL, ietfncmon, "get-schema");
1724 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1725 if (!node) {
1726 lyd_free(data);
1727 return NC_MSG_ERROR;
1728 }
1729 if (rpc_gs->version) {
1730 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1731 if (!node) {
1732 lyd_free(data);
1733 return NC_MSG_ERROR;
1734 }
1735 }
1736 if (rpc_gs->format) {
1737 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1738 if (!node) {
1739 lyd_free(data);
1740 return NC_MSG_ERROR;
1741 }
1742 }
1743 break;
1744
1745 case NC_RPC_SUBSCRIBE:
1746 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1747 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001748 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001749 return NC_MSG_ERROR;
1750 }
1751
1752 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1753
1754 data = lyd_new(NULL, notifs, "create-subscription");
1755 if (rpc_sub->stream) {
1756 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1757 if (!node) {
1758 lyd_free(data);
1759 return NC_MSG_ERROR;
1760 }
1761 }
1762
1763 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001764 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Radek Krejci539efb62016-08-24 15:05:16 +02001765 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001766 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001767 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001768 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001769 lyd_insert_attr(node, NULL, "type", "xpath");
1770 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001771 }
1772 if (!node) {
1773 lyd_free(data);
1774 return NC_MSG_ERROR;
1775 }
1776 }
1777
1778 if (rpc_sub->start) {
1779 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1780 if (!node) {
1781 lyd_free(data);
1782 return NC_MSG_ERROR;
1783 }
1784 }
1785
1786 if (rpc_sub->stop) {
1787 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1788 if (!node) {
1789 lyd_free(data);
1790 return NC_MSG_ERROR;
1791 }
1792 }
1793 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001794 default:
1795 ERRINT;
1796 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001797 }
1798
Michal Vasko49a0cf92016-09-14 09:15:32 +02001799 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT, NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001800 lyd_free(data);
1801 return NC_MSG_ERROR;
1802 }
1803
Michal Vasko62be1ce2016-03-03 13:24:52 +01001804 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001805 if (ret == -1) {
1806 /* error */
1807 r = NC_MSG_ERROR;
1808 } else if (!ret) {
1809 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001810 r = NC_MSG_WOULDBLOCK;
1811 } else {
1812 /* send RPC, store its message ID */
1813 r = nc_send_msg(session, data);
1814 cur_msgid = session->msgid;
1815 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001816 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001817
1818 lyd_free(data);
1819
1820 if (r != NC_MSG_RPC) {
1821 return r;
1822 }
1823
1824 *msgid = cur_msgid;
1825 return NC_MSG_RPC;
1826}