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