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) { |
Jan Kundrát | 0f942e8 | 2018-02-14 14:52:00 +0100 | [diff] [blame^] | 355 | *host = malloc(INET6_ADDRSTRLEN); |
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 | f8aa997 | 2018-01-31 13:19:08 +0100 | [diff] [blame] | 420 | lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL, 0, 0); |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 421 | } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) { |
Michal Vasko | f8aa997 | 2018-01-31 13:19:08 +0100 | [diff] [blame] | 422 | lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 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 |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 670 | nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id) |
| 671 | { |
| 672 | uint8_t q_last; |
| 673 | |
| 674 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
| 675 | ERRINT; |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | /* get a unique queue value (by adding 1 to the last added value, if any) */ |
| 680 | if (ps->queue_len) { |
| 681 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 682 | *id = ps->queue[q_last] + 1; |
| 683 | } else { |
| 684 | *id = 0; |
| 685 | } |
| 686 | |
| 687 | /* add the id into the queue */ |
| 688 | ++ps->queue_len; |
| 689 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 690 | ps->queue[q_last] = *id; |
| 691 | } |
| 692 | |
| 693 | static void |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 694 | nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id) |
| 695 | { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 696 | uint8_t i, q_idx, found = 0; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 697 | |
| 698 | for (i = 0; i < ps->queue_len; ++i) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 699 | /* get the actual queue idx */ |
| 700 | q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 701 | |
| 702 | if (found) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 703 | if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 704 | /* another equal value, simply cannot be */ |
| 705 | ERRINT; |
| 706 | } |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 707 | if (found == 2) { |
| 708 | /* move the following values */ |
| 709 | ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx]; |
| 710 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 711 | } else if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 712 | /* found our id, there can be no more equal valid values */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 713 | if (i == 0) { |
| 714 | found = 1; |
| 715 | } else { |
| 716 | /* this is not okay, our id is in the middle of the queue */ |
| 717 | found = 2; |
| 718 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 719 | } |
| 720 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 721 | if (!found) { |
| 722 | ERRINT; |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 723 | return; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 724 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 725 | |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 726 | --ps->queue_len; |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 727 | if (found == 1) { |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 728 | /* remove the id by moving the queue, otherwise all the values in the queue were moved */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 729 | ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE; |
| 730 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 733 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 734 | 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] | 735 | { |
| 736 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 737 | struct timespec ts; |
| 738 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 739 | nc_gettimespec_real(&ts); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 740 | nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 741 | |
| 742 | /* LOCK */ |
| 743 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 744 | if (ret) { |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 745 | ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 746 | return -1; |
| 747 | } |
| 748 | |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 749 | /* check that the queue is long enough */ |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 750 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 751 | ERR("%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE); |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 752 | pthread_mutex_unlock(&ps->lock); |
| 753 | return -1; |
| 754 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 755 | |
| 756 | /* add ourselves into the queue */ |
| 757 | nc_ps_queue_add_id(ps, id); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 758 | |
| 759 | /* is it our turn? */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 760 | while (ps->queue[ps->queue_begin] != *id) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 761 | nc_gettimespec_real(&ts); |
Michal Vasko | 2b76809 | 2018-02-12 16:37:12 +0100 | [diff] [blame] | 762 | nc_addtimespec(&ts, NC_PS_QUEUE_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 763 | |
| 764 | ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts); |
| 765 | if (ret) { |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 766 | 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] | 767 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 768 | nc_ps_queue_remove_id(ps, *id); |
| 769 | pthread_mutex_unlock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 770 | return -1; |
| 771 | } |
| 772 | } |
| 773 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 774 | /* UNLOCK */ |
| 775 | pthread_mutex_unlock(&ps->lock); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 780 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 781 | 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] | 782 | { |
| 783 | int ret; |
| 784 | struct timespec ts; |
| 785 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 786 | nc_gettimespec_real(&ts); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 787 | nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 788 | |
| 789 | /* LOCK */ |
| 790 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 791 | if (ret) { |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 792 | ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 793 | ret = -1; |
| 794 | } |
| 795 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 796 | /* we must be the first, it was our turn after all, right? */ |
| 797 | if (ps->queue[ps->queue_begin] != id) { |
| 798 | ERRINT; |
Michal Vasko | b1a094b | 2016-10-05 14:04:52 +0200 | [diff] [blame] | 799 | /* UNLOCK */ |
| 800 | if (!ret) { |
| 801 | pthread_mutex_unlock(&ps->lock); |
| 802 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 803 | return -1; |
| 804 | } |
| 805 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 806 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 807 | nc_ps_queue_remove_id(ps, id); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 808 | |
| 809 | /* broadcast to all other threads that the queue moved */ |
| 810 | pthread_cond_broadcast(&ps->cond); |
| 811 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 812 | /* UNLOCK */ |
| 813 | if (!ret) { |
| 814 | pthread_mutex_unlock(&ps->lock); |
| 815 | } |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 820 | API struct nc_pollsession * |
| 821 | nc_ps_new(void) |
| 822 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 823 | struct nc_pollsession *ps; |
| 824 | |
| 825 | ps = calloc(1, sizeof(struct nc_pollsession)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 826 | if (!ps) { |
| 827 | ERRMEM; |
| 828 | return NULL; |
| 829 | } |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 830 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 831 | pthread_mutex_init(&ps->lock, NULL); |
| 832 | |
| 833 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | API void |
| 837 | nc_ps_free(struct nc_pollsession *ps) |
| 838 | { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 839 | uint16_t i; |
| 840 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 841 | if (!ps) { |
| 842 | return; |
| 843 | } |
| 844 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 845 | if (ps->queue_len) { |
| 846 | ERR("FATAL: Freeing a pollsession structure that is currently being worked with!"); |
| 847 | } |
| 848 | |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 849 | for (i = 0; i < ps->session_count; i++) { |
| 850 | free(ps->sessions[i]); |
| 851 | } |
| 852 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 853 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 854 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 855 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 856 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 857 | free(ps); |
| 858 | } |
| 859 | |
| 860 | API int |
| 861 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 862 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 863 | uint8_t q_id; |
| 864 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 865 | if (!ps) { |
| 866 | ERRARG("ps"); |
| 867 | return -1; |
| 868 | } else if (!session) { |
| 869 | ERRARG("session"); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 870 | return -1; |
| 871 | } |
| 872 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 873 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 874 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 875 | return -1; |
| 876 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 877 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 878 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 879 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 880 | if (!ps->sessions) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 881 | ERRMEM; |
| 882 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 883 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 884 | return -1; |
| 885 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 886 | ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions); |
| 887 | if (!ps->sessions[ps->session_count - 1]) { |
| 888 | ERRMEM; |
| 889 | --ps->session_count; |
| 890 | /* UNLOCK */ |
| 891 | nc_ps_unlock(ps, q_id, __func__); |
| 892 | return -1; |
| 893 | } |
| 894 | ps->sessions[ps->session_count - 1]->session = session; |
| 895 | ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 896 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 897 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 898 | return nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 901 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 902 | _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] | 903 | { |
| 904 | uint16_t i; |
| 905 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 906 | if (index >= 0) { |
| 907 | i = (uint16_t)index; |
| 908 | goto remove; |
| 909 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 910 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 911 | if (ps->sessions[i]->session == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 912 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 913 | --ps->session_count; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 914 | if (i <= ps->session_count) { |
| 915 | free(ps->sessions[i]); |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 916 | ps->sessions[i] = ps->sessions[ps->session_count]; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 917 | } |
| 918 | if (!ps->session_count) { |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 919 | free(ps->sessions); |
| 920 | ps->sessions = NULL; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 921 | } |
Michal Vasko | 11d2f6a | 2017-02-02 11:15:06 +0100 | [diff] [blame] | 922 | ps->last_event_session = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 923 | return 0; |
| 924 | } |
| 925 | } |
| 926 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 927 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 928 | } |
| 929 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 930 | API int |
| 931 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 932 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 933 | uint8_t q_id; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 934 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 935 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 936 | if (!ps) { |
| 937 | ERRARG("ps"); |
| 938 | return -1; |
| 939 | } else if (!session) { |
| 940 | ERRARG("session"); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 941 | return -1; |
| 942 | } |
| 943 | |
| 944 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 945 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 946 | return -1; |
| 947 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 948 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 949 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 950 | |
| 951 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 952 | ret2 = nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 953 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 954 | return (ret || ret2 ? -1 : 0); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 957 | API struct nc_session * |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 958 | nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx) |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 959 | { |
| 960 | uint8_t q_id; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 961 | struct nc_session *ret = NULL; |
| 962 | |
| 963 | if (!ps) { |
| 964 | ERRARG("ps"); |
| 965 | return NULL; |
| 966 | } |
| 967 | |
| 968 | /* LOCK */ |
| 969 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 970 | return NULL; |
| 971 | } |
| 972 | |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 973 | if (idx < ps->session_count) { |
| 974 | ret = ps->sessions[idx]->session; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* UNLOCK */ |
| 978 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 979 | |
| 980 | return ret; |
| 981 | } |
| 982 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 983 | API uint16_t |
| 984 | nc_ps_session_count(struct nc_pollsession *ps) |
| 985 | { |
| 986 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 987 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 988 | return 0; |
| 989 | } |
| 990 | |
Michal Vasko | f4462fd | 2017-02-15 14:29:05 +0100 | [diff] [blame] | 991 | return ps->session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 994 | /* must be called holding the session lock! |
| 995 | * returns: NC_PSPOLL_ERROR, |
| 996 | * NC_PSPOLL_BAD_RPC, |
| 997 | * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR, |
| 998 | * NC_PSPOLL_RPC |
| 999 | */ |
| 1000 | static int |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1001 | 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] | 1002 | { |
| 1003 | struct lyxml_elem *xml = NULL; |
| 1004 | NC_MSG_TYPE msgtype; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1005 | struct nc_server_reply *reply = NULL; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1006 | int ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1007 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1008 | if (!session) { |
| 1009 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1010 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1011 | } else if (!rpc) { |
| 1012 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1013 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1014 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1015 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1016 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | msgtype = nc_read_msg(session, &xml); |
| 1020 | |
| 1021 | switch (msgtype) { |
| 1022 | case NC_MSG_RPC: |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1023 | *rpc = calloc(1, sizeof **rpc); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1024 | if (!*rpc) { |
| 1025 | ERRMEM; |
| 1026 | goto error; |
| 1027 | } |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1028 | |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1029 | ly_errno = LY_SUCCESS; |
Michal Vasko | 41adf39 | 2017-01-17 10:39:04 +0100 | [diff] [blame] | 1030 | (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, |
| 1031 | 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] | 1032 | if (!(*rpc)->tree) { |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1033 | /* parsing RPC failed */ |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 1034 | reply = nc_server_reply_err(nc_err_libyang()); |
Radek Krejci | 844662e | 2016-04-13 16:54:43 +0200 | [diff] [blame] | 1035 | ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1036 | nc_server_reply_free(reply); |
| 1037 | if (ret == -1) { |
| 1038 | ERR("Session %u: failed to write reply.", session->id); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1039 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1040 | ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC; |
| 1041 | } else { |
| 1042 | ret = NC_PSPOLL_RPC; |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1043 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1044 | (*rpc)->root = xml; |
| 1045 | break; |
| 1046 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1047 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1048 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1049 | goto error; |
| 1050 | case NC_MSG_REPLY: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 1051 | ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1052 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1053 | goto error; |
| 1054 | case NC_MSG_NOTIF: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 1055 | ERR("Session %u: received <notification> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1056 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1057 | goto error; |
| 1058 | default: |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1059 | /* NC_MSG_ERROR, |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1060 | * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg() |
| 1061 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1062 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1063 | break; |
| 1064 | } |
| 1065 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1066 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1067 | |
| 1068 | error: |
| 1069 | /* cleanup */ |
| 1070 | lyxml_free(server_opts.ctx, xml); |
| 1071 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1072 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1075 | API void |
| 1076 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1077 | { |
| 1078 | global_rpc_clb = clb; |
| 1079 | } |
| 1080 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1081 | API NC_MSG_TYPE |
| 1082 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1083 | { |
| 1084 | NC_MSG_TYPE result = NC_MSG_NOTIF; |
| 1085 | int ret; |
| 1086 | |
| 1087 | /* check parameters */ |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1088 | if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1089 | ERRARG("session"); |
| 1090 | return NC_MSG_ERROR; |
| 1091 | } else if (!notif || !notif->tree || !notif->eventtime) { |
| 1092 | ERRARG("notif"); |
| 1093 | return NC_MSG_ERROR; |
| 1094 | } |
| 1095 | |
| 1096 | /* 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] | 1097 | ret = nc_session_lock(session, timeout, __func__); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1098 | if (ret < 0) { |
| 1099 | return NC_MSG_ERROR; |
| 1100 | } else if (!ret) { |
| 1101 | return NC_MSG_WOULDBLOCK; |
| 1102 | } |
| 1103 | |
| 1104 | ret = nc_write_msg(session, NC_MSG_NOTIF, notif); |
| 1105 | if (ret == -1) { |
| 1106 | ERR("Session %u: failed to write notification.", session->id); |
| 1107 | result = NC_MSG_ERROR; |
| 1108 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1109 | |
| 1110 | nc_session_unlock(session, timeout, __func__); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1111 | |
| 1112 | return result; |
| 1113 | } |
| 1114 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1115 | /* must be called holding the session lock! |
| 1116 | * returns: NC_PSPOLL_ERROR, |
| 1117 | * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR, |
| 1118 | * NC_PSPOLL_REPLY_ERROR, |
| 1119 | * 0 |
| 1120 | */ |
| 1121 | static int |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1122 | 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] | 1123 | { |
| 1124 | nc_rpc_clb clb; |
| 1125 | struct nc_server_reply *reply; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1126 | struct lys_node *rpc_act = NULL; |
| 1127 | struct lyd_node *next, *elem; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1128 | int ret = 0, r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1129 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1130 | if (!rpc) { |
| 1131 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1132 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1135 | if (rpc->tree->schema->nodetype == LYS_RPC) { |
| 1136 | /* RPC */ |
| 1137 | rpc_act = rpc->tree->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1138 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1139 | /* action */ |
| 1140 | LY_TREE_DFS_BEGIN(rpc->tree, next, elem) { |
| 1141 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1142 | rpc_act = elem->schema; |
| 1143 | break; |
| 1144 | } |
| 1145 | LY_TREE_DFS_END(rpc->tree, next, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1146 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1147 | if (!rpc_act) { |
| 1148 | ERRINT; |
| 1149 | return NC_PSPOLL_ERROR; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | if (!rpc_act->priv) { |
| 1154 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1155 | 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] | 1156 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1157 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1158 | reply = clb(rpc->tree, session); |
| 1159 | } |
| 1160 | |
| 1161 | if (!reply) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1162 | 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] | 1163 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1164 | r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply); |
| 1165 | if (reply->type == NC_RPL_ERROR) { |
| 1166 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1167 | } |
| 1168 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1169 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1170 | if (r == -1) { |
| 1171 | ERR("Session %u: failed to write reply.", session->id); |
| 1172 | ret |= NC_PSPOLL_ERROR; |
| 1173 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1174 | |
| 1175 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1176 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1177 | session->status = NC_STATUS_INVALID; |
| 1178 | } |
| 1179 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1180 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1181 | } |
| 1182 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1183 | /* session must be running and session lock held! |
| 1184 | * returns: NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR, (msg filled) |
| 1185 | * NC_PSPOLL_ERROR, (msg filled) |
| 1186 | * NC_PSPOLL_TIMEOUT, |
| 1187 | * NC_PSPOLL_RPC (some application data available), |
| 1188 | * NC_PSPOLL_SSH_CHANNEL, |
| 1189 | * NC_PSPOLL_SSH_MSG |
| 1190 | */ |
| 1191 | static int |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1192 | 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] | 1193 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1194 | struct pollfd pfd; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1195 | int r, ret; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1196 | #ifdef NC_ENABLED_SSH |
| 1197 | struct nc_session *new; |
| 1198 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1199 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1200 | /* check timeout first */ |
| 1201 | 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] | 1202 | && (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1203 | sprintf(msg, "session idle timeout elapsed"); |
| 1204 | session->status = NC_STATUS_INVALID; |
| 1205 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1206 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1207 | } |
| 1208 | |
| 1209 | switch (session->ti_type) { |
| 1210 | #ifdef NC_ENABLED_SSH |
| 1211 | case NC_TI_LIBSSH: |
| 1212 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1213 | if (r == SSH_EOF) { |
| 1214 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1215 | session->status = NC_STATUS_INVALID; |
| 1216 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1217 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1218 | } else if (r == SSH_ERROR) { |
| 1219 | 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] | 1220 | session->status = NC_STATUS_INVALID; |
| 1221 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1222 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1223 | } else if (!r) { |
| 1224 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1225 | /* new SSH message */ |
| 1226 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1227 | if (session->ti.libssh.next) { |
| 1228 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
| 1229 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel |
| 1230 | && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1231 | /* new NETCONF SSH channel */ |
| 1232 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1233 | break; |
| 1234 | } |
| 1235 | } |
| 1236 | if (new != session) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1237 | break; |
| 1238 | } |
| 1239 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1240 | |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1241 | /* just some SSH message */ |
| 1242 | ret = NC_PSPOLL_SSH_MSG; |
| 1243 | } else { |
| 1244 | ret = NC_PSPOLL_TIMEOUT; |
| 1245 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1246 | } else { |
| 1247 | /* we have some application data */ |
| 1248 | ret = NC_PSPOLL_RPC; |
| 1249 | } |
| 1250 | break; |
| 1251 | #endif |
| 1252 | #ifdef NC_ENABLED_TLS |
| 1253 | case NC_TI_OPENSSL: |
| 1254 | r = SSL_pending(session->ti.tls); |
| 1255 | if (!r) { |
| 1256 | /* no data pending in the SSL buffer, poll fd */ |
| 1257 | pfd.fd = SSL_get_rfd(session->ti.tls); |
| 1258 | if (pfd.fd < 0) { |
| 1259 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1260 | ret = NC_PSPOLL_ERROR; |
| 1261 | break; |
| 1262 | } |
| 1263 | pfd.events = POLLIN; |
| 1264 | pfd.revents = 0; |
| 1265 | r = poll(&pfd, 1, 0); |
| 1266 | |
| 1267 | if ((r < 0) && (errno != EINTR)) { |
| 1268 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1269 | session->status = NC_STATUS_INVALID; |
| 1270 | ret = NC_PSPOLL_ERROR; |
| 1271 | } else if (r > 0) { |
| 1272 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1273 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1274 | session->status = NC_STATUS_INVALID; |
| 1275 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1276 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1277 | } else if (pfd.revents & POLLERR) { |
| 1278 | sprintf(msg, "communication socket error"); |
| 1279 | session->status = NC_STATUS_INVALID; |
| 1280 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1281 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1282 | } else { |
| 1283 | ret = NC_PSPOLL_RPC; |
| 1284 | } |
| 1285 | } else { |
| 1286 | ret = NC_PSPOLL_TIMEOUT; |
| 1287 | } |
| 1288 | } else { |
| 1289 | ret = NC_PSPOLL_RPC; |
| 1290 | } |
| 1291 | break; |
| 1292 | #endif |
| 1293 | case NC_TI_FD: |
| 1294 | pfd.fd = session->ti.fd.in; |
| 1295 | pfd.events = POLLIN; |
| 1296 | pfd.revents = 0; |
| 1297 | r = poll(&pfd, 1, 0); |
| 1298 | |
| 1299 | if ((r < 0) && (errno != EINTR)) { |
| 1300 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1301 | session->status = NC_STATUS_INVALID; |
| 1302 | ret = NC_PSPOLL_ERROR; |
| 1303 | } else if (r > 0) { |
| 1304 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1305 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1306 | session->status = NC_STATUS_INVALID; |
| 1307 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1308 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1309 | } else if (pfd.revents & POLLERR) { |
| 1310 | sprintf(msg, "communication socket error"); |
| 1311 | session->status = NC_STATUS_INVALID; |
| 1312 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1313 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1314 | } else { |
| 1315 | ret = NC_PSPOLL_RPC; |
| 1316 | } |
| 1317 | } else { |
| 1318 | ret = NC_PSPOLL_TIMEOUT; |
| 1319 | } |
| 1320 | break; |
| 1321 | case NC_TI_NONE: |
| 1322 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1323 | ret = NC_PSPOLL_ERROR; |
| 1324 | break; |
| 1325 | } |
| 1326 | |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | API int |
| 1331 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1332 | { |
| 1333 | int ret, r; |
| 1334 | uint8_t q_id; |
| 1335 | uint16_t i, j; |
| 1336 | char msg[256]; |
| 1337 | struct timespec ts_timeout, ts_cur; |
| 1338 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1339 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1340 | struct nc_server_rpc *rpc = NULL; |
| 1341 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1342 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1343 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1344 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1347 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1348 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1349 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1350 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1351 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1352 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1353 | nc_ps_unlock(ps, q_id, __func__); |
| 1354 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1355 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1356 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1357 | /* fill timespecs */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1358 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1359 | if (timeout > -1) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1360 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1361 | nc_addtimespec(&ts_timeout, timeout); |
| 1362 | } |
| 1363 | |
| 1364 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1365 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1366 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1367 | if (ps->last_event_session == ps->session_count - 1) { |
| 1368 | i = j = 0; |
| 1369 | } else { |
| 1370 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1371 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1372 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1373 | cur_ps_session = ps->sessions[i]; |
| 1374 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1375 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1376 | /* SESSION LOCK */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1377 | r = nc_session_lock(cur_session, 0, __func__); |
| 1378 | if (r == -1) { |
| 1379 | ret = NC_PSPOLL_ERROR; |
| 1380 | } else if (r == 1) { |
| 1381 | /* 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] | 1382 | switch (cur_ps_session->state) { |
| 1383 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1384 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1385 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1386 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1387 | |
| 1388 | ret = nc_ps_poll_session(cur_session, ts_cur.tv_sec, msg); |
| 1389 | switch (ret) { |
| 1390 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1391 | ERR("Session %u: %s.", cur_session->id, msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1392 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1393 | break; |
| 1394 | case NC_PSPOLL_ERROR: |
| 1395 | ERR("Session %u: %s.", cur_session->id, msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1396 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1397 | break; |
| 1398 | case NC_PSPOLL_TIMEOUT: |
| 1399 | #ifdef NC_ENABLED_SSH |
| 1400 | case NC_PSPOLL_SSH_CHANNEL: |
| 1401 | case NC_PSPOLL_SSH_MSG: |
| 1402 | #endif |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1403 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1404 | break; |
| 1405 | case NC_PSPOLL_RPC: |
| 1406 | /* let's keep the state busy, we are not done with this session */ |
| 1407 | break; |
| 1408 | } |
| 1409 | } else { |
| 1410 | /* session is not fine, let the caller know */ |
| 1411 | ret = NC_PSPOLL_SESSION_TERM; |
| 1412 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1413 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1414 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1415 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1416 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1417 | break; |
| 1418 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1419 | /* it definitely should not be busy because we have the lock */ |
| 1420 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1421 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1422 | break; |
| 1423 | case NC_PS_STATE_INVALID: |
| 1424 | /* we got it locked, but it will be freed, let it be */ |
| 1425 | ret = NC_PSPOLL_TIMEOUT; |
| 1426 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1427 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1428 | |
| 1429 | /* keep the session locked only in this one case */ |
| 1430 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1431 | /* SESSION UNLOCK */ |
| 1432 | nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1433 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1434 | } else { |
| 1435 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1436 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1437 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1438 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1439 | /* something happened */ |
| 1440 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1441 | break; |
| 1442 | } |
| 1443 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1444 | if (i == ps->session_count - 1) { |
| 1445 | i = 0; |
| 1446 | } else { |
| 1447 | ++i; |
| 1448 | } |
| 1449 | } while (i != j); |
| 1450 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1451 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1452 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1453 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1454 | /* update current time */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1455 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1456 | |
| 1457 | if ((timeout > -1) && (nc_difftimespec(&ts_cur, &ts_timeout) < 1)) { |
| 1458 | /* final timeout */ |
| 1459 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1460 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1461 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1462 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1463 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1464 | /* do we want to return the session? */ |
| 1465 | switch (ret) { |
| 1466 | case NC_PSPOLL_RPC: |
| 1467 | case NC_PSPOLL_SESSION_TERM: |
| 1468 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1469 | #ifdef NC_ENABLED_SSH |
| 1470 | case NC_PSPOLL_SSH_CHANNEL: |
| 1471 | case NC_PSPOLL_SSH_MSG: |
| 1472 | #endif |
| 1473 | if (session) { |
| 1474 | *session = cur_session; |
| 1475 | } |
| 1476 | ps->last_event_session = i; |
| 1477 | break; |
| 1478 | default: |
| 1479 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1480 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1481 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1482 | /* PS UNLOCK */ |
| 1483 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1484 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1485 | /* we have some data available and the session is locked */ |
| 1486 | if (ret == NC_PSPOLL_RPC) { |
| 1487 | ret = nc_server_recv_rpc(cur_session, &rpc); |
| 1488 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1489 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1490 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1491 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1492 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1493 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1494 | } |
| 1495 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1496 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1497 | |
| 1498 | /* process RPC, not needed afterwards */ |
| 1499 | ret |= nc_server_send_reply(cur_session, rpc); |
| 1500 | nc_server_rpc_free(rpc, server_opts.ctx); |
| 1501 | |
| 1502 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1503 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1504 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1505 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1506 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1507 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1508 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1509 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1510 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1511 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1512 | |
| 1513 | /* SESSION UNLOCK */ |
| 1514 | nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1517 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1520 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1521 | 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] | 1522 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1523 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1524 | uint16_t i; |
| 1525 | struct nc_session *session; |
| 1526 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1527 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1528 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1529 | return; |
| 1530 | } |
| 1531 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1532 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1533 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1534 | return; |
| 1535 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1536 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1537 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1538 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1539 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1540 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1541 | } |
| 1542 | free(ps->sessions); |
| 1543 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1544 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1545 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1546 | } else { |
| 1547 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1548 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1549 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1550 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1551 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1552 | continue; |
| 1553 | } |
| 1554 | |
| 1555 | ++i; |
| 1556 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1557 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1558 | |
| 1559 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1560 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1563 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1564 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1565 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1566 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1567 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1568 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1569 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1570 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1571 | if (!name) { |
| 1572 | ERRARG("name"); |
| 1573 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1576 | /* BIND LOCK */ |
| 1577 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1578 | |
| 1579 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1580 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1581 | |
| 1582 | /* check name uniqueness */ |
| 1583 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1584 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1585 | ERR("Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1586 | ret = -1; |
| 1587 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1591 | ++server_opts.endpt_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1592 | 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] | 1593 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1594 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1595 | ret = -1; |
| 1596 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1597 | } |
| 1598 | 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] | 1599 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1600 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1601 | 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] | 1602 | if (!server_opts.binds) { |
| 1603 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1604 | ret = -1; |
| 1605 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1606 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1607 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1608 | server_opts.binds[server_opts.endpt_count - 1].address = NULL; |
| 1609 | server_opts.binds[server_opts.endpt_count - 1].port = 0; |
| 1610 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 1611 | server_opts.binds[server_opts.endpt_count - 1].pollin = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1612 | |
| 1613 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1614 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1615 | case NC_TI_LIBSSH: |
| 1616 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1617 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 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 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
| 1623 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 1624 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3; |
| 1625 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10; |
| 1626 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1627 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1628 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1629 | case NC_TI_OPENSSL: |
| 1630 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 1631 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 1632 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1633 | ret = -1; |
| 1634 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1635 | } |
| 1636 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1637 | #endif |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1638 | default: |
| 1639 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1640 | ret = -1; |
| 1641 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1642 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1643 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1644 | cleanup: |
| 1645 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1646 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1647 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1648 | /* BIND UNLOCK */ |
| 1649 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1650 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1651 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1654 | int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1655 | 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] | 1656 | { |
| 1657 | struct nc_endpt *endpt; |
| 1658 | struct nc_bind *bind = NULL; |
| 1659 | uint16_t i; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1660 | int sock = -1, set_addr, ret = 0; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1661 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1662 | if (!endpt_name) { |
| 1663 | ERRARG("endpt_name"); |
| 1664 | return -1; |
| 1665 | } else if ((!address && !port) || (address && port)) { |
| 1666 | ERRARG("address and port"); |
| 1667 | return -1; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1670 | if (address) { |
| 1671 | set_addr = 1; |
| 1672 | } else { |
| 1673 | set_addr = 0; |
| 1674 | } |
| 1675 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1676 | /* BIND LOCK */ |
| 1677 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1678 | |
| 1679 | /* ENDPT LOCK */ |
| 1680 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1681 | if (!endpt) { |
Michal Vasko | 4e455dd | 2017-03-21 15:33:43 +0100 | [diff] [blame] | 1682 | /* BIND UNLOCK */ |
| 1683 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1684 | return -1; |
| 1685 | } |
| 1686 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1687 | bind = &server_opts.binds[i]; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1688 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1689 | if (set_addr) { |
| 1690 | port = bind->port; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1691 | } else { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1692 | address = bind->address; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1695 | /* we have all the information we need to create a listening socket */ |
| 1696 | if (address && port) { |
| 1697 | /* create new socket, close the old one */ |
| 1698 | sock = nc_sock_listen(address, port); |
| 1699 | if (sock == -1) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1700 | ret = -1; |
| 1701 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | if (bind->sock > -1) { |
| 1705 | close(bind->sock); |
| 1706 | } |
| 1707 | bind->sock = sock; |
| 1708 | } /* else we are just setting address or port */ |
| 1709 | |
| 1710 | if (set_addr) { |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1711 | lydict_remove(server_opts.ctx, bind->address); |
| 1712 | bind->address = lydict_insert(server_opts.ctx, address, 0); |
| 1713 | } else { |
| 1714 | bind->port = port; |
| 1715 | } |
| 1716 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1717 | if (sock > -1) { |
| 1718 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1719 | 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] | 1720 | #elif defined(NC_ENABLED_SSH) |
| 1721 | VRB("Listening on %s:%u for SSH connections.", address, port); |
| 1722 | #else |
| 1723 | VRB("Listening on %s:%u for TLS connections.", address, port); |
| 1724 | #endif |
| 1725 | } |
| 1726 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1727 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1728 | /* ENDPT UNLOCK */ |
| 1729 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1730 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1731 | /* BIND UNLOCK */ |
| 1732 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1733 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1734 | return ret; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1737 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1738 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 1739 | { |
| 1740 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 1741 | } |
| 1742 | |
| 1743 | API int |
| 1744 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 1745 | { |
| 1746 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 1747 | } |
| 1748 | |
| 1749 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1750 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1751 | { |
| 1752 | uint32_t i; |
| 1753 | int ret = -1; |
| 1754 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1755 | /* BIND LOCK */ |
| 1756 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1757 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1758 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1759 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1760 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1761 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1762 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1763 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1764 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1765 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1766 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1767 | case NC_TI_LIBSSH: |
| 1768 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1769 | free(server_opts.endpts[i].opts.ssh); |
| 1770 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1771 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1772 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1773 | case NC_TI_OPENSSL: |
| 1774 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1775 | free(server_opts.endpts[i].opts.tls); |
| 1776 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1777 | #endif |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1778 | default: |
| 1779 | ERRINT; |
| 1780 | /* won't get here ...*/ |
| 1781 | break; |
| 1782 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1783 | ret = 0; |
| 1784 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1785 | free(server_opts.endpts); |
| 1786 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1787 | |
| 1788 | /* remove all binds */ |
| 1789 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1790 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 1791 | if (server_opts.binds[i].sock > -1) { |
| 1792 | close(server_opts.binds[i].sock); |
| 1793 | } |
| 1794 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1795 | free(server_opts.binds); |
| 1796 | server_opts.binds = NULL; |
| 1797 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1798 | server_opts.endpt_count = 0; |
| 1799 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1800 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1801 | /* 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] | 1802 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1803 | 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] | 1804 | /* remove endpt */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1805 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1806 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1807 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1808 | case NC_TI_LIBSSH: |
| 1809 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 1810 | free(server_opts.endpts[i].opts.ssh); |
| 1811 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1812 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1813 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1814 | case NC_TI_OPENSSL: |
| 1815 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 1816 | free(server_opts.endpts[i].opts.tls); |
| 1817 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1818 | #endif |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1819 | default: |
| 1820 | ERRINT; |
| 1821 | break; |
| 1822 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1823 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1824 | /* remove bind(s) */ |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1825 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 1826 | if (server_opts.binds[i].sock > -1) { |
| 1827 | close(server_opts.binds[i].sock); |
| 1828 | } |
| 1829 | |
| 1830 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1831 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 1832 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 1833 | free(server_opts.binds); |
| 1834 | server_opts.binds = NULL; |
| 1835 | free(server_opts.endpts); |
| 1836 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 1837 | } else if (i < server_opts.endpt_count) { |
| 1838 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 1839 | 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] | 1840 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1841 | |
| 1842 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 1843 | if (name) { |
| 1844 | break; |
| 1845 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1846 | } |
| 1847 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1850 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1851 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1852 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1853 | /* BIND UNLOCK */ |
| 1854 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1855 | |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1859 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1860 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1861 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1862 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1863 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1864 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1865 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1866 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1867 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1868 | if (!server_opts.ctx) { |
| 1869 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1870 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1871 | } else if (!session) { |
| 1872 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1873 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1874 | } |
| 1875 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1876 | /* BIND LOCK */ |
| 1877 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1878 | |
| 1879 | if (!server_opts.endpt_count) { |
Michal Vasko | 863a6e9 | 2016-07-28 14:27:41 +0200 | [diff] [blame] | 1880 | ERR("No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1881 | /* BIND UNLOCK */ |
| 1882 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1883 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1884 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1885 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1886 | 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] | 1887 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1888 | /* BIND UNLOCK */ |
| 1889 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1890 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 1891 | if (!ret) { |
| 1892 | return NC_MSG_WOULDBLOCK; |
| 1893 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1894 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1895 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1896 | |
| 1897 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 1898 | /* ENDPT READ LOCK */ |
| 1899 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 1900 | |
| 1901 | /* BIND UNLOCK */ |
| 1902 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 1903 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1904 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1905 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1906 | *session = nc_new_session(0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 1907 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1908 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1909 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1910 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1911 | msgtype = NC_MSG_ERROR; |
| 1912 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1913 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1914 | (*session)->status = NC_STATUS_STARTING; |
| 1915 | (*session)->side = NC_SERVER; |
| 1916 | (*session)->ctx = server_opts.ctx; |
| 1917 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 1918 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
| 1919 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1920 | |
| 1921 | /* transport lock */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1922 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1923 | pthread_cond_init((*session)->ti_cond, NULL); |
| 1924 | *(*session)->ti_inuse = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1925 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1926 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1927 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1928 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 1929 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 1930 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1931 | if (ret < 0) { |
| 1932 | msgtype = NC_MSG_ERROR; |
| 1933 | goto cleanup; |
| 1934 | } else if (!ret) { |
| 1935 | msgtype = NC_MSG_WOULDBLOCK; |
| 1936 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1937 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1938 | } else |
| 1939 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1940 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1941 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 1942 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 1943 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1944 | if (ret < 0) { |
| 1945 | msgtype = NC_MSG_ERROR; |
| 1946 | goto cleanup; |
| 1947 | } else if (!ret) { |
| 1948 | msgtype = NC_MSG_WOULDBLOCK; |
| 1949 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1950 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1951 | } else |
| 1952 | #endif |
| 1953 | { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1954 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1955 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1956 | msgtype = NC_MSG_ERROR; |
| 1957 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1960 | (*session)->data = NULL; |
| 1961 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1962 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1963 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1964 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1965 | /* assign new SID atomically */ |
| 1966 | /* LOCK */ |
| 1967 | pthread_spin_lock(&server_opts.sid_lock); |
| 1968 | (*session)->id = server_opts.new_session_id++; |
| 1969 | /* UNLOCK */ |
| 1970 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1971 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1972 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1973 | msgtype = nc_handshake(*session); |
| 1974 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1975 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1976 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1977 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1978 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1979 | |
| 1980 | nc_gettimespec_mono(&ts_cur); |
| 1981 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 1982 | nc_gettimespec_real(&ts_cur); |
| 1983 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1984 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1985 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1986 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1987 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1988 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1989 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1990 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1991 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1992 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1993 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1994 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1997 | API int |
| 1998 | nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti) |
| 1999 | { |
| 2000 | uint16_t i; |
| 2001 | |
| 2002 | if (!name) { |
| 2003 | ERRARG("name"); |
| 2004 | return -1; |
| 2005 | } else if (!ti) { |
| 2006 | ERRARG("ti"); |
| 2007 | return -1; |
| 2008 | } |
| 2009 | |
| 2010 | /* WRITE LOCK */ |
| 2011 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2012 | |
| 2013 | /* check name uniqueness */ |
| 2014 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2015 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2016 | ERR("Call Home client \"%s\" already exists.", name); |
| 2017 | /* WRITE UNLOCK */ |
| 2018 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2019 | return -1; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | ++server_opts.ch_client_count; |
| 2024 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2025 | if (!server_opts.ch_clients) { |
| 2026 | ERRMEM; |
| 2027 | /* WRITE UNLOCK */ |
| 2028 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2029 | return -1; |
| 2030 | } |
| 2031 | server_opts.ch_clients[server_opts.ch_client_count - 1].name = lydict_insert(server_opts.ctx, name, 0); |
| 2032 | server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2033 | server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL; |
| 2034 | 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] | 2035 | |
| 2036 | switch (ti) { |
| 2037 | #ifdef NC_ENABLED_SSH |
| 2038 | case NC_TI_LIBSSH: |
| 2039 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2040 | if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) { |
| 2041 | ERRMEM; |
| 2042 | /* WRITE UNLOCK */ |
| 2043 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2044 | return -1; |
| 2045 | } |
| 2046 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods = |
| 2047 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 2048 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3; |
| 2049 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10; |
| 2050 | break; |
| 2051 | #endif |
| 2052 | #ifdef NC_ENABLED_TLS |
| 2053 | case NC_TI_OPENSSL: |
| 2054 | server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2055 | if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) { |
| 2056 | ERRMEM; |
| 2057 | /* WRITE UNLOCK */ |
| 2058 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2059 | return -1; |
| 2060 | } |
| 2061 | break; |
| 2062 | #endif |
| 2063 | default: |
| 2064 | ERRINT; |
| 2065 | /* WRITE UNLOCK */ |
| 2066 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2067 | return -1; |
| 2068 | } |
| 2069 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2070 | server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0; |
| 2071 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2072 | /* set CH default options */ |
| 2073 | server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED; |
| 2074 | server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3; |
| 2075 | |
| 2076 | pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL); |
| 2077 | |
| 2078 | /* WRITE UNLOCK */ |
| 2079 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2080 | |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2085 | nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2086 | { |
| 2087 | uint16_t i, j; |
| 2088 | int ret = -1; |
| 2089 | |
| 2090 | /* WRITE LOCK */ |
| 2091 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2092 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2093 | if (!name && !ti) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2094 | /* remove all CH clients */ |
| 2095 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2096 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2097 | |
| 2098 | /* remove all endpoints */ |
| 2099 | for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) { |
| 2100 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name); |
| 2101 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address); |
| 2102 | } |
| 2103 | free(server_opts.ch_clients[i].ch_endpts); |
| 2104 | |
| 2105 | switch (server_opts.ch_clients[i].ti) { |
| 2106 | #ifdef NC_ENABLED_SSH |
| 2107 | case NC_TI_LIBSSH: |
| 2108 | nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh); |
| 2109 | free(server_opts.ch_clients[i].opts.ssh); |
| 2110 | break; |
| 2111 | #endif |
| 2112 | #ifdef NC_ENABLED_TLS |
| 2113 | case NC_TI_OPENSSL: |
| 2114 | nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls); |
| 2115 | free(server_opts.ch_clients[i].opts.tls); |
| 2116 | break; |
| 2117 | #endif |
| 2118 | default: |
| 2119 | ERRINT; |
| 2120 | /* won't get here ...*/ |
| 2121 | break; |
| 2122 | } |
| 2123 | |
| 2124 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2125 | |
| 2126 | ret = 0; |
| 2127 | } |
| 2128 | free(server_opts.ch_clients); |
| 2129 | server_opts.ch_clients = NULL; |
| 2130 | |
| 2131 | server_opts.ch_client_count = 0; |
| 2132 | |
| 2133 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2134 | /* remove one client with endpoint(s) or all clients using one protocol */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2135 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2136 | 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] | 2137 | /* remove endpt */ |
| 2138 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name); |
| 2139 | |
| 2140 | switch (server_opts.ch_clients[i].ti) { |
| 2141 | #ifdef NC_ENABLED_SSH |
| 2142 | case NC_TI_LIBSSH: |
| 2143 | nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh); |
| 2144 | free(server_opts.ch_clients[i].opts.ssh); |
| 2145 | break; |
| 2146 | #endif |
| 2147 | #ifdef NC_ENABLED_TLS |
| 2148 | case NC_TI_OPENSSL: |
| 2149 | nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls); |
| 2150 | free(server_opts.ch_clients[i].opts.tls); |
| 2151 | break; |
| 2152 | #endif |
| 2153 | default: |
| 2154 | ERRINT; |
| 2155 | break; |
| 2156 | } |
| 2157 | |
| 2158 | /* remove all endpoints */ |
| 2159 | for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) { |
| 2160 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name); |
| 2161 | lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address); |
| 2162 | } |
| 2163 | free(server_opts.ch_clients[i].ch_endpts); |
| 2164 | |
| 2165 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2166 | |
| 2167 | /* move last client and endpoint(s) to the empty space */ |
| 2168 | --server_opts.ch_client_count; |
| 2169 | if (i < server_opts.ch_client_count) { |
| 2170 | memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count], |
| 2171 | sizeof *server_opts.ch_clients); |
| 2172 | } else if (!server_opts.ch_client_count) { |
| 2173 | free(server_opts.ch_clients); |
| 2174 | server_opts.ch_clients = NULL; |
| 2175 | } |
| 2176 | |
| 2177 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2178 | if (name) { |
| 2179 | break; |
| 2180 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | /* WRITE UNLOCK */ |
| 2186 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2187 | |
| 2188 | return ret; |
| 2189 | } |
| 2190 | |
| 2191 | API int |
| 2192 | nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name) |
| 2193 | { |
| 2194 | uint16_t i; |
| 2195 | struct nc_ch_client *client; |
| 2196 | |
| 2197 | if (!client_name) { |
| 2198 | ERRARG("client_name"); |
| 2199 | return -1; |
| 2200 | } else if (!endpt_name) { |
| 2201 | ERRARG("endpt_name"); |
| 2202 | return -1; |
| 2203 | } |
| 2204 | |
| 2205 | /* LOCK */ |
| 2206 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2207 | if (!client) { |
| 2208 | return -1; |
| 2209 | } |
| 2210 | |
| 2211 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2212 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2213 | ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name); |
| 2214 | /* UNLOCK */ |
| 2215 | nc_server_ch_client_unlock(client); |
| 2216 | return -1; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | ++client->ch_endpt_count; |
| 2221 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2222 | if (!client->ch_endpts) { |
| 2223 | ERRMEM; |
| 2224 | /* UNLOCK */ |
| 2225 | nc_server_ch_client_unlock(client); |
| 2226 | return -1; |
| 2227 | } |
| 2228 | |
| 2229 | client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0); |
| 2230 | client->ch_endpts[client->ch_endpt_count - 1].address = NULL; |
| 2231 | client->ch_endpts[client->ch_endpt_count - 1].port = 0; |
| 2232 | |
| 2233 | /* UNLOCK */ |
| 2234 | nc_server_ch_client_unlock(client); |
| 2235 | |
| 2236 | return 0; |
| 2237 | } |
| 2238 | |
| 2239 | API int |
| 2240 | nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name) |
| 2241 | { |
| 2242 | uint16_t i; |
| 2243 | int ret = -1; |
| 2244 | struct nc_ch_client *client; |
| 2245 | |
| 2246 | if (!client_name) { |
| 2247 | ERRARG("client_name"); |
| 2248 | return -1; |
| 2249 | } |
| 2250 | |
| 2251 | /* LOCK */ |
| 2252 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2253 | if (!client) { |
| 2254 | return -1; |
| 2255 | } |
| 2256 | |
| 2257 | if (!endpt_name) { |
| 2258 | /* remove all endpoints */ |
| 2259 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2260 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2261 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2262 | } |
| 2263 | free(client->ch_endpts); |
| 2264 | client->ch_endpts = NULL; |
| 2265 | client->ch_endpt_count = 0; |
| 2266 | |
| 2267 | ret = 0; |
| 2268 | } else { |
| 2269 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2270 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2271 | lydict_remove(server_opts.ctx, client->ch_endpts[i].name); |
| 2272 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2273 | |
Michal Vasko | 4f92101 | 2016-10-20 14:07:45 +0200 | [diff] [blame] | 2274 | /* move last endpoint to the empty space */ |
| 2275 | --client->ch_endpt_count; |
| 2276 | if (i < client->ch_endpt_count) { |
| 2277 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2278 | } else if (!server_opts.ch_client_count) { |
| 2279 | free(server_opts.ch_clients); |
| 2280 | server_opts.ch_clients = NULL; |
| 2281 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2282 | |
Michal Vasko | 4f92101 | 2016-10-20 14:07:45 +0200 | [diff] [blame] | 2283 | ret = 0; |
| 2284 | break; |
| 2285 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | /* UNLOCK */ |
| 2290 | nc_server_ch_client_unlock(client); |
| 2291 | |
| 2292 | return ret; |
| 2293 | } |
| 2294 | |
| 2295 | API int |
| 2296 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 2297 | { |
| 2298 | uint16_t i; |
| 2299 | int ret = -1; |
| 2300 | struct nc_ch_client *client; |
| 2301 | |
| 2302 | if (!client_name) { |
| 2303 | ERRARG("client_name"); |
| 2304 | return -1; |
| 2305 | } else if (!endpt_name) { |
| 2306 | ERRARG("endpt_name"); |
| 2307 | return -1; |
| 2308 | } else if (!address) { |
| 2309 | ERRARG("address"); |
| 2310 | return -1; |
| 2311 | } |
| 2312 | |
| 2313 | /* LOCK */ |
| 2314 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2315 | if (!client) { |
| 2316 | return -1; |
| 2317 | } |
| 2318 | |
| 2319 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2320 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2321 | lydict_remove(server_opts.ctx, client->ch_endpts[i].address); |
| 2322 | client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0); |
| 2323 | |
| 2324 | ret = 0; |
| 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | /* UNLOCK */ |
| 2330 | nc_server_ch_client_unlock(client); |
| 2331 | |
| 2332 | if (ret == -1) { |
| 2333 | ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name); |
| 2334 | } |
| 2335 | |
| 2336 | return ret; |
| 2337 | } |
| 2338 | |
| 2339 | API int |
| 2340 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 2341 | { |
| 2342 | uint16_t i; |
| 2343 | int ret = -1; |
| 2344 | struct nc_ch_client *client; |
| 2345 | |
| 2346 | if (!client_name) { |
| 2347 | ERRARG("client_name"); |
| 2348 | return -1; |
| 2349 | } else if (!endpt_name) { |
| 2350 | ERRARG("endpt_name"); |
| 2351 | return -1; |
| 2352 | } else if (!port) { |
| 2353 | ERRARG("port"); |
| 2354 | return -1; |
| 2355 | } |
| 2356 | |
| 2357 | /* LOCK */ |
| 2358 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2359 | if (!client) { |
| 2360 | return -1; |
| 2361 | } |
| 2362 | |
| 2363 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2364 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2365 | client->ch_endpts[i].port = port; |
| 2366 | |
| 2367 | ret = 0; |
| 2368 | break; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | /* UNLOCK */ |
| 2373 | nc_server_ch_client_unlock(client); |
| 2374 | |
| 2375 | if (ret == -1) { |
| 2376 | ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name); |
| 2377 | } |
| 2378 | |
| 2379 | return ret; |
| 2380 | } |
| 2381 | |
| 2382 | API int |
| 2383 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 2384 | { |
| 2385 | struct nc_ch_client *client; |
| 2386 | |
| 2387 | if (!client_name) { |
| 2388 | ERRARG("client_name"); |
| 2389 | return -1; |
| 2390 | } else if (!conn_type) { |
| 2391 | ERRARG("conn_type"); |
| 2392 | return -1; |
| 2393 | } |
| 2394 | |
| 2395 | /* LOCK */ |
| 2396 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2397 | if (!client) { |
| 2398 | return -1; |
| 2399 | } |
| 2400 | |
| 2401 | if (client->conn_type != conn_type) { |
| 2402 | client->conn_type = conn_type; |
| 2403 | |
| 2404 | /* set default options */ |
| 2405 | switch (conn_type) { |
| 2406 | case NC_CH_PERSIST: |
| 2407 | client->conn.persist.idle_timeout = 86400; |
| 2408 | client->conn.persist.ka_max_wait = 30; |
| 2409 | client->conn.persist.ka_max_attempts = 3; |
| 2410 | break; |
| 2411 | case NC_CH_PERIOD: |
| 2412 | client->conn.period.idle_timeout = 300; |
| 2413 | client->conn.period.reconnect_timeout = 60; |
| 2414 | break; |
| 2415 | default: |
| 2416 | ERRINT; |
| 2417 | break; |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | /* UNLOCK */ |
| 2422 | nc_server_ch_client_unlock(client); |
| 2423 | |
| 2424 | return 0; |
| 2425 | } |
| 2426 | |
| 2427 | API int |
| 2428 | nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout) |
| 2429 | { |
| 2430 | struct nc_ch_client *client; |
| 2431 | |
| 2432 | if (!client_name) { |
| 2433 | ERRARG("client_name"); |
| 2434 | return -1; |
| 2435 | } |
| 2436 | |
| 2437 | /* LOCK */ |
| 2438 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2439 | if (!client) { |
| 2440 | return -1; |
| 2441 | } |
| 2442 | |
| 2443 | if (client->conn_type != NC_CH_PERSIST) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2444 | 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] | 2445 | /* UNLOCK */ |
| 2446 | nc_server_ch_client_unlock(client); |
| 2447 | return -1; |
| 2448 | } |
| 2449 | |
| 2450 | client->conn.persist.idle_timeout = idle_timeout; |
| 2451 | |
| 2452 | /* UNLOCK */ |
| 2453 | nc_server_ch_client_unlock(client); |
| 2454 | |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | API int |
| 2459 | nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait) |
| 2460 | { |
| 2461 | struct nc_ch_client *client; |
| 2462 | |
| 2463 | if (!client_name) { |
| 2464 | ERRARG("client_name"); |
| 2465 | return -1; |
| 2466 | } else if (!max_wait) { |
| 2467 | ERRARG("max_wait"); |
| 2468 | return -1; |
| 2469 | } |
| 2470 | |
| 2471 | /* LOCK */ |
| 2472 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2473 | if (!client) { |
| 2474 | return -1; |
| 2475 | } |
| 2476 | |
| 2477 | if (client->conn_type != NC_CH_PERSIST) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2478 | 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] | 2479 | /* UNLOCK */ |
| 2480 | nc_server_ch_client_unlock(client); |
| 2481 | return -1; |
| 2482 | } |
| 2483 | |
| 2484 | client->conn.persist.ka_max_wait = max_wait; |
| 2485 | |
| 2486 | /* UNLOCK */ |
| 2487 | nc_server_ch_client_unlock(client); |
| 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | API int |
| 2493 | nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts) |
| 2494 | { |
| 2495 | struct nc_ch_client *client; |
| 2496 | |
| 2497 | if (!client_name) { |
| 2498 | ERRARG("client_name"); |
| 2499 | return -1; |
| 2500 | } |
| 2501 | |
| 2502 | /* LOCK */ |
| 2503 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2504 | if (!client) { |
| 2505 | return -1; |
| 2506 | } |
| 2507 | |
| 2508 | if (client->conn_type != NC_CH_PERSIST) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2509 | 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] | 2510 | /* UNLOCK */ |
| 2511 | nc_server_ch_client_unlock(client); |
| 2512 | return -1; |
| 2513 | } |
| 2514 | |
| 2515 | client->conn.persist.ka_max_attempts = max_attempts; |
| 2516 | |
| 2517 | /* UNLOCK */ |
| 2518 | nc_server_ch_client_unlock(client); |
| 2519 | |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | API int |
| 2524 | nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout) |
| 2525 | { |
| 2526 | struct nc_ch_client *client; |
| 2527 | |
| 2528 | if (!client_name) { |
| 2529 | ERRARG("client_name"); |
| 2530 | return -1; |
| 2531 | } |
| 2532 | |
| 2533 | /* LOCK */ |
| 2534 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2535 | if (!client) { |
| 2536 | return -1; |
| 2537 | } |
| 2538 | |
| 2539 | if (client->conn_type != NC_CH_PERIOD) { |
Darshanajk | 2d2bf4d | 2017-09-15 21:34:47 +1000 | [diff] [blame] | 2540 | 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] | 2541 | /* UNLOCK */ |
| 2542 | nc_server_ch_client_unlock(client); |
| 2543 | return -1; |
| 2544 | } |
| 2545 | |
| 2546 | client->conn.period.idle_timeout = idle_timeout; |
| 2547 | |
| 2548 | /* UNLOCK */ |
| 2549 | nc_server_ch_client_unlock(client); |
| 2550 | |
| 2551 | return 0; |
| 2552 | } |
| 2553 | |
| 2554 | API int |
| 2555 | nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout) |
| 2556 | { |
| 2557 | struct nc_ch_client *client; |
| 2558 | |
| 2559 | if (!client_name) { |
| 2560 | ERRARG("client_name"); |
| 2561 | return -1; |
| 2562 | } else if (!reconnect_timeout) { |
| 2563 | ERRARG("reconnect_timeout"); |
| 2564 | return -1; |
| 2565 | } |
| 2566 | |
| 2567 | /* LOCK */ |
| 2568 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2569 | if (!client) { |
| 2570 | return -1; |
| 2571 | } |
| 2572 | |
| 2573 | if (client->conn_type != NC_CH_PERIOD) { |
| 2574 | ERR("Call Home client \"%s\" is not of periodic connection type."); |
| 2575 | /* UNLOCK */ |
| 2576 | nc_server_ch_client_unlock(client); |
| 2577 | return -1; |
| 2578 | } |
| 2579 | |
| 2580 | client->conn.period.reconnect_timeout = reconnect_timeout; |
| 2581 | |
| 2582 | /* UNLOCK */ |
| 2583 | nc_server_ch_client_unlock(client); |
| 2584 | |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | API int |
| 2589 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 2590 | { |
| 2591 | struct nc_ch_client *client; |
| 2592 | |
| 2593 | if (!client_name) { |
| 2594 | ERRARG("client_name"); |
| 2595 | return -1; |
| 2596 | } |
| 2597 | |
| 2598 | /* LOCK */ |
| 2599 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2600 | if (!client) { |
| 2601 | return -1; |
| 2602 | } |
| 2603 | |
| 2604 | client->start_with = start_with; |
| 2605 | |
| 2606 | /* UNLOCK */ |
| 2607 | nc_server_ch_client_unlock(client); |
| 2608 | |
| 2609 | return 0; |
| 2610 | } |
| 2611 | |
| 2612 | API int |
| 2613 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 2614 | { |
| 2615 | struct nc_ch_client *client; |
| 2616 | |
| 2617 | if (!client_name) { |
| 2618 | ERRARG("client_name"); |
| 2619 | return -1; |
| 2620 | } else if (!max_attempts) { |
| 2621 | ERRARG("max_attempts"); |
| 2622 | return -1; |
| 2623 | } |
| 2624 | |
| 2625 | /* LOCK */ |
| 2626 | client = nc_server_ch_client_lock(client_name, 0, NULL); |
| 2627 | if (!client) { |
| 2628 | return -1; |
| 2629 | } |
| 2630 | |
| 2631 | client->max_attempts = max_attempts; |
| 2632 | |
| 2633 | /* UNLOCK */ |
| 2634 | nc_server_ch_client_unlock(client); |
| 2635 | |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
| 2639 | /* client lock is expected to be held */ |
| 2640 | static NC_MSG_TYPE |
| 2641 | 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] | 2642 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2643 | NC_MSG_TYPE msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2644 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2645 | struct timespec ts_cur; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2646 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2647 | sock = nc_sock_connect(endpt->address, endpt->port); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 2648 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2649 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2652 | *session = nc_new_session(0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2653 | if (!(*session)) { |
| 2654 | ERRMEM; |
| 2655 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2656 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2657 | } |
| 2658 | (*session)->status = NC_STATUS_STARTING; |
| 2659 | (*session)->side = NC_SERVER; |
| 2660 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2661 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2662 | (*session)->host = lydict_insert(server_opts.ctx, endpt->address, 0); |
| 2663 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2664 | |
| 2665 | /* transport lock */ |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2666 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2667 | pthread_cond_init((*session)->ti_cond, NULL); |
| 2668 | *(*session)->ti_inuse = 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2669 | |
| 2670 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2671 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2672 | if (client->ti == NC_TI_LIBSSH) { |
| 2673 | (*session)->data = client->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2674 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2675 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2676 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2677 | if (ret < 0) { |
| 2678 | msgtype = NC_MSG_ERROR; |
| 2679 | goto fail; |
| 2680 | } else if (!ret) { |
| 2681 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2682 | goto fail; |
| 2683 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2684 | } else |
| 2685 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2686 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2687 | if (client->ti == NC_TI_OPENSSL) { |
| 2688 | (*session)->data = client->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 2689 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2690 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 2691 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2692 | if (ret < 0) { |
| 2693 | msgtype = NC_MSG_ERROR; |
| 2694 | goto fail; |
| 2695 | } else if (!ret) { |
| 2696 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2697 | goto fail; |
| 2698 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2699 | } else |
| 2700 | #endif |
| 2701 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2702 | ERRINT; |
| 2703 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2704 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2705 | goto fail; |
| 2706 | } |
| 2707 | |
| 2708 | /* assign new SID atomically */ |
| 2709 | /* LOCK */ |
| 2710 | pthread_spin_lock(&server_opts.sid_lock); |
| 2711 | (*session)->id = server_opts.new_session_id++; |
| 2712 | /* UNLOCK */ |
| 2713 | pthread_spin_unlock(&server_opts.sid_lock); |
| 2714 | |
| 2715 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2716 | msgtype = nc_handshake(*session); |
| 2717 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2718 | goto fail; |
| 2719 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2720 | |
| 2721 | nc_gettimespec_mono(&ts_cur); |
| 2722 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 2723 | nc_gettimespec_real(&ts_cur); |
| 2724 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2725 | (*session)->status = NC_STATUS_RUNNING; |
| 2726 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2727 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2728 | |
| 2729 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2730 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2731 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2732 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 2733 | } |
| 2734 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2735 | struct nc_ch_client_thread_arg { |
| 2736 | char *client_name; |
| 2737 | void (*session_clb)(const char *client_name, struct nc_session *new_session); |
| 2738 | }; |
| 2739 | |
| 2740 | static struct nc_ch_client * |
| 2741 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 2742 | { |
| 2743 | struct nc_ch_client *client; |
| 2744 | |
| 2745 | while (1) { |
| 2746 | /* LOCK */ |
| 2747 | client = nc_server_ch_client_lock(name, 0, NULL); |
| 2748 | if (!client) { |
| 2749 | return NULL; |
| 2750 | } |
| 2751 | if (client->ch_endpt_count) { |
| 2752 | return client; |
| 2753 | } |
| 2754 | /* no endpoints defined yet */ |
| 2755 | |
| 2756 | /* UNLOCK */ |
| 2757 | nc_server_ch_client_unlock(client); |
| 2758 | |
| 2759 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 2760 | } |
| 2761 | |
| 2762 | return NULL; |
| 2763 | } |
| 2764 | |
| 2765 | static int |
| 2766 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 2767 | { |
| 2768 | int ret; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2769 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2770 | struct timespec ts; |
| 2771 | struct nc_ch_client *client; |
| 2772 | |
| 2773 | /* session created, initialize condition */ |
| 2774 | session->opts.server.ch_lock = malloc(sizeof *session->opts.server.ch_lock); |
| 2775 | session->opts.server.ch_cond = malloc(sizeof *session->opts.server.ch_cond); |
| 2776 | if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) { |
| 2777 | ERRMEM; |
| 2778 | nc_session_free(session, NULL); |
| 2779 | return -1; |
| 2780 | } |
| 2781 | pthread_mutex_init(session->opts.server.ch_lock, NULL); |
| 2782 | pthread_cond_init(session->opts.server.ch_cond, NULL); |
| 2783 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2784 | session->flags |= NC_SESSION_CALLHOME; |
| 2785 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2786 | /* CH LOCK */ |
| 2787 | pthread_mutex_lock(session->opts.server.ch_lock); |
| 2788 | |
| 2789 | /* give the session to the user */ |
| 2790 | data->session_clb(data->client_name, session); |
| 2791 | |
| 2792 | do { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 2793 | nc_gettimespec_real(&ts); |
Michal Vasko | e39ae6b | 2017-03-14 09:03:46 +0100 | [diff] [blame] | 2794 | nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2795 | |
| 2796 | ret = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts); |
| 2797 | if (ret && (ret != ETIMEDOUT)) { |
| 2798 | ERR("Pthread condition timedwait failed (%s).", strerror(ret)); |
| 2799 | goto ch_client_remove; |
| 2800 | } |
| 2801 | |
| 2802 | /* check whether the client was not removed */ |
| 2803 | /* LOCK */ |
| 2804 | client = nc_server_ch_client_lock(data->client_name, 0, NULL); |
| 2805 | if (!client) { |
| 2806 | /* client was removed, finish thread */ |
| 2807 | VRB("Call Home client \"%s\" removed, but an established session will not be terminated.", |
| 2808 | data->client_name); |
| 2809 | goto ch_client_remove; |
| 2810 | } |
| 2811 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2812 | if (client->conn_type == NC_CH_PERSIST) { |
| 2813 | /* TODO keep-alives */ |
| 2814 | idle_timeout = client->conn.persist.idle_timeout; |
| 2815 | } else { |
| 2816 | idle_timeout = client->conn.period.idle_timeout; |
| 2817 | } |
| 2818 | |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2819 | nc_gettimespec_mono(&ts); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 2820 | 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] | 2821 | VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id); |
| 2822 | session->status = NC_STATUS_INVALID; |
| 2823 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 2824 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2825 | |
| 2826 | /* UNLOCK */ |
| 2827 | nc_server_ch_client_unlock(client); |
| 2828 | |
| 2829 | } while (session->status == NC_STATUS_RUNNING); |
| 2830 | |
| 2831 | /* CH UNLOCK */ |
| 2832 | pthread_mutex_unlock(session->opts.server.ch_lock); |
| 2833 | |
| 2834 | return 0; |
| 2835 | |
| 2836 | ch_client_remove: |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2837 | /* make the session a standard one */ |
| 2838 | pthread_cond_destroy(session->opts.server.ch_cond); |
| 2839 | free(session->opts.server.ch_cond); |
| 2840 | session->opts.server.ch_cond = NULL; |
| 2841 | |
| 2842 | session->flags &= ~NC_SESSION_CALLHOME; |
| 2843 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2844 | /* CH UNLOCK */ |
| 2845 | pthread_mutex_unlock(session->opts.server.ch_lock); |
| 2846 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2847 | pthread_mutex_destroy(session->opts.server.ch_lock); |
| 2848 | free(session->opts.server.ch_lock); |
| 2849 | session->opts.server.ch_lock = NULL; |
| 2850 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2851 | return 1; |
| 2852 | } |
| 2853 | |
| 2854 | static void * |
| 2855 | nc_ch_client_thread(void *arg) |
| 2856 | { |
| 2857 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 2858 | NC_MSG_TYPE msgtype; |
| 2859 | uint8_t cur_attempts = 0; |
| 2860 | uint16_t i; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 2861 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2862 | struct nc_ch_endpt *cur_endpt; |
| 2863 | struct nc_session *session; |
| 2864 | struct nc_ch_client *client; |
| 2865 | |
| 2866 | /* LOCK */ |
| 2867 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2868 | if (!client) { |
| 2869 | goto cleanup; |
| 2870 | } |
| 2871 | |
| 2872 | cur_endpt = &client->ch_endpts[0]; |
| 2873 | cur_endpt_name = strdup(cur_endpt->name); |
| 2874 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 2875 | VRB("Call Home client \"%s\" connecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2876 | while (1) { |
| 2877 | msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session); |
| 2878 | |
| 2879 | if (msgtype == NC_MSG_HELLO) { |
| 2880 | /* UNLOCK */ |
| 2881 | nc_server_ch_client_unlock(client); |
| 2882 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 2883 | 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] | 2884 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 2885 | goto cleanup; |
| 2886 | } |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 2887 | VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2888 | |
| 2889 | /* LOCK */ |
| 2890 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2891 | if (!client) { |
| 2892 | goto cleanup; |
| 2893 | } |
| 2894 | |
| 2895 | /* session changed status -> it was disconnected for whatever reason, |
| 2896 | * persistent connection immediately tries to reconnect, periodic waits some first */ |
| 2897 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2898 | /* UNLOCK */ |
| 2899 | nc_server_ch_client_unlock(client); |
| 2900 | |
| 2901 | /* TODO wake up sometimes to check for new notifications */ |
| 2902 | usleep(client->conn.period.reconnect_timeout * 60 * 1000000); |
| 2903 | |
| 2904 | /* LOCK */ |
| 2905 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2906 | if (!client) { |
| 2907 | goto cleanup; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | /* set next endpoint to try */ |
| 2912 | if (client->start_with == NC_CH_FIRST_LISTED) { |
| 2913 | cur_endpt = &client->ch_endpts[0]; |
| 2914 | free(cur_endpt_name); |
| 2915 | cur_endpt_name = strdup(cur_endpt->name); |
| 2916 | } /* else we keep the current one */ |
| 2917 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2918 | /* UNLOCK */ |
| 2919 | nc_server_ch_client_unlock(client); |
| 2920 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2921 | /* session was not created */ |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2922 | usleep(NC_CH_ENDPT_FAIL_WAIT * 1000); |
| 2923 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 2924 | /* LOCK */ |
| 2925 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 2926 | if (!client) { |
| 2927 | goto cleanup; |
| 2928 | } |
| 2929 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2930 | ++cur_attempts; |
| 2931 | if (cur_attempts == client->max_attempts) { |
| 2932 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2933 | if (!strcmp(client->ch_endpts[i].name, cur_endpt_name)) { |
| 2934 | break; |
| 2935 | } |
| 2936 | } |
| 2937 | if (i < client->ch_endpt_count - 1) { |
| 2938 | /* just go to the next endpoint */ |
| 2939 | cur_endpt = &client->ch_endpts[i + 1]; |
| 2940 | free(cur_endpt_name); |
| 2941 | cur_endpt_name = strdup(cur_endpt->name); |
| 2942 | } else { |
| 2943 | /* cur_endpoint was removed or is the last, either way start with the first one */ |
| 2944 | cur_endpt = &client->ch_endpts[0]; |
| 2945 | free(cur_endpt_name); |
| 2946 | cur_endpt_name = strdup(cur_endpt->name); |
| 2947 | } |
| 2948 | |
| 2949 | cur_attempts = 0; |
| 2950 | } /* else we keep the current one */ |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | cleanup: |
| 2955 | VRB("Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 2956 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2957 | free(data->client_name); |
| 2958 | free(data); |
| 2959 | return NULL; |
| 2960 | } |
| 2961 | |
| 2962 | API int |
| 2963 | nc_connect_ch_client_dispatch(const char *client_name, |
| 2964 | void (*session_clb)(const char *client_name, struct nc_session *new_session)) { |
| 2965 | int ret; |
| 2966 | pthread_t tid; |
| 2967 | struct nc_ch_client_thread_arg *arg; |
| 2968 | |
| 2969 | if (!client_name) { |
| 2970 | ERRARG("client_name"); |
| 2971 | return -1; |
| 2972 | } else if (!session_clb) { |
| 2973 | ERRARG("session_clb"); |
| 2974 | return -1; |
| 2975 | } |
| 2976 | |
| 2977 | arg = malloc(sizeof *arg); |
| 2978 | if (!arg) { |
| 2979 | ERRMEM; |
| 2980 | return -1; |
| 2981 | } |
| 2982 | arg->client_name = strdup(client_name); |
| 2983 | if (!arg->client_name) { |
| 2984 | ERRMEM; |
| 2985 | free(arg); |
| 2986 | return -1; |
| 2987 | } |
| 2988 | arg->session_clb = session_clb; |
| 2989 | |
| 2990 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 2991 | if (ret) { |
| 2992 | ERR("Creating a new thread failed (%s).", strerror(ret)); |
| 2993 | free(arg->client_name); |
| 2994 | free(arg); |
| 2995 | return -1; |
| 2996 | } |
| 2997 | /* the thread now manages arg */ |
| 2998 | |
| 2999 | pthread_detach(tid); |
| 3000 | |
| 3001 | return 0; |
| 3002 | } |
| 3003 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3004 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3005 | |
Michal Vasko | e8e0770 | 2017-03-15 10:19:30 +0100 | [diff] [blame] | 3006 | API int |
| 3007 | nc_server_endpt_count(void) |
| 3008 | { |
| 3009 | return server_opts.endpt_count; |
| 3010 | } |
| 3011 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3012 | API time_t |
| 3013 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3014 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3015 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3016 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3017 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3018 | } |
| 3019 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3020 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3021 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3022 | |
| 3023 | API void |
| 3024 | nc_session_set_notif_status(struct nc_session *session, int notif_status) |
| 3025 | { |
| 3026 | if (!session || (session->side != NC_SERVER)) { |
| 3027 | ERRARG("session"); |
| 3028 | return; |
| 3029 | } |
| 3030 | |
| 3031 | session->opts.server.ntf_status = (notif_status ? 1 : 0); |
| 3032 | } |
| 3033 | |
| 3034 | API int |
| 3035 | nc_session_get_notif_status(const struct nc_session *session) |
| 3036 | { |
| 3037 | if (!session || (session->side != NC_SERVER)) { |
| 3038 | ERRARG("session"); |
| 3039 | return 0; |
| 3040 | } |
| 3041 | |
| 3042 | return session->opts.server.ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3043 | } |