blob: e8ccd4de4487af095059713350fc5d59d1ce10d3 [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 Vasko3031aae2016-01-27 16:07:18 +0100844int
845nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
846{
847 int sock;
848
849 if (!address || !port) {
850 ERRARG;
851 return -1;
852 }
853
854 sock = nc_sock_listen(address, port);
855 if (sock == -1) {
856 return -1;
857 }
858
859 ++client_opts.ch_bind_count;
860 client_opts.ch_binds = realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
861
862 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
863 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
864 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
865 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
866
867 return 0;
868}
869
870int
871nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
872{
873 uint32_t i;
874 int ret = -1;
875
876 if (!address && !port && !ti) {
877 for (i = 0; i < client_opts.ch_bind_count; ++i) {
878 close(client_opts.ch_binds[i].sock);
879 free((char *)client_opts.ch_binds[i].address);
880
881 ret = 0;
882 }
883 free(client_opts.ch_binds);
884 client_opts.ch_binds = NULL;
885 client_opts.ch_bind_count = 0;
886 } else {
887 for (i = 0; i < client_opts.ch_bind_count; ++i) {
888 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
889 && (!port || (client_opts.ch_binds[i].port == port))
890 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
891 close(client_opts.ch_binds[i].sock);
892 free((char *)client_opts.ch_binds[i].address);
893
894 --client_opts.ch_bind_count;
895 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
896
897 ret = 0;
898 }
899 }
900 }
901
902 return ret;
903}
904
905API int
906nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
907{
908 int sock;
909 char *host = NULL;
910 uint16_t port, idx;
911
912 if (!client_opts.ch_binds || !session) {
913 ERRARG;
914 return -1;
915 }
916
917 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
918
919 if (sock < 1) {
920 return sock;
921 }
922
923 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100924 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx);
Michal Vasko3031aae2016-01-27 16:07:18 +0100925 } else if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100926 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx);
Michal Vasko3031aae2016-01-27 16:07:18 +0100927 } else {
928 *session = NULL;
929 }
930
931 free(host);
932
933 if (!(*session)) {
934 return -1;
935 }
936
937 return 1;
938}
939
Michal Vasko086311b2016-01-08 09:53:11 +0100940API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100941nc_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 +0100942{
943 struct lyxml_elem *xml;
944 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
945
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100946 if (!session || !rpc || !reply) {
947 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100948 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100949 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100950 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100951 return NC_MSG_ERROR;
952 }
953 *reply = NULL;
954
955 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100956
957 if (msgtype == NC_MSG_REPLY) {
958 *reply = parse_reply(session->ctx, xml, rpc);
959 lyxml_free(session->ctx, xml);
960 if (!(*reply)) {
961 return NC_MSG_ERROR;
962 }
963 }
964
965 return msgtype;
966}
967
968API NC_MSG_TYPE
969nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
970{
971 struct lyxml_elem *xml, *ev_time;
972 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
973
974 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100975 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100976 return NC_MSG_ERROR;
977 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100978 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100979 return NC_MSG_ERROR;
980 }
981
982 msgtype = get_msg(session, timeout, 0, &xml);
983
984 if (msgtype == NC_MSG_NOTIF) {
985 *notif = calloc(1, sizeof **notif);
986
987 /* eventTime */
988 LY_TREE_FOR(xml->child, ev_time) {
989 if (!strcmp(ev_time->name, "eventTime")) {
990 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
991 /* lyd_parse does not know this element */
992 lyxml_free(session->ctx, ev_time);
993 break;
994 }
995 }
996 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +0100997 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100998 goto fail;
999 }
1000
1001 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +01001002 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +01001003 lyxml_free(session->ctx, xml);
1004 xml = NULL;
1005 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001006 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001007 goto fail;
1008 }
1009 }
1010
1011 return msgtype;
1012
1013fail:
1014 lydict_remove(session->ctx, (*notif)->datetime);
1015 lyd_free((*notif)->tree);
1016 free(*notif);
1017 *notif = NULL;
1018 lyxml_free(session->ctx, xml);
1019
1020 return NC_MSG_ERROR;
1021}
1022
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001023static void *
1024nc_recv_notif_thread(void *arg)
1025{
1026 struct nc_ntf_thread_arg *ntarg;
1027 struct nc_session *session;
1028 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1029 struct nc_notif *notif;
1030 NC_MSG_TYPE msgtype;
1031
1032 ntarg = (struct nc_ntf_thread_arg *)arg;
1033 session = ntarg->session;
1034 notif_clb = ntarg->notif_clb;
1035 free(ntarg);
1036
1037 while (session->ntf_tid) {
1038 msgtype = nc_recv_notif(session, 0, &notif);
1039 if (msgtype == NC_MSG_NOTIF) {
1040 notif_clb(session, notif);
1041 nc_notif_free(notif);
1042 }
1043
1044 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1045 }
1046
1047 return NULL;
1048}
1049
1050API int
1051nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1052{
1053 struct nc_ntf_thread_arg *ntarg;
1054 int ret;
1055
1056 if (!session || !notif_clb) {
1057 ERRARG;
1058 return -1;
1059 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1060 ERR("Session %u: invalid session to receive Notifications.", session->id);
1061 return -1;
1062 } else if (session->ntf_tid) {
1063 ERR("Session %u: separate notification thread is already running.", session->id);
1064 return -1;
1065 }
1066
1067 ntarg = malloc(sizeof *ntarg);
1068 ntarg->session = session;
1069 ntarg->notif_clb = notif_clb;
1070
1071 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1072 session->ntf_tid = malloc(sizeof *session->ntf_tid);
1073
1074 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1075 if (ret) {
1076 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1077 free(ntarg);
1078 free((pthread_t *)session->ntf_tid);
1079 session->ntf_tid = NULL;
1080 return -1;
1081 }
1082
1083 return 0;
1084}
1085
Michal Vasko086311b2016-01-08 09:53:11 +01001086API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001087nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001088{
1089 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001090 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001091 struct nc_rpc_generic *rpc_gen;
1092 struct nc_rpc_getconfig *rpc_gc;
1093 struct nc_rpc_edit *rpc_e;
1094 struct nc_rpc_copy *rpc_cp;
1095 struct nc_rpc_delete *rpc_del;
1096 struct nc_rpc_lock *rpc_lock;
1097 struct nc_rpc_get *rpc_g;
1098 struct nc_rpc_kill *rpc_k;
1099 struct nc_rpc_commit *rpc_com;
1100 struct nc_rpc_cancel *rpc_can;
1101 struct nc_rpc_validate *rpc_val;
1102 struct nc_rpc_getschema *rpc_gs;
1103 struct nc_rpc_subscribe *rpc_sub;
1104 struct lyd_node *data, *node;
1105 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
1106 char str[11];
1107 uint64_t cur_msgid;
1108
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001109 if (!session || !rpc || !msgid) {
1110 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001111 return NC_MSG_ERROR;
1112 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001113 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001114 return NC_MSG_ERROR;
1115 }
1116
1117 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1118 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1119 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001120 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001121 return NC_MSG_ERROR;
1122 }
1123 }
1124
1125 switch (rpc->type) {
1126 case NC_RPC_GENERIC:
1127 rpc_gen = (struct nc_rpc_generic *)rpc;
1128
1129 if (rpc_gen->has_data) {
1130 data = rpc_gen->content.data;
1131 } else {
1132 data = lyd_parse_data(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
1133 }
1134 break;
1135
1136 case NC_RPC_GETCONFIG:
1137 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1138
1139 data = lyd_new(NULL, ietfnc, "get-config");
1140 node = lyd_new(data, ietfnc, "source");
1141 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1142 if (!node) {
1143 lyd_free(data);
1144 return NC_MSG_ERROR;
1145 }
1146 if (rpc_gc->filter) {
1147 if (rpc_gc->filter[0] == '<') {
1148 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1149 lyd_insert_attr(node, "type", "subtree");
1150 } else {
1151 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1152 lyd_insert_attr(node, "type", "xpath");
1153 lyd_insert_attr(node, "select", rpc_gc->filter);
1154 }
1155 if (!node) {
1156 lyd_free(data);
1157 return NC_MSG_ERROR;
1158 }
1159 }
1160
1161 if (rpc_gc->wd_mode) {
1162 if (!ietfncwd) {
1163 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1164 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001165 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001166 return NC_MSG_ERROR;
1167 }
1168 }
1169 switch (rpc_gc->wd_mode) {
1170 case NC_WD_UNKNOWN:
1171 /* cannot get here */
1172 break;
1173 case NC_WD_ALL:
1174 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1175 break;
1176 case NC_WD_ALL_TAG:
1177 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1178 break;
1179 case NC_WD_TRIM:
1180 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1181 break;
1182 case NC_WD_EXPLICIT:
1183 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1184 break;
1185 }
1186 if (!node) {
1187 lyd_free(data);
1188 return NC_MSG_ERROR;
1189 }
1190 }
1191 break;
1192
1193 case NC_RPC_EDIT:
1194 rpc_e = (struct nc_rpc_edit *)rpc;
1195
1196 data = lyd_new(NULL, ietfnc, "edit-config");
1197 node = lyd_new(data, ietfnc, "target");
1198 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1199 if (!node) {
1200 lyd_free(data);
1201 return NC_MSG_ERROR;
1202 }
1203
1204 if (rpc_e->default_op) {
1205 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1206 if (!node) {
1207 lyd_free(data);
1208 return NC_MSG_ERROR;
1209 }
1210 }
1211
1212 if (rpc_e->test_opt) {
1213 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1214 if (!node) {
1215 lyd_free(data);
1216 return NC_MSG_ERROR;
1217 }
1218 }
1219
1220 if (rpc_e->error_opt) {
1221 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1222 if (!node) {
1223 lyd_free(data);
1224 return NC_MSG_ERROR;
1225 }
1226 }
1227
1228 if (rpc_e->edit_cont[0] == '<') {
1229 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1230 } else {
1231 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1232 }
1233 if (!node) {
1234 lyd_free(data);
1235 return NC_MSG_ERROR;
1236 }
1237 break;
1238
1239 case NC_RPC_COPY:
1240 rpc_cp = (struct nc_rpc_copy *)rpc;
1241
1242 data = lyd_new(NULL, ietfnc, "copy-config");
1243 node = lyd_new(data, ietfnc, "target");
1244 if (rpc_cp->url_trg) {
1245 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1246 } else {
1247 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1248 }
1249 if (!node) {
1250 lyd_free(data);
1251 return NC_MSG_ERROR;
1252 }
1253
1254 node = lyd_new(data, ietfnc, "source");
1255 if (rpc_cp->url_config_src) {
1256 if (rpc_cp->url_config_src[0] == '<') {
1257 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1258 } else {
1259 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1260 }
1261 } else {
1262 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1263 }
1264 if (!node) {
1265 lyd_free(data);
1266 return NC_MSG_ERROR;
1267 }
1268
1269 if (rpc_cp->wd_mode) {
1270 if (!ietfncwd) {
1271 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1272 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001273 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001274 return NC_MSG_ERROR;
1275 }
1276 }
1277 switch (rpc_cp->wd_mode) {
1278 case NC_WD_UNKNOWN:
1279 /* cannot get here */
1280 break;
1281 case NC_WD_ALL:
1282 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1283 break;
1284 case NC_WD_ALL_TAG:
1285 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1286 break;
1287 case NC_WD_TRIM:
1288 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1289 break;
1290 case NC_WD_EXPLICIT:
1291 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1292 break;
1293 }
1294 if (!node) {
1295 lyd_free(data);
1296 return NC_MSG_ERROR;
1297 }
1298 }
1299 break;
1300
1301 case NC_RPC_DELETE:
1302 rpc_del = (struct nc_rpc_delete *)rpc;
1303
1304 data = lyd_new(NULL, ietfnc, "delete-config");
1305 node = lyd_new(data, ietfnc, "target");
1306 if (rpc_del->url) {
1307 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1308 } else {
1309 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1310 }
1311 if (!node) {
1312 lyd_free(data);
1313 return NC_MSG_ERROR;
1314 }
1315 break;
1316
1317 case NC_RPC_LOCK:
1318 rpc_lock = (struct nc_rpc_lock *)rpc;
1319
1320 data = lyd_new(NULL, ietfnc, "lock");
1321 node = lyd_new(data, ietfnc, "target");
1322 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1323 if (!node) {
1324 lyd_free(data);
1325 return NC_MSG_ERROR;
1326 }
1327 break;
1328
1329 case NC_RPC_UNLOCK:
1330 rpc_lock = (struct nc_rpc_lock *)rpc;
1331
1332 data = lyd_new(NULL, ietfnc, "unlock");
1333 node = lyd_new(data, ietfnc, "target");
1334 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1335 if (!node) {
1336 lyd_free(data);
1337 return NC_MSG_ERROR;
1338 }
1339 break;
1340
1341 case NC_RPC_GET:
1342 rpc_g = (struct nc_rpc_get *)rpc;
1343
1344 data = lyd_new(NULL, ietfnc, "get");
1345 if (rpc_g->filter) {
1346 if (rpc_g->filter[0] == '<') {
1347 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1348 lyd_insert_attr(node, "type", "subtree");
1349 } else {
1350 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1351 lyd_insert_attr(node, "type", "xpath");
1352 lyd_insert_attr(node, "select", rpc_g->filter);
1353 }
1354 if (!node) {
1355 lyd_free(data);
1356 return NC_MSG_ERROR;
1357 }
1358 }
1359
1360 if (rpc_g->wd_mode) {
1361 if (!ietfncwd) {
1362 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1363 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001364 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001365 return NC_MSG_ERROR;
1366 }
1367 }
1368 switch (rpc_g->wd_mode) {
1369 case NC_WD_UNKNOWN:
1370 /* cannot get here */
1371 break;
1372 case NC_WD_ALL:
1373 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1374 break;
1375 case NC_WD_ALL_TAG:
1376 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1377 break;
1378 case NC_WD_TRIM:
1379 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1380 break;
1381 case NC_WD_EXPLICIT:
1382 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1383 break;
1384 }
1385 if (!node) {
1386 lyd_free(data);
1387 return NC_MSG_ERROR;
1388 }
1389 }
1390 break;
1391
1392 case NC_RPC_KILL:
1393 rpc_k = (struct nc_rpc_kill *)rpc;
1394
1395 data = lyd_new(NULL, ietfnc, "kill-session");
1396 sprintf(str, "%u", rpc_k->sid);
1397 lyd_new_leaf(data, ietfnc, "session-id", str);
1398 break;
1399
1400 case NC_RPC_COMMIT:
1401 rpc_com = (struct nc_rpc_commit *)rpc;
1402
1403 data = lyd_new(NULL, ietfnc, "commit");
1404 if (rpc_com->confirmed) {
1405 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1406 }
1407
1408 if (rpc_com->confirm_timeout) {
1409 sprintf(str, "%u", rpc_com->confirm_timeout);
1410 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1411 }
1412
1413 if (rpc_com->persist) {
1414 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1415 if (!node) {
1416 lyd_free(data);
1417 return NC_MSG_ERROR;
1418 }
1419 }
1420
1421 if (rpc_com->persist_id) {
1422 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1423 if (!node) {
1424 lyd_free(data);
1425 return NC_MSG_ERROR;
1426 }
1427 }
1428 break;
1429
1430 case NC_RPC_DISCARD:
1431 data = lyd_new(NULL, ietfnc, "discard-changes");
1432 break;
1433
1434 case NC_RPC_CANCEL:
1435 rpc_can = (struct nc_rpc_cancel *)rpc;
1436
1437 data = lyd_new(NULL, ietfnc, "cancel-commit");
1438 if (rpc_can->persist_id) {
1439 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1440 if (!node) {
1441 lyd_free(data);
1442 return NC_MSG_ERROR;
1443 }
1444 }
1445 break;
1446
1447 case NC_RPC_VALIDATE:
1448 rpc_val = (struct nc_rpc_validate *)rpc;
1449
1450 data = lyd_new(NULL, ietfnc, "validate");
1451 node = lyd_new(data, ietfnc, "source");
1452 if (rpc_val->url_config_src) {
1453 if (rpc_val->url_config_src[0] == '<') {
1454 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1455 } else {
1456 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1457 }
1458 } else {
1459 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1460 }
1461 if (!node) {
1462 lyd_free(data);
1463 return NC_MSG_ERROR;
1464 }
1465 break;
1466
1467 case NC_RPC_GETSCHEMA:
1468 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1469 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001470 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001471 return NC_MSG_ERROR;
1472 }
1473
1474 rpc_gs = (struct nc_rpc_getschema *)rpc;
1475
1476 data = lyd_new(NULL, ietfncmon, "get-schema");
1477 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1478 if (!node) {
1479 lyd_free(data);
1480 return NC_MSG_ERROR;
1481 }
1482 if (rpc_gs->version) {
1483 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1484 if (!node) {
1485 lyd_free(data);
1486 return NC_MSG_ERROR;
1487 }
1488 }
1489 if (rpc_gs->format) {
1490 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1491 if (!node) {
1492 lyd_free(data);
1493 return NC_MSG_ERROR;
1494 }
1495 }
1496 break;
1497
1498 case NC_RPC_SUBSCRIBE:
1499 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1500 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001501 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001502 return NC_MSG_ERROR;
1503 }
1504
1505 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1506
1507 data = lyd_new(NULL, notifs, "create-subscription");
1508 if (rpc_sub->stream) {
1509 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1510 if (!node) {
1511 lyd_free(data);
1512 return NC_MSG_ERROR;
1513 }
1514 }
1515
1516 if (rpc_sub->filter) {
1517 if (rpc_sub->filter[0] == '<') {
1518 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1519 lyd_insert_attr(node, "type", "subtree");
1520 } else {
1521 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1522 lyd_insert_attr(node, "type", "xpath");
1523 lyd_insert_attr(node, "select", rpc_sub->filter);
1524 }
1525 if (!node) {
1526 lyd_free(data);
1527 return NC_MSG_ERROR;
1528 }
1529 }
1530
1531 if (rpc_sub->start) {
1532 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1533 if (!node) {
1534 lyd_free(data);
1535 return NC_MSG_ERROR;
1536 }
1537 }
1538
1539 if (rpc_sub->stop) {
1540 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1541 if (!node) {
1542 lyd_free(data);
1543 return NC_MSG_ERROR;
1544 }
1545 }
1546 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001547 default:
1548 ERRINT;
1549 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001550 }
1551
1552 if (lyd_validate(data, LYD_OPT_STRICT)) {
1553 lyd_free(data);
1554 return NC_MSG_ERROR;
1555 }
1556
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001557 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1558 if (ret == -1) {
1559 /* error */
1560 r = NC_MSG_ERROR;
1561 } else if (!ret) {
1562 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001563 r = NC_MSG_WOULDBLOCK;
1564 } else {
1565 /* send RPC, store its message ID */
1566 r = nc_send_msg(session, data);
1567 cur_msgid = session->msgid;
1568 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001569 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001570
1571 lyd_free(data);
1572
1573 if (r != NC_MSG_RPC) {
1574 return r;
1575 }
1576
1577 *msgid = cur_msgid;
1578 return NC_MSG_RPC;
1579}