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