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