blob: 4818ee3d92f16104f0207262dfc39704868ae08c [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 Vasko086311b2016-01-08 09:53:11 +010041
Michal Vasko80ef5d22016-01-18 09:21:02 +010042static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
43
Michal Vasko1a38c862016-01-15 15:50:07 +010044static char *schema_searchpath;
Michal Vasko086311b2016-01-08 09:53:11 +010045
46API int
47nc_schema_searchpath(const char *path)
48{
49 if (schema_searchpath) {
50 free(schema_searchpath);
51 }
Michal Vasko086311b2016-01-08 09:53:11 +010052
Michal Vasko7f1c78b2016-01-19 09:52:14 +010053 if (path) {
54 schema_searchpath = strdup(path);
55 if (!schema_searchpath) {
56 ERRMEM;
57 return 1;
58 }
59 } else {
60 schema_searchpath = NULL;
61 }
62
63 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010064}
65
Michal Vasko086311b2016-01-08 09:53:11 +010066/* SCHEMAS_DIR not used */
67static int
68ctx_check_and_load_model(struct nc_session *session, const char *cpblt)
69{
70 const struct lys_module *module;
71 char *ptr, *ptr2;
72 char *model_name, *revision = NULL, *features = NULL;
73
74 /* parse module */
75 ptr = strstr(cpblt, "module=");
76 if (!ptr) {
77 WRN("Unknown capability \"%s\" could not be parsed.", cpblt);
78 return 1;
79 }
80 ptr += 7;
81 ptr2 = strchr(ptr, '&');
82 if (!ptr2) {
83 ptr2 = ptr + strlen(ptr);
84 }
85 model_name = strndup(ptr, ptr2 - ptr);
86
87 /* parse revision */
88 ptr = strstr(cpblt, "revision=");
89 if (ptr) {
90 ptr += 9;
91 ptr2 = strchr(ptr, '&');
92 if (!ptr2) {
93 ptr2 = ptr + strlen(ptr);
94 }
95 revision = strndup(ptr, ptr2 - ptr);
96 }
97
98 /* load module if needed */
99 module = ly_ctx_get_module(session->ctx, model_name, revision);
100 if (!module) {
101 module = ly_ctx_load_module(session->ctx, model_name, revision);
102 }
103
104 free(model_name);
105 free(revision);
106 if (!module) {
107 return 1;
108 }
109
110 /* parse features */
111 ptr = strstr(cpblt, "features=");
112 if (ptr) {
113 ptr += 9;
114 ptr2 = strchr(ptr, '&');
115 if (!ptr2) {
116 ptr2 = ptr + strlen(ptr);
117 }
118 features = strndup(ptr, ptr2 - ptr);
119 }
120
121 /* enable features */
122 if (features) {
123 /* basically manual strtok_r (to avoid macro) */
124 ptr2 = features;
125 for (ptr = features; *ptr; ++ptr) {
126 if (*ptr == ',') {
127 *ptr = '\0';
128 /* remember last feature */
129 ptr2 = ptr + 1;
130 }
131 }
132
133 ptr = features;
134 lys_features_enable(module, ptr);
135 while (ptr != ptr2) {
136 ptr += strlen(ptr) + 1;
137 lys_features_enable(module, ptr);
138 }
139
140 free(features);
141 }
142
143 return 0;
144}
145
146/* SCHEMAS_DIR used */
147static int
148ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts, int from_file)
149{
150 int i;
151 const struct lys_module *ietfnc;
152
153 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
154 if (!ietfnc) {
155 if (from_file) {
156 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
157 } else {
158 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
159 }
160 }
161 if (!ietfnc) {
162 ERR("Loading base NETCONF schema failed.");
163 return 1;
164 }
165
166 /* set supported capabilities from ietf-netconf */
167 for (i = 0; cpblts[i]; ++i) {
168 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
169 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
170 lys_features_enable(ietfnc, "writable-running");
171 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
172 lys_features_enable(ietfnc, "candidate");
173 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
174 lys_features_enable(ietfnc, "confirmed-commit");
175 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
176 lys_features_enable(ietfnc, "rollback-on-error");
177 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
178 lys_features_enable(ietfnc, "validate");
179 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
180 lys_features_enable(ietfnc, "startup");
181 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
182 lys_features_enable(ietfnc, "url");
183 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
184 lys_features_enable(ietfnc, "xpath");
185 }
186 }
187 }
188
189 return 0;
190}
191
192static char *
193libyang_module_clb(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
194 void (**free_model_data)(char *model_data))
195{
196 struct nc_session *session = (struct nc_session *)user_data;
197 struct nc_rpc *rpc;
198 struct nc_reply *reply;
199 struct nc_reply_data *data_rpl;
200 NC_MSG_TYPE msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100201 char *model_data = NULL, *ptr, *ptr2, *anyxml;
Michal Vasko086311b2016-01-08 09:53:11 +0100202 uint64_t msgid;
203
204 /* TODO later replace with yang to reduce model size? */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100205 rpc = nc_rpc_getschema(name, revision, "yin", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100206 *format = LYS_IN_YIN;
207
208 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
209 usleep(1000);
210 }
211 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100212 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100213 nc_rpc_free(rpc);
214 return NULL;
215 }
216
217 msg = nc_recv_reply(session, rpc, msgid, 250, &reply);
218 nc_rpc_free(rpc);
219 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100220 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100221 return NULL;
222 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100223 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100224 return NULL;
225 }
226
Michal Vasko05ba9df2016-01-13 14:40:27 +0100227 if (reply->type != NC_RPL_DATA) {
228 /* TODO print the error, if error */
Michal Vaskod083db62016-01-19 10:31:29 +0100229 ERR("Session %u: unexpected reply type to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100230 nc_reply_free(reply);
231 return NULL;
232 }
233
Michal Vasko086311b2016-01-08 09:53:11 +0100234 data_rpl = (struct nc_reply_data *)reply;
235 anyxml = lyxml_serialize(((struct lyd_node_anyxml *)data_rpl->data)->value);
236 nc_reply_free(reply);
237 *free_model_data = NULL;
238
239 /* it's with the data root node, remove it */
240 if (anyxml) {
241 ptr = strchr(anyxml, '>');
242 ++ptr;
243
244 ptr2 = strrchr(anyxml, '<');
245
246 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
247 free(anyxml);
248 }
249
250 return model_data;
251}
252
253int
254nc_ctx_check_and_fill(struct nc_session *session)
255{
256 int i, get_schema_support = 0;
257 ly_module_clb old_clb = NULL;
258 void *old_data = NULL;
259
260 assert(session->cpblts && session->ctx);
261
262 /* check if get-schema is supported */
263 for (i = 0; session->cpblts[i]; ++i) {
264 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
265 get_schema_support = 1;
266 break;
267 }
268 }
269
270 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
271 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
272 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
273 /* set module retrieval using <get-schema> */
274 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
275 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
276 } else {
277 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
278 }
279 }
280
281 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
282 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts, !get_schema_support)) {
283 if (old_clb) {
284 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
285 }
286 return 1;
287 }
288
289 /* load all other models */
290 for (i = 0; session->cpblts[i]; ++i) {
291 if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34)
292 || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) {
293 continue;
294 }
295
296 ctx_check_and_load_model(session, session->cpblts[i]);
297 }
298
299 if (old_clb) {
300 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
301 }
302 return 0;
303}
304
305API struct nc_session *
306nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
307{
Michal Vaskod083db62016-01-19 10:31:29 +0100308 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100309
Michal Vaskod083db62016-01-19 10:31:29 +0100310 if ((fdin < 0) || (fdout < 0)) {
311 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100312 return NULL;
313 }
314
315 /* prepare session structure */
316 session = calloc(1, sizeof *session);
317 if (!session) {
318 ERRMEM;
319 return NULL;
320 }
321 session->status = NC_STATUS_STARTING;
322 session->side = NC_CLIENT;
323
324 /* transport specific data */
325 session->ti_type = NC_TI_FD;
326 session->ti.fd.in = fdin;
327 session->ti.fd.out = fdout;
328
329 /* assign context (dicionary needed for handshake) */
330 if (!ctx) {
331 ctx = ly_ctx_new(SCHEMAS_DIR);
332 } else {
333 session->flags |= NC_SESSION_SHAREDCTX;
334 }
335 session->ctx = ctx;
336
337 /* NETCONF handshake */
338 if (nc_handshake(session)) {
339 goto fail;
340 }
341 session->status = NC_STATUS_RUNNING;
342
343 if (nc_ctx_check_and_fill(session)) {
344 goto fail;
345 }
346
347 return session;
348
349fail:
350 nc_session_free(session);
351 return NULL;
352}
353
354int
355nc_connect_getsocket(const char* host, uint16_t port)
356{
357 int i, sock = -1;
358 struct addrinfo hints, *res_list, *res;
359 char port_s[6]; /* length of string representation of short int */
360
361 snprintf(port_s, 6, "%u", port);
362
363 /* Connect to a server */
364 memset(&hints, 0, sizeof hints);
365 hints.ai_family = AF_UNSPEC;
366 hints.ai_socktype = SOCK_STREAM;
367 hints.ai_protocol = IPPROTO_TCP;
368 i = getaddrinfo(host, port_s, &hints, &res_list);
369 if (i != 0) {
370 ERR("Unable to translate the host address (%s).", gai_strerror(i));
371 return -1;
372 }
373
374 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
375 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
376 if (sock == -1) {
377 /* socket was not created, try another resource */
378 i = errno;
379 goto errloop;
380 }
381
382 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
383 /* network connection failed, try another resource */
384 i = errno;
385 close(sock);
386 sock = -1;
387 goto errloop;
388 }
389
390 /* we're done, network connection established */
391 break;
392errloop:
393 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
394 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
395 continue;
396 }
397
398 if (sock == -1) {
399 ERR("Unable to connect to %s:%s.", host, port_s);
400 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100401 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 +0100402 }
403 freeaddrinfo(res_list);
404
405 return sock;
406}
407
Michal Vasko086311b2016-01-08 09:53:11 +0100408static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100409get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100410{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100411 int r, elapsed;
Michal Vasko086311b2016-01-08 09:53:11 +0100412 char *ptr;
413 const char *str_msgid;
414 uint64_t cur_msgid;
415 struct lyxml_elem *xml;
416 struct nc_msg_cont *cont, *prev_cont, **cont_ptr;
417 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
418
419next_message:
420 if (msgtype) {
421 /* second run, wait and give a chance to nc_recv_reply() */
422 usleep(NC_TIMEOUT_STEP);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100423 timeout -= NC_TIMEOUT_STEP;
Michal Vasko086311b2016-01-08 09:53:11 +0100424 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100425 elapsed = 0;
426 r = nc_timedlock(session->ti_lock, timeout, &elapsed);
427 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100428 /* error */
429 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100430 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100431 /* timeout */
432 return NC_MSG_WOULDBLOCK;
433 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100434 if (timeout > 0) {
435 timeout -= elapsed;
436 }
Michal Vasko086311b2016-01-08 09:53:11 +0100437
438 /* try to get notification from the session's queue */
439 if (!msgid && session->notifs) {
440 cont = session->notifs;
441 session->notifs = cont->next;
442
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100443 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100444
445 *msg = cont->msg;
446 free(cont);
447
448 return NC_MSG_NOTIF;
449 }
450
451 /* try to get rpc-reply from the session's queue */
452 if (msgid && session->replies) {
453 prev_cont = NULL;
454 for (cont = session->replies; cont; cont = cont->next) {
455 /* errors checked in the condition below */
456 str_msgid = lyxml_get_attr(cont->msg, "message-id", NULL);
457 cur_msgid = strtoul(str_msgid, &ptr, 10);
458
459 if (cur_msgid == msgid) {
460 if (!prev_cont) {
461 session->replies = cont->next;
462 } else {
463 prev_cont->next = cont->next;
464 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100465 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100466
467 *msg = cont->msg;
468 free(cont);
469
470 return NC_MSG_REPLY;
471 }
472
473 prev_cont = cont;
474 }
475 }
476
477 /* read message from wire */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100478 msgtype = nc_read_msg_poll(session, timeout, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +0100479
480 /* we read rpc-reply, want a notif */
481 if (!msgid && (msgtype == NC_MSG_REPLY)) {
482 /* just check that message-id is fine */
483 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
484 if (!str_msgid) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100485 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100486 ERR("Session %u: received a <rpc-reply> with no message-id, discarding.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100487 lyxml_free(session->ctx, xml);
488 goto next_message;
489 }
490 cur_msgid = strtoul(str_msgid, &ptr, 10);
491 if (ptr[0]) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100492 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100493 ERR("Session %u: received a <rpc-reply> with an invalid message-id (\"%s\"), discarding.",
494 session->id, str_msgid);
Michal Vasko086311b2016-01-08 09:53:11 +0100495 lyxml_free(session->ctx, xml);
496 goto next_message;
497 }
498
499 cont_ptr = &session->replies;
500 while (*cont_ptr) {
501 cont_ptr = &((*cont_ptr)->next);
502 }
503 *cont_ptr = malloc(sizeof **cont_ptr);
504 (*cont_ptr)->msg = xml;
505 (*cont_ptr)->next = NULL;
506 }
507
508 /* we read notif, want a rpc-reply */
509 if (msgid && (msgtype == NC_MSG_NOTIF)) {
510 if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100511 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100512 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100513 lyxml_free(session->ctx, xml);
514 goto next_message;
515 }
516
517 cont_ptr = &session->notifs;
518 while (*cont_ptr) {
519 cont_ptr = &((*cont_ptr)->next);
520 }
521 *cont_ptr = malloc(sizeof **cont_ptr);
522 (*cont_ptr)->msg = xml;
523 (*cont_ptr)->next = NULL;
524 }
525
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100526 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100527
528 switch (msgtype) {
529 case NC_MSG_NOTIF:
530 /* we want a rpc-reply */
531 if (msgid) {
532 goto next_message;
533 }
534 *msg = xml;
535 break;
536
537 case NC_MSG_REPLY:
538 /* we want a notif */
539 if (!msgid) {
540 goto next_message;
541 }
542 *msg = xml;
543 break;
544
545 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100546 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100547 lyxml_free(session->ctx, xml);
548 goto next_message;
549
550 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100551 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100552 lyxml_free(session->ctx, xml);
553 goto next_message;
554
555 default:
556 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
557 * NC_MSG_NONE is not returned by nc_read_msg()
558 */
559 break;
560 }
561
562 return msgtype;
563}
564
565/* cannot strictly fail, but does not need to fill any error parameter at all */
566static void
567parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
568{
569 struct lyxml_elem *iter, *next, *info;
570
571 LY_TREE_FOR(xml->child, iter) {
572 if (!iter->ns) {
573 if (iter->content) {
574 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
575 } else {
576 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
577 }
578 continue;
579 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
580 if (iter->content) {
581 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
582 iter->name, iter->content, iter->ns->value);
583 } else {
584 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
585 }
586 continue;
587 }
588
589 if (!strcmp(iter->name, "error-type")) {
590 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
591 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
592 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
593 } else if (err->type) {
594 WRN("<rpc-error> <error-type> duplicated.");
595 } else {
596 err->type = lydict_insert(ctx, iter->content, 0);
597 }
598 } else if (!strcmp(iter->name, "error-tag")) {
599 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
600 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
601 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
602 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
603 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
604 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
605 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
606 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
607 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
608 && strcmp(iter->content, "malformed-message"))) {
609 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
610 } else if (err->tag) {
611 WRN("<rpc-error> <error-tag> duplicated.");
612 } else {
613 err->tag = lydict_insert(ctx, iter->content, 0);
614 }
615 } else if (!strcmp(iter->name, "error-severity")) {
616 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
617 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
618 } else if (err->severity) {
619 WRN("<rpc-error> <error-severity> duplicated.");
620 } else {
621 err->severity = lydict_insert(ctx, iter->content, 0);
622 }
623 } else if (!strcmp(iter->name, "error-app-tag")) {
624 if (err->apptag) {
625 WRN("<rpc-error> <error-app-tag> duplicated.");
626 } else {
627 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
628 }
629 } else if (!strcmp(iter->name, "error-path")) {
630 if (err->path) {
631 WRN("<rpc-error> <error-path> duplicated.");
632 } else {
633 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
634 }
635 } else if (!strcmp(iter->name, "error-message")) {
636 if (err->message) {
637 WRN("<rpc-error> <error-message> duplicated.");
638 } else {
639 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
640 if (!err->message_lang) {
641 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
642 }
643 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
644 }
645 } else if (!strcmp(iter->name, "error-info")) {
646 LY_TREE_FOR_SAFE(iter->child, next, info) {
647 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
648 if (!strcmp(info->name, "session-id")) {
649 if (err->sid) {
650 WRN("<rpc-error> <error-info> <session-id> duplicated.");
651 } else {
652 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
653 }
654 } else if (!strcmp(info->name, "bad-attr")) {
655 ++err->attr_count;
656 err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr);
657 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
658 } else if (!strcmp(info->name, "bad-element")) {
659 ++err->elem_count;
660 err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem);
661 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
662 } else if (!strcmp(info->name, "bad-namespace")) {
663 ++err->ns_count;
664 err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns);
665 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
666 } else {
667 if (info->content) {
668 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
669 info->name, info->content);
670 } else {
671 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
672 }
673 }
674 } else {
675 lyxml_unlink(ctx, info);
676 ++err->other_count;
677 err->other = realloc(err->other, err->other_count * sizeof *err->other);
678 err->other[err->other_count - 1] = info;
679 }
680 }
681 } else {
682 if (iter->content) {
683 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
684 } else {
685 WRN("<rpc-error> unknown child \"%s\".", iter->name);
686 }
687 }
688 }
689}
690
691static struct nc_reply *
692parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc)
693{
694 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100695 const struct lys_node *schema = NULL;
696 const struct lys_module *mod;
Michal Vasko086311b2016-01-08 09:53:11 +0100697 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100698 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100699 struct nc_reply_data *data_rpl;
700 struct nc_reply *reply = NULL;
701 struct nc_rpc_generic *rpc_gen;
702 int i;
703
704 if (!xml->child) {
705 ERR("An empty <rpc-reply>.");
706 return NULL;
707 }
708
709 /* rpc-error */
710 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
711 /* count and check elements */
712 i = 0;
713 LY_TREE_FOR(xml->child, iter) {
714 if (strcmp(iter->name, "rpc-error")) {
715 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
716 return NULL;
717 } else if (!iter->ns) {
718 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
719 return NULL;
720 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
721 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
722 return NULL;
723 }
724 ++i;
725 }
726
727 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100728 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100729 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100730 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100731 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100732 reply = (struct nc_reply *)error_rpl;
733
734 i = 0;
735 LY_TREE_FOR(xml->child, iter) {
736 parse_rpc_error(ctx, iter, error_rpl->err + i);
737 ++i;
738 }
739
740 /* ok */
741 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
742 if (xml->child->next) {
743 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
744 return NULL;
745 }
746 reply = malloc(sizeof *reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100747 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100748
749 /* some RPC output */
750 } else {
751 switch (rpc->type) {
752 case NC_RPC_GENERIC:
753 rpc_gen = (struct nc_rpc_generic *)rpc;
754
755 if (rpc_gen->has_data) {
756 schema = rpc_gen->content.data->schema;
757 } else {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100758 data = lyd_parse_data(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC);
Michal Vasko086311b2016-01-08 09:53:11 +0100759 if (!data) {
760 ERR("Failed to parse a generic RPC XML.");
761 return NULL;
762 }
763 schema = data->schema;
764 lyd_free(data);
765 data = NULL;
766 }
767 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100768 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100769 return NULL;
770 }
771 break;
772
773 case NC_RPC_GETCONFIG:
774 case NC_RPC_GET:
775 /* special treatment */
776 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
777 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET));
778 if (!data) {
779 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
780 return NULL;
781 }
782 break;
783
784 case NC_RPC_GETSCHEMA:
Michal Vasko0473c4c2016-01-19 10:40:06 +0100785 mod = ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL);
786 if (mod) {
787 schema = lys_get_node(mod, "/get-schema");
788 }
Michal Vasko086311b2016-01-08 09:53:11 +0100789 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100790 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100791 return NULL;
792 }
793 break;
794
795 case NC_RPC_EDIT:
796 case NC_RPC_COPY:
797 case NC_RPC_DELETE:
798 case NC_RPC_LOCK:
799 case NC_RPC_UNLOCK:
800 case NC_RPC_KILL:
801 case NC_RPC_COMMIT:
802 case NC_RPC_DISCARD:
803 case NC_RPC_CANCEL:
804 case NC_RPC_VALIDATE:
805 case NC_RPC_SUBSCRIBE:
806 /* there is no output defined */
807 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
808 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100809 default:
810 ERRINT;
811 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100812 }
813
814 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100815 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100816 if (!data) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100817 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100818 } else {
819 /* <get>, <get-config> */
820 data_rpl->data = data;
821 }
822 if (!data_rpl->data) {
823 ERR("Failed to parse <rpc-reply>.");
824 free(data_rpl);
825 return NULL;
826 }
827 reply = (struct nc_reply *)data_rpl;
828 }
829
830 return reply;
831}
832
833API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100834nc_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 +0100835{
836 struct lyxml_elem *xml;
837 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
838
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100839 if (!session || !rpc || !reply) {
840 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100841 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100842 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100843 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100844 return NC_MSG_ERROR;
845 }
846 *reply = NULL;
847
848 msgtype = get_msg(session, timeout, msgid, &xml);
849 if (msgtype == NC_MSG_WOULDBLOCK) {
850 return NC_MSG_WOULDBLOCK;
851 }
852
853 if (msgtype == NC_MSG_REPLY) {
854 *reply = parse_reply(session->ctx, xml, rpc);
855 lyxml_free(session->ctx, xml);
856 if (!(*reply)) {
857 return NC_MSG_ERROR;
858 }
859 }
860
861 return msgtype;
862}
863
864API NC_MSG_TYPE
865nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
866{
867 struct lyxml_elem *xml, *ev_time;
868 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
869
870 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100871 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100872 return NC_MSG_ERROR;
873 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100874 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100875 return NC_MSG_ERROR;
876 }
877
878 msgtype = get_msg(session, timeout, 0, &xml);
879
880 if (msgtype == NC_MSG_NOTIF) {
881 *notif = calloc(1, sizeof **notif);
882
883 /* eventTime */
884 LY_TREE_FOR(xml->child, ev_time) {
885 if (!strcmp(ev_time->name, "eventTime")) {
886 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
887 /* lyd_parse does not know this element */
888 lyxml_free(session->ctx, ev_time);
889 break;
890 }
891 }
892 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +0100893 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100894 goto fail;
895 }
896
897 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100898 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100899 lyxml_free(session->ctx, xml);
900 xml = NULL;
901 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +0100902 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100903 goto fail;
904 }
905 }
906
907 return msgtype;
908
909fail:
910 lydict_remove(session->ctx, (*notif)->datetime);
911 lyd_free((*notif)->tree);
912 free(*notif);
913 *notif = NULL;
914 lyxml_free(session->ctx, xml);
915
916 return NC_MSG_ERROR;
917}
918
919API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100920nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +0100921{
922 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100923 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100924 struct nc_rpc_generic *rpc_gen;
925 struct nc_rpc_getconfig *rpc_gc;
926 struct nc_rpc_edit *rpc_e;
927 struct nc_rpc_copy *rpc_cp;
928 struct nc_rpc_delete *rpc_del;
929 struct nc_rpc_lock *rpc_lock;
930 struct nc_rpc_get *rpc_g;
931 struct nc_rpc_kill *rpc_k;
932 struct nc_rpc_commit *rpc_com;
933 struct nc_rpc_cancel *rpc_can;
934 struct nc_rpc_validate *rpc_val;
935 struct nc_rpc_getschema *rpc_gs;
936 struct nc_rpc_subscribe *rpc_sub;
937 struct lyd_node *data, *node;
938 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
939 char str[11];
940 uint64_t cur_msgid;
941
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100942 if (!session || !rpc || !msgid) {
943 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100944 return NC_MSG_ERROR;
945 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100946 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100947 return NC_MSG_ERROR;
948 }
949
950 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
951 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
952 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +0100953 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100954 return NC_MSG_ERROR;
955 }
956 }
957
958 switch (rpc->type) {
959 case NC_RPC_GENERIC:
960 rpc_gen = (struct nc_rpc_generic *)rpc;
961
962 if (rpc_gen->has_data) {
963 data = rpc_gen->content.data;
964 } else {
965 data = lyd_parse_data(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
966 }
967 break;
968
969 case NC_RPC_GETCONFIG:
970 rpc_gc = (struct nc_rpc_getconfig *)rpc;
971
972 data = lyd_new(NULL, ietfnc, "get-config");
973 node = lyd_new(data, ietfnc, "source");
974 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
975 if (!node) {
976 lyd_free(data);
977 return NC_MSG_ERROR;
978 }
979 if (rpc_gc->filter) {
980 if (rpc_gc->filter[0] == '<') {
981 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
982 lyd_insert_attr(node, "type", "subtree");
983 } else {
984 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
985 lyd_insert_attr(node, "type", "xpath");
986 lyd_insert_attr(node, "select", rpc_gc->filter);
987 }
988 if (!node) {
989 lyd_free(data);
990 return NC_MSG_ERROR;
991 }
992 }
993
994 if (rpc_gc->wd_mode) {
995 if (!ietfncwd) {
996 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
997 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +0100998 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100999 return NC_MSG_ERROR;
1000 }
1001 }
1002 switch (rpc_gc->wd_mode) {
1003 case NC_WD_UNKNOWN:
1004 /* cannot get here */
1005 break;
1006 case NC_WD_ALL:
1007 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1008 break;
1009 case NC_WD_ALL_TAG:
1010 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1011 break;
1012 case NC_WD_TRIM:
1013 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1014 break;
1015 case NC_WD_EXPLICIT:
1016 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1017 break;
1018 }
1019 if (!node) {
1020 lyd_free(data);
1021 return NC_MSG_ERROR;
1022 }
1023 }
1024 break;
1025
1026 case NC_RPC_EDIT:
1027 rpc_e = (struct nc_rpc_edit *)rpc;
1028
1029 data = lyd_new(NULL, ietfnc, "edit-config");
1030 node = lyd_new(data, ietfnc, "target");
1031 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1032 if (!node) {
1033 lyd_free(data);
1034 return NC_MSG_ERROR;
1035 }
1036
1037 if (rpc_e->default_op) {
1038 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1039 if (!node) {
1040 lyd_free(data);
1041 return NC_MSG_ERROR;
1042 }
1043 }
1044
1045 if (rpc_e->test_opt) {
1046 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1047 if (!node) {
1048 lyd_free(data);
1049 return NC_MSG_ERROR;
1050 }
1051 }
1052
1053 if (rpc_e->error_opt) {
1054 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1055 if (!node) {
1056 lyd_free(data);
1057 return NC_MSG_ERROR;
1058 }
1059 }
1060
1061 if (rpc_e->edit_cont[0] == '<') {
1062 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1063 } else {
1064 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1065 }
1066 if (!node) {
1067 lyd_free(data);
1068 return NC_MSG_ERROR;
1069 }
1070 break;
1071
1072 case NC_RPC_COPY:
1073 rpc_cp = (struct nc_rpc_copy *)rpc;
1074
1075 data = lyd_new(NULL, ietfnc, "copy-config");
1076 node = lyd_new(data, ietfnc, "target");
1077 if (rpc_cp->url_trg) {
1078 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1079 } else {
1080 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1081 }
1082 if (!node) {
1083 lyd_free(data);
1084 return NC_MSG_ERROR;
1085 }
1086
1087 node = lyd_new(data, ietfnc, "source");
1088 if (rpc_cp->url_config_src) {
1089 if (rpc_cp->url_config_src[0] == '<') {
1090 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1091 } else {
1092 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1093 }
1094 } else {
1095 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1096 }
1097 if (!node) {
1098 lyd_free(data);
1099 return NC_MSG_ERROR;
1100 }
1101
1102 if (rpc_cp->wd_mode) {
1103 if (!ietfncwd) {
1104 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1105 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001106 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001107 return NC_MSG_ERROR;
1108 }
1109 }
1110 switch (rpc_cp->wd_mode) {
1111 case NC_WD_UNKNOWN:
1112 /* cannot get here */
1113 break;
1114 case NC_WD_ALL:
1115 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1116 break;
1117 case NC_WD_ALL_TAG:
1118 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1119 break;
1120 case NC_WD_TRIM:
1121 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1122 break;
1123 case NC_WD_EXPLICIT:
1124 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1125 break;
1126 }
1127 if (!node) {
1128 lyd_free(data);
1129 return NC_MSG_ERROR;
1130 }
1131 }
1132 break;
1133
1134 case NC_RPC_DELETE:
1135 rpc_del = (struct nc_rpc_delete *)rpc;
1136
1137 data = lyd_new(NULL, ietfnc, "delete-config");
1138 node = lyd_new(data, ietfnc, "target");
1139 if (rpc_del->url) {
1140 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1141 } else {
1142 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1143 }
1144 if (!node) {
1145 lyd_free(data);
1146 return NC_MSG_ERROR;
1147 }
1148 break;
1149
1150 case NC_RPC_LOCK:
1151 rpc_lock = (struct nc_rpc_lock *)rpc;
1152
1153 data = lyd_new(NULL, ietfnc, "lock");
1154 node = lyd_new(data, ietfnc, "target");
1155 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1156 if (!node) {
1157 lyd_free(data);
1158 return NC_MSG_ERROR;
1159 }
1160 break;
1161
1162 case NC_RPC_UNLOCK:
1163 rpc_lock = (struct nc_rpc_lock *)rpc;
1164
1165 data = lyd_new(NULL, ietfnc, "unlock");
1166 node = lyd_new(data, ietfnc, "target");
1167 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1168 if (!node) {
1169 lyd_free(data);
1170 return NC_MSG_ERROR;
1171 }
1172 break;
1173
1174 case NC_RPC_GET:
1175 rpc_g = (struct nc_rpc_get *)rpc;
1176
1177 data = lyd_new(NULL, ietfnc, "get");
1178 if (rpc_g->filter) {
1179 if (rpc_g->filter[0] == '<') {
1180 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1181 lyd_insert_attr(node, "type", "subtree");
1182 } else {
1183 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1184 lyd_insert_attr(node, "type", "xpath");
1185 lyd_insert_attr(node, "select", rpc_g->filter);
1186 }
1187 if (!node) {
1188 lyd_free(data);
1189 return NC_MSG_ERROR;
1190 }
1191 }
1192
1193 if (rpc_g->wd_mode) {
1194 if (!ietfncwd) {
1195 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1196 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001197 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001198 return NC_MSG_ERROR;
1199 }
1200 }
1201 switch (rpc_g->wd_mode) {
1202 case NC_WD_UNKNOWN:
1203 /* cannot get here */
1204 break;
1205 case NC_WD_ALL:
1206 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1207 break;
1208 case NC_WD_ALL_TAG:
1209 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1210 break;
1211 case NC_WD_TRIM:
1212 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1213 break;
1214 case NC_WD_EXPLICIT:
1215 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1216 break;
1217 }
1218 if (!node) {
1219 lyd_free(data);
1220 return NC_MSG_ERROR;
1221 }
1222 }
1223 break;
1224
1225 case NC_RPC_KILL:
1226 rpc_k = (struct nc_rpc_kill *)rpc;
1227
1228 data = lyd_new(NULL, ietfnc, "kill-session");
1229 sprintf(str, "%u", rpc_k->sid);
1230 lyd_new_leaf(data, ietfnc, "session-id", str);
1231 break;
1232
1233 case NC_RPC_COMMIT:
1234 rpc_com = (struct nc_rpc_commit *)rpc;
1235
1236 data = lyd_new(NULL, ietfnc, "commit");
1237 if (rpc_com->confirmed) {
1238 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1239 }
1240
1241 if (rpc_com->confirm_timeout) {
1242 sprintf(str, "%u", rpc_com->confirm_timeout);
1243 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1244 }
1245
1246 if (rpc_com->persist) {
1247 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1248 if (!node) {
1249 lyd_free(data);
1250 return NC_MSG_ERROR;
1251 }
1252 }
1253
1254 if (rpc_com->persist_id) {
1255 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1256 if (!node) {
1257 lyd_free(data);
1258 return NC_MSG_ERROR;
1259 }
1260 }
1261 break;
1262
1263 case NC_RPC_DISCARD:
1264 data = lyd_new(NULL, ietfnc, "discard-changes");
1265 break;
1266
1267 case NC_RPC_CANCEL:
1268 rpc_can = (struct nc_rpc_cancel *)rpc;
1269
1270 data = lyd_new(NULL, ietfnc, "cancel-commit");
1271 if (rpc_can->persist_id) {
1272 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1273 if (!node) {
1274 lyd_free(data);
1275 return NC_MSG_ERROR;
1276 }
1277 }
1278 break;
1279
1280 case NC_RPC_VALIDATE:
1281 rpc_val = (struct nc_rpc_validate *)rpc;
1282
1283 data = lyd_new(NULL, ietfnc, "validate");
1284 node = lyd_new(data, ietfnc, "source");
1285 if (rpc_val->url_config_src) {
1286 if (rpc_val->url_config_src[0] == '<') {
1287 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1288 } else {
1289 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1290 }
1291 } else {
1292 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1293 }
1294 if (!node) {
1295 lyd_free(data);
1296 return NC_MSG_ERROR;
1297 }
1298 break;
1299
1300 case NC_RPC_GETSCHEMA:
1301 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1302 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001303 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001304 return NC_MSG_ERROR;
1305 }
1306
1307 rpc_gs = (struct nc_rpc_getschema *)rpc;
1308
1309 data = lyd_new(NULL, ietfncmon, "get-schema");
1310 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1311 if (!node) {
1312 lyd_free(data);
1313 return NC_MSG_ERROR;
1314 }
1315 if (rpc_gs->version) {
1316 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1317 if (!node) {
1318 lyd_free(data);
1319 return NC_MSG_ERROR;
1320 }
1321 }
1322 if (rpc_gs->format) {
1323 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1324 if (!node) {
1325 lyd_free(data);
1326 return NC_MSG_ERROR;
1327 }
1328 }
1329 break;
1330
1331 case NC_RPC_SUBSCRIBE:
1332 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1333 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001334 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001335 return NC_MSG_ERROR;
1336 }
1337
1338 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1339
1340 data = lyd_new(NULL, notifs, "create-subscription");
1341 if (rpc_sub->stream) {
1342 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1343 if (!node) {
1344 lyd_free(data);
1345 return NC_MSG_ERROR;
1346 }
1347 }
1348
1349 if (rpc_sub->filter) {
1350 if (rpc_sub->filter[0] == '<') {
1351 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1352 lyd_insert_attr(node, "type", "subtree");
1353 } else {
1354 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1355 lyd_insert_attr(node, "type", "xpath");
1356 lyd_insert_attr(node, "select", rpc_sub->filter);
1357 }
1358 if (!node) {
1359 lyd_free(data);
1360 return NC_MSG_ERROR;
1361 }
1362 }
1363
1364 if (rpc_sub->start) {
1365 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1366 if (!node) {
1367 lyd_free(data);
1368 return NC_MSG_ERROR;
1369 }
1370 }
1371
1372 if (rpc_sub->stop) {
1373 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1374 if (!node) {
1375 lyd_free(data);
1376 return NC_MSG_ERROR;
1377 }
1378 }
1379 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001380 default:
1381 ERRINT;
1382 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001383 }
1384
1385 if (lyd_validate(data, LYD_OPT_STRICT)) {
1386 lyd_free(data);
1387 return NC_MSG_ERROR;
1388 }
1389
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001390 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1391 if (ret == -1) {
1392 /* error */
1393 r = NC_MSG_ERROR;
1394 } else if (!ret) {
1395 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001396 r = NC_MSG_WOULDBLOCK;
1397 } else {
1398 /* send RPC, store its message ID */
1399 r = nc_send_msg(session, data);
1400 cur_msgid = session->msgid;
1401 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001402 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001403
1404 lyd_free(data);
1405
1406 if (r != NC_MSG_RPC) {
1407 return r;
1408 }
1409
1410 *msgid = cur_msgid;
1411 return NC_MSG_RPC;
1412}
1413
1414/* CALL HOME */
1415
1416static int
1417get_listen_socket(const char *address, uint16_t port)
1418{
1419 int sock;
1420 const int optVal = 1;
1421 const socklen_t optLen = sizeof(optVal);
1422 char is_ipv4;
1423 struct sockaddr_storage saddr;
1424
1425 struct sockaddr_in* saddr4;
1426 struct sockaddr_in6* saddr6;
1427
1428 if (!address || !port) {
1429 return -1;
1430 }
1431
1432 if (strchr(address, ':') == NULL) {
1433 is_ipv4 = 1;
1434 } else {
1435 is_ipv4 = 0;
1436 }
1437
1438 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
1439 if (sock == -1) {
1440 ERR("Could not create socket (%s)", strerror(errno));
1441 return -1;
1442 }
1443
1444 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
1445 ERR("Could not set socket SO_REUSEADDR option (%s)", strerror(errno));
1446 close(sock);
1447 return -1;
1448 }
1449
1450 /* TODO may be needed
1451 if (fcntl(sock, F_SETFD, FD_CLOEXEC) != 0) {
1452 nc_verb_error("%s: fcntl failed (%s)", __func__, strerror(errno));
1453 continue;
1454 }*/
1455
1456 bzero(&saddr, sizeof(struct sockaddr_storage));
1457 if (is_ipv4) {
1458 saddr4 = (struct sockaddr_in *)&saddr;
1459
1460 saddr4->sin_family = AF_INET;
1461 saddr4->sin_port = htons(port);
1462
1463 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
1464 ERR("Failed to convert \"%s\" to IPv4 address.", address);
1465 close(sock);
1466 return -1;
1467 }
1468
1469 if (bind(sock, (struct sockaddr*)saddr4, sizeof(struct sockaddr_in)) == -1) {
1470 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
1471 close(sock);
1472 return -1;
1473 }
1474
1475 } else {
1476 saddr6 = (struct sockaddr_in6 *)&saddr;
1477
1478 saddr6->sin6_family = AF_INET6;
1479 saddr6->sin6_port = htons(port);
1480
1481 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
1482 ERR("Failed to convert \"%s\" to IPv6 address.", address);
1483 close(sock);
1484 return -1;
1485 }
1486
1487 if (bind(sock, (struct sockaddr*)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001488 ERR("Could not bind \"%s\" port %d (%s)", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +01001489 close(sock);
1490 return -1;
1491 }
1492 }
1493
1494 if (listen(sock, NC_REVERSE_QUEUE)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001495 ERR("Unable to start listening on \"%s\" port %d (%s)", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +01001496 close(sock);
1497 return -1;
1498 }
1499
1500 return sock;
1501}
1502
1503int
Michal Vaskod083db62016-01-19 10:31:29 +01001504nc_callhome_accept_connection(uint16_t port, int timeout, uint16_t *server_port, char **server_host)
Michal Vasko086311b2016-01-08 09:53:11 +01001505{
1506 struct pollfd reverse_listen_socket = {-1, POLLIN, 0};
1507 int sock;
1508 struct sockaddr_storage remote;
1509 socklen_t addr_size = sizeof(remote);
1510 int status;
1511
1512 reverse_listen_socket.fd = get_listen_socket("::0", port);
1513 if (reverse_listen_socket.fd == -1) {
1514 goto fail;
1515 }
1516
1517 reverse_listen_socket.revents = 0;
1518 while (1) {
1519 DBG("Waiting %ums for incoming Call Home connections.", timeout);
1520 status = poll(&reverse_listen_socket, 1, timeout);
1521
1522 if (status == 0) {
1523 /* timeout */
1524 ERR("Timeout for Call Home listen expired.");
1525 goto fail;
1526 } else if ((status == -1) && (errno == EINTR)) {
1527 /* poll was interrupted - try it again */
1528 continue;
1529 } else if (status < 0) {
1530 /* poll failed - something wrong happened */
1531 ERR("Call Home poll failed (%s).", strerror(errno));
1532 goto fail;
1533 } else if (status > 0) {
1534 if (reverse_listen_socket.revents & (POLLHUP | POLLERR)) {
1535 /* close pipe/fd - other side already did it */
1536 ERR("Call Home listening socket was closed.");
1537 goto fail;
1538 } else if (reverse_listen_socket.revents & POLLIN) {
1539 /* accept call home */
1540 sock = accept(reverse_listen_socket.fd, (struct sockaddr *)&remote, &addr_size);
1541 break;
1542 }
1543 }
1544 }
1545
1546 /* we accepted a connection, that's it */
1547 close(reverse_listen_socket.fd);
1548
1549 /* fill some server info, if interested */
1550 if (remote.ss_family == AF_INET) {
1551 struct sockaddr_in *remote_in = (struct sockaddr_in *)&remote;
1552 if (server_port) {
1553 *server_port = ntohs(remote_in->sin_port);
1554 }
1555 if (server_host) {
1556 *server_host = malloc(INET6_ADDRSTRLEN);
1557 inet_ntop(AF_INET, &(remote_in->sin_addr), *server_host, INET6_ADDRSTRLEN);
1558 }
1559 } else if (remote.ss_family == AF_INET6) {
1560 struct sockaddr_in6 *remote_in = (struct sockaddr_in6 *)&remote;
1561 if (server_port) {
1562 *server_port = ntohs(remote_in->sin6_port);
1563 }
1564 if (server_host) {
1565 *server_host = malloc(INET6_ADDRSTRLEN);
1566 inet_ntop(AF_INET6, &(remote_in->sin6_addr), *server_host, INET6_ADDRSTRLEN);
1567 }
1568 }
1569
1570 return sock;
1571
1572fail:
1573 if (reverse_listen_socket.fd != -1) {
1574 close(reverse_listen_socket.fd);
1575 }
1576
1577 return -1;
1578}