Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file session_server.c |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 server session manipulation functions |
| 5 | * |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2017 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 13 | */ |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 14 | #define _QNX_SOURCE /* getpeereid */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE /* signals, threads, SO_PEERCRED */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 16 | |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <errno.h> |
| 20 | #include <string.h> |
| 21 | #include <poll.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 24 | #include <sys/un.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <netinet/tcp.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | #include <unistd.h> |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 29 | #include <fcntl.h> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 30 | #include <pthread.h> |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 31 | #include <time.h> |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 32 | #include <signal.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 33 | #include <pwd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 34 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 35 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | #include "session_server.h" |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 37 | #include "session_server_ch.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 38 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 39 | struct nc_server_opts server_opts = { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 40 | #ifdef NC_ENABLED_SSH |
| 41 | .authkey_lock = PTHREAD_MUTEX_INITIALIZER, |
| 42 | #endif |
| 43 | .bind_lock = PTHREAD_MUTEX_INITIALIZER, |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 44 | .endpt_lock = PTHREAD_RWLOCK_INITIALIZER, |
| 45 | .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 46 | }; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 47 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 48 | static nc_rpc_clb global_rpc_clb = NULL; |
| 49 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 50 | struct nc_endpt * |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 51 | 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] | 52 | { |
| 53 | uint16_t i; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 54 | struct nc_endpt *endpt = NULL; |
| 55 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 56 | if (!name) { |
| 57 | ERRARG("endpt_name"); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 61 | /* WRITE LOCK */ |
| 62 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 63 | |
| 64 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 65 | 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] | 66 | endpt = &server_opts.endpts[i]; |
| 67 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 71 | if (!endpt) { |
| 72 | ERR("Endpoint \"%s\" was not found.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 73 | /* UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 74 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 78 | if (idx) { |
| 79 | *idx = i; |
| 80 | } |
| 81 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 82 | return endpt; |
| 83 | } |
| 84 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 85 | struct nc_ch_endpt * |
| 86 | 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] | 87 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 88 | uint16_t i, j; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 89 | struct nc_ch_client *client = NULL; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 90 | struct nc_ch_endpt *endpt = NULL; |
| 91 | |
| 92 | *client_p = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 93 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 94 | if (!name) { |
| 95 | ERRARG("client_name"); |
| 96 | return NULL; |
| 97 | } |
| 98 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 99 | /* READ LOCK */ |
| 100 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 101 | |
| 102 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 103 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 104 | client = &server_opts.ch_clients[i]; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 105 | if (!endpt_name && !ti) { |
| 106 | /* return only client */ |
| 107 | break; |
| 108 | } |
| 109 | for (j = 0; j < client->ch_endpt_count; ++j) { |
| 110 | if (!strcmp(client->ch_endpts[j].name, endpt_name) && (!ti || (ti == client->ch_endpts[j].ti))) { |
| 111 | endpt = &client->ch_endpts[j]; |
| 112 | break; |
| 113 | } |
| 114 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (!client) { |
| 120 | ERR("Call Home client \"%s\" was not found.", name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 121 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 122 | /* READ UNLOCK */ |
| 123 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 124 | } else if (endpt_name && ti && !endpt) { |
| 125 | ERR("Call Home client \"%s\" endpoint \"%s\" was not found.", name, endpt_name); |
| 126 | |
| 127 | /* READ UNLOCK */ |
| 128 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 129 | } else { |
| 130 | /* CH CLIENT LOCK */ |
| 131 | pthread_mutex_lock(&client->lock); |
| 132 | |
| 133 | *client_p = client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 136 | return endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
| 140 | nc_server_ch_client_unlock(struct nc_ch_client *client) |
| 141 | { |
| 142 | /* CH CLIENT UNLOCK */ |
| 143 | pthread_mutex_unlock(&client->lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 144 | |
| 145 | /* READ UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 146 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 147 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 148 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 149 | API void |
| 150 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 151 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 152 | if (!session) { |
| 153 | ERRARG("session"); |
| 154 | return; |
| 155 | } else if (!reason) { |
| 156 | ERRARG("reason"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 160 | if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) { |
| 161 | session->killed_by = 0; |
| 162 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 163 | session->term_reason = reason; |
| 164 | } |
| 165 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 166 | API void |
| 167 | nc_session_set_killed_by(struct nc_session *session, uint32_t sid) |
| 168 | { |
| 169 | if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) { |
| 170 | ERRARG("session"); |
| 171 | return; |
| 172 | } else if (!sid) { |
| 173 | ERRARG("sid"); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | session->killed_by = sid; |
| 178 | } |
| 179 | |
| 180 | API void |
| 181 | nc_session_set_status(struct nc_session *session, NC_STATUS status) |
| 182 | { |
| 183 | if (!session) { |
| 184 | ERRARG("session"); |
| 185 | return; |
| 186 | } else if (!status) { |
| 187 | ERRARG("status"); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | session->status = status; |
| 192 | } |
| 193 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 194 | int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 195 | 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] | 196 | { |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 197 | int opt; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 198 | int is_ipv4, sock; |
| 199 | struct sockaddr_storage saddr; |
| 200 | |
| 201 | struct sockaddr_in *saddr4; |
| 202 | struct sockaddr_in6 *saddr6; |
| 203 | |
| 204 | |
| 205 | if (!strchr(address, ':')) { |
| 206 | is_ipv4 = 1; |
| 207 | } else { |
| 208 | is_ipv4 = 0; |
| 209 | } |
| 210 | |
| 211 | sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0); |
| 212 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 213 | ERR("Failed to create socket (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 214 | goto fail; |
| 215 | } |
| 216 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 217 | /* these options will be inherited by accepted sockets */ |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 218 | opt = 1; |
| 219 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) { |
| 220 | ERR("Could not set SO_REUSEADDR socket option (%s).", strerror(errno)); |
| 221 | goto fail; |
| 222 | } |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 223 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
| 224 | ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
| 225 | goto fail; |
| 226 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 227 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 228 | if (nc_sock_enable_keepalive(sock, ka)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 229 | goto fail; |
| 230 | } |
| 231 | |
Michal Vasko | f22d5ff | 2020-04-15 11:10:27 +0200 | [diff] [blame] | 232 | memset(&saddr, 0, sizeof(struct sockaddr_storage)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 233 | if (is_ipv4) { |
| 234 | saddr4 = (struct sockaddr_in *)&saddr; |
| 235 | |
| 236 | saddr4->sin_family = AF_INET; |
| 237 | saddr4->sin_port = htons(port); |
| 238 | |
| 239 | if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 240 | ERR("Failed to convert IPv4 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 241 | goto fail; |
| 242 | } |
| 243 | |
| 244 | if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 245 | ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 246 | goto fail; |
| 247 | } |
| 248 | |
| 249 | } else { |
| 250 | saddr6 = (struct sockaddr_in6 *)&saddr; |
| 251 | |
| 252 | saddr6->sin6_family = AF_INET6; |
| 253 | saddr6->sin6_port = htons(port); |
| 254 | |
| 255 | if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 256 | ERR("Failed to convert IPv6 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 257 | goto fail; |
| 258 | } |
| 259 | |
| 260 | if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 261 | ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 262 | goto fail; |
| 263 | } |
| 264 | } |
| 265 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 266 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 267 | ERR("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] | 268 | goto fail; |
| 269 | } |
| 270 | |
| 271 | return sock; |
| 272 | |
| 273 | fail: |
| 274 | if (sock > -1) { |
| 275 | close(sock); |
| 276 | } |
| 277 | |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | int |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 282 | nc_sock_listen_unix(const char *address, const struct nc_server_unix_opts *opts) |
| 283 | { |
| 284 | struct sockaddr_un sun; |
| 285 | int sock = -1; |
| 286 | |
| 287 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 288 | if (sock == -1) { |
| 289 | ERR("Failed to create socket (%s).", strerror(errno)); |
| 290 | goto fail; |
| 291 | } |
| 292 | |
| 293 | memset(&sun, 0, sizeof(sun)); |
| 294 | sun.sun_family = AF_UNIX; |
| 295 | snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address); |
| 296 | |
| 297 | unlink(sun.sun_path); |
| 298 | if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) { |
| 299 | ERR("Could not bind \"%s\" (%s).", address, strerror(errno)); |
| 300 | goto fail; |
| 301 | } |
| 302 | |
| 303 | if (opts->mode != (mode_t)-1) { |
| 304 | if (chmod(sun.sun_path, opts->mode) < 0) { |
| 305 | ERR("Failed to set unix socket permissions (%s).", strerror(errno)); |
| 306 | goto fail; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (opts->uid != (uid_t)-1 || opts->gid != (gid_t)-1) { |
| 311 | if (chown(sun.sun_path, opts->uid, opts->gid) < 0) { |
| 312 | ERR("Failed to set unix socket uid/gid (%s).", strerror(errno)); |
| 313 | goto fail; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
| 318 | ERR("Unable to start listening on \"%s\" (%s).", address, strerror(errno)); |
| 319 | goto fail; |
| 320 | } |
| 321 | |
| 322 | return sock; |
| 323 | |
| 324 | fail: |
| 325 | if (sock > -1) { |
| 326 | close(sock); |
| 327 | } |
| 328 | |
| 329 | return -1; |
| 330 | } |
| 331 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 332 | /** |
| 333 | * @brief Evaluate socket name for AF_UNIX socket. |
| 334 | * @param[in] acc_sock_fd is file descriptor for the accepted socket (a nonnegative). |
| 335 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 336 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 337 | * @return 0 if the stream socket is unnamed. Parameter host is set to NULL. |
| 338 | * @return -1 in case of error. Parameter host is set to NULL. |
| 339 | */ |
| 340 | static int |
| 341 | sock_host_unix(int acc_sock_fd, char **host) |
| 342 | { |
| 343 | char *sun_path; |
| 344 | struct sockaddr_storage saddr; |
| 345 | socklen_t addr_len; |
| 346 | |
| 347 | *host = NULL; |
| 348 | saddr.ss_family = AF_UNIX; |
| 349 | addr_len = sizeof(saddr); |
| 350 | |
| 351 | if (getsockname(acc_sock_fd, (struct sockaddr *)&saddr, &addr_len)) { |
| 352 | ERR("getsockname failed (%s).", strerror(errno)); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | sun_path = ((struct sockaddr_un *)&saddr)->sun_path; |
| 357 | if (!sun_path) { |
| 358 | /* stream socket is unnamed */ |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | if (!(*host = strdup(sun_path))) { |
| 363 | ERRMEM; |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * @brief Evaluate socket name and port number for AF_INET socket. |
| 372 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 373 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 374 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 375 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 376 | * @return -1 in case of error. Parameter host is set to NULL and port is unchanged. |
| 377 | */ |
| 378 | static int |
| 379 | sock_host_inet(const struct sockaddr_in *addr, char **host, uint16_t *port) |
| 380 | { |
| 381 | *host = malloc(INET_ADDRSTRLEN); |
| 382 | if (!(*host)) { |
| 383 | ERRMEM; |
| 384 | return -1; |
| 385 | } |
| 386 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 387 | if (!inet_ntop(AF_INET, &addr->sin_addr, *host, INET_ADDRSTRLEN)) { |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 388 | ERR("inet_ntop failed(%s)."); |
| 389 | free(*host); |
| 390 | *host = NULL; |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | if (port) { |
| 395 | *port = ntohs(addr->sin_port); |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @brief Evaluate socket name and port number for AF_INET6 socket. |
| 403 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 404 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 405 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 406 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 407 | * @return -1 in case of error. Parameter host is set to the NULL and port is unchanged. |
| 408 | */ |
| 409 | static int |
| 410 | sock_host_inet6(const struct sockaddr_in6 *addr, char **host, uint16_t *port) |
| 411 | { |
| 412 | *host = malloc(INET6_ADDRSTRLEN); |
| 413 | if (!(*host)) { |
| 414 | ERRMEM; |
| 415 | return -1; |
| 416 | } |
| 417 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 418 | if (!inet_ntop(AF_INET6, &addr->sin6_addr, *host, INET6_ADDRSTRLEN)) { |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 419 | ERR("inet_ntop failed(%s)."); |
| 420 | free(*host); |
| 421 | *host = NULL; |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | if (port) { |
| 426 | *port = ntohs(addr->sin6_port); |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 432 | int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 433 | nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 434 | { |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 435 | sigset_t sigmask, origmask; |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 436 | uint16_t i, j, pfd_count; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 437 | struct pollfd *pfd; |
| 438 | struct sockaddr_storage saddr; |
| 439 | socklen_t saddr_len = sizeof(saddr); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 440 | int ret, sock = -1, flags; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 441 | |
| 442 | pfd = malloc(bind_count * sizeof *pfd); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 443 | if (!pfd) { |
| 444 | ERRMEM; |
| 445 | return -1; |
| 446 | } |
| 447 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 448 | for (i = 0, pfd_count = 0; i < bind_count; ++i) { |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 449 | if (binds[i].sock < 0) { |
| 450 | /* invalid socket */ |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 451 | continue; |
| 452 | } |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 453 | if (binds[i].pollin) { |
| 454 | binds[i].pollin = 0; |
| 455 | /* leftover pollin */ |
| 456 | sock = binds[i].sock; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 457 | break; |
| 458 | } |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 459 | pfd[pfd_count].fd = binds[i].sock; |
| 460 | pfd[pfd_count].events = POLLIN; |
| 461 | pfd[pfd_count].revents = 0; |
| 462 | |
| 463 | ++pfd_count; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 466 | if (sock == -1) { |
| 467 | /* poll for a new connection */ |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 468 | sigfillset(&sigmask); |
| 469 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 470 | ret = poll(pfd, pfd_count, timeout); |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 471 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); |
| 472 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 473 | if (!ret) { |
| 474 | /* we timeouted */ |
| 475 | free(pfd); |
| 476 | return 0; |
| 477 | } else if (ret == -1) { |
| 478 | ERR("Poll failed (%s).", strerror(errno)); |
| 479 | free(pfd); |
| 480 | return -1; |
| 481 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 482 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 483 | for (i = 0, j = 0; j < pfd_count; ++i, ++j) { |
| 484 | /* adjust i so that indices in binds and pfd always match */ |
| 485 | while (binds[i].sock != pfd[j].fd) { |
| 486 | ++i; |
| 487 | } |
| 488 | |
| 489 | if (pfd[j].revents & POLLIN) { |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 490 | --ret; |
| 491 | |
| 492 | if (!ret) { |
| 493 | /* the last socket with an event, use it */ |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 494 | sock = pfd[j].fd; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 495 | break; |
| 496 | } else { |
| 497 | /* just remember the event for next time */ |
| 498 | binds[i].pollin = 1; |
| 499 | } |
| 500 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | free(pfd); |
| 504 | |
| 505 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 506 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
Michal Vasko | 3f6cc4a | 2016-01-21 15:58:53 +0100 | [diff] [blame] | 511 | if (ret < 0) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 512 | ERR("Accept failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 513 | return -1; |
| 514 | } |
Michal Vasko | 6ccb29d | 2016-10-13 15:00:27 +0200 | [diff] [blame] | 515 | VRB("Accepted a connection on %s:%u.", binds[i].address, binds[i].port); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 516 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 517 | /* make the socket non-blocking */ |
| 518 | if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) { |
| 519 | ERR("Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 520 | close(ret); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 521 | return -1; |
| 522 | } |
| 523 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 524 | if (idx) { |
| 525 | *idx = i; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 526 | } |
| 527 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 528 | if (!host) { |
| 529 | return ret; |
| 530 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 531 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 532 | if (port) { |
| 533 | *port = 0; |
| 534 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 535 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 536 | if (saddr.ss_family == AF_UNIX) { |
| 537 | sock_host_unix(ret, host); |
| 538 | } else if (saddr.ss_family == AF_INET) { |
| 539 | sock_host_inet((struct sockaddr_in *)&saddr, host, port); |
| 540 | } else if (saddr.ss_family == AF_INET6) { |
| 541 | sock_host_inet6((struct sockaddr_in6 *)&saddr, host, port); |
| 542 | } else { |
| 543 | ERR("Source host of an unknown protocol family."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 549 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 550 | nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session)) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 551 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 552 | const char *identifier = NULL, *revision = NULL, *format = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 553 | char *model_data = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 554 | struct ly_out *out; |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 555 | const struct lys_module *module = NULL, *mod; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 556 | const struct lysp_submodule *submodule = NULL; |
| 557 | struct lyd_node *child, *err, *data = NULL; |
| 558 | LYS_OUTFORMAT outformat = 0; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 559 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 560 | LY_LIST_FOR(lyd_child(rpc), child) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 561 | if (!strcmp(child->schema->name, "identifier")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 562 | identifier = LYD_CANON_VALUE(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 563 | } else if (!strcmp(child->schema->name, "version")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 564 | revision = LYD_CANON_VALUE(child); |
| 565 | if (revision && revision[0] == '\0') { |
| 566 | revision = NULL; |
Radek Krejci | 1afa779 | 2017-03-26 11:24:16 -0500 | [diff] [blame] | 567 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 568 | } else if (!strcmp(child->schema->name, "format")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 569 | format = LYD_CANON_VALUE(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 570 | } |
| 571 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 572 | VRB("Schema \"%s@%s\" was requested.", identifier, revision ? revision : "<any>"); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 573 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 574 | /* check revision */ |
| 575 | if (revision && (strlen(revision) != 10) && strcmp(revision, "1.0")) { |
| 576 | err = nc_err(server_opts.ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 577 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 578 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 579 | } |
| 580 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 581 | if (revision) { |
| 582 | /* get specific module */ |
| 583 | module = ly_ctx_get_module(server_opts.ctx, identifier, revision); |
| 584 | if (!module) { |
| 585 | submodule = ly_ctx_get_submodule(server_opts.ctx, identifier, revision); |
| 586 | } |
| 587 | } else { |
| 588 | /* try to get implemented, then latest module */ |
| 589 | module = ly_ctx_get_module_implemented(server_opts.ctx, identifier); |
| 590 | if (!module) { |
| 591 | module = ly_ctx_get_module_latest(server_opts.ctx, identifier); |
| 592 | } |
| 593 | if (!module) { |
| 594 | submodule = ly_ctx_get_submodule_latest(server_opts.ctx, identifier); |
| 595 | } |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 596 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 597 | if (!module && !submodule) { |
| 598 | err = nc_err(server_opts.ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 599 | nc_err_set_msg(err, "The requested schema was not found.", "en"); |
| 600 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* check format */ |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 604 | if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 605 | outformat = LYS_OUT_YANG; |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 606 | } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 607 | outformat = LYS_OUT_YIN; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 608 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 609 | err = nc_err(server_opts.ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 610 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 611 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 612 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 613 | |
| 614 | /* print */ |
| 615 | ly_out_new_memory(&model_data, 0, &out); |
| 616 | if (module) { |
| 617 | lys_print_module(out, module, outformat, 0, 0); |
| 618 | } else { |
| 619 | lys_print_submodule(out, submodule, outformat, 0, 0); |
| 620 | } |
| 621 | ly_out_free(out, NULL, 0); |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 622 | if (!model_data) { |
| 623 | ERRINT; |
| 624 | return NULL; |
| 625 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 626 | |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 627 | /* create reply */ |
| 628 | mod = ly_ctx_get_module_implemented(server_opts.ctx, "ietf-netconf-monitoring"); |
| 629 | if (!mod || lyd_new_inner(NULL, mod, "get-schema", 0, &data)) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 630 | ERRINT; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 631 | free(model_data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 632 | return NULL; |
| 633 | } |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 634 | lydict_insert_zc(server_opts.ctx, model_data, (const char **)&model_data); |
| 635 | if (lyd_new_any(data, NULL, "data", model_data, 1, LYD_ANYDATA_STRING, 1, NULL)) { |
| 636 | ERRINT; |
| 637 | lydict_remove(server_opts.ctx, model_data); |
| 638 | lyd_free_tree(data); |
| 639 | return NULL; |
| 640 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 641 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 642 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 646 | 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] | 647 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 648 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 649 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 652 | API int |
| 653 | nc_server_init(struct ly_ctx *ctx) |
| 654 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 655 | struct lysc_node *rpc; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 656 | pthread_rwlockattr_t attr; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 657 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 658 | if (!ctx) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 659 | ERRARG("ctx"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 663 | nc_init(); |
| 664 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 665 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 666 | rpc = NULL; |
| 667 | if (ly_ctx_get_module_implemented(ctx, "ietf-netconf-monitoring")) { |
| 668 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0); |
| 669 | } |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 670 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 671 | rpc->priv = nc_clb_default_get_schema; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* set default <close-session> callback if not specififed */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 675 | 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] | 676 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 677 | rpc->priv = nc_clb_default_close_session; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 680 | server_opts.ctx = ctx; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 681 | |
| 682 | server_opts.new_session_id = 1; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 683 | server_opts.new_client_id = 1; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 684 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 685 | errno = 0; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 686 | |
| 687 | if (pthread_rwlockattr_init(&attr) == 0) { |
Rosen Penev | ef2f3ac | 2019-07-15 18:15:28 -0700 | [diff] [blame] | 688 | #if defined(HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP) |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 689 | if (pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) == 0) { |
| 690 | if (pthread_rwlock_init(&server_opts.endpt_lock, &attr) != 0) { |
| 691 | ERR("%s: failed to init rwlock(%s).", __FUNCTION__, strerror(errno)); |
| 692 | } |
| 693 | if (pthread_rwlock_init(&server_opts.ch_client_lock, &attr) != 0) { |
| 694 | ERR("%s: failed to init rwlock(%s).", __FUNCTION__, strerror(errno)); |
| 695 | } |
| 696 | } else { |
| 697 | ERR("%s: failed set attribute (%s).", __FUNCTION__, strerror(errno)); |
| 698 | } |
Rosen Penev | ef2f3ac | 2019-07-15 18:15:28 -0700 | [diff] [blame] | 699 | #endif |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 700 | pthread_rwlockattr_destroy(&attr); |
| 701 | } else { |
| 702 | ERR("%s: failed init attribute (%s).", __FUNCTION__, strerror(errno)); |
| 703 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 707 | API void |
| 708 | nc_server_destroy(void) |
| 709 | { |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 710 | uint32_t i; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 711 | |
| 712 | for (i = 0; i < server_opts.capabilities_count; i++) { |
| 713 | lydict_remove(server_opts.ctx, server_opts.capabilities[i]); |
| 714 | } |
| 715 | free(server_opts.capabilities); |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 716 | server_opts.capabilities = NULL; |
| 717 | server_opts.capabilities_count = 0; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 718 | if (server_opts.content_id_data && server_opts.content_id_data_free) { |
| 719 | server_opts.content_id_data_free(server_opts.content_id_data); |
| 720 | } |
| 721 | server_opts.passwd_auth_data = NULL; |
| 722 | server_opts.passwd_auth_data_free = NULL; |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 723 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 724 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 725 | nc_server_del_endpt(NULL, 0); |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 726 | nc_server_ch_del_client(NULL); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 727 | #endif |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 728 | #ifdef NC_ENABLED_SSH |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 729 | if (server_opts.passwd_auth_data && server_opts.passwd_auth_data_free) { |
| 730 | server_opts.passwd_auth_data_free(server_opts.passwd_auth_data); |
| 731 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 732 | server_opts.passwd_auth_data = NULL; |
| 733 | server_opts.passwd_auth_data_free = NULL; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 734 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 735 | nc_server_ssh_del_authkey(NULL, NULL, 0, NULL); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 736 | |
| 737 | if (server_opts.hostkey_data && server_opts.hostkey_data_free) { |
| 738 | server_opts.hostkey_data_free(server_opts.hostkey_data); |
| 739 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 740 | server_opts.hostkey_data = NULL; |
| 741 | server_opts.hostkey_data_free = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 742 | #endif |
| 743 | #ifdef NC_ENABLED_TLS |
| 744 | if (server_opts.server_cert_data && server_opts.server_cert_data_free) { |
| 745 | server_opts.server_cert_data_free(server_opts.server_cert_data); |
| 746 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 747 | server_opts.server_cert_data = NULL; |
| 748 | server_opts.server_cert_data_free = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 749 | if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) { |
| 750 | server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data); |
| 751 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 752 | server_opts.trusted_cert_list_data = NULL; |
| 753 | server_opts.trusted_cert_list_data_free = NULL; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 754 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 755 | nc_destroy(); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 758 | API int |
| 759 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 760 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 761 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) { |
| 762 | ERRARG("basic_mode"); |
| 763 | return -1; |
| 764 | } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) { |
| 765 | ERRARG("also_supported"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | server_opts.wd_basic_mode = basic_mode; |
| 770 | server_opts.wd_also_supported = also_supported; |
| 771 | return 0; |
| 772 | } |
| 773 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 774 | API void |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 775 | nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported) |
| 776 | { |
| 777 | if (!basic_mode && !also_supported) { |
| 778 | ERRARG("basic_mode and also_supported"); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if (basic_mode) { |
| 783 | *basic_mode = server_opts.wd_basic_mode; |
| 784 | } |
| 785 | if (also_supported) { |
| 786 | *also_supported = server_opts.wd_also_supported; |
| 787 | } |
| 788 | } |
| 789 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 790 | API int |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 791 | nc_server_set_capability(const char *value) |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 792 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 793 | const char **new; |
| 794 | |
| 795 | if (!value || !value[0]) { |
| 796 | ERRARG("value must not be empty"); |
| 797 | return EXIT_FAILURE; |
| 798 | } |
| 799 | |
| 800 | server_opts.capabilities_count++; |
| 801 | new = realloc(server_opts.capabilities, server_opts.capabilities_count * sizeof *server_opts.capabilities); |
| 802 | if (!new) { |
| 803 | ERRMEM; |
| 804 | return EXIT_FAILURE; |
| 805 | } |
| 806 | server_opts.capabilities = new; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 807 | lydict_insert(server_opts.ctx, value, 0, &server_opts.capabilities[server_opts.capabilities_count - 1]); |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 808 | |
| 809 | return EXIT_SUCCESS; |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 810 | } |
| 811 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 812 | API void |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 813 | nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data, |
| 814 | void (*free_user_data)(void *user_data)) |
| 815 | { |
| 816 | server_opts.content_id_clb = content_id_clb; |
| 817 | server_opts.content_id_data = user_data; |
| 818 | server_opts.content_id_data_free = free_user_data; |
| 819 | } |
| 820 | |
| 821 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 822 | nc_server_set_hello_timeout(uint16_t hello_timeout) |
| 823 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 824 | server_opts.hello_timeout = hello_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 825 | } |
| 826 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 827 | API uint16_t |
| 828 | nc_server_get_hello_timeout(void) |
| 829 | { |
| 830 | return server_opts.hello_timeout; |
| 831 | } |
| 832 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 833 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 834 | nc_server_set_idle_timeout(uint16_t idle_timeout) |
| 835 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 836 | server_opts.idle_timeout = idle_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 837 | } |
| 838 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 839 | API uint16_t |
| 840 | nc_server_get_idle_timeout(void) |
| 841 | { |
| 842 | return server_opts.idle_timeout; |
| 843 | } |
| 844 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 845 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 846 | nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 847 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 848 | NC_MSG_TYPE msgtype; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 849 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 850 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 851 | if (!server_opts.ctx) { |
| 852 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 853 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 854 | } else if (fdin < 0) { |
| 855 | ERRARG("fdin"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 856 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 857 | } else if (fdout < 0) { |
| 858 | ERRARG("fdout"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 859 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 860 | } else if (!username) { |
| 861 | ERRARG("username"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 862 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 863 | } else if (!session) { |
| 864 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 865 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 869 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 870 | if (!(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 871 | ERRMEM; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 872 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 873 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 874 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 875 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 876 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 877 | (*session)->ti_type = NC_TI_FD; |
| 878 | (*session)->ti.fd.in = fdin; |
| 879 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 880 | |
| 881 | /* assign context (dicionary needed for handshake) */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 882 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 883 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 884 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 885 | /* assign new SID atomically */ |
Michal Vasko | 7f1fa3c | 2020-09-08 16:30:41 +0200 | [diff] [blame] | 886 | (*session)->id = ATOMIC_INC(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 887 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 888 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 889 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 890 | if (msgtype != NC_MSG_HELLO) { |
| 891 | nc_session_free(*session, NULL); |
| 892 | *session = NULL; |
| 893 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 894 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 895 | |
| 896 | nc_gettimespec_mono(&ts_cur); |
| 897 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 898 | nc_gettimespec_real(&ts_cur); |
| 899 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
| 900 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 901 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 902 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 903 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 904 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 905 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 906 | static void |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 907 | nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id) |
| 908 | { |
| 909 | uint8_t q_last; |
| 910 | |
| 911 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
| 912 | ERRINT; |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | /* get a unique queue value (by adding 1 to the last added value, if any) */ |
| 917 | if (ps->queue_len) { |
| 918 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 919 | *id = ps->queue[q_last] + 1; |
| 920 | } else { |
| 921 | *id = 0; |
| 922 | } |
| 923 | |
| 924 | /* add the id into the queue */ |
| 925 | ++ps->queue_len; |
| 926 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 927 | ps->queue[q_last] = *id; |
| 928 | } |
| 929 | |
| 930 | static void |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 931 | nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id) |
| 932 | { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 933 | uint8_t i, q_idx, found = 0; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 934 | |
| 935 | for (i = 0; i < ps->queue_len; ++i) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 936 | /* get the actual queue idx */ |
| 937 | q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 938 | |
| 939 | if (found) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 940 | if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 941 | /* another equal value, simply cannot be */ |
| 942 | ERRINT; |
| 943 | } |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 944 | if (found == 2) { |
| 945 | /* move the following values */ |
| 946 | ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx]; |
| 947 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 948 | } else if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 949 | /* found our id, there can be no more equal valid values */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 950 | if (i == 0) { |
| 951 | found = 1; |
| 952 | } else { |
| 953 | /* this is not okay, our id is in the middle of the queue */ |
| 954 | found = 2; |
| 955 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 956 | } |
| 957 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 958 | if (!found) { |
| 959 | ERRINT; |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 960 | return; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 961 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 962 | |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 963 | --ps->queue_len; |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 964 | if (found == 1) { |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 965 | /* 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] | 966 | ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE; |
| 967 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 970 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 971 | 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] | 972 | { |
| 973 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 974 | struct timespec ts; |
| 975 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 976 | nc_gettimespec_real(&ts); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 977 | nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 978 | |
| 979 | /* LOCK */ |
| 980 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 981 | if (ret) { |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 982 | ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 983 | return -1; |
| 984 | } |
| 985 | |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 986 | /* check that the queue is long enough */ |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 987 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 988 | ERR("%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE); |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 989 | pthread_mutex_unlock(&ps->lock); |
| 990 | return -1; |
| 991 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 992 | |
| 993 | /* add ourselves into the queue */ |
| 994 | nc_ps_queue_add_id(ps, id); |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 995 | DBL("PS 0x%p TID %lu queue: added %u, head %u, lenght %u", ps, (long unsigned int)pthread_self(), *id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 996 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 997 | |
| 998 | /* is it our turn? */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 999 | while (ps->queue[ps->queue_begin] != *id) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1000 | nc_gettimespec_real(&ts); |
Michal Vasko | 2b76809 | 2018-02-12 16:37:12 +0100 | [diff] [blame] | 1001 | nc_addtimespec(&ts, NC_PS_QUEUE_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1002 | |
| 1003 | ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts); |
| 1004 | if (ret) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1005 | /** |
| 1006 | * This may happen when another thread releases the lock and broadcasts the condition |
| 1007 | * and this thread had already timed out. When this thread is scheduled, it returns timed out error |
| 1008 | * but when actually this thread was ready for condition. |
| 1009 | */ |
preetbhansali | 629dfc4 | 2018-12-17 16:04:40 +0530 | [diff] [blame] | 1010 | if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1011 | break; |
| 1012 | } |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1013 | |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1014 | ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1015 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1016 | nc_ps_queue_remove_id(ps, *id); |
| 1017 | pthread_mutex_unlock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1018 | return -1; |
| 1019 | } |
| 1020 | } |
| 1021 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1022 | /* UNLOCK */ |
| 1023 | pthread_mutex_unlock(&ps->lock); |
| 1024 | |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1028 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1029 | 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] | 1030 | { |
| 1031 | int ret; |
| 1032 | struct timespec ts; |
| 1033 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1034 | nc_gettimespec_real(&ts); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 1035 | nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* LOCK */ |
| 1038 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 1039 | if (ret) { |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1040 | ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1041 | ret = -1; |
| 1042 | } |
| 1043 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1044 | /* we must be the first, it was our turn after all, right? */ |
| 1045 | if (ps->queue[ps->queue_begin] != id) { |
| 1046 | ERRINT; |
Michal Vasko | b1a094b | 2016-10-05 14:04:52 +0200 | [diff] [blame] | 1047 | /* UNLOCK */ |
| 1048 | if (!ret) { |
| 1049 | pthread_mutex_unlock(&ps->lock); |
| 1050 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1051 | return -1; |
| 1052 | } |
| 1053 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1054 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1055 | nc_ps_queue_remove_id(ps, id); |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 1056 | DBL("PS 0x%p TID %lu queue: removed %u, head %u, lenght %u", ps, (long unsigned int)pthread_self(), id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1057 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1058 | |
| 1059 | /* broadcast to all other threads that the queue moved */ |
| 1060 | pthread_cond_broadcast(&ps->cond); |
| 1061 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1062 | /* UNLOCK */ |
| 1063 | if (!ret) { |
| 1064 | pthread_mutex_unlock(&ps->lock); |
| 1065 | } |
| 1066 | |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1070 | API struct nc_pollsession * |
| 1071 | nc_ps_new(void) |
| 1072 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1073 | struct nc_pollsession *ps; |
| 1074 | |
| 1075 | ps = calloc(1, sizeof(struct nc_pollsession)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1076 | if (!ps) { |
| 1077 | ERRMEM; |
| 1078 | return NULL; |
| 1079 | } |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1080 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1081 | pthread_mutex_init(&ps->lock, NULL); |
| 1082 | |
| 1083 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | API void |
| 1087 | nc_ps_free(struct nc_pollsession *ps) |
| 1088 | { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1089 | uint16_t i; |
| 1090 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1091 | if (!ps) { |
| 1092 | return; |
| 1093 | } |
| 1094 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1095 | if (ps->queue_len) { |
| 1096 | ERR("FATAL: Freeing a pollsession structure that is currently being worked with!"); |
| 1097 | } |
| 1098 | |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1099 | for (i = 0; i < ps->session_count; i++) { |
| 1100 | free(ps->sessions[i]); |
| 1101 | } |
| 1102 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1103 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1104 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1105 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1106 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1107 | free(ps); |
| 1108 | } |
| 1109 | |
| 1110 | API int |
| 1111 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1112 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1113 | uint8_t q_id; |
| 1114 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1115 | if (!ps) { |
| 1116 | ERRARG("ps"); |
| 1117 | return -1; |
| 1118 | } else if (!session) { |
| 1119 | ERRARG("session"); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1120 | return -1; |
| 1121 | } |
| 1122 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1123 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1124 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1125 | return -1; |
| 1126 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1127 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1128 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1129 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1130 | if (!ps->sessions) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1131 | ERRMEM; |
| 1132 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1133 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1134 | return -1; |
| 1135 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1136 | ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions); |
| 1137 | if (!ps->sessions[ps->session_count - 1]) { |
| 1138 | ERRMEM; |
| 1139 | --ps->session_count; |
| 1140 | /* UNLOCK */ |
| 1141 | nc_ps_unlock(ps, q_id, __func__); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | ps->sessions[ps->session_count - 1]->session = session; |
| 1145 | ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1146 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1147 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1148 | return nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1151 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1152 | _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] | 1153 | { |
| 1154 | uint16_t i; |
| 1155 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1156 | if (index >= 0) { |
| 1157 | i = (uint16_t)index; |
| 1158 | goto remove; |
| 1159 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1160 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1161 | if (ps->sessions[i]->session == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1162 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1163 | --ps->session_count; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1164 | if (i <= ps->session_count) { |
| 1165 | free(ps->sessions[i]); |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1166 | ps->sessions[i] = ps->sessions[ps->session_count]; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1167 | } |
| 1168 | if (!ps->session_count) { |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1169 | free(ps->sessions); |
| 1170 | ps->sessions = NULL; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1171 | } |
Michal Vasko | 11d2f6a | 2017-02-02 11:15:06 +0100 | [diff] [blame] | 1172 | ps->last_event_session = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | } |
| 1176 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1177 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1180 | API int |
| 1181 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1182 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1183 | uint8_t q_id; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1184 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1185 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1186 | if (!ps) { |
| 1187 | ERRARG("ps"); |
| 1188 | return -1; |
| 1189 | } else if (!session) { |
| 1190 | ERRARG("session"); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1195 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1196 | return -1; |
| 1197 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1198 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1199 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1202 | ret2 = nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1203 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1204 | return (ret || ret2 ? -1 : 0); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1205 | } |
| 1206 | |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1207 | API struct nc_session * |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1208 | nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx) |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1209 | { |
| 1210 | uint8_t q_id; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1211 | struct nc_session *ret = NULL; |
| 1212 | |
| 1213 | if (!ps) { |
| 1214 | ERRARG("ps"); |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | |
| 1218 | /* LOCK */ |
| 1219 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1223 | if (idx < ps->session_count) { |
| 1224 | ret = ps->sessions[idx]->session; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | /* UNLOCK */ |
| 1228 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1229 | |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1233 | API uint16_t |
| 1234 | nc_ps_session_count(struct nc_pollsession *ps) |
| 1235 | { |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1236 | uint8_t q_id; |
| 1237 | uint16_t session_count; |
| 1238 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1239 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1240 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1244 | /* LOCK (just for memory barrier so that we read the current value) */ |
| 1245 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | session_count = ps->session_count; |
| 1250 | |
| 1251 | /* UNLOCK */ |
| 1252 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1253 | |
| 1254 | return session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1257 | /* 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] | 1258 | * returns: NC_PSPOLL_ERROR, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1259 | * NC_PSPOLL_TIMEOUT, |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1260 | * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR, |
| 1261 | * NC_PSPOLL_RPC |
| 1262 | */ |
| 1263 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1264 | 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] | 1265 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1266 | struct ly_in *msg; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1267 | struct nc_server_reply *reply = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1268 | int r, ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1269 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1270 | if (!session) { |
| 1271 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1272 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1273 | } else if (!rpc) { |
| 1274 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1275 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1276 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1277 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1278 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1281 | /* get a message */ |
| 1282 | r = nc_read_msg_io(session, io_timeout, &msg, 0); |
| 1283 | if (r == -2) { |
| 1284 | /* malformed message */ |
| 1285 | ret = NC_PSPOLL_REPLY_ERROR; |
| 1286 | reply = nc_server_reply_err(nc_err(server_opts.ctx, NC_ERR_MALFORMED_MSG)); |
| 1287 | goto send_reply; |
| 1288 | } if (r == -1) { |
| 1289 | return NC_PSPOLL_ERROR; |
| 1290 | } else if (!r) { |
| 1291 | return NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1294 | *rpc = calloc(1, sizeof **rpc); |
| 1295 | if (!*rpc) { |
| 1296 | ERRMEM; |
| 1297 | ret = NC_PSPOLL_REPLY_ERROR; |
| 1298 | goto cleanup; |
| 1299 | } |
| 1300 | |
| 1301 | /* parse the RPC */ |
| 1302 | if (lyd_parse_op(server_opts.ctx, NULL, msg, LYD_XML, LYD_TYPE_RPC_NETCONF, &(*rpc)->envp, &(*rpc)->rpc)) { |
| 1303 | /* bad RPC received */ |
| 1304 | ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC; |
| 1305 | |
| 1306 | if ((*rpc)->envp) { |
| 1307 | /* at least the envelopes were parsed */ |
| 1308 | reply = nc_server_reply_err(nc_err_libyang(server_opts.ctx)); |
| 1309 | } else if (session->version == NC_VERSION_11) { |
| 1310 | /* completely malformed message, NETCONF version 1.1 defines sending error reply from the server (RFC 6241 sec. 3) */ |
| 1311 | reply = nc_server_reply_err(nc_err(server_opts.ctx, NC_ERR_MALFORMED_MSG)); |
| 1312 | } |
| 1313 | |
| 1314 | send_reply: |
| 1315 | if (reply) { |
| 1316 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, (*rpc)->envp, reply); |
| 1317 | nc_server_reply_free(reply); |
| 1318 | if (r != NC_MSG_REPLY) { |
| 1319 | ERR("Session %u: failed to write reply (%s), terminating session.", session->id, nc_msgtype2str[r]); |
| 1320 | if (session->status != NC_STATUS_INVALID) { |
| 1321 | session->status = NC_STATUS_INVALID; |
| 1322 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | } else { |
| 1327 | ret = NC_PSPOLL_RPC; |
| 1328 | } |
| 1329 | |
| 1330 | cleanup: |
| 1331 | ly_in_free(msg, 1); |
| 1332 | if (ret != NC_PSPOLL_RPC) { |
| 1333 | nc_server_rpc_free(*rpc); |
| 1334 | *rpc = NULL; |
| 1335 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1336 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1339 | API void |
| 1340 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1341 | { |
| 1342 | global_rpc_clb = clb; |
| 1343 | } |
| 1344 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1345 | API NC_MSG_TYPE |
| 1346 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1347 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1348 | NC_MSG_TYPE ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1349 | |
| 1350 | /* check parameters */ |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1351 | if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1352 | ERRARG("session"); |
| 1353 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1354 | } else if (!notif || !notif->ntf || !notif->eventtime) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1355 | ERRARG("notif"); |
| 1356 | return NC_MSG_ERROR; |
| 1357 | } |
| 1358 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1359 | /* we do not need RPC lock for this, IO lock will be acquired properly */ |
| 1360 | ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif); |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 1361 | if (ret != NC_MSG_NOTIF) { |
| 1362 | ERR("Session %u: failed to write notification (%s).", session->id, nc_msgtype2str[ret]); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1363 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1364 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1365 | return ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1368 | /* must be called holding the session RPC lock! IO lock will be acquired as needed |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1369 | * returns: NC_PSPOLL_ERROR, |
| 1370 | * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR, |
| 1371 | * NC_PSPOLL_REPLY_ERROR, |
| 1372 | * 0 |
| 1373 | */ |
| 1374 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1375 | nc_server_send_reply_io(struct nc_session *session, int io_timeout, struct nc_server_rpc *rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1376 | { |
| 1377 | nc_rpc_clb clb; |
| 1378 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1379 | const struct lysc_node *rpc_act = NULL; |
| 1380 | struct lyd_node *elem; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1381 | int ret = 0; |
| 1382 | NC_MSG_TYPE r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1383 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1384 | if (!rpc) { |
| 1385 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1386 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1389 | if (rpc->rpc->schema->nodetype == LYS_RPC) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1390 | /* RPC */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1391 | rpc_act = rpc->rpc->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1392 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1393 | /* action */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1394 | LYD_TREE_DFS_BEGIN(rpc->rpc, elem) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1395 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1396 | rpc_act = elem->schema; |
| 1397 | break; |
| 1398 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1399 | LYD_TREE_DFS_END(rpc->rpc, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1400 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1401 | if (!rpc_act) { |
| 1402 | ERRINT; |
| 1403 | return NC_PSPOLL_ERROR; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | if (!rpc_act->priv) { |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1408 | if (!global_rpc_clb) { |
| 1409 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1410 | reply = nc_server_reply_err(nc_err(server_opts.ctx, NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT)); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1411 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1412 | reply = global_rpc_clb(rpc->rpc, session); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1413 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1414 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1415 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1416 | reply = clb(rpc->rpc, session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | if (!reply) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1420 | reply = nc_server_reply_err(nc_err(server_opts.ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1421 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1422 | 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] | 1423 | if (reply->type == NC_RPL_ERROR) { |
| 1424 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1425 | } |
| 1426 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1427 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1428 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 1429 | ERR("Session %u: failed to write reply (%s).", session->id, nc_msgtype2str[r]); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1430 | ret |= NC_PSPOLL_ERROR; |
| 1431 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1432 | |
| 1433 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1434 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1435 | session->status = NC_STATUS_INVALID; |
| 1436 | } |
| 1437 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1438 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1439 | } |
| 1440 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1441 | /* session must be running and session RPC lock held! |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1442 | * returns: NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR, (msg filled) |
| 1443 | * NC_PSPOLL_ERROR, (msg filled) |
| 1444 | * NC_PSPOLL_TIMEOUT, |
| 1445 | * NC_PSPOLL_RPC (some application data available), |
| 1446 | * NC_PSPOLL_SSH_CHANNEL, |
| 1447 | * NC_PSPOLL_SSH_MSG |
| 1448 | */ |
| 1449 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1450 | 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] | 1451 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1452 | struct pollfd pfd; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 1453 | int r, ret = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1454 | #ifdef NC_ENABLED_SSH |
| 1455 | struct nc_session *new; |
| 1456 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1457 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1458 | /* check timeout first */ |
| 1459 | if (!(session->flags & NC_SESSION_CALLHOME) && !session->opts.server.ntf_status && server_opts.idle_timeout |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1460 | && (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1461 | sprintf(msg, "session idle timeout elapsed"); |
| 1462 | session->status = NC_STATUS_INVALID; |
| 1463 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1464 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1465 | } |
| 1466 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1467 | r = nc_session_io_lock(session, io_timeout, __func__); |
| 1468 | if (r < 0) { |
| 1469 | sprintf(msg, "session IO lock failed to be acquired"); |
| 1470 | return NC_PSPOLL_ERROR; |
| 1471 | } else if (!r) { |
| 1472 | return NC_PSPOLL_TIMEOUT; |
| 1473 | } |
| 1474 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1475 | switch (session->ti_type) { |
| 1476 | #ifdef NC_ENABLED_SSH |
| 1477 | case NC_TI_LIBSSH: |
| 1478 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1479 | if (r == SSH_EOF) { |
| 1480 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1481 | session->status = NC_STATUS_INVALID; |
| 1482 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1483 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1484 | } else if (r == SSH_ERROR) { |
| 1485 | 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] | 1486 | session->status = NC_STATUS_INVALID; |
| 1487 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1488 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1489 | } else if (!r) { |
| 1490 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1491 | /* new SSH message */ |
| 1492 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1493 | if (session->ti.libssh.next) { |
| 1494 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
| 1495 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel |
| 1496 | && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1497 | /* new NETCONF SSH channel */ |
| 1498 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1499 | break; |
| 1500 | } |
| 1501 | } |
| 1502 | if (new != session) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1503 | break; |
| 1504 | } |
| 1505 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1506 | |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1507 | /* just some SSH message */ |
| 1508 | ret = NC_PSPOLL_SSH_MSG; |
| 1509 | } else { |
| 1510 | ret = NC_PSPOLL_TIMEOUT; |
| 1511 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1512 | } else { |
| 1513 | /* we have some application data */ |
| 1514 | ret = NC_PSPOLL_RPC; |
| 1515 | } |
| 1516 | break; |
| 1517 | #endif |
| 1518 | #ifdef NC_ENABLED_TLS |
| 1519 | case NC_TI_OPENSSL: |
| 1520 | r = SSL_pending(session->ti.tls); |
| 1521 | if (!r) { |
| 1522 | /* no data pending in the SSL buffer, poll fd */ |
| 1523 | pfd.fd = SSL_get_rfd(session->ti.tls); |
| 1524 | if (pfd.fd < 0) { |
| 1525 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1526 | ret = NC_PSPOLL_ERROR; |
| 1527 | break; |
| 1528 | } |
| 1529 | pfd.events = POLLIN; |
| 1530 | pfd.revents = 0; |
| 1531 | r = poll(&pfd, 1, 0); |
| 1532 | |
| 1533 | if ((r < 0) && (errno != EINTR)) { |
| 1534 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1535 | session->status = NC_STATUS_INVALID; |
| 1536 | ret = NC_PSPOLL_ERROR; |
| 1537 | } else if (r > 0) { |
| 1538 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1539 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1540 | session->status = NC_STATUS_INVALID; |
| 1541 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1542 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1543 | } else if (pfd.revents & POLLERR) { |
| 1544 | sprintf(msg, "communication socket error"); |
| 1545 | session->status = NC_STATUS_INVALID; |
| 1546 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1547 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1548 | } else { |
| 1549 | ret = NC_PSPOLL_RPC; |
| 1550 | } |
| 1551 | } else { |
| 1552 | ret = NC_PSPOLL_TIMEOUT; |
| 1553 | } |
| 1554 | } else { |
| 1555 | ret = NC_PSPOLL_RPC; |
| 1556 | } |
| 1557 | break; |
| 1558 | #endif |
| 1559 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1560 | case NC_TI_UNIX: |
| 1561 | 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] | 1562 | pfd.events = POLLIN; |
| 1563 | pfd.revents = 0; |
| 1564 | r = poll(&pfd, 1, 0); |
| 1565 | |
| 1566 | if ((r < 0) && (errno != EINTR)) { |
| 1567 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1568 | session->status = NC_STATUS_INVALID; |
| 1569 | ret = NC_PSPOLL_ERROR; |
| 1570 | } else if (r > 0) { |
| 1571 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1572 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1573 | session->status = NC_STATUS_INVALID; |
| 1574 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1575 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1576 | } else if (pfd.revents & POLLERR) { |
| 1577 | sprintf(msg, "communication socket error"); |
| 1578 | session->status = NC_STATUS_INVALID; |
| 1579 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1580 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1581 | } else { |
| 1582 | ret = NC_PSPOLL_RPC; |
| 1583 | } |
| 1584 | } else { |
| 1585 | ret = NC_PSPOLL_TIMEOUT; |
| 1586 | } |
| 1587 | break; |
| 1588 | case NC_TI_NONE: |
| 1589 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1590 | ret = NC_PSPOLL_ERROR; |
| 1591 | break; |
| 1592 | } |
| 1593 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1594 | nc_session_io_unlock(session, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | API int |
| 1599 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1600 | { |
| 1601 | int ret, r; |
| 1602 | uint8_t q_id; |
| 1603 | uint16_t i, j; |
| 1604 | char msg[256]; |
| 1605 | struct timespec ts_timeout, ts_cur; |
| 1606 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1607 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1608 | struct nc_server_rpc *rpc = NULL; |
| 1609 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1610 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1611 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1612 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1613 | } |
| 1614 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1615 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1616 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1617 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1618 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1619 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1620 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1621 | nc_ps_unlock(ps, q_id, __func__); |
| 1622 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1623 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1624 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1625 | /* fill timespecs */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1626 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1627 | if (timeout > -1) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1628 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1629 | nc_addtimespec(&ts_timeout, timeout); |
| 1630 | } |
| 1631 | |
| 1632 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1633 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1634 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1635 | if (ps->last_event_session == ps->session_count - 1) { |
| 1636 | i = j = 0; |
| 1637 | } else { |
| 1638 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1639 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1640 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1641 | cur_ps_session = ps->sessions[i]; |
| 1642 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1643 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1644 | /* SESSION RPC LOCK */ |
| 1645 | r = nc_session_rpc_lock(cur_session, 0, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1646 | if (r == -1) { |
| 1647 | ret = NC_PSPOLL_ERROR; |
| 1648 | } else if (r == 1) { |
| 1649 | /* 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] | 1650 | switch (cur_ps_session->state) { |
| 1651 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1652 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1653 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1654 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1655 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1656 | 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] | 1657 | switch (ret) { |
| 1658 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1659 | ERR("Session %u: %s.", cur_session->id, msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1660 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1661 | break; |
| 1662 | case NC_PSPOLL_ERROR: |
| 1663 | ERR("Session %u: %s.", cur_session->id, msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1664 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1665 | break; |
| 1666 | case NC_PSPOLL_TIMEOUT: |
| 1667 | #ifdef NC_ENABLED_SSH |
| 1668 | case NC_PSPOLL_SSH_CHANNEL: |
| 1669 | case NC_PSPOLL_SSH_MSG: |
| 1670 | #endif |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1671 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1672 | break; |
| 1673 | case NC_PSPOLL_RPC: |
| 1674 | /* let's keep the state busy, we are not done with this session */ |
| 1675 | break; |
| 1676 | } |
| 1677 | } else { |
| 1678 | /* session is not fine, let the caller know */ |
| 1679 | ret = NC_PSPOLL_SESSION_TERM; |
| 1680 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1681 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1682 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1683 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1684 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1685 | break; |
| 1686 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1687 | /* it definitely should not be busy because we have the lock */ |
| 1688 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1689 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1690 | break; |
| 1691 | case NC_PS_STATE_INVALID: |
| 1692 | /* we got it locked, but it will be freed, let it be */ |
| 1693 | ret = NC_PSPOLL_TIMEOUT; |
| 1694 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1695 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1696 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1697 | /* keep RPC lock in this one case */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1698 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1699 | /* SESSION RPC UNLOCK */ |
| 1700 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1701 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1702 | } else { |
| 1703 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1704 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1705 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1706 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1707 | /* something happened */ |
| 1708 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1709 | break; |
| 1710 | } |
| 1711 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1712 | if (i == ps->session_count - 1) { |
| 1713 | i = 0; |
| 1714 | } else { |
| 1715 | ++i; |
| 1716 | } |
| 1717 | } while (i != j); |
| 1718 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1719 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1720 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1721 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1722 | /* update current time */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1723 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1724 | |
| 1725 | if ((timeout > -1) && (nc_difftimespec(&ts_cur, &ts_timeout) < 1)) { |
| 1726 | /* final timeout */ |
| 1727 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1728 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1729 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1730 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1731 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1732 | /* do we want to return the session? */ |
| 1733 | switch (ret) { |
| 1734 | case NC_PSPOLL_RPC: |
| 1735 | case NC_PSPOLL_SESSION_TERM: |
| 1736 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1737 | #ifdef NC_ENABLED_SSH |
| 1738 | case NC_PSPOLL_SSH_CHANNEL: |
| 1739 | case NC_PSPOLL_SSH_MSG: |
| 1740 | #endif |
| 1741 | if (session) { |
| 1742 | *session = cur_session; |
| 1743 | } |
| 1744 | ps->last_event_session = i; |
| 1745 | break; |
| 1746 | default: |
| 1747 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1748 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1749 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1750 | /* PS UNLOCK */ |
| 1751 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1752 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1753 | /* 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] | 1754 | if (ret == NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1755 | ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1756 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1757 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1758 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1759 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1760 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1761 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1762 | } |
| 1763 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1764 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1765 | |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1766 | /* process RPC */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1767 | ret |= nc_server_send_reply_io(cur_session, timeout, rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1768 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1769 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1770 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1771 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1772 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1773 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1774 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1775 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1776 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1777 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1778 | nc_server_rpc_free(rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1779 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1780 | /* SESSION RPC UNLOCK */ |
| 1781 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1784 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1785 | } |
| 1786 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1787 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1788 | 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] | 1789 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1790 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1791 | uint16_t i; |
| 1792 | struct nc_session *session; |
| 1793 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1794 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1795 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1796 | return; |
| 1797 | } |
| 1798 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1799 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1800 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1801 | return; |
| 1802 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1803 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1804 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1805 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1806 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1807 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1808 | } |
| 1809 | free(ps->sessions); |
| 1810 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1811 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1812 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1813 | } else { |
| 1814 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1815 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1816 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1817 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1818 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1819 | continue; |
| 1820 | } |
| 1821 | |
| 1822 | ++i; |
| 1823 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1824 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1825 | |
| 1826 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1827 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1830 | static int |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1831 | nc_get_uid(int sock, uid_t *uid) |
| 1832 | { |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1833 | int ret; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1834 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1835 | #ifdef SO_PEERCRED |
| 1836 | struct ucred ucred; |
| 1837 | socklen_t len; |
| 1838 | len = sizeof(ucred); |
| 1839 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len); |
| 1840 | if (!ret) { |
| 1841 | *uid = ucred.uid; |
| 1842 | } |
| 1843 | #else |
| 1844 | ret = getpeereid(sock, uid, NULL); |
| 1845 | #endif |
| 1846 | |
| 1847 | if (ret < 0) { |
| 1848 | ERR("Failed to get credentials from unix socket (%s).", strerror(errno)); |
| 1849 | return -1; |
| 1850 | } |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | static int |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1855 | nc_accept_unix(struct nc_session *session, int sock) |
| 1856 | { |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1857 | #if defined(SO_PEERCRED) || defined(HAVE_GETPEEREID) |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1858 | const struct passwd *pw; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1859 | char *username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1860 | session->ti_type = NC_TI_UNIX; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1861 | uid_t uid; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1862 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1863 | if (nc_get_uid(sock, &uid)) { |
| 1864 | close(sock); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1865 | return -1; |
| 1866 | } |
| 1867 | |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1868 | pw = getpwuid(uid); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1869 | if (pw == NULL) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1870 | ERR("Failed to find username for uid=%u (%s).\n", uid, strerror(errno)); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1871 | close(sock); |
| 1872 | return -1; |
| 1873 | } |
| 1874 | |
| 1875 | username = strdup(pw->pw_name); |
| 1876 | if (username == NULL) { |
| 1877 | ERRMEM; |
| 1878 | close(sock); |
| 1879 | return -1; |
| 1880 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1881 | lydict_insert_zc(server_opts.ctx, username, &session->username); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1882 | |
| 1883 | session->ti.unixsock.sock = sock; |
| 1884 | |
| 1885 | return 1; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 1886 | #else |
| 1887 | return -1; |
| 1888 | #endif |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1891 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1892 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1893 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1894 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1895 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1896 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1897 | if (!name) { |
| 1898 | ERRARG("name"); |
| 1899 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1902 | /* BIND LOCK */ |
| 1903 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1904 | |
| 1905 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1906 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1907 | |
| 1908 | /* check name uniqueness */ |
| 1909 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1910 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1911 | ERR("Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1912 | ret = -1; |
| 1913 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1917 | ++server_opts.endpt_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1918 | server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1919 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1920 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1921 | ret = -1; |
| 1922 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1923 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1924 | memset(&server_opts.endpts[server_opts.endpt_count - 1], 0, sizeof *server_opts.endpts); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1925 | lydict_insert(server_opts.ctx, name, 0, &server_opts.endpts[server_opts.endpt_count - 1].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1926 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1927 | server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1; |
| 1928 | server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10; |
| 1929 | server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1930 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1931 | server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1932 | if (!server_opts.binds) { |
| 1933 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1934 | ret = -1; |
| 1935 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1936 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1937 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1938 | memset(&server_opts.binds[server_opts.endpt_count - 1], 0, sizeof *server_opts.binds); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1939 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
| 1940 | |
| 1941 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1942 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1943 | case NC_TI_LIBSSH: |
| 1944 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1945 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 1946 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1947 | ret = -1; |
| 1948 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1949 | } |
| 1950 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1951 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1952 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 1953 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 30; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1954 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1955 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1956 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1957 | case NC_TI_OPENSSL: |
| 1958 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 1959 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 1960 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1961 | ret = -1; |
| 1962 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1963 | } |
| 1964 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1965 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1966 | case NC_TI_UNIX: |
| 1967 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 1968 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) { |
| 1969 | ERRMEM; |
| 1970 | ret = -1; |
| 1971 | goto cleanup; |
| 1972 | } |
| 1973 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1; |
| 1974 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1; |
| 1975 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1; |
| 1976 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1977 | default: |
| 1978 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1979 | ret = -1; |
| 1980 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1981 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1982 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1983 | cleanup: |
| 1984 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1985 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1986 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1987 | /* BIND UNLOCK */ |
| 1988 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1989 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1990 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1993 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1994 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1995 | { |
| 1996 | uint32_t i; |
| 1997 | int ret = -1; |
| 1998 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1999 | /* BIND LOCK */ |
| 2000 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2001 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2002 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2003 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2004 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2005 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2006 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2007 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2008 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2009 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2010 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2011 | case NC_TI_LIBSSH: |
| 2012 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2013 | free(server_opts.endpts[i].opts.ssh); |
| 2014 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2015 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2016 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2017 | case NC_TI_OPENSSL: |
| 2018 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2019 | free(server_opts.endpts[i].opts.tls); |
| 2020 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2021 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2022 | case NC_TI_UNIX: |
| 2023 | free(server_opts.endpts[i].opts.unixsock); |
| 2024 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2025 | default: |
| 2026 | ERRINT; |
| 2027 | /* won't get here ...*/ |
| 2028 | break; |
| 2029 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2030 | ret = 0; |
| 2031 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2032 | free(server_opts.endpts); |
| 2033 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* remove all binds */ |
| 2036 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2037 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 2038 | if (server_opts.binds[i].sock > -1) { |
| 2039 | close(server_opts.binds[i].sock); |
| 2040 | } |
| 2041 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2042 | free(server_opts.binds); |
| 2043 | server_opts.binds = NULL; |
| 2044 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2045 | server_opts.endpt_count = 0; |
| 2046 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2047 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2048 | /* remove one endpoint with bind(s) or all endpoints using one transport protocol */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2049 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2050 | if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2051 | /* remove endpt */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2052 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2053 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2054 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2055 | case NC_TI_LIBSSH: |
| 2056 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2057 | free(server_opts.endpts[i].opts.ssh); |
| 2058 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2059 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2060 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2061 | case NC_TI_OPENSSL: |
| 2062 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2063 | free(server_opts.endpts[i].opts.tls); |
| 2064 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2065 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2066 | case NC_TI_UNIX: |
| 2067 | free(server_opts.endpts[i].opts.unixsock); |
| 2068 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2069 | default: |
| 2070 | ERRINT; |
| 2071 | break; |
| 2072 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2073 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2074 | /* remove bind(s) */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2075 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 2076 | if (server_opts.binds[i].sock > -1) { |
| 2077 | close(server_opts.binds[i].sock); |
| 2078 | } |
| 2079 | |
| 2080 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2081 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2082 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2083 | free(server_opts.binds); |
| 2084 | server_opts.binds = NULL; |
| 2085 | free(server_opts.endpts); |
| 2086 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2087 | } else if (i < server_opts.endpt_count) { |
| 2088 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 2089 | memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2090 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2091 | |
| 2092 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2093 | if (name) { |
| 2094 | break; |
| 2095 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2096 | } |
| 2097 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2100 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2101 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2102 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2103 | /* BIND UNLOCK */ |
| 2104 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2105 | |
| 2106 | return ret; |
| 2107 | } |
| 2108 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2109 | API int |
| 2110 | nc_server_endpt_count(void) |
| 2111 | { |
| 2112 | return server_opts.endpt_count; |
| 2113 | } |
| 2114 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2115 | API int |
| 2116 | nc_server_is_endpt(const char *name) |
| 2117 | { |
| 2118 | uint16_t i; |
| 2119 | int found = 0; |
| 2120 | |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2121 | if (!name) { |
| 2122 | return found; |
| 2123 | } |
| 2124 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2125 | /* ENDPT READ LOCK */ |
| 2126 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2127 | |
| 2128 | /* check name uniqueness */ |
| 2129 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2130 | if (!strcmp(server_opts.endpts[i].name, name)) { |
| 2131 | found = 1; |
| 2132 | break; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | /* ENDPT UNLOCK */ |
| 2137 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2138 | |
| 2139 | return found; |
| 2140 | } |
| 2141 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2142 | int |
| 2143 | nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port) |
| 2144 | { |
| 2145 | struct nc_endpt *endpt; |
| 2146 | struct nc_bind *bind = NULL; |
| 2147 | uint16_t i; |
| 2148 | int sock = -1, set_addr, ret = 0; |
| 2149 | |
| 2150 | if (!endpt_name) { |
| 2151 | ERRARG("endpt_name"); |
| 2152 | return -1; |
| 2153 | } else if ((!address && !port) || (address && port)) { |
| 2154 | ERRARG("address and port"); |
| 2155 | return -1; |
| 2156 | } |
| 2157 | |
| 2158 | if (address) { |
| 2159 | set_addr = 1; |
| 2160 | } else { |
| 2161 | set_addr = 0; |
| 2162 | } |
| 2163 | |
| 2164 | /* BIND LOCK */ |
| 2165 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2166 | |
| 2167 | /* ENDPT LOCK */ |
| 2168 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2169 | if (!endpt) { |
| 2170 | /* BIND UNLOCK */ |
| 2171 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2172 | return -1; |
| 2173 | } |
| 2174 | |
| 2175 | bind = &server_opts.binds[i]; |
| 2176 | |
| 2177 | if (set_addr) { |
| 2178 | port = bind->port; |
| 2179 | } else { |
| 2180 | address = bind->address; |
| 2181 | } |
| 2182 | |
| 2183 | if (!set_addr && endpt->ti == NC_TI_UNIX) { |
| 2184 | ret = -1; |
| 2185 | goto cleanup; |
| 2186 | } |
| 2187 | |
| 2188 | /* we have all the information we need to create a listening socket */ |
| 2189 | if (address && (port || endpt->ti == NC_TI_UNIX)) { |
| 2190 | /* create new socket, close the old one */ |
| 2191 | if (endpt->ti == NC_TI_UNIX) |
| 2192 | sock = nc_sock_listen_unix(address, endpt->opts.unixsock); |
| 2193 | else |
| 2194 | sock = nc_sock_listen_inet(address, port, &endpt->ka); |
| 2195 | if (sock == -1) { |
| 2196 | ret = -1; |
| 2197 | goto cleanup; |
| 2198 | } |
| 2199 | |
| 2200 | if (bind->sock > -1) { |
| 2201 | close(bind->sock); |
| 2202 | } |
| 2203 | bind->sock = sock; |
| 2204 | } /* else we are just setting address or port */ |
| 2205 | |
| 2206 | if (set_addr) { |
| 2207 | lydict_remove(server_opts.ctx, bind->address); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2208 | lydict_insert(server_opts.ctx, address, 0, &bind->address); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2209 | } else { |
| 2210 | bind->port = port; |
| 2211 | } |
| 2212 | |
| 2213 | if (sock > -1) { |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2214 | switch (endpt->ti) { |
| 2215 | case NC_TI_UNIX: |
| 2216 | VRB("Listening on %s for UNIX connections.", address); |
| 2217 | break; |
| 2218 | #ifdef NC_ENABLED_SSH |
| 2219 | case NC_TI_LIBSSH: |
| 2220 | VRB("Listening on %s:%u for SSH connections.", address, port); |
| 2221 | break; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2222 | #endif |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2223 | #ifdef NC_ENABLED_TLS |
| 2224 | case NC_TI_OPENSSL: |
| 2225 | VRB("Listening on %s:%u for TLS connections.", address, port); |
| 2226 | break; |
| 2227 | #endif |
| 2228 | default: |
| 2229 | ERRINT; |
| 2230 | break; |
| 2231 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | cleanup: |
| 2235 | /* ENDPT UNLOCK */ |
| 2236 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2237 | |
| 2238 | /* BIND UNLOCK */ |
| 2239 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2240 | |
| 2241 | return ret; |
| 2242 | } |
| 2243 | |
| 2244 | API int |
| 2245 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 2246 | { |
| 2247 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 2248 | } |
| 2249 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2250 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 2251 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2252 | API int |
| 2253 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 2254 | { |
| 2255 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 2256 | } |
| 2257 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2258 | #endif |
| 2259 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2260 | API int |
| 2261 | nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid) |
| 2262 | { |
| 2263 | struct nc_endpt *endpt; |
| 2264 | uint16_t i; |
| 2265 | int ret = 0; |
| 2266 | |
| 2267 | if (!endpt_name) { |
| 2268 | ERRARG("endpt_name"); |
| 2269 | return -1; |
| 2270 | } else if (mode == 0) { |
| 2271 | ERRARG("mode"); |
| 2272 | return -1; |
| 2273 | } |
| 2274 | |
| 2275 | /* ENDPT LOCK */ |
| 2276 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2277 | if (!endpt) |
| 2278 | return -1; |
| 2279 | |
| 2280 | if (endpt->ti != NC_TI_UNIX) { |
| 2281 | ret = -1; |
| 2282 | goto cleanup; |
| 2283 | } |
| 2284 | |
| 2285 | endpt->opts.unixsock->mode = mode; |
| 2286 | endpt->opts.unixsock->uid = uid; |
| 2287 | endpt->opts.unixsock->gid = gid; |
| 2288 | |
| 2289 | cleanup: |
| 2290 | /* ENDPT UNLOCK */ |
| 2291 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2292 | |
| 2293 | return ret; |
| 2294 | } |
| 2295 | |
| 2296 | API int |
| 2297 | nc_server_endpt_enable_keepalives(const char *endpt_name, int enable) |
| 2298 | { |
| 2299 | struct nc_endpt *endpt; |
| 2300 | int ret = 0; |
| 2301 | |
| 2302 | if (!endpt_name) { |
| 2303 | ERRARG("endpt_name"); |
| 2304 | return -1; |
| 2305 | } |
| 2306 | |
| 2307 | /* ENDPT LOCK */ |
| 2308 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2309 | if (!endpt) { |
| 2310 | return -1; |
| 2311 | } |
| 2312 | |
| 2313 | endpt->ka.enabled = (enable ? 1 : 0); |
| 2314 | |
| 2315 | /* ENDPT UNLOCK */ |
| 2316 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2317 | |
| 2318 | return ret; |
| 2319 | } |
| 2320 | |
| 2321 | API int |
| 2322 | nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval) |
| 2323 | { |
| 2324 | struct nc_endpt *endpt; |
| 2325 | int ret = 0; |
| 2326 | |
| 2327 | if (!endpt_name) { |
| 2328 | ERRARG("endpt_name"); |
| 2329 | return -1; |
| 2330 | } |
| 2331 | |
| 2332 | /* ENDPT LOCK */ |
| 2333 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2334 | if (!endpt) { |
| 2335 | return -1; |
| 2336 | } |
| 2337 | |
| 2338 | if (idle_time > -1) { |
| 2339 | endpt->ka.idle_time = idle_time; |
| 2340 | } |
| 2341 | if (max_probes > -1) { |
| 2342 | endpt->ka.max_probes = max_probes; |
| 2343 | } |
| 2344 | if (probe_interval > -1) { |
| 2345 | endpt->ka.probe_interval = probe_interval; |
| 2346 | } |
| 2347 | |
| 2348 | /* ENDPT UNLOCK */ |
| 2349 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2350 | |
| 2351 | return ret; |
| 2352 | } |
| 2353 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2354 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2355 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2356 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2357 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2358 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2359 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2360 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2361 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2362 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2363 | if (!server_opts.ctx) { |
| 2364 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2365 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2366 | } else if (!session) { |
| 2367 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2368 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2371 | /* BIND LOCK */ |
| 2372 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2373 | |
| 2374 | if (!server_opts.endpt_count) { |
Michal Vasko | 863a6e9 | 2016-07-28 14:27:41 +0200 | [diff] [blame] | 2375 | ERR("No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2376 | /* BIND UNLOCK */ |
| 2377 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2378 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2379 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2380 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2381 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx); |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 2382 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2383 | /* BIND UNLOCK */ |
| 2384 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 2385 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 2386 | if (!ret) { |
| 2387 | return NC_MSG_WOULDBLOCK; |
| 2388 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2389 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2390 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2391 | |
| 2392 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 2393 | /* ENDPT READ LOCK */ |
| 2394 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2395 | |
| 2396 | /* BIND UNLOCK */ |
| 2397 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2398 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2399 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2400 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2401 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 2402 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2403 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2404 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2405 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2406 | msgtype = NC_MSG_ERROR; |
| 2407 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2408 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2409 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2410 | (*session)->ctx = server_opts.ctx; |
| 2411 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2412 | lydict_insert_zc(server_opts.ctx, host, &(*session)->host); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2413 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2414 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2415 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2416 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2417 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 2418 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2419 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2420 | if (ret < 0) { |
| 2421 | msgtype = NC_MSG_ERROR; |
| 2422 | goto cleanup; |
| 2423 | } else if (!ret) { |
| 2424 | msgtype = NC_MSG_WOULDBLOCK; |
| 2425 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2426 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2427 | } else |
| 2428 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2429 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2430 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 2431 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2432 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2433 | if (ret < 0) { |
| 2434 | msgtype = NC_MSG_ERROR; |
| 2435 | goto cleanup; |
| 2436 | } else if (!ret) { |
| 2437 | msgtype = NC_MSG_WOULDBLOCK; |
| 2438 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2439 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2440 | } else |
| 2441 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2442 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2443 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2444 | ret = nc_accept_unix(*session, sock); |
| 2445 | if (ret < 0) { |
| 2446 | msgtype = NC_MSG_ERROR; |
| 2447 | goto cleanup; |
| 2448 | } |
| 2449 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2450 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2451 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2452 | msgtype = NC_MSG_ERROR; |
| 2453 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2454 | } |
| 2455 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2456 | (*session)->data = NULL; |
| 2457 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2458 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2459 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2460 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2461 | /* assign new SID atomically */ |
Michal Vasko | 7f1fa3c | 2020-09-08 16:30:41 +0200 | [diff] [blame] | 2462 | (*session)->id = ATOMIC_INC(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2463 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2464 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2465 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2466 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2467 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2468 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2469 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2470 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2471 | |
| 2472 | nc_gettimespec_mono(&ts_cur); |
| 2473 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 2474 | nc_gettimespec_real(&ts_cur); |
| 2475 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2476 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2477 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2478 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2479 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2480 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2481 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2482 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2483 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2484 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2485 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2486 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2487 | } |
| 2488 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2489 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 2490 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2491 | /* client is expected to be locked */ |
| 2492 | static int |
| 2493 | _nc_server_ch_client_del_endpt(struct nc_ch_client *client, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2494 | { |
| 2495 | uint16_t i; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2496 | int ret = -1; |
| 2497 | |
| 2498 | if (!endpt_name) { |
| 2499 | /* remove all endpoints */ |
| 2500 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2501 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2502 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2503 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2504 | close(client->ch_endpts[i].sock_pending); |
| 2505 | } |
| 2506 | switch (client->ch_endpts[i].ti) { |
| 2507 | #ifdef NC_ENABLED_SSH |
| 2508 | case NC_TI_LIBSSH: |
| 2509 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2510 | free(client->ch_endpts[i].opts.ssh); |
| 2511 | break; |
| 2512 | #endif |
| 2513 | #ifdef NC_ENABLED_TLS |
| 2514 | case NC_TI_OPENSSL: |
| 2515 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2516 | free(client->ch_endpts[i].opts.tls); |
| 2517 | break; |
| 2518 | #endif |
| 2519 | default: |
| 2520 | ERRINT; |
| 2521 | /* won't get here ...*/ |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | free(client->ch_endpts); |
| 2526 | client->ch_endpts = NULL; |
| 2527 | client->ch_endpt_count = 0; |
| 2528 | |
| 2529 | ret = 0; |
| 2530 | } else { |
| 2531 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2532 | if (!strcmp(client->ch_endpts[i].name, endpt_name) && (!ti || (ti == client->ch_endpts[i].ti))) { |
| 2533 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2534 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2535 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2536 | close(client->ch_endpts[i].sock_pending); |
| 2537 | } |
| 2538 | switch (client->ch_endpts[i].ti) { |
| 2539 | #ifdef NC_ENABLED_SSH |
| 2540 | case NC_TI_LIBSSH: |
| 2541 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2542 | free(client->ch_endpts[i].opts.ssh); |
| 2543 | break; |
| 2544 | #endif |
| 2545 | #ifdef NC_ENABLED_TLS |
| 2546 | case NC_TI_OPENSSL: |
| 2547 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2548 | free(client->ch_endpts[i].opts.tls); |
| 2549 | break; |
| 2550 | #endif |
| 2551 | default: |
| 2552 | ERRINT; |
| 2553 | /* won't get here ...*/ |
| 2554 | break; |
| 2555 | } |
| 2556 | |
| 2557 | /* move last endpoint to the empty space */ |
| 2558 | --client->ch_endpt_count; |
| 2559 | if (i < client->ch_endpt_count) { |
| 2560 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2561 | } else if (!server_opts.ch_client_count) { |
| 2562 | free(server_opts.ch_clients); |
| 2563 | server_opts.ch_clients = NULL; |
| 2564 | } |
| 2565 | |
| 2566 | ret = 0; |
| 2567 | break; |
| 2568 | } |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | return ret; |
| 2573 | } |
| 2574 | |
| 2575 | API int |
| 2576 | nc_server_ch_add_client(const char *name) |
| 2577 | { |
| 2578 | uint16_t i; |
| 2579 | struct nc_ch_client *client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2580 | |
| 2581 | if (!name) { |
| 2582 | ERRARG("name"); |
| 2583 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | /* WRITE LOCK */ |
| 2587 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2588 | |
| 2589 | /* check name uniqueness */ |
| 2590 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2591 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2592 | ERR("Call Home client \"%s\" already exists.", name); |
| 2593 | /* WRITE UNLOCK */ |
| 2594 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2595 | return -1; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | ++server_opts.ch_client_count; |
| 2600 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2601 | if (!server_opts.ch_clients) { |
| 2602 | ERRMEM; |
| 2603 | /* WRITE UNLOCK */ |
| 2604 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2605 | return -1; |
| 2606 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2607 | client = &server_opts.ch_clients[server_opts.ch_client_count - 1]; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2608 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2609 | lydict_insert(server_opts.ctx, name, 0, &client->name); |
Michal Vasko | 7f1fa3c | 2020-09-08 16:30:41 +0200 | [diff] [blame] | 2610 | client->id = ATOMIC_INC(server_opts.new_client_id); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2611 | client->ch_endpts = NULL; |
| 2612 | client->ch_endpt_count = 0; |
| 2613 | client->conn_type = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2614 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2615 | /* set CH default options */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2616 | client->start_with = NC_CH_FIRST_LISTED; |
| 2617 | client->max_attempts = 3; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2618 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2619 | pthread_mutex_init(&client->lock, NULL); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2620 | |
| 2621 | /* WRITE UNLOCK */ |
| 2622 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2623 | |
| 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2628 | nc_server_ch_del_client(const char *name) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2629 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2630 | uint16_t i; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2631 | int ret = -1; |
| 2632 | |
| 2633 | /* WRITE LOCK */ |
| 2634 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2635 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2636 | if (!name) { |
| 2637 | /* remove all CH clients with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2638 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2639 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2640 | |
| 2641 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2642 | _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2643 | |
| 2644 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2645 | ret = 0; |
| 2646 | } |
| 2647 | free(server_opts.ch_clients); |
| 2648 | server_opts.ch_clients = NULL; |
| 2649 | |
| 2650 | server_opts.ch_client_count = 0; |
| 2651 | |
| 2652 | } else { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2653 | /* remove one client with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2654 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2655 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2656 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2657 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2658 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2659 | _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2660 | |
| 2661 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2662 | |
| 2663 | /* move last client and endpoint(s) to the empty space */ |
| 2664 | --server_opts.ch_client_count; |
| 2665 | if (i < server_opts.ch_client_count) { |
| 2666 | memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count], |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2667 | sizeof *server_opts.ch_clients); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2668 | } else if (!server_opts.ch_client_count) { |
| 2669 | free(server_opts.ch_clients); |
| 2670 | server_opts.ch_clients = NULL; |
| 2671 | } |
| 2672 | |
| 2673 | ret = 0; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2674 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | /* WRITE UNLOCK */ |
| 2680 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2681 | |
| 2682 | return ret; |
| 2683 | } |
| 2684 | |
| 2685 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2686 | nc_server_ch_is_client(const char *name) |
| 2687 | { |
| 2688 | uint16_t i; |
| 2689 | int found = 0; |
| 2690 | |
| 2691 | if (!name) { |
| 2692 | return found; |
| 2693 | } |
| 2694 | |
| 2695 | /* READ LOCK */ |
| 2696 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2697 | |
| 2698 | /* check name uniqueness */ |
| 2699 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2700 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2701 | found = 1; |
| 2702 | break; |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | /* UNLOCK */ |
| 2707 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2708 | |
| 2709 | return found; |
| 2710 | } |
| 2711 | |
| 2712 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2713 | nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2714 | { |
| 2715 | uint16_t i; |
| 2716 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2717 | struct nc_ch_endpt *endpt; |
| 2718 | int ret = -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2719 | |
| 2720 | if (!client_name) { |
| 2721 | ERRARG("client_name"); |
| 2722 | return -1; |
| 2723 | } else if (!endpt_name) { |
| 2724 | ERRARG("endpt_name"); |
| 2725 | return -1; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2726 | } else if (!ti) { |
| 2727 | ERRARG("ti"); |
| 2728 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2732 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2733 | if (!client) { |
| 2734 | return -1; |
| 2735 | } |
| 2736 | |
| 2737 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2738 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2739 | ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2740 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | ++client->ch_endpt_count; |
| 2745 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2746 | if (!client->ch_endpts) { |
| 2747 | ERRMEM; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2748 | goto cleanup; |
| 2749 | } |
| 2750 | endpt = &client->ch_endpts[client->ch_endpt_count - 1]; |
| 2751 | |
| 2752 | memset(endpt, 0, sizeof *client->ch_endpts); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2753 | lydict_insert(server_opts.ctx, endpt_name, 0, &endpt->name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2754 | endpt->ti = ti; |
| 2755 | endpt->sock_pending = -1; |
| 2756 | endpt->ka.idle_time = 1; |
| 2757 | endpt->ka.max_probes = 10; |
| 2758 | endpt->ka.probe_interval = 5; |
| 2759 | |
| 2760 | switch (ti) { |
| 2761 | #ifdef NC_ENABLED_SSH |
| 2762 | case NC_TI_LIBSSH: |
| 2763 | endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2764 | if (!endpt->opts.ssh) { |
| 2765 | ERRMEM; |
| 2766 | goto cleanup; |
| 2767 | } |
| 2768 | endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 2769 | endpt->opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2770 | endpt->opts.ssh->auth_timeout = 30; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2771 | break; |
| 2772 | #endif |
| 2773 | #ifdef NC_ENABLED_TLS |
| 2774 | case NC_TI_OPENSSL: |
| 2775 | endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2776 | if (!endpt->opts.tls) { |
| 2777 | ERRMEM; |
| 2778 | goto cleanup; |
| 2779 | } |
| 2780 | break; |
| 2781 | #endif |
| 2782 | default: |
| 2783 | ERRINT; |
| 2784 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2787 | /* success */ |
| 2788 | ret = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2789 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2790 | cleanup: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2791 | /* UNLOCK */ |
| 2792 | nc_server_ch_client_unlock(client); |
| 2793 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2794 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2798 | nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2799 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2800 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2801 | struct nc_ch_client *client; |
| 2802 | |
| 2803 | if (!client_name) { |
| 2804 | ERRARG("client_name"); |
| 2805 | return -1; |
| 2806 | } |
| 2807 | |
| 2808 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2809 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2810 | if (!client) { |
| 2811 | return -1; |
| 2812 | } |
| 2813 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2814 | ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2815 | |
| 2816 | /* UNLOCK */ |
| 2817 | nc_server_ch_client_unlock(client); |
| 2818 | |
| 2819 | return ret; |
| 2820 | } |
| 2821 | |
| 2822 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2823 | nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name) |
| 2824 | { |
| 2825 | uint16_t i; |
| 2826 | struct nc_ch_client *client = NULL; |
| 2827 | int found = 0; |
| 2828 | |
| 2829 | if (!client_name || !endpt_name) { |
| 2830 | return found; |
| 2831 | } |
| 2832 | |
| 2833 | /* READ LOCK */ |
| 2834 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2835 | |
| 2836 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2837 | if (!strcmp(server_opts.ch_clients[i].name, client_name)) { |
| 2838 | client = &server_opts.ch_clients[i]; |
| 2839 | break; |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | if (!client) { |
| 2844 | goto cleanup; |
| 2845 | } |
| 2846 | |
| 2847 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2848 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2849 | found = 1; |
| 2850 | break; |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | cleanup: |
| 2855 | /* UNLOCK */ |
| 2856 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2857 | return found; |
| 2858 | } |
| 2859 | |
| 2860 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2861 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 2862 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2863 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2864 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2865 | |
| 2866 | if (!client_name) { |
| 2867 | ERRARG("client_name"); |
| 2868 | return -1; |
| 2869 | } else if (!endpt_name) { |
| 2870 | ERRARG("endpt_name"); |
| 2871 | return -1; |
| 2872 | } else if (!address) { |
| 2873 | ERRARG("address"); |
| 2874 | return -1; |
| 2875 | } |
| 2876 | |
| 2877 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2878 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2879 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2880 | return -1; |
| 2881 | } |
| 2882 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2883 | lydict_remove(server_opts.ctx, endpt->address); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2884 | lydict_insert(server_opts.ctx, address, 0, &endpt->address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2885 | |
| 2886 | /* UNLOCK */ |
| 2887 | nc_server_ch_client_unlock(client); |
| 2888 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2889 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | API int |
| 2893 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 2894 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2895 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2896 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2897 | |
| 2898 | if (!client_name) { |
| 2899 | ERRARG("client_name"); |
| 2900 | return -1; |
| 2901 | } else if (!endpt_name) { |
| 2902 | ERRARG("endpt_name"); |
| 2903 | return -1; |
| 2904 | } else if (!port) { |
| 2905 | ERRARG("port"); |
| 2906 | return -1; |
| 2907 | } |
| 2908 | |
| 2909 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2910 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2911 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2912 | return -1; |
| 2913 | } |
| 2914 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2915 | endpt->port = port; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2916 | |
| 2917 | /* UNLOCK */ |
| 2918 | nc_server_ch_client_unlock(client); |
| 2919 | |
ravsz | 5c5a442 | 2020-03-31 15:53:21 +0200 | [diff] [blame] | 2920 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2924 | nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable) |
| 2925 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2926 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2927 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2928 | |
| 2929 | if (!client_name) { |
| 2930 | ERRARG("client_name"); |
| 2931 | return -1; |
| 2932 | } else if (!endpt_name) { |
| 2933 | ERRARG("endpt_name"); |
| 2934 | return -1; |
| 2935 | } |
| 2936 | |
| 2937 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2938 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2939 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2940 | return -1; |
| 2941 | } |
| 2942 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2943 | endpt->ka.enabled = (enable ? 1 : 0); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* UNLOCK */ |
| 2946 | nc_server_ch_client_unlock(client); |
| 2947 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 2948 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | API int |
| 2952 | nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes, |
| 2953 | int probe_interval) |
| 2954 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2955 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2956 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2957 | |
| 2958 | if (!client_name) { |
| 2959 | ERRARG("client_name"); |
| 2960 | return -1; |
| 2961 | } else if (!endpt_name) { |
| 2962 | ERRARG("endpt_name"); |
| 2963 | return -1; |
| 2964 | } |
| 2965 | |
| 2966 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2967 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2968 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2969 | return -1; |
| 2970 | } |
| 2971 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2972 | if (idle_time > -1) { |
| 2973 | endpt->ka.idle_time = idle_time; |
| 2974 | } |
| 2975 | if (max_probes > -1) { |
| 2976 | endpt->ka.max_probes = max_probes; |
| 2977 | } |
| 2978 | if (probe_interval > -1) { |
| 2979 | endpt->ka.probe_interval = probe_interval; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | /* UNLOCK */ |
| 2983 | nc_server_ch_client_unlock(client); |
| 2984 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 2985 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2989 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 2990 | { |
| 2991 | struct nc_ch_client *client; |
| 2992 | |
| 2993 | if (!client_name) { |
| 2994 | ERRARG("client_name"); |
| 2995 | return -1; |
| 2996 | } else if (!conn_type) { |
| 2997 | ERRARG("conn_type"); |
| 2998 | return -1; |
| 2999 | } |
| 3000 | |
| 3001 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3002 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3003 | if (!client) { |
| 3004 | return -1; |
| 3005 | } |
| 3006 | |
| 3007 | if (client->conn_type != conn_type) { |
| 3008 | client->conn_type = conn_type; |
| 3009 | |
| 3010 | /* set default options */ |
| 3011 | switch (conn_type) { |
| 3012 | case NC_CH_PERSIST: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3013 | /* no options */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3014 | break; |
| 3015 | case NC_CH_PERIOD: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3016 | client->conn.period.period = 60; |
| 3017 | client->conn.period.anchor_time = 0; |
| 3018 | client->conn.period.idle_timeout = 120; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3019 | break; |
| 3020 | default: |
| 3021 | ERRINT; |
| 3022 | break; |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | /* UNLOCK */ |
| 3027 | nc_server_ch_client_unlock(client); |
| 3028 | |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
| 3032 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3033 | nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period) |
| 3034 | { |
| 3035 | struct nc_ch_client *client; |
| 3036 | |
| 3037 | if (!client_name) { |
| 3038 | ERRARG("client_name"); |
| 3039 | return -1; |
| 3040 | } else if (!period) { |
| 3041 | ERRARG("period"); |
| 3042 | return -1; |
| 3043 | } |
| 3044 | |
| 3045 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3046 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3047 | if (!client) { |
| 3048 | return -1; |
| 3049 | } |
| 3050 | |
| 3051 | if (client->conn_type != NC_CH_PERIOD) { |
ravsz | 20789e1 | 2020-03-31 14:43:05 +0200 | [diff] [blame] | 3052 | ERR("Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3053 | /* UNLOCK */ |
| 3054 | nc_server_ch_client_unlock(client); |
| 3055 | return -1; |
| 3056 | } |
| 3057 | |
| 3058 | client->conn.period.period = period; |
| 3059 | |
| 3060 | /* UNLOCK */ |
| 3061 | nc_server_ch_client_unlock(client); |
| 3062 | |
| 3063 | return 0; |
| 3064 | } |
| 3065 | |
| 3066 | API int |
| 3067 | nc_server_ch_client_periodic_set_anchor_time(const char *client_name, time_t anchor_time) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3068 | { |
| 3069 | struct nc_ch_client *client; |
| 3070 | |
| 3071 | if (!client_name) { |
| 3072 | ERRARG("client_name"); |
| 3073 | return -1; |
| 3074 | } |
| 3075 | |
| 3076 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3077 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3078 | if (!client) { |
| 3079 | return -1; |
| 3080 | } |
| 3081 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3082 | if (client->conn_type != NC_CH_PERIOD) { |
ravsz | 20789e1 | 2020-03-31 14:43:05 +0200 | [diff] [blame] | 3083 | ERR("Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3084 | /* UNLOCK */ |
| 3085 | nc_server_ch_client_unlock(client); |
| 3086 | return -1; |
| 3087 | } |
| 3088 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3089 | client->conn.period.anchor_time = anchor_time; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3090 | |
| 3091 | /* UNLOCK */ |
| 3092 | nc_server_ch_client_unlock(client); |
| 3093 | |
| 3094 | return 0; |
| 3095 | } |
| 3096 | |
| 3097 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3098 | nc_server_ch_client_periodic_set_idle_timeout(const char *client_name, uint16_t idle_timeout) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3099 | { |
| 3100 | struct nc_ch_client *client; |
| 3101 | |
| 3102 | if (!client_name) { |
| 3103 | ERRARG("client_name"); |
| 3104 | return -1; |
| 3105 | } |
| 3106 | |
| 3107 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3108 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3109 | if (!client) { |
| 3110 | return -1; |
| 3111 | } |
| 3112 | |
| 3113 | if (client->conn_type != NC_CH_PERIOD) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 3114 | ERR("Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3115 | /* UNLOCK */ |
| 3116 | nc_server_ch_client_unlock(client); |
| 3117 | return -1; |
| 3118 | } |
| 3119 | |
| 3120 | client->conn.period.idle_timeout = idle_timeout; |
| 3121 | |
| 3122 | /* UNLOCK */ |
| 3123 | nc_server_ch_client_unlock(client); |
| 3124 | |
| 3125 | return 0; |
| 3126 | } |
| 3127 | |
| 3128 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3129 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 3130 | { |
| 3131 | struct nc_ch_client *client; |
| 3132 | |
| 3133 | if (!client_name) { |
| 3134 | ERRARG("client_name"); |
| 3135 | return -1; |
| 3136 | } |
| 3137 | |
| 3138 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3139 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3140 | if (!client) { |
| 3141 | return -1; |
| 3142 | } |
| 3143 | |
| 3144 | client->start_with = start_with; |
| 3145 | |
| 3146 | /* UNLOCK */ |
| 3147 | nc_server_ch_client_unlock(client); |
| 3148 | |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | API int |
| 3153 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 3154 | { |
| 3155 | struct nc_ch_client *client; |
| 3156 | |
| 3157 | if (!client_name) { |
| 3158 | ERRARG("client_name"); |
| 3159 | return -1; |
| 3160 | } else if (!max_attempts) { |
| 3161 | ERRARG("max_attempts"); |
| 3162 | return -1; |
| 3163 | } |
| 3164 | |
| 3165 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3166 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3167 | if (!client) { |
| 3168 | return -1; |
| 3169 | } |
| 3170 | |
| 3171 | client->max_attempts = max_attempts; |
| 3172 | |
| 3173 | /* UNLOCK */ |
| 3174 | nc_server_ch_client_unlock(client); |
| 3175 | |
| 3176 | return 0; |
| 3177 | } |
| 3178 | |
| 3179 | /* client lock is expected to be held */ |
| 3180 | static NC_MSG_TYPE |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3181 | nc_connect_ch_endpt(struct nc_ch_endpt *endpt, struct nc_session **session) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3182 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3183 | NC_MSG_TYPE msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3184 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3185 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3186 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3187 | |
Michal Vasko | 4c612cd | 2021-02-05 08:53:42 +0100 | [diff] [blame] | 3188 | sock = nc_sock_connect(endpt->address, endpt->port, NC_SOCKET_CH_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 3189 | if (sock < 0) { |
Michal Vasko | 0b30e45 | 2021-03-03 10:30:15 +0100 | [diff] [blame] | 3190 | if (endpt->sock_pending > -1) { |
| 3191 | ++endpt->sock_retries; |
| 3192 | if (endpt->sock_retries == NC_SOCKET_CH_RETRIES) { |
| 3193 | ERR("Failed to connect socket %d after %d retries, closing.", endpt->sock_pending, NC_SOCKET_CH_RETRIES); |
| 3194 | close(endpt->sock_pending); |
| 3195 | endpt->sock_pending = -1; |
| 3196 | endpt->sock_retries = 0; |
| 3197 | } |
Michal Vasko | 4c612cd | 2021-02-05 08:53:42 +0100 | [diff] [blame] | 3198 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3199 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3200 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 3201 | /* no need to store the socket as pending any longer */ |
| 3202 | endpt->sock_pending = -1; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3203 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3204 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3205 | if (!(*session)) { |
| 3206 | ERRMEM; |
| 3207 | close(sock); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3208 | free(ip_host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3209 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3210 | } |
| 3211 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3212 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3213 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 3214 | lydict_insert_zc(server_opts.ctx, ip_host, &(*session)->host); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3215 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3216 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3217 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3218 | #ifdef NC_ENABLED_SSH |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3219 | if (endpt->ti == NC_TI_LIBSSH) { |
| 3220 | (*session)->data = endpt->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3221 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3222 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3223 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3224 | if (ret < 0) { |
| 3225 | msgtype = NC_MSG_ERROR; |
| 3226 | goto fail; |
| 3227 | } else if (!ret) { |
| 3228 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3229 | goto fail; |
| 3230 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3231 | } else |
| 3232 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3233 | #ifdef NC_ENABLED_TLS |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3234 | if (endpt->ti == NC_TI_OPENSSL) { |
| 3235 | (*session)->data = endpt->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3236 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3237 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3238 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3239 | if (ret < 0) { |
| 3240 | msgtype = NC_MSG_ERROR; |
| 3241 | goto fail; |
| 3242 | } else if (!ret) { |
| 3243 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3244 | goto fail; |
| 3245 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3246 | } else |
| 3247 | #endif |
| 3248 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3249 | ERRINT; |
| 3250 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3251 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3252 | goto fail; |
| 3253 | } |
| 3254 | |
| 3255 | /* assign new SID atomically */ |
Michal Vasko | 7f1fa3c | 2020-09-08 16:30:41 +0200 | [diff] [blame] | 3256 | (*session)->id = ATOMIC_INC(server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3257 | |
| 3258 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3259 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3260 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3261 | goto fail; |
| 3262 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3263 | |
| 3264 | nc_gettimespec_mono(&ts_cur); |
| 3265 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 3266 | nc_gettimespec_real(&ts_cur); |
| 3267 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3268 | (*session)->status = NC_STATUS_RUNNING; |
| 3269 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3270 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3271 | |
| 3272 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 3273 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3274 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3275 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3276 | } |
| 3277 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3278 | struct nc_ch_client_thread_arg { |
| 3279 | char *client_name; |
| 3280 | void (*session_clb)(const char *client_name, struct nc_session *new_session); |
| 3281 | }; |
| 3282 | |
| 3283 | static struct nc_ch_client * |
| 3284 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 3285 | { |
| 3286 | struct nc_ch_client *client; |
| 3287 | |
| 3288 | while (1) { |
| 3289 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3290 | nc_server_ch_client_lock(name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3291 | if (!client) { |
| 3292 | return NULL; |
| 3293 | } |
| 3294 | if (client->ch_endpt_count) { |
| 3295 | return client; |
| 3296 | } |
| 3297 | /* no endpoints defined yet */ |
| 3298 | |
| 3299 | /* UNLOCK */ |
| 3300 | nc_server_ch_client_unlock(client); |
| 3301 | |
| 3302 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 3303 | } |
| 3304 | |
| 3305 | return NULL; |
| 3306 | } |
| 3307 | |
| 3308 | static int |
| 3309 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 3310 | { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3311 | int ret = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3312 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3313 | struct timespec ts; |
| 3314 | struct nc_ch_client *client; |
| 3315 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3316 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3317 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3318 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3319 | session->flags |= NC_SESSION_CALLHOME; |
| 3320 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3321 | /* give the session to the user */ |
| 3322 | data->session_clb(data->client_name, session); |
| 3323 | |
| 3324 | do { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 3325 | nc_gettimespec_real(&ts); |
Michal Vasko | e39ae6b | 2017-03-14 09:03:46 +0100 | [diff] [blame] | 3326 | nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3327 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3328 | /* CH COND WAIT */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3329 | r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3330 | if (!r) { |
| 3331 | /* we were woken up, something probably happened */ |
| 3332 | if (session->status != NC_STATUS_RUNNING) { |
| 3333 | break; |
| 3334 | } |
| 3335 | } else if (r != ETIMEDOUT) { |
| 3336 | ERR("Pthread condition timedwait failed (%s).", strerror(r)); |
| 3337 | ret = -1; |
| 3338 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 3339 | } |
| 3340 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3341 | /* check whether the client was not removed */ |
| 3342 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3343 | nc_server_ch_client_lock(data->client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3344 | if (!client) { |
| 3345 | /* client was removed, finish thread */ |
| 3346 | VRB("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] | 3347 | data->client_name); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3348 | ret = 1; |
| 3349 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3352 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3353 | idle_timeout = client->conn.period.idle_timeout; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3354 | } else { |
| 3355 | idle_timeout = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3358 | nc_gettimespec_mono(&ts); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3359 | if (!session->opts.server.ntf_status && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) { |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3360 | VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id); |
| 3361 | session->status = NC_STATUS_INVALID; |
| 3362 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 3363 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3364 | |
| 3365 | /* UNLOCK */ |
| 3366 | nc_server_ch_client_unlock(client); |
| 3367 | |
| 3368 | } while (session->status == NC_STATUS_RUNNING); |
| 3369 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3370 | /* signal to nc_session_free() that CH registered this session not being valid anymore */ |
| 3371 | session->flags &= ~NC_SESSION_CALLHOME; |
| 3372 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3373 | /* CH UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3374 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3375 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3376 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | static void * |
| 3380 | nc_ch_client_thread(void *arg) |
| 3381 | { |
| 3382 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 3383 | NC_MSG_TYPE msgtype; |
| 3384 | uint8_t cur_attempts = 0; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3385 | uint16_t next_endpt_index; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3386 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3387 | struct nc_ch_endpt *cur_endpt; |
| 3388 | struct nc_session *session; |
| 3389 | struct nc_ch_client *client; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3390 | uint32_t client_id; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3391 | time_t reconnect_in; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3392 | |
| 3393 | /* LOCK */ |
| 3394 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3395 | if (!client) { |
| 3396 | goto cleanup; |
| 3397 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3398 | client_id = client->id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3399 | |
| 3400 | cur_endpt = &client->ch_endpts[0]; |
| 3401 | cur_endpt_name = strdup(cur_endpt->name); |
| 3402 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3403 | VRB("Call Home client \"%s\" connecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3404 | while (1) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3405 | msgtype = nc_connect_ch_endpt(cur_endpt, &session); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3406 | |
| 3407 | if (msgtype == NC_MSG_HELLO) { |
| 3408 | /* UNLOCK */ |
| 3409 | nc_server_ch_client_unlock(client); |
| 3410 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3411 | VRB("Call Home client \"%s\" session %u established.", data->client_name, session->id); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3412 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 3413 | goto cleanup; |
| 3414 | } |
Michal Vasko | 0a047f0 | 2021-03-03 10:30:52 +0100 | [diff] [blame] | 3415 | VRB("Call Home client \"%s\" session terminated.", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3416 | |
| 3417 | /* LOCK */ |
| 3418 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3419 | if (!client) { |
| 3420 | goto cleanup; |
| 3421 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3422 | if (client->id != client_id) { |
| 3423 | nc_server_ch_client_unlock(client); |
| 3424 | goto cleanup; |
| 3425 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3426 | |
| 3427 | /* session changed status -> it was disconnected for whatever reason, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3428 | * persistent connection immediately tries to reconnect, periodic connects at specific times */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3429 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3430 | /* UNLOCK */ |
| 3431 | nc_server_ch_client_unlock(client); |
| 3432 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3433 | /* sleep until we should reconnect TODO wake up sometimes to check for new notifications */ |
| 3434 | reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60); |
Michal Vasko | 0a047f0 | 2021-03-03 10:30:52 +0100 | [diff] [blame] | 3435 | VRB("Call Home client \"%s\" reconnecting in %d seconds.", data->client_name, reconnect_in); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3436 | sleep(reconnect_in); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3437 | |
| 3438 | /* LOCK */ |
| 3439 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3440 | if (!client) { |
| 3441 | goto cleanup; |
| 3442 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3443 | if (client->id != client_id) { |
| 3444 | nc_server_ch_client_unlock(client); |
| 3445 | goto cleanup; |
| 3446 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /* set next endpoint to try */ |
| 3450 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3451 | next_endpt_index = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3452 | } else if (client->start_with == NC_CH_LAST_CONNECTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3453 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 3454 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3455 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 3456 | break; |
| 3457 | } |
| 3458 | } |
| 3459 | if (next_endpt_index >= client->ch_endpt_count) { |
| 3460 | /* endpoint was removed, start with the first one */ |
| 3461 | next_endpt_index = 0; |
| 3462 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3463 | } else { |
| 3464 | /* just get a random index */ |
| 3465 | next_endpt_index = rand() % client->ch_endpt_count; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3468 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3469 | /* UNLOCK */ |
| 3470 | nc_server_ch_client_unlock(client); |
| 3471 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3472 | /* session was not created */ |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3473 | usleep(NC_CH_ENDPT_FAIL_WAIT * 1000); |
| 3474 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3475 | /* LOCK */ |
| 3476 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3477 | if (!client) { |
| 3478 | goto cleanup; |
| 3479 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3480 | if (client->id != client_id) { |
| 3481 | nc_server_ch_client_unlock(client); |
| 3482 | goto cleanup; |
| 3483 | } |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3484 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3485 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3486 | |
| 3487 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3488 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3489 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3490 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3491 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3492 | } |
| 3493 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3494 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3495 | /* endpoint was removed, start with the first one */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3496 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3497 | cur_attempts = 0; |
| 3498 | } else if (cur_attempts == client->max_attempts) { |
| 3499 | /* we have tried to connect to this endpoint enough times */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3500 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3501 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3502 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3503 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3504 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3505 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | cur_attempts = 0; |
| 3509 | } /* else we keep the current one */ |
| 3510 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3511 | |
| 3512 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3513 | free(cur_endpt_name); |
| 3514 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | cleanup: |
| 3518 | VRB("Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3519 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3520 | free(data->client_name); |
| 3521 | free(data); |
| 3522 | return NULL; |
| 3523 | } |
| 3524 | |
| 3525 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3526 | nc_connect_ch_client_dispatch(const char *client_name, void (*session_clb)(const char *client_name, |
| 3527 | struct nc_session *new_session)) |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3528 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3529 | int ret; |
| 3530 | pthread_t tid; |
| 3531 | struct nc_ch_client_thread_arg *arg; |
| 3532 | |
| 3533 | if (!client_name) { |
| 3534 | ERRARG("client_name"); |
| 3535 | return -1; |
| 3536 | } else if (!session_clb) { |
| 3537 | ERRARG("session_clb"); |
| 3538 | return -1; |
| 3539 | } |
| 3540 | |
| 3541 | arg = malloc(sizeof *arg); |
| 3542 | if (!arg) { |
| 3543 | ERRMEM; |
| 3544 | return -1; |
| 3545 | } |
| 3546 | arg->client_name = strdup(client_name); |
| 3547 | if (!arg->client_name) { |
| 3548 | ERRMEM; |
| 3549 | free(arg); |
| 3550 | return -1; |
| 3551 | } |
| 3552 | arg->session_clb = session_clb; |
| 3553 | |
| 3554 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 3555 | if (ret) { |
| 3556 | ERR("Creating a new thread failed (%s).", strerror(ret)); |
| 3557 | free(arg->client_name); |
| 3558 | free(arg); |
| 3559 | return -1; |
| 3560 | } |
| 3561 | /* the thread now manages arg */ |
| 3562 | |
| 3563 | pthread_detach(tid); |
| 3564 | |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3568 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3569 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3570 | API time_t |
| 3571 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3572 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3573 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3574 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3575 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3576 | } |
| 3577 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3578 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3579 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3580 | |
| 3581 | API void |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3582 | nc_session_inc_notif_status(struct nc_session *session) |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3583 | { |
| 3584 | if (!session || (session->side != NC_SERVER)) { |
| 3585 | ERRARG("session"); |
| 3586 | return; |
| 3587 | } |
| 3588 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3589 | ++session->opts.server.ntf_status; |
| 3590 | } |
| 3591 | |
| 3592 | API void |
| 3593 | nc_session_dec_notif_status(struct nc_session *session) |
| 3594 | { |
| 3595 | if (!session || (session->side != NC_SERVER)) { |
| 3596 | ERRARG("session"); |
| 3597 | return; |
| 3598 | } |
| 3599 | |
| 3600 | if (session->opts.server.ntf_status) { |
| 3601 | --session->opts.server.ntf_status; |
| 3602 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | API int |
| 3606 | nc_session_get_notif_status(const struct nc_session *session) |
| 3607 | { |
| 3608 | if (!session || (session->side != NC_SERVER)) { |
| 3609 | ERRARG("session"); |
| 3610 | return 0; |
| 3611 | } |
| 3612 | |
| 3613 | return session->opts.server.ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3614 | } |
Michal Vasko | 8f43059 | 2019-02-26 08:32:54 +0100 | [diff] [blame] | 3615 | |
| 3616 | API int |
| 3617 | nc_session_is_callhome(const struct nc_session *session) |
| 3618 | { |
| 3619 | if (!session || (session->side != NC_SERVER)) { |
| 3620 | ERRARG("session"); |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
| 3624 | if (session->flags & NC_SESSION_CALLHOME) { |
| 3625 | return 1; |
| 3626 | } |
| 3627 | |
| 3628 | return 0; |
| 3629 | } |