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