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