blob: dfc4a368f34aa36c455e02da12589a03a021cbff [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 Vaskoe8cfaa72016-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 Vaskoe8cfaa72016-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 Vaskoe8cfaa72016-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 Vaskoe8cfaa72016-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 {
Michal Vaskofcdfda12016-10-12 11:10:21 +0200220 msg = nc_recv_reply(session, rpc, msgid, NC_READ_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200221 } 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
Michal Vaskoef578332016-01-25 13:20:09 +0100288/* return 0 - ok, 1 - some models failed to load, -1 - error */
Michal Vasko086311b2016-01-08 09:53:11 +0100289int
290nc_ctx_check_and_fill(struct nc_session *session)
291{
Michal Vaskoe8cfaa72016-11-21 10:19:43 +0100292 const char *module_cpblt;
Michal Vaskoef578332016-01-25 13:20:09 +0100293 int i, get_schema_support = 0, ret = 0, r;
Michal Vasko086311b2016-01-08 09:53:11 +0100294 ly_module_clb old_clb = NULL;
295 void *old_data = NULL;
296
297 assert(session->cpblts && session->ctx);
298
299 /* check if get-schema is supported */
300 for (i = 0; session->cpblts[i]; ++i) {
301 if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
302 get_schema_support = 1;
303 break;
304 }
305 }
306
307 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
308 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) {
309 if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) {
310 /* set module retrieval using <get-schema> */
311 old_clb = ly_ctx_get_module_clb(session->ctx, &old_data);
Michal Vaskoca4ad152016-03-03 15:50:45 +0100312 ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session);
Michal Vasko086311b2016-01-08 09:53:11 +0100313 } else {
314 WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>.");
315 }
316 }
317
318 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Michal Vasko1aaa6602016-02-09 11:04:33 +0100319 if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100320 if (old_clb) {
321 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
322 }
Michal Vaskoef578332016-01-25 13:20:09 +0100323 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100324 }
325
326 /* load all other models */
327 for (i = 0; session->cpblts[i]; ++i) {
Michal Vaskoe8cfaa72016-11-21 10:19:43 +0100328 module_cpblt = strstr(session->cpblts[i], "module=");
329 /* this capability requires a module */
330 if (module_cpblt) {
331 r = ctx_check_and_load_model(session, module_cpblt);
332 if (r == -1) {
333 ret = -1;
334 break;
Michal Vaskoef578332016-01-25 13:20:09 +0100335 }
336
Michal Vaskoe8cfaa72016-11-21 10:19:43 +0100337 /* failed to load schema, but let's try to find it using user callback (or locally, if not set),
338 * if it was using get-schema */
339 if (r == 1) {
340 if (get_schema_support) {
341 VRB("Trying to load the schema from a different source.");
342 /* works even if old_clb is NULL */
343 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
344 r = ctx_check_and_load_model(session, module_cpblt);
345 }
Michal Vaskoef578332016-01-25 13:20:09 +0100346
Michal Vaskoe8cfaa72016-11-21 10:19:43 +0100347 /* fail again (or no other way to try), too bad */
348 if (r) {
349 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
350 }
351
352 /* set get-schema callback back */
353 ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session);
354 }
Michal Vaskoef578332016-01-25 13:20:09 +0100355 }
Michal Vasko086311b2016-01-08 09:53:11 +0100356 }
357
358 if (old_clb) {
359 ly_ctx_set_module_clb(session->ctx, old_clb, old_data);
360 }
Michal Vaskoe8cfaa72016-11-21 10:19:43 +0100361 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
362 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 +0100363 }
364 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100365}
366
367API struct nc_session *
368nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
369{
Michal Vaskod083db62016-01-19 10:31:29 +0100370 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +0100371
Michal Vasko45e53ae2016-04-07 11:46:03 +0200372 if (fdin < 0) {
373 ERRARG("fdin");
374 return NULL;
375 } else if (fdout < 0) {
376 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +0100377 return NULL;
378 }
379
380 /* prepare session structure */
381 session = calloc(1, sizeof *session);
382 if (!session) {
383 ERRMEM;
384 return NULL;
385 }
386 session->status = NC_STATUS_STARTING;
387 session->side = NC_CLIENT;
388
389 /* transport specific data */
390 session->ti_type = NC_TI_FD;
391 session->ti.fd.in = fdin;
392 session->ti.fd.out = fdout;
393
394 /* assign context (dicionary needed for handshake) */
395 if (!ctx) {
396 ctx = ly_ctx_new(SCHEMAS_DIR);
Michal Vaskoe035b8e2016-03-11 10:10:03 +0100397 /* definitely should not happen, but be ready */
398 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
399 /* that's just it */
400 goto fail;
401 }
Michal Vasko086311b2016-01-08 09:53:11 +0100402 } else {
403 session->flags |= NC_SESSION_SHAREDCTX;
404 }
405 session->ctx = ctx;
406
407 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200408 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +0100409 goto fail;
410 }
411 session->status = NC_STATUS_RUNNING;
412
Michal Vaskoef578332016-01-25 13:20:09 +0100413 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100414 goto fail;
415 }
416
417 return session;
418
419fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100420 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100421 return NULL;
422}
423
424int
Michal Vaskof05562c2016-01-20 12:06:43 +0100425nc_sock_connect(const char* host, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100426{
Michal Vasko0190bc32016-03-02 15:47:49 +0100427 int i, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100428 struct addrinfo hints, *res_list, *res;
429 char port_s[6]; /* length of string representation of short int */
430
431 snprintf(port_s, 6, "%u", port);
432
433 /* Connect to a server */
434 memset(&hints, 0, sizeof hints);
435 hints.ai_family = AF_UNSPEC;
436 hints.ai_socktype = SOCK_STREAM;
437 hints.ai_protocol = IPPROTO_TCP;
438 i = getaddrinfo(host, port_s, &hints, &res_list);
439 if (i != 0) {
440 ERR("Unable to translate the host address (%s).", gai_strerror(i));
441 return -1;
442 }
443
444 for (i = 0, res = res_list; res != NULL; res = res->ai_next) {
445 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
446 if (sock == -1) {
447 /* socket was not created, try another resource */
448 i = errno;
449 goto errloop;
450 }
451
452 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
453 /* network connection failed, try another resource */
454 i = errno;
455 close(sock);
456 sock = -1;
457 goto errloop;
458 }
459
Michal Vasko0190bc32016-03-02 15:47:49 +0100460 /* make the socket non-blocking */
461 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
462 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100463 close(sock);
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200464 freeaddrinfo(res_list);
Michal Vasko0190bc32016-03-02 15:47:49 +0100465 return -1;
466 }
467
Michal Vasko086311b2016-01-08 09:53:11 +0100468 /* we're done, network connection established */
469 break;
470errloop:
471 VRB("Unable to connect to %s:%s over %s (%s).", host, port_s,
472 (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i));
473 continue;
474 }
475
476 if (sock == -1) {
477 ERR("Unable to connect to %s:%s.", host, port_s);
478 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100479 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 +0100480 }
481 freeaddrinfo(res_list);
482
483 return sock;
484}
485
Michal Vasko086311b2016-01-08 09:53:11 +0100486static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100487get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100488{
Michal Vasko62be1ce2016-03-03 13:24:52 +0100489 int r;
Michal Vasko086311b2016-01-08 09:53:11 +0100490 char *ptr;
491 const char *str_msgid;
492 uint64_t cur_msgid;
493 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100494 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +0100495 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
496
Michal vasko50cc94f2016-10-04 13:46:20 +0200497 r = nc_timedlock(session->ti_lock, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100498 if (r == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +0100499 /* error */
500 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 } else if (!r) {
Michal Vasko086311b2016-01-08 09:53:11 +0100502 /* timeout */
503 return NC_MSG_WOULDBLOCK;
504 }
505
506 /* try to get notification from the session's queue */
507 if (!msgid && session->notifs) {
508 cont = session->notifs;
509 session->notifs = cont->next;
510
Michal Vasko71ba2da2016-05-04 10:53:16 +0200511 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100512 free(cont);
513
Michal Vasko71ba2da2016-05-04 10:53:16 +0200514 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +0100515 }
516
517 /* try to get rpc-reply from the session's queue */
518 if (msgid && session->replies) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200519 cont = session->replies;
520 session->replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +0100521
Michal Vasko71ba2da2016-05-04 10:53:16 +0200522 xml = cont->msg;
523 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +0100524
Michal Vasko71ba2da2016-05-04 10:53:16 +0200525 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +0100526 }
527
Michal Vasko71ba2da2016-05-04 10:53:16 +0200528 if (!msgtype) {
529 /* read message from wire */
530 msgtype = nc_read_msg_poll(session, timeout, &xml);
531 }
Michal Vasko086311b2016-01-08 09:53:11 +0100532
533 /* we read rpc-reply, want a notif */
534 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100535 cont_ptr = &session->replies;
536 while (*cont_ptr) {
537 cont_ptr = &((*cont_ptr)->next);
538 }
539 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100540 if (!*cont_ptr) {
541 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200542 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100543 lyxml_free(session->ctx, xml);
544 return NC_MSG_ERROR;
545 }
Michal Vasko086311b2016-01-08 09:53:11 +0100546 (*cont_ptr)->msg = xml;
547 (*cont_ptr)->next = NULL;
548 }
549
550 /* we read notif, want a rpc-reply */
551 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100552 /* TODO check whether the session is even subscribed */
553 /*if (!session->notif) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100554 pthread_mutex_unlock(session->ti_lock);
Michal Vaskod083db62016-01-19 10:31:29 +0100555 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100556 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +0100557 return NC_MSG_ERROR;
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100558 }*/
Michal Vasko086311b2016-01-08 09:53:11 +0100559
560 cont_ptr = &session->notifs;
561 while (*cont_ptr) {
562 cont_ptr = &((*cont_ptr)->next);
563 }
564 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100565 if (!cont_ptr) {
566 ERRMEM;
Michal Vasko71ba2da2016-05-04 10:53:16 +0200567 pthread_mutex_unlock(session->ti_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100568 lyxml_free(session->ctx, xml);
569 return NC_MSG_ERROR;
570 }
Michal Vasko086311b2016-01-08 09:53:11 +0100571 (*cont_ptr)->msg = xml;
572 (*cont_ptr)->next = NULL;
573 }
574
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100575 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +0100576
577 switch (msgtype) {
578 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100579 if (!msgid) {
580 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100581 }
Michal Vasko086311b2016-01-08 09:53:11 +0100582 break;
583
584 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +0100585 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +0200586 /* check message-id */
587 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
588 if (!str_msgid) {
589 ERR("Session %u: received a <rpc-reply> without a message-id.", session->id);
590 msgtype = NC_MSG_REPLY_ERR_MSGID;
591 } else {
592 cur_msgid = strtoul(str_msgid, &ptr, 10);
593 if (cur_msgid != msgid) {
594 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
595 session->id, str_msgid);
596 msgtype = NC_MSG_REPLY_ERR_MSGID;
597 }
598 }
Michal Vasko2518b6b2016-01-28 13:24:53 +0100599 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +0100600 }
Michal Vasko086311b2016-01-08 09:53:11 +0100601 break;
602
603 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100604 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100605 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200606 msgtype = NC_MSG_ERROR;
607 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100608
609 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +0100610 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100611 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +0200612 msgtype = NC_MSG_ERROR;
613 break;
Michal Vasko086311b2016-01-08 09:53:11 +0100614
615 default:
616 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
617 * NC_MSG_NONE is not returned by nc_read_msg()
618 */
619 break;
620 }
621
622 return msgtype;
623}
624
625/* cannot strictly fail, but does not need to fill any error parameter at all */
626static void
627parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
628{
629 struct lyxml_elem *iter, *next, *info;
630
631 LY_TREE_FOR(xml->child, iter) {
632 if (!iter->ns) {
633 if (iter->content) {
634 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
635 } else {
636 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
637 }
638 continue;
639 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
640 if (iter->content) {
641 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
642 iter->name, iter->content, iter->ns->value);
643 } else {
644 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
645 }
646 continue;
647 }
648
649 if (!strcmp(iter->name, "error-type")) {
650 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
651 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
652 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
653 } else if (err->type) {
654 WRN("<rpc-error> <error-type> duplicated.");
655 } else {
656 err->type = lydict_insert(ctx, iter->content, 0);
657 }
658 } else if (!strcmp(iter->name, "error-tag")) {
659 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
660 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
661 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
662 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
663 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
664 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
665 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
666 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
667 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
668 && strcmp(iter->content, "malformed-message"))) {
669 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
670 } else if (err->tag) {
671 WRN("<rpc-error> <error-tag> duplicated.");
672 } else {
673 err->tag = lydict_insert(ctx, iter->content, 0);
674 }
675 } else if (!strcmp(iter->name, "error-severity")) {
676 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
677 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
678 } else if (err->severity) {
679 WRN("<rpc-error> <error-severity> duplicated.");
680 } else {
681 err->severity = lydict_insert(ctx, iter->content, 0);
682 }
683 } else if (!strcmp(iter->name, "error-app-tag")) {
684 if (err->apptag) {
685 WRN("<rpc-error> <error-app-tag> duplicated.");
686 } else {
687 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
688 }
689 } else if (!strcmp(iter->name, "error-path")) {
690 if (err->path) {
691 WRN("<rpc-error> <error-path> duplicated.");
692 } else {
693 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
694 }
695 } else if (!strcmp(iter->name, "error-message")) {
696 if (err->message) {
697 WRN("<rpc-error> <error-message> duplicated.");
698 } else {
699 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
700 if (!err->message_lang) {
701 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
702 }
703 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
704 }
705 } else if (!strcmp(iter->name, "error-info")) {
706 LY_TREE_FOR_SAFE(iter->child, next, info) {
707 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
708 if (!strcmp(info->name, "session-id")) {
709 if (err->sid) {
710 WRN("<rpc-error> <error-info> <session-id> duplicated.");
711 } else {
712 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
713 }
714 } else if (!strcmp(info->name, "bad-attr")) {
715 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100716 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
717 if (!err->attr) {
718 ERRMEM;
719 return;
720 }
Michal Vasko086311b2016-01-08 09:53:11 +0100721 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
722 } else if (!strcmp(info->name, "bad-element")) {
723 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100724 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
725 if (!err->elem) {
726 ERRMEM;
727 return;
728 }
Michal Vasko086311b2016-01-08 09:53:11 +0100729 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
730 } else if (!strcmp(info->name, "bad-namespace")) {
731 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100732 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
733 if (!err->ns) {
734 ERRMEM;
735 return;
736 }
Michal Vasko086311b2016-01-08 09:53:11 +0100737 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
738 } else {
739 if (info->content) {
740 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
741 info->name, info->content);
742 } else {
743 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
744 }
745 }
746 } else {
747 lyxml_unlink(ctx, info);
748 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100749 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
750 if (!err->other) {
751 ERRMEM;
752 return;
753 }
Michal Vasko086311b2016-01-08 09:53:11 +0100754 err->other[err->other_count - 1] = info;
755 }
756 }
757 } else {
758 if (iter->content) {
759 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
760 } else {
761 WRN("<rpc-error> unknown child \"%s\".", iter->name);
762 }
763 }
764 }
765}
766
767static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100768parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +0100769{
770 struct lyxml_elem *iter;
Michal Vaskocc3d52b2016-10-18 12:17:22 +0200771 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +0100772 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +0100773 struct nc_reply_data *data_rpl;
774 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +0200775 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +0100776 int i;
777
778 if (!xml->child) {
779 ERR("An empty <rpc-reply>.");
780 return NULL;
781 }
782
783 /* rpc-error */
784 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
785 /* count and check elements */
786 i = 0;
787 LY_TREE_FOR(xml->child, iter) {
788 if (strcmp(iter->name, "rpc-error")) {
789 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
790 return NULL;
791 } else if (!iter->ns) {
792 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
793 return NULL;
794 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
795 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
796 return NULL;
797 }
798 ++i;
799 }
800
801 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100802 if (!error_rpl) {
803 ERRMEM;
804 return NULL;
805 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100806 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100807 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100808 if (!error_rpl->err) {
809 ERRMEM;
810 free(error_rpl);
811 return NULL;
812 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100813 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +0100814 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100815 reply = (struct nc_reply *)error_rpl;
816
817 i = 0;
818 LY_TREE_FOR(xml->child, iter) {
819 parse_rpc_error(ctx, iter, error_rpl->err + i);
820 ++i;
821 }
822
823 /* ok */
824 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
825 if (xml->child->next) {
826 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
827 return NULL;
828 }
829 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100830 if (!reply) {
831 ERRMEM;
832 return NULL;
833 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100834 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +0100835
836 /* some RPC output */
837 } else {
838 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200839 case NC_RPC_ACT_GENERIC:
840 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +0100841
842 if (rpc_gen->has_data) {
Michal Vaskocc3d52b2016-10-18 12:17:22 +0200843 rpc_act = rpc_gen->content.data;
Michal Vasko086311b2016-01-08 09:53:11 +0100844 } else {
Michal Vaskocc3d52b2016-10-18 12:17:22 +0200845 rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL);
846 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +0200847 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +0100848 return NULL;
849 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200850 }
Michal Vasko086311b2016-01-08 09:53:11 +0100851 break;
852
853 case NC_RPC_GETCONFIG:
854 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +0100855 if (!xml->child->child) {
856 /* we did not receive any data */
857 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100858 if (!data_rpl) {
859 ERRMEM;
860 return NULL;
861 }
Michal Vasko13ed2942016-02-29 16:21:00 +0100862 data_rpl->type = NC_RPL_DATA;
863 data_rpl->data = NULL;
864 return (struct nc_reply *)data_rpl;
865 }
866
Michal Vasko086311b2016-01-08 09:53:11 +0100867 /* special treatment */
868 data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100869 | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +0100870 if (!data) {
871 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
872 return NULL;
873 }
874 break;
875
876 case NC_RPC_GETSCHEMA:
Michal Vaskocc3d52b2016-10-18 12:17:22 +0200877 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL), "get-schema");
878 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100879 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100880 return NULL;
881 }
882 break;
883
884 case NC_RPC_EDIT:
885 case NC_RPC_COPY:
886 case NC_RPC_DELETE:
887 case NC_RPC_LOCK:
888 case NC_RPC_UNLOCK:
889 case NC_RPC_KILL:
890 case NC_RPC_COMMIT:
891 case NC_RPC_DISCARD:
892 case NC_RPC_CANCEL:
893 case NC_RPC_VALIDATE:
894 case NC_RPC_SUBSCRIBE:
895 /* there is no output defined */
896 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
897 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100898 default:
899 ERRINT;
900 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100901 }
902
903 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100904 if (!data_rpl) {
905 ERRMEM;
906 return NULL;
907 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100908 data_rpl->type = NC_RPL_DATA;
Michal Vasko086311b2016-01-08 09:53:11 +0100909 if (!data) {
Michal Vasko68b3f292016-09-16 12:00:32 +0200910 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +0200911 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100912 } else {
913 /* <get>, <get-config> */
914 data_rpl->data = data;
915 }
Michal Vaskocc3d52b2016-10-18 12:17:22 +0200916 lyd_free_withsiblings(rpc_act);
Michal Vasko086311b2016-01-08 09:53:11 +0100917 if (!data_rpl->data) {
918 ERR("Failed to parse <rpc-reply>.");
919 free(data_rpl);
920 return NULL;
921 }
922 reply = (struct nc_reply *)data_rpl;
923 }
924
925 return reply;
926}
927
Radek Krejci53691be2016-02-22 13:58:37 +0100928#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +0100929
Michal Vasko3031aae2016-01-27 16:07:18 +0100930int
931nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
932{
933 int sock;
934
Michal Vasko45e53ae2016-04-07 11:46:03 +0200935 if (!address) {
936 ERRARG("address");
937 return -1;
938 } else if (!port) {
939 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +0100940 return -1;
941 }
942
943 sock = nc_sock_listen(address, port);
944 if (sock == -1) {
945 return -1;
946 }
947
948 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100949 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
950 if (!client_opts.ch_binds) {
951 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +0100952 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100953 return -1;
954 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100955
956 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;
964 client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti;
965
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))
989 && (!ti || (client_opts.ch_binds[i].ti == ti))) {
990 close(client_opts.ch_binds[i].sock);
991 free((char *)client_opts.ch_binds[i].address);
992
993 --client_opts.ch_bind_count;
994 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
995
996 ret = 0;
997 }
998 }
999 }
1000
1001 return ret;
1002}
1003
1004API int
1005nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1006{
1007 int sock;
1008 char *host = NULL;
1009 uint16_t port, idx;
1010
Michal Vasko45e53ae2016-04-07 11:46:03 +02001011 if (!client_opts.ch_binds) {
1012 ERRINIT;
1013 return -1;
1014 } else if (!session) {
1015 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001016 return -1;
1017 }
1018
1019 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1020
Michal Vasko50456e82016-02-02 12:16:08 +01001021 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001022 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001023 return sock;
1024 }
1025
Radek Krejci53691be2016-02-22 13:58:37 +01001026#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001027 if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001028 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001029 } else
1030#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001031#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001032 if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001033 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001034 } else
1035#endif
1036 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001037 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001038 *session = NULL;
1039 }
1040
1041 free(host);
1042
1043 if (!(*session)) {
1044 return -1;
1045 }
1046
1047 return 1;
1048}
1049
Radek Krejci53691be2016-02-22 13:58:37 +01001050#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001051
Michal Vaskobdfb5242016-05-24 09:11:01 +02001052API const char **
1053nc_session_get_cpblts(const struct nc_session *session)
1054{
1055 if (!session) {
1056 ERRARG("session");
1057 return NULL;
1058 }
1059
1060 return session->cpblts;
1061}
1062
1063API const char *
1064nc_session_cpblt(const struct nc_session *session, const char *capab)
1065{
1066 int i, len;
1067
1068 if (!session) {
1069 ERRARG("session");
1070 return NULL;
1071 } else if (!capab) {
1072 ERRARG("capab");
1073 return NULL;
1074 }
1075
1076 len = strlen(capab);
1077 for (i = 0; session->cpblts[i]; ++i) {
1078 if (!strncmp(session->cpblts[i], capab, len)) {
1079 return session->cpblts[i];
1080 }
1081 }
1082
1083 return NULL;
1084}
1085
Michal Vasko9cd26a82016-05-31 08:58:48 +02001086API int
1087nc_session_ntf_thread_running(const struct nc_session *session)
1088{
1089 if (!session) {
1090 ERRARG("session");
1091 return 0;
1092 }
1093
1094 return session->ntf_tid ? 1 : 0;
1095}
1096
Michal Vaskob7558c52016-02-26 15:04:19 +01001097API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001098nc_client_init(void)
1099{
1100 nc_init();
1101}
1102
1103API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001104nc_client_destroy(void)
1105{
Michal Vasko7f1c0ef2016-03-11 11:13:06 +01001106 nc_client_set_schema_searchpath(NULL);
Michal Vaskob7558c52016-02-26 15:04:19 +01001107#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1108 nc_client_ch_del_bind(NULL, 0, 0);
1109#endif
1110#ifdef NC_ENABLED_SSH
1111 nc_client_ssh_destroy_opts();
1112#endif
Michal Vaskoc979d3a2016-02-26 15:26:21 +01001113#ifdef NC_ENABLED_TLS
Michal Vaskob7558c52016-02-26 15:04:19 +01001114 nc_client_tls_destroy_opts();
1115#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001116 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001117}
1118
Michal Vasko086311b2016-01-08 09:53:11 +01001119API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001120nc_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 +01001121{
1122 struct lyxml_elem *xml;
1123 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1124
Michal Vasko45e53ae2016-04-07 11:46:03 +02001125 if (!session) {
1126 ERRARG("session");
1127 return NC_MSG_ERROR;
1128 } else if (!rpc) {
1129 ERRARG("rpc");
1130 return NC_MSG_ERROR;
1131 } else if (!reply) {
1132 ERRARG("reply");
1133 return NC_MSG_ERROR;
1134 } else if (parseroptions & LYD_OPT_TYPEMASK) {
1135 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01001136 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001137 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001138 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001139 return NC_MSG_ERROR;
1140 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001141 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoe8cfaa72016-11-21 10:19:43 +01001142 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
1143 parseroptions &= LYD_OPT_STRICT;
1144 }
Michal Vasko086311b2016-01-08 09:53:11 +01001145 *reply = NULL;
1146
1147 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01001148
Michal Vasko71ba2da2016-05-04 10:53:16 +02001149 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001150 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01001151 lyxml_free(session->ctx, xml);
1152 if (!(*reply)) {
1153 return NC_MSG_ERROR;
1154 }
1155 }
1156
1157 return msgtype;
1158}
1159
1160API NC_MSG_TYPE
1161nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
1162{
1163 struct lyxml_elem *xml, *ev_time;
1164 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1165
Michal Vasko45e53ae2016-04-07 11:46:03 +02001166 if (!session) {
1167 ERRARG("session");
1168 return NC_MSG_ERROR;
1169 } else if (!notif) {
1170 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01001171 return NC_MSG_ERROR;
1172 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001173 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001174 return NC_MSG_ERROR;
1175 }
1176
1177 msgtype = get_msg(session, timeout, 0, &xml);
1178
1179 if (msgtype == NC_MSG_NOTIF) {
1180 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001181 if (!*notif) {
1182 ERRMEM;
1183 lyxml_free(session->ctx, xml);
1184 return NC_MSG_ERROR;
1185 }
Michal Vasko086311b2016-01-08 09:53:11 +01001186
1187 /* eventTime */
1188 LY_TREE_FOR(xml->child, ev_time) {
1189 if (!strcmp(ev_time->name, "eventTime")) {
1190 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
1191 /* lyd_parse does not know this element */
1192 lyxml_free(session->ctx, ev_time);
1193 break;
1194 }
1195 }
1196 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01001197 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001198 goto fail;
1199 }
1200
1201 /* notification body */
Michal Vaskoe8cfaa72016-11-21 10:19:43 +01001202 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT
1203 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001204 lyxml_free(session->ctx, xml);
1205 xml = NULL;
1206 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01001207 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001208 goto fail;
1209 }
1210 }
1211
1212 return msgtype;
1213
1214fail:
1215 lydict_remove(session->ctx, (*notif)->datetime);
1216 lyd_free((*notif)->tree);
1217 free(*notif);
1218 *notif = NULL;
1219 lyxml_free(session->ctx, xml);
1220
1221 return NC_MSG_ERROR;
1222}
1223
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001224static void *
1225nc_recv_notif_thread(void *arg)
1226{
1227 struct nc_ntf_thread_arg *ntarg;
1228 struct nc_session *session;
1229 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
1230 struct nc_notif *notif;
1231 NC_MSG_TYPE msgtype;
1232
1233 ntarg = (struct nc_ntf_thread_arg *)arg;
1234 session = ntarg->session;
1235 notif_clb = ntarg->notif_clb;
1236 free(ntarg);
1237
1238 while (session->ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02001239 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001240 if (msgtype == NC_MSG_NOTIF) {
1241 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01001242 if (!strcmp(notif->tree->schema->name, "notificationComplete")
1243 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
1244 nc_notif_free(notif);
1245 break;
1246 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001247 nc_notif_free(notif);
Michal Vasko0651c902016-05-19 15:55:42 +02001248 } else if (msgtype == NC_MSG_ERROR) {
1249 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001250 }
1251
1252 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
1253 }
1254
Michal Vasko0651c902016-05-19 15:55:42 +02001255 VRB("Session %u: notification thread exit.", session->id);
1256 session->ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001257 return NULL;
1258}
1259
1260API int
1261nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
1262{
1263 struct nc_ntf_thread_arg *ntarg;
1264 int ret;
1265
Michal Vasko45e53ae2016-04-07 11:46:03 +02001266 if (!session) {
1267 ERRARG("session");
1268 return -1;
1269 } else if (!notif_clb) {
1270 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001271 return -1;
1272 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
1273 ERR("Session %u: invalid session to receive Notifications.", session->id);
1274 return -1;
1275 } else if (session->ntf_tid) {
1276 ERR("Session %u: separate notification thread is already running.", session->id);
1277 return -1;
1278 }
1279
1280 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001281 if (!ntarg) {
1282 ERRMEM;
1283 return -1;
1284 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001285 ntarg->session = session;
1286 ntarg->notif_clb = notif_clb;
1287
1288 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
1289 session->ntf_tid = malloc(sizeof *session->ntf_tid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001290 if (!session->ntf_tid) {
1291 ERRMEM;
1292 free(ntarg);
1293 return -1;
1294 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01001295
1296 ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg);
1297 if (ret) {
1298 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
1299 free(ntarg);
1300 free((pthread_t *)session->ntf_tid);
1301 session->ntf_tid = NULL;
1302 return -1;
1303 }
1304
1305 return 0;
1306}
1307
Michal Vasko086311b2016-01-08 09:53:11 +01001308API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001309nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001310{
1311 NC_MSG_TYPE r;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001312 int ret;
Michal Vasko90e8e692016-07-13 12:27:57 +02001313 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01001314 struct nc_rpc_getconfig *rpc_gc;
1315 struct nc_rpc_edit *rpc_e;
1316 struct nc_rpc_copy *rpc_cp;
1317 struct nc_rpc_delete *rpc_del;
1318 struct nc_rpc_lock *rpc_lock;
1319 struct nc_rpc_get *rpc_g;
1320 struct nc_rpc_kill *rpc_k;
1321 struct nc_rpc_commit *rpc_com;
1322 struct nc_rpc_cancel *rpc_can;
1323 struct nc_rpc_validate *rpc_val;
1324 struct nc_rpc_getschema *rpc_gs;
1325 struct nc_rpc_subscribe *rpc_sub;
1326 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01001327 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02001328 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01001329 uint64_t cur_msgid;
1330
Michal Vasko45e53ae2016-04-07 11:46:03 +02001331 if (!session) {
1332 ERRARG("session");
1333 return NC_MSG_ERROR;
1334 } else if (!rpc) {
1335 ERRARG("rpc");
1336 return NC_MSG_ERROR;
1337 } else if (!msgid) {
1338 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01001339 return NC_MSG_ERROR;
1340 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01001341 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001342 return NC_MSG_ERROR;
1343 }
1344
Michal Vasko90e8e692016-07-13 12:27:57 +02001345 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001346 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
1347 if (!ietfnc) {
Michal Vaskod083db62016-01-19 10:31:29 +01001348 ERR("Session %u: missing ietf-netconf schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001349 return NC_MSG_ERROR;
1350 }
1351 }
1352
1353 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001354 case NC_RPC_ACT_GENERIC:
1355 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001356
1357 if (rpc_gen->has_data) {
1358 data = rpc_gen->content.data;
1359 } else {
Michal Vaskoe8cfaa72016-11-21 10:19:43 +01001360 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC
1361 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001362 }
1363 break;
1364
1365 case NC_RPC_GETCONFIG:
1366 rpc_gc = (struct nc_rpc_getconfig *)rpc;
1367
1368 data = lyd_new(NULL, ietfnc, "get-config");
1369 node = lyd_new(data, ietfnc, "source");
1370 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
1371 if (!node) {
1372 lyd_free(data);
1373 return NC_MSG_ERROR;
1374 }
1375 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001376 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vasko9161da52016-10-07 13:34:57 +02001377 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001378 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001379 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001380 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001381 lyd_insert_attr(node, NULL, "type", "xpath");
1382 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001383 }
1384 if (!node) {
1385 lyd_free(data);
1386 return NC_MSG_ERROR;
1387 }
1388 }
1389
1390 if (rpc_gc->wd_mode) {
1391 if (!ietfncwd) {
1392 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1393 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001394 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001395 return NC_MSG_ERROR;
1396 }
1397 }
1398 switch (rpc_gc->wd_mode) {
1399 case NC_WD_UNKNOWN:
1400 /* cannot get here */
1401 break;
1402 case NC_WD_ALL:
1403 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1404 break;
1405 case NC_WD_ALL_TAG:
1406 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1407 break;
1408 case NC_WD_TRIM:
1409 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1410 break;
1411 case NC_WD_EXPLICIT:
1412 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1413 break;
1414 }
1415 if (!node) {
1416 lyd_free(data);
1417 return NC_MSG_ERROR;
1418 }
1419 }
1420 break;
1421
1422 case NC_RPC_EDIT:
1423 rpc_e = (struct nc_rpc_edit *)rpc;
1424
1425 data = lyd_new(NULL, ietfnc, "edit-config");
1426 node = lyd_new(data, ietfnc, "target");
1427 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
1428 if (!node) {
1429 lyd_free(data);
1430 return NC_MSG_ERROR;
1431 }
1432
1433 if (rpc_e->default_op) {
1434 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
1435 if (!node) {
1436 lyd_free(data);
1437 return NC_MSG_ERROR;
1438 }
1439 }
1440
1441 if (rpc_e->test_opt) {
1442 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
1443 if (!node) {
1444 lyd_free(data);
1445 return NC_MSG_ERROR;
1446 }
1447 }
1448
1449 if (rpc_e->error_opt) {
1450 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
1451 if (!node) {
1452 lyd_free(data);
1453 return NC_MSG_ERROR;
1454 }
1455 }
1456
Michal Vasko7793bc62016-09-16 11:58:41 +02001457 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vasko9161da52016-10-07 13:34:57 +02001458 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001459 } else {
1460 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
1461 }
1462 if (!node) {
1463 lyd_free(data);
1464 return NC_MSG_ERROR;
1465 }
1466 break;
1467
1468 case NC_RPC_COPY:
1469 rpc_cp = (struct nc_rpc_copy *)rpc;
1470
1471 data = lyd_new(NULL, ietfnc, "copy-config");
1472 node = lyd_new(data, ietfnc, "target");
1473 if (rpc_cp->url_trg) {
1474 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
1475 } else {
1476 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
1477 }
1478 if (!node) {
1479 lyd_free(data);
1480 return NC_MSG_ERROR;
1481 }
1482
1483 node = lyd_new(data, ietfnc, "source");
1484 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001485 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vasko9161da52016-10-07 13:34:57 +02001486 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001487 } else {
1488 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
1489 }
1490 } else {
1491 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
1492 }
1493 if (!node) {
1494 lyd_free(data);
1495 return NC_MSG_ERROR;
1496 }
1497
1498 if (rpc_cp->wd_mode) {
1499 if (!ietfncwd) {
1500 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1501 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001502 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001503 return NC_MSG_ERROR;
1504 }
1505 }
1506 switch (rpc_cp->wd_mode) {
1507 case NC_WD_UNKNOWN:
1508 /* cannot get here */
1509 break;
1510 case NC_WD_ALL:
1511 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1512 break;
1513 case NC_WD_ALL_TAG:
1514 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1515 break;
1516 case NC_WD_TRIM:
1517 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1518 break;
1519 case NC_WD_EXPLICIT:
1520 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1521 break;
1522 }
1523 if (!node) {
1524 lyd_free(data);
1525 return NC_MSG_ERROR;
1526 }
1527 }
1528 break;
1529
1530 case NC_RPC_DELETE:
1531 rpc_del = (struct nc_rpc_delete *)rpc;
1532
1533 data = lyd_new(NULL, ietfnc, "delete-config");
1534 node = lyd_new(data, ietfnc, "target");
1535 if (rpc_del->url) {
1536 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
1537 } else {
1538 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
1539 }
1540 if (!node) {
1541 lyd_free(data);
1542 return NC_MSG_ERROR;
1543 }
1544 break;
1545
1546 case NC_RPC_LOCK:
1547 rpc_lock = (struct nc_rpc_lock *)rpc;
1548
1549 data = lyd_new(NULL, ietfnc, "lock");
1550 node = lyd_new(data, ietfnc, "target");
1551 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1552 if (!node) {
1553 lyd_free(data);
1554 return NC_MSG_ERROR;
1555 }
1556 break;
1557
1558 case NC_RPC_UNLOCK:
1559 rpc_lock = (struct nc_rpc_lock *)rpc;
1560
1561 data = lyd_new(NULL, ietfnc, "unlock");
1562 node = lyd_new(data, ietfnc, "target");
1563 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
1564 if (!node) {
1565 lyd_free(data);
1566 return NC_MSG_ERROR;
1567 }
1568 break;
1569
1570 case NC_RPC_GET:
1571 rpc_g = (struct nc_rpc_get *)rpc;
1572
1573 data = lyd_new(NULL, ietfnc, "get");
1574 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001575 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vasko9161da52016-10-07 13:34:57 +02001576 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001577 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001578 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001579 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001580 lyd_insert_attr(node, NULL, "type", "xpath");
1581 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001582 }
1583 if (!node) {
1584 lyd_free(data);
1585 return NC_MSG_ERROR;
1586 }
1587 }
1588
1589 if (rpc_g->wd_mode) {
1590 if (!ietfncwd) {
1591 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL);
1592 if (!ietfncwd) {
Michal Vaskod083db62016-01-19 10:31:29 +01001593 ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001594 return NC_MSG_ERROR;
1595 }
1596 }
1597 switch (rpc_g->wd_mode) {
1598 case NC_WD_UNKNOWN:
1599 /* cannot get here */
1600 break;
1601 case NC_WD_ALL:
1602 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
1603 break;
1604 case NC_WD_ALL_TAG:
1605 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
1606 break;
1607 case NC_WD_TRIM:
1608 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
1609 break;
1610 case NC_WD_EXPLICIT:
1611 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
1612 break;
1613 }
1614 if (!node) {
1615 lyd_free(data);
1616 return NC_MSG_ERROR;
1617 }
1618 }
1619 break;
1620
1621 case NC_RPC_KILL:
1622 rpc_k = (struct nc_rpc_kill *)rpc;
1623
1624 data = lyd_new(NULL, ietfnc, "kill-session");
1625 sprintf(str, "%u", rpc_k->sid);
1626 lyd_new_leaf(data, ietfnc, "session-id", str);
1627 break;
1628
1629 case NC_RPC_COMMIT:
1630 rpc_com = (struct nc_rpc_commit *)rpc;
1631
1632 data = lyd_new(NULL, ietfnc, "commit");
1633 if (rpc_com->confirmed) {
1634 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
1635 }
1636
1637 if (rpc_com->confirm_timeout) {
1638 sprintf(str, "%u", rpc_com->confirm_timeout);
1639 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
1640 }
1641
1642 if (rpc_com->persist) {
1643 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
1644 if (!node) {
1645 lyd_free(data);
1646 return NC_MSG_ERROR;
1647 }
1648 }
1649
1650 if (rpc_com->persist_id) {
1651 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
1652 if (!node) {
1653 lyd_free(data);
1654 return NC_MSG_ERROR;
1655 }
1656 }
1657 break;
1658
1659 case NC_RPC_DISCARD:
1660 data = lyd_new(NULL, ietfnc, "discard-changes");
1661 break;
1662
1663 case NC_RPC_CANCEL:
1664 rpc_can = (struct nc_rpc_cancel *)rpc;
1665
1666 data = lyd_new(NULL, ietfnc, "cancel-commit");
1667 if (rpc_can->persist_id) {
1668 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
1669 if (!node) {
1670 lyd_free(data);
1671 return NC_MSG_ERROR;
1672 }
1673 }
1674 break;
1675
1676 case NC_RPC_VALIDATE:
1677 rpc_val = (struct nc_rpc_validate *)rpc;
1678
1679 data = lyd_new(NULL, ietfnc, "validate");
1680 node = lyd_new(data, ietfnc, "source");
1681 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02001682 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vasko9161da52016-10-07 13:34:57 +02001683 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01001684 } else {
1685 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
1686 }
1687 } else {
1688 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
1689 }
1690 if (!node) {
1691 lyd_free(data);
1692 return NC_MSG_ERROR;
1693 }
1694 break;
1695
1696 case NC_RPC_GETSCHEMA:
1697 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL);
1698 if (!ietfncmon) {
Michal Vaskod083db62016-01-19 10:31:29 +01001699 ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001700 return NC_MSG_ERROR;
1701 }
1702
1703 rpc_gs = (struct nc_rpc_getschema *)rpc;
1704
1705 data = lyd_new(NULL, ietfncmon, "get-schema");
1706 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
1707 if (!node) {
1708 lyd_free(data);
1709 return NC_MSG_ERROR;
1710 }
1711 if (rpc_gs->version) {
1712 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
1713 if (!node) {
1714 lyd_free(data);
1715 return NC_MSG_ERROR;
1716 }
1717 }
1718 if (rpc_gs->format) {
1719 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
1720 if (!node) {
1721 lyd_free(data);
1722 return NC_MSG_ERROR;
1723 }
1724 }
1725 break;
1726
1727 case NC_RPC_SUBSCRIBE:
1728 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL);
1729 if (!notifs) {
Michal Vaskod083db62016-01-19 10:31:29 +01001730 ERR("Session %u: missing notifications schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001731 return NC_MSG_ERROR;
1732 }
1733
1734 rpc_sub = (struct nc_rpc_subscribe *)rpc;
1735
1736 data = lyd_new(NULL, notifs, "create-subscription");
1737 if (rpc_sub->stream) {
1738 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
1739 if (!node) {
1740 lyd_free(data);
1741 return NC_MSG_ERROR;
1742 }
1743 }
1744
1745 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01001746 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vasko9161da52016-10-07 13:34:57 +02001747 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01001748 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01001749 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02001750 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01001751 lyd_insert_attr(node, NULL, "type", "xpath");
1752 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01001753 }
1754 if (!node) {
1755 lyd_free(data);
1756 return NC_MSG_ERROR;
1757 }
1758 }
1759
1760 if (rpc_sub->start) {
1761 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
1762 if (!node) {
1763 lyd_free(data);
1764 return NC_MSG_ERROR;
1765 }
1766 }
1767
1768 if (rpc_sub->stop) {
1769 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
1770 if (!node) {
1771 lyd_free(data);
1772 return NC_MSG_ERROR;
1773 }
1774 }
1775 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001776 default:
1777 ERRINT;
1778 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001779 }
1780
Michal Vaskoe8cfaa72016-11-21 10:19:43 +01001781 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 +01001782 lyd_free(data);
1783 return NC_MSG_ERROR;
1784 }
1785
Michal vasko50cc94f2016-10-04 13:46:20 +02001786 ret = nc_timedlock(session->ti_lock, timeout, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001787 if (ret == -1) {
1788 /* error */
1789 r = NC_MSG_ERROR;
1790 } else if (!ret) {
1791 /* blocking */
Michal Vasko086311b2016-01-08 09:53:11 +01001792 r = NC_MSG_WOULDBLOCK;
1793 } else {
1794 /* send RPC, store its message ID */
1795 r = nc_send_msg(session, data);
1796 cur_msgid = session->msgid;
1797 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001798 pthread_mutex_unlock(session->ti_lock);
Michal Vasko086311b2016-01-08 09:53:11 +01001799
1800 lyd_free(data);
1801
1802 if (r != NC_MSG_RPC) {
1803 return r;
1804 }
1805
1806 *msgid = cur_msgid;
1807 return NC_MSG_RPC;
1808}