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