blob: 20a0660cc1dec003f05b4feb67c711a600e0a42e [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)) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100482 /* just check that there is a message-id */
Michal Vasko086311b2016-01-08 09:53:11 +0100483 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 }
Michal Vasko086311b2016-01-08 09:53:11 +0100490
491 cont_ptr = &session->replies;
492 while (*cont_ptr) {
493 cont_ptr = &((*cont_ptr)->next);
494 }
495 *cont_ptr = malloc(sizeof **cont_ptr);
496 (*cont_ptr)->msg = xml;
497 (*cont_ptr)->next = NULL;
498 }
499
500 /* we read notif, want a rpc-reply */
501 if (msgid && (msgtype == NC_MSG_NOTIF)) {
502 if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100503 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100504 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100505 lyxml_free(session->ctx, xml);
506 goto next_message;
507 }
508
509 cont_ptr = &session->notifs;
510 while (*cont_ptr) {
511 cont_ptr = &((*cont_ptr)->next);
512 }
513 *cont_ptr = malloc(sizeof **cont_ptr);
514 (*cont_ptr)->msg = xml;
515 (*cont_ptr)->next = NULL;
516 }
517
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100518 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100519
520 switch (msgtype) {
521 case NC_MSG_NOTIF:
522 /* we want a rpc-reply */
523 if (msgid) {
524 goto next_message;
525 }
526 *msg = xml;
527 break;
528
529 case NC_MSG_REPLY:
530 /* we want a notif */
531 if (!msgid) {
532 goto next_message;
533 }
534 *msg = xml;
535 break;
536
537 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100538 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100539 lyxml_free(session->ctx, xml);
540 goto next_message;
541
542 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100543 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100544 lyxml_free(session->ctx, xml);
545 goto next_message;
546
547 default:
548 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
549 * NC_MSG_NONE is not returned by nc_read_msg()
550 */
551 break;
552 }
553
554 return msgtype;
555}
556
557/* cannot strictly fail, but does not need to fill any error parameter at all */
558static void
559parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
560{
561 struct lyxml_elem *iter, *next, *info;
562
563 LY_TREE_FOR(xml->child, iter) {
564 if (!iter->ns) {
565 if (iter->content) {
566 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
567 } else {
568 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
569 }
570 continue;
571 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
572 if (iter->content) {
573 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
574 iter->name, iter->content, iter->ns->value);
575 } else {
576 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
577 }
578 continue;
579 }
580
581 if (!strcmp(iter->name, "error-type")) {
582 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
583 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
584 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
585 } else if (err->type) {
586 WRN("<rpc-error> <error-type> duplicated.");
587 } else {
588 err->type = lydict_insert(ctx, iter->content, 0);
589 }
590 } else if (!strcmp(iter->name, "error-tag")) {
591 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
592 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
593 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
594 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
595 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
596 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
597 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
598 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
599 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
600 && strcmp(iter->content, "malformed-message"))) {
601 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
602 } else if (err->tag) {
603 WRN("<rpc-error> <error-tag> duplicated.");
604 } else {
605 err->tag = lydict_insert(ctx, iter->content, 0);
606 }
607 } else if (!strcmp(iter->name, "error-severity")) {
608 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
609 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
610 } else if (err->severity) {
611 WRN("<rpc-error> <error-severity> duplicated.");
612 } else {
613 err->severity = lydict_insert(ctx, iter->content, 0);
614 }
615 } else if (!strcmp(iter->name, "error-app-tag")) {
616 if (err->apptag) {
617 WRN("<rpc-error> <error-app-tag> duplicated.");
618 } else {
619 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
620 }
621 } else if (!strcmp(iter->name, "error-path")) {
622 if (err->path) {
623 WRN("<rpc-error> <error-path> duplicated.");
624 } else {
625 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
626 }
627 } else if (!strcmp(iter->name, "error-message")) {
628 if (err->message) {
629 WRN("<rpc-error> <error-message> duplicated.");
630 } else {
631 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
632 if (!err->message_lang) {
633 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
634 }
635 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
636 }
637 } else if (!strcmp(iter->name, "error-info")) {
638 LY_TREE_FOR_SAFE(iter->child, next, info) {
639 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
640 if (!strcmp(info->name, "session-id")) {
641 if (err->sid) {
642 WRN("<rpc-error> <error-info> <session-id> duplicated.");
643 } else {
644 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
645 }
646 } else if (!strcmp(info->name, "bad-attr")) {
647 ++err->attr_count;
648 err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr);
649 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
650 } else if (!strcmp(info->name, "bad-element")) {
651 ++err->elem_count;
652 err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem);
653 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
654 } else if (!strcmp(info->name, "bad-namespace")) {
655 ++err->ns_count;
656 err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns);
657 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
658 } else {
659 if (info->content) {
660 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
661 info->name, info->content);
662 } else {
663 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
664 }
665 }
666 } else {
667 lyxml_unlink(ctx, info);
668 ++err->other_count;
669 err->other = realloc(err->other, err->other_count * sizeof *err->other);
670 err->other[err->other_count - 1] = info;
671 }
672 }
673 } else {
674 if (iter->content) {
675 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
676 } else {
677 WRN("<rpc-error> unknown child \"%s\".", iter->name);
678 }
679 }
680 }
681}
682
683static struct nc_reply *
684parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc)
685{
686 struct lyxml_elem *iter;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100687 const struct lys_node *schema = NULL;
688 const struct lys_module *mod;
Michal Vasko086311b2016-01-08 09:53:11 +0100689 struct lyd_node *data = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100690 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100691 struct nc_reply_data *data_rpl;
692 struct nc_reply *reply = NULL;
693 struct nc_rpc_generic *rpc_gen;
694 int i;
695
696 if (!xml->child) {
697 ERR("An empty <rpc-reply>.");
698 return NULL;
699 }
700
701 /* rpc-error */
702 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
703 /* count and check elements */
704 i = 0;
705 LY_TREE_FOR(xml->child, iter) {
706 if (strcmp(iter->name, "rpc-error")) {
707 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
708 return NULL;
709 } else if (!iter->ns) {
710 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
711 return NULL;
712 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
713 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
714 return NULL;
715 }
716 ++i;
717 }
718
719 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100720 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100721 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100722 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100723 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100724 reply = (struct nc_reply *)error_rpl;
725
726 i = 0;
727 LY_TREE_FOR(xml->child, iter) {
728 parse_rpc_error(ctx, iter, error_rpl->err + i);
729 ++i;
730 }
731
732 /* ok */
733 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
734 if (xml->child->next) {
735 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
736 return NULL;
737 }
738 reply = malloc(sizeof *reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100739 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100740
741 /* some RPC output */
742 } else {
743 switch (rpc->type) {
744 case NC_RPC_GENERIC:
745 rpc_gen = (struct nc_rpc_generic *)rpc;
746
747 if (rpc_gen->has_data) {
748 schema = rpc_gen->content.data->schema;
749 } else {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100750 data = lyd_parse_data(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC);
Michal Vasko086311b2016-01-08 09:53:11 +0100751 if (!data) {
752 ERR("Failed to parse a generic RPC XML.");
753 return NULL;
754 }
755 schema = data->schema;
756 lyd_free(data);
757 data = NULL;
758 }
759 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100760 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100761 return NULL;
762 }
763 break;
764
765 case NC_RPC_GETCONFIG:
766 case NC_RPC_GET:
767 /* special treatment */
768 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
769 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET));
770 if (!data) {
771 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
772 return NULL;
773 }
774 break;
775
776 case NC_RPC_GETSCHEMA:
Michal Vasko0473c4c2016-01-19 10:40:06 +0100777 mod = ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL);
778 if (mod) {
779 schema = lys_get_node(mod, "/get-schema");
780 }
Michal Vasko086311b2016-01-08 09:53:11 +0100781 if (!schema) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100782 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100783 return NULL;
784 }
785 break;
786
787 case NC_RPC_EDIT:
788 case NC_RPC_COPY:
789 case NC_RPC_DELETE:
790 case NC_RPC_LOCK:
791 case NC_RPC_UNLOCK:
792 case NC_RPC_KILL:
793 case NC_RPC_COMMIT:
794 case NC_RPC_DISCARD:
795 case NC_RPC_CANCEL:
796 case NC_RPC_VALIDATE:
797 case NC_RPC_SUBSCRIBE:
798 /* there is no output defined */
799 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
800 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100801 default:
802 ERRINT;
803 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100804 }
805
806 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100807 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100808 if (!data) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100809 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPCREPLY, schema);
Michal Vasko086311b2016-01-08 09:53:11 +0100810 } else {
811 /* <get>, <get-config> */
812 data_rpl->data = data;
813 }
814 if (!data_rpl->data) {
815 ERR("Failed to parse <rpc-reply>.");
816 free(data_rpl);
817 return NULL;
818 }
819 reply = (struct nc_reply *)data_rpl;
820 }
821
822 return reply;
823}
824
825API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100826nc_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 +0100827{
828 struct lyxml_elem *xml;
829 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
830
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100831 if (!session || !rpc || !reply) {
832 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100833 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100834 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100835 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100836 return NC_MSG_ERROR;
837 }
838 *reply = NULL;
839
840 msgtype = get_msg(session, timeout, msgid, &xml);
841 if (msgtype == NC_MSG_WOULDBLOCK) {
842 return NC_MSG_WOULDBLOCK;
843 }
844
845 if (msgtype == NC_MSG_REPLY) {
846 *reply = parse_reply(session->ctx, xml, rpc);
847 lyxml_free(session->ctx, xml);
848 if (!(*reply)) {
849 return NC_MSG_ERROR;
850 }
851 }
852
853 return msgtype;
854}
855
856API NC_MSG_TYPE
857nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
858{
859 struct lyxml_elem *xml, *ev_time;
860 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
861
862 if (!session || !notif) {
Michal Vaskod083db62016-01-19 10:31:29 +0100863 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100864 return NC_MSG_ERROR;
865 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100866 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100867 return NC_MSG_ERROR;
868 }
869
870 msgtype = get_msg(session, timeout, 0, &xml);
871
872 if (msgtype == NC_MSG_NOTIF) {
873 *notif = calloc(1, sizeof **notif);
874
875 /* eventTime */
876 LY_TREE_FOR(xml->child, ev_time) {
877 if (!strcmp(ev_time->name, "eventTime")) {
878 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
879 /* lyd_parse does not know this element */
880 lyxml_free(session->ctx, ev_time);
881 break;
882 }
883 }
884 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +0100885 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100886 goto fail;
887 }
888
889 /* notification body */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100890 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100891 lyxml_free(session->ctx, xml);
892 xml = NULL;
893 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +0100894 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100895 goto fail;
896 }
897 }
898
899 return msgtype;
900
901fail:
902 lydict_remove(session->ctx, (*notif)->datetime);
903 lyd_free((*notif)->tree);
904 free(*notif);
905 *notif = NULL;
906 lyxml_free(session->ctx, xml);
907
908 return NC_MSG_ERROR;
909}
910
911API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100912nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +0100913{
914 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100915 int ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100916 struct nc_rpc_generic *rpc_gen;
917 struct nc_rpc_getconfig *rpc_gc;
918 struct nc_rpc_edit *rpc_e;
919 struct nc_rpc_copy *rpc_cp;
920 struct nc_rpc_delete *rpc_del;
921 struct nc_rpc_lock *rpc_lock;
922 struct nc_rpc_get *rpc_g;
923 struct nc_rpc_kill *rpc_k;
924 struct nc_rpc_commit *rpc_com;
925 struct nc_rpc_cancel *rpc_can;
926 struct nc_rpc_validate *rpc_val;
927 struct nc_rpc_getschema *rpc_gs;
928 struct nc_rpc_subscribe *rpc_sub;
929 struct lyd_node *data, *node;
930 const struct lys_module *ietfnc, *ietfncmon, *notifs, *ietfncwd = NULL;
931 char str[11];
932 uint64_t cur_msgid;
933
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100934 if (!session || !rpc || !msgid) {
935 ERRARG;
Michal Vasko086311b2016-01-08 09:53:11 +0100936 return NC_MSG_ERROR;
937 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +0100938 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100939 return NC_MSG_ERROR;
940 }
941
942 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
943 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
944 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +0100945 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100946 return NC_MSG_ERROR;
947 }
948 }
949
950 switch (rpc->type) {
951 case NC_RPC_GENERIC:
952 rpc_gen = (struct nc_rpc_generic *)rpc;
953
954 if (rpc_gen->has_data) {
955 data = rpc_gen->content.data;
956 } else {
957 data = lyd_parse_data(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_STRICT);
958 }
959 break;
960
961 case NC_RPC_GETCONFIG:
962 rpc_gc = (struct nc_rpc_getconfig *)rpc;
963
964 data = lyd_new(NULL, ietfnc, "get-config");
965 node = lyd_new(data, ietfnc, "source");
966 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
967 if (!node) {
968 lyd_free(data);
969 return NC_MSG_ERROR;
970 }
971 if (rpc_gc->filter) {
972 if (rpc_gc->filter[0] == '<') {
973 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_gc->filter);
974 lyd_insert_attr(node, "type", "subtree");
975 } else {
976 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
977 lyd_insert_attr(node, "type", "xpath");
978 lyd_insert_attr(node, "select", rpc_gc->filter);
979 }
980 if (!node) {
981 lyd_free(data);
982 return NC_MSG_ERROR;
983 }
984 }
985
986 if (rpc_gc->wd_mode) {
987 if (!ietfncwd) {
988 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
989 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +0100990 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100991 return NC_MSG_ERROR;
992 }
993 }
994 switch (rpc_gc->wd_mode) {
995 case NC_WD_UNKNOWN:
996 /* cannot get here */
997 break;
998 case NC_WD_ALL:
999 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1000 break;
1001 case NC_WD_ALL_TAG:
1002 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1003 break;
1004 case NC_WD_TRIM:
1005 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1006 break;
1007 case NC_WD_EXPLICIT:
1008 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1009 break;
1010 }
1011 if (!node) {
1012 lyd_free(data);
1013 return NC_MSG_ERROR;
1014 }
1015 }
1016 break;
1017
1018 case NC_RPC_EDIT:
1019 rpc_e = (struct nc_rpc_edit *)rpc;
1020
1021 data = lyd_new(NULL, ietfnc, "edit-config");
1022 node = lyd_new(data, ietfnc, "target");
1023 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1024 if (!node) {
1025 lyd_free(data);
1026 return NC_MSG_ERROR;
1027 }
1028
1029 if (rpc_e->default_op) {
1030 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1031 if (!node) {
1032 lyd_free(data);
1033 return NC_MSG_ERROR;
1034 }
1035 }
1036
1037 if (rpc_e->test_opt) {
1038 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1039 if (!node) {
1040 lyd_free(data);
1041 return NC_MSG_ERROR;
1042 }
1043 }
1044
1045 if (rpc_e->error_opt) {
1046 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1047 if (!node) {
1048 lyd_free(data);
1049 return NC_MSG_ERROR;
1050 }
1051 }
1052
1053 if (rpc_e->edit_cont[0] == '<') {
1054 node = lyd_new_anyxml(data, ietfnc, "config", rpc_e->edit_cont);
1055 } else {
1056 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1057 }
1058 if (!node) {
1059 lyd_free(data);
1060 return NC_MSG_ERROR;
1061 }
1062 break;
1063
1064 case NC_RPC_COPY:
1065 rpc_cp = (struct nc_rpc_copy *)rpc;
1066
1067 data = lyd_new(NULL, ietfnc, "copy-config");
1068 node = lyd_new(data, ietfnc, "target");
1069 if (rpc_cp->url_trg) {
1070 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1071 } else {
1072 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1073 }
1074 if (!node) {
1075 lyd_free(data);
1076 return NC_MSG_ERROR;
1077 }
1078
1079 node = lyd_new(data, ietfnc, "source");
1080 if (rpc_cp->url_config_src) {
1081 if (rpc_cp->url_config_src[0] == '<') {
1082 node = lyd_new_anyxml(node, ietfnc, "config", rpc_cp->url_config_src);
1083 } else {
1084 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1085 }
1086 } else {
1087 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1088 }
1089 if (!node) {
1090 lyd_free(data);
1091 return NC_MSG_ERROR;
1092 }
1093
1094 if (rpc_cp->wd_mode) {
1095 if (!ietfncwd) {
1096 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1097 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001098 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001099 return NC_MSG_ERROR;
1100 }
1101 }
1102 switch (rpc_cp->wd_mode) {
1103 case NC_WD_UNKNOWN:
1104 /* cannot get here */
1105 break;
1106 case NC_WD_ALL:
1107 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1108 break;
1109 case NC_WD_ALL_TAG:
1110 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1111 break;
1112 case NC_WD_TRIM:
1113 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1114 break;
1115 case NC_WD_EXPLICIT:
1116 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1117 break;
1118 }
1119 if (!node) {
1120 lyd_free(data);
1121 return NC_MSG_ERROR;
1122 }
1123 }
1124 break;
1125
1126 case NC_RPC_DELETE:
1127 rpc_del = (struct nc_rpc_delete *)rpc;
1128
1129 data = lyd_new(NULL, ietfnc, "delete-config");
1130 node = lyd_new(data, ietfnc, "target");
1131 if (rpc_del->url) {
1132 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1133 } else {
1134 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1135 }
1136 if (!node) {
1137 lyd_free(data);
1138 return NC_MSG_ERROR;
1139 }
1140 break;
1141
1142 case NC_RPC_LOCK:
1143 rpc_lock = (struct nc_rpc_lock *)rpc;
1144
1145 data = lyd_new(NULL, ietfnc, "lock");
1146 node = lyd_new(data, ietfnc, "target");
1147 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1148 if (!node) {
1149 lyd_free(data);
1150 return NC_MSG_ERROR;
1151 }
1152 break;
1153
1154 case NC_RPC_UNLOCK:
1155 rpc_lock = (struct nc_rpc_lock *)rpc;
1156
1157 data = lyd_new(NULL, ietfnc, "unlock");
1158 node = lyd_new(data, ietfnc, "target");
1159 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1160 if (!node) {
1161 lyd_free(data);
1162 return NC_MSG_ERROR;
1163 }
1164 break;
1165
1166 case NC_RPC_GET:
1167 rpc_g = (struct nc_rpc_get *)rpc;
1168
1169 data = lyd_new(NULL, ietfnc, "get");
1170 if (rpc_g->filter) {
1171 if (rpc_g->filter[0] == '<') {
1172 node = lyd_new_anyxml(data, ietfnc, "filter", rpc_g->filter);
1173 lyd_insert_attr(node, "type", "subtree");
1174 } else {
1175 node = lyd_new_anyxml(data, ietfnc, "filter", NULL);
1176 lyd_insert_attr(node, "type", "xpath");
1177 lyd_insert_attr(node, "select", rpc_g->filter);
1178 }
1179 if (!node) {
1180 lyd_free(data);
1181 return NC_MSG_ERROR;
1182 }
1183 }
1184
1185 if (rpc_g->wd_mode) {
1186 if (!ietfncwd) {
1187 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1188 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001189 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001190 return NC_MSG_ERROR;
1191 }
1192 }
1193 switch (rpc_g->wd_mode) {
1194 case NC_WD_UNKNOWN:
1195 /* cannot get here */
1196 break;
1197 case NC_WD_ALL:
1198 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1199 break;
1200 case NC_WD_ALL_TAG:
1201 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1202 break;
1203 case NC_WD_TRIM:
1204 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1205 break;
1206 case NC_WD_EXPLICIT:
1207 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1208 break;
1209 }
1210 if (!node) {
1211 lyd_free(data);
1212 return NC_MSG_ERROR;
1213 }
1214 }
1215 break;
1216
1217 case NC_RPC_KILL:
1218 rpc_k = (struct nc_rpc_kill *)rpc;
1219
1220 data = lyd_new(NULL, ietfnc, "kill-session");
1221 sprintf(str, "%u", rpc_k->sid);
1222 lyd_new_leaf(data, ietfnc, "session-id", str);
1223 break;
1224
1225 case NC_RPC_COMMIT:
1226 rpc_com = (struct nc_rpc_commit *)rpc;
1227
1228 data = lyd_new(NULL, ietfnc, "commit");
1229 if (rpc_com->confirmed) {
1230 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1231 }
1232
1233 if (rpc_com->confirm_timeout) {
1234 sprintf(str, "%u", rpc_com->confirm_timeout);
1235 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1236 }
1237
1238 if (rpc_com->persist) {
1239 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1240 if (!node) {
1241 lyd_free(data);
1242 return NC_MSG_ERROR;
1243 }
1244 }
1245
1246 if (rpc_com->persist_id) {
1247 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1248 if (!node) {
1249 lyd_free(data);
1250 return NC_MSG_ERROR;
1251 }
1252 }
1253 break;
1254
1255 case NC_RPC_DISCARD:
1256 data = lyd_new(NULL, ietfnc, "discard-changes");
1257 break;
1258
1259 case NC_RPC_CANCEL:
1260 rpc_can = (struct nc_rpc_cancel *)rpc;
1261
1262 data = lyd_new(NULL, ietfnc, "cancel-commit");
1263 if (rpc_can->persist_id) {
1264 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1265 if (!node) {
1266 lyd_free(data);
1267 return NC_MSG_ERROR;
1268 }
1269 }
1270 break;
1271
1272 case NC_RPC_VALIDATE:
1273 rpc_val = (struct nc_rpc_validate *)rpc;
1274
1275 data = lyd_new(NULL, ietfnc, "validate");
1276 node = lyd_new(data, ietfnc, "source");
1277 if (rpc_val->url_config_src) {
1278 if (rpc_val->url_config_src[0] == '<') {
1279 node = lyd_new_anyxml(node, ietfnc, "config", rpc_val->url_config_src);
1280 } else {
1281 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1282 }
1283 } else {
1284 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1285 }
1286 if (!node) {
1287 lyd_free(data);
1288 return NC_MSG_ERROR;
1289 }
1290 break;
1291
1292 case NC_RPC_GETSCHEMA:
1293 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1294 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001295 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001296 return NC_MSG_ERROR;
1297 }
1298
1299 rpc_gs = (struct nc_rpc_getschema *)rpc;
1300
1301 data = lyd_new(NULL, ietfncmon, "get-schema");
1302 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1303 if (!node) {
1304 lyd_free(data);
1305 return NC_MSG_ERROR;
1306 }
1307 if (rpc_gs->version) {
1308 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1309 if (!node) {
1310 lyd_free(data);
1311 return NC_MSG_ERROR;
1312 }
1313 }
1314 if (rpc_gs->format) {
1315 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1316 if (!node) {
1317 lyd_free(data);
1318 return NC_MSG_ERROR;
1319 }
1320 }
1321 break;
1322
1323 case NC_RPC_SUBSCRIBE:
1324 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1325 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001326 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001327 return NC_MSG_ERROR;
1328 }
1329
1330 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1331
1332 data = lyd_new(NULL, notifs, "create-subscription");
1333 if (rpc_sub->stream) {
1334 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1335 if (!node) {
1336 lyd_free(data);
1337 return NC_MSG_ERROR;
1338 }
1339 }
1340
1341 if (rpc_sub->filter) {
1342 if (rpc_sub->filter[0] == '<') {
1343 node = lyd_new_anyxml(data, notifs, "filter", rpc_sub->filter);
1344 lyd_insert_attr(node, "type", "subtree");
1345 } else {
1346 node = lyd_new_anyxml(data, notifs, "filter", NULL);
1347 lyd_insert_attr(node, "type", "xpath");
1348 lyd_insert_attr(node, "select", rpc_sub->filter);
1349 }
1350 if (!node) {
1351 lyd_free(data);
1352 return NC_MSG_ERROR;
1353 }
1354 }
1355
1356 if (rpc_sub->start) {
1357 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1358 if (!node) {
1359 lyd_free(data);
1360 return NC_MSG_ERROR;
1361 }
1362 }
1363
1364 if (rpc_sub->stop) {
1365 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1366 if (!node) {
1367 lyd_free(data);
1368 return NC_MSG_ERROR;
1369 }
1370 }
1371 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001372 default:
1373 ERRINT;
1374 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001375 }
1376
1377 if (lyd_validate(data, LYD_OPT_STRICT)) {
1378 lyd_free(data);
1379 return NC_MSG_ERROR;
1380 }
1381
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001382 ret = nc_timedlock(session->ti_lock, timeout, NULL);
1383 if (ret == -1) {
1384 /* error */
1385 r = NC_MSG_ERROR;
1386 } else if (!ret) {
1387 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001388 r = NC_MSG_WOULDBLOCK;
1389 } else {
1390 /* send RPC, store its message ID */
1391 r = nc_send_msg(session, data);
1392 cur_msgid = session->msgid;
1393 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001394 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001395
1396 lyd_free(data);
1397
1398 if (r != NC_MSG_RPC) {
1399 return r;
1400 }
1401
1402 *msgid = cur_msgid;
1403 return NC_MSG_RPC;
1404}
1405
1406/* CALL HOME */
1407
1408static int
1409get_listen_socket(const char *address, uint16_t port)
1410{
1411 int sock;
1412 const int optVal = 1;
1413 const socklen_t optLen = sizeof(optVal);
1414 char is_ipv4;
1415 struct sockaddr_storage saddr;
1416
1417 struct sockaddr_in* saddr4;
1418 struct sockaddr_in6* saddr6;
1419
1420 if (!address || !port) {
1421 return -1;
1422 }
1423
1424 if (strchr(address, ':') == NULL) {
1425 is_ipv4 = 1;
1426 } else {
1427 is_ipv4 = 0;
1428 }
1429
1430 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
1431 if (sock == -1) {
1432 ERR("Could not create socket (%s)", strerror(errno));
1433 return -1;
1434 }
1435
1436 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
1437 ERR("Could not set socket SO_REUSEADDR option (%s)", strerror(errno));
1438 close(sock);
1439 return -1;
1440 }
1441
1442 /* TODO may be needed
1443 if (fcntl(sock, F_SETFD, FD_CLOEXEC) != 0) {
1444 nc_verb_error("%s: fcntl failed (%s)", __func__, strerror(errno));
1445 continue;
1446 }*/
1447
1448 bzero(&saddr, sizeof(struct sockaddr_storage));
1449 if (is_ipv4) {
1450 saddr4 = (struct sockaddr_in *)&saddr;
1451
1452 saddr4->sin_family = AF_INET;
1453 saddr4->sin_port = htons(port);
1454
1455 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
1456 ERR("Failed to convert \"%s\" to IPv4 address.", address);
1457 close(sock);
1458 return -1;
1459 }
1460
1461 if (bind(sock, (struct sockaddr*)saddr4, sizeof(struct sockaddr_in)) == -1) {
1462 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
1463 close(sock);
1464 return -1;
1465 }
1466
1467 } else {
1468 saddr6 = (struct sockaddr_in6 *)&saddr;
1469
1470 saddr6->sin6_family = AF_INET6;
1471 saddr6->sin6_port = htons(port);
1472
1473 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
1474 ERR("Failed to convert \"%s\" to IPv6 address.", address);
1475 close(sock);
1476 return -1;
1477 }
1478
1479 if (bind(sock, (struct sockaddr*)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001480 ERR("Could not bind \"%s\" port %d (%s)", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +01001481 close(sock);
1482 return -1;
1483 }
1484 }
1485
1486 if (listen(sock, NC_REVERSE_QUEUE)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001487 ERR("Unable to start listening on \"%s\" port %d (%s)", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +01001488 close(sock);
1489 return -1;
1490 }
1491
1492 return sock;
1493}
1494
1495int
Michal Vaskod083db62016-01-19 10:31:29 +01001496nc_callhome_accept_connection(uint16_t port, int timeout, uint16_t *server_port, char **server_host)
Michal Vasko086311b2016-01-08 09:53:11 +01001497{
1498 struct pollfd reverse_listen_socket = {-1, POLLIN, 0};
1499 int sock;
1500 struct sockaddr_storage remote;
1501 socklen_t addr_size = sizeof(remote);
1502 int status;
1503
1504 reverse_listen_socket.fd = get_listen_socket("::0", port);
1505 if (reverse_listen_socket.fd == -1) {
1506 goto fail;
1507 }
1508
1509 reverse_listen_socket.revents = 0;
1510 while (1) {
1511 DBG("Waiting %ums for incoming Call Home connections.", timeout);
1512 status = poll(&reverse_listen_socket, 1, timeout);
1513
1514 if (status == 0) {
1515 /* timeout */
1516 ERR("Timeout for Call Home listen expired.");
1517 goto fail;
1518 } else if ((status == -1) && (errno == EINTR)) {
1519 /* poll was interrupted - try it again */
1520 continue;
1521 } else if (status < 0) {
1522 /* poll failed - something wrong happened */
1523 ERR("Call Home poll failed (%s).", strerror(errno));
1524 goto fail;
1525 } else if (status > 0) {
1526 if (reverse_listen_socket.revents & (POLLHUP | POLLERR)) {
1527 /* close pipe/fd - other side already did it */
1528 ERR("Call Home listening socket was closed.");
1529 goto fail;
1530 } else if (reverse_listen_socket.revents & POLLIN) {
1531 /* accept call home */
1532 sock = accept(reverse_listen_socket.fd, (struct sockaddr *)&remote, &addr_size);
1533 break;
1534 }
1535 }
1536 }
1537
1538 /* we accepted a connection, that's it */
1539 close(reverse_listen_socket.fd);
1540
1541 /* fill some server info, if interested */
1542 if (remote.ss_family == AF_INET) {
1543 struct sockaddr_in *remote_in = (struct sockaddr_in *)&remote;
1544 if (server_port) {
1545 *server_port = ntohs(remote_in->sin_port);
1546 }
1547 if (server_host) {
1548 *server_host = malloc(INET6_ADDRSTRLEN);
1549 inet_ntop(AF_INET, &(remote_in->sin_addr), *server_host, INET6_ADDRSTRLEN);
1550 }
1551 } else if (remote.ss_family == AF_INET6) {
1552 struct sockaddr_in6 *remote_in = (struct sockaddr_in6 *)&remote;
1553 if (server_port) {
1554 *server_port = ntohs(remote_in->sin6_port);
1555 }
1556 if (server_host) {
1557 *server_host = malloc(INET6_ADDRSTRLEN);
1558 inet_ntop(AF_INET6, &(remote_in->sin6_addr), *server_host, INET6_ADDRSTRLEN);
1559 }
1560 }
1561
1562 return sock;
1563
1564fail:
1565 if (reverse_listen_socket.fd != -1) {
1566 close(reverse_listen_socket.fd);
1567 }
1568
1569 return -1;
1570}