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 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 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 | */ |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <errno.h> |
| 18 | #include <string.h> |
| 19 | #include <poll.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <arpa/inet.h> |
| 24 | #include <unistd.h> |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 25 | #include <fcntl.h> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 26 | #include <pthread.h> |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 27 | #include <time.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 28 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 29 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 30 | #include "session_server.h" |
| 31 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 32 | struct nc_server_opts server_opts = { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 33 | .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 34 | }; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 35 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 36 | static nc_rpc_clb global_rpc_clb = NULL; |
| 37 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 38 | extern struct nc_server_ssh_opts ssh_ch_opts; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 39 | extern pthread_mutex_t ssh_ch_opts_lock; |
| 40 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 41 | extern struct nc_server_tls_opts tls_ch_opts; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 42 | extern pthread_mutex_t tls_ch_opts_lock; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 43 | |
| 44 | struct nc_endpt * |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 45 | nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 46 | { |
| 47 | uint16_t i; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 48 | struct nc_endpt *endpt = NULL; |
| 49 | |
| 50 | /* READ LOCK */ |
| 51 | pthread_rwlock_rdlock(&server_opts.endpt_array_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 52 | |
| 53 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 54 | if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 55 | endpt = &server_opts.endpts[i]; |
| 56 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 60 | if (!endpt) { |
| 61 | ERR("Endpoint \"%s\" was not found.", name); |
| 62 | /* READ UNLOCK */ |
| 63 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | /* ENDPT LOCK */ |
| 68 | pthread_mutex_lock(&endpt->endpt_lock); |
| 69 | |
| 70 | return endpt; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | nc_server_endpt_unlock(struct nc_endpt *endpt) |
| 75 | { |
| 76 | /* ENDPT UNLOCK */ |
| 77 | pthread_mutex_unlock(&endpt->endpt_lock); |
| 78 | |
| 79 | /* READ UNLOCK */ |
Michal Vasko | 27562ad | 2016-02-02 15:50:39 +0100 | [diff] [blame] | 80 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 81 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 82 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 83 | API void |
| 84 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 85 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 86 | if (!session) { |
| 87 | ERRARG("session"); |
| 88 | return; |
| 89 | } else if (!reason) { |
| 90 | ERRARG("reason"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | session->term_reason = reason; |
| 95 | } |
| 96 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 97 | int |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 98 | nc_sock_listen(const char *address, uint16_t port) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 99 | { |
| 100 | const int optVal = 1; |
| 101 | const socklen_t optLen = sizeof(optVal); |
| 102 | int is_ipv4, sock; |
| 103 | struct sockaddr_storage saddr; |
| 104 | |
| 105 | struct sockaddr_in *saddr4; |
| 106 | struct sockaddr_in6 *saddr6; |
| 107 | |
| 108 | |
| 109 | if (!strchr(address, ':')) { |
| 110 | is_ipv4 = 1; |
| 111 | } else { |
| 112 | is_ipv4 = 0; |
| 113 | } |
| 114 | |
| 115 | sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0); |
| 116 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 117 | ERR("Failed to create socket (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 118 | goto fail; |
| 119 | } |
| 120 | |
| 121 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 122 | ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 123 | goto fail; |
| 124 | } |
| 125 | |
| 126 | bzero(&saddr, sizeof(struct sockaddr_storage)); |
| 127 | if (is_ipv4) { |
| 128 | saddr4 = (struct sockaddr_in *)&saddr; |
| 129 | |
| 130 | saddr4->sin_family = AF_INET; |
| 131 | saddr4->sin_port = htons(port); |
| 132 | |
| 133 | if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 134 | ERR("Failed to convert IPv4 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 135 | goto fail; |
| 136 | } |
| 137 | |
| 138 | if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 139 | ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 140 | goto fail; |
| 141 | } |
| 142 | |
| 143 | } else { |
| 144 | saddr6 = (struct sockaddr_in6 *)&saddr; |
| 145 | |
| 146 | saddr6->sin6_family = AF_INET6; |
| 147 | saddr6->sin6_port = htons(port); |
| 148 | |
| 149 | if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 150 | ERR("Failed to convert IPv6 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 151 | goto fail; |
| 152 | } |
| 153 | |
| 154 | if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 155 | ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 156 | goto fail; |
| 157 | } |
| 158 | } |
| 159 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 160 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 161 | 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] | 162 | goto fail; |
| 163 | } |
| 164 | |
| 165 | return sock; |
| 166 | |
| 167 | fail: |
| 168 | if (sock > -1) { |
| 169 | close(sock); |
| 170 | } |
| 171 | |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 176 | 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] | 177 | { |
| 178 | uint16_t i; |
| 179 | struct pollfd *pfd; |
| 180 | struct sockaddr_storage saddr; |
| 181 | socklen_t saddr_len = sizeof(saddr); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 182 | int ret, sock = -1, flags; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 183 | |
| 184 | pfd = malloc(bind_count * sizeof *pfd); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 185 | if (!pfd) { |
| 186 | ERRMEM; |
| 187 | return -1; |
| 188 | } |
| 189 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 190 | for (i = 0; i < bind_count; ++i) { |
| 191 | pfd[i].fd = binds[i].sock; |
| 192 | pfd[i].events = POLLIN; |
| 193 | pfd[i].revents = 0; |
| 194 | } |
| 195 | |
| 196 | /* poll for a new connection */ |
| 197 | errno = 0; |
| 198 | ret = poll(pfd, bind_count, timeout); |
| 199 | if (!ret) { |
| 200 | /* we timeouted */ |
| 201 | free(pfd); |
| 202 | return 0; |
| 203 | } else if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 204 | ERR("Poll failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 205 | free(pfd); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | for (i = 0; i < bind_count; ++i) { |
| 210 | if (pfd[i].revents & POLLIN) { |
| 211 | sock = pfd[i].fd; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | free(pfd); |
| 216 | |
| 217 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 218 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
Michal Vasko | 3f6cc4a | 2016-01-21 15:58:53 +0100 | [diff] [blame] | 223 | if (ret < 0) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 224 | ERR("Accept failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 228 | /* make the socket non-blocking */ |
| 229 | if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) { |
| 230 | ERR("Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 231 | close(ret); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 235 | if (idx) { |
| 236 | *idx = i; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 239 | /* host was requested */ |
| 240 | if (host) { |
| 241 | if (saddr.ss_family == AF_INET) { |
| 242 | *host = malloc(15); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 243 | if (*host) { |
| 244 | if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) { |
| 245 | ERR("inet_ntop failed (%s).", strerror(errno)); |
| 246 | free(*host); |
| 247 | *host = NULL; |
| 248 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 249 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 250 | if (port) { |
| 251 | *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port); |
| 252 | } |
| 253 | } else { |
| 254 | ERRMEM; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 255 | } |
| 256 | } else if (saddr.ss_family == AF_INET6) { |
| 257 | *host = malloc(40); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 258 | if (*host) { |
| 259 | if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) { |
| 260 | ERR("inet_ntop failed (%s).", strerror(errno)); |
| 261 | free(*host); |
| 262 | *host = NULL; |
| 263 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 264 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 265 | if (port) { |
| 266 | *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port); |
| 267 | } |
| 268 | } else { |
| 269 | ERRMEM; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 270 | } |
| 271 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 272 | ERR("Source host of an unknown protocol family."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 279 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 280 | 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] | 281 | { |
| 282 | const char *identifier = NULL, *version = NULL, *format = NULL; |
| 283 | char *model_data = NULL; |
| 284 | const struct lys_module *module; |
| 285 | struct nc_server_error *err; |
| 286 | struct lyd_node *child, *data = NULL; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 287 | const struct lys_node *sdata = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 288 | |
| 289 | LY_TREE_FOR(rpc->child, child) { |
| 290 | if (!strcmp(child->schema->name, "identifier")) { |
| 291 | identifier = ((struct lyd_node_leaf_list *)child)->value_str; |
| 292 | } else if (!strcmp(child->schema->name, "version")) { |
| 293 | version = ((struct lyd_node_leaf_list *)child)->value_str; |
| 294 | } else if (!strcmp(child->schema->name, "format")) { |
| 295 | format = ((struct lyd_node_leaf_list *)child)->value_str; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* check version */ |
| 300 | if (version && (strlen(version) != 10) && strcmp(version, "1.0")) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 301 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 302 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 303 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* check and get module with the name identifier */ |
| 307 | module = ly_ctx_get_module(server_opts.ctx, identifier, version); |
| 308 | if (!module) { |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 309 | module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version); |
| 310 | } |
| 311 | if (!module) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 312 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 313 | nc_err_set_msg(err, "The requested schema was not found.", "en"); |
| 314 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* check format */ |
| 318 | if (!format || !strcmp(format, "yang")) { |
| 319 | lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL); |
| 320 | } else if (!strcmp(format, "yin")) { |
| 321 | lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL); |
| 322 | } else { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 323 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 324 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 325 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 326 | } |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 327 | if (!model_data) { |
| 328 | ERRINT; |
| 329 | return NULL; |
| 330 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 331 | |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 332 | 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] | 333 | if (!sdata) { |
| 334 | ERRINT; |
| 335 | free(model_data); |
| 336 | return NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 337 | } |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 338 | |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 339 | data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data, LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 340 | if (!data) { |
| 341 | ERRINT; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 342 | free(model_data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 345 | free(model_data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 346 | |
| 347 | return nc_server_reply_data(data, NC_PARAMTYPE_FREE); |
| 348 | } |
| 349 | |
| 350 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 351 | 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] | 352 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 353 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 354 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 357 | API int |
| 358 | nc_server_init(struct ly_ctx *ctx) |
| 359 | { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 360 | const struct lys_node *rpc; |
| 361 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 362 | if (!ctx) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 363 | ERRARG("ctx"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 364 | return -1; |
| 365 | } |
| 366 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 367 | nc_init(); |
| 368 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 369 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 370 | rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema"); |
Michal Vasko | fd100c9 | 2016-03-01 15:23:46 +0100 | [diff] [blame] | 371 | if (rpc && !rpc->priv) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 372 | lys_set_private(rpc, nc_clb_default_get_schema); |
| 373 | } |
| 374 | |
| 375 | /* set default <close-session> callback if not specififed */ |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 376 | rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session"); |
Michal Vasko | fd100c9 | 2016-03-01 15:23:46 +0100 | [diff] [blame] | 377 | if (rpc && !rpc->priv) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 378 | lys_set_private(rpc, nc_clb_default_close_session); |
| 379 | } |
| 380 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 381 | server_opts.ctx = ctx; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 382 | |
| 383 | server_opts.new_session_id = 1; |
| 384 | pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE); |
| 385 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 389 | API void |
| 390 | nc_server_destroy(void) |
| 391 | { |
| 392 | pthread_spin_destroy(&server_opts.sid_lock); |
| 393 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 394 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 395 | nc_server_del_endpt(NULL, 0); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 396 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 397 | nc_destroy(); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 400 | API int |
| 401 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 402 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 403 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) { |
| 404 | ERRARG("basic_mode"); |
| 405 | return -1; |
| 406 | } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) { |
| 407 | ERRARG("also_supported"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | server_opts.wd_basic_mode = basic_mode; |
| 412 | server_opts.wd_also_supported = also_supported; |
| 413 | return 0; |
| 414 | } |
| 415 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 416 | API void |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 417 | nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported) |
| 418 | { |
| 419 | if (!basic_mode && !also_supported) { |
| 420 | ERRARG("basic_mode and also_supported"); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | if (basic_mode) { |
| 425 | *basic_mode = server_opts.wd_basic_mode; |
| 426 | } |
| 427 | if (also_supported) { |
| 428 | *also_supported = server_opts.wd_also_supported; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 433 | nc_server_set_capab_interleave(int interleave_support) |
| 434 | { |
| 435 | if (interleave_support) { |
| 436 | server_opts.interleave_capab = 1; |
| 437 | } else { |
| 438 | server_opts.interleave_capab = 0; |
| 439 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 442 | API int |
| 443 | nc_server_get_capab_interleave(void) |
| 444 | { |
| 445 | return server_opts.interleave_capab; |
| 446 | } |
| 447 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 448 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 449 | nc_server_set_hello_timeout(uint16_t hello_timeout) |
| 450 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 451 | server_opts.hello_timeout = hello_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 454 | API uint16_t |
| 455 | nc_server_get_hello_timeout(void) |
| 456 | { |
| 457 | return server_opts.hello_timeout; |
| 458 | } |
| 459 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 460 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 461 | nc_server_set_idle_timeout(uint16_t idle_timeout) |
| 462 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 463 | server_opts.idle_timeout = idle_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 466 | API uint16_t |
| 467 | nc_server_get_idle_timeout(void) |
| 468 | { |
| 469 | return server_opts.idle_timeout; |
| 470 | } |
| 471 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 472 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 473 | 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] | 474 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 475 | NC_MSG_TYPE msgtype; |
| 476 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 477 | if (!server_opts.ctx) { |
| 478 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 479 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 480 | } else if (fdin < 0) { |
| 481 | ERRARG("fdin"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 482 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 483 | } else if (fdout < 0) { |
| 484 | ERRARG("fdout"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 485 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 486 | } else if (!username) { |
| 487 | ERRARG("username"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 488 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 489 | } else if (!session) { |
| 490 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 491 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /* prepare session structure */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 495 | *session = calloc(1, sizeof **session); |
| 496 | if (!(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 497 | ERRMEM; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 498 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 499 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 500 | (*session)->status = NC_STATUS_STARTING; |
| 501 | (*session)->side = NC_SERVER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 502 | |
| 503 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 504 | (*session)->ti_type = NC_TI_FD; |
| 505 | (*session)->ti.fd.in = fdin; |
| 506 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 507 | |
| 508 | /* assign context (dicionary needed for handshake) */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 509 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 510 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 511 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 512 | /* assign new SID atomically */ |
| 513 | pthread_spin_lock(&server_opts.sid_lock); |
| 514 | (*session)->id = server_opts.new_session_id++; |
| 515 | pthread_spin_unlock(&server_opts.sid_lock); |
| 516 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 517 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 518 | msgtype = nc_handshake(*session); |
| 519 | if (msgtype != NC_MSG_HELLO) { |
| 520 | nc_session_free(*session, NULL); |
| 521 | *session = NULL; |
| 522 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 523 | } |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 524 | (*session)->session_start = (*session)->last_rpc = time(NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 525 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 526 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 527 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 528 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 529 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 530 | int |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 531 | nc_ps_lock(struct nc_pollsession *ps) |
| 532 | { |
| 533 | int ret; |
| 534 | uint8_t our_id, queue_last; |
| 535 | struct timespec ts; |
| 536 | |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 537 | nc_gettimespec(&ts); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 538 | ts.tv_sec += NC_READ_TIMEOUT; |
| 539 | |
| 540 | /* LOCK */ |
| 541 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 542 | if (ret) { |
| 543 | ERR("Failed to lock a pollsession (%s).", strerror(ret)); |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | /* get a unique queue value (by adding 1 to the last added value, if any) */ |
| 548 | if (ps->queue_len) { |
| 549 | queue_last = ps->queue_begin + ps->queue_len - 1; |
| 550 | if (queue_last > NC_PS_QUEUE_SIZE - 1) { |
| 551 | queue_last -= NC_PS_QUEUE_SIZE; |
| 552 | } |
| 553 | our_id = ps->queue[queue_last] + 1; |
| 554 | } else { |
| 555 | our_id = 0; |
| 556 | } |
| 557 | |
| 558 | /* add ourselves into the queue */ |
| 559 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
| 560 | ERR("Pollsession queue too small."); |
| 561 | return -1; |
| 562 | } |
| 563 | ++ps->queue_len; |
| 564 | queue_last = ps->queue_begin + ps->queue_len - 1; |
| 565 | if (queue_last > NC_PS_QUEUE_SIZE - 1) { |
| 566 | queue_last -= NC_PS_QUEUE_SIZE; |
| 567 | } |
| 568 | ps->queue[queue_last] = our_id; |
| 569 | |
| 570 | /* is it our turn? */ |
| 571 | while (ps->queue[ps->queue_begin] != our_id) { |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 572 | nc_gettimespec(&ts); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 573 | ts.tv_sec += NC_READ_TIMEOUT; |
| 574 | |
| 575 | ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts); |
| 576 | if (ret) { |
| 577 | ERR("Failed to wait for a pollsession condition (%s).", strerror(ret)); |
| 578 | /* remove ourselves from the queue */ |
| 579 | ps->queue_begin = (ps->queue_begin < NC_PS_QUEUE_SIZE - 1 ? ps->queue_begin + 1 : 0); |
| 580 | --ps->queue_len; |
| 581 | return -1; |
| 582 | } |
| 583 | } |
| 584 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 585 | /* UNLOCK */ |
| 586 | pthread_mutex_unlock(&ps->lock); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 591 | int |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 592 | nc_ps_unlock(struct nc_pollsession *ps) |
| 593 | { |
| 594 | int ret; |
| 595 | struct timespec ts; |
| 596 | |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 597 | nc_gettimespec(&ts); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 598 | ts.tv_sec += NC_READ_TIMEOUT; |
| 599 | |
| 600 | /* LOCK */ |
| 601 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 602 | if (ret) { |
| 603 | ERR("Failed to lock a pollsession (%s).", strerror(ret)); |
| 604 | ret = -1; |
| 605 | } |
| 606 | |
| 607 | /* remove ourselves from the queue */ |
| 608 | ps->queue_begin = (ps->queue_begin < NC_PS_QUEUE_SIZE - 1 ? ps->queue_begin + 1 : 0); |
| 609 | --ps->queue_len; |
| 610 | |
| 611 | /* broadcast to all other threads that the queue moved */ |
| 612 | pthread_cond_broadcast(&ps->cond); |
| 613 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 614 | /* UNLOCK */ |
| 615 | if (!ret) { |
| 616 | pthread_mutex_unlock(&ps->lock); |
| 617 | } |
| 618 | |
| 619 | return ret; |
| 620 | } |
| 621 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 622 | API struct nc_pollsession * |
| 623 | nc_ps_new(void) |
| 624 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 625 | struct nc_pollsession *ps; |
| 626 | |
| 627 | ps = calloc(1, sizeof(struct nc_pollsession)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 628 | if (!ps) { |
| 629 | ERRMEM; |
| 630 | return NULL; |
| 631 | } |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 632 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 633 | pthread_mutex_init(&ps->lock, NULL); |
| 634 | |
| 635 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | API void |
| 639 | nc_ps_free(struct nc_pollsession *ps) |
| 640 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 641 | if (!ps) { |
| 642 | return; |
| 643 | } |
| 644 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 645 | if (ps->queue_len) { |
| 646 | ERR("FATAL: Freeing a pollsession structure that is currently being worked with!"); |
| 647 | } |
| 648 | |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 649 | free(ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 650 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 651 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 652 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 653 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 654 | free(ps); |
| 655 | } |
| 656 | |
| 657 | API int |
| 658 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 659 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 660 | if (!ps) { |
| 661 | ERRARG("ps"); |
| 662 | return -1; |
| 663 | } else if (!session) { |
| 664 | ERRARG("session"); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 668 | /* LOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 669 | if (nc_ps_lock(ps)) { |
| 670 | return -1; |
| 671 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 672 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 673 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 674 | ps->pfds = nc_realloc(ps->pfds, ps->session_count * sizeof *ps->pfds); |
| 675 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
| 676 | if (!ps->pfds || !ps->sessions) { |
| 677 | ERRMEM; |
| 678 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 679 | nc_ps_unlock(ps); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 680 | return -1; |
| 681 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 682 | |
| 683 | switch (session->ti_type) { |
| 684 | case NC_TI_FD: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 685 | ps->pfds[ps->session_count - 1].fd = session->ti.fd.in; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 686 | break; |
| 687 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 688 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 689 | case NC_TI_LIBSSH: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 690 | ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 691 | break; |
| 692 | #endif |
| 693 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 694 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 695 | case NC_TI_OPENSSL: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 696 | ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 697 | break; |
| 698 | #endif |
| 699 | |
| 700 | default: |
| 701 | ERRINT; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 702 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 703 | nc_ps_unlock(ps); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 704 | return -1; |
| 705 | } |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 706 | ps->pfds[ps->session_count - 1].events = POLLIN; |
| 707 | ps->pfds[ps->session_count - 1].revents = 0; |
| 708 | ps->sessions[ps->session_count - 1] = session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 709 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 710 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 711 | return nc_ps_unlock(ps); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 714 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 715 | _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] | 716 | { |
| 717 | uint16_t i; |
| 718 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 719 | if (index >= 0) { |
| 720 | i = (uint16_t)index; |
| 721 | goto remove; |
| 722 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 723 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 724 | if (ps->sessions[i] == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 725 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 726 | --ps->session_count; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 727 | if (i < ps->session_count) { |
| 728 | ps->sessions[i] = ps->sessions[ps->session_count]; |
| 729 | memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds); |
| 730 | } else if (!ps->session_count) { |
| 731 | free(ps->sessions); |
| 732 | ps->sessions = NULL; |
| 733 | free(ps->pfds); |
| 734 | ps->pfds = NULL; |
| 735 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | } |
| 739 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 740 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 743 | API int |
| 744 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 745 | { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 746 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 747 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 748 | if (!ps) { |
| 749 | ERRARG("ps"); |
| 750 | return -1; |
| 751 | } else if (!session) { |
| 752 | ERRARG("session"); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 753 | return -1; |
| 754 | } |
| 755 | |
| 756 | /* LOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 757 | if (nc_ps_lock(ps)) { |
| 758 | return -1; |
| 759 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 760 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 761 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 762 | |
| 763 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 764 | ret2 = nc_ps_unlock(ps); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 765 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 766 | return (ret || ret2 ? -1 : 0); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 767 | } |
| 768 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 769 | API uint16_t |
| 770 | nc_ps_session_count(struct nc_pollsession *ps) |
| 771 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 772 | uint16_t count; |
| 773 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 774 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 775 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 779 | /* LOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 780 | if (nc_ps_lock(ps)) { |
| 781 | return -1; |
| 782 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 783 | |
| 784 | count = ps->session_count; |
| 785 | |
| 786 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 787 | nc_ps_unlock(ps); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 788 | |
| 789 | return count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 792 | /* must be called holding the session lock! |
| 793 | * returns: NC_PSPOLL_ERROR, |
| 794 | * NC_PSPOLL_BAD_RPC, |
| 795 | * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR, |
| 796 | * NC_PSPOLL_RPC |
| 797 | */ |
| 798 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 799 | nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc) |
| 800 | { |
| 801 | struct lyxml_elem *xml = NULL; |
| 802 | NC_MSG_TYPE msgtype; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 803 | struct nc_server_reply *reply = NULL; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 804 | int ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 805 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 806 | if (!session) { |
| 807 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 808 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 809 | } else if (!rpc) { |
| 810 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 811 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 812 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 813 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 814 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | msgtype = nc_read_msg(session, &xml); |
| 818 | |
| 819 | switch (msgtype) { |
| 820 | case NC_MSG_RPC: |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 821 | *rpc = calloc(1, sizeof **rpc); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 822 | if (!*rpc) { |
| 823 | ERRMEM; |
| 824 | goto error; |
| 825 | } |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 826 | |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 827 | ly_errno = LY_SUCCESS; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 828 | (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC); |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 829 | if (!(*rpc)->tree) { |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 830 | /* parsing RPC failed */ |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 831 | reply = nc_server_reply_err(nc_err_libyang()); |
Radek Krejci | 844662e | 2016-04-13 16:54:43 +0200 | [diff] [blame] | 832 | ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 833 | nc_server_reply_free(reply); |
| 834 | if (ret == -1) { |
| 835 | ERR("Session %u: failed to write reply.", session->id); |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 836 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 837 | ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC; |
| 838 | } else { |
| 839 | ret = NC_PSPOLL_RPC; |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 840 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 841 | (*rpc)->root = xml; |
| 842 | break; |
| 843 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 844 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 845 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 846 | goto error; |
| 847 | case NC_MSG_REPLY: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 848 | ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 849 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 850 | goto error; |
| 851 | case NC_MSG_NOTIF: |
Michal Vasko | 81614ee | 2016-02-02 12:20:14 +0100 | [diff] [blame] | 852 | ERR("Session %u: received <notification> from a NETCONF client.", session->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 853 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 854 | goto error; |
| 855 | default: |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 856 | /* NC_MSG_ERROR, |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 857 | * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg() |
| 858 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 859 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 863 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 864 | |
| 865 | error: |
| 866 | /* cleanup */ |
| 867 | lyxml_free(server_opts.ctx, xml); |
| 868 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 869 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 870 | } |
| 871 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 872 | API void |
| 873 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 874 | { |
| 875 | global_rpc_clb = clb; |
| 876 | } |
| 877 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 878 | /* must be called holding the session lock! |
| 879 | * returns: NC_PSPOLL_ERROR, |
| 880 | * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR, |
| 881 | * NC_PSPOLL_REPLY_ERROR, |
| 882 | * 0 |
| 883 | */ |
| 884 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 885 | nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc) |
| 886 | { |
| 887 | nc_rpc_clb clb; |
| 888 | struct nc_server_reply *reply; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 889 | int ret = 0, r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 890 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 891 | if (!rpc) { |
| 892 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 893 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 894 | } |
| 895 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 896 | if (rpc->tree->schema->priv) { |
| 897 | clb = (nc_rpc_clb) rpc->tree->schema->priv; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 898 | reply = clb(rpc->tree, session); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 899 | } else { |
| 900 | if (global_rpc_clb) { |
| 901 | reply = global_rpc_clb(rpc->tree, session); |
| 902 | } else { /* no callback, reply with a not-implemented error */ |
| 903 | reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT)); |
| 904 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | if (!reply) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 908 | 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] | 909 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 910 | r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply); |
| 911 | if (reply->type == NC_RPL_ERROR) { |
| 912 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 913 | } |
| 914 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 915 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 916 | if (r == -1) { |
| 917 | ERR("Session %u: failed to write reply.", session->id); |
| 918 | ret |= NC_PSPOLL_ERROR; |
| 919 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 920 | |
| 921 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 922 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 923 | session->status = NC_STATUS_INVALID; |
| 924 | } |
| 925 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 926 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | API int |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 930 | 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] | 931 | { |
| 932 | int ret; |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 933 | uint16_t i; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 934 | time_t cur_time; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 935 | struct nc_session *cur_session; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 936 | struct nc_server_rpc *rpc = NULL; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 937 | |
| 938 | if (!ps || !ps->session_count) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 939 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 940 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 943 | cur_time = time(NULL); |
| 944 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 945 | /* LOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 946 | if (nc_ps_lock(ps)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 947 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 948 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 949 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 950 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 951 | if (ps->sessions[i]->status != NC_STATUS_RUNNING) { |
| 952 | ERR("Session %u: session not running.", ps->sessions[i]->id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 953 | ret = NC_PSPOLL_ERROR; |
| 954 | if (session) { |
| 955 | *session = ps->sessions[i]; |
| 956 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 957 | goto finish; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 958 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 959 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 960 | /* TODO invalidate only sessions without subscription */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 961 | if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) { |
| 962 | ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id); |
| 963 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 964 | ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 965 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 966 | if (session) { |
| 967 | *session = ps->sessions[i]; |
| 968 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 969 | goto finish; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 972 | if (ps->pfds[i].revents) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 973 | break; |
| 974 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 977 | if (i == ps->session_count) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 978 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 979 | retry_poll: |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 980 | #endif |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 981 | /* no leftover event */ |
| 982 | i = 0; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 983 | ret = poll(ps->pfds, ps->session_count, timeout); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 984 | if (ret < 0) { |
| 985 | ERR("Poll failed (%s).", strerror(errno)); |
| 986 | ret = NC_PSPOLL_ERROR; |
| 987 | goto finish; |
| 988 | } else if (!ret) { |
| 989 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 990 | goto finish; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 991 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 994 | /* find the first fd with POLLIN, we don't care if there are more now */ |
| 995 | for (; i < ps->session_count; ++i) { |
Michal Vasko | 46eac55 | 2016-05-30 15:27:25 +0200 | [diff] [blame] | 996 | if (ps->pfds[i].revents & (POLLHUP | POLLNVAL)) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 997 | ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id); |
| 998 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 999 | ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1000 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1001 | if (session) { |
| 1002 | *session = ps->sessions[i]; |
| 1003 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1004 | goto finish; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 1005 | } else if (ps->pfds[i].revents & POLLERR) { |
| 1006 | ERR("Session %u: communication socket error.", ps->sessions[i]->id); |
| 1007 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 1008 | ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1009 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1010 | if (session) { |
| 1011 | *session = ps->sessions[i]; |
| 1012 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1013 | goto finish; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 1014 | } else if (ps->pfds[i].revents & POLLIN) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1015 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1016 | if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) { |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 1017 | uint16_t j; |
| 1018 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1019 | /* things are not that simple with SSH... */ |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 1020 | ret = nc_ssh_pollin(ps->sessions[i], timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1021 | |
| 1022 | /* clear POLLIN on sessions sharing this session's SSH session */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1023 | if (ret & (NC_PSPOLL_RPC | NC_PSPOLL_SSH_MSG | NC_PSPOLL_SSH_CHANNEL)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1024 | for (j = i + 1; j < ps->session_count; ++j) { |
| 1025 | if (ps->pfds[j].fd == ps->pfds[i].fd) { |
| 1026 | ps->pfds[j].revents = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1031 | /* SSH message only */ |
| 1032 | if (!(ret & (NC_PSPOLL_RPC | NC_PSPOLL_PENDING))) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1033 | ps->pfds[i].revents = 0; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1034 | if (session) { |
| 1035 | *session = ps->sessions[i]; |
| 1036 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1037 | goto finish; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1038 | |
| 1039 | /* event occurred on some other channel */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1040 | } else if (ret & NC_PSPOLL_PENDING) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1041 | ps->pfds[i].revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1042 | if (i == ps->session_count - 1) { |
| 1043 | /* last session and it is not the right channel, ... */ |
Michal Vasko | 8c74883 | 2016-02-03 15:32:16 +0100 | [diff] [blame] | 1044 | if (!timeout) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1045 | /* ... timeout is 0, so that is it */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1046 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1047 | goto finish; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1048 | } |
Michal Vasko | 8c74883 | 2016-02-03 15:32:16 +0100 | [diff] [blame] | 1049 | /* ... retry polling reasonable time apart ... */ |
| 1050 | usleep(NC_TIMEOUT_STEP); |
| 1051 | if (timeout > 0) { |
| 1052 | /* ... and decrease timeout, if not -1 */ |
Michal Vasko | 7b38e23 | 2016-02-26 15:01:07 +0100 | [diff] [blame] | 1053 | timeout -= NC_TIMEOUT_STEP * 1000; |
Michal Vasko | 8c74883 | 2016-02-03 15:32:16 +0100 | [diff] [blame] | 1054 | } |
| 1055 | goto retry_poll; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1056 | } |
| 1057 | /* check other sessions */ |
| 1058 | continue; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1059 | } |
| 1060 | } |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1061 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1062 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1063 | /* we are going to process it now */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 1064 | ps->pfds[i].revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | if (i == ps->session_count) { |
| 1070 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1071 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1072 | goto finish; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* this is the session with some data available for reading */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1076 | cur_session = ps->sessions[i]; |
| 1077 | if (session) { |
| 1078 | *session = cur_session; |
| 1079 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1080 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1081 | /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1082 | ret = nc_timedlock(cur_session->ti_lock, timeout); |
| 1083 | if (ret < 0) { |
| 1084 | ret = NC_PSPOLL_ERROR; |
| 1085 | goto finish; |
| 1086 | } else if (!ret) { |
| 1087 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1088 | goto finish; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1091 | ret = nc_recv_rpc(cur_session, &rpc); |
| 1092 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1093 | pthread_mutex_unlock(cur_session->ti_lock); |
| 1094 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1095 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1096 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1097 | goto finish; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1100 | cur_session->last_rpc = time(NULL); |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1101 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1102 | /* process RPC */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1103 | ret |= nc_send_reply(cur_session, rpc); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1104 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1105 | pthread_mutex_unlock(cur_session->ti_lock); |
| 1106 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1107 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1108 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1109 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1110 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1111 | } |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1112 | |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 1113 | nc_server_rpc_free(rpc, server_opts.ctx); |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1114 | |
| 1115 | /* is there some other socket waiting? */ |
| 1116 | for (++i; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 1117 | if (ps->pfds[i].revents) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1118 | ret |= NC_PSPOLL_PENDING; |
| 1119 | break; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1123 | finish: |
| 1124 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1125 | nc_ps_unlock(ps); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1126 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1129 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1130 | 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] | 1131 | { |
| 1132 | uint16_t i; |
| 1133 | struct nc_session *session; |
| 1134 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1135 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1136 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1137 | return; |
| 1138 | } |
| 1139 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1140 | /* LOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1141 | if (nc_ps_lock(ps)) { |
| 1142 | return; |
| 1143 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1144 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1145 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1146 | for (i = 0; i < ps->session_count; i++) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1147 | nc_session_free(ps->sessions[i], data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1148 | } |
| 1149 | free(ps->sessions); |
| 1150 | ps->sessions = NULL; |
| 1151 | free(ps->pfds); |
| 1152 | ps->pfds = NULL; |
| 1153 | ps->session_count = 0; |
| 1154 | } else { |
| 1155 | for (i = 0; i < ps->session_count; ) { |
| 1156 | if (ps->sessions[i]->status != NC_STATUS_RUNNING) { |
| 1157 | session = ps->sessions[i]; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1158 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1159 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1160 | continue; |
| 1161 | } |
| 1162 | |
| 1163 | ++i; |
| 1164 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1165 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1166 | |
| 1167 | /* UNLOCK */ |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1168 | nc_ps_unlock(ps); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1171 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1172 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1173 | int |
| 1174 | nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1175 | { |
| 1176 | int sock; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1177 | uint16_t i; |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1178 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 08a629a | 2016-02-02 12:20:47 +0100 | [diff] [blame] | 1179 | struct nc_server_ssh_opts *ssh_opts; |
| 1180 | #endif |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1181 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1182 | if (!name) { |
| 1183 | ERRARG("name"); |
| 1184 | return -1; |
| 1185 | } else if (!address) { |
| 1186 | ERRARG("address"); |
| 1187 | return -1; |
| 1188 | } else if (!port) { |
| 1189 | ERRARG("port"); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1190 | return -1; |
| 1191 | } |
| 1192 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1193 | /* WRITE LOCK */ |
| 1194 | pthread_rwlock_wrlock(&server_opts.endpt_array_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1195 | |
| 1196 | /* check name uniqueness */ |
| 1197 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | d4c03a8 | 2016-02-08 15:27:26 +0100 | [diff] [blame] | 1198 | if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1199 | ERR("Endpoint \"%s\" already exists.", name); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1200 | /* WRITE UNLOCK */ |
Michal Vasko | 9faf1c8 | 2016-02-01 13:26:19 +0100 | [diff] [blame] | 1201 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1202 | return -1; |
| 1203 | } |
| 1204 | } |
| 1205 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1206 | sock = nc_sock_listen(address, port); |
| 1207 | if (sock == -1) { |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1208 | /* WRITE UNLOCK */ |
| 1209 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1210 | return -1; |
| 1211 | } |
| 1212 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1213 | ++server_opts.endpt_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1214 | server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds); |
| 1215 | server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts); |
| 1216 | if (!server_opts.binds || !server_opts.endpts) { |
| 1217 | ERRMEM; |
| 1218 | /* WRITE UNLOCK */ |
| 1219 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 1220 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1221 | return -1; |
| 1222 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1223 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1224 | server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0); |
| 1225 | server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1226 | server_opts.binds[server_opts.endpt_count - 1].port = port; |
| 1227 | server_opts.binds[server_opts.endpt_count - 1].sock = sock; |
| 1228 | server_opts.binds[server_opts.endpt_count - 1].ti = ti; |
| 1229 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1230 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1231 | case NC_TI_LIBSSH: |
Michal Vasko | 08a629a | 2016-02-02 12:20:47 +0100 | [diff] [blame] | 1232 | ssh_opts = calloc(1, sizeof *ssh_opts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1233 | if (!ssh_opts) { |
| 1234 | ERRMEM; |
| 1235 | /* WRITE UNLOCK */ |
| 1236 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 1237 | return -1; |
| 1238 | } |
Michal Vasko | 08a629a | 2016-02-02 12:20:47 +0100 | [diff] [blame] | 1239 | /* set default values */ |
| 1240 | ssh_opts->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 1241 | ssh_opts->auth_attempts = 3; |
| 1242 | ssh_opts->auth_timeout = 10; |
| 1243 | |
| 1244 | server_opts.endpts[server_opts.endpt_count - 1].ti_opts = ssh_opts; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1245 | break; |
| 1246 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1247 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1248 | case NC_TI_OPENSSL: |
| 1249 | server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_tls_opts)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1250 | if (!server_opts.endpts[server_opts.endpt_count - 1].ti_opts) { |
| 1251 | ERRMEM; |
| 1252 | /* WRITE UNLOCK */ |
| 1253 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 1254 | return -1; |
| 1255 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1256 | break; |
| 1257 | #endif |
| 1258 | default: |
| 1259 | ERRINT; |
| 1260 | server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL; |
| 1261 | break; |
| 1262 | } |
| 1263 | pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1264 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1265 | /* WRITE UNLOCK */ |
| 1266 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1267 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
| 1270 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1271 | int |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1272 | nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 1273 | { |
| 1274 | struct nc_endpt *endpt; |
| 1275 | struct nc_bind *bind = NULL; |
| 1276 | uint16_t i; |
| 1277 | int sock; |
| 1278 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1279 | if (!endpt_name) { |
| 1280 | ERRARG("endpt_name"); |
| 1281 | return -1; |
| 1282 | } else if ((!address && !port) || (address && port)) { |
| 1283 | ERRARG("address and port"); |
| 1284 | return -1; |
| 1285 | } else if (!ti) { |
| 1286 | ERRARG("ti"); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1287 | return -1; |
| 1288 | } |
| 1289 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1290 | /* LOCK */ |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1291 | endpt = nc_server_endpt_lock(endpt_name, ti); |
| 1292 | if (!endpt) { |
| 1293 | return -1; |
| 1294 | } |
| 1295 | |
| 1296 | /* we need to learn the index, to get the bind :-/ */ |
| 1297 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1298 | if (&server_opts.endpts[i] == endpt) { |
| 1299 | bind = &server_opts.binds[i]; |
| 1300 | } |
| 1301 | } |
| 1302 | if (!bind) { |
| 1303 | ERRINT; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1304 | goto fail; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | if (address) { |
| 1308 | sock = nc_sock_listen(address, bind->port); |
| 1309 | } else { |
| 1310 | sock = nc_sock_listen(bind->address, port); |
| 1311 | } |
| 1312 | if (sock == -1) { |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1313 | goto fail; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /* close old socket, update parameters */ |
| 1317 | close(bind->sock); |
| 1318 | bind->sock = sock; |
| 1319 | if (address) { |
| 1320 | lydict_remove(server_opts.ctx, bind->address); |
| 1321 | bind->address = lydict_insert(server_opts.ctx, address, 0); |
| 1322 | } else { |
| 1323 | bind->port = port; |
| 1324 | } |
| 1325 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1326 | /* UNLOCK */ |
Michal Vasko | 7a93af7 | 2016-02-01 16:00:15 +0100 | [diff] [blame] | 1327 | nc_server_endpt_unlock(endpt); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1328 | return 0; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1329 | |
| 1330 | fail: |
| 1331 | /* UNLOCK */ |
| 1332 | nc_server_endpt_unlock(endpt); |
| 1333 | return -1; |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1337 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1338 | { |
| 1339 | uint32_t i; |
| 1340 | int ret = -1; |
| 1341 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1342 | /* WRITE LOCK */ |
| 1343 | pthread_rwlock_wrlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1344 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1345 | if (!name && !ti) { |
| 1346 | /* remove all */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1347 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1348 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1349 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1350 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1351 | close(server_opts.binds[i].sock); |
| 1352 | pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock); |
| 1353 | switch (server_opts.binds[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1354 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1355 | case NC_TI_LIBSSH: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1356 | nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1357 | break; |
| 1358 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1359 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1360 | case NC_TI_OPENSSL: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1361 | nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1362 | break; |
| 1363 | #endif |
| 1364 | default: |
| 1365 | ERRINT; |
| 1366 | break; |
| 1367 | } |
| 1368 | free(server_opts.endpts[i].ti_opts); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1369 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1370 | ret = 0; |
| 1371 | } |
Michal Vasko | 7ddc570 | 2016-02-08 15:29:39 +0100 | [diff] [blame] | 1372 | free(server_opts.binds); |
| 1373 | server_opts.binds = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1374 | free(server_opts.endpts); |
| 1375 | server_opts.endpts = NULL; |
| 1376 | server_opts.endpt_count = 0; |
| 1377 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1378 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1379 | /* remove one name endpoint or all ti endpoints */ |
| 1380 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 1381 | if ((server_opts.binds[i].ti == ti) && |
| 1382 | (!name || !strcmp(server_opts.endpts[i].name, name))) { |
| 1383 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1384 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1385 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1386 | close(server_opts.binds[i].sock); |
| 1387 | pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock); |
| 1388 | switch (server_opts.binds[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1389 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1390 | case NC_TI_LIBSSH: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1391 | nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1392 | break; |
| 1393 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1394 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1395 | case NC_TI_OPENSSL: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1396 | nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1397 | break; |
| 1398 | #endif |
| 1399 | default: |
| 1400 | ERRINT; |
| 1401 | break; |
| 1402 | } |
| 1403 | free(server_opts.endpts[i].ti_opts); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1404 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1405 | --server_opts.endpt_count; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 1406 | if (i < server_opts.endpt_count) { |
| 1407 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 1408 | memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
| 1409 | } else if (!server_opts.endpt_count) { |
| 1410 | free(server_opts.binds); |
| 1411 | server_opts.binds = NULL; |
| 1412 | free(server_opts.endpts); |
| 1413 | server_opts.endpts = NULL; |
| 1414 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1415 | |
| 1416 | ret = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1417 | |
| 1418 | if (name) { |
| 1419 | /* one name endpoint removed, they are unique, we're done */ |
| 1420 | break; |
| 1421 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1422 | } |
| 1423 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1426 | /* WRITE UNLOCK */ |
| 1427 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1428 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1429 | return ret; |
| 1430 | } |
| 1431 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1432 | API NC_MSG_TYPE |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1433 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1434 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1435 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1436 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1437 | char *host = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1438 | uint16_t port, idx; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1439 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1440 | if (!server_opts.ctx) { |
| 1441 | ERRINIT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1442 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1443 | } else if (!session) { |
| 1444 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1445 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1446 | } |
| 1447 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1448 | /* we have to hold WRITE for the whole time, since there is not |
| 1449 | * a way of downgrading the lock to READ */ |
| 1450 | /* WRITE LOCK */ |
| 1451 | pthread_rwlock_wrlock(&server_opts.endpt_array_lock); |
| 1452 | |
| 1453 | if (!server_opts.endpt_count) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1454 | ERRINIT; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1455 | /* WRITE UNLOCK */ |
| 1456 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1457 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1458 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1459 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1460 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1461 | |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 1462 | if (ret < 1) { |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1463 | /* WRITE UNLOCK */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1464 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1465 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 1466 | if (!ret) { |
| 1467 | return NC_MSG_WOULDBLOCK; |
| 1468 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1469 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1470 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1471 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1472 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1473 | *session = calloc(1, sizeof **session); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 1474 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1475 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1476 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1477 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1478 | msgtype = NC_MSG_ERROR; |
| 1479 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1480 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1481 | (*session)->status = NC_STATUS_STARTING; |
| 1482 | (*session)->side = NC_SERVER; |
| 1483 | (*session)->ctx = server_opts.ctx; |
| 1484 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 1485 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
| 1486 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1487 | |
| 1488 | /* transport lock */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1489 | (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock); |
| 1490 | if (!(*session)->ti_lock) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1491 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1492 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1493 | msgtype = NC_MSG_ERROR; |
| 1494 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1495 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1496 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1497 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1498 | (*session)->data = server_opts.endpts[idx].ti_opts; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1499 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1500 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1501 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1502 | if (server_opts.binds[idx].ti == NC_TI_LIBSSH) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1503 | ret = nc_accept_ssh_session(*session, sock, timeout); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1504 | if (ret < 0) { |
| 1505 | msgtype = NC_MSG_ERROR; |
| 1506 | goto cleanup; |
| 1507 | } else if (!ret) { |
| 1508 | msgtype = NC_MSG_WOULDBLOCK; |
| 1509 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1510 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1511 | } else |
| 1512 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1513 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1514 | if (server_opts.binds[idx].ti == NC_TI_OPENSSL) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1515 | ret = nc_accept_tls_session(*session, sock, timeout); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1516 | if (ret < 0) { |
| 1517 | msgtype = NC_MSG_ERROR; |
| 1518 | goto cleanup; |
| 1519 | } else if (!ret) { |
| 1520 | msgtype = NC_MSG_WOULDBLOCK; |
| 1521 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1522 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1523 | } else |
| 1524 | #endif |
| 1525 | { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1526 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1527 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1528 | msgtype = NC_MSG_ERROR; |
| 1529 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1530 | } |
| 1531 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1532 | (*session)->data = NULL; |
| 1533 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 1534 | /* WRITE UNLOCK */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1535 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 1536 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1537 | /* assign new SID atomically */ |
| 1538 | /* LOCK */ |
| 1539 | pthread_spin_lock(&server_opts.sid_lock); |
| 1540 | (*session)->id = server_opts.new_session_id++; |
| 1541 | /* UNLOCK */ |
| 1542 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1543 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1544 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1545 | msgtype = nc_handshake(*session); |
| 1546 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1547 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1548 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1549 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1550 | } |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 1551 | (*session)->session_start = time(NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1552 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1553 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1554 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1555 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1556 | cleanup: |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1557 | /* WRITE UNLOCK */ |
| 1558 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 1559 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1560 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1561 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1562 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1565 | NC_MSG_TYPE |
Michal Vasko | 8f5270d | 2016-02-29 16:22:25 +0100 | [diff] [blame] | 1566 | nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1567 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1568 | NC_MSG_TYPE msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1569 | int sock, ret; |
| 1570 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1571 | if (!host) { |
| 1572 | ERRARG("host"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1573 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1574 | } else if (!port) { |
| 1575 | ERRARG("port"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1576 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1577 | } else if (!ti) { |
| 1578 | ERRARG("ti"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1579 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1580 | } else if (!session) { |
| 1581 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1582 | return NC_MSG_ERROR; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1585 | sock = nc_sock_connect(host, port); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1586 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1587 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | *session = calloc(1, sizeof **session); |
| 1591 | if (!(*session)) { |
| 1592 | ERRMEM; |
| 1593 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1594 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1595 | } |
| 1596 | (*session)->status = NC_STATUS_STARTING; |
| 1597 | (*session)->side = NC_SERVER; |
| 1598 | (*session)->ctx = server_opts.ctx; |
| 1599 | (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1600 | (*session)->host = lydict_insert(server_opts.ctx, host, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1601 | (*session)->port = port; |
| 1602 | |
| 1603 | /* transport lock */ |
| 1604 | (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock); |
| 1605 | if (!(*session)->ti_lock) { |
| 1606 | ERRMEM; |
| 1607 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1608 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1609 | goto fail; |
| 1610 | } |
| 1611 | pthread_mutex_init((*session)->ti_lock, NULL); |
| 1612 | |
| 1613 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1614 | #ifdef NC_ENABLED_SSH |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1615 | if (ti == NC_TI_LIBSSH) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1616 | /* OPTS LOCK */ |
| 1617 | pthread_mutex_lock(&ssh_ch_opts_lock); |
| 1618 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1619 | (*session)->data = &ssh_ch_opts; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1620 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1621 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1622 | |
| 1623 | /* OPTS UNLOCK */ |
| 1624 | pthread_mutex_unlock(&ssh_ch_opts_lock); |
| 1625 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1626 | if (ret < 0) { |
| 1627 | msgtype = NC_MSG_ERROR; |
| 1628 | goto fail; |
| 1629 | } else if (!ret) { |
| 1630 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1631 | goto fail; |
| 1632 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1633 | } else |
| 1634 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1635 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1636 | if (ti == NC_TI_OPENSSL) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1637 | /* OPTS LOCK */ |
| 1638 | pthread_mutex_lock(&tls_ch_opts_lock); |
| 1639 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1640 | (*session)->data = &tls_ch_opts; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1641 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1642 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1643 | |
| 1644 | /* OPTS UNLOCK */ |
| 1645 | pthread_mutex_unlock(&tls_ch_opts_lock); |
| 1646 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1647 | if (ret < 0) { |
| 1648 | msgtype = NC_MSG_ERROR; |
| 1649 | goto fail; |
| 1650 | } else if (!ret) { |
| 1651 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1652 | goto fail; |
| 1653 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1654 | } else |
| 1655 | #endif |
| 1656 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1657 | ERRINT; |
| 1658 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1659 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1660 | goto fail; |
| 1661 | } |
| 1662 | |
| 1663 | /* assign new SID atomically */ |
| 1664 | /* LOCK */ |
| 1665 | pthread_spin_lock(&server_opts.sid_lock); |
| 1666 | (*session)->id = server_opts.new_session_id++; |
| 1667 | /* UNLOCK */ |
| 1668 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1669 | |
| 1670 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1671 | msgtype = nc_handshake(*session); |
| 1672 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1673 | goto fail; |
| 1674 | } |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 1675 | (*session)->session_start = time(NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1676 | (*session)->status = NC_STATUS_RUNNING; |
| 1677 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1678 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1679 | |
| 1680 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1681 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1682 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1683 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1684 | } |
| 1685 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1686 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 1687 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 1688 | API time_t |
| 1689 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 1690 | { |
| 1691 | if (!session) { |
| 1692 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 1693 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 1696 | return session->session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 1697 | } |