blob: 009b255a1e94dadea7c250883b1af5f527e8203f [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 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <assert.h>
16#include <errno.h>
17#include <fcntl.h>
18#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010019#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <arpa/inet.h>
28#include <poll.h>
29
30#include <libyang/libyang.h>
31
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010033#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010034#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010035
Michal Vasko80ef5d22016-01-18 09:21:02 +010036static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
37
Michal Vaskodaf9a092016-02-09 10:42:05 +010038struct nc_client_opts client_opts;
Michal Vasko086311b2016-01-08 09:53:11 +010039
40API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +010041nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +010042{
Michal Vasko3031aae2016-01-27 16:07:18 +010043 if (client_opts.schema_searchpath) {
44 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +010045 }
Michal Vasko086311b2016-01-08 09:53:11 +010046
Michal Vasko7f1c78b2016-01-19 09:52:14 +010047 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +010048 client_opts.schema_searchpath = strdup(path);
49 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +010050 ERRMEM;
51 return 1;
52 }
53 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +010054 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +010055 }
56
57 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +010058}
59
Michal Vasko7f1c0ef2016-03-11 11:13:06 +010060API const char *
61nc_client_get_schema_searchpath(void)
62{
63 return client_opts.schema_searchpath;
64}
65
Michal Vasko3031aae2016-01-27 16:07:18 +010066/* SCHEMAS_DIR not used (implicitly) */
Michal Vasko086311b2016-01-08 09:53:11 +010067static int
Michal Vaskoeee99412016-11-21 10:19:43 +010068ctx_check_and_load_model(struct nc_session *session, const char *module_cpblt)
Michal Vasko086311b2016-01-08 09:53:11 +010069{
70 const struct lys_module *module;
71 char *ptr, *ptr2;
72 char *model_name, *revision = NULL, *features = NULL;
73
Michal Vaskoeee99412016-11-21 10:19:43 +010074 assert(!strncmp(module_cpblt, "module=", 7));
75
76 ptr = (char *)module_cpblt + 7;
Michal Vasko086311b2016-01-08 09:53:11 +010077 ptr2 = strchr(ptr, '&');
78 if (!ptr2) {
79 ptr2 = ptr + strlen(ptr);
80 }
81 model_name = strndup(ptr, ptr2 - ptr);
82
83 /* parse revision */
Michal Vaskoeee99412016-11-21 10:19:43 +010084 ptr = strstr(module_cpblt, "revision=");
Michal Vasko086311b2016-01-08 09:53:11 +010085 if (ptr) {
86 ptr += 9;
87 ptr2 = strchr(ptr, '&');
88 if (!ptr2) {
89 ptr2 = ptr + strlen(ptr);
90 }
91 revision = strndup(ptr, ptr2 - ptr);
92 }
93
94 /* load module if needed */
95 module = ly_ctx_get_module(session->ctx, model_name, revision);
96 if (!module) {
97 module = ly_ctx_load_module(session->ctx, model_name, revision);
98 }
99
Michal Vasko086311b2016-01-08 09:53:11 +0100100 free(revision);
101 if (!module) {
Michal Vaskoef578332016-01-25 13:20:09 +0100102 WRN("Failed to load model \"%s\".", model_name);
103 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100104 return 1;
105 }
Michal Vaskoef578332016-01-25 13:20:09 +0100106 free(model_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100107
108 /* parse features */
Michal Vaskoeee99412016-11-21 10:19:43 +0100109 ptr = strstr(module_cpblt, "features=");
Michal Vasko086311b2016-01-08 09:53:11 +0100110 if (ptr) {
111 ptr += 9;
112 ptr2 = strchr(ptr, '&');
113 if (!ptr2) {
114 ptr2 = ptr + strlen(ptr);
115 }
116 features = strndup(ptr, ptr2 - ptr);
117 }
118
119 /* enable features */
120 if (features) {
121 /* basically manual strtok_r (to avoid macro) */
122 ptr2 = features;
123 for (ptr = features; *ptr; ++ptr) {
124 if (*ptr == ',') {
125 *ptr = '\0';
126 /* remember last feature */
127 ptr2 = ptr + 1;
128 }
129 }
130
131 ptr = features;
132 lys_features_enable(module, ptr);
133 while (ptr != ptr2) {
134 ptr += strlen(ptr) + 1;
135 lys_features_enable(module, ptr);
136 }
137
138 free(features);
139 }
140
141 return 0;
142}
143
Michal Vasko1aaa6602016-02-09 11:04:33 +0100144/* SCHEMAS_DIR used as the last resort */
Michal Vasko086311b2016-01-08 09:53:11 +0100145static int
Michal Vasko1aaa6602016-02-09 11:04:33 +0100146ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts)
Michal Vasko086311b2016-01-08 09:53:11 +0100147{
148 int i;
149 const struct lys_module *ietfnc;
150
151 ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
152 if (!ietfnc) {
Michal Vasko1aaa6602016-02-09 11:04:33 +0100153 ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL);
154 if (!ietfnc) {
Michal Vasko086311b2016-01-08 09:53:11 +0100155 ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
Michal Vasko086311b2016-01-08 09:53:11 +0100156 }
157 }
158 if (!ietfnc) {
159 ERR("Loading base NETCONF schema failed.");
160 return 1;
161 }
162
163 /* set supported capabilities from ietf-netconf */
164 for (i = 0; cpblts[i]; ++i) {
165 if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) {
166 if (!strncmp(cpblts[i] + 35, "writable-running", 16)) {
167 lys_features_enable(ietfnc, "writable-running");
168 } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) {
169 lys_features_enable(ietfnc, "candidate");
170 } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) {
171 lys_features_enable(ietfnc, "confirmed-commit");
172 } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) {
173 lys_features_enable(ietfnc, "rollback-on-error");
174 } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) {
175 lys_features_enable(ietfnc, "validate");
176 } else if (!strncmp(cpblts[i] + 35, "startup", 7)) {
177 lys_features_enable(ietfnc, "startup");
178 } else if (!strncmp(cpblts[i] + 35, "url", 3)) {
179 lys_features_enable(ietfnc, "url");
180 } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) {
181 lys_features_enable(ietfnc, "xpath");
182 }
183 }
184 }
185
186 return 0;
187}
188
189static char *
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200190libyang_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
191 void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data))
Michal Vasko086311b2016-01-08 09:53:11 +0100192{
193 struct nc_session *session = (struct nc_session *)user_data;
194 struct nc_rpc *rpc;
195 struct nc_reply *reply;
196 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200197 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200198 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100199 NC_MSG_TYPE msg;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200200 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100201 uint64_t msgid;
202
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200203 if (submod_name) {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200204 rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200205 } else {
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200206 rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST);
Michal Vaskod5ad5f72016-07-25 16:17:46 +0200207 }
Michal Vasko086311b2016-01-08 09:53:11 +0100208 *format = LYS_IN_YIN;
209
210 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
211 usleep(1000);
212 }
213 if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100214 ERR("Session %u: failed to send the <get-schema> RPC.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100215 nc_rpc_free(rpc);
216 return NULL;
217 }
218
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200219 do {
220 msg = nc_recv_reply(session, rpc, msgid, 1000, 0, &reply);
221 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100222 nc_rpc_free(rpc);
223 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100225 return NULL;
226 } else if (msg == NC_MSG_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100227 ERR("Session %u: failed to receive a reply to <get-schema>.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100228 return NULL;
229 }
230
Michal Vasko998ba412016-09-16 12:00:07 +0200231 switch (reply->type) {
232 case NC_RPL_OK:
233 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id);
234 nc_reply_free(reply);
235 return NULL;
236 case NC_RPL_DATA:
237 /* fine */
238 break;
239 case NC_RPL_ERROR:
240 error_rpl = (struct nc_reply_error *)reply;
241 if (error_rpl->count) {
242 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
243 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
244 } else {
245 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id);
246 }
247 nc_reply_free(reply);
248 return NULL;
249 case NC_RPL_NOTIF:
250 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100251 nc_reply_free(reply);
252 return NULL;
253 }
254
Michal Vasko086311b2016-01-08 09:53:11 +0100255 data_rpl = (struct nc_reply_data *)reply;
Michal Vaskob2583f12016-05-12 11:40:23 +0200256 if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
257 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
258 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id);
259 nc_reply_free(reply);
260 return NULL;
261 }
Radek Krejci539efb62016-08-24 15:05:16 +0200262 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
263 switch (get_schema_data->value_type) {
264 case LYD_ANYDATA_CONSTSTRING:
265 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200266 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200267 break;
268 case LYD_ANYDATA_DATATREE:
269 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
270 break;
271 case LYD_ANYDATA_XML:
272 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
273 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200274 case LYD_ANYDATA_JSON:
275 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200276 case LYD_ANYDATA_SXML:
277 case LYD_ANYDATA_SXMLD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200278 ERRINT;
279 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200280 }
Michal Vasko086311b2016-01-08 09:53:11 +0100281 nc_reply_free(reply);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100282 *free_model_data = free;
Michal Vasko3e7eaf42016-09-20 10:51:23 +0200283 *format = LYS_IN_YANG;
Michal Vasko086311b2016-01-08 09:53:11 +0100284
Michal Vasko086311b2016-01-08 09:53:11 +0100285 return model_data;
286}
287
288int
289nc_ctx_check_and_fill(struct nc_session *session)
290{
Michal Vaskoeee99412016-11-21 10:19:43 +0100291 const char *module_cpblt;
Michal Vaskoef578332016-01-25 13:20:09 +0100292 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100293 ly_module_clb old_clb = NULL;
294 void *old_data = NULL;
295
Michal Vasko2e6defd2016-10-07 15:48:15 +0200296 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100297
298 /* check if get-schema is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200299 for (i = 0; session->opts.client.cpblts[i]; ++i) {
300 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100301 get_schema_support = 1;
302 break;
303 }
304 }
305
306 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
307 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
308 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
309 /* set module retrieval using <get-schema> */
310 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100311 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100312 } else {
313 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
314 }
315 }
316
317 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200318 if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100319 if (old_clb) {
320 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
321 }
Michal Vaskoef578332016-01-25 13:20:09 +0100322 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100323 }
324
325 /* load all other models */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200326 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Michal Vaskoeee99412016-11-21 10:19:43 +0100327 module_cpblt = strstr(session->opts.client.cpblts[i], "module=");
328 /* this capability requires a module */
329 if (module_cpblt) {
330 r = ctx_check_and_load_model(session, module_cpblt);
331 if (r == -1) {
332 ret = -1;
333 break;
Michal Vaskoef578332016-01-25 13:20:09 +0100334 }
335
Michal Vaskoeee99412016-11-21 10:19:43 +0100336 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
337 * if it was using get-schema */
338 if (r == 1) {
339 if (get_schema_support) {
340 VRB("Trying to load the schema from a different source.");
341 /* works even if old_clb is NULL */
342 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
343 r = ctx_check_and_load_model(session, module_cpblt);
344 }
Michal Vaskoef578332016-01-25 13:20:09 +0100345
Michal Vaskoeee99412016-11-21 10:19:43 +0100346 /* fail again (or no other way to try), too bad */
347 if (r) {
348 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
349 }
350
351 /* set get-schema callback back */
352 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
353 }
Michal Vaskoef578332016-01-25 13:20:09 +0100354 }
Michal Vasko086311b2016-01-08 09:53:11 +0100355 }
356
357 if (old_clb) {
358 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
359 }
Michal Vaskoeee99412016-11-21 10:19:43 +0100360 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
361 WRN("Some models failed to be loaded, any data from these models (and any other unknown) will be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +0100362 }
363 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100364}
365
366API struct nc_session *
367nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
368{
Michal Vaskod083db62016-01-19 10:31:29 +0100369 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100370
Michal Vasko45e53ae2016-04-07 11:46:03 +0200371 if (fdin < 0) {
372 ERRARG("fdin");
373 return NULL;
374 } else if (fdout < 0) {
375 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100376 return NULL;
377 }
378
379 /* prepare session structure */
380 session = calloc(1, sizeof *session);
381 if (!session) {
382 ERRMEM;
383 return NULL;
384 }
385 session->status = NC_STATUS_STARTING;
386 session->side = NC_CLIENT;
387
388 /* transport specific data */
389 session->ti_type = NC_TI_FD;
390 session->ti.fd.in = fdin;
391 session->ti.fd.out = fdout;
392
393 /* assign context (dicionary needed for handshake) */
394 if (!ctx) {
395 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100396 /* definitely should not happen, but be ready */
397 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
398 /* that's just it */
399 goto fail;
400 }
Michal Vasko086311b2016-01-08 09:53:11 +0100401 } else {
402 session->flags |= NC_SESSION_SHAREDCTX;
403 }
404 session->ctx = ctx;
405
406 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200407 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100408 goto fail;
409 }
410 session->status = NC_STATUS_RUNNING;
411
Michal Vaskoef578332016-01-25 13:20:09 +0100412 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100413 goto fail;
414 }
415
416 return session;
417
418fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100419 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100420 return NULL;
421}
422
423int
Michal Vaskof05562c2016-01-20 12:06:43 +0100424nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100425{
Michal Vasko0190bc32016-03-02 15:47:49 +0100426 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100427 struct addrinfo hints, *res_list, *res;
428 char port_s[6]; /* length of string representation of short int */
429
430 snprintf(port_s, 6, "%u", port);
431
432 /* Connect to a server */
433 memset(&hints, 0, sizeof hints);
434 hints.ai_family = AF_UNSPEC;
435 hints.ai_socktype = SOCK_STREAM;
436 hints.ai_protocol = IPPROTO_TCP;
437 i = getaddrinfo(host, port_s, &hints, &res_list);
438 if (i != 0) {
439 ERR("Unable to translate the host address (%s).", gai_strerror(i));
440 return -1;
441 }
442
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200443 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko086311b2016-01-08 09:53:11 +0100444 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
445 if (sock == -1) {
446 /* socket was not created, try another resource */
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200447 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100448 }
449
450 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
451 /* network connection failed, try another resource */
Michal Vasko086311b2016-01-08 09:53:11 +0100452 close(sock);
453 sock = -1;
Michal Vasko1f0b2ac2016-10-13 10:33:50 +0200454 continue;
Michal Vasko086311b2016-01-08 09:53:11 +0100455 }
456
Michal Vasko0190bc32016-03-02 15:47:49 +0100457 /* make the socket non-blocking */
458 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
459 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100460 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200461 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100462 return -1;
463 }
464
Michal Vasko086311b2016-01-08 09:53:11 +0100465 /* we're done, network connection established */
466 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100467 }
468
Michal Vasko29af44b2016-10-13 10:59:55 +0200469 if (sock != -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100470 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 +0100471 }
472 freeaddrinfo(res_list);
473
474 return sock;
475}
476
Michal Vasko086311b2016-01-08 09:53:11 +0100477static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100478get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100479{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100480 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100481 char *ptr;
482 const char *str_msgid;
483 uint64_t cur_msgid;
484 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100485 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100486 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
487
Michal vasko50cc94f2016-10-04 13:46:20 +0200488 r = nc_timedlock(session->ti_lock, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100489 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100490 /* error */
491 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100492 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100493 /* timeout */
494 return NC_MSG_WOULDBLOCK;
495 }
496
497 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200498 if (!msgid && session->opts.client.notifs) {
499 cont = session->opts.client.notifs;
500 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +0100501
Michal Vasko71ba2da2016-05-04 10:53:16 +0200502 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100503 free(cont);
504
Michal Vasko71ba2da2016-05-04 10:53:16 +0200505 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100506 }
507
508 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200509 if (msgid && session->opts.client.replies) {
510 cont = session->opts.client.replies;
511 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100512
Michal Vasko71ba2da2016-05-04 10:53:16 +0200513 xml = cont->msg;
514 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100515
Michal Vasko71ba2da2016-05-04 10:53:16 +0200516 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100517 }
518
Michal Vasko71ba2da2016-05-04 10:53:16 +0200519 if (!msgtype) {
520 /* read message from wire */
521 msgtype = nc_read_msg_poll(session, timeout, &xml);
522 }
Michal Vasko086311b2016-01-08 09:53:11 +0100523
524 /* we read rpc-reply, want a notif */
525 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200526 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +0100527 while (*cont_ptr) {
528 cont_ptr = &((*cont_ptr)->next);
529 }
530 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100531 if (!*cont_ptr) {
532 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200533 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100534 lyxml_free(session->ctx, xml);
535 return NC_MSG_ERROR;
536 }
Michal Vasko086311b2016-01-08 09:53:11 +0100537 (*cont_ptr)->msg = xml;
538 (*cont_ptr)->next = NULL;
539 }
540
541 /* we read notif, want a rpc-reply */
542 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100543 /* TODO check whether the session is even subscribed */
544 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100545 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100546 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100547 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100548 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100549 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100550
Michal Vasko2e6defd2016-10-07 15:48:15 +0200551 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +0100552 while (*cont_ptr) {
553 cont_ptr = &((*cont_ptr)->next);
554 }
555 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100556 if (!cont_ptr) {
557 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200558 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100559 lyxml_free(session->ctx, xml);
560 return NC_MSG_ERROR;
561 }
Michal Vasko086311b2016-01-08 09:53:11 +0100562 (*cont_ptr)->msg = xml;
563 (*cont_ptr)->next = NULL;
564 }
565
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100566 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100567
568 switch (msgtype) {
569 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100570 if (!msgid) {
571 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100572 }
Michal Vasko086311b2016-01-08 09:53:11 +0100573 break;
574
575 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100576 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200577 /* check message-id */
578 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
579 if (!str_msgid) {
580 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
581 msgtype = NC_MSG_REPLY_ERR_MSGID;
582 } else {
583 cur_msgid = strtoul(str_msgid, &ptr, 10);
584 if (cur_msgid != msgid) {
585 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
586 session->id, str_msgid);
587 msgtype = NC_MSG_REPLY_ERR_MSGID;
588 }
589 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100590 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100591 }
Michal Vasko086311b2016-01-08 09:53:11 +0100592 break;
593
594 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100595 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100596 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200597 msgtype = NC_MSG_ERROR;
598 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100599
600 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100601 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100602 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200603 msgtype = NC_MSG_ERROR;
604 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100605
606 default:
607 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
608 * NC_MSG_NONE is not returned by nc_read_msg()
609 */
610 break;
611 }
612
613 return msgtype;
614}
615
616/* cannot strictly fail, but does not need to fill any error parameter at all */
617static void
618parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
619{
620 struct lyxml_elem *iter, *next, *info;
621
622 LY_TREE_FOR(xml->child, iter) {
623 if (!iter->ns) {
624 if (iter->content) {
625 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
626 } else {
627 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
628 }
629 continue;
630 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
631 if (iter->content) {
632 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
633 iter->name, iter->content, iter->ns->value);
634 } else {
635 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
636 }
637 continue;
638 }
639
640 if (!strcmp(iter->name, "error-type")) {
641 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
642 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
643 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
644 } else if (err->type) {
645 WRN("<rpc-error> <error-type> duplicated.");
646 } else {
647 err->type = lydict_insert(ctx, iter->content, 0);
648 }
649 } else if (!strcmp(iter->name, "error-tag")) {
650 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
651 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
652 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
653 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
654 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
655 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
656 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
657 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
658 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
659 && strcmp(iter->content, "malformed-message"))) {
660 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
661 } else if (err->tag) {
662 WRN("<rpc-error> <error-tag> duplicated.");
663 } else {
664 err->tag = lydict_insert(ctx, iter->content, 0);
665 }
666 } else if (!strcmp(iter->name, "error-severity")) {
667 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
668 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
669 } else if (err->severity) {
670 WRN("<rpc-error> <error-severity> duplicated.");
671 } else {
672 err->severity = lydict_insert(ctx, iter->content, 0);
673 }
674 } else if (!strcmp(iter->name, "error-app-tag")) {
675 if (err->apptag) {
676 WRN("<rpc-error> <error-app-tag> duplicated.");
677 } else {
678 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
679 }
680 } else if (!strcmp(iter->name, "error-path")) {
681 if (err->path) {
682 WRN("<rpc-error> <error-path> duplicated.");
683 } else {
684 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
685 }
686 } else if (!strcmp(iter->name, "error-message")) {
687 if (err->message) {
688 WRN("<rpc-error> <error-message> duplicated.");
689 } else {
690 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
691 if (!err->message_lang) {
692 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
693 }
694 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
695 }
696 } else if (!strcmp(iter->name, "error-info")) {
697 LY_TREE_FOR_SAFE(iter->child, next, info) {
698 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
699 if (!strcmp(info->name, "session-id")) {
700 if (err->sid) {
701 WRN("<rpc-error> <error-info> <session-id> duplicated.");
702 } else {
703 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
704 }
705 } else if (!strcmp(info->name, "bad-attr")) {
706 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100707 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
708 if (!err->attr) {
709 ERRMEM;
710 return;
711 }
Michal Vasko086311b2016-01-08 09:53:11 +0100712 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
713 } else if (!strcmp(info->name, "bad-element")) {
714 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100715 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
716 if (!err->elem) {
717 ERRMEM;
718 return;
719 }
Michal Vasko086311b2016-01-08 09:53:11 +0100720 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
721 } else if (!strcmp(info->name, "bad-namespace")) {
722 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100723 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
724 if (!err->ns) {
725 ERRMEM;
726 return;
727 }
Michal Vasko086311b2016-01-08 09:53:11 +0100728 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
729 } else {
730 if (info->content) {
731 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
732 info->name, info->content);
733 } else {
734 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
735 }
736 }
737 } else {
738 lyxml_unlink(ctx, info);
739 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100740 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
741 if (!err->other) {
742 ERRMEM;
743 return;
744 }
Michal Vasko086311b2016-01-08 09:53:11 +0100745 err->other[err->other_count - 1] = info;
746 }
747 }
748 } else {
749 if (iter->content) {
750 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
751 } else {
752 WRN("<rpc-error> unknown child \"%s\".", iter->name);
753 }
754 }
755 }
756}
757
758static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100759parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100760{
761 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +0200762 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100763 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100764 struct nc_reply_data *data_rpl;
765 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200766 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100767 int i;
768
769 if (!xml->child) {
770 ERR("An empty <rpc-reply>.");
771 return NULL;
772 }
773
774 /* rpc-error */
775 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
776 /* count and check elements */
777 i = 0;
778 LY_TREE_FOR(xml->child, iter) {
779 if (strcmp(iter->name, "rpc-error")) {
780 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
781 return NULL;
782 } else if (!iter->ns) {
783 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
784 return NULL;
785 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
786 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
787 return NULL;
788 }
789 ++i;
790 }
791
792 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100793 if (!error_rpl) {
794 ERRMEM;
795 return NULL;
796 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100797 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100798 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100799 if (!error_rpl->err) {
800 ERRMEM;
801 free(error_rpl);
802 return NULL;
803 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100804 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100805 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100806 reply = (struct nc_reply *)error_rpl;
807
808 i = 0;
809 LY_TREE_FOR(xml->child, iter) {
810 parse_rpc_error(ctx, iter, error_rpl->err + i);
811 ++i;
812 }
813
814 /* ok */
815 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
816 if (xml->child->next) {
817 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
818 return NULL;
819 }
820 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100821 if (!reply) {
822 ERRMEM;
823 return NULL;
824 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100825 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100826
827 /* some RPC output */
828 } else {
829 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200830 case NC_RPC_ACT_GENERIC:
831 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100832
833 if (rpc_gen->has_data) {
Michal Vaskoe1708602016-10-18 12:17:22 +0200834 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100835 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +0100836 rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
Michal Vaskoe1708602016-10-18 12:17:22 +0200837 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200838 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100839 return NULL;
840 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200841 }
Michal Vasko086311b2016-01-08 09:53:11 +0100842 break;
843
844 case NC_RPC_GETCONFIG:
845 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100846 if (!xml->child->child) {
847 /* we did not receive any data */
848 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100849 if (!data_rpl) {
850 ERRMEM;
851 return NULL;
852 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100853 data_rpl->type = NC_RPL_DATA;
854 data_rpl->data = NULL;
855 return (struct nc_reply *)data_rpl;
856 }
857
Michal Vasko086311b2016-01-08 09:53:11 +0100858 /* special treatment */
Michal Vasko0a5ae9a2016-11-15 11:54:47 +0100859 data = lyd_parse_xml(ctx, &xml->child->child,
860 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +0100861 | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100862 if (!data) {
863 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
864 return NULL;
865 }
866 break;
867
868 case NC_RPC_GETSCHEMA:
Michal Vaskoe1708602016-10-18 12:17:22 +0200869 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL), "get-schema");
870 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100871 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100872 return NULL;
873 }
874 break;
875
876 case NC_RPC_EDIT:
877 case NC_RPC_COPY:
878 case NC_RPC_DELETE:
879 case NC_RPC_LOCK:
880 case NC_RPC_UNLOCK:
881 case NC_RPC_KILL:
882 case NC_RPC_COMMIT:
883 case NC_RPC_DISCARD:
884 case NC_RPC_CANCEL:
885 case NC_RPC_VALIDATE:
886 case NC_RPC_SUBSCRIBE:
887 /* there is no output defined */
888 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
889 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100890 default:
891 ERRINT;
892 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100893 }
894
895 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100896 if (!data_rpl) {
897 ERRMEM;
898 return NULL;
899 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100900 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100901 if (!data) {
Michal Vasko0a5ae9a2016-11-15 11:54:47 +0100902 data_rpl->data = lyd_parse_xml(ctx, &xml->child,
Michal Vaskoeee99412016-11-21 10:19:43 +0100903 LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions, rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100904 } else {
905 /* <get>, <get-config> */
906 data_rpl->data = data;
907 }
Michal Vaskoe1708602016-10-18 12:17:22 +0200908 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +0100909 if (!data_rpl->data) {
910 ERR("Failed to parse <rpc-reply>.");
911 free(data_rpl);
912 return NULL;
913 }
914 reply = (struct nc_reply *)data_rpl;
915 }
916
917 return reply;
918}
919
Radek Krejci53691be2016-02-22 13:58:37 +0100920#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100921
Michal Vasko3031aae2016-01-27 16:07:18 +0100922int
923nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
924{
925 int sock;
926
Michal Vasko45e53ae2016-04-07 11:46:03 +0200927 if (!address) {
928 ERRARG("address");
929 return -1;
930 } else if (!port) {
931 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100932 return -1;
933 }
934
935 sock = nc_sock_listen(address, port);
936 if (sock == -1) {
937 return -1;
938 }
939
940 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100941 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
942 if (!client_opts.ch_binds) {
943 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100944 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100945 return -1;
946 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100947
Michal Vasko2e6defd2016-10-07 15:48:15 +0200948 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
949 if (!client_opts.ch_bind_ti) {
950 ERRMEM;
951 close(sock);
952 return -1;
953 }
954 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
955
Michal Vasko3031aae2016-01-27 16:07:18 +0100956 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100957 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
958 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100959 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100960 return -1;
961 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100962 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
963 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +0200964 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +0100965
966 return 0;
967}
968
969int
970nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
971{
972 uint32_t i;
973 int ret = -1;
974
975 if (!address && !port && !ti) {
976 for (i = 0; i < client_opts.ch_bind_count; ++i) {
977 close(client_opts.ch_binds[i].sock);
978 free((char *)client_opts.ch_binds[i].address);
979
980 ret = 0;
981 }
982 free(client_opts.ch_binds);
983 client_opts.ch_binds = NULL;
984 client_opts.ch_bind_count = 0;
985 } else {
986 for (i = 0; i < client_opts.ch_bind_count; ++i) {
987 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
988 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +0200989 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100990 close(client_opts.ch_binds[i].sock);
991 free((char *)client_opts.ch_binds[i].address);
992
993 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +0200994 if (!client_opts.ch_bind_count) {
995 free(client_opts.ch_binds);
996 client_opts.ch_binds = NULL;
997 } else if (i < client_opts.ch_bind_count) {
998 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
999 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1000 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001001
1002 ret = 0;
1003 }
1004 }
1005 }
1006
1007 return ret;
1008}
1009
1010API int
1011nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1012{
1013 int sock;
1014 char *host = NULL;
1015 uint16_t port, idx;
1016
Michal Vasko45e53ae2016-04-07 11:46:03 +02001017 if (!client_opts.ch_binds) {
1018 ERRINIT;
1019 return -1;
1020 } else if (!session) {
1021 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001022 return -1;
1023 }
1024
1025 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1026
Michal Vasko50456e82016-02-02 12:16:08 +01001027 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001028 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001029 return sock;
1030 }
1031
Radek Krejci53691be2016-02-22 13:58:37 +01001032#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001033 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001034 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001035 } else
1036#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001037#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001038 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001039 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001040 } else
1041#endif
1042 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001043 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001044 *session = NULL;
1045 }
1046
1047 free(host);
1048
1049 if (!(*session)) {
1050 return -1;
1051 }
1052
1053 return 1;
1054}
1055
Radek Krejci53691be2016-02-22 13:58:37 +01001056#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001057
Michal Vaskobdfb5242016-05-24 09:11:01 +02001058API const char **
1059nc_session_get_cpblts(const struct nc_session *session)
1060{
1061 if (!session) {
1062 ERRARG("session");
1063 return NULL;
1064 }
1065
Michal Vasko2e6defd2016-10-07 15:48:15 +02001066 return session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001067}
1068
1069API const char *
1070nc_session_cpblt(const struct nc_session *session, const char *capab)
1071{
1072 int i, len;
1073
1074 if (!session) {
1075 ERRARG("session");
1076 return NULL;
1077 } else if (!capab) {
1078 ERRARG("capab");
1079 return NULL;
1080 }
1081
1082 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001083 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1084 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1085 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001086 }
1087 }
1088
1089 return NULL;
1090}
1091
Michal Vasko9cd26a82016-05-31 08:58:48 +02001092API int
1093nc_session_ntf_thread_running(const struct nc_session *session)
1094{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001095 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001096 ERRARG("session");
1097 return 0;
1098 }
1099
Michal Vasko2e6defd2016-10-07 15:48:15 +02001100 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001101}
1102
Michal Vaskob7558c52016-02-26 15:04:19 +01001103API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001104nc_client_init(void)
1105{
1106 nc_init();
1107}
1108
1109API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001110nc_client_destroy(void)
1111{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001112 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001113#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1114 nc_client_ch_del_bind(NULL, 0, 0);
1115#endif
1116#ifdef NC_ENABLED_SSH
1117 nc_client_ssh_destroy_opts();
1118#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001119#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001120 nc_client_tls_destroy_opts();
1121#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001122 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001123}
1124
Michal Vasko086311b2016-01-08 09:53:11 +01001125API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001126nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, int parseroptions, struct nc_reply **reply)
Michal Vasko086311b2016-01-08 09:53:11 +01001127{
1128 struct lyxml_elem *xml;
1129 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1130
Michal Vasko45e53ae2016-04-07 11:46:03 +02001131 if (!session) {
1132 ERRARG("session");
1133 return NC_MSG_ERROR;
1134 } else if (!rpc) {
1135 ERRARG("rpc");
1136 return NC_MSG_ERROR;
1137 } else if (!reply) {
1138 ERRARG("reply");
1139 return NC_MSG_ERROR;
1140 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1141 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001142 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001143 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001144 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001145 return NC_MSG_ERROR;
1146 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001147 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01001148 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1149 parseroptions &= LYD_OPT_STRICT;
1150 }
Michal Vasko086311b2016-01-08 09:53:11 +01001151 *reply = NULL;
1152
1153 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001154
Michal Vasko71ba2da2016-05-04 10:53:16 +02001155 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01001156 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001157 lyxml_free(session->ctx, xml);
1158 if (!(*reply)) {
1159 return NC_MSG_ERROR;
1160 }
1161 }
1162
1163 return msgtype;
1164}
1165
1166API NC_MSG_TYPE
1167nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1168{
1169 struct lyxml_elem *xml, *ev_time;
1170 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1171
Michal Vasko45e53ae2016-04-07 11:46:03 +02001172 if (!session) {
1173 ERRARG("session");
1174 return NC_MSG_ERROR;
1175 } else if (!notif) {
1176 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001177 return NC_MSG_ERROR;
1178 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001179 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001180 return NC_MSG_ERROR;
1181 }
1182
1183 msgtype = get_msg(session, timeout, 0, &xml);
1184
1185 if (msgtype == NC_MSG_NOTIF) {
1186 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001187 if (!*notif) {
1188 ERRMEM;
1189 lyxml_free(session->ctx, xml);
1190 return NC_MSG_ERROR;
1191 }
Michal Vasko086311b2016-01-08 09:53:11 +01001192
1193 /* eventTime */
1194 LY_TREE_FOR(xml->child, ev_time) {
1195 if (!strcmp(ev_time->name, "eventTime")) {
1196 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1197 /* lyd_parse does not know this element */
1198 lyxml_free(session->ctx, ev_time);
1199 break;
1200 }
1201 }
1202 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001203 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001204 goto fail;
1205 }
1206
1207 /* notification body */
Michal Vaskoeee99412016-11-21 10:19:43 +01001208 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT
1209 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001210 lyxml_free(session->ctx, xml);
1211 xml = NULL;
1212 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001213 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001214 goto fail;
1215 }
1216 }
1217
1218 return msgtype;
1219
1220fail:
1221 lydict_remove(session->ctx, (*notif)->datetime);
1222 lyd_free((*notif)->tree);
1223 free(*notif);
1224 *notif = NULL;
1225 lyxml_free(session->ctx, xml);
1226
1227 return NC_MSG_ERROR;
1228}
1229
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001230static void *
1231nc_recv_notif_thread(void *arg)
1232{
1233 struct nc_ntf_thread_arg *ntarg;
1234 struct nc_session *session;
1235 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1236 struct nc_notif *notif;
1237 NC_MSG_TYPE msgtype;
1238
1239 ntarg = (struct nc_ntf_thread_arg *)arg;
1240 session = ntarg->session;
1241 notif_clb = ntarg->notif_clb;
1242 free(ntarg);
1243
Michal Vasko2e6defd2016-10-07 15:48:15 +02001244 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001245 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001246 if (msgtype == NC_MSG_NOTIF) {
1247 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001248 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1249 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1250 nc_notif_free(notif);
1251 break;
1252 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001253 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001254 } else if (msgtype == NC_MSG_ERROR) {
1255 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001256 }
1257
1258 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1259 }
1260
Michal Vasko0651c902016-05-19 15:55:42 +02001261 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001262 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001263 return NULL;
1264}
1265
1266API int
1267nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1268{
1269 struct nc_ntf_thread_arg *ntarg;
1270 int ret;
1271
Michal Vasko45e53ae2016-04-07 11:46:03 +02001272 if (!session) {
1273 ERRARG("session");
1274 return -1;
1275 } else if (!notif_clb) {
1276 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001277 return -1;
1278 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1279 ERR("Session %u: invalid session to receive Notifications.", session->id);
1280 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001281 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001282 ERR("Session %u: separate notification thread is already running.", session->id);
1283 return -1;
1284 }
1285
1286 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001287 if (!ntarg) {
1288 ERRMEM;
1289 return -1;
1290 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001291 ntarg->session = session;
1292 ntarg->notif_clb = notif_clb;
1293
1294 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001295 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
1296 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001297 ERRMEM;
1298 free(ntarg);
1299 return -1;
1300 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001301
Michal Vasko2e6defd2016-10-07 15:48:15 +02001302 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001303 if (ret) {
1304 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1305 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001306 free((pthread_t *)session->opts.client.ntf_tid);
1307 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001308 return -1;
1309 }
1310
1311 return 0;
1312}
1313
Michal Vasko086311b2016-01-08 09:53:11 +01001314API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001315nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001316{
1317 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001318 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001319 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001320 struct nc_rpc_getconfig *rpc_gc;
1321 struct nc_rpc_edit *rpc_e;
1322 struct nc_rpc_copy *rpc_cp;
1323 struct nc_rpc_delete *rpc_del;
1324 struct nc_rpc_lock *rpc_lock;
1325 struct nc_rpc_get *rpc_g;
1326 struct nc_rpc_kill *rpc_k;
1327 struct nc_rpc_commit *rpc_com;
1328 struct nc_rpc_cancel *rpc_can;
1329 struct nc_rpc_validate *rpc_val;
1330 struct nc_rpc_getschema *rpc_gs;
1331 struct nc_rpc_subscribe *rpc_sub;
1332 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001333 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001334 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001335 uint64_t cur_msgid;
1336
Michal Vasko45e53ae2016-04-07 11:46:03 +02001337 if (!session) {
1338 ERRARG("session");
1339 return NC_MSG_ERROR;
1340 } else if (!rpc) {
1341 ERRARG("rpc");
1342 return NC_MSG_ERROR;
1343 } else if (!msgid) {
1344 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001345 return NC_MSG_ERROR;
1346 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001347 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001348 return NC_MSG_ERROR;
1349 }
1350
Michal Vasko90e8e692016-07-13 12:27:57 +02001351 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001352 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1353 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001354 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001355 return NC_MSG_ERROR;
1356 }
1357 }
1358
1359 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001360 case NC_RPC_ACT_GENERIC:
1361 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001362
1363 if (rpc_gen->has_data) {
1364 data = rpc_gen->content.data;
1365 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001366 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC
1367 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001368 }
1369 break;
1370
1371 case NC_RPC_GETCONFIG:
1372 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1373
1374 data = lyd_new(NULL, ietfnc, "get-config");
1375 node = lyd_new(data, ietfnc, "source");
1376 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1377 if (!node) {
1378 lyd_free(data);
1379 return NC_MSG_ERROR;
1380 }
1381 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001382 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001383 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001384 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001385 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001386 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001387 lyd_insert_attr(node, NULL, "type", "xpath");
1388 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001389 }
1390 if (!node) {
1391 lyd_free(data);
1392 return NC_MSG_ERROR;
1393 }
1394 }
1395
1396 if (rpc_gc->wd_mode) {
1397 if (!ietfncwd) {
1398 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1399 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001400 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001401 return NC_MSG_ERROR;
1402 }
1403 }
1404 switch (rpc_gc->wd_mode) {
1405 case NC_WD_UNKNOWN:
1406 /* cannot get here */
1407 break;
1408 case NC_WD_ALL:
1409 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1410 break;
1411 case NC_WD_ALL_TAG:
1412 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1413 break;
1414 case NC_WD_TRIM:
1415 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1416 break;
1417 case NC_WD_EXPLICIT:
1418 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1419 break;
1420 }
1421 if (!node) {
1422 lyd_free(data);
1423 return NC_MSG_ERROR;
1424 }
1425 }
1426 break;
1427
1428 case NC_RPC_EDIT:
1429 rpc_e = (struct nc_rpc_edit *)rpc;
1430
1431 data = lyd_new(NULL, ietfnc, "edit-config");
1432 node = lyd_new(data, ietfnc, "target");
1433 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1434 if (!node) {
1435 lyd_free(data);
1436 return NC_MSG_ERROR;
1437 }
1438
1439 if (rpc_e->default_op) {
1440 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1441 if (!node) {
1442 lyd_free(data);
1443 return NC_MSG_ERROR;
1444 }
1445 }
1446
1447 if (rpc_e->test_opt) {
1448 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1449 if (!node) {
1450 lyd_free(data);
1451 return NC_MSG_ERROR;
1452 }
1453 }
1454
1455 if (rpc_e->error_opt) {
1456 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1457 if (!node) {
1458 lyd_free(data);
1459 return NC_MSG_ERROR;
1460 }
1461 }
1462
Michal Vasko7793bc62016-09-16 11:58:41 +02001463 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001464 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001465 } else {
1466 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1467 }
1468 if (!node) {
1469 lyd_free(data);
1470 return NC_MSG_ERROR;
1471 }
1472 break;
1473
1474 case NC_RPC_COPY:
1475 rpc_cp = (struct nc_rpc_copy *)rpc;
1476
1477 data = lyd_new(NULL, ietfnc, "copy-config");
1478 node = lyd_new(data, ietfnc, "target");
1479 if (rpc_cp->url_trg) {
1480 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1481 } else {
1482 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1483 }
1484 if (!node) {
1485 lyd_free(data);
1486 return NC_MSG_ERROR;
1487 }
1488
1489 node = lyd_new(data, ietfnc, "source");
1490 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001491 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001492 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001493 } else {
1494 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1495 }
1496 } else {
1497 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1498 }
1499 if (!node) {
1500 lyd_free(data);
1501 return NC_MSG_ERROR;
1502 }
1503
1504 if (rpc_cp->wd_mode) {
1505 if (!ietfncwd) {
1506 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1507 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001508 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001509 return NC_MSG_ERROR;
1510 }
1511 }
1512 switch (rpc_cp->wd_mode) {
1513 case NC_WD_UNKNOWN:
1514 /* cannot get here */
1515 break;
1516 case NC_WD_ALL:
1517 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1518 break;
1519 case NC_WD_ALL_TAG:
1520 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1521 break;
1522 case NC_WD_TRIM:
1523 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1524 break;
1525 case NC_WD_EXPLICIT:
1526 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1527 break;
1528 }
1529 if (!node) {
1530 lyd_free(data);
1531 return NC_MSG_ERROR;
1532 }
1533 }
1534 break;
1535
1536 case NC_RPC_DELETE:
1537 rpc_del = (struct nc_rpc_delete *)rpc;
1538
1539 data = lyd_new(NULL, ietfnc, "delete-config");
1540 node = lyd_new(data, ietfnc, "target");
1541 if (rpc_del->url) {
1542 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1543 } else {
1544 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1545 }
1546 if (!node) {
1547 lyd_free(data);
1548 return NC_MSG_ERROR;
1549 }
1550 break;
1551
1552 case NC_RPC_LOCK:
1553 rpc_lock = (struct nc_rpc_lock *)rpc;
1554
1555 data = lyd_new(NULL, ietfnc, "lock");
1556 node = lyd_new(data, ietfnc, "target");
1557 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1558 if (!node) {
1559 lyd_free(data);
1560 return NC_MSG_ERROR;
1561 }
1562 break;
1563
1564 case NC_RPC_UNLOCK:
1565 rpc_lock = (struct nc_rpc_lock *)rpc;
1566
1567 data = lyd_new(NULL, ietfnc, "unlock");
1568 node = lyd_new(data, ietfnc, "target");
1569 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1570 if (!node) {
1571 lyd_free(data);
1572 return NC_MSG_ERROR;
1573 }
1574 break;
1575
1576 case NC_RPC_GET:
1577 rpc_g = (struct nc_rpc_get *)rpc;
1578
1579 data = lyd_new(NULL, ietfnc, "get");
1580 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001581 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001582 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001583 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001584 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001585 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001586 lyd_insert_attr(node, NULL, "type", "xpath");
1587 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001588 }
1589 if (!node) {
1590 lyd_free(data);
1591 return NC_MSG_ERROR;
1592 }
1593 }
1594
1595 if (rpc_g->wd_mode) {
1596 if (!ietfncwd) {
1597 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1598 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001599 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001600 return NC_MSG_ERROR;
1601 }
1602 }
1603 switch (rpc_g->wd_mode) {
1604 case NC_WD_UNKNOWN:
1605 /* cannot get here */
1606 break;
1607 case NC_WD_ALL:
1608 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1609 break;
1610 case NC_WD_ALL_TAG:
1611 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1612 break;
1613 case NC_WD_TRIM:
1614 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1615 break;
1616 case NC_WD_EXPLICIT:
1617 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1618 break;
1619 }
1620 if (!node) {
1621 lyd_free(data);
1622 return NC_MSG_ERROR;
1623 }
1624 }
1625 break;
1626
1627 case NC_RPC_KILL:
1628 rpc_k = (struct nc_rpc_kill *)rpc;
1629
1630 data = lyd_new(NULL, ietfnc, "kill-session");
1631 sprintf(str, "%u", rpc_k->sid);
1632 lyd_new_leaf(data, ietfnc, "session-id", str);
1633 break;
1634
1635 case NC_RPC_COMMIT:
1636 rpc_com = (struct nc_rpc_commit *)rpc;
1637
1638 data = lyd_new(NULL, ietfnc, "commit");
1639 if (rpc_com->confirmed) {
1640 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1641 }
1642
1643 if (rpc_com->confirm_timeout) {
1644 sprintf(str, "%u", rpc_com->confirm_timeout);
1645 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1646 }
1647
1648 if (rpc_com->persist) {
1649 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1650 if (!node) {
1651 lyd_free(data);
1652 return NC_MSG_ERROR;
1653 }
1654 }
1655
1656 if (rpc_com->persist_id) {
1657 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1658 if (!node) {
1659 lyd_free(data);
1660 return NC_MSG_ERROR;
1661 }
1662 }
1663 break;
1664
1665 case NC_RPC_DISCARD:
1666 data = lyd_new(NULL, ietfnc, "discard-changes");
1667 break;
1668
1669 case NC_RPC_CANCEL:
1670 rpc_can = (struct nc_rpc_cancel *)rpc;
1671
1672 data = lyd_new(NULL, ietfnc, "cancel-commit");
1673 if (rpc_can->persist_id) {
1674 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1675 if (!node) {
1676 lyd_free(data);
1677 return NC_MSG_ERROR;
1678 }
1679 }
1680 break;
1681
1682 case NC_RPC_VALIDATE:
1683 rpc_val = (struct nc_rpc_validate *)rpc;
1684
1685 data = lyd_new(NULL, ietfnc, "validate");
1686 node = lyd_new(data, ietfnc, "source");
1687 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001688 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001689 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001690 } else {
1691 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1692 }
1693 } else {
1694 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1695 }
1696 if (!node) {
1697 lyd_free(data);
1698 return NC_MSG_ERROR;
1699 }
1700 break;
1701
1702 case NC_RPC_GETSCHEMA:
1703 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1704 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001705 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001706 return NC_MSG_ERROR;
1707 }
1708
1709 rpc_gs = (struct nc_rpc_getschema *)rpc;
1710
1711 data = lyd_new(NULL, ietfncmon, "get-schema");
1712 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1713 if (!node) {
1714 lyd_free(data);
1715 return NC_MSG_ERROR;
1716 }
1717 if (rpc_gs->version) {
1718 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1719 if (!node) {
1720 lyd_free(data);
1721 return NC_MSG_ERROR;
1722 }
1723 }
1724 if (rpc_gs->format) {
1725 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1726 if (!node) {
1727 lyd_free(data);
1728 return NC_MSG_ERROR;
1729 }
1730 }
1731 break;
1732
1733 case NC_RPC_SUBSCRIBE:
1734 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1735 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001736 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001737 return NC_MSG_ERROR;
1738 }
1739
1740 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1741
1742 data = lyd_new(NULL, notifs, "create-subscription");
1743 if (rpc_sub->stream) {
1744 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1745 if (!node) {
1746 lyd_free(data);
1747 return NC_MSG_ERROR;
1748 }
1749 }
1750
1751 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001752 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02001753 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001754 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001755 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001756 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001757 lyd_insert_attr(node, NULL, "type", "xpath");
1758 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001759 }
1760 if (!node) {
1761 lyd_free(data);
1762 return NC_MSG_ERROR;
1763 }
1764 }
1765
1766 if (rpc_sub->start) {
1767 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1768 if (!node) {
1769 lyd_free(data);
1770 return NC_MSG_ERROR;
1771 }
1772 }
1773
1774 if (rpc_sub->stop) {
1775 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1776 if (!node) {
1777 lyd_free(data);
1778 return NC_MSG_ERROR;
1779 }
1780 }
1781 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001782 default:
1783 ERRINT;
1784 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001785 }
1786
Michal Vaskoeee99412016-11-21 10:19:43 +01001787 if (lyd_validate(&data, LYD_OPT_RPC | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001788 lyd_free(data);
1789 return NC_MSG_ERROR;
1790 }
1791
Michal vasko50cc94f2016-10-04 13:46:20 +02001792 ret = nc_timedlock(session->ti_lock, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001793 if (ret == -1) {
1794 /* error */
1795 r = NC_MSG_ERROR;
1796 } else if (!ret) {
1797 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001798 r = NC_MSG_WOULDBLOCK;
1799 } else {
1800 /* send RPC, store its message ID */
1801 r = nc_send_msg(session, data);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001802 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01001803 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001804 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001805
1806 lyd_free(data);
1807
1808 if (r != NC_MSG_RPC) {
1809 return r;
1810 }
1811
1812 *msgid = cur_msgid;
1813 return NC_MSG_RPC;
1814}