blob: c7ff7ccd08dac4d9081a008e165b98f67a0df820 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_client.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 session client functions
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
Radek Krejcifd5b6682017-06-13 15:52:53 +020016#define _GNU_SOURCE
Michal Vaskobbf52c82020-04-07 13:08:50 +020017
18#ifdef __linux__
19# include <sys/syscall.h>
20#endif
21
Michal Vaskob83a3fa2021-05-26 09:53:42 +020022#include <arpa/inet.h>
Michal Vasko086311b2016-01-08 09:53:11 +010023#include <assert.h>
tadeas-vintrlik54f142a2021-08-23 10:36:18 +020024#include <ctype.h>
Michal Vasko086311b2016-01-08 09:53:11 +010025#include <errno.h>
26#include <fcntl.h>
27#include <netdb.h>
Radek Krejci4cf58ec2016-02-26 15:04:52 +010028#include <netinet/in.h>
Michal Vasko83ad17e2019-01-30 10:11:37 +010029#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <poll.h>
Michal Vasko086311b2016-01-08 09:53:11 +010031#include <pthread.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010033#include <stdlib.h>
34#include <string.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020035#include <sys/select.h>
Michal Vasko086311b2016-01-08 09:53:11 +010036#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020039#include <sys/un.h>
Michal Vasko086311b2016-01-08 09:53:11 +010040#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010041
42#include <libyang/libyang.h>
43
Michal Vasko9e8ac262020-04-07 13:06:45 +020044#include "compat.h"
Michal Vasko086311b2016-01-08 09:53:11 +010045#include "libnetconf.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010046#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020047#include "session_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010048
Michal Vasko8a4e1462020-05-07 11:32:31 +020049#include "../modules/ietf_netconf@2013-09-29_yang.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020050#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
Michal Vasko8a4e1462020-05-07 11:32:31 +020051
Michal Vasko80ef5d22016-01-18 09:21:02 +010052static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
53
Radek Krejci62aa0642017-05-25 16:33:49 +020054#ifdef NC_ENABLED_SSH
Radek Krejci62aa0642017-05-25 16:33:49 +020055char *sshauth_password(const char *username, const char *hostname, void *priv);
56char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020057char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
Radek Krejci62aa0642017-05-25 16:33:49 +020058#endif /* NC_ENABLED_SSH */
59
60static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
61static pthread_key_t nc_client_context_key;
62#ifdef __linux__
63static struct nc_client_context context_main = {
Michal Vaskoe49a15f2019-05-27 14:18:36 +020064 .opts.ka = {
65 .enabled = 1,
66 .idle_time = 1,
67 .max_probes = 10,
68 .probe_interval = 5
69 },
Radek Krejci62aa0642017-05-25 16:33:49 +020070#ifdef NC_ENABLED_SSH
71 .ssh_opts = {
roman41a11e42022-06-22 09:27:08 +020072 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Radek Krejci62aa0642017-05-25 16:33:49 +020073 .auth_password = sshauth_password,
74 .auth_interactive = sshauth_interactive,
75 .auth_privkey_passphrase = sshauth_privkey_passphrase
76 },
77 .ssh_ch_opts = {
78 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Radek Krejci62aa0642017-05-25 16:33:49 +020079 .auth_password = sshauth_password,
80 .auth_interactive = sshauth_interactive,
81 .auth_privkey_passphrase = sshauth_privkey_passphrase
Radek Krejci5cebc6b2017-05-26 13:24:38 +020082 },
Radek Krejci62aa0642017-05-25 16:33:49 +020083#endif /* NC_ENABLED_SSH */
84 /* .tls_ structures zeroed */
Radek Krejci5cebc6b2017-05-26 13:24:38 +020085 .refcount = 0
Radek Krejci62aa0642017-05-25 16:33:49 +020086};
87#endif
88
89static void
90nc_client_context_free(void *ptr)
91{
92 struct nc_client_context *c = (struct nc_client_context *)ptr;
93
Radek Krejci5cebc6b2017-05-26 13:24:38 +020094 if (--(c->refcount)) {
95 /* still used */
96 return;
97 }
98
Radek Krejci62aa0642017-05-25 16:33:49 +020099#ifdef __linux__
100 /* in __linux__ we use static memory in the main thread,
101 * so this check is for programs terminating the main()
102 * function by pthread_exit() :)
103 */
104 if (c != &context_main)
105#endif
106 {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200107 /* for the main thread the same is done in nc_client_destroy() */
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400108 free(c->opts.schema_searchpath);
109
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200110#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400111 int i;
Michal Vasko292c5542023-02-01 14:33:17 +0100112
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400113 for (i = 0; i < c->opts.ch_bind_count; ++i) {
114 close(c->opts.ch_binds[i].sock);
115 free((char *)c->opts.ch_binds[i].address);
116 }
117 free(c->opts.ch_binds);
118 c->opts.ch_binds = NULL;
119 c->opts.ch_bind_count = 0;
Radek Krejci62aa0642017-05-25 16:33:49 +0200120#endif
121#ifdef NC_ENABLED_SSH
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400122 _nc_client_ssh_destroy_opts(&c->ssh_opts);
123 _nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200124#endif
125#ifdef NC_ENABLED_TLS
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400126 _nc_client_tls_destroy_opts(&c->tls_opts);
127 _nc_client_tls_destroy_opts(&c->tls_ch_opts);
Radek Krejci62aa0642017-05-25 16:33:49 +0200128#endif
129 free(c);
130 }
131}
132
133static void
134nc_client_context_createkey(void)
135{
136 int r;
137
138 /* initiate */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200139 while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
Radek Krejci62aa0642017-05-25 16:33:49 +0200140 pthread_setspecific(nc_client_context_key, NULL);
141}
142
143struct nc_client_context *
144nc_client_context_location(void)
145{
146 struct nc_client_context *e;
147
148 pthread_once(&nc_client_context_once, nc_client_context_createkey);
149 e = pthread_getspecific(nc_client_context_key);
150 if (!e) {
151 /* prepare ly_err storage */
152#ifdef __linux__
153 if (getpid() == syscall(SYS_gettid)) {
154 /* main thread - use global variable instead of thread-specific variable. */
155 e = &context_main;
156 } else
157#endif /* __linux__ */
158 {
159 e = calloc(1, sizeof *e);
160 /* set default values */
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200161 e->refcount = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200162#ifdef NC_ENABLED_SSH
163 e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
roman41a11e42022-06-22 09:27:08 +0200164 e->ssh_opts.auth_pref[0].value = 1;
Radek Krejci62aa0642017-05-25 16:33:49 +0200165 e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
166 e->ssh_opts.auth_pref[1].value = 2;
167 e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
roman41a11e42022-06-22 09:27:08 +0200168 e->ssh_opts.auth_pref[2].value = 3;
Radek Krejci62aa0642017-05-25 16:33:49 +0200169 e->ssh_opts.auth_password = sshauth_password;
170 e->ssh_opts.auth_interactive = sshauth_interactive;
171 e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
172
roman41a11e42022-06-22 09:27:08 +0200173 /* callhome settings are the same */
Radek Krejci62aa0642017-05-25 16:33:49 +0200174 memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
175 e->ssh_ch_opts.auth_pref[0].value = 1;
176 e->ssh_ch_opts.auth_pref[1].value = 2;
177 e->ssh_ch_opts.auth_pref[2].value = 3;
178#endif /* NC_ENABLED_SSH */
179 }
180 pthread_setspecific(nc_client_context_key, e);
181 }
182
183 return e;
184}
185
186#define client_opts nc_client_context_location()->opts
Michal Vasko086311b2016-01-08 09:53:11 +0100187
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200188API void *
189nc_client_get_thread_context(void)
190{
191 return nc_client_context_location();
192}
193
194API void
195nc_client_set_thread_context(void *context)
196{
197 struct nc_client_context *old, *new;
198
199 if (!context) {
roman40672412023-05-04 11:10:22 +0200200 ERRARG(NULL, "context");
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200201 return;
202 }
203
204 new = (struct nc_client_context *)context;
205 old = nc_client_context_location();
206 if (old == new) {
207 /* nothing to change */
208 return;
209 }
210
211 /* replace old by new, increase reference counter in the newly set context */
212 nc_client_context_free(old);
213 new->refcount++;
214 pthread_setspecific(nc_client_context_key, new);
215}
216
Michal Vasko78939072022-12-12 07:43:18 +0100217/**
218 * @brief Ext data callback for a context to provide schema mount data.
219 */
220static LY_ERR
221nc_ly_ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
222{
223 struct nc_session *session = user_data;
224
225 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) {
226 return LY_EINVAL;
227 }
228
229 if (!session->opts.client.ext_data) {
230 ERR(session, "Unable to parse mounted data, no operational schema-mounts data received from the server.");
231 return LY_ENOTFOUND;
232 }
233
234 /* return ext data */
235 *ext_data = session->opts.client.ext_data;
236 *ext_data_free = 0;
237
238 return LY_SUCCESS;
239}
240
Radek Krejcifd5b6682017-06-13 15:52:53 +0200241int
Michal Vasko78939072022-12-12 07:43:18 +0100242nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200243{
244 /* assign context (dicionary needed for handshake) */
245 if (!ctx) {
Michal Vasko77367452021-02-16 16:32:18 +0100246 if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200247 return EXIT_FAILURE;
248 }
249
Michal Vasko5ca5d972022-09-14 13:51:31 +0200250 /* user path must be first, the first path is used to store modules retreived via get-schema */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200251 if (client_opts.schema_searchpath) {
252 ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
Michal Vasko5d0cdc32022-12-12 07:38:21 +0100253 }
Michal Vasko80227222022-12-12 09:05:24 +0100254 if (!access(NC_CLIENT_SEARCH_DIR, F_OK)) {
255 ly_ctx_set_searchdir(ctx, NC_CLIENT_SEARCH_DIR);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200256 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200257
Michal Vasko5ca5d972022-09-14 13:51:31 +0200258 /* set callback for getting modules, if provided */
Radek Krejcifd5b6682017-06-13 15:52:53 +0200259 ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
Michal Vasko78939072022-12-12 07:43:18 +0100260
261 /* set ext data callback to avoid errors that no callback is set, the data are stored later, if any */
262 ly_ctx_set_ext_data_clb(ctx, nc_ly_ext_data_clb, session);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200263 } else {
264 session->flags |= NC_SESSION_SHAREDCTX;
265 }
266
267 session->ctx = ctx;
268
269 return EXIT_SUCCESS;
270}
271
Michal Vasko086311b2016-01-08 09:53:11 +0100272API int
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100273nc_client_set_schema_searchpath(const char *path)
Michal Vasko086311b2016-01-08 09:53:11 +0100274{
Michal Vasko3031aae2016-01-27 16:07:18 +0100275 if (client_opts.schema_searchpath) {
276 free(client_opts.schema_searchpath);
Michal Vasko086311b2016-01-08 09:53:11 +0100277 }
Michal Vasko086311b2016-01-08 09:53:11 +0100278
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100279 if (path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100280 client_opts.schema_searchpath = strdup(path);
281 if (!client_opts.schema_searchpath) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100282 ERRMEM;
283 return 1;
284 }
285 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100286 client_opts.schema_searchpath = NULL;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100287 }
288
289 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100290}
291
Michal Vasko7f1c0ef2016-03-11 11:13:06 +0100292API const char *
293nc_client_get_schema_searchpath(void)
294{
295 return client_opts.schema_searchpath;
296}
297
Radek Krejcifd5b6682017-06-13 15:52:53 +0200298API int
299nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
Michal Vasko086311b2016-01-08 09:53:11 +0100300{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200301 client_opts.schema_clb = clb;
302 if (clb) {
303 client_opts.schema_clb_data = user_data;
304 } else {
305 client_opts.schema_clb_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100306 }
307
308 return 0;
309}
310
Radek Krejcifd5b6682017-06-13 15:52:53 +0200311API ly_module_imp_clb
312nc_client_get_schema_callback(void **user_data)
313{
314 if (user_data) {
315 (*user_data) = client_opts.schema_clb_data;
316 }
317 return client_opts.schema_clb;
318}
319
Michal Vaskoa272e092023-07-10 14:34:03 +0200320API void
321nc_client_set_new_session_context_autofill(int enabled)
322{
323 client_opts.auto_context_fill_disabled = !enabled;
324}
325
Michal Vasko5ca5d972022-09-14 13:51:31 +0200326struct module_info {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200327 char *name;
328 char *revision;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200329
Radek Krejci65ef6d52018-08-16 16:35:02 +0200330 struct {
331 char *name;
332 char *revision;
333 } *submodules;
Michal Vasko77367452021-02-16 16:32:18 +0100334 char **features;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200335 int implemented;
336};
337
338struct clb_data_s {
339 void *user_data;
340 ly_module_imp_clb user_clb;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200341 struct module_info *modules;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200342 struct nc_session *session;
343 int has_get_schema;
344};
345
Michal Vaskoc088f982021-10-05 12:23:07 +0200346/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200347 * @brief Retrieve YANG module content from a local file.
Michal Vaskoc088f982021-10-05 12:23:07 +0200348 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200349 * @param[in] name Module name.
350 * @param[in] rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200351 * @param[in] clb_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200352 * @param[out] format Module format.
353 * @return Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200354 */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200355static char *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200356retrieve_module_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200357 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100358{
Radek Krejci65ef6d52018-08-16 16:35:02 +0200359 char *localfile = NULL;
360 FILE *f;
361 long length, l;
362 char *model_data = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100363
Radek Krejci3b60e5c2018-08-17 10:10:16 +0200364 if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200365 !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
366 name, rev, &localfile, format)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200367 return NULL;
368 }
369 if (localfile) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200370 VRB(clb_data->session, "Reading module \"%s@%s\" from local file \"%s\".", name, rev ? rev : "<latest>",
Michal Vasko7502b602022-08-26 11:51:17 +0200371 localfile);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200372 f = fopen(localfile, "r");
373 if (!f) {
Michal Vasko7502b602022-08-26 11:51:17 +0200374 ERR(clb_data->session, "Unable to open file \"%s\" (%s).", localfile, strerror(errno));
Radek Krejci65ef6d52018-08-16 16:35:02 +0200375 free(localfile);
376 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100377 }
Michal Vasko086311b2016-01-08 09:53:11 +0100378
Radek Krejci65ef6d52018-08-16 16:35:02 +0200379 fseek(f, 0, SEEK_END);
380 length = ftell(f);
Radek Krejci3f499a62018-08-17 10:16:57 +0200381 if (length < 0) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200382 ERR(clb_data->session, "Unable to get the size of module file \"%s\".", localfile);
Radek Krejci3f499a62018-08-17 10:16:57 +0200383 free(localfile);
384 fclose(f);
385 return NULL;
386 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200387 fseek(f, 0, SEEK_SET);
388
389 model_data = malloc(length + 1);
390 if (!model_data) {
391 ERRMEM;
392 } else if ((l = fread(model_data, 1, length, f)) != length) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200393 ERR(clb_data->session, "Reading module from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
Michal Vasko05532772021-06-03 12:12:38 +0200394 length);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200395 free(model_data);
396 model_data = NULL;
397 } else {
398 /* terminating NULL byte */
399 model_data[length] = '\0';
Michal Vasko086311b2016-01-08 09:53:11 +0100400 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200401 fclose(f);
402 free(localfile);
Michal Vasko086311b2016-01-08 09:53:11 +0100403 }
404
Radek Krejci65ef6d52018-08-16 16:35:02 +0200405 return model_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100406}
407
Michal Vaskoc088f982021-10-05 12:23:07 +0200408/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200409 * @brief Retrieve YANG module content from a reply to get-schema RPC.
Michal Vaskoc088f982021-10-05 12:23:07 +0200410 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200411 * @param[in] name Module name.
412 * @param[in] rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200413 * @param[in] clb_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200414 * @param[out] format Module format.
415 * @return Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200416 */
Michal Vasko086311b2016-01-08 09:53:11 +0100417static char *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200418retrieve_module_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200419 LYS_INFORMAT *format)
Michal Vasko086311b2016-01-08 09:53:11 +0100420{
Michal Vasko086311b2016-01-08 09:53:11 +0100421 struct nc_rpc *rpc;
Michal Vasko77367452021-02-16 16:32:18 +0100422 struct lyd_node *envp = NULL, *op = NULL;
423 struct lyd_node_any *get_schema_data;
Michal Vasko086311b2016-01-08 09:53:11 +0100424 NC_MSG_TYPE msg;
Michal Vasko086311b2016-01-08 09:53:11 +0100425 uint64_t msgid;
Michal Vasko7502b602022-08-26 11:51:17 +0200426 char *localfile = NULL, *envp_str = NULL, *model_data = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200427 FILE *f;
Michal Vasko086311b2016-01-08 09:53:11 +0100428
Michal Vasko5ca5d972022-09-14 13:51:31 +0200429 VRB(clb_data->session, "Reading module \"%s@%s\" from server via get-schema.", name, rev ? rev : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200430 rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
Michal Vasko086311b2016-01-08 09:53:11 +0100431
Radek Krejci65ef6d52018-08-16 16:35:02 +0200432 while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
Michal Vasko086311b2016-01-08 09:53:11 +0100433 usleep(1000);
434 }
435 if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200436 ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
Michal Vasko086311b2016-01-08 09:53:11 +0100437 nc_rpc_free(rpc);
438 return NULL;
439 }
440
Michal Vaskoff8ddc02016-10-03 14:13:29 +0200441 do {
Michal Vasko77367452021-02-16 16:32:18 +0100442 msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200443 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Michal Vasko086311b2016-01-08 09:53:11 +0100444 nc_rpc_free(rpc);
445 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko05532772021-06-03 12:12:38 +0200446 ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
Michal Vasko77367452021-02-16 16:32:18 +0100447 goto cleanup;
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100448 } else if (msg == NC_MSG_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200449 ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
Michal Vasko77367452021-02-16 16:32:18 +0100450 goto cleanup;
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100451 } else if (!op) {
Michal Vasko7502b602022-08-26 11:51:17 +0200452 assert(envp);
453 lyd_print_mem(&envp_str, envp, LYD_XML, 0);
454 WRN(clb_data->session, "Received an unexpected reply to <get-schema>:\n%s", envp_str);
455 free(envp_str);
Michal Vasko8d0f3b32022-01-12 08:29:14 +0100456 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +0100457 }
458
Michal Vasko77367452021-02-16 16:32:18 +0100459 if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
Michal Vasko05532772021-06-03 12:12:38 +0200460 ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
Michal Vasko77367452021-02-16 16:32:18 +0100461 goto cleanup;
Michal Vaskob2583f12016-05-12 11:40:23 +0200462 }
Michal Vasko77367452021-02-16 16:32:18 +0100463 get_schema_data = (struct lyd_node_any *)lyd_child(op);
Radek Krejci539efb62016-08-24 15:05:16 +0200464 switch (get_schema_data->value_type) {
Radek Krejci539efb62016-08-24 15:05:16 +0200465 case LYD_ANYDATA_STRING:
Michal Vasko77367452021-02-16 16:32:18 +0100466 case LYD_ANYDATA_XML:
Michal Vaskob2583f12016-05-12 11:40:23 +0200467 model_data = strdup(get_schema_data->value.str);
Radek Krejci539efb62016-08-24 15:05:16 +0200468 break;
469 case LYD_ANYDATA_DATATREE:
Michal Vasko77367452021-02-16 16:32:18 +0100470 lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Radek Krejci539efb62016-08-24 15:05:16 +0200471 break;
Radek Krejci03438ec2016-09-21 14:12:26 +0200472 case LYD_ANYDATA_JSON:
Michal Vaskod838d292018-08-15 11:39:05 +0200473 case LYD_ANYDATA_LYB:
Radek Krejci03438ec2016-09-21 14:12:26 +0200474 ERRINT;
Michal Vasko77367452021-02-16 16:32:18 +0100475 break;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200476 }
Michal Vasko086311b2016-01-08 09:53:11 +0100477
Radek Krejci488b0702018-08-29 14:47:15 +0200478 if (model_data && !model_data[0]) {
479 /* empty data */
480 free(model_data);
481 model_data = NULL;
482 }
Michal Vaskoa1721172022-12-12 09:06:53 +0100483 if (!model_data) {
484 goto cleanup;
485 }
486
487 /* set format */
488 *format = LYS_IN_YANG;
Radek Krejci488b0702018-08-29 14:47:15 +0200489
Michal Vasko5ca5d972022-09-14 13:51:31 +0200490 /* try to store the model_data into local module repository */
Michal Vaskoa1721172022-12-12 09:06:53 +0100491 lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), 0, name, rev, &localfile, NULL);
492 if (client_opts.schema_searchpath && !localfile) {
493 if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
494 rev ? rev : "") == -1) {
495 ERRMEM;
496 } else {
497 f = fopen(localfile, "w");
498 if (!f) {
499 WRN(clb_data->session, "Unable to store \"%s\" as a local copy of module retrieved via <get-schema> (%s).",
500 localfile, strerror(errno));
Michal Vaskof945da52018-02-15 08:45:13 +0100501 } else {
Michal Vaskoa1721172022-12-12 09:06:53 +0100502 fputs(model_data, f);
503 fclose(f);
Michal Vaskof945da52018-02-15 08:45:13 +0100504 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200505 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200506 }
Michal Vaskoa1721172022-12-12 09:06:53 +0100507 free(localfile);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200508
Michal Vasko77367452021-02-16 16:32:18 +0100509cleanup:
510 lyd_free_tree(envp);
511 lyd_free_tree(op);
Michal Vasko086311b2016-01-08 09:53:11 +0100512 return model_data;
513}
514
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200515static void
516free_with_user_data(void *data, void *user_data)
Jan Kundrát35972df2018-09-06 19:00:01 +0200517{
518 free(data);
519 (void)user_data;
520}
521
Michal Vaskoc088f982021-10-05 12:23:07 +0200522/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200523 * @brief Retrieve YANG module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200524 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200525 * @param[in] mod_name Module name.
526 * @param[in] mod_rev Module revision.
Michal Vasko5e32c402022-01-12 14:05:09 +0100527 * @param[in] user_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200528 * @param[out] format Module format.
529 * @param[out] module_data Module content.
Michal Vasko5e32c402022-01-12 14:05:09 +0100530 * @param[out] free_module_data Callback for freeing @p module_data.
531 * @return LY_ERR value.
532 */
533static LY_ERR
Michal Vasko5ca5d972022-09-14 13:51:31 +0200534retrieve_module_data(const char *mod_name, const char *mod_rev, void *user_data, LYS_INFORMAT *format,
Michal Vasko5e32c402022-01-12 14:05:09 +0100535 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
536{
537 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
538 char *model_data = NULL;
539
Michal Vasko5e32c402022-01-12 14:05:09 +0100540 /* 1. try to get data locally */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200541 model_data = retrieve_module_data_localfile(mod_name, mod_rev, clb_data, format);
Michal Vasko5e32c402022-01-12 14:05:09 +0100542
543 /* 2. try to use <get-schema> */
544 if (!model_data && clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200545 model_data = retrieve_module_data_getschema(mod_name, mod_rev, clb_data, format);
Michal Vasko5e32c402022-01-12 14:05:09 +0100546 }
547
548 /* 3. try to use user callback */
549 if (!model_data && clb_data->user_clb) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200550 VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", mod_name, mod_rev ? mod_rev : "<latest>");
Michal Vasko5e32c402022-01-12 14:05:09 +0100551 clb_data->user_clb(mod_name, mod_rev, NULL, NULL, clb_data->user_data, format, (const char **)&model_data,
552 free_module_data);
553 }
554
555 *free_module_data = free_with_user_data;
556 *module_data = model_data;
557 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
558}
559
560/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200561 * @brief Retrieve YANG import module content.
Michal Vasko5e32c402022-01-12 14:05:09 +0100562 *
Michal Vasko5ca5d972022-09-14 13:51:31 +0200563 * @param[in] mod_name Module name.
564 * @param[in] mod_rev Module revision.
Michal Vaskoc088f982021-10-05 12:23:07 +0200565 * @param[in] submod_name Optional submodule name.
566 * @param[in] sub_rev Submodule revision.
567 * @param[in] user_data get-schema callback data.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200568 * @param[out] format Module format.
569 * @param[out] module_data Module content.
Michal Vaskoc088f982021-10-05 12:23:07 +0200570 * @param[out] free_module_data Callback for freeing @p module_data.
571 * @return LY_ERR value.
572 */
Michal Vasko77367452021-02-16 16:32:18 +0100573static LY_ERR
Michal Vasko5ca5d972022-09-14 13:51:31 +0200574retrieve_module_data_imp(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
Michal Vasko77367452021-02-16 16:32:18 +0100575 void *user_data, LYS_INFORMAT *format, const char **module_data,
576 void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci65ef6d52018-08-16 16:35:02 +0200577{
578 struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
Michal Vasko5e32c402022-01-12 14:05:09 +0100579 uint32_t u, v, match = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200580 const char *name = NULL, *rev = NULL;
581 char *model_data = NULL;
582
Michal Vasko5ca5d972022-09-14 13:51:31 +0200583 /* get and check the final name and revision of the module to be retrieved */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200584 if (!mod_rev || !mod_rev[0]) {
585 /* newest revision requested - get the newest revision from the list of available modules on server */
586 match = 0;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200587 for (u = 0; clb_data->modules[u].name; ++u) {
588 if (strcmp(mod_name, clb_data->modules[u].name)) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200589 continue;
590 }
Michal Vasko5ca5d972022-09-14 13:51:31 +0200591 if (!match || (strcmp(mod_rev, clb_data->modules[u].revision) > 0)) {
592 mod_rev = clb_data->modules[u].revision;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200593 }
594 match = u + 1;
595 }
596 if (!match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200597 /* valid situation if we are retrieving YANG 1.1 module and have only capabilities for now
Michal Vasko1d2665c2021-02-12 09:28:44 +0100598 * (when loading ietf-datastore for ietf-yang-library) */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200599 VRB(clb_data->session, "Unable to identify revision of the import module \"%s\" from "
Michal Vasko05532772021-06-03 12:12:38 +0200600 "the available server side information.", mod_name);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200601 }
602 }
603 if (submod_name) {
604 name = submod_name;
605 if (sub_rev) {
606 rev = sub_rev;
Radek Krejci6455eb12018-08-17 09:26:29 +0200607 } else if (match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200608 if (!clb_data->modules[match - 1].submodules) {
Michal Vasko05532772021-06-03 12:12:38 +0200609 VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
Michal Vasko5ca5d972022-09-14 13:51:31 +0200610 "in import module \"%s\", from the available server side information.", submod_name, mod_name);
Radek Krejci1a7efb42018-08-17 11:51:23 +0200611 } else {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200612 for (v = 0; clb_data->modules[match - 1].submodules[v].name; ++v) {
613 if (!strcmp(submod_name, clb_data->modules[match - 1].submodules[v].name)) {
614 rev = sub_rev = clb_data->modules[match - 1].submodules[v].revision;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200615 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200616 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200617 if (!rev) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200618 ERR(clb_data->session, "Requested submodule \"%s\" is not found in import module \"%s\" on server side.",
Michal Vasko05532772021-06-03 12:12:38 +0200619 submod_name, mod_name);
Michal Vasko77367452021-02-16 16:32:18 +0100620 return LY_ENOTFOUND;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200621 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200622 }
623 }
624 } else {
625 name = mod_name;
626 rev = mod_rev;
627 }
628
Radek Krejci65ef6d52018-08-16 16:35:02 +0200629 if (match) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200630 /* we have enough information to avoid communication with server and try to get the module locally */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200631
632 /* 1. try to get data locally */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200633 model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200634
635 /* 2. try to use <get-schema> */
636 if (!model_data && clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200637 model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200638 }
639 } else {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200640 /* we are unsure which revision of the module we should load, so first try to get
Radek Krejci65ef6d52018-08-16 16:35:02 +0200641 * the newest revision from the server via get-schema and only if the server does not
642 * implement get-schema, try to load the newest revision locally. This is imperfect
643 * solution, but there are situation when a client does not know what revision is
644 * actually implemented by the server. */
645
646 /* 1. try to use <get-schema> */
647 if (clb_data->has_get_schema) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200648 model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200649 }
650
651 /* 2. try to get data locally */
652 if (!model_data) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200653 model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200654 }
655 }
656
657 /* 3. try to use user callback */
658 if (!model_data && clb_data->user_clb) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200659 VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", name, rev ? rev : "<latest>");
Michal Vasko77367452021-02-16 16:32:18 +0100660 clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
661 (const char **)&model_data, free_module_data);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200662 }
663
Jan Kundrát35972df2018-09-06 19:00:01 +0200664 *free_module_data = free_with_user_data;
Michal Vasko77367452021-02-16 16:32:18 +0100665 *module_data = model_data;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200666 return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200667}
668
Michal Vaskoc088f982021-10-05 12:23:07 +0200669/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200670 * @brief Load a YANG module into context.
Michal Vaskoc088f982021-10-05 12:23:07 +0200671 *
672 * @param[in] session NC session.
Michal Vasko5ca5d972022-09-14 13:51:31 +0200673 * @param[in] name Module name.
674 * @param[in] revision Module revision.
675 * @param[in] features Enabled module features.
676 * @param[in] modules Server module info built from capabilities.
677 * @param[in] user_clb User callback for retrieving module data.
Michal Vaskoc088f982021-10-05 12:23:07 +0200678 * @param[in] user_data User data for @p user_clb.
679 * @param[in] has_get_schema Whether the server supports get-schema.
680 * @param[out] mod Loaded module.
681 * @return 0 on success.
682 * @return -1 on error.
683 */
Michal Vaskoceae0152018-02-14 16:03:59 +0100684static int
Michal Vasko5e32c402022-01-12 14:05:09 +0100685nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, const char **features,
Michal Vasko5ca5d972022-09-14 13:51:31 +0200686 struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod)
Michal Vaskoceae0152018-02-14 16:03:59 +0100687{
688 int ret = 0;
689 struct ly_err_item *eitem;
Jan Kundrát6d7c3962018-09-06 19:01:07 +0200690 const char *module_data = NULL;
Michal Vasko5e32c402022-01-12 14:05:09 +0100691 struct ly_in *in;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200692 LYS_INFORMAT format;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200693
694 void (*free_module_data)(void *, void *) = NULL;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200695 struct clb_data_s clb_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100696
Michal Vasko5e32c402022-01-12 14:05:09 +0100697 /* try to use a module from the context */
Michal Vaskoc3e402f2022-09-14 13:34:55 +0200698 *mod = ly_ctx_get_module_implemented(session->ctx, name);
699 if (!*mod) {
700 if (revision) {
701 *mod = ly_ctx_get_module(session->ctx, name, revision);
702 } else {
Michal Vasko5e32c402022-01-12 14:05:09 +0100703 *mod = ly_ctx_get_module_latest(session->ctx, name);
704 }
Michal Vaskoc3e402f2022-09-14 13:34:55 +0200705 } else if (revision && (!(*mod)->revision || strcmp((*mod)->revision, revision))) {
706 WRN(session, "Server implements module \"%s\" in revision \"%s\" but revision \"%s\" is already implemented"
707 " and will be used instead.", name, revision, (*mod)->revision ? (*mod)->revision : "<none>");
Radek Krejci65ef6d52018-08-16 16:35:02 +0200708 }
Michal Vasko5e32c402022-01-12 14:05:09 +0100709
Michal Vaskoceae0152018-02-14 16:03:59 +0100710 if (*mod) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100711 /* make the present module implemented and/or enable all its features */
712 if (lys_set_implemented(*mod, features)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200713 ERR(session, "Failed to implement module \"%s\".", (*mod)->name);
Michal Vasko5e32c402022-01-12 14:05:09 +0100714 ret = -1;
Michal Vaskoceae0152018-02-14 16:03:59 +0100715 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200716 } else {
Michal Vaskoceae0152018-02-14 16:03:59 +0100717 /* missing implemented module, load it ... */
Radek Krejci65ef6d52018-08-16 16:35:02 +0200718 clb_data.has_get_schema = has_get_schema;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200719 clb_data.modules = modules;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200720 clb_data.session = session;
721 clb_data.user_clb = user_clb;
722 clb_data.user_data = user_data;
Michal Vaskoceae0152018-02-14 16:03:59 +0100723
724 /* clear all the errors and just collect them for now */
725 ly_err_clean(session->ctx, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200726 ly_log_options(LY_LOSTORE);
Michal Vaskoceae0152018-02-14 16:03:59 +0100727
Radek Krejci65ef6d52018-08-16 16:35:02 +0200728 /* get module data */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200729 retrieve_module_data(name, revision, &clb_data, &format, &module_data, &free_module_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100730
Radek Krejci65ef6d52018-08-16 16:35:02 +0200731 if (module_data) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100732 /* set import callback */
Michal Vasko5ca5d972022-09-14 13:51:31 +0200733 ly_ctx_set_module_imp_clb(session->ctx, retrieve_module_data_imp, &clb_data);
Michal Vaskoceae0152018-02-14 16:03:59 +0100734
Michal Vasko5ca5d972022-09-14 13:51:31 +0200735 /* parse the module */
Michal Vasko5e32c402022-01-12 14:05:09 +0100736 ly_in_new_memory(module_data, &in);
737 lys_parse(session->ctx, in, format, features, mod);
738 ly_in_free(in, 0);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200739 if (*free_module_data) {
Michal Vasko77367452021-02-16 16:32:18 +0100740 (*free_module_data)((char *)module_data, user_data);
Michal Vaskodbf7c272018-02-19 09:07:59 +0100741 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100742
Radek Krejci65ef6d52018-08-16 16:35:02 +0200743 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
744 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100745
Michal Vaskodbf7c272018-02-19 09:07:59 +0100746 /* restore logging options, then print errors on definite failure */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200747 ly_log_options(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskoceae0152018-02-14 16:03:59 +0100748 if (!(*mod)) {
Michal Vasko5e32c402022-01-12 14:05:09 +0100749 for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
Michal Vasko77367452021-02-16 16:32:18 +0100750 ly_err_print(session->ctx, eitem);
Michal Vaskoceae0152018-02-14 16:03:59 +0100751 }
752 ret = -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200753 } else {
754 /* print only warnings */
Michal Vasko5e32c402022-01-12 14:05:09 +0100755 for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
Radek Krejci65ef6d52018-08-16 16:35:02 +0200756 if (eitem->level == LY_LLWRN) {
Michal Vasko77367452021-02-16 16:32:18 +0100757 ly_err_print(session->ctx, eitem);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200758 }
759 }
Michal Vaskoceae0152018-02-14 16:03:59 +0100760 }
761
Michal Vaskodbf7c272018-02-19 09:07:59 +0100762 /* clean the errors */
Michal Vaskoceae0152018-02-14 16:03:59 +0100763 ly_err_clean(session->ctx, NULL);
Michal Vaskoceae0152018-02-14 16:03:59 +0100764 }
765
766 return ret;
767}
768
Radek Krejci65ef6d52018-08-16 16:35:02 +0200769static void
Michal Vasko5ca5d972022-09-14 13:51:31 +0200770free_module_info(struct module_info *list)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200771{
Michal Vasko77367452021-02-16 16:32:18 +0100772 uint32_t u, v;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200773
Radek Krejci65ef6d52018-08-16 16:35:02 +0200774 if (!list) {
775 return;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200776 }
777
Radek Krejci65ef6d52018-08-16 16:35:02 +0200778 for (u = 0; list[u].name; ++u) {
779 free(list[u].name);
780 free(list[u].revision);
Michal Vasko77367452021-02-16 16:32:18 +0100781 if (list[u].features) {
782 for (v = 0; list[u].features[v]; ++v) {
783 free(list[u].features[v]);
784 }
785 free(list[u].features);
Radek Krejci65ef6d52018-08-16 16:35:02 +0200786 }
Radek Krejci1a7efb42018-08-17 11:51:23 +0200787 if (list[u].submodules) {
788 for (v = 0; list[u].submodules[v].name; ++v) {
789 free(list[u].submodules[v].name);
790 free(list[u].submodules[v].revision);
791 }
792 free(list[u].submodules);
793 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200794 }
795 free(list);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200796}
797
Michal Vaskoc088f982021-10-05 12:23:07 +0200798/**
Michal Vasko78939072022-12-12 07:43:18 +0100799 * @brief Retrieve yang-library and schema-mounts operational data from the server.
Michal Vaskoc088f982021-10-05 12:23:07 +0200800 *
801 * @param[in] session NC session.
802 * @param[in] has_get_data Whether get-data RPC is available or only get.
Michal Vasko78939072022-12-12 07:43:18 +0100803 * @param[in] filter Filter to use.
804 * @param[out] oper_data Received data.
Michal Vaskoc088f982021-10-05 12:23:07 +0200805 * @return 0 on success.
806 * @return -1 on error.
807 */
Radek Krejci235d8cb2018-08-17 14:04:32 +0200808static int
Michal Vasko78939072022-12-12 07:43:18 +0100809get_oper_data(struct nc_session *session, int has_get_data, const char *filter, struct lyd_node **oper_data)
Radek Krejcifd5b6682017-06-13 15:52:53 +0200810{
Radek Krejcifd5b6682017-06-13 15:52:53 +0200811 struct nc_rpc *rpc = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100812 struct lyd_node *op = NULL, *envp = NULL;
813 struct lyd_node_any *data;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200814 NC_MSG_TYPE msg;
815 uint64_t msgid;
Michal Vasko303263d2021-10-05 12:18:21 +0200816 int ret = 0;
817 const char *rpc_name;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200818
Michal Vasko78939072022-12-12 07:43:18 +0100819 /* get data from the server */
Michal Vasko303263d2021-10-05 12:18:21 +0200820 if (has_get_data) {
Michal Vasko78939072022-12-12 07:43:18 +0100821 rpc_name = "<get-data>";
822 rpc = nc_rpc_getdata("ietf-datastores:operational", filter, "false", NULL, 0, 0, 0, 0, 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200823 } else {
Michal Vasko78939072022-12-12 07:43:18 +0100824 rpc_name = "<get>";
825 rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
Radek Krejci261737f2018-08-21 09:22:34 +0200826 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200827 if (!rpc) {
828 goto cleanup;
829 }
830
831 while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
832 usleep(1000);
833 }
834 if (msg == NC_MSG_ERROR) {
Michal Vasko78939072022-12-12 07:43:18 +0100835 WRN(session, "Failed to send %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200836 goto cleanup;
837 }
838
839 do {
Michal Vasko77367452021-02-16 16:32:18 +0100840 lyd_free_tree(envp);
841 lyd_free_tree(op);
842
843 msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
Robin Jarry52ddb952019-10-15 16:23:01 +0200844 } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200845 if (msg == NC_MSG_WOULDBLOCK) {
Michal Vasko78939072022-12-12 07:43:18 +0100846 WRN(session, "Timeout for receiving reply to a %s RPC expired.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200847 goto cleanup;
Michal Vasko77367452021-02-16 16:32:18 +0100848 } else if (msg == NC_MSG_ERROR) {
Michal Vasko78939072022-12-12 07:43:18 +0100849 WRN(session, "Failed to receive a reply to %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200850 goto cleanup;
Jan Kundrát1d73a5a2022-03-30 16:54:49 +0200851 } else if (!op || !lyd_child(op) || !lyd_child(op)->schema || strcmp(lyd_child(op)->schema->name, "data")) {
Michal Vasko78939072022-12-12 07:43:18 +0100852 WRN(session, "Unexpected reply without data to a %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200853 goto cleanup;
854 }
855
Michal Vasko77367452021-02-16 16:32:18 +0100856 data = (struct lyd_node_any *)lyd_child(op);
857 if (data->value_type != LYD_ANYDATA_DATATREE) {
Michal Vasko78939072022-12-12 07:43:18 +0100858 WRN(session, "Unexpected data in reply to a %s RPC.", rpc_name);
Michal Vasko40528752021-06-21 15:41:51 +0200859 goto cleanup;
860 } else if (!data->value.tree) {
Michal Vasko78939072022-12-12 07:43:18 +0100861 WRN(session, "No data in reply to a %s RPC.", rpc_name);
Radek Krejcifd5b6682017-06-13 15:52:53 +0200862 goto cleanup;
863 }
864
Michal Vasko78939072022-12-12 07:43:18 +0100865 *oper_data = data->value.tree;
866 data->value.tree = NULL;
867
868cleanup:
869 nc_rpc_free(rpc);
870 lyd_free_tree(envp);
871 lyd_free_tree(op);
872
873 if (session->status != NC_STATUS_RUNNING) {
874 /* something bad happened, discard the session */
875 ERR(session, "Invalid session, discarding.");
876 ret = -1;
877 }
878
879 return ret;
880}
881
882/**
883 * @brief Build server module info from ietf-yang-library data.
884 *
885 * @param[in] session NC session.
886 * @param[in] get_data_sup Whether get-data RPC is available or only get.
887 * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
888 * @param[out] result Server modules.
889 * @return 0 on success.
890 * @return -1 on error.
891 */
892static int
893build_module_info_yl(struct nc_session *session, int get_data_sup, int xpath_sup, struct module_info **result)
894{
895 struct ly_set *modules = NULL;
896 uint32_t u, v, submodules_count, feature_count;
897 struct lyd_node *iter, *child, *oper_data = NULL;
898 struct lys_module *mod;
899 int ret = 0;
900
901 /* get yang-library operational data */
902 if (xpath_sup) {
903 if (get_oper_data(session, get_data_sup, "/ietf-yang-library:*", &oper_data)) {
904 goto cleanup;
905 }
906 } else {
907 if (get_oper_data(session, get_data_sup,
908 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", &oper_data)) {
909 goto cleanup;
910 }
911 }
912 if (!oper_data) {
913 goto cleanup;
914 }
915
916 if (lyd_find_xpath(oper_data, "/ietf-yang-library:modules-state/module", &modules)) {
917 WRN(NULL, "No yang-library module information found.");
Radek Krejci2d088832018-08-21 13:40:14 +0200918 goto cleanup;
Radek Krejciac919522018-08-17 11:00:17 +0200919 }
Radek Krejci65ef6d52018-08-16 16:35:02 +0200920
Michal Vasko77367452021-02-16 16:32:18 +0100921 (*result) = calloc(modules->count + 1, sizeof **result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200922 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +0200923 ERRMEM;
Michal Vasko303263d2021-10-05 12:18:21 +0200924 ret = -1;
Radek Krejcic9390502018-08-17 10:23:13 +0200925 goto cleanup;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200926 }
927
Michal Vasko77367452021-02-16 16:32:18 +0100928 for (u = 0; u < modules->count; ++u) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200929 submodules_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100930 feature_count = 0;
931 mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
932 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
Michal Vasko5d9d5972021-06-09 14:17:01 +0200933 if (!iter->schema || (iter->schema->module != mod)) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200934 /* ignore node from other schemas (augments) */
935 continue;
936 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200937 if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200938 /* ignore empty nodes */
939 continue;
940 }
941 if (!strcmp(iter->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200942 (*result)[u].name = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200943 } else if (!strcmp(iter->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200944 (*result)[u].revision = strdup(lyd_get_value(iter));
Radek Krejcifd5b6682017-06-13 15:52:53 +0200945 } else if (!strcmp(iter->schema->name, "conformance-type")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200946 (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
Radek Krejcifd5b6682017-06-13 15:52:53 +0200947 } else if (!strcmp(iter->schema->name, "feature")) {
Michal Vasko77367452021-02-16 16:32:18 +0100948 (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
949 if (!(*result)[u].features) {
950 ERRMEM;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200951 free_module_info(*result);
Michal Vasko77367452021-02-16 16:32:18 +0100952 *result = NULL;
Michal Vasko303263d2021-10-05 12:18:21 +0200953 ret = -1;
Michal Vasko77367452021-02-16 16:32:18 +0100954 goto cleanup;
955 }
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200956 (*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
Michal Vasko77367452021-02-16 16:32:18 +0100957 (*result)[u].features[feature_count + 1] = NULL;
958 ++feature_count;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200959 } else if (!strcmp(iter->schema->name, "submodule")) {
960 submodules_count++;
961 }
962 }
963
964 if (submodules_count) {
Radek Krejci235d8cb2018-08-17 14:04:32 +0200965 (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
966 if (!(*result)[u].submodules) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200967 ERRMEM;
Michal Vasko5ca5d972022-09-14 13:51:31 +0200968 free_module_info(*result);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200969 *result = NULL;
Michal Vasko303263d2021-10-05 12:18:21 +0200970 ret = -1;
Radek Krejci235d8cb2018-08-17 14:04:32 +0200971 goto cleanup;
Radek Krejci1a7efb42018-08-17 11:51:23 +0200972 } else {
973 v = 0;
Michal Vasko77367452021-02-16 16:32:18 +0100974 LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
975 mod = modules->dnodes[u]->schema->module;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200976 if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
Michal Vasko77367452021-02-16 16:32:18 +0100977 LY_LIST_FOR(lyd_child(iter), child) {
Radek Krejci1a7efb42018-08-17 11:51:23 +0200978 if (mod != child->schema->module) {
979 continue;
980 } else if (!strcmp(child->schema->name, "name")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200981 (*result)[u].submodules[v].name = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200982 } else if (!strcmp(child->schema->name, "revision")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200983 (*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
Radek Krejci1a7efb42018-08-17 11:51:23 +0200984 }
985 }
986 }
987 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200988 }
989 }
990 }
991
Radek Krejcifd5b6682017-06-13 15:52:53 +0200992cleanup:
Michal Vasko78939072022-12-12 07:43:18 +0100993 lyd_free_siblings(oper_data);
Michal Vasko77367452021-02-16 16:32:18 +0100994 ly_set_free(modules, NULL);
Radek Krejci235d8cb2018-08-17 14:04:32 +0200995 return ret;
Radek Krejci65ef6d52018-08-16 16:35:02 +0200996}
997
Michal Vaskoc088f982021-10-05 12:23:07 +0200998/**
Michal Vasko5ca5d972022-09-14 13:51:31 +0200999 * @brief Build server module info from received capabilities.
Michal Vaskoc088f982021-10-05 12:23:07 +02001000 *
1001 * @param[in] cpblts Server capabilities.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001002 * @param[out] result Server modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001003 * @return 0 on success.
1004 * @return -1 on error.
1005 */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001006static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001007build_module_info_cpblts(char **cpblts, struct module_info **result)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001008{
Michal Vasko77367452021-02-16 16:32:18 +01001009 uint32_t u, v, feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001010 char *module_cpblt, *ptr, *ptr2;
1011
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001012 for (u = 0; cpblts[u]; ++u) {}
Radek Krejci235d8cb2018-08-17 14:04:32 +02001013 (*result) = calloc(u + 1, sizeof **result);
1014 if (!(*result)) {
Radek Krejcic9390502018-08-17 10:23:13 +02001015 ERRMEM;
Michal Vasko303263d2021-10-05 12:18:21 +02001016 return -1;
Radek Krejcic9390502018-08-17 10:23:13 +02001017 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001018
1019 for (u = v = 0; cpblts[u]; ++u) {
1020 module_cpblt = strstr(cpblts[u], "module=");
1021 /* this capability requires a module */
1022 if (!module_cpblt) {
1023 continue;
1024 }
1025
1026 /* get module's name */
1027 ptr = (char *)module_cpblt + 7;
1028 ptr2 = strchr(ptr, '&');
1029 if (!ptr2) {
1030 ptr2 = ptr + strlen(ptr);
1031 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001032 (*result)[v].name = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001033
1034 /* get module's revision */
1035 ptr = strstr(module_cpblt, "revision=");
1036 if (ptr) {
1037 ptr += 9;
1038 ptr2 = strchr(ptr, '&');
1039 if (!ptr2) {
1040 ptr2 = ptr + strlen(ptr);
1041 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001042 (*result)[v].revision = strndup(ptr, ptr2 - ptr);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001043 }
1044
1045 /* all are implemented since there is no better information in capabilities list */
Radek Krejci235d8cb2018-08-17 14:04:32 +02001046 (*result)[v].implemented = 1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001047
1048 /* get module's features */
1049 ptr = strstr(module_cpblt, "features=");
1050 if (ptr) {
1051 ptr += 9;
Michal Vasko77367452021-02-16 16:32:18 +01001052 feature_count = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001053 for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) {
1054 if (*ptr == ',') {
Michal Vasko77367452021-02-16 16:32:18 +01001055 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
1056 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
1057 (*result)[v].features[feature_count + 1] = NULL;
1058 ++feature_count;
1059
Radek Krejci65ef6d52018-08-16 16:35:02 +02001060 ptr2 = ptr + 1;
1061 }
1062 }
1063 /* the last one */
Michal Vasko77367452021-02-16 16:32:18 +01001064 (*result)[v].features = nc_realloc((*result)[v].features, (feature_count + 2) * sizeof *(*result)[v].features);
1065 (*result)[v].features[feature_count] = strndup(ptr2, ptr - ptr2);
1066 (*result)[v].features[feature_count + 1] = NULL;
1067 ++feature_count;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001068 }
1069 ++v;
1070 }
Radek Krejci235d8cb2018-08-17 14:04:32 +02001071
Michal Vasko303263d2021-10-05 12:18:21 +02001072 return 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001073}
1074
Michal Vaskoc088f982021-10-05 12:23:07 +02001075/**
Michal Vasko5ca5d972022-09-14 13:51:31 +02001076 * @brief Fill client context based on server modules info.
Michal Vaskoc088f982021-10-05 12:23:07 +02001077 *
1078 * @param[in] session NC session with the context to modify.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001079 * @param[in] modules Server modules info.
1080 * @param[in] user_clb User callback for retrieving specific modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001081 * @param[in] user_data User data for @p user_clb.
1082 * @param[in] has_get_schema Whether server supports get-schema RPC.
1083 * @return 0 on success.
1084 * @return -1 on error.
1085 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001086static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001087nc_ctx_fill(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb, void *user_data,
Michal Vasko77367452021-02-16 16:32:18 +01001088 int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001089{
Michal Vasko303263d2021-10-05 12:18:21 +02001090 int ret = -1;
Michal Vasko77367452021-02-16 16:32:18 +01001091 struct lys_module *mod;
1092 uint32_t u;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001093
1094 for (u = 0; modules[u].name; ++u) {
Michal Vasko488c7f52018-09-19 11:19:44 +02001095 /* skip import-only modules */
1096 if (!modules[u].implemented) {
1097 continue;
1098 }
1099
Radek Krejci65ef6d52018-08-16 16:35:02 +02001100 /* we can continue even if it fails */
Michal Vasko5e32c402022-01-12 14:05:09 +01001101 nc_ctx_load_module(session, modules[u].name, modules[u].revision, (const char **)modules[u].features, modules,
1102 user_clb, user_data, has_get_schema, &mod);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001103
1104 if (!mod) {
1105 if (session->status != NC_STATUS_RUNNING) {
1106 /* something bad heppened, discard the session */
Michal Vasko05532772021-06-03 12:12:38 +02001107 ERR(session, "Invalid session, discarding.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001108 goto cleanup;
1109 }
1110
Michal Vasko5ca5d972022-09-14 13:51:31 +02001111 /* all loading ways failed, the module will be ignored in the received data */
1112 WRN(session, "Failed to load module \"%s@%s\".", modules[u].name, modules[u].revision ?
Michal Vasko05532772021-06-03 12:12:38 +02001113 modules[u].revision : "<latest>");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001114 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001115 }
Michal Vasko60f66602017-10-17 13:52:18 +02001116 }
1117
Michal Vasko77367452021-02-16 16:32:18 +01001118 /* success */
Michal Vasko303263d2021-10-05 12:18:21 +02001119 ret = 0;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001120
1121cleanup:
Radek Krejcifd5b6682017-06-13 15:52:53 +02001122 return ret;
1123}
1124
Michal Vaskoc088f982021-10-05 12:23:07 +02001125/**
Michal Vasko5ca5d972022-09-14 13:51:31 +02001126 * @brief Fill client context with ietf-netconf module.
Michal Vaskoc088f982021-10-05 12:23:07 +02001127 *
1128 * @param[in] session NC session with the context to modify.
Michal Vasko5ca5d972022-09-14 13:51:31 +02001129 * @param[in] modules Server module info.
1130 * @param[in] user_clb User callback for retrieving specific modules.
Michal Vaskoc088f982021-10-05 12:23:07 +02001131 * @param[in] user_data User data for @p user_clb.
1132 * @param[in] has_get_schema Whether server supports get-schema RPC.
1133 * @return 0 on success.
1134 * @return -1 on error.
1135 */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001136static int
Michal Vasko5ca5d972022-09-14 13:51:31 +02001137nc_ctx_fill_ietf_netconf(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb,
Michal Vasko77367452021-02-16 16:32:18 +01001138 void *user_data, int has_get_schema)
Radek Krejci65ef6d52018-08-16 16:35:02 +02001139{
Michal Vasko77367452021-02-16 16:32:18 +01001140 uint32_t u;
Michal Vasko5e32c402022-01-12 14:05:09 +01001141 const char **features = NULL;
1142 struct ly_in *in;
Michal Vasko77367452021-02-16 16:32:18 +01001143 struct lys_module *ietfnc;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001144
Michal Vasko5e32c402022-01-12 14:05:09 +01001145 /* find supported features (capabilities) in ietf-netconf */
1146 for (u = 0; modules[u].name; ++u) {
1147 if (!strcmp(modules[u].name, "ietf-netconf")) {
1148 assert(modules[u].implemented);
1149 features = (const char **)modules[u].features;
1150 break;
1151 }
1152 }
1153 if (!modules[u].name) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001154 ERR(session, "Base NETCONF module not supported by the server.");
Michal Vasko5e32c402022-01-12 14:05:09 +01001155 return -1;
1156 }
1157
Michal Vasko77367452021-02-16 16:32:18 +01001158 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vasko5e32c402022-01-12 14:05:09 +01001159 if (ietfnc) {
1160 /* make sure to enable all the features if already loaded */
1161 lys_set_implemented(ietfnc, features);
1162 } else {
1163 /* load the module */
1164 nc_ctx_load_module(session, "ietf-netconf", NULL, features, modules, user_clb, user_data, has_get_schema, &ietfnc);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001165 if (!ietfnc) {
Michal Vasko5e32c402022-01-12 14:05:09 +01001166 ly_in_new_memory(ietf_netconf_2013_09_29_yang, &in);
1167 lys_parse(session->ctx, in, LYS_IN_YANG, features, &ietfnc);
1168 ly_in_free(in, 0);
Michal Vasko8a4e1462020-05-07 11:32:31 +02001169 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001170 }
1171 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001172 ERR(session, "Loading base NETCONF module failed.");
Michal Vasko303263d2021-10-05 12:18:21 +02001173 return -1;
Radek Krejci65ef6d52018-08-16 16:35:02 +02001174 }
1175
Radek Krejci65ef6d52018-08-16 16:35:02 +02001176 return 0;
1177}
1178
Michal Vasko78939072022-12-12 07:43:18 +01001179/**
1180 * @brief Set client session context to support schema-mount if possible.
1181 *
1182 * @param[in] session NC session with the context to modify.
1183 * @param[in] get_data_sup Whether get-data RPC is available or only get.
1184 * @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
1185 * @return 0 on success.
1186 * @return -1 on error.
1187 */
1188static int
1189nc_ctx_schema_mount(struct nc_session *session, int get_data_sup, int xpath_sup)
1190{
1191 int rc = 0;
1192 struct lyd_node *oper_data = NULL;
1193
1194 if (session->flags & NC_SESSION_SHAREDCTX) {
1195 /* context is already fully set up */
1196 goto cleanup;
1197 }
1198
1199 /* get yang-library and schema-mounts operational data */
1200 if (xpath_sup) {
1201 if ((rc = get_oper_data(session, get_data_sup, "/ietf-yang-library:* | /ietf-yang-schema-mount:*", &oper_data))) {
1202 goto cleanup;
1203 }
1204 } else {
1205 if ((rc = get_oper_data(session, get_data_sup,
1206 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>"
1207 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\"/>", &oper_data))) {
1208 goto cleanup;
1209 }
1210 }
1211
1212 if (!oper_data || lyd_find_path(oper_data, "/ietf-yang-schema-mount:schema-mounts", 0, NULL)) {
1213 /* no schema-mounts operational data */
1214 goto cleanup;
1215 }
1216
1217 /* validate the data for the parent reference prefixes to be resolved */
1218 if (lyd_validate_all(&oper_data, NULL, LYD_VALIDATE_PRESENT, NULL)) {
1219 ERR(session, "Invalid operational data received from the server (%s).", ly_errmsg(LYD_CTX(oper_data)));
1220 rc = -1;
1221 goto cleanup;
1222 }
1223
1224 /* store the data in the session */
1225 session->opts.client.ext_data = oper_data;
1226 oper_data = NULL;
1227
1228cleanup:
1229 lyd_free_siblings(oper_data);
1230 return rc;
1231}
1232
Michal Vasko086311b2016-01-08 09:53:11 +01001233int
1234nc_ctx_check_and_fill(struct nc_session *session)
1235{
Michal Vaskod6c6f852022-12-14 15:37:21 +01001236 int i, get_schema_support = 0, yanglib_support = 0, xpath_support = 0, nmda_support = 0, ret = -1;
Michal Vaskocdeee432017-01-13 13:51:01 +01001237 ly_module_imp_clb old_clb = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001238 void *old_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001239 struct lys_module *mod = NULL;
Radek Krejcib1d250e2018-04-12 13:54:18 +02001240 char *revision;
Michal Vasko5ca5d972022-09-14 13:51:31 +02001241 struct module_info *server_modules = NULL, *sm = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001242
Michal Vasko2e6defd2016-10-07 15:48:15 +02001243 assert(session->opts.client.cpblts && session->ctx);
Michal Vasko086311b2016-01-08 09:53:11 +01001244
Michal Vaskoa272e092023-07-10 14:34:03 +02001245 if (client_opts.auto_context_fill_disabled) {
1246 VRB(session, "Context of the new session is left only with the default YANG modules.");
1247 return 0;
1248 }
1249
Radek Krejci65ef6d52018-08-16 16:35:02 +02001250 /* 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 +02001251 old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001252
Radek Krejci65ef6d52018-08-16 16:35:02 +02001253 /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit
Michal Vasko5ca5d972022-09-14 13:51:31 +02001254 * modules only to those present on the server side */
Michal Vasko77367452021-02-16 16:32:18 +01001255 ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001256
1257 /* our callback is set later with appropriate data */
1258 ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
1259
1260 /* check if get-schema and yang-library is supported */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001261 for (i = 0; session->opts.client.cpblts[i]; ++i) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001262 if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001263 get_schema_support = 1 + i;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001264 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) {
Radek Krejcib1d250e2018-04-12 13:54:18 +02001265 yanglib_support = 1 + i;
Michal Vasko78939072022-12-12 07:43:18 +01001266 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:xpath:1.0", 44)) {
1267 xpath_support = 1 + i;
Michal Vaskod6c6f852022-12-14 15:37:21 +01001268 } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-nmda", 45)) {
1269 nmda_support = 1 + i;
Michal Vasko086311b2016-01-08 09:53:11 +01001270 }
1271 }
Michal Vaskod6c6f852022-12-14 15:37:21 +01001272 VRB(session, "Capability for <get-schema> support%s found.", get_schema_support ? "" : " not");
1273 VRB(session, "Capability for yang-library support%s found.", yanglib_support ? "" : " not");
1274 VRB(session, "Capability for XPath filter support%s found.", xpath_support ? "" : " not");
1275 VRB(session, "Capability for NMDA RPCs support%s found.", nmda_support ? "" : " not");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001276
Michal Vasko5ca5d972022-09-14 13:51:31 +02001277 /* get information about server's modules from capabilities list until we will have yang-library */
1278 if (build_module_info_cpblts(session->opts.client.cpblts, &server_modules) || !server_modules) {
1279 ERR(session, "Unable to get server module information from the <hello>'s capabilities.");
Radek Krejci65ef6d52018-08-16 16:35:02 +02001280 goto cleanup;
1281 }
1282
Michal Vasko086311b2016-01-08 09:53:11 +01001283 /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */
Michal Vasko77367452021-02-16 16:32:18 +01001284 if (get_schema_support && lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_IN_YANG, NULL)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001285 WRN(session, "Loading NETCONF monitoring module failed, cannot use <get-schema>.");
Michal Vasko8a4e1462020-05-07 11:32:31 +02001286 get_schema_support = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001287 }
1288
1289 /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001290 if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001291 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001292 }
1293
Radek Krejci65ef6d52018-08-16 16:35:02 +02001294 /* get correct version of ietf-yang-library into context */
1295 if (yanglib_support) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001296 /* use get-schema to get server's ietf-yang-library */
Radek Krejcib1d250e2018-04-12 13:54:18 +02001297 revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision=");
1298 if (!revision) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001299 WRN(session, "Loading NETCONF ietf-yang-library module failed, missing revision in NETCONF <hello> message.");
Michal Vasko05532772021-06-03 12:12:38 +02001300 WRN(session, "Unable to automatically use <get-schema>.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001301 yanglib_support = 0;
1302 } else {
1303 revision = strndup(&revision[9], 10);
Michal Vasko5e32c402022-01-12 14:05:09 +01001304 if (nc_ctx_load_module(session, "ietf-yang-library", revision, NULL, server_modules, old_clb, old_data,
Michal Vasko0d877f92020-04-02 13:52:43 +02001305 get_schema_support, &mod)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001306 WRN(session, "Loading NETCONF ietf-yang-library module failed, unable to use it to learn all "
Michal Vasko05532772021-06-03 12:12:38 +02001307 "the supported modules.");
Radek Krejcib1d250e2018-04-12 13:54:18 +02001308 yanglib_support = 0;
1309 }
Michal Vasko0d877f92020-04-02 13:52:43 +02001310 if (strcmp(revision, "2019-01-04") >= 0) {
1311 /* we also need ietf-datastores to be implemented */
Michal Vasko5e32c402022-01-12 14:05:09 +01001312 if (nc_ctx_load_module(session, "ietf-datastores", NULL, NULL, server_modules, old_clb, old_data,
Michal Vasko0d877f92020-04-02 13:52:43 +02001313 get_schema_support, &mod)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001314 WRN(session, "Loading NETCONF ietf-datastores module failed, unable to use yang-library "
Michal Vasko05532772021-06-03 12:12:38 +02001315 "to learn all the supported modules.");
Michal Vasko0d877f92020-04-02 13:52:43 +02001316 yanglib_support = 0;
1317 }
1318 }
Radek Krejcib1d250e2018-04-12 13:54:18 +02001319 free(revision);
1320 }
1321 }
1322
Michal Vaskod6c6f852022-12-14 15:37:21 +01001323 /* ietf-netconf-nmda is needed to issue get-data */
1324 if (nmda_support && nc_ctx_load_module(session, "ietf-netconf-nmda", NULL, NULL, server_modules, old_clb, old_data,
1325 get_schema_support, &mod)) {
1326 WRN(session, "Loading NMDA module failed, unable to use <get-data>.");
1327 nmda_support = 0;
1328 }
1329
Michal Vasko5ca5d972022-09-14 13:51:31 +02001330 /* prepare structured information about server's modules */
Radek Krejci9aa1e352018-05-16 16:05:42 +02001331 if (yanglib_support) {
Michal Vaskod6c6f852022-12-14 15:37:21 +01001332 if (build_module_info_yl(session, nmda_support, xpath_support, &sm)) {
Radek Krejcif0633792018-08-20 15:12:09 +02001333 goto cleanup;
1334 } else if (!sm) {
Michal Vasko05532772021-06-03 12:12:38 +02001335 VRB(session, "Trying to use capabilities instead of ietf-yang-library data.");
Radek Krejcif0633792018-08-20 15:12:09 +02001336 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001337 /* prefer yang-library information, currently we have it from capabilities used for getting correct
Michal Vasko5ca5d972022-09-14 13:51:31 +02001338 * yang-library module */
1339 free_module_info(server_modules);
Radek Krejcif0633792018-08-20 15:12:09 +02001340 server_modules = sm;
Radek Krejci235d8cb2018-08-17 14:04:32 +02001341 }
Radek Krejci65ef6d52018-08-16 16:35:02 +02001342 }
Michal Vaskoef578332016-01-25 13:20:09 +01001343
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001344 /* compile all modules at once to avoid invalid errors or warnings */
1345 ly_ctx_set_options(session->ctx, LY_CTX_EXPLICIT_COMPILE);
1346
1347 /* fill the context */
Radek Krejci65ef6d52018-08-16 16:35:02 +02001348 if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) {
1349 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001350 }
1351
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001352 /* compile it */
1353 if (ly_ctx_compile(session->ctx)) {
1354 goto cleanup;
1355 }
1356
Michal Vasko78939072022-12-12 07:43:18 +01001357 /* set support for schema-mount, if possible */
Michal Vaskod6c6f852022-12-14 15:37:21 +01001358 if (nc_ctx_schema_mount(session, nmda_support, xpath_support)) {
Michal Vasko78939072022-12-12 07:43:18 +01001359 goto cleanup;
1360 }
1361
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001362 /* success */
Radek Krejcifd5b6682017-06-13 15:52:53 +02001363 ret = 0;
1364
Michal Vaskoeee99412016-11-21 10:19:43 +01001365 if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001366 WRN(session, "Some modules failed to be loaded, any data from these modules (and any other unknown) will "
Michal Vasko05532772021-06-03 12:12:38 +02001367 "be ignored.");
Michal Vaskoef578332016-01-25 13:20:09 +01001368 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001369
1370cleanup:
Michal Vasko5ca5d972022-09-14 13:51:31 +02001371 free_module_info(server_modules);
Radek Krejci65ef6d52018-08-16 16:35:02 +02001372
Radek Krejcifd5b6682017-06-13 15:52:53 +02001373 /* set user callback back */
1374 ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data);
Michal Vasko77367452021-02-16 16:32:18 +01001375 ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS);
Michal Vaskoa9e9d452022-05-04 15:48:25 +02001376 ly_ctx_unset_options(session->ctx, LY_CTX_EXPLICIT_COMPILE);
Radek Krejcifd5b6682017-06-13 15:52:53 +02001377
Michal Vaskoef578332016-01-25 13:20:09 +01001378 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001379}
1380
1381API struct nc_session *
1382nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx)
1383{
Michal Vaskod083db62016-01-19 10:31:29 +01001384 struct nc_session *session;
Michal Vasko086311b2016-01-08 09:53:11 +01001385
Michal Vasko45e53ae2016-04-07 11:46:03 +02001386 if (fdin < 0) {
roman40672412023-05-04 11:10:22 +02001387 ERRARG(NULL, "fdin");
Michal Vasko45e53ae2016-04-07 11:46:03 +02001388 return NULL;
1389 } else if (fdout < 0) {
roman40672412023-05-04 11:10:22 +02001390 ERRARG(NULL, "fdout");
Michal Vasko086311b2016-01-08 09:53:11 +01001391 return NULL;
1392 }
1393
1394 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001395 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001396 if (!session) {
1397 ERRMEM;
1398 return NULL;
1399 }
1400 session->status = NC_STATUS_STARTING;
Michal Vasko086311b2016-01-08 09:53:11 +01001401
1402 /* transport specific data */
1403 session->ti_type = NC_TI_FD;
1404 session->ti.fd.in = fdin;
1405 session->ti.fd.out = fdout;
1406
Michal Vasko78939072022-12-12 07:43:18 +01001407 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Robin Jarry4e38e292019-10-15 17:14:14 +02001408 goto fail;
Michal Vasko086311b2016-01-08 09:53:11 +01001409 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001410 ctx = session->ctx;
Michal Vasko086311b2016-01-08 09:53:11 +01001411
1412 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001413 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko086311b2016-01-08 09:53:11 +01001414 goto fail;
1415 }
1416 session->status = NC_STATUS_RUNNING;
1417
Michal Vaskoef578332016-01-25 13:20:09 +01001418 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko086311b2016-01-08 09:53:11 +01001419 goto fail;
1420 }
1421
1422 return session;
1423
1424fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001425 nc_session_free(session, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001426 return NULL;
1427}
1428
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001429API struct nc_session *
1430nc_connect_unix(const char *address, struct ly_ctx *ctx)
1431{
1432 struct nc_session *session = NULL;
1433 struct sockaddr_un sun;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001434 struct passwd *pw, pw_buf;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001435 char *username;
1436 int sock = -1;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001437 char *buf = NULL;
1438 size_t buf_size = 0;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001439
roman40672412023-05-04 11:10:22 +02001440 NC_CHECK_ARG_RET(NULL, address, NULL);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001441
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001442 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1443 if (sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001444 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001445 goto fail;
1446 }
1447
1448 memset(&sun, 0, sizeof(sun));
1449 sun.sun_family = AF_UNIX;
1450 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
1451
1452 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001453 ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001454 goto fail;
1455 }
1456
1457 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001458 ERR(NULL, "fcntl failed (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001459 goto fail;
1460 }
1461
1462 /* prepare session structure */
1463 session = nc_new_session(NC_CLIENT, 0);
1464 if (!session) {
1465 ERRMEM;
1466 goto fail;
1467 }
1468 session->status = NC_STATUS_STARTING;
1469
1470 /* transport specific data */
1471 session->ti_type = NC_TI_UNIX;
1472 session->ti.unixsock.sock = sock;
1473 sock = -1; /* do not close sock in fail label anymore */
1474
Michal Vasko78939072022-12-12 07:43:18 +01001475 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Robin Jarry4e38e292019-10-15 17:14:14 +02001476 goto fail;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001477 }
Robin Jarry4e38e292019-10-15 17:14:14 +02001478 ctx = session->ctx;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001479
Michal Vasko93224072021-11-09 12:14:28 +01001480 session->path = strdup(address);
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001481
romanf6e32012023-04-24 15:51:26 +02001482 pw = nc_getpw(geteuid(), NULL, &pw_buf, &buf, &buf_size);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02001483 if (!pw) {
1484 ERR(NULL, "Failed to find username for UID %u.", (unsigned int)geteuid());
1485 goto fail;
1486 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001487 username = strdup(pw->pw_name);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02001488 free(buf);
1489 if (!username) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001490 ERRMEM;
1491 goto fail;
1492 }
Michal Vasko93224072021-11-09 12:14:28 +01001493 session->username = username;
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001494
1495 /* NETCONF handshake */
1496 if (nc_handshake_io(session) != NC_MSG_HELLO) {
1497 goto fail;
1498 }
1499 session->status = NC_STATUS_RUNNING;
1500
1501 if (nc_ctx_check_and_fill(session) == -1) {
1502 goto fail;
1503 }
1504
1505 return session;
1506
1507fail:
1508 nc_session_free(session, NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001509 if (sock >= 0) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001510 close(sock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001511 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001512 return NULL;
1513}
1514
Michal Vasko056f53c2022-10-21 13:38:15 +02001515/**
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001516 * @brief Convert socket IP address to string.
1517 *
1518 * @param[in] saddr Sockaddr to convert.
1519 * @param[out] str_ip String IP address.
Michal Vaskoc07d9762023-03-27 10:32:18 +02001520 * @param[out] port Optional port.
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001521 * @return 0 on success.
1522 * @return -1 on error.
1523 */
1524static int
Michal Vaskoc07d9762023-03-27 10:32:18 +02001525nc_saddr2str(const struct sockaddr *saddr, char **str_ip, uint16_t *port)
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001526{
1527 void *addr;
1528 socklen_t str_len;
1529
1530 assert((saddr->sa_family == AF_INET) || (saddr->sa_family == AF_INET6));
1531
1532 str_len = (saddr->sa_family == AF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
1533 *str_ip = malloc(str_len);
1534 if (!*str_ip) {
1535 ERRMEM;
1536 return -1;
1537 }
1538
1539 if (saddr->sa_family == AF_INET) {
Michal Vaskoc0f85ca2023-03-27 10:19:00 +02001540 addr = &((struct sockaddr_in *)saddr)->sin_addr;
Michal Vaskoc07d9762023-03-27 10:32:18 +02001541 if (port) {
1542 *port = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1543 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001544 } else {
Michal Vaskoc0f85ca2023-03-27 10:19:00 +02001545 addr = &((struct sockaddr_in6 *)saddr)->sin6_addr;
Michal Vaskoc07d9762023-03-27 10:32:18 +02001546 if (port) {
1547 *port = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1548 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001549 }
1550 if (!inet_ntop(saddr->sa_family, addr, *str_ip, str_len)) {
1551 ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno));
1552 free(*str_ip);
1553 return -1;
1554 }
1555
1556 return 0;
1557}
1558
1559/**
Michal Vasko056f53c2022-10-21 13:38:15 +02001560 * @brief Try to connect a socket, optionally a pending one from a previous attempt.
1561 *
1562 * @param[in] timeout_ms Timeout in ms to wait for the connection to be fully established, -1 to block.
1563 * @param[in,out] sock_pending Optional previously created socked that was not fully connected yet. If provided and
1564 * connected, is set to -1.
1565 * @param[in] res Addrinfo resource to use when creating a new socket.
1566 * @param[in] ka Keepalives to set.
1567 * @return Connected socket or -1 on error.
Frank Rimpler9f838b02018-07-25 06:44:03 +00001568 */
1569static int
Michal Vasko056f53c2022-10-21 13:38:15 +02001570sock_connect(int timeout_ms, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +01001571{
Michal Vasko5e8d0192019-06-24 19:19:49 +02001572 int flags, ret, error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001573 int sock = -1;
Michal Vaskob9336672023-10-11 09:27:30 +02001574 struct pollfd fds = {0};
Frank Rimpler9f838b02018-07-25 06:44:03 +00001575 socklen_t len = sizeof(int);
Michal Vasko7e7f99d2019-09-12 13:49:46 +02001576 uint16_t port;
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001577 char *str;
Michal Vasko086311b2016-01-08 09:53:11 +01001578
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001579 if (sock_pending && (*sock_pending != -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001580 VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001581 sock = *sock_pending;
1582 } else {
1583 assert(res);
Michal Vaskoc07d9762023-03-27 10:32:18 +02001584 if (nc_saddr2str(res->ai_addr, &str, &port)) {
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001585 return -1;
1586 }
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001587 VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port);
1588 free(str);
Michal Vasko5e8d0192019-06-24 19:19:49 +02001589
1590 /* connect to a server */
Michal Vasko086311b2016-01-08 09:53:11 +01001591 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1592 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001593 ERR(NULL, "Socket could not be created (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001594 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001595 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001596 /* make the socket non-blocking */
1597 if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +02001598 ERR(NULL, "fcntl() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001599 goto cleanup;
Michal Vasko7d965dc2018-03-12 14:39:23 +01001600 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001601 /* non-blocking connect! */
1602 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
1603 if (errno != EINPROGRESS) {
1604 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001605 ERR(NULL, "connect() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001606 goto cleanup;
1607 }
1608 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001609 }
Michal Vasko7d965dc2018-03-12 14:39:23 +01001610
Michal Vaskob9336672023-10-11 09:27:30 +02001611 fds.fd = sock;
1612 fds.events = POLLOUT;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001613
Michal Vaskob9336672023-10-11 09:27:30 +02001614 /* wait until we can write data to the socket */
1615 ret = poll(&fds, 1, timeout_ms);
1616 if (ret == -1) {
1617 /* error */
1618 ERR(NULL, "poll() failed (%s).", strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001619 goto cleanup;
Michal Vaskob9336672023-10-11 09:27:30 +02001620 } else if (ret == 0) {
Michal Vasko5e8d0192019-06-24 19:19:49 +02001621 /* there was a timeout */
Michal Vasko056f53c2022-10-21 13:38:15 +02001622 VRB(NULL, "Timed out after %d ms (%s).", timeout_ms, strerror(errno));
Frank Rimpler9f838b02018-07-25 06:44:03 +00001623 if (sock_pending) {
1624 /* no sock-close, we'll try it again */
1625 *sock_pending = sock;
1626 } else {
1627 close(sock);
1628 }
1629 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001630 }
Radek Krejci782041a2018-08-20 10:09:45 +02001631
1632 /* check the usability of the socket */
Michal Vasko5e8d0192019-06-24 19:19:49 +02001633 error = 0;
Radek Krejci782041a2018-08-20 10:09:45 +02001634 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001635 ERR(NULL, "getsockopt() failed (%s).", strerror(errno));
Radek Krejci782041a2018-08-20 10:09:45 +02001636 goto cleanup;
1637 }
Michal Vaskoe27ab4e2020-07-27 11:10:58 +02001638 if (error) {
Radek Krejci782041a2018-08-20 10:09:45 +02001639 /* network connection failed, try another resource */
Michal Vasko05532772021-06-03 12:12:38 +02001640 VRB(NULL, "getsockopt() error (%s).", strerror(error));
Radek Krejci782041a2018-08-20 10:09:45 +02001641 errno = error;
1642 goto cleanup;
1643 }
Michal Vaskobe52dc22018-10-17 09:28:17 +02001644
1645 /* enable keep-alive */
romanc1d2b092023-02-02 08:58:27 +01001646 if (nc_sock_configure_keepalive(sock, ka)) {
Michal Vaskobe52dc22018-10-17 09:28:17 +02001647 goto cleanup;
1648 }
1649
Michal Vasko056f53c2022-10-21 13:38:15 +02001650 /* connected */
1651 if (sock_pending) {
1652 *sock_pending = -1;
1653 }
Michal Vasko086311b2016-01-08 09:53:11 +01001654 return sock;
Michal Vasko06c860d2018-07-09 16:08:52 +02001655
Frank Rimpler9f838b02018-07-25 06:44:03 +00001656cleanup:
1657 if (sock_pending) {
1658 *sock_pending = -1;
Michal Vasko06c860d2018-07-09 16:08:52 +02001659 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001660 close(sock);
Michal Vasko06c860d2018-07-09 16:08:52 +02001661 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001662}
1663
Frank Rimpler9f838b02018-07-25 06:44:03 +00001664int
Michal Vasko056f53c2022-10-21 13:38:15 +02001665nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepalives *ka, int *sock_pending, char **ip_host)
Frank Rimpler9f838b02018-07-25 06:44:03 +00001666{
Michal Vasko83ad17e2019-01-30 10:11:37 +01001667 int i, opt;
Michal Vasko66032bc2019-01-22 15:03:12 +01001668 int sock = sock_pending ? *sock_pending : -1;
Michal Vasko0be85692021-03-02 08:04:57 +01001669 struct addrinfo hints, *res_list = NULL, *res;
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001670 char port_s[6]; /* length of string representation of short int */
1671 struct sockaddr_storage saddr;
1672 socklen_t addr_len = sizeof saddr;
1673
1674 *ip_host = NULL;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001675
Michal Vasko056f53c2022-10-21 13:38:15 +02001676 DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout_ms, sock);
Frank Rimpler9f838b02018-07-25 06:44:03 +00001677
1678 /* no pending socket */
1679 if (sock == -1) {
Michal Vasko66032bc2019-01-22 15:03:12 +01001680 /* connect to a server */
Frank Rimpler9f838b02018-07-25 06:44:03 +00001681 snprintf(port_s, 6, "%u", port);
1682 memset(&hints, 0, sizeof hints);
1683 hints.ai_family = AF_UNSPEC;
1684 hints.ai_socktype = SOCK_STREAM;
1685 hints.ai_protocol = IPPROTO_TCP;
1686 i = getaddrinfo(host, port_s, &hints, &res_list);
1687 if (i != 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001688 ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i));
Michal Vaskocb846632019-12-13 15:12:45 +01001689 goto error;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001690 }
1691
1692 for (res = res_list; res != NULL; res = res->ai_next) {
Michal Vasko056f53c2022-10-21 13:38:15 +02001693 sock = sock_connect(timeout_ms, sock_pending, res, ka);
Michal Vasko2af7c382020-07-27 14:21:35 +02001694 if (sock == -1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001695 if (!sock_pending || (*sock_pending == -1)) {
Michal Vasko2af7c382020-07-27 14:21:35 +02001696 /* try the next resource */
1697 continue;
1698 } else {
1699 /* timeout, keep pending socket */
Michal Vasko0be85692021-03-02 08:04:57 +01001700 break;
Michal Vasko2af7c382020-07-27 14:21:35 +02001701 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001702 }
Michal Vasko05532772021-06-03 12:12:38 +02001703 VRB(NULL, "Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4");
Michal Vasko83ad17e2019-01-30 10:11:37 +01001704
1705 opt = 1;
1706 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001707 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vaskocb846632019-12-13 15:12:45 +01001708 goto error;
Michal Vasko83ad17e2019-01-30 10:11:37 +01001709 }
1710
Michal Vaskoc07d9762023-03-27 10:32:18 +02001711 if (nc_saddr2str(res->ai_addr, ip_host, NULL)) {
Michal Vasko7b1c4c22023-03-24 13:27:40 +01001712 goto error;
Michal Vasko66032bc2019-01-22 15:03:12 +01001713 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001714 break;
1715 }
1716 freeaddrinfo(res_list);
1717
1718 } else {
1719 /* try to get a connection with the pending socket */
1720 assert(sock_pending);
Michal Vasko056f53c2022-10-21 13:38:15 +02001721 sock = sock_connect(timeout_ms, sock_pending, NULL, ka);
Tie.Liao9bca73d2023-03-23 17:25:00 +01001722
1723 if (sock > 0) {
Barbaros Tokaoglu96da5912023-06-19 18:57:05 +03001724 if (getpeername(sock, (struct sockaddr *)&saddr, &addr_len)) {
1725 ERR(NULL, "getpeername failed (%s).", strerror(errno));
Tie.Liao9bca73d2023-03-23 17:25:00 +01001726 goto error;
1727 }
1728
Michal Vaskoc07d9762023-03-27 10:32:18 +02001729 if (nc_saddr2str((struct sockaddr *)&saddr, ip_host, NULL)) {
Tie.Liao9bca73d2023-03-23 17:25:00 +01001730 goto error;
1731 }
Tie.Liao9bca73d2023-03-23 17:25:00 +01001732 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00001733 }
1734
1735 return sock;
Michal Vaskocb846632019-12-13 15:12:45 +01001736
1737error:
Michal Vasko0be85692021-03-02 08:04:57 +01001738 if (res_list) {
1739 freeaddrinfo(res_list);
1740 }
Michal Vaskocb846632019-12-13 15:12:45 +01001741 if (sock != -1) {
1742 close(sock);
1743 }
1744 if (sock_pending) {
1745 *sock_pending = -1;
1746 }
1747 return -1;
Frank Rimpler9f838b02018-07-25 06:44:03 +00001748}
1749
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001750#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko3d865d22016-01-28 16:00:53 +01001751
Michal Vasko3031aae2016-01-27 16:07:18 +01001752int
Michal Vasko9d4cca52022-09-07 11:19:57 +02001753nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hostname, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +01001754{
1755 int sock;
1756
roman40672412023-05-04 11:10:22 +02001757 NC_CHECK_ARG_RET(NULL, address, port, -1);
Michal Vasko3031aae2016-01-27 16:07:18 +01001758
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001759 sock = nc_sock_listen_inet(address, port, &client_opts.ka);
Michal Vasko3031aae2016-01-27 16:07:18 +01001760 if (sock == -1) {
1761 return -1;
1762 }
1763
1764 ++client_opts.ch_bind_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001765 client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds);
1766 if (!client_opts.ch_binds) {
1767 ERRMEM;
Michal Vasko0f74da52016-03-03 08:52:52 +01001768 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001769 return -1;
1770 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001771
Michal Vasko9d4cca52022-09-07 11:19:57 +02001772 client_opts.ch_binds_aux = nc_realloc(client_opts.ch_binds_aux, client_opts.ch_bind_count * sizeof *client_opts.ch_binds_aux);
1773 if (!client_opts.ch_binds_aux) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001774 ERRMEM;
1775 close(sock);
1776 return -1;
1777 }
Michal Vasko9d4cca52022-09-07 11:19:57 +02001778 client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].ti = ti;
1779 client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].hostname = hostname ? strdup(hostname) : NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001780
Michal Vasko3031aae2016-01-27 16:07:18 +01001781 client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address);
1782 client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port;
1783 client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001784 client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001785
1786 return 0;
1787}
1788
1789int
1790nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1791{
1792 uint32_t i;
1793 int ret = -1;
1794
1795 if (!address && !port && !ti) {
1796 for (i = 0; i < client_opts.ch_bind_count; ++i) {
1797 close(client_opts.ch_binds[i].sock);
Michal Vasko9d4cca52022-09-07 11:19:57 +02001798 free(client_opts.ch_binds[i].address);
1799
1800 free(client_opts.ch_binds_aux[i].hostname);
Michal Vasko3031aae2016-01-27 16:07:18 +01001801
1802 ret = 0;
1803 }
Michal Vasko9d4cca52022-09-07 11:19:57 +02001804 client_opts.ch_bind_count = 0;
1805
Michal Vasko3031aae2016-01-27 16:07:18 +01001806 free(client_opts.ch_binds);
1807 client_opts.ch_binds = NULL;
Michal Vasko9d4cca52022-09-07 11:19:57 +02001808
1809 free(client_opts.ch_binds_aux);
1810 client_opts.ch_binds_aux = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001811 } else {
1812 for (i = 0; i < client_opts.ch_bind_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001813 if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) &&
1814 (!port || (client_opts.ch_binds[i].port == port)) &&
Michal Vasko9d4cca52022-09-07 11:19:57 +02001815 (!ti || (client_opts.ch_binds_aux[i].ti == ti))) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001816 close(client_opts.ch_binds[i].sock);
Michal Vasko9d4cca52022-09-07 11:19:57 +02001817 free(client_opts.ch_binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001818
1819 --client_opts.ch_bind_count;
Michal Vasko66c762a2016-10-13 10:34:14 +02001820 if (!client_opts.ch_bind_count) {
1821 free(client_opts.ch_binds);
1822 client_opts.ch_binds = NULL;
Michal Vasko9d4cca52022-09-07 11:19:57 +02001823
1824 free(client_opts.ch_binds_aux);
1825 client_opts.ch_binds_aux = NULL;
Michal Vasko66c762a2016-10-13 10:34:14 +02001826 } else if (i < client_opts.ch_bind_count) {
Michal Vasko9d4cca52022-09-07 11:19:57 +02001827 memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count],
1828 sizeof *client_opts.ch_binds);
1829
1830 memcpy(&client_opts.ch_binds_aux[i], &client_opts.ch_binds_aux[client_opts.ch_bind_count],
1831 sizeof *client_opts.ch_binds_aux);
Michal Vasko66c762a2016-10-13 10:34:14 +02001832 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001833
1834 ret = 0;
1835 }
1836 }
1837 }
1838
1839 return ret;
1840}
1841
1842API int
1843nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
1844{
1845 int sock;
1846 char *host = NULL;
1847 uint16_t port, idx;
1848
roman40672412023-05-04 11:10:22 +02001849 NC_CHECK_ARG_RET(NULL, session, -1);
1850
Michal Vasko45e53ae2016-04-07 11:46:03 +02001851 if (!client_opts.ch_binds) {
1852 ERRINIT;
1853 return -1;
Michal Vasko3031aae2016-01-27 16:07:18 +01001854 }
1855
1856 sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001857 if (sock < 1) {
Michal Vaskob737d752016-02-09 09:01:27 +01001858 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001859 return sock;
1860 }
1861
Radek Krejci53691be2016-02-22 13:58:37 +01001862#ifdef NC_ENABLED_SSH
Michal Vasko9d4cca52022-09-07 11:19:57 +02001863 if (client_opts.ch_binds_aux[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001864 *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT);
Michal Vasko3d865d22016-01-28 16:00:53 +01001865 } else
1866#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001867#ifdef NC_ENABLED_TLS
Michal Vasko9d4cca52022-09-07 11:19:57 +02001868 if (client_opts.ch_binds_aux[idx].ti == NC_TI_OPENSSL) {
1869 *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT,
1870 client_opts.ch_binds_aux[idx].hostname);
Michal Vasko3d865d22016-01-28 16:00:53 +01001871 } else
1872#endif
1873 {
Michal Vaskofee717c2016-02-01 13:25:43 +01001874 close(sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001875 *session = NULL;
1876 }
1877
1878 free(host);
1879
1880 if (!(*session)) {
1881 return -1;
1882 }
1883
1884 return 1;
1885}
1886
Radek Krejci53691be2016-02-22 13:58:37 +01001887#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko3d865d22016-01-28 16:00:53 +01001888
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001889API const char * const *
Michal Vaskobdfb5242016-05-24 09:11:01 +02001890nc_session_get_cpblts(const struct nc_session *session)
1891{
roman40672412023-05-04 11:10:22 +02001892 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vaskobdfb5242016-05-24 09:11:01 +02001893
Michal Vasko1b2ddc92017-05-24 08:59:49 +02001894 return (const char * const *)session->opts.client.cpblts;
Michal Vaskobdfb5242016-05-24 09:11:01 +02001895}
1896
1897API const char *
1898nc_session_cpblt(const struct nc_session *session, const char *capab)
1899{
1900 int i, len;
1901
roman40672412023-05-04 11:10:22 +02001902 NC_CHECK_ARG_RET(session, session, capab, NULL);
Michal Vaskobdfb5242016-05-24 09:11:01 +02001903
1904 len = strlen(capab);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001905 for (i = 0; session->opts.client.cpblts[i]; ++i) {
1906 if (!strncmp(session->opts.client.cpblts[i], capab, len)) {
1907 return session->opts.client.cpblts[i];
Michal Vaskobdfb5242016-05-24 09:11:01 +02001908 }
1909 }
1910
1911 return NULL;
1912}
1913
Michal Vasko9cd26a82016-05-31 08:58:48 +02001914API int
1915nc_session_ntf_thread_running(const struct nc_session *session)
1916{
roman40672412023-05-04 11:10:22 +02001917 NC_CHECK_ARG_RET(session, session, 0);
1918
1919 if (session->side != NC_CLIENT) {
1920 ERRARG(NULL, "session");
Michal Vasko9cd26a82016-05-31 08:58:48 +02001921 return 0;
1922 }
1923
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02001924 return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running);
Michal Vasko9cd26a82016-05-31 08:58:48 +02001925}
1926
Michal Vaskob7558c52016-02-26 15:04:19 +01001927API void
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001928nc_client_init(void)
1929{
1930 nc_init();
1931}
1932
1933API void
Michal Vaskob7558c52016-02-26 15:04:19 +01001934nc_client_destroy(void)
1935{
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001936 nc_client_set_schema_searchpath(NULL);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001937#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Radek Krejci5cebc6b2017-05-26 13:24:38 +02001938 nc_client_ch_del_bind(NULL, 0, 0);
1939#endif
1940#ifdef NC_ENABLED_SSH
1941 nc_client_ssh_destroy_opts();
1942#endif
1943#ifdef NC_ENABLED_TLS
1944 nc_client_tls_destroy_opts();
1945#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001946 nc_destroy();
Michal Vaskob7558c52016-02-26 15:04:19 +01001947}
1948
Michal Vasko77367452021-02-16 16:32:18 +01001949static NC_MSG_TYPE
1950recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01001951{
Michal Vasko77367452021-02-16 16:32:18 +01001952 char *ptr;
1953 struct lyd_attr *attr;
1954 uint64_t cur_msgid;
1955
1956 assert(envp && !envp->schema);
1957
1958 /* find the message-id attribute */
1959 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1960 if (!strcmp(attr->name.name, "message-id")) {
1961 break;
1962 }
1963 }
1964
1965 if (!attr) {
Michal Vasko05532772021-06-03 12:12:38 +02001966 ERR(session, "Received a <rpc-reply> without a message-id.");
Michal Vasko77367452021-02-16 16:32:18 +01001967 return NC_MSG_REPLY_ERR_MSGID;
1968 }
1969
1970 cur_msgid = strtoul(attr->value, &ptr, 10);
1971 if (cur_msgid != msgid) {
Michal Vasko05532772021-06-03 12:12:38 +02001972 ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").",
1973 cur_msgid, msgid);
Michal Vasko77367452021-02-16 16:32:18 +01001974 return NC_MSG_REPLY_ERR_MSGID;
1975 }
1976
1977 return NC_MSG_REPLY;
1978}
1979
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001980/**
1981 * @brief Used to roughly estimate the type of the message, does not actually parse or verify it.
1982 *
1983 * @param[in] session NETCONF session used to send error messages.
1984 * @param[in] msg Message to check for type.
1985 * @return NC_MSG_REPLY If format roughly matches a rpc-reply;
1986 * @return NC_MSG_NOTIF If format roughly matches a notification;
1987 * @return NC_MSG_ERROR If format is malformed or unrecognized.
1988 */
1989static NC_MSG_TYPE
1990get_msg_type(struct nc_session *session, struct ly_in *msg)
1991{
1992 const char *str, *end;
1993
1994 str = ly_in_memory(msg, NULL);
1995
1996 while (*str) {
1997 /* Skip whitespaces */
1998 while (isspace(*str)) {
1999 str++;
2000 }
2001
2002 if (*str == '<') {
2003 str++;
2004 if (!strncmp(str, "!--", 3)) {
2005 /* Skip comments */
2006 end = "-->";
2007 str = strstr(str, end);
2008 } else if (!strncmp(str, "?xml", 4)) {
2009 /* Skip xml declaration */
2010 end = "?>";
2011 str = strstr(str, end);
2012 } else if (!strncmp(str, "rpc-reply", 9)) {
2013 return NC_MSG_REPLY;
2014 } else if (!strncmp(str, "notification", 12)) {
2015 return NC_MSG_NOTIF;
2016 } else {
2017 ERR(session, "Unknown xml element '%.10s'.", str);
2018 return NC_MSG_ERROR;
2019 }
2020 if (!str) {
2021 /* No matching ending tag found */
2022 ERR(session, "No matching ending tag '%s' found in xml message.", end);
2023 return NC_MSG_ERROR;
2024 }
2025 str += strlen(end);
2026 } else {
2027 /* Not a valid xml */
2028 ERR(session, "Unexpected character '%c' in xml message.", *str);
2029 return NC_MSG_ERROR;
2030 }
2031 }
2032
2033 /* Unexpected end of message */
2034 ERR(session, "Unexpected end of xml message.");
2035 return NC_MSG_ERROR;
2036}
2037
2038/**
2039 * @brief Function to receive either replies or notifications.
2040 *
2041 * @param[in] session NETCONF session from which this function receives messages.
2042 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite.
2043 * @param[in] expected Type of the message the caller desired.
2044 * @param[out] message If receiving a message succeeded this is the message, NULL otherwise.
2045 * @return NC_MSG_REPLY If a rpc-reply was received;
2046 * @return NC_MSG_NOTIF If a notification was received;
Michal Vaskofdba4a32022-01-05 12:13:53 +01002047 * @return NC_MSG_ERROR If any error occurred;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002048 * @return NC_MSG_WOULDBLOCK If the timeout was reached.
2049 */
2050static NC_MSG_TYPE
2051recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message)
2052{
2053 struct nc_msg_cont **cont_ptr;
2054 struct ly_in *msg = NULL;
2055 struct nc_msg_cont *cont, *prev;
2056 NC_MSG_TYPE ret = NC_MSG_ERROR;
2057 int r;
2058
2059 *message = NULL;
2060
2061 /* MSGS LOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02002062 r = nc_session_client_msgs_lock(session, &timeout, __func__);
2063 if (!r) {
2064 ret = NC_MSG_WOULDBLOCK;
2065 goto cleanup;
2066 } else if (r == -1) {
2067 ret = NC_MSG_ERROR;
2068 goto cleanup;
2069 }
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002070
2071 /* Find the expected message in the buffer */
2072 prev = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02002073 for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) {
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002074 prev = cont;
2075 }
2076
2077 if (cont) {
2078 /* Remove found message from buffer */
2079 if (prev) {
2080 prev->next = cont->next;
2081 } else {
2082 session->opts.client.msgs = cont->next;
2083 }
2084
2085 /* Use the buffer message */
2086 ret = cont->type;
2087 msg = cont->msg;
2088 free(cont);
Michal Vasko01130bd2021-08-26 11:47:38 +02002089 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002090 }
2091
2092 /* Read a message from the wire */
2093 r = nc_read_msg_poll_io(session, timeout, &msg);
2094 if (!r) {
2095 ret = NC_MSG_WOULDBLOCK;
Michal Vasko01130bd2021-08-26 11:47:38 +02002096 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002097 } else if (r == -1) {
2098 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02002099 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002100 }
2101
2102 /* Basic check to determine message type */
2103 ret = get_msg_type(session, msg);
2104 if (ret == NC_MSG_ERROR) {
Michal Vasko01130bd2021-08-26 11:47:38 +02002105 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002106 }
2107
2108 /* If received a message of different type store it in the buffer */
2109 if (ret != expected) {
2110 cont_ptr = &session->opts.client.msgs;
2111 while (*cont_ptr) {
2112 cont_ptr = &((*cont_ptr)->next);
2113 }
2114 *cont_ptr = malloc(sizeof **cont_ptr);
2115 if (!*cont_ptr) {
2116 ERRMEM;
2117 ret = NC_MSG_ERROR;
Michal Vasko01130bd2021-08-26 11:47:38 +02002118 goto cleanup_unlock;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002119 }
2120 (*cont_ptr)->msg = msg;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002121 msg = NULL;
Michal Vasko01130bd2021-08-26 11:47:38 +02002122 (*cont_ptr)->type = ret;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002123 (*cont_ptr)->next = NULL;
2124 }
2125
Michal Vasko01130bd2021-08-26 11:47:38 +02002126cleanup_unlock:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002127 /* MSGS UNLOCK */
Michal Vasko01130bd2021-08-26 11:47:38 +02002128 nc_session_client_msgs_unlock(session, __func__);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002129
Michal Vasko01130bd2021-08-26 11:47:38 +02002130cleanup:
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002131 if (ret == expected) {
2132 *message = msg;
2133 } else {
2134 ly_in_free(msg, 1);
2135 }
2136 return ret;
2137}
2138
Michal Vasko77367452021-02-16 16:32:18 +01002139static NC_MSG_TYPE
2140recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp)
2141{
Michal Vasko77367452021-02-16 16:32:18 +01002142 LY_ERR lyrc;
2143 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002144 NC_MSG_TYPE ret = NC_MSG_ERROR;
2145
2146 assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION)));
2147
2148 *envp = NULL;
2149
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002150 /* Receive messages until a rpc-reply is found or a timeout or error reached */
2151 ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg);
2152 if (ret != NC_MSG_REPLY) {
Michal Vasko77367452021-02-16 16:32:18 +01002153 goto cleanup;
2154 }
2155
2156 /* parse */
2157 lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL);
2158 if (!lyrc) {
2159 ret = recv_reply_check_msgid(session, *envp, msgid);
2160 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002161 } else {
Michal Vasko05532772021-06-03 12:12:38 +02002162 ERR(session, "Received an invalid message (%s).", ly_errmsg(LYD_CTX(op)));
Michal Vasko9a108052022-04-01 12:06:54 +02002163 lyd_free_tree(*envp);
2164 *envp = NULL;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002165 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002166 goto cleanup;
2167 }
2168
Michal Vasko77367452021-02-16 16:32:18 +01002169cleanup:
2170 ly_in_free(msg, 1);
2171 return ret;
2172}
2173
2174static int
2175recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op)
2176{
Michal Vasko143aa142021-10-01 15:31:48 +02002177 LY_ERR lyrc = LY_SUCCESS;
Michal Vasko77367452021-02-16 16:32:18 +01002178 struct nc_rpc_act_generic *rpc_gen;
2179 struct ly_in *in;
2180 struct lyd_node *tree, *op2;
2181 const struct lys_module *mod;
Michal Vasko305faca2021-03-25 09:16:02 +01002182 const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002183
2184 switch (rpc->type) {
2185 case NC_RPC_ACT_GENERIC:
2186 rpc_gen = (struct nc_rpc_act_generic *)rpc;
2187 if (rpc_gen->has_data) {
2188 tree = rpc_gen->content.data;
2189
2190 /* find the operation node */
2191 lyrc = LY_EINVAL;
2192 LYD_TREE_DFS_BEGIN(tree, op2) {
2193 if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
2194 lyrc = lyd_dup_single(op2, NULL, 0, op);
2195 break;
2196 }
2197 LYD_TREE_DFS_END(tree, op2);
2198 }
2199 } else {
2200 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2201 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2);
2202 ly_in_free(in, 0);
2203 if (lyrc) {
Michal Vasko9a108052022-04-01 12:06:54 +02002204 lyd_free_tree(tree);
Michal Vasko77367452021-02-16 16:32:18 +01002205 return -1;
2206 }
2207
2208 /* we want just the operation node */
2209 lyrc = lyd_dup_single(op2, NULL, 0, op);
2210
2211 lyd_free_tree(tree);
2212 }
2213 break;
2214 case NC_RPC_GETCONFIG:
2215 module_name = "ietf-netconf";
2216 rpc_name = "get-config";
2217 break;
2218 case NC_RPC_EDIT:
2219 module_name = "ietf-netconf";
2220 rpc_name = "edit-config";
2221 break;
2222 case NC_RPC_COPY:
2223 module_name = "ietf-netconf";
2224 rpc_name = "copy-config";
2225 break;
2226 case NC_RPC_DELETE:
2227 module_name = "ietf-netconf";
2228 rpc_name = "delete-config";
2229 break;
2230 case NC_RPC_LOCK:
2231 module_name = "ietf-netconf";
2232 rpc_name = "lock";
2233 break;
2234 case NC_RPC_UNLOCK:
2235 module_name = "ietf-netconf";
2236 rpc_name = "unlock";
2237 break;
2238 case NC_RPC_GET:
2239 module_name = "ietf-netconf";
2240 rpc_name = "get";
2241 break;
2242 case NC_RPC_KILL:
2243 module_name = "ietf-netconf";
2244 rpc_name = "kill-session";
2245 break;
2246 case NC_RPC_COMMIT:
2247 module_name = "ietf-netconf";
2248 rpc_name = "commit";
2249 break;
2250 case NC_RPC_DISCARD:
2251 module_name = "ietf-netconf";
2252 rpc_name = "discard-changes";
2253 break;
2254 case NC_RPC_CANCEL:
2255 module_name = "ietf-netconf";
2256 rpc_name = "cancel-commit";
2257 break;
2258 case NC_RPC_VALIDATE:
2259 module_name = "ietf-netconf";
2260 rpc_name = "validate";
2261 break;
2262 case NC_RPC_GETSCHEMA:
2263 module_name = "ietf-netconf-monitoring";
2264 rpc_name = "get-schema";
2265 break;
2266 case NC_RPC_SUBSCRIBE:
2267 module_name = "notifications";
Michal Vaskoeed893d2021-05-25 17:11:08 +02002268 rpc_name = "create-subscription";
Michal Vasko77367452021-02-16 16:32:18 +01002269 break;
2270 case NC_RPC_GETDATA:
2271 module_name = "ietf-netconf-nmda";
2272 rpc_name = "get-data";
2273 break;
2274 case NC_RPC_EDITDATA:
2275 module_name = "ietf-netconf-nmda";
2276 rpc_name = "edit-data";
2277 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002278 case NC_RPC_ESTABLISHSUB:
2279 module_name = "ietf-subscribed-notifications";
2280 rpc_name = "establish-subscription";
2281 break;
2282 case NC_RPC_MODIFYSUB:
2283 module_name = "ietf-subscribed-notifications";
2284 rpc_name = "modify-subscription";
2285 break;
2286 case NC_RPC_DELETESUB:
2287 module_name = "ietf-subscribed-notifications";
2288 rpc_name = "delete-subscription";
2289 break;
2290 case NC_RPC_KILLSUB:
2291 module_name = "ietf-subscribed-notifications";
2292 rpc_name = "kill-subscription";
2293 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002294 case NC_RPC_ESTABLISHPUSH:
2295 module_name = "ietf-subscribed-notifications";
2296 rpc_name = "establish-subscription";
2297 module_check = "ietf-yang-push";
2298 break;
2299 case NC_RPC_MODIFYPUSH:
2300 module_name = "ietf-subscribed-notifications";
2301 rpc_name = "modify-subscription";
2302 module_check = "ietf-yang-push";
2303 break;
2304 case NC_RPC_RESYNCSUB:
2305 module_name = "ietf-yang-push";
2306 rpc_name = "resync-subscription";
2307 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002308 case NC_RPC_UNKNOWN:
Michal Vasko77367452021-02-16 16:32:18 +01002309 lyrc = LY_EINT;
2310 break;
2311 }
2312
2313 if (module_name && rpc_name) {
2314 mod = ly_ctx_get_module_implemented(session->ctx, module_name);
2315 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002316 ERR(session, "Missing \"%s\" module in the context.", module_name);
Michal Vasko77367452021-02-16 16:32:18 +01002317 return -1;
2318 }
2319
2320 /* create the operation node */
2321 lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op);
2322 }
Michal Vasko305faca2021-03-25 09:16:02 +01002323 if (module_check) {
2324 if (!ly_ctx_get_module_implemented(session->ctx, module_check)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002325 ERR(session, "Missing \"%s\" module in the context.", module_check);
Michal Vasko305faca2021-03-25 09:16:02 +01002326 return -1;
2327 }
2328 }
Michal Vasko77367452021-02-16 16:32:18 +01002329
2330 if (lyrc) {
2331 return -1;
2332 }
2333 return 0;
2334}
2335
2336API NC_MSG_TYPE
2337nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp,
2338 struct lyd_node **op)
2339{
2340 NC_MSG_TYPE ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002341
roman40672412023-05-04 11:10:22 +02002342 NC_CHECK_ARG_RET(session, session, rpc, envp, op, NC_MSG_ERROR);
2343
2344 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002345 ERR(session, "Invalid session to receive RPC replies.");
Michal Vasko086311b2016-01-08 09:53:11 +01002346 return NC_MSG_ERROR;
2347 }
Michal Vasko77367452021-02-16 16:32:18 +01002348
2349 /* get a duplicate of the RPC node to append reply to */
2350 if (recv_reply_dup_rpc(session, rpc, op)) {
2351 return NC_MSG_ERROR;
Michal Vaskoeee99412016-11-21 10:19:43 +01002352 }
Michal Vasko086311b2016-01-08 09:53:11 +01002353
Michal Vasko77367452021-02-16 16:32:18 +01002354 /* receive a reply */
2355 ret = recv_reply(session, timeout, *op, msgid, envp);
Michal Vasko086311b2016-01-08 09:53:11 +01002356
Michal Vasko77367452021-02-16 16:32:18 +01002357 /* do not return the RPC copy on error or if the reply includes no data */
2358 if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) {
2359 lyd_free_tree(*op);
2360 *op = NULL;
2361 }
2362 return ret;
2363}
2364
2365static NC_MSG_TYPE
2366recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
2367{
Michal Vasko77367452021-02-16 16:32:18 +01002368 LY_ERR lyrc;
2369 struct ly_in *msg = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01002370 NC_MSG_TYPE ret = NC_MSG_ERROR;
2371
2372 *op = NULL;
2373 *envp = NULL;
2374
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002375 /* Receive messages until a notification is found or a timeout or error reached */
2376 ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg);
2377 if (ret != NC_MSG_NOTIF) {
Michal Vasko77367452021-02-16 16:32:18 +01002378 goto cleanup;
2379 }
2380
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002381 /* Parse */
Michal Vasko77367452021-02-16 16:32:18 +01002382 lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op);
2383 if (!lyrc) {
Michal Vasko77367452021-02-16 16:32:18 +01002384 goto cleanup;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002385 } else {
Michal Vasko05532772021-06-03 12:12:38 +02002386 ERR(session, "Received an invalid message (%s).", ly_errmsg(session->ctx));
Michal Vasko9a108052022-04-01 12:06:54 +02002387 lyd_free_tree(*envp);
2388 *envp = NULL;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02002389 ret = NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01002390 goto cleanup;
2391 }
2392
Michal Vasko77367452021-02-16 16:32:18 +01002393cleanup:
2394 ly_in_free(msg, 1);
2395 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01002396}
2397
2398API NC_MSG_TYPE
Michal Vasko77367452021-02-16 16:32:18 +01002399nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op)
Michal Vasko086311b2016-01-08 09:53:11 +01002400{
roman40672412023-05-04 11:10:22 +02002401 NC_CHECK_ARG_RET(session, session, envp, op, NC_MSG_ERROR);
2402
2403 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002404 ERR(session, "Invalid session to receive Notifications.");
Michal Vasko086311b2016-01-08 09:53:11 +01002405 return NC_MSG_ERROR;
2406 }
2407
Michal Vasko77367452021-02-16 16:32:18 +01002408 /* receive a notification */
2409 return recv_notif(session, timeout, envp, op);
Michal Vasko086311b2016-01-08 09:53:11 +01002410}
2411
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002412static void *
2413nc_recv_notif_thread(void *arg)
2414{
2415 struct nc_ntf_thread_arg *ntarg;
2416 struct nc_session *session;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002417 nc_notif_dispatch_clb notif_clb;
2418 void *user_data;
Michal Vasko743ff9f2022-10-20 10:03:13 +02002419
Michal Vaskoffb35e92022-10-20 09:07:25 +02002420 void (*free_data)(void *);
Michal Vasko77367452021-02-16 16:32:18 +01002421 struct lyd_node *envp, *op;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002422 NC_MSG_TYPE msgtype;
Michal Vasko131120a2018-05-29 15:44:02 +02002423
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002424 /* detach ourselves */
Michal Vasko131120a2018-05-29 15:44:02 +02002425 pthread_detach(pthread_self());
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002426
2427 ntarg = (struct nc_ntf_thread_arg *)arg;
2428 session = ntarg->session;
2429 notif_clb = ntarg->notif_clb;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002430 user_data = ntarg->user_data;
2431 free_data = ntarg->free_data;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002432 free(ntarg);
2433
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002434 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
Michal Vasko77367452021-02-16 16:32:18 +01002435 msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002436 if (msgtype == NC_MSG_NOTIF) {
Michal Vaskoffb35e92022-10-20 09:07:25 +02002437 notif_clb(session, envp, op, user_data);
Michal Vasko77367452021-02-16 16:32:18 +01002438 if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) {
2439 lyd_free_tree(envp);
Michal Vasko0c0239c2023-02-01 14:31:06 +01002440 lyd_free_all(op);
Michal Vaskof0537d82016-01-29 14:42:38 +01002441 break;
2442 }
Michal Vasko77367452021-02-16 16:32:18 +01002443 lyd_free_tree(envp);
Michal Vasko0c0239c2023-02-01 14:31:06 +01002444 lyd_free_all(op);
Michal Vaskoce326052018-01-04 10:32:03 +01002445 } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) {
Michal Vasko132f7f42017-09-14 13:40:18 +02002446 /* quit this thread once the session is broken */
2447 break;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002448 }
2449
2450 usleep(NC_CLIENT_NOTIF_THREAD_SLEEP);
2451 }
2452
Michal Vasko05532772021-06-03 12:12:38 +02002453 VRB(session, "Notification thread exit.");
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002454 ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count);
Michal Vaskoffb35e92022-10-20 09:07:25 +02002455 if (free_data) {
2456 free_data(user_data);
2457 }
2458
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002459 return NULL;
2460}
2461
2462API int
Michal Vaskoffb35e92022-10-20 09:07:25 +02002463nc_recv_notif_dispatch(struct nc_session *session, nc_notif_dispatch_clb notif_clb)
2464{
2465 return nc_recv_notif_dispatch_data(session, notif_clb, NULL, NULL);
2466}
2467
2468API int
2469nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb notif_clb, void *user_data,
2470 void (*free_data)(void *))
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002471{
2472 struct nc_ntf_thread_arg *ntarg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002473 pthread_t tid;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002474 int ret;
2475
roman40672412023-05-04 11:10:22 +02002476 NC_CHECK_ARG_RET(session, session, notif_clb, -1);
2477
2478 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002479 ERR(session, "Invalid session to receive Notifications.");
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002480 return -1;
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002481 }
2482
2483 ntarg = malloc(sizeof *ntarg);
Michal Vasko4eb3c312016-03-01 14:09:37 +01002484 if (!ntarg) {
2485 ERRMEM;
2486 return -1;
2487 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002488 ntarg->session = session;
2489 ntarg->notif_clb = notif_clb;
Michal Vaskoffb35e92022-10-20 09:07:25 +02002490 ntarg->user_data = user_data;
2491 ntarg->free_data = free_data;
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002492 ATOMIC_INC_RELAXED(session->opts.client.ntf_thread_count);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002493
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002494 /* just so that nc_recv_notif_thread() does not immediately exit */
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002495 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 1);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002496
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002497 ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg);
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002498 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02002499 ERR(session, "Failed to create a new thread (%s).", strerror(errno));
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002500 free(ntarg);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +02002501 if (ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count) == 1) {
2502 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
2503 }
Michal Vaskoa8ad4482016-01-28 14:25:54 +01002504 return -1;
2505 }
2506
2507 return 0;
2508}
2509
Michal Vasko77367452021-02-16 16:32:18 +01002510static const char *
2511nc_wd2str(NC_WD_MODE wd)
2512{
2513 switch (wd) {
2514 case NC_WD_ALL:
2515 return "report-all";
2516 case NC_WD_ALL_TAG:
2517 return "report-all-tagged";
2518 case NC_WD_TRIM:
2519 return "trim";
2520 case NC_WD_EXPLICIT:
2521 return "explicit";
2522 default:
2523 break;
2524 }
2525
2526 return NULL;
2527}
2528
Michal Vasko086311b2016-01-08 09:53:11 +01002529API NC_MSG_TYPE
Michal Vasko7f1c78b2016-01-19 09:52:14 +01002530nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid)
Michal Vasko086311b2016-01-08 09:53:11 +01002531{
2532 NC_MSG_TYPE r;
Michal Vasko131120a2018-05-29 15:44:02 +02002533 int dofree = 1;
Michal Vasko77367452021-02-16 16:32:18 +01002534 struct ly_in *in;
Michal Vasko90e8e692016-07-13 12:27:57 +02002535 struct nc_rpc_act_generic *rpc_gen;
Michal Vasko086311b2016-01-08 09:53:11 +01002536 struct nc_rpc_getconfig *rpc_gc;
2537 struct nc_rpc_edit *rpc_e;
2538 struct nc_rpc_copy *rpc_cp;
2539 struct nc_rpc_delete *rpc_del;
2540 struct nc_rpc_lock *rpc_lock;
2541 struct nc_rpc_get *rpc_g;
2542 struct nc_rpc_kill *rpc_k;
2543 struct nc_rpc_commit *rpc_com;
2544 struct nc_rpc_cancel *rpc_can;
2545 struct nc_rpc_validate *rpc_val;
2546 struct nc_rpc_getschema *rpc_gs;
2547 struct nc_rpc_subscribe *rpc_sub;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002548 struct nc_rpc_getdata *rpc_getd;
2549 struct nc_rpc_editdata *rpc_editd;
Michal Vasko96f247a2021-03-15 13:32:10 +01002550 struct nc_rpc_establishsub *rpc_estsub;
2551 struct nc_rpc_modifysub *rpc_modsub;
2552 struct nc_rpc_deletesub *rpc_delsub;
2553 struct nc_rpc_killsub *rpc_killsub;
Michal Vasko305faca2021-03-25 09:16:02 +01002554 struct nc_rpc_establishpush *rpc_estpush;
2555 struct nc_rpc_modifypush *rpc_modpush;
2556 struct nc_rpc_resyncsub *rpc_resyncsub;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002557 struct lyd_node *data = NULL, *node, *cont;
Michal Vasko305faca2021-03-25 09:16:02 +01002558 const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd;
Michal Vaskoab9deb62021-05-27 11:37:00 +02002559 LY_ERR lyrc = 0;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002560 int i;
Radek Krejci539efb62016-08-24 15:05:16 +02002561 char str[11];
Michal Vasko086311b2016-01-08 09:53:11 +01002562 uint64_t cur_msgid;
2563
roman40672412023-05-04 11:10:22 +02002564 NC_CHECK_ARG_RET(session, session, rpc, msgid, NC_MSG_ERROR);
2565
2566 if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) {
Michal Vasko05532772021-06-03 12:12:38 +02002567 ERR(session, "Invalid session to send RPCs.");
Michal Vasko086311b2016-01-08 09:53:11 +01002568 return NC_MSG_ERROR;
2569 }
2570
Michal Vaskoc1171a42019-11-05 12:06:46 +01002571 switch (rpc->type) {
2572 case NC_RPC_ACT_GENERIC:
2573 /* checked when parsing */
2574 break;
2575 case NC_RPC_GETCONFIG:
2576 case NC_RPC_EDIT:
2577 case NC_RPC_COPY:
2578 case NC_RPC_DELETE:
2579 case NC_RPC_LOCK:
2580 case NC_RPC_UNLOCK:
2581 case NC_RPC_GET:
2582 case NC_RPC_KILL:
2583 case NC_RPC_COMMIT:
2584 case NC_RPC_DISCARD:
2585 case NC_RPC_CANCEL:
2586 case NC_RPC_VALIDATE:
Michal Vasko77367452021-02-16 16:32:18 +01002587 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002588 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002589 ERR(session, "Missing \"ietf-netconf\" module in the context.");
Michal Vasko086311b2016-01-08 09:53:11 +01002590 return NC_MSG_ERROR;
2591 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002592 break;
2593 case NC_RPC_GETSCHEMA:
Michal Vasko77367452021-02-16 16:32:18 +01002594 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002595 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002596 ERR(session, "Missing \"ietf-netconf-monitoring\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002597 return NC_MSG_ERROR;
2598 }
2599 break;
2600 case NC_RPC_SUBSCRIBE:
Michal Vasko77367452021-02-16 16:32:18 +01002601 mod = ly_ctx_get_module_implemented(session->ctx, "notifications");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002602 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002603 ERR(session, "Missing \"notifications\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002604 return NC_MSG_ERROR;
2605 }
2606 break;
2607 case NC_RPC_GETDATA:
2608 case NC_RPC_EDITDATA:
Michal Vasko77367452021-02-16 16:32:18 +01002609 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002610 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002611 ERR(session, "Missing \"ietf-netconf-nmda\" module in the context.");
Michal Vaskoc1171a42019-11-05 12:06:46 +01002612 return NC_MSG_ERROR;
2613 }
2614 break;
Michal Vasko96f247a2021-03-15 13:32:10 +01002615 case NC_RPC_ESTABLISHSUB:
2616 case NC_RPC_MODIFYSUB:
2617 case NC_RPC_DELETESUB:
2618 case NC_RPC_KILLSUB:
2619 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2620 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002621 ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context.");
Michal Vasko96f247a2021-03-15 13:32:10 +01002622 return NC_MSG_ERROR;
2623 }
2624 break;
Michal Vasko305faca2021-03-25 09:16:02 +01002625 case NC_RPC_ESTABLISHPUSH:
2626 case NC_RPC_MODIFYPUSH:
2627 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications");
2628 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002629 ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002630 return NC_MSG_ERROR;
2631 }
2632 mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2633 if (!mod2) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002634 ERR(session, "Missing \"ietf-yang-push\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002635 return NC_MSG_ERROR;
2636 }
2637 break;
2638 case NC_RPC_RESYNCSUB:
2639 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push");
2640 if (!mod) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02002641 ERR(session, "Missing \"ietf-yang-push\" module in the context.");
Michal Vasko305faca2021-03-25 09:16:02 +01002642 return NC_MSG_ERROR;
2643 }
2644 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002645 case NC_RPC_UNKNOWN:
2646 ERRINT;
2647 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01002648 }
2649
Michal Vaskoab9deb62021-05-27 11:37:00 +02002650#define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break;
2651
Michal Vasko086311b2016-01-08 09:53:11 +01002652 switch (rpc->type) {
Michal Vasko90e8e692016-07-13 12:27:57 +02002653 case NC_RPC_ACT_GENERIC:
2654 rpc_gen = (struct nc_rpc_act_generic *)rpc;
Michal Vasko086311b2016-01-08 09:53:11 +01002655
2656 if (rpc_gen->has_data) {
2657 data = rpc_gen->content.data;
Radek Krejcib4b19062018-02-07 16:33:06 +01002658 dofree = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01002659 } else {
Michal Vasko77367452021-02-16 16:32:18 +01002660 ly_in_new_memory(rpc_gen->content.xml_str, &in);
2661 lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL);
2662 ly_in_free(in, 0);
2663 if (lyrc) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002664 break;
Michal Vaskoeec410f2017-11-24 09:14:55 +01002665 }
Michal Vasko086311b2016-01-08 09:53:11 +01002666 }
2667 break;
2668
2669 case NC_RPC_GETCONFIG:
2670 rpc_gc = (struct nc_rpc_getconfig *)rpc;
2671
Michal Vaskoab9deb62021-05-27 11:37:00 +02002672 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data));
2673 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
2674 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002675 if (rpc_gc->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002676 if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002677 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, 0, LYD_ANYDATA_XML, 0, &node));
2678 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002679 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002680 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2681 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2682 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002683 }
2684 }
2685
2686 if (rpc_gc->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002687 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002688 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002689 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002690 lyrc = LY_ENOTFOUND;
2691 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002692 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002693 CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_gc->wd_mode), 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002694 }
2695 break;
2696
2697 case NC_RPC_EDIT:
2698 rpc_e = (struct nc_rpc_edit *)rpc;
2699
Michal Vaskoab9deb62021-05-27 11:37:00 +02002700 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data));
2701 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2702 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002703
2704 if (rpc_e->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002705 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op], 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002706 }
Michal Vasko086311b2016-01-08 09:53:11 +01002707 if (rpc_e->test_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002708 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt], 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002709 }
Michal Vasko086311b2016-01-08 09:53:11 +01002710 if (rpc_e->error_opt) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002711 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt], 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002712 }
Michal Vasko7793bc62016-09-16 11:58:41 +02002713 if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002714 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_e->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002715 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002716 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002717 }
2718 break;
2719
2720 case NC_RPC_COPY:
2721 rpc_cp = (struct nc_rpc_copy *)rpc;
2722
Michal Vaskoab9deb62021-05-27 11:37:00 +02002723 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data));
2724 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002725 if (rpc_cp->url_trg) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002726 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002727 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002728 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002729 }
2730
Michal Vaskoab9deb62021-05-27 11:37:00 +02002731 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002732 if (rpc_cp->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002733 if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002734 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_cp->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002735 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002736 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002737 }
2738 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002739 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002740 }
2741
2742 if (rpc_cp->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002743 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002744 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002745 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002746 lyrc = LY_ENOTFOUND;
2747 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002748 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002749 CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_cp->wd_mode), 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002750 }
2751 break;
2752
2753 case NC_RPC_DELETE:
2754 rpc_del = (struct nc_rpc_delete *)rpc;
2755
Michal Vaskoab9deb62021-05-27 11:37:00 +02002756 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data));
2757 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002758 if (rpc_del->url) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002759 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002760 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002761 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002762 }
2763 break;
2764
2765 case NC_RPC_LOCK:
2766 rpc_lock = (struct nc_rpc_lock *)rpc;
2767
Michal Vaskoab9deb62021-05-27 11:37:00 +02002768 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data));
2769 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2770 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002771 break;
2772
2773 case NC_RPC_UNLOCK:
2774 rpc_lock = (struct nc_rpc_lock *)rpc;
2775
Michal Vaskoab9deb62021-05-27 11:37:00 +02002776 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data));
2777 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont));
2778 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002779 break;
2780
2781 case NC_RPC_GET:
2782 rpc_g = (struct nc_rpc_get *)rpc;
2783
Michal Vaskoab9deb62021-05-27 11:37:00 +02002784 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002785 if (rpc_g->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002786 if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002787 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, 0, LYD_ANYDATA_XML, 0, &node));
2788 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002789 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002790 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2791 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2792 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002793 }
2794 }
2795
2796 if (rpc_g->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002797 ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01002798 if (!ietfncwd) {
Michal Vasko69e98752022-12-14 14:20:17 +01002799 ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02002800 lyrc = LY_ENOTFOUND;
2801 break;
Michal Vasko086311b2016-01-08 09:53:11 +01002802 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002803 CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_g->wd_mode), 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002804 }
2805 break;
2806
2807 case NC_RPC_KILL:
2808 rpc_k = (struct nc_rpc_kill *)rpc;
2809
Michal Vaskoab9deb62021-05-27 11:37:00 +02002810 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002811 sprintf(str, "%u", rpc_k->sid);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002812 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002813 break;
2814
2815 case NC_RPC_COMMIT:
2816 rpc_com = (struct nc_rpc_commit *)rpc;
2817
Michal Vaskoab9deb62021-05-27 11:37:00 +02002818 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002819 if (rpc_com->confirmed) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002820 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002821 }
2822
2823 if (rpc_com->confirm_timeout) {
2824 sprintf(str, "%u", rpc_com->confirm_timeout);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002825 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002826 }
Michal Vasko086311b2016-01-08 09:53:11 +01002827 if (rpc_com->persist) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002828 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002829 }
Michal Vasko086311b2016-01-08 09:53:11 +01002830 if (rpc_com->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002831 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002832 }
2833 break;
2834
2835 case NC_RPC_DISCARD:
Michal Vaskoab9deb62021-05-27 11:37:00 +02002836 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002837 break;
2838
2839 case NC_RPC_CANCEL:
2840 rpc_can = (struct nc_rpc_cancel *)rpc;
2841
Michal Vaskoab9deb62021-05-27 11:37:00 +02002842 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002843 if (rpc_can->persist_id) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002844 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002845 }
2846 break;
2847
2848 case NC_RPC_VALIDATE:
2849 rpc_val = (struct nc_rpc_validate *)rpc;
2850
Michal Vaskoab9deb62021-05-27 11:37:00 +02002851 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data));
2852 CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont));
Michal Vasko086311b2016-01-08 09:53:11 +01002853 if (rpc_val->url_config_src) {
Michal Vasko7793bc62016-09-16 11:58:41 +02002854 if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002855 CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_val->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002856 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002857 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002858 }
2859 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002860 CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002861 }
2862 break;
2863
2864 case NC_RPC_GETSCHEMA:
Michal Vasko086311b2016-01-08 09:53:11 +01002865 rpc_gs = (struct nc_rpc_getschema *)rpc;
2866
Michal Vaskoab9deb62021-05-27 11:37:00 +02002867 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data));
2868 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002869 if (rpc_gs->version) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002870 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002871 }
2872 if (rpc_gs->format) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002873 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002874 }
2875 break;
2876
2877 case NC_RPC_SUBSCRIBE:
Michal Vasko086311b2016-01-08 09:53:11 +01002878 rpc_sub = (struct nc_rpc_subscribe *)rpc;
2879
Michal Vaskoab9deb62021-05-27 11:37:00 +02002880 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data));
Michal Vasko086311b2016-01-08 09:53:11 +01002881 if (rpc_sub->stream) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002882 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002883 }
2884
2885 if (rpc_sub->filter) {
Michal Vaskof3c647b2016-03-08 12:17:33 +01002886 if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002887 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, 0, LYD_ANYDATA_XML, 0, &node));
2888 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002889 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002890 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node));
2891 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL));
2892 CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002893 }
2894 }
Michal Vasko086311b2016-01-08 09:53:11 +01002895 if (rpc_sub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002896 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002897 }
Michal Vasko086311b2016-01-08 09:53:11 +01002898 if (rpc_sub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002899 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL));
Michal Vasko086311b2016-01-08 09:53:11 +01002900 }
2901 break;
Michal Vaskoc1171a42019-11-05 12:06:46 +01002902
2903 case NC_RPC_GETDATA:
2904 rpc_getd = (struct nc_rpc_getdata *)rpc;
2905
Michal Vaskoab9deb62021-05-27 11:37:00 +02002906 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data));
2907 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL));
2908
Michal Vaskoc1171a42019-11-05 12:06:46 +01002909 if (rpc_getd->filter) {
2910 if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002911 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "subtree-filter", rpc_getd->filter, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002912 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002913 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002914 }
2915 }
2916 if (rpc_getd->config_filter) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002917 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002918 }
2919 for (i = 0; i < rpc_getd->origin_filter_count; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002920 CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" :
2921 "origin-filter", rpc_getd->origin_filter[i], 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002922 }
2923 if (rpc_getd->max_depth) {
2924 sprintf(str, "%u", rpc_getd->max_depth);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002925 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002926 }
2927 if (rpc_getd->with_origin) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002928 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002929 }
2930
2931 if (rpc_getd->wd_mode) {
Michal Vasko77367452021-02-16 16:32:18 +01002932 /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */
Michal Vaskoab9deb62021-05-27 11:37:00 +02002933 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-defaults", nc_wd2str(rpc_getd->wd_mode), 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002934 }
2935 break;
2936
2937 case NC_RPC_EDITDATA:
2938 rpc_editd = (struct nc_rpc_editdata *)rpc;
2939
Michal Vaskoab9deb62021-05-27 11:37:00 +02002940 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data));
2941 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002942
2943 if (rpc_editd->default_op) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002944 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0,
2945 NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002946 }
Michal Vaskoc1171a42019-11-05 12:06:46 +01002947 if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002948 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_editd->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002949 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002950 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL));
Michal Vaskoc1171a42019-11-05 12:06:46 +01002951 }
2952 break;
2953
Michal Vasko96f247a2021-03-15 13:32:10 +01002954 case NC_RPC_ESTABLISHSUB:
2955 rpc_estsub = (struct nc_rpc_establishsub *)rpc;
2956
Michal Vaskoab9deb62021-05-27 11:37:00 +02002957 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002958
2959 if (rpc_estsub->filter) {
2960 if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002961 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, 0, LYD_ANYDATA_XML,
2962 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002963 } else if (rpc_estsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002964 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002965 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002966 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002967 }
2968 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02002969 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002970
2971 if (rpc_estsub->start) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002972 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002973 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002974 if (rpc_estsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002975 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002976 }
Michal Vasko96f247a2021-03-15 13:32:10 +01002977 if (rpc_estsub->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002978 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002979 }
2980 break;
2981
2982 case NC_RPC_MODIFYSUB:
2983 rpc_modsub = (struct nc_rpc_modifysub *)rpc;
2984
Michal Vaskoab9deb62021-05-27 11:37:00 +02002985 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01002986
2987 sprintf(str, "%u", rpc_modsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02002988 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002989
2990 if (rpc_modsub->filter) {
2991 if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002992 CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, 0, LYD_ANYDATA_XML,
2993 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002994 } else if (rpc_modsub->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002995 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002996 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02002997 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01002998 }
2999 }
Michal Vasko96f247a2021-03-15 13:32:10 +01003000 if (rpc_modsub->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003001 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003002 }
3003 break;
3004
3005 case NC_RPC_DELETESUB:
3006 rpc_delsub = (struct nc_rpc_deletesub *)rpc;
3007
Michal Vaskoab9deb62021-05-27 11:37:00 +02003008 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01003009
3010 sprintf(str, "%u", rpc_delsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003011 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003012 break;
3013
3014 case NC_RPC_KILLSUB:
3015 rpc_killsub = (struct nc_rpc_killsub *)rpc;
3016
Michal Vaskoab9deb62021-05-27 11:37:00 +02003017 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data));
Michal Vasko96f247a2021-03-15 13:32:10 +01003018
3019 sprintf(str, "%u", rpc_killsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003020 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko96f247a2021-03-15 13:32:10 +01003021 break;
3022
Michal Vasko305faca2021-03-25 09:16:02 +01003023 case NC_RPC_ESTABLISHPUSH:
3024 rpc_estpush = (struct nc_rpc_establishpush *)rpc;
3025
Michal Vaskoab9deb62021-05-27 11:37:00 +02003026 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data));
3027 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003028
3029 if (rpc_estpush->filter) {
3030 if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003031 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter, 0,
3032 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003033 } else if (rpc_estpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003034 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003035 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003036 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003037 }
3038 }
3039
3040 if (rpc_estpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003041 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003042 }
Michal Vasko305faca2021-03-25 09:16:02 +01003043 if (rpc_estpush->encoding) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003044 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003045 }
3046
3047 if (rpc_estpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003048 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003049 sprintf(str, "%" PRIu32, rpc_estpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003050 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003051 if (rpc_estpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003052 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003053 }
3054 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003055 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003056 if (rpc_estpush->dampening_period) {
3057 sprintf(str, "%" PRIu32, rpc_estpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003058 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003059 }
Michal Vaskoab9deb62021-05-27 11:37:00 +02003060 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0,
3061 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003062 if (rpc_estpush->excluded_change) {
3063 for (i = 0; rpc_estpush->excluded_change[i]; ++i) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003064 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0,
3065 NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003066 }
3067 }
3068 }
3069 break;
3070
3071 case NC_RPC_MODIFYPUSH:
3072 rpc_modpush = (struct nc_rpc_modifypush *)rpc;
3073
Michal Vaskoab9deb62021-05-27 11:37:00 +02003074 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01003075
3076 sprintf(str, "%u", rpc_modpush->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003077 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
3078 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003079
3080 if (rpc_modpush->filter) {
3081 if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003082 CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter, 0,
3083 LYD_ANYDATA_XML, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003084 } else if (rpc_modpush->filter[0] == '/') {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003085 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003086 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003087 CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003088 }
3089 }
Michal Vasko305faca2021-03-25 09:16:02 +01003090 if (rpc_modpush->stop) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003091 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003092 }
3093
3094 if (rpc_modpush->periodic) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003095 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003096 sprintf(str, "%" PRIu32, rpc_modpush->period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003097 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003098 if (rpc_modpush->anchor_time) {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003099 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003100 }
3101 } else {
Michal Vaskoab9deb62021-05-27 11:37:00 +02003102 CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont));
Michal Vasko305faca2021-03-25 09:16:02 +01003103 if (rpc_modpush->dampening_period) {
3104 sprintf(str, "%" PRIu32, rpc_modpush->dampening_period);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003105 CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003106 }
3107 }
3108 break;
3109
3110 case NC_RPC_RESYNCSUB:
3111 rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc;
3112
Michal Vaskoab9deb62021-05-27 11:37:00 +02003113 CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data));
Michal Vasko305faca2021-03-25 09:16:02 +01003114 sprintf(str, "%u", rpc_resyncsub->id);
Michal Vaskoab9deb62021-05-27 11:37:00 +02003115 CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL));
Michal Vasko305faca2021-03-25 09:16:02 +01003116 break;
3117
Michal Vasko96f247a2021-03-15 13:32:10 +01003118 case NC_RPC_UNKNOWN:
Michal Vasko7f1c78b2016-01-19 09:52:14 +01003119 ERRINT;
3120 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01003121 }
3122
Michal Vaskoab9deb62021-05-27 11:37:00 +02003123#undef CHECK_LYRC_BREAK
3124
3125 if (lyrc) {
Michal Vasko05532772021-06-03 12:12:38 +02003126 ERR(session, "Failed to create RPC, perhaps a required feature is disabled.");
Michal Vaskoab9deb62021-05-27 11:37:00 +02003127 lyd_free_tree(data);
Michal Vasko131120a2018-05-29 15:44:02 +02003128 return NC_MSG_ERROR;
3129 }
3130
Michal Vasko131120a2018-05-29 15:44:02 +02003131 /* send RPC, store its message ID */
3132 r = nc_send_msg_io(session, timeout, data);
3133 cur_msgid = session->opts.client.msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01003134
Radek Krejcib4b19062018-02-07 16:33:06 +01003135 if (dofree) {
Michal Vasko77367452021-02-16 16:32:18 +01003136 lyd_free_tree(data);
Radek Krejcib4b19062018-02-07 16:33:06 +01003137 }
Michal Vasko086311b2016-01-08 09:53:11 +01003138
Michal Vasko131120a2018-05-29 15:44:02 +02003139 if (r == NC_MSG_RPC) {
3140 *msgid = cur_msgid;
Michal Vasko086311b2016-01-08 09:53:11 +01003141 }
Michal Vasko131120a2018-05-29 15:44:02 +02003142 return r;
Michal Vasko086311b2016-01-08 09:53:11 +01003143}
Michal Vaskode2946c2017-01-12 12:19:26 +01003144
3145API void
3146nc_client_session_set_not_strict(struct nc_session *session)
3147{
3148 if (session->side != NC_CLIENT) {
roman40672412023-05-04 11:10:22 +02003149 ERRARG(NULL, "session");
Michal Vaskode2946c2017-01-12 12:19:26 +01003150 return;
3151 }
3152
3153 session->flags |= NC_SESSION_CLIENT_NOT_STRICT;
3154}