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