blob: 3f92af06920d8587c4f4e20f7ec8880a5e431fdb [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#include <assert.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <netdb.h>
27#include <pthread.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/socket.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <unistd.h>
34#include <arpa/inet.h>
35#include <poll.h>
36
37#include <libyang/libyang.h>
38
Michal Vasko086311b2016-01-08 09:53:11 +010039#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010040#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010041#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010042
Michal Vasko80ef5d22016-01-18 09:21:02 +010043static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
44
Michal Vasko3031aae2016-01-27 16:07:18 +010045static struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010046
47API int
Michal Vasko3031aae2016-01-27 16:07:18 +010048nc_client_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010049{
Michal Vasko3031aae2016-01-27 16:07:18 +010050 if (client_opts.schema_searchpath) {
51 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010052 }
Michal Vasko086311b2016-01-08 09:53:11 +010053
Michal Vasko7f1c78b2016-01-19 09:52:14 +010054 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010055 client_opts.schema_searchpath = strdup(path);
56 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010057 ERRMEM;
58 return 1;
59 }
60 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010061 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010062 }
63
64 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010065}
66
Michal Vasko3031aae2016-01-27 16:07:18 +010067/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010068static int
69ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
70{
71 const struct lys_module *module;
72 char *ptr, *ptr2;
73 char *model_name, *revision = NULL, *features = NULL;
74
75 /* parse module */
76 ptr = strstr(cpblt, "module=");
77 if (!ptr) {
Michal Vaskoef578332016-01-25 13:20:09 +010078 ERR("Unknown capability \"%s\" could not be parsed.", cpblt);
79 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +010080 }
81 ptr += 7;
82 ptr2 = strchr(ptr, '&');
83 if (!ptr2) {
84 ptr2 = ptr + strlen(ptr);
85 }
86 model_name = strndup(ptr, ptr2 - ptr);
87
88 /* parse revision */
89 ptr = strstr(cpblt, "revision=");
90 if (ptr) {
91 ptr += 9;
92 ptr2 = strchr(ptr, '&');
93 if (!ptr2) {
94 ptr2 = ptr + strlen(ptr);
95 }
96 revision = strndup(ptr, ptr2 - ptr);
97 }
98
99 /* load module if needed */
100 module = ly_ctx_get_module(session->ctx, model_name, revision);
101 if (!module) {
102 module = ly_ctx_load_module(session->ctx, model_name, revision);
103 }
104
Michal Vasko086311b2016-01-08 09:53:11 +0100105 free(revision);
106 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100107 WRN("Failed to load model \"%s\".", model_name);
108 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100109 return 1;
110 }
Michal Vaskoef578332016-01-25 13:20:09 +0100111 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100112
113 /* parse features */
114 ptr = strstr(cpblt, "features=");
115 if (ptr) {
116 ptr += 9;
117 ptr2 = strchr(ptr, '&');
118 if (!ptr2) {
119 ptr2 = ptr + strlen(ptr);
120 }
121 features = strndup(ptr, ptr2 - ptr);
122 }
123
124 /* enable features */
125 if (features) {
126 /* basically manual strtok_r (to avoid macro) */
127 ptr2 = features;
128 for (ptr = features; *ptr; ++ptr) {
129 if (*ptr == ',') {
130 *ptr = '\0';
131 /* remember last feature */
132 ptr2 = ptr + 1;
133 }
134 }
135
136 ptr = features;
137 lys_features_enable(module, ptr);
138 while (ptr != ptr2) {
139 ptr += strlen(ptr) + 1;
140 lys_features_enable(module, ptr);
141 }
142
143 free(features);
144 }
145
146 return 0;
147}
148
149/* SCHEMAS_DIR used */
150static int
151ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts, int from_file)
152{
153 int i;
154 const struct lys_module *ietfnc;
155
156 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
157 if (!ietfnc) {
158 if (from_file) {
159 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
160 } else {
161 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
162 }
163 }
164 if (!ietfnc) {
165 ERR("Loading base NETCONF schema failed.");
166 return 1;
167 }
168
169 /* set supported capabilities from ietf-netconf */
170 for (i = 0; cpblts[i]; ++i) {
171 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
172 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
173 lys_features_enable(ietfnc, "writable-running");
174 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
175 lys_features_enable(ietfnc, "candidate");
176 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
177 lys_features_enable(ietfnc, "confirmed-commit");
178 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
179 lys_features_enable(ietfnc, "rollback-on-error");
180 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
181 lys_features_enable(ietfnc, "validate");
182 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
183 lys_features_enable(ietfnc, "startup");
184 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
185 lys_features_enable(ietfnc, "url");
186 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
187 lys_features_enable(ietfnc, "xpath");
188 }
189 }
190 }
191
192 return 0;
193}
194
195static char *
196libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
197 void (**free_model_data)(char *model_data))
198{
199 struct nc_session *session = (struct nc_session *)user_data;
200 struct nc_rpc *rpc;
201 struct nc_reply *reply;
202 struct nc_reply_data *data_rpl;
203 NC_MSG_TYPE msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100204 char *model_data = NULL, *ptr, *ptr2, *anyxml;
Michal Vasko086311b2016-01-08 09:53:11 +0100205 uint64_t msgid;
206
207 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100208 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100209 *format = LYS_IN_YIN;
210
211 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
212 usleep(1000);
213 }
214 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100215 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100216 nc_rpc_free(rpc);
217 return NULL;
218 }
219
220 msg = nc_recv_reply(session, rpc, msgid, 250, &reply);
221 nc_rpc_free(rpc);
222 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100223 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100224 return NULL;
225 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100226 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100227 return NULL;
228 }
229
Michal Vasko05ba9df2016-01-13 14:40:27 +0100230 if (reply->type != NC_RPL_DATA) {
231 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100232 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100233 nc_reply_free(reply);
234 return NULL;
235 }
236
Michal Vasko086311b2016-01-08 09:53:11 +0100237 data_rpl = (struct nc_reply_data *)reply;
238 anyxml = lyxml_serialize(((struct lyd_node_anyxml *)data_rpl->data)->value);
239 nc_reply_free(reply);
240 *free_model_data = NULL;
241
242 /* it's with the data root node, remove it */
243 if (anyxml) {
244 ptr = strchr(anyxml, '>');
245 ++ptr;
246
247 ptr2 = strrchr(anyxml, '<');
248
249 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
250 free(anyxml);
251 }
252
253 return model_data;
254}
255
Michal Vaskoef578332016-01-25 13:20:09 +0100256/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100257int
258nc_ctx_check_and_fill(struct nc_session *session)
259{
Michal Vaskoef578332016-01-25 13:20:09 +0100260 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100261 ly_module_clb old_clb = NULL;
262 void *old_data = NULL;
263
264 assert(session->cpblts && session->ctx);
265
266 /* check if get-schema is supported */
267 for (i = 0; session->cpblts[i]; ++i) {
268 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
269 get_schema_support = 1;
270 break;
271 }
272 }
273
274 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
275 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
276 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
277 /* set module retrieval using <get-schema> */
278 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
279 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
280 } else {
281 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
282 }
283 }
284
285 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
286 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts, !get_schema_support)) {
287 if (old_clb) {
288 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
289 }
Michal Vaskoef578332016-01-25 13:20:09 +0100290 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100291 }
292
293 /* load all other models */
294 for (i = 0; session->cpblts[i]; ++i) {
295 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
296 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
297 continue;
298 }
299
Michal Vaskoef578332016-01-25 13:20:09 +0100300 r = ctx_check_and_load_model(session, session->cpblts[i]);
301 if (r == -1) {
302 ret = -1;
303 break;
304 }
305
306 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
307 * if it was using get-schema */
308 if (r == 1) {
309 if (get_schema_support) {
310 VRB("Trying to load the schema from a different source.");
311 /* works even if old_clb is NULL */
312 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
313 r = ctx_check_and_load_model(session, session->cpblts[i]);
314 }
315
316 /* fail again (or no other way to try), too bad */
317 if (r) {
318 ret = 1;
319 }
320
321 /* set get-schema callback back */
322 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
323 }
Michal Vasko086311b2016-01-08 09:53:11 +0100324 }
325
326 if (old_clb) {
327 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
328 }
Michal Vaskoef578332016-01-25 13:20:09 +0100329 if (ret == 1) {
330 WRN("Some models failed to be loaded, any data from these models will be ignored.");
331 }
332 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100333}
334
335API struct nc_session *
336nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
337{
Michal Vaskod083db62016-01-19 10:31:29 +0100338 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100339
Michal Vaskod083db62016-01-19 10:31:29 +0100340 if ((fdin < 0) || (fdout < 0)) {
341 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100342 return NULL;
343 }
344
345 /* prepare session structure */
346 session = calloc(1, sizeof *session);
347 if (!session) {
348 ERRMEM;
349 return NULL;
350 }
351 session->status = NC_STATUS_STARTING;
352 session->side = NC_CLIENT;
353
354 /* transport specific data */
355 session->ti_type = NC_TI_FD;
356 session->ti.fd.in = fdin;
357 session->ti.fd.out = fdout;
358
359 /* assign context (dicionary needed for handshake) */
360 if (!ctx) {
361 ctx = ly_ctx_new(SCHEMAS_DIR);
362 } else {
363 session->flags |= NC_SESSION_SHAREDCTX;
364 }
365 session->ctx = ctx;
366
367 /* NETCONF handshake */
368 if (nc_handshake(session)) {
369 goto fail;
370 }
371 session->status = NC_STATUS_RUNNING;
372
Michal Vaskoef578332016-01-25 13:20:09 +0100373 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100374 goto fail;
375 }
376
377 return session;
378
379fail:
380 nc_session_free(session);
381 return NULL;
382}
383
384int
Michal Vaskof05562c2016-01-20 12:06:43 +0100385nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100386{
387 int i, sock = -1;
388 struct addrinfo hints, *res_list, *res;
389 char port_s[6]; /* length of string representation of short int */
390
391 snprintf(port_s, 6, "%u", port);
392
393 /* Connect to a server */
394 memset(&hints, 0, sizeof hints);
395 hints.ai_family = AF_UNSPEC;
396 hints.ai_socktype = SOCK_STREAM;
397 hints.ai_protocol = IPPROTO_TCP;
398 i = getaddrinfo(host, port_s, &hints, &res_list);
399 if (i != 0) {
400 ERR("Unable to translate the host address (%s).", gai_strerror(i));
401 return -1;
402 }
403
404 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
405 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
406 if (sock == -1) {
407 /* socket was not created, try another resource */
408 i = errno;
409 goto errloop;
410 }
411
412 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
413 /* network connection failed, try another resource */
414 i = errno;
415 close(sock);
416 sock = -1;
417 goto errloop;
418 }
419
420 /* we're done, network connection established */
421 break;
422errloop:
423 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
424 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
425 continue;
426 }
427
428 if (sock == -1) {
429 ERR("Unable to connect to %s:%s.", host, port_s);
430 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100431 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 +0100432 }
433 freeaddrinfo(res_list);
434
435 return sock;
436}
437
Michal Vasko086311b2016-01-08 09:53:11 +0100438static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100439get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100440{
Michal Vasko2518b6b2016-01-28 13:24:53 +0100441 int r, elapsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100442 char *ptr;
443 const char *str_msgid;
444 uint64_t cur_msgid;
445 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100446 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100447 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
448
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100449 r = nc_timedlock(session->ti_lock, timeout, &elapsed);
450 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100451 /* error */
452 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100453 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100454 /* timeout */
455 return NC_MSG_WOULDBLOCK;
456 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100457 if (timeout > 0) {
458 timeout -= elapsed;
459 }
Michal Vasko086311b2016-01-08 09:53:11 +0100460
461 /* try to get notification from the session's queue */
462 if (!msgid && session->notifs) {
463 cont = session->notifs;
464 session->notifs = cont->next;
465
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100466 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100467
468 *msg = cont->msg;
469 free(cont);
470
471 return NC_MSG_NOTIF;
472 }
473
474 /* try to get rpc-reply from the session's queue */
475 if (msgid && session->replies) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100476 while (session->replies) {
477 cont = session->replies;
478 session->replies = cont->next;
479
Michal Vasko086311b2016-01-08 09:53:11 +0100480 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
481 cur_msgid = strtoul(str_msgid, &ptr, 10);
482
483 if (cur_msgid == msgid) {
Michal Vasko2518b6b2016-01-28 13:24:53 +0100484 session->replies = cont->next;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100485 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100486
487 *msg = cont->msg;
488 free(cont);
489
490 return NC_MSG_REPLY;
491 }
492
Michal Vasko2518b6b2016-01-28 13:24:53 +0100493 ERR("Session %u: discarding a <rpc-reply> with an unexpected message-id \"%s\".", str_msgid);
494 lyxml_free(session->ctx, cont->msg);
495 free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100496 }
497 }
498
499 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100500 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100501
502 /* we read rpc-reply, want a notif */
503 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100504 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100505 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
506 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100507 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100508 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100509 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100510 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100511 }
Michal Vasko086311b2016-01-08 09:53:11 +0100512
513 cont_ptr = &session->replies;
514 while (*cont_ptr) {
515 cont_ptr = &((*cont_ptr)->next);
516 }
517 *cont_ptr = malloc(sizeof **cont_ptr);
518 (*cont_ptr)->msg = xml;
519 (*cont_ptr)->next = NULL;
520 }
521
522 /* we read notif, want a rpc-reply */
523 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100524 /* TODO check whether the session is even subscribed */
525 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100526 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100527 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100528 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100529 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100530 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100531
532 cont_ptr = &session->notifs;
533 while (*cont_ptr) {
534 cont_ptr = &((*cont_ptr)->next);
535 }
536 *cont_ptr = malloc(sizeof **cont_ptr);
537 (*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) {
552 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100553 }
Michal Vasko086311b2016-01-08 09:53:11 +0100554 break;
555
556 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100557 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100558 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100559 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100560
561 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100562 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100564 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100565
566 default:
567 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
568 * NC_MSG_NONE is not returned by nc_read_msg()
569 */
570 break;
571 }
572
573 return msgtype;
574}
575
576/* cannot strictly fail, but does not need to fill any error parameter at all */
577static void
578parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
579{
580 struct lyxml_elem *iter, *next, *info;
581
582 LY_TREE_FOR(xml->child, iter) {
583 if (!iter->ns) {
584 if (iter->content) {
585 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
586 } else {
587 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
588 }
589 continue;
590 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
591 if (iter->content) {
592 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
593 iter->name, iter->content, iter->ns->value);
594 } else {
595 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
596 }
597 continue;
598 }
599
600 if (!strcmp(iter->name, "error-type")) {
601 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
602 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
603 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
604 } else if (err->type) {
605 WRN("<rpc-error> <error-type> duplicated.");
606 } else {
607 err->type = lydict_insert(ctx, iter->content, 0);
608 }
609 } else if (!strcmp(iter->name, "error-tag")) {
610 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
611 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
612 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
613 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
614 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
615 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
616 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
617 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
618 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
619 && strcmp(iter->content, "malformed-message"))) {
620 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
621 } else if (err->tag) {
622 WRN("<rpc-error> <error-tag> duplicated.");
623 } else {
624 err->tag = lydict_insert(ctx, iter->content, 0);
625 }
626 } else if (!strcmp(iter->name, "error-severity")) {
627 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
628 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
629 } else if (err->severity) {
630 WRN("<rpc-error> <error-severity> duplicated.");
631 } else {
632 err->severity = lydict_insert(ctx, iter->content, 0);
633 }
634 } else if (!strcmp(iter->name, "error-app-tag")) {
635 if (err->apptag) {
636 WRN("<rpc-error> <error-app-tag> duplicated.");
637 } else {
638 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
639 }
640 } else if (!strcmp(iter->name, "error-path")) {
641 if (err->path) {
642 WRN("<rpc-error> <error-path> duplicated.");
643 } else {
644 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
645 }
646 } else if (!strcmp(iter->name, "error-message")) {
647 if (err->message) {
648 WRN("<rpc-error> <error-message> duplicated.");
649 } else {
650 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
651 if (!err->message_lang) {
652 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
653 }
654 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
655 }
656 } else if (!strcmp(iter->name, "error-info")) {
657 LY_TREE_FOR_SAFE(iter->child, next, info) {
658 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
659 if (!strcmp(info->name, "session-id")) {
660 if (err->sid) {
661 WRN("<rpc-error> <error-info> <session-id> duplicated.");
662 } else {
663 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
664 }
665 } else if (!strcmp(info->name, "bad-attr")) {
666 ++err->attr_count;
667 err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr);
668 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
669 } else if (!strcmp(info->name, "bad-element")) {
670 ++err->elem_count;
671 err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem);
672 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
673 } else if (!strcmp(info->name, "bad-namespace")) {
674 ++err->ns_count;
675 err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns);
676 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
677 } else {
678 if (info->content) {
679 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
680 info->name, info->content);
681 } else {
682 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
683 }
684 }
685 } else {
686 lyxml_unlink(ctx, info);
687 ++err->other_count;
688 err->other = realloc(err->other, err->other_count * sizeof *err->other);
689 err->other[err->other_count - 1] = info;
690 }
691 }
692 } else {
693 if (iter->content) {
694 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
695 } else {
696 WRN("<rpc-error> unknown child \"%s\".", iter->name);
697 }
698 }
699 }
700}
701
702static struct nc_reply *
703parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc)
704{
705 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100706 const struct lys_node *schema = NULL;
707 const struct lys_module *mod;
Michal Vasko086311b2016-01-08 09:53:11 +0100708 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100709 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100710 struct nc_reply_data *data_rpl;
711 struct nc_reply *reply = NULL;
712 struct nc_rpc_generic *rpc_gen;
713 int i;
714
715 if (!xml->child) {
716 ERR("An empty <rpc-reply>.");
717 return NULL;
718 }
719
720 /* rpc-error */
721 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
722 /* count and check elements */
723 i = 0;
724 LY_TREE_FOR(xml->child, iter) {
725 if (strcmp(iter->name, "rpc-error")) {
726 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
727 return NULL;
728 } else if (!iter->ns) {
729 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
730 return NULL;
731 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
732 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
733 return NULL;
734 }
735 ++i;
736 }
737
738 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100739 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100740 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100741 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100742 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100743 reply = (struct nc_reply *)error_rpl;
744
745 i = 0;
746 LY_TREE_FOR(xml->child, iter) {
747 parse_rpc_error(ctx, iter, error_rpl->err + i);
748 ++i;
749 }
750
751 /* ok */
752 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
753 if (xml->child->next) {
754 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
755 return NULL;
756 }
757 reply = malloc(sizeof *reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100758 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100759
760 /* some RPC output */
761 } else {
762 switch (rpc->type) {
763 case NC_RPC_GENERIC:
764 rpc_gen = (struct nc_rpc_generic *)rpc;
765
766 if (rpc_gen->has_data) {
767 schema = rpc_gen->content.data->schema;
768 } else {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100769 data = lyd_parse_data(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC);
Michal Vasko086311b2016-01-08 09:53:11 +0100770 if (!data) {
771 ERR("Failed to parse a generic RPC XML.");
772 return NULL;
773 }
774 schema = data->schema;
775 lyd_free(data);
776 data = NULL;
777 }
778 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100779 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100780 return NULL;
781 }
782 break;
783
784 case NC_RPC_GETCONFIG:
785 case NC_RPC_GET:
786 /* special treatment */
787 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
788 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET));
789 if (!data) {
790 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
791 return NULL;
792 }
793 break;
794
795 case NC_RPC_GETSCHEMA:
Michal Vasko0473c4c2016-01-19 10:40:06 +0100796 mod = ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL);
797 if (mod) {
798 schema = lys_get_node(mod, "/get-schema");
799 }
Michal Vasko086311b2016-01-08 09:53:11 +0100800 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100801 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100802 return NULL;
803 }
804 break;
805
806 case NC_RPC_EDIT:
807 case NC_RPC_COPY:
808 case NC_RPC_DELETE:
809 case NC_RPC_LOCK:
810 case NC_RPC_UNLOCK:
811 case NC_RPC_KILL:
812 case NC_RPC_COMMIT:
813 case NC_RPC_DISCARD:
814 case NC_RPC_CANCEL:
815 case NC_RPC_VALIDATE:
816 case NC_RPC_SUBSCRIBE:
817 /* there is no output defined */
818 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
819 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100820 default:
821 ERRINT;
822 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100823 }
824
825 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100826 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100827 if (!data) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100828 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100829 } else {
830 /* <get>, <get-config> */
831 data_rpl->data = data;
832 }
833 if (!data_rpl->data) {
834 ERR("Failed to parse <rpc-reply>.");
835 free(data_rpl);
836 return NULL;
837 }
838 reply = (struct nc_reply *)data_rpl;
839 }
840
841 return reply;
842}
843
Michal Vasko3d865d22016-01-28 16:00:53 +0100844#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
845
Michal Vasko3031aae2016-01-27 16:07:18 +0100846int
847nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
848{
849 int sock;
850
851 if (!address || !port) {
852 ERRARG;
853 return -1;
854 }
855
856 sock = nc_sock_listen(address, port);
857 if (sock == -1) {
858 return -1;
859 }
860
861 ++client_opts.ch_bind_count;
862 client_opts.ch_binds = realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
863
864 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
865 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
866 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
867 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
868
869 return 0;
870}
871
872int
873nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
874{
875 uint32_t i;
876 int ret = -1;
877
878 if (!address && !port && !ti) {
879 for (i = 0; i < client_opts.ch_bind_count; ++i) {
880 close(client_opts.ch_binds[i].sock);
881 free((char *)client_opts.ch_binds[i].address);
882
883 ret = 0;
884 }
885 free(client_opts.ch_binds);
886 client_opts.ch_binds = NULL;
887 client_opts.ch_bind_count = 0;
888 } else {
889 for (i = 0; i < client_opts.ch_bind_count; ++i) {
890 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
891 && (!port || (client_opts.ch_binds[i].port == port))
892 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
893 close(client_opts.ch_binds[i].sock);
894 free((char *)client_opts.ch_binds[i].address);
895
896 --client_opts.ch_bind_count;
897 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
898
899 ret = 0;
900 }
901 }
902 }
903
904 return ret;
905}
906
907API int
908nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
909{
910 int sock;
911 char *host = NULL;
912 uint16_t port, idx;
913
914 if (!client_opts.ch_binds || !session) {
915 ERRARG;
916 return -1;
917 }
918
919 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
920
921 if (sock < 1) {
922 return sock;
923 }
924
Michal Vasko3d865d22016-01-28 16:00:53 +0100925#ifdef ENABLE_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100926 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100927 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100928 } else
929#endif
930#ifdef ENABLE_TLS
931 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100932 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx);
Michal Vasko3d865d22016-01-28 16:00:53 +0100933 } else
934#endif
935 {
Michal Vasko3031aae2016-01-27 16:07:18 +0100936 *session = NULL;
937 }
938
939 free(host);
940
941 if (!(*session)) {
942 return -1;
943 }
944
945 return 1;
946}
947
Michal Vasko3d865d22016-01-28 16:00:53 +0100948#endif /* ENABLE_SSH || ENABLE_TLS */
949
Michal Vasko086311b2016-01-08 09:53:11 +0100950API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100951nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct nc_reply **reply)
Michal Vasko086311b2016-01-08 09:53:11 +0100952{
953 struct lyxml_elem *xml;
954 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
955
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100956 if (!session || !rpc || !reply) {
957 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100958 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100959 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100960 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100961 return NC_MSG_ERROR;
962 }
963 *reply = NULL;
964
965 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100966
967 if (msgtype == NC_MSG_REPLY) {
968 *reply = parse_reply(session->ctx, xml, rpc);
969 lyxml_free(session->ctx, xml);
970 if (!(*reply)) {
971 return NC_MSG_ERROR;
972 }
973 }
974
975 return msgtype;
976}
977
978API NC_MSG_TYPE
979nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
980{
981 struct lyxml_elem *xml, *ev_time;
982 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
983
984 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100985 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100986 return NC_MSG_ERROR;
987 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100988 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100989 return NC_MSG_ERROR;
990 }
991
992 msgtype = get_msg(session, timeout, 0, &xml);
993
994 if (msgtype == NC_MSG_NOTIF) {
995 *notif = calloc(1, sizeof **notif);
996
997 /* eventTime */
998 LY_TREE_FOR(xml->child, ev_time) {
999 if (!strcmp(ev_time->name, "eventTime")) {
1000 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1001 /* lyd_parse does not know this element */
1002 lyxml_free(session->ctx, ev_time);
1003 break;
1004 }
1005 }
1006 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001007 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001008 goto fail;
1009 }
1010
1011 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001012 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001013 lyxml_free(session->ctx, xml);
1014 xml = NULL;
1015 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001016 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001017 goto fail;
1018 }
1019 }
1020
1021 return msgtype;
1022
1023fail:
1024 lydict_remove(session->ctx, (*notif)->datetime);
1025 lyd_free((*notif)->tree);
1026 free(*notif);
1027 *notif = NULL;
1028 lyxml_free(session->ctx, xml);
1029
1030 return NC_MSG_ERROR;
1031}
1032
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001033static void *
1034nc_recv_notif_thread(void *arg)
1035{
1036 struct nc_ntf_thread_arg *ntarg;
1037 struct nc_session *session;
1038 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1039 struct nc_notif *notif;
1040 NC_MSG_TYPE msgtype;
1041
1042 ntarg = (struct nc_ntf_thread_arg *)arg;
1043 session = ntarg->session;
1044 notif_clb = ntarg->notif_clb;
1045 free(ntarg);
1046
1047 while (session->ntf_tid) {
1048 msgtype = nc_recv_notif(session, 0, &notif);
1049 if (msgtype == NC_MSG_NOTIF) {
1050 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001051 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1052 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1053 nc_notif_free(notif);
1054 break;
1055 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001056 nc_notif_free(notif);
1057 }
1058
1059 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1060 }
1061
1062 return NULL;
1063}
1064
1065API int
1066nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1067{
1068 struct nc_ntf_thread_arg *ntarg;
1069 int ret;
1070
1071 if (!session || !notif_clb) {
1072 ERRARG;
1073 return -1;
1074 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1075 ERR("Session %u: invalid session to receive Notifications.", session->id);
1076 return -1;
1077 } else if (session->ntf_tid) {
1078 ERR("Session %u: separate notification thread is already running.", session->id);
1079 return -1;
1080 }
1081
1082 ntarg = malloc(sizeof *ntarg);
1083 ntarg->session = session;
1084 ntarg->notif_clb = notif_clb;
1085
1086 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1087 session->ntf_tid = malloc(sizeof *session->ntf_tid);
1088
1089 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1090 if (ret) {
1091 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1092 free(ntarg);
1093 free((pthread_t *)session->ntf_tid);
1094 session->ntf_tid = NULL;
1095 return -1;
1096 }
1097
1098 return 0;
1099}
1100
Michal Vasko086311b2016-01-08 09:53:11 +01001101API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001102nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001103{
1104 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001105 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001106 struct nc_rpc_generic *rpc_gen;
1107 struct nc_rpc_getconfig *rpc_gc;
1108 struct nc_rpc_edit *rpc_e;
1109 struct nc_rpc_copy *rpc_cp;
1110 struct nc_rpc_delete *rpc_del;
1111 struct nc_rpc_lock *rpc_lock;
1112 struct nc_rpc_get *rpc_g;
1113 struct nc_rpc_kill *rpc_k;
1114 struct nc_rpc_commit *rpc_com;
1115 struct nc_rpc_cancel *rpc_can;
1116 struct nc_rpc_validate *rpc_val;
1117 struct nc_rpc_getschema *rpc_gs;
1118 struct nc_rpc_subscribe *rpc_sub;
1119 struct lyd_node *data, *node;
1120 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
1121 char str[11];
1122 uint64_t cur_msgid;
1123
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001124 if (!session || !rpc || !msgid) {
1125 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001126 return NC_MSG_ERROR;
1127 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001128 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001129 return NC_MSG_ERROR;
1130 }
1131
1132 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1133 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1134 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001135 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001136 return NC_MSG_ERROR;
1137 }
1138 }
1139
1140 switch (rpc->type) {
1141 case NC_RPC_GENERIC:
1142 rpc_gen = (struct nc_rpc_generic *)rpc;
1143
1144 if (rpc_gen->has_data) {
1145 data = rpc_gen->content.data;
1146 } else {
1147 data = lyd_parse_data(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
1148 }
1149 break;
1150
1151 case NC_RPC_GETCONFIG:
1152 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1153
1154 data = lyd_new(NULL, ietfnc, "get-config");
1155 node = lyd_new(data, ietfnc, "source");
1156 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1157 if (!node) {
1158 lyd_free(data);
1159 return NC_MSG_ERROR;
1160 }
1161 if (rpc_gc->filter) {
1162 if (rpc_gc->filter[0] == '<') {
1163 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1164 lyd_insert_attr(node, "type", "subtree");
1165 } else {
1166 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1167 lyd_insert_attr(node, "type", "xpath");
1168 lyd_insert_attr(node, "select", rpc_gc->filter);
1169 }
1170 if (!node) {
1171 lyd_free(data);
1172 return NC_MSG_ERROR;
1173 }
1174 }
1175
1176 if (rpc_gc->wd_mode) {
1177 if (!ietfncwd) {
1178 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1179 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001180 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001181 return NC_MSG_ERROR;
1182 }
1183 }
1184 switch (rpc_gc->wd_mode) {
1185 case NC_WD_UNKNOWN:
1186 /* cannot get here */
1187 break;
1188 case NC_WD_ALL:
1189 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1190 break;
1191 case NC_WD_ALL_TAG:
1192 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1193 break;
1194 case NC_WD_TRIM:
1195 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1196 break;
1197 case NC_WD_EXPLICIT:
1198 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1199 break;
1200 }
1201 if (!node) {
1202 lyd_free(data);
1203 return NC_MSG_ERROR;
1204 }
1205 }
1206 break;
1207
1208 case NC_RPC_EDIT:
1209 rpc_e = (struct nc_rpc_edit *)rpc;
1210
1211 data = lyd_new(NULL, ietfnc, "edit-config");
1212 node = lyd_new(data, ietfnc, "target");
1213 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1214 if (!node) {
1215 lyd_free(data);
1216 return NC_MSG_ERROR;
1217 }
1218
1219 if (rpc_e->default_op) {
1220 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1221 if (!node) {
1222 lyd_free(data);
1223 return NC_MSG_ERROR;
1224 }
1225 }
1226
1227 if (rpc_e->test_opt) {
1228 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1229 if (!node) {
1230 lyd_free(data);
1231 return NC_MSG_ERROR;
1232 }
1233 }
1234
1235 if (rpc_e->error_opt) {
1236 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1237 if (!node) {
1238 lyd_free(data);
1239 return NC_MSG_ERROR;
1240 }
1241 }
1242
1243 if (rpc_e->edit_cont[0] == '<') {
1244 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1245 } else {
1246 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1247 }
1248 if (!node) {
1249 lyd_free(data);
1250 return NC_MSG_ERROR;
1251 }
1252 break;
1253
1254 case NC_RPC_COPY:
1255 rpc_cp = (struct nc_rpc_copy *)rpc;
1256
1257 data = lyd_new(NULL, ietfnc, "copy-config");
1258 node = lyd_new(data, ietfnc, "target");
1259 if (rpc_cp->url_trg) {
1260 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1261 } else {
1262 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1263 }
1264 if (!node) {
1265 lyd_free(data);
1266 return NC_MSG_ERROR;
1267 }
1268
1269 node = lyd_new(data, ietfnc, "source");
1270 if (rpc_cp->url_config_src) {
1271 if (rpc_cp->url_config_src[0] == '<') {
1272 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1273 } else {
1274 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1275 }
1276 } else {
1277 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1278 }
1279 if (!node) {
1280 lyd_free(data);
1281 return NC_MSG_ERROR;
1282 }
1283
1284 if (rpc_cp->wd_mode) {
1285 if (!ietfncwd) {
1286 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1287 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001288 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001289 return NC_MSG_ERROR;
1290 }
1291 }
1292 switch (rpc_cp->wd_mode) {
1293 case NC_WD_UNKNOWN:
1294 /* cannot get here */
1295 break;
1296 case NC_WD_ALL:
1297 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1298 break;
1299 case NC_WD_ALL_TAG:
1300 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1301 break;
1302 case NC_WD_TRIM:
1303 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1304 break;
1305 case NC_WD_EXPLICIT:
1306 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1307 break;
1308 }
1309 if (!node) {
1310 lyd_free(data);
1311 return NC_MSG_ERROR;
1312 }
1313 }
1314 break;
1315
1316 case NC_RPC_DELETE:
1317 rpc_del = (struct nc_rpc_delete *)rpc;
1318
1319 data = lyd_new(NULL, ietfnc, "delete-config");
1320 node = lyd_new(data, ietfnc, "target");
1321 if (rpc_del->url) {
1322 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1323 } else {
1324 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1325 }
1326 if (!node) {
1327 lyd_free(data);
1328 return NC_MSG_ERROR;
1329 }
1330 break;
1331
1332 case NC_RPC_LOCK:
1333 rpc_lock = (struct nc_rpc_lock *)rpc;
1334
1335 data = lyd_new(NULL, ietfnc, "lock");
1336 node = lyd_new(data, ietfnc, "target");
1337 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1338 if (!node) {
1339 lyd_free(data);
1340 return NC_MSG_ERROR;
1341 }
1342 break;
1343
1344 case NC_RPC_UNLOCK:
1345 rpc_lock = (struct nc_rpc_lock *)rpc;
1346
1347 data = lyd_new(NULL, ietfnc, "unlock");
1348 node = lyd_new(data, ietfnc, "target");
1349 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1350 if (!node) {
1351 lyd_free(data);
1352 return NC_MSG_ERROR;
1353 }
1354 break;
1355
1356 case NC_RPC_GET:
1357 rpc_g = (struct nc_rpc_get *)rpc;
1358
1359 data = lyd_new(NULL, ietfnc, "get");
1360 if (rpc_g->filter) {
1361 if (rpc_g->filter[0] == '<') {
1362 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1363 lyd_insert_attr(node, "type", "subtree");
1364 } else {
1365 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1366 lyd_insert_attr(node, "type", "xpath");
1367 lyd_insert_attr(node, "select", rpc_g->filter);
1368 }
1369 if (!node) {
1370 lyd_free(data);
1371 return NC_MSG_ERROR;
1372 }
1373 }
1374
1375 if (rpc_g->wd_mode) {
1376 if (!ietfncwd) {
1377 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1378 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001379 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001380 return NC_MSG_ERROR;
1381 }
1382 }
1383 switch (rpc_g->wd_mode) {
1384 case NC_WD_UNKNOWN:
1385 /* cannot get here */
1386 break;
1387 case NC_WD_ALL:
1388 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1389 break;
1390 case NC_WD_ALL_TAG:
1391 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1392 break;
1393 case NC_WD_TRIM:
1394 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1395 break;
1396 case NC_WD_EXPLICIT:
1397 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1398 break;
1399 }
1400 if (!node) {
1401 lyd_free(data);
1402 return NC_MSG_ERROR;
1403 }
1404 }
1405 break;
1406
1407 case NC_RPC_KILL:
1408 rpc_k = (struct nc_rpc_kill *)rpc;
1409
1410 data = lyd_new(NULL, ietfnc, "kill-session");
1411 sprintf(str, "%u", rpc_k->sid);
1412 lyd_new_leaf(data, ietfnc, "session-id", str);
1413 break;
1414
1415 case NC_RPC_COMMIT:
1416 rpc_com = (struct nc_rpc_commit *)rpc;
1417
1418 data = lyd_new(NULL, ietfnc, "commit");
1419 if (rpc_com->confirmed) {
1420 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1421 }
1422
1423 if (rpc_com->confirm_timeout) {
1424 sprintf(str, "%u", rpc_com->confirm_timeout);
1425 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1426 }
1427
1428 if (rpc_com->persist) {
1429 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1430 if (!node) {
1431 lyd_free(data);
1432 return NC_MSG_ERROR;
1433 }
1434 }
1435
1436 if (rpc_com->persist_id) {
1437 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1438 if (!node) {
1439 lyd_free(data);
1440 return NC_MSG_ERROR;
1441 }
1442 }
1443 break;
1444
1445 case NC_RPC_DISCARD:
1446 data = lyd_new(NULL, ietfnc, "discard-changes");
1447 break;
1448
1449 case NC_RPC_CANCEL:
1450 rpc_can = (struct nc_rpc_cancel *)rpc;
1451
1452 data = lyd_new(NULL, ietfnc, "cancel-commit");
1453 if (rpc_can->persist_id) {
1454 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1455 if (!node) {
1456 lyd_free(data);
1457 return NC_MSG_ERROR;
1458 }
1459 }
1460 break;
1461
1462 case NC_RPC_VALIDATE:
1463 rpc_val = (struct nc_rpc_validate *)rpc;
1464
1465 data = lyd_new(NULL, ietfnc, "validate");
1466 node = lyd_new(data, ietfnc, "source");
1467 if (rpc_val->url_config_src) {
1468 if (rpc_val->url_config_src[0] == '<') {
1469 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1470 } else {
1471 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1472 }
1473 } else {
1474 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1475 }
1476 if (!node) {
1477 lyd_free(data);
1478 return NC_MSG_ERROR;
1479 }
1480 break;
1481
1482 case NC_RPC_GETSCHEMA:
1483 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1484 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001485 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001486 return NC_MSG_ERROR;
1487 }
1488
1489 rpc_gs = (struct nc_rpc_getschema *)rpc;
1490
1491 data = lyd_new(NULL, ietfncmon, "get-schema");
1492 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1493 if (!node) {
1494 lyd_free(data);
1495 return NC_MSG_ERROR;
1496 }
1497 if (rpc_gs->version) {
1498 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1499 if (!node) {
1500 lyd_free(data);
1501 return NC_MSG_ERROR;
1502 }
1503 }
1504 if (rpc_gs->format) {
1505 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1506 if (!node) {
1507 lyd_free(data);
1508 return NC_MSG_ERROR;
1509 }
1510 }
1511 break;
1512
1513 case NC_RPC_SUBSCRIBE:
1514 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1515 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001516 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001517 return NC_MSG_ERROR;
1518 }
1519
1520 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1521
1522 data = lyd_new(NULL, notifs, "create-subscription");
1523 if (rpc_sub->stream) {
1524 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1525 if (!node) {
1526 lyd_free(data);
1527 return NC_MSG_ERROR;
1528 }
1529 }
1530
1531 if (rpc_sub->filter) {
1532 if (rpc_sub->filter[0] == '<') {
1533 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1534 lyd_insert_attr(node, "type", "subtree");
1535 } else {
1536 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1537 lyd_insert_attr(node, "type", "xpath");
1538 lyd_insert_attr(node, "select", rpc_sub->filter);
1539 }
1540 if (!node) {
1541 lyd_free(data);
1542 return NC_MSG_ERROR;
1543 }
1544 }
1545
1546 if (rpc_sub->start) {
1547 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1548 if (!node) {
1549 lyd_free(data);
1550 return NC_MSG_ERROR;
1551 }
1552 }
1553
1554 if (rpc_sub->stop) {
1555 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1556 if (!node) {
1557 lyd_free(data);
1558 return NC_MSG_ERROR;
1559 }
1560 }
1561 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001562 default:
1563 ERRINT;
1564 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001565 }
1566
1567 if (lyd_validate(data, LYD_OPT_STRICT)) {
1568 lyd_free(data);
1569 return NC_MSG_ERROR;
1570 }
1571
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001572 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1573 if (ret == -1) {
1574 /* error */
1575 r = NC_MSG_ERROR;
1576 } else if (!ret) {
1577 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001578 r = NC_MSG_WOULDBLOCK;
1579 } else {
1580 /* send RPC, store its message ID */
1581 r = nc_send_msg(session, data);
1582 cur_msgid = session->msgid;
1583 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001584 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001585
1586 lyd_free(data);
1587
1588 if (r != NC_MSG_RPC) {
1589 return r;
1590 }
1591
1592 *msgid = cur_msgid;
1593 return NC_MSG_RPC;
1594}