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