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