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 | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 40 | .ctx_lock = PTHREAD_MUTEX_INITIALIZER, |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 41 | .bind_lock = PTHREAD_MUTEX_INITIALIZER |
| 42 | }; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 43 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 44 | API void |
| 45 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 46 | { |
| 47 | if (!session || !reason) { |
| 48 | ERRARG; |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | session->term_reason = reason; |
| 53 | } |
| 54 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 55 | int |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 56 | nc_sock_listen(const char *address, uint16_t port) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 57 | { |
| 58 | const int optVal = 1; |
| 59 | const socklen_t optLen = sizeof(optVal); |
| 60 | int is_ipv4, sock; |
| 61 | struct sockaddr_storage saddr; |
| 62 | |
| 63 | struct sockaddr_in *saddr4; |
| 64 | struct sockaddr_in6 *saddr6; |
| 65 | |
| 66 | |
| 67 | if (!strchr(address, ':')) { |
| 68 | is_ipv4 = 1; |
| 69 | } else { |
| 70 | is_ipv4 = 0; |
| 71 | } |
| 72 | |
| 73 | sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0); |
| 74 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 75 | ERR("Failed to create socket (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 76 | goto fail; |
| 77 | } |
| 78 | |
| 79 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 80 | ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 81 | goto fail; |
| 82 | } |
| 83 | |
| 84 | bzero(&saddr, sizeof(struct sockaddr_storage)); |
| 85 | if (is_ipv4) { |
| 86 | saddr4 = (struct sockaddr_in *)&saddr; |
| 87 | |
| 88 | saddr4->sin_family = AF_INET; |
| 89 | saddr4->sin_port = htons(port); |
| 90 | |
| 91 | if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 92 | ERR("Failed to convert IPv4 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 93 | goto fail; |
| 94 | } |
| 95 | |
| 96 | if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 97 | ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 98 | goto fail; |
| 99 | } |
| 100 | |
| 101 | } else { |
| 102 | saddr6 = (struct sockaddr_in6 *)&saddr; |
| 103 | |
| 104 | saddr6->sin6_family = AF_INET6; |
| 105 | saddr6->sin6_port = htons(port); |
| 106 | |
| 107 | if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 108 | ERR("Failed to convert IPv6 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 109 | goto fail; |
| 110 | } |
| 111 | |
| 112 | if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 113 | ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 114 | goto fail; |
| 115 | } |
| 116 | } |
| 117 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 118 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 119 | 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] | 120 | goto fail; |
| 121 | } |
| 122 | |
| 123 | return sock; |
| 124 | |
| 125 | fail: |
| 126 | if (sock > -1) { |
| 127 | close(sock); |
| 128 | } |
| 129 | |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | int |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 134 | nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 135 | { |
| 136 | uint16_t i; |
| 137 | struct pollfd *pfd; |
| 138 | struct sockaddr_storage saddr; |
| 139 | socklen_t saddr_len = sizeof(saddr); |
| 140 | int ret, sock = -1; |
| 141 | |
| 142 | pfd = malloc(bind_count * sizeof *pfd); |
| 143 | for (i = 0; i < bind_count; ++i) { |
| 144 | pfd[i].fd = binds[i].sock; |
| 145 | pfd[i].events = POLLIN; |
| 146 | pfd[i].revents = 0; |
| 147 | } |
| 148 | |
| 149 | /* poll for a new connection */ |
| 150 | errno = 0; |
| 151 | ret = poll(pfd, bind_count, timeout); |
| 152 | if (!ret) { |
| 153 | /* we timeouted */ |
| 154 | free(pfd); |
| 155 | return 0; |
| 156 | } else if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 157 | ERR("Poll failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 158 | free(pfd); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | for (i = 0; i < bind_count; ++i) { |
| 163 | if (pfd[i].revents & POLLIN) { |
| 164 | sock = pfd[i].fd; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | free(pfd); |
| 169 | |
| 170 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 171 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
| 176 | if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 177 | ERR("Accept failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 178 | return -1; |
| 179 | } |
| 180 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 181 | if (ti) { |
| 182 | *ti = binds[i].ti; |
| 183 | } |
| 184 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 185 | /* host was requested */ |
| 186 | if (host) { |
| 187 | if (saddr.ss_family == AF_INET) { |
| 188 | *host = malloc(15); |
| 189 | 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] | 190 | ERR("inet_ntop failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 191 | free(*host); |
| 192 | *host = NULL; |
| 193 | } |
| 194 | |
| 195 | if (port) { |
| 196 | *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port); |
| 197 | } |
| 198 | } else if (saddr.ss_family == AF_INET6) { |
| 199 | *host = malloc(40); |
| 200 | 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] | 201 | ERR("inet_ntop failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 202 | free(*host); |
| 203 | *host = NULL; |
| 204 | } |
| 205 | |
| 206 | if (port) { |
| 207 | *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port); |
| 208 | } |
| 209 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 210 | ERR("Source host of an unknown protocol family."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 217 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 218 | 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] | 219 | { |
| 220 | const char *identifier = NULL, *version = NULL, *format = NULL; |
| 221 | char *model_data = NULL; |
| 222 | const struct lys_module *module; |
| 223 | struct nc_server_error *err; |
| 224 | struct lyd_node *child, *data = NULL; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 225 | const struct lys_node *sdata = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 226 | |
| 227 | LY_TREE_FOR(rpc->child, child) { |
| 228 | if (!strcmp(child->schema->name, "identifier")) { |
| 229 | identifier = ((struct lyd_node_leaf_list *)child)->value_str; |
| 230 | } else if (!strcmp(child->schema->name, "version")) { |
| 231 | version = ((struct lyd_node_leaf_list *)child)->value_str; |
| 232 | } else if (!strcmp(child->schema->name, "format")) { |
| 233 | format = ((struct lyd_node_leaf_list *)child)->value_str; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* check version */ |
| 238 | if (version && (strlen(version) != 10) && strcmp(version, "1.0")) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 239 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 240 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 241 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* check and get module with the name identifier */ |
| 245 | module = ly_ctx_get_module(server_opts.ctx, identifier, version); |
| 246 | if (!module) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 247 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 248 | nc_err_set_msg(err, "The requested schema was not found.", "en"); |
| 249 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* check format */ |
| 253 | if (!format || !strcmp(format, "yang")) { |
| 254 | lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL); |
| 255 | } else if (!strcmp(format, "yin")) { |
| 256 | lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL); |
| 257 | } else { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 258 | err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
| 259 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 260 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 263 | module = ly_ctx_get_module(server_opts.ctx, "ietf-netconf-monitoring", NULL); |
| 264 | if (module) { |
| 265 | sdata = lys_get_node(module, "/get-schema/output/data"); |
| 266 | } |
| 267 | if (model_data && sdata) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 268 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 269 | data = lyd_output_new_anyxml(sdata, model_data); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 270 | nc_ctx_unlock(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 271 | } |
| 272 | free(model_data); |
| 273 | if (!data) { |
| 274 | ERRINT; |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | return nc_server_reply_data(data, NC_PARAMTYPE_FREE); |
| 279 | } |
| 280 | |
| 281 | static struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 282 | 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] | 283 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 284 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 285 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 288 | API int |
| 289 | nc_server_init(struct ly_ctx *ctx) |
| 290 | { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 291 | const struct lys_node *rpc; |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 292 | const struct lys_module *mod; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 293 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 294 | if (!ctx) { |
| 295 | ERRARG; |
| 296 | return -1; |
| 297 | } |
| 298 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 299 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 300 | rpc = NULL; |
| 301 | mod = ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL); |
| 302 | if (mod) { |
| 303 | rpc = lys_get_node(mod, "/get-schema"); |
| 304 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 305 | if (rpc && !rpc->private) { |
| 306 | lys_set_private(rpc, nc_clb_default_get_schema); |
| 307 | } |
| 308 | |
| 309 | /* set default <close-session> callback if not specififed */ |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 310 | rpc = NULL; |
| 311 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 312 | if (mod) { |
| 313 | rpc = lys_get_node(mod, "/close-session"); |
| 314 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 315 | if (rpc && !rpc->private) { |
| 316 | lys_set_private(rpc, nc_clb_default_close_session); |
| 317 | } |
| 318 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 319 | server_opts.ctx = ctx; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 320 | |
| 321 | server_opts.new_session_id = 1; |
| 322 | pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE); |
| 323 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 327 | API void |
| 328 | nc_server_destroy(void) |
| 329 | { |
| 330 | pthread_spin_destroy(&server_opts.sid_lock); |
| 331 | |
| 332 | #if defined(ENABLE_SSH) || defined(ENABLE_TLS) |
| 333 | nc_server_del_bind(NULL, 0, 0); |
| 334 | #endif |
| 335 | } |
| 336 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 337 | API int |
| 338 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 339 | { |
| 340 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG) |
| 341 | || (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT)))) { |
| 342 | ERRARG; |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | server_opts.wd_basic_mode = basic_mode; |
| 347 | server_opts.wd_also_supported = also_supported; |
| 348 | return 0; |
| 349 | } |
| 350 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 351 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 352 | nc_server_set_capab_interleave(int interleave_support) |
| 353 | { |
| 354 | if (interleave_support) { |
| 355 | server_opts.interleave_capab = 1; |
| 356 | } else { |
| 357 | server_opts.interleave_capab = 0; |
| 358 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 361 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 362 | nc_server_set_hello_timeout(uint16_t hello_timeout) |
| 363 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 364 | server_opts.hello_timeout = hello_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 367 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 368 | nc_server_set_idle_timeout(uint16_t idle_timeout) |
| 369 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 370 | server_opts.idle_timeout = idle_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 374 | 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] | 375 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 376 | if (!server_opts.ctx || (fdin < 0) || (fdout < 0) || !username || !session) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 377 | ERRARG; |
| 378 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* prepare session structure */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 382 | *session = calloc(1, sizeof **session); |
| 383 | if (!(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 384 | ERRMEM; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 385 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 386 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 387 | (*session)->status = NC_STATUS_STARTING; |
| 388 | (*session)->side = NC_SERVER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 389 | |
| 390 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 391 | (*session)->ti_type = NC_TI_FD; |
| 392 | (*session)->ti.fd.in = fdin; |
| 393 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 394 | |
| 395 | /* assign context (dicionary needed for handshake) */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 396 | (*session)->flags = NC_SESSION_SHAREDCTX; |
| 397 | (*session)->ctx = server_opts.ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 398 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 399 | /* assign new SID atomically */ |
| 400 | pthread_spin_lock(&server_opts.sid_lock); |
| 401 | (*session)->id = server_opts.new_session_id++; |
| 402 | pthread_spin_unlock(&server_opts.sid_lock); |
| 403 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 404 | /* NETCONF handshake */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 405 | if (nc_handshake(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 406 | goto fail; |
| 407 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 408 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 409 | (*session)->last_rpc = time(NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 410 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 411 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 412 | |
| 413 | fail: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 414 | nc_session_free(*session); |
| 415 | *session = NULL; |
| 416 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 417 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 418 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 419 | API struct nc_pollsession * |
| 420 | nc_ps_new(void) |
| 421 | { |
| 422 | return calloc(1, sizeof(struct nc_pollsession)); |
| 423 | } |
| 424 | |
| 425 | API void |
| 426 | nc_ps_free(struct nc_pollsession *ps) |
| 427 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 428 | if (!ps) { |
| 429 | return; |
| 430 | } |
| 431 | |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 432 | free(ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 433 | free(ps->sessions); |
| 434 | free(ps); |
| 435 | } |
| 436 | |
| 437 | API int |
| 438 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 439 | { |
| 440 | if (!ps || !session) { |
| 441 | ERRARG; |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | ++ps->session_count; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 446 | ps->pfds = realloc(ps->pfds, ps->session_count * sizeof *ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 447 | ps->sessions = realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
| 448 | |
| 449 | switch (session->ti_type) { |
| 450 | case NC_TI_FD: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 451 | ps->pfds[ps->session_count - 1].fd = session->ti.fd.in; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | #ifdef ENABLE_SSH |
| 455 | case NC_TI_LIBSSH: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 456 | 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] | 457 | break; |
| 458 | #endif |
| 459 | |
| 460 | #ifdef ENABLE_TLS |
| 461 | case NC_TI_OPENSSL: |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 462 | 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] | 463 | break; |
| 464 | #endif |
| 465 | |
| 466 | default: |
| 467 | ERRINT; |
| 468 | return -1; |
| 469 | } |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 470 | ps->pfds[ps->session_count - 1].events = POLLIN; |
| 471 | ps->pfds[ps->session_count - 1].revents = 0; |
| 472 | ps->sessions[ps->session_count - 1] = session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | API int |
| 478 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 479 | { |
| 480 | uint16_t i; |
| 481 | |
| 482 | if (!ps || !session) { |
| 483 | ERRARG; |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 488 | if (ps->sessions[i] == session) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 489 | --ps->session_count; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 490 | ps->sessions[i] = ps->sessions[ps->session_count]; |
| 491 | memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | /* must be called holding the session lock! */ |
| 500 | static NC_MSG_TYPE |
| 501 | nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc) |
| 502 | { |
| 503 | struct lyxml_elem *xml = NULL; |
| 504 | NC_MSG_TYPE msgtype; |
| 505 | |
| 506 | if (!session || !rpc) { |
| 507 | ERRARG; |
| 508 | return NC_MSG_ERROR; |
| 509 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 510 | ERR("Session %u: invalid session to receive RPCs.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 511 | return NC_MSG_ERROR; |
| 512 | } |
| 513 | |
| 514 | msgtype = nc_read_msg(session, &xml); |
| 515 | |
| 516 | switch (msgtype) { |
| 517 | case NC_MSG_RPC: |
| 518 | *rpc = malloc(sizeof **rpc); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 519 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 520 | (*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] | 521 | nc_ctx_unlock(); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 522 | (*rpc)->root = xml; |
| 523 | break; |
| 524 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 525 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 526 | goto error; |
| 527 | case NC_MSG_REPLY: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 528 | ERR("Session %u: received <rpc-reply> from NETCONF client.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 529 | goto error; |
| 530 | case NC_MSG_NOTIF: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 531 | ERR("Session %u: received <notification> from NETCONF client.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 532 | goto error; |
| 533 | default: |
| 534 | /* NC_MSG_ERROR - pass it out; |
| 535 | * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg() |
| 536 | */ |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | return msgtype; |
| 541 | |
| 542 | error: |
| 543 | /* cleanup */ |
| 544 | lyxml_free(server_opts.ctx, xml); |
| 545 | |
| 546 | return NC_MSG_ERROR; |
| 547 | } |
| 548 | |
| 549 | /* must be called holding the session lock! */ |
| 550 | static NC_MSG_TYPE |
| 551 | nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc) |
| 552 | { |
| 553 | nc_rpc_clb clb; |
| 554 | struct nc_server_reply *reply; |
| 555 | int ret; |
| 556 | |
| 557 | /* no callback, reply with a not-implemented error */ |
| 558 | if (!rpc->tree->schema->private) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 559 | 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] | 560 | } else { |
| 561 | clb = (nc_rpc_clb)rpc->tree->schema->private; |
| 562 | reply = clb(rpc->tree, session); |
| 563 | } |
| 564 | |
| 565 | if (!reply) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 566 | 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] | 567 | } |
| 568 | |
| 569 | ret = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply); |
| 570 | |
| 571 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 572 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 573 | session->status = NC_STATUS_INVALID; |
| 574 | } |
| 575 | |
| 576 | if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 577 | ERR("Session %u: failed to write reply.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 578 | nc_server_reply_free(reply); |
| 579 | return NC_MSG_ERROR; |
| 580 | } |
| 581 | nc_server_reply_free(reply); |
| 582 | |
| 583 | return NC_MSG_REPLY; |
| 584 | } |
| 585 | |
| 586 | API int |
| 587 | nc_ps_poll(struct nc_pollsession *ps, int timeout) |
| 588 | { |
| 589 | int ret; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 590 | uint16_t i, j; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 591 | time_t cur_time; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 592 | NC_MSG_TYPE msgtype; |
| 593 | struct nc_session *session; |
| 594 | struct nc_server_rpc *rpc; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 595 | struct timespec old_ts; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 596 | |
| 597 | if (!ps || !ps->session_count) { |
| 598 | ERRARG; |
| 599 | return -1; |
| 600 | } |
| 601 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 602 | cur_time = time(NULL); |
| 603 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 604 | for (i = 0; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 605 | if (ps->sessions[i]->status != NC_STATUS_RUNNING) { |
| 606 | ERR("Session %u: session not running.", ps->sessions[i]->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 607 | return -1; |
| 608 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 609 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 610 | /* TODO invalidate only sessions without subscription */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 611 | if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) { |
| 612 | ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id); |
| 613 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 614 | ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 615 | return 3; |
| 616 | } |
| 617 | |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 618 | if (ps->pfds[i].revents) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 619 | break; |
| 620 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | if (timeout > 0) { |
| 624 | clock_gettime(CLOCK_MONOTONIC_RAW, &old_ts); |
| 625 | } |
| 626 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 627 | if (i == ps->session_count) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 628 | retry_poll: |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 629 | /* no leftover event */ |
| 630 | i = 0; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 631 | ret = poll(ps->pfds, ps->session_count, timeout); |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 632 | if (ret < 1) { |
| 633 | return ret; |
| 634 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 637 | /* find the first fd with POLLIN, we don't care if there are more now */ |
| 638 | for (; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 639 | if (ps->pfds[i].revents & POLLHUP) { |
| 640 | ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id); |
| 641 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 642 | ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 643 | return 3; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 644 | } else if (ps->pfds[i].revents & POLLERR) { |
| 645 | ERR("Session %u: communication socket error.", ps->sessions[i]->id); |
| 646 | ps->sessions[i]->status = NC_STATUS_INVALID; |
| 647 | ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 648 | return 3; |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 649 | } else if (ps->pfds[i].revents & POLLIN) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 650 | #ifdef ENABLE_SSH |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 651 | if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) { |
| 652 | /* things are not that simple with SSH... */ |
| 653 | ret = nc_ssh_pollin(ps->sessions[i], &timeout); |
| 654 | |
| 655 | /* clear POLLIN on sessions sharing this session's SSH session */ |
| 656 | if ((ret == 1) || (ret >= 4)) { |
| 657 | for (j = i + 1; j < ps->session_count; ++j) { |
| 658 | if (ps->pfds[j].fd == ps->pfds[i].fd) { |
| 659 | ps->pfds[j].revents = 0; |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* actual event happened */ |
| 665 | if ((ret <= 0) || (ret >= 3)) { |
| 666 | ps->pfds[i].revents = 0; |
| 667 | return ret; |
| 668 | |
| 669 | /* event occurred on some other channel */ |
| 670 | } else if (ret == 2) { |
| 671 | ps->pfds[i].revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 672 | if (i == ps->session_count - 1) { |
| 673 | /* last session and it is not the right channel, ... */ |
| 674 | if (timeout > 0) { |
| 675 | /* ... decrease timeout, wait it all out and try again, last time */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 676 | nc_subtract_elapsed(&timeout, &old_ts); |
| 677 | usleep(timeout * 1000); |
| 678 | timeout = 0; |
| 679 | goto retry_poll; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 680 | } else if (!timeout) { |
| 681 | /* ... timeout is 0, so that is it */ |
| 682 | return 0; |
| 683 | } else { |
| 684 | /* ... retry polling reasonable time apart */ |
| 685 | usleep(NC_TIMEOUT_STEP); |
| 686 | goto retry_poll; |
| 687 | } |
| 688 | } |
| 689 | /* check other sessions */ |
| 690 | continue; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | #endif /* ENABLE_SSH */ |
| 694 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 695 | /* we are going to process it now */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 696 | ps->pfds[i].revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 697 | break; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (i == ps->session_count) { |
| 702 | ERRINT; |
| 703 | return -1; |
| 704 | } |
| 705 | |
| 706 | /* this is the session with some data available for reading */ |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 707 | session = ps->sessions[i]; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 708 | |
| 709 | if (timeout > 0) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 710 | nc_subtract_elapsed(&timeout, &old_ts); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 711 | } |
| 712 | |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 713 | /* 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] | 714 | ret = nc_timedlock(session->ti_lock, timeout, NULL); |
| 715 | if (ret != 1) { |
| 716 | /* error or timeout */ |
| 717 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | msgtype = nc_recv_rpc(session, &rpc); |
| 721 | if (msgtype == NC_MSG_ERROR) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 722 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 723 | if (session->status != NC_STATUS_RUNNING) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 724 | return 3; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 725 | } |
| 726 | return -1; |
| 727 | } |
| 728 | |
| 729 | /* process RPC */ |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 730 | session->last_rpc = time(NULL); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 731 | msgtype = nc_send_reply(session, rpc); |
| 732 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 733 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 734 | |
| 735 | if (msgtype == NC_MSG_ERROR) { |
| 736 | nc_server_rpc_free(rpc); |
| 737 | if (session->status != NC_STATUS_RUNNING) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 738 | return 3; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 739 | } |
| 740 | return -1; |
| 741 | } |
| 742 | |
| 743 | nc_server_rpc_free(rpc); |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 744 | |
| 745 | /* is there some other socket waiting? */ |
| 746 | for (++i; i < ps->session_count; ++i) { |
Michal Vasko | 3a71513 | 2016-01-21 15:40:31 +0100 | [diff] [blame] | 747 | if (ps->pfds[i].revents) { |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 748 | return 2; |
| 749 | } |
| 750 | } |
| 751 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 752 | return 1; |
| 753 | } |
| 754 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 755 | API int |
| 756 | nc_ctx_lock(int timeout, int *elapsed) |
| 757 | { |
| 758 | return nc_timedlock(&server_opts.ctx_lock, timeout, elapsed); |
| 759 | } |
| 760 | |
| 761 | API int |
| 762 | nc_ctx_unlock(void) |
| 763 | { |
| 764 | int ret; |
| 765 | |
| 766 | ret = pthread_mutex_unlock(&server_opts.ctx_lock); |
| 767 | |
| 768 | if (ret) { |
| 769 | ERR("Mutex unlock failed (%s).", strerror(ret)); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 776 | #if defined(ENABLE_SSH) || defined(ENABLE_TLS) |
| 777 | |
| 778 | API int |
| 779 | nc_server_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 780 | { |
| 781 | int sock; |
| 782 | |
| 783 | if (!address || !port || ((ti != NC_TI_LIBSSH) && (ti != NC_TI_OPENSSL))) { |
| 784 | ERRARG; |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | sock = nc_sock_listen(address, port); |
| 789 | if (sock == -1) { |
| 790 | return -1; |
| 791 | } |
| 792 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 793 | /* LOCK */ |
| 794 | pthread_mutex_lock(&server_opts.bind_lock); |
| 795 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 796 | ++server_opts.bind_count; |
| 797 | server_opts.binds = realloc(server_opts.binds, server_opts.bind_count * sizeof *server_opts.binds); |
| 798 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 799 | nc_ctx_lock(-1, NULL); |
| 800 | server_opts.binds[server_opts.bind_count - 1].address = lydict_insert(server_opts.ctx, address, 0); |
| 801 | nc_ctx_unlock(); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 802 | server_opts.binds[server_opts.bind_count - 1].port = port; |
| 803 | server_opts.binds[server_opts.bind_count - 1].sock = sock; |
| 804 | server_opts.binds[server_opts.bind_count - 1].ti = ti; |
| 805 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 806 | /* UNLOCK */ |
| 807 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 808 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | API int |
| 813 | nc_server_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 814 | { |
| 815 | uint32_t i; |
| 816 | int ret = -1; |
| 817 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 818 | /* LOCK */ |
| 819 | pthread_mutex_lock(&server_opts.bind_lock); |
| 820 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 821 | if (!address && !port && !ti) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 822 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 823 | for (i = 0; i < server_opts.bind_count; ++i) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 824 | close(server_opts.binds[i].sock); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 825 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 826 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 827 | ret = 0; |
| 828 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 829 | nc_ctx_unlock(); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 830 | free(server_opts.binds); |
| 831 | server_opts.binds = NULL; |
| 832 | server_opts.bind_count = 0; |
| 833 | } else { |
| 834 | for (i = 0; i < server_opts.bind_count; ++i) { |
| 835 | if ((!address || !strcmp(server_opts.binds[i].address, address)) |
| 836 | && (!port || (server_opts.binds[i].port == port)) |
| 837 | && (!ti || (server_opts.binds[i].ti == ti))) { |
| 838 | close(server_opts.binds[i].sock); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 839 | nc_ctx_lock(-1, NULL); |
| 840 | lydict_remove(server_opts.ctx, server_opts.binds[i].address); |
| 841 | nc_ctx_unlock(); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 842 | |
| 843 | --server_opts.bind_count; |
Michal Vasko | 5b003bf | 2016-01-19 10:56:19 +0100 | [diff] [blame] | 844 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.bind_count], sizeof *server_opts.binds); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 845 | |
| 846 | ret = 0; |
| 847 | } |
| 848 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 851 | /* UNLOCK */ |
| 852 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 853 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 857 | API int |
| 858 | nc_accept(int timeout, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 859 | { |
| 860 | NC_TRANSPORT_IMPL ti; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 861 | int sock, ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 862 | char *host; |
| 863 | uint16_t port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 864 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 865 | if (!server_opts.ctx || !server_opts.binds || !session) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 866 | ERRARG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 867 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 870 | /* LOCK */ |
| 871 | pthread_mutex_lock(&server_opts.bind_lock); |
| 872 | |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 873 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.bind_count, timeout, &ti, &host, &port); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 874 | |
| 875 | /* UNLOCK */ |
| 876 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 877 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 878 | if (ret < 0) { |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 879 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 880 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 881 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 882 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 883 | *session = calloc(1, sizeof **session); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame^] | 884 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 885 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 886 | close(sock); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 887 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 888 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 889 | (*session)->status = NC_STATUS_STARTING; |
| 890 | (*session)->side = NC_SERVER; |
| 891 | (*session)->ctx = server_opts.ctx; |
| 892 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 893 | nc_ctx_lock(-1, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 894 | (*session)->host = lydict_insert_zc(server_opts.ctx, host); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 895 | nc_ctx_unlock(); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 896 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 897 | |
| 898 | /* transport lock */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 899 | (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock); |
| 900 | if (!(*session)->ti_lock) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 901 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 902 | close(sock); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 903 | ret = -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 904 | goto fail; |
| 905 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 906 | pthread_mutex_init((*session)->ti_lock, NULL); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 907 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 908 | /* sock gets assigned to session or closed */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 909 | if (ti == NC_TI_LIBSSH) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 910 | ret = nc_accept_ssh_session(*session, sock, timeout); |
| 911 | if (ret < 1) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 912 | goto fail; |
| 913 | } |
| 914 | } else if (ti == NC_TI_OPENSSL) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 915 | ret = nc_accept_tls_session(*session, sock, timeout); |
| 916 | if (ret < 1) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 917 | goto fail; |
| 918 | } |
| 919 | } else { |
| 920 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 921 | close(sock); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 922 | ret = -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 923 | goto fail; |
| 924 | } |
| 925 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 926 | /* assign new SID atomically */ |
| 927 | /* LOCK */ |
| 928 | pthread_spin_lock(&server_opts.sid_lock); |
| 929 | (*session)->id = server_opts.new_session_id++; |
| 930 | /* UNLOCK */ |
| 931 | pthread_spin_unlock(&server_opts.sid_lock); |
| 932 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 933 | /* NETCONF handshake */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 934 | if (nc_handshake(*session)) { |
| 935 | ret = -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 936 | goto fail; |
| 937 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 938 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 939 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 940 | return 1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 941 | |
| 942 | fail: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 943 | nc_session_free(*session); |
| 944 | *session = NULL; |
| 945 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | #endif /* ENABLE_SSH || ENABLE_TLS */ |