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