blob: 6dc5740297bfa698a1c4e88ab2bfe6424988b904 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 server session manipulation functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <stdint.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010025#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028
Michal Vasko1a38c862016-01-15 15:50:07 +010029#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010030#include "session_server.h"
31
Michal Vaskob48aa812016-01-18 14:13:09 +010032struct nc_server_opts server_opts = {
Michal Vasko3031aae2016-01-27 16:07:18 +010033 .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010034};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010035
Michal Vasko3031aae2016-01-27 16:07:18 +010036extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010037extern pthread_mutex_t ssh_ch_opts_lock;
38
Michal Vasko3031aae2016-01-27 16:07:18 +010039extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010040extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010041
42struct nc_endpt *
Michal Vaskoe2713da2016-08-22 16:06:40 +020043nc_server_endpt_lock(const char *name, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010044{
45 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010046 struct nc_endpt *endpt = NULL;
47
48 /* READ LOCK */
49 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010050
51 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +020052 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010053 endpt = &server_opts.endpts[i];
54 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010055 }
56 }
57
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010058 if (!endpt) {
59 ERR("Endpoint \"%s\" was not found.", name);
60 /* READ UNLOCK */
61 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
62 return NULL;
63 }
64
65 /* ENDPT LOCK */
66 pthread_mutex_lock(&endpt->endpt_lock);
67
Michal Vaskoe2713da2016-08-22 16:06:40 +020068 if (idx) {
69 *idx = i;
70 }
71
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010072 return endpt;
73}
74
75void
76nc_server_endpt_unlock(struct nc_endpt *endpt)
77{
78 /* ENDPT UNLOCK */
79 pthread_mutex_unlock(&endpt->endpt_lock);
80
81 /* READ UNLOCK */
Michal Vasko27562ad2016-02-02 15:50:39 +010082 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010083}
Michal Vasko086311b2016-01-08 09:53:11 +010084
Michal Vasko1a38c862016-01-15 15:50:07 +010085API void
86nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
87{
Michal Vasko45e53ae2016-04-07 11:46:03 +020088 if (!session) {
89 ERRARG("session");
90 return;
91 } else if (!reason) {
92 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +010093 return;
94 }
95
96 session->term_reason = reason;
97}
98
Michal Vasko086311b2016-01-08 09:53:11 +010099int
Michal Vaskof05562c2016-01-20 12:06:43 +0100100nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100101{
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 Vaskod083db62016-01-19 10:31:29 +0100119 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100120 goto fail;
121 }
122
123 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100124 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100125 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 Vaskod083db62016-01-19 10:31:29 +0100136 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100137 goto fail;
138 }
139
140 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100141 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100142 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 Vaskod083db62016-01-19 10:31:29 +0100152 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100153 goto fail;
154 }
155
156 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100157 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100158 goto fail;
159 }
160 }
161
Michal Vaskofb89d772016-01-08 12:25:35 +0100162 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100163 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100164 goto fail;
165 }
166
167 return sock;
168
169fail:
170 if (sock > -1) {
171 close(sock);
172 }
173
174 return -1;
175}
176
177int
Michal Vasko3031aae2016-01-27 16:07:18 +0100178nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx)
Michal Vasko086311b2016-01-08 09:53:11 +0100179{
180 uint16_t i;
181 struct pollfd *pfd;
182 struct sockaddr_storage saddr;
183 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100184 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100185
186 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100187 if (!pfd) {
188 ERRMEM;
189 return -1;
190 }
191
Michal Vasko94acafc2016-09-23 13:40:10 +0200192 i = 0;
193 while (i < bind_count) {
194 if (binds[i].sock < 0) {
195 /* invalid socket */
196 --bind_count;
197 continue;
198 }
Michal Vasko086311b2016-01-08 09:53:11 +0100199 pfd[i].fd = binds[i].sock;
200 pfd[i].events = POLLIN;
201 pfd[i].revents = 0;
Michal Vasko94acafc2016-09-23 13:40:10 +0200202
203 ++i;
Michal Vasko086311b2016-01-08 09:53:11 +0100204 }
205
206 /* poll for a new connection */
207 errno = 0;
208 ret = poll(pfd, bind_count, timeout);
209 if (!ret) {
210 /* we timeouted */
211 free(pfd);
212 return 0;
213 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100214 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100215 free(pfd);
216 return -1;
217 }
218
219 for (i = 0; i < bind_count; ++i) {
220 if (pfd[i].revents & POLLIN) {
221 sock = pfd[i].fd;
222 break;
223 }
224 }
225 free(pfd);
226
227 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100228 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100229 return -1;
230 }
231
232 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100233 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100234 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100235 return -1;
236 }
237
Michal Vasko0190bc32016-03-02 15:47:49 +0100238 /* make the socket non-blocking */
239 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
240 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100241 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100242 return -1;
243 }
244
Michal Vasko3031aae2016-01-27 16:07:18 +0100245 if (idx) {
246 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100247 }
248
Michal Vasko086311b2016-01-08 09:53:11 +0100249 /* host was requested */
250 if (host) {
251 if (saddr.ss_family == AF_INET) {
252 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100253 if (*host) {
254 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
255 ERR("inet_ntop failed (%s).", strerror(errno));
256 free(*host);
257 *host = NULL;
258 }
Michal Vasko086311b2016-01-08 09:53:11 +0100259
Michal Vasko4eb3c312016-03-01 14:09:37 +0100260 if (port) {
261 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
262 }
263 } else {
264 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100265 }
266 } else if (saddr.ss_family == AF_INET6) {
267 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100268 if (*host) {
269 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
270 ERR("inet_ntop failed (%s).", strerror(errno));
271 free(*host);
272 *host = NULL;
273 }
Michal Vasko086311b2016-01-08 09:53:11 +0100274
Michal Vasko4eb3c312016-03-01 14:09:37 +0100275 if (port) {
276 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
277 }
278 } else {
279 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100280 }
281 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100282 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100283 }
284 }
285
286 return ret;
287}
288
Michal Vasko05ba9df2016-01-13 14:40:27 +0100289static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100290nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100291{
292 const char *identifier = NULL, *version = NULL, *format = NULL;
293 char *model_data = NULL;
294 const struct lys_module *module;
295 struct nc_server_error *err;
296 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100297 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100298
299 LY_TREE_FOR(rpc->child, child) {
300 if (!strcmp(child->schema->name, "identifier")) {
301 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
302 } else if (!strcmp(child->schema->name, "version")) {
303 version = ((struct lyd_node_leaf_list *)child)->value_str;
304 } else if (!strcmp(child->schema->name, "format")) {
305 format = ((struct lyd_node_leaf_list *)child)->value_str;
306 }
307 }
308
309 /* check version */
310 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100311 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
312 nc_err_set_msg(err, "The requested version is not supported.", "en");
313 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100314 }
315
316 /* check and get module with the name identifier */
317 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
318 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200319 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
320 }
321 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100322 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
323 nc_err_set_msg(err, "The requested schema was not found.", "en");
324 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100325 }
326
327 /* check format */
328 if (!format || !strcmp(format, "yang")) {
329 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
330 } else if (!strcmp(format, "yin")) {
331 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
332 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100333 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
334 nc_err_set_msg(err, "The requested format is not supported.", "en");
335 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100336 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200337 if (!model_data) {
338 ERRINT;
339 return NULL;
340 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100341
Michal Vasko303245c2016-03-24 15:20:03 +0100342 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200343 if (!sdata) {
344 ERRINT;
345 free(model_data);
346 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100347 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200348
Radek Krejci539efb62016-08-24 15:05:16 +0200349 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
350 LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100351 if (!data) {
352 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200353 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100354 return NULL;
355 }
356
Radek Krejci36dfdb32016-09-01 16:56:35 +0200357 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100358}
359
360static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100361nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100362{
Michal Vasko428087d2016-01-14 16:04:28 +0100363 session->term_reason = NC_SESSION_TERM_CLOSED;
364 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100365}
366
Michal Vasko086311b2016-01-08 09:53:11 +0100367API int
368nc_server_init(struct ly_ctx *ctx)
369{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100370 const struct lys_node *rpc;
371
Michal Vasko086311b2016-01-08 09:53:11 +0100372 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200373 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100374 return -1;
375 }
376
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100377 nc_init();
378
Michal Vasko05ba9df2016-01-13 14:40:27 +0100379 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100380 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100381 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100382 lys_set_private(rpc, nc_clb_default_get_schema);
383 }
384
385 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100386 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100387 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100388 lys_set_private(rpc, nc_clb_default_close_session);
389 }
390
Michal Vasko086311b2016-01-08 09:53:11 +0100391 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100392
393 server_opts.new_session_id = 1;
394 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
395
Michal Vasko086311b2016-01-08 09:53:11 +0100396 return 0;
397}
398
Michal Vaskob48aa812016-01-18 14:13:09 +0100399API void
400nc_server_destroy(void)
401{
402 pthread_spin_destroy(&server_opts.sid_lock);
403
Radek Krejci53691be2016-02-22 13:58:37 +0100404#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200405 nc_server_del_endpt(NULL);
Michal Vaskob48aa812016-01-18 14:13:09 +0100406#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100407 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100408}
409
Michal Vasko086311b2016-01-08 09:53:11 +0100410API int
411nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
412{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200413 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
414 ERRARG("basic_mode");
415 return -1;
416 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
417 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100418 return -1;
419 }
420
421 server_opts.wd_basic_mode = basic_mode;
422 server_opts.wd_also_supported = also_supported;
423 return 0;
424}
425
Michal Vasko1a38c862016-01-15 15:50:07 +0100426API void
Michal Vasko55f03972016-04-13 08:56:01 +0200427nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
428{
429 if (!basic_mode && !also_supported) {
430 ERRARG("basic_mode and also_supported");
431 return;
432 }
433
434 if (basic_mode) {
435 *basic_mode = server_opts.wd_basic_mode;
436 }
437 if (also_supported) {
438 *also_supported = server_opts.wd_also_supported;
439 }
440}
441
442API void
Michal Vasko086311b2016-01-08 09:53:11 +0100443nc_server_set_capab_interleave(int interleave_support)
444{
445 if (interleave_support) {
446 server_opts.interleave_capab = 1;
447 } else {
448 server_opts.interleave_capab = 0;
449 }
Michal Vasko086311b2016-01-08 09:53:11 +0100450}
451
Michal Vasko55f03972016-04-13 08:56:01 +0200452API int
453nc_server_get_capab_interleave(void)
454{
455 return server_opts.interleave_capab;
456}
457
Michal Vasko1a38c862016-01-15 15:50:07 +0100458API void
Michal Vasko086311b2016-01-08 09:53:11 +0100459nc_server_set_hello_timeout(uint16_t hello_timeout)
460{
Michal Vasko086311b2016-01-08 09:53:11 +0100461 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100462}
463
Michal Vasko55f03972016-04-13 08:56:01 +0200464API uint16_t
465nc_server_get_hello_timeout(void)
466{
467 return server_opts.hello_timeout;
468}
469
Michal Vasko1a38c862016-01-15 15:50:07 +0100470API void
Michal Vasko086311b2016-01-08 09:53:11 +0100471nc_server_set_idle_timeout(uint16_t idle_timeout)
472{
Michal Vasko086311b2016-01-08 09:53:11 +0100473 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100474}
475
Michal Vasko55f03972016-04-13 08:56:01 +0200476API uint16_t
477nc_server_get_idle_timeout(void)
478{
479 return server_opts.idle_timeout;
480}
481
Michal Vasko71090fc2016-05-24 16:37:28 +0200482API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100483nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100484{
Michal Vasko71090fc2016-05-24 16:37:28 +0200485 NC_MSG_TYPE msgtype;
486
Michal Vasko45e53ae2016-04-07 11:46:03 +0200487 if (!server_opts.ctx) {
488 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200489 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200490 } else if (fdin < 0) {
491 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200492 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200493 } else if (fdout < 0) {
494 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200495 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200496 } else if (!username) {
497 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200498 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200499 } else if (!session) {
500 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200501 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100502 }
503
504 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100505 *session = calloc(1, sizeof **session);
506 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100507 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200508 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100509 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100510 (*session)->status = NC_STATUS_STARTING;
511 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100512
513 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100514 (*session)->ti_type = NC_TI_FD;
515 (*session)->ti.fd.in = fdin;
516 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100517
518 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100519 (*session)->flags = NC_SESSION_SHAREDCTX;
520 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100521
Michal Vaskob48aa812016-01-18 14:13:09 +0100522 /* assign new SID atomically */
523 pthread_spin_lock(&server_opts.sid_lock);
524 (*session)->id = server_opts.new_session_id++;
525 pthread_spin_unlock(&server_opts.sid_lock);
526
Michal Vasko086311b2016-01-08 09:53:11 +0100527 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200528 msgtype = nc_handshake(*session);
529 if (msgtype != NC_MSG_HELLO) {
530 nc_session_free(*session, NULL);
531 *session = NULL;
532 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100533 }
Michal Vaskof8352352016-05-24 09:11:36 +0200534 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100535 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100536
Michal Vasko71090fc2016-05-24 16:37:28 +0200537 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100538}
Michal Vasko9e036d52016-01-08 10:49:26 +0100539
Michal Vaskobdcf2362016-07-26 11:35:43 +0200540static void
541nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
542{
543 uint8_t i, found = 0;
544
545 for (i = 0; i < ps->queue_len; ++i) {
546 /* idx round buffer adjust */
547 if (ps->queue_begin + i == NC_PS_QUEUE_SIZE) {
548 i = -ps->queue_begin;
549 }
550
551 if (found) {
552 /* move the value back one place */
553 if (ps->queue[ps->queue_begin + i] == id) {
554 /* another equal value, simply cannot be */
555 ERRINT;
556 }
557
558 if (ps->queue_begin + i == 0) {
559 ps->queue[NC_PS_QUEUE_SIZE - 1] = ps->queue[ps->queue_begin + i];
560 } else {
561 ps->queue[ps->queue_begin + i - 1] = ps->queue[ps->queue_begin + i];
562 }
563 } else if (ps->queue[ps->queue_begin + i] == id) {
564 /* found our id, there can be no more equal valid values */
565 found = 1;
566 }
567 }
568
569 if (!found) {
570 ERRINT;
571 }
572 --ps->queue_len;
573}
574
Michal Vaskof04a52a2016-04-07 10:52:10 +0200575int
Michal Vasko227f8ff2016-07-26 14:08:59 +0200576nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200577{
578 int ret;
Michal Vaskobdcf2362016-07-26 11:35:43 +0200579 uint8_t queue_last;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200580 struct timespec ts;
581
Radek Krejci7ac16052016-07-15 11:48:18 +0200582 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200583 ts.tv_sec += NC_READ_TIMEOUT;
584
585 /* LOCK */
586 ret = pthread_mutex_timedlock(&ps->lock, &ts);
587 if (ret) {
Michal Vasko227f8ff2016-07-26 14:08:59 +0200588 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200589 return -1;
590 }
591
592 /* get a unique queue value (by adding 1 to the last added value, if any) */
593 if (ps->queue_len) {
594 queue_last = ps->queue_begin + ps->queue_len - 1;
595 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
596 queue_last -= NC_PS_QUEUE_SIZE;
597 }
Michal Vaskobdcf2362016-07-26 11:35:43 +0200598 *id = ps->queue[queue_last] + 1;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200599 } else {
Michal Vaskobdcf2362016-07-26 11:35:43 +0200600 *id = 0;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200601 }
602
603 /* add ourselves into the queue */
604 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko227f8ff2016-07-26 14:08:59 +0200605 ERR("%s: pollsession queue too small.", func);
Michal Vasko8c242532016-07-26 12:23:24 +0200606 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200607 return -1;
608 }
609 ++ps->queue_len;
610 queue_last = ps->queue_begin + ps->queue_len - 1;
611 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
612 queue_last -= NC_PS_QUEUE_SIZE;
613 }
Michal Vaskobdcf2362016-07-26 11:35:43 +0200614 ps->queue[queue_last] = *id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200615
616 /* is it our turn? */
Michal Vaskobdcf2362016-07-26 11:35:43 +0200617 while (ps->queue[ps->queue_begin] != *id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200618 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200619 ts.tv_sec += NC_READ_TIMEOUT;
620
621 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
622 if (ret) {
Michal Vasko227f8ff2016-07-26 14:08:59 +0200623 ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200624 /* remove ourselves from the queue */
Michal Vaskobdcf2362016-07-26 11:35:43 +0200625 nc_ps_queue_remove_id(ps, *id);
626 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200627 return -1;
628 }
629 }
630
Michal Vaskobe86fe32016-04-07 10:43:03 +0200631 /* UNLOCK */
632 pthread_mutex_unlock(&ps->lock);
633
634 return 0;
635}
636
Michal Vaskof04a52a2016-04-07 10:52:10 +0200637int
Michal Vasko227f8ff2016-07-26 14:08:59 +0200638nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200639{
640 int ret;
641 struct timespec ts;
642
Radek Krejci7ac16052016-07-15 11:48:18 +0200643 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200644 ts.tv_sec += NC_READ_TIMEOUT;
645
646 /* LOCK */
647 ret = pthread_mutex_timedlock(&ps->lock, &ts);
648 if (ret) {
Michal Vasko227f8ff2016-07-26 14:08:59 +0200649 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200650 ret = -1;
651 }
652
Michal Vaskobdcf2362016-07-26 11:35:43 +0200653 /* we must be the first, it was our turn after all, right? */
654 if (ps->queue[ps->queue_begin] != id) {
655 ERRINT;
656 return -1;
657 }
658
Michal Vaskobe86fe32016-04-07 10:43:03 +0200659 /* remove ourselves from the queue */
Michal Vaskobdcf2362016-07-26 11:35:43 +0200660 nc_ps_queue_remove_id(ps, id);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200661
662 /* broadcast to all other threads that the queue moved */
663 pthread_cond_broadcast(&ps->cond);
664
Michal Vaskobe86fe32016-04-07 10:43:03 +0200665 /* UNLOCK */
666 if (!ret) {
667 pthread_mutex_unlock(&ps->lock);
668 }
669
670 return ret;
671}
672
Michal Vasko428087d2016-01-14 16:04:28 +0100673API struct nc_pollsession *
674nc_ps_new(void)
675{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100676 struct nc_pollsession *ps;
677
678 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100679 if (!ps) {
680 ERRMEM;
681 return NULL;
682 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200683 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100684 pthread_mutex_init(&ps->lock, NULL);
685
686 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100687}
688
689API void
690nc_ps_free(struct nc_pollsession *ps)
691{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100692 if (!ps) {
693 return;
694 }
695
Michal Vaskobe86fe32016-04-07 10:43:03 +0200696 if (ps->queue_len) {
697 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
698 }
699
Michal Vasko3a715132016-01-21 15:40:31 +0100700 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100701 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100702 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200703 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100704
Michal Vasko428087d2016-01-14 16:04:28 +0100705 free(ps);
706}
707
708API int
709nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
710{
Michal Vaskobdcf2362016-07-26 11:35:43 +0200711 uint8_t q_id;
712
Michal Vasko45e53ae2016-04-07 11:46:03 +0200713 if (!ps) {
714 ERRARG("ps");
715 return -1;
716 } else if (!session) {
717 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100718 return -1;
719 }
720
Michal Vasko48a63ed2016-03-01 09:48:21 +0100721 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200722 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200723 return -1;
724 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100725
Michal Vasko428087d2016-01-14 16:04:28 +0100726 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100727 ps->pfds = nc_realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
728 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
729 if (!ps->pfds || !ps->sessions) {
730 ERRMEM;
731 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200732 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100733 return -1;
734 }
Michal Vasko428087d2016-01-14 16:04:28 +0100735
736 switch (session->ti_type) {
737 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100738 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100739 break;
740
Radek Krejci53691be2016-02-22 13:58:37 +0100741#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100742 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100743 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100744 break;
745#endif
746
Radek Krejci53691be2016-02-22 13:58:37 +0100747#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100748 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100749 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100750 break;
751#endif
752
753 default:
754 ERRINT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100755 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200756 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100757 return -1;
758 }
Michal Vasko3a715132016-01-21 15:40:31 +0100759 ps->pfds[ps->session_count - 1].events = POLLIN;
760 ps->pfds[ps->session_count - 1].revents = 0;
761 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100762
Michal Vasko48a63ed2016-03-01 09:48:21 +0100763 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200764 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100765}
766
Michal Vasko48a63ed2016-03-01 09:48:21 +0100767static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100768_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100769{
770 uint16_t i;
771
Radek Krejcid5f978f2016-03-03 13:14:45 +0100772 if (index >= 0) {
773 i = (uint16_t)index;
774 goto remove;
775 }
Michal Vasko428087d2016-01-14 16:04:28 +0100776 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100777 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100778remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100779 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100780 if (i < ps->session_count) {
781 ps->sessions[i] = ps->sessions[ps->session_count];
782 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
783 } else if (!ps->session_count) {
784 free(ps->sessions);
785 ps->sessions = NULL;
786 free(ps->pfds);
787 ps->pfds = NULL;
788 }
Michal Vasko428087d2016-01-14 16:04:28 +0100789 return 0;
790 }
791 }
792
Michal Vaskof0537d82016-01-29 14:42:38 +0100793 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100794}
795
Michal Vasko48a63ed2016-03-01 09:48:21 +0100796API int
797nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
798{
Michal Vaskobdcf2362016-07-26 11:35:43 +0200799 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200800 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100801
Michal Vasko45e53ae2016-04-07 11:46:03 +0200802 if (!ps) {
803 ERRARG("ps");
804 return -1;
805 } else if (!session) {
806 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100807 return -1;
808 }
809
810 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200811 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200812 return -1;
813 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100814
Radek Krejcid5f978f2016-03-03 13:14:45 +0100815 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100816
817 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200818 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100819
Michal Vaskobe86fe32016-04-07 10:43:03 +0200820 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100821}
822
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100823API uint16_t
824nc_ps_session_count(struct nc_pollsession *ps)
825{
Michal Vaskobdcf2362016-07-26 11:35:43 +0200826 uint8_t q_id;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100827 uint16_t count;
828
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100829 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200830 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100831 return 0;
832 }
833
Michal Vasko48a63ed2016-03-01 09:48:21 +0100834 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200835 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200836 return -1;
837 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100838
839 count = ps->session_count;
840
841 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +0200842 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100843
844 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100845}
846
Michal Vasko71090fc2016-05-24 16:37:28 +0200847/* must be called holding the session lock!
848 * returns: NC_PSPOLL_ERROR,
849 * NC_PSPOLL_BAD_RPC,
850 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
851 * NC_PSPOLL_RPC
852 */
853static int
Michal Vasko428087d2016-01-14 16:04:28 +0100854nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
855{
856 struct lyxml_elem *xml = NULL;
857 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200858 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200859 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100860
Michal Vasko45e53ae2016-04-07 11:46:03 +0200861 if (!session) {
862 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200863 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200864 } else if (!rpc) {
865 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200866 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100867 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100868 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200869 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100870 }
871
872 msgtype = nc_read_msg(session, &xml);
873
874 switch (msgtype) {
875 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200876 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100877 if (!*rpc) {
878 ERRMEM;
879 goto error;
880 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100881
Radek Krejcif93c7d42016-04-06 13:41:15 +0200882 ly_errno = LY_SUCCESS;
Michal Vasko68b3f292016-09-16 12:00:32 +0200883 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_RPC | LYD_OPT_DESTRUCT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100884 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200885 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200886 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200887 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200888 nc_server_reply_free(reply);
889 if (ret == -1) {
890 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200891 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200892 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
893 } else {
894 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100895 }
Michal Vasko428087d2016-01-14 16:04:28 +0100896 (*rpc)->root = xml;
897 break;
898 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100899 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200900 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100901 goto error;
902 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100903 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200904 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100905 goto error;
906 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100907 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200908 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100909 goto error;
910 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200911 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100912 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
913 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200914 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100915 break;
916 }
917
Michal Vasko71090fc2016-05-24 16:37:28 +0200918 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100919
920error:
921 /* cleanup */
922 lyxml_free(server_opts.ctx, xml);
923
Michal Vasko71090fc2016-05-24 16:37:28 +0200924 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100925}
926
Michal Vasko71090fc2016-05-24 16:37:28 +0200927/* must be called holding the session lock!
928 * returns: NC_PSPOLL_ERROR,
929 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
930 * NC_PSPOLL_REPLY_ERROR,
931 * 0
932 */
933static int
Michal Vasko428087d2016-01-14 16:04:28 +0100934nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
935{
936 nc_rpc_clb clb;
937 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +0200938 struct lys_node *rpc_act = NULL;
939 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +0200940 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +0100941
Michal Vasko4a827e52016-03-03 10:59:00 +0100942 if (!rpc) {
943 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200944 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +0100945 }
946
Michal Vasko90e8e692016-07-13 12:27:57 +0200947 if (rpc->tree->schema->nodetype == LYS_RPC) {
948 /* RPC */
949 rpc_act = rpc->tree->schema;
950 } else {
951 /* action */
952 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
953 if (elem->schema->nodetype == LYS_ACTION) {
954 rpc_act = elem->schema;
955 break;
956 }
957 LY_TREE_DFS_END(rpc->tree, next, elem);
958 }
959 if (!rpc_act) {
960 ERRINT;
961 return NC_PSPOLL_ERROR;
962 }
963 }
964
965 if (!rpc_act->priv) {
966 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +0100967 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100968 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +0200969 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +0100970 reply = clb(rpc->tree, session);
971 }
972
973 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100974 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100975 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200976 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
977 if (reply->type == NC_RPL_ERROR) {
978 ret |= NC_PSPOLL_REPLY_ERROR;
979 }
980 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +0100981
Michal Vasko71090fc2016-05-24 16:37:28 +0200982 if (r == -1) {
983 ERR("Session %u: failed to write reply.", session->id);
984 ret |= NC_PSPOLL_ERROR;
985 }
Michal Vasko428087d2016-01-14 16:04:28 +0100986
987 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
988 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
989 session->status = NC_STATUS_INVALID;
990 }
991
Michal Vasko71090fc2016-05-24 16:37:28 +0200992 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100993}
994
995API int
Michal Vasko71090fc2016-05-24 16:37:28 +0200996nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +0100997{
998 int ret;
Michal Vaskobdcf2362016-07-26 11:35:43 +0200999 uint8_t q_id;
Michal Vasko3512e402016-01-28 16:22:34 +01001000 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001001 time_t cur_time;
Michal Vasko71090fc2016-05-24 16:37:28 +02001002 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +01001003 struct nc_server_rpc *rpc = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +01001004
1005 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001006 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001007 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001008 }
1009
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001010 cur_time = time(NULL);
1011
Michal Vasko48a63ed2016-03-01 09:48:21 +01001012 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001013 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001014 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001015 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001016
Michal Vasko428087d2016-01-14 16:04:28 +01001017 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001018 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1019 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001020 ret = NC_PSPOLL_ERROR;
1021 if (session) {
1022 *session = ps->sessions[i];
1023 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001024 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001025 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001026
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001027 /* TODO invalidate only sessions without subscription */
Michal Vasko15dfe722016-07-28 15:47:25 +02001028 if (server_opts.idle_timeout && (cur_time >= ps->sessions[i]->last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001029 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1030 ps->sessions[i]->status = NC_STATUS_INVALID;
1031 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001032 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1033 if (session) {
1034 *session = ps->sessions[i];
1035 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001036 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001037 }
1038
Michal Vasko3a715132016-01-21 15:40:31 +01001039 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +01001040 break;
1041 }
Michal Vasko428087d2016-01-14 16:04:28 +01001042 }
1043
Michal Vaskobd8ef262016-01-20 11:09:27 +01001044 if (i == ps->session_count) {
Radek Krejci53691be2016-02-22 13:58:37 +01001045#ifdef NC_ENABLED_SSH
Michal Vasko3a715132016-01-21 15:40:31 +01001046retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +01001047#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +01001048 /* no leftover event */
1049 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +01001050 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001051 if (ret < 0) {
1052 ERR("Poll failed (%s).", strerror(errno));
1053 ret = NC_PSPOLL_ERROR;
1054 goto finish;
1055 } else if (!ret) {
1056 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001057 goto finish;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001058 }
Michal Vasko428087d2016-01-14 16:04:28 +01001059 }
1060
Michal Vaskobd8ef262016-01-20 11:09:27 +01001061 /* find the first fd with POLLIN, we don't care if there are more now */
1062 for (; i < ps->session_count; ++i) {
Michal Vasko46eac552016-05-30 15:27:25 +02001063 if (ps->pfds[i].revents & (POLLHUP | POLLNVAL)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001064 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
1065 ps->sessions[i]->status = NC_STATUS_INVALID;
1066 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko71090fc2016-05-24 16:37:28 +02001067 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1068 if (session) {
1069 *session = ps->sessions[i];
1070 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001071 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001072 } else if (ps->pfds[i].revents & POLLERR) {
1073 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
1074 ps->sessions[i]->status = NC_STATUS_INVALID;
1075 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko71090fc2016-05-24 16:37:28 +02001076 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1077 if (session) {
1078 *session = ps->sessions[i];
1079 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001080 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001081 } else if (ps->pfds[i].revents & POLLIN) {
Radek Krejci53691be2016-02-22 13:58:37 +01001082#ifdef NC_ENABLED_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +01001083 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +01001084 uint16_t j;
1085
Michal Vasko96164bf2016-01-21 15:41:58 +01001086 /* things are not that simple with SSH... */
Michal Vasko62be1ce2016-03-03 13:24:52 +01001087 ret = nc_ssh_pollin(ps->sessions[i], timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +01001088
1089 /* clear POLLIN on sessions sharing this session's SSH session */
Michal Vasko71090fc2016-05-24 16:37:28 +02001090 if (ret & (NC_PSPOLL_RPC | NC_PSPOLL_SSH_MSG | NC_PSPOLL_SSH_CHANNEL)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001091 for (j = i + 1; j < ps->session_count; ++j) {
1092 if (ps->pfds[j].fd == ps->pfds[i].fd) {
1093 ps->pfds[j].revents = 0;
1094 }
1095 }
1096 }
1097
Michal Vasko71090fc2016-05-24 16:37:28 +02001098 /* SSH message only */
1099 if (!(ret & (NC_PSPOLL_RPC | NC_PSPOLL_PENDING))) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001100 ps->pfds[i].revents = 0;
Michal Vasko71090fc2016-05-24 16:37:28 +02001101 if (session) {
1102 *session = ps->sessions[i];
1103 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001104 goto finish;
Michal Vasko96164bf2016-01-21 15:41:58 +01001105
1106 /* event occurred on some other channel */
Michal Vasko71090fc2016-05-24 16:37:28 +02001107 } else if (ret & NC_PSPOLL_PENDING) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001108 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001109 if (i == ps->session_count - 1) {
1110 /* last session and it is not the right channel, ... */
Michal Vasko8c748832016-02-03 15:32:16 +01001111 if (!timeout) {
Michal Vasko428087d2016-01-14 16:04:28 +01001112 /* ... timeout is 0, so that is it */
Michal Vasko71090fc2016-05-24 16:37:28 +02001113 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001114 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001115 }
Michal Vasko8c748832016-02-03 15:32:16 +01001116 /* ... retry polling reasonable time apart ... */
1117 usleep(NC_TIMEOUT_STEP);
1118 if (timeout > 0) {
1119 /* ... and decrease timeout, if not -1 */
Michal Vasko7b38e232016-02-26 15:01:07 +01001120 timeout -= NC_TIMEOUT_STEP * 1000;
Michal Vasko8c748832016-02-03 15:32:16 +01001121 }
1122 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +01001123 }
1124 /* check other sessions */
1125 continue;
Michal Vasko428087d2016-01-14 16:04:28 +01001126 }
1127 }
Radek Krejci53691be2016-02-22 13:58:37 +01001128#endif /* NC_ENABLED_SSH */
Michal Vasko428087d2016-01-14 16:04:28 +01001129
Michal Vaskobd8ef262016-01-20 11:09:27 +01001130 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +01001131 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001132 break;
1133 }
1134 }
1135
1136 if (i == ps->session_count) {
1137 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001138 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001139 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001140 }
1141
1142 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001143 cur_session = ps->sessions[i];
1144 if (session) {
1145 *session = cur_session;
1146 }
Michal Vasko428087d2016-01-14 16:04:28 +01001147
Michal Vaskobd8ef262016-01-20 11:09:27 +01001148 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal vasko50cc94f2016-10-04 13:46:20 +02001149 ret = nc_timedlock(cur_session->ti_lock, timeout, __func__);
Michal Vasko71090fc2016-05-24 16:37:28 +02001150 if (ret < 0) {
1151 ret = NC_PSPOLL_ERROR;
1152 goto finish;
1153 } else if (!ret) {
1154 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001155 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001156 }
1157
Michal Vasko71090fc2016-05-24 16:37:28 +02001158 ret = nc_recv_rpc(cur_session, &rpc);
1159 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1160 pthread_mutex_unlock(cur_session->ti_lock);
1161 if (cur_session->status != NC_STATUS_RUNNING) {
1162 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001163 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001164 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001165 }
1166
Michal Vasko71090fc2016-05-24 16:37:28 +02001167 cur_session->last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001168
Michal Vasko428087d2016-01-14 16:04:28 +01001169 /* process RPC */
Michal Vasko71090fc2016-05-24 16:37:28 +02001170 ret |= nc_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001171
Michal Vasko71090fc2016-05-24 16:37:28 +02001172 pthread_mutex_unlock(cur_session->ti_lock);
1173 if (cur_session->status != NC_STATUS_RUNNING) {
1174 ret |= NC_PSPOLL_SESSION_TERM;
1175 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1176 ret |= NC_PSPOLL_SESSION_ERROR;
1177 }
Michal Vasko428087d2016-01-14 16:04:28 +01001178 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001179
Michal Vaskoca4a2422016-02-02 12:17:14 +01001180 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001181
1182 /* is there some other socket waiting? */
1183 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001184 if (ps->pfds[i].revents) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001185 ret |= NC_PSPOLL_PENDING;
1186 break;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001187 }
1188 }
1189
Michal Vasko48a63ed2016-03-01 09:48:21 +01001190finish:
1191 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001192 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001193 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001194}
1195
Michal Vaskod09eae62016-02-01 10:32:52 +01001196API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001197nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001198{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001199 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001200 uint16_t i;
1201 struct nc_session *session;
1202
Michal Vasko9a25e932016-02-01 10:36:42 +01001203 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001204 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001205 return;
1206 }
1207
Michal Vasko48a63ed2016-03-01 09:48:21 +01001208 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001209 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001210 return;
1211 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001212
Michal Vasko48a63ed2016-03-01 09:48:21 +01001213 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001214 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001215 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001216 }
1217 free(ps->sessions);
1218 ps->sessions = NULL;
1219 free(ps->pfds);
1220 ps->pfds = NULL;
1221 ps->session_count = 0;
1222 } else {
1223 for (i = 0; i < ps->session_count; ) {
1224 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1225 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001226 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001227 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001228 continue;
1229 }
1230
1231 ++i;
1232 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001233 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001234
1235 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001236 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001237}
1238
Radek Krejci53691be2016-02-22 13:58:37 +01001239#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001240
Michal Vaskoe2713da2016-08-22 16:06:40 +02001241API int
1242nc_server_add_endpt(const char *name)
Michal Vasko9e036d52016-01-08 10:49:26 +01001243{
Michal Vasko3031aae2016-01-27 16:07:18 +01001244 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +01001245#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001246 uint16_t bind_ssh_idx;
1247#endif
1248#ifdef NC_ENABLED_TLS
1249 uint16_t bind_tls_idx;
Michal Vasko08a629a2016-02-02 12:20:47 +01001250#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001251
Michal Vasko45e53ae2016-04-07 11:46:03 +02001252 if (!name) {
1253 ERRARG("name");
1254 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001255 }
1256
Michal Vasko51e514d2016-02-02 15:51:52 +01001257 /* WRITE LOCK */
1258 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001259
1260 /* check name uniqueness */
1261 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001262 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001263 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001264 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +01001265 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001266 return -1;
1267 }
1268 }
1269
Michal Vasko3031aae2016-01-27 16:07:18 +01001270 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001271 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001272 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001273 ERRMEM;
1274 /* WRITE UNLOCK */
1275 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001276 return -1;
1277 }
1278 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1279
1280#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1281 server_opts.binds = nc_realloc(server_opts.binds, 2 * server_opts.endpt_count * sizeof *server_opts.binds);
1282 bind_ssh_idx = (server_opts.endpt_count - 1) * 2;
1283 bind_tls_idx = (server_opts.endpt_count - 1) * 2 + 1;
1284#else
1285 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
1286# ifdef NC_ENABLED_SSH
1287 bind_ssh_idx = server_opts.endpt_count - 1;
1288# endif
1289# ifdef NC_ENABLED_TLS
1290 bind_tls_idx = server_opts.endpt_count - 1;
1291# endif
1292#endif
1293 if (!server_opts.binds) {
1294 ERRMEM;
1295 /* WRITE UNLOCK */
1296 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001297 return -1;
1298 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001299
Radek Krejci53691be2016-02-22 13:58:37 +01001300#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001301 server_opts.binds[bind_ssh_idx].address = NULL;
1302 server_opts.binds[bind_ssh_idx].port = 0;
1303 server_opts.binds[bind_ssh_idx].sock = -1;
1304 server_opts.binds[bind_ssh_idx].ti = NC_TI_LIBSSH;
Michal Vasko08a629a2016-02-02 12:20:47 +01001305
Michal Vaskoe2713da2016-08-22 16:06:40 +02001306 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts = calloc(1, sizeof(struct nc_server_ssh_opts));
1307 if (!server_opts.endpts[server_opts.endpt_count - 1].ssh_opts) {
1308 ERRMEM;
1309 /* WRITE UNLOCK */
1310 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1311 return -1;
Michal Vasko3031aae2016-01-27 16:07:18 +01001312 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001313 /* set default values */
1314 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_methods =
1315 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1316 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_attempts = 3;
1317 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_timeout = 10;
1318#endif
1319
1320#ifdef NC_ENABLED_TLS
1321 server_opts.binds[bind_tls_idx].address = NULL;
1322 server_opts.binds[bind_tls_idx].port = 0;
1323 server_opts.binds[bind_tls_idx].sock = -1;
1324 server_opts.binds[bind_tls_idx].ti = NC_TI_OPENSSL;
1325
1326 server_opts.endpts[server_opts.endpt_count - 1].tls_opts = calloc(1, sizeof(struct nc_server_tls_opts));
1327 if (!server_opts.endpts[server_opts.endpt_count - 1].tls_opts) {
1328 ERRMEM;
1329 /* WRITE UNLOCK */
1330 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1331 return -1;
1332 }
1333#endif
1334
Michal Vasko3031aae2016-01-27 16:07:18 +01001335 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001336
Michal Vasko3031aae2016-01-27 16:07:18 +01001337 /* WRITE UNLOCK */
1338 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001339
Michal Vasko9e036d52016-01-08 10:49:26 +01001340 return 0;
1341}
1342
Michal Vasko3031aae2016-01-27 16:07:18 +01001343int
Michal Vaskoda514772016-02-01 11:32:01 +01001344nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1345{
1346 struct nc_endpt *endpt;
1347 struct nc_bind *bind = NULL;
1348 uint16_t i;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001349 int sock = -1, set_addr;
Michal Vaskoda514772016-02-01 11:32:01 +01001350
Michal Vasko45e53ae2016-04-07 11:46:03 +02001351 if (!endpt_name) {
1352 ERRARG("endpt_name");
1353 return -1;
1354 } else if ((!address && !port) || (address && port)) {
1355 ERRARG("address and port");
1356 return -1;
1357 } else if (!ti) {
1358 ERRARG("ti");
Michal Vaskoda514772016-02-01 11:32:01 +01001359 return -1;
1360 }
1361
Michal Vaskoe2713da2016-08-22 16:06:40 +02001362 if (address) {
1363 set_addr = 1;
1364 } else {
1365 set_addr = 0;
1366 }
1367
Michal Vasko51e514d2016-02-02 15:51:52 +01001368 /* LOCK */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001369 endpt = nc_server_endpt_lock(endpt_name, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001370 if (!endpt) {
1371 return -1;
1372 }
1373
Michal Vaskoe2713da2016-08-22 16:06:40 +02001374#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1375 if (ti == NC_TI_LIBSSH) {
1376 bind = &server_opts.binds[2 * i];
1377 } else {
1378 bind = &server_opts.binds[2 * i + 1];
Michal Vaskoda514772016-02-01 11:32:01 +01001379 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001380#else
1381 bind = &server_opts.binds[i];
Michal Vasko5e391372016-08-22 16:20:58 +02001382 if (bind->ti != ti) {
1383 ERRINT;
1384 goto fail;
1385 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001386#endif
Michal Vaskoda514772016-02-01 11:32:01 +01001387 if (!bind) {
1388 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001389 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001390 }
1391
Michal Vaskoe2713da2016-08-22 16:06:40 +02001392 if (set_addr) {
1393 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001394 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001395 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001396 }
1397
Michal Vaskoe2713da2016-08-22 16:06:40 +02001398 /* we have all the information we need to create a listening socket */
1399 if (address && port) {
1400 /* create new socket, close the old one */
1401 sock = nc_sock_listen(address, port);
1402 if (sock == -1) {
1403 goto fail;
1404 }
1405
1406 if (bind->sock > -1) {
1407 close(bind->sock);
1408 }
1409 bind->sock = sock;
1410 } /* else we are just setting address or port */
1411
1412 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001413 lydict_remove(server_opts.ctx, bind->address);
1414 bind->address = lydict_insert(server_opts.ctx, address, 0);
1415 } else {
1416 bind->port = port;
1417 }
1418
Michal Vaskoe2713da2016-08-22 16:06:40 +02001419 if (sock > -1) {
1420#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1421 VRB("Listening on %s:%u for %s connections.", address, port, (ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
1422#elif defined(NC_ENABLED_SSH)
1423 VRB("Listening on %s:%u for SSH connections.", address, port);
1424#else
1425 VRB("Listening on %s:%u for TLS connections.", address, port);
1426#endif
1427 }
1428
Michal Vasko51e514d2016-02-02 15:51:52 +01001429 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001430 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001431 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001432
1433fail:
1434 /* UNLOCK */
1435 nc_server_endpt_unlock(endpt);
1436 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001437}
1438
Michal Vaskoe2713da2016-08-22 16:06:40 +02001439API int
1440nc_server_del_endpt(const char *name)
Michal Vasko9e036d52016-01-08 10:49:26 +01001441{
1442 uint32_t i;
1443 int ret = -1;
1444
Michal Vasko3031aae2016-01-27 16:07:18 +01001445 /* WRITE LOCK */
1446 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001447
Michal Vaskoe2713da2016-08-22 16:06:40 +02001448 if (!name) {
1449 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001450 for (i = 0; i < server_opts.endpt_count; ++i) {
1451 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko3031aae2016-01-27 16:07:18 +01001452 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
Radek Krejci53691be2016-02-22 13:58:37 +01001453#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001454 nc_server_ssh_clear_opts(server_opts.endpts[i].ssh_opts);
1455 free(server_opts.endpts[i].ssh_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001456#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001457#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001458 nc_server_tls_clear_opts(server_opts.endpts[i].tls_opts);
1459 free(server_opts.endpts[i].tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001460#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001461 ret = 0;
1462 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001463 free(server_opts.endpts);
1464 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001465
1466 /* remove all binds */
1467 for (i = 0; i < server_opts.endpt_count; ++i) {
1468 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1469 if (server_opts.binds[i].sock > -1) {
1470 close(server_opts.binds[i].sock);
1471 }
1472 }
1473#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1474 for (; i < 2 * server_opts.endpt_count; ++i) {
1475 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1476 if (server_opts.binds[i].sock > -1) {
1477 close(server_opts.binds[i].sock);
1478 }
1479 }
1480#endif
1481 free(server_opts.binds);
1482 server_opts.binds = NULL;
1483
Michal Vasko3031aae2016-01-27 16:07:18 +01001484 server_opts.endpt_count = 0;
1485
Michal Vasko1a38c862016-01-15 15:50:07 +01001486 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001487 /* remove one endpoint with bind(s) */
Michal Vasko3031aae2016-01-27 16:07:18 +01001488 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001489 if (!strcmp(server_opts.endpts[i].name, name)) {
1490 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001491 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko3031aae2016-01-27 16:07:18 +01001492 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
Radek Krejci53691be2016-02-22 13:58:37 +01001493#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001494 nc_server_ssh_clear_opts(server_opts.endpts[i].ssh_opts);
1495 free(server_opts.endpts[i].ssh_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001496#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001497#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001498 nc_server_tls_clear_opts(server_opts.endpts[i].tls_opts);
1499 free(server_opts.endpts[i].tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001500#endif
Michal Vasko1a38c862016-01-15 15:50:07 +01001501
Michal Vaskoe2713da2016-08-22 16:06:40 +02001502 /* remove bind(s) */
1503#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1504 i *= 2;
1505 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1506 if (server_opts.binds[i].sock > -1) {
1507 close(server_opts.binds[i].sock);
1508 }
1509 ++i;
1510#endif
1511 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1512 if (server_opts.binds[i].sock > -1) {
1513 close(server_opts.binds[i].sock);
1514 }
1515
1516 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001517 --server_opts.endpt_count;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001518#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1519 --i;
1520 i /= 2;
1521 if (i < server_opts.endpt_count) {
1522 memcpy(&server_opts.binds[2 * i], &server_opts.binds[2 * server_opts.endpt_count], 2 * sizeof *server_opts.binds);
1523 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1524 }
1525#else
Michal Vaskoc0256492016-02-02 12:19:06 +01001526 if (i < server_opts.endpt_count) {
1527 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1528 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001529 }
1530#endif
1531 else if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001532 free(server_opts.binds);
1533 server_opts.binds = NULL;
1534 free(server_opts.endpts);
1535 server_opts.endpts = NULL;
1536 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001537
1538 ret = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001539 break;
Michal Vasko1a38c862016-01-15 15:50:07 +01001540 }
1541 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001542 }
1543
Michal Vasko3031aae2016-01-27 16:07:18 +01001544 /* WRITE UNLOCK */
1545 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001546
Michal Vasko9e036d52016-01-08 10:49:26 +01001547 return ret;
1548}
1549
Michal Vasko71090fc2016-05-24 16:37:28 +02001550API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001551nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001552{
Michal Vasko71090fc2016-05-24 16:37:28 +02001553 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001554 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001555 char *host = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001556 uint16_t port, endpt_idx, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001557
Michal Vasko45e53ae2016-04-07 11:46:03 +02001558 if (!server_opts.ctx) {
1559 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001560 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001561 } else if (!session) {
1562 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001563 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001564 }
1565
Michal Vasko51e514d2016-02-02 15:51:52 +01001566 /* we have to hold WRITE for the whole time, since there is not
1567 * a way of downgrading the lock to READ */
1568 /* WRITE LOCK */
1569 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1570
1571 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001572 ERR("No endpoints to accept sessions on.");
Michal Vasko51e514d2016-02-02 15:51:52 +01001573 /* WRITE UNLOCK */
1574 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001575 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001576 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001577
Michal Vasko94acafc2016-09-23 13:40:10 +02001578#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1579 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count * 2, timeout, &host, &port, &bind_idx);
1580#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001581 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko94acafc2016-09-23 13:40:10 +02001582#endif
Michal Vaskob48aa812016-01-18 14:13:09 +01001583
Michal Vasko50456e82016-02-02 12:16:08 +01001584 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001585 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001586 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001587 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001588 if (!ret) {
1589 return NC_MSG_WOULDBLOCK;
1590 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001591 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001592 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001593 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001594
Michal Vasko1a38c862016-01-15 15:50:07 +01001595 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001596 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001597 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001598 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001599 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001600 msgtype = NC_MSG_ERROR;
1601 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001602 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001603 (*session)->status = NC_STATUS_STARTING;
1604 (*session)->side = NC_SERVER;
1605 (*session)->ctx = server_opts.ctx;
1606 (*session)->flags = NC_SESSION_SHAREDCTX;
1607 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1608 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001609
1610 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001611 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1612 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001613 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001614 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001615 msgtype = NC_MSG_ERROR;
1616 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001617 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001618 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001619
Michal Vaskoe2713da2016-08-22 16:06:40 +02001620 endpt_idx = bind_idx;
1621 /* transform index as needed */
1622#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1623 if (server_opts.binds[bind_idx].ti == NC_TI_OPENSSL) {
1624 --endpt_idx;
1625 }
1626 endpt_idx /= 2;
1627#endif
Michal Vasko3031aae2016-01-27 16:07:18 +01001628
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001629 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001630#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001631 if (server_opts.binds[bind_idx].ti == NC_TI_LIBSSH) {
1632 (*session)->data = server_opts.endpts[endpt_idx].ssh_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001633 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001634 if (ret < 0) {
1635 msgtype = NC_MSG_ERROR;
1636 goto cleanup;
1637 } else if (!ret) {
1638 msgtype = NC_MSG_WOULDBLOCK;
1639 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001640 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001641 } else
1642#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001643#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001644 if (server_opts.binds[bind_idx].ti == NC_TI_OPENSSL) {
1645 (*session)->data = server_opts.endpts[endpt_idx].tls_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001646 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001647 if (ret < 0) {
1648 msgtype = NC_MSG_ERROR;
1649 goto cleanup;
1650 } else if (!ret) {
1651 msgtype = NC_MSG_WOULDBLOCK;
1652 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001653 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001654 } else
1655#endif
1656 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001657 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001658 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001659 msgtype = NC_MSG_ERROR;
1660 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001661 }
1662
Michal Vasko2cc4c682016-03-01 09:16:48 +01001663 (*session)->data = NULL;
1664
Michal Vasko51e514d2016-02-02 15:51:52 +01001665 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001666 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1667
Michal Vaskob48aa812016-01-18 14:13:09 +01001668 /* assign new SID atomically */
1669 /* LOCK */
1670 pthread_spin_lock(&server_opts.sid_lock);
1671 (*session)->id = server_opts.new_session_id++;
1672 /* UNLOCK */
1673 pthread_spin_unlock(&server_opts.sid_lock);
1674
Michal Vasko9e036d52016-01-08 10:49:26 +01001675 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001676 msgtype = nc_handshake(*session);
1677 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001678 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001679 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001680 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001681 }
Michal Vasko2f975cf2016-07-28 15:47:00 +02001682 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001683 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001684
Michal Vasko71090fc2016-05-24 16:37:28 +02001685 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001686
Michal Vasko71090fc2016-05-24 16:37:28 +02001687cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001688 /* WRITE UNLOCK */
1689 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1690
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001691 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001692 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001693 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001694}
1695
Michal Vasko71090fc2016-05-24 16:37:28 +02001696NC_MSG_TYPE
Michal Vasko8f5270d2016-02-29 16:22:25 +01001697nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01001698{
Michal Vasko71090fc2016-05-24 16:37:28 +02001699 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001700 int sock, ret;
1701
Michal Vasko45e53ae2016-04-07 11:46:03 +02001702 if (!host) {
1703 ERRARG("host");
Michal Vasko71090fc2016-05-24 16:37:28 +02001704 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001705 } else if (!port) {
1706 ERRARG("port");
Michal Vasko71090fc2016-05-24 16:37:28 +02001707 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001708 } else if (!ti) {
1709 ERRARG("ti");
Michal Vasko71090fc2016-05-24 16:37:28 +02001710 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001711 } else if (!session) {
1712 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001713 return NC_MSG_ERROR;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001714 }
1715
Michal Vaskob05053d2016-01-22 16:12:06 +01001716 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001717 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001718 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001719 }
1720
1721 *session = calloc(1, sizeof **session);
1722 if (!(*session)) {
1723 ERRMEM;
1724 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001725 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001726 }
1727 (*session)->status = NC_STATUS_STARTING;
1728 (*session)->side = NC_SERVER;
1729 (*session)->ctx = server_opts.ctx;
1730 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001731 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001732 (*session)->port = port;
1733
1734 /* transport lock */
1735 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1736 if (!(*session)->ti_lock) {
1737 ERRMEM;
1738 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001739 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001740 goto fail;
1741 }
1742 pthread_mutex_init((*session)->ti_lock, NULL);
1743
1744 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001745#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001746 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001747 /* OPTS LOCK */
1748 pthread_mutex_lock(&ssh_ch_opts_lock);
1749
Michal Vasko2cc4c682016-03-01 09:16:48 +01001750 (*session)->data = &ssh_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001751 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001752 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001753
1754 /* OPTS UNLOCK */
1755 pthread_mutex_unlock(&ssh_ch_opts_lock);
1756
Michal Vasko71090fc2016-05-24 16:37:28 +02001757 if (ret < 0) {
1758 msgtype = NC_MSG_ERROR;
1759 goto fail;
1760 } else if (!ret) {
1761 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001762 goto fail;
1763 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001764 } else
1765#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001766#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001767 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001768 /* OPTS LOCK */
1769 pthread_mutex_lock(&tls_ch_opts_lock);
1770
Michal Vasko2cc4c682016-03-01 09:16:48 +01001771 (*session)->data = &tls_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001772 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001773 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001774
1775 /* OPTS UNLOCK */
1776 pthread_mutex_unlock(&tls_ch_opts_lock);
1777
Michal Vasko71090fc2016-05-24 16:37:28 +02001778 if (ret < 0) {
1779 msgtype = NC_MSG_ERROR;
1780 goto fail;
1781 } else if (!ret) {
1782 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001783 goto fail;
1784 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001785 } else
1786#endif
1787 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001788 ERRINT;
1789 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001790 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001791 goto fail;
1792 }
1793
1794 /* assign new SID atomically */
1795 /* LOCK */
1796 pthread_spin_lock(&server_opts.sid_lock);
1797 (*session)->id = server_opts.new_session_id++;
1798 /* UNLOCK */
1799 pthread_spin_unlock(&server_opts.sid_lock);
1800
1801 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001802 msgtype = nc_handshake(*session);
1803 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01001804 goto fail;
1805 }
Michal Vasko2f975cf2016-07-28 15:47:00 +02001806 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001807 (*session)->status = NC_STATUS_RUNNING;
1808
Michal Vasko71090fc2016-05-24 16:37:28 +02001809 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001810
1811fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001812 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001813 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001814 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001815}
1816
Radek Krejci53691be2016-02-22 13:58:37 +01001817#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02001818
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001819API time_t
1820nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02001821{
1822 if (!session) {
1823 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001824 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02001825 }
1826
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001827 return session->session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02001828}