blob: de18c14fe6f065cad690d038b7e68bb28ee4c318 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <assert.h>
16#include <errno.h>
17#include <fcntl.h>
18#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010019#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <arpa/inet.h>
28#include <poll.h>
29
30#include <libyang/libyang.h>
31
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010033#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010034#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010035
Michal Vasko80ef5d22016-01-18 09:21:02 +010036static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
37
Michal Vaskodaf9a092016-02-09 10:42:05 +010038struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010039
40API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +010041nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010042{
Michal Vasko3031aae2016-01-27 16:07:18 +010043 if (client_opts.schema_searchpath) {
44 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010045 }
Michal Vasko086311b2016-01-08 09:53:11 +010046
Michal Vasko7f1c78b2016-01-19 09:52:14 +010047 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010048 client_opts.schema_searchpath = strdup(path);
49 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010050 ERRMEM;
51 return 1;
52 }
53 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010054 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010055 }
56
57 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010058}
59
Michal Vasko7f1c0ef2016-03-11 11:13:06 +010060API const char *
61nc_client_get_schema_searchpath(void)
62{
63 return client_opts.schema_searchpath;
64}
65
Michal Vasko3031aae2016-01-27 16:07:18 +010066/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010067static int
68ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
69{
70 const struct lys_module *module;
71 char *ptr, *ptr2;
72 char *model_name, *revision = NULL, *features = NULL;
73
74 /* parse module */
75 ptr = strstr(cpblt, "module=");
76 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010077 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
78 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010079 }
80 ptr += 7;
81 ptr2 = strchr(ptr, '&');
82 if (!ptr2) {
83 ptr2 = ptr + strlen(ptr);
84 }
85 model_name = strndup(ptr, ptr2 - ptr);
86
87 /* parse revision */
88 ptr = strstr(cpblt, "revision=");
89 if (ptr) {
90 ptr += 9;
91 ptr2 = strchr(ptr, '&');
92 if (!ptr2) {
93 ptr2 = ptr + strlen(ptr);
94 }
95 revision = strndup(ptr, ptr2 - ptr);
96 }
97
98 /* load module if needed */
99 module = ly_ctx_get_module(session->ctx, model_name, revision);
100 if (!module) {
101 module = ly_ctx_load_module(session->ctx, model_name, revision);
102 }
103
Michal Vasko086311b2016-01-08 09:53:11 +0100104 free(revision);
105 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100106 WRN("Failed to load model \"%s\".", model_name);
107 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100108 return 1;
109 }
Michal Vaskoef578332016-01-25 13:20:09 +0100110 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100111
112 /* parse features */
113 ptr = strstr(cpblt, "features=");
114 if (ptr) {
115 ptr += 9;
116 ptr2 = strchr(ptr, '&');
117 if (!ptr2) {
118 ptr2 = ptr + strlen(ptr);
119 }
120 features = strndup(ptr, ptr2 - ptr);
121 }
122
123 /* enable features */
124 if (features) {
125 /* basically manual strtok_r (to avoid macro) */
126 ptr2 = features;
127 for (ptr = features; *ptr; ++ptr) {
128 if (*ptr == ',') {
129 *ptr = '\0';
130 /* remember last feature */
131 ptr2 = ptr + 1;
132 }
133 }
134
135 ptr = features;
136 lys_features_enable(module, ptr);
137 while (ptr != ptr2) {
138 ptr += strlen(ptr) + 1;
139 lys_features_enable(module, ptr);
140 }
141
142 free(features);
143 }
144
145 return 0;
146}
147
Michal Vasko1aaa6602016-02-09 11:04:33 +0100148/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100149static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100150ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100151{
152 int i;
153 const struct lys_module *ietfnc;
154
155 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
156 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100157 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
158 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100159 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100160 }
161 }
162 if (!ietfnc) {
163 ERR("Loading base NETCONF schema failed.");
164 return 1;
165 }
166
167 /* set supported capabilities from ietf-netconf */
168 for (i = 0; cpblts[i]; ++i) {
169 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
170 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
171 lys_features_enable(ietfnc, "writable-running");
172 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
173 lys_features_enable(ietfnc, "candidate");
174 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
175 lys_features_enable(ietfnc, "confirmed-commit");
176 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
177 lys_features_enable(ietfnc, "rollback-on-error");
178 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
179 lys_features_enable(ietfnc, "validate");
180 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
181 lys_features_enable(ietfnc, "startup");
182 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
183 lys_features_enable(ietfnc, "url");
184 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
185 lys_features_enable(ietfnc, "xpath");
186 }
187 }
188 }
189
190 return 0;
191}
192
193static char *
194libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
Michal Vaskoca4ad152016-03-03 15:50:45 +0100195 void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100196{
197 struct nc_session *session = (struct nc_session *)user_data;
198 struct nc_rpc *rpc;
199 struct nc_reply *reply;
200 struct nc_reply_data *data_rpl;
Michal Vaskob2583f12016-05-12 11:40:23 +0200201 struct lyd_node_anyxml *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100202 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200203 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100204 uint64_t msgid;
205
206 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100207 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100208 *format = LYS_IN_YIN;
209
210 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
211 usleep(1000);
212 }
213 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100214 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100215 nc_rpc_free(rpc);
216 return NULL;
217 }
218
Michal vasko2423e912016-10-03 14:16:19 +0200219 do {
220 msg = nc_recv_reply(session, rpc, msgid, 1000, 0, &reply);
221 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100222 nc_rpc_free(rpc);
223 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100225 return NULL;
226 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100227 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100228 return NULL;
229 }
230
Michal Vasko05ba9df2016-01-13 14:40:27 +0100231 if (reply->type != NC_RPL_DATA) {
232 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100233 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100234 nc_reply_free(reply);
235 return NULL;
236 }
237
Michal Vasko086311b2016-01-08 09:53:11 +0100238 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200239 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
240 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
241 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
242 nc_reply_free(reply);
243 return NULL;
244 }
245 get_schema_data = (struct lyd_node_anyxml *)data_rpl->data->child;
246 if (get_schema_data->xml_struct) {
247 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200248 } else {
Michal Vaskob2583f12016-05-12 11:40:23 +0200249 model_data = strdup(get_schema_data->value.str);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200250 }
Michal Vasko086311b2016-01-08 09:53:11 +0100251 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100252 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100253
Michal Vasko086311b2016-01-08 09:53:11 +0100254 return model_data;
255}
256
Michal Vaskoef578332016-01-25 13:20:09 +0100257/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100258int
259nc_ctx_check_and_fill(struct nc_session *session)
260{
Michal Vaskoef578332016-01-25 13:20:09 +0100261 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100262 ly_module_clb old_clb = NULL;
263 void *old_data = NULL;
264
265 assert(session->cpblts && session->ctx);
266
267 /* check if get-schema is supported */
268 for (i = 0; session->cpblts[i]; ++i) {
269 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
270 get_schema_support = 1;
271 break;
272 }
273 }
274
275 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
276 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
277 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
278 /* set module retrieval using <get-schema> */
279 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100280 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100281 } else {
282 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
283 }
284 }
285
286 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100287 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100288 if (old_clb) {
289 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
290 }
Michal Vaskoef578332016-01-25 13:20:09 +0100291 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100292 }
293
294 /* load all other models */
295 for (i = 0; session->cpblts[i]; ++i) {
296 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
297 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
298 continue;
299 }
300
Michal Vaskoef578332016-01-25 13:20:09 +0100301 r = ctx_check_and_load_model(session, session->cpblts[i]);
302 if (r == -1) {
303 ret = -1;
304 break;
305 }
306
307 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
308 * if it was using get-schema */
309 if (r == 1) {
310 if (get_schema_support) {
311 VRB("Trying to load the schema from a different source.");
312 /* works even if old_clb is NULL */
313 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
314 r = ctx_check_and_load_model(session, session->cpblts[i]);
315 }
316
317 /* fail again (or no other way to try), too bad */
318 if (r) {
319 ret = 1;
320 }
321
322 /* set get-schema callback back */
323 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
324 }
Michal Vasko086311b2016-01-08 09:53:11 +0100325 }
326
327 if (old_clb) {
328 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
329 }
Michal Vaskoef578332016-01-25 13:20:09 +0100330 if (ret == 1) {
331 WRN("Some models failed to be loaded, any data from these models will be ignored.");
332 }
333 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100334}
335
336API struct nc_session *
337nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
338{
Michal Vaskod083db62016-01-19 10:31:29 +0100339 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100340
Michal Vasko45e53ae2016-04-07 11:46:03 +0200341 if (fdin < 0) {
342 ERRARG("fdin");
343 return NULL;
344 } else if (fdout < 0) {
345 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100346 return NULL;
347 }
348
349 /* prepare session structure */
350 session = calloc(1, sizeof *session);
351 if (!session) {
352 ERRMEM;
353 return NULL;
354 }
355 session->status = NC_STATUS_STARTING;
356 session->side = NC_CLIENT;
357
358 /* transport specific data */
359 session->ti_type = NC_TI_FD;
360 session->ti.fd.in = fdin;
361 session->ti.fd.out = fdout;
362
363 /* assign context (dicionary needed for handshake) */
364 if (!ctx) {
365 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100366 /* definitely should not happen, but be ready */
367 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
368 /* that's just it */
369 goto fail;
370 }
Michal Vasko086311b2016-01-08 09:53:11 +0100371 } else {
372 session->flags |= NC_SESSION_SHAREDCTX;
373 }
374 session->ctx = ctx;
375
376 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200377 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100378 goto fail;
379 }
380 session->status = NC_STATUS_RUNNING;
381
Michal Vaskoef578332016-01-25 13:20:09 +0100382 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100383 goto fail;
384 }
385
386 return session;
387
388fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100389 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100390 return NULL;
391}
392
393int
Michal Vaskof05562c2016-01-20 12:06:43 +0100394nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100395{
Michal Vasko0190bc32016-03-02 15:47:49 +0100396 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100397 struct addrinfo hints, *res_list, *res;
398 char port_s[6]; /* length of string representation of short int */
399
400 snprintf(port_s, 6, "%u", port);
401
402 /* Connect to a server */
403 memset(&hints, 0, sizeof hints);
404 hints.ai_family = AF_UNSPEC;
405 hints.ai_socktype = SOCK_STREAM;
406 hints.ai_protocol = IPPROTO_TCP;
407 i = getaddrinfo(host, port_s, &hints, &res_list);
408 if (i != 0) {
409 ERR("Unable to translate the host address (%s).", gai_strerror(i));
410 return -1;
411 }
412
413 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
414 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
415 if (sock == -1) {
416 /* socket was not created, try another resource */
417 i = errno;
418 goto errloop;
419 }
420
421 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
422 /* network connection failed, try another resource */
423 i = errno;
424 close(sock);
425 sock = -1;
426 goto errloop;
427 }
428
Michal Vasko0190bc32016-03-02 15:47:49 +0100429 /* make the socket non-blocking */
430 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
431 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100432 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100433 return -1;
434 }
435
Michal Vasko086311b2016-01-08 09:53:11 +0100436 /* we're done, network connection established */
437 break;
438errloop:
439 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
440 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
441 continue;
442 }
443
444 if (sock == -1) {
445 ERR("Unable to connect to %s:%s.", host, port_s);
446 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100447 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 +0100448 }
449 freeaddrinfo(res_list);
450
451 return sock;
452}
453
Michal Vasko086311b2016-01-08 09:53:11 +0100454static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100455get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100456{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100457 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100458 char *ptr;
459 const char *str_msgid;
460 uint64_t cur_msgid;
461 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100462 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100463 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
464
Michal Vasko62be1ce2016-03-03 13:24:52 +0100465 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100466 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100467 /* error */
468 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100469 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100470 /* timeout */
471 return NC_MSG_WOULDBLOCK;
472 }
473
474 /* try to get notification from the session's queue */
475 if (!msgid && session->notifs) {
476 cont = session->notifs;
477 session->notifs = cont->next;
478
Michal Vasko71ba2da2016-05-04 10:53:16 +0200479 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100480 free(cont);
481
Michal Vasko71ba2da2016-05-04 10:53:16 +0200482 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100483 }
484
485 /* try to get rpc-reply from the session's queue */
486 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200487 cont = session->replies;
488 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100489
Michal Vasko71ba2da2016-05-04 10:53:16 +0200490 xml = cont->msg;
491 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100492
Michal Vasko71ba2da2016-05-04 10:53:16 +0200493 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100494 }
495
Michal Vasko71ba2da2016-05-04 10:53:16 +0200496 if (!msgtype) {
497 /* read message from wire */
498 msgtype = nc_read_msg_poll(session, timeout, &xml);
499 }
Michal Vasko086311b2016-01-08 09:53:11 +0100500
501 /* we read rpc-reply, want a notif */
502 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100503 cont_ptr = &session->replies;
504 while (*cont_ptr) {
505 cont_ptr = &((*cont_ptr)->next);
506 }
507 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100508 if (!*cont_ptr) {
509 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200510 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100511 lyxml_free(session->ctx, xml);
512 return NC_MSG_ERROR;
513 }
Michal Vasko086311b2016-01-08 09:53:11 +0100514 (*cont_ptr)->msg = xml;
515 (*cont_ptr)->next = NULL;
516 }
517
518 /* we read notif, want a rpc-reply */
519 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100520 /* TODO check whether the session is even subscribed */
521 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100522 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100523 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100524 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100525 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100526 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100527
528 cont_ptr = &session->notifs;
529 while (*cont_ptr) {
530 cont_ptr = &((*cont_ptr)->next);
531 }
532 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100533 if (!cont_ptr) {
534 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200535 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100536 lyxml_free(session->ctx, xml);
537 return NC_MSG_ERROR;
538 }
Michal Vasko086311b2016-01-08 09:53:11 +0100539 (*cont_ptr)->msg = xml;
540 (*cont_ptr)->next = NULL;
541 }
542
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100543 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100544
545 switch (msgtype) {
546 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100547 if (!msgid) {
548 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100549 }
Michal Vasko086311b2016-01-08 09:53:11 +0100550 break;
551
552 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100553 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200554 /* check message-id */
555 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
556 if (!str_msgid) {
557 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
558 msgtype = NC_MSG_REPLY_ERR_MSGID;
559 } else {
560 cur_msgid = strtoul(str_msgid, &ptr, 10);
561 if (cur_msgid != msgid) {
562 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
563 session->id, str_msgid);
564 msgtype = NC_MSG_REPLY_ERR_MSGID;
565 }
566 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100567 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100568 }
Michal Vasko086311b2016-01-08 09:53:11 +0100569 break;
570
571 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100572 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100573 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200574 msgtype = NC_MSG_ERROR;
575 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100576
577 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100578 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100579 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200580 msgtype = NC_MSG_ERROR;
581 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100582
583 default:
584 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
585 * NC_MSG_NONE is not returned by nc_read_msg()
586 */
587 break;
588 }
589
590 return msgtype;
591}
592
593/* cannot strictly fail, but does not need to fill any error parameter at all */
594static void
595parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
596{
597 struct lyxml_elem *iter, *next, *info;
598
599 LY_TREE_FOR(xml->child, iter) {
600 if (!iter->ns) {
601 if (iter->content) {
602 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
603 } else {
604 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
605 }
606 continue;
607 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
608 if (iter->content) {
609 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
610 iter->name, iter->content, iter->ns->value);
611 } else {
612 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
613 }
614 continue;
615 }
616
617 if (!strcmp(iter->name, "error-type")) {
618 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
619 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
620 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
621 } else if (err->type) {
622 WRN("<rpc-error> <error-type> duplicated.");
623 } else {
624 err->type = lydict_insert(ctx, iter->content, 0);
625 }
626 } else if (!strcmp(iter->name, "error-tag")) {
627 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
628 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
629 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
630 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
631 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
632 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
633 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
634 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
635 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
636 && strcmp(iter->content, "malformed-message"))) {
637 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
638 } else if (err->tag) {
639 WRN("<rpc-error> <error-tag> duplicated.");
640 } else {
641 err->tag = lydict_insert(ctx, iter->content, 0);
642 }
643 } else if (!strcmp(iter->name, "error-severity")) {
644 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
645 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
646 } else if (err->severity) {
647 WRN("<rpc-error> <error-severity> duplicated.");
648 } else {
649 err->severity = lydict_insert(ctx, iter->content, 0);
650 }
651 } else if (!strcmp(iter->name, "error-app-tag")) {
652 if (err->apptag) {
653 WRN("<rpc-error> <error-app-tag> duplicated.");
654 } else {
655 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
656 }
657 } else if (!strcmp(iter->name, "error-path")) {
658 if (err->path) {
659 WRN("<rpc-error> <error-path> duplicated.");
660 } else {
661 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
662 }
663 } else if (!strcmp(iter->name, "error-message")) {
664 if (err->message) {
665 WRN("<rpc-error> <error-message> duplicated.");
666 } else {
667 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
668 if (!err->message_lang) {
669 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
670 }
671 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
672 }
673 } else if (!strcmp(iter->name, "error-info")) {
674 LY_TREE_FOR_SAFE(iter->child, next, info) {
675 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
676 if (!strcmp(info->name, "session-id")) {
677 if (err->sid) {
678 WRN("<rpc-error> <error-info> <session-id> duplicated.");
679 } else {
680 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
681 }
682 } else if (!strcmp(info->name, "bad-attr")) {
683 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100684 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
685 if (!err->attr) {
686 ERRMEM;
687 return;
688 }
Michal Vasko086311b2016-01-08 09:53:11 +0100689 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
690 } else if (!strcmp(info->name, "bad-element")) {
691 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100692 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
693 if (!err->elem) {
694 ERRMEM;
695 return;
696 }
Michal Vasko086311b2016-01-08 09:53:11 +0100697 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
698 } else if (!strcmp(info->name, "bad-namespace")) {
699 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100700 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
701 if (!err->ns) {
702 ERRMEM;
703 return;
704 }
Michal Vasko086311b2016-01-08 09:53:11 +0100705 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
706 } else {
707 if (info->content) {
708 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
709 info->name, info->content);
710 } else {
711 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
712 }
713 }
714 } else {
715 lyxml_unlink(ctx, info);
716 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100717 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
718 if (!err->other) {
719 ERRMEM;
720 return;
721 }
Michal Vasko086311b2016-01-08 09:53:11 +0100722 err->other[err->other_count - 1] = info;
723 }
724 }
725 } else {
726 if (iter->content) {
727 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
728 } else {
729 WRN("<rpc-error> unknown child \"%s\".", iter->name);
730 }
731 }
732 }
733}
734
735static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100736parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100737{
738 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100739 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100740 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100741 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100742 struct nc_reply_data *data_rpl;
743 struct nc_reply *reply = NULL;
744 struct nc_rpc_generic *rpc_gen;
745 int i;
746
747 if (!xml->child) {
748 ERR("An empty <rpc-reply>.");
749 return NULL;
750 }
751
752 /* rpc-error */
753 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
754 /* count and check elements */
755 i = 0;
756 LY_TREE_FOR(xml->child, iter) {
757 if (strcmp(iter->name, "rpc-error")) {
758 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
759 return NULL;
760 } else if (!iter->ns) {
761 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
762 return NULL;
763 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
764 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
765 return NULL;
766 }
767 ++i;
768 }
769
770 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100771 if (!error_rpl) {
772 ERRMEM;
773 return NULL;
774 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100775 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100776 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100777 if (!error_rpl->err) {
778 ERRMEM;
779 free(error_rpl);
780 return NULL;
781 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100782 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100783 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100784 reply = (struct nc_reply *)error_rpl;
785
786 i = 0;
787 LY_TREE_FOR(xml->child, iter) {
788 parse_rpc_error(ctx, iter, error_rpl->err + i);
789 ++i;
790 }
791
792 /* ok */
793 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
794 if (xml->child->next) {
795 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
796 return NULL;
797 }
798 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100799 if (!reply) {
800 ERRMEM;
801 return NULL;
802 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100803 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100804
805 /* some RPC output */
806 } else {
807 switch (rpc->type) {
808 case NC_RPC_GENERIC:
809 rpc_gen = (struct nc_rpc_generic *)rpc;
810
811 if (rpc_gen->has_data) {
812 schema = rpc_gen->content.data->schema;
813 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100814 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100815 if (!data) {
816 ERR("Failed to parse a generic RPC XML.");
817 return NULL;
818 }
819 schema = data->schema;
820 lyd_free(data);
821 data = NULL;
822 }
823 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100824 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100825 return NULL;
826 }
827 break;
828
829 case NC_RPC_GETCONFIG:
830 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100831 if (!xml->child->child) {
832 /* we did not receive any data */
833 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100834 if (!data_rpl) {
835 ERRMEM;
836 return NULL;
837 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100838 data_rpl->type = NC_RPL_DATA;
839 data_rpl->data = NULL;
840 return (struct nc_reply *)data_rpl;
841 }
842
Michal Vasko086311b2016-01-08 09:53:11 +0100843 /* special treatment */
844 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100845 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100846 if (!data) {
847 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
848 return NULL;
849 }
850 break;
851
852 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100853 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100854 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100855 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100856 return NULL;
857 }
858 break;
859
860 case NC_RPC_EDIT:
861 case NC_RPC_COPY:
862 case NC_RPC_DELETE:
863 case NC_RPC_LOCK:
864 case NC_RPC_UNLOCK:
865 case NC_RPC_KILL:
866 case NC_RPC_COMMIT:
867 case NC_RPC_DISCARD:
868 case NC_RPC_CANCEL:
869 case NC_RPC_VALIDATE:
870 case NC_RPC_SUBSCRIBE:
871 /* there is no output defined */
872 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
873 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100874 default:
875 ERRINT;
876 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100877 }
878
879 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100880 if (!data_rpl) {
881 ERRMEM;
882 return NULL;
883 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100884 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100885 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100886 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100887 } else {
888 /* <get>, <get-config> */
889 data_rpl->data = data;
890 }
891 if (!data_rpl->data) {
892 ERR("Failed to parse <rpc-reply>.");
893 free(data_rpl);
894 return NULL;
895 }
896 reply = (struct nc_reply *)data_rpl;
897 }
898
899 return reply;
900}
901
Radek Krejci53691be2016-02-22 13:58:37 +0100902#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100903
Michal Vasko3031aae2016-01-27 16:07:18 +0100904int
905nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
906{
907 int sock;
908
Michal Vasko45e53ae2016-04-07 11:46:03 +0200909 if (!address) {
910 ERRARG("address");
911 return -1;
912 } else if (!port) {
913 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100914 return -1;
915 }
916
917 sock = nc_sock_listen(address, port);
918 if (sock == -1) {
919 return -1;
920 }
921
922 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100923 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
924 if (!client_opts.ch_binds) {
925 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100926 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100927 return -1;
928 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100929
930 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100931 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
932 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100933 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100934 return -1;
935 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100936 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
937 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
938 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
939
940 return 0;
941}
942
943int
944nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
945{
946 uint32_t i;
947 int ret = -1;
948
949 if (!address && !port && !ti) {
950 for (i = 0; i < client_opts.ch_bind_count; ++i) {
951 close(client_opts.ch_binds[i].sock);
952 free((char *)client_opts.ch_binds[i].address);
953
954 ret = 0;
955 }
956 free(client_opts.ch_binds);
957 client_opts.ch_binds = NULL;
958 client_opts.ch_bind_count = 0;
959 } else {
960 for (i = 0; i < client_opts.ch_bind_count; ++i) {
961 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
962 && (!port || (client_opts.ch_binds[i].port == port))
963 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
964 close(client_opts.ch_binds[i].sock);
965 free((char *)client_opts.ch_binds[i].address);
966
967 --client_opts.ch_bind_count;
968 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
969
970 ret = 0;
971 }
972 }
973 }
974
975 return ret;
976}
977
978API int
979nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
980{
981 int sock;
982 char *host = NULL;
983 uint16_t port, idx;
984
Michal Vasko45e53ae2016-04-07 11:46:03 +0200985 if (!client_opts.ch_binds) {
986 ERRINIT;
987 return -1;
988 } else if (!session) {
989 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +0100990 return -1;
991 }
992
993 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
994
Michal Vasko50456e82016-02-02 12:16:08 +0100995 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100996 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100997 return sock;
998 }
999
Radek Krejci53691be2016-02-22 13:58:37 +01001000#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001001 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001002 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001003 } else
1004#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001005#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001006 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001007 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001008 } else
1009#endif
1010 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001011 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001012 *session = NULL;
1013 }
1014
1015 free(host);
1016
1017 if (!(*session)) {
1018 return -1;
1019 }
1020
1021 return 1;
1022}
1023
Radek Krejci53691be2016-02-22 13:58:37 +01001024#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001025
Michal Vaskobdfb5242016-05-24 09:11:01 +02001026API const char **
1027nc_session_get_cpblts(const struct nc_session *session)
1028{
1029 if (!session) {
1030 ERRARG("session");
1031 return NULL;
1032 }
1033
1034 return session->cpblts;
1035}
1036
1037API const char *
1038nc_session_cpblt(const struct nc_session *session, const char *capab)
1039{
1040 int i, len;
1041
1042 if (!session) {
1043 ERRARG("session");
1044 return NULL;
1045 } else if (!capab) {
1046 ERRARG("capab");
1047 return NULL;
1048 }
1049
1050 len = strlen(capab);
1051 for (i = 0; session->cpblts[i]; ++i) {
1052 if (!strncmp(session->cpblts[i], capab, len)) {
1053 return session->cpblts[i];
1054 }
1055 }
1056
1057 return NULL;
1058}
1059
Michal Vasko9cd26a82016-05-31 08:58:48 +02001060API int
1061nc_session_ntf_thread_running(const struct nc_session *session)
1062{
1063 if (!session) {
1064 ERRARG("session");
1065 return 0;
1066 }
1067
1068 return session->ntf_tid ? 1 : 0;
1069}
1070
Michal Vaskob7558c52016-02-26 15:04:19 +01001071API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001072nc_client_init(void)
1073{
1074 nc_init();
1075}
1076
1077API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001078nc_client_destroy(void)
1079{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001080 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001081#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1082 nc_client_ch_del_bind(NULL, 0, 0);
1083#endif
1084#ifdef NC_ENABLED_SSH
1085 nc_client_ssh_destroy_opts();
1086#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001087#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001088 nc_client_tls_destroy_opts();
1089#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001090 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001091}
1092
Michal Vasko086311b2016-01-08 09:53:11 +01001093API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001094nc_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 +01001095{
1096 struct lyxml_elem *xml;
1097 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1098
Michal Vasko45e53ae2016-04-07 11:46:03 +02001099 if (!session) {
1100 ERRARG("session");
1101 return NC_MSG_ERROR;
1102 } else if (!rpc) {
1103 ERRARG("rpc");
1104 return NC_MSG_ERROR;
1105 } else if (!reply) {
1106 ERRARG("reply");
1107 return NC_MSG_ERROR;
1108 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1109 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001110 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001111 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001112 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001113 return NC_MSG_ERROR;
1114 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001115 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001116 *reply = NULL;
1117
1118 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001119
Michal Vasko71ba2da2016-05-04 10:53:16 +02001120 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001121 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001122 lyxml_free(session->ctx, xml);
1123 if (!(*reply)) {
1124 return NC_MSG_ERROR;
1125 }
1126 }
1127
1128 return msgtype;
1129}
1130
1131API NC_MSG_TYPE
1132nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1133{
1134 struct lyxml_elem *xml, *ev_time;
1135 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1136
Michal Vasko45e53ae2016-04-07 11:46:03 +02001137 if (!session) {
1138 ERRARG("session");
1139 return NC_MSG_ERROR;
1140 } else if (!notif) {
1141 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001142 return NC_MSG_ERROR;
1143 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001144 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001145 return NC_MSG_ERROR;
1146 }
1147
1148 msgtype = get_msg(session, timeout, 0, &xml);
1149
1150 if (msgtype == NC_MSG_NOTIF) {
1151 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001152 if (!*notif) {
1153 ERRMEM;
1154 lyxml_free(session->ctx, xml);
1155 return NC_MSG_ERROR;
1156 }
Michal Vasko086311b2016-01-08 09:53:11 +01001157
1158 /* eventTime */
1159 LY_TREE_FOR(xml->child, ev_time) {
1160 if (!strcmp(ev_time->name, "eventTime")) {
1161 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1162 /* lyd_parse does not know this element */
1163 lyxml_free(session->ctx, ev_time);
1164 break;
1165 }
1166 }
1167 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001168 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001169 goto fail;
1170 }
1171
1172 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001173 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001174 lyxml_free(session->ctx, xml);
1175 xml = NULL;
1176 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001177 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001178 goto fail;
1179 }
1180 }
1181
1182 return msgtype;
1183
1184fail:
1185 lydict_remove(session->ctx, (*notif)->datetime);
1186 lyd_free((*notif)->tree);
1187 free(*notif);
1188 *notif = NULL;
1189 lyxml_free(session->ctx, xml);
1190
1191 return NC_MSG_ERROR;
1192}
1193
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001194static void *
1195nc_recv_notif_thread(void *arg)
1196{
1197 struct nc_ntf_thread_arg *ntarg;
1198 struct nc_session *session;
1199 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1200 struct nc_notif *notif;
1201 NC_MSG_TYPE msgtype;
1202
1203 ntarg = (struct nc_ntf_thread_arg *)arg;
1204 session = ntarg->session;
1205 notif_clb = ntarg->notif_clb;
1206 free(ntarg);
1207
1208 while (session->ntf_tid) {
1209 msgtype = nc_recv_notif(session, 0, &notif);
1210 if (msgtype == NC_MSG_NOTIF) {
1211 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001212 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1213 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1214 nc_notif_free(notif);
1215 break;
1216 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001217 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001218 } else if (msgtype == NC_MSG_ERROR) {
1219 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001220 }
1221
1222 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1223 }
1224
Michal Vasko0651c902016-05-19 15:55:42 +02001225 VRB("Session %u: notification thread exit.", session->id);
1226 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001227 return NULL;
1228}
1229
1230API int
1231nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1232{
1233 struct nc_ntf_thread_arg *ntarg;
1234 int ret;
1235
Michal Vasko45e53ae2016-04-07 11:46:03 +02001236 if (!session) {
1237 ERRARG("session");
1238 return -1;
1239 } else if (!notif_clb) {
1240 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001241 return -1;
1242 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1243 ERR("Session %u: invalid session to receive Notifications.", session->id);
1244 return -1;
1245 } else if (session->ntf_tid) {
1246 ERR("Session %u: separate notification thread is already running.", session->id);
1247 return -1;
1248 }
1249
1250 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001251 if (!ntarg) {
1252 ERRMEM;
1253 return -1;
1254 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001255 ntarg->session = session;
1256 ntarg->notif_clb = notif_clb;
1257
1258 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1259 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001260 if (!session->ntf_tid) {
1261 ERRMEM;
1262 free(ntarg);
1263 return -1;
1264 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001265
1266 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1267 if (ret) {
1268 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1269 free(ntarg);
1270 free((pthread_t *)session->ntf_tid);
1271 session->ntf_tid = NULL;
1272 return -1;
1273 }
1274
1275 return 0;
1276}
1277
Michal Vasko086311b2016-01-08 09:53:11 +01001278API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001279nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001280{
1281 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001282 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001283 struct nc_rpc_generic *rpc_gen;
1284 struct nc_rpc_getconfig *rpc_gc;
1285 struct nc_rpc_edit *rpc_e;
1286 struct nc_rpc_copy *rpc_cp;
1287 struct nc_rpc_delete *rpc_del;
1288 struct nc_rpc_lock *rpc_lock;
1289 struct nc_rpc_get *rpc_g;
1290 struct nc_rpc_kill *rpc_k;
1291 struct nc_rpc_commit *rpc_com;
1292 struct nc_rpc_cancel *rpc_can;
1293 struct nc_rpc_validate *rpc_val;
1294 struct nc_rpc_getschema *rpc_gs;
1295 struct nc_rpc_subscribe *rpc_sub;
1296 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001297 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vaskod91f6e62016-04-05 11:34:22 +02001298 char str[11], *filter;
Michal Vasko086311b2016-01-08 09:53:11 +01001299 uint64_t cur_msgid;
1300
Michal Vasko45e53ae2016-04-07 11:46:03 +02001301 if (!session) {
1302 ERRARG("session");
1303 return NC_MSG_ERROR;
1304 } else if (!rpc) {
1305 ERRARG("rpc");
1306 return NC_MSG_ERROR;
1307 } else if (!msgid) {
1308 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001309 return NC_MSG_ERROR;
1310 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001311 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001312 return NC_MSG_ERROR;
1313 }
1314
1315 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1316 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1317 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001318 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001319 return NC_MSG_ERROR;
1320 }
1321 }
1322
1323 switch (rpc->type) {
1324 case NC_RPC_GENERIC:
1325 rpc_gen = (struct nc_rpc_generic *)rpc;
1326
1327 if (rpc_gen->has_data) {
1328 data = rpc_gen->content.data;
1329 } else {
Michal Vasko03df9e32016-05-17 10:22:53 +02001330 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001331 }
1332 break;
1333
1334 case NC_RPC_GETCONFIG:
1335 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1336
1337 data = lyd_new(NULL, ietfnc, "get-config");
1338 node = lyd_new(data, ietfnc, "source");
1339 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1340 if (!node) {
1341 lyd_free(data);
1342 return NC_MSG_ERROR;
1343 }
1344 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001345 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001346 /* we need a copy of the filter */
1347 filter = strdup(rpc_gc->filter);
1348 if (!filter) {
1349 ERRMEM;
1350 lyd_free(data);
1351 return NC_MSG_ERROR;
1352 }
1353
1354 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001355 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001356 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001357 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001358 lyd_insert_attr(node, NULL, "type", "xpath");
1359 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001360 }
1361 if (!node) {
1362 lyd_free(data);
1363 return NC_MSG_ERROR;
1364 }
1365 }
1366
1367 if (rpc_gc->wd_mode) {
1368 if (!ietfncwd) {
1369 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1370 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001371 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001372 return NC_MSG_ERROR;
1373 }
1374 }
1375 switch (rpc_gc->wd_mode) {
1376 case NC_WD_UNKNOWN:
1377 /* cannot get here */
1378 break;
1379 case NC_WD_ALL:
1380 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1381 break;
1382 case NC_WD_ALL_TAG:
1383 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1384 break;
1385 case NC_WD_TRIM:
1386 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1387 break;
1388 case NC_WD_EXPLICIT:
1389 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1390 break;
1391 }
1392 if (!node) {
1393 lyd_free(data);
1394 return NC_MSG_ERROR;
1395 }
1396 }
1397 break;
1398
1399 case NC_RPC_EDIT:
1400 rpc_e = (struct nc_rpc_edit *)rpc;
1401
1402 data = lyd_new(NULL, ietfnc, "edit-config");
1403 node = lyd_new(data, ietfnc, "target");
1404 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1405 if (!node) {
1406 lyd_free(data);
1407 return NC_MSG_ERROR;
1408 }
1409
1410 if (rpc_e->default_op) {
1411 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1412 if (!node) {
1413 lyd_free(data);
1414 return NC_MSG_ERROR;
1415 }
1416 }
1417
1418 if (rpc_e->test_opt) {
1419 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1420 if (!node) {
1421 lyd_free(data);
1422 return NC_MSG_ERROR;
1423 }
1424 }
1425
1426 if (rpc_e->error_opt) {
1427 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1428 if (!node) {
1429 lyd_free(data);
1430 return NC_MSG_ERROR;
1431 }
1432 }
1433
1434 if (rpc_e->edit_cont[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001435 /* we need a copy of the content */
1436 filter = strdup(rpc_e->edit_cont);
1437 if (!filter) {
1438 ERRMEM;
1439 lyd_free(data);
1440 return NC_MSG_ERROR;
1441 }
1442
1443 node = lyd_new_anyxml_str(data, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001444 } else {
1445 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1446 }
1447 if (!node) {
1448 lyd_free(data);
1449 return NC_MSG_ERROR;
1450 }
1451 break;
1452
1453 case NC_RPC_COPY:
1454 rpc_cp = (struct nc_rpc_copy *)rpc;
1455
1456 data = lyd_new(NULL, ietfnc, "copy-config");
1457 node = lyd_new(data, ietfnc, "target");
1458 if (rpc_cp->url_trg) {
1459 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1460 } else {
1461 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1462 }
1463 if (!node) {
1464 lyd_free(data);
1465 return NC_MSG_ERROR;
1466 }
1467
1468 node = lyd_new(data, ietfnc, "source");
1469 if (rpc_cp->url_config_src) {
1470 if (rpc_cp->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001471 /* we need a copy of the content */
1472 filter = strdup(rpc_cp->url_config_src);
1473 if (!filter) {
1474 ERRMEM;
1475 lyd_free(data);
1476 return NC_MSG_ERROR;
1477 }
1478
1479 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001480 } else {
1481 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1482 }
1483 } else {
1484 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1485 }
1486 if (!node) {
1487 lyd_free(data);
1488 return NC_MSG_ERROR;
1489 }
1490
1491 if (rpc_cp->wd_mode) {
1492 if (!ietfncwd) {
1493 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1494 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001495 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001496 return NC_MSG_ERROR;
1497 }
1498 }
1499 switch (rpc_cp->wd_mode) {
1500 case NC_WD_UNKNOWN:
1501 /* cannot get here */
1502 break;
1503 case NC_WD_ALL:
1504 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1505 break;
1506 case NC_WD_ALL_TAG:
1507 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1508 break;
1509 case NC_WD_TRIM:
1510 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1511 break;
1512 case NC_WD_EXPLICIT:
1513 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1514 break;
1515 }
1516 if (!node) {
1517 lyd_free(data);
1518 return NC_MSG_ERROR;
1519 }
1520 }
1521 break;
1522
1523 case NC_RPC_DELETE:
1524 rpc_del = (struct nc_rpc_delete *)rpc;
1525
1526 data = lyd_new(NULL, ietfnc, "delete-config");
1527 node = lyd_new(data, ietfnc, "target");
1528 if (rpc_del->url) {
1529 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1530 } else {
1531 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1532 }
1533 if (!node) {
1534 lyd_free(data);
1535 return NC_MSG_ERROR;
1536 }
1537 break;
1538
1539 case NC_RPC_LOCK:
1540 rpc_lock = (struct nc_rpc_lock *)rpc;
1541
1542 data = lyd_new(NULL, ietfnc, "lock");
1543 node = lyd_new(data, ietfnc, "target");
1544 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1545 if (!node) {
1546 lyd_free(data);
1547 return NC_MSG_ERROR;
1548 }
1549 break;
1550
1551 case NC_RPC_UNLOCK:
1552 rpc_lock = (struct nc_rpc_lock *)rpc;
1553
1554 data = lyd_new(NULL, ietfnc, "unlock");
1555 node = lyd_new(data, ietfnc, "target");
1556 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1557 if (!node) {
1558 lyd_free(data);
1559 return NC_MSG_ERROR;
1560 }
1561 break;
1562
1563 case NC_RPC_GET:
1564 rpc_g = (struct nc_rpc_get *)rpc;
1565
1566 data = lyd_new(NULL, ietfnc, "get");
1567 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001568 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001569 /* we need a copy of the filter */
1570 filter = strdup(rpc_g->filter);
1571 if (!filter) {
1572 ERRMEM;
1573 lyd_free(data);
1574 return NC_MSG_ERROR;
1575 }
1576
1577 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001578 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001579 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001580 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001581 lyd_insert_attr(node, NULL, "type", "xpath");
1582 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001583 }
1584 if (!node) {
1585 lyd_free(data);
1586 return NC_MSG_ERROR;
1587 }
1588 }
1589
1590 if (rpc_g->wd_mode) {
1591 if (!ietfncwd) {
1592 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1593 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001594 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001595 return NC_MSG_ERROR;
1596 }
1597 }
1598 switch (rpc_g->wd_mode) {
1599 case NC_WD_UNKNOWN:
1600 /* cannot get here */
1601 break;
1602 case NC_WD_ALL:
1603 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1604 break;
1605 case NC_WD_ALL_TAG:
1606 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1607 break;
1608 case NC_WD_TRIM:
1609 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1610 break;
1611 case NC_WD_EXPLICIT:
1612 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1613 break;
1614 }
1615 if (!node) {
1616 lyd_free(data);
1617 return NC_MSG_ERROR;
1618 }
1619 }
1620 break;
1621
1622 case NC_RPC_KILL:
1623 rpc_k = (struct nc_rpc_kill *)rpc;
1624
1625 data = lyd_new(NULL, ietfnc, "kill-session");
1626 sprintf(str, "%u", rpc_k->sid);
1627 lyd_new_leaf(data, ietfnc, "session-id", str);
1628 break;
1629
1630 case NC_RPC_COMMIT:
1631 rpc_com = (struct nc_rpc_commit *)rpc;
1632
1633 data = lyd_new(NULL, ietfnc, "commit");
1634 if (rpc_com->confirmed) {
1635 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1636 }
1637
1638 if (rpc_com->confirm_timeout) {
1639 sprintf(str, "%u", rpc_com->confirm_timeout);
1640 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1641 }
1642
1643 if (rpc_com->persist) {
1644 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1645 if (!node) {
1646 lyd_free(data);
1647 return NC_MSG_ERROR;
1648 }
1649 }
1650
1651 if (rpc_com->persist_id) {
1652 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1653 if (!node) {
1654 lyd_free(data);
1655 return NC_MSG_ERROR;
1656 }
1657 }
1658 break;
1659
1660 case NC_RPC_DISCARD:
1661 data = lyd_new(NULL, ietfnc, "discard-changes");
1662 break;
1663
1664 case NC_RPC_CANCEL:
1665 rpc_can = (struct nc_rpc_cancel *)rpc;
1666
1667 data = lyd_new(NULL, ietfnc, "cancel-commit");
1668 if (rpc_can->persist_id) {
1669 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1670 if (!node) {
1671 lyd_free(data);
1672 return NC_MSG_ERROR;
1673 }
1674 }
1675 break;
1676
1677 case NC_RPC_VALIDATE:
1678 rpc_val = (struct nc_rpc_validate *)rpc;
1679
1680 data = lyd_new(NULL, ietfnc, "validate");
1681 node = lyd_new(data, ietfnc, "source");
1682 if (rpc_val->url_config_src) {
1683 if (rpc_val->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001684 /* we need a copy of the config */
1685 filter = strdup(rpc_val->url_config_src);
1686 if (!filter) {
1687 ERRMEM;
1688 lyd_free(data);
1689 return NC_MSG_ERROR;
1690 }
1691
1692 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001693 } else {
1694 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1695 }
1696 } else {
1697 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1698 }
1699 if (!node) {
1700 lyd_free(data);
1701 return NC_MSG_ERROR;
1702 }
1703 break;
1704
1705 case NC_RPC_GETSCHEMA:
1706 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1707 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001708 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001709 return NC_MSG_ERROR;
1710 }
1711
1712 rpc_gs = (struct nc_rpc_getschema *)rpc;
1713
1714 data = lyd_new(NULL, ietfncmon, "get-schema");
1715 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1716 if (!node) {
1717 lyd_free(data);
1718 return NC_MSG_ERROR;
1719 }
1720 if (rpc_gs->version) {
1721 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1722 if (!node) {
1723 lyd_free(data);
1724 return NC_MSG_ERROR;
1725 }
1726 }
1727 if (rpc_gs->format) {
1728 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1729 if (!node) {
1730 lyd_free(data);
1731 return NC_MSG_ERROR;
1732 }
1733 }
1734 break;
1735
1736 case NC_RPC_SUBSCRIBE:
1737 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1738 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001739 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001740 return NC_MSG_ERROR;
1741 }
1742
1743 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1744
1745 data = lyd_new(NULL, notifs, "create-subscription");
1746 if (rpc_sub->stream) {
1747 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1748 if (!node) {
1749 lyd_free(data);
1750 return NC_MSG_ERROR;
1751 }
1752 }
1753
1754 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001755 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001756 /* we need a copy of the filter */
1757 filter = strdup(rpc_sub->filter);
1758 if (!filter) {
1759 ERRMEM;
1760 lyd_free(data);
1761 return NC_MSG_ERROR;
1762 }
1763
1764 node = lyd_new_anyxml_str(data, notifs, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001765 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001766 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001767 node = lyd_new_anyxml_str(data, notifs, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001768 lyd_insert_attr(node, NULL, "type", "xpath");
1769 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001770 }
1771 if (!node) {
1772 lyd_free(data);
1773 return NC_MSG_ERROR;
1774 }
1775 }
1776
1777 if (rpc_sub->start) {
1778 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1779 if (!node) {
1780 lyd_free(data);
1781 return NC_MSG_ERROR;
1782 }
1783 }
1784
1785 if (rpc_sub->stop) {
1786 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1787 if (!node) {
1788 lyd_free(data);
1789 return NC_MSG_ERROR;
1790 }
1791 }
1792 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001793 default:
1794 ERRINT;
1795 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001796 }
1797
Michal Vasko1d8212b2016-04-13 14:24:52 +02001798 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001799 lyd_free(data);
1800 return NC_MSG_ERROR;
1801 }
1802
Michal Vasko62be1ce2016-03-03 13:24:52 +01001803 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001804 if (ret == -1) {
1805 /* error */
1806 r = NC_MSG_ERROR;
1807 } else if (!ret) {
1808 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001809 r = NC_MSG_WOULDBLOCK;
1810 } else {
1811 /* send RPC, store its message ID */
1812 r = nc_send_msg(session, data);
1813 cur_msgid = session->msgid;
1814 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001815 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001816
1817 lyd_free(data);
1818
1819 if (r != NC_MSG_RPC) {
1820 return r;
1821 }
1822
1823 *msgid = cur_msgid;
1824 return NC_MSG_RPC;
1825}