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 | { |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1129 | uint8_t q_id; |
| 1130 | uint16_t session_count; |
| 1131 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1132 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1133 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1137 | /* LOCK (just for memory barrier so that we read the current value) */ |
| 1138 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | session_count = ps->session_count; |
| 1143 | |
| 1144 | /* UNLOCK */ |
| 1145 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1146 | |
| 1147 | return session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1150 | /* 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] | 1151 | * returns: NC_PSPOLL_ERROR, |
| 1152 | * NC_PSPOLL_BAD_RPC, |
| 1153 | * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR, |
| 1154 | * NC_PSPOLL_RPC |
| 1155 | */ |
| 1156 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1157 | 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] | 1158 | { |
| 1159 | struct lyxml_elem *xml = NULL; |
| 1160 | NC_MSG_TYPE msgtype; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1161 | struct nc_server_reply *reply = NULL; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1162 | int ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1163 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1164 | if (!session) { |
| 1165 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1166 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1167 | } else if (!rpc) { |
| 1168 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1169 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1170 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1171 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1172 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1175 | msgtype = nc_read_msg_io(session, io_timeout, &xml, 0); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1176 | |
| 1177 | switch (msgtype) { |
| 1178 | case NC_MSG_RPC: |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1179 | *rpc = calloc(1, sizeof **rpc); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1180 | if (!*rpc) { |
| 1181 | ERRMEM; |
| 1182 | goto error; |
| 1183 | } |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1184 | |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1185 | ly_errno = LY_SUCCESS; |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 1186 | (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, |
| 1187 | 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] | 1188 | if (!(*rpc)->tree) { |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1189 | /* parsing RPC failed */ |
Michal Vasko | c997024 | 2018-02-14 16:03:35 +0100 | [diff] [blame] | 1190 | reply = nc_server_reply_err(nc_err_libyang(server_opts.ctx)); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1191 | 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] | 1192 | nc_server_reply_free(reply); |
| 1193 | if (ret == -1) { |
| 1194 | ERR("Session %u: failed to write reply.", session->id); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1195 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1196 | ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC; |
| 1197 | } else { |
| 1198 | ret = NC_PSPOLL_RPC; |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1199 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1200 | (*rpc)->root = xml; |
| 1201 | break; |
| 1202 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1203 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1204 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1205 | goto error; |
| 1206 | case NC_MSG_REPLY: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 1207 | ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1208 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1209 | goto error; |
| 1210 | case NC_MSG_NOTIF: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 1211 | ERR("Session %u: received <notification> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1212 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1213 | goto error; |
| 1214 | default: |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1215 | /* NC_MSG_ERROR, |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1216 | * 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] | 1217 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1218 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1222 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1223 | |
| 1224 | error: |
| 1225 | /* cleanup */ |
| 1226 | lyxml_free(server_opts.ctx, xml); |
| 1227 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1228 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1229 | } |
| 1230 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1231 | API void |
| 1232 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1233 | { |
| 1234 | global_rpc_clb = clb; |
| 1235 | } |
| 1236 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1237 | API NC_MSG_TYPE |
| 1238 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1239 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1240 | NC_MSG_TYPE ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1241 | |
| 1242 | /* check parameters */ |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1243 | if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1244 | ERRARG("session"); |
| 1245 | return NC_MSG_ERROR; |
| 1246 | } else if (!notif || !notif->tree || !notif->eventtime) { |
| 1247 | ERRARG("notif"); |
| 1248 | return NC_MSG_ERROR; |
| 1249 | } |
| 1250 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1251 | /* we do not need RPC lock for this, IO lock will be acquired properly */ |
| 1252 | ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif); |
| 1253 | if (ret == NC_MSG_ERROR) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1254 | ERR("Session %u: failed to write notification.", session->id); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1255 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1256 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1257 | return ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1260 | /* 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] | 1261 | * returns: NC_PSPOLL_ERROR, |
| 1262 | * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR, |
| 1263 | * NC_PSPOLL_REPLY_ERROR, |
| 1264 | * 0 |
| 1265 | */ |
| 1266 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1267 | 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] | 1268 | { |
| 1269 | nc_rpc_clb clb; |
| 1270 | struct nc_server_reply *reply; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1271 | struct lys_node *rpc_act = NULL; |
| 1272 | struct lyd_node *next, *elem; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1273 | int ret = 0; |
| 1274 | NC_MSG_TYPE r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1275 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1276 | if (!rpc) { |
| 1277 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1278 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1281 | if (rpc->tree->schema->nodetype == LYS_RPC) { |
| 1282 | /* RPC */ |
| 1283 | rpc_act = rpc->tree->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1284 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1285 | /* action */ |
| 1286 | LY_TREE_DFS_BEGIN(rpc->tree, next, elem) { |
| 1287 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1288 | rpc_act = elem->schema; |
| 1289 | break; |
| 1290 | } |
| 1291 | LY_TREE_DFS_END(rpc->tree, next, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1292 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1293 | if (!rpc_act) { |
| 1294 | ERRINT; |
| 1295 | return NC_PSPOLL_ERROR; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | if (!rpc_act->priv) { |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1300 | if (!global_rpc_clb) { |
| 1301 | /* no callback, reply with a not-implemented error */ |
| 1302 | reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT)); |
| 1303 | } else { |
| 1304 | reply = global_rpc_clb(rpc->tree, session); |
| 1305 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1306 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1307 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1308 | reply = clb(rpc->tree, session); |
| 1309 | } |
| 1310 | |
| 1311 | if (!reply) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1312 | 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] | 1313 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1314 | 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] | 1315 | if (reply->type == NC_RPL_ERROR) { |
| 1316 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1317 | } |
| 1318 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1319 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1320 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1321 | ERR("Session %u: failed to write reply.", session->id); |
| 1322 | ret |= NC_PSPOLL_ERROR; |
| 1323 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1324 | |
| 1325 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1326 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1327 | session->status = NC_STATUS_INVALID; |
| 1328 | } |
| 1329 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1330 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1333 | /* session must be running and session RPC lock held! |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1334 | * returns: NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR, (msg filled) |
| 1335 | * NC_PSPOLL_ERROR, (msg filled) |
| 1336 | * NC_PSPOLL_TIMEOUT, |
| 1337 | * NC_PSPOLL_RPC (some application data available), |
| 1338 | * NC_PSPOLL_SSH_CHANNEL, |
| 1339 | * NC_PSPOLL_SSH_MSG |
| 1340 | */ |
| 1341 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1342 | 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] | 1343 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1344 | struct pollfd pfd; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 1345 | int r, ret = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1346 | #ifdef NC_ENABLED_SSH |
| 1347 | struct nc_session *new; |
| 1348 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1349 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1350 | /* check timeout first */ |
| 1351 | 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] | 1352 | && (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1353 | sprintf(msg, "session idle timeout elapsed"); |
| 1354 | session->status = NC_STATUS_INVALID; |
| 1355 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1356 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1357 | } |
| 1358 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1359 | r = nc_session_io_lock(session, io_timeout, __func__); |
| 1360 | if (r < 0) { |
| 1361 | sprintf(msg, "session IO lock failed to be acquired"); |
| 1362 | return NC_PSPOLL_ERROR; |
| 1363 | } else if (!r) { |
| 1364 | return NC_PSPOLL_TIMEOUT; |
| 1365 | } |
| 1366 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1367 | switch (session->ti_type) { |
| 1368 | #ifdef NC_ENABLED_SSH |
| 1369 | case NC_TI_LIBSSH: |
| 1370 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1371 | if (r == SSH_EOF) { |
| 1372 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1373 | session->status = NC_STATUS_INVALID; |
| 1374 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1375 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1376 | } else if (r == SSH_ERROR) { |
| 1377 | 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] | 1378 | session->status = NC_STATUS_INVALID; |
| 1379 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1380 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1381 | } else if (!r) { |
| 1382 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1383 | /* new SSH message */ |
| 1384 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1385 | if (session->ti.libssh.next) { |
| 1386 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
| 1387 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel |
| 1388 | && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1389 | /* new NETCONF SSH channel */ |
| 1390 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1391 | break; |
| 1392 | } |
| 1393 | } |
| 1394 | if (new != session) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1395 | break; |
| 1396 | } |
| 1397 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1398 | |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1399 | /* just some SSH message */ |
| 1400 | ret = NC_PSPOLL_SSH_MSG; |
| 1401 | } else { |
| 1402 | ret = NC_PSPOLL_TIMEOUT; |
| 1403 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1404 | } else { |
| 1405 | /* we have some application data */ |
| 1406 | ret = NC_PSPOLL_RPC; |
| 1407 | } |
| 1408 | break; |
| 1409 | #endif |
| 1410 | #ifdef NC_ENABLED_TLS |
| 1411 | case NC_TI_OPENSSL: |
| 1412 | r = SSL_pending(session->ti.tls); |
| 1413 | if (!r) { |
| 1414 | /* no data pending in the SSL buffer, poll fd */ |
| 1415 | pfd.fd = SSL_get_rfd(session->ti.tls); |
| 1416 | if (pfd.fd < 0) { |
| 1417 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1418 | ret = NC_PSPOLL_ERROR; |
| 1419 | break; |
| 1420 | } |
| 1421 | pfd.events = POLLIN; |
| 1422 | pfd.revents = 0; |
| 1423 | r = poll(&pfd, 1, 0); |
| 1424 | |
| 1425 | if ((r < 0) && (errno != EINTR)) { |
| 1426 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1427 | session->status = NC_STATUS_INVALID; |
| 1428 | ret = NC_PSPOLL_ERROR; |
| 1429 | } else if (r > 0) { |
| 1430 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1431 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1432 | session->status = NC_STATUS_INVALID; |
| 1433 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1434 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1435 | } else if (pfd.revents & POLLERR) { |
| 1436 | sprintf(msg, "communication socket error"); |
| 1437 | session->status = NC_STATUS_INVALID; |
| 1438 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1439 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1440 | } else { |
| 1441 | ret = NC_PSPOLL_RPC; |
| 1442 | } |
| 1443 | } else { |
| 1444 | ret = NC_PSPOLL_TIMEOUT; |
| 1445 | } |
| 1446 | } else { |
| 1447 | ret = NC_PSPOLL_RPC; |
| 1448 | } |
| 1449 | break; |
| 1450 | #endif |
| 1451 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1452 | case NC_TI_UNIX: |
| 1453 | 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] | 1454 | pfd.events = POLLIN; |
| 1455 | pfd.revents = 0; |
| 1456 | r = poll(&pfd, 1, 0); |
| 1457 | |
| 1458 | if ((r < 0) && (errno != EINTR)) { |
| 1459 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1460 | session->status = NC_STATUS_INVALID; |
| 1461 | ret = NC_PSPOLL_ERROR; |
| 1462 | } else if (r > 0) { |
| 1463 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1464 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1465 | session->status = NC_STATUS_INVALID; |
| 1466 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1467 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1468 | } else if (pfd.revents & POLLERR) { |
| 1469 | sprintf(msg, "communication socket error"); |
| 1470 | session->status = NC_STATUS_INVALID; |
| 1471 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1472 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1473 | } else { |
| 1474 | ret = NC_PSPOLL_RPC; |
| 1475 | } |
| 1476 | } else { |
| 1477 | ret = NC_PSPOLL_TIMEOUT; |
| 1478 | } |
| 1479 | break; |
| 1480 | case NC_TI_NONE: |
| 1481 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1482 | ret = NC_PSPOLL_ERROR; |
| 1483 | break; |
| 1484 | } |
| 1485 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1486 | nc_session_io_unlock(session, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1487 | return ret; |
| 1488 | } |
| 1489 | |
| 1490 | API int |
| 1491 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1492 | { |
| 1493 | int ret, r; |
| 1494 | uint8_t q_id; |
| 1495 | uint16_t i, j; |
| 1496 | char msg[256]; |
| 1497 | struct timespec ts_timeout, ts_cur; |
| 1498 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1499 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1500 | struct nc_server_rpc *rpc = NULL; |
| 1501 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1502 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1503 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1504 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1505 | } |
| 1506 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1507 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1508 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1509 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1510 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1511 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1512 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1513 | nc_ps_unlock(ps, q_id, __func__); |
| 1514 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1515 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1516 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1517 | /* fill timespecs */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1518 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1519 | if (timeout > -1) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1520 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1521 | nc_addtimespec(&ts_timeout, timeout); |
| 1522 | } |
| 1523 | |
| 1524 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1525 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1526 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1527 | if (ps->last_event_session == ps->session_count - 1) { |
| 1528 | i = j = 0; |
| 1529 | } else { |
| 1530 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1531 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1532 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1533 | cur_ps_session = ps->sessions[i]; |
| 1534 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1535 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1536 | /* SESSION RPC LOCK */ |
| 1537 | r = nc_session_rpc_lock(cur_session, 0, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1538 | if (r == -1) { |
| 1539 | ret = NC_PSPOLL_ERROR; |
| 1540 | } else if (r == 1) { |
| 1541 | /* 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] | 1542 | switch (cur_ps_session->state) { |
| 1543 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1544 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1545 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1546 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1547 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1548 | 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] | 1549 | switch (ret) { |
| 1550 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1551 | ERR("Session %u: %s.", cur_session->id, msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1552 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1553 | break; |
| 1554 | case NC_PSPOLL_ERROR: |
| 1555 | ERR("Session %u: %s.", cur_session->id, msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1556 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1557 | break; |
| 1558 | case NC_PSPOLL_TIMEOUT: |
| 1559 | #ifdef NC_ENABLED_SSH |
| 1560 | case NC_PSPOLL_SSH_CHANNEL: |
| 1561 | case NC_PSPOLL_SSH_MSG: |
| 1562 | #endif |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1563 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1564 | break; |
| 1565 | case NC_PSPOLL_RPC: |
| 1566 | /* let's keep the state busy, we are not done with this session */ |
| 1567 | break; |
| 1568 | } |
| 1569 | } else { |
| 1570 | /* session is not fine, let the caller know */ |
| 1571 | ret = NC_PSPOLL_SESSION_TERM; |
| 1572 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1573 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1574 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1575 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1576 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1577 | break; |
| 1578 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1579 | /* it definitely should not be busy because we have the lock */ |
| 1580 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1581 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1582 | break; |
| 1583 | case NC_PS_STATE_INVALID: |
| 1584 | /* we got it locked, but it will be freed, let it be */ |
| 1585 | ret = NC_PSPOLL_TIMEOUT; |
| 1586 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1587 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1588 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1589 | /* keep RPC lock in this one case */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1590 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1591 | /* SESSION RPC UNLOCK */ |
| 1592 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1593 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1594 | } else { |
| 1595 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1596 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1597 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1598 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1599 | /* something happened */ |
| 1600 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1601 | break; |
| 1602 | } |
| 1603 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1604 | if (i == ps->session_count - 1) { |
| 1605 | i = 0; |
| 1606 | } else { |
| 1607 | ++i; |
| 1608 | } |
| 1609 | } while (i != j); |
| 1610 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1611 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1612 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1613 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1614 | /* update current time */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1615 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1616 | |
| 1617 | if ((timeout > -1) && (nc_difftimespec(&ts_cur, &ts_timeout) < 1)) { |
| 1618 | /* final timeout */ |
| 1619 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1620 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1621 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1622 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1623 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1624 | /* do we want to return the session? */ |
| 1625 | switch (ret) { |
| 1626 | case NC_PSPOLL_RPC: |
| 1627 | case NC_PSPOLL_SESSION_TERM: |
| 1628 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1629 | #ifdef NC_ENABLED_SSH |
| 1630 | case NC_PSPOLL_SSH_CHANNEL: |
| 1631 | case NC_PSPOLL_SSH_MSG: |
| 1632 | #endif |
| 1633 | if (session) { |
| 1634 | *session = cur_session; |
| 1635 | } |
| 1636 | ps->last_event_session = i; |
| 1637 | break; |
| 1638 | default: |
| 1639 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1640 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1641 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1642 | /* PS UNLOCK */ |
| 1643 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1644 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1645 | /* 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] | 1646 | if (ret == NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1647 | ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1648 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1649 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1650 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1651 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1652 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1653 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1654 | } |
| 1655 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1656 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1657 | |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1658 | /* process RPC */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1659 | ret |= nc_server_send_reply_io(cur_session, timeout, rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1660 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1661 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1662 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1663 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1664 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1665 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1666 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1667 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1668 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1669 | } |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1670 | nc_server_rpc_free(rpc, server_opts.ctx); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1671 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1672 | /* SESSION RPC UNLOCK */ |
| 1673 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1674 | } |
| 1675 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1676 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1677 | } |
| 1678 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1679 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1680 | 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] | 1681 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1682 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1683 | uint16_t i; |
| 1684 | struct nc_session *session; |
| 1685 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1686 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1687 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1688 | return; |
| 1689 | } |
| 1690 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1691 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1692 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1693 | return; |
| 1694 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1695 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1696 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1697 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1698 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1699 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1700 | } |
| 1701 | free(ps->sessions); |
| 1702 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1703 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1704 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1705 | } else { |
| 1706 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1707 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1708 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1709 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1710 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1711 | continue; |
| 1712 | } |
| 1713 | |
| 1714 | ++i; |
| 1715 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1716 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1717 | |
| 1718 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1719 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1720 | } |
| 1721 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1722 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1723 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1724 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1725 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1726 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1727 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1728 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1729 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1730 | if (!name) { |
| 1731 | ERRARG("name"); |
| 1732 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1733 | } |
| 1734 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1735 | /* BIND LOCK */ |
| 1736 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1737 | |
| 1738 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1739 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1740 | |
| 1741 | /* check name uniqueness */ |
| 1742 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1743 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1744 | ERR("Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1745 | ret = -1; |
| 1746 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1750 | ++server_opts.endpt_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1751 | 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] | 1752 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1753 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1754 | ret = -1; |
| 1755 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1756 | } |
| 1757 | 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] | 1758 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1759 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1760 | 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] | 1761 | if (!server_opts.binds) { |
| 1762 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1763 | ret = -1; |
| 1764 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1765 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1766 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1767 | server_opts.binds[server_opts.endpt_count - 1].address = NULL; |
| 1768 | server_opts.binds[server_opts.endpt_count - 1].port = 0; |
| 1769 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 1770 | server_opts.binds[server_opts.endpt_count - 1].pollin = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1771 | |
| 1772 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1773 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1774 | case NC_TI_LIBSSH: |
| 1775 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1776 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 1777 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1778 | ret = -1; |
| 1779 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1780 | } |
| 1781 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
| 1782 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 1783 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3; |
| 1784 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10; |
| 1785 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1786 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1787 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1788 | case NC_TI_OPENSSL: |
| 1789 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 1790 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 1791 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1792 | ret = -1; |
| 1793 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1794 | } |
| 1795 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1796 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1797 | case NC_TI_UNIX: |
| 1798 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 1799 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) { |
| 1800 | ERRMEM; |
| 1801 | ret = -1; |
| 1802 | goto cleanup; |
| 1803 | } |
| 1804 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1; |
| 1805 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1; |
| 1806 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1; |
| 1807 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1808 | default: |
| 1809 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1810 | ret = -1; |
| 1811 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1812 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1813 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1814 | cleanup: |
| 1815 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1816 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1817 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1818 | /* BIND UNLOCK */ |
| 1819 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1820 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1821 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1822 | } |
| 1823 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1824 | int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1825 | 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] | 1826 | { |
| 1827 | struct nc_endpt *endpt; |
| 1828 | struct nc_bind *bind = NULL; |
| 1829 | uint16_t i; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1830 | int sock = -1, set_addr, ret = 0; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1831 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1832 | if (!endpt_name) { |
| 1833 | ERRARG("endpt_name"); |
| 1834 | return -1; |
| 1835 | } else if ((!address && !port) || (address && port)) { |
| 1836 | ERRARG("address and port"); |
| 1837 | return -1; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1840 | if (address) { |
| 1841 | set_addr = 1; |
| 1842 | } else { |
| 1843 | set_addr = 0; |
| 1844 | } |
| 1845 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1846 | /* BIND LOCK */ |
| 1847 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1848 | |
| 1849 | /* ENDPT LOCK */ |
| 1850 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1851 | if (!endpt) { |
Michal Vasko | 4e455dd | 2017-03-21 15:33:43 +0100 | [diff] [blame] | 1852 | /* BIND UNLOCK */ |
| 1853 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1854 | return -1; |
| 1855 | } |
| 1856 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1857 | bind = &server_opts.binds[i]; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1858 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1859 | if (set_addr) { |
| 1860 | port = bind->port; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1861 | } else { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1862 | address = bind->address; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1863 | } |
| 1864 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1865 | if (!set_addr && endpt->ti == NC_TI_UNIX) { |
| 1866 | ret = -1; |
| 1867 | goto cleanup; |
| 1868 | } |
| 1869 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1870 | /* we have all the information we need to create a listening socket */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1871 | if (address && (port || endpt->ti == NC_TI_UNIX)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1872 | /* create new socket, close the old one */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1873 | if (endpt->ti == NC_TI_UNIX) |
| 1874 | sock = nc_sock_listen_unix(address, endpt->opts.unixsock); |
| 1875 | else |
| 1876 | sock = nc_sock_listen_inet(address, port); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1877 | if (sock == -1) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1878 | ret = -1; |
| 1879 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | if (bind->sock > -1) { |
| 1883 | close(bind->sock); |
| 1884 | } |
| 1885 | bind->sock = sock; |
| 1886 | } /* else we are just setting address or port */ |
| 1887 | |
| 1888 | if (set_addr) { |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1889 | lydict_remove(server_opts.ctx, bind->address); |
| 1890 | bind->address = lydict_insert(server_opts.ctx, address, 0); |
| 1891 | } else { |
| 1892 | bind->port = port; |
| 1893 | } |
| 1894 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1895 | if (sock > -1) { |
| 1896 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1897 | 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] | 1898 | #elif defined(NC_ENABLED_SSH) |
| 1899 | VRB("Listening on %s:%u for SSH connections.", address, port); |
| 1900 | #else |
| 1901 | VRB("Listening on %s:%u for TLS connections.", address, port); |
| 1902 | #endif |
| 1903 | } |
| 1904 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1905 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1906 | /* ENDPT UNLOCK */ |
| 1907 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1908 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1909 | /* BIND UNLOCK */ |
| 1910 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1911 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1912 | return ret; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1915 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1916 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 1917 | { |
| 1918 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 1919 | } |
| 1920 | |
| 1921 | API int |
| 1922 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 1923 | { |
| 1924 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 1925 | } |
| 1926 | |
| 1927 | API int |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1928 | nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid) |
| 1929 | { |
| 1930 | struct nc_endpt *endpt; |
| 1931 | uint16_t i; |
| 1932 | int ret = 0; |
| 1933 | |
| 1934 | if (!endpt_name) { |
| 1935 | ERRARG("endpt_name"); |
| 1936 | return -1; |
| 1937 | } else if (mode == 0) { |
| 1938 | ERRARG("mode"); |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
| 1942 | /* ENDPT LOCK */ |
| 1943 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 1944 | if (!endpt) |
| 1945 | return -1; |
| 1946 | |
| 1947 | if (endpt->ti != NC_TI_UNIX) { |
| 1948 | ret = -1; |
| 1949 | goto cleanup; |
| 1950 | } |
| 1951 | |
| 1952 | endpt->opts.unixsock->mode = mode; |
| 1953 | endpt->opts.unixsock->uid = uid; |
| 1954 | endpt->opts.unixsock->gid = gid; |
| 1955 | |
| 1956 | cleanup: |
| 1957 | /* ENDPT UNLOCK */ |
| 1958 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 1959 | |
| 1960 | return ret; |
| 1961 | } |
| 1962 | |
| 1963 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1964 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1965 | { |
| 1966 | uint32_t i; |
| 1967 | int ret = -1; |
| 1968 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1969 | /* BIND LOCK */ |
| 1970 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1971 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1972 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1973 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1974 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1975 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1976 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1977 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1978 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1979 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1980 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1981 | case NC_TI_LIBSSH: |
| 1982 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1983 | free(server_opts.endpts[i].opts.ssh); |
| 1984 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1985 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1986 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1987 | case NC_TI_OPENSSL: |
| 1988 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1989 | free(server_opts.endpts[i].opts.tls); |
| 1990 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1991 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1992 | case NC_TI_UNIX: |
| 1993 | free(server_opts.endpts[i].opts.unixsock); |
| 1994 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1995 | default: |
| 1996 | ERRINT; |
| 1997 | /* won't get here ...*/ |
| 1998 | break; |
| 1999 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2000 | ret = 0; |
| 2001 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2002 | free(server_opts.endpts); |
| 2003 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2004 | |
| 2005 | /* remove all binds */ |
| 2006 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2007 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 2008 | if (server_opts.binds[i].sock > -1) { |
| 2009 | close(server_opts.binds[i].sock); |
| 2010 | } |
| 2011 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2012 | free(server_opts.binds); |
| 2013 | server_opts.binds = NULL; |
| 2014 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2015 | server_opts.endpt_count = 0; |
| 2016 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2017 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2018 | /* 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] | 2019 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2020 | 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] | 2021 | /* remove endpt */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2022 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2023 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2024 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2025 | case NC_TI_LIBSSH: |
| 2026 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2027 | free(server_opts.endpts[i].opts.ssh); |
| 2028 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2029 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2030 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2031 | case NC_TI_OPENSSL: |
| 2032 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2033 | free(server_opts.endpts[i].opts.tls); |
| 2034 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2035 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2036 | case NC_TI_UNIX: |
| 2037 | free(server_opts.endpts[i].opts.unixsock); |
| 2038 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2039 | default: |
| 2040 | ERRINT; |
| 2041 | break; |
| 2042 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2043 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2044 | /* remove bind(s) */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2045 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 2046 | if (server_opts.binds[i].sock > -1) { |
| 2047 | close(server_opts.binds[i].sock); |
| 2048 | } |
| 2049 | |
| 2050 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2051 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2052 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2053 | free(server_opts.binds); |
| 2054 | server_opts.binds = NULL; |
| 2055 | free(server_opts.endpts); |
| 2056 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2057 | } else if (i < server_opts.endpt_count) { |
| 2058 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 2059 | 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] | 2060 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2061 | |
| 2062 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2063 | if (name) { |
| 2064 | break; |
| 2065 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2066 | } |
| 2067 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2070 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2071 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2072 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2073 | /* BIND UNLOCK */ |
| 2074 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2075 | |
| 2076 | return ret; |
| 2077 | } |
| 2078 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2079 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2080 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2081 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2082 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2083 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2084 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2085 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2086 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2087 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2088 | if (!server_opts.ctx) { |
| 2089 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2090 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2091 | } else if (!session) { |
| 2092 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2093 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2096 | /* BIND LOCK */ |
| 2097 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2098 | |
| 2099 | if (!server_opts.endpt_count) { |
Michal Vasko | 863a6e9 | 2016-07-28 14:27:41 +0200 | [diff] [blame] | 2100 | ERR("No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2101 | /* BIND UNLOCK */ |
| 2102 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2103 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2104 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2105 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2106 | 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] | 2107 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2108 | /* BIND UNLOCK */ |
| 2109 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 2110 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 2111 | if (!ret) { |
| 2112 | return NC_MSG_WOULDBLOCK; |
| 2113 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2114 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2115 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2116 | |
| 2117 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 2118 | /* ENDPT READ LOCK */ |
| 2119 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2120 | |
| 2121 | /* BIND UNLOCK */ |
| 2122 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2123 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2124 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2125 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2126 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 2127 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2128 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2129 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2130 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2131 | msgtype = NC_MSG_ERROR; |
| 2132 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2133 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2134 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2135 | (*session)->ctx = server_opts.ctx; |
| 2136 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 2137 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
| 2138 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2139 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2140 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2141 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2142 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 2143 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2144 | ret = nc_accept_ssh_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 |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2154 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2155 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 2156 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2157 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2158 | if (ret < 0) { |
| 2159 | msgtype = NC_MSG_ERROR; |
| 2160 | goto cleanup; |
| 2161 | } else if (!ret) { |
| 2162 | msgtype = NC_MSG_WOULDBLOCK; |
| 2163 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2164 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2165 | } else |
| 2166 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2167 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2168 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2169 | ret = nc_accept_unix(*session, sock); |
| 2170 | if (ret < 0) { |
| 2171 | msgtype = NC_MSG_ERROR; |
| 2172 | goto cleanup; |
| 2173 | } |
| 2174 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2175 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2176 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2177 | msgtype = NC_MSG_ERROR; |
| 2178 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2181 | (*session)->data = NULL; |
| 2182 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2183 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2184 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2185 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2186 | /* assign new SID atomically */ |
Michal Vasko | 69a3ff6 | 2018-11-09 09:31:48 +0100 | [diff] [blame] | 2187 | (*session)->id = ATOMIC_INC(&server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2188 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2189 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2190 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2191 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2192 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2193 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2194 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2195 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2196 | |
| 2197 | nc_gettimespec_mono(&ts_cur); |
| 2198 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 2199 | nc_gettimespec_real(&ts_cur); |
| 2200 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2201 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2202 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2203 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2204 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2205 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2206 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2207 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2208 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2209 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2210 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2211 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2212 | } |
| 2213 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2214 | API int |
| 2215 | nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti) |
| 2216 | { |
| 2217 | uint16_t i; |
| 2218 | |
| 2219 | if (!name) { |
| 2220 | ERRARG("name"); |
| 2221 | return -1; |
| 2222 | } else if (!ti) { |
| 2223 | ERRARG("ti"); |
| 2224 | return -1; |
| 2225 | } |
| 2226 | |
| 2227 | /* WRITE LOCK */ |
| 2228 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2229 | |
| 2230 | /* check name uniqueness */ |
| 2231 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2232 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2233 | ERR("Call Home client \"%s\" already exists.", name); |
| 2234 | /* WRITE UNLOCK */ |
| 2235 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2236 | return -1; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | ++server_opts.ch_client_count; |
| 2241 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2242 | if (!server_opts.ch_clients) { |
| 2243 | ERRMEM; |
| 2244 | /* WRITE UNLOCK */ |
| 2245 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2246 | return -1; |
| 2247 | } |
| 2248 | 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] | 2249 | 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] | 2250 | server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2251 | server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL; |
| 2252 | 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] | 2253 | |
| 2254 | switch (ti) { |
| 2255 | #ifdef NC_ENABLED_SSH |
| 2256 | case NC_TI_LIBSSH: |
| 2257 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2258 | if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) { |
| 2259 | ERRMEM; |
| 2260 | /* WRITE UNLOCK */ |
| 2261 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2262 | return -1; |
| 2263 | } |
| 2264 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods = |
| 2265 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 2266 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3; |
| 2267 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10; |
| 2268 | break; |
| 2269 | #endif |
| 2270 | #ifdef NC_ENABLED_TLS |
| 2271 | case NC_TI_OPENSSL: |
| 2272 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2273 | if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) { |
| 2274 | ERRMEM; |
| 2275 | /* WRITE UNLOCK */ |
| 2276 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2277 | return -1; |
| 2278 | } |
| 2279 | break; |
| 2280 | #endif |
| 2281 | default: |
| 2282 | ERRINT; |
| 2283 | /* WRITE UNLOCK */ |
| 2284 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2285 | return -1; |
| 2286 | } |
| 2287 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2288 | server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0; |
| 2289 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2290 | /* set CH default options */ |
| 2291 | server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED; |
| 2292 | server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3; |
| 2293 | |
| 2294 | pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL); |
| 2295 | |
| 2296 | /* WRITE UNLOCK */ |
| 2297 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2298 | |
| 2299 | return 0; |
| 2300 | } |
| 2301 | |
| 2302 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2303 | nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2304 | { |
| 2305 | uint16_t i, j; |
| 2306 | int ret = -1; |
| 2307 | |
| 2308 | /* WRITE LOCK */ |
| 2309 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2310 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2311 | if (!name && !ti) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2312 | /* remove all CH clients */ |
| 2313 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2314 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2315 | |
| 2316 | /* remove all endpoints */ |
| 2317 | for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) { |
| 2318 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name); |
| 2319 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address); |
| 2320 | } |
| 2321 | free(server_opts.ch_clients[i].ch_endpts); |
| 2322 | |
| 2323 | switch (server_opts.ch_clients[i].ti) { |
| 2324 | #ifdef NC_ENABLED_SSH |
| 2325 | case NC_TI_LIBSSH: |
| 2326 | nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh); |
| 2327 | free(server_opts.ch_clients[i].opts.ssh); |
| 2328 | break; |
| 2329 | #endif |
| 2330 | #ifdef NC_ENABLED_TLS |
| 2331 | case NC_TI_OPENSSL: |
| 2332 | nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls); |
| 2333 | free(server_opts.ch_clients[i].opts.tls); |
| 2334 | break; |
| 2335 | #endif |
| 2336 | default: |
| 2337 | ERRINT; |
| 2338 | /* won't get here ...*/ |
| 2339 | break; |
| 2340 | } |
| 2341 | |
| 2342 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2343 | |
| 2344 | ret = 0; |
| 2345 | } |
| 2346 | free(server_opts.ch_clients); |
| 2347 | server_opts.ch_clients = NULL; |
| 2348 | |
| 2349 | server_opts.ch_client_count = 0; |
| 2350 | |
| 2351 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2352 | /* remove one client with endpoint(s) or all clients using one protocol */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2353 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2354 | 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] | 2355 | /* remove endpt */ |
| 2356 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2357 | |
| 2358 | switch (server_opts.ch_clients[i].ti) { |
| 2359 | #ifdef NC_ENABLED_SSH |
| 2360 | case NC_TI_LIBSSH: |
| 2361 | nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh); |
| 2362 | free(server_opts.ch_clients[i].opts.ssh); |
| 2363 | break; |
| 2364 | #endif |
| 2365 | #ifdef NC_ENABLED_TLS |
| 2366 | case NC_TI_OPENSSL: |
| 2367 | nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls); |
| 2368 | free(server_opts.ch_clients[i].opts.tls); |
| 2369 | break; |
| 2370 | #endif |
| 2371 | default: |
| 2372 | ERRINT; |
| 2373 | break; |
| 2374 | } |
| 2375 | |
| 2376 | /* remove all endpoints */ |
| 2377 | for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) { |
| 2378 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name); |
| 2379 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address); |
| 2380 | } |
| 2381 | free(server_opts.ch_clients[i].ch_endpts); |
| 2382 | |
| 2383 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2384 | |
| 2385 | /* move last client and endpoint(s) to the empty space */ |
| 2386 | --server_opts.ch_client_count; |
| 2387 | if (i < server_opts.ch_client_count) { |
| 2388 | memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count], |
| 2389 | sizeof *server_opts.ch_clients); |
| 2390 | } else if (!server_opts.ch_client_count) { |
| 2391 | free(server_opts.ch_clients); |
| 2392 | server_opts.ch_clients = NULL; |
| 2393 | } |
| 2394 | |
| 2395 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2396 | if (name) { |
| 2397 | break; |
| 2398 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | /* WRITE UNLOCK */ |
| 2404 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2405 | |
| 2406 | return ret; |
| 2407 | } |
| 2408 | |
| 2409 | API int |
| 2410 | nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name) |
| 2411 | { |
| 2412 | uint16_t i; |
| 2413 | struct nc_ch_client *client; |
| 2414 | |
| 2415 | if (!client_name) { |
| 2416 | ERRARG("client_name"); |
| 2417 | return -1; |
| 2418 | } else if (!endpt_name) { |
| 2419 | ERRARG("endpt_name"); |
| 2420 | return -1; |
| 2421 | } |
| 2422 | |
| 2423 | /* LOCK */ |
| 2424 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2425 | if (!client) { |
| 2426 | return -1; |
| 2427 | } |
| 2428 | |
| 2429 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2430 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2431 | ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name); |
| 2432 | /* UNLOCK */ |
| 2433 | nc_server_ch_client_unlock(client); |
| 2434 | return -1; |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | ++client->ch_endpt_count; |
| 2439 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2440 | if (!client->ch_endpts) { |
| 2441 | ERRMEM; |
| 2442 | /* UNLOCK */ |
| 2443 | nc_server_ch_client_unlock(client); |
| 2444 | return -1; |
| 2445 | } |
| 2446 | |
| 2447 | client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0); |
| 2448 | client->ch_endpts[client->ch_endpt_count - 1].address = NULL; |
| 2449 | client->ch_endpts[client->ch_endpt_count - 1].port = 0; |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 2450 | client->ch_endpts[client->ch_endpt_count - 1].sock_pending = -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2451 | |
| 2452 | /* UNLOCK */ |
| 2453 | nc_server_ch_client_unlock(client); |
| 2454 | |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | API int |
| 2459 | nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name) |
| 2460 | { |
| 2461 | uint16_t i; |
| 2462 | int ret = -1; |
| 2463 | struct nc_ch_client *client; |
| 2464 | |
| 2465 | if (!client_name) { |
| 2466 | ERRARG("client_name"); |
| 2467 | return -1; |
| 2468 | } |
| 2469 | |
| 2470 | /* LOCK */ |
| 2471 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2472 | if (!client) { |
| 2473 | return -1; |
| 2474 | } |
| 2475 | |
| 2476 | if (!endpt_name) { |
| 2477 | /* remove all endpoints */ |
| 2478 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2479 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2480 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 2481 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2482 | close(client->ch_endpts[i].sock_pending); |
| 2483 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2484 | } |
| 2485 | free(client->ch_endpts); |
| 2486 | client->ch_endpts = NULL; |
| 2487 | client->ch_endpt_count = 0; |
| 2488 | |
| 2489 | ret = 0; |
| 2490 | } else { |
| 2491 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2492 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2493 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2494 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2495 | |
Michal Vasko | 4f92101 | 2016-10-20 14:07:45 +0200 | [diff] [blame] | 2496 | /* move last endpoint to the empty space */ |
| 2497 | --client->ch_endpt_count; |
| 2498 | if (i < client->ch_endpt_count) { |
| 2499 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2500 | } else if (!server_opts.ch_client_count) { |
| 2501 | free(server_opts.ch_clients); |
| 2502 | server_opts.ch_clients = NULL; |
| 2503 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2504 | |
Michal Vasko | 4f92101 | 2016-10-20 14:07:45 +0200 | [diff] [blame] | 2505 | ret = 0; |
| 2506 | break; |
| 2507 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | /* UNLOCK */ |
| 2512 | nc_server_ch_client_unlock(client); |
| 2513 | |
| 2514 | return ret; |
| 2515 | } |
| 2516 | |
| 2517 | API int |
| 2518 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 2519 | { |
| 2520 | uint16_t i; |
| 2521 | int ret = -1; |
| 2522 | struct nc_ch_client *client; |
| 2523 | |
| 2524 | if (!client_name) { |
| 2525 | ERRARG("client_name"); |
| 2526 | return -1; |
| 2527 | } else if (!endpt_name) { |
| 2528 | ERRARG("endpt_name"); |
| 2529 | return -1; |
| 2530 | } else if (!address) { |
| 2531 | ERRARG("address"); |
| 2532 | return -1; |
| 2533 | } |
| 2534 | |
| 2535 | /* LOCK */ |
| 2536 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2537 | if (!client) { |
| 2538 | return -1; |
| 2539 | } |
| 2540 | |
| 2541 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2542 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2543 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2544 | client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0); |
| 2545 | |
| 2546 | ret = 0; |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | /* UNLOCK */ |
| 2552 | nc_server_ch_client_unlock(client); |
| 2553 | |
| 2554 | if (ret == -1) { |
| 2555 | ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name); |
| 2556 | } |
| 2557 | |
| 2558 | return ret; |
| 2559 | } |
| 2560 | |
| 2561 | API int |
| 2562 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 2563 | { |
| 2564 | uint16_t i; |
| 2565 | int ret = -1; |
| 2566 | struct nc_ch_client *client; |
| 2567 | |
| 2568 | if (!client_name) { |
| 2569 | ERRARG("client_name"); |
| 2570 | return -1; |
| 2571 | } else if (!endpt_name) { |
| 2572 | ERRARG("endpt_name"); |
| 2573 | return -1; |
| 2574 | } else if (!port) { |
| 2575 | ERRARG("port"); |
| 2576 | return -1; |
| 2577 | } |
| 2578 | |
| 2579 | /* LOCK */ |
| 2580 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2581 | if (!client) { |
| 2582 | return -1; |
| 2583 | } |
| 2584 | |
| 2585 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2586 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2587 | client->ch_endpts[i].port = port; |
| 2588 | |
| 2589 | ret = 0; |
| 2590 | break; |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | /* UNLOCK */ |
| 2595 | nc_server_ch_client_unlock(client); |
| 2596 | |
| 2597 | if (ret == -1) { |
| 2598 | ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name); |
| 2599 | } |
| 2600 | |
| 2601 | return ret; |
| 2602 | } |
| 2603 | |
| 2604 | API int |
| 2605 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 2606 | { |
| 2607 | struct nc_ch_client *client; |
| 2608 | |
| 2609 | if (!client_name) { |
| 2610 | ERRARG("client_name"); |
| 2611 | return -1; |
| 2612 | } else if (!conn_type) { |
| 2613 | ERRARG("conn_type"); |
| 2614 | return -1; |
| 2615 | } |
| 2616 | |
| 2617 | /* LOCK */ |
| 2618 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2619 | if (!client) { |
| 2620 | return -1; |
| 2621 | } |
| 2622 | |
| 2623 | if (client->conn_type != conn_type) { |
| 2624 | client->conn_type = conn_type; |
| 2625 | |
| 2626 | /* set default options */ |
| 2627 | switch (conn_type) { |
| 2628 | case NC_CH_PERSIST: |
| 2629 | client->conn.persist.idle_timeout = 86400; |
| 2630 | client->conn.persist.ka_max_wait = 30; |
| 2631 | client->conn.persist.ka_max_attempts = 3; |
| 2632 | break; |
| 2633 | case NC_CH_PERIOD: |
| 2634 | client->conn.period.idle_timeout = 300; |
| 2635 | client->conn.period.reconnect_timeout = 60; |
| 2636 | break; |
| 2637 | default: |
| 2638 | ERRINT; |
| 2639 | break; |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | /* UNLOCK */ |
| 2644 | nc_server_ch_client_unlock(client); |
| 2645 | |
| 2646 | return 0; |
| 2647 | } |
| 2648 | |
| 2649 | API int |
| 2650 | nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout) |
| 2651 | { |
| 2652 | struct nc_ch_client *client; |
| 2653 | |
| 2654 | if (!client_name) { |
| 2655 | ERRARG("client_name"); |
| 2656 | return -1; |
| 2657 | } |
| 2658 | |
| 2659 | /* LOCK */ |
| 2660 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2661 | if (!client) { |
| 2662 | return -1; |
| 2663 | } |
| 2664 | |
| 2665 | if (client->conn_type != NC_CH_PERSIST) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2666 | 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] | 2667 | /* UNLOCK */ |
| 2668 | nc_server_ch_client_unlock(client); |
| 2669 | return -1; |
| 2670 | } |
| 2671 | |
| 2672 | client->conn.persist.idle_timeout = idle_timeout; |
| 2673 | |
| 2674 | /* UNLOCK */ |
| 2675 | nc_server_ch_client_unlock(client); |
| 2676 | |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
| 2680 | API int |
| 2681 | nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait) |
| 2682 | { |
| 2683 | struct nc_ch_client *client; |
| 2684 | |
| 2685 | if (!client_name) { |
| 2686 | ERRARG("client_name"); |
| 2687 | return -1; |
| 2688 | } else if (!max_wait) { |
| 2689 | ERRARG("max_wait"); |
| 2690 | return -1; |
| 2691 | } |
| 2692 | |
| 2693 | /* LOCK */ |
| 2694 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2695 | if (!client) { |
| 2696 | return -1; |
| 2697 | } |
| 2698 | |
| 2699 | if (client->conn_type != NC_CH_PERSIST) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2700 | 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] | 2701 | /* UNLOCK */ |
| 2702 | nc_server_ch_client_unlock(client); |
| 2703 | return -1; |
| 2704 | } |
| 2705 | |
| 2706 | client->conn.persist.ka_max_wait = max_wait; |
| 2707 | |
| 2708 | /* UNLOCK */ |
| 2709 | nc_server_ch_client_unlock(client); |
| 2710 | |
| 2711 | return 0; |
| 2712 | } |
| 2713 | |
| 2714 | API int |
| 2715 | nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts) |
| 2716 | { |
| 2717 | struct nc_ch_client *client; |
| 2718 | |
| 2719 | if (!client_name) { |
| 2720 | ERRARG("client_name"); |
| 2721 | return -1; |
| 2722 | } |
| 2723 | |
| 2724 | /* LOCK */ |
| 2725 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2726 | if (!client) { |
| 2727 | return -1; |
| 2728 | } |
| 2729 | |
| 2730 | if (client->conn_type != NC_CH_PERSIST) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2731 | 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] | 2732 | /* UNLOCK */ |
| 2733 | nc_server_ch_client_unlock(client); |
| 2734 | return -1; |
| 2735 | } |
| 2736 | |
| 2737 | client->conn.persist.ka_max_attempts = max_attempts; |
| 2738 | |
| 2739 | /* UNLOCK */ |
| 2740 | nc_server_ch_client_unlock(client); |
| 2741 | |
| 2742 | return 0; |
| 2743 | } |
| 2744 | |
| 2745 | API int |
| 2746 | nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout) |
| 2747 | { |
| 2748 | struct nc_ch_client *client; |
| 2749 | |
| 2750 | if (!client_name) { |
| 2751 | ERRARG("client_name"); |
| 2752 | return -1; |
| 2753 | } |
| 2754 | |
| 2755 | /* LOCK */ |
| 2756 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2757 | if (!client) { |
| 2758 | return -1; |
| 2759 | } |
| 2760 | |
| 2761 | if (client->conn_type != NC_CH_PERIOD) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2762 | 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] | 2763 | /* UNLOCK */ |
| 2764 | nc_server_ch_client_unlock(client); |
| 2765 | return -1; |
| 2766 | } |
| 2767 | |
| 2768 | client->conn.period.idle_timeout = idle_timeout; |
| 2769 | |
| 2770 | /* UNLOCK */ |
| 2771 | nc_server_ch_client_unlock(client); |
| 2772 | |
| 2773 | return 0; |
| 2774 | } |
| 2775 | |
| 2776 | API int |
| 2777 | nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout) |
| 2778 | { |
| 2779 | struct nc_ch_client *client; |
| 2780 | |
| 2781 | if (!client_name) { |
| 2782 | ERRARG("client_name"); |
| 2783 | return -1; |
| 2784 | } else if (!reconnect_timeout) { |
| 2785 | ERRARG("reconnect_timeout"); |
| 2786 | return -1; |
| 2787 | } |
| 2788 | |
| 2789 | /* LOCK */ |
| 2790 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2791 | if (!client) { |
| 2792 | return -1; |
| 2793 | } |
| 2794 | |
| 2795 | if (client->conn_type != NC_CH_PERIOD) { |
| 2796 | ERR("Call Home client \"%s\" is not of periodic connection type."); |
| 2797 | /* UNLOCK */ |
| 2798 | nc_server_ch_client_unlock(client); |
| 2799 | return -1; |
| 2800 | } |
| 2801 | |
| 2802 | client->conn.period.reconnect_timeout = reconnect_timeout; |
| 2803 | |
| 2804 | /* UNLOCK */ |
| 2805 | nc_server_ch_client_unlock(client); |
| 2806 | |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
| 2810 | API int |
| 2811 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 2812 | { |
| 2813 | struct nc_ch_client *client; |
| 2814 | |
| 2815 | if (!client_name) { |
| 2816 | ERRARG("client_name"); |
| 2817 | return -1; |
| 2818 | } |
| 2819 | |
| 2820 | /* LOCK */ |
| 2821 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2822 | if (!client) { |
| 2823 | return -1; |
| 2824 | } |
| 2825 | |
| 2826 | client->start_with = start_with; |
| 2827 | |
| 2828 | /* UNLOCK */ |
| 2829 | nc_server_ch_client_unlock(client); |
| 2830 | |
| 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | API int |
| 2835 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 2836 | { |
| 2837 | struct nc_ch_client *client; |
| 2838 | |
| 2839 | if (!client_name) { |
| 2840 | ERRARG("client_name"); |
| 2841 | return -1; |
| 2842 | } else if (!max_attempts) { |
| 2843 | ERRARG("max_attempts"); |
| 2844 | return -1; |
| 2845 | } |
| 2846 | |
| 2847 | /* LOCK */ |
| 2848 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2849 | if (!client) { |
| 2850 | return -1; |
| 2851 | } |
| 2852 | |
| 2853 | client->max_attempts = max_attempts; |
| 2854 | |
| 2855 | /* UNLOCK */ |
| 2856 | nc_server_ch_client_unlock(client); |
| 2857 | |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
| 2861 | /* client lock is expected to be held */ |
| 2862 | static NC_MSG_TYPE |
| 2863 | 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] | 2864 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2865 | NC_MSG_TYPE msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2866 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2867 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 2868 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2869 | |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 2870 | 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] | 2871 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2872 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2873 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 2874 | /* no need to store the socket as pending any longer */ |
| 2875 | endpt->sock_pending = -1; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2876 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2877 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2878 | if (!(*session)) { |
| 2879 | ERRMEM; |
| 2880 | close(sock); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 2881 | free(ip_host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2882 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2883 | } |
| 2884 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2885 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2886 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 2887 | (*session)->host = lydict_insert_zc(server_opts.ctx, ip_host); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2888 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2889 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2890 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2891 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2892 | if (client->ti == NC_TI_LIBSSH) { |
| 2893 | (*session)->data = client->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2894 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2895 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2896 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2897 | if (ret < 0) { |
| 2898 | msgtype = NC_MSG_ERROR; |
| 2899 | goto fail; |
| 2900 | } else if (!ret) { |
| 2901 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2902 | goto fail; |
| 2903 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2904 | } else |
| 2905 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2906 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2907 | if (client->ti == NC_TI_OPENSSL) { |
| 2908 | (*session)->data = client->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2909 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2910 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2911 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2912 | if (ret < 0) { |
| 2913 | msgtype = NC_MSG_ERROR; |
| 2914 | goto fail; |
| 2915 | } else if (!ret) { |
| 2916 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2917 | goto fail; |
| 2918 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2919 | } else |
| 2920 | #endif |
| 2921 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2922 | ERRINT; |
| 2923 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2924 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2925 | goto fail; |
| 2926 | } |
| 2927 | |
| 2928 | /* assign new SID atomically */ |
Michal Vasko | 69a3ff6 | 2018-11-09 09:31:48 +0100 | [diff] [blame] | 2929 | (*session)->id = ATOMIC_INC(&server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2930 | |
| 2931 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2932 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2933 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2934 | goto fail; |
| 2935 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2936 | |
| 2937 | nc_gettimespec_mono(&ts_cur); |
| 2938 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 2939 | nc_gettimespec_real(&ts_cur); |
| 2940 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2941 | (*session)->status = NC_STATUS_RUNNING; |
| 2942 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2943 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2944 | |
| 2945 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2946 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2947 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2948 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2949 | } |
| 2950 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2951 | struct nc_ch_client_thread_arg { |
| 2952 | char *client_name; |
| 2953 | void (*session_clb)(const char *client_name, struct nc_session *new_session); |
| 2954 | }; |
| 2955 | |
| 2956 | static struct nc_ch_client * |
| 2957 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 2958 | { |
| 2959 | struct nc_ch_client *client; |
| 2960 | |
| 2961 | while (1) { |
| 2962 | /* LOCK */ |
| 2963 | client = nc_server_ch_client_lock(name, 0, NULL); |
| 2964 | if (!client) { |
| 2965 | return NULL; |
| 2966 | } |
| 2967 | if (client->ch_endpt_count) { |
| 2968 | return client; |
| 2969 | } |
| 2970 | /* no endpoints defined yet */ |
| 2971 | |
| 2972 | /* UNLOCK */ |
| 2973 | nc_server_ch_client_unlock(client); |
| 2974 | |
| 2975 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 2976 | } |
| 2977 | |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
| 2981 | static int |
| 2982 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 2983 | { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 2984 | int ret = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2985 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2986 | struct timespec ts; |
| 2987 | struct nc_ch_client *client; |
| 2988 | |
| 2989 | /* session created, initialize condition */ |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 2990 | session->opts.server.ch_lock = calloc(1, sizeof *session->opts.server.ch_lock); |
| 2991 | 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] | 2992 | if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) { |
| 2993 | ERRMEM; |
| 2994 | nc_session_free(session, NULL); |
| 2995 | return -1; |
| 2996 | } |
| 2997 | pthread_mutex_init(session->opts.server.ch_lock, NULL); |
| 2998 | pthread_cond_init(session->opts.server.ch_cond, NULL); |
| 2999 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3000 | session->flags |= NC_SESSION_CALLHOME; |
| 3001 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3002 | /* CH LOCK */ |
| 3003 | pthread_mutex_lock(session->opts.server.ch_lock); |
| 3004 | |
| 3005 | /* give the session to the user */ |
| 3006 | data->session_clb(data->client_name, session); |
| 3007 | |
| 3008 | do { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 3009 | nc_gettimespec_real(&ts); |
Michal Vasko | e39ae6b | 2017-03-14 09:03:46 +0100 | [diff] [blame] | 3010 | nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3011 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3012 | r = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts); |
| 3013 | if (!r) { |
| 3014 | /* we were woken up, something probably happened */ |
| 3015 | if (session->status != NC_STATUS_RUNNING) { |
| 3016 | break; |
| 3017 | } |
| 3018 | } else if (r != ETIMEDOUT) { |
| 3019 | ERR("Pthread condition timedwait failed (%s).", strerror(r)); |
| 3020 | ret = -1; |
| 3021 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 3022 | } |
| 3023 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3024 | /* check whether the client was not removed */ |
| 3025 | /* LOCK */ |
| 3026 | client = nc_server_ch_client_lock(data->client_name, 0, NULL); |
| 3027 | if (!client) { |
| 3028 | /* client was removed, finish thread */ |
| 3029 | VRB("Call Home client \"%s\" removed, but an established session will not be terminated.", |
| 3030 | data->client_name); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3031 | ret = 1; |
| 3032 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3033 | } |
| 3034 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3035 | if (client->conn_type == NC_CH_PERSIST) { |
| 3036 | /* TODO keep-alives */ |
| 3037 | idle_timeout = client->conn.persist.idle_timeout; |
| 3038 | } else { |
| 3039 | idle_timeout = client->conn.period.idle_timeout; |
| 3040 | } |
| 3041 | |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3042 | nc_gettimespec_mono(&ts); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3043 | 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] | 3044 | VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id); |
| 3045 | session->status = NC_STATUS_INVALID; |
| 3046 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 3047 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3048 | |
| 3049 | /* UNLOCK */ |
| 3050 | nc_server_ch_client_unlock(client); |
| 3051 | |
| 3052 | } while (session->status == NC_STATUS_RUNNING); |
| 3053 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3054 | /* CH UNLOCK */ |
| 3055 | pthread_mutex_unlock(session->opts.server.ch_lock); |
| 3056 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3057 | if (session->status == NC_STATUS_CLOSING) { |
| 3058 | /* signal to nc_session_free() that we registered session being freed, otherwise it matters not */ |
| 3059 | session->flags &= ~NC_SESSION_CALLHOME; |
| 3060 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3061 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3062 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | static void * |
| 3066 | nc_ch_client_thread(void *arg) |
| 3067 | { |
| 3068 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 3069 | NC_MSG_TYPE msgtype; |
| 3070 | uint8_t cur_attempts = 0; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3071 | uint16_t next_endpt_index; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3072 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3073 | struct nc_ch_endpt *cur_endpt; |
| 3074 | struct nc_session *session; |
| 3075 | struct nc_ch_client *client; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3076 | uint32_t client_id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3077 | |
| 3078 | /* LOCK */ |
| 3079 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3080 | if (!client) { |
| 3081 | goto cleanup; |
| 3082 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3083 | client_id = client->id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3084 | |
| 3085 | cur_endpt = &client->ch_endpts[0]; |
| 3086 | cur_endpt_name = strdup(cur_endpt->name); |
| 3087 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3088 | VRB("Call Home client \"%s\" connecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3089 | while (1) { |
| 3090 | msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session); |
| 3091 | |
| 3092 | if (msgtype == NC_MSG_HELLO) { |
| 3093 | /* UNLOCK */ |
| 3094 | nc_server_ch_client_unlock(client); |
| 3095 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3096 | 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] | 3097 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 3098 | goto cleanup; |
| 3099 | } |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 3100 | VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3101 | |
| 3102 | /* LOCK */ |
| 3103 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3104 | if (!client) { |
| 3105 | goto cleanup; |
| 3106 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3107 | if (client->id != client_id) { |
| 3108 | nc_server_ch_client_unlock(client); |
| 3109 | goto cleanup; |
| 3110 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3111 | |
| 3112 | /* session changed status -> it was disconnected for whatever reason, |
| 3113 | * persistent connection immediately tries to reconnect, periodic waits some first */ |
| 3114 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3115 | /* UNLOCK */ |
| 3116 | nc_server_ch_client_unlock(client); |
| 3117 | |
| 3118 | /* TODO wake up sometimes to check for new notifications */ |
| 3119 | usleep(client->conn.period.reconnect_timeout * 60 * 1000000); |
| 3120 | |
| 3121 | /* LOCK */ |
| 3122 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3123 | if (!client) { |
| 3124 | goto cleanup; |
| 3125 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3126 | if (client->id != client_id) { |
| 3127 | nc_server_ch_client_unlock(client); |
| 3128 | goto cleanup; |
| 3129 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | /* set next endpoint to try */ |
| 3133 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3134 | next_endpt_index = 0; |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3135 | } else { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3136 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 3137 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3138 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 3139 | break; |
| 3140 | } |
| 3141 | } |
| 3142 | if (next_endpt_index >= client->ch_endpt_count) { |
| 3143 | /* endpoint was removed, start with the first one */ |
| 3144 | next_endpt_index = 0; |
| 3145 | } |
| 3146 | } |
| 3147 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3148 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3149 | /* UNLOCK */ |
| 3150 | nc_server_ch_client_unlock(client); |
| 3151 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3152 | /* session was not created */ |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3153 | usleep(NC_CH_ENDPT_FAIL_WAIT * 1000); |
| 3154 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3155 | /* LOCK */ |
| 3156 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3157 | if (!client) { |
| 3158 | goto cleanup; |
| 3159 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3160 | if (client->id != client_id) { |
| 3161 | nc_server_ch_client_unlock(client); |
| 3162 | goto cleanup; |
| 3163 | } |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3164 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3165 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3166 | |
| 3167 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3168 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3169 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3170 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3171 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3174 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3175 | /* endpoint was removed, start with the first one */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3176 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3177 | cur_attempts = 0; |
| 3178 | } else if (cur_attempts == client->max_attempts) { |
| 3179 | /* we have tried to connect to this endpoint enough times */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3180 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3181 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3182 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3183 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3184 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3185 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | cur_attempts = 0; |
| 3189 | } /* else we keep the current one */ |
| 3190 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3191 | |
| 3192 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3193 | free(cur_endpt_name); |
| 3194 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | cleanup: |
| 3198 | VRB("Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3199 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3200 | free(data->client_name); |
| 3201 | free(data); |
| 3202 | return NULL; |
| 3203 | } |
| 3204 | |
| 3205 | API int |
| 3206 | nc_connect_ch_client_dispatch(const char *client_name, |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3207 | void (*session_clb)(const char *client_name, struct nc_session *new_session)) |
| 3208 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3209 | int ret; |
| 3210 | pthread_t tid; |
| 3211 | struct nc_ch_client_thread_arg *arg; |
| 3212 | |
| 3213 | if (!client_name) { |
| 3214 | ERRARG("client_name"); |
| 3215 | return -1; |
| 3216 | } else if (!session_clb) { |
| 3217 | ERRARG("session_clb"); |
| 3218 | return -1; |
| 3219 | } |
| 3220 | |
| 3221 | arg = malloc(sizeof *arg); |
| 3222 | if (!arg) { |
| 3223 | ERRMEM; |
| 3224 | return -1; |
| 3225 | } |
| 3226 | arg->client_name = strdup(client_name); |
| 3227 | if (!arg->client_name) { |
| 3228 | ERRMEM; |
| 3229 | free(arg); |
| 3230 | return -1; |
| 3231 | } |
| 3232 | arg->session_clb = session_clb; |
| 3233 | |
| 3234 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 3235 | if (ret) { |
| 3236 | ERR("Creating a new thread failed (%s).", strerror(ret)); |
| 3237 | free(arg->client_name); |
| 3238 | free(arg); |
| 3239 | return -1; |
| 3240 | } |
| 3241 | /* the thread now manages arg */ |
| 3242 | |
| 3243 | pthread_detach(tid); |
| 3244 | |
| 3245 | return 0; |
| 3246 | } |
| 3247 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3248 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3249 | |
Michal Vasko | e8e0770 | 2017-03-15 10:19:30 +0100 | [diff] [blame] | 3250 | API int |
| 3251 | nc_server_endpt_count(void) |
| 3252 | { |
| 3253 | return server_opts.endpt_count; |
| 3254 | } |
| 3255 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3256 | API time_t |
| 3257 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3258 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3259 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3260 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3261 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3264 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3265 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3266 | |
| 3267 | API void |
| 3268 | nc_session_set_notif_status(struct nc_session *session, int notif_status) |
| 3269 | { |
| 3270 | if (!session || (session->side != NC_SERVER)) { |
| 3271 | ERRARG("session"); |
| 3272 | return; |
| 3273 | } |
| 3274 | |
| 3275 | session->opts.server.ntf_status = (notif_status ? 1 : 0); |
| 3276 | } |
| 3277 | |
| 3278 | API int |
| 3279 | nc_session_get_notif_status(const struct nc_session *session) |
| 3280 | { |
| 3281 | if (!session || (session->side != NC_SERVER)) { |
| 3282 | ERRARG("session"); |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | return session->opts.server.ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3287 | } |
Michal Vasko | 8f43059 | 2019-02-26 08:32:54 +0100 | [diff] [blame] | 3288 | |
| 3289 | API int |
| 3290 | nc_session_is_callhome(const struct nc_session *session) |
| 3291 | { |
| 3292 | if (!session || (session->side != NC_SERVER)) { |
| 3293 | ERRARG("session"); |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
| 3297 | if (session->flags & NC_SESSION_CALLHOME) { |
| 3298 | return 1; |
| 3299 | } |
| 3300 | |
| 3301 | return 0; |
| 3302 | } |