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