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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <errno.h> |
| 26 | #include <string.h> |
| 27 | #include <poll.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <arpa/inet.h> |
| 32 | #include <unistd.h> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 33 | #include <pthread.h> |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 34 | #include <time.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 35 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 36 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 37 | #include "session_server.h" |
| 38 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 39 | struct nc_server_opts server_opts = { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 40 | .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 41 | }; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 42 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 43 | extern struct nc_server_ssh_opts ssh_ch_opts; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 44 | extern pthread_mutex_t ssh_ch_opts_lock; |
| 45 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 46 | extern struct nc_server_tls_opts tls_ch_opts; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 47 | extern pthread_mutex_t tls_ch_opts_lock; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 48 | |
| 49 | struct nc_endpt * |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 50 | nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 51 | { |
| 52 | uint16_t i; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 53 | struct nc_endpt *endpt = NULL; |
| 54 | |
| 55 | /* READ LOCK */ |
| 56 | pthread_rwlock_rdlock(&server_opts.endpt_array_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 57 | |
| 58 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 59 | if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 60 | endpt = &server_opts.endpts[i]; |
| 61 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 65 | if (!endpt) { |
| 66 | ERR("Endpoint \"%s\" was not found.", name); |
| 67 | /* READ UNLOCK */ |
| 68 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | /* ENDPT LOCK */ |
| 73 | pthread_mutex_lock(&endpt->endpt_lock); |
| 74 | |
| 75 | return endpt; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | nc_server_endpt_unlock(struct nc_endpt *endpt) |
| 80 | { |
| 81 | /* ENDPT UNLOCK */ |
| 82 | pthread_mutex_unlock(&endpt->endpt_lock); |
| 83 | |
| 84 | /* READ UNLOCK */ |
| 85 | pthread_rwlock_rdlock(&server_opts.endpt_array_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 86 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 87 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 88 | API void |
| 89 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 90 | { |
| 91 | if (!session || !reason) { |
| 92 | ERRARG; |
| 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); |
| 184 | int ret, sock = -1; |
| 185 | |
| 186 | pfd = malloc(bind_count * sizeof *pfd); |
| 187 | for (i = 0; i < bind_count; ++i) { |
| 188 | pfd[i].fd = binds[i].sock; |
| 189 | pfd[i].events = POLLIN; |
| 190 | pfd[i].revents = 0; |
| 191 | } |
| 192 | |
| 193 | /* poll for a new connection */ |
| 194 | errno = 0; |
| 195 | ret = poll(pfd, bind_count, timeout); |
| 196 | if (!ret) { |
| 197 | /* we timeouted */ |
| 198 | free(pfd); |
| 199 | return 0; |
| 200 | } else if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 201 | ERR("Poll failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 202 | free(pfd); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | for (i = 0; i < bind_count; ++i) { |
| 207 | if (pfd[i].revents & POLLIN) { |
| 208 | sock = pfd[i].fd; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | free(pfd); |
| 213 | |
| 214 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 215 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
Michal Vasko | 3f6cc4a | 2016-01-21 15:58:53 +0100 | [diff] [blame] | 220 | if (ret < 0) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 221 | ERR("Accept failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 225 | if (idx) { |
| 226 | *idx = i; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 229 | /* host was requested */ |
| 230 | if (host) { |
| 231 | if (saddr.ss_family == AF_INET) { |
| 232 | *host = malloc(15); |
| 233 | if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 234 | ERR("inet_ntop failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 235 | free(*host); |
| 236 | *host = NULL; |
| 237 | } |
| 238 | |
| 239 | if (port) { |
| 240 | *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port); |
| 241 | } |
| 242 | } else if (saddr.ss_family == AF_INET6) { |
| 243 | *host = malloc(40); |
| 244 | if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 245 | ERR("inet_ntop failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 246 | free(*host); |
| 247 | *host = NULL; |
| 248 | } |
| 249 | |
| 250 | if (port) { |
| 251 | *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port); |
| 252 | } |
| 253 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 254 | ERR("Source host of an unknown protocol family."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 261 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 262 | 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] | 263 | { |
| 264 | const char *identifier = NULL, *version = NULL, *format = NULL; |
| 265 | char *model_data = NULL; |
| 266 | const struct lys_module *module; |
| 267 | struct nc_server_error *err; |
| 268 | struct lyd_node *child, *data = NULL; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 269 | const struct lys_node *sdata = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 270 | |
| 271 | LY_TREE_FOR(rpc->child, child) { |
| 272 | if (!strcmp(child->schema->name, "identifier")) { |
| 273 | identifier = ((struct lyd_node_leaf_list *)child)->value_str; |
| 274 | } else if (!strcmp(child->schema->name, "version")) { |
| 275 | version = ((struct lyd_node_leaf_list *)child)->value_str; |
| 276 | } else if (!strcmp(child->schema->name, "format")) { |
| 277 | format = ((struct lyd_node_leaf_list *)child)->value_str; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* check version */ |
| 282 | if (version && (strlen(version) != 10) && strcmp(version, "1.0")) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 283 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 284 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 285 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* check and get module with the name identifier */ |
| 289 | module = ly_ctx_get_module(server_opts.ctx, identifier, version); |
| 290 | if (!module) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 291 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 292 | nc_err_set_msg(err, "The requested schema was not found.", "en"); |
| 293 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* check format */ |
| 297 | if (!format || !strcmp(format, "yang")) { |
| 298 | lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL); |
| 299 | } else if (!strcmp(format, "yin")) { |
| 300 | lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL); |
| 301 | } else { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 302 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 303 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 304 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 307 | module = ly_ctx_get_module(server_opts.ctx, "ietf-netconf-monitoring", NULL); |
| 308 | if (module) { |
| 309 | sdata = lys_get_node(module, "/get-schema/output/data"); |
| 310 | } |
| 311 | if (model_data && sdata) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 312 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 313 | data = lyd_output_new_anyxml(sdata, model_data); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 314 | nc_ctx_unlock(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 315 | } |
| 316 | free(model_data); |
| 317 | if (!data) { |
| 318 | ERRINT; |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | return nc_server_reply_data(data, NC_PARAMTYPE_FREE); |
| 323 | } |
| 324 | |
| 325 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 326 | 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] | 327 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 328 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 329 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 332 | API int |
| 333 | nc_server_init(struct ly_ctx *ctx) |
| 334 | { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 335 | const struct lys_node *rpc; |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 336 | const struct lys_module *mod; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 337 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 338 | if (!ctx) { |
| 339 | ERRARG; |
| 340 | return -1; |
| 341 | } |
| 342 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 343 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 344 | rpc = NULL; |
| 345 | mod = ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL); |
| 346 | if (mod) { |
| 347 | rpc = lys_get_node(mod, "/get-schema"); |
| 348 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 349 | if (rpc && !rpc->private) { |
| 350 | lys_set_private(rpc, nc_clb_default_get_schema); |
| 351 | } |
| 352 | |
| 353 | /* set default <close-session> callback if not specififed */ |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 354 | rpc = NULL; |
| 355 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 356 | if (mod) { |
| 357 | rpc = lys_get_node(mod, "/close-session"); |
| 358 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 359 | if (rpc && !rpc->private) { |
| 360 | lys_set_private(rpc, nc_clb_default_close_session); |
| 361 | } |
| 362 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 363 | server_opts.ctx = ctx; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 364 | |
| 365 | server_opts.new_session_id = 1; |
| 366 | pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE); |
| 367 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 371 | API void |
| 372 | nc_server_destroy(void) |
| 373 | { |
| 374 | pthread_spin_destroy(&server_opts.sid_lock); |
| 375 | |
| 376 | #if defined(ENABLE_SSH) || defined(ENABLE_TLS) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 377 | nc_server_del_endpt(NULL, 0); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 378 | #endif |
| 379 | } |
| 380 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 381 | API int |
| 382 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 383 | { |
| 384 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG) |
| 385 | || (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT)))) { |
| 386 | ERRARG; |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | server_opts.wd_basic_mode = basic_mode; |
| 391 | server_opts.wd_also_supported = also_supported; |
| 392 | return 0; |
| 393 | } |
| 394 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 395 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 396 | nc_server_set_capab_interleave(int interleave_support) |
| 397 | { |
| 398 | if (interleave_support) { |
| 399 | server_opts.interleave_capab = 1; |
| 400 | } else { |
| 401 | server_opts.interleave_capab = 0; |
| 402 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 405 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 406 | nc_server_set_hello_timeout(uint16_t hello_timeout) |
| 407 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 408 | server_opts.hello_timeout = hello_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 411 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 412 | nc_server_set_idle_timeout(uint16_t idle_timeout) |
| 413 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 414 | server_opts.idle_timeout = idle_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 418 | 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] | 419 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 420 | if (!server_opts.ctx || (fdin < 0) || (fdout < 0) || !username || !session) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 421 | ERRARG; |
| 422 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* prepare session structure */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 426 | *session = calloc(1, sizeof **session); |
| 427 | if (!(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 428 | ERRMEM; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 429 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 430 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 431 | (*session)->status = NC_STATUS_STARTING; |
| 432 | (*session)->side = NC_SERVER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 433 | |
| 434 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 435 | (*session)->ti_type = NC_TI_FD; |
| 436 | (*session)->ti.fd.in = fdin; |
| 437 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 438 | |
| 439 | /* assign context (dicionary needed for handshake) */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 440 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 441 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 442 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 443 | /* assign new SID atomically */ |
| 444 | pthread_spin_lock(&server_opts.sid_lock); |
| 445 | (*session)->id = server_opts.new_session_id++; |
| 446 | pthread_spin_unlock(&server_opts.sid_lock); |
| 447 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 448 | /* NETCONF handshake */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 449 | if (nc_handshake(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 450 | goto fail; |
| 451 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 452 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 453 | (*session)->last_rpc = time(NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 454 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 455 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 456 | |
| 457 | fail: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 458 | nc_session_free(*session); |
| 459 | *session = NULL; |
| 460 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 461 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 462 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 463 | API struct nc_pollsession * |
| 464 | nc_ps_new(void) |
| 465 | { |
| 466 | return calloc(1, sizeof(struct nc_pollsession)); |
| 467 | } |
| 468 | |
| 469 | API void |
| 470 | nc_ps_free(struct nc_pollsession *ps) |
| 471 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 472 | if (!ps) { |
| 473 | return; |
| 474 | } |
| 475 | |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 476 | free(ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 477 | free(ps->sessions); |
| 478 | free(ps); |
| 479 | } |
| 480 | |
| 481 | API int |
| 482 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 483 | { |
| 484 | if (!ps || !session) { |
| 485 | ERRARG; |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | ++ps->session_count; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 490 | ps->pfds = realloc(ps->pfds, ps->session_count * sizeof *ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 491 | ps->sessions = realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
| 492 | |
| 493 | switch (session->ti_type) { |
| 494 | case NC_TI_FD: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 495 | ps->pfds[ps->session_count - 1].fd = session->ti.fd.in; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | #ifdef ENABLE_SSH |
| 499 | case NC_TI_LIBSSH: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 500 | 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] | 501 | break; |
| 502 | #endif |
| 503 | |
| 504 | #ifdef ENABLE_TLS |
| 505 | case NC_TI_OPENSSL: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 506 | 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] | 507 | break; |
| 508 | #endif |
| 509 | |
| 510 | default: |
| 511 | ERRINT; |
| 512 | return -1; |
| 513 | } |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 514 | ps->pfds[ps->session_count - 1].events = POLLIN; |
| 515 | ps->pfds[ps->session_count - 1].revents = 0; |
| 516 | ps->sessions[ps->session_count - 1] = session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | API int |
| 522 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 523 | { |
| 524 | uint16_t i; |
| 525 | |
| 526 | if (!ps || !session) { |
| 527 | ERRARG; |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 532 | if (ps->sessions[i] == session) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 533 | --ps->session_count; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 534 | ps->sessions[i] = ps->sessions[ps->session_count]; |
| 535 | memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | } |
| 539 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 540 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* must be called holding the session lock! */ |
| 544 | static NC_MSG_TYPE |
| 545 | nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc) |
| 546 | { |
| 547 | struct lyxml_elem *xml = NULL; |
| 548 | NC_MSG_TYPE msgtype; |
| 549 | |
| 550 | if (!session || !rpc) { |
| 551 | ERRARG; |
| 552 | return NC_MSG_ERROR; |
| 553 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 554 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 555 | return NC_MSG_ERROR; |
| 556 | } |
| 557 | |
| 558 | msgtype = nc_read_msg(session, &xml); |
| 559 | |
| 560 | switch (msgtype) { |
| 561 | case NC_MSG_RPC: |
| 562 | *rpc = malloc(sizeof **rpc); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 563 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 564 | (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 565 | nc_ctx_unlock(); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 566 | (*rpc)->root = xml; |
| 567 | break; |
| 568 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 569 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 570 | goto error; |
| 571 | case NC_MSG_REPLY: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 572 | ERR("Session %u: received <rpc-reply> from NETCONF client.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 573 | goto error; |
| 574 | case NC_MSG_NOTIF: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 575 | ERR("Session %u: received <notification> from NETCONF client.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 576 | goto error; |
| 577 | default: |
| 578 | /* NC_MSG_ERROR - pass it out; |
| 579 | * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg() |
| 580 | */ |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | return msgtype; |
| 585 | |
| 586 | error: |
| 587 | /* cleanup */ |
| 588 | lyxml_free(server_opts.ctx, xml); |
| 589 | |
| 590 | return NC_MSG_ERROR; |
| 591 | } |
| 592 | |
| 593 | /* must be called holding the session lock! */ |
| 594 | static NC_MSG_TYPE |
| 595 | nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc) |
| 596 | { |
| 597 | nc_rpc_clb clb; |
| 598 | struct nc_server_reply *reply; |
| 599 | int ret; |
| 600 | |
| 601 | /* no callback, reply with a not-implemented error */ |
| 602 | if (!rpc->tree->schema->private) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 603 | 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] | 604 | } else { |
| 605 | clb = (nc_rpc_clb)rpc->tree->schema->private; |
| 606 | reply = clb(rpc->tree, session); |
| 607 | } |
| 608 | |
| 609 | if (!reply) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 610 | 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] | 611 | } |
| 612 | |
| 613 | ret = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply); |
| 614 | |
| 615 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 616 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 617 | session->status = NC_STATUS_INVALID; |
| 618 | } |
| 619 | |
| 620 | if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 621 | ERR("Session %u: failed to write reply.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 622 | nc_server_reply_free(reply); |
| 623 | return NC_MSG_ERROR; |
| 624 | } |
| 625 | nc_server_reply_free(reply); |
| 626 | |
| 627 | return NC_MSG_REPLY; |
| 628 | } |
| 629 | |
| 630 | API int |
| 631 | nc_ps_poll(struct nc_pollsession *ps, int timeout) |
| 632 | { |
| 633 | int ret; |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 634 | uint16_t i; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 635 | time_t cur_time; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 636 | NC_MSG_TYPE msgtype; |
| 637 | struct nc_session *session; |
| 638 | struct nc_server_rpc *rpc; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 639 | struct timespec old_ts; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 640 | |
| 641 | if (!ps || !ps->session_count) { |
| 642 | ERRARG; |
| 643 | return -1; |
| 644 | } |
| 645 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 646 | cur_time = time(NULL); |
| 647 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 648 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 649 | if (ps->sessions[i]->status != NC_STATUS_RUNNING) { |
| 650 | ERR("Session %u: session not running.", ps->sessions[i]->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 651 | return -1; |
| 652 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 653 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 654 | /* TODO invalidate only sessions without subscription */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 655 | if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) { |
| 656 | ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id); |
| 657 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 658 | ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 659 | return 3; |
| 660 | } |
| 661 | |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 662 | if (ps->pfds[i].revents) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 663 | break; |
| 664 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | if (timeout > 0) { |
| 668 | clock_gettime(CLOCK_MONOTONIC_RAW, &old_ts); |
| 669 | } |
| 670 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 671 | if (i == ps->session_count) { |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 672 | #ifdef ENABLE_SSH |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 673 | retry_poll: |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 674 | #endif |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 675 | /* no leftover event */ |
| 676 | i = 0; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 677 | ret = poll(ps->pfds, ps->session_count, timeout); |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 678 | if (ret < 1) { |
| 679 | return ret; |
| 680 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 683 | /* find the first fd with POLLIN, we don't care if there are more now */ |
| 684 | for (; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 685 | if (ps->pfds[i].revents & POLLHUP) { |
| 686 | ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id); |
| 687 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 688 | ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 689 | return 3; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 690 | } else if (ps->pfds[i].revents & POLLERR) { |
| 691 | ERR("Session %u: communication socket error.", ps->sessions[i]->id); |
| 692 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 693 | ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 694 | return 3; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 695 | } else if (ps->pfds[i].revents & POLLIN) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 696 | #ifdef ENABLE_SSH |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 697 | if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) { |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 698 | uint16_t j; |
| 699 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 700 | /* things are not that simple with SSH... */ |
| 701 | ret = nc_ssh_pollin(ps->sessions[i], &timeout); |
| 702 | |
| 703 | /* clear POLLIN on sessions sharing this session's SSH session */ |
| 704 | if ((ret == 1) || (ret >= 4)) { |
| 705 | for (j = i + 1; j < ps->session_count; ++j) { |
| 706 | if (ps->pfds[j].fd == ps->pfds[i].fd) { |
| 707 | ps->pfds[j].revents = 0; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* actual event happened */ |
| 713 | if ((ret <= 0) || (ret >= 3)) { |
| 714 | ps->pfds[i].revents = 0; |
| 715 | return ret; |
| 716 | |
| 717 | /* event occurred on some other channel */ |
| 718 | } else if (ret == 2) { |
| 719 | ps->pfds[i].revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 720 | if (i == ps->session_count - 1) { |
| 721 | /* last session and it is not the right channel, ... */ |
| 722 | if (timeout > 0) { |
| 723 | /* ... decrease timeout, wait it all out and try again, last time */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 724 | nc_subtract_elapsed(&timeout, &old_ts); |
| 725 | usleep(timeout * 1000); |
| 726 | timeout = 0; |
| 727 | goto retry_poll; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 728 | } else if (!timeout) { |
| 729 | /* ... timeout is 0, so that is it */ |
| 730 | return 0; |
| 731 | } else { |
| 732 | /* ... retry polling reasonable time apart */ |
| 733 | usleep(NC_TIMEOUT_STEP); |
| 734 | goto retry_poll; |
| 735 | } |
| 736 | } |
| 737 | /* check other sessions */ |
| 738 | continue; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | #endif /* ENABLE_SSH */ |
| 742 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 743 | /* we are going to process it now */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 744 | ps->pfds[i].revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (i == ps->session_count) { |
| 750 | ERRINT; |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | /* this is the session with some data available for reading */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 755 | session = ps->sessions[i]; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 756 | |
| 757 | if (timeout > 0) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 758 | nc_subtract_elapsed(&timeout, &old_ts); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 761 | /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 762 | ret = nc_timedlock(session->ti_lock, timeout, NULL); |
| 763 | if (ret != 1) { |
| 764 | /* error or timeout */ |
| 765 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | msgtype = nc_recv_rpc(session, &rpc); |
| 769 | if (msgtype == NC_MSG_ERROR) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 770 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 771 | if (session->status != NC_STATUS_RUNNING) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 772 | return 3; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 773 | } |
| 774 | return -1; |
| 775 | } |
| 776 | |
| 777 | /* process RPC */ |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 778 | session->last_rpc = time(NULL); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 779 | msgtype = nc_send_reply(session, rpc); |
| 780 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 781 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 782 | |
| 783 | if (msgtype == NC_MSG_ERROR) { |
| 784 | nc_server_rpc_free(rpc); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 785 | return -1; |
| 786 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 787 | nc_server_rpc_free(rpc); |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 788 | |
Michal Vasko | bd8b4e1 | 2016-01-22 16:11:20 +0100 | [diff] [blame] | 789 | /* status change takes precedence over leftover events (return 2) */ |
| 790 | if (session->status != NC_STATUS_RUNNING) { |
| 791 | return 3; |
| 792 | } |
| 793 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 794 | /* is there some other socket waiting? */ |
| 795 | for (++i; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 796 | if (ps->pfds[i].revents) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 797 | return 2; |
| 798 | } |
| 799 | } |
| 800 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 801 | return 1; |
| 802 | } |
| 803 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 804 | API int |
| 805 | nc_ctx_lock(int timeout, int *elapsed) |
| 806 | { |
| 807 | return nc_timedlock(&server_opts.ctx_lock, timeout, elapsed); |
| 808 | } |
| 809 | |
| 810 | API int |
| 811 | nc_ctx_unlock(void) |
| 812 | { |
| 813 | int ret; |
| 814 | |
| 815 | ret = pthread_mutex_unlock(&server_opts.ctx_lock); |
| 816 | |
| 817 | if (ret) { |
| 818 | ERR("Mutex unlock failed (%s).", strerror(ret)); |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 825 | #if defined(ENABLE_SSH) || defined(ENABLE_TLS) |
| 826 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 827 | int |
| 828 | nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 829 | { |
| 830 | int sock; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 831 | uint16_t i; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 832 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 833 | if (!name || !address || !port) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 834 | ERRARG; |
| 835 | return -1; |
| 836 | } |
| 837 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 838 | /* READ LOCK */ |
| 839 | pthread_rwlock_rdlock(&server_opts.endpt_array_lock); |
| 840 | |
| 841 | /* check name uniqueness */ |
| 842 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 843 | if (!strcmp(server_opts.endpts[i].name, name)) { |
| 844 | ERR("Endpoint \"%s\" already exists.", name); |
| 845 | return -1; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /* READ UNLOCK */ |
| 850 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 851 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 852 | sock = nc_sock_listen(address, port); |
| 853 | if (sock == -1) { |
| 854 | return -1; |
| 855 | } |
| 856 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 857 | /* WRITE LOCK */ |
| 858 | pthread_rwlock_wrlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 859 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 860 | ++server_opts.endpt_count; |
| 861 | server_opts.binds = realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds); |
| 862 | server_opts.endpts = realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 863 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 864 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 865 | server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0); |
| 866 | server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 867 | nc_ctx_unlock(); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 868 | server_opts.binds[server_opts.endpt_count - 1].port = port; |
| 869 | server_opts.binds[server_opts.endpt_count - 1].sock = sock; |
| 870 | server_opts.binds[server_opts.endpt_count - 1].ti = ti; |
| 871 | switch (ti) { |
| 872 | #ifdef ENABLE_SSH |
| 873 | case NC_TI_LIBSSH: |
| 874 | server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 875 | break; |
| 876 | #endif |
| 877 | #ifdef ENABLE_TLS |
| 878 | case NC_TI_OPENSSL: |
| 879 | server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 880 | break; |
| 881 | #endif |
| 882 | default: |
| 883 | ERRINT; |
| 884 | server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL; |
| 885 | break; |
| 886 | } |
| 887 | 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] | 888 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 889 | /* WRITE UNLOCK */ |
| 890 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 891 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 892 | return 0; |
| 893 | } |
| 894 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 895 | int |
| 896 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 897 | { |
| 898 | uint32_t i; |
| 899 | int ret = -1; |
| 900 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 901 | /* WRITE LOCK */ |
| 902 | pthread_rwlock_wrlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 903 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 904 | if (!name && !ti) { |
| 905 | /* remove all */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 906 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 907 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 908 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 909 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 910 | close(server_opts.binds[i].sock); |
| 911 | pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock); |
| 912 | switch (server_opts.binds[i].ti) { |
| 913 | #ifdef ENABLE_SSH |
| 914 | case NC_TI_LIBSSH: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 915 | nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 916 | break; |
| 917 | #endif |
| 918 | #ifdef ENABLE_TLS |
| 919 | case NC_TI_OPENSSL: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 920 | nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 921 | break; |
| 922 | #endif |
| 923 | default: |
| 924 | ERRINT; |
| 925 | break; |
| 926 | } |
| 927 | free(server_opts.endpts[i].ti_opts); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 928 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 929 | ret = 0; |
| 930 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 931 | nc_ctx_unlock(); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 932 | free(server_opts.endpts); |
| 933 | server_opts.endpts = NULL; |
| 934 | server_opts.endpt_count = 0; |
| 935 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 936 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 937 | /* remove one name endpoint or all ti endpoints */ |
| 938 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 939 | if ((server_opts.binds[i].ti == ti) && |
| 940 | (!name || !strcmp(server_opts.endpts[i].name, name))) { |
| 941 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 942 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 943 | lydict_remove(server_opts.ctx, server_opts.endpts[i].name); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 944 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 945 | nc_ctx_unlock(); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 946 | close(server_opts.binds[i].sock); |
| 947 | pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock); |
| 948 | switch (server_opts.binds[i].ti) { |
| 949 | #ifdef ENABLE_SSH |
| 950 | case NC_TI_LIBSSH: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 951 | nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 952 | break; |
| 953 | #endif |
| 954 | #ifdef ENABLE_TLS |
| 955 | case NC_TI_OPENSSL: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 956 | nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 957 | break; |
| 958 | #endif |
| 959 | default: |
| 960 | ERRINT; |
| 961 | break; |
| 962 | } |
| 963 | free(server_opts.endpts[i].ti_opts); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 964 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 965 | --server_opts.endpt_count; |
| 966 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 967 | memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 968 | |
| 969 | ret = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 970 | |
| 971 | if (name) { |
| 972 | /* one name endpoint removed, they are unique, we're done */ |
| 973 | break; |
| 974 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 975 | } |
| 976 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 979 | /* WRITE UNLOCK */ |
| 980 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 981 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 982 | return ret; |
| 983 | } |
| 984 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 985 | API int |
| 986 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 987 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 988 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 989 | char *host = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 990 | uint16_t port, idx; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 991 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 992 | if (!server_opts.ctx || !server_opts.endpt_count || !session) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 993 | ERRARG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 994 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 997 | /* READ LOCK */ |
| 998 | pthread_rwlock_rdlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 999 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1000 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1001 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1002 | if (ret < 0) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1003 | /* READ UNLOCK */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1004 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1005 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1006 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1007 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1008 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1009 | /* ENDPT LOCK */ |
| 1010 | pthread_mutex_lock(&server_opts.endpts[idx].endpt_lock); |
| 1011 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1012 | *session = calloc(1, sizeof **session); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 1013 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1014 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1015 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 1016 | free(host); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1017 | ret = -1; |
| 1018 | goto fail; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1019 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1020 | (*session)->status = NC_STATUS_STARTING; |
| 1021 | (*session)->side = NC_SERVER; |
| 1022 | (*session)->ctx = server_opts.ctx; |
| 1023 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1024 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1025 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1026 | nc_ctx_unlock(); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1027 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1028 | |
| 1029 | /* transport lock */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1030 | (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock); |
| 1031 | if (!(*session)->ti_lock) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1032 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1033 | close(sock); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1034 | ret = -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1035 | goto fail; |
| 1036 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1037 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1038 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1039 | (*session)->ti_opts = server_opts.endpts[idx].ti_opts; |
| 1040 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1041 | /* sock gets assigned to session or closed */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1042 | #ifdef ENABLE_SSH |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1043 | if (server_opts.binds[idx].ti == NC_TI_LIBSSH) { |
| 1044 | ret = nc_accept_ssh_session(*session, sock, timeout); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1045 | if (ret < 1) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1046 | goto fail; |
| 1047 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1048 | } else |
| 1049 | #endif |
| 1050 | #ifdef ENABLE_TLS |
| 1051 | if (server_opts.binds[idx].ti == NC_TI_OPENSSL) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1052 | ret = nc_accept_tls_session(*session, sock, timeout); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1053 | if (ret < 1) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1054 | goto fail; |
| 1055 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1056 | } else |
| 1057 | #endif |
| 1058 | { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1059 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1060 | close(sock); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1061 | ret = -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1062 | goto fail; |
| 1063 | } |
| 1064 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1065 | /* ENDPT UNLOCK */ |
| 1066 | pthread_mutex_lock(&server_opts.endpts[idx].endpt_lock); |
| 1067 | |
| 1068 | /* READ UNLOCK */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1069 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 1070 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1071 | /* assign new SID atomically */ |
| 1072 | /* LOCK */ |
| 1073 | pthread_spin_lock(&server_opts.sid_lock); |
| 1074 | (*session)->id = server_opts.new_session_id++; |
| 1075 | /* UNLOCK */ |
| 1076 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1077 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1078 | /* NETCONF handshake */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1079 | if (nc_handshake(*session)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1080 | nc_session_free(*session); |
| 1081 | *session = NULL; |
| 1082 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1083 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1084 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1085 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1086 | return 1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1087 | |
| 1088 | fail: |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1089 | /* ENDPT UNLOCK */ |
| 1090 | pthread_mutex_lock(&server_opts.endpts[idx].endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1091 | /* WRITE UNLOCK */ |
| 1092 | pthread_rwlock_unlock(&server_opts.endpt_array_lock); |
| 1093 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1094 | nc_session_free(*session); |
| 1095 | *session = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1096 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1099 | int |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1100 | nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, int timeout, struct nc_session **session) |
| 1101 | { |
| 1102 | int sock, ret; |
| 1103 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1104 | if (!host || !port || !ti || !session) { |
| 1105 | ERRARG; |
| 1106 | return -1; |
| 1107 | } |
| 1108 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1109 | sock = nc_sock_connect(host, port); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1110 | if (sock < 0) { |
| 1111 | return -1; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | *session = calloc(1, sizeof **session); |
| 1115 | if (!(*session)) { |
| 1116 | ERRMEM; |
| 1117 | close(sock); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | (*session)->status = NC_STATUS_STARTING; |
| 1121 | (*session)->side = NC_SERVER; |
| 1122 | (*session)->ctx = server_opts.ctx; |
| 1123 | (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME; |
| 1124 | nc_ctx_lock(-1, NULL); |
| 1125 | (*session)->host = lydict_insert(server_opts.ctx, host, 0); |
| 1126 | nc_ctx_unlock(); |
| 1127 | (*session)->port = port; |
| 1128 | |
| 1129 | /* transport lock */ |
| 1130 | (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock); |
| 1131 | if (!(*session)->ti_lock) { |
| 1132 | ERRMEM; |
| 1133 | close(sock); |
| 1134 | ret = -1; |
| 1135 | goto fail; |
| 1136 | } |
| 1137 | pthread_mutex_init((*session)->ti_lock, NULL); |
| 1138 | |
| 1139 | /* sock gets assigned to session or closed */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1140 | #ifdef ENABLE_SSH |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1141 | if (ti == NC_TI_LIBSSH) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1142 | /* OPTS LOCK */ |
| 1143 | pthread_mutex_lock(&ssh_ch_opts_lock); |
| 1144 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1145 | (*session)->ti_opts = &ssh_ch_opts; |
| 1146 | ret = nc_accept_ssh_session(*session, sock, timeout); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1147 | (*session)->ti_opts = NULL; |
| 1148 | |
| 1149 | /* OPTS UNLOCK */ |
| 1150 | pthread_mutex_unlock(&ssh_ch_opts_lock); |
| 1151 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1152 | if (ret < 1) { |
| 1153 | goto fail; |
| 1154 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1155 | } else |
| 1156 | #endif |
| 1157 | #ifdef ENABLE_TLS |
| 1158 | if (ti == NC_TI_OPENSSL) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1159 | /* OPTS LOCK */ |
| 1160 | pthread_mutex_lock(&tls_ch_opts_lock); |
| 1161 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1162 | (*session)->ti_opts = &tls_ch_opts; |
| 1163 | ret = nc_accept_tls_session(*session, sock, timeout); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 1164 | (*session)->ti_opts = NULL; |
| 1165 | |
| 1166 | /* OPTS UNLOCK */ |
| 1167 | pthread_mutex_unlock(&tls_ch_opts_lock); |
| 1168 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1169 | if (ret < 1) { |
| 1170 | goto fail; |
| 1171 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1172 | } else |
| 1173 | #endif |
| 1174 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1175 | ERRINT; |
| 1176 | close(sock); |
| 1177 | ret = -1; |
| 1178 | goto fail; |
| 1179 | } |
| 1180 | |
| 1181 | /* assign new SID atomically */ |
| 1182 | /* LOCK */ |
| 1183 | pthread_spin_lock(&server_opts.sid_lock); |
| 1184 | (*session)->id = server_opts.new_session_id++; |
| 1185 | /* UNLOCK */ |
| 1186 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1187 | |
| 1188 | /* NETCONF handshake */ |
| 1189 | if (nc_handshake(*session)) { |
| 1190 | ret = -1; |
| 1191 | goto fail; |
| 1192 | } |
| 1193 | (*session)->status = NC_STATUS_RUNNING; |
| 1194 | |
| 1195 | return 1; |
| 1196 | |
| 1197 | fail: |
| 1198 | nc_session_free(*session); |
| 1199 | *session = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1200 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1203 | #endif /* ENABLE_SSH || ENABLE_TLS */ |