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