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