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