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