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_server.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 server session manipulation 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 |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2024 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 | */ |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 15 | #define _QNX_SOURCE /* getpeereid */ |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE /* threads, SO_PEERCRED */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 17 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 18 | #include <arpa/inet.h> |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 20 | #include <errno.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 21 | #include <fcntl.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 22 | #include <netinet/in.h> |
| 23 | #include <netinet/tcp.h> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 24 | #include <pthread.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 25 | #include <pwd.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/socket.h> |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 30 | #include <sys/stat.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/un.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 35 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 36 | #include "compat.h" |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 37 | #include "config.h" |
| 38 | #include "log_p.h" |
| 39 | #include "messages_p.h" |
| 40 | #include "messages_server.h" |
| 41 | #include "server_config_p.h" |
| 42 | #include "session.h" |
| 43 | #include "session_p.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 44 | #include "session_server.h" |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 45 | #include "session_server_ch.h" |
roman | 9225e91 | 2024-04-05 12:33:09 +0200 | [diff] [blame] | 46 | #include "session_wrapper.h" |
| 47 | |
| 48 | #ifdef NC_ENABLED_SSH_TLS |
| 49 | #include <curl/curl.h> |
| 50 | #include <libssh/libssh.h> |
| 51 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 52 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 53 | struct nc_server_opts server_opts = { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 54 | .config_lock = PTHREAD_RWLOCK_INITIALIZER, |
| 55 | .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER, |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 56 | .idle_timeout = 180, /**< default idle timeout (not in config for UNIX socket) */ |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 57 | }; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 58 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 59 | static nc_rpc_clb global_rpc_clb = NULL; |
| 60 | |
roman | 423cc0d | 2023-11-24 11:29:47 +0100 | [diff] [blame] | 61 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 62 | /** |
| 63 | * @brief Lock CH client structures for reading and lock the specific client. |
| 64 | * |
| 65 | * @param[in] name Name of the CH client. |
| 66 | * @return CH client, NULL if not found. |
| 67 | */ |
| 68 | static struct nc_ch_client * |
| 69 | nc_server_ch_client_lock(const char *name) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 70 | { |
| 71 | uint16_t i; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 72 | struct nc_ch_client *client = NULL; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 73 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 74 | assert(name); |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 75 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 76 | /* READ LOCK */ |
| 77 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 78 | |
| 79 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 80 | if (server_opts.ch_clients[i].name && !strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 81 | client = &server_opts.ch_clients[i]; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (!client) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 87 | /* READ UNLOCK */ |
| 88 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 89 | } else { |
| 90 | /* CH CLIENT LOCK */ |
| 91 | pthread_mutex_lock(&client->lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 94 | return client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 97 | /** |
| 98 | * @brief Unlock CH client strcutures and the specific client. |
| 99 | * |
| 100 | * @param[in] endpt Locked CH client structure. |
| 101 | */ |
| 102 | static void |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 103 | nc_server_ch_client_unlock(struct nc_ch_client *client) |
| 104 | { |
| 105 | /* CH CLIENT UNLOCK */ |
| 106 | pthread_mutex_unlock(&client->lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 107 | |
| 108 | /* READ UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 109 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 110 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 111 | |
roman | 423cc0d | 2023-11-24 11:29:47 +0100 | [diff] [blame] | 112 | #endif /* NC_ENABLED_SSH_TLS */ |
| 113 | |
roman | 78df0fa | 2023-11-02 10:33:57 +0100 | [diff] [blame] | 114 | int |
| 115 | nc_server_get_referenced_endpt(const char *name, struct nc_endpt **endpt) |
| 116 | { |
| 117 | uint16_t i; |
| 118 | |
| 119 | for (i = 0; i < server_opts.endpt_count; i++) { |
| 120 | if (!strcmp(name, server_opts.endpts[i].name)) { |
| 121 | *endpt = &server_opts.endpts[i]; |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | ERR(NULL, "Referenced endpoint \"%s\" was not found.", name); |
| 127 | return 1; |
| 128 | } |
| 129 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 130 | API void |
| 131 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 132 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 133 | if (!session) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 134 | ERRARG(session, "session"); |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 135 | return; |
| 136 | } else if (!reason) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 137 | ERRARG(session, "reason"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 141 | if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) { |
| 142 | session->killed_by = 0; |
| 143 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 144 | session->term_reason = reason; |
| 145 | } |
| 146 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 147 | API void |
| 148 | nc_session_set_killed_by(struct nc_session *session, uint32_t sid) |
| 149 | { |
| 150 | if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 151 | ERRARG(session, "session"); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 152 | return; |
| 153 | } else if (!sid) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 154 | ERRARG(session, "sid"); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | session->killed_by = sid; |
| 159 | } |
| 160 | |
| 161 | API void |
| 162 | nc_session_set_status(struct nc_session *session, NC_STATUS status) |
| 163 | { |
| 164 | if (!session) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 165 | ERRARG(session, "session"); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 166 | return; |
| 167 | } else if (!status) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 168 | ERRARG(session, "status"); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| 172 | session->status = status; |
| 173 | } |
| 174 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 175 | API int |
| 176 | nc_server_init_ctx(struct ly_ctx **ctx) |
| 177 | { |
| 178 | int new_ctx = 0, i, ret = 0; |
| 179 | struct lys_module *module; |
| 180 | /* all features */ |
| 181 | const char *ietf_netconf_features[] = {"writable-running", "candidate", "rollback-on-error", "validate", "startup", "url", "xpath", "confirmed-commit", NULL}; |
| 182 | /* all features (module has no features) */ |
| 183 | const char *ietf_netconf_monitoring_features[] = {NULL}; |
| 184 | |
| 185 | NC_CHECK_ARG_RET(NULL, ctx, 1); |
| 186 | |
| 187 | if (!*ctx) { |
| 188 | /* context not given, create a new one */ |
| 189 | if (ly_ctx_new(NC_SERVER_SEARCH_DIR, 0, ctx)) { |
| 190 | ERR(NULL, "Couldn't create new libyang context.\n"); |
| 191 | ret = 1; |
| 192 | goto cleanup; |
| 193 | } |
| 194 | new_ctx = 1; |
| 195 | } |
| 196 | |
| 197 | if (new_ctx) { |
| 198 | /* new context created, implement both modules */ |
| 199 | if (!ly_ctx_load_module(*ctx, "ietf-netconf", NULL, ietf_netconf_features)) { |
| 200 | ERR(NULL, "Loading module \"ietf-netconf\" failed.\n"); |
| 201 | ret = 1; |
| 202 | goto cleanup; |
| 203 | } |
| 204 | |
| 205 | if (!ly_ctx_load_module(*ctx, "ietf-netconf-monitoring", NULL, ietf_netconf_monitoring_features)) { |
| 206 | ERR(NULL, "Loading module \"ietf-netconf-monitoring\" failed.\n"); |
| 207 | ret = 1; |
| 208 | goto cleanup; |
| 209 | } |
| 210 | |
| 211 | goto cleanup; |
| 212 | } |
| 213 | |
| 214 | module = ly_ctx_get_module_implemented(*ctx, "ietf-netconf"); |
| 215 | if (module) { |
| 216 | /* ietf-netconf module is present, check features */ |
| 217 | for (i = 0; ietf_netconf_features[i]; i++) { |
| 218 | if (lys_feature_value(module, ietf_netconf_features[i])) { |
| 219 | /* feature not found, enable all of them */ |
| 220 | if (!ly_ctx_load_module(*ctx, "ietf-netconf", NULL, ietf_netconf_features)) { |
| 221 | ERR(NULL, "Loading module \"ietf-netconf\" failed.\n"); |
| 222 | ret = 1; |
| 223 | goto cleanup; |
| 224 | } |
| 225 | |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } else { |
| 230 | /* ietf-netconf module not found, add it */ |
| 231 | if (!ly_ctx_load_module(*ctx, "ietf-netconf", NULL, ietf_netconf_features)) { |
| 232 | ERR(NULL, "Loading module \"ietf-netconf\" failed.\n"); |
| 233 | ret = 1; |
| 234 | goto cleanup; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | module = ly_ctx_get_module_implemented(*ctx, "ietf-netconf-monitoring"); |
| 239 | if (!module) { |
| 240 | /* ietf-netconf-monitoring module not found, add it */ |
| 241 | if (!ly_ctx_load_module(*ctx, "ietf-netconf-monitoring", NULL, ietf_netconf_monitoring_features)) { |
| 242 | ERR(NULL, "Loading module \"ietf-netconf-monitoring\" failed.\n"); |
| 243 | ret = 1; |
| 244 | goto cleanup; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | cleanup: |
| 249 | if (new_ctx && ret) { |
| 250 | ly_ctx_destroy(*ctx); |
| 251 | *ctx = NULL; |
| 252 | } |
| 253 | return ret; |
| 254 | } |
| 255 | |
roman | 96c27f9 | 2023-11-02 11:09:46 +0100 | [diff] [blame] | 256 | #ifdef NC_ENABLED_SSH_TLS |
| 257 | |
roman | 450c00b | 2023-11-02 10:31:45 +0100 | [diff] [blame] | 258 | API void |
| 259 | nc_server_ch_set_dispatch_data(nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 260 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb, |
| 261 | void *new_session_cb_data) |
| 262 | { |
| 263 | NC_CHECK_ARG_RET(NULL, acquire_ctx_cb, release_ctx_cb, new_session_cb, ); |
| 264 | |
| 265 | server_opts.ch_dispatch_data.acquire_ctx_cb = acquire_ctx_cb; |
| 266 | server_opts.ch_dispatch_data.release_ctx_cb = release_ctx_cb; |
| 267 | server_opts.ch_dispatch_data.ctx_cb_data = ctx_cb_data; |
| 268 | server_opts.ch_dispatch_data.new_session_cb = new_session_cb; |
| 269 | server_opts.ch_dispatch_data.new_session_cb_data = new_session_cb_data; |
| 270 | } |
| 271 | |
roman | 96c27f9 | 2023-11-02 11:09:46 +0100 | [diff] [blame] | 272 | #endif |
| 273 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 274 | int |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 275 | nc_sock_listen_inet(const char *address, uint16_t port) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 276 | { |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 277 | int opt; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 278 | int is_ipv4, sock; |
| 279 | struct sockaddr_storage saddr; |
| 280 | |
| 281 | struct sockaddr_in *saddr4; |
| 282 | struct sockaddr_in6 *saddr6; |
| 283 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 284 | if (!strchr(address, ':')) { |
| 285 | is_ipv4 = 1; |
| 286 | } else { |
| 287 | is_ipv4 = 0; |
| 288 | } |
| 289 | |
| 290 | sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0); |
| 291 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 292 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 293 | goto fail; |
| 294 | } |
| 295 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 296 | /* these options will be inherited by accepted sockets */ |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 297 | opt = 1; |
| 298 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 299 | ERR(NULL, "Could not set SO_REUSEADDR socket option (%s).", strerror(errno)); |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 300 | goto fail; |
| 301 | } |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 302 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 303 | ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 304 | goto fail; |
| 305 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 306 | |
Michal Vasko | f22d5ff | 2020-04-15 11:10:27 +0200 | [diff] [blame] | 307 | memset(&saddr, 0, sizeof(struct sockaddr_storage)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 308 | if (is_ipv4) { |
| 309 | saddr4 = (struct sockaddr_in *)&saddr; |
| 310 | |
| 311 | saddr4->sin_family = AF_INET; |
| 312 | saddr4->sin_port = htons(port); |
| 313 | |
| 314 | if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 315 | ERR(NULL, "Failed to convert IPv4 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 316 | goto fail; |
| 317 | } |
| 318 | |
| 319 | if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 320 | ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 321 | goto fail; |
| 322 | } |
| 323 | |
| 324 | } else { |
| 325 | saddr6 = (struct sockaddr_in6 *)&saddr; |
| 326 | |
| 327 | saddr6->sin6_family = AF_INET6; |
| 328 | saddr6->sin6_port = htons(port); |
| 329 | |
| 330 | if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 331 | ERR(NULL, "Failed to convert IPv6 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 332 | goto fail; |
| 333 | } |
| 334 | |
| 335 | if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 336 | ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 337 | goto fail; |
| 338 | } |
| 339 | } |
| 340 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 341 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 342 | ERR(NULL, "Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 343 | goto fail; |
| 344 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 345 | return sock; |
| 346 | |
| 347 | fail: |
| 348 | if (sock > -1) { |
| 349 | close(sock); |
| 350 | } |
| 351 | |
| 352 | return -1; |
| 353 | } |
| 354 | |
Michal Vasko | c429a8e | 2024-01-15 15:04:57 +0100 | [diff] [blame] | 355 | /** |
| 356 | * @brief Create a listening socket (AF_UNIX). |
| 357 | * |
| 358 | * @param[in] opts The server options (unix permissions and address of the socket). |
| 359 | * @return Listening socket, -1 on error. |
| 360 | */ |
| 361 | static int |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 362 | nc_sock_listen_unix(const struct nc_server_unix_opts *opts) |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 363 | { |
| 364 | struct sockaddr_un sun; |
| 365 | int sock = -1; |
| 366 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 367 | if (strlen(opts->address) > sizeof(sun.sun_path) - 1) { |
| 368 | ERR(NULL, "Socket path \"%s\" is longer than maximum length %d.", opts->address, (int)(sizeof(sun.sun_path) - 1)); |
Michal Vasko | 93e96f1 | 2021-09-30 10:02:09 +0200 | [diff] [blame] | 369 | goto fail; |
| 370 | } |
| 371 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 372 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 373 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 374 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 375 | goto fail; |
| 376 | } |
| 377 | |
| 378 | memset(&sun, 0, sizeof(sun)); |
| 379 | sun.sun_family = AF_UNIX; |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 380 | snprintf(sun.sun_path, sizeof(sun.sun_path) - 1, "%s", opts->address); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 381 | |
| 382 | unlink(sun.sun_path); |
| 383 | if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 384 | ERR(NULL, "Could not bind \"%s\" (%s).", opts->address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 385 | goto fail; |
| 386 | } |
| 387 | |
| 388 | if (opts->mode != (mode_t)-1) { |
| 389 | if (chmod(sun.sun_path, opts->mode) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 390 | ERR(NULL, "Failed to set unix socket permissions (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 391 | goto fail; |
| 392 | } |
| 393 | } |
| 394 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 395 | if ((opts->uid != (uid_t)-1) || (opts->gid != (gid_t)-1)) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 396 | if (chown(sun.sun_path, opts->uid, opts->gid) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 397 | ERR(NULL, "Failed to set unix socket uid/gid (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 398 | goto fail; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 403 | ERR(NULL, "Unable to start listening on \"%s\" (%s).", opts->address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 404 | goto fail; |
| 405 | } |
| 406 | |
| 407 | return sock; |
| 408 | |
| 409 | fail: |
| 410 | if (sock > -1) { |
| 411 | close(sock); |
| 412 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 416 | /** |
| 417 | * @brief Evaluate socket name for AF_UNIX socket. |
| 418 | * @param[in] acc_sock_fd is file descriptor for the accepted socket (a nonnegative). |
| 419 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 420 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 421 | * @return 0 if the stream socket is unnamed. Parameter host is set to NULL. |
| 422 | * @return -1 in case of error. Parameter host is set to NULL. |
| 423 | */ |
| 424 | static int |
| 425 | sock_host_unix(int acc_sock_fd, char **host) |
| 426 | { |
| 427 | char *sun_path; |
| 428 | struct sockaddr_storage saddr; |
| 429 | socklen_t addr_len; |
| 430 | |
| 431 | *host = NULL; |
| 432 | saddr.ss_family = AF_UNIX; |
| 433 | addr_len = sizeof(saddr); |
| 434 | |
| 435 | if (getsockname(acc_sock_fd, (struct sockaddr *)&saddr, &addr_len)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 436 | ERR(NULL, "getsockname failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 437 | return -1; |
| 438 | } |
| 439 | |
| 440 | sun_path = ((struct sockaddr_un *)&saddr)->sun_path; |
| 441 | if (!sun_path) { |
| 442 | /* stream socket is unnamed */ |
| 443 | return 0; |
| 444 | } |
| 445 | |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 446 | NC_CHECK_ERRMEM_RET(!(*host = strdup(sun_path)), -1); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * @brief Evaluate socket name and port number for AF_INET socket. |
| 453 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 454 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 455 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 456 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 457 | * @return -1 in case of error. Parameter host is set to NULL and port is unchanged. |
| 458 | */ |
| 459 | static int |
| 460 | sock_host_inet(const struct sockaddr_in *addr, char **host, uint16_t *port) |
| 461 | { |
| 462 | *host = malloc(INET_ADDRSTRLEN); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 463 | NC_CHECK_ERRMEM_RET(!(*host), -1); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 464 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 465 | if (!inet_ntop(AF_INET, &addr->sin_addr, *host, INET_ADDRSTRLEN)) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 466 | ERR(NULL, "inet_ntop failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 467 | free(*host); |
| 468 | *host = NULL; |
| 469 | return -1; |
| 470 | } |
| 471 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 472 | *port = ntohs(addr->sin_port); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * @brief Evaluate socket name and port number for AF_INET6 socket. |
| 479 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 480 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 481 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 482 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 483 | * @return -1 in case of error. Parameter host is set to the NULL and port is unchanged. |
| 484 | */ |
| 485 | static int |
| 486 | sock_host_inet6(const struct sockaddr_in6 *addr, char **host, uint16_t *port) |
| 487 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 488 | *host = malloc(INET6_ADDRSTRLEN); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 489 | NC_CHECK_ERRMEM_RET(!(*host), -1); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 490 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 491 | if (!inet_ntop(AF_INET6, &addr->sin6_addr, *host, INET6_ADDRSTRLEN)) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 492 | ERR(NULL, "inet_ntop failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 493 | free(*host); |
| 494 | *host = NULL; |
| 495 | return -1; |
| 496 | } |
| 497 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 498 | *port = ntohs(addr->sin6_port); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 503 | int |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 504 | nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t *bind_lock, int timeout, char **host, |
| 505 | uint16_t *port, uint16_t *idx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 506 | { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 507 | uint16_t i, j, pfd_count, client_port; |
| 508 | char *client_address; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 509 | struct pollfd *pfd; |
| 510 | struct sockaddr_storage saddr; |
| 511 | socklen_t saddr_len = sizeof(saddr); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 512 | int ret, client_sock, sock = -1, flags; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 513 | |
| 514 | pfd = malloc(bind_count * sizeof *pfd); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 515 | NC_CHECK_ERRMEM_RET(!pfd, -1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 516 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 517 | /* LOCK */ |
| 518 | pthread_mutex_lock(bind_lock); |
| 519 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 520 | for (i = 0, pfd_count = 0; i < bind_count; ++i) { |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 521 | if (binds[i].sock < 0) { |
| 522 | /* invalid socket */ |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 523 | continue; |
| 524 | } |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 525 | if (binds[i].pollin) { |
| 526 | binds[i].pollin = 0; |
| 527 | /* leftover pollin */ |
| 528 | sock = binds[i].sock; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 529 | break; |
| 530 | } |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 531 | pfd[pfd_count].fd = binds[i].sock; |
| 532 | pfd[pfd_count].events = POLLIN; |
| 533 | pfd[pfd_count].revents = 0; |
| 534 | |
| 535 | ++pfd_count; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 538 | if (sock == -1) { |
| 539 | /* poll for a new connection */ |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 540 | ret = nc_poll(pfd, pfd_count, timeout); |
| 541 | if (ret < 1) { |
| 542 | free(pfd); |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 543 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 544 | /* UNLOCK */ |
| 545 | pthread_mutex_unlock(bind_lock); |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 546 | |
| 547 | return ret; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 548 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 549 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 550 | for (i = 0, j = 0; j < pfd_count; ++i, ++j) { |
| 551 | /* adjust i so that indices in binds and pfd always match */ |
| 552 | while (binds[i].sock != pfd[j].fd) { |
| 553 | ++i; |
| 554 | } |
| 555 | |
| 556 | if (pfd[j].revents & POLLIN) { |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 557 | --ret; |
| 558 | |
| 559 | if (!ret) { |
| 560 | /* the last socket with an event, use it */ |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 561 | sock = pfd[j].fd; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 562 | break; |
| 563 | } else { |
| 564 | /* just remember the event for next time */ |
| 565 | binds[i].pollin = 1; |
| 566 | } |
| 567 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | free(pfd); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 571 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 572 | ERRINT; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 573 | /* UNLOCK */ |
| 574 | pthread_mutex_unlock(bind_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 575 | return -1; |
| 576 | } |
| 577 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 578 | /* accept connection */ |
| 579 | client_sock = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
| 580 | if (client_sock < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 581 | ERR(NULL, "Accept failed (%s).", strerror(errno)); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 582 | /* UNLOCK */ |
| 583 | pthread_mutex_unlock(bind_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 584 | return -1; |
| 585 | } |
| 586 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 587 | /* make the socket non-blocking */ |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 588 | if (((flags = fcntl(client_sock, F_GETFL)) == -1) || (fcntl(client_sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 589 | ERR(NULL, "Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 590 | goto fail; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 593 | /* learn information about the client end */ |
| 594 | if (saddr.ss_family == AF_UNIX) { |
| 595 | if (sock_host_unix(client_sock, &client_address)) { |
| 596 | goto fail; |
| 597 | } |
| 598 | client_port = 0; |
| 599 | } else if (saddr.ss_family == AF_INET) { |
| 600 | if (sock_host_inet((struct sockaddr_in *)&saddr, &client_address, &client_port)) { |
| 601 | goto fail; |
| 602 | } |
| 603 | } else if (saddr.ss_family == AF_INET6) { |
| 604 | if (sock_host_inet6((struct sockaddr_in6 *)&saddr, &client_address, &client_port)) { |
| 605 | goto fail; |
| 606 | } |
| 607 | } else { |
| 608 | ERR(NULL, "Source host of an unknown protocol family."); |
| 609 | goto fail; |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 610 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 611 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 612 | if (saddr.ss_family == AF_UNIX) { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 613 | VRB(NULL, "Accepted a connection on %s.", binds[i].address); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 614 | } else { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 615 | VRB(NULL, "Accepted a connection on %s:%u from %s:%u.", binds[i].address, binds[i].port, client_address, client_port); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 618 | if (host) { |
| 619 | *host = client_address; |
| 620 | } else { |
| 621 | free(client_address); |
| 622 | } |
| 623 | if (port) { |
| 624 | *port = client_port; |
| 625 | } |
| 626 | if (idx) { |
| 627 | *idx = i; |
| 628 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 629 | /* UNLOCK */ |
| 630 | pthread_mutex_unlock(bind_lock); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 631 | return client_sock; |
| 632 | |
| 633 | fail: |
| 634 | close(client_sock); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 635 | /* UNLOCK */ |
| 636 | pthread_mutex_unlock(bind_lock); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 637 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 640 | API struct nc_server_reply * |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 641 | nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 642 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 643 | const char *identifier = NULL, *revision = NULL, *format = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 644 | char *model_data = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 645 | struct ly_out *out; |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 646 | const struct lys_module *module = NULL, *mod; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 647 | const struct lysp_submodule *submodule = NULL; |
| 648 | struct lyd_node *child, *err, *data = NULL; |
| 649 | LYS_OUTFORMAT outformat = 0; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 650 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 651 | LY_LIST_FOR(lyd_child(rpc), child) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 652 | if (!strcmp(child->schema->name, "identifier")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 653 | identifier = lyd_get_value(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 654 | } else if (!strcmp(child->schema->name, "version")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 655 | revision = lyd_get_value(child); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 656 | if (revision && (revision[0] == '\0')) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 657 | revision = NULL; |
Radek Krejci | 1afa779 | 2017-03-26 11:24:16 -0500 | [diff] [blame] | 658 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 659 | } else if (!strcmp(child->schema->name, "format")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 660 | format = lyd_get_value(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 661 | } |
| 662 | } |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 663 | VRB(session, "Module \"%s@%s\" was requested.", identifier, revision ? revision : "<any>"); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 664 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 665 | /* check revision */ |
| 666 | if (revision && (strlen(revision) != 10) && strcmp(revision, "1.0")) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 667 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 668 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 669 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 672 | if (revision) { |
| 673 | /* get specific module */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 674 | module = ly_ctx_get_module(session->ctx, identifier, revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 675 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 676 | submodule = ly_ctx_get_submodule(session->ctx, identifier, revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 677 | } |
| 678 | } else { |
| 679 | /* try to get implemented, then latest module */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 680 | module = ly_ctx_get_module_implemented(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 681 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 682 | module = ly_ctx_get_module_latest(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 683 | } |
| 684 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 685 | submodule = ly_ctx_get_submodule_latest(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 686 | } |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 687 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 688 | if (!module && !submodule) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 689 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 690 | nc_err_set_msg(err, "The requested module was not found.", "en"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 691 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* check format */ |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 695 | if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 696 | outformat = LYS_OUT_YANG; |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 697 | } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 698 | outformat = LYS_OUT_YIN; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 699 | } else { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 700 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 701 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 702 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 703 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 704 | |
| 705 | /* print */ |
| 706 | ly_out_new_memory(&model_data, 0, &out); |
| 707 | if (module) { |
| 708 | lys_print_module(out, module, outformat, 0, 0); |
| 709 | } else { |
| 710 | lys_print_submodule(out, submodule, outformat, 0, 0); |
| 711 | } |
| 712 | ly_out_free(out, NULL, 0); |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 713 | if (!model_data) { |
| 714 | ERRINT; |
| 715 | return NULL; |
| 716 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 717 | |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 718 | /* create reply */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 719 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring"); |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 720 | if (!mod || lyd_new_inner(NULL, mod, "get-schema", 0, &data)) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 721 | ERRINT; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 722 | free(model_data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 723 | return NULL; |
| 724 | } |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 725 | if (lyd_new_any(data, NULL, "data", model_data, LYD_ANYDATA_STRING, LYD_NEW_ANY_USE_VALUE | LYD_NEW_VAL_OUTPUT, NULL)) { |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 726 | ERRINT; |
Michal Vasko | a50f68e | 2022-02-24 16:10:54 +0100 | [diff] [blame] | 727 | free(model_data); |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 728 | lyd_free_tree(data); |
| 729 | return NULL; |
| 730 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 731 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 732 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 735 | API struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 736 | nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 737 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 738 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 739 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 740 | } |
| 741 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 742 | /** |
| 743 | * @brief Initialize a context with default RPC callbacks if none are set. |
| 744 | * |
| 745 | * @param[in] ctx Context to initialize. |
| 746 | */ |
| 747 | static void |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 748 | nc_server_init_cb_ctx(const struct ly_ctx *ctx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 749 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 750 | struct lysc_node *rpc; |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 751 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 752 | if (global_rpc_clb) { |
| 753 | /* expect it to handle these RPCs as well */ |
| 754 | return; |
| 755 | } |
| 756 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 757 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 758 | rpc = NULL; |
| 759 | if (ly_ctx_get_module_implemented(ctx, "ietf-netconf-monitoring")) { |
| 760 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0); |
| 761 | } |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 762 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 763 | rpc->priv = nc_clb_default_get_schema; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 766 | /* set default <close-session> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 767 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf:close-session", 0); |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 768 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 769 | rpc->priv = nc_clb_default_close_session; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 770 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 771 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 772 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 773 | API int |
| 774 | nc_server_init(void) |
| 775 | { |
Michal Vasko | 29f2f02 | 2024-03-13 09:06:48 +0100 | [diff] [blame] | 776 | pthread_rwlockattr_t *attr_p = NULL; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 777 | int r; |
| 778 | |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 779 | ATOMIC_STORE_RELAXED(server_opts.new_session_id, 1); |
| 780 | ATOMIC_STORE_RELAXED(server_opts.new_client_id, 1); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 781 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 782 | #ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP |
Michal Vasko | 29f2f02 | 2024-03-13 09:06:48 +0100 | [diff] [blame] | 783 | pthread_rwlockattr_t attr; |
| 784 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 785 | if ((r = pthread_rwlockattr_init(&attr))) { |
| 786 | ERR(NULL, "%s: failed init attribute (%s).", __func__, strerror(r)); |
| 787 | goto error; |
| 788 | } |
| 789 | attr_p = &attr; |
| 790 | if ((r = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP))) { |
| 791 | ERR(NULL, "%s: failed set attribute (%s).", __func__, strerror(r)); |
| 792 | goto error; |
| 793 | } |
Rosen Penev | ef2f3ac | 2019-07-15 18:15:28 -0700 | [diff] [blame] | 794 | #endif |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 795 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 796 | if ((r = pthread_rwlock_init(&server_opts.config_lock, attr_p))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 797 | ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r)); |
| 798 | goto error; |
| 799 | } |
| 800 | if ((r = pthread_rwlock_init(&server_opts.ch_client_lock, attr_p))) { |
| 801 | ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r)); |
| 802 | goto error; |
| 803 | } |
| 804 | |
| 805 | if (attr_p) { |
| 806 | pthread_rwlockattr_destroy(attr_p); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 807 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 808 | |
| 809 | #ifdef NC_ENABLED_SSH_TLS |
| 810 | if (curl_global_init(CURL_GLOBAL_SSL | CURL_GLOBAL_ACK_EINTR)) { |
| 811 | ERR(NULL, "%s: failed to init CURL.", __func__); |
| 812 | goto error; |
| 813 | } |
Michal Vasko | 5788c80 | 2024-05-03 16:14:40 +0200 | [diff] [blame] | 814 | |
| 815 | /* optional for dynamic library, mandatory for static */ |
| 816 | if (ssh_init()) { |
| 817 | ERR(NULL, "%s: failed to init libssh.", __func__); |
| 818 | goto error; |
| 819 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 820 | #endif |
| 821 | |
| 822 | if ((r = pthread_mutex_init(&server_opts.bind_lock, NULL))) { |
| 823 | ERR(NULL, "%s: failed to init bind lock(%s).", __func__, strerror(r)); |
| 824 | goto error; |
| 825 | } |
| 826 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 827 | return 0; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 828 | |
| 829 | error: |
| 830 | if (attr_p) { |
| 831 | pthread_rwlockattr_destroy(attr_p); |
| 832 | } |
| 833 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 836 | API void |
| 837 | nc_server_destroy(void) |
| 838 | { |
roman | a2ff4ef | 2024-01-19 14:41:46 +0100 | [diff] [blame] | 839 | uint32_t i, endpt_count; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 840 | |
| 841 | for (i = 0; i < server_opts.capabilities_count; i++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 842 | free(server_opts.capabilities[i]); |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 843 | } |
| 844 | free(server_opts.capabilities); |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 845 | server_opts.capabilities = NULL; |
| 846 | server_opts.capabilities_count = 0; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 847 | if (server_opts.content_id_data && server_opts.content_id_data_free) { |
| 848 | server_opts.content_id_data_free(server_opts.content_id_data); |
| 849 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 850 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 851 | nc_server_config_listen(NULL, NC_OP_DELETE); |
| 852 | nc_server_config_ch(NULL, NC_OP_DELETE); |
| 853 | |
roman | a2ff4ef | 2024-01-19 14:41:46 +0100 | [diff] [blame] | 854 | endpt_count = server_opts.endpt_count; |
| 855 | for (i = 0; i < endpt_count; i++) { |
| 856 | if (server_opts.endpts[i].ti == NC_TI_UNIX) { |
| 857 | _nc_server_del_endpt_unix_socket(&server_opts.endpts[i], &server_opts.binds[i]); |
| 858 | } |
| 859 | } |
| 860 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 861 | pthread_mutex_destroy(&server_opts.bind_lock); |
| 862 | |
| 863 | #ifdef NC_ENABLED_SSH_TLS |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 864 | free(server_opts.authkey_path_fmt); |
| 865 | server_opts.authkey_path_fmt = NULL; |
roman | 808f3f6 | 2023-11-23 16:01:04 +0100 | [diff] [blame] | 866 | free(server_opts.pam_config_name); |
| 867 | server_opts.pam_config_name = NULL; |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame] | 868 | if (server_opts.interactive_auth_data && server_opts.interactive_auth_data_free) { |
| 869 | server_opts.interactive_auth_data_free(server_opts.interactive_auth_data); |
| 870 | } |
| 871 | server_opts.interactive_auth_data = NULL; |
| 872 | server_opts.interactive_auth_data_free = NULL; |
| 873 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 874 | nc_server_config_ks_keystore(NULL, NC_OP_DELETE); |
| 875 | nc_server_config_ts_truststore(NULL, NC_OP_DELETE); |
| 876 | curl_global_cleanup(); |
Michal Vasko | 5788c80 | 2024-05-03 16:14:40 +0200 | [diff] [blame] | 877 | ssh_finalize(); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 878 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 881 | API int |
| 882 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 883 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 884 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 885 | ERRARG(NULL, "basic_mode"); |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 886 | return -1; |
| 887 | } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 888 | ERRARG(NULL, "also_supported"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 889 | return -1; |
| 890 | } |
| 891 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 892 | ATOMIC_STORE_RELAXED(server_opts.wd_basic_mode, basic_mode); |
| 893 | ATOMIC_STORE_RELAXED(server_opts.wd_also_supported, also_supported); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 897 | API void |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 898 | nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported) |
| 899 | { |
| 900 | if (!basic_mode && !also_supported) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 901 | ERRARG(NULL, "basic_mode and also_supported"); |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 902 | return; |
| 903 | } |
| 904 | |
| 905 | if (basic_mode) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 906 | *basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode); |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 907 | } |
| 908 | if (also_supported) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 909 | *also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported); |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 913 | API int |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 914 | nc_server_set_capability(const char *value) |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 915 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 916 | void *mem; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 917 | |
| 918 | if (!value || !value[0]) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 919 | ERRARG(NULL, "value must not be empty"); |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 920 | return EXIT_FAILURE; |
| 921 | } |
| 922 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 923 | mem = realloc(server_opts.capabilities, (server_opts.capabilities_count + 1) * sizeof *server_opts.capabilities); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 924 | NC_CHECK_ERRMEM_RET(!mem, EXIT_FAILURE); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 925 | server_opts.capabilities = mem; |
| 926 | |
| 927 | server_opts.capabilities[server_opts.capabilities_count] = strdup(value); |
| 928 | server_opts.capabilities_count++; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 929 | |
| 930 | return EXIT_SUCCESS; |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 933 | API void |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 934 | nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data, |
| 935 | void (*free_user_data)(void *user_data)) |
| 936 | { |
| 937 | server_opts.content_id_clb = content_id_clb; |
| 938 | server_opts.content_id_data = user_data; |
| 939 | server_opts.content_id_data_free = free_user_data; |
| 940 | } |
| 941 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 942 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 943 | nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx, struct nc_session **session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 944 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 945 | NC_MSG_TYPE msgtype; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 946 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 947 | |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 948 | NC_CHECK_ARG_RET(NULL, ctx, username, fdin >= 0, fdout >= 0, session, NC_MSG_ERROR); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 949 | |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 950 | NC_CHECK_SRV_INIT_RET(NC_MSG_ERROR); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 951 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 952 | /* init ctx as needed */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 953 | nc_server_init_cb_ctx(ctx); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 954 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 955 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 956 | *session = nc_new_session(NC_SERVER, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 957 | NC_CHECK_ERRMEM_RET(!(*session), NC_MSG_ERROR); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 958 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 959 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 960 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 961 | (*session)->ti_type = NC_TI_FD; |
| 962 | (*session)->ti.fd.in = fdin; |
| 963 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 964 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 965 | /* assign context */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 966 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 967 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 968 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 969 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 970 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 971 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 972 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 973 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 974 | if (msgtype != NC_MSG_HELLO) { |
| 975 | nc_session_free(*session, NULL); |
| 976 | *session = NULL; |
| 977 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 978 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 979 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 980 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 981 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 982 | nc_realtime_get(&ts_cur); |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 983 | (*session)->opts.server.session_start = ts_cur; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 984 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 985 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 986 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 987 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 988 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 989 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 990 | static void |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 991 | nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id) |
| 992 | { |
| 993 | uint8_t q_last; |
| 994 | |
| 995 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
| 996 | ERRINT; |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | /* get a unique queue value (by adding 1 to the last added value, if any) */ |
| 1001 | if (ps->queue_len) { |
| 1002 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 1003 | *id = ps->queue[q_last] + 1; |
| 1004 | } else { |
| 1005 | *id = 0; |
| 1006 | } |
| 1007 | |
| 1008 | /* add the id into the queue */ |
| 1009 | ++ps->queue_len; |
| 1010 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 1011 | ps->queue[q_last] = *id; |
| 1012 | } |
| 1013 | |
| 1014 | static void |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1015 | nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id) |
| 1016 | { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1017 | uint8_t i, q_idx, found = 0; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1018 | |
| 1019 | for (i = 0; i < ps->queue_len; ++i) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1020 | /* get the actual queue idx */ |
| 1021 | q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1022 | |
| 1023 | if (found) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1024 | if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1025 | /* another equal value, simply cannot be */ |
| 1026 | ERRINT; |
| 1027 | } |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1028 | if (found == 2) { |
| 1029 | /* move the following values */ |
| 1030 | ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx]; |
| 1031 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1032 | } else if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1033 | /* found our id, there can be no more equal valid values */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1034 | if (i == 0) { |
| 1035 | found = 1; |
| 1036 | } else { |
| 1037 | /* this is not okay, our id is in the middle of the queue */ |
| 1038 | found = 2; |
| 1039 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1040 | } |
| 1041 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1042 | if (!found) { |
| 1043 | ERRINT; |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1044 | return; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1045 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1046 | |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1047 | --ps->queue_len; |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1048 | if (found == 1) { |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1049 | /* remove the id by moving the queue, otherwise all the values in the queue were moved */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1050 | ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE; |
| 1051 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1054 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1055 | nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func) |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1056 | { |
| 1057 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1058 | struct timespec ts; |
| 1059 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1060 | /* LOCK */ |
Michal Vasko | 8c7def5 | 2023-06-06 14:48:56 +0200 | [diff] [blame] | 1061 | ret = pthread_mutex_lock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1062 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1063 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1064 | return -1; |
| 1065 | } |
| 1066 | |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1067 | /* check that the queue is long enough */ |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1068 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1069 | ERR(NULL, "%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE); |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1070 | pthread_mutex_unlock(&ps->lock); |
| 1071 | return -1; |
| 1072 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1073 | |
| 1074 | /* add ourselves into the queue */ |
| 1075 | nc_ps_queue_add_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1076 | DBL(NULL, "PS 0x%p TID %lu queue: added %u, head %u, length %u", ps, (long unsigned int)pthread_self(), *id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1077 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1078 | |
| 1079 | /* is it our turn? */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1080 | while (ps->queue[ps->queue_begin] != *id) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1081 | nc_timeouttime_get(&ts, NC_PS_QUEUE_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1082 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1083 | ret = pthread_cond_clockwait(&ps->cond, &ps->lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1084 | if (ret) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1085 | /** |
| 1086 | * This may happen when another thread releases the lock and broadcasts the condition |
| 1087 | * and this thread had already timed out. When this thread is scheduled, it returns timed out error |
| 1088 | * but when actually this thread was ready for condition. |
| 1089 | */ |
preetbhansali | 629dfc4 | 2018-12-17 16:04:40 +0530 | [diff] [blame] | 1090 | if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1091 | break; |
| 1092 | } |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1093 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1094 | ERR(NULL, "%s: failed to wait for a pollsession condition (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1095 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1096 | nc_ps_queue_remove_id(ps, *id); |
| 1097 | pthread_mutex_unlock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1098 | return -1; |
| 1099 | } |
| 1100 | } |
| 1101 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1102 | /* UNLOCK */ |
| 1103 | pthread_mutex_unlock(&ps->lock); |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1108 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1109 | nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func) |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1110 | { |
| 1111 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1112 | |
| 1113 | /* LOCK */ |
Michal Vasko | 8c7def5 | 2023-06-06 14:48:56 +0200 | [diff] [blame] | 1114 | ret = pthread_mutex_lock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1115 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1116 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1117 | ret = -1; |
| 1118 | } |
| 1119 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1120 | /* we must be the first, it was our turn after all, right? */ |
| 1121 | if (ps->queue[ps->queue_begin] != id) { |
| 1122 | ERRINT; |
Michal Vasko | b1a094b | 2016-10-05 14:04:52 +0200 | [diff] [blame] | 1123 | /* UNLOCK */ |
| 1124 | if (!ret) { |
| 1125 | pthread_mutex_unlock(&ps->lock); |
| 1126 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1127 | return -1; |
| 1128 | } |
| 1129 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1130 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1131 | nc_ps_queue_remove_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1132 | DBL(NULL, "PS 0x%p TID %lu queue: removed %u, head %u, length %u", ps, (long unsigned int)pthread_self(), id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1133 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* broadcast to all other threads that the queue moved */ |
| 1136 | pthread_cond_broadcast(&ps->cond); |
| 1137 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1138 | /* UNLOCK */ |
| 1139 | if (!ret) { |
| 1140 | pthread_mutex_unlock(&ps->lock); |
| 1141 | } |
| 1142 | |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1146 | API struct nc_pollsession * |
| 1147 | nc_ps_new(void) |
| 1148 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1149 | struct nc_pollsession *ps; |
| 1150 | |
| 1151 | ps = calloc(1, sizeof(struct nc_pollsession)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1152 | NC_CHECK_ERRMEM_RET(!ps, NULL); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1153 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1154 | pthread_mutex_init(&ps->lock, NULL); |
| 1155 | |
| 1156 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | API void |
| 1160 | nc_ps_free(struct nc_pollsession *ps) |
| 1161 | { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1162 | uint16_t i; |
| 1163 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1164 | if (!ps) { |
| 1165 | return; |
| 1166 | } |
| 1167 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1168 | if (ps->queue_len) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1169 | ERR(NULL, "FATAL: Freeing a pollsession structure that is currently being worked with!"); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1172 | for (i = 0; i < ps->session_count; i++) { |
| 1173 | free(ps->sessions[i]); |
| 1174 | } |
| 1175 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1176 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1177 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1178 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1179 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1180 | free(ps); |
| 1181 | } |
| 1182 | |
| 1183 | API int |
| 1184 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1185 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1186 | uint8_t q_id; |
| 1187 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1188 | NC_CHECK_ARG_RET(session, ps, session, -1); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1189 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1190 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1191 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1192 | return -1; |
| 1193 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1194 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1195 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1196 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1197 | if (!ps->sessions) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1198 | ERRMEM; |
| 1199 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1200 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1201 | return -1; |
| 1202 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1203 | ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions); |
| 1204 | if (!ps->sessions[ps->session_count - 1]) { |
| 1205 | ERRMEM; |
| 1206 | --ps->session_count; |
| 1207 | /* UNLOCK */ |
| 1208 | nc_ps_unlock(ps, q_id, __func__); |
| 1209 | return -1; |
| 1210 | } |
| 1211 | ps->sessions[ps->session_count - 1]->session = session; |
| 1212 | ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1213 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1214 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1215 | return nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1218 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1219 | _nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1220 | { |
| 1221 | uint16_t i; |
| 1222 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1223 | if (index >= 0) { |
| 1224 | i = (uint16_t)index; |
| 1225 | goto remove; |
| 1226 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1227 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1228 | if (ps->sessions[i]->session == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1229 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1230 | --ps->session_count; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1231 | if (i <= ps->session_count) { |
| 1232 | free(ps->sessions[i]); |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1233 | ps->sessions[i] = ps->sessions[ps->session_count]; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1234 | } |
| 1235 | if (!ps->session_count) { |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1236 | free(ps->sessions); |
| 1237 | ps->sessions = NULL; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1238 | } |
Michal Vasko | 11d2f6a | 2017-02-02 11:15:06 +0100 | [diff] [blame] | 1239 | ps->last_event_session = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1240 | return 0; |
| 1241 | } |
| 1242 | } |
| 1243 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1244 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1245 | } |
| 1246 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1247 | API int |
| 1248 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1249 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1250 | uint8_t q_id; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1251 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1252 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1253 | NC_CHECK_ARG_RET(session, ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1254 | |
| 1255 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1256 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1257 | return -1; |
| 1258 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1259 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1260 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1261 | |
| 1262 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1263 | ret2 = nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1264 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1265 | return ret || ret2 ? -1 : 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1268 | API struct nc_session * |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1269 | nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx) |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1270 | { |
| 1271 | uint8_t q_id; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1272 | struct nc_session *ret = NULL; |
| 1273 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1274 | NC_CHECK_ARG_RET(NULL, ps, NULL); |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1275 | |
| 1276 | /* LOCK */ |
| 1277 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1281 | if (idx < ps->session_count) { |
| 1282 | ret = ps->sessions[idx]->session; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /* UNLOCK */ |
| 1286 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1287 | |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
Michal Vasko | 3ec3b11 | 2022-07-21 12:32:33 +0200 | [diff] [blame] | 1291 | API struct nc_session * |
| 1292 | nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data) |
| 1293 | { |
| 1294 | uint8_t q_id; |
| 1295 | uint16_t i; |
| 1296 | struct nc_session *ret = NULL; |
| 1297 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1298 | NC_CHECK_ARG_RET(NULL, ps, NULL); |
Michal Vasko | 3ec3b11 | 2022-07-21 12:32:33 +0200 | [diff] [blame] | 1299 | |
| 1300 | /* LOCK */ |
| 1301 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1302 | return NULL; |
| 1303 | } |
| 1304 | |
| 1305 | for (i = 0; i < ps->session_count; ++i) { |
| 1306 | if (match_cb(ps->sessions[i]->session, cb_data)) { |
| 1307 | ret = ps->sessions[i]->session; |
| 1308 | break; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /* UNLOCK */ |
| 1313 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1314 | |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1318 | API uint16_t |
| 1319 | nc_ps_session_count(struct nc_pollsession *ps) |
| 1320 | { |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1321 | uint8_t q_id; |
| 1322 | uint16_t session_count; |
| 1323 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1324 | NC_CHECK_ARG_RET(NULL, ps, 0); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1325 | |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1326 | /* LOCK (just for memory barrier so that we read the current value) */ |
| 1327 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | session_count = ps->session_count; |
| 1332 | |
| 1333 | /* UNLOCK */ |
| 1334 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1335 | |
| 1336 | return session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1339 | static NC_MSG_TYPE |
| 1340 | recv_rpc_check_msgid(struct nc_session *session, const struct lyd_node *envp) |
| 1341 | { |
| 1342 | struct lyd_attr *attr; |
| 1343 | |
| 1344 | assert(envp && !envp->schema); |
| 1345 | |
| 1346 | /* find the message-id attribute */ |
| 1347 | LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) { |
| 1348 | if (!strcmp(attr->name.name, "message-id")) { |
| 1349 | break; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (!attr) { |
| 1354 | ERR(session, "Received an <rpc> without a message-id."); |
| 1355 | return NC_MSG_REPLY_ERR_MSGID; |
| 1356 | } |
| 1357 | |
| 1358 | return NC_MSG_RPC; |
| 1359 | } |
| 1360 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1361 | /* should be called holding the session RPC lock! IO lock will be acquired as needed |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1362 | * returns: NC_PSPOLL_ERROR, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1363 | * NC_PSPOLL_TIMEOUT, |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1364 | * NC_PSPOLL_BAD_RPC (| NC_PSPOLL_REPLY_ERROR), |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1365 | * NC_PSPOLL_RPC |
| 1366 | */ |
| 1367 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1368 | nc_server_recv_rpc_io(struct nc_session *session, int io_timeout, struct nc_server_rpc **rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1369 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1370 | struct ly_in *msg; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1371 | struct nc_server_reply *reply = NULL; |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1372 | struct lyd_node *e; |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1373 | int r, ret = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1374 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1375 | NC_CHECK_ARG_RET(session, session, rpc, NC_PSPOLL_ERROR); |
| 1376 | |
| 1377 | if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1378 | ERR(session, "Invalid session to receive RPCs."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1379 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1380 | } |
| 1381 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1382 | *rpc = NULL; |
| 1383 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1384 | /* get a message */ |
| 1385 | r = nc_read_msg_io(session, io_timeout, &msg, 0); |
| 1386 | if (r == -2) { |
| 1387 | /* malformed message */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1388 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG)); |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1389 | goto cleanup; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1390 | } |
| 1391 | if (r == -1) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1392 | return NC_PSPOLL_ERROR; |
| 1393 | } else if (!r) { |
| 1394 | return NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1397 | *rpc = calloc(1, sizeof **rpc); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1398 | NC_CHECK_ERRMEM_GOTO(!*rpc, ret = NC_PSPOLL_ERROR, cleanup); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1399 | |
| 1400 | /* parse the RPC */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1401 | if (!lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_RPC_NETCONF, &(*rpc)->envp, &(*rpc)->rpc)) { |
| 1402 | /* check message-id */ |
| 1403 | if (recv_rpc_check_msgid(session, (*rpc)->envp) == NC_MSG_RPC) { |
| 1404 | /* valid RPC */ |
| 1405 | ret = NC_PSPOLL_RPC; |
| 1406 | } else { |
| 1407 | /* no message-id */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1408 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MISSING_ATTR, NC_ERR_TYPE_RPC, "message-id", "rpc")); |
| 1409 | } |
| 1410 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1411 | /* bad RPC received */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1412 | if ((*rpc)->envp) { |
| 1413 | /* at least the envelopes were parsed */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1414 | e = nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
Michal Vasko | 58791da | 2024-02-26 13:52:59 +0100 | [diff] [blame] | 1415 | nc_err_set_msg(e, ly_err_last(session->ctx)->msg, "en"); |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1416 | reply = nc_server_reply_err(e); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1417 | } else if (session->version == NC_VERSION_11) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1418 | /* completely malformed message, NETCONF version 1.1 defines sending error reply from |
| 1419 | * the server (RFC 6241 sec. 3) */ |
| 1420 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG)); |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1421 | } else { |
| 1422 | /* at least set the return value */ |
| 1423 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1424 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | cleanup: |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1428 | if (reply) { |
| 1429 | /* send error reply */ |
| 1430 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, *rpc ? (*rpc)->envp : NULL, reply); |
| 1431 | nc_server_reply_free(reply); |
| 1432 | if (r != NC_MSG_REPLY) { |
| 1433 | ERR(session, "Failed to write reply (%s), terminating session.", nc_msgtype2str[r]); |
| 1434 | if (session->status != NC_STATUS_INVALID) { |
| 1435 | session->status = NC_STATUS_INVALID; |
| 1436 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1437 | } |
| 1438 | } |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1439 | |
| 1440 | /* bad RPC and an error reply sent */ |
| 1441 | ret = NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR; |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1444 | ly_in_free(msg, 1); |
| 1445 | if (ret != NC_PSPOLL_RPC) { |
| 1446 | nc_server_rpc_free(*rpc); |
| 1447 | *rpc = NULL; |
| 1448 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1449 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1452 | API void |
| 1453 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1454 | { |
| 1455 | global_rpc_clb = clb; |
| 1456 | } |
| 1457 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1458 | API NC_MSG_TYPE |
| 1459 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1460 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1461 | NC_MSG_TYPE ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1462 | |
| 1463 | /* check parameters */ |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1464 | if (!session || (session->side != NC_SERVER) || !nc_session_get_notif_status(session)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1465 | ERRARG(NULL, "session"); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1466 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1467 | } else if (!notif || !notif->ntf || !notif->eventtime) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1468 | ERRARG(NULL, "notif"); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1469 | return NC_MSG_ERROR; |
| 1470 | } |
| 1471 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1472 | /* we do not need RPC lock for this, IO lock will be acquired properly */ |
| 1473 | ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif); |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 1474 | if (ret != NC_MSG_NOTIF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1475 | ERR(session, "Failed to write notification (%s).", nc_msgtype2str[ret]); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1476 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1477 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1478 | return ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
Michal Vasko | f946776 | 2023-03-28 09:02:08 +0200 | [diff] [blame] | 1481 | /** |
| 1482 | * @brief Send a reply acquiring IO lock as needed. |
| 1483 | * Session RPC lock must be held! |
| 1484 | * |
| 1485 | * @param[in] session Session to use. |
| 1486 | * @param[in] io_timeout Timeout to use for acquiring IO lock. |
| 1487 | * @param[in] rpc RPC to sent. |
| 1488 | * @return 0 on success. |
| 1489 | * @return Bitmask of NC_PSPOLL_ERROR (any fatal error) and NC_PSPOLL_REPLY_ERROR (reply failed to be sent). |
| 1490 | * @return NC_PSPOLL_ERROR on other errors. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1491 | */ |
| 1492 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1493 | nc_server_send_reply_io(struct nc_session *session, int io_timeout, const struct nc_server_rpc *rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1494 | { |
| 1495 | nc_rpc_clb clb; |
| 1496 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1497 | const struct lysc_node *rpc_act = NULL; |
| 1498 | struct lyd_node *elem; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1499 | int ret = 0; |
| 1500 | NC_MSG_TYPE r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1501 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1502 | if (!rpc) { |
| 1503 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1504 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1505 | } |
| 1506 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1507 | if (rpc->rpc->schema->nodetype == LYS_RPC) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1508 | /* RPC */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1509 | rpc_act = rpc->rpc->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1510 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1511 | /* action */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1512 | LYD_TREE_DFS_BEGIN(rpc->rpc, elem) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1513 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1514 | rpc_act = elem->schema; |
| 1515 | break; |
| 1516 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1517 | LYD_TREE_DFS_END(rpc->rpc, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1518 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1519 | if (!rpc_act) { |
| 1520 | ERRINT; |
| 1521 | return NC_PSPOLL_ERROR; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | if (!rpc_act->priv) { |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1526 | if (!global_rpc_clb) { |
| 1527 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1528 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT)); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1529 | } else { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1530 | reply = global_rpc_clb(rpc->rpc, session); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1531 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1532 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1533 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1534 | reply = clb(rpc->rpc, session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | if (!reply) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1538 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1539 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1540 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, rpc->envp, reply); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1541 | if (reply->type == NC_RPL_ERROR) { |
| 1542 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1543 | } |
| 1544 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1545 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1546 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 1546949 | 2021-06-09 08:40:48 +0200 | [diff] [blame] | 1547 | ERR(session, "Failed to write reply (%s).", nc_msgtype2str[r]); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1548 | ret |= NC_PSPOLL_ERROR; |
| 1549 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1550 | |
| 1551 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1552 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1553 | session->status = NC_STATUS_INVALID; |
| 1554 | } |
| 1555 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1556 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1557 | } |
| 1558 | |
Michal Vasko | f946776 | 2023-03-28 09:02:08 +0200 | [diff] [blame] | 1559 | /** |
| 1560 | * @brief Poll a session from pspoll acquiring IO lock as needed. |
| 1561 | * Session must be running and session RPC lock held! |
| 1562 | * |
| 1563 | * @param[in] session Session to use. |
| 1564 | * @param[in] io_timeout Timeout to use for acquiring IO lock. |
| 1565 | * @param[in] now_mono Current monotonic timestamp. |
| 1566 | * @param[in,out] msg Message to fill in case of an error. |
| 1567 | * @return NC_PSPOLL_RPC if some application data are available. |
| 1568 | * @return NC_PSPOLL_TIMEOUT if a timeout elapsed. |
| 1569 | * @return NC_PSPOLL_SSH_CHANNEL if a new SSH channel has been created. |
| 1570 | * @return NC_PSPOLL_SSH_MSG if just an SSH message has been processed. |
| 1571 | * @return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR if session has been terminated (@p msg filled). |
| 1572 | * @return NC_PSPOLL_ERROR on other fatal errors (@p msg filled). |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1573 | */ |
| 1574 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1575 | nc_ps_poll_session_io(struct nc_session *session, int io_timeout, time_t now_mono, char *msg) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1576 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1577 | struct pollfd pfd; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 1578 | int r, ret = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1579 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1580 | #ifdef NC_ENABLED_SSH_TLS |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 1581 | ssh_message ssh_msg; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1582 | struct nc_session *new; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1583 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1584 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1585 | /* check timeout first */ |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1586 | if (!(session->flags & NC_SESSION_CALLHOME) && !nc_session_get_notif_status(session) && server_opts.idle_timeout && |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1587 | (now_mono >= session->opts.server.last_rpc + (unsigned) server_opts.idle_timeout)) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1588 | sprintf(msg, "Session idle timeout elapsed"); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1589 | session->status = NC_STATUS_INVALID; |
| 1590 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1591 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1592 | } |
| 1593 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1594 | r = nc_session_io_lock(session, io_timeout, __func__); |
| 1595 | if (r < 0) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1596 | sprintf(msg, "Session IO lock failed to be acquired"); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1597 | return NC_PSPOLL_ERROR; |
| 1598 | } else if (!r) { |
| 1599 | return NC_PSPOLL_TIMEOUT; |
| 1600 | } |
| 1601 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1602 | switch (session->ti_type) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1603 | #ifdef NC_ENABLED_SSH_TLS |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 1604 | case NC_TI_SSH: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1605 | ssh_msg = ssh_message_get(session->ti.libssh.session); |
| 1606 | if (ssh_msg) { |
| 1607 | nc_session_ssh_msg(session, NULL, ssh_msg, NULL); |
| 1608 | if (session->ti.libssh.next) { |
| 1609 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
| 1610 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel && |
| 1611 | (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1612 | /* new NETCONF SSH channel */ |
| 1613 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1614 | break; |
| 1615 | } |
| 1616 | } |
| 1617 | if (new != session) { |
| 1618 | ssh_message_free(ssh_msg); |
| 1619 | break; |
| 1620 | } |
| 1621 | } |
| 1622 | if (!ret) { |
| 1623 | /* just some SSH message */ |
| 1624 | ret = NC_PSPOLL_SSH_MSG; |
| 1625 | } |
| 1626 | ssh_message_free(ssh_msg); |
| 1627 | |
| 1628 | /* break because 1) we don't want to return anything here ORred with NC_PSPOLL_RPC |
| 1629 | * and 2) we don't want to delay openning a new channel by waiting for a RPC to get processed |
| 1630 | */ |
| 1631 | break; |
| 1632 | } |
| 1633 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1634 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1635 | if (r == SSH_EOF) { |
| 1636 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1637 | session->status = NC_STATUS_INVALID; |
| 1638 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1639 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1640 | } else if (r == SSH_ERROR) { |
| 1641 | sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1642 | session->status = NC_STATUS_INVALID; |
| 1643 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1644 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1645 | } else if (!r) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1646 | /* no application data received */ |
| 1647 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1648 | } else { |
| 1649 | /* we have some application data */ |
| 1650 | ret = NC_PSPOLL_RPC; |
| 1651 | } |
| 1652 | break; |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 1653 | case NC_TI_TLS: |
roman | ce43511 | 2024-04-23 15:12:09 +0200 | [diff] [blame] | 1654 | r = nc_tls_get_num_pending_bytes_wrap(session->ti.tls.session); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1655 | if (!r) { |
| 1656 | /* no data pending in the SSL buffer, poll fd */ |
roman | 9225e91 | 2024-04-05 12:33:09 +0200 | [diff] [blame] | 1657 | pfd.fd = nc_tls_get_fd_wrap(session); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1658 | if (pfd.fd < 0) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1659 | sprintf(msg, "Internal error (%s:%d)", __FILE__, __LINE__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1660 | ret = NC_PSPOLL_ERROR; |
| 1661 | break; |
| 1662 | } |
| 1663 | pfd.events = POLLIN; |
| 1664 | pfd.revents = 0; |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 1665 | r = nc_poll(&pfd, 1, 0); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1666 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 1667 | if (r < 0) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1668 | sprintf(msg, "Poll failed (%s)", strerror(errno)); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1669 | session->status = NC_STATUS_INVALID; |
| 1670 | ret = NC_PSPOLL_ERROR; |
| 1671 | } else if (r > 0) { |
| 1672 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1673 | sprintf(msg, "Communication socket unexpectedly closed"); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1674 | session->status = NC_STATUS_INVALID; |
| 1675 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1676 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1677 | } else if (pfd.revents & POLLERR) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1678 | sprintf(msg, "Communication socket error"); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1679 | session->status = NC_STATUS_INVALID; |
| 1680 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1681 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1682 | } else { |
| 1683 | ret = NC_PSPOLL_RPC; |
| 1684 | } |
| 1685 | } else { |
| 1686 | ret = NC_PSPOLL_TIMEOUT; |
| 1687 | } |
| 1688 | } else { |
| 1689 | ret = NC_PSPOLL_RPC; |
| 1690 | } |
| 1691 | break; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1692 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1693 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1694 | case NC_TI_UNIX: |
| 1695 | pfd.fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1696 | pfd.events = POLLIN; |
| 1697 | pfd.revents = 0; |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 1698 | r = nc_poll(&pfd, 1, 0); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1699 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 1700 | if (r < 0) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1701 | sprintf(msg, "Poll failed (%s)", strerror(errno)); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1702 | session->status = NC_STATUS_INVALID; |
| 1703 | ret = NC_PSPOLL_ERROR; |
| 1704 | } else if (r > 0) { |
| 1705 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1706 | sprintf(msg, "Communication socket unexpectedly closed"); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1707 | session->status = NC_STATUS_INVALID; |
| 1708 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1709 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1710 | } else if (pfd.revents & POLLERR) { |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1711 | sprintf(msg, "Communication socket error"); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1712 | session->status = NC_STATUS_INVALID; |
| 1713 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1714 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1715 | } else { |
| 1716 | ret = NC_PSPOLL_RPC; |
| 1717 | } |
| 1718 | } else { |
| 1719 | ret = NC_PSPOLL_TIMEOUT; |
| 1720 | } |
| 1721 | break; |
| 1722 | case NC_TI_NONE: |
Michal Vasko | 4607daf | 2024-01-15 15:05:15 +0100 | [diff] [blame] | 1723 | sprintf(msg, "Internal error (%s:%d)", __FILE__, __LINE__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1724 | ret = NC_PSPOLL_ERROR; |
| 1725 | break; |
| 1726 | } |
| 1727 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1728 | nc_session_io_unlock(session, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1729 | return ret; |
| 1730 | } |
| 1731 | |
| 1732 | API int |
| 1733 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1734 | { |
Michal Vasko | 443faa0 | 2022-10-20 09:09:03 +0200 | [diff] [blame] | 1735 | int ret = NC_PSPOLL_ERROR, r; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1736 | uint8_t q_id; |
| 1737 | uint16_t i, j; |
| 1738 | char msg[256]; |
| 1739 | struct timespec ts_timeout, ts_cur; |
| 1740 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1741 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1742 | struct nc_server_rpc *rpc = NULL; |
| 1743 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1744 | NC_CHECK_ARG_RET(NULL, ps, NC_PSPOLL_ERROR); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1745 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1746 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1747 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1748 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1749 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1750 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1751 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1752 | nc_ps_unlock(ps, q_id, __func__); |
| 1753 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1754 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1755 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1756 | /* fill timespecs */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1757 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1758 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1759 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1763 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1764 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1765 | if (ps->last_event_session == ps->session_count - 1) { |
| 1766 | i = j = 0; |
| 1767 | } else { |
| 1768 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1769 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1770 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1771 | cur_ps_session = ps->sessions[i]; |
| 1772 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1773 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1774 | /* SESSION RPC LOCK */ |
| 1775 | r = nc_session_rpc_lock(cur_session, 0, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1776 | if (r == -1) { |
| 1777 | ret = NC_PSPOLL_ERROR; |
| 1778 | } else if (r == 1) { |
| 1779 | /* no one else is currently working with the session, so we can, otherwise skip it */ |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1780 | switch (cur_ps_session->state) { |
| 1781 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1782 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1783 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1784 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1785 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1786 | ret = nc_ps_poll_session_io(cur_session, NC_SESSION_LOCK_TIMEOUT, ts_cur.tv_sec, msg); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1787 | switch (ret) { |
| 1788 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1789 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1790 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1791 | break; |
| 1792 | case NC_PSPOLL_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1793 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1794 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1795 | break; |
| 1796 | case NC_PSPOLL_TIMEOUT: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1797 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1798 | case NC_PSPOLL_SSH_CHANNEL: |
| 1799 | case NC_PSPOLL_SSH_MSG: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1800 | #endif /* NC_ENABLED_SSH_TLS */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1801 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1802 | break; |
| 1803 | case NC_PSPOLL_RPC: |
| 1804 | /* let's keep the state busy, we are not done with this session */ |
| 1805 | break; |
| 1806 | } |
| 1807 | } else { |
| 1808 | /* session is not fine, let the caller know */ |
| 1809 | ret = NC_PSPOLL_SESSION_TERM; |
| 1810 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1811 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1812 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1813 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1814 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1815 | break; |
| 1816 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1817 | /* it definitely should not be busy because we have the lock */ |
| 1818 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1819 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1820 | break; |
| 1821 | case NC_PS_STATE_INVALID: |
| 1822 | /* we got it locked, but it will be freed, let it be */ |
| 1823 | ret = NC_PSPOLL_TIMEOUT; |
| 1824 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1825 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1826 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1827 | /* keep RPC lock in this one case */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1828 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1829 | /* SESSION RPC UNLOCK */ |
| 1830 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1831 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1832 | } else { |
| 1833 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1834 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1835 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1836 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1837 | /* something happened */ |
| 1838 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1839 | break; |
| 1840 | } |
| 1841 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1842 | if (i == ps->session_count - 1) { |
| 1843 | i = 0; |
| 1844 | } else { |
| 1845 | ++i; |
| 1846 | } |
| 1847 | } while (i != j); |
| 1848 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1849 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1850 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1851 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1852 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1853 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1854 | /* final timeout */ |
| 1855 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1856 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1857 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1858 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1859 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1860 | /* do we want to return the session? */ |
| 1861 | switch (ret) { |
| 1862 | case NC_PSPOLL_RPC: |
| 1863 | case NC_PSPOLL_SESSION_TERM: |
| 1864 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1865 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1866 | case NC_PSPOLL_SSH_CHANNEL: |
| 1867 | case NC_PSPOLL_SSH_MSG: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1868 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1869 | if (session) { |
| 1870 | *session = cur_session; |
| 1871 | } |
| 1872 | ps->last_event_session = i; |
| 1873 | break; |
| 1874 | default: |
| 1875 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1876 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1877 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1878 | /* PS UNLOCK */ |
| 1879 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1880 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1881 | /* we have some data available and the session is RPC locked (but not IO locked) */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1882 | if (ret == NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1883 | ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1884 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1885 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1886 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1887 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1888 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1889 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1890 | } |
| 1891 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1892 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1893 | |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1894 | /* process RPC */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1895 | ret |= nc_server_send_reply_io(cur_session, timeout, rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1896 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1897 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1898 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1899 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1900 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1901 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1902 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1903 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1904 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1905 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1906 | nc_server_rpc_free(rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1907 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1908 | /* SESSION RPC UNLOCK */ |
| 1909 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1912 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1915 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1916 | nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *)) |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1917 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1918 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1919 | uint16_t i; |
| 1920 | struct nc_session *session; |
| 1921 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1922 | if (!ps) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1923 | ERRARG(NULL, "ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1924 | return; |
| 1925 | } |
| 1926 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1927 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1928 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1929 | return; |
| 1930 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1931 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1932 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1933 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1934 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1935 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1936 | } |
| 1937 | free(ps->sessions); |
| 1938 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1939 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1940 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1941 | } else { |
| 1942 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1943 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1944 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1945 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1946 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1947 | continue; |
| 1948 | } |
| 1949 | |
| 1950 | ++i; |
| 1951 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1952 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1953 | |
| 1954 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1955 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 1958 | int |
| 1959 | nc_server_set_address_port(struct nc_endpt *endpt, struct nc_bind *bind, const char *address, uint16_t port) |
| 1960 | { |
| 1961 | int sock = -1, set_addr, ret = 0; |
| 1962 | |
| 1963 | assert((address && !port) || (!address && port) || (endpt->ti == NC_TI_UNIX)); |
| 1964 | |
| 1965 | if (address) { |
| 1966 | set_addr = 1; |
| 1967 | } else { |
| 1968 | set_addr = 0; |
| 1969 | } |
| 1970 | |
| 1971 | if (set_addr) { |
| 1972 | port = bind->port; |
| 1973 | } else { |
| 1974 | address = bind->address; |
| 1975 | } |
| 1976 | |
| 1977 | /* we have all the information we need to create a listening socket */ |
| 1978 | if ((address && port) || (endpt->ti == NC_TI_UNIX)) { |
| 1979 | /* create new socket, close the old one */ |
| 1980 | if (endpt->ti == NC_TI_UNIX) { |
| 1981 | sock = nc_sock_listen_unix(endpt->opts.unixsock); |
| 1982 | } else { |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 1983 | sock = nc_sock_listen_inet(address, port); |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | if (sock == -1) { |
| 1987 | ret = 1; |
| 1988 | goto cleanup; |
| 1989 | } |
| 1990 | |
| 1991 | if (bind->sock > -1) { |
| 1992 | close(bind->sock); |
| 1993 | } |
| 1994 | bind->sock = sock; |
| 1995 | } |
| 1996 | |
| 1997 | if (sock > -1) { |
| 1998 | switch (endpt->ti) { |
| 1999 | case NC_TI_UNIX: |
| 2000 | VRB(NULL, "Listening on %s for UNIX connections.", endpt->opts.unixsock->address); |
| 2001 | break; |
| 2002 | #ifdef NC_ENABLED_SSH_TLS |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 2003 | case NC_TI_SSH: |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 2004 | VRB(NULL, "Listening on %s:%u for SSH connections.", address, port); |
| 2005 | break; |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 2006 | case NC_TI_TLS: |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 2007 | VRB(NULL, "Listening on %s:%u for TLS connections.", address, port); |
| 2008 | break; |
| 2009 | #endif /* NC_ENABLED_SSH_TLS */ |
| 2010 | default: |
| 2011 | ERRINT; |
| 2012 | ret = 1; |
| 2013 | break; |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | cleanup: |
| 2018 | return ret; |
| 2019 | } |
| 2020 | |
Michal Vasko | 29f2f02 | 2024-03-13 09:06:48 +0100 | [diff] [blame] | 2021 | #if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID) |
| 2022 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2023 | /** |
| 2024 | * @brief Get UID of the owner of a socket. |
| 2025 | * |
| 2026 | * @param[in] sock Socket to analyze. |
| 2027 | * @param[out] uid Socket owner UID. |
| 2028 | * @return 0 on success, |
| 2029 | * @return -1 on error. |
| 2030 | */ |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2031 | static int |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 2032 | nc_get_uid(int sock, uid_t *uid) |
| 2033 | { |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2034 | int r; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 2035 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2036 | #ifdef SO_PEERCRED |
| 2037 | struct ucred ucred; |
| 2038 | socklen_t len; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 2039 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2040 | len = sizeof(ucred); |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2041 | r = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len); |
| 2042 | if (!r) { |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2043 | *uid = ucred.uid; |
| 2044 | } |
| 2045 | #else |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2046 | r = getpeereid(sock, uid, NULL); |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2047 | #endif |
| 2048 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2049 | if (r < 0) { |
| 2050 | ERR(NULL, "Failed to get owner UID of a UNIX socket (%s).", strerror(errno)); |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2051 | return -1; |
| 2052 | } |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 2053 | return 0; |
| 2054 | } |
| 2055 | |
Michal Vasko | 29f2f02 | 2024-03-13 09:06:48 +0100 | [diff] [blame] | 2056 | #endif |
| 2057 | |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 2058 | static int |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2059 | nc_accept_unix(struct nc_session *session, int sock) |
| 2060 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2061 | #if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID) |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 2062 | struct passwd *pw, pw_buf; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2063 | char *username; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 2064 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2065 | session->ti_type = NC_TI_UNIX; |
Michal Vasko | 143aa14 | 2021-10-01 15:31:48 +0200 | [diff] [blame] | 2066 | uid_t uid = 0; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 2067 | char *buf = NULL; |
| 2068 | size_t buf_len = 0; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2069 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2070 | if (nc_get_uid(sock, &uid)) { |
| 2071 | close(sock); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2072 | return -1; |
| 2073 | } |
| 2074 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 2075 | pw = nc_getpw(uid, NULL, &pw_buf, &buf, &buf_len); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2076 | if (pw == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2077 | ERR(NULL, "Failed to find username for uid=%u (%s).\n", uid, strerror(errno)); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2078 | close(sock); |
| 2079 | return -1; |
| 2080 | } |
| 2081 | |
| 2082 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 2083 | free(buf); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2084 | if (username == NULL) { |
| 2085 | ERRMEM; |
| 2086 | close(sock); |
| 2087 | return -1; |
| 2088 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2089 | session->username = username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2090 | |
| 2091 | session->ti.unixsock.sock = sock; |
| 2092 | |
| 2093 | return 1; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 2094 | #else |
Michal Vasko | 29f2f02 | 2024-03-13 09:06:48 +0100 | [diff] [blame] | 2095 | (void)session; |
| 2096 | (void)sock; |
| 2097 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 2098 | return -1; |
| 2099 | #endif |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2102 | API int |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 2103 | nc_server_add_endpt_unix_socket_listen(const char *endpt_name, const char *unix_socket_path, mode_t mode, uid_t uid, gid_t gid) |
| 2104 | { |
| 2105 | int ret = 0; |
| 2106 | void *tmp; |
| 2107 | uint16_t i; |
| 2108 | |
| 2109 | NC_CHECK_ARG_RET(NULL, endpt_name, unix_socket_path, 1); |
| 2110 | |
| 2111 | /* CONFIG LOCK */ |
| 2112 | pthread_rwlock_wrlock(&server_opts.config_lock); |
| 2113 | |
| 2114 | /* check name uniqueness */ |
| 2115 | for (i = 0; i < server_opts.endpt_count; i++) { |
| 2116 | if (!strcmp(endpt_name, server_opts.endpts[i].name)) { |
| 2117 | ERR(NULL, "Endpoint \"%s\" already exists.", endpt_name); |
| 2118 | ret = 1; |
| 2119 | goto cleanup; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | /* alloc a new endpoint */ |
| 2124 | tmp = nc_realloc(server_opts.endpts, (server_opts.endpt_count + 1) * sizeof *server_opts.endpts); |
| 2125 | NC_CHECK_ERRMEM_GOTO(!tmp, ret = 1, cleanup); |
| 2126 | server_opts.endpts = tmp; |
| 2127 | memset(&server_opts.endpts[server_opts.endpt_count], 0, sizeof *server_opts.endpts); |
| 2128 | |
| 2129 | /* alloc a new bind */ |
| 2130 | tmp = nc_realloc(server_opts.binds, (server_opts.endpt_count + 1) * sizeof *server_opts.binds); |
| 2131 | NC_CHECK_ERRMEM_GOTO(!tmp, ret = 1, cleanup); |
| 2132 | server_opts.binds = tmp; |
| 2133 | memset(&server_opts.binds[server_opts.endpt_count], 0, sizeof *server_opts.binds); |
| 2134 | server_opts.binds[server_opts.endpt_count].sock = -1; |
| 2135 | server_opts.endpt_count++; |
| 2136 | |
| 2137 | /* set name and ti */ |
| 2138 | server_opts.endpts[server_opts.endpt_count - 1].name = strdup(endpt_name); |
| 2139 | NC_CHECK_ERRMEM_GOTO(!server_opts.endpts[server_opts.endpt_count - 1].name, ret = 1, cleanup); |
| 2140 | server_opts.endpts[server_opts.endpt_count - 1].ti = NC_TI_UNIX; |
| 2141 | |
| 2142 | /* set the bind data */ |
| 2143 | server_opts.binds[server_opts.endpt_count - 1].address = strdup(unix_socket_path); |
| 2144 | NC_CHECK_ERRMEM_GOTO(!server_opts.binds[server_opts.endpt_count - 1].address, ret = 1, cleanup); |
| 2145 | |
| 2146 | /* alloc unix opts */ |
| 2147 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 2148 | NC_CHECK_ERRMEM_GOTO(!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock, ret = 1, cleanup); |
| 2149 | |
| 2150 | /* set the opts data */ |
| 2151 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->address = strdup(unix_socket_path); |
| 2152 | NC_CHECK_ERRMEM_GOTO(!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->address, ret = 1, cleanup); |
| 2153 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode == (mode_t) -1) ? (mode_t) -1 : mode; |
| 2154 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid == (uid_t) -1) ? (uid_t) -1 : uid; |
| 2155 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid == (gid_t) -1) ? (gid_t) -1 : gid; |
| 2156 | |
| 2157 | /* start listening */ |
| 2158 | ret = nc_server_set_address_port(&server_opts.endpts[server_opts.endpt_count - 1], |
| 2159 | &server_opts.binds[server_opts.endpt_count - 1], NULL, 0); |
| 2160 | if (ret) { |
| 2161 | ERR(NULL, "Listening on UNIX socket \"%s\" failed.", unix_socket_path); |
| 2162 | goto cleanup; |
| 2163 | } |
| 2164 | |
| 2165 | cleanup: |
| 2166 | /* CONFIG UNLOCK */ |
| 2167 | pthread_rwlock_unlock(&server_opts.config_lock); |
| 2168 | return ret; |
| 2169 | } |
| 2170 | |
| 2171 | static void |
| 2172 | nc_server_del_endpt_unix_socket_opts(struct nc_bind *bind, struct nc_server_unix_opts *opts) |
| 2173 | { |
| 2174 | if (bind->sock > -1) { |
| 2175 | close(bind->sock); |
| 2176 | } |
| 2177 | |
| 2178 | unlink(bind->address); |
| 2179 | free(bind->address); |
| 2180 | free(opts->address); |
| 2181 | |
| 2182 | free(opts); |
| 2183 | } |
| 2184 | |
| 2185 | void |
| 2186 | _nc_server_del_endpt_unix_socket(struct nc_endpt *endpt, struct nc_bind *bind) |
| 2187 | { |
| 2188 | free(endpt->name); |
| 2189 | nc_server_del_endpt_unix_socket_opts(bind, endpt->opts.unixsock); |
| 2190 | |
| 2191 | server_opts.endpt_count--; |
| 2192 | if (!server_opts.endpt_count) { |
| 2193 | free(server_opts.endpts); |
| 2194 | free(server_opts.binds); |
| 2195 | server_opts.endpts = NULL; |
| 2196 | server_opts.binds = NULL; |
| 2197 | } else if (endpt != &server_opts.endpts[server_opts.endpt_count]) { |
| 2198 | memcpy(endpt, &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
| 2199 | memcpy(bind, &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | API void |
| 2204 | nc_server_del_endpt_unix_socket(const char *endpt_name) |
| 2205 | { |
| 2206 | uint16_t i; |
| 2207 | struct nc_endpt *endpt = NULL; |
| 2208 | struct nc_bind *bind; |
| 2209 | |
roman | b6ad37a | 2023-12-07 13:08:46 +0100 | [diff] [blame] | 2210 | NC_CHECK_ARG_RET(NULL, endpt_name, ); |
| 2211 | |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 2212 | /* CONFIG LOCK */ |
| 2213 | pthread_rwlock_wrlock(&server_opts.config_lock); |
| 2214 | |
roman | fb3f7cf | 2023-11-30 16:10:09 +0100 | [diff] [blame] | 2215 | for (i = 0; i < server_opts.endpt_count; i++) { |
| 2216 | if (!strcmp(server_opts.endpts[i].name, endpt_name)) { |
| 2217 | endpt = &server_opts.endpts[i]; |
| 2218 | bind = &server_opts.binds[i]; |
| 2219 | break; |
| 2220 | } |
| 2221 | } |
| 2222 | if (!endpt) { |
| 2223 | ERR(NULL, "Endpoint \"%s\" not found.", endpt_name); |
| 2224 | goto end; |
| 2225 | } |
| 2226 | if (endpt->ti != NC_TI_UNIX) { |
| 2227 | ERR(NULL, "Endpoint \"%s\" is not a UNIX socket endpoint.", endpt_name); |
| 2228 | goto end; |
| 2229 | } |
| 2230 | |
| 2231 | _nc_server_del_endpt_unix_socket(endpt, bind); |
| 2232 | |
| 2233 | end: |
| 2234 | /* CONFIG UNLOCK */ |
| 2235 | pthread_rwlock_unlock(&server_opts.config_lock); |
| 2236 | } |
| 2237 | |
| 2238 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2239 | nc_server_endpt_count(void) |
| 2240 | { |
| 2241 | return server_opts.endpt_count; |
| 2242 | } |
| 2243 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2244 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2245 | nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2246 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2247 | NC_MSG_TYPE msgtype; |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2248 | int sock = -1, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2249 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2250 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2251 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2252 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2253 | NC_CHECK_ARG_RET(NULL, ctx, session, NC_MSG_ERROR); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2254 | |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 2255 | NC_CHECK_SRV_INIT_RET(NC_MSG_ERROR); |
| 2256 | |
| 2257 | *session = NULL; |
| 2258 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2259 | /* init ctx as needed */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2260 | nc_server_init_cb_ctx(ctx); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2261 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2262 | /* CONFIG LOCK */ |
| 2263 | pthread_rwlock_rdlock(&server_opts.config_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2264 | |
| 2265 | if (!server_opts.endpt_count) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2266 | ERR(NULL, "No endpoints to accept sessions on."); |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2267 | msgtype = NC_MSG_ERROR; |
| 2268 | goto cleanup; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2269 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2270 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2271 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, &server_opts.bind_lock, timeout, &host, &port, &bind_idx); |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 2272 | if (ret < 1) { |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2273 | msgtype = (!ret ? NC_MSG_WOULDBLOCK : NC_MSG_ERROR); |
| 2274 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2275 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2276 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2277 | |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 2278 | /* configure keepalives */ |
| 2279 | if (nc_sock_configure_ka(sock, &server_opts.endpts[bind_idx].ka)) { |
| 2280 | msgtype = NC_MSG_ERROR; |
| 2281 | goto cleanup; |
| 2282 | } |
| 2283 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2284 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2285 | NC_CHECK_ERRMEM_GOTO(!(*session), msgtype = NC_MSG_ERROR, cleanup); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2286 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2287 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2288 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2289 | (*session)->host = host; |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2290 | host = NULL; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2291 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2292 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2293 | /* sock gets assigned to session or closed */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2294 | #ifdef NC_ENABLED_SSH_TLS |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 2295 | if (server_opts.endpts[bind_idx].ti == NC_TI_SSH) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2296 | ret = nc_accept_ssh_session(*session, server_opts.endpts[bind_idx].opts.ssh, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2297 | sock = -1; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2298 | if (ret < 0) { |
| 2299 | msgtype = NC_MSG_ERROR; |
| 2300 | goto cleanup; |
| 2301 | } else if (!ret) { |
| 2302 | msgtype = NC_MSG_WOULDBLOCK; |
| 2303 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2304 | } |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 2305 | } else if (server_opts.endpts[bind_idx].ti == NC_TI_TLS) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2306 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2307 | ret = nc_accept_tls_session(*session, server_opts.endpts[bind_idx].opts.tls, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2308 | sock = -1; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2309 | if (ret < 0) { |
| 2310 | msgtype = NC_MSG_ERROR; |
| 2311 | goto cleanup; |
| 2312 | } else if (!ret) { |
| 2313 | msgtype = NC_MSG_WOULDBLOCK; |
| 2314 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2315 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2316 | } else |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2317 | #endif /* NC_ENABLED_SSH_TLS */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2318 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2319 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2320 | ret = nc_accept_unix(*session, sock); |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2321 | sock = -1; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2322 | if (ret < 0) { |
| 2323 | msgtype = NC_MSG_ERROR; |
| 2324 | goto cleanup; |
| 2325 | } |
| 2326 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2327 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2328 | msgtype = NC_MSG_ERROR; |
| 2329 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2330 | } |
| 2331 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2332 | (*session)->data = NULL; |
| 2333 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2334 | /* CONFIG UNLOCK */ |
| 2335 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2336 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2337 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2338 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2339 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2340 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2341 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2342 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2343 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2344 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2345 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2346 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2347 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2348 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2349 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2350 | nc_realtime_get(&ts_cur); |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 2351 | (*session)->opts.server.session_start = ts_cur; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2352 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2353 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2354 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2355 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2356 | cleanup: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2357 | /* CONFIG UNLOCK */ |
| 2358 | pthread_rwlock_unlock(&server_opts.config_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2359 | |
Michal Vasko | 87b4256 | 2024-05-03 12:02:01 +0200 | [diff] [blame] | 2360 | free(host); |
| 2361 | if (sock > -1) { |
| 2362 | close(sock); |
| 2363 | } |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2364 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2365 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2366 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2369 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2370 | |
| 2371 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2372 | nc_server_ch_is_client(const char *name) |
| 2373 | { |
| 2374 | uint16_t i; |
| 2375 | int found = 0; |
| 2376 | |
| 2377 | if (!name) { |
| 2378 | return found; |
| 2379 | } |
| 2380 | |
| 2381 | /* READ LOCK */ |
| 2382 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2383 | |
| 2384 | /* check name uniqueness */ |
| 2385 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2386 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2387 | found = 1; |
| 2388 | break; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | /* UNLOCK */ |
| 2393 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2394 | |
| 2395 | return found; |
| 2396 | } |
| 2397 | |
| 2398 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2399 | nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name) |
| 2400 | { |
| 2401 | uint16_t i; |
| 2402 | struct nc_ch_client *client = NULL; |
| 2403 | int found = 0; |
| 2404 | |
| 2405 | if (!client_name || !endpt_name) { |
| 2406 | return found; |
| 2407 | } |
| 2408 | |
| 2409 | /* READ LOCK */ |
| 2410 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2411 | |
| 2412 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2413 | if (!strcmp(server_opts.ch_clients[i].name, client_name)) { |
| 2414 | client = &server_opts.ch_clients[i]; |
| 2415 | break; |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | if (!client) { |
| 2420 | goto cleanup; |
| 2421 | } |
| 2422 | |
| 2423 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2424 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2425 | found = 1; |
| 2426 | break; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | cleanup: |
| 2431 | /* UNLOCK */ |
| 2432 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2433 | return found; |
| 2434 | } |
| 2435 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 2436 | /** |
| 2437 | * @brief Create a connection for an endpoint. |
| 2438 | * |
| 2439 | * Client lock is expected to be held. |
| 2440 | * |
| 2441 | * @param[in] endpt Endpoint to use. |
| 2442 | * @param[in] acquire_ctx_cb Callback for acquiring the libyang context. |
| 2443 | * @param[in] release_ctx_cb Callback for releasing the libyang context. |
| 2444 | * @param[in] ctx_cb_data Context callbacks data. |
| 2445 | * @param[out] session Created NC session. |
| 2446 | * @return NC_MSG values. |
| 2447 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2448 | static NC_MSG_TYPE |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 2449 | nc_connect_ch_endpt(struct nc_ch_endpt *endpt, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 2450 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, struct nc_session **session) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2451 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2452 | NC_MSG_TYPE msgtype; |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 2453 | const struct ly_ctx *ctx = NULL; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2454 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2455 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 2456 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2457 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 2458 | sock = nc_sock_connect(endpt->address, endpt->port, NC_CH_CONNECT_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 2459 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2460 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2461 | } |
| 2462 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2463 | /* acquire context */ |
| 2464 | ctx = acquire_ctx_cb(ctx_cb_data); |
| 2465 | if (!ctx) { |
| 2466 | ERR(NULL, "Failed to acquire context for a new Call Home session."); |
| 2467 | close(sock); |
| 2468 | free(ip_host); |
| 2469 | return NC_MSG_ERROR; |
| 2470 | } |
| 2471 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2472 | /* init ctx as needed */ |
| 2473 | nc_server_init_cb_ctx(ctx); |
| 2474 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2475 | /* create session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2476 | *session = nc_new_session(NC_SERVER, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2477 | NC_CHECK_ERRMEM_GOTO(!(*session), close(sock); free(ip_host); msgtype = NC_MSG_ERROR, fail); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2478 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2479 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 2480 | (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2481 | (*session)->host = ip_host; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2482 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2483 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2484 | /* sock gets assigned to session or closed */ |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 2485 | if (endpt->ti == NC_TI_SSH) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2486 | ret = nc_accept_ssh_session(*session, endpt->opts.ssh, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2487 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2488 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2489 | if (ret < 0) { |
| 2490 | msgtype = NC_MSG_ERROR; |
| 2491 | goto fail; |
| 2492 | } else if (!ret) { |
| 2493 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2494 | goto fail; |
| 2495 | } |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 2496 | } else if (endpt->ti == NC_TI_TLS) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2497 | (*session)->data = endpt->opts.tls; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2498 | ret = nc_accept_tls_session(*session, endpt->opts.tls, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2499 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2500 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2501 | if (ret < 0) { |
| 2502 | msgtype = NC_MSG_ERROR; |
| 2503 | goto fail; |
| 2504 | } else if (!ret) { |
| 2505 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2506 | goto fail; |
| 2507 | } |
roman | 423cc0d | 2023-11-24 11:29:47 +0100 | [diff] [blame] | 2508 | } else { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2509 | ERRINT; |
| 2510 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2511 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2512 | goto fail; |
| 2513 | } |
| 2514 | |
| 2515 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2516 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2517 | |
| 2518 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2519 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2520 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2521 | goto fail; |
| 2522 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2523 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2524 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2525 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2526 | nc_realtime_get(&ts_cur); |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 2527 | (*session)->opts.server.session_start = ts_cur; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2528 | (*session)->status = NC_STATUS_RUNNING; |
| 2529 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2530 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2531 | |
| 2532 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2533 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2534 | *session = NULL; |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 2535 | if (ctx) { |
| 2536 | release_ctx_cb(ctx_cb_data); |
| 2537 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2538 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2539 | } |
| 2540 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2541 | /** |
| 2542 | * @brief Wait for any event after a NC session was established on a CH client. |
| 2543 | * |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2544 | * @param[in] data CH client thread argument. |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2545 | * @param[in] session New NC session. The session is invalid upon being freed (= function exit). |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2546 | * @return 0 if session was terminated normally, |
| 2547 | * @return 1 if the CH client was removed, |
| 2548 | * @return -1 on error. |
| 2549 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2550 | static int |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2551 | nc_server_ch_client_thread_session_cond_wait(struct nc_ch_client_thread_arg *data, struct nc_session *session) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2552 | { |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2553 | int rc = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2554 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2555 | struct timespec ts; |
| 2556 | struct nc_ch_client *client; |
| 2557 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2558 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 2559 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2560 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 2561 | session->flags |= NC_SESSION_CH_THREAD; |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 2562 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2563 | /* give the session to the user */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2564 | if (data->new_session_cb(data->client_name, session, data->new_session_cb_data)) { |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 2565 | /* something is wrong, free the session */ |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 2566 | session->flags &= ~NC_SESSION_CH_THREAD; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* CH UNLOCK */ |
| 2569 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 2570 | |
Michal Vasko | 77d56d7 | 2022-09-07 10:30:48 +0200 | [diff] [blame] | 2571 | /* session terminated, free it and release its context */ |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 2572 | nc_session_free(session, NULL); |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 2573 | data->release_ctx_cb(data->ctx_cb_data); |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2574 | return 0; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 2575 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2576 | |
| 2577 | do { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2578 | nc_timeouttime_get(&ts, NC_CH_THREAD_IDLE_TIMEOUT_SLEEP); |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2579 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 2580 | /* CH COND WAIT */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2581 | r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 2582 | if (!r) { |
| 2583 | /* we were woken up, something probably happened */ |
| 2584 | if (session->status != NC_STATUS_RUNNING) { |
| 2585 | break; |
| 2586 | } |
| 2587 | } else if (r != ETIMEDOUT) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2588 | ERR(session, "Pthread condition timedwait failed (%s).", strerror(r)); |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2589 | rc = -1; |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 2590 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2593 | /* check whether the client was not removed */ |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2594 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2595 | /* LOCK */ |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2596 | client = nc_server_ch_client_lock(data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2597 | if (!client) { |
| 2598 | /* client was removed, finish thread */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2599 | VRB(session, "Call Home client \"%s\" removed, but an established session will not be terminated.", |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2600 | data->client_name); |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2601 | rc = 1; |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 2602 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2603 | } |
| 2604 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2605 | if (client->conn_type == NC_CH_PERIOD) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2606 | idle_timeout = client->idle_timeout; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2607 | } else { |
| 2608 | idle_timeout = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2611 | nc_timeouttime_get(&ts, 0); |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 2612 | if (!nc_session_get_notif_status(session) && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2613 | VRB(session, "Call Home client \"%s\": session idle timeout elapsed.", client->name); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2614 | session->status = NC_STATUS_INVALID; |
| 2615 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 2616 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2617 | |
| 2618 | /* UNLOCK */ |
| 2619 | nc_server_ch_client_unlock(client); |
| 2620 | |
| 2621 | } while (session->status == NC_STATUS_RUNNING); |
| 2622 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 2623 | /* signal to nc_session_free() that CH thread is terminating */ |
| 2624 | session->flags &= ~NC_SESSION_CH_THREAD; |
| 2625 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 2626 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 2627 | /* CH UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 2628 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 2629 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2630 | return rc; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2633 | /** |
| 2634 | * @brief Waits for some amount of time while reacting to signals about terminating a Call Home thread. |
| 2635 | * |
| 2636 | * @param[in] session An established session. |
| 2637 | * @param[in] data Call Home thread's data. |
| 2638 | * @param[in] cond_wait_time Time in seconds to sleep for, after which a reconnect is attempted. |
| 2639 | * |
| 2640 | * @return 0 if the thread should stop running, 1 if it should continue. |
| 2641 | */ |
| 2642 | static int |
| 2643 | nc_server_ch_client_thread_is_running_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data, uint64_t cond_wait_time) |
| 2644 | { |
| 2645 | struct timespec ts; |
| 2646 | int ret = 0, thread_running; |
| 2647 | |
| 2648 | /* COND LOCK */ |
| 2649 | pthread_mutex_lock(&data->cond_lock); |
| 2650 | /* get reconnect timeout in ms */ |
| 2651 | nc_timeouttime_get(&ts, cond_wait_time * 1000); |
| 2652 | while (!ret && data->thread_running) { |
| 2653 | ret = pthread_cond_clockwait(&data->cond, &data->cond_lock, COMPAT_CLOCK_ID, &ts); |
| 2654 | } |
| 2655 | |
| 2656 | thread_running = data->thread_running; |
| 2657 | /* COND UNLOCK */ |
| 2658 | pthread_mutex_unlock(&data->cond_lock); |
| 2659 | |
| 2660 | if (!thread_running) { |
| 2661 | /* thread is terminating */ |
| 2662 | VRB(session, "Call Home thread signaled to exit, client \"%s\" probably removed.", data->client_name); |
| 2663 | ret = 0; |
| 2664 | } else if (ret == ETIMEDOUT) { |
| 2665 | /* time to reconnect */ |
| 2666 | VRB(session, "Call Home client \"%s\" timeout of %" PRIu64 " seconds expired, reconnecting.", data->client_name, cond_wait_time); |
| 2667 | ret = 1; |
| 2668 | } else if (ret) { |
| 2669 | ERR(session, "Pthread condition timedwait failed (%s).", strerror(ret)); |
| 2670 | ret = 0; |
| 2671 | } |
| 2672 | |
| 2673 | return ret; |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * @brief Checks if a Call Home thread should terminate. |
| 2678 | * |
| 2679 | * Checks the shared boolean variable thread_running. This should be done everytime |
| 2680 | * before entering a critical section. |
| 2681 | * |
| 2682 | * @param[in] data Call Home thread's data. |
| 2683 | * |
| 2684 | * @return 0 if the thread should stop running, -1 if it can continue. |
| 2685 | */ |
| 2686 | static int |
| 2687 | nc_server_ch_client_thread_is_running(struct nc_ch_client_thread_arg *data) |
| 2688 | { |
| 2689 | int ret = -1; |
| 2690 | |
| 2691 | /* COND LOCK */ |
| 2692 | pthread_mutex_lock(&data->cond_lock); |
| 2693 | if (!data->thread_running) { |
| 2694 | /* thread should stop running */ |
| 2695 | ret = 0; |
| 2696 | } |
| 2697 | /* COND UNLOCK */ |
| 2698 | pthread_mutex_unlock(&data->cond_lock); |
| 2699 | |
| 2700 | return ret; |
| 2701 | } |
| 2702 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2703 | /** |
| 2704 | * @brief Lock CH client structures for reading and lock the specific client if it has some endpoints, wait otherwise. |
| 2705 | * |
| 2706 | * @param[in] name Name of the CH client. |
| 2707 | * @return Pointer to the CH client. |
| 2708 | */ |
| 2709 | static struct nc_ch_client * |
| 2710 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 2711 | { |
| 2712 | struct nc_ch_client *client; |
| 2713 | |
| 2714 | while (1) { |
| 2715 | /* LOCK */ |
| 2716 | client = nc_server_ch_client_lock(name); |
| 2717 | if (!client) { |
| 2718 | return NULL; |
| 2719 | } |
| 2720 | if (client->ch_endpt_count) { |
| 2721 | return client; |
| 2722 | } |
| 2723 | /* no endpoints defined yet */ |
| 2724 | |
| 2725 | /* UNLOCK */ |
| 2726 | nc_server_ch_client_unlock(client); |
| 2727 | |
| 2728 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 2729 | } |
| 2730 | |
| 2731 | return NULL; |
| 2732 | } |
| 2733 | |
| 2734 | /** |
| 2735 | * @brief Call Home client management thread. |
| 2736 | * |
| 2737 | * @param[in] arg CH client thread argument. |
| 2738 | * @return NULL. |
| 2739 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2740 | static void * |
| 2741 | nc_ch_client_thread(void *arg) |
| 2742 | { |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2743 | struct nc_ch_client_thread_arg *data = arg; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2744 | NC_MSG_TYPE msgtype; |
| 2745 | uint8_t cur_attempts = 0; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2746 | uint16_t next_endpt_index, max_wait; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 2747 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2748 | struct nc_ch_endpt *cur_endpt; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2749 | struct nc_session *session = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2750 | struct nc_ch_client *client; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2751 | uint32_t reconnect_in; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2752 | |
| 2753 | /* LOCK */ |
| 2754 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2755 | if (!client) { |
| 2756 | VRB(NULL, "Call Home client \"%s\" removed.", data->client_name); |
| 2757 | goto cleanup; |
| 2758 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2759 | |
| 2760 | cur_endpt = &client->ch_endpts[0]; |
| 2761 | cur_endpt_name = strdup(cur_endpt->name); |
| 2762 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2763 | while (nc_server_ch_client_thread_is_running(data)) { |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 2764 | if (!cur_attempts) { |
| 2765 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" connecting...", data->client_name, cur_endpt_name); |
| 2766 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2767 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2768 | msgtype = nc_connect_ch_endpt(cur_endpt, data->acquire_ctx_cb, data->release_ctx_cb, data->ctx_cb_data, &session); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2769 | if (msgtype == NC_MSG_HELLO) { |
| 2770 | /* UNLOCK */ |
| 2771 | nc_server_ch_client_unlock(client); |
| 2772 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2773 | if (!nc_server_ch_client_thread_is_running(data)) { |
| 2774 | /* thread should stop running */ |
| 2775 | goto cleanup; |
| 2776 | } |
| 2777 | |
| 2778 | /* run while the session is established */ |
| 2779 | VRB(session, "Call Home client \"%s\" session %u established.", data->client_name, session->id); |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2780 | if (nc_server_ch_client_thread_session_cond_wait(data, session)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2781 | goto cleanup; |
| 2782 | } |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2783 | session = NULL; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2784 | |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2785 | VRB(NULL, "Call Home client \"%s\" session terminated.", data->client_name); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2786 | if (!nc_server_ch_client_thread_is_running(data)) { |
| 2787 | /* thread should stop running */ |
| 2788 | goto cleanup; |
| 2789 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2790 | |
| 2791 | /* LOCK */ |
| 2792 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2793 | if (!client) { |
| 2794 | VRB(NULL, "Call Home client \"%s\" removed.", data->client_name); |
| 2795 | goto cleanup; |
| 2796 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2797 | |
| 2798 | /* session changed status -> it was disconnected for whatever reason, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2799 | * persistent connection immediately tries to reconnect, periodic connects at specific times */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2800 | if (client->conn_type == NC_CH_PERIOD) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2801 | if (client->anchor_time) { |
Michal Vasko | 18e1fa0 | 2021-11-29 09:02:05 +0100 | [diff] [blame] | 2802 | /* anchored */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2803 | reconnect_in = (time(NULL) - client->anchor_time) % (client->period * 60); |
Michal Vasko | 18e1fa0 | 2021-11-29 09:02:05 +0100 | [diff] [blame] | 2804 | } else { |
| 2805 | /* fixed timeout */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2806 | reconnect_in = client->period * 60; |
Michal Vasko | 18e1fa0 | 2021-11-29 09:02:05 +0100 | [diff] [blame] | 2807 | } |
| 2808 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2809 | /* UNLOCK */ |
| 2810 | nc_server_ch_client_unlock(client); |
| 2811 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2812 | /* wait for the timeout to elapse, so we can try to reconnect */ |
| 2813 | VRB(session, "Call Home client \"%s\" reconnecting in %" PRIu32 " seconds.", data->client_name, reconnect_in); |
| 2814 | if (!nc_server_ch_client_thread_is_running_wait(session, data, reconnect_in)) { |
| 2815 | goto cleanup; |
| 2816 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2817 | |
| 2818 | /* LOCK */ |
| 2819 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2820 | assert(client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | /* set next endpoint to try */ |
| 2824 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2825 | next_endpt_index = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2826 | } else if (client->start_with == NC_CH_LAST_CONNECTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2827 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 2828 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 2829 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 2830 | break; |
| 2831 | } |
| 2832 | } |
| 2833 | if (next_endpt_index >= client->ch_endpt_count) { |
| 2834 | /* endpoint was removed, start with the first one */ |
| 2835 | next_endpt_index = 0; |
| 2836 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2837 | } else { |
| 2838 | /* just get a random index */ |
| 2839 | next_endpt_index = rand() % client->ch_endpt_count; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2842 | } else { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2843 | /* session was not created, wait a little bit and try again */ |
| 2844 | max_wait = client->max_wait; |
| 2845 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2846 | /* UNLOCK */ |
| 2847 | nc_server_ch_client_unlock(client); |
| 2848 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2849 | /* wait for max_wait seconds */ |
| 2850 | if (!nc_server_ch_client_thread_is_running_wait(session, data, max_wait)) { |
| 2851 | /* thread should stop running */ |
| 2852 | goto cleanup; |
| 2853 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2854 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2855 | /* LOCK */ |
| 2856 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2857 | assert(client); |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2858 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2859 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 2860 | |
| 2861 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2862 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 2863 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 2864 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2865 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 2866 | } |
| 2867 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2868 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 2869 | /* endpoint was removed, start with the first one */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2870 | VRB(session, "Call Home client \"%s\" endpoint \"%s\" removed.", data->client_name, cur_endpt_name); |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2871 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 2872 | cur_attempts = 0; |
| 2873 | } else if (cur_attempts == client->max_attempts) { |
| 2874 | /* we have tried to connect to this endpoint enough times */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2875 | VRB(session, "Call Home client \"%s\" endpoint \"%s\" failed connection attempt limit %" PRIu8 " reached.", |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 2876 | data->client_name, cur_endpt_name, client->max_attempts); |
| 2877 | |
| 2878 | /* clear a pending socket, if any */ |
| 2879 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 2880 | if (cur_endpt->sock_pending > -1) { |
| 2881 | close(cur_endpt->sock_pending); |
| 2882 | cur_endpt->sock_pending = -1; |
| 2883 | } |
| 2884 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2885 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2886 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2887 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2888 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 2889 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 2890 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2891 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2892 | cur_attempts = 0; |
| 2893 | } /* else we keep the current one */ |
| 2894 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 2895 | |
| 2896 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 2897 | free(cur_endpt_name); |
| 2898 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2899 | } |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2900 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2901 | /* UNLOCK if we break out of the loop */ |
| 2902 | nc_server_ch_client_unlock(client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2903 | |
| 2904 | cleanup: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2905 | VRB(session, "Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 2906 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2907 | free(data->client_name); |
Jan Kundrát | fd3a01d | 2024-04-10 03:03:08 +0200 | [diff] [blame] | 2908 | pthread_mutex_lock(&data->cond_lock); |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2909 | pthread_cond_destroy(&data->cond); |
Jan Kundrát | fd3a01d | 2024-04-10 03:03:08 +0200 | [diff] [blame] | 2910 | pthread_mutex_unlock(&data->cond_lock); |
roman | 8341e8b | 2023-11-23 16:12:42 +0100 | [diff] [blame] | 2911 | pthread_mutex_destroy(&data->cond_lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2912 | free(data); |
| 2913 | return NULL; |
| 2914 | } |
| 2915 | |
| 2916 | API int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2917 | nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2918 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb, |
| 2919 | void *new_session_cb_data) |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 2920 | { |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2921 | int rc = 0, r; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2922 | pthread_t tid; |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2923 | struct nc_ch_client_thread_arg *arg = NULL; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2924 | struct nc_ch_client *ch_client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2925 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2926 | NC_CHECK_ARG_RET(NULL, client_name, acquire_ctx_cb, release_ctx_cb, new_session_cb, -1); |
| 2927 | |
roman | d82caf1 | 2024-03-05 14:21:39 +0100 | [diff] [blame] | 2928 | NC_CHECK_SRV_INIT_RET(-1); |
| 2929 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2930 | /* LOCK */ |
| 2931 | ch_client = nc_server_ch_client_lock(client_name); |
| 2932 | if (!ch_client) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2933 | ERR(NULL, "Client \"%s\" not found.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2934 | return -1; |
| 2935 | } |
| 2936 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2937 | /* create the thread argument */ |
| 2938 | arg = calloc(1, sizeof *arg); |
| 2939 | NC_CHECK_ERRMEM_GOTO(!arg, rc = -1, cleanup); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2940 | arg->client_name = strdup(client_name); |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2941 | NC_CHECK_ERRMEM_GOTO(!arg->client_name, rc = -1, cleanup); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2942 | arg->acquire_ctx_cb = acquire_ctx_cb; |
| 2943 | arg->release_ctx_cb = release_ctx_cb; |
| 2944 | arg->ctx_cb_data = ctx_cb_data; |
| 2945 | arg->new_session_cb = new_session_cb; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2946 | arg->new_session_cb_data = new_session_cb_data; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2947 | pthread_cond_init(&arg->cond, NULL); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2948 | pthread_mutex_init(&arg->cond_lock, NULL); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2949 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2950 | /* creating the thread */ |
| 2951 | arg->thread_running = 1; |
| 2952 | if ((r = pthread_create(&tid, NULL, nc_ch_client_thread, arg))) { |
| 2953 | ERR(NULL, "Creating a new thread failed (%s).", strerror(r)); |
| 2954 | rc = -1; |
| 2955 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2956 | } |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2957 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2958 | /* the thread now manages arg */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2959 | ch_client->tid = tid; |
| 2960 | ch_client->thread_data = arg; |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2961 | arg = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2962 | |
Michal Vasko | 6f86598 | 2023-11-21 12:10:42 +0100 | [diff] [blame] | 2963 | cleanup: |
| 2964 | /* UNLOCK */ |
| 2965 | nc_server_ch_client_unlock(ch_client); |
| 2966 | |
| 2967 | if (arg) { |
| 2968 | free(arg->client_name); |
| 2969 | free(arg); |
| 2970 | } |
| 2971 | return rc; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2974 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2975 | |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 2976 | API struct timespec |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 2977 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2978 | { |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 2979 | struct timespec fail = {0}; |
| 2980 | |
| 2981 | NC_CHECK_ARG_RET(session, session, fail); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2982 | |
| 2983 | if (session->side != NC_SERVER) { |
| 2984 | ERRARG(session, "session"); |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 2985 | return fail; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2986 | } |
| 2987 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2988 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2989 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 2990 | |
| 2991 | API void |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 2992 | nc_session_inc_notif_status(struct nc_session *session) |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 2993 | { |
| 2994 | if (!session || (session->side != NC_SERVER)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 2995 | ERRARG(session, "session"); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 2996 | return; |
| 2997 | } |
| 2998 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 2999 | /* NTF STATUS LOCK */ |
| 3000 | pthread_mutex_lock(&session->opts.server.ntf_status_lock); |
| 3001 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3002 | ++session->opts.server.ntf_status; |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3003 | |
| 3004 | /* NTF STATUS UNLOCK */ |
| 3005 | pthread_mutex_unlock(&session->opts.server.ntf_status_lock); |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | API void |
| 3009 | nc_session_dec_notif_status(struct nc_session *session) |
| 3010 | { |
| 3011 | if (!session || (session->side != NC_SERVER)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 3012 | ERRARG(session, "session"); |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3013 | return; |
| 3014 | } |
| 3015 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3016 | /* NTF STATUS LOCK */ |
| 3017 | pthread_mutex_lock(&session->opts.server.ntf_status_lock); |
| 3018 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3019 | if (session->opts.server.ntf_status) { |
| 3020 | --session->opts.server.ntf_status; |
| 3021 | } |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3022 | |
| 3023 | /* NTF STATUS UNLOCK */ |
| 3024 | pthread_mutex_unlock(&session->opts.server.ntf_status_lock); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | API int |
| 3028 | nc_session_get_notif_status(const struct nc_session *session) |
| 3029 | { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3030 | uint32_t ntf_status; |
| 3031 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3032 | if (!session || (session->side != NC_SERVER)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 3033 | ERRARG(session, "session"); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3034 | return 0; |
| 3035 | } |
| 3036 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3037 | /* NTF STATUS LOCK */ |
| 3038 | pthread_mutex_lock(&((struct nc_session *)session)->opts.server.ntf_status_lock); |
| 3039 | |
| 3040 | ntf_status = session->opts.server.ntf_status; |
| 3041 | |
| 3042 | /* NTF STATUS UNLOCK */ |
| 3043 | pthread_mutex_unlock(&((struct nc_session *)session)->opts.server.ntf_status_lock); |
| 3044 | |
| 3045 | return ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3046 | } |