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 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1719 | static int |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1720 | nc_get_uid(int sock, uid_t *uid) |
| 1721 | { |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1722 | int ret; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1723 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1724 | #ifdef SO_PEERCRED |
| 1725 | struct ucred ucred; |
| 1726 | socklen_t len; |
| 1727 | len = sizeof(ucred); |
| 1728 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len); |
| 1729 | if (!ret) { |
| 1730 | *uid = ucred.uid; |
| 1731 | } |
| 1732 | #else |
| 1733 | ret = getpeereid(sock, uid, NULL); |
| 1734 | #endif |
| 1735 | |
| 1736 | if (ret < 0) { |
| 1737 | ERR("Failed to get credentials from unix socket (%s).", strerror(errno)); |
| 1738 | return -1; |
| 1739 | } |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | static int |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1744 | nc_accept_unix(struct nc_session *session, int sock) |
| 1745 | { |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1746 | #if defined(SO_PEERCRED) || defined(HAVE_GETPEEREID) |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1747 | const struct passwd *pw; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1748 | char *username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1749 | session->ti_type = NC_TI_UNIX; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1750 | uid_t uid; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1751 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1752 | if (nc_get_uid(sock, &uid)) { |
| 1753 | close(sock); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1754 | return -1; |
| 1755 | } |
| 1756 | |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1757 | pw = getpwuid(uid); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1758 | if (pw == NULL) { |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1759 | ERR("Failed to find username for uid=%u (%s).\n", uid, |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1760 | strerror(errno)); |
| 1761 | close(sock); |
| 1762 | return -1; |
| 1763 | } |
| 1764 | |
| 1765 | username = strdup(pw->pw_name); |
| 1766 | if (username == NULL) { |
| 1767 | ERRMEM; |
| 1768 | close(sock); |
| 1769 | return -1; |
| 1770 | } |
| 1771 | session->username = lydict_insert_zc(server_opts.ctx, username); |
| 1772 | |
| 1773 | session->ti.unixsock.sock = sock; |
| 1774 | |
| 1775 | return 1; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 1776 | #else |
| 1777 | return -1; |
| 1778 | #endif |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1781 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1782 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1783 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1784 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1785 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1786 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1787 | if (!name) { |
| 1788 | ERRARG("name"); |
| 1789 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1790 | } |
| 1791 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1792 | /* BIND LOCK */ |
| 1793 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1794 | |
| 1795 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1796 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1797 | |
| 1798 | /* check name uniqueness */ |
| 1799 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1800 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1801 | ERR("Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1802 | ret = -1; |
| 1803 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1807 | ++server_opts.endpt_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1808 | 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] | 1809 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1810 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1811 | ret = -1; |
| 1812 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1813 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1814 | 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] | 1815 | 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] | 1816 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1817 | server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1; |
| 1818 | server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10; |
| 1819 | server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1820 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1821 | 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] | 1822 | if (!server_opts.binds) { |
| 1823 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1824 | ret = -1; |
| 1825 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1826 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1827 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1828 | 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] | 1829 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
| 1830 | |
| 1831 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1832 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1833 | case NC_TI_LIBSSH: |
| 1834 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1835 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 1836 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1837 | ret = -1; |
| 1838 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1839 | } |
| 1840 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
| 1841 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 1842 | 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] | 1843 | 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] | 1844 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1845 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1846 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1847 | case NC_TI_OPENSSL: |
| 1848 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 1849 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 1850 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1851 | ret = -1; |
| 1852 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1853 | } |
| 1854 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1855 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1856 | case NC_TI_UNIX: |
| 1857 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 1858 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) { |
| 1859 | ERRMEM; |
| 1860 | ret = -1; |
| 1861 | goto cleanup; |
| 1862 | } |
| 1863 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1; |
| 1864 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1; |
| 1865 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1; |
| 1866 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1867 | default: |
| 1868 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1869 | ret = -1; |
| 1870 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1871 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1872 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1873 | cleanup: |
| 1874 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1875 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1876 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1877 | /* BIND UNLOCK */ |
| 1878 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1879 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1880 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1881 | } |
| 1882 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1883 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1884 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1885 | { |
| 1886 | uint32_t i; |
| 1887 | int ret = -1; |
| 1888 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1889 | /* BIND LOCK */ |
| 1890 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1891 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1892 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1893 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1894 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1895 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1896 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1897 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1898 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1899 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1900 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1901 | case NC_TI_LIBSSH: |
| 1902 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1903 | free(server_opts.endpts[i].opts.ssh); |
| 1904 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1905 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1906 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1907 | case NC_TI_OPENSSL: |
| 1908 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1909 | free(server_opts.endpts[i].opts.tls); |
| 1910 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1911 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1912 | case NC_TI_UNIX: |
| 1913 | free(server_opts.endpts[i].opts.unixsock); |
| 1914 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1915 | default: |
| 1916 | ERRINT; |
| 1917 | /* won't get here ...*/ |
| 1918 | break; |
| 1919 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1920 | ret = 0; |
| 1921 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1922 | free(server_opts.endpts); |
| 1923 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1924 | |
| 1925 | /* remove all binds */ |
| 1926 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1927 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 1928 | if (server_opts.binds[i].sock > -1) { |
| 1929 | close(server_opts.binds[i].sock); |
| 1930 | } |
| 1931 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1932 | free(server_opts.binds); |
| 1933 | server_opts.binds = NULL; |
| 1934 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1935 | server_opts.endpt_count = 0; |
| 1936 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1937 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1938 | /* 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] | 1939 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1940 | 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] | 1941 | /* remove endpt */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1942 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1943 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1944 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1945 | case NC_TI_LIBSSH: |
| 1946 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1947 | free(server_opts.endpts[i].opts.ssh); |
| 1948 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1949 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1950 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1951 | case NC_TI_OPENSSL: |
| 1952 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1953 | free(server_opts.endpts[i].opts.tls); |
| 1954 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1955 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1956 | case NC_TI_UNIX: |
| 1957 | free(server_opts.endpts[i].opts.unixsock); |
| 1958 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1959 | default: |
| 1960 | ERRINT; |
| 1961 | break; |
| 1962 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1963 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1964 | /* remove bind(s) */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1965 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 1966 | if (server_opts.binds[i].sock > -1) { |
| 1967 | close(server_opts.binds[i].sock); |
| 1968 | } |
| 1969 | |
| 1970 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1971 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 1972 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 1973 | free(server_opts.binds); |
| 1974 | server_opts.binds = NULL; |
| 1975 | free(server_opts.endpts); |
| 1976 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 1977 | } else if (i < server_opts.endpt_count) { |
| 1978 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 1979 | 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] | 1980 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1981 | |
| 1982 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1983 | if (name) { |
| 1984 | break; |
| 1985 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1986 | } |
| 1987 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1990 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1991 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1992 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1993 | /* BIND UNLOCK */ |
| 1994 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1995 | |
| 1996 | return ret; |
| 1997 | } |
| 1998 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1999 | API int |
| 2000 | nc_server_endpt_count(void) |
| 2001 | { |
| 2002 | return server_opts.endpt_count; |
| 2003 | } |
| 2004 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2005 | API int |
| 2006 | nc_server_is_endpt(const char *name) |
| 2007 | { |
| 2008 | uint16_t i; |
| 2009 | int found = 0; |
| 2010 | |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2011 | if (!name) { |
| 2012 | return found; |
| 2013 | } |
| 2014 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2015 | /* ENDPT READ LOCK */ |
| 2016 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2017 | |
| 2018 | /* check name uniqueness */ |
| 2019 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2020 | if (!strcmp(server_opts.endpts[i].name, name)) { |
| 2021 | found = 1; |
| 2022 | break; |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | /* ENDPT UNLOCK */ |
| 2027 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2028 | |
| 2029 | return found; |
| 2030 | } |
| 2031 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2032 | int |
| 2033 | nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port) |
| 2034 | { |
| 2035 | struct nc_endpt *endpt; |
| 2036 | struct nc_bind *bind = NULL; |
| 2037 | uint16_t i; |
| 2038 | int sock = -1, set_addr, ret = 0; |
| 2039 | |
| 2040 | if (!endpt_name) { |
| 2041 | ERRARG("endpt_name"); |
| 2042 | return -1; |
| 2043 | } else if ((!address && !port) || (address && port)) { |
| 2044 | ERRARG("address and port"); |
| 2045 | return -1; |
| 2046 | } |
| 2047 | |
| 2048 | if (address) { |
| 2049 | set_addr = 1; |
| 2050 | } else { |
| 2051 | set_addr = 0; |
| 2052 | } |
| 2053 | |
| 2054 | /* BIND LOCK */ |
| 2055 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2056 | |
| 2057 | /* ENDPT LOCK */ |
| 2058 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2059 | if (!endpt) { |
| 2060 | /* BIND UNLOCK */ |
| 2061 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2062 | return -1; |
| 2063 | } |
| 2064 | |
| 2065 | bind = &server_opts.binds[i]; |
| 2066 | |
| 2067 | if (set_addr) { |
| 2068 | port = bind->port; |
| 2069 | } else { |
| 2070 | address = bind->address; |
| 2071 | } |
| 2072 | |
| 2073 | if (!set_addr && endpt->ti == NC_TI_UNIX) { |
| 2074 | ret = -1; |
| 2075 | goto cleanup; |
| 2076 | } |
| 2077 | |
| 2078 | /* we have all the information we need to create a listening socket */ |
| 2079 | if (address && (port || endpt->ti == NC_TI_UNIX)) { |
| 2080 | /* create new socket, close the old one */ |
| 2081 | if (endpt->ti == NC_TI_UNIX) |
| 2082 | sock = nc_sock_listen_unix(address, endpt->opts.unixsock); |
| 2083 | else |
| 2084 | sock = nc_sock_listen_inet(address, port, &endpt->ka); |
| 2085 | if (sock == -1) { |
| 2086 | ret = -1; |
| 2087 | goto cleanup; |
| 2088 | } |
| 2089 | |
| 2090 | if (bind->sock > -1) { |
| 2091 | close(bind->sock); |
| 2092 | } |
| 2093 | bind->sock = sock; |
| 2094 | } /* else we are just setting address or port */ |
| 2095 | |
| 2096 | if (set_addr) { |
| 2097 | lydict_remove(server_opts.ctx, bind->address); |
| 2098 | bind->address = lydict_insert(server_opts.ctx, address, 0); |
| 2099 | } else { |
| 2100 | bind->port = port; |
| 2101 | } |
| 2102 | |
| 2103 | if (sock > -1) { |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame^] | 2104 | switch (endpt->ti) { |
| 2105 | case NC_TI_UNIX: |
| 2106 | VRB("Listening on %s for UNIX connections.", address); |
| 2107 | break; |
| 2108 | #ifdef NC_ENABLED_SSH |
| 2109 | case NC_TI_LIBSSH: |
| 2110 | VRB("Listening on %s:%u for SSH connections.", address, port); |
| 2111 | break; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2112 | #endif |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame^] | 2113 | #ifdef NC_ENABLED_TLS |
| 2114 | case NC_TI_OPENSSL: |
| 2115 | VRB("Listening on %s:%u for TLS connections.", address, port); |
| 2116 | break; |
| 2117 | #endif |
| 2118 | default: |
| 2119 | ERRINT; |
| 2120 | break; |
| 2121 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | cleanup: |
| 2125 | /* ENDPT UNLOCK */ |
| 2126 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2127 | |
| 2128 | /* BIND UNLOCK */ |
| 2129 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2130 | |
| 2131 | return ret; |
| 2132 | } |
| 2133 | |
| 2134 | API int |
| 2135 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 2136 | { |
| 2137 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 2138 | } |
| 2139 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame^] | 2140 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 2141 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2142 | API int |
| 2143 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 2144 | { |
| 2145 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 2146 | } |
| 2147 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame^] | 2148 | #endif |
| 2149 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2150 | API int |
| 2151 | nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid) |
| 2152 | { |
| 2153 | struct nc_endpt *endpt; |
| 2154 | uint16_t i; |
| 2155 | int ret = 0; |
| 2156 | |
| 2157 | if (!endpt_name) { |
| 2158 | ERRARG("endpt_name"); |
| 2159 | return -1; |
| 2160 | } else if (mode == 0) { |
| 2161 | ERRARG("mode"); |
| 2162 | return -1; |
| 2163 | } |
| 2164 | |
| 2165 | /* ENDPT LOCK */ |
| 2166 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2167 | if (!endpt) |
| 2168 | return -1; |
| 2169 | |
| 2170 | if (endpt->ti != NC_TI_UNIX) { |
| 2171 | ret = -1; |
| 2172 | goto cleanup; |
| 2173 | } |
| 2174 | |
| 2175 | endpt->opts.unixsock->mode = mode; |
| 2176 | endpt->opts.unixsock->uid = uid; |
| 2177 | endpt->opts.unixsock->gid = gid; |
| 2178 | |
| 2179 | cleanup: |
| 2180 | /* ENDPT UNLOCK */ |
| 2181 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2182 | |
| 2183 | return ret; |
| 2184 | } |
| 2185 | |
| 2186 | API int |
| 2187 | nc_server_endpt_enable_keepalives(const char *endpt_name, int enable) |
| 2188 | { |
| 2189 | struct nc_endpt *endpt; |
| 2190 | int ret = 0; |
| 2191 | |
| 2192 | if (!endpt_name) { |
| 2193 | ERRARG("endpt_name"); |
| 2194 | return -1; |
| 2195 | } |
| 2196 | |
| 2197 | /* ENDPT LOCK */ |
| 2198 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2199 | if (!endpt) { |
| 2200 | return -1; |
| 2201 | } |
| 2202 | |
| 2203 | endpt->ka.enabled = (enable ? 1 : 0); |
| 2204 | |
| 2205 | /* ENDPT UNLOCK */ |
| 2206 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2207 | |
| 2208 | return ret; |
| 2209 | } |
| 2210 | |
| 2211 | API int |
| 2212 | nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval) |
| 2213 | { |
| 2214 | struct nc_endpt *endpt; |
| 2215 | int ret = 0; |
| 2216 | |
| 2217 | if (!endpt_name) { |
| 2218 | ERRARG("endpt_name"); |
| 2219 | return -1; |
| 2220 | } |
| 2221 | |
| 2222 | /* ENDPT LOCK */ |
| 2223 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2224 | if (!endpt) { |
| 2225 | return -1; |
| 2226 | } |
| 2227 | |
| 2228 | if (idle_time > -1) { |
| 2229 | endpt->ka.idle_time = idle_time; |
| 2230 | } |
| 2231 | if (max_probes > -1) { |
| 2232 | endpt->ka.max_probes = max_probes; |
| 2233 | } |
| 2234 | if (probe_interval > -1) { |
| 2235 | endpt->ka.probe_interval = probe_interval; |
| 2236 | } |
| 2237 | |
| 2238 | /* ENDPT UNLOCK */ |
| 2239 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2240 | |
| 2241 | return ret; |
| 2242 | } |
| 2243 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2244 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2245 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2246 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2247 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2248 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2249 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2250 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2251 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2252 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2253 | if (!server_opts.ctx) { |
| 2254 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2255 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2256 | } else if (!session) { |
| 2257 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2258 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2261 | /* BIND LOCK */ |
| 2262 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2263 | |
| 2264 | if (!server_opts.endpt_count) { |
Michal Vasko | 863a6e9 | 2016-07-28 14:27:41 +0200 | [diff] [blame] | 2265 | ERR("No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2266 | /* BIND UNLOCK */ |
| 2267 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2268 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2269 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2270 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2271 | 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] | 2272 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2273 | /* BIND UNLOCK */ |
| 2274 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 2275 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 2276 | if (!ret) { |
| 2277 | return NC_MSG_WOULDBLOCK; |
| 2278 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2279 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2280 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2281 | |
| 2282 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 2283 | /* ENDPT READ LOCK */ |
| 2284 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2285 | |
| 2286 | /* BIND UNLOCK */ |
| 2287 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2288 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2289 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2290 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2291 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 2292 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2293 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2294 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2295 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2296 | msgtype = NC_MSG_ERROR; |
| 2297 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2298 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2299 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2300 | (*session)->ctx = server_opts.ctx; |
| 2301 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 2302 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
| 2303 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2304 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2305 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2306 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2307 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 2308 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2309 | ret = nc_accept_ssh_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 |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2319 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2320 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 2321 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2322 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2323 | if (ret < 0) { |
| 2324 | msgtype = NC_MSG_ERROR; |
| 2325 | goto cleanup; |
| 2326 | } else if (!ret) { |
| 2327 | msgtype = NC_MSG_WOULDBLOCK; |
| 2328 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2329 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2330 | } else |
| 2331 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2332 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2333 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2334 | ret = nc_accept_unix(*session, sock); |
| 2335 | if (ret < 0) { |
| 2336 | msgtype = NC_MSG_ERROR; |
| 2337 | goto cleanup; |
| 2338 | } |
| 2339 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2340 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2341 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2342 | msgtype = NC_MSG_ERROR; |
| 2343 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2344 | } |
| 2345 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2346 | (*session)->data = NULL; |
| 2347 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2348 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2349 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2350 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2351 | /* assign new SID atomically */ |
Michal Vasko | 69a3ff6 | 2018-11-09 09:31:48 +0100 | [diff] [blame] | 2352 | (*session)->id = ATOMIC_INC(&server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2353 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2354 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2355 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2356 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2357 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2358 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2359 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2360 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2361 | |
| 2362 | nc_gettimespec_mono(&ts_cur); |
| 2363 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 2364 | nc_gettimespec_real(&ts_cur); |
| 2365 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2366 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2367 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2368 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2369 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2370 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2371 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2372 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2373 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2374 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2375 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2376 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame^] | 2379 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 2380 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2381 | /* client is expected to be locked */ |
| 2382 | static int |
| 2383 | _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] | 2384 | { |
| 2385 | uint16_t i; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2386 | int ret = -1; |
| 2387 | |
| 2388 | if (!endpt_name) { |
| 2389 | /* remove all endpoints */ |
| 2390 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2391 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2392 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2393 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2394 | close(client->ch_endpts[i].sock_pending); |
| 2395 | } |
| 2396 | switch (client->ch_endpts[i].ti) { |
| 2397 | #ifdef NC_ENABLED_SSH |
| 2398 | case NC_TI_LIBSSH: |
| 2399 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2400 | free(client->ch_endpts[i].opts.ssh); |
| 2401 | break; |
| 2402 | #endif |
| 2403 | #ifdef NC_ENABLED_TLS |
| 2404 | case NC_TI_OPENSSL: |
| 2405 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2406 | free(client->ch_endpts[i].opts.tls); |
| 2407 | break; |
| 2408 | #endif |
| 2409 | default: |
| 2410 | ERRINT; |
| 2411 | /* won't get here ...*/ |
| 2412 | break; |
| 2413 | } |
| 2414 | } |
| 2415 | free(client->ch_endpts); |
| 2416 | client->ch_endpts = NULL; |
| 2417 | client->ch_endpt_count = 0; |
| 2418 | |
| 2419 | ret = 0; |
| 2420 | } else { |
| 2421 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2422 | if (!strcmp(client->ch_endpts[i].name, endpt_name) && (!ti || (ti == client->ch_endpts[i].ti))) { |
| 2423 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2424 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2425 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2426 | close(client->ch_endpts[i].sock_pending); |
| 2427 | } |
| 2428 | switch (client->ch_endpts[i].ti) { |
| 2429 | #ifdef NC_ENABLED_SSH |
| 2430 | case NC_TI_LIBSSH: |
| 2431 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2432 | free(client->ch_endpts[i].opts.ssh); |
| 2433 | break; |
| 2434 | #endif |
| 2435 | #ifdef NC_ENABLED_TLS |
| 2436 | case NC_TI_OPENSSL: |
| 2437 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2438 | free(client->ch_endpts[i].opts.tls); |
| 2439 | break; |
| 2440 | #endif |
| 2441 | default: |
| 2442 | ERRINT; |
| 2443 | /* won't get here ...*/ |
| 2444 | break; |
| 2445 | } |
| 2446 | |
| 2447 | /* move last endpoint to the empty space */ |
| 2448 | --client->ch_endpt_count; |
| 2449 | if (i < client->ch_endpt_count) { |
| 2450 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2451 | } else if (!server_opts.ch_client_count) { |
| 2452 | free(server_opts.ch_clients); |
| 2453 | server_opts.ch_clients = NULL; |
| 2454 | } |
| 2455 | |
| 2456 | ret = 0; |
| 2457 | break; |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | return ret; |
| 2463 | } |
| 2464 | |
| 2465 | API int |
| 2466 | nc_server_ch_add_client(const char *name) |
| 2467 | { |
| 2468 | uint16_t i; |
| 2469 | struct nc_ch_client *client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2470 | |
| 2471 | if (!name) { |
| 2472 | ERRARG("name"); |
| 2473 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /* WRITE LOCK */ |
| 2477 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2478 | |
| 2479 | /* check name uniqueness */ |
| 2480 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2481 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2482 | ERR("Call Home client \"%s\" already exists.", name); |
| 2483 | /* WRITE UNLOCK */ |
| 2484 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2485 | return -1; |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | ++server_opts.ch_client_count; |
| 2490 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2491 | if (!server_opts.ch_clients) { |
| 2492 | ERRMEM; |
| 2493 | /* WRITE UNLOCK */ |
| 2494 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2495 | return -1; |
| 2496 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2497 | client = &server_opts.ch_clients[server_opts.ch_client_count - 1]; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2498 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2499 | client->name = lydict_insert(server_opts.ctx, name, 0); |
| 2500 | client->id = ATOMIC_INC(&server_opts.new_client_id); |
| 2501 | client->ch_endpts = NULL; |
| 2502 | client->ch_endpt_count = 0; |
| 2503 | client->conn_type = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2504 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2505 | /* set CH default options */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2506 | client->start_with = NC_CH_FIRST_LISTED; |
| 2507 | client->max_attempts = 3; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2508 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2509 | pthread_mutex_init(&client->lock, NULL); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2510 | |
| 2511 | /* WRITE UNLOCK */ |
| 2512 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2513 | |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
| 2517 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2518 | nc_server_ch_del_client(const char *name) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2519 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2520 | uint16_t i; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2521 | int ret = -1; |
| 2522 | |
| 2523 | /* WRITE LOCK */ |
| 2524 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2525 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2526 | if (!name) { |
| 2527 | /* remove all CH clients with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2528 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2529 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2530 | |
| 2531 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2532 | _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] | 2533 | |
| 2534 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2535 | ret = 0; |
| 2536 | } |
| 2537 | free(server_opts.ch_clients); |
| 2538 | server_opts.ch_clients = NULL; |
| 2539 | |
| 2540 | server_opts.ch_client_count = 0; |
| 2541 | |
| 2542 | } else { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2543 | /* remove one client with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2544 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2545 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2546 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2547 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2548 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2549 | _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] | 2550 | |
| 2551 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2552 | |
| 2553 | /* move last client and endpoint(s) to the empty space */ |
| 2554 | --server_opts.ch_client_count; |
| 2555 | if (i < server_opts.ch_client_count) { |
| 2556 | 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] | 2557 | sizeof *server_opts.ch_clients); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2558 | } else if (!server_opts.ch_client_count) { |
| 2559 | free(server_opts.ch_clients); |
| 2560 | server_opts.ch_clients = NULL; |
| 2561 | } |
| 2562 | |
| 2563 | ret = 0; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2564 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2565 | } |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | /* WRITE UNLOCK */ |
| 2570 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2571 | |
| 2572 | return ret; |
| 2573 | } |
| 2574 | |
| 2575 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2576 | nc_server_ch_is_client(const char *name) |
| 2577 | { |
| 2578 | uint16_t i; |
| 2579 | int found = 0; |
| 2580 | |
| 2581 | if (!name) { |
| 2582 | return found; |
| 2583 | } |
| 2584 | |
| 2585 | /* READ LOCK */ |
| 2586 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2587 | |
| 2588 | /* check name uniqueness */ |
| 2589 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2590 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2591 | found = 1; |
| 2592 | break; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | /* UNLOCK */ |
| 2597 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2598 | |
| 2599 | return found; |
| 2600 | } |
| 2601 | |
| 2602 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2603 | 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] | 2604 | { |
| 2605 | uint16_t i; |
| 2606 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2607 | struct nc_ch_endpt *endpt; |
| 2608 | int ret = -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2609 | |
| 2610 | if (!client_name) { |
| 2611 | ERRARG("client_name"); |
| 2612 | return -1; |
| 2613 | } else if (!endpt_name) { |
| 2614 | ERRARG("endpt_name"); |
| 2615 | return -1; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2616 | } else if (!ti) { |
| 2617 | ERRARG("ti"); |
| 2618 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2622 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2623 | if (!client) { |
| 2624 | return -1; |
| 2625 | } |
| 2626 | |
| 2627 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2628 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2629 | 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] | 2630 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | ++client->ch_endpt_count; |
| 2635 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2636 | if (!client->ch_endpts) { |
| 2637 | ERRMEM; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2638 | goto cleanup; |
| 2639 | } |
| 2640 | endpt = &client->ch_endpts[client->ch_endpt_count - 1]; |
| 2641 | |
| 2642 | memset(endpt, 0, sizeof *client->ch_endpts); |
| 2643 | endpt->name = lydict_insert(server_opts.ctx, endpt_name, 0); |
| 2644 | endpt->ti = ti; |
| 2645 | endpt->sock_pending = -1; |
| 2646 | endpt->ka.idle_time = 1; |
| 2647 | endpt->ka.max_probes = 10; |
| 2648 | endpt->ka.probe_interval = 5; |
| 2649 | |
| 2650 | switch (ti) { |
| 2651 | #ifdef NC_ENABLED_SSH |
| 2652 | case NC_TI_LIBSSH: |
| 2653 | endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2654 | if (!endpt->opts.ssh) { |
| 2655 | ERRMEM; |
| 2656 | goto cleanup; |
| 2657 | } |
| 2658 | endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 2659 | endpt->opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2660 | endpt->opts.ssh->auth_timeout = 30; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2661 | break; |
| 2662 | #endif |
| 2663 | #ifdef NC_ENABLED_TLS |
| 2664 | case NC_TI_OPENSSL: |
| 2665 | endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2666 | if (!endpt->opts.tls) { |
| 2667 | ERRMEM; |
| 2668 | goto cleanup; |
| 2669 | } |
| 2670 | break; |
| 2671 | #endif |
| 2672 | default: |
| 2673 | ERRINT; |
| 2674 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2677 | /* success */ |
| 2678 | ret = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2679 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2680 | cleanup: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2681 | /* UNLOCK */ |
| 2682 | nc_server_ch_client_unlock(client); |
| 2683 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2684 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2688 | 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] | 2689 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2690 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2691 | struct nc_ch_client *client; |
| 2692 | |
| 2693 | if (!client_name) { |
| 2694 | ERRARG("client_name"); |
| 2695 | return -1; |
| 2696 | } |
| 2697 | |
| 2698 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2699 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2700 | if (!client) { |
| 2701 | return -1; |
| 2702 | } |
| 2703 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2704 | ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2705 | |
| 2706 | /* UNLOCK */ |
| 2707 | nc_server_ch_client_unlock(client); |
| 2708 | |
| 2709 | return ret; |
| 2710 | } |
| 2711 | |
| 2712 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2713 | nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name) |
| 2714 | { |
| 2715 | uint16_t i; |
| 2716 | struct nc_ch_client *client = NULL; |
| 2717 | int found = 0; |
| 2718 | |
| 2719 | if (!client_name || !endpt_name) { |
| 2720 | return found; |
| 2721 | } |
| 2722 | |
| 2723 | /* READ LOCK */ |
| 2724 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2725 | |
| 2726 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2727 | if (!strcmp(server_opts.ch_clients[i].name, client_name)) { |
| 2728 | client = &server_opts.ch_clients[i]; |
| 2729 | break; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | if (!client) { |
| 2734 | goto cleanup; |
| 2735 | } |
| 2736 | |
| 2737 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2738 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2739 | found = 1; |
| 2740 | break; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | cleanup: |
| 2745 | /* UNLOCK */ |
| 2746 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2747 | return found; |
| 2748 | } |
| 2749 | |
| 2750 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2751 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 2752 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2753 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2754 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2755 | |
| 2756 | if (!client_name) { |
| 2757 | ERRARG("client_name"); |
| 2758 | return -1; |
| 2759 | } else if (!endpt_name) { |
| 2760 | ERRARG("endpt_name"); |
| 2761 | return -1; |
| 2762 | } else if (!address) { |
| 2763 | ERRARG("address"); |
| 2764 | return -1; |
| 2765 | } |
| 2766 | |
| 2767 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2768 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2769 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2770 | return -1; |
| 2771 | } |
| 2772 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2773 | lydict_remove(server_opts.ctx, endpt->address); |
| 2774 | endpt->address = lydict_insert(server_opts.ctx, address, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2775 | |
| 2776 | /* UNLOCK */ |
| 2777 | nc_server_ch_client_unlock(client); |
| 2778 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2779 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | API int |
| 2783 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 2784 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2785 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2786 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2787 | |
| 2788 | if (!client_name) { |
| 2789 | ERRARG("client_name"); |
| 2790 | return -1; |
| 2791 | } else if (!endpt_name) { |
| 2792 | ERRARG("endpt_name"); |
| 2793 | return -1; |
| 2794 | } else if (!port) { |
| 2795 | ERRARG("port"); |
| 2796 | return -1; |
| 2797 | } |
| 2798 | |
| 2799 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2800 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2801 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2802 | return -1; |
| 2803 | } |
| 2804 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2805 | endpt->port = port; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2806 | |
| 2807 | /* UNLOCK */ |
| 2808 | nc_server_ch_client_unlock(client); |
| 2809 | |
ravsz | 5c5a442 | 2020-03-31 15:53:21 +0200 | [diff] [blame] | 2810 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2814 | nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable) |
| 2815 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2816 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2817 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2818 | |
| 2819 | if (!client_name) { |
| 2820 | ERRARG("client_name"); |
| 2821 | return -1; |
| 2822 | } else if (!endpt_name) { |
| 2823 | ERRARG("endpt_name"); |
| 2824 | return -1; |
| 2825 | } |
| 2826 | |
| 2827 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2828 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2829 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2830 | return -1; |
| 2831 | } |
| 2832 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2833 | endpt->ka.enabled = (enable ? 1 : 0); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2834 | |
| 2835 | /* UNLOCK */ |
| 2836 | nc_server_ch_client_unlock(client); |
| 2837 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 2838 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | API int |
| 2842 | nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes, |
| 2843 | int probe_interval) |
| 2844 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2845 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2846 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2847 | |
| 2848 | if (!client_name) { |
| 2849 | ERRARG("client_name"); |
| 2850 | return -1; |
| 2851 | } else if (!endpt_name) { |
| 2852 | ERRARG("endpt_name"); |
| 2853 | return -1; |
| 2854 | } |
| 2855 | |
| 2856 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2857 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2858 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2859 | return -1; |
| 2860 | } |
| 2861 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2862 | if (idle_time > -1) { |
| 2863 | endpt->ka.idle_time = idle_time; |
| 2864 | } |
| 2865 | if (max_probes > -1) { |
| 2866 | endpt->ka.max_probes = max_probes; |
| 2867 | } |
| 2868 | if (probe_interval > -1) { |
| 2869 | endpt->ka.probe_interval = probe_interval; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | /* UNLOCK */ |
| 2873 | nc_server_ch_client_unlock(client); |
| 2874 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 2875 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2879 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 2880 | { |
| 2881 | struct nc_ch_client *client; |
| 2882 | |
| 2883 | if (!client_name) { |
| 2884 | ERRARG("client_name"); |
| 2885 | return -1; |
| 2886 | } else if (!conn_type) { |
| 2887 | ERRARG("conn_type"); |
| 2888 | return -1; |
| 2889 | } |
| 2890 | |
| 2891 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2892 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2893 | if (!client) { |
| 2894 | return -1; |
| 2895 | } |
| 2896 | |
| 2897 | if (client->conn_type != conn_type) { |
| 2898 | client->conn_type = conn_type; |
| 2899 | |
| 2900 | /* set default options */ |
| 2901 | switch (conn_type) { |
| 2902 | case NC_CH_PERSIST: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2903 | /* no options */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2904 | break; |
| 2905 | case NC_CH_PERIOD: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2906 | client->conn.period.period = 60; |
| 2907 | client->conn.period.anchor_time = 0; |
| 2908 | client->conn.period.idle_timeout = 120; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2909 | break; |
| 2910 | default: |
| 2911 | ERRINT; |
| 2912 | break; |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | /* UNLOCK */ |
| 2917 | nc_server_ch_client_unlock(client); |
| 2918 | |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2923 | nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period) |
| 2924 | { |
| 2925 | struct nc_ch_client *client; |
| 2926 | |
| 2927 | if (!client_name) { |
| 2928 | ERRARG("client_name"); |
| 2929 | return -1; |
| 2930 | } else if (!period) { |
| 2931 | ERRARG("period"); |
| 2932 | return -1; |
| 2933 | } |
| 2934 | |
| 2935 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2936 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2937 | if (!client) { |
| 2938 | return -1; |
| 2939 | } |
| 2940 | |
| 2941 | if (client->conn_type != NC_CH_PERIOD) { |
ravsz | 20789e1 | 2020-03-31 14:43:05 +0200 | [diff] [blame] | 2942 | 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] | 2943 | /* UNLOCK */ |
| 2944 | nc_server_ch_client_unlock(client); |
| 2945 | return -1; |
| 2946 | } |
| 2947 | |
| 2948 | client->conn.period.period = period; |
| 2949 | |
| 2950 | /* UNLOCK */ |
| 2951 | nc_server_ch_client_unlock(client); |
| 2952 | |
| 2953 | return 0; |
| 2954 | } |
| 2955 | |
| 2956 | API int |
| 2957 | 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] | 2958 | { |
| 2959 | struct nc_ch_client *client; |
| 2960 | |
| 2961 | if (!client_name) { |
| 2962 | ERRARG("client_name"); |
| 2963 | return -1; |
| 2964 | } |
| 2965 | |
| 2966 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2967 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2968 | if (!client) { |
| 2969 | return -1; |
| 2970 | } |
| 2971 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2972 | if (client->conn_type != NC_CH_PERIOD) { |
ravsz | 20789e1 | 2020-03-31 14:43:05 +0200 | [diff] [blame] | 2973 | 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] | 2974 | /* UNLOCK */ |
| 2975 | nc_server_ch_client_unlock(client); |
| 2976 | return -1; |
| 2977 | } |
| 2978 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2979 | client->conn.period.anchor_time = anchor_time; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2980 | |
| 2981 | /* UNLOCK */ |
| 2982 | nc_server_ch_client_unlock(client); |
| 2983 | |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2988 | 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] | 2989 | { |
| 2990 | struct nc_ch_client *client; |
| 2991 | |
| 2992 | if (!client_name) { |
| 2993 | ERRARG("client_name"); |
| 2994 | return -1; |
| 2995 | } |
| 2996 | |
| 2997 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2998 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2999 | if (!client) { |
| 3000 | return -1; |
| 3001 | } |
| 3002 | |
| 3003 | if (client->conn_type != NC_CH_PERIOD) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 3004 | 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] | 3005 | /* UNLOCK */ |
| 3006 | nc_server_ch_client_unlock(client); |
| 3007 | return -1; |
| 3008 | } |
| 3009 | |
| 3010 | client->conn.period.idle_timeout = idle_timeout; |
| 3011 | |
| 3012 | /* UNLOCK */ |
| 3013 | nc_server_ch_client_unlock(client); |
| 3014 | |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
| 3018 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3019 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 3020 | { |
| 3021 | struct nc_ch_client *client; |
| 3022 | |
| 3023 | if (!client_name) { |
| 3024 | ERRARG("client_name"); |
| 3025 | return -1; |
| 3026 | } |
| 3027 | |
| 3028 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3029 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3030 | if (!client) { |
| 3031 | return -1; |
| 3032 | } |
| 3033 | |
| 3034 | client->start_with = start_with; |
| 3035 | |
| 3036 | /* UNLOCK */ |
| 3037 | nc_server_ch_client_unlock(client); |
| 3038 | |
| 3039 | return 0; |
| 3040 | } |
| 3041 | |
| 3042 | API int |
| 3043 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 3044 | { |
| 3045 | struct nc_ch_client *client; |
| 3046 | |
| 3047 | if (!client_name) { |
| 3048 | ERRARG("client_name"); |
| 3049 | return -1; |
| 3050 | } else if (!max_attempts) { |
| 3051 | ERRARG("max_attempts"); |
| 3052 | return -1; |
| 3053 | } |
| 3054 | |
| 3055 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3056 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3057 | if (!client) { |
| 3058 | return -1; |
| 3059 | } |
| 3060 | |
| 3061 | client->max_attempts = max_attempts; |
| 3062 | |
| 3063 | /* UNLOCK */ |
| 3064 | nc_server_ch_client_unlock(client); |
| 3065 | |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
| 3069 | /* client lock is expected to be held */ |
| 3070 | static NC_MSG_TYPE |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3071 | 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] | 3072 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3073 | NC_MSG_TYPE msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3074 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3075 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3076 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3077 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3078 | 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] | 3079 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3080 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3081 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 3082 | /* no need to store the socket as pending any longer */ |
| 3083 | endpt->sock_pending = -1; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3084 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3085 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3086 | if (!(*session)) { |
| 3087 | ERRMEM; |
| 3088 | close(sock); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3089 | free(ip_host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3090 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3091 | } |
| 3092 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3093 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3094 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3095 | (*session)->host = lydict_insert_zc(server_opts.ctx, ip_host); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3096 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3097 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3098 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3099 | #ifdef NC_ENABLED_SSH |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3100 | if (endpt->ti == NC_TI_LIBSSH) { |
| 3101 | (*session)->data = endpt->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3102 | ret = nc_accept_ssh_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 |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3114 | #ifdef NC_ENABLED_TLS |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3115 | if (endpt->ti == NC_TI_OPENSSL) { |
| 3116 | (*session)->data = endpt->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3117 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3118 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3119 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3120 | if (ret < 0) { |
| 3121 | msgtype = NC_MSG_ERROR; |
| 3122 | goto fail; |
| 3123 | } else if (!ret) { |
| 3124 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3125 | goto fail; |
| 3126 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3127 | } else |
| 3128 | #endif |
| 3129 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3130 | ERRINT; |
| 3131 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3132 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3133 | goto fail; |
| 3134 | } |
| 3135 | |
| 3136 | /* assign new SID atomically */ |
Michal Vasko | 69a3ff6 | 2018-11-09 09:31:48 +0100 | [diff] [blame] | 3137 | (*session)->id = ATOMIC_INC(&server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3138 | |
| 3139 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3140 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3141 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3142 | goto fail; |
| 3143 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3144 | |
| 3145 | nc_gettimespec_mono(&ts_cur); |
| 3146 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 3147 | nc_gettimespec_real(&ts_cur); |
| 3148 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3149 | (*session)->status = NC_STATUS_RUNNING; |
| 3150 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3151 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3152 | |
| 3153 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 3154 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3155 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3156 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3157 | } |
| 3158 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3159 | struct nc_ch_client_thread_arg { |
| 3160 | char *client_name; |
| 3161 | void (*session_clb)(const char *client_name, struct nc_session *new_session); |
| 3162 | }; |
| 3163 | |
| 3164 | static struct nc_ch_client * |
| 3165 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 3166 | { |
| 3167 | struct nc_ch_client *client; |
| 3168 | |
| 3169 | while (1) { |
| 3170 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3171 | nc_server_ch_client_lock(name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3172 | if (!client) { |
| 3173 | return NULL; |
| 3174 | } |
| 3175 | if (client->ch_endpt_count) { |
| 3176 | return client; |
| 3177 | } |
| 3178 | /* no endpoints defined yet */ |
| 3179 | |
| 3180 | /* UNLOCK */ |
| 3181 | nc_server_ch_client_unlock(client); |
| 3182 | |
| 3183 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 3184 | } |
| 3185 | |
| 3186 | return NULL; |
| 3187 | } |
| 3188 | |
| 3189 | static int |
| 3190 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 3191 | { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3192 | int ret = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3193 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3194 | struct timespec ts; |
| 3195 | struct nc_ch_client *client; |
| 3196 | |
| 3197 | /* session created, initialize condition */ |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3198 | session->opts.server.ch_lock = calloc(1, sizeof *session->opts.server.ch_lock); |
| 3199 | 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] | 3200 | if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) { |
| 3201 | ERRMEM; |
| 3202 | nc_session_free(session, NULL); |
| 3203 | return -1; |
| 3204 | } |
| 3205 | pthread_mutex_init(session->opts.server.ch_lock, NULL); |
| 3206 | pthread_cond_init(session->opts.server.ch_cond, NULL); |
| 3207 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3208 | session->flags |= NC_SESSION_CALLHOME; |
| 3209 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3210 | /* CH LOCK */ |
| 3211 | pthread_mutex_lock(session->opts.server.ch_lock); |
| 3212 | |
| 3213 | /* give the session to the user */ |
| 3214 | data->session_clb(data->client_name, session); |
| 3215 | |
| 3216 | do { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 3217 | nc_gettimespec_real(&ts); |
Michal Vasko | e39ae6b | 2017-03-14 09:03:46 +0100 | [diff] [blame] | 3218 | nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3219 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3220 | r = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts); |
| 3221 | if (!r) { |
| 3222 | /* we were woken up, something probably happened */ |
| 3223 | if (session->status != NC_STATUS_RUNNING) { |
| 3224 | break; |
| 3225 | } |
| 3226 | } else if (r != ETIMEDOUT) { |
| 3227 | ERR("Pthread condition timedwait failed (%s).", strerror(r)); |
| 3228 | ret = -1; |
| 3229 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 3230 | } |
| 3231 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3232 | /* check whether the client was not removed */ |
| 3233 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3234 | nc_server_ch_client_lock(data->client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3235 | if (!client) { |
| 3236 | /* client was removed, finish thread */ |
| 3237 | 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] | 3238 | data->client_name); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3239 | ret = 1; |
| 3240 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3241 | } |
| 3242 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3243 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3244 | idle_timeout = client->conn.period.idle_timeout; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3245 | } else { |
| 3246 | idle_timeout = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3247 | } |
| 3248 | |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3249 | nc_gettimespec_mono(&ts); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3250 | 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] | 3251 | VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id); |
| 3252 | session->status = NC_STATUS_INVALID; |
| 3253 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 3254 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3255 | |
| 3256 | /* UNLOCK */ |
| 3257 | nc_server_ch_client_unlock(client); |
| 3258 | |
| 3259 | } while (session->status == NC_STATUS_RUNNING); |
| 3260 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3261 | /* CH UNLOCK */ |
| 3262 | pthread_mutex_unlock(session->opts.server.ch_lock); |
| 3263 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3264 | if (session->status == NC_STATUS_CLOSING) { |
| 3265 | /* signal to nc_session_free() that we registered session being freed, otherwise it matters not */ |
| 3266 | session->flags &= ~NC_SESSION_CALLHOME; |
| 3267 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3268 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3269 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | static void * |
| 3273 | nc_ch_client_thread(void *arg) |
| 3274 | { |
| 3275 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 3276 | NC_MSG_TYPE msgtype; |
| 3277 | uint8_t cur_attempts = 0; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3278 | uint16_t next_endpt_index; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3279 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3280 | struct nc_ch_endpt *cur_endpt; |
| 3281 | struct nc_session *session; |
| 3282 | struct nc_ch_client *client; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3283 | uint32_t client_id; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3284 | time_t reconnect_in; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3285 | |
| 3286 | /* LOCK */ |
| 3287 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3288 | if (!client) { |
| 3289 | goto cleanup; |
| 3290 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3291 | client_id = client->id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3292 | |
| 3293 | cur_endpt = &client->ch_endpts[0]; |
| 3294 | cur_endpt_name = strdup(cur_endpt->name); |
| 3295 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3296 | VRB("Call Home client \"%s\" connecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3297 | while (1) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3298 | msgtype = nc_connect_ch_endpt(cur_endpt, &session); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3299 | |
| 3300 | if (msgtype == NC_MSG_HELLO) { |
| 3301 | /* UNLOCK */ |
| 3302 | nc_server_ch_client_unlock(client); |
| 3303 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3304 | 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] | 3305 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 3306 | goto cleanup; |
| 3307 | } |
Michal Vasko | 4ec210b | 2020-04-16 09:32:01 +0200 | [diff] [blame] | 3308 | VRB("Call Home client \"%s\" session terminated, reconnecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3309 | |
| 3310 | /* LOCK */ |
| 3311 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3312 | if (!client) { |
| 3313 | goto cleanup; |
| 3314 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3315 | if (client->id != client_id) { |
| 3316 | nc_server_ch_client_unlock(client); |
| 3317 | goto cleanup; |
| 3318 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3319 | |
| 3320 | /* session changed status -> it was disconnected for whatever reason, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3321 | * persistent connection immediately tries to reconnect, periodic connects at specific times */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3322 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3323 | /* UNLOCK */ |
| 3324 | nc_server_ch_client_unlock(client); |
| 3325 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3326 | /* sleep until we should reconnect TODO wake up sometimes to check for new notifications */ |
| 3327 | reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60); |
| 3328 | sleep(reconnect_in); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3329 | |
| 3330 | /* LOCK */ |
| 3331 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3332 | if (!client) { |
| 3333 | goto cleanup; |
| 3334 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3335 | if (client->id != client_id) { |
| 3336 | nc_server_ch_client_unlock(client); |
| 3337 | goto cleanup; |
| 3338 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | /* set next endpoint to try */ |
| 3342 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3343 | next_endpt_index = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3344 | } else if (client->start_with == NC_CH_LAST_CONNECTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3345 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 3346 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3347 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 3348 | break; |
| 3349 | } |
| 3350 | } |
| 3351 | if (next_endpt_index >= client->ch_endpt_count) { |
| 3352 | /* endpoint was removed, start with the first one */ |
| 3353 | next_endpt_index = 0; |
| 3354 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3355 | } else { |
| 3356 | /* just get a random index */ |
| 3357 | next_endpt_index = rand() % client->ch_endpt_count; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3360 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3361 | /* UNLOCK */ |
| 3362 | nc_server_ch_client_unlock(client); |
| 3363 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3364 | /* session was not created */ |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3365 | usleep(NC_CH_ENDPT_FAIL_WAIT * 1000); |
| 3366 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3367 | /* LOCK */ |
| 3368 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3369 | if (!client) { |
| 3370 | goto cleanup; |
| 3371 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3372 | if (client->id != client_id) { |
| 3373 | nc_server_ch_client_unlock(client); |
| 3374 | goto cleanup; |
| 3375 | } |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3376 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3377 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3378 | |
| 3379 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3380 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3381 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3382 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3383 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3386 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3387 | /* endpoint was removed, start with the first one */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3388 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3389 | cur_attempts = 0; |
| 3390 | } else if (cur_attempts == client->max_attempts) { |
| 3391 | /* we have tried to connect to this endpoint enough times */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3392 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3393 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3394 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3395 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3396 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3397 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | cur_attempts = 0; |
| 3401 | } /* else we keep the current one */ |
| 3402 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3403 | |
| 3404 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3405 | free(cur_endpt_name); |
| 3406 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | cleanup: |
| 3410 | VRB("Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3411 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3412 | free(data->client_name); |
| 3413 | free(data); |
| 3414 | return NULL; |
| 3415 | } |
| 3416 | |
| 3417 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3418 | nc_connect_ch_client_dispatch(const char *client_name, void (*session_clb)(const char *client_name, |
| 3419 | struct nc_session *new_session)) |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3420 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3421 | int ret; |
| 3422 | pthread_t tid; |
| 3423 | struct nc_ch_client_thread_arg *arg; |
| 3424 | |
| 3425 | if (!client_name) { |
| 3426 | ERRARG("client_name"); |
| 3427 | return -1; |
| 3428 | } else if (!session_clb) { |
| 3429 | ERRARG("session_clb"); |
| 3430 | return -1; |
| 3431 | } |
| 3432 | |
| 3433 | arg = malloc(sizeof *arg); |
| 3434 | if (!arg) { |
| 3435 | ERRMEM; |
| 3436 | return -1; |
| 3437 | } |
| 3438 | arg->client_name = strdup(client_name); |
| 3439 | if (!arg->client_name) { |
| 3440 | ERRMEM; |
| 3441 | free(arg); |
| 3442 | return -1; |
| 3443 | } |
| 3444 | arg->session_clb = session_clb; |
| 3445 | |
| 3446 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 3447 | if (ret) { |
| 3448 | ERR("Creating a new thread failed (%s).", strerror(ret)); |
| 3449 | free(arg->client_name); |
| 3450 | free(arg); |
| 3451 | return -1; |
| 3452 | } |
| 3453 | /* the thread now manages arg */ |
| 3454 | |
| 3455 | pthread_detach(tid); |
| 3456 | |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3460 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3461 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3462 | API time_t |
| 3463 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3464 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3465 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3466 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3467 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3468 | } |
| 3469 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3470 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3471 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3472 | |
| 3473 | API void |
| 3474 | nc_session_set_notif_status(struct nc_session *session, int notif_status) |
| 3475 | { |
| 3476 | if (!session || (session->side != NC_SERVER)) { |
| 3477 | ERRARG("session"); |
| 3478 | return; |
| 3479 | } |
| 3480 | |
| 3481 | session->opts.server.ntf_status = (notif_status ? 1 : 0); |
| 3482 | } |
| 3483 | |
| 3484 | API int |
| 3485 | nc_session_get_notif_status(const struct nc_session *session) |
| 3486 | { |
| 3487 | if (!session || (session->side != NC_SERVER)) { |
| 3488 | ERRARG("session"); |
| 3489 | return 0; |
| 3490 | } |
| 3491 | |
| 3492 | return session->opts.server.ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3493 | } |
Michal Vasko | 8f43059 | 2019-02-26 08:32:54 +0100 | [diff] [blame] | 3494 | |
| 3495 | API int |
| 3496 | nc_session_is_callhome(const struct nc_session *session) |
| 3497 | { |
| 3498 | if (!session || (session->side != NC_SERVER)) { |
| 3499 | ERRARG("session"); |
| 3500 | return 0; |
| 3501 | } |
| 3502 | |
| 3503 | if (session->flags & NC_SESSION_CALLHOME) { |
| 3504 | return 1; |
| 3505 | } |
| 3506 | |
| 3507 | return 0; |
| 3508 | } |