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 | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 886 | API struct nc_session * |
| 887 | nc_ps_get_session_by_sid(const struct nc_pollsession *ps, uint32_t sid) |
| 888 | { |
| 889 | uint8_t q_id; |
| 890 | uint16_t i; |
| 891 | struct nc_session *ret = NULL; |
| 892 | |
| 893 | if (!ps) { |
| 894 | ERRARG("ps"); |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | /* LOCK */ |
| 899 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 900 | return NULL; |
| 901 | } |
| 902 | |
| 903 | for (i = 0; i < ps->session_count; ++i) { |
| 904 | if (ps->sessions[i]->id == sid) { |
| 905 | ret = ps->sessions[i]; |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /* UNLOCK */ |
| 911 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 912 | |
| 913 | return ret; |
| 914 | } |
| 915 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 916 | API uint16_t |
| 917 | nc_ps_session_count(struct nc_pollsession *ps) |
| 918 | { |
| 919 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 920 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
Michal Vasko | f4462fd | 2017-02-15 14:29:05 +0100 | [diff] [blame] | 924 | return ps->session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 925 | } |
| 926 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 927 | /* must be called holding the session lock! |
| 928 | * returns: NC_PSPOLL_ERROR, |
| 929 | * NC_PSPOLL_BAD_RPC, |
| 930 | * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR, |
| 931 | * NC_PSPOLL_RPC |
| 932 | */ |
| 933 | static int |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 934 | 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] | 935 | { |
| 936 | struct lyxml_elem *xml = NULL; |
| 937 | NC_MSG_TYPE msgtype; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 938 | struct nc_server_reply *reply = NULL; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 939 | int ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 940 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 941 | if (!session) { |
| 942 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 943 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 944 | } else if (!rpc) { |
| 945 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 946 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 947 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 948 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 949 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | msgtype = nc_read_msg(session, &xml); |
| 953 | |
| 954 | switch (msgtype) { |
| 955 | case NC_MSG_RPC: |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 956 | *rpc = calloc(1, sizeof **rpc); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 957 | if (!*rpc) { |
| 958 | ERRMEM; |
| 959 | goto error; |
| 960 | } |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 961 | |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 962 | ly_errno = LY_SUCCESS; |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 963 | (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, |
| 964 | 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] | 965 | if (!(*rpc)->tree) { |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 966 | /* parsing RPC failed */ |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 967 | reply = nc_server_reply_err(nc_err_libyang()); |
Radek Krejci | 844662e | 2016-04-13 16:54:43 +0200 | [diff] [blame] | 968 | ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 969 | nc_server_reply_free(reply); |
| 970 | if (ret == -1) { |
| 971 | ERR("Session %u: failed to write reply.", session->id); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 972 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 973 | ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC; |
| 974 | } else { |
| 975 | ret = NC_PSPOLL_RPC; |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 976 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 977 | (*rpc)->root = xml; |
| 978 | break; |
| 979 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 980 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 981 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 982 | goto error; |
| 983 | case NC_MSG_REPLY: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 984 | ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 985 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 986 | goto error; |
| 987 | case NC_MSG_NOTIF: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 988 | ERR("Session %u: received <notification> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 989 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 990 | goto error; |
| 991 | default: |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 992 | /* NC_MSG_ERROR, |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 993 | * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg() |
| 994 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 995 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 999 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1000 | |
| 1001 | error: |
| 1002 | /* cleanup */ |
| 1003 | lyxml_free(server_opts.ctx, xml); |
| 1004 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1005 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1008 | API void |
| 1009 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1010 | { |
| 1011 | global_rpc_clb = clb; |
| 1012 | } |
| 1013 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1014 | API NC_MSG_TYPE |
| 1015 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1016 | { |
| 1017 | NC_MSG_TYPE result = NC_MSG_NOTIF; |
| 1018 | int ret; |
| 1019 | |
| 1020 | /* check parameters */ |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1021 | if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1022 | ERRARG("session"); |
| 1023 | return NC_MSG_ERROR; |
| 1024 | } else if (!notif || !notif->tree || !notif->eventtime) { |
| 1025 | ERRARG("notif"); |
| 1026 | return NC_MSG_ERROR; |
| 1027 | } |
| 1028 | |
| 1029 | /* 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] | 1030 | ret = nc_session_lock(session, timeout, __func__); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1031 | if (ret < 0) { |
| 1032 | return NC_MSG_ERROR; |
| 1033 | } else if (!ret) { |
| 1034 | return NC_MSG_WOULDBLOCK; |
| 1035 | } |
| 1036 | |
| 1037 | ret = nc_write_msg(session, NC_MSG_NOTIF, notif); |
| 1038 | if (ret == -1) { |
| 1039 | ERR("Session %u: failed to write notification.", session->id); |
| 1040 | result = NC_MSG_ERROR; |
| 1041 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1042 | |
| 1043 | nc_session_unlock(session, timeout, __func__); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1044 | |
| 1045 | return result; |
| 1046 | } |
| 1047 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1048 | /* must be called holding the session lock! |
| 1049 | * returns: NC_PSPOLL_ERROR, |
| 1050 | * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR, |
| 1051 | * NC_PSPOLL_REPLY_ERROR, |
| 1052 | * 0 |
| 1053 | */ |
| 1054 | static int |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1055 | 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] | 1056 | { |
| 1057 | nc_rpc_clb clb; |
| 1058 | struct nc_server_reply *reply; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1059 | struct lys_node *rpc_act = NULL; |
| 1060 | struct lyd_node *next, *elem; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1061 | int ret = 0, r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1062 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1063 | if (!rpc) { |
| 1064 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1065 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1068 | if (rpc->tree->schema->nodetype == LYS_RPC) { |
| 1069 | /* RPC */ |
| 1070 | rpc_act = rpc->tree->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1071 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1072 | /* action */ |
| 1073 | LY_TREE_DFS_BEGIN(rpc->tree, next, elem) { |
| 1074 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1075 | rpc_act = elem->schema; |
| 1076 | break; |
| 1077 | } |
| 1078 | LY_TREE_DFS_END(rpc->tree, next, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1079 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1080 | if (!rpc_act) { |
| 1081 | ERRINT; |
| 1082 | return NC_PSPOLL_ERROR; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | if (!rpc_act->priv) { |
| 1087 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1088 | 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] | 1089 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1090 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1091 | reply = clb(rpc->tree, session); |
| 1092 | } |
| 1093 | |
| 1094 | if (!reply) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1095 | 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] | 1096 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1097 | r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply); |
| 1098 | if (reply->type == NC_RPL_ERROR) { |
| 1099 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1100 | } |
| 1101 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1102 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1103 | if (r == -1) { |
| 1104 | ERR("Session %u: failed to write reply.", session->id); |
| 1105 | ret |= NC_PSPOLL_ERROR; |
| 1106 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1109 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1110 | session->status = NC_STATUS_INVALID; |
| 1111 | } |
| 1112 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1113 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | API int |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1117 | 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] | 1118 | { |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1119 | int ret, r, poll_ret; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1120 | uint8_t q_id; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1121 | uint16_t i, j; |
| 1122 | char msg[256]; |
| 1123 | NC_SESSION_TERM_REASON term_reason; |
| 1124 | struct pollfd pfd; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1125 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1126 | struct nc_session *cur_session; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1127 | struct nc_server_rpc *rpc = NULL; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1128 | #ifdef NC_ENABLED_SSH |
| 1129 | struct nc_session *new; |
| 1130 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1131 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1132 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1133 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1134 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1137 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1138 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1139 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1140 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1141 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1142 | if (!ps->session_count) { |
| 1143 | ret = NC_PSPOLL_NOSESSIONS; |
| 1144 | goto ps_unlock_finish; |
| 1145 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1146 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1147 | /* check timeout of all the sessions */ |
| 1148 | nc_gettimespec(&ts_cur); |
| 1149 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1150 | if (!(ps->sessions[i]->flags & NC_SESSION_CALLHOME) && !ps->sessions[i]->opts.server.ntf_status |
| 1151 | && server_opts.idle_timeout |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1152 | && (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] | 1153 | ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id); |
| 1154 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 1155 | ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1156 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1157 | if (session) { |
| 1158 | *session = ps->sessions[i]; |
| 1159 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1160 | goto ps_unlock_finish; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1161 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1164 | if (timeout > -1) { |
| 1165 | nc_gettimespec(&ts_timeout); |
| 1166 | nc_addtimespec(&ts_timeout, timeout); |
| 1167 | } |
| 1168 | |
| 1169 | /* poll all the sessions one-by-one */ |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1170 | do { |
| 1171 | /* loop from i to j */ |
| 1172 | if (ps->last_event_session == ps->session_count - 1) { |
| 1173 | i = j = 0; |
| 1174 | } else { |
| 1175 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1176 | } |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1177 | do { |
| 1178 | cur_session = ps->sessions[i]; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1179 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1180 | /* SESSION LOCK */ |
| 1181 | if ((cur_session->status == NC_STATUS_RUNNING) |
| 1182 | && !*cur_session->ti_inuse && ((r = nc_session_lock(cur_session, 0, __func__)))) { |
| 1183 | /* we go here if we successfully lock the session or there was an error, on timeout we simply skip it */ |
| 1184 | if (r == -1) { |
| 1185 | ret = NC_PSPOLL_ERROR; |
| 1186 | goto ps_unlock_finish; |
| 1187 | } |
| 1188 | /* damn race condition */ |
| 1189 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1190 | /* SESSION UNLOCK */ |
| 1191 | nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
| 1192 | goto next_iteration; |
| 1193 | } |
| 1194 | |
| 1195 | switch (cur_session->ti_type) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1196 | #ifdef NC_ENABLED_SSH |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1197 | case NC_TI_LIBSSH: |
| 1198 | r = ssh_channel_poll_timeout(cur_session->ti.libssh.channel, 0, 0); |
| 1199 | if (r < 1) { |
| 1200 | if (r == SSH_EOF) { |
| 1201 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1202 | term_reason = NC_SESSION_TERM_DROPPED; |
| 1203 | poll_ret = -2; |
| 1204 | } else if (r == SSH_ERROR) { |
| 1205 | sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(cur_session->ti.libssh.session)); |
| 1206 | term_reason = NC_SESSION_TERM_OTHER; |
| 1207 | poll_ret = -2; |
| 1208 | } else { |
| 1209 | poll_ret = 0; |
| 1210 | } |
| 1211 | break; |
| 1212 | } |
| 1213 | |
| 1214 | /* we have some data, but it may be just an SSH message */ |
| 1215 | r = ssh_execute_message_callbacks(cur_session->ti.libssh.session); |
| 1216 | if (r != SSH_OK) { |
| 1217 | 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] | 1218 | term_reason = NC_SESSION_TERM_OTHER; |
| 1219 | poll_ret = -2; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1220 | } else if (cur_session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1221 | /* new SSH message */ |
| 1222 | cur_session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1223 | if (cur_session->ti.libssh.next) { |
| 1224 | for (new = cur_session->ti.libssh.next; new != cur_session; new = new->ti.libssh.next) { |
| 1225 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel |
| 1226 | && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1227 | /* new NETCONF SSH channel */ |
| 1228 | if (session) { |
| 1229 | *session = cur_session; |
| 1230 | } |
| 1231 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1232 | goto session_ps_unlock_finish; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1233 | } |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1234 | } |
| 1235 | } |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1236 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1237 | /* just some SSH message */ |
| 1238 | if (session) { |
| 1239 | *session = cur_session; |
| 1240 | } |
| 1241 | ret = NC_PSPOLL_SSH_MSG; |
| 1242 | goto session_ps_unlock_finish; |
| 1243 | } else { |
| 1244 | /* we have some application data */ |
| 1245 | poll_ret = 1; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1246 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1247 | break; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1248 | #endif |
| 1249 | #ifdef NC_ENABLED_TLS |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1250 | case NC_TI_OPENSSL: |
| 1251 | r = SSL_pending(cur_session->ti.tls); |
| 1252 | if (!r) { |
| 1253 | /* no data pending in the SSL buffer, poll fd */ |
| 1254 | pfd.fd = SSL_get_rfd(cur_session->ti.tls); |
| 1255 | if (pfd.fd < 0) { |
| 1256 | ERRINT; |
| 1257 | ret = NC_PSPOLL_ERROR; |
| 1258 | goto session_ps_unlock_finish; |
| 1259 | } |
| 1260 | pfd.events = POLLIN; |
| 1261 | pfd.revents = 0; |
| 1262 | r = poll(&pfd, 1, 0); |
| 1263 | |
| 1264 | if ((r < 0) && (errno != EINTR)) { |
| 1265 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1266 | poll_ret = -1; |
| 1267 | } else if (r > 0) { |
| 1268 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1269 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1270 | term_reason = NC_SESSION_TERM_DROPPED; |
| 1271 | poll_ret = -2; |
| 1272 | } else if (pfd.revents & POLLERR) { |
| 1273 | sprintf(msg, "communication socket error"); |
| 1274 | term_reason = NC_SESSION_TERM_OTHER; |
| 1275 | poll_ret = -2; |
| 1276 | } else { |
| 1277 | poll_ret = 1; |
| 1278 | } |
| 1279 | } else { |
| 1280 | poll_ret = 0; |
| 1281 | } |
| 1282 | } else { |
| 1283 | poll_ret = 1; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1284 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1285 | break; |
| 1286 | #endif |
| 1287 | case NC_TI_FD: |
| 1288 | pfd.fd = cur_session->ti.fd.in; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1289 | pfd.events = POLLIN; |
| 1290 | pfd.revents = 0; |
| 1291 | r = poll(&pfd, 1, 0); |
| 1292 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1293 | if ((r < 0) && (errno != EINTR)) { |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1294 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1295 | poll_ret = -1; |
| 1296 | } else if (r > 0) { |
| 1297 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1298 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1299 | term_reason = NC_SESSION_TERM_DROPPED; |
| 1300 | poll_ret = -2; |
| 1301 | } else if (pfd.revents & POLLERR) { |
| 1302 | sprintf(msg, "communication socket error"); |
| 1303 | term_reason = NC_SESSION_TERM_OTHER; |
| 1304 | poll_ret = -2; |
| 1305 | } else { |
| 1306 | poll_ret = 1; |
| 1307 | } |
| 1308 | } else { |
| 1309 | poll_ret = 0; |
| 1310 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1311 | break; |
| 1312 | case NC_TI_NONE: |
| 1313 | ERRINT; |
| 1314 | ret = NC_PSPOLL_ERROR; |
| 1315 | goto session_ps_unlock_finish; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1316 | } |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1317 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1318 | /* here: poll_ret == -2 - session error, session terminated, |
| 1319 | * poll_ret == -1 - generic error, |
| 1320 | * poll_ret == 0 - nothing to read, |
| 1321 | * poll_ret > 0 - data available |
| 1322 | */ |
| 1323 | if (poll_ret == -2) { |
| 1324 | ERR("Session %u: %s.", cur_session->id, msg); |
| 1325 | cur_session->status = NC_STATUS_INVALID; |
| 1326 | cur_session->term_reason = term_reason; |
| 1327 | if (session) { |
| 1328 | *session = cur_session; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1329 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1330 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1331 | goto session_ps_unlock_finish; |
| 1332 | } else if (poll_ret == -1) { |
| 1333 | ERR("Session %u: %s.", cur_session->id, msg); |
| 1334 | ret = NC_PSPOLL_ERROR; |
| 1335 | goto session_ps_unlock_finish; |
| 1336 | } else if (poll_ret > 0) { |
| 1337 | break; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1338 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1339 | |
| 1340 | /* SESSION UNLOCK */ |
| 1341 | nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
| 1342 | } else { |
| 1343 | /* timeout */ |
| 1344 | poll_ret = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1345 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1346 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1347 | next_iteration: |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1348 | if (i == ps->session_count - 1) { |
| 1349 | i = 0; |
| 1350 | } else { |
| 1351 | ++i; |
| 1352 | } |
| 1353 | } while (i != j); |
| 1354 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1355 | /* no event, no session remains locked */ |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1356 | if (!poll_ret && (timeout > -1)) { |
| 1357 | usleep(NC_TIMEOUT_STEP); |
| 1358 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1359 | nc_gettimespec(&ts_cur); |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1360 | /* final timeout */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1361 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1362 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1363 | goto ps_unlock_finish; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1364 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1365 | } |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1366 | } while (!poll_ret); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1367 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1368 | /* 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] | 1369 | if (session) { |
| 1370 | *session = cur_session; |
| 1371 | } |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1372 | ps->last_event_session = i; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1373 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1374 | /* PS UNLOCK */ |
| 1375 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1376 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1377 | ret = nc_server_recv_rpc(cur_session, &rpc); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1378 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1379 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1380 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1381 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1382 | goto session_unlock_finish; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1385 | cur_session->opts.server.last_rpc = time(NULL); |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1386 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1387 | /* process RPC, not needed afterwards */ |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1388 | ret |= nc_server_send_reply(cur_session, rpc); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1389 | nc_server_rpc_free(rpc, server_opts.ctx); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1390 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1391 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1392 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1393 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1394 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1395 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1396 | } |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1397 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1398 | session_unlock_finish: |
| 1399 | /* SESSION UNLOCK */ |
| 1400 | nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
| 1401 | return ret; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1402 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1403 | session_ps_unlock_finish: |
| 1404 | /* SESSION UNLOCK */ |
| 1405 | nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
| 1406 | |
| 1407 | ps_unlock_finish: |
| 1408 | /* PS UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1409 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1410 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1413 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1414 | 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] | 1415 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1416 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1417 | uint16_t i; |
| 1418 | struct nc_session *session; |
| 1419 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1420 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1421 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1422 | return; |
| 1423 | } |
| 1424 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1425 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1426 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1427 | return; |
| 1428 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1429 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1430 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1431 | for (i = 0; i < ps->session_count; i++) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1432 | nc_session_free(ps->sessions[i], data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1433 | } |
| 1434 | free(ps->sessions); |
| 1435 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1436 | ps->session_count = 0; |
Michal Vasko | 99f251b | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1437 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1438 | } else { |
| 1439 | for (i = 0; i < ps->session_count; ) { |
| 1440 | if (ps->sessions[i]->status != NC_STATUS_RUNNING) { |
| 1441 | session = ps->sessions[i]; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1442 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1443 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1444 | continue; |
| 1445 | } |
| 1446 | |
| 1447 | ++i; |
| 1448 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1449 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1450 | |
| 1451 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1452 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1455 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1456 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1457 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1458 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1459 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1460 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1461 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1462 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1463 | if (!name) { |
| 1464 | ERRARG("name"); |
| 1465 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1468 | /* BIND LOCK */ |
| 1469 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1470 | |
| 1471 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1472 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1473 | |
| 1474 | /* check name uniqueness */ |
| 1475 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1476 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1477 | ERR("Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1478 | ret = -1; |
| 1479 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1483 | ++server_opts.endpt_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1484 | 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] | 1485 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1486 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1487 | ret = -1; |
| 1488 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1489 | } |
| 1490 | 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] | 1491 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1492 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1493 | 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] | 1494 | if (!server_opts.binds) { |
| 1495 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1496 | ret = -1; |
| 1497 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1498 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1499 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1500 | server_opts.binds[server_opts.endpt_count - 1].address = NULL; |
| 1501 | server_opts.binds[server_opts.endpt_count - 1].port = 0; |
| 1502 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 1503 | server_opts.binds[server_opts.endpt_count - 1].pollin = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1504 | |
| 1505 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1506 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1507 | case NC_TI_LIBSSH: |
| 1508 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1509 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 1510 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1511 | ret = -1; |
| 1512 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1513 | } |
| 1514 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
| 1515 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 1516 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3; |
| 1517 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10; |
| 1518 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1519 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1520 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1521 | case NC_TI_OPENSSL: |
| 1522 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 1523 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 1524 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1525 | ret = -1; |
| 1526 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1527 | } |
| 1528 | break; |
| 1529 | #endif |
| 1530 | default: |
| 1531 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1532 | ret = -1; |
| 1533 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1534 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1535 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1536 | cleanup: |
| 1537 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1538 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1539 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1540 | /* BIND UNLOCK */ |
| 1541 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 1542 | |
| 1543 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1546 | int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1547 | 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] | 1548 | { |
| 1549 | struct nc_endpt *endpt; |
| 1550 | struct nc_bind *bind = NULL; |
| 1551 | uint16_t i; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1552 | int sock = -1, set_addr, ret = 0; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1553 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1554 | if (!endpt_name) { |
| 1555 | ERRARG("endpt_name"); |
| 1556 | return -1; |
| 1557 | } else if ((!address && !port) || (address && port)) { |
| 1558 | ERRARG("address and port"); |
| 1559 | return -1; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1562 | if (address) { |
| 1563 | set_addr = 1; |
| 1564 | } else { |
| 1565 | set_addr = 0; |
| 1566 | } |
| 1567 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1568 | /* BIND LOCK */ |
| 1569 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1570 | |
| 1571 | /* ENDPT LOCK */ |
| 1572 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1573 | if (!endpt) { |
Michal Vasko | 4e455dd | 2017-03-21 15:33:43 +0100 | [diff] [blame^] | 1574 | /* BIND UNLOCK */ |
| 1575 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1576 | return -1; |
| 1577 | } |
| 1578 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1579 | bind = &server_opts.binds[i]; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1580 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1581 | if (set_addr) { |
| 1582 | port = bind->port; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1583 | } else { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1584 | address = bind->address; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1585 | } |
| 1586 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1587 | /* we have all the information we need to create a listening socket */ |
| 1588 | if (address && port) { |
| 1589 | /* create new socket, close the old one */ |
| 1590 | sock = nc_sock_listen(address, port); |
| 1591 | if (sock == -1) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1592 | ret = -1; |
| 1593 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | if (bind->sock > -1) { |
| 1597 | close(bind->sock); |
| 1598 | } |
| 1599 | bind->sock = sock; |
| 1600 | } /* else we are just setting address or port */ |
| 1601 | |
| 1602 | if (set_addr) { |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1603 | lydict_remove(server_opts.ctx, bind->address); |
| 1604 | bind->address = lydict_insert(server_opts.ctx, address, 0); |
| 1605 | } else { |
| 1606 | bind->port = port; |
| 1607 | } |
| 1608 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1609 | if (sock > -1) { |
| 1610 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1611 | 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] | 1612 | #elif defined(NC_ENABLED_SSH) |
| 1613 | VRB("Listening on %s:%u for SSH connections.", address, port); |
| 1614 | #else |
| 1615 | VRB("Listening on %s:%u for TLS connections.", address, port); |
| 1616 | #endif |
| 1617 | } |
| 1618 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1619 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1620 | /* ENDPT UNLOCK */ |
| 1621 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 1622 | |
| 1623 | /* BIND UNLOCK */ |
| 1624 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1625 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1626 | return ret; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1629 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1630 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 1631 | { |
| 1632 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 1633 | } |
| 1634 | |
| 1635 | API int |
| 1636 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 1637 | { |
| 1638 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 1639 | } |
| 1640 | |
| 1641 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1642 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1643 | { |
| 1644 | uint32_t i; |
| 1645 | int ret = -1; |
| 1646 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1647 | /* BIND LOCK */ |
| 1648 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1649 | |
| 1650 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1651 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1652 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1653 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1654 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1655 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1656 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1657 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1658 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1659 | case NC_TI_LIBSSH: |
| 1660 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1661 | free(server_opts.endpts[i].opts.ssh); |
| 1662 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1663 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1664 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1665 | case NC_TI_OPENSSL: |
| 1666 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1667 | free(server_opts.endpts[i].opts.tls); |
| 1668 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1669 | #endif |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1670 | default: |
| 1671 | ERRINT; |
| 1672 | /* won't get here ...*/ |
| 1673 | break; |
| 1674 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1675 | ret = 0; |
| 1676 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1677 | free(server_opts.endpts); |
| 1678 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1679 | |
| 1680 | /* remove all binds */ |
| 1681 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1682 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 1683 | if (server_opts.binds[i].sock > -1) { |
| 1684 | close(server_opts.binds[i].sock); |
| 1685 | } |
| 1686 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1687 | free(server_opts.binds); |
| 1688 | server_opts.binds = NULL; |
| 1689 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1690 | server_opts.endpt_count = 0; |
| 1691 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1692 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1693 | /* 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] | 1694 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1695 | 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] | 1696 | /* remove endpt */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1697 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1698 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1699 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1700 | case NC_TI_LIBSSH: |
| 1701 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1702 | free(server_opts.endpts[i].opts.ssh); |
| 1703 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1704 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1705 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1706 | case NC_TI_OPENSSL: |
| 1707 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1708 | free(server_opts.endpts[i].opts.tls); |
| 1709 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1710 | #endif |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1711 | default: |
| 1712 | ERRINT; |
| 1713 | break; |
| 1714 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1715 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1716 | /* remove bind(s) */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1717 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 1718 | if (server_opts.binds[i].sock > -1) { |
| 1719 | close(server_opts.binds[i].sock); |
| 1720 | } |
| 1721 | |
| 1722 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1723 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 1724 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 1725 | free(server_opts.binds); |
| 1726 | server_opts.binds = NULL; |
| 1727 | free(server_opts.endpts); |
| 1728 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 1729 | } else if (i < server_opts.endpt_count) { |
| 1730 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 1731 | 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] | 1732 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1733 | |
| 1734 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1735 | if (name) { |
| 1736 | break; |
| 1737 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1738 | } |
| 1739 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1740 | } |
| 1741 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1742 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1743 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1744 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1745 | /* BIND UNLOCK */ |
| 1746 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 1747 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1748 | return ret; |
| 1749 | } |
| 1750 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1751 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1752 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1753 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1754 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1755 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1756 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1757 | uint16_t port, bind_idx; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1758 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1759 | if (!server_opts.ctx) { |
| 1760 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1761 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1762 | } else if (!session) { |
| 1763 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1764 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1765 | } |
| 1766 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1767 | /* BIND LOCK */ |
| 1768 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1769 | |
| 1770 | if (!server_opts.endpt_count) { |
Michal Vasko | 863a6e9 | 2016-07-28 14:27:41 +0200 | [diff] [blame] | 1771 | ERR("No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1772 | /* BIND UNLOCK */ |
| 1773 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1774 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1775 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1776 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1777 | 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] | 1778 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1779 | /* BIND UNLOCK */ |
| 1780 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1781 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 1782 | if (!ret) { |
| 1783 | return NC_MSG_WOULDBLOCK; |
| 1784 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1785 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1786 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1787 | |
| 1788 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 1789 | /* ENDPT READ LOCK */ |
| 1790 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 1791 | |
| 1792 | /* BIND UNLOCK */ |
| 1793 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 1794 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1795 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1796 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1797 | *session = nc_new_session(0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 1798 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1799 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1800 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1801 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1802 | msgtype = NC_MSG_ERROR; |
| 1803 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1804 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1805 | (*session)->status = NC_STATUS_STARTING; |
| 1806 | (*session)->side = NC_SERVER; |
| 1807 | (*session)->ctx = server_opts.ctx; |
| 1808 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 1809 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
| 1810 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1811 | |
| 1812 | /* transport lock */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1813 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1814 | pthread_cond_init((*session)->ti_cond, NULL); |
| 1815 | *(*session)->ti_inuse = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1816 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1817 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1818 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1819 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 1820 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 1821 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1822 | if (ret < 0) { |
| 1823 | msgtype = NC_MSG_ERROR; |
| 1824 | goto cleanup; |
| 1825 | } else if (!ret) { |
| 1826 | msgtype = NC_MSG_WOULDBLOCK; |
| 1827 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1828 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1829 | } else |
| 1830 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1831 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1832 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 1833 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 1834 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1835 | if (ret < 0) { |
| 1836 | msgtype = NC_MSG_ERROR; |
| 1837 | goto cleanup; |
| 1838 | } else if (!ret) { |
| 1839 | msgtype = NC_MSG_WOULDBLOCK; |
| 1840 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1841 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1842 | } else |
| 1843 | #endif |
| 1844 | { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1845 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1846 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1847 | msgtype = NC_MSG_ERROR; |
| 1848 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1849 | } |
| 1850 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1851 | (*session)->data = NULL; |
| 1852 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1853 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1854 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1855 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1856 | /* assign new SID atomically */ |
| 1857 | /* LOCK */ |
| 1858 | pthread_spin_lock(&server_opts.sid_lock); |
| 1859 | (*session)->id = server_opts.new_session_id++; |
| 1860 | /* UNLOCK */ |
| 1861 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1862 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1863 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1864 | msgtype = nc_handshake(*session); |
| 1865 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1866 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1867 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1868 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1869 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1870 | (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1871 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1872 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1873 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1874 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1875 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1876 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1877 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1878 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1879 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1880 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1881 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1882 | } |
| 1883 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1884 | API int |
| 1885 | nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti) |
| 1886 | { |
| 1887 | uint16_t i; |
| 1888 | |
| 1889 | if (!name) { |
| 1890 | ERRARG("name"); |
| 1891 | return -1; |
| 1892 | } else if (!ti) { |
| 1893 | ERRARG("ti"); |
| 1894 | return -1; |
| 1895 | } |
| 1896 | |
| 1897 | /* WRITE LOCK */ |
| 1898 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 1899 | |
| 1900 | /* check name uniqueness */ |
| 1901 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 1902 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 1903 | ERR("Call Home client \"%s\" already exists.", name); |
| 1904 | /* WRITE UNLOCK */ |
| 1905 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1906 | return -1; |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | ++server_opts.ch_client_count; |
| 1911 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 1912 | if (!server_opts.ch_clients) { |
| 1913 | ERRMEM; |
| 1914 | /* WRITE UNLOCK */ |
| 1915 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1916 | return -1; |
| 1917 | } |
| 1918 | server_opts.ch_clients[server_opts.ch_client_count - 1].name = lydict_insert(server_opts.ctx, name, 0); |
| 1919 | server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1920 | server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL; |
| 1921 | 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] | 1922 | |
| 1923 | switch (ti) { |
| 1924 | #ifdef NC_ENABLED_SSH |
| 1925 | case NC_TI_LIBSSH: |
| 1926 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1927 | if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) { |
| 1928 | ERRMEM; |
| 1929 | /* WRITE UNLOCK */ |
| 1930 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1931 | return -1; |
| 1932 | } |
| 1933 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods = |
| 1934 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 1935 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3; |
| 1936 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10; |
| 1937 | break; |
| 1938 | #endif |
| 1939 | #ifdef NC_ENABLED_TLS |
| 1940 | case NC_TI_OPENSSL: |
| 1941 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 1942 | if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) { |
| 1943 | ERRMEM; |
| 1944 | /* WRITE UNLOCK */ |
| 1945 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1946 | return -1; |
| 1947 | } |
| 1948 | break; |
| 1949 | #endif |
| 1950 | default: |
| 1951 | ERRINT; |
| 1952 | /* WRITE UNLOCK */ |
| 1953 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1954 | return -1; |
| 1955 | } |
| 1956 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1957 | server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0; |
| 1958 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1959 | /* set CH default options */ |
| 1960 | server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED; |
| 1961 | server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3; |
| 1962 | |
| 1963 | pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL); |
| 1964 | |
| 1965 | /* WRITE UNLOCK */ |
| 1966 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1967 | |
| 1968 | return 0; |
| 1969 | } |
| 1970 | |
| 1971 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1972 | nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1973 | { |
| 1974 | uint16_t i, j; |
| 1975 | int ret = -1; |
| 1976 | |
| 1977 | /* WRITE LOCK */ |
| 1978 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 1979 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1980 | if (!name && !ti) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1981 | /* remove all CH clients */ |
| 1982 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 1983 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 1984 | |
| 1985 | /* remove all endpoints */ |
| 1986 | for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) { |
| 1987 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name); |
| 1988 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address); |
| 1989 | } |
| 1990 | free(server_opts.ch_clients[i].ch_endpts); |
| 1991 | |
| 1992 | switch (server_opts.ch_clients[i].ti) { |
| 1993 | #ifdef NC_ENABLED_SSH |
| 1994 | case NC_TI_LIBSSH: |
| 1995 | nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh); |
| 1996 | free(server_opts.ch_clients[i].opts.ssh); |
| 1997 | break; |
| 1998 | #endif |
| 1999 | #ifdef NC_ENABLED_TLS |
| 2000 | case NC_TI_OPENSSL: |
| 2001 | nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls); |
| 2002 | free(server_opts.ch_clients[i].opts.tls); |
| 2003 | break; |
| 2004 | #endif |
| 2005 | default: |
| 2006 | ERRINT; |
| 2007 | /* won't get here ...*/ |
| 2008 | break; |
| 2009 | } |
| 2010 | |
| 2011 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2012 | |
| 2013 | ret = 0; |
| 2014 | } |
| 2015 | free(server_opts.ch_clients); |
| 2016 | server_opts.ch_clients = NULL; |
| 2017 | |
| 2018 | server_opts.ch_client_count = 0; |
| 2019 | |
| 2020 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2021 | /* remove one client with endpoint(s) or all clients using one protocol */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2022 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2023 | 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] | 2024 | /* remove endpt */ |
| 2025 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2026 | |
| 2027 | switch (server_opts.ch_clients[i].ti) { |
| 2028 | #ifdef NC_ENABLED_SSH |
| 2029 | case NC_TI_LIBSSH: |
| 2030 | nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh); |
| 2031 | free(server_opts.ch_clients[i].opts.ssh); |
| 2032 | break; |
| 2033 | #endif |
| 2034 | #ifdef NC_ENABLED_TLS |
| 2035 | case NC_TI_OPENSSL: |
| 2036 | nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls); |
| 2037 | free(server_opts.ch_clients[i].opts.tls); |
| 2038 | break; |
| 2039 | #endif |
| 2040 | default: |
| 2041 | ERRINT; |
| 2042 | break; |
| 2043 | } |
| 2044 | |
| 2045 | /* remove all endpoints */ |
| 2046 | for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) { |
| 2047 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name); |
| 2048 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address); |
| 2049 | } |
| 2050 | free(server_opts.ch_clients[i].ch_endpts); |
| 2051 | |
| 2052 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2053 | |
| 2054 | /* move last client and endpoint(s) to the empty space */ |
| 2055 | --server_opts.ch_client_count; |
| 2056 | if (i < server_opts.ch_client_count) { |
| 2057 | memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count], |
| 2058 | sizeof *server_opts.ch_clients); |
| 2059 | } else if (!server_opts.ch_client_count) { |
| 2060 | free(server_opts.ch_clients); |
| 2061 | server_opts.ch_clients = NULL; |
| 2062 | } |
| 2063 | |
| 2064 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2065 | if (name) { |
| 2066 | break; |
| 2067 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | /* WRITE UNLOCK */ |
| 2073 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2074 | |
| 2075 | return ret; |
| 2076 | } |
| 2077 | |
| 2078 | API int |
| 2079 | nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name) |
| 2080 | { |
| 2081 | uint16_t i; |
| 2082 | struct nc_ch_client *client; |
| 2083 | |
| 2084 | if (!client_name) { |
| 2085 | ERRARG("client_name"); |
| 2086 | return -1; |
| 2087 | } else if (!endpt_name) { |
| 2088 | ERRARG("endpt_name"); |
| 2089 | return -1; |
| 2090 | } |
| 2091 | |
| 2092 | /* LOCK */ |
| 2093 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2094 | if (!client) { |
| 2095 | return -1; |
| 2096 | } |
| 2097 | |
| 2098 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2099 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2100 | ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name); |
| 2101 | /* UNLOCK */ |
| 2102 | nc_server_ch_client_unlock(client); |
| 2103 | return -1; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | ++client->ch_endpt_count; |
| 2108 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2109 | if (!client->ch_endpts) { |
| 2110 | ERRMEM; |
| 2111 | /* UNLOCK */ |
| 2112 | nc_server_ch_client_unlock(client); |
| 2113 | return -1; |
| 2114 | } |
| 2115 | |
| 2116 | client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0); |
| 2117 | client->ch_endpts[client->ch_endpt_count - 1].address = NULL; |
| 2118 | client->ch_endpts[client->ch_endpt_count - 1].port = 0; |
| 2119 | |
| 2120 | /* UNLOCK */ |
| 2121 | nc_server_ch_client_unlock(client); |
| 2122 | |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | API int |
| 2127 | nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name) |
| 2128 | { |
| 2129 | uint16_t i; |
| 2130 | int ret = -1; |
| 2131 | struct nc_ch_client *client; |
| 2132 | |
| 2133 | if (!client_name) { |
| 2134 | ERRARG("client_name"); |
| 2135 | return -1; |
| 2136 | } |
| 2137 | |
| 2138 | /* LOCK */ |
| 2139 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2140 | if (!client) { |
| 2141 | return -1; |
| 2142 | } |
| 2143 | |
| 2144 | if (!endpt_name) { |
| 2145 | /* remove all endpoints */ |
| 2146 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2147 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2148 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2149 | } |
| 2150 | free(client->ch_endpts); |
| 2151 | client->ch_endpts = NULL; |
| 2152 | client->ch_endpt_count = 0; |
| 2153 | |
| 2154 | ret = 0; |
| 2155 | } else { |
| 2156 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2157 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2158 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2159 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2160 | |
Michal Vasko | 4f92101 | 2016-10-20 14:07:45 +0200 | [diff] [blame] | 2161 | /* move last endpoint to the empty space */ |
| 2162 | --client->ch_endpt_count; |
| 2163 | if (i < client->ch_endpt_count) { |
| 2164 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2165 | } else if (!server_opts.ch_client_count) { |
| 2166 | free(server_opts.ch_clients); |
| 2167 | server_opts.ch_clients = NULL; |
| 2168 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2169 | |
Michal Vasko | 4f92101 | 2016-10-20 14:07:45 +0200 | [diff] [blame] | 2170 | ret = 0; |
| 2171 | break; |
| 2172 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | /* UNLOCK */ |
| 2177 | nc_server_ch_client_unlock(client); |
| 2178 | |
| 2179 | return ret; |
| 2180 | } |
| 2181 | |
| 2182 | API int |
| 2183 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 2184 | { |
| 2185 | uint16_t i; |
| 2186 | int ret = -1; |
| 2187 | struct nc_ch_client *client; |
| 2188 | |
| 2189 | if (!client_name) { |
| 2190 | ERRARG("client_name"); |
| 2191 | return -1; |
| 2192 | } else if (!endpt_name) { |
| 2193 | ERRARG("endpt_name"); |
| 2194 | return -1; |
| 2195 | } else if (!address) { |
| 2196 | ERRARG("address"); |
| 2197 | return -1; |
| 2198 | } |
| 2199 | |
| 2200 | /* LOCK */ |
| 2201 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2202 | if (!client) { |
| 2203 | return -1; |
| 2204 | } |
| 2205 | |
| 2206 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2207 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2208 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2209 | client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0); |
| 2210 | |
| 2211 | ret = 0; |
| 2212 | break; |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | /* UNLOCK */ |
| 2217 | nc_server_ch_client_unlock(client); |
| 2218 | |
| 2219 | if (ret == -1) { |
| 2220 | ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name); |
| 2221 | } |
| 2222 | |
| 2223 | return ret; |
| 2224 | } |
| 2225 | |
| 2226 | API int |
| 2227 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 2228 | { |
| 2229 | uint16_t i; |
| 2230 | int ret = -1; |
| 2231 | struct nc_ch_client *client; |
| 2232 | |
| 2233 | if (!client_name) { |
| 2234 | ERRARG("client_name"); |
| 2235 | return -1; |
| 2236 | } else if (!endpt_name) { |
| 2237 | ERRARG("endpt_name"); |
| 2238 | return -1; |
| 2239 | } else if (!port) { |
| 2240 | ERRARG("port"); |
| 2241 | return -1; |
| 2242 | } |
| 2243 | |
| 2244 | /* LOCK */ |
| 2245 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2246 | if (!client) { |
| 2247 | return -1; |
| 2248 | } |
| 2249 | |
| 2250 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2251 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2252 | client->ch_endpts[i].port = port; |
| 2253 | |
| 2254 | ret = 0; |
| 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | /* UNLOCK */ |
| 2260 | nc_server_ch_client_unlock(client); |
| 2261 | |
| 2262 | if (ret == -1) { |
| 2263 | ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name); |
| 2264 | } |
| 2265 | |
| 2266 | return ret; |
| 2267 | } |
| 2268 | |
| 2269 | API int |
| 2270 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 2271 | { |
| 2272 | struct nc_ch_client *client; |
| 2273 | |
| 2274 | if (!client_name) { |
| 2275 | ERRARG("client_name"); |
| 2276 | return -1; |
| 2277 | } else if (!conn_type) { |
| 2278 | ERRARG("conn_type"); |
| 2279 | return -1; |
| 2280 | } |
| 2281 | |
| 2282 | /* LOCK */ |
| 2283 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2284 | if (!client) { |
| 2285 | return -1; |
| 2286 | } |
| 2287 | |
| 2288 | if (client->conn_type != conn_type) { |
| 2289 | client->conn_type = conn_type; |
| 2290 | |
| 2291 | /* set default options */ |
| 2292 | switch (conn_type) { |
| 2293 | case NC_CH_PERSIST: |
| 2294 | client->conn.persist.idle_timeout = 86400; |
| 2295 | client->conn.persist.ka_max_wait = 30; |
| 2296 | client->conn.persist.ka_max_attempts = 3; |
| 2297 | break; |
| 2298 | case NC_CH_PERIOD: |
| 2299 | client->conn.period.idle_timeout = 300; |
| 2300 | client->conn.period.reconnect_timeout = 60; |
| 2301 | break; |
| 2302 | default: |
| 2303 | ERRINT; |
| 2304 | break; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | /* UNLOCK */ |
| 2309 | nc_server_ch_client_unlock(client); |
| 2310 | |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | API int |
| 2315 | nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout) |
| 2316 | { |
| 2317 | struct nc_ch_client *client; |
| 2318 | |
| 2319 | if (!client_name) { |
| 2320 | ERRARG("client_name"); |
| 2321 | return -1; |
| 2322 | } |
| 2323 | |
| 2324 | /* LOCK */ |
| 2325 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2326 | if (!client) { |
| 2327 | return -1; |
| 2328 | } |
| 2329 | |
| 2330 | if (client->conn_type != NC_CH_PERSIST) { |
| 2331 | ERR("Call Home client \"%s\" is not of persistent connection type."); |
| 2332 | /* UNLOCK */ |
| 2333 | nc_server_ch_client_unlock(client); |
| 2334 | return -1; |
| 2335 | } |
| 2336 | |
| 2337 | client->conn.persist.idle_timeout = idle_timeout; |
| 2338 | |
| 2339 | /* UNLOCK */ |
| 2340 | nc_server_ch_client_unlock(client); |
| 2341 | |
| 2342 | return 0; |
| 2343 | } |
| 2344 | |
| 2345 | API int |
| 2346 | nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait) |
| 2347 | { |
| 2348 | struct nc_ch_client *client; |
| 2349 | |
| 2350 | if (!client_name) { |
| 2351 | ERRARG("client_name"); |
| 2352 | return -1; |
| 2353 | } else if (!max_wait) { |
| 2354 | ERRARG("max_wait"); |
| 2355 | return -1; |
| 2356 | } |
| 2357 | |
| 2358 | /* LOCK */ |
| 2359 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2360 | if (!client) { |
| 2361 | return -1; |
| 2362 | } |
| 2363 | |
| 2364 | if (client->conn_type != NC_CH_PERSIST) { |
| 2365 | ERR("Call Home client \"%s\" is not of persistent connection type."); |
| 2366 | /* UNLOCK */ |
| 2367 | nc_server_ch_client_unlock(client); |
| 2368 | return -1; |
| 2369 | } |
| 2370 | |
| 2371 | client->conn.persist.ka_max_wait = max_wait; |
| 2372 | |
| 2373 | /* UNLOCK */ |
| 2374 | nc_server_ch_client_unlock(client); |
| 2375 | |
| 2376 | return 0; |
| 2377 | } |
| 2378 | |
| 2379 | API int |
| 2380 | nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts) |
| 2381 | { |
| 2382 | struct nc_ch_client *client; |
| 2383 | |
| 2384 | if (!client_name) { |
| 2385 | ERRARG("client_name"); |
| 2386 | return -1; |
| 2387 | } |
| 2388 | |
| 2389 | /* LOCK */ |
| 2390 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2391 | if (!client) { |
| 2392 | return -1; |
| 2393 | } |
| 2394 | |
| 2395 | if (client->conn_type != NC_CH_PERSIST) { |
| 2396 | ERR("Call Home client \"%s\" is not of persistent connection type."); |
| 2397 | /* UNLOCK */ |
| 2398 | nc_server_ch_client_unlock(client); |
| 2399 | return -1; |
| 2400 | } |
| 2401 | |
| 2402 | client->conn.persist.ka_max_attempts = max_attempts; |
| 2403 | |
| 2404 | /* UNLOCK */ |
| 2405 | nc_server_ch_client_unlock(client); |
| 2406 | |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | API int |
| 2411 | nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout) |
| 2412 | { |
| 2413 | struct nc_ch_client *client; |
| 2414 | |
| 2415 | if (!client_name) { |
| 2416 | ERRARG("client_name"); |
| 2417 | return -1; |
| 2418 | } |
| 2419 | |
| 2420 | /* LOCK */ |
| 2421 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2422 | if (!client) { |
| 2423 | return -1; |
| 2424 | } |
| 2425 | |
| 2426 | if (client->conn_type != NC_CH_PERIOD) { |
| 2427 | ERR("Call Home client \"%s\" is not of periodic connection type."); |
| 2428 | /* UNLOCK */ |
| 2429 | nc_server_ch_client_unlock(client); |
| 2430 | return -1; |
| 2431 | } |
| 2432 | |
| 2433 | client->conn.period.idle_timeout = idle_timeout; |
| 2434 | |
| 2435 | /* UNLOCK */ |
| 2436 | nc_server_ch_client_unlock(client); |
| 2437 | |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
| 2441 | API int |
| 2442 | nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout) |
| 2443 | { |
| 2444 | struct nc_ch_client *client; |
| 2445 | |
| 2446 | if (!client_name) { |
| 2447 | ERRARG("client_name"); |
| 2448 | return -1; |
| 2449 | } else if (!reconnect_timeout) { |
| 2450 | ERRARG("reconnect_timeout"); |
| 2451 | return -1; |
| 2452 | } |
| 2453 | |
| 2454 | /* LOCK */ |
| 2455 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2456 | if (!client) { |
| 2457 | return -1; |
| 2458 | } |
| 2459 | |
| 2460 | if (client->conn_type != NC_CH_PERIOD) { |
| 2461 | ERR("Call Home client \"%s\" is not of periodic connection type."); |
| 2462 | /* UNLOCK */ |
| 2463 | nc_server_ch_client_unlock(client); |
| 2464 | return -1; |
| 2465 | } |
| 2466 | |
| 2467 | client->conn.period.reconnect_timeout = reconnect_timeout; |
| 2468 | |
| 2469 | /* UNLOCK */ |
| 2470 | nc_server_ch_client_unlock(client); |
| 2471 | |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
| 2475 | API int |
| 2476 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 2477 | { |
| 2478 | struct nc_ch_client *client; |
| 2479 | |
| 2480 | if (!client_name) { |
| 2481 | ERRARG("client_name"); |
| 2482 | return -1; |
| 2483 | } |
| 2484 | |
| 2485 | /* LOCK */ |
| 2486 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2487 | if (!client) { |
| 2488 | return -1; |
| 2489 | } |
| 2490 | |
| 2491 | client->start_with = start_with; |
| 2492 | |
| 2493 | /* UNLOCK */ |
| 2494 | nc_server_ch_client_unlock(client); |
| 2495 | |
| 2496 | return 0; |
| 2497 | } |
| 2498 | |
| 2499 | API int |
| 2500 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 2501 | { |
| 2502 | struct nc_ch_client *client; |
| 2503 | |
| 2504 | if (!client_name) { |
| 2505 | ERRARG("client_name"); |
| 2506 | return -1; |
| 2507 | } else if (!max_attempts) { |
| 2508 | ERRARG("max_attempts"); |
| 2509 | return -1; |
| 2510 | } |
| 2511 | |
| 2512 | /* LOCK */ |
| 2513 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2514 | if (!client) { |
| 2515 | return -1; |
| 2516 | } |
| 2517 | |
| 2518 | client->max_attempts = max_attempts; |
| 2519 | |
| 2520 | /* UNLOCK */ |
| 2521 | nc_server_ch_client_unlock(client); |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | /* client lock is expected to be held */ |
| 2527 | static NC_MSG_TYPE |
| 2528 | 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] | 2529 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2530 | NC_MSG_TYPE msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2531 | int sock, ret; |
| 2532 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2533 | sock = nc_sock_connect(endpt->address, endpt->port); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 2534 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2535 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2536 | } |
| 2537 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2538 | *session = nc_new_session(0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2539 | if (!(*session)) { |
| 2540 | ERRMEM; |
| 2541 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2542 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2543 | } |
| 2544 | (*session)->status = NC_STATUS_STARTING; |
| 2545 | (*session)->side = NC_SERVER; |
| 2546 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2547 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2548 | (*session)->host = lydict_insert(server_opts.ctx, endpt->address, 0); |
| 2549 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2550 | |
| 2551 | /* transport lock */ |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2552 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2553 | pthread_cond_init((*session)->ti_cond, NULL); |
| 2554 | *(*session)->ti_inuse = 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2555 | |
| 2556 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2557 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2558 | if (client->ti == NC_TI_LIBSSH) { |
| 2559 | (*session)->data = client->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2560 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2561 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2562 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2563 | if (ret < 0) { |
| 2564 | msgtype = NC_MSG_ERROR; |
| 2565 | goto fail; |
| 2566 | } else if (!ret) { |
| 2567 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2568 | goto fail; |
| 2569 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2570 | } else |
| 2571 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2572 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2573 | if (client->ti == NC_TI_OPENSSL) { |
| 2574 | (*session)->data = client->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2575 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2576 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2577 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2578 | if (ret < 0) { |
| 2579 | msgtype = NC_MSG_ERROR; |
| 2580 | goto fail; |
| 2581 | } else if (!ret) { |
| 2582 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2583 | goto fail; |
| 2584 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2585 | } else |
| 2586 | #endif |
| 2587 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2588 | ERRINT; |
| 2589 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2590 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2591 | goto fail; |
| 2592 | } |
| 2593 | |
| 2594 | /* assign new SID atomically */ |
| 2595 | /* LOCK */ |
| 2596 | pthread_spin_lock(&server_opts.sid_lock); |
| 2597 | (*session)->id = server_opts.new_session_id++; |
| 2598 | /* UNLOCK */ |
| 2599 | pthread_spin_unlock(&server_opts.sid_lock); |
| 2600 | |
| 2601 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2602 | msgtype = nc_handshake(*session); |
| 2603 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2604 | goto fail; |
| 2605 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2606 | (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2607 | (*session)->status = NC_STATUS_RUNNING; |
| 2608 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2609 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2610 | |
| 2611 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2612 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2613 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2614 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2615 | } |
| 2616 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2617 | struct nc_ch_client_thread_arg { |
| 2618 | char *client_name; |
| 2619 | void (*session_clb)(const char *client_name, struct nc_session *new_session); |
| 2620 | }; |
| 2621 | |
| 2622 | static struct nc_ch_client * |
| 2623 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 2624 | { |
| 2625 | struct nc_ch_client *client; |
| 2626 | |
| 2627 | while (1) { |
| 2628 | /* LOCK */ |
| 2629 | client = nc_server_ch_client_lock(name, 0, NULL); |
| 2630 | if (!client) { |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | if (client->ch_endpt_count) { |
| 2634 | return client; |
| 2635 | } |
| 2636 | /* no endpoints defined yet */ |
| 2637 | |
| 2638 | /* UNLOCK */ |
| 2639 | nc_server_ch_client_unlock(client); |
| 2640 | |
| 2641 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 2642 | } |
| 2643 | |
| 2644 | return NULL; |
| 2645 | } |
| 2646 | |
| 2647 | static int |
| 2648 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 2649 | { |
| 2650 | int ret; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2651 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2652 | struct timespec ts; |
| 2653 | struct nc_ch_client *client; |
| 2654 | |
| 2655 | /* session created, initialize condition */ |
| 2656 | session->opts.server.ch_lock = malloc(sizeof *session->opts.server.ch_lock); |
| 2657 | session->opts.server.ch_cond = malloc(sizeof *session->opts.server.ch_cond); |
| 2658 | if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) { |
| 2659 | ERRMEM; |
| 2660 | nc_session_free(session, NULL); |
| 2661 | return -1; |
| 2662 | } |
| 2663 | pthread_mutex_init(session->opts.server.ch_lock, NULL); |
| 2664 | pthread_cond_init(session->opts.server.ch_cond, NULL); |
| 2665 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2666 | session->flags |= NC_SESSION_CALLHOME; |
| 2667 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2668 | /* CH LOCK */ |
| 2669 | pthread_mutex_lock(session->opts.server.ch_lock); |
| 2670 | |
| 2671 | /* give the session to the user */ |
| 2672 | data->session_clb(data->client_name, session); |
| 2673 | |
| 2674 | do { |
| 2675 | nc_gettimespec(&ts); |
Michal Vasko | e39ae6b | 2017-03-14 09:03:46 +0100 | [diff] [blame] | 2676 | nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2677 | |
| 2678 | ret = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts); |
| 2679 | if (ret && (ret != ETIMEDOUT)) { |
| 2680 | ERR("Pthread condition timedwait failed (%s).", strerror(ret)); |
| 2681 | goto ch_client_remove; |
| 2682 | } |
| 2683 | |
| 2684 | /* check whether the client was not removed */ |
| 2685 | /* LOCK */ |
| 2686 | client = nc_server_ch_client_lock(data->client_name, 0, NULL); |
| 2687 | if (!client) { |
| 2688 | /* client was removed, finish thread */ |
| 2689 | VRB("Call Home client \"%s\" removed, but an established session will not be terminated.", |
| 2690 | data->client_name); |
| 2691 | goto ch_client_remove; |
| 2692 | } |
| 2693 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2694 | if (client->conn_type == NC_CH_PERSIST) { |
| 2695 | /* TODO keep-alives */ |
| 2696 | idle_timeout = client->conn.persist.idle_timeout; |
| 2697 | } else { |
| 2698 | idle_timeout = client->conn.period.idle_timeout; |
| 2699 | } |
| 2700 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 2701 | 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] | 2702 | VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id); |
| 2703 | session->status = NC_STATUS_INVALID; |
| 2704 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 2705 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2706 | |
| 2707 | /* UNLOCK */ |
| 2708 | nc_server_ch_client_unlock(client); |
| 2709 | |
| 2710 | } while (session->status == NC_STATUS_RUNNING); |
| 2711 | |
| 2712 | /* CH UNLOCK */ |
| 2713 | pthread_mutex_unlock(session->opts.server.ch_lock); |
| 2714 | |
| 2715 | return 0; |
| 2716 | |
| 2717 | ch_client_remove: |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2718 | /* make the session a standard one */ |
| 2719 | pthread_cond_destroy(session->opts.server.ch_cond); |
| 2720 | free(session->opts.server.ch_cond); |
| 2721 | session->opts.server.ch_cond = NULL; |
| 2722 | |
| 2723 | session->flags &= ~NC_SESSION_CALLHOME; |
| 2724 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2725 | /* CH UNLOCK */ |
| 2726 | pthread_mutex_unlock(session->opts.server.ch_lock); |
| 2727 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2728 | pthread_mutex_destroy(session->opts.server.ch_lock); |
| 2729 | free(session->opts.server.ch_lock); |
| 2730 | session->opts.server.ch_lock = NULL; |
| 2731 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2732 | return 1; |
| 2733 | } |
| 2734 | |
| 2735 | static void * |
| 2736 | nc_ch_client_thread(void *arg) |
| 2737 | { |
| 2738 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 2739 | NC_MSG_TYPE msgtype; |
| 2740 | uint8_t cur_attempts = 0; |
| 2741 | uint16_t i; |
| 2742 | char *cur_endpt_name; |
| 2743 | struct nc_ch_endpt *cur_endpt; |
| 2744 | struct nc_session *session; |
| 2745 | struct nc_ch_client *client; |
| 2746 | |
| 2747 | /* LOCK */ |
| 2748 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2749 | if (!client) { |
| 2750 | goto cleanup; |
| 2751 | } |
| 2752 | |
| 2753 | cur_endpt = &client->ch_endpts[0]; |
| 2754 | cur_endpt_name = strdup(cur_endpt->name); |
| 2755 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 2756 | VRB("Call Home client \"%s\" connecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2757 | while (1) { |
| 2758 | msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session); |
| 2759 | |
| 2760 | if (msgtype == NC_MSG_HELLO) { |
| 2761 | /* UNLOCK */ |
| 2762 | nc_server_ch_client_unlock(client); |
| 2763 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 2764 | 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] | 2765 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 2766 | goto cleanup; |
| 2767 | } |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 2768 | VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2769 | |
| 2770 | /* LOCK */ |
| 2771 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2772 | if (!client) { |
| 2773 | goto cleanup; |
| 2774 | } |
| 2775 | |
| 2776 | /* session changed status -> it was disconnected for whatever reason, |
| 2777 | * persistent connection immediately tries to reconnect, periodic waits some first */ |
| 2778 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2779 | /* UNLOCK */ |
| 2780 | nc_server_ch_client_unlock(client); |
| 2781 | |
| 2782 | /* TODO wake up sometimes to check for new notifications */ |
| 2783 | usleep(client->conn.period.reconnect_timeout * 60 * 1000000); |
| 2784 | |
| 2785 | /* LOCK */ |
| 2786 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2787 | if (!client) { |
| 2788 | goto cleanup; |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | /* set next endpoint to try */ |
| 2793 | if (client->start_with == NC_CH_FIRST_LISTED) { |
| 2794 | cur_endpt = &client->ch_endpts[0]; |
| 2795 | free(cur_endpt_name); |
| 2796 | cur_endpt_name = strdup(cur_endpt->name); |
| 2797 | } /* else we keep the current one */ |
| 2798 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2799 | /* UNLOCK */ |
| 2800 | nc_server_ch_client_unlock(client); |
| 2801 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2802 | /* session was not created */ |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2803 | usleep(NC_CH_ENDPT_FAIL_WAIT * 1000); |
| 2804 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2805 | /* LOCK */ |
| 2806 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2807 | if (!client) { |
| 2808 | goto cleanup; |
| 2809 | } |
| 2810 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2811 | ++cur_attempts; |
| 2812 | if (cur_attempts == client->max_attempts) { |
| 2813 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2814 | if (!strcmp(client->ch_endpts[i].name, cur_endpt_name)) { |
| 2815 | break; |
| 2816 | } |
| 2817 | } |
| 2818 | if (i < client->ch_endpt_count - 1) { |
| 2819 | /* just go to the next endpoint */ |
| 2820 | cur_endpt = &client->ch_endpts[i + 1]; |
| 2821 | free(cur_endpt_name); |
| 2822 | cur_endpt_name = strdup(cur_endpt->name); |
| 2823 | } else { |
| 2824 | /* cur_endpoint was removed or is the last, either way start with the first one */ |
| 2825 | cur_endpt = &client->ch_endpts[0]; |
| 2826 | free(cur_endpt_name); |
| 2827 | cur_endpt_name = strdup(cur_endpt->name); |
| 2828 | } |
| 2829 | |
| 2830 | cur_attempts = 0; |
| 2831 | } /* else we keep the current one */ |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | cleanup: |
| 2836 | VRB("Call Home client \"%s\" thread exit.", data->client_name); |
| 2837 | |
| 2838 | free(data->client_name); |
| 2839 | free(data); |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | |
| 2843 | API int |
| 2844 | nc_connect_ch_client_dispatch(const char *client_name, |
| 2845 | void (*session_clb)(const char *client_name, struct nc_session *new_session)) { |
| 2846 | int ret; |
| 2847 | pthread_t tid; |
| 2848 | struct nc_ch_client_thread_arg *arg; |
| 2849 | |
| 2850 | if (!client_name) { |
| 2851 | ERRARG("client_name"); |
| 2852 | return -1; |
| 2853 | } else if (!session_clb) { |
| 2854 | ERRARG("session_clb"); |
| 2855 | return -1; |
| 2856 | } |
| 2857 | |
| 2858 | arg = malloc(sizeof *arg); |
| 2859 | if (!arg) { |
| 2860 | ERRMEM; |
| 2861 | return -1; |
| 2862 | } |
| 2863 | arg->client_name = strdup(client_name); |
| 2864 | if (!arg->client_name) { |
| 2865 | ERRMEM; |
| 2866 | free(arg); |
| 2867 | return -1; |
| 2868 | } |
| 2869 | arg->session_clb = session_clb; |
| 2870 | |
| 2871 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 2872 | if (ret) { |
| 2873 | ERR("Creating a new thread failed (%s).", strerror(ret)); |
| 2874 | free(arg->client_name); |
| 2875 | free(arg); |
| 2876 | return -1; |
| 2877 | } |
| 2878 | /* the thread now manages arg */ |
| 2879 | |
| 2880 | pthread_detach(tid); |
| 2881 | |
| 2882 | return 0; |
| 2883 | } |
| 2884 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2885 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2886 | |
Michal Vasko | e8e0770 | 2017-03-15 10:19:30 +0100 | [diff] [blame] | 2887 | API int |
| 2888 | nc_server_endpt_count(void) |
| 2889 | { |
| 2890 | return server_opts.endpt_count; |
| 2891 | } |
| 2892 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 2893 | API time_t |
| 2894 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2895 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2896 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2897 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 2898 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2901 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 2902 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 2903 | |
| 2904 | API void |
| 2905 | nc_session_set_notif_status(struct nc_session *session, int notif_status) |
| 2906 | { |
| 2907 | if (!session || (session->side != NC_SERVER)) { |
| 2908 | ERRARG("session"); |
| 2909 | return; |
| 2910 | } |
| 2911 | |
| 2912 | session->opts.server.ntf_status = (notif_status ? 1 : 0); |
| 2913 | } |
| 2914 | |
| 2915 | API int |
| 2916 | nc_session_get_notif_status(const struct nc_session *session) |
| 2917 | { |
| 2918 | if (!session || (session->side != NC_SERVER)) { |
| 2919 | ERRARG("session"); |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
| 2923 | return session->opts.server.ntf_status; |
| 2924 | } |