Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_client.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 session client functions |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
| 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
Michal Vasko | bbf52c8 | 2020-04-07 13:08:50 +0200 | [diff] [blame] | 17 | |
| 18 | #ifdef __linux__ |
| 19 | # include <sys/syscall.h> |
| 20 | #endif |
| 21 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 23 | #include <assert.h> |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 24 | #include <ctype.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <netdb.h> |
Radek Krejci | 4cf58ec | 2016-02-26 15:04:52 +0100 | [diff] [blame] | 28 | #include <netinet/in.h> |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 29 | #include <netinet/tcp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 30 | #include <poll.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 31 | #include <pthread.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 32 | #include <pwd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 35 | #include <sys/select.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 39 | #include <sys/un.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 40 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 41 | |
| 42 | #include <libyang/libyang.h> |
| 43 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 44 | #include "compat.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 45 | #include "libnetconf.h" |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 46 | #include "messages_client.h" |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 47 | #include "session_client.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 48 | |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 49 | #include "../modules/ietf_netconf@2013-09-29_yang.h" |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 50 | #include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h" |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 51 | |
Michal Vasko | 80ef5d2 | 2016-01-18 09:21:02 +0100 | [diff] [blame] | 52 | static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"}; |
| 53 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 54 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 55 | char *sshauth_password(const char *username, const char *hostname, void *priv); |
| 56 | char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 57 | char *sshauth_privkey_passphrase(const char *privkey_path, void *priv); |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 58 | #endif /* NC_ENABLED_SSH */ |
| 59 | |
| 60 | static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT; |
| 61 | static pthread_key_t nc_client_context_key; |
| 62 | #ifdef __linux__ |
| 63 | static struct nc_client_context context_main = { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 64 | .opts.ka = { |
| 65 | .enabled = 1, |
| 66 | .idle_time = 1, |
| 67 | .max_probes = 10, |
| 68 | .probe_interval = 5 |
| 69 | }, |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 70 | #ifdef NC_ENABLED_SSH |
| 71 | .ssh_opts = { |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 72 | .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}}, |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 73 | .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 Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 79 | .auth_password = sshauth_password, |
| 80 | .auth_interactive = sshauth_interactive, |
| 81 | .auth_privkey_passphrase = sshauth_privkey_passphrase |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 82 | }, |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 83 | #endif /* NC_ENABLED_SSH */ |
| 84 | /* .tls_ structures zeroed */ |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 85 | .refcount = 0 |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 86 | }; |
| 87 | #endif |
| 88 | |
| 89 | static void |
| 90 | nc_client_context_free(void *ptr) |
| 91 | { |
| 92 | struct nc_client_context *c = (struct nc_client_context *)ptr; |
| 93 | |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 94 | if (--(c->refcount)) { |
| 95 | /* still used */ |
| 96 | return; |
| 97 | } |
| 98 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 99 | #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 Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 107 | /* for the main thread the same is done in nc_client_destroy() */ |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 108 | free(c->opts.schema_searchpath); |
| 109 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 110 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 111 | int i; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 112 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 113 | 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 Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 120 | #endif |
| 121 | #ifdef NC_ENABLED_SSH |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 122 | _nc_client_ssh_destroy_opts(&c->ssh_opts); |
| 123 | _nc_client_ssh_destroy_opts(&c->ssh_ch_opts); |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 124 | #endif |
| 125 | #ifdef NC_ENABLED_TLS |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 126 | _nc_client_tls_destroy_opts(&c->tls_opts); |
| 127 | _nc_client_tls_destroy_opts(&c->tls_ch_opts); |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 128 | #endif |
| 129 | free(c); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | nc_client_context_createkey(void) |
| 135 | { |
| 136 | int r; |
| 137 | |
| 138 | /* initiate */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 139 | while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {} |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 140 | pthread_setspecific(nc_client_context_key, NULL); |
| 141 | } |
| 142 | |
| 143 | struct nc_client_context * |
| 144 | nc_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 Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 161 | e->refcount = 1; |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 162 | #ifdef NC_ENABLED_SSH |
| 163 | e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 164 | e->ssh_opts.auth_pref[0].value = 1; |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 165 | 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; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 168 | e->ssh_opts.auth_pref[2].value = 3; |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 169 | 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 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 173 | /* callhome settings are the same */ |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 174 | 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 Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 187 | |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 188 | API void * |
| 189 | nc_client_get_thread_context(void) |
| 190 | { |
| 191 | return nc_client_context_location(); |
| 192 | } |
| 193 | |
| 194 | API void |
| 195 | nc_client_set_thread_context(void *context) |
| 196 | { |
| 197 | struct nc_client_context *old, *new; |
| 198 | |
| 199 | if (!context) { |
| 200 | ERRARG(context); |
| 201 | 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 Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 217 | /** |
| 218 | * @brief Ext data callback for a context to provide schema mount data. |
| 219 | */ |
| 220 | static LY_ERR |
| 221 | nc_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 Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 241 | int |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 242 | nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx) |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 243 | { |
| 244 | /* assign context (dicionary needed for handshake) */ |
| 245 | if (!ctx) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 246 | if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 247 | return EXIT_FAILURE; |
| 248 | } |
| 249 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 250 | /* user path must be first, the first path is used to store modules retreived via get-schema */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 251 | if (client_opts.schema_searchpath) { |
| 252 | ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath); |
Michal Vasko | 5d0cdc3 | 2022-12-12 07:38:21 +0100 | [diff] [blame] | 253 | } |
Michal Vasko | 8022722 | 2022-12-12 09:05:24 +0100 | [diff] [blame] | 254 | if (!access(NC_CLIENT_SEARCH_DIR, F_OK)) { |
| 255 | ly_ctx_set_searchdir(ctx, NC_CLIENT_SEARCH_DIR); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 256 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 257 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 258 | /* set callback for getting modules, if provided */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 259 | ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data); |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 260 | |
| 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 Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 263 | } else { |
| 264 | session->flags |= NC_SESSION_SHAREDCTX; |
| 265 | } |
| 266 | |
| 267 | session->ctx = ctx; |
| 268 | |
| 269 | return EXIT_SUCCESS; |
| 270 | } |
| 271 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 272 | API int |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 273 | nc_client_set_schema_searchpath(const char *path) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 274 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 275 | if (client_opts.schema_searchpath) { |
| 276 | free(client_opts.schema_searchpath); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 277 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 278 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 279 | if (path) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 280 | client_opts.schema_searchpath = strdup(path); |
| 281 | if (!client_opts.schema_searchpath) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 282 | ERRMEM; |
| 283 | return 1; |
| 284 | } |
| 285 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 286 | client_opts.schema_searchpath = NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 292 | API const char * |
| 293 | nc_client_get_schema_searchpath(void) |
| 294 | { |
| 295 | return client_opts.schema_searchpath; |
| 296 | } |
| 297 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 298 | API int |
| 299 | nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 300 | { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 301 | 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 Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 311 | API ly_module_imp_clb |
| 312 | nc_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 Vasko | a272e09 | 2023-07-10 14:34:03 +0200 | [diff] [blame] | 320 | API void |
| 321 | nc_client_set_new_session_context_autofill(int enabled) |
| 322 | { |
| 323 | client_opts.auto_context_fill_disabled = !enabled; |
| 324 | } |
| 325 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 326 | struct module_info { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 327 | char *name; |
| 328 | char *revision; |
Michal Vasko | 6ce8e82 | 2022-08-02 15:09:17 +0200 | [diff] [blame] | 329 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 330 | struct { |
| 331 | char *name; |
| 332 | char *revision; |
| 333 | } *submodules; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 334 | char **features; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 335 | int implemented; |
| 336 | }; |
| 337 | |
| 338 | struct clb_data_s { |
| 339 | void *user_data; |
| 340 | ly_module_imp_clb user_clb; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 341 | struct module_info *modules; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 342 | struct nc_session *session; |
| 343 | int has_get_schema; |
| 344 | }; |
| 345 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 346 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 347 | * @brief Retrieve YANG module content from a local file. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 348 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 349 | * @param[in] name Module name. |
| 350 | * @param[in] rev Module revision. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 351 | * @param[in] clb_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 352 | * @param[out] format Module format. |
| 353 | * @return Module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 354 | */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 355 | static char * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 356 | retrieve_module_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 357 | LYS_INFORMAT *format) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 358 | { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 359 | char *localfile = NULL; |
| 360 | FILE *f; |
| 361 | long length, l; |
| 362 | char *model_data = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 363 | |
Radek Krejci | 3b60e5c | 2018-08-17 10:10:16 +0200 | [diff] [blame] | 364 | if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 365 | !(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD), |
| 366 | name, rev, &localfile, format)) { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
| 369 | if (localfile) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 370 | VRB(clb_data->session, "Reading module \"%s@%s\" from local file \"%s\".", name, rev ? rev : "<latest>", |
Michal Vasko | 7502b60 | 2022-08-26 11:51:17 +0200 | [diff] [blame] | 371 | localfile); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 372 | f = fopen(localfile, "r"); |
| 373 | if (!f) { |
Michal Vasko | 7502b60 | 2022-08-26 11:51:17 +0200 | [diff] [blame] | 374 | ERR(clb_data->session, "Unable to open file \"%s\" (%s).", localfile, strerror(errno)); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 375 | free(localfile); |
| 376 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 377 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 378 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 379 | fseek(f, 0, SEEK_END); |
| 380 | length = ftell(f); |
Radek Krejci | 3f499a6 | 2018-08-17 10:16:57 +0200 | [diff] [blame] | 381 | if (length < 0) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 382 | ERR(clb_data->session, "Unable to get the size of module file \"%s\".", localfile); |
Radek Krejci | 3f499a6 | 2018-08-17 10:16:57 +0200 | [diff] [blame] | 383 | free(localfile); |
| 384 | fclose(f); |
| 385 | return NULL; |
| 386 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 387 | 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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 393 | ERR(clb_data->session, "Reading module from \"%s\" failed (%d bytes read, but %d expected).", localfile, l, |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 394 | length); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 395 | free(model_data); |
| 396 | model_data = NULL; |
| 397 | } else { |
| 398 | /* terminating NULL byte */ |
| 399 | model_data[length] = '\0'; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 400 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 401 | fclose(f); |
| 402 | free(localfile); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 405 | return model_data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 408 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 409 | * @brief Retrieve YANG module content from a reply to get-schema RPC. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 410 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 411 | * @param[in] name Module name. |
| 412 | * @param[in] rev Module revision. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 413 | * @param[in] clb_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 414 | * @param[out] format Module format. |
| 415 | * @return Module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 416 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 417 | static char * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 418 | retrieve_module_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 419 | LYS_INFORMAT *format) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 420 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 421 | struct nc_rpc *rpc; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 422 | struct lyd_node *envp = NULL, *op = NULL; |
| 423 | struct lyd_node_any *get_schema_data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 424 | NC_MSG_TYPE msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 425 | uint64_t msgid; |
Michal Vasko | 7502b60 | 2022-08-26 11:51:17 +0200 | [diff] [blame] | 426 | char *localfile = NULL, *envp_str = NULL, *model_data = NULL; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 427 | FILE *f; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 428 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 429 | VRB(clb_data->session, "Reading module \"%s@%s\" from server via get-schema.", name, rev ? rev : "<latest>"); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 430 | rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 431 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 432 | while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 433 | usleep(1000); |
| 434 | } |
| 435 | if (msg == NC_MSG_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 436 | ERR(clb_data->session, "Failed to send the <get-schema> RPC."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 437 | nc_rpc_free(rpc); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
Michal Vasko | ff8ddc0 | 2016-10-03 14:13:29 +0200 | [diff] [blame] | 441 | do { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 442 | msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op); |
Robin Jarry | 52ddb95 | 2019-10-15 16:23:01 +0200 | [diff] [blame] | 443 | } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 444 | nc_rpc_free(rpc); |
| 445 | if (msg == NC_MSG_WOULDBLOCK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 446 | ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 447 | goto cleanup; |
Michal Vasko | 8d0f3b3 | 2022-01-12 08:29:14 +0100 | [diff] [blame] | 448 | } else if (msg == NC_MSG_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 449 | ERR(clb_data->session, "Failed to receive a reply to <get-schema>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 450 | goto cleanup; |
Michal Vasko | 8d0f3b3 | 2022-01-12 08:29:14 +0100 | [diff] [blame] | 451 | } else if (!op) { |
Michal Vasko | 7502b60 | 2022-08-26 11:51:17 +0200 | [diff] [blame] | 452 | 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 Vasko | 8d0f3b3 | 2022-01-12 08:29:14 +0100 | [diff] [blame] | 456 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 459 | if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 460 | ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 461 | goto cleanup; |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 462 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 463 | get_schema_data = (struct lyd_node_any *)lyd_child(op); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 464 | switch (get_schema_data->value_type) { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 465 | case LYD_ANYDATA_STRING: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 466 | case LYD_ANYDATA_XML: |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 467 | model_data = strdup(get_schema_data->value.str); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 468 | break; |
| 469 | case LYD_ANYDATA_DATATREE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 470 | lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 471 | break; |
Radek Krejci | 03438ec | 2016-09-21 14:12:26 +0200 | [diff] [blame] | 472 | case LYD_ANYDATA_JSON: |
Michal Vasko | d838d29 | 2018-08-15 11:39:05 +0200 | [diff] [blame] | 473 | case LYD_ANYDATA_LYB: |
Radek Krejci | 03438ec | 2016-09-21 14:12:26 +0200 | [diff] [blame] | 474 | ERRINT; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 475 | break; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 476 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 477 | |
Radek Krejci | 488b070 | 2018-08-29 14:47:15 +0200 | [diff] [blame] | 478 | if (model_data && !model_data[0]) { |
| 479 | /* empty data */ |
| 480 | free(model_data); |
| 481 | model_data = NULL; |
| 482 | } |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 483 | if (!model_data) { |
| 484 | goto cleanup; |
| 485 | } |
| 486 | |
| 487 | /* set format */ |
| 488 | *format = LYS_IN_YANG; |
Radek Krejci | 488b070 | 2018-08-29 14:47:15 +0200 | [diff] [blame] | 489 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 490 | /* try to store the model_data into local module repository */ |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 491 | 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 Vasko | f945da5 | 2018-02-15 08:45:13 +0100 | [diff] [blame] | 501 | } else { |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 502 | fputs(model_data, f); |
| 503 | fclose(f); |
Michal Vasko | f945da5 | 2018-02-15 08:45:13 +0100 | [diff] [blame] | 504 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 505 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 506 | } |
Michal Vasko | a172117 | 2022-12-12 09:06:53 +0100 | [diff] [blame] | 507 | free(localfile); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 508 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 509 | cleanup: |
| 510 | lyd_free_tree(envp); |
| 511 | lyd_free_tree(op); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 512 | return model_data; |
| 513 | } |
| 514 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 515 | static void |
| 516 | free_with_user_data(void *data, void *user_data) |
Jan Kundrát | 35972df | 2018-09-06 19:00:01 +0200 | [diff] [blame] | 517 | { |
| 518 | free(data); |
| 519 | (void)user_data; |
| 520 | } |
| 521 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 522 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 523 | * @brief Retrieve YANG module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 524 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 525 | * @param[in] mod_name Module name. |
| 526 | * @param[in] mod_rev Module revision. |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 527 | * @param[in] user_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 528 | * @param[out] format Module format. |
| 529 | * @param[out] module_data Module content. |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 530 | * @param[out] free_module_data Callback for freeing @p module_data. |
| 531 | * @return LY_ERR value. |
| 532 | */ |
| 533 | static LY_ERR |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 534 | retrieve_module_data(const char *mod_name, const char *mod_rev, void *user_data, LYS_INFORMAT *format, |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 535 | 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 Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 540 | /* 1. try to get data locally */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 541 | model_data = retrieve_module_data_localfile(mod_name, mod_rev, clb_data, format); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 542 | |
| 543 | /* 2. try to use <get-schema> */ |
| 544 | if (!model_data && clb_data->has_get_schema) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 545 | model_data = retrieve_module_data_getschema(mod_name, mod_rev, clb_data, format); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* 3. try to use user callback */ |
| 549 | if (!model_data && clb_data->user_clb) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 550 | VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", mod_name, mod_rev ? mod_rev : "<latest>"); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 551 | 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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 561 | * @brief Retrieve YANG import module content. |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 562 | * |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 563 | * @param[in] mod_name Module name. |
| 564 | * @param[in] mod_rev Module revision. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 565 | * @param[in] submod_name Optional submodule name. |
| 566 | * @param[in] sub_rev Submodule revision. |
| 567 | * @param[in] user_data get-schema callback data. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 568 | * @param[out] format Module format. |
| 569 | * @param[out] module_data Module content. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 570 | * @param[out] free_module_data Callback for freeing @p module_data. |
| 571 | * @return LY_ERR value. |
| 572 | */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 573 | static LY_ERR |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 574 | retrieve_module_data_imp(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 575 | void *user_data, LYS_INFORMAT *format, const char **module_data, |
| 576 | void (**free_module_data)(void *model_data, void *user_data)) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 577 | { |
| 578 | struct clb_data_s *clb_data = (struct clb_data_s *)user_data; |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 579 | uint32_t u, v, match = 1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 580 | const char *name = NULL, *rev = NULL; |
| 581 | char *model_data = NULL; |
| 582 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 583 | /* get and check the final name and revision of the module to be retrieved */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 584 | 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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 587 | for (u = 0; clb_data->modules[u].name; ++u) { |
| 588 | if (strcmp(mod_name, clb_data->modules[u].name)) { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 589 | continue; |
| 590 | } |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 591 | if (!match || (strcmp(mod_rev, clb_data->modules[u].revision) > 0)) { |
| 592 | mod_rev = clb_data->modules[u].revision; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 593 | } |
| 594 | match = u + 1; |
| 595 | } |
| 596 | if (!match) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 597 | /* valid situation if we are retrieving YANG 1.1 module and have only capabilities for now |
Michal Vasko | 1d2665c | 2021-02-12 09:28:44 +0100 | [diff] [blame] | 598 | * (when loading ietf-datastore for ietf-yang-library) */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 599 | VRB(clb_data->session, "Unable to identify revision of the import module \"%s\" from " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 600 | "the available server side information.", mod_name); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | if (submod_name) { |
| 604 | name = submod_name; |
| 605 | if (sub_rev) { |
| 606 | rev = sub_rev; |
Radek Krejci | 6455eb1 | 2018-08-17 09:26:29 +0200 | [diff] [blame] | 607 | } else if (match) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 608 | if (!clb_data->modules[match - 1].submodules) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 609 | VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", " |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 610 | "in import module \"%s\", from the available server side information.", submod_name, mod_name); |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 611 | } else { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 612 | 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 Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 615 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 616 | } |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 617 | if (!rev) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 618 | ERR(clb_data->session, "Requested submodule \"%s\" is not found in import module \"%s\" on server side.", |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 619 | submod_name, mod_name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 620 | return LY_ENOTFOUND; |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 621 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } else { |
| 625 | name = mod_name; |
| 626 | rev = mod_rev; |
| 627 | } |
| 628 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 629 | if (match) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 630 | /* we have enough information to avoid communication with server and try to get the module locally */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 631 | |
| 632 | /* 1. try to get data locally */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 633 | model_data = retrieve_module_data_localfile(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 634 | |
| 635 | /* 2. try to use <get-schema> */ |
| 636 | if (!model_data && clb_data->has_get_schema) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 637 | model_data = retrieve_module_data_getschema(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 638 | } |
| 639 | } else { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 640 | /* we are unsure which revision of the module we should load, so first try to get |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 641 | * 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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 648 | model_data = retrieve_module_data_getschema(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* 2. try to get data locally */ |
| 652 | if (!model_data) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 653 | model_data = retrieve_module_data_localfile(name, rev, clb_data, format); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | /* 3. try to use user callback */ |
| 658 | if (!model_data && clb_data->user_clb) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 659 | VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", name, rev ? rev : "<latest>"); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 660 | 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Jan Kundrát | 35972df | 2018-09-06 19:00:01 +0200 | [diff] [blame] | 664 | *free_module_data = free_with_user_data; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 665 | *module_data = model_data; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 666 | return *module_data ? LY_SUCCESS : LY_ENOTFOUND; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 669 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 670 | * @brief Load a YANG module into context. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 671 | * |
| 672 | * @param[in] session NC session. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 673 | * @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 Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 678 | * @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 Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 684 | static int |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 685 | nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, const char **features, |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 686 | struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod) |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 687 | { |
| 688 | int ret = 0; |
| 689 | struct ly_err_item *eitem; |
Jan Kundrát | 6d7c396 | 2018-09-06 19:01:07 +0200 | [diff] [blame] | 690 | const char *module_data = NULL; |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 691 | struct ly_in *in; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 692 | LYS_INFORMAT format; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 693 | |
| 694 | void (*free_module_data)(void *, void *) = NULL; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 695 | struct clb_data_s clb_data; |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 696 | |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 697 | /* try to use a module from the context */ |
Michal Vasko | c3e402f | 2022-09-14 13:34:55 +0200 | [diff] [blame] | 698 | *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 Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 703 | *mod = ly_ctx_get_module_latest(session->ctx, name); |
| 704 | } |
Michal Vasko | c3e402f | 2022-09-14 13:34:55 +0200 | [diff] [blame] | 705 | } 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 708 | } |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 709 | |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 710 | if (*mod) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 711 | /* make the present module implemented and/or enable all its features */ |
| 712 | if (lys_set_implemented(*mod, features)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 713 | ERR(session, "Failed to implement module \"%s\".", (*mod)->name); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 714 | ret = -1; |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 715 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 716 | } else { |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 717 | /* missing implemented module, load it ... */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 718 | clb_data.has_get_schema = has_get_schema; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 719 | clb_data.modules = modules; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 720 | clb_data.session = session; |
| 721 | clb_data.user_clb = user_clb; |
| 722 | clb_data.user_data = user_data; |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 723 | |
| 724 | /* clear all the errors and just collect them for now */ |
| 725 | ly_err_clean(session->ctx, NULL); |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 726 | ly_log_options(LY_LOSTORE); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 727 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 728 | /* get module data */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 729 | retrieve_module_data(name, revision, &clb_data, &format, &module_data, &free_module_data); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 730 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 731 | if (module_data) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 732 | /* set import callback */ |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 733 | ly_ctx_set_module_imp_clb(session->ctx, retrieve_module_data_imp, &clb_data); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 734 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 735 | /* parse the module */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 736 | ly_in_new_memory(module_data, &in); |
| 737 | lys_parse(session->ctx, in, format, features, mod); |
| 738 | ly_in_free(in, 0); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 739 | if (*free_module_data) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 740 | (*free_module_data)((char *)module_data, user_data); |
Michal Vasko | dbf7c27 | 2018-02-19 09:07:59 +0100 | [diff] [blame] | 741 | } |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 742 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 743 | ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL); |
| 744 | } |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 745 | |
Michal Vasko | dbf7c27 | 2018-02-19 09:07:59 +0100 | [diff] [blame] | 746 | /* restore logging options, then print errors on definite failure */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 747 | ly_log_options(LY_LOLOG | LY_LOSTORE_LAST); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 748 | if (!(*mod)) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 749 | for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 750 | ly_err_print(session->ctx, eitem); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 751 | } |
| 752 | ret = -1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 753 | } else { |
| 754 | /* print only warnings */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 755 | for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) { |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 756 | if (eitem->level == LY_LLWRN) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 757 | ly_err_print(session->ctx, eitem); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 758 | } |
| 759 | } |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 760 | } |
| 761 | |
Michal Vasko | dbf7c27 | 2018-02-19 09:07:59 +0100 | [diff] [blame] | 762 | /* clean the errors */ |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 763 | ly_err_clean(session->ctx, NULL); |
Michal Vasko | ceae015 | 2018-02-14 16:03:59 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 769 | static void |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 770 | free_module_info(struct module_info *list) |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 771 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 772 | uint32_t u, v; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 773 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 774 | if (!list) { |
| 775 | return; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 778 | for (u = 0; list[u].name; ++u) { |
| 779 | free(list[u].name); |
| 780 | free(list[u].revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 781 | 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 786 | } |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 787 | 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 794 | } |
| 795 | free(list); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 798 | /** |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 799 | * @brief Retrieve yang-library and schema-mounts operational data from the server. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 800 | * |
| 801 | * @param[in] session NC session. |
| 802 | * @param[in] has_get_data Whether get-data RPC is available or only get. |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 803 | * @param[in] filter Filter to use. |
| 804 | * @param[out] oper_data Received data. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 805 | * @return 0 on success. |
| 806 | * @return -1 on error. |
| 807 | */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 808 | static int |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 809 | get_oper_data(struct nc_session *session, int has_get_data, const char *filter, struct lyd_node **oper_data) |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 810 | { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 811 | struct nc_rpc *rpc = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 812 | struct lyd_node *op = NULL, *envp = NULL; |
| 813 | struct lyd_node_any *data; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 814 | NC_MSG_TYPE msg; |
| 815 | uint64_t msgid; |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 816 | int ret = 0; |
| 817 | const char *rpc_name; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 818 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 819 | /* get data from the server */ |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 820 | if (has_get_data) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 821 | rpc_name = "<get-data>"; |
| 822 | rpc = nc_rpc_getdata("ietf-datastores:operational", filter, "false", NULL, 0, 0, 0, 0, 0, NC_PARAMTYPE_CONST); |
Radek Krejci | 261737f | 2018-08-21 09:22:34 +0200 | [diff] [blame] | 823 | } else { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 824 | rpc_name = "<get>"; |
| 825 | rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST); |
Radek Krejci | 261737f | 2018-08-21 09:22:34 +0200 | [diff] [blame] | 826 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 827 | 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 Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 835 | WRN(session, "Failed to send %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 836 | goto cleanup; |
| 837 | } |
| 838 | |
| 839 | do { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 840 | 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 Jarry | 52ddb95 | 2019-10-15 16:23:01 +0200 | [diff] [blame] | 844 | } while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 845 | if (msg == NC_MSG_WOULDBLOCK) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 846 | WRN(session, "Timeout for receiving reply to a %s RPC expired.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 847 | goto cleanup; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 848 | } else if (msg == NC_MSG_ERROR) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 849 | WRN(session, "Failed to receive a reply to %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 850 | goto cleanup; |
Jan Kundrát | 1d73a5a | 2022-03-30 16:54:49 +0200 | [diff] [blame] | 851 | } else if (!op || !lyd_child(op) || !lyd_child(op)->schema || strcmp(lyd_child(op)->schema->name, "data")) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 852 | WRN(session, "Unexpected reply without data to a %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 853 | goto cleanup; |
| 854 | } |
| 855 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 856 | data = (struct lyd_node_any *)lyd_child(op); |
| 857 | if (data->value_type != LYD_ANYDATA_DATATREE) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 858 | WRN(session, "Unexpected data in reply to a %s RPC.", rpc_name); |
Michal Vasko | 4052875 | 2021-06-21 15:41:51 +0200 | [diff] [blame] | 859 | goto cleanup; |
| 860 | } else if (!data->value.tree) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 861 | WRN(session, "No data in reply to a %s RPC.", rpc_name); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 862 | goto cleanup; |
| 863 | } |
| 864 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 865 | *oper_data = data->value.tree; |
| 866 | data->value.tree = NULL; |
| 867 | |
| 868 | cleanup: |
| 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 | */ |
| 892 | static int |
| 893 | build_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 Krejci | 2d08883 | 2018-08-21 13:40:14 +0200 | [diff] [blame] | 918 | goto cleanup; |
Radek Krejci | ac91952 | 2018-08-17 11:00:17 +0200 | [diff] [blame] | 919 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 920 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 921 | (*result) = calloc(modules->count + 1, sizeof **result); |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 922 | if (!(*result)) { |
Radek Krejci | c939050 | 2018-08-17 10:23:13 +0200 | [diff] [blame] | 923 | ERRMEM; |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 924 | ret = -1; |
Radek Krejci | c939050 | 2018-08-17 10:23:13 +0200 | [diff] [blame] | 925 | goto cleanup; |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 928 | for (u = 0; u < modules->count; ++u) { |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 929 | submodules_count = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 930 | feature_count = 0; |
| 931 | mod = ((struct lyd_node *)modules->dnodes[u])->schema->module; |
| 932 | LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) { |
Michal Vasko | 5d9d597 | 2021-06-09 14:17:01 +0200 | [diff] [blame] | 933 | if (!iter->schema || (iter->schema->module != mod)) { |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 934 | /* ignore node from other schemas (augments) */ |
| 935 | continue; |
| 936 | } |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 937 | if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 938 | /* ignore empty nodes */ |
| 939 | continue; |
| 940 | } |
| 941 | if (!strcmp(iter->schema->name, "name")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 942 | (*result)[u].name = strdup(lyd_get_value(iter)); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 943 | } else if (!strcmp(iter->schema->name, "revision")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 944 | (*result)[u].revision = strdup(lyd_get_value(iter)); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 945 | } else if (!strcmp(iter->schema->name, "conformance-type")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 946 | (*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement"); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 947 | } else if (!strcmp(iter->schema->name, "feature")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 948 | (*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features); |
| 949 | if (!(*result)[u].features) { |
| 950 | ERRMEM; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 951 | free_module_info(*result); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 952 | *result = NULL; |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 953 | ret = -1; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 954 | goto cleanup; |
| 955 | } |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 956 | (*result)[u].features[feature_count] = strdup(lyd_get_value(iter)); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 957 | (*result)[u].features[feature_count + 1] = NULL; |
| 958 | ++feature_count; |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 959 | } else if (!strcmp(iter->schema->name, "submodule")) { |
| 960 | submodules_count++; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (submodules_count) { |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 965 | (*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules); |
| 966 | if (!(*result)[u].submodules) { |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 967 | ERRMEM; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 968 | free_module_info(*result); |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 969 | *result = NULL; |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 970 | ret = -1; |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 971 | goto cleanup; |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 972 | } else { |
| 973 | v = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 974 | LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) { |
| 975 | mod = modules->dnodes[u]->schema->module; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 976 | if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 977 | LY_LIST_FOR(lyd_child(iter), child) { |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 978 | if (mod != child->schema->module) { |
| 979 | continue; |
| 980 | } else if (!strcmp(child->schema->name, "name")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 981 | (*result)[u].submodules[v].name = strdup(lyd_get_value(child)); |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 982 | } else if (!strcmp(child->schema->name, "revision")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 983 | (*result)[u].submodules[v].revision = strdup(lyd_get_value(child)); |
Radek Krejci | 1a7efb4 | 2018-08-17 11:51:23 +0200 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 992 | cleanup: |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 993 | lyd_free_siblings(oper_data); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 994 | ly_set_free(modules, NULL); |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 995 | return ret; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 998 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 999 | * @brief Build server module info from received capabilities. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1000 | * |
| 1001 | * @param[in] cpblts Server capabilities. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1002 | * @param[out] result Server modules. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1003 | * @return 0 on success. |
| 1004 | * @return -1 on error. |
| 1005 | */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1006 | static int |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1007 | build_module_info_cpblts(char **cpblts, struct module_info **result) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1008 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1009 | uint32_t u, v, feature_count; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1010 | char *module_cpblt, *ptr, *ptr2; |
| 1011 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1012 | for (u = 0; cpblts[u]; ++u) {} |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1013 | (*result) = calloc(u + 1, sizeof **result); |
| 1014 | if (!(*result)) { |
Radek Krejci | c939050 | 2018-08-17 10:23:13 +0200 | [diff] [blame] | 1015 | ERRMEM; |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1016 | return -1; |
Radek Krejci | c939050 | 2018-08-17 10:23:13 +0200 | [diff] [blame] | 1017 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1018 | |
| 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 Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1032 | (*result)[v].name = strndup(ptr, ptr2 - ptr); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1033 | |
| 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 Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1042 | (*result)[v].revision = strndup(ptr, ptr2 - ptr); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* all are implemented since there is no better information in capabilities list */ |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1046 | (*result)[v].implemented = 1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1047 | |
| 1048 | /* get module's features */ |
| 1049 | ptr = strstr(module_cpblt, "features="); |
| 1050 | if (ptr) { |
| 1051 | ptr += 9; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1052 | feature_count = 0; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1053 | for (ptr2 = ptr; *ptr && *ptr != '&'; ++ptr) { |
| 1054 | if (*ptr == ',') { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1055 | (*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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1060 | ptr2 = ptr + 1; |
| 1061 | } |
| 1062 | } |
| 1063 | /* the last one */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1064 | (*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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1068 | } |
| 1069 | ++v; |
| 1070 | } |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1071 | |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1072 | return 0; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1075 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1076 | * @brief Fill client context based on server modules info. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1077 | * |
| 1078 | * @param[in] session NC session with the context to modify. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1079 | * @param[in] modules Server modules info. |
| 1080 | * @param[in] user_clb User callback for retrieving specific modules. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1081 | * @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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1086 | static int |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1087 | nc_ctx_fill(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1088 | int has_get_schema) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1089 | { |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1090 | int ret = -1; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1091 | struct lys_module *mod; |
| 1092 | uint32_t u; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1093 | |
| 1094 | for (u = 0; modules[u].name; ++u) { |
Michal Vasko | 488c7f5 | 2018-09-19 11:19:44 +0200 | [diff] [blame] | 1095 | /* skip import-only modules */ |
| 1096 | if (!modules[u].implemented) { |
| 1097 | continue; |
| 1098 | } |
| 1099 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1100 | /* we can continue even if it fails */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1101 | 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1103 | |
| 1104 | if (!mod) { |
| 1105 | if (session->status != NC_STATUS_RUNNING) { |
| 1106 | /* something bad heppened, discard the session */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1107 | ERR(session, "Invalid session, discarding."); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1108 | goto cleanup; |
| 1109 | } |
| 1110 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1111 | /* 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 Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1113 | modules[u].revision : "<latest>"); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1114 | session->flags |= NC_SESSION_CLIENT_NOT_STRICT; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1115 | } |
Michal Vasko | 60f6660 | 2017-10-17 13:52:18 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1118 | /* success */ |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1119 | ret = 0; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1120 | |
| 1121 | cleanup: |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1122 | return ret; |
| 1123 | } |
| 1124 | |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1125 | /** |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1126 | * @brief Fill client context with ietf-netconf module. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1127 | * |
| 1128 | * @param[in] session NC session with the context to modify. |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1129 | * @param[in] modules Server module info. |
| 1130 | * @param[in] user_clb User callback for retrieving specific modules. |
Michal Vasko | c088f98 | 2021-10-05 12:23:07 +0200 | [diff] [blame] | 1131 | * @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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1136 | static int |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1137 | nc_ctx_fill_ietf_netconf(struct nc_session *session, struct module_info *modules, ly_module_imp_clb user_clb, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1138 | void *user_data, int has_get_schema) |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1139 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1140 | uint32_t u; |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1141 | const char **features = NULL; |
| 1142 | struct ly_in *in; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1143 | struct lys_module *ietfnc; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1144 | |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1145 | /* 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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1154 | ERR(session, "Base NETCONF module not supported by the server."); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1155 | return -1; |
| 1156 | } |
| 1157 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1158 | ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf"); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1159 | 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 Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 1165 | if (!ietfnc) { |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1166 | 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 Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 1169 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1170 | } |
| 1171 | if (!ietfnc) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1172 | ERR(session, "Loading base NETCONF module failed."); |
Michal Vasko | 303263d | 2021-10-05 12:18:21 +0200 | [diff] [blame] | 1173 | return -1; |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1179 | /** |
| 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 | */ |
| 1188 | static int |
| 1189 | nc_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 | |
| 1228 | cleanup: |
| 1229 | lyd_free_siblings(oper_data); |
| 1230 | return rc; |
| 1231 | } |
| 1232 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1233 | int |
| 1234 | nc_ctx_check_and_fill(struct nc_session *session) |
| 1235 | { |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1236 | int i, get_schema_support = 0, yanglib_support = 0, xpath_support = 0, nmda_support = 0, ret = -1; |
Michal Vasko | cdeee43 | 2017-01-13 13:51:01 +0100 | [diff] [blame] | 1237 | ly_module_imp_clb old_clb = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1238 | void *old_data = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1239 | struct lys_module *mod = NULL; |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1240 | char *revision; |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1241 | struct module_info *server_modules = NULL, *sm = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1242 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1243 | assert(session->opts.client.cpblts && session->ctx); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1244 | |
Michal Vasko | a272e09 | 2023-07-10 14:34:03 +0200 | [diff] [blame] | 1245 | 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1250 | /* store the original user's callback, we will be switching between local search, get-schema and user callback */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1251 | old_clb = ly_ctx_get_module_imp_clb(session->ctx, &old_data); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1252 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1253 | /* switch off default searchpath to use only our callback integrating modifying searchpath algorithm to limit |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1254 | * modules only to those present on the server side */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1255 | ly_ctx_set_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1256 | |
| 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 Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1261 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1262 | if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?", 52)) { |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1263 | get_schema_support = 1 + i; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1264 | } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:yang-library:", 48)) { |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1265 | yanglib_support = 1 + i; |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1266 | } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability:xpath:1.0", 44)) { |
| 1267 | xpath_support = 1 + i; |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1268 | } else if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-nmda", 45)) { |
| 1269 | nmda_support = 1 + i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1270 | } |
| 1271 | } |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1272 | 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1276 | |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1277 | /* 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1280 | goto cleanup; |
| 1281 | } |
| 1282 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1283 | /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1284 | if (get_schema_support && lys_parse_mem(session->ctx, ietf_netconf_monitoring_2010_10_04_yang, LYS_IN_YANG, NULL)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1285 | WRN(session, "Loading NETCONF monitoring module failed, cannot use <get-schema>."); |
Michal Vasko | 8a4e146 | 2020-05-07 11:32:31 +0200 | [diff] [blame] | 1286 | get_schema_support = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */ |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1290 | if (nc_ctx_fill_ietf_netconf(session, server_modules, old_clb, old_data, get_schema_support)) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1291 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1294 | /* get correct version of ietf-yang-library into context */ |
| 1295 | if (yanglib_support) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1296 | /* use get-schema to get server's ietf-yang-library */ |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1297 | revision = strstr(session->opts.client.cpblts[yanglib_support - 1], "revision="); |
| 1298 | if (!revision) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1299 | WRN(session, "Loading NETCONF ietf-yang-library module failed, missing revision in NETCONF <hello> message."); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1300 | WRN(session, "Unable to automatically use <get-schema>."); |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1301 | yanglib_support = 0; |
| 1302 | } else { |
| 1303 | revision = strndup(&revision[9], 10); |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1304 | if (nc_ctx_load_module(session, "ietf-yang-library", revision, NULL, server_modules, old_clb, old_data, |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1305 | get_schema_support, &mod)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1306 | WRN(session, "Loading NETCONF ietf-yang-library module failed, unable to use it to learn all " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1307 | "the supported modules."); |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1308 | yanglib_support = 0; |
| 1309 | } |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1310 | if (strcmp(revision, "2019-01-04") >= 0) { |
| 1311 | /* we also need ietf-datastores to be implemented */ |
Michal Vasko | 5e32c40 | 2022-01-12 14:05:09 +0100 | [diff] [blame] | 1312 | if (nc_ctx_load_module(session, "ietf-datastores", NULL, NULL, server_modules, old_clb, old_data, |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1313 | get_schema_support, &mod)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1314 | WRN(session, "Loading NETCONF ietf-datastores module failed, unable to use yang-library " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1315 | "to learn all the supported modules."); |
Michal Vasko | 0d877f9 | 2020-04-02 13:52:43 +0200 | [diff] [blame] | 1316 | yanglib_support = 0; |
| 1317 | } |
| 1318 | } |
Radek Krejci | b1d250e | 2018-04-12 13:54:18 +0200 | [diff] [blame] | 1319 | free(revision); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1323 | /* 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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1330 | /* prepare structured information about server's modules */ |
Radek Krejci | 9aa1e35 | 2018-05-16 16:05:42 +0200 | [diff] [blame] | 1331 | if (yanglib_support) { |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1332 | if (build_module_info_yl(session, nmda_support, xpath_support, &sm)) { |
Radek Krejci | f063379 | 2018-08-20 15:12:09 +0200 | [diff] [blame] | 1333 | goto cleanup; |
| 1334 | } else if (!sm) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1335 | VRB(session, "Trying to use capabilities instead of ietf-yang-library data."); |
Radek Krejci | f063379 | 2018-08-20 15:12:09 +0200 | [diff] [blame] | 1336 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1337 | /* prefer yang-library information, currently we have it from capabilities used for getting correct |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1338 | * yang-library module */ |
| 1339 | free_module_info(server_modules); |
Radek Krejci | f063379 | 2018-08-20 15:12:09 +0200 | [diff] [blame] | 1340 | server_modules = sm; |
Radek Krejci | 235d8cb | 2018-08-17 14:04:32 +0200 | [diff] [blame] | 1341 | } |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1342 | } |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1343 | |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1344 | /* 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 Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1348 | if (nc_ctx_fill(session, server_modules, old_clb, old_data, get_schema_support)) { |
| 1349 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1352 | /* compile it */ |
| 1353 | if (ly_ctx_compile(session->ctx)) { |
| 1354 | goto cleanup; |
| 1355 | } |
| 1356 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1357 | /* set support for schema-mount, if possible */ |
Michal Vasko | d6c6f85 | 2022-12-14 15:37:21 +0100 | [diff] [blame] | 1358 | if (nc_ctx_schema_mount(session, nmda_support, xpath_support)) { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1359 | goto cleanup; |
| 1360 | } |
| 1361 | |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1362 | /* success */ |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1363 | ret = 0; |
| 1364 | |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 1365 | if (session->flags & NC_SESSION_CLIENT_NOT_STRICT) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1366 | WRN(session, "Some modules failed to be loaded, any data from these modules (and any other unknown) will " |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1367 | "be ignored."); |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1368 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1369 | |
| 1370 | cleanup: |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1371 | free_module_info(server_modules); |
Radek Krejci | 65ef6d5 | 2018-08-16 16:35:02 +0200 | [diff] [blame] | 1372 | |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1373 | /* set user callback back */ |
| 1374 | ly_ctx_set_module_imp_clb(session->ctx, old_clb, old_data); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1375 | ly_ctx_unset_options(session->ctx, LY_CTX_DISABLE_SEARCHDIRS); |
Michal Vasko | a9e9d45 | 2022-05-04 15:48:25 +0200 | [diff] [blame] | 1376 | ly_ctx_unset_options(session->ctx, LY_CTX_EXPLICIT_COMPILE); |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1377 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1378 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | API struct nc_session * |
| 1382 | nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx) |
| 1383 | { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1384 | struct nc_session *session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1385 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1386 | if (fdin < 0) { |
| 1387 | ERRARG("fdin"); |
| 1388 | return NULL; |
| 1389 | } else if (fdout < 0) { |
| 1390 | ERRARG("fdout"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1391 | return NULL; |
| 1392 | } |
| 1393 | |
| 1394 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1395 | session = nc_new_session(NC_CLIENT, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1396 | if (!session) { |
| 1397 | ERRMEM; |
| 1398 | return NULL; |
| 1399 | } |
| 1400 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1401 | |
| 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 Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1407 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1408 | goto fail; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1409 | } |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1410 | ctx = session->ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1411 | |
| 1412 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1413 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1414 | goto fail; |
| 1415 | } |
| 1416 | session->status = NC_STATUS_RUNNING; |
| 1417 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1418 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1419 | goto fail; |
| 1420 | } |
| 1421 | |
| 1422 | return session; |
| 1423 | |
| 1424 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1425 | nc_session_free(session, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1426 | return NULL; |
| 1427 | } |
| 1428 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1429 | API struct nc_session * |
| 1430 | nc_connect_unix(const char *address, struct ly_ctx *ctx) |
| 1431 | { |
| 1432 | struct nc_session *session = NULL; |
| 1433 | struct sockaddr_un sun; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1434 | struct passwd *pw, pw_buf; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1435 | char *username; |
| 1436 | int sock = -1; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1437 | char *buf = NULL; |
| 1438 | size_t buf_size = 0; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1439 | |
| 1440 | if (address == NULL) { |
| 1441 | ERRARG("address"); |
| 1442 | return NULL; |
| 1443 | } |
| 1444 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1445 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1446 | if (sock < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1447 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1448 | goto fail; |
| 1449 | } |
| 1450 | |
| 1451 | memset(&sun, 0, sizeof(sun)); |
| 1452 | sun.sun_family = AF_UNIX; |
| 1453 | snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address); |
| 1454 | |
| 1455 | if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1456 | ERR(NULL, "Cannot connect to sock server %s (%s)", address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1457 | goto fail; |
| 1458 | } |
| 1459 | |
| 1460 | if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1461 | ERR(NULL, "fcntl failed (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1462 | goto fail; |
| 1463 | } |
| 1464 | |
| 1465 | /* prepare session structure */ |
| 1466 | session = nc_new_session(NC_CLIENT, 0); |
| 1467 | if (!session) { |
| 1468 | ERRMEM; |
| 1469 | goto fail; |
| 1470 | } |
| 1471 | session->status = NC_STATUS_STARTING; |
| 1472 | |
| 1473 | /* transport specific data */ |
| 1474 | session->ti_type = NC_TI_UNIX; |
| 1475 | session->ti.unixsock.sock = sock; |
| 1476 | sock = -1; /* do not close sock in fail label anymore */ |
| 1477 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1478 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1479 | goto fail; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1480 | } |
Robin Jarry | 4e38e29 | 2019-10-15 17:14:14 +0200 | [diff] [blame] | 1481 | ctx = session->ctx; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1482 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1483 | session->path = strdup(address); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1484 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1485 | pw = nc_getpw(geteuid(), NULL, &pw_buf, &buf, &buf_size); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1486 | if (!pw) { |
| 1487 | ERR(NULL, "Failed to find username for UID %u.", (unsigned int)geteuid()); |
| 1488 | goto fail; |
| 1489 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1490 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1491 | free(buf); |
| 1492 | if (!username) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1493 | ERRMEM; |
| 1494 | goto fail; |
| 1495 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1496 | session->username = username; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1497 | |
| 1498 | /* NETCONF handshake */ |
| 1499 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
| 1500 | goto fail; |
| 1501 | } |
| 1502 | session->status = NC_STATUS_RUNNING; |
| 1503 | |
| 1504 | if (nc_ctx_check_and_fill(session) == -1) { |
| 1505 | goto fail; |
| 1506 | } |
| 1507 | |
| 1508 | return session; |
| 1509 | |
| 1510 | fail: |
| 1511 | nc_session_free(session, NULL); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1512 | if (sock >= 0) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1513 | close(sock); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1514 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1515 | return NULL; |
| 1516 | } |
| 1517 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1518 | /** |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1519 | * @brief Convert socket IP address to string. |
| 1520 | * |
| 1521 | * @param[in] saddr Sockaddr to convert. |
| 1522 | * @param[out] str_ip String IP address. |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1523 | * @param[out] port Optional port. |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1524 | * @return 0 on success. |
| 1525 | * @return -1 on error. |
| 1526 | */ |
| 1527 | static int |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1528 | nc_saddr2str(const struct sockaddr *saddr, char **str_ip, uint16_t *port) |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1529 | { |
| 1530 | void *addr; |
| 1531 | socklen_t str_len; |
| 1532 | |
| 1533 | assert((saddr->sa_family == AF_INET) || (saddr->sa_family == AF_INET6)); |
| 1534 | |
| 1535 | str_len = (saddr->sa_family == AF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN; |
| 1536 | *str_ip = malloc(str_len); |
| 1537 | if (!*str_ip) { |
| 1538 | ERRMEM; |
| 1539 | return -1; |
| 1540 | } |
| 1541 | |
| 1542 | if (saddr->sa_family == AF_INET) { |
Michal Vasko | c0f85ca | 2023-03-27 10:19:00 +0200 | [diff] [blame] | 1543 | addr = &((struct sockaddr_in *)saddr)->sin_addr; |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1544 | if (port) { |
| 1545 | *port = ntohs(((struct sockaddr_in *)saddr)->sin_port); |
| 1546 | } |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1547 | } else { |
Michal Vasko | c0f85ca | 2023-03-27 10:19:00 +0200 | [diff] [blame] | 1548 | addr = &((struct sockaddr_in6 *)saddr)->sin6_addr; |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1549 | if (port) { |
| 1550 | *port = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); |
| 1551 | } |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1552 | } |
| 1553 | if (!inet_ntop(saddr->sa_family, addr, *str_ip, str_len)) { |
| 1554 | ERR(NULL, "Converting host to IP address failed (%s).", strerror(errno)); |
| 1555 | free(*str_ip); |
| 1556 | return -1; |
| 1557 | } |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | /** |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1563 | * @brief Try to connect a socket, optionally a pending one from a previous attempt. |
| 1564 | * |
| 1565 | * @param[in] timeout_ms Timeout in ms to wait for the connection to be fully established, -1 to block. |
| 1566 | * @param[in,out] sock_pending Optional previously created socked that was not fully connected yet. If provided and |
| 1567 | * connected, is set to -1. |
| 1568 | * @param[in] res Addrinfo resource to use when creating a new socket. |
| 1569 | * @param[in] ka Keepalives to set. |
| 1570 | * @return Connected socket or -1 on error. |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1571 | */ |
| 1572 | static int |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1573 | sock_connect(int timeout_ms, int *sock_pending, struct addrinfo *res, struct nc_keepalives *ka) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1574 | { |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1575 | int flags, ret, error; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1576 | int sock = -1; |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1577 | struct pollfd fds = {0}; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1578 | socklen_t len = sizeof(int); |
Michal Vasko | 7e7f99d | 2019-09-12 13:49:46 +0200 | [diff] [blame] | 1579 | uint16_t port; |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1580 | char *str; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1581 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1582 | if (sock_pending && (*sock_pending != -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1583 | VRB(NULL, "Trying to connect the pending socket %d.", *sock_pending); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1584 | sock = *sock_pending; |
| 1585 | } else { |
| 1586 | assert(res); |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1587 | if (nc_saddr2str(res->ai_addr, &str, &port)) { |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1588 | return -1; |
| 1589 | } |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1590 | VRB(NULL, "Trying to connect via %s to %s:%u.", (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", str, port); |
| 1591 | free(str); |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1592 | |
| 1593 | /* connect to a server */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1594 | sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 1595 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1596 | ERR(NULL, "Socket could not be created (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1597 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1598 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1599 | /* make the socket non-blocking */ |
| 1600 | if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1601 | ERR(NULL, "fcntl() failed (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1602 | goto cleanup; |
Michal Vasko | 7d965dc | 2018-03-12 14:39:23 +0100 | [diff] [blame] | 1603 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1604 | /* non-blocking connect! */ |
| 1605 | if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { |
| 1606 | if (errno != EINPROGRESS) { |
| 1607 | /* network connection failed, try another resource */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1608 | ERR(NULL, "connect() failed (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1609 | goto cleanup; |
| 1610 | } |
| 1611 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1612 | } |
Michal Vasko | 7d965dc | 2018-03-12 14:39:23 +0100 | [diff] [blame] | 1613 | |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1614 | fds.fd = sock; |
| 1615 | fds.events = POLLOUT; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1616 | |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1617 | /* wait until we can write data to the socket */ |
| 1618 | ret = poll(&fds, 1, timeout_ms); |
| 1619 | if (ret == -1) { |
| 1620 | /* error */ |
| 1621 | ERR(NULL, "poll() failed (%s).", strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1622 | goto cleanup; |
Michal Vasko | b933667 | 2023-10-11 09:27:30 +0200 | [diff] [blame] | 1623 | } else if (ret == 0) { |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1624 | /* there was a timeout */ |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1625 | VRB(NULL, "Timed out after %d ms (%s).", timeout_ms, strerror(errno)); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1626 | if (sock_pending) { |
| 1627 | /* no sock-close, we'll try it again */ |
| 1628 | *sock_pending = sock; |
| 1629 | } else { |
| 1630 | close(sock); |
| 1631 | } |
| 1632 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1633 | } |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1634 | |
| 1635 | /* check the usability of the socket */ |
Michal Vasko | 5e8d019 | 2019-06-24 19:19:49 +0200 | [diff] [blame] | 1636 | error = 0; |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1637 | if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1638 | ERR(NULL, "getsockopt() failed (%s).", strerror(errno)); |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1639 | goto cleanup; |
| 1640 | } |
Michal Vasko | e27ab4e | 2020-07-27 11:10:58 +0200 | [diff] [blame] | 1641 | if (error) { |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1642 | /* network connection failed, try another resource */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1643 | VRB(NULL, "getsockopt() error (%s).", strerror(error)); |
Radek Krejci | 782041a | 2018-08-20 10:09:45 +0200 | [diff] [blame] | 1644 | errno = error; |
| 1645 | goto cleanup; |
| 1646 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 1647 | |
| 1648 | /* enable keep-alive */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1649 | if (nc_sock_configure_keepalive(sock, ka)) { |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 1650 | goto cleanup; |
| 1651 | } |
| 1652 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1653 | /* connected */ |
| 1654 | if (sock_pending) { |
| 1655 | *sock_pending = -1; |
| 1656 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1657 | return sock; |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 1658 | |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1659 | cleanup: |
| 1660 | if (sock_pending) { |
| 1661 | *sock_pending = -1; |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 1662 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1663 | close(sock); |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 1664 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1665 | } |
| 1666 | |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1667 | int |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1668 | nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepalives *ka, int *sock_pending, char **ip_host) |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1669 | { |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1670 | int i, opt; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1671 | int sock = sock_pending ? *sock_pending : -1; |
Michal Vasko | 0be8569 | 2021-03-02 08:04:57 +0100 | [diff] [blame] | 1672 | struct addrinfo hints, *res_list = NULL, *res; |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1673 | char port_s[6]; /* length of string representation of short int */ |
| 1674 | struct sockaddr_storage saddr; |
| 1675 | socklen_t addr_len = sizeof saddr; |
| 1676 | |
| 1677 | *ip_host = NULL; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1678 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1679 | DBG(NULL, "nc_sock_connect(%s, %u, %d, %d)", host, port, timeout_ms, sock); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1680 | |
| 1681 | /* no pending socket */ |
| 1682 | if (sock == -1) { |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1683 | /* connect to a server */ |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1684 | snprintf(port_s, 6, "%u", port); |
| 1685 | memset(&hints, 0, sizeof hints); |
| 1686 | hints.ai_family = AF_UNSPEC; |
| 1687 | hints.ai_socktype = SOCK_STREAM; |
| 1688 | hints.ai_protocol = IPPROTO_TCP; |
| 1689 | i = getaddrinfo(host, port_s, &hints, &res_list); |
| 1690 | if (i != 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1691 | ERR(NULL, "Unable to translate the host address (%s).", gai_strerror(i)); |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1692 | goto error; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | for (res = res_list; res != NULL; res = res->ai_next) { |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1696 | sock = sock_connect(timeout_ms, sock_pending, res, ka); |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1697 | if (sock == -1) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1698 | if (!sock_pending || (*sock_pending == -1)) { |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1699 | /* try the next resource */ |
| 1700 | continue; |
| 1701 | } else { |
| 1702 | /* timeout, keep pending socket */ |
Michal Vasko | 0be8569 | 2021-03-02 08:04:57 +0100 | [diff] [blame] | 1703 | break; |
Michal Vasko | 2af7c38 | 2020-07-27 14:21:35 +0200 | [diff] [blame] | 1704 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1705 | } |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1706 | VRB(NULL, "Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4"); |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1707 | |
| 1708 | opt = 1; |
| 1709 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1710 | ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1711 | goto error; |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 1712 | } |
| 1713 | |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1714 | if (nc_saddr2str(res->ai_addr, ip_host, NULL)) { |
Michal Vasko | 7b1c4c2 | 2023-03-24 13:27:40 +0100 | [diff] [blame] | 1715 | goto error; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1716 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1717 | break; |
| 1718 | } |
| 1719 | freeaddrinfo(res_list); |
| 1720 | |
| 1721 | } else { |
| 1722 | /* try to get a connection with the pending socket */ |
| 1723 | assert(sock_pending); |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 1724 | sock = sock_connect(timeout_ms, sock_pending, NULL, ka); |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1725 | |
| 1726 | if (sock > 0) { |
Barbaros Tokaoglu | 96da591 | 2023-06-19 18:57:05 +0300 | [diff] [blame] | 1727 | if (getpeername(sock, (struct sockaddr *)&saddr, &addr_len)) { |
| 1728 | ERR(NULL, "getpeername failed (%s).", strerror(errno)); |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1729 | goto error; |
| 1730 | } |
| 1731 | |
Michal Vasko | c07d976 | 2023-03-27 10:32:18 +0200 | [diff] [blame] | 1732 | if (nc_saddr2str((struct sockaddr *)&saddr, ip_host, NULL)) { |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1733 | goto error; |
| 1734 | } |
Tie.Liao | 9bca73d | 2023-03-23 17:25:00 +0100 | [diff] [blame] | 1735 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | return sock; |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1739 | |
| 1740 | error: |
Michal Vasko | 0be8569 | 2021-03-02 08:04:57 +0100 | [diff] [blame] | 1741 | if (res_list) { |
| 1742 | freeaddrinfo(res_list); |
| 1743 | } |
Michal Vasko | cb84663 | 2019-12-13 15:12:45 +0100 | [diff] [blame] | 1744 | if (sock != -1) { |
| 1745 | close(sock); |
| 1746 | } |
| 1747 | if (sock_pending) { |
| 1748 | *sock_pending = -1; |
| 1749 | } |
| 1750 | return -1; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1753 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1754 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1755 | int |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1756 | nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hostname, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1757 | { |
| 1758 | int sock; |
| 1759 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1760 | if (!address) { |
| 1761 | ERRARG("address"); |
| 1762 | return -1; |
| 1763 | } else if (!port) { |
| 1764 | ERRARG("port"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1765 | return -1; |
| 1766 | } |
| 1767 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1768 | sock = nc_sock_listen_inet(address, port, &client_opts.ka); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1769 | if (sock == -1) { |
| 1770 | return -1; |
| 1771 | } |
| 1772 | |
| 1773 | ++client_opts.ch_bind_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1774 | client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds); |
| 1775 | if (!client_opts.ch_binds) { |
| 1776 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 1777 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1778 | return -1; |
| 1779 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1780 | |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1781 | client_opts.ch_binds_aux = nc_realloc(client_opts.ch_binds_aux, client_opts.ch_bind_count * sizeof *client_opts.ch_binds_aux); |
| 1782 | if (!client_opts.ch_binds_aux) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1783 | ERRMEM; |
| 1784 | close(sock); |
| 1785 | return -1; |
| 1786 | } |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1787 | client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].ti = ti; |
| 1788 | client_opts.ch_binds_aux[client_opts.ch_bind_count - 1].hostname = hostname ? strdup(hostname) : NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1789 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1790 | client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address); |
| 1791 | client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port; |
| 1792 | client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 1793 | client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1794 | |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | int |
| 1799 | nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 1800 | { |
| 1801 | uint32_t i; |
| 1802 | int ret = -1; |
| 1803 | |
| 1804 | if (!address && !port && !ti) { |
| 1805 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 1806 | close(client_opts.ch_binds[i].sock); |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1807 | free(client_opts.ch_binds[i].address); |
| 1808 | |
| 1809 | free(client_opts.ch_binds_aux[i].hostname); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1810 | |
| 1811 | ret = 0; |
| 1812 | } |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1813 | client_opts.ch_bind_count = 0; |
| 1814 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1815 | free(client_opts.ch_binds); |
| 1816 | client_opts.ch_binds = NULL; |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1817 | |
| 1818 | free(client_opts.ch_binds_aux); |
| 1819 | client_opts.ch_binds_aux = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1820 | } else { |
| 1821 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1822 | if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) && |
| 1823 | (!port || (client_opts.ch_binds[i].port == port)) && |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1824 | (!ti || (client_opts.ch_binds_aux[i].ti == ti))) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1825 | close(client_opts.ch_binds[i].sock); |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1826 | free(client_opts.ch_binds[i].address); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1827 | |
| 1828 | --client_opts.ch_bind_count; |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1829 | if (!client_opts.ch_bind_count) { |
| 1830 | free(client_opts.ch_binds); |
| 1831 | client_opts.ch_binds = NULL; |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1832 | |
| 1833 | free(client_opts.ch_binds_aux); |
| 1834 | client_opts.ch_binds_aux = NULL; |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1835 | } else if (i < client_opts.ch_bind_count) { |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1836 | memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], |
| 1837 | sizeof *client_opts.ch_binds); |
| 1838 | |
| 1839 | memcpy(&client_opts.ch_binds_aux[i], &client_opts.ch_binds_aux[client_opts.ch_bind_count], |
| 1840 | sizeof *client_opts.ch_binds_aux); |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1841 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1842 | |
| 1843 | ret = 0; |
| 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | return ret; |
| 1849 | } |
| 1850 | |
| 1851 | API int |
| 1852 | nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session) |
| 1853 | { |
| 1854 | int sock; |
| 1855 | char *host = NULL; |
| 1856 | uint16_t port, idx; |
| 1857 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1858 | if (!client_opts.ch_binds) { |
| 1859 | ERRINIT; |
| 1860 | return -1; |
| 1861 | } else if (!session) { |
| 1862 | ERRARG("session"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1863 | return -1; |
| 1864 | } |
| 1865 | |
| 1866 | sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx); |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 1867 | if (sock < 1) { |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1868 | free(host); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1869 | return sock; |
| 1870 | } |
| 1871 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1872 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1873 | if (client_opts.ch_binds_aux[idx].ti == NC_TI_LIBSSH) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1874 | *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1875 | } else |
| 1876 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1877 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1878 | if (client_opts.ch_binds_aux[idx].ti == NC_TI_OPENSSL) { |
| 1879 | *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT, |
| 1880 | client_opts.ch_binds_aux[idx].hostname); |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1881 | } else |
| 1882 | #endif |
| 1883 | { |
Michal Vasko | fee717c | 2016-02-01 13:25:43 +0100 | [diff] [blame] | 1884 | close(sock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1885 | *session = NULL; |
| 1886 | } |
| 1887 | |
| 1888 | free(host); |
| 1889 | |
| 1890 | if (!(*session)) { |
| 1891 | return -1; |
| 1892 | } |
| 1893 | |
| 1894 | return 1; |
| 1895 | } |
| 1896 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1897 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1898 | |
Michal Vasko | 1b2ddc9 | 2017-05-24 08:59:49 +0200 | [diff] [blame] | 1899 | API const char * const * |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1900 | nc_session_get_cpblts(const struct nc_session *session) |
| 1901 | { |
| 1902 | if (!session) { |
| 1903 | ERRARG("session"); |
| 1904 | return NULL; |
| 1905 | } |
| 1906 | |
Michal Vasko | 1b2ddc9 | 2017-05-24 08:59:49 +0200 | [diff] [blame] | 1907 | return (const char * const *)session->opts.client.cpblts; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | API const char * |
| 1911 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 1912 | { |
| 1913 | int i, len; |
| 1914 | |
| 1915 | if (!session) { |
| 1916 | ERRARG("session"); |
| 1917 | return NULL; |
| 1918 | } else if (!capab) { |
| 1919 | ERRARG("capab"); |
| 1920 | return NULL; |
| 1921 | } |
| 1922 | |
| 1923 | len = strlen(capab); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1924 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
| 1925 | if (!strncmp(session->opts.client.cpblts[i], capab, len)) { |
| 1926 | return session->opts.client.cpblts[i]; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1933 | API int |
| 1934 | nc_session_ntf_thread_running(const struct nc_session *session) |
| 1935 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1936 | if (!session || (session->side != NC_CLIENT)) { |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1937 | ERRARG("session"); |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 1941 | return ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running); |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1944 | API void |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1945 | nc_client_init(void) |
| 1946 | { |
| 1947 | nc_init(); |
| 1948 | } |
| 1949 | |
| 1950 | API void |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1951 | nc_client_destroy(void) |
| 1952 | { |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 1953 | nc_client_set_schema_searchpath(NULL); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1954 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 1955 | nc_client_ch_del_bind(NULL, 0, 0); |
| 1956 | #endif |
| 1957 | #ifdef NC_ENABLED_SSH |
| 1958 | nc_client_ssh_destroy_opts(); |
| 1959 | #endif |
| 1960 | #ifdef NC_ENABLED_TLS |
| 1961 | nc_client_tls_destroy_opts(); |
| 1962 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1963 | nc_destroy(); |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1966 | static NC_MSG_TYPE |
| 1967 | recv_reply_check_msgid(struct nc_session *session, const struct lyd_node *envp, uint64_t msgid) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1968 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1969 | char *ptr; |
| 1970 | struct lyd_attr *attr; |
| 1971 | uint64_t cur_msgid; |
| 1972 | |
| 1973 | assert(envp && !envp->schema); |
| 1974 | |
| 1975 | /* find the message-id attribute */ |
| 1976 | LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) { |
| 1977 | if (!strcmp(attr->name.name, "message-id")) { |
| 1978 | break; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | if (!attr) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1983 | ERR(session, "Received a <rpc-reply> without a message-id."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1984 | return NC_MSG_REPLY_ERR_MSGID; |
| 1985 | } |
| 1986 | |
| 1987 | cur_msgid = strtoul(attr->value, &ptr, 10); |
| 1988 | if (cur_msgid != msgid) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1989 | ERR(session, "Received a <rpc-reply> with an unexpected message-id %" PRIu64 " (expected %" PRIu64 ").", |
| 1990 | cur_msgid, msgid); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1991 | return NC_MSG_REPLY_ERR_MSGID; |
| 1992 | } |
| 1993 | |
| 1994 | return NC_MSG_REPLY; |
| 1995 | } |
| 1996 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 1997 | /** |
| 1998 | * @brief Used to roughly estimate the type of the message, does not actually parse or verify it. |
| 1999 | * |
| 2000 | * @param[in] session NETCONF session used to send error messages. |
| 2001 | * @param[in] msg Message to check for type. |
| 2002 | * @return NC_MSG_REPLY If format roughly matches a rpc-reply; |
| 2003 | * @return NC_MSG_NOTIF If format roughly matches a notification; |
| 2004 | * @return NC_MSG_ERROR If format is malformed or unrecognized. |
| 2005 | */ |
| 2006 | static NC_MSG_TYPE |
| 2007 | get_msg_type(struct nc_session *session, struct ly_in *msg) |
| 2008 | { |
| 2009 | const char *str, *end; |
| 2010 | |
| 2011 | str = ly_in_memory(msg, NULL); |
| 2012 | |
| 2013 | while (*str) { |
| 2014 | /* Skip whitespaces */ |
| 2015 | while (isspace(*str)) { |
| 2016 | str++; |
| 2017 | } |
| 2018 | |
| 2019 | if (*str == '<') { |
| 2020 | str++; |
| 2021 | if (!strncmp(str, "!--", 3)) { |
| 2022 | /* Skip comments */ |
| 2023 | end = "-->"; |
| 2024 | str = strstr(str, end); |
| 2025 | } else if (!strncmp(str, "?xml", 4)) { |
| 2026 | /* Skip xml declaration */ |
| 2027 | end = "?>"; |
| 2028 | str = strstr(str, end); |
| 2029 | } else if (!strncmp(str, "rpc-reply", 9)) { |
| 2030 | return NC_MSG_REPLY; |
| 2031 | } else if (!strncmp(str, "notification", 12)) { |
| 2032 | return NC_MSG_NOTIF; |
| 2033 | } else { |
| 2034 | ERR(session, "Unknown xml element '%.10s'.", str); |
| 2035 | return NC_MSG_ERROR; |
| 2036 | } |
| 2037 | if (!str) { |
| 2038 | /* No matching ending tag found */ |
| 2039 | ERR(session, "No matching ending tag '%s' found in xml message.", end); |
| 2040 | return NC_MSG_ERROR; |
| 2041 | } |
| 2042 | str += strlen(end); |
| 2043 | } else { |
| 2044 | /* Not a valid xml */ |
| 2045 | ERR(session, "Unexpected character '%c' in xml message.", *str); |
| 2046 | return NC_MSG_ERROR; |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | /* Unexpected end of message */ |
| 2051 | ERR(session, "Unexpected end of xml message."); |
| 2052 | return NC_MSG_ERROR; |
| 2053 | } |
| 2054 | |
| 2055 | /** |
| 2056 | * @brief Function to receive either replies or notifications. |
| 2057 | * |
| 2058 | * @param[in] session NETCONF session from which this function receives messages. |
| 2059 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite. |
| 2060 | * @param[in] expected Type of the message the caller desired. |
| 2061 | * @param[out] message If receiving a message succeeded this is the message, NULL otherwise. |
| 2062 | * @return NC_MSG_REPLY If a rpc-reply was received; |
| 2063 | * @return NC_MSG_NOTIF If a notification was received; |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 2064 | * @return NC_MSG_ERROR If any error occurred; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2065 | * @return NC_MSG_WOULDBLOCK If the timeout was reached. |
| 2066 | */ |
| 2067 | static NC_MSG_TYPE |
| 2068 | recv_msg(struct nc_session *session, int timeout, NC_MSG_TYPE expected, struct ly_in **message) |
| 2069 | { |
| 2070 | struct nc_msg_cont **cont_ptr; |
| 2071 | struct ly_in *msg = NULL; |
| 2072 | struct nc_msg_cont *cont, *prev; |
| 2073 | NC_MSG_TYPE ret = NC_MSG_ERROR; |
| 2074 | int r; |
| 2075 | |
| 2076 | *message = NULL; |
| 2077 | |
| 2078 | /* MSGS LOCK */ |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2079 | r = nc_session_client_msgs_lock(session, &timeout, __func__); |
| 2080 | if (!r) { |
| 2081 | ret = NC_MSG_WOULDBLOCK; |
| 2082 | goto cleanup; |
| 2083 | } else if (r == -1) { |
| 2084 | ret = NC_MSG_ERROR; |
| 2085 | goto cleanup; |
| 2086 | } |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2087 | |
| 2088 | /* Find the expected message in the buffer */ |
| 2089 | prev = NULL; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2090 | for (cont = session->opts.client.msgs; cont && (cont->type != expected); cont = cont->next) { |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2091 | prev = cont; |
| 2092 | } |
| 2093 | |
| 2094 | if (cont) { |
| 2095 | /* Remove found message from buffer */ |
| 2096 | if (prev) { |
| 2097 | prev->next = cont->next; |
| 2098 | } else { |
| 2099 | session->opts.client.msgs = cont->next; |
| 2100 | } |
| 2101 | |
| 2102 | /* Use the buffer message */ |
| 2103 | ret = cont->type; |
| 2104 | msg = cont->msg; |
| 2105 | free(cont); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2106 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | /* Read a message from the wire */ |
| 2110 | r = nc_read_msg_poll_io(session, timeout, &msg); |
| 2111 | if (!r) { |
| 2112 | ret = NC_MSG_WOULDBLOCK; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2113 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2114 | } else if (r == -1) { |
| 2115 | ret = NC_MSG_ERROR; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2116 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | /* Basic check to determine message type */ |
| 2120 | ret = get_msg_type(session, msg); |
| 2121 | if (ret == NC_MSG_ERROR) { |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2122 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* If received a message of different type store it in the buffer */ |
| 2126 | if (ret != expected) { |
| 2127 | cont_ptr = &session->opts.client.msgs; |
| 2128 | while (*cont_ptr) { |
| 2129 | cont_ptr = &((*cont_ptr)->next); |
| 2130 | } |
| 2131 | *cont_ptr = malloc(sizeof **cont_ptr); |
| 2132 | if (!*cont_ptr) { |
| 2133 | ERRMEM; |
| 2134 | ret = NC_MSG_ERROR; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2135 | goto cleanup_unlock; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2136 | } |
| 2137 | (*cont_ptr)->msg = msg; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2138 | msg = NULL; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2139 | (*cont_ptr)->type = ret; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2140 | (*cont_ptr)->next = NULL; |
| 2141 | } |
| 2142 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2143 | cleanup_unlock: |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2144 | /* MSGS UNLOCK */ |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2145 | nc_session_client_msgs_unlock(session, __func__); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2146 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 2147 | cleanup: |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2148 | if (ret == expected) { |
| 2149 | *message = msg; |
| 2150 | } else { |
| 2151 | ly_in_free(msg, 1); |
| 2152 | } |
| 2153 | return ret; |
| 2154 | } |
| 2155 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2156 | static NC_MSG_TYPE |
| 2157 | recv_reply(struct nc_session *session, int timeout, struct lyd_node *op, uint64_t msgid, struct lyd_node **envp) |
| 2158 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2159 | LY_ERR lyrc; |
| 2160 | struct ly_in *msg = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2161 | NC_MSG_TYPE ret = NC_MSG_ERROR; |
| 2162 | |
| 2163 | assert(op && (op->schema->nodetype & (LYS_RPC | LYS_ACTION))); |
| 2164 | |
| 2165 | *envp = NULL; |
| 2166 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2167 | /* Receive messages until a rpc-reply is found or a timeout or error reached */ |
| 2168 | ret = recv_msg(session, timeout, NC_MSG_REPLY, &msg); |
| 2169 | if (ret != NC_MSG_REPLY) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2170 | goto cleanup; |
| 2171 | } |
| 2172 | |
| 2173 | /* parse */ |
| 2174 | lyrc = lyd_parse_op(NULL, op, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, envp, NULL); |
| 2175 | if (!lyrc) { |
| 2176 | ret = recv_reply_check_msgid(session, *envp, msgid); |
| 2177 | goto cleanup; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2178 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2179 | ERR(session, "Received an invalid message (%s).", ly_errmsg(LYD_CTX(op))); |
Michal Vasko | 9a10805 | 2022-04-01 12:06:54 +0200 | [diff] [blame] | 2180 | lyd_free_tree(*envp); |
| 2181 | *envp = NULL; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2182 | ret = NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2183 | goto cleanup; |
| 2184 | } |
| 2185 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2186 | cleanup: |
| 2187 | ly_in_free(msg, 1); |
| 2188 | return ret; |
| 2189 | } |
| 2190 | |
| 2191 | static int |
| 2192 | recv_reply_dup_rpc(struct nc_session *session, struct nc_rpc *rpc, struct lyd_node **op) |
| 2193 | { |
Michal Vasko | 143aa14 | 2021-10-01 15:31:48 +0200 | [diff] [blame] | 2194 | LY_ERR lyrc = LY_SUCCESS; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2195 | struct nc_rpc_act_generic *rpc_gen; |
| 2196 | struct ly_in *in; |
| 2197 | struct lyd_node *tree, *op2; |
| 2198 | const struct lys_module *mod; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2199 | const char *module_name = NULL, *rpc_name = NULL, *module_check = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2200 | |
| 2201 | switch (rpc->type) { |
| 2202 | case NC_RPC_ACT_GENERIC: |
| 2203 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
| 2204 | if (rpc_gen->has_data) { |
| 2205 | tree = rpc_gen->content.data; |
| 2206 | |
| 2207 | /* find the operation node */ |
| 2208 | lyrc = LY_EINVAL; |
| 2209 | LYD_TREE_DFS_BEGIN(tree, op2) { |
| 2210 | if (op2->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 2211 | lyrc = lyd_dup_single(op2, NULL, 0, op); |
| 2212 | break; |
| 2213 | } |
| 2214 | LYD_TREE_DFS_END(tree, op2); |
| 2215 | } |
| 2216 | } else { |
| 2217 | ly_in_new_memory(rpc_gen->content.xml_str, &in); |
| 2218 | lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op2); |
| 2219 | ly_in_free(in, 0); |
| 2220 | if (lyrc) { |
Michal Vasko | 9a10805 | 2022-04-01 12:06:54 +0200 | [diff] [blame] | 2221 | lyd_free_tree(tree); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2222 | return -1; |
| 2223 | } |
| 2224 | |
| 2225 | /* we want just the operation node */ |
| 2226 | lyrc = lyd_dup_single(op2, NULL, 0, op); |
| 2227 | |
| 2228 | lyd_free_tree(tree); |
| 2229 | } |
| 2230 | break; |
| 2231 | case NC_RPC_GETCONFIG: |
| 2232 | module_name = "ietf-netconf"; |
| 2233 | rpc_name = "get-config"; |
| 2234 | break; |
| 2235 | case NC_RPC_EDIT: |
| 2236 | module_name = "ietf-netconf"; |
| 2237 | rpc_name = "edit-config"; |
| 2238 | break; |
| 2239 | case NC_RPC_COPY: |
| 2240 | module_name = "ietf-netconf"; |
| 2241 | rpc_name = "copy-config"; |
| 2242 | break; |
| 2243 | case NC_RPC_DELETE: |
| 2244 | module_name = "ietf-netconf"; |
| 2245 | rpc_name = "delete-config"; |
| 2246 | break; |
| 2247 | case NC_RPC_LOCK: |
| 2248 | module_name = "ietf-netconf"; |
| 2249 | rpc_name = "lock"; |
| 2250 | break; |
| 2251 | case NC_RPC_UNLOCK: |
| 2252 | module_name = "ietf-netconf"; |
| 2253 | rpc_name = "unlock"; |
| 2254 | break; |
| 2255 | case NC_RPC_GET: |
| 2256 | module_name = "ietf-netconf"; |
| 2257 | rpc_name = "get"; |
| 2258 | break; |
| 2259 | case NC_RPC_KILL: |
| 2260 | module_name = "ietf-netconf"; |
| 2261 | rpc_name = "kill-session"; |
| 2262 | break; |
| 2263 | case NC_RPC_COMMIT: |
| 2264 | module_name = "ietf-netconf"; |
| 2265 | rpc_name = "commit"; |
| 2266 | break; |
| 2267 | case NC_RPC_DISCARD: |
| 2268 | module_name = "ietf-netconf"; |
| 2269 | rpc_name = "discard-changes"; |
| 2270 | break; |
| 2271 | case NC_RPC_CANCEL: |
| 2272 | module_name = "ietf-netconf"; |
| 2273 | rpc_name = "cancel-commit"; |
| 2274 | break; |
| 2275 | case NC_RPC_VALIDATE: |
| 2276 | module_name = "ietf-netconf"; |
| 2277 | rpc_name = "validate"; |
| 2278 | break; |
| 2279 | case NC_RPC_GETSCHEMA: |
| 2280 | module_name = "ietf-netconf-monitoring"; |
| 2281 | rpc_name = "get-schema"; |
| 2282 | break; |
| 2283 | case NC_RPC_SUBSCRIBE: |
| 2284 | module_name = "notifications"; |
Michal Vasko | eed893d | 2021-05-25 17:11:08 +0200 | [diff] [blame] | 2285 | rpc_name = "create-subscription"; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2286 | break; |
| 2287 | case NC_RPC_GETDATA: |
| 2288 | module_name = "ietf-netconf-nmda"; |
| 2289 | rpc_name = "get-data"; |
| 2290 | break; |
| 2291 | case NC_RPC_EDITDATA: |
| 2292 | module_name = "ietf-netconf-nmda"; |
| 2293 | rpc_name = "edit-data"; |
| 2294 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2295 | case NC_RPC_ESTABLISHSUB: |
| 2296 | module_name = "ietf-subscribed-notifications"; |
| 2297 | rpc_name = "establish-subscription"; |
| 2298 | break; |
| 2299 | case NC_RPC_MODIFYSUB: |
| 2300 | module_name = "ietf-subscribed-notifications"; |
| 2301 | rpc_name = "modify-subscription"; |
| 2302 | break; |
| 2303 | case NC_RPC_DELETESUB: |
| 2304 | module_name = "ietf-subscribed-notifications"; |
| 2305 | rpc_name = "delete-subscription"; |
| 2306 | break; |
| 2307 | case NC_RPC_KILLSUB: |
| 2308 | module_name = "ietf-subscribed-notifications"; |
| 2309 | rpc_name = "kill-subscription"; |
| 2310 | break; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2311 | case NC_RPC_ESTABLISHPUSH: |
| 2312 | module_name = "ietf-subscribed-notifications"; |
| 2313 | rpc_name = "establish-subscription"; |
| 2314 | module_check = "ietf-yang-push"; |
| 2315 | break; |
| 2316 | case NC_RPC_MODIFYPUSH: |
| 2317 | module_name = "ietf-subscribed-notifications"; |
| 2318 | rpc_name = "modify-subscription"; |
| 2319 | module_check = "ietf-yang-push"; |
| 2320 | break; |
| 2321 | case NC_RPC_RESYNCSUB: |
| 2322 | module_name = "ietf-yang-push"; |
| 2323 | rpc_name = "resync-subscription"; |
| 2324 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2325 | case NC_RPC_UNKNOWN: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2326 | lyrc = LY_EINT; |
| 2327 | break; |
| 2328 | } |
| 2329 | |
| 2330 | if (module_name && rpc_name) { |
| 2331 | mod = ly_ctx_get_module_implemented(session->ctx, module_name); |
| 2332 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2333 | ERR(session, "Missing \"%s\" module in the context.", module_name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2334 | return -1; |
| 2335 | } |
| 2336 | |
| 2337 | /* create the operation node */ |
| 2338 | lyrc = lyd_new_inner(NULL, mod, rpc_name, 0, op); |
| 2339 | } |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2340 | if (module_check) { |
| 2341 | if (!ly_ctx_get_module_implemented(session->ctx, module_check)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2342 | ERR(session, "Missing \"%s\" module in the context.", module_check); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2343 | return -1; |
| 2344 | } |
| 2345 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2346 | |
| 2347 | if (lyrc) { |
| 2348 | return -1; |
| 2349 | } |
| 2350 | return 0; |
| 2351 | } |
| 2352 | |
| 2353 | API NC_MSG_TYPE |
| 2354 | nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, struct lyd_node **envp, |
| 2355 | struct lyd_node **op) |
| 2356 | { |
| 2357 | NC_MSG_TYPE ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2358 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2359 | if (!session) { |
| 2360 | ERRARG("session"); |
| 2361 | return NC_MSG_ERROR; |
| 2362 | } else if (!rpc) { |
| 2363 | ERRARG("rpc"); |
| 2364 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2365 | } else if (!envp) { |
| 2366 | ERRARG("envp"); |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2367 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2368 | } else if (!op) { |
| 2369 | ERRARG("op"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2370 | return NC_MSG_ERROR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 2371 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2372 | ERR(session, "Invalid session to receive RPC replies."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2373 | return NC_MSG_ERROR; |
| 2374 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2375 | |
| 2376 | /* get a duplicate of the RPC node to append reply to */ |
| 2377 | if (recv_reply_dup_rpc(session, rpc, op)) { |
| 2378 | return NC_MSG_ERROR; |
Michal Vasko | eee9941 | 2016-11-21 10:19:43 +0100 | [diff] [blame] | 2379 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2380 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2381 | /* receive a reply */ |
| 2382 | ret = recv_reply(session, timeout, *op, msgid, envp); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2383 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2384 | /* do not return the RPC copy on error or if the reply includes no data */ |
| 2385 | if (((ret != NC_MSG_REPLY) && (ret != NC_MSG_REPLY_ERR_MSGID)) || !lyd_child(*op)) { |
| 2386 | lyd_free_tree(*op); |
| 2387 | *op = NULL; |
| 2388 | } |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
| 2392 | static NC_MSG_TYPE |
| 2393 | recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op) |
| 2394 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2395 | LY_ERR lyrc; |
| 2396 | struct ly_in *msg = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2397 | NC_MSG_TYPE ret = NC_MSG_ERROR; |
| 2398 | |
| 2399 | *op = NULL; |
| 2400 | *envp = NULL; |
| 2401 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2402 | /* Receive messages until a notification is found or a timeout or error reached */ |
| 2403 | ret = recv_msg(session, timeout, NC_MSG_NOTIF, &msg); |
| 2404 | if (ret != NC_MSG_NOTIF) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2405 | goto cleanup; |
| 2406 | } |
| 2407 | |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2408 | /* Parse */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2409 | lyrc = lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_NOTIF_NETCONF, envp, op); |
| 2410 | if (!lyrc) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2411 | goto cleanup; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2412 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2413 | ERR(session, "Received an invalid message (%s).", ly_errmsg(session->ctx)); |
Michal Vasko | 9a10805 | 2022-04-01 12:06:54 +0200 | [diff] [blame] | 2414 | lyd_free_tree(*envp); |
| 2415 | *envp = NULL; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 2416 | ret = NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2417 | goto cleanup; |
| 2418 | } |
| 2419 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2420 | cleanup: |
| 2421 | ly_in_free(msg, 1); |
| 2422 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | API NC_MSG_TYPE |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2426 | nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2427 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2428 | if (!session) { |
| 2429 | ERRARG("session"); |
| 2430 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2431 | } else if (!envp) { |
| 2432 | ERRARG("envp"); |
| 2433 | return NC_MSG_ERROR; |
| 2434 | } else if (!op) { |
| 2435 | ERRARG("op"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2436 | return NC_MSG_ERROR; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2437 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2438 | ERR(session, "Invalid session to receive Notifications."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2439 | return NC_MSG_ERROR; |
| 2440 | } |
| 2441 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2442 | /* receive a notification */ |
| 2443 | return recv_notif(session, timeout, envp, op); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2444 | } |
| 2445 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2446 | static void * |
| 2447 | nc_recv_notif_thread(void *arg) |
| 2448 | { |
| 2449 | struct nc_ntf_thread_arg *ntarg; |
| 2450 | struct nc_session *session; |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2451 | nc_notif_dispatch_clb notif_clb; |
| 2452 | void *user_data; |
Michal Vasko | 743ff9f | 2022-10-20 10:03:13 +0200 | [diff] [blame] | 2453 | |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2454 | void (*free_data)(void *); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2455 | struct lyd_node *envp, *op; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2456 | NC_MSG_TYPE msgtype; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2457 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2458 | /* detach ourselves */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2459 | pthread_detach(pthread_self()); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2460 | |
| 2461 | ntarg = (struct nc_ntf_thread_arg *)arg; |
| 2462 | session = ntarg->session; |
| 2463 | notif_clb = ntarg->notif_clb; |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2464 | user_data = ntarg->user_data; |
| 2465 | free_data = ntarg->free_data; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2466 | free(ntarg); |
| 2467 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2468 | while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2469 | msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, &envp, &op); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2470 | if (msgtype == NC_MSG_NOTIF) { |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2471 | notif_clb(session, envp, op, user_data); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2472 | if (!strcmp(op->schema->name, "notificationComplete") && !strcmp(op->schema->module->name, "nc-notifications")) { |
| 2473 | lyd_free_tree(envp); |
Michal Vasko | 0c0239c | 2023-02-01 14:31:06 +0100 | [diff] [blame] | 2474 | lyd_free_all(op); |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 2475 | break; |
| 2476 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2477 | lyd_free_tree(envp); |
Michal Vasko | 0c0239c | 2023-02-01 14:31:06 +0100 | [diff] [blame] | 2478 | lyd_free_all(op); |
Michal Vasko | ce32605 | 2018-01-04 10:32:03 +0100 | [diff] [blame] | 2479 | } else if ((msgtype == NC_MSG_ERROR) && (session->status != NC_STATUS_RUNNING)) { |
Michal Vasko | 132f7f4 | 2017-09-14 13:40:18 +0200 | [diff] [blame] | 2480 | /* quit this thread once the session is broken */ |
| 2481 | break; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | usleep(NC_CLIENT_NOTIF_THREAD_SLEEP); |
| 2485 | } |
| 2486 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2487 | VRB(session, "Notification thread exit."); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2488 | ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count); |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2489 | if (free_data) { |
| 2490 | free_data(user_data); |
| 2491 | } |
| 2492 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2493 | return NULL; |
| 2494 | } |
| 2495 | |
| 2496 | API int |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2497 | nc_recv_notif_dispatch(struct nc_session *session, nc_notif_dispatch_clb notif_clb) |
| 2498 | { |
| 2499 | return nc_recv_notif_dispatch_data(session, notif_clb, NULL, NULL); |
| 2500 | } |
| 2501 | |
| 2502 | API int |
| 2503 | nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb notif_clb, void *user_data, |
| 2504 | void (*free_data)(void *)) |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2505 | { |
| 2506 | struct nc_ntf_thread_arg *ntarg; |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2507 | pthread_t tid; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2508 | int ret; |
| 2509 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2510 | if (!session) { |
| 2511 | ERRARG("session"); |
| 2512 | return -1; |
| 2513 | } else if (!notif_clb) { |
| 2514 | ERRARG("notif_clb"); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2515 | return -1; |
| 2516 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2517 | ERR(session, "Invalid session to receive Notifications."); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2518 | return -1; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | ntarg = malloc(sizeof *ntarg); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2522 | if (!ntarg) { |
| 2523 | ERRMEM; |
| 2524 | return -1; |
| 2525 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2526 | ntarg->session = session; |
| 2527 | ntarg->notif_clb = notif_clb; |
Michal Vasko | ffb35e9 | 2022-10-20 09:07:25 +0200 | [diff] [blame] | 2528 | ntarg->user_data = user_data; |
| 2529 | ntarg->free_data = free_data; |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2530 | ATOMIC_INC_RELAXED(session->opts.client.ntf_thread_count); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2531 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2532 | /* just so that nc_recv_notif_thread() does not immediately exit */ |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2533 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 1); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2534 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2535 | ret = pthread_create(&tid, NULL, nc_recv_notif_thread, ntarg); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2536 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2537 | ERR(session, "Failed to create a new thread (%s).", strerror(errno)); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2538 | free(ntarg); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 2539 | if (ATOMIC_DEC_RELAXED(session->opts.client.ntf_thread_count) == 1) { |
| 2540 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0); |
| 2541 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 2542 | return -1; |
| 2543 | } |
| 2544 | |
| 2545 | return 0; |
| 2546 | } |
| 2547 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2548 | static const char * |
| 2549 | nc_wd2str(NC_WD_MODE wd) |
| 2550 | { |
| 2551 | switch (wd) { |
| 2552 | case NC_WD_ALL: |
| 2553 | return "report-all"; |
| 2554 | case NC_WD_ALL_TAG: |
| 2555 | return "report-all-tagged"; |
| 2556 | case NC_WD_TRIM: |
| 2557 | return "trim"; |
| 2558 | case NC_WD_EXPLICIT: |
| 2559 | return "explicit"; |
| 2560 | default: |
| 2561 | break; |
| 2562 | } |
| 2563 | |
| 2564 | return NULL; |
| 2565 | } |
| 2566 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2567 | API NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 2568 | nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2569 | { |
| 2570 | NC_MSG_TYPE r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2571 | int dofree = 1; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2572 | struct ly_in *in; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 2573 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2574 | struct nc_rpc_getconfig *rpc_gc; |
| 2575 | struct nc_rpc_edit *rpc_e; |
| 2576 | struct nc_rpc_copy *rpc_cp; |
| 2577 | struct nc_rpc_delete *rpc_del; |
| 2578 | struct nc_rpc_lock *rpc_lock; |
| 2579 | struct nc_rpc_get *rpc_g; |
| 2580 | struct nc_rpc_kill *rpc_k; |
| 2581 | struct nc_rpc_commit *rpc_com; |
| 2582 | struct nc_rpc_cancel *rpc_can; |
| 2583 | struct nc_rpc_validate *rpc_val; |
| 2584 | struct nc_rpc_getschema *rpc_gs; |
| 2585 | struct nc_rpc_subscribe *rpc_sub; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2586 | struct nc_rpc_getdata *rpc_getd; |
| 2587 | struct nc_rpc_editdata *rpc_editd; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2588 | struct nc_rpc_establishsub *rpc_estsub; |
| 2589 | struct nc_rpc_modifysub *rpc_modsub; |
| 2590 | struct nc_rpc_deletesub *rpc_delsub; |
| 2591 | struct nc_rpc_killsub *rpc_killsub; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2592 | struct nc_rpc_establishpush *rpc_estpush; |
| 2593 | struct nc_rpc_modifypush *rpc_modpush; |
| 2594 | struct nc_rpc_resyncsub *rpc_resyncsub; |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2595 | struct lyd_node *data = NULL, *node, *cont; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2596 | const struct lys_module *mod = NULL, *mod2 = NULL, *ietfncwd; |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2597 | LY_ERR lyrc = 0; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2598 | int i; |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 2599 | char str[11]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2600 | uint64_t cur_msgid; |
| 2601 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2602 | if (!session) { |
| 2603 | ERRARG("session"); |
| 2604 | return NC_MSG_ERROR; |
| 2605 | } else if (!rpc) { |
| 2606 | ERRARG("rpc"); |
| 2607 | return NC_MSG_ERROR; |
| 2608 | } else if (!msgid) { |
| 2609 | ERRARG("msgid"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2610 | return NC_MSG_ERROR; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2611 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2612 | ERR(session, "Invalid session to send RPCs."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2613 | return NC_MSG_ERROR; |
| 2614 | } |
| 2615 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2616 | switch (rpc->type) { |
| 2617 | case NC_RPC_ACT_GENERIC: |
| 2618 | /* checked when parsing */ |
| 2619 | break; |
| 2620 | case NC_RPC_GETCONFIG: |
| 2621 | case NC_RPC_EDIT: |
| 2622 | case NC_RPC_COPY: |
| 2623 | case NC_RPC_DELETE: |
| 2624 | case NC_RPC_LOCK: |
| 2625 | case NC_RPC_UNLOCK: |
| 2626 | case NC_RPC_GET: |
| 2627 | case NC_RPC_KILL: |
| 2628 | case NC_RPC_COMMIT: |
| 2629 | case NC_RPC_DISCARD: |
| 2630 | case NC_RPC_CANCEL: |
| 2631 | case NC_RPC_VALIDATE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2632 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2633 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2634 | ERR(session, "Missing \"ietf-netconf\" module in the context."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2635 | return NC_MSG_ERROR; |
| 2636 | } |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2637 | break; |
| 2638 | case NC_RPC_GETSCHEMA: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2639 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2640 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2641 | ERR(session, "Missing \"ietf-netconf-monitoring\" module in the context."); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2642 | return NC_MSG_ERROR; |
| 2643 | } |
| 2644 | break; |
| 2645 | case NC_RPC_SUBSCRIBE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2646 | mod = ly_ctx_get_module_implemented(session->ctx, "notifications"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2647 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2648 | ERR(session, "Missing \"notifications\" module in the context."); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2649 | return NC_MSG_ERROR; |
| 2650 | } |
| 2651 | break; |
| 2652 | case NC_RPC_GETDATA: |
| 2653 | case NC_RPC_EDITDATA: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2654 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-nmda"); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2655 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2656 | ERR(session, "Missing \"ietf-netconf-nmda\" module in the context."); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2657 | return NC_MSG_ERROR; |
| 2658 | } |
| 2659 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2660 | case NC_RPC_ESTABLISHSUB: |
| 2661 | case NC_RPC_MODIFYSUB: |
| 2662 | case NC_RPC_DELETESUB: |
| 2663 | case NC_RPC_KILLSUB: |
| 2664 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications"); |
| 2665 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2666 | ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context."); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2667 | return NC_MSG_ERROR; |
| 2668 | } |
| 2669 | break; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2670 | case NC_RPC_ESTABLISHPUSH: |
| 2671 | case NC_RPC_MODIFYPUSH: |
| 2672 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-subscribed-notifications"); |
| 2673 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2674 | ERR(session, "Missing \"ietf-subscribed-notifications\" module in the context."); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2675 | return NC_MSG_ERROR; |
| 2676 | } |
| 2677 | mod2 = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push"); |
| 2678 | if (!mod2) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2679 | ERR(session, "Missing \"ietf-yang-push\" module in the context."); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2680 | return NC_MSG_ERROR; |
| 2681 | } |
| 2682 | break; |
| 2683 | case NC_RPC_RESYNCSUB: |
| 2684 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-yang-push"); |
| 2685 | if (!mod) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 2686 | ERR(session, "Missing \"ietf-yang-push\" module in the context."); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 2687 | return NC_MSG_ERROR; |
| 2688 | } |
| 2689 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2690 | case NC_RPC_UNKNOWN: |
| 2691 | ERRINT; |
| 2692 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2695 | #define CHECK_LYRC_BREAK(func_call) if ((lyrc = func_call)) break; |
| 2696 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2697 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 2698 | case NC_RPC_ACT_GENERIC: |
| 2699 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2700 | |
| 2701 | if (rpc_gen->has_data) { |
| 2702 | data = rpc_gen->content.data; |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 2703 | dofree = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2704 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2705 | ly_in_new_memory(rpc_gen->content.xml_str, &in); |
| 2706 | lyrc = lyd_parse_op(session->ctx, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &data, NULL); |
| 2707 | ly_in_free(in, 0); |
| 2708 | if (lyrc) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2709 | break; |
Michal Vasko | eec410f | 2017-11-24 09:14:55 +0100 | [diff] [blame] | 2710 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2711 | } |
| 2712 | break; |
| 2713 | |
| 2714 | case NC_RPC_GETCONFIG: |
| 2715 | rpc_gc = (struct nc_rpc_getconfig *)rpc; |
| 2716 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2717 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-config", 0, &data)); |
| 2718 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont)); |
| 2719 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_gc->source], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2720 | if (rpc_gc->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2721 | if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2722 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_gc->filter, 0, LYD_ANYDATA_XML, 0, &node)); |
| 2723 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2724 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2725 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node)); |
| 2726 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL)); |
| 2727 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_gc->filter, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | if (rpc_gc->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2732 | ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2733 | if (!ietfncwd) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 2734 | ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2735 | lyrc = LY_ENOTFOUND; |
| 2736 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2737 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2738 | CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_gc->wd_mode), 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2739 | } |
| 2740 | break; |
| 2741 | |
| 2742 | case NC_RPC_EDIT: |
| 2743 | rpc_e = (struct nc_rpc_edit *)rpc; |
| 2744 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2745 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-config", 0, &data)); |
| 2746 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
| 2747 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_e->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2748 | |
| 2749 | if (rpc_e->default_op) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2750 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_e->default_op], 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2751 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2752 | if (rpc_e->test_opt) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2753 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "test-option", rpcedit_testopt2str[rpc_e->test_opt], 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2754 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2755 | if (rpc_e->error_opt) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2756 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "error-option", rpcedit_erropt2str[rpc_e->error_opt], 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2757 | } |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2758 | if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2759 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_e->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2760 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2761 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_e->edit_cont, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2762 | } |
| 2763 | break; |
| 2764 | |
| 2765 | case NC_RPC_COPY: |
| 2766 | rpc_cp = (struct nc_rpc_copy *)rpc; |
| 2767 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2768 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "copy-config", 0, &data)); |
| 2769 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2770 | if (rpc_cp->url_trg) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2771 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_trg, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2772 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2773 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2774 | } |
| 2775 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2776 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2777 | if (rpc_cp->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2778 | if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2779 | CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_cp->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2780 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2781 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_cp->url_config_src, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2782 | } |
| 2783 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2784 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_cp->source], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | if (rpc_cp->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2788 | ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2789 | if (!ietfncwd) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 2790 | ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2791 | lyrc = LY_ENOTFOUND; |
| 2792 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2793 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2794 | CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_cp->wd_mode), 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2795 | } |
| 2796 | break; |
| 2797 | |
| 2798 | case NC_RPC_DELETE: |
| 2799 | rpc_del = (struct nc_rpc_delete *)rpc; |
| 2800 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2801 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-config", 0, &data)); |
| 2802 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2803 | if (rpc_del->url) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2804 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_del->url, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2805 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2806 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_del->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2807 | } |
| 2808 | break; |
| 2809 | |
| 2810 | case NC_RPC_LOCK: |
| 2811 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 2812 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2813 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "lock", 0, &data)); |
| 2814 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
| 2815 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2816 | break; |
| 2817 | |
| 2818 | case NC_RPC_UNLOCK: |
| 2819 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 2820 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2821 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "unlock", 0, &data)); |
| 2822 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "target", 0, &cont)); |
| 2823 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_lock->target], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2824 | break; |
| 2825 | |
| 2826 | case NC_RPC_GET: |
| 2827 | rpc_g = (struct nc_rpc_get *)rpc; |
| 2828 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2829 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2830 | if (rpc_g->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2831 | if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2832 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_g->filter, 0, LYD_ANYDATA_XML, 0, &node)); |
| 2833 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2834 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2835 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node)); |
| 2836 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL)); |
| 2837 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_g->filter, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | if (rpc_g->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2842 | ietfncwd = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2843 | if (!ietfncwd) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 2844 | ERR(session, "Missing \"ietf-netconf-with-defaults\" module in the context."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2845 | lyrc = LY_ENOTFOUND; |
| 2846 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2847 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2848 | CHECK_LYRC_BREAK(lyd_new_term(data, ietfncwd, "with-defaults", nc_wd2str(rpc_g->wd_mode), 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2849 | } |
| 2850 | break; |
| 2851 | |
| 2852 | case NC_RPC_KILL: |
| 2853 | rpc_k = (struct nc_rpc_kill *)rpc; |
| 2854 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2855 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-session", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2856 | sprintf(str, "%u", rpc_k->sid); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2857 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "session-id", str, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2858 | break; |
| 2859 | |
| 2860 | case NC_RPC_COMMIT: |
| 2861 | rpc_com = (struct nc_rpc_commit *)rpc; |
| 2862 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2863 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "commit", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2864 | if (rpc_com->confirmed) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2865 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirmed", NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | if (rpc_com->confirm_timeout) { |
| 2869 | sprintf(str, "%u", rpc_com->confirm_timeout); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2870 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "confirm-timeout", str, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2871 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2872 | if (rpc_com->persist) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2873 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist", rpc_com->persist, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2874 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2875 | if (rpc_com->persist_id) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2876 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_com->persist_id, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2877 | } |
| 2878 | break; |
| 2879 | |
| 2880 | case NC_RPC_DISCARD: |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2881 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "discard-changes", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2882 | break; |
| 2883 | |
| 2884 | case NC_RPC_CANCEL: |
| 2885 | rpc_can = (struct nc_rpc_cancel *)rpc; |
| 2886 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2887 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "cancel-commit", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2888 | if (rpc_can->persist_id) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2889 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "persist-id", rpc_can->persist_id, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2890 | } |
| 2891 | break; |
| 2892 | |
| 2893 | case NC_RPC_VALIDATE: |
| 2894 | rpc_val = (struct nc_rpc_validate *)rpc; |
| 2895 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2896 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "validate", 0, &data)); |
| 2897 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod, "source", 0, &cont)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2898 | if (rpc_val->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 2899 | if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2900 | CHECK_LYRC_BREAK(lyd_new_any(cont, mod, "config", rpc_val->url_config_src, 0, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2901 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2902 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, "url", rpc_val->url_config_src, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2903 | } |
| 2904 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2905 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod, ncds2str[rpc_val->source], NULL, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2906 | } |
| 2907 | break; |
| 2908 | |
| 2909 | case NC_RPC_GETSCHEMA: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2910 | rpc_gs = (struct nc_rpc_getschema *)rpc; |
| 2911 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2912 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-schema", 0, &data)); |
| 2913 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "identifier", rpc_gs->identifier, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2914 | if (rpc_gs->version) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2915 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "version", rpc_gs->version, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2916 | } |
| 2917 | if (rpc_gs->format) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2918 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "format", rpc_gs->format, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2919 | } |
| 2920 | break; |
| 2921 | |
| 2922 | case NC_RPC_SUBSCRIBE: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2923 | rpc_sub = (struct nc_rpc_subscribe *)rpc; |
| 2924 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2925 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "create-subscription", 0, &data)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2926 | if (rpc_sub->stream) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2927 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_sub->stream, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | if (rpc_sub->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 2931 | if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2932 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", rpc_sub->filter, 0, LYD_ANYDATA_XML, 0, &node)); |
| 2933 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "subtree", 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2934 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2935 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "filter", NULL, 0, LYD_ANYDATA_STRING, 0, &node)); |
| 2936 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:type", "xpath", 0, NULL)); |
| 2937 | CHECK_LYRC_BREAK(lyd_new_meta(NULL, node, NULL, "ietf-netconf:select", rpc_sub->filter, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2938 | } |
| 2939 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2940 | if (rpc_sub->start) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2941 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "startTime", rpc_sub->start, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2942 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2943 | if (rpc_sub->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2944 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stopTime", rpc_sub->stop, 0, NULL)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2945 | } |
| 2946 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2947 | |
| 2948 | case NC_RPC_GETDATA: |
| 2949 | rpc_getd = (struct nc_rpc_getdata *)rpc; |
| 2950 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2951 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "get-data", 0, &data)); |
| 2952 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_getd->datastore, 0, NULL)); |
| 2953 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2954 | if (rpc_getd->filter) { |
| 2955 | if (!rpc_getd->filter[0] || (rpc_getd->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2956 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "subtree-filter", rpc_getd->filter, 0, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2957 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2958 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "xpath-filter", rpc_getd->filter, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | if (rpc_getd->config_filter) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2962 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "config-filter", rpc_getd->config_filter, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2963 | } |
| 2964 | for (i = 0; i < rpc_getd->origin_filter_count; ++i) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2965 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, rpc_getd->negated_origin_filter ? "negated-origin-filter" : |
| 2966 | "origin-filter", rpc_getd->origin_filter[i], 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2967 | } |
| 2968 | if (rpc_getd->max_depth) { |
| 2969 | sprintf(str, "%u", rpc_getd->max_depth); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2970 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "max-depth", str, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2971 | } |
| 2972 | if (rpc_getd->with_origin) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2973 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-origin", NULL, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | if (rpc_getd->wd_mode) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2977 | /* "with-defaults" are used from a grouping so it belongs to the ietf-netconf-nmda module */ |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2978 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "with-defaults", nc_wd2str(rpc_getd->wd_mode), 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2979 | } |
| 2980 | break; |
| 2981 | |
| 2982 | case NC_RPC_EDITDATA: |
| 2983 | rpc_editd = (struct nc_rpc_editdata *)rpc; |
| 2984 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2985 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "edit-data", 0, &data)); |
| 2986 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "datastore", rpc_editd->datastore, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2987 | |
| 2988 | if (rpc_editd->default_op) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2989 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "default-operation", rpcedit_dfltop2str[rpc_editd->default_op], 0, |
| 2990 | NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2991 | } |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2992 | if (!rpc_editd->edit_cont[0] || (rpc_editd->edit_cont[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2993 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "config", rpc_editd->edit_cont, 0, LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2994 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 2995 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "url", rpc_editd->edit_cont, 0, NULL)); |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 2996 | } |
| 2997 | break; |
| 2998 | |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 2999 | case NC_RPC_ESTABLISHSUB: |
| 3000 | rpc_estsub = (struct nc_rpc_establishsub *)rpc; |
| 3001 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3002 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3003 | |
| 3004 | if (rpc_estsub->filter) { |
| 3005 | if (!rpc_estsub->filter[0] || (rpc_estsub->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3006 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_estsub->filter, 0, LYD_ANYDATA_XML, |
| 3007 | 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3008 | } else if (rpc_estsub->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3009 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_estsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3010 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3011 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_estsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3012 | } |
| 3013 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3014 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream", rpc_estsub->stream, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3015 | |
| 3016 | if (rpc_estsub->start) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3017 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "replay-start-time", rpc_estsub->start, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3018 | } |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3019 | if (rpc_estsub->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3020 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estsub->stop, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3021 | } |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3022 | if (rpc_estsub->encoding) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3023 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estsub->encoding, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3024 | } |
| 3025 | break; |
| 3026 | |
| 3027 | case NC_RPC_MODIFYSUB: |
| 3028 | rpc_modsub = (struct nc_rpc_modifysub *)rpc; |
| 3029 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3030 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3031 | |
| 3032 | sprintf(str, "%u", rpc_modsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3033 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3034 | |
| 3035 | if (rpc_modsub->filter) { |
| 3036 | if (!rpc_modsub->filter[0] || (rpc_modsub->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3037 | CHECK_LYRC_BREAK(lyd_new_any(data, mod, "stream-subtree-filter", rpc_modsub->filter, 0, LYD_ANYDATA_XML, |
| 3038 | 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3039 | } else if (rpc_modsub->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3040 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-xpath-filter", rpc_modsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3041 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3042 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stream-filter-name", rpc_modsub->filter, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3043 | } |
| 3044 | } |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3045 | if (rpc_modsub->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3046 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modsub->stop, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3047 | } |
| 3048 | break; |
| 3049 | |
| 3050 | case NC_RPC_DELETESUB: |
| 3051 | rpc_delsub = (struct nc_rpc_deletesub *)rpc; |
| 3052 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3053 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "delete-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3054 | |
| 3055 | sprintf(str, "%u", rpc_delsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3056 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3057 | break; |
| 3058 | |
| 3059 | case NC_RPC_KILLSUB: |
| 3060 | rpc_killsub = (struct nc_rpc_killsub *)rpc; |
| 3061 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3062 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "kill-subscription", 0, &data)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3063 | |
| 3064 | sprintf(str, "%u", rpc_killsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3065 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3066 | break; |
| 3067 | |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3068 | case NC_RPC_ESTABLISHPUSH: |
| 3069 | rpc_estpush = (struct nc_rpc_establishpush *)rpc; |
| 3070 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3071 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "establish-subscription", 0, &data)); |
| 3072 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_estpush->datastore, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3073 | |
| 3074 | if (rpc_estpush->filter) { |
| 3075 | if (!rpc_estpush->filter[0] || (rpc_estpush->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3076 | CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_estpush->filter, 0, |
| 3077 | LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3078 | } else if (rpc_estpush->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3079 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_estpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3080 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3081 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_estpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | if (rpc_estpush->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3086 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_estpush->stop, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3087 | } |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3088 | if (rpc_estpush->encoding) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3089 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "encoding", rpc_estpush->encoding, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | if (rpc_estpush->periodic) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3093 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3094 | sprintf(str, "%" PRIu32, rpc_estpush->period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3095 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3096 | if (rpc_estpush->anchor_time) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3097 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_estpush->anchor_time, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3098 | } |
| 3099 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3100 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3101 | if (rpc_estpush->dampening_period) { |
| 3102 | sprintf(str, "%" PRIu32, rpc_estpush->dampening_period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3103 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3104 | } |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3105 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "sync-on-start", rpc_estpush->sync_on_start ? "true" : "false", 0, |
| 3106 | NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3107 | if (rpc_estpush->excluded_change) { |
| 3108 | for (i = 0; rpc_estpush->excluded_change[i]; ++i) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3109 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "excluded-change", rpc_estpush->excluded_change[i], 0, |
| 3110 | NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | break; |
| 3115 | |
| 3116 | case NC_RPC_MODIFYPUSH: |
| 3117 | rpc_modpush = (struct nc_rpc_modifypush *)rpc; |
| 3118 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3119 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "modify-subscription", 0, &data)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3120 | |
| 3121 | sprintf(str, "%u", rpc_modpush->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3122 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
| 3123 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore", rpc_modpush->datastore, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3124 | |
| 3125 | if (rpc_modpush->filter) { |
| 3126 | if (!rpc_modpush->filter[0] || (rpc_modpush->filter[0] == '<')) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3127 | CHECK_LYRC_BREAK(lyd_new_any(data, mod2, "datastore-subtree-filter", rpc_modpush->filter, 0, |
| 3128 | LYD_ANYDATA_XML, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3129 | } else if (rpc_modpush->filter[0] == '/') { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3130 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "datastore-xpath-filter", rpc_modpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3131 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3132 | CHECK_LYRC_BREAK(lyd_new_term(data, mod2, "selection-filter-ref", rpc_modpush->filter, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3133 | } |
| 3134 | } |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3135 | if (rpc_modpush->stop) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3136 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "stop-time", rpc_modpush->stop, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | if (rpc_modpush->periodic) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3140 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "periodic", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3141 | sprintf(str, "%" PRIu32, rpc_modpush->period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3142 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3143 | if (rpc_modpush->anchor_time) { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3144 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "anchor-time", rpc_modpush->anchor_time, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3145 | } |
| 3146 | } else { |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3147 | CHECK_LYRC_BREAK(lyd_new_inner(data, mod2, "on-change", 0, &cont)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3148 | if (rpc_modpush->dampening_period) { |
| 3149 | sprintf(str, "%" PRIu32, rpc_modpush->dampening_period); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3150 | CHECK_LYRC_BREAK(lyd_new_term(cont, mod2, "dampening-period", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | break; |
| 3154 | |
| 3155 | case NC_RPC_RESYNCSUB: |
| 3156 | rpc_resyncsub = (struct nc_rpc_resyncsub *)rpc; |
| 3157 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3158 | CHECK_LYRC_BREAK(lyd_new_inner(NULL, mod, "resync-subscription", 0, &data)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3159 | sprintf(str, "%u", rpc_resyncsub->id); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3160 | CHECK_LYRC_BREAK(lyd_new_term(data, mod, "id", str, 0, NULL)); |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 3161 | break; |
| 3162 | |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 3163 | case NC_RPC_UNKNOWN: |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 3164 | ERRINT; |
| 3165 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3166 | } |
| 3167 | |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3168 | #undef CHECK_LYRC_BREAK |
| 3169 | |
| 3170 | if (lyrc) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3171 | ERR(session, "Failed to create RPC, perhaps a required feature is disabled."); |
Michal Vasko | ab9deb6 | 2021-05-27 11:37:00 +0200 | [diff] [blame] | 3172 | lyd_free_tree(data); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3173 | return NC_MSG_ERROR; |
| 3174 | } |
| 3175 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3176 | /* send RPC, store its message ID */ |
| 3177 | r = nc_send_msg_io(session, timeout, data); |
| 3178 | cur_msgid = session->opts.client.msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3179 | |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 3180 | if (dofree) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 3181 | lyd_free_tree(data); |
Radek Krejci | b4b1906 | 2018-02-07 16:33:06 +0100 | [diff] [blame] | 3182 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3183 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3184 | if (r == NC_MSG_RPC) { |
| 3185 | *msgid = cur_msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3186 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3187 | return r; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3188 | } |
Michal Vasko | de2946c | 2017-01-12 12:19:26 +0100 | [diff] [blame] | 3189 | |
| 3190 | API void |
| 3191 | nc_client_session_set_not_strict(struct nc_session *session) |
| 3192 | { |
| 3193 | if (session->side != NC_CLIENT) { |
| 3194 | ERRARG("session"); |
| 3195 | return; |
| 3196 | } |
| 3197 | |
| 3198 | session->flags |= NC_SESSION_CLIENT_NOT_STRICT; |
| 3199 | } |