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