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