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