blob: 206d7b6c3fc2f911ad3c71751c0b0228d94521f2 [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);
1051 nc_notif_free(notif);
1052 }
1053
1054 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1055 }
1056
1057 return NULL;
1058}
1059
1060API int
1061nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1062{
1063 struct nc_ntf_thread_arg *ntarg;
1064 int ret;
1065
1066 if (!session || !notif_clb) {
1067 ERRARG;
1068 return -1;
1069 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1070 ERR("Session %u: invalid session to receive Notifications.", session->id);
1071 return -1;
1072 } else if (session->ntf_tid) {
1073 ERR("Session %u: separate notification thread is already running.", session->id);
1074 return -1;
1075 }
1076
1077 ntarg = malloc(sizeof *ntarg);
1078 ntarg->session = session;
1079 ntarg->notif_clb = notif_clb;
1080
1081 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1082 session->ntf_tid = malloc(sizeof *session->ntf_tid);
1083
1084 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1085 if (ret) {
1086 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1087 free(ntarg);
1088 free((pthread_t *)session->ntf_tid);
1089 session->ntf_tid = NULL;
1090 return -1;
1091 }
1092
1093 return 0;
1094}
1095
Michal Vasko086311b2016-01-08 09:53:11 +01001096API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001097nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001098{
1099 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001100 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001101 struct nc_rpc_generic *rpc_gen;
1102 struct nc_rpc_getconfig *rpc_gc;
1103 struct nc_rpc_edit *rpc_e;
1104 struct nc_rpc_copy *rpc_cp;
1105 struct nc_rpc_delete *rpc_del;
1106 struct nc_rpc_lock *rpc_lock;
1107 struct nc_rpc_get *rpc_g;
1108 struct nc_rpc_kill *rpc_k;
1109 struct nc_rpc_commit *rpc_com;
1110 struct nc_rpc_cancel *rpc_can;
1111 struct nc_rpc_validate *rpc_val;
1112 struct nc_rpc_getschema *rpc_gs;
1113 struct nc_rpc_subscribe *rpc_sub;
1114 struct lyd_node *data, *node;
1115 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
1116 char str[11];
1117 uint64_t cur_msgid;
1118
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001119 if (!session || !rpc || !msgid) {
1120 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +01001121 return NC_MSG_ERROR;
1122 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001123 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001124 return NC_MSG_ERROR;
1125 }
1126
1127 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
1128 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1129 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001130 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001131 return NC_MSG_ERROR;
1132 }
1133 }
1134
1135 switch (rpc->type) {
1136 case NC_RPC_GENERIC:
1137 rpc_gen = (struct nc_rpc_generic *)rpc;
1138
1139 if (rpc_gen->has_data) {
1140 data = rpc_gen->content.data;
1141 } else {
1142 data = lyd_parse_data(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
1143 }
1144 break;
1145
1146 case NC_RPC_GETCONFIG:
1147 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1148
1149 data = lyd_new(NULL, ietfnc, "get-config");
1150 node = lyd_new(data, ietfnc, "source");
1151 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1152 if (!node) {
1153 lyd_free(data);
1154 return NC_MSG_ERROR;
1155 }
1156 if (rpc_gc->filter) {
1157 if (rpc_gc->filter[0] == '<') {
1158 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
1159 lyd_insert_attr(node, "type", "subtree");
1160 } else {
1161 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1162 lyd_insert_attr(node, "type", "xpath");
1163 lyd_insert_attr(node, "select", rpc_gc->filter);
1164 }
1165 if (!node) {
1166 lyd_free(data);
1167 return NC_MSG_ERROR;
1168 }
1169 }
1170
1171 if (rpc_gc->wd_mode) {
1172 if (!ietfncwd) {
1173 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1174 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001175 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001176 return NC_MSG_ERROR;
1177 }
1178 }
1179 switch (rpc_gc->wd_mode) {
1180 case NC_WD_UNKNOWN:
1181 /* cannot get here */
1182 break;
1183 case NC_WD_ALL:
1184 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1185 break;
1186 case NC_WD_ALL_TAG:
1187 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1188 break;
1189 case NC_WD_TRIM:
1190 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1191 break;
1192 case NC_WD_EXPLICIT:
1193 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1194 break;
1195 }
1196 if (!node) {
1197 lyd_free(data);
1198 return NC_MSG_ERROR;
1199 }
1200 }
1201 break;
1202
1203 case NC_RPC_EDIT:
1204 rpc_e = (struct nc_rpc_edit *)rpc;
1205
1206 data = lyd_new(NULL, ietfnc, "edit-config");
1207 node = lyd_new(data, ietfnc, "target");
1208 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1209 if (!node) {
1210 lyd_free(data);
1211 return NC_MSG_ERROR;
1212 }
1213
1214 if (rpc_e->default_op) {
1215 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1216 if (!node) {
1217 lyd_free(data);
1218 return NC_MSG_ERROR;
1219 }
1220 }
1221
1222 if (rpc_e->test_opt) {
1223 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1224 if (!node) {
1225 lyd_free(data);
1226 return NC_MSG_ERROR;
1227 }
1228 }
1229
1230 if (rpc_e->error_opt) {
1231 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1232 if (!node) {
1233 lyd_free(data);
1234 return NC_MSG_ERROR;
1235 }
1236 }
1237
1238 if (rpc_e->edit_cont[0] == '<') {
1239 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1240 } else {
1241 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1242 }
1243 if (!node) {
1244 lyd_free(data);
1245 return NC_MSG_ERROR;
1246 }
1247 break;
1248
1249 case NC_RPC_COPY:
1250 rpc_cp = (struct nc_rpc_copy *)rpc;
1251
1252 data = lyd_new(NULL, ietfnc, "copy-config");
1253 node = lyd_new(data, ietfnc, "target");
1254 if (rpc_cp->url_trg) {
1255 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1256 } else {
1257 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1258 }
1259 if (!node) {
1260 lyd_free(data);
1261 return NC_MSG_ERROR;
1262 }
1263
1264 node = lyd_new(data, ietfnc, "source");
1265 if (rpc_cp->url_config_src) {
1266 if (rpc_cp->url_config_src[0] == '<') {
1267 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1268 } else {
1269 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1270 }
1271 } else {
1272 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1273 }
1274 if (!node) {
1275 lyd_free(data);
1276 return NC_MSG_ERROR;
1277 }
1278
1279 if (rpc_cp->wd_mode) {
1280 if (!ietfncwd) {
1281 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1282 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001283 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001284 return NC_MSG_ERROR;
1285 }
1286 }
1287 switch (rpc_cp->wd_mode) {
1288 case NC_WD_UNKNOWN:
1289 /* cannot get here */
1290 break;
1291 case NC_WD_ALL:
1292 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1293 break;
1294 case NC_WD_ALL_TAG:
1295 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1296 break;
1297 case NC_WD_TRIM:
1298 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1299 break;
1300 case NC_WD_EXPLICIT:
1301 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1302 break;
1303 }
1304 if (!node) {
1305 lyd_free(data);
1306 return NC_MSG_ERROR;
1307 }
1308 }
1309 break;
1310
1311 case NC_RPC_DELETE:
1312 rpc_del = (struct nc_rpc_delete *)rpc;
1313
1314 data = lyd_new(NULL, ietfnc, "delete-config");
1315 node = lyd_new(data, ietfnc, "target");
1316 if (rpc_del->url) {
1317 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1318 } else {
1319 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1320 }
1321 if (!node) {
1322 lyd_free(data);
1323 return NC_MSG_ERROR;
1324 }
1325 break;
1326
1327 case NC_RPC_LOCK:
1328 rpc_lock = (struct nc_rpc_lock *)rpc;
1329
1330 data = lyd_new(NULL, ietfnc, "lock");
1331 node = lyd_new(data, ietfnc, "target");
1332 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1333 if (!node) {
1334 lyd_free(data);
1335 return NC_MSG_ERROR;
1336 }
1337 break;
1338
1339 case NC_RPC_UNLOCK:
1340 rpc_lock = (struct nc_rpc_lock *)rpc;
1341
1342 data = lyd_new(NULL, ietfnc, "unlock");
1343 node = lyd_new(data, ietfnc, "target");
1344 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1345 if (!node) {
1346 lyd_free(data);
1347 return NC_MSG_ERROR;
1348 }
1349 break;
1350
1351 case NC_RPC_GET:
1352 rpc_g = (struct nc_rpc_get *)rpc;
1353
1354 data = lyd_new(NULL, ietfnc, "get");
1355 if (rpc_g->filter) {
1356 if (rpc_g->filter[0] == '<') {
1357 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1358 lyd_insert_attr(node, "type", "subtree");
1359 } else {
1360 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1361 lyd_insert_attr(node, "type", "xpath");
1362 lyd_insert_attr(node, "select", rpc_g->filter);
1363 }
1364 if (!node) {
1365 lyd_free(data);
1366 return NC_MSG_ERROR;
1367 }
1368 }
1369
1370 if (rpc_g->wd_mode) {
1371 if (!ietfncwd) {
1372 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1373 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001374 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001375 return NC_MSG_ERROR;
1376 }
1377 }
1378 switch (rpc_g->wd_mode) {
1379 case NC_WD_UNKNOWN:
1380 /* cannot get here */
1381 break;
1382 case NC_WD_ALL:
1383 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1384 break;
1385 case NC_WD_ALL_TAG:
1386 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1387 break;
1388 case NC_WD_TRIM:
1389 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1390 break;
1391 case NC_WD_EXPLICIT:
1392 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1393 break;
1394 }
1395 if (!node) {
1396 lyd_free(data);
1397 return NC_MSG_ERROR;
1398 }
1399 }
1400 break;
1401
1402 case NC_RPC_KILL:
1403 rpc_k = (struct nc_rpc_kill *)rpc;
1404
1405 data = lyd_new(NULL, ietfnc, "kill-session");
1406 sprintf(str, "%u", rpc_k->sid);
1407 lyd_new_leaf(data, ietfnc, "session-id", str);
1408 break;
1409
1410 case NC_RPC_COMMIT:
1411 rpc_com = (struct nc_rpc_commit *)rpc;
1412
1413 data = lyd_new(NULL, ietfnc, "commit");
1414 if (rpc_com->confirmed) {
1415 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1416 }
1417
1418 if (rpc_com->confirm_timeout) {
1419 sprintf(str, "%u", rpc_com->confirm_timeout);
1420 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1421 }
1422
1423 if (rpc_com->persist) {
1424 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1425 if (!node) {
1426 lyd_free(data);
1427 return NC_MSG_ERROR;
1428 }
1429 }
1430
1431 if (rpc_com->persist_id) {
1432 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1433 if (!node) {
1434 lyd_free(data);
1435 return NC_MSG_ERROR;
1436 }
1437 }
1438 break;
1439
1440 case NC_RPC_DISCARD:
1441 data = lyd_new(NULL, ietfnc, "discard-changes");
1442 break;
1443
1444 case NC_RPC_CANCEL:
1445 rpc_can = (struct nc_rpc_cancel *)rpc;
1446
1447 data = lyd_new(NULL, ietfnc, "cancel-commit");
1448 if (rpc_can->persist_id) {
1449 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1450 if (!node) {
1451 lyd_free(data);
1452 return NC_MSG_ERROR;
1453 }
1454 }
1455 break;
1456
1457 case NC_RPC_VALIDATE:
1458 rpc_val = (struct nc_rpc_validate *)rpc;
1459
1460 data = lyd_new(NULL, ietfnc, "validate");
1461 node = lyd_new(data, ietfnc, "source");
1462 if (rpc_val->url_config_src) {
1463 if (rpc_val->url_config_src[0] == '<') {
1464 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1465 } else {
1466 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1467 }
1468 } else {
1469 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1470 }
1471 if (!node) {
1472 lyd_free(data);
1473 return NC_MSG_ERROR;
1474 }
1475 break;
1476
1477 case NC_RPC_GETSCHEMA:
1478 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1479 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001480 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001481 return NC_MSG_ERROR;
1482 }
1483
1484 rpc_gs = (struct nc_rpc_getschema *)rpc;
1485
1486 data = lyd_new(NULL, ietfncmon, "get-schema");
1487 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1488 if (!node) {
1489 lyd_free(data);
1490 return NC_MSG_ERROR;
1491 }
1492 if (rpc_gs->version) {
1493 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1494 if (!node) {
1495 lyd_free(data);
1496 return NC_MSG_ERROR;
1497 }
1498 }
1499 if (rpc_gs->format) {
1500 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1501 if (!node) {
1502 lyd_free(data);
1503 return NC_MSG_ERROR;
1504 }
1505 }
1506 break;
1507
1508 case NC_RPC_SUBSCRIBE:
1509 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1510 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001511 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001512 return NC_MSG_ERROR;
1513 }
1514
1515 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1516
1517 data = lyd_new(NULL, notifs, "create-subscription");
1518 if (rpc_sub->stream) {
1519 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1520 if (!node) {
1521 lyd_free(data);
1522 return NC_MSG_ERROR;
1523 }
1524 }
1525
1526 if (rpc_sub->filter) {
1527 if (rpc_sub->filter[0] == '<') {
1528 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1529 lyd_insert_attr(node, "type", "subtree");
1530 } else {
1531 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1532 lyd_insert_attr(node, "type", "xpath");
1533 lyd_insert_attr(node, "select", rpc_sub->filter);
1534 }
1535 if (!node) {
1536 lyd_free(data);
1537 return NC_MSG_ERROR;
1538 }
1539 }
1540
1541 if (rpc_sub->start) {
1542 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1543 if (!node) {
1544 lyd_free(data);
1545 return NC_MSG_ERROR;
1546 }
1547 }
1548
1549 if (rpc_sub->stop) {
1550 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1551 if (!node) {
1552 lyd_free(data);
1553 return NC_MSG_ERROR;
1554 }
1555 }
1556 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001557 default:
1558 ERRINT;
1559 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001560 }
1561
1562 if (lyd_validate(data, LYD_OPT_STRICT)) {
1563 lyd_free(data);
1564 return NC_MSG_ERROR;
1565 }
1566
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001567 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1568 if (ret == -1) {
1569 /* error */
1570 r = NC_MSG_ERROR;
1571 } else if (!ret) {
1572 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001573 r = NC_MSG_WOULDBLOCK;
1574 } else {
1575 /* send RPC, store its message ID */
1576 r = nc_send_msg(session, data);
1577 cur_msgid = session->msgid;
1578 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001579 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001580
1581 lyd_free(data);
1582
1583 if (r != NC_MSG_RPC) {
1584 return r;
1585 }
1586
1587 *msgid = cur_msgid;
1588 return NC_MSG_RPC;
1589}