blob: 61f954d0f699271a37c6c61f750255375e2b090e [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
fanchanghu966f2de2016-07-21 02:28:57 -040036static nc_rpc_clb global_rpc_clb = NULL;
37
Michal Vasko3031aae2016-01-27 16:07:18 +010038extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010039extern pthread_mutex_t ssh_ch_opts_lock;
40
Michal Vasko3031aae2016-01-27 16:07:18 +010041extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010042extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010043
44struct nc_endpt *
Michal Vaskoe2713da2016-08-22 16:06:40 +020045nc_server_endpt_lock(const char *name, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010046{
47 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010048 struct nc_endpt *endpt = NULL;
49
50 /* READ LOCK */
51 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010052
53 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +020054 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010055 endpt = &server_opts.endpts[i];
56 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010057 }
58 }
59
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010060 if (!endpt) {
61 ERR("Endpoint \"%s\" was not found.", name);
62 /* READ UNLOCK */
63 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
64 return NULL;
65 }
66
67 /* ENDPT LOCK */
68 pthread_mutex_lock(&endpt->endpt_lock);
69
Michal Vaskoe2713da2016-08-22 16:06:40 +020070 if (idx) {
71 *idx = i;
72 }
73
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010074 return endpt;
75}
76
77void
78nc_server_endpt_unlock(struct nc_endpt *endpt)
79{
80 /* ENDPT UNLOCK */
81 pthread_mutex_unlock(&endpt->endpt_lock);
82
83 /* READ UNLOCK */
Michal Vasko27562ad2016-02-02 15:50:39 +010084 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010085}
Michal Vasko086311b2016-01-08 09:53:11 +010086
Michal Vasko1a38c862016-01-15 15:50:07 +010087API void
88nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
89{
Michal Vasko45e53ae2016-04-07 11:46:03 +020090 if (!session) {
91 ERRARG("session");
92 return;
93 } else if (!reason) {
94 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +010095 return;
96 }
97
98 session->term_reason = reason;
99}
100
Michal Vasko086311b2016-01-08 09:53:11 +0100101int
Michal Vaskof05562c2016-01-20 12:06:43 +0100102nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100103{
104 const int optVal = 1;
105 const socklen_t optLen = sizeof(optVal);
106 int is_ipv4, sock;
107 struct sockaddr_storage saddr;
108
109 struct sockaddr_in *saddr4;
110 struct sockaddr_in6 *saddr6;
111
112
113 if (!strchr(address, ':')) {
114 is_ipv4 = 1;
115 } else {
116 is_ipv4 = 0;
117 }
118
119 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
120 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100121 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100122 goto fail;
123 }
124
125 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100126 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100127 goto fail;
128 }
129
130 bzero(&saddr, sizeof(struct sockaddr_storage));
131 if (is_ipv4) {
132 saddr4 = (struct sockaddr_in *)&saddr;
133
134 saddr4->sin_family = AF_INET;
135 saddr4->sin_port = htons(port);
136
137 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100138 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100139 goto fail;
140 }
141
142 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100143 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100144 goto fail;
145 }
146
147 } else {
148 saddr6 = (struct sockaddr_in6 *)&saddr;
149
150 saddr6->sin6_family = AF_INET6;
151 saddr6->sin6_port = htons(port);
152
153 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100154 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100155 goto fail;
156 }
157
158 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100159 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100160 goto fail;
161 }
162 }
163
Michal Vaskofb89d772016-01-08 12:25:35 +0100164 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100165 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100166 goto fail;
167 }
168
169 return sock;
170
171fail:
172 if (sock > -1) {
173 close(sock);
174 }
175
176 return -1;
177}
178
179int
Michal Vasko3031aae2016-01-27 16:07:18 +0100180nc_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 +0100181{
182 uint16_t i;
183 struct pollfd *pfd;
184 struct sockaddr_storage saddr;
185 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100186 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100187
188 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100189 if (!pfd) {
190 ERRMEM;
191 return -1;
192 }
193
Michal Vasko94acafc2016-09-23 13:40:10 +0200194 i = 0;
195 while (i < bind_count) {
196 if (binds[i].sock < 0) {
197 /* invalid socket */
198 --bind_count;
199 continue;
200 }
Michal Vasko086311b2016-01-08 09:53:11 +0100201 pfd[i].fd = binds[i].sock;
202 pfd[i].events = POLLIN;
203 pfd[i].revents = 0;
Michal Vasko94acafc2016-09-23 13:40:10 +0200204
205 ++i;
Michal Vasko086311b2016-01-08 09:53:11 +0100206 }
207
208 /* poll for a new connection */
209 errno = 0;
210 ret = poll(pfd, bind_count, timeout);
211 if (!ret) {
212 /* we timeouted */
213 free(pfd);
214 return 0;
215 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100216 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100217 free(pfd);
218 return -1;
219 }
220
221 for (i = 0; i < bind_count; ++i) {
222 if (pfd[i].revents & POLLIN) {
223 sock = pfd[i].fd;
224 break;
225 }
226 }
227 free(pfd);
228
229 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100230 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100231 return -1;
232 }
233
234 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100235 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100236 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100237 return -1;
238 }
239
Michal Vasko0190bc32016-03-02 15:47:49 +0100240 /* make the socket non-blocking */
241 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
242 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100243 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100244 return -1;
245 }
246
Michal Vasko3031aae2016-01-27 16:07:18 +0100247 if (idx) {
248 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100249 }
250
Michal Vasko086311b2016-01-08 09:53:11 +0100251 /* host was requested */
252 if (host) {
253 if (saddr.ss_family == AF_INET) {
254 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100255 if (*host) {
256 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
257 ERR("inet_ntop failed (%s).", strerror(errno));
258 free(*host);
259 *host = NULL;
260 }
Michal Vasko086311b2016-01-08 09:53:11 +0100261
Michal Vasko4eb3c312016-03-01 14:09:37 +0100262 if (port) {
263 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
264 }
265 } else {
266 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100267 }
268 } else if (saddr.ss_family == AF_INET6) {
269 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100270 if (*host) {
271 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
272 ERR("inet_ntop failed (%s).", strerror(errno));
273 free(*host);
274 *host = NULL;
275 }
Michal Vasko086311b2016-01-08 09:53:11 +0100276
Michal Vasko4eb3c312016-03-01 14:09:37 +0100277 if (port) {
278 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
279 }
280 } else {
281 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100282 }
283 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100284 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100285 }
286 }
287
288 return ret;
289}
290
Michal Vasko05ba9df2016-01-13 14:40:27 +0100291static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100292nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100293{
294 const char *identifier = NULL, *version = NULL, *format = NULL;
295 char *model_data = NULL;
296 const struct lys_module *module;
297 struct nc_server_error *err;
298 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100299 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100300
301 LY_TREE_FOR(rpc->child, child) {
302 if (!strcmp(child->schema->name, "identifier")) {
303 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
304 } else if (!strcmp(child->schema->name, "version")) {
305 version = ((struct lyd_node_leaf_list *)child)->value_str;
306 } else if (!strcmp(child->schema->name, "format")) {
307 format = ((struct lyd_node_leaf_list *)child)->value_str;
308 }
309 }
310
311 /* check version */
312 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100313 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
314 nc_err_set_msg(err, "The requested version is not supported.", "en");
315 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100316 }
317
318 /* check and get module with the name identifier */
319 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
320 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200321 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
322 }
323 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100324 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
325 nc_err_set_msg(err, "The requested schema was not found.", "en");
326 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100327 }
328
329 /* check format */
Radek Krejci90fba642016-12-07 15:59:45 +0100330 if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100331 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
Radek Krejci90fba642016-12-07 15:59:45 +0100332 } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100333 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
334 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100335 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
336 nc_err_set_msg(err, "The requested format is not supported.", "en");
337 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100338 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200339 if (!model_data) {
340 ERRINT;
341 return NULL;
342 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343
Michal Vasko303245c2016-03-24 15:20:03 +0100344 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200345 if (!sdata) {
346 ERRINT;
347 free(model_data);
348 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100349 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200350
Radek Krejci539efb62016-08-24 15:05:16 +0200351 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
352 LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100353 if (!data) {
354 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200355 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100356 return NULL;
357 }
358
Radek Krejci36dfdb32016-09-01 16:56:35 +0200359 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100360}
361
362static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100363nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100364{
Michal Vasko428087d2016-01-14 16:04:28 +0100365 session->term_reason = NC_SESSION_TERM_CLOSED;
366 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100367}
368
Michal Vasko086311b2016-01-08 09:53:11 +0100369API int
370nc_server_init(struct ly_ctx *ctx)
371{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100372 const struct lys_node *rpc;
373
Michal Vasko086311b2016-01-08 09:53:11 +0100374 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200375 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100376 return -1;
377 }
378
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100379 nc_init();
380
Michal Vasko05ba9df2016-01-13 14:40:27 +0100381 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100382 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100383 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100384 lys_set_private(rpc, nc_clb_default_get_schema);
385 }
386
387 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100388 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100389 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100390 lys_set_private(rpc, nc_clb_default_close_session);
391 }
392
Michal Vasko086311b2016-01-08 09:53:11 +0100393 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100394
395 server_opts.new_session_id = 1;
396 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
397
Michal Vasko086311b2016-01-08 09:53:11 +0100398 return 0;
399}
400
Michal Vaskob48aa812016-01-18 14:13:09 +0100401API void
402nc_server_destroy(void)
403{
404 pthread_spin_destroy(&server_opts.sid_lock);
405
Radek Krejci53691be2016-02-22 13:58:37 +0100406#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200407 nc_server_del_endpt(NULL);
Michal Vaskob48aa812016-01-18 14:13:09 +0100408#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100409 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100410}
411
Michal Vasko086311b2016-01-08 09:53:11 +0100412API int
413nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
414{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200415 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
416 ERRARG("basic_mode");
417 return -1;
418 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
419 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100420 return -1;
421 }
422
423 server_opts.wd_basic_mode = basic_mode;
424 server_opts.wd_also_supported = also_supported;
425 return 0;
426}
427
Michal Vasko1a38c862016-01-15 15:50:07 +0100428API void
Michal Vasko55f03972016-04-13 08:56:01 +0200429nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
430{
431 if (!basic_mode && !also_supported) {
432 ERRARG("basic_mode and also_supported");
433 return;
434 }
435
436 if (basic_mode) {
437 *basic_mode = server_opts.wd_basic_mode;
438 }
439 if (also_supported) {
440 *also_supported = server_opts.wd_also_supported;
441 }
442}
443
444API void
Michal Vasko086311b2016-01-08 09:53:11 +0100445nc_server_set_capab_interleave(int interleave_support)
446{
447 if (interleave_support) {
448 server_opts.interleave_capab = 1;
449 } else {
450 server_opts.interleave_capab = 0;
451 }
Michal Vasko086311b2016-01-08 09:53:11 +0100452}
453
Michal Vasko55f03972016-04-13 08:56:01 +0200454API int
455nc_server_get_capab_interleave(void)
456{
457 return server_opts.interleave_capab;
458}
459
Michal Vasko1a38c862016-01-15 15:50:07 +0100460API void
Michal Vasko086311b2016-01-08 09:53:11 +0100461nc_server_set_hello_timeout(uint16_t hello_timeout)
462{
Michal Vasko086311b2016-01-08 09:53:11 +0100463 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100464}
465
Michal Vasko55f03972016-04-13 08:56:01 +0200466API uint16_t
467nc_server_get_hello_timeout(void)
468{
469 return server_opts.hello_timeout;
470}
471
Michal Vasko1a38c862016-01-15 15:50:07 +0100472API void
Michal Vasko086311b2016-01-08 09:53:11 +0100473nc_server_set_idle_timeout(uint16_t idle_timeout)
474{
Michal Vasko086311b2016-01-08 09:53:11 +0100475 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100476}
477
Michal Vasko55f03972016-04-13 08:56:01 +0200478API uint16_t
479nc_server_get_idle_timeout(void)
480{
481 return server_opts.idle_timeout;
482}
483
Michal Vasko71090fc2016-05-24 16:37:28 +0200484API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100485nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100486{
Michal Vasko71090fc2016-05-24 16:37:28 +0200487 NC_MSG_TYPE msgtype;
488
Michal Vasko45e53ae2016-04-07 11:46:03 +0200489 if (!server_opts.ctx) {
490 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200491 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200492 } else if (fdin < 0) {
493 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200494 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200495 } else if (fdout < 0) {
496 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200497 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200498 } else if (!username) {
499 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200500 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200501 } else if (!session) {
502 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200503 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100504 }
505
506 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100507 *session = calloc(1, sizeof **session);
508 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100509 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200510 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100511 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100512 (*session)->status = NC_STATUS_STARTING;
513 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100514
515 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100516 (*session)->ti_type = NC_TI_FD;
517 (*session)->ti.fd.in = fdin;
518 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100519
520 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100521 (*session)->flags = NC_SESSION_SHAREDCTX;
522 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100523
Michal Vaskob48aa812016-01-18 14:13:09 +0100524 /* assign new SID atomically */
525 pthread_spin_lock(&server_opts.sid_lock);
526 (*session)->id = server_opts.new_session_id++;
527 pthread_spin_unlock(&server_opts.sid_lock);
528
Michal Vasko086311b2016-01-08 09:53:11 +0100529 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200530 msgtype = nc_handshake(*session);
531 if (msgtype != NC_MSG_HELLO) {
532 nc_session_free(*session, NULL);
533 *session = NULL;
534 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100535 }
Michal Vaskof8352352016-05-24 09:11:36 +0200536 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100537 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100538
Michal Vasko71090fc2016-05-24 16:37:28 +0200539 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100540}
Michal Vasko9e036d52016-01-08 10:49:26 +0100541
Michal Vaskob30b99c2016-07-26 11:35:43 +0200542static void
543nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
544{
545 uint8_t i, found = 0;
546
547 for (i = 0; i < ps->queue_len; ++i) {
548 /* idx round buffer adjust */
549 if (ps->queue_begin + i == NC_PS_QUEUE_SIZE) {
550 i = -ps->queue_begin;
551 }
552
553 if (found) {
554 /* move the value back one place */
555 if (ps->queue[ps->queue_begin + i] == id) {
556 /* another equal value, simply cannot be */
557 ERRINT;
558 }
559
560 if (ps->queue_begin + i == 0) {
561 ps->queue[NC_PS_QUEUE_SIZE - 1] = ps->queue[ps->queue_begin + i];
562 } else {
563 ps->queue[ps->queue_begin + i - 1] = ps->queue[ps->queue_begin + i];
564 }
565 } else if (ps->queue[ps->queue_begin + i] == id) {
566 /* found our id, there can be no more equal valid values */
567 found = 1;
568 }
569 }
570
571 if (!found) {
572 ERRINT;
573 }
574 --ps->queue_len;
575}
576
Michal Vaskof04a52a2016-04-07 10:52:10 +0200577int
Michal Vasko26043172016-07-26 14:08:59 +0200578nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200579{
580 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200581 uint8_t queue_last;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200582 struct timespec ts;
583
Radek Krejci7ac16052016-07-15 11:48:18 +0200584 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200585 ts.tv_sec += NC_READ_TIMEOUT;
586
587 /* LOCK */
588 ret = pthread_mutex_timedlock(&ps->lock, &ts);
589 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200590 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200591 return -1;
592 }
593
594 /* get a unique queue value (by adding 1 to the last added value, if any) */
595 if (ps->queue_len) {
596 queue_last = ps->queue_begin + ps->queue_len - 1;
597 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
598 queue_last -= NC_PS_QUEUE_SIZE;
599 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200600 *id = ps->queue[queue_last] + 1;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200601 } else {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200602 *id = 0;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200603 }
604
605 /* add ourselves into the queue */
606 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko26043172016-07-26 14:08:59 +0200607 ERR("%s: pollsession queue too small.", func);
Michal Vasko0ea456b2016-07-26 12:23:24 +0200608 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200609 return -1;
610 }
611 ++ps->queue_len;
612 queue_last = ps->queue_begin + ps->queue_len - 1;
613 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
614 queue_last -= NC_PS_QUEUE_SIZE;
615 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200616 ps->queue[queue_last] = *id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200617
618 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200619 while (ps->queue[ps->queue_begin] != *id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200620 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200621 ts.tv_sec += NC_READ_TIMEOUT;
622
623 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
624 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200625 ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200626 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200627 nc_ps_queue_remove_id(ps, *id);
628 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200629 return -1;
630 }
631 }
632
Michal Vaskobe86fe32016-04-07 10:43:03 +0200633 /* UNLOCK */
634 pthread_mutex_unlock(&ps->lock);
635
636 return 0;
637}
638
Michal Vaskof04a52a2016-04-07 10:52:10 +0200639int
Michal Vasko26043172016-07-26 14:08:59 +0200640nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200641{
642 int ret;
643 struct timespec ts;
644
Radek Krejci7ac16052016-07-15 11:48:18 +0200645 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200646 ts.tv_sec += NC_READ_TIMEOUT;
647
648 /* LOCK */
649 ret = pthread_mutex_timedlock(&ps->lock, &ts);
650 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200651 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200652 ret = -1;
653 }
654
Michal Vaskob30b99c2016-07-26 11:35:43 +0200655 /* we must be the first, it was our turn after all, right? */
656 if (ps->queue[ps->queue_begin] != id) {
657 ERRINT;
Michal Vaskob1a094b2016-10-05 14:04:52 +0200658 /* UNLOCK */
659 if (!ret) {
660 pthread_mutex_unlock(&ps->lock);
661 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200662 return -1;
663 }
664
Michal Vaskobe86fe32016-04-07 10:43:03 +0200665 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200666 nc_ps_queue_remove_id(ps, id);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200667
668 /* broadcast to all other threads that the queue moved */
669 pthread_cond_broadcast(&ps->cond);
670
Michal Vaskobe86fe32016-04-07 10:43:03 +0200671 /* UNLOCK */
672 if (!ret) {
673 pthread_mutex_unlock(&ps->lock);
674 }
675
676 return ret;
677}
678
Michal Vasko428087d2016-01-14 16:04:28 +0100679API struct nc_pollsession *
680nc_ps_new(void)
681{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100682 struct nc_pollsession *ps;
683
684 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100685 if (!ps) {
686 ERRMEM;
687 return NULL;
688 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200689 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100690 pthread_mutex_init(&ps->lock, NULL);
691
692 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100693}
694
695API void
696nc_ps_free(struct nc_pollsession *ps)
697{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100698 if (!ps) {
699 return;
700 }
701
Michal Vaskobe86fe32016-04-07 10:43:03 +0200702 if (ps->queue_len) {
703 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
704 }
705
Michal Vasko428087d2016-01-14 16:04:28 +0100706 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100707 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200708 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100709
Michal Vasko428087d2016-01-14 16:04:28 +0100710 free(ps);
711}
712
713API int
714nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
715{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200716 uint8_t q_id;
717
Michal Vasko45e53ae2016-04-07 11:46:03 +0200718 if (!ps) {
719 ERRARG("ps");
720 return -1;
721 } else if (!session) {
722 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100723 return -1;
724 }
725
Michal Vasko48a63ed2016-03-01 09:48:21 +0100726 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200727 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200728 return -1;
729 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100730
Michal Vasko428087d2016-01-14 16:04:28 +0100731 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100732 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
Michal Vasko9a327362017-01-11 11:31:46 +0100733 if (!ps->sessions) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100734 ERRMEM;
735 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200736 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100737 return -1;
738 }
Michal Vasko3a715132016-01-21 15:40:31 +0100739 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100740
Michal Vasko48a63ed2016-03-01 09:48:21 +0100741 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200742 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100743}
744
Michal Vasko48a63ed2016-03-01 09:48:21 +0100745static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100746_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100747{
748 uint16_t i;
749
Radek Krejcid5f978f2016-03-03 13:14:45 +0100750 if (index >= 0) {
751 i = (uint16_t)index;
752 goto remove;
753 }
Michal Vasko428087d2016-01-14 16:04:28 +0100754 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100755 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100756remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100757 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100758 if (i < ps->session_count) {
759 ps->sessions[i] = ps->sessions[ps->session_count];
Michal Vasko9a327362017-01-11 11:31:46 +0100760 if (ps->last_event_session == i) {
761 ps->last_event_session = 0;
762 }
Michal Vasko58005732016-02-02 15:50:52 +0100763 } else if (!ps->session_count) {
764 free(ps->sessions);
765 ps->sessions = NULL;
Michal Vasko58005732016-02-02 15:50:52 +0100766 }
Michal Vasko428087d2016-01-14 16:04:28 +0100767 return 0;
768 }
769 }
770
Michal Vaskof0537d82016-01-29 14:42:38 +0100771 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100772}
773
Michal Vasko48a63ed2016-03-01 09:48:21 +0100774API int
775nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
776{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200777 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200778 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100779
Michal Vasko45e53ae2016-04-07 11:46:03 +0200780 if (!ps) {
781 ERRARG("ps");
782 return -1;
783 } else if (!session) {
784 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100785 return -1;
786 }
787
788 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200789 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200790 return -1;
791 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100792
Radek Krejcid5f978f2016-03-03 13:14:45 +0100793 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100794
795 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200796 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100797
Michal Vaskobe86fe32016-04-07 10:43:03 +0200798 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100799}
800
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100801API uint16_t
802nc_ps_session_count(struct nc_pollsession *ps)
803{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200804 uint8_t q_id;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100805 uint16_t count;
806
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100807 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200808 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100809 return 0;
810 }
811
Michal Vasko48a63ed2016-03-01 09:48:21 +0100812 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200813 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200814 return -1;
815 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100816
817 count = ps->session_count;
818
819 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200820 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100821
822 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100823}
824
Michal Vasko71090fc2016-05-24 16:37:28 +0200825/* must be called holding the session lock!
826 * returns: NC_PSPOLL_ERROR,
827 * NC_PSPOLL_BAD_RPC,
828 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
829 * NC_PSPOLL_RPC
830 */
831static int
Michal Vasko428087d2016-01-14 16:04:28 +0100832nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
833{
834 struct lyxml_elem *xml = NULL;
835 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200836 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200837 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100838
Michal Vasko45e53ae2016-04-07 11:46:03 +0200839 if (!session) {
840 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200841 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200842 } else if (!rpc) {
843 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200844 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100845 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100846 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200847 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100848 }
849
850 msgtype = nc_read_msg(session, &xml);
851
852 switch (msgtype) {
853 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200854 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100855 if (!*rpc) {
856 ERRMEM;
857 goto error;
858 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100859
Radek Krejcif93c7d42016-04-06 13:41:15 +0200860 ly_errno = LY_SUCCESS;
Michal Vasko68b3f292016-09-16 12:00:32 +0200861 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_RPC | LYD_OPT_DESTRUCT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100862 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200863 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200864 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200865 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200866 nc_server_reply_free(reply);
867 if (ret == -1) {
868 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200869 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200870 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
871 } else {
872 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100873 }
Michal Vasko428087d2016-01-14 16:04:28 +0100874 (*rpc)->root = xml;
875 break;
876 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100877 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200878 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100879 goto error;
880 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100881 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200882 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100883 goto error;
884 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100885 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200886 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100887 goto error;
888 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200889 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100890 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
891 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200892 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100893 break;
894 }
895
Michal Vasko71090fc2016-05-24 16:37:28 +0200896 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100897
898error:
899 /* cleanup */
900 lyxml_free(server_opts.ctx, xml);
901
Michal Vasko71090fc2016-05-24 16:37:28 +0200902 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100903}
904
fanchanghu966f2de2016-07-21 02:28:57 -0400905API void
906nc_set_global_rpc_clb(nc_rpc_clb clb)
907{
908 global_rpc_clb = clb;
909}
910
Michal Vasko71090fc2016-05-24 16:37:28 +0200911/* must be called holding the session lock!
912 * returns: NC_PSPOLL_ERROR,
913 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
914 * NC_PSPOLL_REPLY_ERROR,
915 * 0
916 */
917static int
Michal Vasko428087d2016-01-14 16:04:28 +0100918nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
919{
920 nc_rpc_clb clb;
921 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +0200922 struct lys_node *rpc_act = NULL;
923 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +0200924 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +0100925
Michal Vasko4a827e52016-03-03 10:59:00 +0100926 if (!rpc) {
927 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200928 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +0100929 }
930
Michal Vasko90e8e692016-07-13 12:27:57 +0200931 if (rpc->tree->schema->nodetype == LYS_RPC) {
932 /* RPC */
933 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -0400934 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +0200935 /* action */
936 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
937 if (elem->schema->nodetype == LYS_ACTION) {
938 rpc_act = elem->schema;
939 break;
940 }
941 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -0400942 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200943 if (!rpc_act) {
944 ERRINT;
945 return NC_PSPOLL_ERROR;
946 }
947 }
948
949 if (!rpc_act->priv) {
950 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +0100951 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100952 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +0200953 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +0100954 reply = clb(rpc->tree, session);
955 }
956
957 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100958 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100959 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200960 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
961 if (reply->type == NC_RPL_ERROR) {
962 ret |= NC_PSPOLL_REPLY_ERROR;
963 }
964 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +0100965
Michal Vasko71090fc2016-05-24 16:37:28 +0200966 if (r == -1) {
967 ERR("Session %u: failed to write reply.", session->id);
968 ret |= NC_PSPOLL_ERROR;
969 }
Michal Vasko428087d2016-01-14 16:04:28 +0100970
971 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
972 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
973 session->status = NC_STATUS_INVALID;
974 }
975
Michal Vasko71090fc2016-05-24 16:37:28 +0200976 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100977}
978
979API int
Michal Vasko71090fc2016-05-24 16:37:28 +0200980nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +0100981{
Michal Vasko9a327362017-01-11 11:31:46 +0100982 int ret, r, poll_ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200983 uint8_t q_id;
Michal Vasko9a327362017-01-11 11:31:46 +0100984 uint16_t i, j;
985 char msg[256];
986 NC_SESSION_TERM_REASON term_reason;
987 struct pollfd pfd;
988 struct timespec begin_ts, cur_ts;
Michal Vasko71090fc2016-05-24 16:37:28 +0200989 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +0100990 struct nc_server_rpc *rpc = NULL;
Michal Vasko9a327362017-01-11 11:31:46 +0100991#ifdef NC_ENABLED_SSH
992 struct nc_session *new;
993#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100994
995 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200996 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +0200997 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100998 }
999
Michal Vasko9a327362017-01-11 11:31:46 +01001000 nc_gettimespec(&begin_ts);
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001001
Michal Vasko48a63ed2016-03-01 09:48:21 +01001002 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001003 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001004 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001005 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001006
Michal Vasko9a327362017-01-11 11:31:46 +01001007 /* check that all session are fine */
Michal Vasko428087d2016-01-14 16:04:28 +01001008 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001009 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1010 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001011 ret = NC_PSPOLL_ERROR;
1012 if (session) {
1013 *session = ps->sessions[i];
1014 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001015 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001016 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001017
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001018 /* TODO invalidate only sessions without subscription */
Michal Vasko9a327362017-01-11 11:31:46 +01001019 if (server_opts.idle_timeout && (begin_ts.tv_sec >= ps->sessions[i]->last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001020 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1021 ps->sessions[i]->status = NC_STATUS_INVALID;
1022 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001023 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1024 if (session) {
1025 *session = ps->sessions[i];
1026 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001027 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001028 }
Michal Vasko428087d2016-01-14 16:04:28 +01001029 }
1030
Michal Vasko9a327362017-01-11 11:31:46 +01001031 /* poll on all the sessions one-by-one */
1032 do {
1033 /* loop from i to j */
1034 if (ps->last_event_session == ps->session_count - 1) {
1035 i = j = 0;
1036 } else {
1037 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001038 }
Michal Vasko9a327362017-01-11 11:31:46 +01001039 do {
1040 cur_session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +01001041
Michal Vasko9a327362017-01-11 11:31:46 +01001042 switch (cur_session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +01001043#ifdef NC_ENABLED_SSH
Michal Vasko9a327362017-01-11 11:31:46 +01001044 case NC_TI_LIBSSH:
1045 r = ssh_channel_poll_timeout(cur_session->ti.libssh.channel, 0, 0);
1046 if (r < 1) {
1047 if (r == SSH_EOF) {
1048 sprintf(msg, "SSH channel unexpectedly closed");
1049 term_reason = NC_SESSION_TERM_DROPPED;
1050 poll_ret = -2;
1051 } else if (r == SSH_ERROR) {
1052 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(cur_session->ti.libssh.session));
1053 poll_ret = -1;
1054 } else {
1055 poll_ret = 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001056 }
Michal Vasko9a327362017-01-11 11:31:46 +01001057 break;
Michal Vasko96164bf2016-01-21 15:41:58 +01001058 }
Michal Vasko9a327362017-01-11 11:31:46 +01001059 /* we have some data, but it may be just an SSH message */
Michal Vasko96164bf2016-01-21 15:41:58 +01001060
Michal Vasko9a327362017-01-11 11:31:46 +01001061 r = nc_timedlock(cur_session->ti_lock, timeout, __func__);
1062 if (r < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001063 if (session) {
Michal Vasko9a327362017-01-11 11:31:46 +01001064 *session = cur_session;
Michal Vasko71090fc2016-05-24 16:37:28 +02001065 }
Michal Vasko9a327362017-01-11 11:31:46 +01001066 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001067 goto finish;
Michal Vasko9a327362017-01-11 11:31:46 +01001068 } else if (!r) {
1069 if (session) {
1070 *session = cur_session;
Michal Vasko428087d2016-01-14 16:04:28 +01001071 }
Michal Vasko9a327362017-01-11 11:31:46 +01001072 ret = NC_PSPOLL_TIMEOUT;
1073 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001074 }
Michal Vasko9a327362017-01-11 11:31:46 +01001075 r = ssh_execute_message_callbacks(cur_session->ti.libssh.session);
1076 pthread_mutex_unlock(cur_session->ti_lock);
1077
1078 if (r != SSH_OK) {
1079 sprintf(msg, "failed to receive SSH messages (%s)", ssh_get_error(cur_session->ti.libssh.session));
1080 term_reason = NC_SESSION_TERM_OTHER;
1081 poll_ret = -1;
1082 } else if (cur_session->flags & NC_SESSION_SSH_NEW_MSG) {
1083 /* new SSH message */
1084 cur_session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1085 if (cur_session->ti.libssh.next) {
1086 for (new = cur_session->ti.libssh.next; new != cur_session; new = new->ti.libssh.next) {
1087 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel
1088 && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1089 /* new NETCONF SSH channel */
1090 if (session) {
1091 *session = cur_session;
1092 }
1093 ret = NC_PSPOLL_SSH_CHANNEL;
1094 goto finish;
1095 }
1096 }
1097 }
1098
1099 /* just some SSH message */
1100 if (session) {
1101 *session = cur_session;
1102 }
1103 ret = NC_PSPOLL_SSH_MSG;
1104 goto finish;
1105 } else {
1106 /* we have some application data */
1107 poll_ret = 1;
1108 }
1109 break;
1110#endif
1111#ifdef NC_ENABLED_TLS
1112 case NC_TI_OPENSSL:
1113 r = SSL_pending(cur_session->ti.tls);
1114 if (!r) {
1115 /* no data pending in the SSL buffer, poll fd */
1116 pfd.fd = SSL_get_rfd(cur_session->ti.tls);
1117 if (pfd.fd < 0) {
1118 ERRINT;
1119 ret = NC_PSPOLL_ERROR;
1120 goto finish;
1121 }
1122 pfd.events = POLLIN;
1123 pfd.revents = 0;
1124 r = poll(&pfd, 1, 0);
1125
1126 if (r < 0) {
1127 sprintf(msg, "poll failed (%s)", strerror(errno));
1128 poll_ret = -1;
1129 } else if (r > 0) {
1130 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1131 sprintf(msg, "communication socket unexpectedly closed");
1132 term_reason = NC_SESSION_TERM_DROPPED;
1133 poll_ret = -2;
1134 } else if (pfd.revents & POLLERR) {
1135 sprintf(msg, "communication socket error");
1136 term_reason = NC_SESSION_TERM_OTHER;
1137 poll_ret = -2;
1138 } else {
1139 poll_ret = 1;
1140 }
1141 } else {
1142 poll_ret = 0;
1143 }
1144 } else {
1145 poll_ret = 1;
1146 }
1147 break;
1148#endif
1149 case NC_TI_FD:
1150 pfd.fd = cur_session->ti.fd.in;
1151 pfd.events = POLLIN;
1152 pfd.revents = 0;
1153 r = poll(&pfd, 1, 0);
1154
1155 if (r < 0) {
1156 sprintf(msg, "poll failed (%s)", strerror(errno));
1157 poll_ret = 0;
1158 } else if (r > 0) {
1159 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1160 sprintf(msg, "communication socket unexpectedly closed");
1161 term_reason = NC_SESSION_TERM_DROPPED;
1162 poll_ret = -2;
1163 } else if (pfd.revents & POLLERR) {
1164 sprintf(msg, "communication socket error");
1165 term_reason = NC_SESSION_TERM_OTHER;
1166 poll_ret = -2;
1167 } else {
1168 poll_ret = 1;
1169 }
1170 } else {
1171 poll_ret = 0;
1172 }
1173 break;
1174 case NC_TI_NONE:
1175 ERRINT;
1176 ret = NC_PSPOLL_ERROR;
1177 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001178 }
Michal Vasko428087d2016-01-14 16:04:28 +01001179
Michal Vasko9a327362017-01-11 11:31:46 +01001180 /* here: poll_ret == -2 - session error, session terminated,
1181 * poll_ret == -1 - generic error,
1182 * poll_ret == 0 - nothing to read,
1183 * poll_ret > 0 - data available */
1184 if (poll_ret == -2) {
1185 ERR("Session %u: %s.", cur_session->id, msg);
1186 cur_session->status = NC_STATUS_INVALID;
1187 cur_session->term_reason = term_reason;
1188 if (session) {
1189 *session = cur_session;
1190 }
1191 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1192 goto finish;
1193 } else if (poll_ret == -1) {
1194 ERR("Session %u: %s.", cur_session->id, msg);
1195 ret = NC_PSPOLL_ERROR;
1196 goto finish;
1197 } else if (poll_ret > 0) {
1198 break;
1199 }
1200
1201 /* next iteration */
1202 if (i == ps->session_count - 1) {
1203 i = 0;
1204 } else {
1205 ++i;
1206 }
1207 } while (i != j);
1208
1209 /* no event */
1210 if (!poll_ret && (timeout > -1)) {
1211 usleep(NC_TIMEOUT_STEP);
1212
1213 nc_gettimespec(&cur_ts);
1214 /* final timeout */
1215 if (nc_difftimespec(&begin_ts, &cur_ts) >= (unsigned)timeout) {
1216 ret = NC_PSPOLL_TIMEOUT;
1217 goto finish;
1218 }
Michal Vasko428087d2016-01-14 16:04:28 +01001219 }
Michal Vasko9a327362017-01-11 11:31:46 +01001220 } while (!poll_ret);
Michal Vasko428087d2016-01-14 16:04:28 +01001221
1222 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001223 if (session) {
1224 *session = cur_session;
1225 }
Michal Vasko9a327362017-01-11 11:31:46 +01001226 ps->last_event_session = i;
Michal Vasko428087d2016-01-14 16:04:28 +01001227
Michal Vaskobd8ef262016-01-20 11:09:27 +01001228 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko9a327362017-01-11 11:31:46 +01001229 r = nc_timedlock(cur_session->ti_lock, timeout, __func__);
1230 if (r < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001231 ret = NC_PSPOLL_ERROR;
1232 goto finish;
Michal Vasko9a327362017-01-11 11:31:46 +01001233 } else if (!r) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001234 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001235 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001236 }
1237
Michal Vasko71090fc2016-05-24 16:37:28 +02001238 ret = nc_recv_rpc(cur_session, &rpc);
1239 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1240 pthread_mutex_unlock(cur_session->ti_lock);
1241 if (cur_session->status != NC_STATUS_RUNNING) {
1242 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001243 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001244 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001245 }
1246
Michal Vasko71090fc2016-05-24 16:37:28 +02001247 cur_session->last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001248
Michal Vasko428087d2016-01-14 16:04:28 +01001249 /* process RPC */
Michal Vasko71090fc2016-05-24 16:37:28 +02001250 ret |= nc_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001251
Michal Vasko71090fc2016-05-24 16:37:28 +02001252 pthread_mutex_unlock(cur_session->ti_lock);
1253 if (cur_session->status != NC_STATUS_RUNNING) {
1254 ret |= NC_PSPOLL_SESSION_TERM;
1255 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1256 ret |= NC_PSPOLL_SESSION_ERROR;
1257 }
Michal Vasko428087d2016-01-14 16:04:28 +01001258 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001259
Michal Vaskoca4a2422016-02-02 12:17:14 +01001260 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001261
Michal Vasko48a63ed2016-03-01 09:48:21 +01001262finish:
1263 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001264 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001265 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001266}
1267
Michal Vaskod09eae62016-02-01 10:32:52 +01001268API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001269nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001270{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001271 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001272 uint16_t i;
1273 struct nc_session *session;
1274
Michal Vasko9a25e932016-02-01 10:36:42 +01001275 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001276 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001277 return;
1278 }
1279
Michal Vasko48a63ed2016-03-01 09:48:21 +01001280 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001281 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001282 return;
1283 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001284
Michal Vasko48a63ed2016-03-01 09:48:21 +01001285 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001286 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001287 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001288 }
1289 free(ps->sessions);
1290 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001291 ps->session_count = 0;
Michal Vasko9a327362017-01-11 11:31:46 +01001292 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001293 } else {
1294 for (i = 0; i < ps->session_count; ) {
1295 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1296 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001297 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001298 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001299 continue;
1300 }
1301
1302 ++i;
1303 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001304 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001305
1306 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001307 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001308}
1309
Radek Krejci53691be2016-02-22 13:58:37 +01001310#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001311
Michal Vaskoe2713da2016-08-22 16:06:40 +02001312API int
1313nc_server_add_endpt(const char *name)
Michal Vasko9e036d52016-01-08 10:49:26 +01001314{
Michal Vasko3031aae2016-01-27 16:07:18 +01001315 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +01001316#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001317 uint16_t bind_ssh_idx;
1318#endif
1319#ifdef NC_ENABLED_TLS
1320 uint16_t bind_tls_idx;
Michal Vasko08a629a2016-02-02 12:20:47 +01001321#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001322
Michal Vasko45e53ae2016-04-07 11:46:03 +02001323 if (!name) {
1324 ERRARG("name");
1325 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001326 }
1327
Michal Vasko51e514d2016-02-02 15:51:52 +01001328 /* WRITE LOCK */
1329 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001330
1331 /* check name uniqueness */
1332 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001333 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001334 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001335 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +01001336 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001337 return -1;
1338 }
1339 }
1340
Michal Vasko3031aae2016-01-27 16:07:18 +01001341 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001342 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001343 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001344 ERRMEM;
1345 /* WRITE UNLOCK */
1346 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001347 return -1;
1348 }
1349 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1350
1351#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1352 server_opts.binds = nc_realloc(server_opts.binds, 2 * server_opts.endpt_count * sizeof *server_opts.binds);
1353 bind_ssh_idx = (server_opts.endpt_count - 1) * 2;
1354 bind_tls_idx = (server_opts.endpt_count - 1) * 2 + 1;
1355#else
1356 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
1357# ifdef NC_ENABLED_SSH
1358 bind_ssh_idx = server_opts.endpt_count - 1;
1359# endif
1360# ifdef NC_ENABLED_TLS
1361 bind_tls_idx = server_opts.endpt_count - 1;
1362# endif
1363#endif
1364 if (!server_opts.binds) {
1365 ERRMEM;
1366 /* WRITE UNLOCK */
1367 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001368 return -1;
1369 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001370
Radek Krejci53691be2016-02-22 13:58:37 +01001371#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001372 server_opts.binds[bind_ssh_idx].address = NULL;
1373 server_opts.binds[bind_ssh_idx].port = 0;
1374 server_opts.binds[bind_ssh_idx].sock = -1;
1375 server_opts.binds[bind_ssh_idx].ti = NC_TI_LIBSSH;
Michal Vasko08a629a2016-02-02 12:20:47 +01001376
Michal Vaskoe2713da2016-08-22 16:06:40 +02001377 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts = calloc(1, sizeof(struct nc_server_ssh_opts));
1378 if (!server_opts.endpts[server_opts.endpt_count - 1].ssh_opts) {
1379 ERRMEM;
1380 /* WRITE UNLOCK */
1381 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1382 return -1;
Michal Vasko3031aae2016-01-27 16:07:18 +01001383 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001384 /* set default values */
1385 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_methods =
1386 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1387 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_attempts = 3;
1388 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_timeout = 10;
1389#endif
1390
1391#ifdef NC_ENABLED_TLS
1392 server_opts.binds[bind_tls_idx].address = NULL;
1393 server_opts.binds[bind_tls_idx].port = 0;
1394 server_opts.binds[bind_tls_idx].sock = -1;
1395 server_opts.binds[bind_tls_idx].ti = NC_TI_OPENSSL;
1396
1397 server_opts.endpts[server_opts.endpt_count - 1].tls_opts = calloc(1, sizeof(struct nc_server_tls_opts));
1398 if (!server_opts.endpts[server_opts.endpt_count - 1].tls_opts) {
1399 ERRMEM;
1400 /* WRITE UNLOCK */
1401 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1402 return -1;
1403 }
1404#endif
1405
Michal Vasko3031aae2016-01-27 16:07:18 +01001406 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001407
Michal Vasko3031aae2016-01-27 16:07:18 +01001408 /* WRITE UNLOCK */
1409 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001410
Michal Vasko9e036d52016-01-08 10:49:26 +01001411 return 0;
1412}
1413
Michal Vasko3031aae2016-01-27 16:07:18 +01001414int
Michal Vaskoda514772016-02-01 11:32:01 +01001415nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1416{
1417 struct nc_endpt *endpt;
1418 struct nc_bind *bind = NULL;
1419 uint16_t i;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001420 int sock = -1, set_addr;
Michal Vaskoda514772016-02-01 11:32:01 +01001421
Michal Vasko45e53ae2016-04-07 11:46:03 +02001422 if (!endpt_name) {
1423 ERRARG("endpt_name");
1424 return -1;
1425 } else if ((!address && !port) || (address && port)) {
1426 ERRARG("address and port");
1427 return -1;
1428 } else if (!ti) {
1429 ERRARG("ti");
Michal Vaskoda514772016-02-01 11:32:01 +01001430 return -1;
1431 }
1432
Michal Vaskoe2713da2016-08-22 16:06:40 +02001433 if (address) {
1434 set_addr = 1;
1435 } else {
1436 set_addr = 0;
1437 }
1438
Michal Vasko51e514d2016-02-02 15:51:52 +01001439 /* LOCK */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001440 endpt = nc_server_endpt_lock(endpt_name, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001441 if (!endpt) {
1442 return -1;
1443 }
1444
Michal Vaskoe2713da2016-08-22 16:06:40 +02001445#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1446 if (ti == NC_TI_LIBSSH) {
1447 bind = &server_opts.binds[2 * i];
1448 } else {
1449 bind = &server_opts.binds[2 * i + 1];
Michal Vaskoda514772016-02-01 11:32:01 +01001450 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001451#else
1452 bind = &server_opts.binds[i];
Michal Vasko5e391372016-08-22 16:20:58 +02001453 if (bind->ti != ti) {
Michal Vaskoda514772016-02-01 11:32:01 +01001454 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001455 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001456 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001457#endif
Michal Vaskoda514772016-02-01 11:32:01 +01001458
Michal Vaskoe2713da2016-08-22 16:06:40 +02001459 if (set_addr) {
1460 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001461 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001462 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001463 }
1464
Michal Vaskoe2713da2016-08-22 16:06:40 +02001465 /* we have all the information we need to create a listening socket */
1466 if (address && port) {
1467 /* create new socket, close the old one */
1468 sock = nc_sock_listen(address, port);
1469 if (sock == -1) {
1470 goto fail;
1471 }
1472
1473 if (bind->sock > -1) {
1474 close(bind->sock);
1475 }
1476 bind->sock = sock;
1477 } /* else we are just setting address or port */
1478
1479 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001480 lydict_remove(server_opts.ctx, bind->address);
1481 bind->address = lydict_insert(server_opts.ctx, address, 0);
1482 } else {
1483 bind->port = port;
1484 }
1485
Michal Vaskoe2713da2016-08-22 16:06:40 +02001486 if (sock > -1) {
1487#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1488 VRB("Listening on %s:%u for %s connections.", address, port, (ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
1489#elif defined(NC_ENABLED_SSH)
1490 VRB("Listening on %s:%u for SSH connections.", address, port);
1491#else
1492 VRB("Listening on %s:%u for TLS connections.", address, port);
1493#endif
1494 }
1495
Michal Vasko51e514d2016-02-02 15:51:52 +01001496 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001497 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001498 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001499
1500fail:
1501 /* UNLOCK */
1502 nc_server_endpt_unlock(endpt);
1503 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001504}
1505
Michal Vaskoe2713da2016-08-22 16:06:40 +02001506API int
1507nc_server_del_endpt(const char *name)
Michal Vasko9e036d52016-01-08 10:49:26 +01001508{
1509 uint32_t i;
1510 int ret = -1;
1511
Michal Vasko3031aae2016-01-27 16:07:18 +01001512 /* WRITE LOCK */
1513 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001514
Michal Vaskoe2713da2016-08-22 16:06:40 +02001515 if (!name) {
1516 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001517 for (i = 0; i < server_opts.endpt_count; ++i) {
1518 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko3031aae2016-01-27 16:07:18 +01001519 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
Radek Krejci53691be2016-02-22 13:58:37 +01001520#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001521 nc_server_ssh_clear_opts(server_opts.endpts[i].ssh_opts);
1522 free(server_opts.endpts[i].ssh_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001523#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001524#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001525 nc_server_tls_clear_opts(server_opts.endpts[i].tls_opts);
1526 free(server_opts.endpts[i].tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001527#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001528 ret = 0;
1529 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001530 free(server_opts.endpts);
1531 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001532
1533 /* remove all binds */
1534 for (i = 0; i < server_opts.endpt_count; ++i) {
1535 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1536 if (server_opts.binds[i].sock > -1) {
1537 close(server_opts.binds[i].sock);
1538 }
1539 }
1540#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1541 for (; i < 2 * server_opts.endpt_count; ++i) {
1542 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1543 if (server_opts.binds[i].sock > -1) {
1544 close(server_opts.binds[i].sock);
1545 }
1546 }
1547#endif
1548 free(server_opts.binds);
1549 server_opts.binds = NULL;
1550
Michal Vasko3031aae2016-01-27 16:07:18 +01001551 server_opts.endpt_count = 0;
1552
Michal Vasko1a38c862016-01-15 15:50:07 +01001553 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001554 /* remove one endpoint with bind(s) */
Michal Vasko3031aae2016-01-27 16:07:18 +01001555 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001556 if (!strcmp(server_opts.endpts[i].name, name)) {
1557 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001558 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko3031aae2016-01-27 16:07:18 +01001559 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
Radek Krejci53691be2016-02-22 13:58:37 +01001560#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001561 nc_server_ssh_clear_opts(server_opts.endpts[i].ssh_opts);
1562 free(server_opts.endpts[i].ssh_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001563#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001564#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001565 nc_server_tls_clear_opts(server_opts.endpts[i].tls_opts);
1566 free(server_opts.endpts[i].tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001567#endif
Michal Vasko1a38c862016-01-15 15:50:07 +01001568
Michal Vaskoe2713da2016-08-22 16:06:40 +02001569 /* remove bind(s) */
1570#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1571 i *= 2;
1572 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1573 if (server_opts.binds[i].sock > -1) {
1574 close(server_opts.binds[i].sock);
1575 }
1576 ++i;
1577#endif
1578 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1579 if (server_opts.binds[i].sock > -1) {
1580 close(server_opts.binds[i].sock);
1581 }
1582
1583 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001584 --server_opts.endpt_count;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001585#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1586 --i;
1587 i /= 2;
1588 if (i < server_opts.endpt_count) {
1589 memcpy(&server_opts.binds[2 * i], &server_opts.binds[2 * server_opts.endpt_count], 2 * sizeof *server_opts.binds);
1590 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1591 }
1592#else
Michal Vaskoc0256492016-02-02 12:19:06 +01001593 if (i < server_opts.endpt_count) {
1594 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1595 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001596 }
1597#endif
1598 else if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001599 free(server_opts.binds);
1600 server_opts.binds = NULL;
1601 free(server_opts.endpts);
1602 server_opts.endpts = NULL;
1603 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001604
1605 ret = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001606 break;
Michal Vasko1a38c862016-01-15 15:50:07 +01001607 }
1608 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001609 }
1610
Michal Vasko3031aae2016-01-27 16:07:18 +01001611 /* WRITE UNLOCK */
1612 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001613
Michal Vasko9e036d52016-01-08 10:49:26 +01001614 return ret;
1615}
1616
Michal Vasko71090fc2016-05-24 16:37:28 +02001617API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001618nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001619{
Michal Vasko71090fc2016-05-24 16:37:28 +02001620 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001621 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001622 char *host = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001623 uint16_t port, endpt_idx, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001624
Michal Vasko45e53ae2016-04-07 11:46:03 +02001625 if (!server_opts.ctx) {
1626 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001627 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001628 } else if (!session) {
1629 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001630 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001631 }
1632
Michal Vasko51e514d2016-02-02 15:51:52 +01001633 /* we have to hold WRITE for the whole time, since there is not
1634 * a way of downgrading the lock to READ */
1635 /* WRITE LOCK */
1636 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1637
1638 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001639 ERR("No endpoints to accept sessions on.");
Michal Vasko51e514d2016-02-02 15:51:52 +01001640 /* WRITE UNLOCK */
1641 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001642 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001643 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001644
Michal Vasko94acafc2016-09-23 13:40:10 +02001645#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1646 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count * 2, timeout, &host, &port, &bind_idx);
1647#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001648 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko94acafc2016-09-23 13:40:10 +02001649#endif
Michal Vaskob48aa812016-01-18 14:13:09 +01001650
Michal Vasko50456e82016-02-02 12:16:08 +01001651 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001652 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001653 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001654 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001655 if (!ret) {
1656 return NC_MSG_WOULDBLOCK;
1657 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001658 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001659 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001660 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001661
Michal Vasko1a38c862016-01-15 15:50:07 +01001662 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001663 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001664 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001665 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001666 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001667 msgtype = NC_MSG_ERROR;
1668 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001669 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001670 (*session)->status = NC_STATUS_STARTING;
1671 (*session)->side = NC_SERVER;
1672 (*session)->ctx = server_opts.ctx;
1673 (*session)->flags = NC_SESSION_SHAREDCTX;
1674 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1675 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001676
1677 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001678 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1679 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001680 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001681 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001682 msgtype = NC_MSG_ERROR;
1683 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001684 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001685 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001686
Michal Vaskoe2713da2016-08-22 16:06:40 +02001687 endpt_idx = bind_idx;
1688 /* transform index as needed */
1689#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1690 if (server_opts.binds[bind_idx].ti == NC_TI_OPENSSL) {
1691 --endpt_idx;
1692 }
1693 endpt_idx /= 2;
1694#endif
Michal Vasko3031aae2016-01-27 16:07:18 +01001695
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001696 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001697#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001698 if (server_opts.binds[bind_idx].ti == NC_TI_LIBSSH) {
1699 (*session)->data = server_opts.endpts[endpt_idx].ssh_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001700 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001701 if (ret < 0) {
1702 msgtype = NC_MSG_ERROR;
1703 goto cleanup;
1704 } else if (!ret) {
1705 msgtype = NC_MSG_WOULDBLOCK;
1706 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001707 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001708 } else
1709#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001710#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001711 if (server_opts.binds[bind_idx].ti == NC_TI_OPENSSL) {
1712 (*session)->data = server_opts.endpts[endpt_idx].tls_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001713 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001714 if (ret < 0) {
1715 msgtype = NC_MSG_ERROR;
1716 goto cleanup;
1717 } else if (!ret) {
1718 msgtype = NC_MSG_WOULDBLOCK;
1719 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001720 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001721 } else
1722#endif
1723 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001724 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001725 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001726 msgtype = NC_MSG_ERROR;
1727 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001728 }
1729
Michal Vasko2cc4c682016-03-01 09:16:48 +01001730 (*session)->data = NULL;
1731
Michal Vasko51e514d2016-02-02 15:51:52 +01001732 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001733 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1734
Michal Vaskob48aa812016-01-18 14:13:09 +01001735 /* assign new SID atomically */
1736 /* LOCK */
1737 pthread_spin_lock(&server_opts.sid_lock);
1738 (*session)->id = server_opts.new_session_id++;
1739 /* UNLOCK */
1740 pthread_spin_unlock(&server_opts.sid_lock);
1741
Michal Vasko9e036d52016-01-08 10:49:26 +01001742 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001743 msgtype = nc_handshake(*session);
1744 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001745 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001746 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001747 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001748 }
Michal Vaskof802fdc2016-07-28 15:47:00 +02001749 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001750 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001751
Michal Vasko71090fc2016-05-24 16:37:28 +02001752 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001753
Michal Vasko71090fc2016-05-24 16:37:28 +02001754cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001755 /* WRITE UNLOCK */
1756 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1757
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001758 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001759 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001760 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001761}
1762
Michal Vasko71090fc2016-05-24 16:37:28 +02001763NC_MSG_TYPE
Michal Vasko8f5270d2016-02-29 16:22:25 +01001764nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01001765{
Michal Vasko71090fc2016-05-24 16:37:28 +02001766 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001767 int sock, ret;
1768
Michal Vasko45e53ae2016-04-07 11:46:03 +02001769 if (!host) {
1770 ERRARG("host");
Michal Vasko71090fc2016-05-24 16:37:28 +02001771 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001772 } else if (!port) {
1773 ERRARG("port");
Michal Vasko71090fc2016-05-24 16:37:28 +02001774 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001775 } else if (!ti) {
1776 ERRARG("ti");
Michal Vasko71090fc2016-05-24 16:37:28 +02001777 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001778 } else if (!session) {
1779 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001780 return NC_MSG_ERROR;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001781 }
1782
Michal Vaskob05053d2016-01-22 16:12:06 +01001783 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001784 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001785 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001786 }
1787
1788 *session = calloc(1, sizeof **session);
1789 if (!(*session)) {
1790 ERRMEM;
1791 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001792 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001793 }
1794 (*session)->status = NC_STATUS_STARTING;
1795 (*session)->side = NC_SERVER;
1796 (*session)->ctx = server_opts.ctx;
1797 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001798 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001799 (*session)->port = port;
1800
1801 /* transport lock */
1802 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1803 if (!(*session)->ti_lock) {
1804 ERRMEM;
1805 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001806 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001807 goto fail;
1808 }
1809 pthread_mutex_init((*session)->ti_lock, NULL);
1810
1811 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001812#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001813 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001814 /* OPTS LOCK */
1815 pthread_mutex_lock(&ssh_ch_opts_lock);
1816
Michal Vasko2cc4c682016-03-01 09:16:48 +01001817 (*session)->data = &ssh_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001818 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001819 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001820
1821 /* OPTS UNLOCK */
1822 pthread_mutex_unlock(&ssh_ch_opts_lock);
1823
Michal Vasko71090fc2016-05-24 16:37:28 +02001824 if (ret < 0) {
1825 msgtype = NC_MSG_ERROR;
1826 goto fail;
1827 } else if (!ret) {
1828 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001829 goto fail;
1830 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001831 } else
1832#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001833#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001834 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001835 /* OPTS LOCK */
1836 pthread_mutex_lock(&tls_ch_opts_lock);
1837
Michal Vasko2cc4c682016-03-01 09:16:48 +01001838 (*session)->data = &tls_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001839 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001840 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001841
1842 /* OPTS UNLOCK */
1843 pthread_mutex_unlock(&tls_ch_opts_lock);
1844
Michal Vasko71090fc2016-05-24 16:37:28 +02001845 if (ret < 0) {
1846 msgtype = NC_MSG_ERROR;
1847 goto fail;
1848 } else if (!ret) {
1849 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001850 goto fail;
1851 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001852 } else
1853#endif
1854 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001855 ERRINT;
1856 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001857 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001858 goto fail;
1859 }
1860
1861 /* assign new SID atomically */
1862 /* LOCK */
1863 pthread_spin_lock(&server_opts.sid_lock);
1864 (*session)->id = server_opts.new_session_id++;
1865 /* UNLOCK */
1866 pthread_spin_unlock(&server_opts.sid_lock);
1867
1868 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001869 msgtype = nc_handshake(*session);
1870 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01001871 goto fail;
1872 }
Michal Vaskof802fdc2016-07-28 15:47:00 +02001873 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001874 (*session)->status = NC_STATUS_RUNNING;
1875
Michal Vasko71090fc2016-05-24 16:37:28 +02001876 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001877
1878fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001879 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001880 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001881 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001882}
1883
Radek Krejci53691be2016-02-22 13:58:37 +01001884#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02001885
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001886API time_t
1887nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02001888{
1889 if (!session) {
1890 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001891 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02001892 }
1893
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001894 return session->session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02001895}