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