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