blob: e27c1d33e6dd3451e8a001723e863a4599d18bd0 [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 Vasko3031aae2016-01-27 16:07:18 +010041nc_client_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 Vasko3031aae2016-01-27 16:07:18 +010060/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010061static int
62ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
63{
64 const struct lys_module *module;
65 char *ptr, *ptr2;
66 char *model_name, *revision = NULL, *features = NULL;
67
68 /* parse module */
69 ptr = strstr(cpblt, "module=");
70 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010071 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
72 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010073 }
74 ptr += 7;
75 ptr2 = strchr(ptr, '&');
76 if (!ptr2) {
77 ptr2 = ptr + strlen(ptr);
78 }
79 model_name = strndup(ptr, ptr2 - ptr);
80
81 /* parse revision */
82 ptr = strstr(cpblt, "revision=");
83 if (ptr) {
84 ptr += 9;
85 ptr2 = strchr(ptr, '&');
86 if (!ptr2) {
87 ptr2 = ptr + strlen(ptr);
88 }
89 revision = strndup(ptr, ptr2 - ptr);
90 }
91
92 /* load module if needed */
93 module = ly_ctx_get_module(session->ctx, model_name, revision);
94 if (!module) {
95 module = ly_ctx_load_module(session->ctx, model_name, revision);
96 }
97
Michal Vasko086311b2016-01-08 09:53:11 +010098 free(revision);
99 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100100 WRN("Failed to load model \"%s\".", model_name);
101 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100102 return 1;
103 }
Michal Vaskoef578332016-01-25 13:20:09 +0100104 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100105
106 /* parse features */
107 ptr = strstr(cpblt, "features=");
108 if (ptr) {
109 ptr += 9;
110 ptr2 = strchr(ptr, '&');
111 if (!ptr2) {
112 ptr2 = ptr + strlen(ptr);
113 }
114 features = strndup(ptr, ptr2 - ptr);
115 }
116
117 /* enable features */
118 if (features) {
119 /* basically manual strtok_r (to avoid macro) */
120 ptr2 = features;
121 for (ptr = features; *ptr; ++ptr) {
122 if (*ptr == ',') {
123 *ptr = '\0';
124 /* remember last feature */
125 ptr2 = ptr + 1;
126 }
127 }
128
129 ptr = features;
130 lys_features_enable(module, ptr);
131 while (ptr != ptr2) {
132 ptr += strlen(ptr) + 1;
133 lys_features_enable(module, ptr);
134 }
135
136 free(features);
137 }
138
139 return 0;
140}
141
Michal Vasko1aaa6602016-02-09 11:04:33 +0100142/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100143static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100144ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100145{
146 int i;
147 const struct lys_module *ietfnc;
148
149 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
150 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100151 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
152 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100153 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100154 }
155 }
156 if (!ietfnc) {
157 ERR("Loading base NETCONF schema failed.");
158 return 1;
159 }
160
161 /* set supported capabilities from ietf-netconf */
162 for (i = 0; cpblts[i]; ++i) {
163 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
164 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
165 lys_features_enable(ietfnc, "writable-running");
166 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
167 lys_features_enable(ietfnc, "candidate");
168 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
169 lys_features_enable(ietfnc, "confirmed-commit");
170 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
171 lys_features_enable(ietfnc, "rollback-on-error");
172 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
173 lys_features_enable(ietfnc, "validate");
174 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
175 lys_features_enable(ietfnc, "startup");
176 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
177 lys_features_enable(ietfnc, "url");
178 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
179 lys_features_enable(ietfnc, "xpath");
180 }
181 }
182 }
183
184 return 0;
185}
186
187static char *
188libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
Michal Vaskoca4ad152016-03-03 15:50:45 +0100189 void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100190{
191 struct nc_session *session = (struct nc_session *)user_data;
192 struct nc_rpc *rpc;
193 struct nc_reply *reply;
194 struct nc_reply_data *data_rpl;
195 NC_MSG_TYPE msg;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100196 char *model_data = NULL, *ptr, *ptr2, *anyxml = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100197 uint64_t msgid;
198
199 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100200 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100201 *format = LYS_IN_YIN;
202
203 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
204 usleep(1000);
205 }
206 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100207 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100208 nc_rpc_free(rpc);
209 return NULL;
210 }
211
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100212 msg = nc_recv_reply(session, rpc, msgid, 250, 0, &reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100213 nc_rpc_free(rpc);
214 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100215 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100216 return NULL;
217 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100218 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100219 return NULL;
220 }
221
Michal Vasko05ba9df2016-01-13 14:40:27 +0100222 if (reply->type != NC_RPL_DATA) {
223 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100225 nc_reply_free(reply);
226 return NULL;
227 }
228
Michal Vasko086311b2016-01-08 09:53:11 +0100229 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskoa4c23d82016-02-03 15:48:09 +0100230 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data_rpl->data)->value, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100231 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100232 *free_model_data = free;
Michal Vasko086311b2016-01-08 09:53:11 +0100233
234 /* it's with the data root node, remove it */
235 if (anyxml) {
236 ptr = strchr(anyxml, '>');
237 ++ptr;
238
239 ptr2 = strrchr(anyxml, '<');
240
241 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
242 free(anyxml);
243 }
244
245 return model_data;
246}
247
Michal Vaskoef578332016-01-25 13:20:09 +0100248/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100249int
250nc_ctx_check_and_fill(struct nc_session *session)
251{
Michal Vaskoef578332016-01-25 13:20:09 +0100252 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100253 ly_module_clb old_clb = NULL;
254 void *old_data = NULL;
255
256 assert(session->cpblts && session->ctx);
257
258 /* check if get-schema is supported */
259 for (i = 0; session->cpblts[i]; ++i) {
260 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
261 get_schema_support = 1;
262 break;
263 }
264 }
265
266 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
267 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
268 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
269 /* set module retrieval using <get-schema> */
270 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100271 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100272 } else {
273 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
274 }
275 }
276
277 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100278 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100279 if (old_clb) {
280 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
281 }
Michal Vaskoef578332016-01-25 13:20:09 +0100282 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100283 }
284
285 /* load all other models */
286 for (i = 0; session->cpblts[i]; ++i) {
287 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
288 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
289 continue;
290 }
291
Michal Vaskoef578332016-01-25 13:20:09 +0100292 r = ctx_check_and_load_model(session, session->cpblts[i]);
293 if (r == -1) {
294 ret = -1;
295 break;
296 }
297
298 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
299 * if it was using get-schema */
300 if (r == 1) {
301 if (get_schema_support) {
302 VRB("Trying to load the schema from a different source.");
303 /* works even if old_clb is NULL */
304 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
305 r = ctx_check_and_load_model(session, session->cpblts[i]);
306 }
307
308 /* fail again (or no other way to try), too bad */
309 if (r) {
310 ret = 1;
311 }
312
313 /* set get-schema callback back */
314 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
315 }
Michal Vasko086311b2016-01-08 09:53:11 +0100316 }
317
318 if (old_clb) {
319 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
320 }
Michal Vaskoef578332016-01-25 13:20:09 +0100321 if (ret == 1) {
322 WRN("Some models failed to be loaded, any data from these models will be ignored.");
323 }
324 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100325}
326
327API struct nc_session *
328nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
329{
Michal Vaskod083db62016-01-19 10:31:29 +0100330 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100331
Michal Vaskod083db62016-01-19 10:31:29 +0100332 if ((fdin < 0) || (fdout < 0)) {
333 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100334 return NULL;
335 }
336
337 /* prepare session structure */
338 session = calloc(1, sizeof *session);
339 if (!session) {
340 ERRMEM;
341 return NULL;
342 }
343 session->status = NC_STATUS_STARTING;
344 session->side = NC_CLIENT;
345
346 /* transport specific data */
347 session->ti_type = NC_TI_FD;
348 session->ti.fd.in = fdin;
349 session->ti.fd.out = fdout;
350
351 /* assign context (dicionary needed for handshake) */
352 if (!ctx) {
353 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100354 /* definitely should not happen, but be ready */
355 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
356 /* that's just it */
357 goto fail;
358 }
Michal Vasko086311b2016-01-08 09:53:11 +0100359 } else {
360 session->flags |= NC_SESSION_SHAREDCTX;
361 }
362 session->ctx = ctx;
363
364 /* NETCONF handshake */
365 if (nc_handshake(session)) {
366 goto fail;
367 }
368 session->status = NC_STATUS_RUNNING;
369
Michal Vaskoef578332016-01-25 13:20:09 +0100370 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100371 goto fail;
372 }
373
374 return session;
375
376fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100377 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100378 return NULL;
379}
380
381int
Michal Vaskof05562c2016-01-20 12:06:43 +0100382nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100383{
Michal Vasko0190bc32016-03-02 15:47:49 +0100384 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100385 struct addrinfo hints, *res_list, *res;
386 char port_s[6]; /* length of string representation of short int */
387
388 snprintf(port_s, 6, "%u", port);
389
390 /* Connect to a server */
391 memset(&hints, 0, sizeof hints);
392 hints.ai_family = AF_UNSPEC;
393 hints.ai_socktype = SOCK_STREAM;
394 hints.ai_protocol = IPPROTO_TCP;
395 i = getaddrinfo(host, port_s, &hints, &res_list);
396 if (i != 0) {
397 ERR("Unable to translate the host address (%s).", gai_strerror(i));
398 return -1;
399 }
400
401 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
402 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
403 if (sock == -1) {
404 /* socket was not created, try another resource */
405 i = errno;
406 goto errloop;
407 }
408
409 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
410 /* network connection failed, try another resource */
411 i = errno;
412 close(sock);
413 sock = -1;
414 goto errloop;
415 }
416
Michal Vasko0190bc32016-03-02 15:47:49 +0100417 /* make the socket non-blocking */
418 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
419 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100420 close(sock);
Michal Vasko0190bc32016-03-02 15:47:49 +0100421 return -1;
422 }
423
Michal Vasko086311b2016-01-08 09:53:11 +0100424 /* we're done, network connection established */
425 break;
426errloop:
427 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
428 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
429 continue;
430 }
431
432 if (sock == -1) {
433 ERR("Unable to connect to %s:%s.", host, port_s);
434 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100435 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 +0100436 }
437 freeaddrinfo(res_list);
438
439 return sock;
440}
441
Michal Vasko086311b2016-01-08 09:53:11 +0100442static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100443get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100444{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100445 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100446 char *ptr;
447 const char *str_msgid;
448 uint64_t cur_msgid;
449 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100450 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100451 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
452
Michal Vasko62be1ce2016-03-03 13:24:52 +0100453 r = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100454 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100455 /* error */
456 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100457 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100458 /* timeout */
459 return NC_MSG_WOULDBLOCK;
460 }
461
462 /* try to get notification from the session's queue */
463 if (!msgid && session->notifs) {
464 cont = session->notifs;
465 session->notifs = cont->next;
466
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100467 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100468
469 *msg = cont->msg;
470 free(cont);
471
472 return NC_MSG_NOTIF;
473 }
474
475 /* try to get rpc-reply from the session's queue */
476 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100477 while (session->replies) {
478 cont = session->replies;
479 session->replies = cont->next;
480
Michal Vasko086311b2016-01-08 09:53:11 +0100481 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
482 cur_msgid = strtoul(str_msgid, &ptr, 10);
483
484 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100485 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100486 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100487
488 *msg = cont->msg;
489 free(cont);
490
491 return NC_MSG_REPLY;
492 }
493
Michal Vasko2518b6b2016-01-28 13:24:53 +0100494 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
495 lyxml_free(session->ctx, cont->msg);
Michal Vaskob2d91072016-02-01 13:25:20 +0100496 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100497 }
498 }
499
500 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100501 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100502
503 /* we read rpc-reply, want a notif */
504 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100505 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100506 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
507 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100508 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100509 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100510 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100511 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100512 }
Michal Vasko086311b2016-01-08 09:53:11 +0100513
514 cont_ptr = &session->replies;
515 while (*cont_ptr) {
516 cont_ptr = &((*cont_ptr)->next);
517 }
518 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100519 if (!*cont_ptr) {
520 ERRMEM;
521 lyxml_free(session->ctx, xml);
522 return NC_MSG_ERROR;
523 }
Michal Vasko086311b2016-01-08 09:53:11 +0100524 (*cont_ptr)->msg = xml;
525 (*cont_ptr)->next = NULL;
526 }
527
528 /* we read notif, want a rpc-reply */
529 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100530 /* TODO check whether the session is even subscribed */
531 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100532 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100533 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100534 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100535 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100536 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100537
538 cont_ptr = &session->notifs;
539 while (*cont_ptr) {
540 cont_ptr = &((*cont_ptr)->next);
541 }
542 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100543 if (!cont_ptr) {
544 ERRMEM;
545 lyxml_free(session->ctx, xml);
546 return NC_MSG_ERROR;
547 }
Michal Vasko086311b2016-01-08 09:53:11 +0100548 (*cont_ptr)->msg = xml;
549 (*cont_ptr)->next = NULL;
550 }
551
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100552 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100553
554 switch (msgtype) {
555 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100556 if (!msgid) {
557 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100558 }
Michal Vasko086311b2016-01-08 09:53:11 +0100559 break;
560
561 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100562 if (msgid) {
563 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100564 }
Michal Vasko086311b2016-01-08 09:53:11 +0100565 break;
566
567 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100568 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100569 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100570 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100571
572 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100573 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100574 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100575 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100576
577 default:
578 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
579 * NC_MSG_NONE is not returned by nc_read_msg()
580 */
581 break;
582 }
583
584 return msgtype;
585}
586
587/* cannot strictly fail, but does not need to fill any error parameter at all */
588static void
589parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
590{
591 struct lyxml_elem *iter, *next, *info;
592
593 LY_TREE_FOR(xml->child, iter) {
594 if (!iter->ns) {
595 if (iter->content) {
596 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
597 } else {
598 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
599 }
600 continue;
601 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
602 if (iter->content) {
603 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
604 iter->name, iter->content, iter->ns->value);
605 } else {
606 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
607 }
608 continue;
609 }
610
611 if (!strcmp(iter->name, "error-type")) {
612 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
613 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
614 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
615 } else if (err->type) {
616 WRN("<rpc-error> <error-type> duplicated.");
617 } else {
618 err->type = lydict_insert(ctx, iter->content, 0);
619 }
620 } else if (!strcmp(iter->name, "error-tag")) {
621 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
622 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
623 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
624 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
625 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
626 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
627 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
628 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
629 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
630 && strcmp(iter->content, "malformed-message"))) {
631 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
632 } else if (err->tag) {
633 WRN("<rpc-error> <error-tag> duplicated.");
634 } else {
635 err->tag = lydict_insert(ctx, iter->content, 0);
636 }
637 } else if (!strcmp(iter->name, "error-severity")) {
638 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
639 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
640 } else if (err->severity) {
641 WRN("<rpc-error> <error-severity> duplicated.");
642 } else {
643 err->severity = lydict_insert(ctx, iter->content, 0);
644 }
645 } else if (!strcmp(iter->name, "error-app-tag")) {
646 if (err->apptag) {
647 WRN("<rpc-error> <error-app-tag> duplicated.");
648 } else {
649 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
650 }
651 } else if (!strcmp(iter->name, "error-path")) {
652 if (err->path) {
653 WRN("<rpc-error> <error-path> duplicated.");
654 } else {
655 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
656 }
657 } else if (!strcmp(iter->name, "error-message")) {
658 if (err->message) {
659 WRN("<rpc-error> <error-message> duplicated.");
660 } else {
661 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
662 if (!err->message_lang) {
663 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
664 }
665 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
666 }
667 } else if (!strcmp(iter->name, "error-info")) {
668 LY_TREE_FOR_SAFE(iter->child, next, info) {
669 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
670 if (!strcmp(info->name, "session-id")) {
671 if (err->sid) {
672 WRN("<rpc-error> <error-info> <session-id> duplicated.");
673 } else {
674 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
675 }
676 } else if (!strcmp(info->name, "bad-attr")) {
677 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100678 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
679 if (!err->attr) {
680 ERRMEM;
681 return;
682 }
Michal Vasko086311b2016-01-08 09:53:11 +0100683 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
684 } else if (!strcmp(info->name, "bad-element")) {
685 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100686 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
687 if (!err->elem) {
688 ERRMEM;
689 return;
690 }
Michal Vasko086311b2016-01-08 09:53:11 +0100691 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
692 } else if (!strcmp(info->name, "bad-namespace")) {
693 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100694 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
695 if (!err->ns) {
696 ERRMEM;
697 return;
698 }
Michal Vasko086311b2016-01-08 09:53:11 +0100699 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
700 } else {
701 if (info->content) {
702 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
703 info->name, info->content);
704 } else {
705 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
706 }
707 }
708 } else {
709 lyxml_unlink(ctx, info);
710 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100711 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
712 if (!err->other) {
713 ERRMEM;
714 return;
715 }
Michal Vasko086311b2016-01-08 09:53:11 +0100716 err->other[err->other_count - 1] = info;
717 }
718 }
719 } else {
720 if (iter->content) {
721 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
722 } else {
723 WRN("<rpc-error> unknown child \"%s\".", iter->name);
724 }
725 }
726 }
727}
728
729static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100730parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100731{
732 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100733 const struct lys_node *schema = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100734 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100735 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100736 struct nc_reply_data *data_rpl;
737 struct nc_reply *reply = NULL;
738 struct nc_rpc_generic *rpc_gen;
739 int i;
740
741 if (!xml->child) {
742 ERR("An empty <rpc-reply>.");
743 return NULL;
744 }
745
746 /* rpc-error */
747 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
748 /* count and check elements */
749 i = 0;
750 LY_TREE_FOR(xml->child, iter) {
751 if (strcmp(iter->name, "rpc-error")) {
752 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
753 return NULL;
754 } else if (!iter->ns) {
755 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
756 return NULL;
757 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
758 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
759 return NULL;
760 }
761 ++i;
762 }
763
764 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100765 if (!error_rpl) {
766 ERRMEM;
767 return NULL;
768 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100769 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100770 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100771 if (!error_rpl->err) {
772 ERRMEM;
773 free(error_rpl);
774 return NULL;
775 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100776 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100777 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100778 reply = (struct nc_reply *)error_rpl;
779
780 i = 0;
781 LY_TREE_FOR(xml->child, iter) {
782 parse_rpc_error(ctx, iter, error_rpl->err + i);
783 ++i;
784 }
785
786 /* ok */
787 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
788 if (xml->child->next) {
789 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
790 return NULL;
791 }
792 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100793 if (!reply) {
794 ERRMEM;
795 return NULL;
796 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100797 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100798
799 /* some RPC output */
800 } else {
801 switch (rpc->type) {
802 case NC_RPC_GENERIC:
803 rpc_gen = (struct nc_rpc_generic *)rpc;
804
805 if (rpc_gen->has_data) {
806 schema = rpc_gen->content.data->schema;
807 } else {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100808 data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100809 if (!data) {
810 ERR("Failed to parse a generic RPC XML.");
811 return NULL;
812 }
813 schema = data->schema;
814 lyd_free(data);
815 data = NULL;
816 }
817 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100818 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100819 return NULL;
820 }
821 break;
822
823 case NC_RPC_GETCONFIG:
824 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100825 if (!xml->child->child) {
826 /* we did not receive any data */
827 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100828 if (!data_rpl) {
829 ERRMEM;
830 return NULL;
831 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100832 data_rpl->type = NC_RPL_DATA;
833 data_rpl->data = NULL;
834 return (struct nc_reply *)data_rpl;
835 }
836
Michal Vasko086311b2016-01-08 09:53:11 +0100837 /* special treatment */
838 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100839 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100840 if (!data) {
841 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
842 return NULL;
843 }
844 break;
845
846 case NC_RPC_GETSCHEMA:
Michal Vaskofea54dc2016-02-17 13:12:16 +0100847 schema = ly_ctx_get_node(ctx, "/ietf-netconf-monitoring:get-schema");
Michal Vasko086311b2016-01-08 09:53:11 +0100848 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100849 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100850 return NULL;
851 }
852 break;
853
854 case NC_RPC_EDIT:
855 case NC_RPC_COPY:
856 case NC_RPC_DELETE:
857 case NC_RPC_LOCK:
858 case NC_RPC_UNLOCK:
859 case NC_RPC_KILL:
860 case NC_RPC_COMMIT:
861 case NC_RPC_DISCARD:
862 case NC_RPC_CANCEL:
863 case NC_RPC_VALIDATE:
864 case NC_RPC_SUBSCRIBE:
865 /* there is no output defined */
866 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
867 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100868 default:
869 ERRINT;
870 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100871 }
872
873 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100874 if (!data_rpl) {
875 ERRMEM;
876 return NULL;
877 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100878 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100879 if (!data) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100880 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY | parseroptions, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100881 } else {
882 /* <get>, <get-config> */
883 data_rpl->data = data;
884 }
885 if (!data_rpl->data) {
886 ERR("Failed to parse <rpc-reply>.");
887 free(data_rpl);
888 return NULL;
889 }
890 reply = (struct nc_reply *)data_rpl;
891 }
892
893 return reply;
894}
895
Radek Krejci53691be2016-02-22 13:58:37 +0100896#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100897
Michal Vasko3031aae2016-01-27 16:07:18 +0100898int
899nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
900{
901 int sock;
902
903 if (!address || !port) {
904 ERRARG;
905 return -1;
906 }
907
908 sock = nc_sock_listen(address, port);
909 if (sock == -1) {
910 return -1;
911 }
912
913 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100914 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
915 if (!client_opts.ch_binds) {
916 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100917 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100918 return -1;
919 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100920
921 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100922 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
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 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
928 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
929 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
930
931 return 0;
932}
933
934int
935nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
936{
937 uint32_t i;
938 int ret = -1;
939
940 if (!address && !port && !ti) {
941 for (i = 0; i < client_opts.ch_bind_count; ++i) {
942 close(client_opts.ch_binds[i].sock);
943 free((char *)client_opts.ch_binds[i].address);
944
945 ret = 0;
946 }
947 free(client_opts.ch_binds);
948 client_opts.ch_binds = NULL;
949 client_opts.ch_bind_count = 0;
950 } else {
951 for (i = 0; i < client_opts.ch_bind_count; ++i) {
952 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
953 && (!port || (client_opts.ch_binds[i].port == port))
954 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
955 close(client_opts.ch_binds[i].sock);
956 free((char *)client_opts.ch_binds[i].address);
957
958 --client_opts.ch_bind_count;
959 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
960
961 ret = 0;
962 }
963 }
964 }
965
966 return ret;
967}
968
969API int
970nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
971{
972 int sock;
973 char *host = NULL;
974 uint16_t port, idx;
975
976 if (!client_opts.ch_binds || !session) {
977 ERRARG;
978 return -1;
979 }
980
981 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
982
Michal Vasko50456e82016-02-02 12:16:08 +0100983 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +0100984 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +0100985 return sock;
986 }
987
Radek Krejci53691be2016-02-22 13:58:37 +0100988#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100989 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100990 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100991 } else
992#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100993#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +0100994 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100995 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +0100996 } else
997#endif
998 {
Michal Vaskofee717c2016-02-01 13:25:43 +0100999 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001000 *session = NULL;
1001 }
1002
1003 free(host);
1004
1005 if (!(*session)) {
1006 return -1;
1007 }
1008
1009 return 1;
1010}
1011
Radek Krejci53691be2016-02-22 13:58:37 +01001012#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001013
Michal Vaskob7558c52016-02-26 15:04:19 +01001014API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001015nc_client_init(void)
1016{
1017 nc_init();
1018}
1019
1020API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001021nc_client_destroy(void)
1022{
1023 nc_client_schema_searchpath(NULL);
1024#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1025 nc_client_ch_del_bind(NULL, 0, 0);
1026#endif
1027#ifdef NC_ENABLED_SSH
1028 nc_client_ssh_destroy_opts();
1029#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001030#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001031 nc_client_tls_destroy_opts();
1032#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001033 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001034}
1035
Michal Vasko086311b2016-01-08 09:53:11 +01001036API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001037nc_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 +01001038{
1039 struct lyxml_elem *xml;
1040 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1041
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001042 if (!session || !rpc || !reply || (parseroptions & LYD_OPT_TYPEMASK)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001043 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001044 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001045 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001046 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001047 return NC_MSG_ERROR;
1048 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001049 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vasko086311b2016-01-08 09:53:11 +01001050 *reply = NULL;
1051
1052 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001053
1054 if (msgtype == NC_MSG_REPLY) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001055 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001056 lyxml_free(session->ctx, xml);
1057 if (!(*reply)) {
1058 return NC_MSG_ERROR;
1059 }
1060 }
1061
1062 return msgtype;
1063}
1064
1065API NC_MSG_TYPE
1066nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1067{
1068 struct lyxml_elem *xml, *ev_time;
1069 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1070
1071 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +01001072 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001073 return NC_MSG_ERROR;
1074 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001075 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001076 return NC_MSG_ERROR;
1077 }
1078
1079 msgtype = get_msg(session, timeout, 0, &xml);
1080
1081 if (msgtype == NC_MSG_NOTIF) {
1082 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001083 if (!*notif) {
1084 ERRMEM;
1085 lyxml_free(session->ctx, xml);
1086 return NC_MSG_ERROR;
1087 }
Michal Vasko086311b2016-01-08 09:53:11 +01001088
1089 /* eventTime */
1090 LY_TREE_FOR(xml->child, ev_time) {
1091 if (!strcmp(ev_time->name, "eventTime")) {
1092 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1093 /* lyd_parse does not know this element */
1094 lyxml_free(session->ctx, ev_time);
1095 break;
1096 }
1097 }
1098 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001099 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001100 goto fail;
1101 }
1102
1103 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001104 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001105 lyxml_free(session->ctx, xml);
1106 xml = NULL;
1107 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001108 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001109 goto fail;
1110 }
1111 }
1112
1113 return msgtype;
1114
1115fail:
1116 lydict_remove(session->ctx, (*notif)->datetime);
1117 lyd_free((*notif)->tree);
1118 free(*notif);
1119 *notif = NULL;
1120 lyxml_free(session->ctx, xml);
1121
1122 return NC_MSG_ERROR;
1123}
1124
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001125static void *
1126nc_recv_notif_thread(void *arg)
1127{
1128 struct nc_ntf_thread_arg *ntarg;
1129 struct nc_session *session;
1130 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1131 struct nc_notif *notif;
1132 NC_MSG_TYPE msgtype;
1133
1134 ntarg = (struct nc_ntf_thread_arg *)arg;
1135 session = ntarg->session;
1136 notif_clb = ntarg->notif_clb;
1137 free(ntarg);
1138
1139 while (session->ntf_tid) {
1140 msgtype = nc_recv_notif(session, 0, &notif);
1141 if (msgtype == NC_MSG_NOTIF) {
1142 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001143 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1144 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1145 nc_notif_free(notif);
1146 break;
1147 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001148 nc_notif_free(notif);
1149 }
1150
1151 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1152 }
1153
1154 return NULL;
1155}
1156
1157API int
1158nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1159{
1160 struct nc_ntf_thread_arg *ntarg;
1161 int ret;
1162
1163 if (!session || !notif_clb) {
1164 ERRARG;
1165 return -1;
1166 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1167 ERR("Session %u: invalid session to receive Notifications.", session->id);
1168 return -1;
1169 } else if (session->ntf_tid) {
1170 ERR("Session %u: separate notification thread is already running.", session->id);
1171 return -1;
1172 }
1173
1174 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001175 if (!ntarg) {
1176 ERRMEM;
1177 return -1;
1178 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001179 ntarg->session = session;
1180 ntarg->notif_clb = notif_clb;
1181
1182 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1183 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001184 if (!session->ntf_tid) {
1185 ERRMEM;
1186 free(ntarg);
1187 return -1;
1188 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001189
1190 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1191 if (ret) {
1192 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1193 free(ntarg);
1194 free((pthread_t *)session->ntf_tid);
1195 session->ntf_tid = NULL;
1196 return -1;
1197 }
1198
1199 return 0;
1200}
1201
Michal Vasko086311b2016-01-08 09:53:11 +01001202API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001203nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001204{
1205 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001206 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001207 struct nc_rpc_generic *rpc_gen;
1208 struct nc_rpc_getconfig *rpc_gc;
1209 struct nc_rpc_edit *rpc_e;
1210 struct nc_rpc_copy *rpc_cp;
1211 struct nc_rpc_delete *rpc_del;
1212 struct nc_rpc_lock *rpc_lock;
1213 struct nc_rpc_get *rpc_g;
1214 struct nc_rpc_kill *rpc_k;
1215 struct nc_rpc_commit *rpc_com;
1216 struct nc_rpc_cancel *rpc_can;
1217 struct nc_rpc_validate *rpc_val;
1218 struct nc_rpc_getschema *rpc_gs;
1219 struct nc_rpc_subscribe *rpc_sub;
1220 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001221 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001222 char str[11];
1223 uint64_t cur_msgid;
1224
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001225 if (!session || !rpc || !msgid) {
1226 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001227 return NC_MSG_ERROR;
1228 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001229 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001230 return NC_MSG_ERROR;
1231 }
1232
1233 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1234 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1235 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001236 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001237 return NC_MSG_ERROR;
1238 }
1239 }
1240
1241 switch (rpc->type) {
1242 case NC_RPC_GENERIC:
1243 rpc_gen = (struct nc_rpc_generic *)rpc;
1244
1245 if (rpc_gen->has_data) {
1246 data = rpc_gen->content.data;
1247 } else {
Michal Vaskoa4c23d82016-02-03 15:48:09 +01001248 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
Michal Vasko086311b2016-01-08 09:53:11 +01001249 }
1250 break;
1251
1252 case NC_RPC_GETCONFIG:
1253 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1254
1255 data = lyd_new(NULL, ietfnc, "get-config");
1256 node = lyd_new(data, ietfnc, "source");
1257 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1258 if (!node) {
1259 lyd_free(data);
1260 return NC_MSG_ERROR;
1261 }
1262 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001263 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001264 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1265 lyd_insert_attr(node, "type", "subtree");
1266 } else {
1267 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1268 lyd_insert_attr(node, "type", "xpath");
1269 lyd_insert_attr(node, "select", rpc_gc->filter);
1270 }
1271 if (!node) {
1272 lyd_free(data);
1273 return NC_MSG_ERROR;
1274 }
1275 }
1276
1277 if (rpc_gc->wd_mode) {
1278 if (!ietfncwd) {
1279 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1280 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001281 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001282 return NC_MSG_ERROR;
1283 }
1284 }
1285 switch (rpc_gc->wd_mode) {
1286 case NC_WD_UNKNOWN:
1287 /* cannot get here */
1288 break;
1289 case NC_WD_ALL:
1290 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1291 break;
1292 case NC_WD_ALL_TAG:
1293 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1294 break;
1295 case NC_WD_TRIM:
1296 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1297 break;
1298 case NC_WD_EXPLICIT:
1299 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1300 break;
1301 }
1302 if (!node) {
1303 lyd_free(data);
1304 return NC_MSG_ERROR;
1305 }
1306 }
1307 break;
1308
1309 case NC_RPC_EDIT:
1310 rpc_e = (struct nc_rpc_edit *)rpc;
1311
1312 data = lyd_new(NULL, ietfnc, "edit-config");
1313 node = lyd_new(data, ietfnc, "target");
1314 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1315 if (!node) {
1316 lyd_free(data);
1317 return NC_MSG_ERROR;
1318 }
1319
1320 if (rpc_e->default_op) {
1321 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1322 if (!node) {
1323 lyd_free(data);
1324 return NC_MSG_ERROR;
1325 }
1326 }
1327
1328 if (rpc_e->test_opt) {
1329 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1330 if (!node) {
1331 lyd_free(data);
1332 return NC_MSG_ERROR;
1333 }
1334 }
1335
1336 if (rpc_e->error_opt) {
1337 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1338 if (!node) {
1339 lyd_free(data);
1340 return NC_MSG_ERROR;
1341 }
1342 }
1343
1344 if (rpc_e->edit_cont[0] == '<') {
1345 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1346 } else {
1347 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1348 }
1349 if (!node) {
1350 lyd_free(data);
1351 return NC_MSG_ERROR;
1352 }
1353 break;
1354
1355 case NC_RPC_COPY:
1356 rpc_cp = (struct nc_rpc_copy *)rpc;
1357
1358 data = lyd_new(NULL, ietfnc, "copy-config");
1359 node = lyd_new(data, ietfnc, "target");
1360 if (rpc_cp->url_trg) {
1361 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1362 } else {
1363 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1364 }
1365 if (!node) {
1366 lyd_free(data);
1367 return NC_MSG_ERROR;
1368 }
1369
1370 node = lyd_new(data, ietfnc, "source");
1371 if (rpc_cp->url_config_src) {
1372 if (rpc_cp->url_config_src[0] == '<') {
1373 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1374 } else {
1375 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1376 }
1377 } else {
1378 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1379 }
1380 if (!node) {
1381 lyd_free(data);
1382 return NC_MSG_ERROR;
1383 }
1384
1385 if (rpc_cp->wd_mode) {
1386 if (!ietfncwd) {
1387 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1388 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001389 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001390 return NC_MSG_ERROR;
1391 }
1392 }
1393 switch (rpc_cp->wd_mode) {
1394 case NC_WD_UNKNOWN:
1395 /* cannot get here */
1396 break;
1397 case NC_WD_ALL:
1398 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1399 break;
1400 case NC_WD_ALL_TAG:
1401 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1402 break;
1403 case NC_WD_TRIM:
1404 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1405 break;
1406 case NC_WD_EXPLICIT:
1407 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1408 break;
1409 }
1410 if (!node) {
1411 lyd_free(data);
1412 return NC_MSG_ERROR;
1413 }
1414 }
1415 break;
1416
1417 case NC_RPC_DELETE:
1418 rpc_del = (struct nc_rpc_delete *)rpc;
1419
1420 data = lyd_new(NULL, ietfnc, "delete-config");
1421 node = lyd_new(data, ietfnc, "target");
1422 if (rpc_del->url) {
1423 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1424 } else {
1425 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1426 }
1427 if (!node) {
1428 lyd_free(data);
1429 return NC_MSG_ERROR;
1430 }
1431 break;
1432
1433 case NC_RPC_LOCK:
1434 rpc_lock = (struct nc_rpc_lock *)rpc;
1435
1436 data = lyd_new(NULL, ietfnc, "lock");
1437 node = lyd_new(data, ietfnc, "target");
1438 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1439 if (!node) {
1440 lyd_free(data);
1441 return NC_MSG_ERROR;
1442 }
1443 break;
1444
1445 case NC_RPC_UNLOCK:
1446 rpc_lock = (struct nc_rpc_lock *)rpc;
1447
1448 data = lyd_new(NULL, ietfnc, "unlock");
1449 node = lyd_new(data, ietfnc, "target");
1450 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1451 if (!node) {
1452 lyd_free(data);
1453 return NC_MSG_ERROR;
1454 }
1455 break;
1456
1457 case NC_RPC_GET:
1458 rpc_g = (struct nc_rpc_get *)rpc;
1459
1460 data = lyd_new(NULL, ietfnc, "get");
1461 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001462 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001463 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1464 lyd_insert_attr(node, "type", "subtree");
1465 } else {
1466 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1467 lyd_insert_attr(node, "type", "xpath");
1468 lyd_insert_attr(node, "select", rpc_g->filter);
1469 }
1470 if (!node) {
1471 lyd_free(data);
1472 return NC_MSG_ERROR;
1473 }
1474 }
1475
1476 if (rpc_g->wd_mode) {
1477 if (!ietfncwd) {
1478 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1479 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001480 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001481 return NC_MSG_ERROR;
1482 }
1483 }
1484 switch (rpc_g->wd_mode) {
1485 case NC_WD_UNKNOWN:
1486 /* cannot get here */
1487 break;
1488 case NC_WD_ALL:
1489 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1490 break;
1491 case NC_WD_ALL_TAG:
1492 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1493 break;
1494 case NC_WD_TRIM:
1495 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1496 break;
1497 case NC_WD_EXPLICIT:
1498 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1499 break;
1500 }
1501 if (!node) {
1502 lyd_free(data);
1503 return NC_MSG_ERROR;
1504 }
1505 }
1506 break;
1507
1508 case NC_RPC_KILL:
1509 rpc_k = (struct nc_rpc_kill *)rpc;
1510
1511 data = lyd_new(NULL, ietfnc, "kill-session");
1512 sprintf(str, "%u", rpc_k->sid);
1513 lyd_new_leaf(data, ietfnc, "session-id", str);
1514 break;
1515
1516 case NC_RPC_COMMIT:
1517 rpc_com = (struct nc_rpc_commit *)rpc;
1518
1519 data = lyd_new(NULL, ietfnc, "commit");
1520 if (rpc_com->confirmed) {
1521 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1522 }
1523
1524 if (rpc_com->confirm_timeout) {
1525 sprintf(str, "%u", rpc_com->confirm_timeout);
1526 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1527 }
1528
1529 if (rpc_com->persist) {
1530 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1531 if (!node) {
1532 lyd_free(data);
1533 return NC_MSG_ERROR;
1534 }
1535 }
1536
1537 if (rpc_com->persist_id) {
1538 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1539 if (!node) {
1540 lyd_free(data);
1541 return NC_MSG_ERROR;
1542 }
1543 }
1544 break;
1545
1546 case NC_RPC_DISCARD:
1547 data = lyd_new(NULL, ietfnc, "discard-changes");
1548 break;
1549
1550 case NC_RPC_CANCEL:
1551 rpc_can = (struct nc_rpc_cancel *)rpc;
1552
1553 data = lyd_new(NULL, ietfnc, "cancel-commit");
1554 if (rpc_can->persist_id) {
1555 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1556 if (!node) {
1557 lyd_free(data);
1558 return NC_MSG_ERROR;
1559 }
1560 }
1561 break;
1562
1563 case NC_RPC_VALIDATE:
1564 rpc_val = (struct nc_rpc_validate *)rpc;
1565
1566 data = lyd_new(NULL, ietfnc, "validate");
1567 node = lyd_new(data, ietfnc, "source");
1568 if (rpc_val->url_config_src) {
1569 if (rpc_val->url_config_src[0] == '<') {
1570 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1571 } else {
1572 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1573 }
1574 } else {
1575 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1576 }
1577 if (!node) {
1578 lyd_free(data);
1579 return NC_MSG_ERROR;
1580 }
1581 break;
1582
1583 case NC_RPC_GETSCHEMA:
1584 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1585 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001586 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001587 return NC_MSG_ERROR;
1588 }
1589
1590 rpc_gs = (struct nc_rpc_getschema *)rpc;
1591
1592 data = lyd_new(NULL, ietfncmon, "get-schema");
1593 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1594 if (!node) {
1595 lyd_free(data);
1596 return NC_MSG_ERROR;
1597 }
1598 if (rpc_gs->version) {
1599 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1600 if (!node) {
1601 lyd_free(data);
1602 return NC_MSG_ERROR;
1603 }
1604 }
1605 if (rpc_gs->format) {
1606 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1607 if (!node) {
1608 lyd_free(data);
1609 return NC_MSG_ERROR;
1610 }
1611 }
1612 break;
1613
1614 case NC_RPC_SUBSCRIBE:
1615 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1616 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001617 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001618 return NC_MSG_ERROR;
1619 }
1620
1621 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1622
1623 data = lyd_new(NULL, notifs, "create-subscription");
1624 if (rpc_sub->stream) {
1625 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1626 if (!node) {
1627 lyd_free(data);
1628 return NC_MSG_ERROR;
1629 }
1630 }
1631
1632 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001633 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vasko086311b2016-01-08 09:53:11 +01001634 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1635 lyd_insert_attr(node, "type", "subtree");
1636 } else {
1637 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1638 lyd_insert_attr(node, "type", "xpath");
1639 lyd_insert_attr(node, "select", rpc_sub->filter);
1640 }
1641 if (!node) {
1642 lyd_free(data);
1643 return NC_MSG_ERROR;
1644 }
1645 }
1646
1647 if (rpc_sub->start) {
1648 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1649 if (!node) {
1650 lyd_free(data);
1651 return NC_MSG_ERROR;
1652 }
1653 }
1654
1655 if (rpc_sub->stop) {
1656 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1657 if (!node) {
1658 lyd_free(data);
1659 return NC_MSG_ERROR;
1660 }
1661 }
1662 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001663 default:
1664 ERRINT;
1665 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001666 }
1667
1668 if (lyd_validate(data, LYD_OPT_STRICT)) {
1669 lyd_free(data);
1670 return NC_MSG_ERROR;
1671 }
1672
Michal Vasko62be1ce2016-03-03 13:24:52 +01001673 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001674 if (ret == -1) {
1675 /* error */
1676 r = NC_MSG_ERROR;
1677 } else if (!ret) {
1678 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001679 r = NC_MSG_WOULDBLOCK;
1680 } else {
1681 /* send RPC, store its message ID */
1682 r = nc_send_msg(session, data);
1683 cur_msgid = session->msgid;
1684 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001685 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001686
1687 lyd_free(data);
1688
1689 if (r != NC_MSG_RPC) {
1690 return r;
1691 }
1692
1693 *msgid = cur_msgid;
1694 return NC_MSG_RPC;
1695}