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