blob: c0488373c767cbd4c0eecee36581955c6d26ea1c [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 Vasko36b17ba2016-05-03 11:54:21 +0200219 msg = nc_recv_reply(session, rpc, msgid, 1000, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100220 nc_rpc_free(rpc);
221 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100222 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 return NULL;
224 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100225 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100226 return NULL;
227 }
228
Michal Vasko05ba9df2016-01-13 14:40:27 +0100229 if (reply->type != NC_RPL_DATA) {
230 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100231 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100232 nc_reply_free(reply);
233 return NULL;
234 }
235
Michal Vasko086311b2016-01-08 09:53:11 +0100236 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200237 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
238 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
239 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
240 nc_reply_free(reply);
241 return NULL;
242 }
243 get_schema_data = (struct lyd_node_anyxml *)data_rpl->data->child;
244 if (get_schema_data->xml_struct) {
245 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200246 } else {
Michal Vaskob2583f12016-05-12 11:40:23 +0200247 model_data = strdup(get_schema_data->value.str);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200248 }
Michal Vasko086311b2016-01-08 09:53:11 +0100249 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100250 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100251
Michal Vasko086311b2016-01-08 09:53:11 +0100252 return model_data;
253}
254
Michal Vaskoef578332016-01-25 13:20:09 +0100255/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100256int
257nc_ctx_check_and_fill(struct nc_session *session)
258{
Michal Vaskoef578332016-01-25 13:20:09 +0100259 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100260 ly_module_clb old_clb = NULL;
261 void *old_data = NULL;
262
263 assert(session->cpblts && session->ctx);
264
265 /* check if get-schema is supported */
266 for (i = 0; session->cpblts[i]; ++i) {
267 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
268 get_schema_support = 1;
269 break;
270 }
271 }
272
273 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
274 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
275 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
276 /* set module retrieval using <get-schema> */
277 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100278 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100279 } else {
280 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
281 }
282 }
283
284 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100285 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100286 if (old_clb) {
287 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
288 }
Michal Vaskoef578332016-01-25 13:20:09 +0100289 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100290 }
291
292 /* load all other models */
293 for (i = 0; session->cpblts[i]; ++i) {
294 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
295 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
296 continue;
297 }
298
Michal Vaskoef578332016-01-25 13:20:09 +0100299 r = ctx_check_and_load_model(session, session->cpblts[i]);
300 if (r == -1) {
301 ret = -1;
302 break;
303 }
304
305 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
306 * if it was using get-schema */
307 if (r == 1) {
308 if (get_schema_support) {
309 VRB("Trying to load the schema from a different source.");
310 /* works even if old_clb is NULL */
311 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
312 r = ctx_check_and_load_model(session, session->cpblts[i]);
313 }
314
315 /* fail again (or no other way to try), too bad */
316 if (r) {
317 ret = 1;
318 }
319
320 /* set get-schema callback back */
321 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
322 }
Michal Vasko086311b2016-01-08 09:53:11 +0100323 }
324
325 if (old_clb) {
326 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
327 }
Michal Vaskoef578332016-01-25 13:20:09 +0100328 if (ret == 1) {
329 WRN("Some models failed to be loaded, any data from these models will be ignored.");
330 }
331 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100332}
333
334API struct nc_session *
335nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
336{
Michal Vaskod083db62016-01-19 10:31:29 +0100337 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100338
Michal Vasko45e53ae2016-04-07 11:46:03 +0200339 if (fdin < 0) {
340 ERRARG("fdin");
341 return NULL;
342 } else if (fdout < 0) {
343 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100344 return NULL;
345 }
346
347 /* prepare session structure */
348 session = calloc(1, sizeof *session);
349 if (!session) {
350 ERRMEM;
351 return NULL;
352 }
353 session->status = NC_STATUS_STARTING;
354 session->side = NC_CLIENT;
355
356 /* transport specific data */
357 session->ti_type = NC_TI_FD;
358 session->ti.fd.in = fdin;
359 session->ti.fd.out = fdout;
360
361 /* assign context (dicionary needed for handshake) */
362 if (!ctx) {
363 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100364 /* definitely should not happen, but be ready */
365 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
366 /* that's just it */
367 goto fail;
368 }
Michal Vasko086311b2016-01-08 09:53:11 +0100369 } else {
370 session->flags |= NC_SESSION_SHAREDCTX;
371 }
372 session->ctx = ctx;
373
374 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200375 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100376 goto fail;
377 }
378 session->status = NC_STATUS_RUNNING;
379
Michal Vaskoef578332016-01-25 13:20:09 +0100380 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100381 goto fail;
382 }
383
384 return session;
385
386fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100387 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100388 return NULL;
389}
390
391int
Michal Vaskof05562c2016-01-20 12:06:43 +0100392nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100393{
Michal Vasko0190bc32016-03-02 15:47:49 +0100394 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100395 struct addrinfo hints, *res_list, *res;
396 char port_s[6]; /* length of string representation of short int */
397
398 snprintf(port_s, 6, "%u", port);
399
400 /* Connect to a server */
401 memset(&hints, 0, sizeof hints);
402 hints.ai_family = AF_UNSPEC;
403 hints.ai_socktype = SOCK_STREAM;
404 hints.ai_protocol = IPPROTO_TCP;
405 i = getaddrinfo(host, port_s, &hints, &res_list);
406 if (i != 0) {
407 ERR("Unable to translate the host address (%s).", gai_strerror(i));
408 return -1;
409 }
410
411 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
412 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
413 if (sock == -1) {
414 /* socket was not created, try another resource */
415 i = errno;
416 goto errloop;
417 }
418
419 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
420 /* network connection failed, try another resource */
421 i = errno;
422 close(sock);
423 sock = -1;
424 goto errloop;
425 }
426
Michal Vasko0190bc32016-03-02 15:47:49 +0100427 /* make the socket non-blocking */
428 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
429 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100430 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100431 return -1;
432 }
433
Michal Vasko086311b2016-01-08 09:53:11 +0100434 /* we're done, network connection established */
435 break;
436errloop:
437 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
438 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
439 continue;
440 }
441
442 if (sock == -1) {
443 ERR("Unable to connect to %s:%s.", host, port_s);
444 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100445 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 +0100446 }
447 freeaddrinfo(res_list);
448
449 return sock;
450}
451
Michal Vasko086311b2016-01-08 09:53:11 +0100452static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100453get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100454{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100455 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100456 char *ptr;
457 const char *str_msgid;
458 uint64_t cur_msgid;
459 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100460 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100461 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
462
Michal Vasko62be1ce2016-03-03 13:24:52 +0100463 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100464 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100465 /* error */
466 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100467 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100468 /* timeout */
469 return NC_MSG_WOULDBLOCK;
470 }
471
472 /* try to get notification from the session's queue */
473 if (!msgid && session->notifs) {
474 cont = session->notifs;
475 session->notifs = cont->next;
476
Michal Vasko71ba2da2016-05-04 10:53:16 +0200477 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100478 free(cont);
479
Michal Vasko71ba2da2016-05-04 10:53:16 +0200480 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100481 }
482
483 /* try to get rpc-reply from the session's queue */
484 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200485 cont = session->replies;
486 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100487
Michal Vasko71ba2da2016-05-04 10:53:16 +0200488 xml = cont->msg;
489 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100490
Michal Vasko71ba2da2016-05-04 10:53:16 +0200491 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100492 }
493
Michal Vasko71ba2da2016-05-04 10:53:16 +0200494 if (!msgtype) {
495 /* read message from wire */
496 msgtype = nc_read_msg_poll(session, timeout, &xml);
497 }
Michal Vasko086311b2016-01-08 09:53:11 +0100498
499 /* we read rpc-reply, want a notif */
500 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100501 cont_ptr = &session->replies;
502 while (*cont_ptr) {
503 cont_ptr = &((*cont_ptr)->next);
504 }
505 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100506 if (!*cont_ptr) {
507 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200508 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100509 lyxml_free(session->ctx, xml);
510 return NC_MSG_ERROR;
511 }
Michal Vasko086311b2016-01-08 09:53:11 +0100512 (*cont_ptr)->msg = xml;
513 (*cont_ptr)->next = NULL;
514 }
515
516 /* we read notif, want a rpc-reply */
517 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100518 /* TODO check whether the session is even subscribed */
519 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100520 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100521 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100522 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100523 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100524 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100525
526 cont_ptr = &session->notifs;
527 while (*cont_ptr) {
528 cont_ptr = &((*cont_ptr)->next);
529 }
530 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100531 if (!cont_ptr) {
532 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200533 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100534 lyxml_free(session->ctx, xml);
535 return NC_MSG_ERROR;
536 }
Michal Vasko086311b2016-01-08 09:53:11 +0100537 (*cont_ptr)->msg = xml;
538 (*cont_ptr)->next = NULL;
539 }
540
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100541 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100542
543 switch (msgtype) {
544 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100545 if (!msgid) {
546 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100547 }
Michal Vasko086311b2016-01-08 09:53:11 +0100548 break;
549
550 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100551 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200552 /* check message-id */
553 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
554 if (!str_msgid) {
555 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
556 msgtype = NC_MSG_REPLY_ERR_MSGID;
557 } else {
558 cur_msgid = strtoul(str_msgid, &ptr, 10);
559 if (cur_msgid != msgid) {
560 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
561 session->id, str_msgid);
562 msgtype = NC_MSG_REPLY_ERR_MSGID;
563 }
564 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100565 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100566 }
Michal Vasko086311b2016-01-08 09:53:11 +0100567 break;
568
569 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100570 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100571 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200572 msgtype = NC_MSG_ERROR;
573 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100574
575 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100576 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100577 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200578 msgtype = NC_MSG_ERROR;
579 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100580
581 default:
582 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
583 * NC_MSG_NONE is not returned by nc_read_msg()
584 */
585 break;
586 }
587
588 return msgtype;
589}
590
591/* cannot strictly fail, but does not need to fill any error parameter at all */
592static void
593parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
594{
595 struct lyxml_elem *iter, *next, *info;
596
597 LY_TREE_FOR(xml->child, iter) {
598 if (!iter->ns) {
599 if (iter->content) {
600 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
601 } else {
602 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
603 }
604 continue;
605 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
606 if (iter->content) {
607 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
608 iter->name, iter->content, iter->ns->value);
609 } else {
610 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
611 }
612 continue;
613 }
614
615 if (!strcmp(iter->name, "error-type")) {
616 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
617 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
618 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
619 } else if (err->type) {
620 WRN("<rpc-error> <error-type> duplicated.");
621 } else {
622 err->type = lydict_insert(ctx, iter->content, 0);
623 }
624 } else if (!strcmp(iter->name, "error-tag")) {
625 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
626 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
627 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
628 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
629 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
630 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
631 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
632 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
633 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
634 && strcmp(iter->content, "malformed-message"))) {
635 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
636 } else if (err->tag) {
637 WRN("<rpc-error> <error-tag> duplicated.");
638 } else {
639 err->tag = lydict_insert(ctx, iter->content, 0);
640 }
641 } else if (!strcmp(iter->name, "error-severity")) {
642 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
643 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
644 } else if (err->severity) {
645 WRN("<rpc-error> <error-severity> duplicated.");
646 } else {
647 err->severity = lydict_insert(ctx, iter->content, 0);
648 }
649 } else if (!strcmp(iter->name, "error-app-tag")) {
650 if (err->apptag) {
651 WRN("<rpc-error> <error-app-tag> duplicated.");
652 } else {
653 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
654 }
655 } else if (!strcmp(iter->name, "error-path")) {
656 if (err->path) {
657 WRN("<rpc-error> <error-path> duplicated.");
658 } else {
659 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
660 }
661 } else if (!strcmp(iter->name, "error-message")) {
662 if (err->message) {
663 WRN("<rpc-error> <error-message> duplicated.");
664 } else {
665 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
666 if (!err->message_lang) {
667 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
668 }
669 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
670 }
671 } else if (!strcmp(iter->name, "error-info")) {
672 LY_TREE_FOR_SAFE(iter->child, next, info) {
673 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
674 if (!strcmp(info->name, "session-id")) {
675 if (err->sid) {
676 WRN("<rpc-error> <error-info> <session-id> duplicated.");
677 } else {
678 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
679 }
680 } else if (!strcmp(info->name, "bad-attr")) {
681 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100682 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
683 if (!err->attr) {
684 ERRMEM;
685 return;
686 }
Michal Vasko086311b2016-01-08 09:53:11 +0100687 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
688 } else if (!strcmp(info->name, "bad-element")) {
689 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100690 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
691 if (!err->elem) {
692 ERRMEM;
693 return;
694 }
Michal Vasko086311b2016-01-08 09:53:11 +0100695 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
696 } else if (!strcmp(info->name, "bad-namespace")) {
697 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100698 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
699 if (!err->ns) {
700 ERRMEM;
701 return;
702 }
Michal Vasko086311b2016-01-08 09:53:11 +0100703 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
704 } else {
705 if (info->content) {
706 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
707 info->name, info->content);
708 } else {
709 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
710 }
711 }
712 } else {
713 lyxml_unlink(ctx, info);
714 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100715 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
716 if (!err->other) {
717 ERRMEM;
718 return;
719 }
Michal Vasko086311b2016-01-08 09:53:11 +0100720 err->other[err->other_count - 1] = info;
721 }
722 }
723 } else {
724 if (iter->content) {
725 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
726 } else {
727 WRN("<rpc-error> unknown child \"%s\".", iter->name);
728 }
729 }
730 }
731}
732
733static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100734parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100735{
736 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100737 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100738 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100739 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100740 struct nc_reply_data *data_rpl;
741 struct nc_reply *reply = NULL;
742 struct nc_rpc_generic *rpc_gen;
743 int i;
744
745 if (!xml->child) {
746 ERR("An empty <rpc-reply>.");
747 return NULL;
748 }
749
750 /* rpc-error */
751 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
752 /* count and check elements */
753 i = 0;
754 LY_TREE_FOR(xml->child, iter) {
755 if (strcmp(iter->name, "rpc-error")) {
756 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
757 return NULL;
758 } else if (!iter->ns) {
759 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
760 return NULL;
761 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
762 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
763 return NULL;
764 }
765 ++i;
766 }
767
768 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100769 if (!error_rpl) {
770 ERRMEM;
771 return NULL;
772 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100773 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100774 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100775 if (!error_rpl->err) {
776 ERRMEM;
777 free(error_rpl);
778 return NULL;
779 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100780 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100781 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100782 reply = (struct nc_reply *)error_rpl;
783
784 i = 0;
785 LY_TREE_FOR(xml->child, iter) {
786 parse_rpc_error(ctx, iter, error_rpl->err + i);
787 ++i;
788 }
789
790 /* ok */
791 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
792 if (xml->child->next) {
793 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
794 return NULL;
795 }
796 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100797 if (!reply) {
798 ERRMEM;
799 return NULL;
800 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100801 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100802
803 /* some RPC output */
804 } else {
805 switch (rpc->type) {
806 case NC_RPC_GENERIC:
807 rpc_gen = (struct nc_rpc_generic *)rpc;
808
809 if (rpc_gen->has_data) {
810 schema = rpc_gen->content.data->schema;
811 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100812 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100813 if (!data) {
814 ERR("Failed to parse a generic RPC XML.");
815 return NULL;
816 }
817 schema = data->schema;
818 lyd_free(data);
819 data = NULL;
820 }
821 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100822 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100823 return NULL;
824 }
825 break;
826
827 case NC_RPC_GETCONFIG:
828 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100829 if (!xml->child->child) {
830 /* we did not receive any data */
831 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100832 if (!data_rpl) {
833 ERRMEM;
834 return NULL;
835 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100836 data_rpl->type = NC_RPL_DATA;
837 data_rpl->data = NULL;
838 return (struct nc_reply *)data_rpl;
839 }
840
Michal Vasko086311b2016-01-08 09:53:11 +0100841 /* special treatment */
842 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100843 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100844 if (!data) {
845 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
846 return NULL;
847 }
848 break;
849
850 case NC_RPC_GETSCHEMA:
Michal Vasko303245c2016-03-24 15:20:03 +0100851 schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100852 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100853 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100854 return NULL;
855 }
856 break;
857
858 case NC_RPC_EDIT:
859 case NC_RPC_COPY:
860 case NC_RPC_DELETE:
861 case NC_RPC_LOCK:
862 case NC_RPC_UNLOCK:
863 case NC_RPC_KILL:
864 case NC_RPC_COMMIT:
865 case NC_RPC_DISCARD:
866 case NC_RPC_CANCEL:
867 case NC_RPC_VALIDATE:
868 case NC_RPC_SUBSCRIBE:
869 /* there is no output defined */
870 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
871 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100872 default:
873 ERRINT;
874 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100875 }
876
877 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100878 if (!data_rpl) {
879 ERRMEM;
880 return NULL;
881 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100882 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100883 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100884 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100885 } else {
886 /* <get>, <get-config> */
887 data_rpl->data = data;
888 }
889 if (!data_rpl->data) {
890 ERR("Failed to parse <rpc-reply>.");
891 free(data_rpl);
892 return NULL;
893 }
894 reply = (struct nc_reply *)data_rpl;
895 }
896
897 return reply;
898}
899
Radek Krejci53691be2016-02-22 13:58:37 +0100900#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100901
Michal Vasko3031aae2016-01-27 16:07:18 +0100902int
903nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
904{
905 int sock;
906
Michal Vasko45e53ae2016-04-07 11:46:03 +0200907 if (!address) {
908 ERRARG("address");
909 return -1;
910 } else if (!port) {
911 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100912 return -1;
913 }
914
915 sock = nc_sock_listen(address, port);
916 if (sock == -1) {
917 return -1;
918 }
919
920 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100921 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
922 if (!client_opts.ch_binds) {
923 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100924 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100925 return -1;
926 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100927
928 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100929 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
930 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100931 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100932 return -1;
933 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100934 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
935 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
936 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
937
938 return 0;
939}
940
941int
942nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
943{
944 uint32_t i;
945 int ret = -1;
946
947 if (!address && !port && !ti) {
948 for (i = 0; i < client_opts.ch_bind_count; ++i) {
949 close(client_opts.ch_binds[i].sock);
950 free((char *)client_opts.ch_binds[i].address);
951
952 ret = 0;
953 }
954 free(client_opts.ch_binds);
955 client_opts.ch_binds = NULL;
956 client_opts.ch_bind_count = 0;
957 } else {
958 for (i = 0; i < client_opts.ch_bind_count; ++i) {
959 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
960 && (!port || (client_opts.ch_binds[i].port == port))
961 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
962 close(client_opts.ch_binds[i].sock);
963 free((char *)client_opts.ch_binds[i].address);
964
965 --client_opts.ch_bind_count;
966 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
967
968 ret = 0;
969 }
970 }
971 }
972
973 return ret;
974}
975
976API int
977nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
978{
979 int sock;
980 char *host = NULL;
981 uint16_t port, idx;
982
Michal Vasko45e53ae2016-04-07 11:46:03 +0200983 if (!client_opts.ch_binds) {
984 ERRINIT;
985 return -1;
986 } else if (!session) {
987 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +0100988 return -1;
989 }
990
991 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
992
Michal Vasko50456e82016-02-02 12:16:08 +0100993 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100994 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100995 return sock;
996 }
997
Radek Krejci53691be2016-02-22 13:58:37 +0100998#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100999 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001000 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001001 } else
1002#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001003#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001004 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001005 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001006 } else
1007#endif
1008 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001009 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001010 *session = NULL;
1011 }
1012
1013 free(host);
1014
1015 if (!(*session)) {
1016 return -1;
1017 }
1018
1019 return 1;
1020}
1021
Radek Krejci53691be2016-02-22 13:58:37 +01001022#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001023
Michal Vaskobdfb5242016-05-24 09:11:01 +02001024API const char **
1025nc_session_get_cpblts(const struct nc_session *session)
1026{
1027 if (!session) {
1028 ERRARG("session");
1029 return NULL;
1030 }
1031
1032 return session->cpblts;
1033}
1034
1035API const char *
1036nc_session_cpblt(const struct nc_session *session, const char *capab)
1037{
1038 int i, len;
1039
1040 if (!session) {
1041 ERRARG("session");
1042 return NULL;
1043 } else if (!capab) {
1044 ERRARG("capab");
1045 return NULL;
1046 }
1047
1048 len = strlen(capab);
1049 for (i = 0; session->cpblts[i]; ++i) {
1050 if (!strncmp(session->cpblts[i], capab, len)) {
1051 return session->cpblts[i];
1052 }
1053 }
1054
1055 return NULL;
1056}
1057
Michal Vasko9cd26a82016-05-31 08:58:48 +02001058API int
1059nc_session_ntf_thread_running(const struct nc_session *session)
1060{
1061 if (!session) {
1062 ERRARG("session");
1063 return 0;
1064 }
1065
1066 return session->ntf_tid ? 1 : 0;
1067}
1068
Michal Vaskob7558c52016-02-26 15:04:19 +01001069API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001070nc_client_init(void)
1071{
1072 nc_init();
1073}
1074
1075API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001076nc_client_destroy(void)
1077{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001078 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001079#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1080 nc_client_ch_del_bind(NULL, 0, 0);
1081#endif
1082#ifdef NC_ENABLED_SSH
1083 nc_client_ssh_destroy_opts();
1084#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001085#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001086 nc_client_tls_destroy_opts();
1087#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001088 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001089}
1090
Michal Vasko086311b2016-01-08 09:53:11 +01001091API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001092nc_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 +01001093{
1094 struct lyxml_elem *xml;
1095 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1096
Michal Vasko45e53ae2016-04-07 11:46:03 +02001097 if (!session) {
1098 ERRARG("session");
1099 return NC_MSG_ERROR;
1100 } else if (!rpc) {
1101 ERRARG("rpc");
1102 return NC_MSG_ERROR;
1103 } else if (!reply) {
1104 ERRARG("reply");
1105 return NC_MSG_ERROR;
1106 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1107 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001108 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001109 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001110 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001111 return NC_MSG_ERROR;
1112 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001113 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001114 *reply = NULL;
1115
1116 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001117
Michal Vasko71ba2da2016-05-04 10:53:16 +02001118 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001119 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001120 lyxml_free(session->ctx, xml);
1121 if (!(*reply)) {
1122 return NC_MSG_ERROR;
1123 }
1124 }
1125
1126 return msgtype;
1127}
1128
1129API NC_MSG_TYPE
1130nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1131{
1132 struct lyxml_elem *xml, *ev_time;
1133 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1134
Michal Vasko45e53ae2016-04-07 11:46:03 +02001135 if (!session) {
1136 ERRARG("session");
1137 return NC_MSG_ERROR;
1138 } else if (!notif) {
1139 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001140 return NC_MSG_ERROR;
1141 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001142 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001143 return NC_MSG_ERROR;
1144 }
1145
1146 msgtype = get_msg(session, timeout, 0, &xml);
1147
1148 if (msgtype == NC_MSG_NOTIF) {
1149 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001150 if (!*notif) {
1151 ERRMEM;
1152 lyxml_free(session->ctx, xml);
1153 return NC_MSG_ERROR;
1154 }
Michal Vasko086311b2016-01-08 09:53:11 +01001155
1156 /* eventTime */
1157 LY_TREE_FOR(xml->child, ev_time) {
1158 if (!strcmp(ev_time->name, "eventTime")) {
1159 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1160 /* lyd_parse does not know this element */
1161 lyxml_free(session->ctx, ev_time);
1162 break;
1163 }
1164 }
1165 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001166 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001167 goto fail;
1168 }
1169
1170 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001171 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001172 lyxml_free(session->ctx, xml);
1173 xml = NULL;
1174 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001175 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001176 goto fail;
1177 }
1178 }
1179
1180 return msgtype;
1181
1182fail:
1183 lydict_remove(session->ctx, (*notif)->datetime);
1184 lyd_free((*notif)->tree);
1185 free(*notif);
1186 *notif = NULL;
1187 lyxml_free(session->ctx, xml);
1188
1189 return NC_MSG_ERROR;
1190}
1191
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001192static void *
1193nc_recv_notif_thread(void *arg)
1194{
1195 struct nc_ntf_thread_arg *ntarg;
1196 struct nc_session *session;
1197 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1198 struct nc_notif *notif;
1199 NC_MSG_TYPE msgtype;
1200
1201 ntarg = (struct nc_ntf_thread_arg *)arg;
1202 session = ntarg->session;
1203 notif_clb = ntarg->notif_clb;
1204 free(ntarg);
1205
1206 while (session->ntf_tid) {
1207 msgtype = nc_recv_notif(session, 0, &notif);
1208 if (msgtype == NC_MSG_NOTIF) {
1209 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001210 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1211 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1212 nc_notif_free(notif);
1213 break;
1214 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001215 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001216 } else if (msgtype == NC_MSG_ERROR) {
1217 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001218 }
1219
1220 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1221 }
1222
Michal Vasko0651c902016-05-19 15:55:42 +02001223 VRB("Session %u: notification thread exit.", session->id);
1224 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001225 return NULL;
1226}
1227
1228API int
1229nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1230{
1231 struct nc_ntf_thread_arg *ntarg;
1232 int ret;
1233
Michal Vasko45e53ae2016-04-07 11:46:03 +02001234 if (!session) {
1235 ERRARG("session");
1236 return -1;
1237 } else if (!notif_clb) {
1238 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001239 return -1;
1240 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1241 ERR("Session %u: invalid session to receive Notifications.", session->id);
1242 return -1;
1243 } else if (session->ntf_tid) {
1244 ERR("Session %u: separate notification thread is already running.", session->id);
1245 return -1;
1246 }
1247
1248 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001249 if (!ntarg) {
1250 ERRMEM;
1251 return -1;
1252 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001253 ntarg->session = session;
1254 ntarg->notif_clb = notif_clb;
1255
1256 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1257 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001258 if (!session->ntf_tid) {
1259 ERRMEM;
1260 free(ntarg);
1261 return -1;
1262 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001263
1264 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1265 if (ret) {
1266 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1267 free(ntarg);
1268 free((pthread_t *)session->ntf_tid);
1269 session->ntf_tid = NULL;
1270 return -1;
1271 }
1272
1273 return 0;
1274}
1275
Michal Vasko086311b2016-01-08 09:53:11 +01001276API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001277nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001278{
1279 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001280 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001281 struct nc_rpc_generic *rpc_gen;
1282 struct nc_rpc_getconfig *rpc_gc;
1283 struct nc_rpc_edit *rpc_e;
1284 struct nc_rpc_copy *rpc_cp;
1285 struct nc_rpc_delete *rpc_del;
1286 struct nc_rpc_lock *rpc_lock;
1287 struct nc_rpc_get *rpc_g;
1288 struct nc_rpc_kill *rpc_k;
1289 struct nc_rpc_commit *rpc_com;
1290 struct nc_rpc_cancel *rpc_can;
1291 struct nc_rpc_validate *rpc_val;
1292 struct nc_rpc_getschema *rpc_gs;
1293 struct nc_rpc_subscribe *rpc_sub;
1294 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001295 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vaskod91f6e62016-04-05 11:34:22 +02001296 char str[11], *filter;
Michal Vasko086311b2016-01-08 09:53:11 +01001297 uint64_t cur_msgid;
1298
Michal Vasko45e53ae2016-04-07 11:46:03 +02001299 if (!session) {
1300 ERRARG("session");
1301 return NC_MSG_ERROR;
1302 } else if (!rpc) {
1303 ERRARG("rpc");
1304 return NC_MSG_ERROR;
1305 } else if (!msgid) {
1306 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001307 return NC_MSG_ERROR;
1308 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001309 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001310 return NC_MSG_ERROR;
1311 }
1312
1313 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1314 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1315 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001316 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001317 return NC_MSG_ERROR;
1318 }
1319 }
1320
1321 switch (rpc->type) {
1322 case NC_RPC_GENERIC:
1323 rpc_gen = (struct nc_rpc_generic *)rpc;
1324
1325 if (rpc_gen->has_data) {
1326 data = rpc_gen->content.data;
1327 } else {
Michal Vasko03df9e32016-05-17 10:22:53 +02001328 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 +01001329 }
1330 break;
1331
1332 case NC_RPC_GETCONFIG:
1333 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1334
1335 data = lyd_new(NULL, ietfnc, "get-config");
1336 node = lyd_new(data, ietfnc, "source");
1337 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1338 if (!node) {
1339 lyd_free(data);
1340 return NC_MSG_ERROR;
1341 }
1342 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001343 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001344 /* we need a copy of the filter */
1345 filter = strdup(rpc_gc->filter);
1346 if (!filter) {
1347 ERRMEM;
1348 lyd_free(data);
1349 return NC_MSG_ERROR;
1350 }
1351
1352 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001353 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001354 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001355 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001356 lyd_insert_attr(node, NULL, "type", "xpath");
1357 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001358 }
1359 if (!node) {
1360 lyd_free(data);
1361 return NC_MSG_ERROR;
1362 }
1363 }
1364
1365 if (rpc_gc->wd_mode) {
1366 if (!ietfncwd) {
1367 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1368 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001369 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001370 return NC_MSG_ERROR;
1371 }
1372 }
1373 switch (rpc_gc->wd_mode) {
1374 case NC_WD_UNKNOWN:
1375 /* cannot get here */
1376 break;
1377 case NC_WD_ALL:
1378 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1379 break;
1380 case NC_WD_ALL_TAG:
1381 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1382 break;
1383 case NC_WD_TRIM:
1384 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1385 break;
1386 case NC_WD_EXPLICIT:
1387 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1388 break;
1389 }
1390 if (!node) {
1391 lyd_free(data);
1392 return NC_MSG_ERROR;
1393 }
1394 }
1395 break;
1396
1397 case NC_RPC_EDIT:
1398 rpc_e = (struct nc_rpc_edit *)rpc;
1399
1400 data = lyd_new(NULL, ietfnc, "edit-config");
1401 node = lyd_new(data, ietfnc, "target");
1402 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1403 if (!node) {
1404 lyd_free(data);
1405 return NC_MSG_ERROR;
1406 }
1407
1408 if (rpc_e->default_op) {
1409 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1410 if (!node) {
1411 lyd_free(data);
1412 return NC_MSG_ERROR;
1413 }
1414 }
1415
1416 if (rpc_e->test_opt) {
1417 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1418 if (!node) {
1419 lyd_free(data);
1420 return NC_MSG_ERROR;
1421 }
1422 }
1423
1424 if (rpc_e->error_opt) {
1425 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1426 if (!node) {
1427 lyd_free(data);
1428 return NC_MSG_ERROR;
1429 }
1430 }
1431
1432 if (rpc_e->edit_cont[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001433 /* we need a copy of the content */
1434 filter = strdup(rpc_e->edit_cont);
1435 if (!filter) {
1436 ERRMEM;
1437 lyd_free(data);
1438 return NC_MSG_ERROR;
1439 }
1440
1441 node = lyd_new_anyxml_str(data, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001442 } else {
1443 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1444 }
1445 if (!node) {
1446 lyd_free(data);
1447 return NC_MSG_ERROR;
1448 }
1449 break;
1450
1451 case NC_RPC_COPY:
1452 rpc_cp = (struct nc_rpc_copy *)rpc;
1453
1454 data = lyd_new(NULL, ietfnc, "copy-config");
1455 node = lyd_new(data, ietfnc, "target");
1456 if (rpc_cp->url_trg) {
1457 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1458 } else {
1459 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1460 }
1461 if (!node) {
1462 lyd_free(data);
1463 return NC_MSG_ERROR;
1464 }
1465
1466 node = lyd_new(data, ietfnc, "source");
1467 if (rpc_cp->url_config_src) {
1468 if (rpc_cp->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001469 /* we need a copy of the content */
1470 filter = strdup(rpc_cp->url_config_src);
1471 if (!filter) {
1472 ERRMEM;
1473 lyd_free(data);
1474 return NC_MSG_ERROR;
1475 }
1476
1477 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001478 } else {
1479 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1480 }
1481 } else {
1482 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1483 }
1484 if (!node) {
1485 lyd_free(data);
1486 return NC_MSG_ERROR;
1487 }
1488
1489 if (rpc_cp->wd_mode) {
1490 if (!ietfncwd) {
1491 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1492 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001493 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001494 return NC_MSG_ERROR;
1495 }
1496 }
1497 switch (rpc_cp->wd_mode) {
1498 case NC_WD_UNKNOWN:
1499 /* cannot get here */
1500 break;
1501 case NC_WD_ALL:
1502 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1503 break;
1504 case NC_WD_ALL_TAG:
1505 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1506 break;
1507 case NC_WD_TRIM:
1508 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1509 break;
1510 case NC_WD_EXPLICIT:
1511 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1512 break;
1513 }
1514 if (!node) {
1515 lyd_free(data);
1516 return NC_MSG_ERROR;
1517 }
1518 }
1519 break;
1520
1521 case NC_RPC_DELETE:
1522 rpc_del = (struct nc_rpc_delete *)rpc;
1523
1524 data = lyd_new(NULL, ietfnc, "delete-config");
1525 node = lyd_new(data, ietfnc, "target");
1526 if (rpc_del->url) {
1527 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1528 } else {
1529 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1530 }
1531 if (!node) {
1532 lyd_free(data);
1533 return NC_MSG_ERROR;
1534 }
1535 break;
1536
1537 case NC_RPC_LOCK:
1538 rpc_lock = (struct nc_rpc_lock *)rpc;
1539
1540 data = lyd_new(NULL, ietfnc, "lock");
1541 node = lyd_new(data, ietfnc, "target");
1542 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1543 if (!node) {
1544 lyd_free(data);
1545 return NC_MSG_ERROR;
1546 }
1547 break;
1548
1549 case NC_RPC_UNLOCK:
1550 rpc_lock = (struct nc_rpc_lock *)rpc;
1551
1552 data = lyd_new(NULL, ietfnc, "unlock");
1553 node = lyd_new(data, ietfnc, "target");
1554 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1555 if (!node) {
1556 lyd_free(data);
1557 return NC_MSG_ERROR;
1558 }
1559 break;
1560
1561 case NC_RPC_GET:
1562 rpc_g = (struct nc_rpc_get *)rpc;
1563
1564 data = lyd_new(NULL, ietfnc, "get");
1565 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001566 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001567 /* we need a copy of the filter */
1568 filter = strdup(rpc_g->filter);
1569 if (!filter) {
1570 ERRMEM;
1571 lyd_free(data);
1572 return NC_MSG_ERROR;
1573 }
1574
1575 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001576 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001577 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001578 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001579 lyd_insert_attr(node, NULL, "type", "xpath");
1580 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001581 }
1582 if (!node) {
1583 lyd_free(data);
1584 return NC_MSG_ERROR;
1585 }
1586 }
1587
1588 if (rpc_g->wd_mode) {
1589 if (!ietfncwd) {
1590 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1591 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001592 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001593 return NC_MSG_ERROR;
1594 }
1595 }
1596 switch (rpc_g->wd_mode) {
1597 case NC_WD_UNKNOWN:
1598 /* cannot get here */
1599 break;
1600 case NC_WD_ALL:
1601 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1602 break;
1603 case NC_WD_ALL_TAG:
1604 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1605 break;
1606 case NC_WD_TRIM:
1607 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1608 break;
1609 case NC_WD_EXPLICIT:
1610 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1611 break;
1612 }
1613 if (!node) {
1614 lyd_free(data);
1615 return NC_MSG_ERROR;
1616 }
1617 }
1618 break;
1619
1620 case NC_RPC_KILL:
1621 rpc_k = (struct nc_rpc_kill *)rpc;
1622
1623 data = lyd_new(NULL, ietfnc, "kill-session");
1624 sprintf(str, "%u", rpc_k->sid);
1625 lyd_new_leaf(data, ietfnc, "session-id", str);
1626 break;
1627
1628 case NC_RPC_COMMIT:
1629 rpc_com = (struct nc_rpc_commit *)rpc;
1630
1631 data = lyd_new(NULL, ietfnc, "commit");
1632 if (rpc_com->confirmed) {
1633 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1634 }
1635
1636 if (rpc_com->confirm_timeout) {
1637 sprintf(str, "%u", rpc_com->confirm_timeout);
1638 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1639 }
1640
1641 if (rpc_com->persist) {
1642 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1643 if (!node) {
1644 lyd_free(data);
1645 return NC_MSG_ERROR;
1646 }
1647 }
1648
1649 if (rpc_com->persist_id) {
1650 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1651 if (!node) {
1652 lyd_free(data);
1653 return NC_MSG_ERROR;
1654 }
1655 }
1656 break;
1657
1658 case NC_RPC_DISCARD:
1659 data = lyd_new(NULL, ietfnc, "discard-changes");
1660 break;
1661
1662 case NC_RPC_CANCEL:
1663 rpc_can = (struct nc_rpc_cancel *)rpc;
1664
1665 data = lyd_new(NULL, ietfnc, "cancel-commit");
1666 if (rpc_can->persist_id) {
1667 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1668 if (!node) {
1669 lyd_free(data);
1670 return NC_MSG_ERROR;
1671 }
1672 }
1673 break;
1674
1675 case NC_RPC_VALIDATE:
1676 rpc_val = (struct nc_rpc_validate *)rpc;
1677
1678 data = lyd_new(NULL, ietfnc, "validate");
1679 node = lyd_new(data, ietfnc, "source");
1680 if (rpc_val->url_config_src) {
1681 if (rpc_val->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001682 /* we need a copy of the config */
1683 filter = strdup(rpc_val->url_config_src);
1684 if (!filter) {
1685 ERRMEM;
1686 lyd_free(data);
1687 return NC_MSG_ERROR;
1688 }
1689
1690 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001691 } else {
1692 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1693 }
1694 } else {
1695 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1696 }
1697 if (!node) {
1698 lyd_free(data);
1699 return NC_MSG_ERROR;
1700 }
1701 break;
1702
1703 case NC_RPC_GETSCHEMA:
1704 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1705 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001706 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001707 return NC_MSG_ERROR;
1708 }
1709
1710 rpc_gs = (struct nc_rpc_getschema *)rpc;
1711
1712 data = lyd_new(NULL, ietfncmon, "get-schema");
1713 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1714 if (!node) {
1715 lyd_free(data);
1716 return NC_MSG_ERROR;
1717 }
1718 if (rpc_gs->version) {
1719 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1720 if (!node) {
1721 lyd_free(data);
1722 return NC_MSG_ERROR;
1723 }
1724 }
1725 if (rpc_gs->format) {
1726 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1727 if (!node) {
1728 lyd_free(data);
1729 return NC_MSG_ERROR;
1730 }
1731 }
1732 break;
1733
1734 case NC_RPC_SUBSCRIBE:
1735 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1736 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001737 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001738 return NC_MSG_ERROR;
1739 }
1740
1741 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1742
1743 data = lyd_new(NULL, notifs, "create-subscription");
1744 if (rpc_sub->stream) {
1745 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1746 if (!node) {
1747 lyd_free(data);
1748 return NC_MSG_ERROR;
1749 }
1750 }
1751
1752 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001753 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001754 /* we need a copy of the filter */
1755 filter = strdup(rpc_sub->filter);
1756 if (!filter) {
1757 ERRMEM;
1758 lyd_free(data);
1759 return NC_MSG_ERROR;
1760 }
1761
1762 node = lyd_new_anyxml_str(data, notifs, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001763 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001764 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001765 node = lyd_new_anyxml_str(data, notifs, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001766 lyd_insert_attr(node, NULL, "type", "xpath");
1767 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001768 }
1769 if (!node) {
1770 lyd_free(data);
1771 return NC_MSG_ERROR;
1772 }
1773 }
1774
1775 if (rpc_sub->start) {
1776 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1777 if (!node) {
1778 lyd_free(data);
1779 return NC_MSG_ERROR;
1780 }
1781 }
1782
1783 if (rpc_sub->stop) {
1784 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1785 if (!node) {
1786 lyd_free(data);
1787 return NC_MSG_ERROR;
1788 }
1789 }
1790 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001791 default:
1792 ERRINT;
1793 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001794 }
1795
Michal Vasko1d8212b2016-04-13 14:24:52 +02001796 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001797 lyd_free(data);
1798 return NC_MSG_ERROR;
1799 }
1800
Michal Vasko62be1ce2016-03-03 13:24:52 +01001801 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001802 if (ret == -1) {
1803 /* error */
1804 r = NC_MSG_ERROR;
1805 } else if (!ret) {
1806 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001807 r = NC_MSG_WOULDBLOCK;
1808 } else {
1809 /* send RPC, store its message ID */
1810 r = nc_send_msg(session, data);
1811 cur_msgid = session->msgid;
1812 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001813 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001814
1815 lyd_free(data);
1816
1817 if (r != NC_MSG_RPC) {
1818 return r;
1819 }
1820
1821 *msgid = cur_msgid;
1822 return NC_MSG_RPC;
1823}