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