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