blob: d675240565c8db2625641b45579699ea74cea056 [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 */
375 if (nc_handshake(session)) {
376 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 Vaskob7558c52016-02-26 15:04:19 +01001058API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001059nc_client_init(void)
1060{
1061 nc_init();
1062}
1063
1064API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001065nc_client_destroy(void)
1066{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001067 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001068#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1069 nc_client_ch_del_bind(NULL, 0, 0);
1070#endif
1071#ifdef NC_ENABLED_SSH
1072 nc_client_ssh_destroy_opts();
1073#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001074#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001075 nc_client_tls_destroy_opts();
1076#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001077 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001078}
1079
Michal Vasko086311b2016-01-08 09:53:11 +01001080API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001081nc_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 +01001082{
1083 struct lyxml_elem *xml;
1084 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1085
Michal Vasko45e53ae2016-04-07 11:46:03 +02001086 if (!session) {
1087 ERRARG("session");
1088 return NC_MSG_ERROR;
1089 } else if (!rpc) {
1090 ERRARG("rpc");
1091 return NC_MSG_ERROR;
1092 } else if (!reply) {
1093 ERRARG("reply");
1094 return NC_MSG_ERROR;
1095 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1096 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001097 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001098 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001099 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001100 return NC_MSG_ERROR;
1101 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001102 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001103 *reply = NULL;
1104
1105 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001106
Michal Vasko71ba2da2016-05-04 10:53:16 +02001107 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001108 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001109 lyxml_free(session->ctx, xml);
1110 if (!(*reply)) {
1111 return NC_MSG_ERROR;
1112 }
1113 }
1114
1115 return msgtype;
1116}
1117
1118API NC_MSG_TYPE
1119nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1120{
1121 struct lyxml_elem *xml, *ev_time;
1122 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1123
Michal Vasko45e53ae2016-04-07 11:46:03 +02001124 if (!session) {
1125 ERRARG("session");
1126 return NC_MSG_ERROR;
1127 } else if (!notif) {
1128 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001129 return NC_MSG_ERROR;
1130 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001131 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001132 return NC_MSG_ERROR;
1133 }
1134
1135 msgtype = get_msg(session, timeout, 0, &xml);
1136
1137 if (msgtype == NC_MSG_NOTIF) {
1138 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001139 if (!*notif) {
1140 ERRMEM;
1141 lyxml_free(session->ctx, xml);
1142 return NC_MSG_ERROR;
1143 }
Michal Vasko086311b2016-01-08 09:53:11 +01001144
1145 /* eventTime */
1146 LY_TREE_FOR(xml->child, ev_time) {
1147 if (!strcmp(ev_time->name, "eventTime")) {
1148 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1149 /* lyd_parse does not know this element */
1150 lyxml_free(session->ctx, ev_time);
1151 break;
1152 }
1153 }
1154 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001155 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001156 goto fail;
1157 }
1158
1159 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001160 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001161 lyxml_free(session->ctx, xml);
1162 xml = NULL;
1163 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001164 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001165 goto fail;
1166 }
1167 }
1168
1169 return msgtype;
1170
1171fail:
1172 lydict_remove(session->ctx, (*notif)->datetime);
1173 lyd_free((*notif)->tree);
1174 free(*notif);
1175 *notif = NULL;
1176 lyxml_free(session->ctx, xml);
1177
1178 return NC_MSG_ERROR;
1179}
1180
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001181static void *
1182nc_recv_notif_thread(void *arg)
1183{
1184 struct nc_ntf_thread_arg *ntarg;
1185 struct nc_session *session;
1186 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1187 struct nc_notif *notif;
1188 NC_MSG_TYPE msgtype;
1189
1190 ntarg = (struct nc_ntf_thread_arg *)arg;
1191 session = ntarg->session;
1192 notif_clb = ntarg->notif_clb;
1193 free(ntarg);
1194
1195 while (session->ntf_tid) {
1196 msgtype = nc_recv_notif(session, 0, &notif);
1197 if (msgtype == NC_MSG_NOTIF) {
1198 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001199 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1200 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1201 nc_notif_free(notif);
1202 break;
1203 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001204 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001205 } else if (msgtype == NC_MSG_ERROR) {
1206 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001207 }
1208
1209 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1210 }
1211
Michal Vasko0651c902016-05-19 15:55:42 +02001212 VRB("Session %u: notification thread exit.", session->id);
1213 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001214 return NULL;
1215}
1216
1217API int
1218nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1219{
1220 struct nc_ntf_thread_arg *ntarg;
1221 int ret;
1222
Michal Vasko45e53ae2016-04-07 11:46:03 +02001223 if (!session) {
1224 ERRARG("session");
1225 return -1;
1226 } else if (!notif_clb) {
1227 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001228 return -1;
1229 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1230 ERR("Session %u: invalid session to receive Notifications.", session->id);
1231 return -1;
1232 } else if (session->ntf_tid) {
1233 ERR("Session %u: separate notification thread is already running.", session->id);
1234 return -1;
1235 }
1236
1237 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001238 if (!ntarg) {
1239 ERRMEM;
1240 return -1;
1241 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001242 ntarg->session = session;
1243 ntarg->notif_clb = notif_clb;
1244
1245 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1246 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001247 if (!session->ntf_tid) {
1248 ERRMEM;
1249 free(ntarg);
1250 return -1;
1251 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001252
1253 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1254 if (ret) {
1255 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1256 free(ntarg);
1257 free((pthread_t *)session->ntf_tid);
1258 session->ntf_tid = NULL;
1259 return -1;
1260 }
1261
1262 return 0;
1263}
1264
Michal Vasko086311b2016-01-08 09:53:11 +01001265API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001266nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001267{
1268 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001269 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001270 struct nc_rpc_generic *rpc_gen;
1271 struct nc_rpc_getconfig *rpc_gc;
1272 struct nc_rpc_edit *rpc_e;
1273 struct nc_rpc_copy *rpc_cp;
1274 struct nc_rpc_delete *rpc_del;
1275 struct nc_rpc_lock *rpc_lock;
1276 struct nc_rpc_get *rpc_g;
1277 struct nc_rpc_kill *rpc_k;
1278 struct nc_rpc_commit *rpc_com;
1279 struct nc_rpc_cancel *rpc_can;
1280 struct nc_rpc_validate *rpc_val;
1281 struct nc_rpc_getschema *rpc_gs;
1282 struct nc_rpc_subscribe *rpc_sub;
1283 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001284 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vaskod91f6e62016-04-05 11:34:22 +02001285 char str[11], *filter;
Michal Vasko086311b2016-01-08 09:53:11 +01001286 uint64_t cur_msgid;
1287
Michal Vasko45e53ae2016-04-07 11:46:03 +02001288 if (!session) {
1289 ERRARG("session");
1290 return NC_MSG_ERROR;
1291 } else if (!rpc) {
1292 ERRARG("rpc");
1293 return NC_MSG_ERROR;
1294 } else if (!msgid) {
1295 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001296 return NC_MSG_ERROR;
1297 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001298 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001299 return NC_MSG_ERROR;
1300 }
1301
1302 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1303 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1304 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001305 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001306 return NC_MSG_ERROR;
1307 }
1308 }
1309
1310 switch (rpc->type) {
1311 case NC_RPC_GENERIC:
1312 rpc_gen = (struct nc_rpc_generic *)rpc;
1313
1314 if (rpc_gen->has_data) {
1315 data = rpc_gen->content.data;
1316 } else {
Michal Vasko03df9e32016-05-17 10:22:53 +02001317 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 +01001318 }
1319 break;
1320
1321 case NC_RPC_GETCONFIG:
1322 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1323
1324 data = lyd_new(NULL, ietfnc, "get-config");
1325 node = lyd_new(data, ietfnc, "source");
1326 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1327 if (!node) {
1328 lyd_free(data);
1329 return NC_MSG_ERROR;
1330 }
1331 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001332 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001333 /* we need a copy of the filter */
1334 filter = strdup(rpc_gc->filter);
1335 if (!filter) {
1336 ERRMEM;
1337 lyd_free(data);
1338 return NC_MSG_ERROR;
1339 }
1340
1341 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001342 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001343 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001344 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001345 lyd_insert_attr(node, NULL, "type", "xpath");
1346 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001347 }
1348 if (!node) {
1349 lyd_free(data);
1350 return NC_MSG_ERROR;
1351 }
1352 }
1353
1354 if (rpc_gc->wd_mode) {
1355 if (!ietfncwd) {
1356 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1357 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001358 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001359 return NC_MSG_ERROR;
1360 }
1361 }
1362 switch (rpc_gc->wd_mode) {
1363 case NC_WD_UNKNOWN:
1364 /* cannot get here */
1365 break;
1366 case NC_WD_ALL:
1367 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1368 break;
1369 case NC_WD_ALL_TAG:
1370 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1371 break;
1372 case NC_WD_TRIM:
1373 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1374 break;
1375 case NC_WD_EXPLICIT:
1376 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1377 break;
1378 }
1379 if (!node) {
1380 lyd_free(data);
1381 return NC_MSG_ERROR;
1382 }
1383 }
1384 break;
1385
1386 case NC_RPC_EDIT:
1387 rpc_e = (struct nc_rpc_edit *)rpc;
1388
1389 data = lyd_new(NULL, ietfnc, "edit-config");
1390 node = lyd_new(data, ietfnc, "target");
1391 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1392 if (!node) {
1393 lyd_free(data);
1394 return NC_MSG_ERROR;
1395 }
1396
1397 if (rpc_e->default_op) {
1398 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1399 if (!node) {
1400 lyd_free(data);
1401 return NC_MSG_ERROR;
1402 }
1403 }
1404
1405 if (rpc_e->test_opt) {
1406 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1407 if (!node) {
1408 lyd_free(data);
1409 return NC_MSG_ERROR;
1410 }
1411 }
1412
1413 if (rpc_e->error_opt) {
1414 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1415 if (!node) {
1416 lyd_free(data);
1417 return NC_MSG_ERROR;
1418 }
1419 }
1420
1421 if (rpc_e->edit_cont[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001422 /* we need a copy of the content */
1423 filter = strdup(rpc_e->edit_cont);
1424 if (!filter) {
1425 ERRMEM;
1426 lyd_free(data);
1427 return NC_MSG_ERROR;
1428 }
1429
1430 node = lyd_new_anyxml_str(data, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001431 } else {
1432 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1433 }
1434 if (!node) {
1435 lyd_free(data);
1436 return NC_MSG_ERROR;
1437 }
1438 break;
1439
1440 case NC_RPC_COPY:
1441 rpc_cp = (struct nc_rpc_copy *)rpc;
1442
1443 data = lyd_new(NULL, ietfnc, "copy-config");
1444 node = lyd_new(data, ietfnc, "target");
1445 if (rpc_cp->url_trg) {
1446 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1447 } else {
1448 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1449 }
1450 if (!node) {
1451 lyd_free(data);
1452 return NC_MSG_ERROR;
1453 }
1454
1455 node = lyd_new(data, ietfnc, "source");
1456 if (rpc_cp->url_config_src) {
1457 if (rpc_cp->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001458 /* we need a copy of the content */
1459 filter = strdup(rpc_cp->url_config_src);
1460 if (!filter) {
1461 ERRMEM;
1462 lyd_free(data);
1463 return NC_MSG_ERROR;
1464 }
1465
1466 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001467 } else {
1468 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1469 }
1470 } else {
1471 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1472 }
1473 if (!node) {
1474 lyd_free(data);
1475 return NC_MSG_ERROR;
1476 }
1477
1478 if (rpc_cp->wd_mode) {
1479 if (!ietfncwd) {
1480 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1481 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001482 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001483 return NC_MSG_ERROR;
1484 }
1485 }
1486 switch (rpc_cp->wd_mode) {
1487 case NC_WD_UNKNOWN:
1488 /* cannot get here */
1489 break;
1490 case NC_WD_ALL:
1491 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1492 break;
1493 case NC_WD_ALL_TAG:
1494 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1495 break;
1496 case NC_WD_TRIM:
1497 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1498 break;
1499 case NC_WD_EXPLICIT:
1500 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1501 break;
1502 }
1503 if (!node) {
1504 lyd_free(data);
1505 return NC_MSG_ERROR;
1506 }
1507 }
1508 break;
1509
1510 case NC_RPC_DELETE:
1511 rpc_del = (struct nc_rpc_delete *)rpc;
1512
1513 data = lyd_new(NULL, ietfnc, "delete-config");
1514 node = lyd_new(data, ietfnc, "target");
1515 if (rpc_del->url) {
1516 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1517 } else {
1518 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1519 }
1520 if (!node) {
1521 lyd_free(data);
1522 return NC_MSG_ERROR;
1523 }
1524 break;
1525
1526 case NC_RPC_LOCK:
1527 rpc_lock = (struct nc_rpc_lock *)rpc;
1528
1529 data = lyd_new(NULL, ietfnc, "lock");
1530 node = lyd_new(data, ietfnc, "target");
1531 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1532 if (!node) {
1533 lyd_free(data);
1534 return NC_MSG_ERROR;
1535 }
1536 break;
1537
1538 case NC_RPC_UNLOCK:
1539 rpc_lock = (struct nc_rpc_lock *)rpc;
1540
1541 data = lyd_new(NULL, ietfnc, "unlock");
1542 node = lyd_new(data, ietfnc, "target");
1543 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1544 if (!node) {
1545 lyd_free(data);
1546 return NC_MSG_ERROR;
1547 }
1548 break;
1549
1550 case NC_RPC_GET:
1551 rpc_g = (struct nc_rpc_get *)rpc;
1552
1553 data = lyd_new(NULL, ietfnc, "get");
1554 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001555 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001556 /* we need a copy of the filter */
1557 filter = strdup(rpc_g->filter);
1558 if (!filter) {
1559 ERRMEM;
1560 lyd_free(data);
1561 return NC_MSG_ERROR;
1562 }
1563
1564 node = lyd_new_anyxml_str(data, ietfnc, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001565 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001566 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001567 node = lyd_new_anyxml_str(data, ietfnc, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001568 lyd_insert_attr(node, NULL, "type", "xpath");
1569 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001570 }
1571 if (!node) {
1572 lyd_free(data);
1573 return NC_MSG_ERROR;
1574 }
1575 }
1576
1577 if (rpc_g->wd_mode) {
1578 if (!ietfncwd) {
1579 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1580 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001581 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001582 return NC_MSG_ERROR;
1583 }
1584 }
1585 switch (rpc_g->wd_mode) {
1586 case NC_WD_UNKNOWN:
1587 /* cannot get here */
1588 break;
1589 case NC_WD_ALL:
1590 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1591 break;
1592 case NC_WD_ALL_TAG:
1593 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1594 break;
1595 case NC_WD_TRIM:
1596 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1597 break;
1598 case NC_WD_EXPLICIT:
1599 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1600 break;
1601 }
1602 if (!node) {
1603 lyd_free(data);
1604 return NC_MSG_ERROR;
1605 }
1606 }
1607 break;
1608
1609 case NC_RPC_KILL:
1610 rpc_k = (struct nc_rpc_kill *)rpc;
1611
1612 data = lyd_new(NULL, ietfnc, "kill-session");
1613 sprintf(str, "%u", rpc_k->sid);
1614 lyd_new_leaf(data, ietfnc, "session-id", str);
1615 break;
1616
1617 case NC_RPC_COMMIT:
1618 rpc_com = (struct nc_rpc_commit *)rpc;
1619
1620 data = lyd_new(NULL, ietfnc, "commit");
1621 if (rpc_com->confirmed) {
1622 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1623 }
1624
1625 if (rpc_com->confirm_timeout) {
1626 sprintf(str, "%u", rpc_com->confirm_timeout);
1627 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1628 }
1629
1630 if (rpc_com->persist) {
1631 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1632 if (!node) {
1633 lyd_free(data);
1634 return NC_MSG_ERROR;
1635 }
1636 }
1637
1638 if (rpc_com->persist_id) {
1639 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1640 if (!node) {
1641 lyd_free(data);
1642 return NC_MSG_ERROR;
1643 }
1644 }
1645 break;
1646
1647 case NC_RPC_DISCARD:
1648 data = lyd_new(NULL, ietfnc, "discard-changes");
1649 break;
1650
1651 case NC_RPC_CANCEL:
1652 rpc_can = (struct nc_rpc_cancel *)rpc;
1653
1654 data = lyd_new(NULL, ietfnc, "cancel-commit");
1655 if (rpc_can->persist_id) {
1656 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1657 if (!node) {
1658 lyd_free(data);
1659 return NC_MSG_ERROR;
1660 }
1661 }
1662 break;
1663
1664 case NC_RPC_VALIDATE:
1665 rpc_val = (struct nc_rpc_validate *)rpc;
1666
1667 data = lyd_new(NULL, ietfnc, "validate");
1668 node = lyd_new(data, ietfnc, "source");
1669 if (rpc_val->url_config_src) {
1670 if (rpc_val->url_config_src[0] == '<') {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001671 /* we need a copy of the config */
1672 filter = strdup(rpc_val->url_config_src);
1673 if (!filter) {
1674 ERRMEM;
1675 lyd_free(data);
1676 return NC_MSG_ERROR;
1677 }
1678
1679 node = lyd_new_anyxml_str(node, ietfnc, "config", filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001680 } else {
1681 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1682 }
1683 } else {
1684 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1685 }
1686 if (!node) {
1687 lyd_free(data);
1688 return NC_MSG_ERROR;
1689 }
1690 break;
1691
1692 case NC_RPC_GETSCHEMA:
1693 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1694 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001695 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001696 return NC_MSG_ERROR;
1697 }
1698
1699 rpc_gs = (struct nc_rpc_getschema *)rpc;
1700
1701 data = lyd_new(NULL, ietfncmon, "get-schema");
1702 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1703 if (!node) {
1704 lyd_free(data);
1705 return NC_MSG_ERROR;
1706 }
1707 if (rpc_gs->version) {
1708 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1709 if (!node) {
1710 lyd_free(data);
1711 return NC_MSG_ERROR;
1712 }
1713 }
1714 if (rpc_gs->format) {
1715 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1716 if (!node) {
1717 lyd_free(data);
1718 return NC_MSG_ERROR;
1719 }
1720 }
1721 break;
1722
1723 case NC_RPC_SUBSCRIBE:
1724 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1725 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001726 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001727 return NC_MSG_ERROR;
1728 }
1729
1730 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1731
1732 data = lyd_new(NULL, notifs, "create-subscription");
1733 if (rpc_sub->stream) {
1734 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1735 if (!node) {
1736 lyd_free(data);
1737 return NC_MSG_ERROR;
1738 }
1739 }
1740
1741 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001742 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001743 /* we need a copy of the filter */
1744 filter = strdup(rpc_sub->filter);
1745 if (!filter) {
1746 ERRMEM;
1747 lyd_free(data);
1748 return NC_MSG_ERROR;
1749 }
1750
1751 node = lyd_new_anyxml_str(data, notifs, "filter", filter);
Michal Vasko303245c2016-03-24 15:20:03 +01001752 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001753 } else {
Michal Vaskod91f6e62016-04-05 11:34:22 +02001754 node = lyd_new_anyxml_str(data, notifs, "filter", NULL);
Michal Vasko303245c2016-03-24 15:20:03 +01001755 lyd_insert_attr(node, NULL, "type", "xpath");
1756 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001757 }
1758 if (!node) {
1759 lyd_free(data);
1760 return NC_MSG_ERROR;
1761 }
1762 }
1763
1764 if (rpc_sub->start) {
1765 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1766 if (!node) {
1767 lyd_free(data);
1768 return NC_MSG_ERROR;
1769 }
1770 }
1771
1772 if (rpc_sub->stop) {
1773 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1774 if (!node) {
1775 lyd_free(data);
1776 return NC_MSG_ERROR;
1777 }
1778 }
1779 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001780 default:
1781 ERRINT;
1782 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001783 }
1784
Michal Vasko1d8212b2016-04-13 14:24:52 +02001785 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001786 lyd_free(data);
1787 return NC_MSG_ERROR;
1788 }
1789
Michal Vasko62be1ce2016-03-03 13:24:52 +01001790 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001791 if (ret == -1) {
1792 /* error */
1793 r = NC_MSG_ERROR;
1794 } else if (!ret) {
1795 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001796 r = NC_MSG_WOULDBLOCK;
1797 } else {
1798 /* send RPC, store its message ID */
1799 r = nc_send_msg(session, data);
1800 cur_msgid = session->msgid;
1801 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001802 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001803
1804 lyd_free(data);
1805
1806 if (r != NC_MSG_RPC) {
1807 return r;
1808 }
1809
1810 *msgid = cur_msgid;
1811 return NC_MSG_RPC;
1812}