blob: 738fbb171c34e3adc869d0225fd4c9e6fc7b2128 [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
Radek Krejcifd5b6682017-06-13 15:52:53 +020015#define _GNU_SOURCE
Michal Vasko086311b2016-01-08 09:53:11 +010016#include <assert.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010020#include <netinet/in.h>
Michal Vasko086311b2016-01-08 09:53:11 +010021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020025#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010026#include <sys/stat.h>
Radek Krejci62aa0642017-05-25 16:33:49 +020027#include <sys/syscall.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028#include <sys/types.h>
29#include <unistd.h>
30#include <arpa/inet.h>
31#include <poll.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020032#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010033
34#include <libyang/libyang.h>
35
Michal Vasko086311b2016-01-08 09:53:11 +010036#include "libnetconf.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010037#include "session_client.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010038#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010039
Michal Vasko80ef5d22016-01-18 09:21:02 +010040static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
41
Radek Krejci62aa0642017-05-25 16:33:49 +020042#ifdef NC_ENABLED_SSH
43int sshauth_hostkey_check(const char *hostname, ssh_session session, void *priv);
44char *sshauth_password(const char *username, const char *hostname, void *priv);
45char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
46char *sshauth_privkey_passphrase(const char* privkey_path, void *priv);
47#endif /* NC_ENABLED_SSH */
48
49static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
50static pthread_key_t nc_client_context_key;
51#ifdef __linux__
52static struct nc_client_context context_main = {
53 /* .opts zeroed */
54#ifdef NC_ENABLED_SSH
55 .ssh_opts = {
56 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
57 .auth_hostkey_check = sshauth_hostkey_check,
58 .auth_password = sshauth_password,
59 .auth_interactive = sshauth_interactive,
60 .auth_privkey_passphrase = sshauth_privkey_passphrase
61 },
62 .ssh_ch_opts = {
63 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
64 .auth_hostkey_check = sshauth_hostkey_check,
65 .auth_password = sshauth_password,
66 .auth_interactive = sshauth_interactive,
67 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020068 },
Radek Krejci62aa0642017-05-25 16:33:49 +020069#endif /* NC_ENABLED_SSH */
70 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020071 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020072};
73#endif
74
75static void
76nc_client_context_free(void *ptr)
77{
78 struct nc_client_context *c = (struct nc_client_context *)ptr;
79
Radek Krejci5cebc6b2017-05-26 13:24:38 +020080 if (--(c->refcount)) {
81 /* still used */
82 return;
83 }
84
Radek Krejci62aa0642017-05-25 16:33:49 +020085#ifdef __linux__
86 /* in __linux__ we use static memory in the main thread,
87 * so this check is for programs terminating the main()
88 * function by pthread_exit() :)
89 */
90 if (c != &context_main)
91#endif
92 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +020093 /* for the main thread the same is done in nc_client_destroy() */
Radek Krejci62aa0642017-05-25 16:33:49 +020094 nc_client_set_schema_searchpath(NULL);
95#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
96 nc_client_ch_del_bind(NULL, 0, 0);
97#endif
98#ifdef NC_ENABLED_SSH
99 nc_client_ssh_destroy_opts();
100#endif
101#ifdef NC_ENABLED_TLS
102 nc_client_tls_destroy_opts();
103#endif
104 free(c);
105 }
106}
107
108static void
109nc_client_context_createkey(void)
110{
111 int r;
112
113 /* initiate */
114 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN);
115 pthread_setspecific(nc_client_context_key, NULL);
116}
117
118struct nc_client_context *
119nc_client_context_location(void)
120{
121 struct nc_client_context *e;
122
123 pthread_once(&nc_client_context_once, nc_client_context_createkey);
124 e = pthread_getspecific(nc_client_context_key);
125 if (!e) {
126 /* prepare ly_err storage */
127#ifdef __linux__
128 if (getpid() == syscall(SYS_gettid)) {
129 /* main thread - use global variable instead of thread-specific variable. */
130 e = &context_main;
131 } else
132#endif /* __linux__ */
133 {
134 e = calloc(1, sizeof *e);
135 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200136 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200137#ifdef NC_ENABLED_SSH
138 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
139 e->ssh_opts.auth_pref[0].value = 3;
140 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
141 e->ssh_opts.auth_pref[1].value = 2;
142 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
143 e->ssh_opts.auth_pref[2].value = 1;
144 e->ssh_opts.auth_hostkey_check = sshauth_hostkey_check;
145 e->ssh_opts.auth_password = sshauth_password;
146 e->ssh_opts.auth_interactive = sshauth_interactive;
147 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
148
149 /* callhome settings are the same except the inverted auth methods preferences */
150 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
151 e->ssh_ch_opts.auth_pref[0].value = 1;
152 e->ssh_ch_opts.auth_pref[1].value = 2;
153 e->ssh_ch_opts.auth_pref[2].value = 3;
154#endif /* NC_ENABLED_SSH */
155 }
156 pthread_setspecific(nc_client_context_key, e);
157 }
158
159 return e;
160}
161
162#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100163
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200164API void *
165nc_client_get_thread_context(void)
166{
167 return nc_client_context_location();
168}
169
170API void
171nc_client_set_thread_context(void *context)
172{
173 struct nc_client_context *old, *new;
174
175 if (!context) {
176 ERRARG(context);
177 return;
178 }
179
180 new = (struct nc_client_context *)context;
181 old = nc_client_context_location();
182 if (old == new) {
183 /* nothing to change */
184 return;
185 }
186
187 /* replace old by new, increase reference counter in the newly set context */
188 nc_client_context_free(old);
189 new->refcount++;
190 pthread_setspecific(nc_client_context_key, new);
191}
192
Radek Krejcifd5b6682017-06-13 15:52:53 +0200193int
194nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
195{
196 /* assign context (dicionary needed for handshake) */
197 if (!ctx) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200198 ctx = ly_ctx_new(NULL, LY_CTX_NOYANGLIBRARY);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200199 if (!ctx) {
200 return EXIT_FAILURE;
201 }
202
203 /* user path must be first, the first path is used to store schemas retreived via get-schema */
204 if (client_opts.schema_searchpath) {
205 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
206 }
Michal Vaskoff7e3562018-02-15 13:41:22 +0100207 ly_ctx_set_searchdir(ctx, NC_SCHEMAS_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200208
209 /* set callback for getting schemas, if provided */
210 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
211 } else {
212 session->flags |= NC_SESSION_SHAREDCTX;
213 }
214
215 session->ctx = ctx;
216
217 return EXIT_SUCCESS;
218}
219
Michal Vasko086311b2016-01-08 09:53:11 +0100220API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100221nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100222{
Michal Vasko3031aae2016-01-27 16:07:18 +0100223 if (client_opts.schema_searchpath) {
224 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100225 }
Michal Vasko086311b2016-01-08 09:53:11 +0100226
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100227 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100228 client_opts.schema_searchpath = strdup(path);
229 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100230 ERRMEM;
231 return 1;
232 }
233 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100234 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100235 }
236
237 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100238}
239
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100240API const char *
241nc_client_get_schema_searchpath(void)
242{
243 return client_opts.schema_searchpath;
244}
245
Radek Krejcifd5b6682017-06-13 15:52:53 +0200246API int
247nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100248{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200249 client_opts.schema_clb = clb;
250 if (clb) {
251 client_opts.schema_clb_data = user_data;
252 } else {
253 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100254 }
255
256 return 0;
257}
258
Radek Krejcifd5b6682017-06-13 15:52:53 +0200259API ly_module_imp_clb
260nc_client_get_schema_callback(void **user_data)
261{
262 if (user_data) {
263 (*user_data) = client_opts.schema_clb_data;
264 }
265 return client_opts.schema_clb;
266}
267
Radek Krejci65ef6d52018-08-16 16:35:02 +0200268
269struct schema_info {
270 char *name;
271 char *revision;
272 struct {
273 char *name;
274 char *revision;
275 } *submodules;
276 struct ly_set features;
277 int implemented;
278};
279
280struct clb_data_s {
281 void *user_data;
282 ly_module_imp_clb user_clb;
283 struct schema_info *schemas;
284 struct nc_session *session;
285 int has_get_schema;
286};
287
288static char *
289retrieve_schema_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
290 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100291{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200292 char *localfile = NULL;
293 FILE *f;
294 long length, l;
295 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100296
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200297 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
298 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
299 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200300 return NULL;
301 }
302 if (localfile) {
303 VRB("Session %u: reading schema from localfile \"%s\".", clb_data->session->id, localfile);
304 f = fopen(localfile, "r");
305 if (!f) {
306 ERR("Session %u: unable to open \"%s\" file to get schema (%s).",
307 clb_data->session->id, localfile, strerror(errno));
308 free(localfile);
309 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100310 }
Michal Vasko086311b2016-01-08 09:53:11 +0100311
Radek Krejci65ef6d52018-08-16 16:35:02 +0200312 fseek(f, 0, SEEK_END);
313 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200314 if (length < 0) {
315 ERR("Session %u: unable to get size of schema file \"%s\".",
316 clb_data->session->id, localfile);
317 free(localfile);
318 fclose(f);
319 return NULL;
320 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200321 fseek(f, 0, SEEK_SET);
322
323 model_data = malloc(length + 1);
324 if (!model_data) {
325 ERRMEM;
326 } else if ((l = fread(model_data, 1, length, f)) != length) {
327 ERR("Session %u: reading schema from \"%s\" failed (%d bytes read, but %d expected).",
328 clb_data->session->id, localfile, l, length);
329 free(model_data);
330 model_data = NULL;
331 } else {
332 /* terminating NULL byte */
333 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100334 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200335 fclose(f);
336 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100337 }
338
Radek Krejci65ef6d52018-08-16 16:35:02 +0200339 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100340}
341
342static char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200343retrieve_schema_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
344 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100345{
Michal Vasko086311b2016-01-08 09:53:11 +0100346 struct nc_rpc *rpc;
347 struct nc_reply *reply;
348 struct nc_reply_data *data_rpl;
Michal Vasko998ba412016-09-16 12:00:07 +0200349 struct nc_reply_error *error_rpl;
Radek Krejci539efb62016-08-24 15:05:16 +0200350 struct lyd_node_anydata *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100351 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100352 uint64_t msgid;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200353 char *localfile = NULL;
354 FILE *f;
355 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100356
Radek Krejci65ef6d52018-08-16 16:35:02 +0200357 VRB("Session %u: reading schema from server via get-schema.", clb_data->session->id);
358 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100359
Radek Krejci65ef6d52018-08-16 16:35:02 +0200360 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100361 usleep(1000);
362 }
363 if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200364 ERR("Session %u: failed to send the <get-schema> RPC.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100365 nc_rpc_free(rpc);
366 return NULL;
367 }
368
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200369 do {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200370 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200371 } while (msg == NC_MSG_NOTIF);
Michal Vasko086311b2016-01-08 09:53:11 +0100372 nc_rpc_free(rpc);
373 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200374 ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100375 return NULL;
376 } else if (msg == NC_MSG_ERROR) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200377 ERR("Session %u: failed to receive a reply to <get-schema>.", clb_data->session->id);
Michal Vasko086311b2016-01-08 09:53:11 +0100378 return NULL;
379 }
380
Michal Vasko998ba412016-09-16 12:00:07 +0200381 switch (reply->type) {
382 case NC_RPL_OK:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200383 ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200384 nc_reply_free(reply);
385 return NULL;
386 case NC_RPL_DATA:
387 /* fine */
388 break;
389 case NC_RPL_ERROR:
390 error_rpl = (struct nc_reply_error *)reply;
391 if (error_rpl->count) {
392 ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200393 clb_data->session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
Michal Vasko998ba412016-09-16 12:00:07 +0200394 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200395 ERR("Session %u: unexpected reply error to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko998ba412016-09-16 12:00:07 +0200396 }
397 nc_reply_free(reply);
398 return NULL;
399 case NC_RPL_NOTIF:
Radek Krejci65ef6d52018-08-16 16:35:02 +0200400 ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", clb_data->session->id);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100401 nc_reply_free(reply);
402 return NULL;
403 }
404
Michal Vasko086311b2016-01-08 09:53:11 +0100405 data_rpl = (struct nc_reply_data *)reply;
Michal Vasko1c042702018-11-29 08:27:51 +0100406 if (!data_rpl->data || (data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema")
Michal Vaskob2583f12016-05-12 11:40:23 +0200407 || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200408 ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", clb_data->session->id);
Michal Vaskob2583f12016-05-12 11:40:23 +0200409 nc_reply_free(reply);
410 return NULL;
411 }
Radek Krejci539efb62016-08-24 15:05:16 +0200412 get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child;
413 switch (get_schema_data->value_type) {
414 case LYD_ANYDATA_CONSTSTRING:
415 case LYD_ANYDATA_STRING:
Michal Vaskob2583f12016-05-12 11:40:23 +0200416 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200417 break;
418 case LYD_ANYDATA_DATATREE:
419 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS);
420 break;
421 case LYD_ANYDATA_XML:
422 lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS);
423 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200424 case LYD_ANYDATA_JSON:
425 case LYD_ANYDATA_JSOND:
Michal Vasko94868ed2016-09-29 10:33:38 +0200426 case LYD_ANYDATA_SXML:
427 case LYD_ANYDATA_SXMLD:
Michal Vaskod838d292018-08-15 11:39:05 +0200428 case LYD_ANYDATA_LYB:
429 case LYD_ANYDATA_LYBD:
Radek Krejci03438ec2016-09-21 14:12:26 +0200430 ERRINT;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200431 nc_reply_free(reply);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200432 }
Michal Vasko086311b2016-01-08 09:53:11 +0100433 nc_reply_free(reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100434
Radek Krejci488b0702018-08-29 14:47:15 +0200435 if (model_data && !model_data[0]) {
436 /* empty data */
437 free(model_data);
438 model_data = NULL;
439 }
440
Radek Krejcifd5b6682017-06-13 15:52:53 +0200441 /* try to store the model_data into local schema repository */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200442 if (model_data) {
443 *format = LYS_IN_YANG;
444 if (client_opts.schema_searchpath) {
445 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name,
446 rev ? "@" : "", rev ? rev : "") == -1) {
447 ERRMEM;
Michal Vaskof945da52018-02-15 08:45:13 +0100448 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200449 f = fopen(localfile, "w");
450 if (!f) {
451 WRN("Unable to store \"%s\" as a local copy of schema retrieved via <get-schema> (%s).",
452 localfile, strerror(errno));
453 } else {
454 fputs(model_data, f);
455 fclose(f);
456 }
457 free(localfile);
Michal Vaskof945da52018-02-15 08:45:13 +0100458 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200459 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200460 }
461
Michal Vasko086311b2016-01-08 09:53:11 +0100462 return model_data;
463}
464
Jan Kundrát35972df2018-09-06 19:00:01 +0200465static void free_with_user_data(void *data, void *user_data)
466{
467 free(data);
468 (void)user_data;
469}
470
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200471static const char *
Radek Krejci65ef6d52018-08-16 16:35:02 +0200472retrieve_schema_data(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Jan Kundrát35972df2018-09-06 19:00:01 +0200473 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200474{
475 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
476 unsigned int u, v, match = 1;
477 const char *name = NULL, *rev = NULL;
478 char *model_data = NULL;
479
480 /* get and check the final name and revision of the schema to be retrieved */
481 if (!mod_rev || !mod_rev[0]) {
482 /* newest revision requested - get the newest revision from the list of available modules on server */
483 match = 0;
484 for (u = 0; clb_data->schemas[u].name; ++u) {
485 if (strcmp(mod_name, clb_data->schemas[u].name)) {
486 continue;
487 }
488 if (!match || strcmp(mod_rev, clb_data->schemas[u].revision) > 0) {
489 mod_rev = clb_data->schemas[u].revision;
490 }
491 match = u + 1;
492 }
493 if (!match) {
494 WRN("Session %u: unable to identify revision of the schema \"%s\" from the available server side information.",
495 clb_data->session->id, mod_name);
496 }
497 }
498 if (submod_name) {
499 name = submod_name;
500 if (sub_rev) {
501 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200502 } else if (match) {
503 if (!clb_data->schemas[match - 1].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200504 WRN("Session %u: Unable to identify revision of the requested submodule \"%s\", in schema \"%s\", from the available server side information.",
Radek Krejci65ef6d52018-08-16 16:35:02 +0200505 clb_data->session->id, submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200506 } else {
507 for (v = 0; clb_data->schemas[match - 1].submodules[v].name; ++v) {
508 if (!strcmp(submod_name, clb_data->schemas[match - 1].submodules[v].name)) {
509 rev = sub_rev = clb_data->schemas[match - 1].submodules[v].revision;
510 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200511 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200512 if (!rev) {
513 ERR("Session %u: requested submodule \"%s\" is not known for schema \"%s\" on server side.",
514 clb_data->session->id, submod_name, mod_name);
515 return NULL;
516 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200517 }
518 }
519 } else {
520 name = mod_name;
521 rev = mod_rev;
522 }
523
524 VRB("Session %u: retreiving data for schema \"%s\", revision \"%s\".", clb_data->session->id, name, rev);
525
526 if (match) {
527 /* we have enough information to avoid communication with server and try to get
528 * the schema locally */
529
530 /* 1. try to get data locally */
531 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
532
533 /* 2. try to use <get-schema> */
534 if (!model_data && clb_data->has_get_schema) {
535 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
536 }
537 } else {
538 /* we are unsure which revision of the schema we should load, so first try to get
539 * the newest revision from the server via get-schema and only if the server does not
540 * implement get-schema, try to load the newest revision locally. This is imperfect
541 * solution, but there are situation when a client does not know what revision is
542 * actually implemented by the server. */
543
544 /* 1. try to use <get-schema> */
545 if (clb_data->has_get_schema) {
546 model_data = retrieve_schema_data_getschema(name, rev, clb_data, format);
547 }
548
549 /* 2. try to get data locally */
550 if (!model_data) {
551 model_data = retrieve_schema_data_localfile(name, rev, clb_data, format);
552 }
553 }
554
555 /* 3. try to use user callback */
556 if (!model_data && clb_data->user_clb) {
557 VRB("Session %u: reading schema via user callback.", clb_data->session->id);
558 return clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format, free_module_data);
559 }
560
Jan Kundrát35972df2018-09-06 19:00:01 +0200561 *free_module_data = free_with_user_data;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200562 return model_data;
563}
564
Michal Vaskoceae0152018-02-14 16:03:59 +0100565static int
Radek Krejci65ef6d52018-08-16 16:35:02 +0200566nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, struct schema_info *schemas,
Radek Krejci9aa1e352018-05-16 16:05:42 +0200567 ly_module_imp_clb user_clb, void *user_data, int has_get_schema, const struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100568{
569 int ret = 0;
570 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200571 const char *module_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200572 LYS_INFORMAT format;
Jan Kundrát35972df2018-09-06 19:00:01 +0200573 void (*free_module_data)(void*, void*) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200574 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100575
Radek Krejci65ef6d52018-08-16 16:35:02 +0200576 *mod = NULL;
577 if (revision) {
578 *mod = ly_ctx_get_module(session->ctx, name, revision, 0);
579 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100580 if (*mod) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200581 if (!(*mod)->implemented) {
Michal Vaskoceae0152018-02-14 16:03:59 +0100582 /* make the present module implemented */
583 if (lys_set_implemented(*mod)) {
584 ERR("Failed to implement model \"%s\".", (*mod)->name);
585 ret = -1;
586 }
587 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200588 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100589 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200590 clb_data.has_get_schema = has_get_schema;
591 clb_data.schemas = schemas;
592 clb_data.session = session;
593 clb_data.user_clb = user_clb;
594 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100595
596 /* clear all the errors and just collect them for now */
597 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200598 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100599
Radek Krejci65ef6d52018-08-16 16:35:02 +0200600 /* get module data */
601 module_data = retrieve_schema_data(name, revision, NULL, NULL, &clb_data, &format, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100602
Radek Krejci65ef6d52018-08-16 16:35:02 +0200603 if (module_data) {
604 /* parse the schema */
605 ly_ctx_set_module_imp_clb(session->ctx, retrieve_schema_data, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100606
Radek Krejci65ef6d52018-08-16 16:35:02 +0200607 *mod = lys_parse_mem(session->ctx, module_data, format);
608 if (*free_module_data) {
Jan Kundrát35972df2018-09-06 19:00:01 +0200609 (*free_module_data)((char*)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100610 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100611
Radek Krejci65ef6d52018-08-16 16:35:02 +0200612 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
613 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100614
Michal Vaskodbf7c272018-02-19 09:07:59 +0100615 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200616 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100617 if (!(*mod)) {
618 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
619 ly_err_print(eitem);
620 }
621 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200622 } else {
623 /* print only warnings */
624 for (eitem = ly_err_first(session->ctx); eitem && eitem->next; eitem = eitem->next) {
625 if (eitem->level == LY_LLWRN) {
626 ly_err_print(eitem);
627 }
628 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100629 }
630
Michal Vaskodbf7c272018-02-19 09:07:59 +0100631 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100632 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100633 }
634
635 return ret;
636}
637
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638static void
639free_schema_info(struct schema_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200640{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200641 unsigned int u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200642
Radek Krejci65ef6d52018-08-16 16:35:02 +0200643 if (!list) {
644 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200645 }
646
Radek Krejci65ef6d52018-08-16 16:35:02 +0200647 for (u = 0; list[u].name; ++u) {
648 free(list[u].name);
649 free(list[u].revision);
650 for (v = 0; v < list[u].features.number; ++v) {
651 free(list[u].features.set.g[v]);
652 }
653 free(list[u].features.set.g);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200654 if (list[u].submodules) {
655 for (v = 0; list[u].submodules[v].name; ++v) {
656 free(list[u].submodules[v].name);
657 free(list[u].submodules[v].revision);
658 }
659 free(list[u].submodules);
660 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200661 }
662 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200663}
664
Radek Krejci235d8cb2018-08-17 14:04:32 +0200665
666static int
667build_schema_info_yl(struct nc_session *session, struct schema_info **result)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200668{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200669 struct nc_rpc *rpc = NULL;
670 struct nc_reply *reply = NULL;
671 struct nc_reply_error *error_rpl;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200672 struct lyd_node *yldata = NULL;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200673 NC_MSG_TYPE msg;
674 uint64_t msgid;
Radek Krejcia8dfaea2018-08-17 10:55:09 +0200675 struct ly_set *modules = NULL;
Radek Krejci2d088832018-08-21 13:40:14 +0200676 unsigned int u, v, submodules_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200677 struct lyd_node *iter, *child;
678 struct lys_module *mod;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200679 int ret = EXIT_SUCCESS;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200680
681 /* get yang-library data from the server */
Radek Krejci261737f2018-08-21 09:22:34 +0200682 if (nc_session_cpblt(session, "urn:ietf:params:netconf:capability:xpath:1.0")) {
683 rpc = nc_rpc_get("/ietf-yang-library:*", 0, NC_PARAMTYPE_CONST);
684 } else {
685 rpc = nc_rpc_get("<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", 0, NC_PARAMTYPE_CONST);
686 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200687 if (!rpc) {
688 goto cleanup;
689 }
690
691 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
692 usleep(1000);
693 }
694 if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200695 WRN("Session %u: failed to send request for yang-library data.",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200696 session->id);
697 goto cleanup;
698 }
699
700 do {
701 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, 0, &reply);
702 } while (msg == NC_MSG_NOTIF);
703 if (msg == NC_MSG_WOULDBLOCK) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200704 WRN("Session %u: timeout for receiving reply to a <get> yang-library data expired.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200705 goto cleanup;
706 } else if (msg == NC_MSG_ERROR) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200707 WRN("Session %u: failed to receive a reply to <get> of yang-library data.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200708 goto cleanup;
709 }
710
711 switch (reply->type) {
712 case NC_RPL_OK:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200713 WRN("Session %u: unexpected reply OK to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200714 goto cleanup;
715 case NC_RPL_DATA:
716 /* fine */
717 break;
718 case NC_RPL_ERROR:
719 error_rpl = (struct nc_reply_error *)reply;
720 if (error_rpl->count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200721 WRN("Session %u: error reply to a yang-library <get> RPC (tag \"%s\", message \"%s\").",
Radek Krejcifd5b6682017-06-13 15:52:53 +0200722 session->id, error_rpl->err[0].tag, error_rpl->err[0].message);
723 } else {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200724 WRN("Session %u: unexpected reply error to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200725 }
726 goto cleanup;
727 case NC_RPL_NOTIF:
Radek Krejci235d8cb2018-08-17 14:04:32 +0200728 WRN("Session %u: unexpected reply notification to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200729 goto cleanup;
730 }
731
Radek Krejci65ef6d52018-08-16 16:35:02 +0200732 yldata = ((struct nc_reply_data *)reply)->data;
733 if (!yldata || strcmp(yldata->schema->module->name, "ietf-yang-library")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200734 WRN("Session %u: unexpected data in reply to a yang-library <get> RPC.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200735 goto cleanup;
736 }
737
Radek Krejci65ef6d52018-08-16 16:35:02 +0200738 modules = lyd_find_path(yldata, "/ietf-yang-library:modules-state/module");
Radek Krejciac919522018-08-17 11:00:17 +0200739 if (!modules) {
Radek Krejci2d088832018-08-21 13:40:14 +0200740 WRN("Session %u: no module information in reply to a yang-library <get> RPC.", session->id);
741 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200742 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200743
Radek Krejci2d088832018-08-21 13:40:14 +0200744 (*result) = calloc(modules->number + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200745 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200746 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200747 ret = EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200748 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200749 }
750
Radek Krejci2d088832018-08-21 13:40:14 +0200751 for (u = 0; u < modules->number; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200752 submodules_count = 0;
753 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200754 LY_TREE_FOR(modules->set.d[u]->child, iter) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200755 if (iter->schema->module != mod) {
756 /* ignore node from other schemas (augments) */
757 continue;
758 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200759 if (!((struct lyd_node_leaf_list *)iter)->value_str || !((struct lyd_node_leaf_list *)iter)->value_str[0]) {
760 /* ignore empty nodes */
761 continue;
762 }
763 if (!strcmp(iter->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200764 (*result)[u].name = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200765 } else if (!strcmp(iter->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200766 (*result)[u].revision = strdup(((struct lyd_node_leaf_list *)iter)->value_str);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200767 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200768 (*result)[u].implemented = !strcmp(((struct lyd_node_leaf_list *)iter)->value_str, "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200769 } else if (!strcmp(iter->schema->name, "feature")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200770 ly_set_add(&(*result)[u].features, (void *)strdup(((struct lyd_node_leaf_list *)iter)->value_str), LY_SET_OPT_USEASLIST);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200771 } else if (!strcmp(iter->schema->name, "submodule")) {
772 submodules_count++;
773 }
774 }
775
776 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200777 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
778 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200779 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200780 free_schema_info(*result);
781 *result = NULL;
782 ret = EXIT_FAILURE;
783 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200784 } else {
785 v = 0;
786 LY_TREE_FOR(modules->set.d[u]->child, iter) {
787 mod = ((struct lyd_node *)modules->set.d[u])->schema->module;
788 if (mod == iter->schema->module && !strcmp(iter->schema->name, "submodule")) {
789 LY_TREE_FOR(iter->child, child) {
790 if (mod != child->schema->module) {
791 continue;
792 } else if (!strcmp(child->schema->name, "name")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200793 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200794 } else if (!strcmp(child->schema->name, "revision")) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200795 (*result)[u].submodules[v].name = strdup(((struct lyd_node_leaf_list *)child)->value_str);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200796 }
797 }
798 }
799 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200800 }
801 }
802 }
803
Radek Krejcifd5b6682017-06-13 15:52:53 +0200804cleanup:
805 nc_rpc_free(rpc);
806 nc_reply_free(reply);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200807 ly_set_free(modules);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200808
Radek Krejci235d8cb2018-08-17 14:04:32 +0200809 if (session->status != NC_STATUS_RUNNING) {
810 /* something bad heppened, discard the session */
811 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
812 ret = EXIT_FAILURE;
813 }
814
815 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200816}
817
Radek Krejci235d8cb2018-08-17 14:04:32 +0200818static int
819build_schema_info_cpblts(char **cpblts, struct schema_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +0200820{
821 unsigned int u, v;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200822 char *module_cpblt, *ptr, *ptr2;
823
824 for (u = 0; cpblts[u]; ++u);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200825 (*result) = calloc(u + 1, sizeof **result);
826 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200827 ERRMEM;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200828 return EXIT_FAILURE;
Radek Krejcic9390502018-08-17 10:23:13 +0200829 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200830
831 for (u = v = 0; cpblts[u]; ++u) {
832 module_cpblt = strstr(cpblts[u], "module=");
833 /* this capability requires a module */
834 if (!module_cpblt) {
835 continue;
836 }
837
838 /* get module's name */
839 ptr = (char *)module_cpblt + 7;
840 ptr2 = strchr(ptr, '&');
841 if (!ptr2) {
842 ptr2 = ptr + strlen(ptr);
843 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200844 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200845
846 /* get module's revision */
847 ptr = strstr(module_cpblt, "revision=");
848 if (ptr) {
849 ptr += 9;
850 ptr2 = strchr(ptr, '&');
851 if (!ptr2) {
852 ptr2 = ptr + strlen(ptr);
853 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200854 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200855 }
856
857 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200858 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200859
860 /* get module's features */
861 ptr = strstr(module_cpblt, "features=");
862 if (ptr) {
863 ptr += 9;
864 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
865 if (*ptr == ',') {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200866 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200867 ptr2 = ptr + 1;
868 }
869 }
870 /* the last one */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200871 ly_set_add(&(*result)[v].features, (void *)strndup(ptr2, ptr - ptr2), LY_SET_OPT_USEASLIST);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200872 }
873 ++v;
874 }
Radek Krejci235d8cb2018-08-17 14:04:32 +0200875
876 return EXIT_SUCCESS;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200877}
878
879static int
880nc_ctx_fill(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
881{
882 int ret = EXIT_FAILURE;
883 const struct lys_module *mod;
884 unsigned int u, v;
885
886 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +0200887 /* skip import-only modules */
888 if (!modules[u].implemented) {
889 continue;
890 }
891
Radek Krejci65ef6d52018-08-16 16:35:02 +0200892 /* we can continue even if it fails */
893 nc_ctx_load_module(session, modules[u].name, modules[u].revision, modules, user_clb, user_data, has_get_schema, &mod);
894
895 if (!mod) {
896 if (session->status != NC_STATUS_RUNNING) {
897 /* something bad heppened, discard the session */
898 ERR("Session %d: invalid session, discarding.", nc_session_get_id(session));
899 goto cleanup;
900 }
901
902 /* all loading ways failed, the schema will be ignored in the received data */
903 WRN("Failed to load schema \"%s@%s\".", modules[u].name, modules[u].revision ? modules[u].revision : "<latest>");
904 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
905 } else {
906 /* set features - first disable all to enable specified then */
907 lys_features_disable(mod, "*");
908 for (v = 0; v < modules[u].features.number; v++) {
909 lys_features_enable(mod, (const char*)modules[u].features.set.g[v]);
910 }
911 }
Michal Vasko60f66602017-10-17 13:52:18 +0200912 }
913
Radek Krejci65ef6d52018-08-16 16:35:02 +0200914 /* done */
915 ret = EXIT_SUCCESS;
916
917cleanup:
918
Radek Krejcifd5b6682017-06-13 15:52:53 +0200919 return ret;
920}
921
Radek Krejci65ef6d52018-08-16 16:35:02 +0200922static int
923nc_ctx_fill_ietf_netconf(struct nc_session *session, struct schema_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema)
924{
925 unsigned int u, v;
926 const struct lys_module *ietfnc;
927
928 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
929 if (!ietfnc) {
930 nc_ctx_load_module(session, "ietf-netconf", NULL, modules, user_clb, user_data, has_get_schema, &ietfnc);
931 if (!ietfnc) {
932 WRN("Unable to find correct \"ietf-netconf\" schema, trying to use backup from \"%s\".", NC_SCHEMAS_DIR"/ietf-netconf.yin");
933 ietfnc = lys_parse_path(session->ctx, NC_SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN);
934 }
935 }
936 if (!ietfnc) {
937 ERR("Loading base NETCONF schema failed.");
938 return 1;
939 }
940
941 /* set supported capabilities from ietf-netconf */
942 for (u = 0; modules[u].name; ++u) {
943 if (strcmp(modules[u].name, "ietf-netconf") || !modules[u].implemented) {
944 continue;
945 }
946
947 lys_features_disable(ietfnc, "*");
948 for (v = 0; v < modules[u].features.number; v++) {
949 lys_features_enable(ietfnc, (const char*)modules[u].features.set.g[v]);
950 }
951 }
952
953 return 0;
954}
955
Michal Vasko086311b2016-01-08 09:53:11 +0100956int
957nc_ctx_check_and_fill(struct nc_session *session)
958{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200959 int i, get_schema_support = 0, yanglib_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +0100960 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100961 void *old_data = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +0200962 const struct lys_module *mod = NULL;
963 char *revision;
Radek Krejcib056ff82018-08-20 15:58:39 +0200964 struct schema_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100965
Michal Vasko2e6defd2016-10-07 15:48:15 +0200966 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100967
Radek Krejci65ef6d52018-08-16 16:35:02 +0200968 /* store the original user's callback, we will be switching between local search, get-schema and user callback */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200969 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200970
Radek Krejci65ef6d52018-08-16 16:35:02 +0200971 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
972 * schemas only to those present on the server side */
973 ly_ctx_set_disable_searchdirs(session->ctx);
974
975 /* our callback is set later with appropriate data */
976 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
977
978 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200979 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200980 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200981 get_schema_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200982 if (yanglib_support) {
983 break;
984 }
Radek Krejcicde410d2018-08-21 09:56:49 +0200985 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:1.0", 51)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +0200986 yanglib_support = 1 + i;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200987 if (get_schema_support) {
988 break;
989 }
Michal Vasko086311b2016-01-08 09:53:11 +0100990 }
991 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200992
993 /* get information about server's schemas from capabilities list until we will have yang-library */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200994 if (build_schema_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
995 ERR("Session %u: unable to get server's schema information from the <hello>'s capabilities.", session->id);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200996 goto cleanup;
997 }
998
Michal Vasko086311b2016-01-08 09:53:11 +0100999 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001000 if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001001 if (nc_ctx_load_module(session, "ietf-netconf-monitoring", NULL, server_modules, old_clb, old_data, 0, &mod)) {
1002 WRN("Session %u: loading NETCONF monitoring schema failed, cannot use <get-schema>.", session->id);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001003 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001004 }
1005 }
1006
1007 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001008 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001009 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001010 }
1011
Radek Krejci65ef6d52018-08-16 16:35:02 +02001012 /* get correct version of ietf-yang-library into context */
1013 if (yanglib_support) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001014 /* use get schema to get server's ietf-yang-library */
1015 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1016 if (!revision) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001017 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, missing revision in NETCONF <hello> message.", session->id);
1018 WRN("Session %u: unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001019 yanglib_support = 0;
1020 } else {
1021 revision = strndup(&revision[9], 10);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001022 if (nc_ctx_load_module(session, "ietf-yang-library", revision, server_modules, old_clb, old_data, get_schema_support, &mod)) {
1023 WRN("Session %u: loading NETCONF ietf-yang-library schema failed, unable to automatically use <get-schema>.", session->id);
Radek Krejcib1d250e2018-04-12 13:54:18 +02001024 yanglib_support = 0;
1025 }
1026 free(revision);
1027 }
1028 }
1029
Radek Krejci65ef6d52018-08-16 16:35:02 +02001030 /* prepare structured information about server's schemas */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001031 if (yanglib_support) {
Radek Krejcif0633792018-08-20 15:12:09 +02001032 if (build_schema_info_yl(session, &sm)) {
1033 goto cleanup;
1034 } else if (!sm) {
1035 VRB("Session %u: trying to use capabilities instead of ietf-yang-library data.", session->id);
1036 } else {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001037 /* prefer yang-library information, currently we have it from capabilities used for getting correct yang-library schema */
1038 free_schema_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001039 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001040 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001041 }
Michal Vaskoef578332016-01-25 13:20:09 +01001042
Radek Krejci65ef6d52018-08-16 16:35:02 +02001043 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1044 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001045 }
1046
Radek Krejcifd5b6682017-06-13 15:52:53 +02001047 /* succsess */
1048 ret = 0;
1049
Michal Vaskoeee99412016-11-21 10:19:43 +01001050 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001051 WRN("Session %u: some models failed to be loaded, any data from these models (and any other unknown) will be ignored.", session->id);
Michal Vaskoef578332016-01-25 13:20:09 +01001052 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001053
1054cleanup:
Radek Krejci65ef6d52018-08-16 16:35:02 +02001055 free_schema_info(server_modules);
1056
Radek Krejcifd5b6682017-06-13 15:52:53 +02001057 /* set user callback back */
1058 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001059 ly_ctx_unset_disable_searchdirs(session->ctx);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001060
Michal Vaskoef578332016-01-25 13:20:09 +01001061 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001062}
1063
1064API struct nc_session *
1065nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1066{
Michal Vaskod083db62016-01-19 10:31:29 +01001067 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001068
Michal Vasko45e53ae2016-04-07 11:46:03 +02001069 if (fdin < 0) {
1070 ERRARG("fdin");
1071 return NULL;
1072 } else if (fdout < 0) {
1073 ERRARG("fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001074 return NULL;
1075 }
1076
1077 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001078 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001079 if (!session) {
1080 ERRMEM;
1081 return NULL;
1082 }
1083 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001084
1085 /* transport specific data */
1086 session->ti_type = NC_TI_FD;
1087 session->ti.fd.in = fdin;
1088 session->ti.fd.out = fdout;
1089
1090 /* assign context (dicionary needed for handshake) */
1091 if (!ctx) {
Radek Krejci65ef6d52018-08-16 16:35:02 +02001092 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001093 /* definitely should not happen, but be ready */
Radek Krejci3222b7d2017-09-21 16:04:30 +02001094 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001095 /* that's just it */
1096 goto fail;
1097 }
Michal Vasko086311b2016-01-08 09:53:11 +01001098 } else {
1099 session->flags |= NC_SESSION_SHAREDCTX;
1100 }
1101 session->ctx = ctx;
1102
1103 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001104 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001105 goto fail;
1106 }
1107 session->status = NC_STATUS_RUNNING;
1108
Michal Vaskoef578332016-01-25 13:20:09 +01001109 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001110 goto fail;
1111 }
1112
1113 return session;
1114
1115fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001116 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001117 return NULL;
1118}
1119
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001120API struct nc_session *
1121nc_connect_unix(const char *address, struct ly_ctx *ctx)
1122{
1123 struct nc_session *session = NULL;
1124 struct sockaddr_un sun;
1125 const struct passwd *pw;
1126 char *username;
1127 int sock = -1;
1128
1129 if (address == NULL) {
1130 ERRARG("address");
1131 return NULL;
1132 }
1133
1134 pw = getpwuid(geteuid());
1135 if (pw == NULL) {
1136 ERR("Failed to find username for euid=%u.\n", geteuid());
1137 goto fail;
1138 }
1139
1140 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1141 if (sock < 0) {
1142 ERR("Failed to create socket (%s).", strerror(errno));
1143 goto fail;
1144 }
1145
1146 memset(&sun, 0, sizeof(sun));
1147 sun.sun_family = AF_UNIX;
1148 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1149
1150 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
1151 ERR("cannot connect to sock server %s (%s)",
1152 address, strerror(errno));
1153 goto fail;
1154 }
1155
1156 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
1157 ERR("Fcntl failed (%s).", strerror(errno));
1158 goto fail;
1159 }
1160
1161 /* prepare session structure */
1162 session = nc_new_session(NC_CLIENT, 0);
1163 if (!session) {
1164 ERRMEM;
1165 goto fail;
1166 }
1167 session->status = NC_STATUS_STARTING;
1168
1169 /* transport specific data */
1170 session->ti_type = NC_TI_UNIX;
1171 session->ti.unixsock.sock = sock;
1172 sock = -1; /* do not close sock in fail label anymore */
1173
1174 /* assign context (dictionary needed for handshake) */
1175 if (!ctx) {
1176 ctx = ly_ctx_new(NC_SCHEMAS_DIR, LY_CTX_NOYANGLIBRARY);
1177 /* definitely should not happen, but be ready */
1178 if (!ctx && !(ctx = ly_ctx_new(NULL, 0))) {
1179 /* that's just it */
1180 goto fail;
1181 }
1182 } else {
1183 session->flags |= NC_SESSION_SHAREDCTX;
1184 }
1185 session->ctx = ctx;
1186
1187 session->path = lydict_insert(ctx, address, 0);
1188
1189 username = strdup(pw->pw_name);
1190 if (username == NULL) {
1191 ERRMEM;
1192 goto fail;
1193 }
1194 session->username = lydict_insert_zc(ctx, username);
1195
1196 /* NETCONF handshake */
1197 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1198 goto fail;
1199 }
1200 session->status = NC_STATUS_RUNNING;
1201
1202 if (nc_ctx_check_and_fill(session) == -1) {
1203 goto fail;
1204 }
1205
1206 return session;
1207
1208fail:
1209 nc_session_free(session, NULL);
1210 if (sock >= 0)
1211 close(sock);
1212 return NULL;
1213}
1214
Frank Rimpler9f838b02018-07-25 06:44:03 +00001215/*
1216 Helper for a non-blocking connect (which is required because of the locking
1217 concept for e.g. call home settings). For more details see nc_sock_connect().
1218 */
1219static int
1220_non_blocking_connect(int timeout, int* sock_pending, struct addrinfo *res)
Michal Vasko086311b2016-01-08 09:53:11 +01001221{
Frank Rimplerc7a2acb2018-08-06 07:31:08 +00001222 int flags, ret=0;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001223 int sock = -1;
1224 fd_set wset;
1225 struct timeval ts;
1226 int error = 0;
1227 socklen_t len = sizeof(int);
Michal Vasko086311b2016-01-08 09:53:11 +01001228
Frank Rimpler9f838b02018-07-25 06:44:03 +00001229 if (sock_pending && *sock_pending != -1) {
1230 VRB("Trying to connect the pending socket=%d.", *sock_pending );
1231 sock = *sock_pending;
1232 } else {
1233 assert(res);
1234 VRB("Trying to connect via %s.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
1235 /* Connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001236 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1237 if (sock == -1) {
Frank Rimpler9f838b02018-07-25 06:44:03 +00001238 ERR("socket couldn't be created.", strerror(errno));
1239 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001240 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001241 /* make the socket non-blocking */
1242 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
1243 ERR("Fcntl failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001244 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001245 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001246 /* non-blocking connect! */
1247 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1248 if (errno != EINPROGRESS) {
1249 /* network connection failed, try another resource */
1250 ERR("connect failed: (%s).", strerror(errno));
1251 goto cleanup;
1252 }
1253 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001254 }
1255 ts.tv_sec = timeout;
1256 ts.tv_usec = 0;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001257
Frank Rimpler9f838b02018-07-25 06:44:03 +00001258 FD_ZERO(&wset);
1259 FD_SET(sock, &wset);
1260
1261 if ((ret = select(sock + 1, NULL, &wset, NULL, (timeout != -1) ? &ts : NULL)) < 0) {
1262 ERR("select failed: (%s).", strerror(errno));
1263 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001264 }
1265
Frank Rimpler9f838b02018-07-25 06:44:03 +00001266 if (ret == 0) { //we had a timeout
1267 VRB("timed out after %ds (%s).", timeout, strerror(errno));
1268 if (sock_pending) {
1269 /* no sock-close, we'll try it again */
1270 *sock_pending = sock;
1271 } else {
1272 close(sock);
1273 }
1274 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001275 }
Radek Krejci782041a2018-08-20 10:09:45 +02001276
1277 /* check the usability of the socket */
1278 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1279 ERR("getsockopt failed: (%s).", strerror(errno));
1280 goto cleanup;
1281 }
1282 if (error == ECONNREFUSED) {
1283 /* network connection failed, try another resource */
1284 VRB("getsockopt error: (%s).", strerror(error));
1285 errno = error;
1286 goto cleanup;
1287 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001288
1289 /* enable keep-alive */
1290 if (nc_sock_enable_keepalive(sock)) {
1291 goto cleanup;
1292 }
1293
Michal Vasko086311b2016-01-08 09:53:11 +01001294 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001295
Frank Rimpler9f838b02018-07-25 06:44:03 +00001296cleanup:
1297 if (sock_pending) {
1298 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001299 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001300 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001301 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001302}
1303
Frank Rimpler9f838b02018-07-25 06:44:03 +00001304/* A given timeout value limits the time how long the function blocks. If it has to block
1305 only for some seconds, a socket connection might not yet have been fully established.
1306 Therefore the active (pending) socket will be stored in *sock_pending, but the return
1307 value will be -1. In such a case a subsequent invokation is required, by providing the
1308 stored sock_pending, again.
1309 In general, if this function returns -1, when a timeout has been given, this function
1310 has to be invoked, until it returns a valid socket.
1311 */
1312int
Michal Vasko66032bc2019-01-22 15:03:12 +01001313nc_sock_connect(const char *host, uint16_t port, int timeout, int *sock_pending, char **ip_host)
Frank Rimpler9f838b02018-07-25 06:44:03 +00001314{
1315 int i;
Michal Vasko66032bc2019-01-22 15:03:12 +01001316 int sock = sock_pending ? *sock_pending : -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001317 struct addrinfo hints, *res_list, *res;
1318 char port_s[6]; /* length of string representation of short int */
Michal Vasko66032bc2019-01-22 15:03:12 +01001319 char *buf;
1320 void *addr;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001321
Michal Vasko66032bc2019-01-22 15:03:12 +01001322 DBG("nc_sock_connect(%s, %u, %d, %d)", host, port, timeout, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001323
1324 /* no pending socket */
1325 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001326 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001327 snprintf(port_s, 6, "%u", port);
1328 memset(&hints, 0, sizeof hints);
1329 hints.ai_family = AF_UNSPEC;
1330 hints.ai_socktype = SOCK_STREAM;
1331 hints.ai_protocol = IPPROTO_TCP;
1332 i = getaddrinfo(host, port_s, &hints, &res_list);
1333 if (i != 0) {
1334 ERR("Unable to translate the host address (%s).", gai_strerror(i));
1335 return -1;
1336 }
1337
1338 for (res = res_list; res != NULL; res = res->ai_next) {
1339 sock = _non_blocking_connect(timeout, sock_pending, res);
1340 if (sock == -1 && (!sock_pending || *sock_pending == -1)) {
1341 /* try the next resource */
1342 continue;
1343 }
1344 VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko66032bc2019-01-22 15:03:12 +01001345 if (ip_host && ((res->ai_family == AF_INET6) || (res->ai_family == AF_INET))) {
1346 buf = malloc(INET6_ADDRSTRLEN);
1347 if (!buf) {
1348 ERRMEM;
1349 close(sock);
1350 return -1;
1351 }
1352 if (res->ai_family == AF_INET) {
1353 addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
1354 } else {
1355 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
1356 }
1357 if (!inet_ntop(res->ai_family, addr, buf, INET6_ADDRSTRLEN)) {
1358 ERR("Converting host to IP address failed (%s).", strerror(errno));
1359 free(buf);
1360 close(sock);
1361 return -1;
1362 }
1363
1364 *ip_host = buf;
1365 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001366 break;
1367 }
1368 freeaddrinfo(res_list);
1369
1370 } else {
1371 /* try to get a connection with the pending socket */
1372 assert(sock_pending);
1373 sock = _non_blocking_connect(timeout, sock_pending, NULL);
1374 }
1375
1376 return sock;
1377}
1378
Michal Vasko086311b2016-01-08 09:53:11 +01001379static NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001380get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001381{
Michal Vasko086311b2016-01-08 09:53:11 +01001382 char *ptr;
1383 const char *str_msgid;
1384 uint64_t cur_msgid;
1385 struct lyxml_elem *xml;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001386 struct nc_msg_cont *cont, **cont_ptr;
Michal Vasko086311b2016-01-08 09:53:11 +01001387 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1388
Michal Vasko086311b2016-01-08 09:53:11 +01001389 /* try to get notification from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001390 if (!msgid && session->opts.client.notifs) {
1391 cont = session->opts.client.notifs;
1392 session->opts.client.notifs = cont->next;
Michal Vasko086311b2016-01-08 09:53:11 +01001393
Michal Vasko71ba2da2016-05-04 10:53:16 +02001394 xml = cont->msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001395 free(cont);
1396
Michal Vasko71ba2da2016-05-04 10:53:16 +02001397 msgtype = NC_MSG_NOTIF;
Michal Vasko086311b2016-01-08 09:53:11 +01001398 }
1399
1400 /* try to get rpc-reply from the session's queue */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001401 if (msgid && session->opts.client.replies) {
1402 cont = session->opts.client.replies;
1403 session->opts.client.replies = cont->next;
Michal Vasko2518b6b2016-01-28 13:24:53 +01001404
Michal Vasko71ba2da2016-05-04 10:53:16 +02001405 xml = cont->msg;
1406 free(cont);
Michal Vasko086311b2016-01-08 09:53:11 +01001407
Michal Vasko71ba2da2016-05-04 10:53:16 +02001408 msgtype = NC_MSG_REPLY;
Michal Vasko086311b2016-01-08 09:53:11 +01001409 }
1410
Michal Vasko71ba2da2016-05-04 10:53:16 +02001411 if (!msgtype) {
1412 /* read message from wire */
Michal Vasko131120a2018-05-29 15:44:02 +02001413 msgtype = nc_read_msg_poll_io(session, timeout, &xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001414 }
Michal Vasko086311b2016-01-08 09:53:11 +01001415
1416 /* we read rpc-reply, want a notif */
1417 if (!msgid && (msgtype == NC_MSG_REPLY)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001418 cont_ptr = &session->opts.client.replies;
Michal Vasko086311b2016-01-08 09:53:11 +01001419 while (*cont_ptr) {
1420 cont_ptr = &((*cont_ptr)->next);
1421 }
1422 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001423 if (!*cont_ptr) {
1424 ERRMEM;
1425 lyxml_free(session->ctx, xml);
1426 return NC_MSG_ERROR;
1427 }
Michal Vasko086311b2016-01-08 09:53:11 +01001428 (*cont_ptr)->msg = xml;
1429 (*cont_ptr)->next = NULL;
1430 }
1431
1432 /* we read notif, want a rpc-reply */
1433 if (msgid && (msgtype == NC_MSG_NOTIF)) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001434 if (!session->opts.client.ntf_tid) {
Michal Vaskod083db62016-01-19 10:31:29 +01001435 ERR("Session %u: received a <notification> but session is not subscribed.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001436 lyxml_free(session->ctx, xml);
Michal Vasko2518b6b2016-01-28 13:24:53 +01001437 return NC_MSG_ERROR;
Michal Vasko3486a7c2017-03-03 13:28:07 +01001438 }
Michal Vasko086311b2016-01-08 09:53:11 +01001439
Michal Vasko2e6defd2016-10-07 15:48:15 +02001440 cont_ptr = &session->opts.client.notifs;
Michal Vasko086311b2016-01-08 09:53:11 +01001441 while (*cont_ptr) {
1442 cont_ptr = &((*cont_ptr)->next);
1443 }
1444 *cont_ptr = malloc(sizeof **cont_ptr);
Michal Vasko140eecd2018-05-30 09:55:56 +02001445 if (!*cont_ptr) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001446 ERRMEM;
1447 lyxml_free(session->ctx, xml);
1448 return NC_MSG_ERROR;
1449 }
Michal Vasko086311b2016-01-08 09:53:11 +01001450 (*cont_ptr)->msg = xml;
1451 (*cont_ptr)->next = NULL;
1452 }
1453
Michal Vasko086311b2016-01-08 09:53:11 +01001454 switch (msgtype) {
1455 case NC_MSG_NOTIF:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001456 if (!msgid) {
1457 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001458 }
Michal Vasko086311b2016-01-08 09:53:11 +01001459 break;
1460
1461 case NC_MSG_REPLY:
Michal Vasko2518b6b2016-01-28 13:24:53 +01001462 if (msgid) {
Michal Vasko71ba2da2016-05-04 10:53:16 +02001463 /* check message-id */
1464 str_msgid = lyxml_get_attr(xml, "message-id", NULL);
1465 if (!str_msgid) {
Michal Vasko14fdbb62018-01-18 09:13:20 +01001466 WRN("Session %u: received a <rpc-reply> without a message-id.", session->id);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001467 } else {
1468 cur_msgid = strtoul(str_msgid, &ptr, 10);
1469 if (cur_msgid != msgid) {
1470 ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".",
1471 session->id, str_msgid);
1472 msgtype = NC_MSG_REPLY_ERR_MSGID;
1473 }
1474 }
Michal Vasko2518b6b2016-01-28 13:24:53 +01001475 *msg = xml;
Michal Vasko086311b2016-01-08 09:53:11 +01001476 }
Michal Vasko086311b2016-01-08 09:53:11 +01001477 break;
1478
1479 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001480 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001481 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001482 msgtype = NC_MSG_ERROR;
1483 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001484
1485 case NC_MSG_RPC:
Michal Vaskod083db62016-01-19 10:31:29 +01001486 ERR("Session %u: received <rpc> from a NETCONF server.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01001487 lyxml_free(session->ctx, xml);
Michal Vasko71ba2da2016-05-04 10:53:16 +02001488 msgtype = NC_MSG_ERROR;
1489 break;
Michal Vasko086311b2016-01-08 09:53:11 +01001490
1491 default:
1492 /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out;
1493 * NC_MSG_NONE is not returned by nc_read_msg()
1494 */
1495 break;
1496 }
1497
1498 return msgtype;
1499}
1500
1501/* cannot strictly fail, but does not need to fill any error parameter at all */
1502static void
1503parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err)
1504{
1505 struct lyxml_elem *iter, *next, *info;
1506
1507 LY_TREE_FOR(xml->child, iter) {
1508 if (!iter->ns) {
1509 if (iter->content) {
1510 WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content);
1511 } else {
1512 WRN("<rpc-error> child \"%s\" without namespace.", iter->name);
1513 }
1514 continue;
1515 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1516 if (iter->content) {
1517 WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".",
1518 iter->name, iter->content, iter->ns->value);
1519 } else {
1520 WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value);
1521 }
1522 continue;
1523 }
1524
1525 if (!strcmp(iter->name, "error-type")) {
1526 if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc")
1527 && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) {
1528 WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : ""));
1529 } else if (err->type) {
1530 WRN("<rpc-error> <error-type> duplicated.");
1531 } else {
1532 err->type = lydict_insert(ctx, iter->content, 0);
1533 }
1534 } else if (!strcmp(iter->name, "error-tag")) {
1535 if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value")
1536 && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute")
1537 && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute")
1538 && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element")
1539 && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace")
1540 && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied")
1541 && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed")
1542 && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing")
1543 && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed")
1544 && strcmp(iter->content, "malformed-message"))) {
1545 WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : ""));
1546 } else if (err->tag) {
1547 WRN("<rpc-error> <error-tag> duplicated.");
1548 } else {
1549 err->tag = lydict_insert(ctx, iter->content, 0);
1550 }
1551 } else if (!strcmp(iter->name, "error-severity")) {
1552 if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) {
1553 WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : ""));
1554 } else if (err->severity) {
1555 WRN("<rpc-error> <error-severity> duplicated.");
1556 } else {
1557 err->severity = lydict_insert(ctx, iter->content, 0);
1558 }
1559 } else if (!strcmp(iter->name, "error-app-tag")) {
1560 if (err->apptag) {
1561 WRN("<rpc-error> <error-app-tag> duplicated.");
1562 } else {
1563 err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1564 }
1565 } else if (!strcmp(iter->name, "error-path")) {
1566 if (err->path) {
1567 WRN("<rpc-error> <error-path> duplicated.");
1568 } else {
1569 err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1570 }
1571 } else if (!strcmp(iter->name, "error-message")) {
1572 if (err->message) {
1573 WRN("<rpc-error> <error-message> duplicated.");
1574 } else {
1575 err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL);
Michal Vaskob0222c42018-01-03 15:17:12 +01001576 if (err->message_lang) {
1577 err->message_lang = lydict_insert(ctx, err->message_lang, 0);
1578 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001579 VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute.");
1580 }
1581 err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0);
1582 }
1583 } else if (!strcmp(iter->name, "error-info")) {
1584 LY_TREE_FOR_SAFE(iter->child, next, info) {
1585 if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) {
1586 if (!strcmp(info->name, "session-id")) {
1587 if (err->sid) {
1588 WRN("<rpc-error> <error-info> <session-id> duplicated.");
1589 } else {
1590 err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1591 }
Michal Vasko630485f2018-01-03 14:28:32 +01001592 } else if (!strcmp(info->name, "bad-attribute")) {
Michal Vasko086311b2016-01-08 09:53:11 +01001593 ++err->attr_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001594 err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr);
1595 if (!err->attr) {
1596 ERRMEM;
1597 return;
1598 }
Michal Vasko086311b2016-01-08 09:53:11 +01001599 err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1600 } else if (!strcmp(info->name, "bad-element")) {
1601 ++err->elem_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001602 err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem);
1603 if (!err->elem) {
1604 ERRMEM;
1605 return;
1606 }
Michal Vasko086311b2016-01-08 09:53:11 +01001607 err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1608 } else if (!strcmp(info->name, "bad-namespace")) {
1609 ++err->ns_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001610 err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns);
1611 if (!err->ns) {
1612 ERRMEM;
1613 return;
1614 }
Michal Vasko086311b2016-01-08 09:53:11 +01001615 err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0);
1616 } else {
1617 if (info->content) {
1618 WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".",
1619 info->name, info->content);
1620 } else {
1621 WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name);
1622 }
1623 }
1624 } else {
1625 lyxml_unlink(ctx, info);
1626 ++err->other_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001627 err->other = nc_realloc(err->other, err->other_count * sizeof *err->other);
1628 if (!err->other) {
1629 ERRMEM;
1630 return;
1631 }
Michal Vasko086311b2016-01-08 09:53:11 +01001632 err->other[err->other_count - 1] = info;
1633 }
1634 }
1635 } else {
1636 if (iter->content) {
1637 WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content);
1638 } else {
1639 WRN("<rpc-error> unknown child \"%s\".", iter->name);
1640 }
1641 }
1642 }
1643}
1644
1645static struct nc_reply *
Michal Vaskoeb7080e2016-02-18 13:27:05 +01001646parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions)
Michal Vasko086311b2016-01-08 09:53:11 +01001647{
1648 struct lyxml_elem *iter;
Michal Vaskoe1708602016-10-18 12:17:22 +02001649 struct lyd_node *data = NULL, *rpc_act = NULL;
Michal Vasko1a38c862016-01-15 15:50:07 +01001650 struct nc_client_reply_error *error_rpl;
Michal Vasko086311b2016-01-08 09:53:11 +01001651 struct nc_reply_data *data_rpl;
1652 struct nc_reply *reply = NULL;
Michal Vasko90e8e692016-07-13 12:27:57 +02001653 struct nc_rpc_act_generic *rpc_gen;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001654 int i, data_parsed = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001655
1656 if (!xml->child) {
1657 ERR("An empty <rpc-reply>.");
1658 return NULL;
1659 }
1660
1661 /* rpc-error */
1662 if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1663 /* count and check elements */
1664 i = 0;
1665 LY_TREE_FOR(xml->child, iter) {
1666 if (strcmp(iter->name, "rpc-error")) {
1667 ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name);
1668 return NULL;
1669 } else if (!iter->ns) {
1670 ERR("<rpc-reply> content mismatch (<rpc-error> without namespace).");
1671 return NULL;
1672 } else if (strcmp(iter->ns->value, NC_NS_BASE)) {
1673 ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value);
1674 return NULL;
1675 }
1676 ++i;
1677 }
1678
1679 error_rpl = malloc(sizeof *error_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001680 if (!error_rpl) {
1681 ERRMEM;
1682 return NULL;
1683 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001684 error_rpl->type = NC_RPL_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001685 error_rpl->err = calloc(i, sizeof *error_rpl->err);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001686 if (!error_rpl->err) {
1687 ERRMEM;
1688 free(error_rpl);
1689 return NULL;
1690 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001691 error_rpl->count = i;
Michal Vasko1a38c862016-01-15 15:50:07 +01001692 error_rpl->ctx = ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001693 reply = (struct nc_reply *)error_rpl;
1694
1695 i = 0;
1696 LY_TREE_FOR(xml->child, iter) {
1697 parse_rpc_error(ctx, iter, error_rpl->err + i);
1698 ++i;
1699 }
1700
1701 /* ok */
1702 } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) {
1703 if (xml->child->next) {
1704 ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name);
1705 return NULL;
1706 }
1707 reply = malloc(sizeof *reply);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001708 if (!reply) {
1709 ERRMEM;
1710 return NULL;
1711 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001712 reply->type = NC_RPL_OK;
Michal Vasko086311b2016-01-08 09:53:11 +01001713
1714 /* some RPC output */
1715 } else {
1716 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001717 case NC_RPC_ACT_GENERIC:
1718 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01001719
1720 if (rpc_gen->has_data) {
Michal Vaskodd3e3142018-07-10 08:30:20 +02001721 rpc_act = lyd_dup(rpc_gen->content.data, 1);
1722 if (!rpc_act) {
1723 ERR("Failed to duplicate a generic RPC/action.");
1724 return NULL;
1725 }
Michal Vasko086311b2016-01-08 09:53:11 +01001726 } else {
Michal Vaskoeee99412016-11-21 10:19:43 +01001727 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 +02001728 if (!rpc_act) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001729 ERR("Failed to parse a generic RPC/action XML.");
Michal Vasko086311b2016-01-08 09:53:11 +01001730 return NULL;
1731 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001732 }
Michal Vasko086311b2016-01-08 09:53:11 +01001733 break;
1734
1735 case NC_RPC_GETCONFIG:
1736 case NC_RPC_GET:
Michal Vasko13ed2942016-02-29 16:21:00 +01001737 if (!xml->child->child) {
1738 /* we did not receive any data */
1739 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001740 if (!data_rpl) {
1741 ERRMEM;
1742 return NULL;
1743 }
Michal Vasko13ed2942016-02-29 16:21:00 +01001744 data_rpl->type = NC_RPL_DATA;
1745 data_rpl->data = NULL;
1746 return (struct nc_reply *)data_rpl;
1747 }
1748
Michal Vasko086311b2016-01-08 09:53:11 +01001749 /* special treatment */
Michal Vaskof45e0c12018-11-28 08:38:23 +01001750 ly_errno = 0;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +01001751 data = lyd_parse_xml(ctx, &xml->child->child,
1752 LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET)
Michal Vaskoeee99412016-11-21 10:19:43 +01001753 | parseroptions);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001754 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001755 ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get"));
1756 return NULL;
1757 }
Michal Vaskof45e0c12018-11-28 08:38:23 +01001758 data_parsed = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001759 break;
1760
1761 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02001762 rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL, 1), "get-schema");
Michal Vaskoe1708602016-10-18 12:17:22 +02001763 if (!rpc_act) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001764 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001765 return NULL;
1766 }
1767 break;
1768
1769 case NC_RPC_EDIT:
1770 case NC_RPC_COPY:
1771 case NC_RPC_DELETE:
1772 case NC_RPC_LOCK:
1773 case NC_RPC_UNLOCK:
1774 case NC_RPC_KILL:
1775 case NC_RPC_COMMIT:
1776 case NC_RPC_DISCARD:
1777 case NC_RPC_CANCEL:
1778 case NC_RPC_VALIDATE:
1779 case NC_RPC_SUBSCRIBE:
1780 /* there is no output defined */
1781 ERR("Unexpected data reply (root elem \"%s\").", xml->child->name);
1782 return NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001783 default:
1784 ERRINT;
1785 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001786 }
1787
1788 data_rpl = malloc(sizeof *data_rpl);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001789 if (!data_rpl) {
1790 ERRMEM;
1791 return NULL;
1792 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001793 data_rpl->type = NC_RPL_DATA;
Michal Vaskof45e0c12018-11-28 08:38:23 +01001794
1795 ly_errno = 0;
1796 if (!data_parsed) {
Michal Vasko68b3f292016-09-16 12:00:32 +02001797 data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions,
Michal Vaskocc3d52b2016-10-18 12:17:22 +02001798 rpc_act, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001799 } else {
1800 /* <get>, <get-config> */
1801 data_rpl->data = data;
1802 }
Michal Vaskoe1708602016-10-18 12:17:22 +02001803 lyd_free_withsiblings(rpc_act);
Michal Vaskof45e0c12018-11-28 08:38:23 +01001804 if (ly_errno) {
Michal Vasko086311b2016-01-08 09:53:11 +01001805 ERR("Failed to parse <rpc-reply>.");
1806 free(data_rpl);
1807 return NULL;
1808 }
1809 reply = (struct nc_reply *)data_rpl;
1810 }
1811
1812 return reply;
1813}
1814
Radek Krejci53691be2016-02-22 13:58:37 +01001815#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001816
Michal Vasko3031aae2016-01-27 16:07:18 +01001817int
1818nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1819{
1820 int sock;
1821
Michal Vasko45e53ae2016-04-07 11:46:03 +02001822 if (!address) {
1823 ERRARG("address");
1824 return -1;
1825 } else if (!port) {
1826 ERRARG("port");
Michal Vasko3031aae2016-01-27 16:07:18 +01001827 return -1;
1828 }
1829
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001830 sock = nc_sock_listen_inet(address, port);
Michal Vasko3031aae2016-01-27 16:07:18 +01001831 if (sock == -1) {
1832 return -1;
1833 }
1834
1835 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001836 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1837 if (!client_opts.ch_binds) {
1838 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001839 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001840 return -1;
1841 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001842
Michal Vasko2e6defd2016-10-07 15:48:15 +02001843 client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti);
1844 if (!client_opts.ch_bind_ti) {
1845 ERRMEM;
1846 close(sock);
1847 return -1;
1848 }
1849 client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti;
1850
Michal Vasko3031aae2016-01-27 16:07:18 +01001851 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001852 if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) {
1853 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001854 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001855 return -1;
1856 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001857 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1858 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001859 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001860
1861 return 0;
1862}
1863
1864int
1865nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1866{
1867 uint32_t i;
1868 int ret = -1;
1869
1870 if (!address && !port && !ti) {
1871 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1872 close(client_opts.ch_binds[i].sock);
1873 free((char *)client_opts.ch_binds[i].address);
1874
1875 ret = 0;
1876 }
1877 free(client_opts.ch_binds);
1878 client_opts.ch_binds = NULL;
1879 client_opts.ch_bind_count = 0;
1880 } else {
1881 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1882 if ((!address || !strcmp(client_opts.ch_binds[i].address, address))
1883 && (!port || (client_opts.ch_binds[i].port == port))
Michal Vasko2e6defd2016-10-07 15:48:15 +02001884 && (!ti || (client_opts.ch_bind_ti[i] == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001885 close(client_opts.ch_binds[i].sock);
1886 free((char *)client_opts.ch_binds[i].address);
1887
1888 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001889 if (!client_opts.ch_bind_count) {
1890 free(client_opts.ch_binds);
1891 client_opts.ch_binds = NULL;
1892 } else if (i < client_opts.ch_bind_count) {
1893 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds);
1894 client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count];
1895 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001896
1897 ret = 0;
1898 }
1899 }
1900 }
1901
1902 return ret;
1903}
1904
1905API int
1906nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1907{
1908 int sock;
1909 char *host = NULL;
1910 uint16_t port, idx;
1911
Michal Vasko45e53ae2016-04-07 11:46:03 +02001912 if (!client_opts.ch_binds) {
1913 ERRINIT;
1914 return -1;
1915 } else if (!session) {
1916 ERRARG("session");
Michal Vasko3031aae2016-01-27 16:07:18 +01001917 return -1;
1918 }
1919
1920 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
1921
Michal Vasko50456e82016-02-02 12:16:08 +01001922 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001923 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001924 return sock;
1925 }
1926
Radek Krejci53691be2016-02-22 13:58:37 +01001927#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001928 if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001929 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001930 } else
1931#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001932#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001933 if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001934 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001935 } else
1936#endif
1937 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001938 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001939 *session = NULL;
1940 }
1941
1942 free(host);
1943
1944 if (!(*session)) {
1945 return -1;
1946 }
1947
1948 return 1;
1949}
1950
Radek Krejci53691be2016-02-22 13:58:37 +01001951#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001952
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001953API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001954nc_session_get_cpblts(const struct nc_session *session)
1955{
1956 if (!session) {
1957 ERRARG("session");
1958 return NULL;
1959 }
1960
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001961 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001962}
1963
1964API const char *
1965nc_session_cpblt(const struct nc_session *session, const char *capab)
1966{
1967 int i, len;
1968
1969 if (!session) {
1970 ERRARG("session");
1971 return NULL;
1972 } else if (!capab) {
1973 ERRARG("capab");
1974 return NULL;
1975 }
1976
1977 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001978 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1979 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1980 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001981 }
1982 }
1983
1984 return NULL;
1985}
1986
Michal Vasko9cd26a82016-05-31 08:58:48 +02001987API int
1988nc_session_ntf_thread_running(const struct nc_session *session)
1989{
Michal Vasko2e6defd2016-10-07 15:48:15 +02001990 if (!session || (session->side != NC_CLIENT)) {
Michal Vasko9cd26a82016-05-31 08:58:48 +02001991 ERRARG("session");
1992 return 0;
1993 }
1994
Michal Vasko2e6defd2016-10-07 15:48:15 +02001995 return session->opts.client.ntf_tid ? 1 : 0;
Michal Vasko9cd26a82016-05-31 08:58:48 +02001996}
1997
Michal Vaskob7558c52016-02-26 15:04:19 +01001998API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001999nc_client_init(void)
2000{
2001 nc_init();
2002}
2003
2004API void
Michal Vaskob7558c52016-02-26 15:04:19 +01002005nc_client_destroy(void)
2006{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02002007 nc_client_set_schema_searchpath(NULL);
2008#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
2009 nc_client_ch_del_bind(NULL, 0, 0);
2010#endif
2011#ifdef NC_ENABLED_SSH
2012 nc_client_ssh_destroy_opts();
2013#endif
2014#ifdef NC_ENABLED_TLS
2015 nc_client_tls_destroy_opts();
2016#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01002017 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01002018}
2019
Michal Vasko086311b2016-01-08 09:53:11 +01002020API NC_MSG_TYPE
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002021nc_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 +01002022{
2023 struct lyxml_elem *xml;
2024 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2025
Michal Vasko45e53ae2016-04-07 11:46:03 +02002026 if (!session) {
2027 ERRARG("session");
2028 return NC_MSG_ERROR;
2029 } else if (!rpc) {
2030 ERRARG("rpc");
2031 return NC_MSG_ERROR;
2032 } else if (!reply) {
2033 ERRARG("reply");
2034 return NC_MSG_ERROR;
2035 } else if (parseroptions & LYD_OPT_TYPEMASK) {
2036 ERRARG("parseroptions");
Michal Vasko086311b2016-01-08 09:53:11 +01002037 return NC_MSG_ERROR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002038 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vaskod083db62016-01-19 10:31:29 +01002039 ERR("Session %u: invalid session to receive RPC replies.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002040 return NC_MSG_ERROR;
2041 }
Michal Vaskoeb7080e2016-02-18 13:27:05 +01002042 parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS);
Michal Vaskoeee99412016-11-21 10:19:43 +01002043 if (!(session->flags & NC_SESSION_CLIENT_NOT_STRICT)) {
2044 parseroptions &= LYD_OPT_STRICT;
2045 }
Michal Vasko41adf392017-01-17 10:39:04 +01002046 /* no mechanism to check external dependencies is provided */
2047 parseroptions|= LYD_OPT_NOEXTDEPS;
Michal Vasko086311b2016-01-08 09:53:11 +01002048 *reply = NULL;
2049
2050 msgtype = get_msg(session, timeout, msgid, &xml);
Michal Vasko086311b2016-01-08 09:53:11 +01002051
Michal Vasko71ba2da2016-05-04 10:53:16 +02002052 if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) {
Michal Vaskoeee99412016-11-21 10:19:43 +01002053 *reply = parse_reply(session->ctx, xml, rpc, parseroptions);
Michal Vasko086311b2016-01-08 09:53:11 +01002054 lyxml_free(session->ctx, xml);
2055 if (!(*reply)) {
2056 return NC_MSG_ERROR;
2057 }
2058 }
2059
2060 return msgtype;
2061}
2062
2063API NC_MSG_TYPE
2064nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif)
2065{
2066 struct lyxml_elem *xml, *ev_time;
2067 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
2068
Michal Vasko45e53ae2016-04-07 11:46:03 +02002069 if (!session) {
2070 ERRARG("session");
2071 return NC_MSG_ERROR;
2072 } else if (!notif) {
2073 ERRARG("notif");
Michal Vasko086311b2016-01-08 09:53:11 +01002074 return NC_MSG_ERROR;
2075 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002076 ERR("Session %u: invalid session to receive Notifications.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002077 return NC_MSG_ERROR;
2078 }
2079
2080 msgtype = get_msg(session, timeout, 0, &xml);
2081
2082 if (msgtype == NC_MSG_NOTIF) {
2083 *notif = calloc(1, sizeof **notif);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002084 if (!*notif) {
2085 ERRMEM;
2086 lyxml_free(session->ctx, xml);
2087 return NC_MSG_ERROR;
2088 }
Michal Vasko086311b2016-01-08 09:53:11 +01002089
2090 /* eventTime */
2091 LY_TREE_FOR(xml->child, ev_time) {
2092 if (!strcmp(ev_time->name, "eventTime")) {
2093 (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0);
2094 /* lyd_parse does not know this element */
2095 lyxml_free(session->ctx, ev_time);
2096 break;
2097 }
2098 }
2099 if (!(*notif)->datetime) {
Michal Vaskod083db62016-01-19 10:31:29 +01002100 ERR("Session %u: notification is missing the \"eventTime\" element.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002101 goto fail;
2102 }
2103
2104 /* notification body */
Michal Vasko41adf392017-01-17 10:39:04 +01002105 (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01002106 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01002107 lyxml_free(session->ctx, xml);
2108 xml = NULL;
2109 if (!(*notif)->tree) {
Michal Vaskod083db62016-01-19 10:31:29 +01002110 ERR("Session %u: failed to parse a new notification.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002111 goto fail;
2112 }
2113 }
2114
2115 return msgtype;
2116
2117fail:
2118 lydict_remove(session->ctx, (*notif)->datetime);
2119 lyd_free((*notif)->tree);
2120 free(*notif);
2121 *notif = NULL;
2122 lyxml_free(session->ctx, xml);
2123
2124 return NC_MSG_ERROR;
2125}
2126
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002127static void *
2128nc_recv_notif_thread(void *arg)
2129{
2130 struct nc_ntf_thread_arg *ntarg;
2131 struct nc_session *session;
2132 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
2133 struct nc_notif *notif;
2134 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002135 pthread_t *ntf_tid;
2136
2137 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002138
2139 ntarg = (struct nc_ntf_thread_arg *)arg;
2140 session = ntarg->session;
2141 notif_clb = ntarg->notif_clb;
2142 free(ntarg);
2143
Michal Vasko131120a2018-05-29 15:44:02 +02002144 /* remember our allocated tid, we will be freeing it */
2145 ntf_tid = (pthread_t *)session->opts.client.ntf_tid;
2146
Michal Vasko2e6defd2016-10-07 15:48:15 +02002147 while (session->opts.client.ntf_tid) {
Michal vasko4e1a36c2016-10-05 13:04:37 +02002148 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &notif);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002149 if (msgtype == NC_MSG_NOTIF) {
2150 notif_clb(session, notif);
Michal Vaskof0537d82016-01-29 14:42:38 +01002151 if (!strcmp(notif->tree->schema->name, "notificationComplete")
2152 && !strcmp(notif->tree->schema->module->name, "nc-notifications")) {
2153 nc_notif_free(notif);
2154 break;
2155 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002156 nc_notif_free(notif);
Michal Vaskoce326052018-01-04 10:32:03 +01002157 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002158 /* quit this thread once the session is broken */
2159 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002160 }
2161
2162 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2163 }
2164
Michal Vasko0651c902016-05-19 15:55:42 +02002165 VRB("Session %u: notification thread exit.", session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002166 session->opts.client.ntf_tid = NULL;
Michal Vasko131120a2018-05-29 15:44:02 +02002167 free(ntf_tid);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002168 return NULL;
2169}
2170
2171API int
2172nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif))
2173{
2174 struct nc_ntf_thread_arg *ntarg;
2175 int ret;
2176
Michal Vasko45e53ae2016-04-07 11:46:03 +02002177 if (!session) {
2178 ERRARG("session");
2179 return -1;
2180 } else if (!notif_clb) {
2181 ERRARG("notif_clb");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002182 return -1;
2183 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
2184 ERR("Session %u: invalid session to receive Notifications.", session->id);
2185 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002186 } else if (session->opts.client.ntf_tid) {
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002187 ERR("Session %u: separate notification thread is already running.", session->id);
2188 return -1;
2189 }
2190
2191 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002192 if (!ntarg) {
2193 ERRMEM;
2194 return -1;
2195 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002196 ntarg->session = session;
2197 ntarg->notif_clb = notif_clb;
2198
2199 /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002200 session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid);
2201 if (!session->opts.client.ntf_tid) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002202 ERRMEM;
2203 free(ntarg);
2204 return -1;
2205 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002206
Michal Vasko2e6defd2016-10-07 15:48:15 +02002207 ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002208 if (ret) {
2209 ERR("Session %u: failed to create a new thread (%s).", strerror(errno));
2210 free(ntarg);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002211 free((pthread_t *)session->opts.client.ntf_tid);
2212 session->opts.client.ntf_tid = NULL;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002213 return -1;
2214 }
2215
2216 return 0;
2217}
2218
Michal Vasko086311b2016-01-08 09:53:11 +01002219API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002220nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002221{
2222 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002223 int dofree = 1;
Michal Vasko90e8e692016-07-13 12:27:57 +02002224 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002225 struct nc_rpc_getconfig *rpc_gc;
2226 struct nc_rpc_edit *rpc_e;
2227 struct nc_rpc_copy *rpc_cp;
2228 struct nc_rpc_delete *rpc_del;
2229 struct nc_rpc_lock *rpc_lock;
2230 struct nc_rpc_get *rpc_g;
2231 struct nc_rpc_kill *rpc_k;
2232 struct nc_rpc_commit *rpc_com;
2233 struct nc_rpc_cancel *rpc_can;
2234 struct nc_rpc_validate *rpc_val;
2235 struct nc_rpc_getschema *rpc_gs;
2236 struct nc_rpc_subscribe *rpc_sub;
2237 struct lyd_node *data, *node;
Michal Vasko9d8bee62016-03-03 10:58:24 +01002238 const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL;
Radek Krejci539efb62016-08-24 15:05:16 +02002239 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002240 uint64_t cur_msgid;
2241
Michal Vasko45e53ae2016-04-07 11:46:03 +02002242 if (!session) {
2243 ERRARG("session");
2244 return NC_MSG_ERROR;
2245 } else if (!rpc) {
2246 ERRARG("rpc");
2247 return NC_MSG_ERROR;
2248 } else if (!msgid) {
2249 ERRARG("msgid");
Michal Vasko086311b2016-01-08 09:53:11 +01002250 return NC_MSG_ERROR;
2251 } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) {
Michal Vaskod083db62016-01-19 10:31:29 +01002252 ERR("Session %u: invalid session to send RPCs.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002253 return NC_MSG_ERROR;
2254 }
2255
Michal Vasko90e8e692016-07-13 12:27:57 +02002256 if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002257 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002258 if (!ietfnc) {
Michal Vasko086cd572017-01-12 12:19:05 +01002259 ERR("Session %u: missing \"ietf-netconf\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002260 return NC_MSG_ERROR;
2261 }
2262 }
2263
2264 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002265 case NC_RPC_ACT_GENERIC:
2266 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002267
2268 if (rpc_gen->has_data) {
2269 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002270 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002271 } else {
Michal Vasko41adf392017-01-17 10:39:04 +01002272 data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
Michal Vaskoeee99412016-11-21 10:19:43 +01002273 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL);
Michal Vaskoeec410f2017-11-24 09:14:55 +01002274 if (!data) {
2275 return NC_MSG_ERROR;
2276 }
Michal Vasko086311b2016-01-08 09:53:11 +01002277 }
2278 break;
2279
2280 case NC_RPC_GETCONFIG:
2281 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2282
2283 data = lyd_new(NULL, ietfnc, "get-config");
2284 node = lyd_new(data, ietfnc, "source");
2285 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL);
2286 if (!node) {
2287 lyd_free(data);
2288 return NC_MSG_ERROR;
2289 }
2290 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002291 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002292 node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002293 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002294 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002295 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002296 lyd_insert_attr(node, NULL, "type", "xpath");
2297 lyd_insert_attr(node, NULL, "select", rpc_gc->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002298 }
2299 if (!node) {
2300 lyd_free(data);
2301 return NC_MSG_ERROR;
2302 }
2303 }
2304
2305 if (rpc_gc->wd_mode) {
2306 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002307 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002308 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002309 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002310 return NC_MSG_ERROR;
2311 }
2312 }
2313 switch (rpc_gc->wd_mode) {
2314 case NC_WD_UNKNOWN:
2315 /* cannot get here */
2316 break;
2317 case NC_WD_ALL:
2318 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2319 break;
2320 case NC_WD_ALL_TAG:
2321 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2322 break;
2323 case NC_WD_TRIM:
2324 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2325 break;
2326 case NC_WD_EXPLICIT:
2327 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2328 break;
2329 }
2330 if (!node) {
2331 lyd_free(data);
2332 return NC_MSG_ERROR;
2333 }
2334 }
2335 break;
2336
2337 case NC_RPC_EDIT:
2338 rpc_e = (struct nc_rpc_edit *)rpc;
2339
2340 data = lyd_new(NULL, ietfnc, "edit-config");
2341 node = lyd_new(data, ietfnc, "target");
2342 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL);
2343 if (!node) {
2344 lyd_free(data);
2345 return NC_MSG_ERROR;
2346 }
2347
2348 if (rpc_e->default_op) {
2349 node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]);
2350 if (!node) {
2351 lyd_free(data);
2352 return NC_MSG_ERROR;
2353 }
2354 }
2355
2356 if (rpc_e->test_opt) {
2357 node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]);
2358 if (!node) {
2359 lyd_free(data);
2360 return NC_MSG_ERROR;
2361 }
2362 }
2363
2364 if (rpc_e->error_opt) {
2365 node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]);
2366 if (!node) {
2367 lyd_free(data);
2368 return NC_MSG_ERROR;
2369 }
2370 }
2371
Michal Vasko7793bc62016-09-16 11:58:41 +02002372 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002373 node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002374 } else {
2375 node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont);
2376 }
2377 if (!node) {
2378 lyd_free(data);
2379 return NC_MSG_ERROR;
2380 }
2381 break;
2382
2383 case NC_RPC_COPY:
2384 rpc_cp = (struct nc_rpc_copy *)rpc;
2385
2386 data = lyd_new(NULL, ietfnc, "copy-config");
2387 node = lyd_new(data, ietfnc, "target");
2388 if (rpc_cp->url_trg) {
2389 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg);
2390 } else {
2391 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL);
2392 }
2393 if (!node) {
2394 lyd_free(data);
2395 return NC_MSG_ERROR;
2396 }
2397
2398 node = lyd_new(data, ietfnc, "source");
2399 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002400 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002401 node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002402 } else {
2403 node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src);
2404 }
2405 } else {
2406 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL);
2407 }
2408 if (!node) {
2409 lyd_free(data);
2410 return NC_MSG_ERROR;
2411 }
2412
2413 if (rpc_cp->wd_mode) {
2414 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002415 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002416 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002417 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002418 return NC_MSG_ERROR;
2419 }
2420 }
2421 switch (rpc_cp->wd_mode) {
2422 case NC_WD_UNKNOWN:
2423 /* cannot get here */
2424 break;
2425 case NC_WD_ALL:
2426 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2427 break;
2428 case NC_WD_ALL_TAG:
2429 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2430 break;
2431 case NC_WD_TRIM:
2432 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2433 break;
2434 case NC_WD_EXPLICIT:
2435 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2436 break;
2437 }
2438 if (!node) {
2439 lyd_free(data);
2440 return NC_MSG_ERROR;
2441 }
2442 }
2443 break;
2444
2445 case NC_RPC_DELETE:
2446 rpc_del = (struct nc_rpc_delete *)rpc;
2447
2448 data = lyd_new(NULL, ietfnc, "delete-config");
2449 node = lyd_new(data, ietfnc, "target");
2450 if (rpc_del->url) {
2451 node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url);
2452 } else {
2453 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL);
2454 }
2455 if (!node) {
2456 lyd_free(data);
2457 return NC_MSG_ERROR;
2458 }
2459 break;
2460
2461 case NC_RPC_LOCK:
2462 rpc_lock = (struct nc_rpc_lock *)rpc;
2463
2464 data = lyd_new(NULL, ietfnc, "lock");
2465 node = lyd_new(data, ietfnc, "target");
2466 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2467 if (!node) {
2468 lyd_free(data);
2469 return NC_MSG_ERROR;
2470 }
2471 break;
2472
2473 case NC_RPC_UNLOCK:
2474 rpc_lock = (struct nc_rpc_lock *)rpc;
2475
2476 data = lyd_new(NULL, ietfnc, "unlock");
2477 node = lyd_new(data, ietfnc, "target");
2478 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL);
2479 if (!node) {
2480 lyd_free(data);
2481 return NC_MSG_ERROR;
2482 }
2483 break;
2484
2485 case NC_RPC_GET:
2486 rpc_g = (struct nc_rpc_get *)rpc;
2487
2488 data = lyd_new(NULL, ietfnc, "get");
2489 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002490 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002491 node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002492 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002493 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002494 node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002495 lyd_insert_attr(node, NULL, "type", "xpath");
2496 lyd_insert_attr(node, NULL, "select", rpc_g->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002497 }
2498 if (!node) {
2499 lyd_free(data);
2500 return NC_MSG_ERROR;
2501 }
2502 }
2503
2504 if (rpc_g->wd_mode) {
2505 if (!ietfncwd) {
Radek Krejci3222b7d2017-09-21 16:04:30 +02002506 ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002507 if (!ietfncwd) {
Michal Vasko086cd572017-01-12 12:19:05 +01002508 ERR("Session %u: missing \"ietf-netconf-with-defaults\" schema in the context.", session->id);
Michal Vaskoc41fdea2017-08-09 10:18:44 +02002509 lyd_free(data);
Michal Vasko086311b2016-01-08 09:53:11 +01002510 return NC_MSG_ERROR;
2511 }
2512 }
2513 switch (rpc_g->wd_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01002514 case NC_WD_ALL:
2515 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all");
2516 break;
2517 case NC_WD_ALL_TAG:
2518 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged");
2519 break;
2520 case NC_WD_TRIM:
2521 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim");
2522 break;
2523 case NC_WD_EXPLICIT:
2524 node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit");
2525 break;
Michal Vasko2260f552018-06-06 10:06:12 +02002526 default:
2527 /* cannot get here */
2528 node = NULL;
2529 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002530 }
2531 if (!node) {
2532 lyd_free(data);
2533 return NC_MSG_ERROR;
2534 }
2535 }
2536 break;
2537
2538 case NC_RPC_KILL:
2539 rpc_k = (struct nc_rpc_kill *)rpc;
2540
2541 data = lyd_new(NULL, ietfnc, "kill-session");
2542 sprintf(str, "%u", rpc_k->sid);
2543 lyd_new_leaf(data, ietfnc, "session-id", str);
2544 break;
2545
2546 case NC_RPC_COMMIT:
2547 rpc_com = (struct nc_rpc_commit *)rpc;
2548
2549 data = lyd_new(NULL, ietfnc, "commit");
2550 if (rpc_com->confirmed) {
2551 lyd_new_leaf(data, ietfnc, "confirmed", NULL);
2552 }
2553
2554 if (rpc_com->confirm_timeout) {
2555 sprintf(str, "%u", rpc_com->confirm_timeout);
2556 lyd_new_leaf(data, ietfnc, "confirm-timeout", str);
2557 }
2558
2559 if (rpc_com->persist) {
2560 node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist);
2561 if (!node) {
2562 lyd_free(data);
2563 return NC_MSG_ERROR;
2564 }
2565 }
2566
2567 if (rpc_com->persist_id) {
2568 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id);
2569 if (!node) {
2570 lyd_free(data);
2571 return NC_MSG_ERROR;
2572 }
2573 }
2574 break;
2575
2576 case NC_RPC_DISCARD:
2577 data = lyd_new(NULL, ietfnc, "discard-changes");
2578 break;
2579
2580 case NC_RPC_CANCEL:
2581 rpc_can = (struct nc_rpc_cancel *)rpc;
2582
2583 data = lyd_new(NULL, ietfnc, "cancel-commit");
2584 if (rpc_can->persist_id) {
2585 node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id);
2586 if (!node) {
2587 lyd_free(data);
2588 return NC_MSG_ERROR;
2589 }
2590 }
2591 break;
2592
2593 case NC_RPC_VALIDATE:
2594 rpc_val = (struct nc_rpc_validate *)rpc;
2595
2596 data = lyd_new(NULL, ietfnc, "validate");
2597 node = lyd_new(data, ietfnc, "source");
2598 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002599 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002600 node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML);
Michal Vasko086311b2016-01-08 09:53:11 +01002601 } else {
2602 node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src);
2603 }
2604 } else {
2605 node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL);
2606 }
2607 if (!node) {
2608 lyd_free(data);
2609 return NC_MSG_ERROR;
2610 }
2611 break;
2612
2613 case NC_RPC_GETSCHEMA:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002614 ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002615 if (!ietfncmon) {
Michal Vasko086cd572017-01-12 12:19:05 +01002616 ERR("Session %u: missing \"ietf-netconf-monitoring\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002617 return NC_MSG_ERROR;
2618 }
2619
2620 rpc_gs = (struct nc_rpc_getschema *)rpc;
2621
2622 data = lyd_new(NULL, ietfncmon, "get-schema");
2623 node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier);
2624 if (!node) {
2625 lyd_free(data);
2626 return NC_MSG_ERROR;
2627 }
2628 if (rpc_gs->version) {
2629 node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version);
2630 if (!node) {
2631 lyd_free(data);
2632 return NC_MSG_ERROR;
2633 }
2634 }
2635 if (rpc_gs->format) {
2636 node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format);
2637 if (!node) {
2638 lyd_free(data);
2639 return NC_MSG_ERROR;
2640 }
2641 }
2642 break;
2643
2644 case NC_RPC_SUBSCRIBE:
Radek Krejci3222b7d2017-09-21 16:04:30 +02002645 notifs = ly_ctx_get_module(session->ctx, "notifications", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +01002646 if (!notifs) {
Michal Vasko086cd572017-01-12 12:19:05 +01002647 ERR("Session %u: missing \"notifications\" schema in the context.", session->id);
Michal Vasko086311b2016-01-08 09:53:11 +01002648 return NC_MSG_ERROR;
2649 }
2650
2651 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2652
2653 data = lyd_new(NULL, notifs, "create-subscription");
2654 if (rpc_sub->stream) {
2655 node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream);
2656 if (!node) {
2657 lyd_free(data);
2658 return NC_MSG_ERROR;
2659 }
2660 }
2661
2662 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002663 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoc741ddb2016-10-07 13:34:57 +02002664 node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML);
Michal Vasko303245c2016-03-24 15:20:03 +01002665 lyd_insert_attr(node, NULL, "type", "subtree");
Michal Vasko086311b2016-01-08 09:53:11 +01002666 } else {
Radek Krejci539efb62016-08-24 15:05:16 +02002667 node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING);
Michal Vasko303245c2016-03-24 15:20:03 +01002668 lyd_insert_attr(node, NULL, "type", "xpath");
2669 lyd_insert_attr(node, NULL, "select", rpc_sub->filter);
Michal Vasko086311b2016-01-08 09:53:11 +01002670 }
2671 if (!node) {
2672 lyd_free(data);
2673 return NC_MSG_ERROR;
2674 }
2675 }
2676
2677 if (rpc_sub->start) {
2678 node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start);
2679 if (!node) {
2680 lyd_free(data);
2681 return NC_MSG_ERROR;
2682 }
2683 }
2684
2685 if (rpc_sub->stop) {
2686 node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop);
2687 if (!node) {
2688 lyd_free(data);
2689 return NC_MSG_ERROR;
2690 }
2691 }
2692 break;
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002693 default:
2694 ERRINT;
2695 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002696 }
2697
Michal Vasko131120a2018-05-29 15:44:02 +02002698 if (!data) {
2699 /* error was already printed */
2700 return NC_MSG_ERROR;
2701 }
2702
Michal Vasko41adf392017-01-17 10:39:04 +01002703 if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_NOEXTDEPS
2704 | (session->flags & NC_SESSION_CLIENT_NOT_STRICT ? 0 : LYD_OPT_STRICT), NULL)) {
Radek Krejcib4b19062018-02-07 16:33:06 +01002705 if (dofree) {
2706 lyd_free(data);
2707 }
Michal Vasko086311b2016-01-08 09:53:11 +01002708 return NC_MSG_ERROR;
2709 }
2710
Michal Vasko131120a2018-05-29 15:44:02 +02002711 /* send RPC, store its message ID */
2712 r = nc_send_msg_io(session, timeout, data);
2713 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002714
Radek Krejcib4b19062018-02-07 16:33:06 +01002715 if (dofree) {
2716 lyd_free(data);
2717 }
Michal Vasko086311b2016-01-08 09:53:11 +01002718
Michal Vasko131120a2018-05-29 15:44:02 +02002719 if (r == NC_MSG_RPC) {
2720 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01002721 }
Michal Vasko131120a2018-05-29 15:44:02 +02002722 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01002723}
Michal Vaskode2946c2017-01-12 12:19:26 +01002724
2725API void
2726nc_client_session_set_not_strict(struct nc_session *session)
2727{
2728 if (session->side != NC_CLIENT) {
2729 ERRARG("session");
2730 return;
2731 }
2732
2733 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
2734}