blob: 277dfc32446819ac89764b15509cabea29a98fd7 [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 */
330 if (!format || !strcmp(format, "yang")) {
331 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
332 } else if (!strcmp(format, "yin")) {
333 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 Vasko3a715132016-01-21 15:40:31 +0100706 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100707 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100708 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200709 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100710
Michal Vasko428087d2016-01-14 16:04:28 +0100711 free(ps);
712}
713
714API int
715nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
716{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200717 uint8_t q_id;
718
Michal Vasko45e53ae2016-04-07 11:46:03 +0200719 if (!ps) {
720 ERRARG("ps");
721 return -1;
722 } else if (!session) {
723 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100724 return -1;
725 }
726
Michal Vasko48a63ed2016-03-01 09:48:21 +0100727 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200728 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200729 return -1;
730 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100731
Michal Vasko428087d2016-01-14 16:04:28 +0100732 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100733 ps->pfds = nc_realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
734 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
735 if (!ps->pfds || !ps->sessions) {
736 ERRMEM;
737 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200738 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100739 return -1;
740 }
Michal Vasko428087d2016-01-14 16:04:28 +0100741
742 switch (session->ti_type) {
743 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100744 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100745 break;
746
Radek Krejci53691be2016-02-22 13:58:37 +0100747#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100748 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100749 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100750 break;
751#endif
752
Radek Krejci53691be2016-02-22 13:58:37 +0100753#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100754 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100755 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100756 break;
757#endif
758
759 default:
760 ERRINT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100761 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200762 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100763 return -1;
764 }
Michal Vasko3a715132016-01-21 15:40:31 +0100765 ps->pfds[ps->session_count - 1].events = POLLIN;
766 ps->pfds[ps->session_count - 1].revents = 0;
767 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100768
Michal Vasko48a63ed2016-03-01 09:48:21 +0100769 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200770 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100771}
772
Michal Vasko48a63ed2016-03-01 09:48:21 +0100773static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100774_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100775{
776 uint16_t i;
777
Radek Krejcid5f978f2016-03-03 13:14:45 +0100778 if (index >= 0) {
779 i = (uint16_t)index;
780 goto remove;
781 }
Michal Vasko428087d2016-01-14 16:04:28 +0100782 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100783 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100784remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100785 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100786 if (i < ps->session_count) {
787 ps->sessions[i] = ps->sessions[ps->session_count];
788 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
789 } else if (!ps->session_count) {
790 free(ps->sessions);
791 ps->sessions = NULL;
792 free(ps->pfds);
793 ps->pfds = NULL;
794 }
Michal Vasko428087d2016-01-14 16:04:28 +0100795 return 0;
796 }
797 }
798
Michal Vaskof0537d82016-01-29 14:42:38 +0100799 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100800}
801
Michal Vasko48a63ed2016-03-01 09:48:21 +0100802API int
803nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
804{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200805 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200806 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100807
Michal Vasko45e53ae2016-04-07 11:46:03 +0200808 if (!ps) {
809 ERRARG("ps");
810 return -1;
811 } else if (!session) {
812 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100813 return -1;
814 }
815
816 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200817 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200818 return -1;
819 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100820
Radek Krejcid5f978f2016-03-03 13:14:45 +0100821 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100822
823 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200824 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100825
Michal Vaskobe86fe32016-04-07 10:43:03 +0200826 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100827}
828
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100829API uint16_t
830nc_ps_session_count(struct nc_pollsession *ps)
831{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200832 uint8_t q_id;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100833 uint16_t count;
834
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100835 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200836 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100837 return 0;
838 }
839
Michal Vasko48a63ed2016-03-01 09:48:21 +0100840 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200841 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200842 return -1;
843 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100844
845 count = ps->session_count;
846
847 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200848 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100849
850 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100851}
852
Michal Vasko71090fc2016-05-24 16:37:28 +0200853/* must be called holding the session lock!
854 * returns: NC_PSPOLL_ERROR,
855 * NC_PSPOLL_BAD_RPC,
856 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
857 * NC_PSPOLL_RPC
858 */
859static int
Michal Vasko428087d2016-01-14 16:04:28 +0100860nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
861{
862 struct lyxml_elem *xml = NULL;
863 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200864 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200865 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100866
Michal Vasko45e53ae2016-04-07 11:46:03 +0200867 if (!session) {
868 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200869 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200870 } else if (!rpc) {
871 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200872 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100873 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100874 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200875 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100876 }
877
878 msgtype = nc_read_msg(session, &xml);
879
880 switch (msgtype) {
881 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200882 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100883 if (!*rpc) {
884 ERRMEM;
885 goto error;
886 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100887
Radek Krejcif93c7d42016-04-06 13:41:15 +0200888 ly_errno = LY_SUCCESS;
Michal Vasko68b3f292016-09-16 12:00:32 +0200889 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_RPC | LYD_OPT_DESTRUCT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100890 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200891 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200892 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200893 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200894 nc_server_reply_free(reply);
895 if (ret == -1) {
896 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200897 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200898 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
899 } else {
900 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100901 }
Michal Vasko428087d2016-01-14 16:04:28 +0100902 (*rpc)->root = xml;
903 break;
904 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100905 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200906 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100907 goto error;
908 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100909 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200910 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100911 goto error;
912 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100913 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200914 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100915 goto error;
916 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200917 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100918 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
919 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200920 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100921 break;
922 }
923
Michal Vasko71090fc2016-05-24 16:37:28 +0200924 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100925
926error:
927 /* cleanup */
928 lyxml_free(server_opts.ctx, xml);
929
Michal Vasko71090fc2016-05-24 16:37:28 +0200930 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100931}
932
fanchanghu966f2de2016-07-21 02:28:57 -0400933API void
934nc_set_global_rpc_clb(nc_rpc_clb clb)
935{
936 global_rpc_clb = clb;
937}
938
Michal Vasko71090fc2016-05-24 16:37:28 +0200939/* must be called holding the session lock!
940 * returns: NC_PSPOLL_ERROR,
941 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
942 * NC_PSPOLL_REPLY_ERROR,
943 * 0
944 */
945static int
Michal Vasko428087d2016-01-14 16:04:28 +0100946nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
947{
948 nc_rpc_clb clb;
949 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +0200950 struct lys_node *rpc_act = NULL;
951 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +0200952 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +0100953
Michal Vasko4a827e52016-03-03 10:59:00 +0100954 if (!rpc) {
955 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200956 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +0100957 }
958
Michal Vasko90e8e692016-07-13 12:27:57 +0200959 if (rpc->tree->schema->nodetype == LYS_RPC) {
960 /* RPC */
961 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -0400962 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +0200963 /* action */
964 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
965 if (elem->schema->nodetype == LYS_ACTION) {
966 rpc_act = elem->schema;
967 break;
968 }
969 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -0400970 }
Michal Vasko90e8e692016-07-13 12:27:57 +0200971 if (!rpc_act) {
972 ERRINT;
973 return NC_PSPOLL_ERROR;
974 }
975 }
976
977 if (!rpc_act->priv) {
978 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +0100979 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100980 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +0200981 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +0100982 reply = clb(rpc->tree, session);
983 }
984
985 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100986 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100987 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200988 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
989 if (reply->type == NC_RPL_ERROR) {
990 ret |= NC_PSPOLL_REPLY_ERROR;
991 }
992 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +0100993
Michal Vasko71090fc2016-05-24 16:37:28 +0200994 if (r == -1) {
995 ERR("Session %u: failed to write reply.", session->id);
996 ret |= NC_PSPOLL_ERROR;
997 }
Michal Vasko428087d2016-01-14 16:04:28 +0100998
999 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1000 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1001 session->status = NC_STATUS_INVALID;
1002 }
1003
Michal Vasko71090fc2016-05-24 16:37:28 +02001004 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001005}
1006
1007API int
Michal Vasko71090fc2016-05-24 16:37:28 +02001008nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +01001009{
1010 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +02001011 uint8_t q_id;
Michal Vasko3512e402016-01-28 16:22:34 +01001012 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001013 time_t cur_time;
Michal Vasko71090fc2016-05-24 16:37:28 +02001014 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +01001015 struct nc_server_rpc *rpc = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +01001016
1017 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001018 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001019 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001020 }
1021
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001022 cur_time = time(NULL);
1023
Michal Vasko48a63ed2016-03-01 09:48:21 +01001024 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001025 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001026 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001027 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001028
Michal Vasko428087d2016-01-14 16:04:28 +01001029 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001030 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1031 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001032 ret = NC_PSPOLL_ERROR;
1033 if (session) {
1034 *session = ps->sessions[i];
1035 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001036 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001037 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001038
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001039 /* TODO invalidate only sessions without subscription */
Michal Vaskobae4ca32016-07-28 15:47:25 +02001040 if (server_opts.idle_timeout && (cur_time >= ps->sessions[i]->last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001041 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1042 ps->sessions[i]->status = NC_STATUS_INVALID;
1043 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001044 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1045 if (session) {
1046 *session = ps->sessions[i];
1047 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001048 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001049 }
1050
Michal Vasko3a715132016-01-21 15:40:31 +01001051 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +01001052 break;
1053 }
Michal Vasko428087d2016-01-14 16:04:28 +01001054 }
1055
Michal Vaskobd8ef262016-01-20 11:09:27 +01001056 if (i == ps->session_count) {
Radek Krejci53691be2016-02-22 13:58:37 +01001057#ifdef NC_ENABLED_SSH
Michal Vasko3a715132016-01-21 15:40:31 +01001058retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +01001059#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +01001060 /* no leftover event */
1061 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +01001062 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001063 if (ret < 0) {
1064 ERR("Poll failed (%s).", strerror(errno));
1065 ret = NC_PSPOLL_ERROR;
1066 goto finish;
1067 } else if (!ret) {
1068 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001069 goto finish;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001070 }
Michal Vasko428087d2016-01-14 16:04:28 +01001071 }
1072
Michal Vaskobd8ef262016-01-20 11:09:27 +01001073 /* find the first fd with POLLIN, we don't care if there are more now */
1074 for (; i < ps->session_count; ++i) {
Michal Vasko46eac552016-05-30 15:27:25 +02001075 if (ps->pfds[i].revents & (POLLHUP | POLLNVAL)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001076 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
1077 ps->sessions[i]->status = NC_STATUS_INVALID;
1078 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko71090fc2016-05-24 16:37:28 +02001079 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1080 if (session) {
1081 *session = ps->sessions[i];
1082 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001083 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001084 } else if (ps->pfds[i].revents & POLLERR) {
1085 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
1086 ps->sessions[i]->status = NC_STATUS_INVALID;
1087 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko71090fc2016-05-24 16:37:28 +02001088 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1089 if (session) {
1090 *session = ps->sessions[i];
1091 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001092 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001093 } else if (ps->pfds[i].revents & POLLIN) {
Radek Krejci53691be2016-02-22 13:58:37 +01001094#ifdef NC_ENABLED_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +01001095 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +01001096 uint16_t j;
1097
Michal Vasko96164bf2016-01-21 15:41:58 +01001098 /* things are not that simple with SSH... */
Michal Vasko62be1ce2016-03-03 13:24:52 +01001099 ret = nc_ssh_pollin(ps->sessions[i], timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +01001100
1101 /* clear POLLIN on sessions sharing this session's SSH session */
Michal Vasko71090fc2016-05-24 16:37:28 +02001102 if (ret & (NC_PSPOLL_RPC | NC_PSPOLL_SSH_MSG | NC_PSPOLL_SSH_CHANNEL)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001103 for (j = i + 1; j < ps->session_count; ++j) {
1104 if (ps->pfds[j].fd == ps->pfds[i].fd) {
1105 ps->pfds[j].revents = 0;
1106 }
1107 }
1108 }
1109
Michal Vasko71090fc2016-05-24 16:37:28 +02001110 /* SSH message only */
1111 if (!(ret & (NC_PSPOLL_RPC | NC_PSPOLL_PENDING))) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001112 ps->pfds[i].revents = 0;
Michal Vasko71090fc2016-05-24 16:37:28 +02001113 if (session) {
1114 *session = ps->sessions[i];
1115 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001116 goto finish;
Michal Vasko96164bf2016-01-21 15:41:58 +01001117
1118 /* event occurred on some other channel */
Michal Vasko71090fc2016-05-24 16:37:28 +02001119 } else if (ret & NC_PSPOLL_PENDING) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001120 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001121 if (i == ps->session_count - 1) {
1122 /* last session and it is not the right channel, ... */
Michal Vasko8c748832016-02-03 15:32:16 +01001123 if (!timeout) {
Michal Vasko428087d2016-01-14 16:04:28 +01001124 /* ... timeout is 0, so that is it */
Michal Vasko71090fc2016-05-24 16:37:28 +02001125 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001126 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001127 }
Michal Vasko8c748832016-02-03 15:32:16 +01001128 /* ... retry polling reasonable time apart ... */
1129 usleep(NC_TIMEOUT_STEP);
1130 if (timeout > 0) {
1131 /* ... and decrease timeout, if not -1 */
Michal Vasko7b38e232016-02-26 15:01:07 +01001132 timeout -= NC_TIMEOUT_STEP * 1000;
Michal Vasko8c748832016-02-03 15:32:16 +01001133 }
1134 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +01001135 }
1136 /* check other sessions */
1137 continue;
Michal Vasko428087d2016-01-14 16:04:28 +01001138 }
1139 }
Radek Krejci53691be2016-02-22 13:58:37 +01001140#endif /* NC_ENABLED_SSH */
Michal Vasko428087d2016-01-14 16:04:28 +01001141
Michal Vaskobd8ef262016-01-20 11:09:27 +01001142 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +01001143 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001144 break;
1145 }
1146 }
1147
1148 if (i == ps->session_count) {
1149 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001150 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001151 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001152 }
1153
1154 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001155 cur_session = ps->sessions[i];
1156 if (session) {
1157 *session = cur_session;
1158 }
Michal Vasko428087d2016-01-14 16:04:28 +01001159
Michal Vaskobd8ef262016-01-20 11:09:27 +01001160 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal vasko953939c2016-10-04 13:46:20 +02001161 ret = nc_timedlock(cur_session->ti_lock, timeout, __func__);
Michal Vasko71090fc2016-05-24 16:37:28 +02001162 if (ret < 0) {
1163 ret = NC_PSPOLL_ERROR;
1164 goto finish;
1165 } else if (!ret) {
1166 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001167 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001168 }
1169
Michal Vasko71090fc2016-05-24 16:37:28 +02001170 ret = nc_recv_rpc(cur_session, &rpc);
1171 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1172 pthread_mutex_unlock(cur_session->ti_lock);
1173 if (cur_session->status != NC_STATUS_RUNNING) {
1174 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001175 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001176 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001177 }
1178
Michal Vasko71090fc2016-05-24 16:37:28 +02001179 cur_session->last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001180
Michal Vasko428087d2016-01-14 16:04:28 +01001181 /* process RPC */
Michal Vasko71090fc2016-05-24 16:37:28 +02001182 ret |= nc_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001183
Michal Vasko71090fc2016-05-24 16:37:28 +02001184 pthread_mutex_unlock(cur_session->ti_lock);
1185 if (cur_session->status != NC_STATUS_RUNNING) {
1186 ret |= NC_PSPOLL_SESSION_TERM;
1187 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1188 ret |= NC_PSPOLL_SESSION_ERROR;
1189 }
Michal Vasko428087d2016-01-14 16:04:28 +01001190 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001191
Michal Vaskoca4a2422016-02-02 12:17:14 +01001192 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001193
1194 /* is there some other socket waiting? */
1195 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001196 if (ps->pfds[i].revents) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001197 ret |= NC_PSPOLL_PENDING;
1198 break;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001199 }
1200 }
1201
Michal Vasko48a63ed2016-03-01 09:48:21 +01001202finish:
1203 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001204 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001205 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001206}
1207
Michal Vaskod09eae62016-02-01 10:32:52 +01001208API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001209nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001210{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001211 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001212 uint16_t i;
1213 struct nc_session *session;
1214
Michal Vasko9a25e932016-02-01 10:36:42 +01001215 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001216 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001217 return;
1218 }
1219
Michal Vasko48a63ed2016-03-01 09:48:21 +01001220 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001221 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001222 return;
1223 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001224
Michal Vasko48a63ed2016-03-01 09:48:21 +01001225 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001226 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001227 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001228 }
1229 free(ps->sessions);
1230 ps->sessions = NULL;
1231 free(ps->pfds);
1232 ps->pfds = NULL;
1233 ps->session_count = 0;
1234 } else {
1235 for (i = 0; i < ps->session_count; ) {
1236 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1237 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001238 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001239 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001240 continue;
1241 }
1242
1243 ++i;
1244 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001245 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001246
1247 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001248 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001249}
1250
Radek Krejci53691be2016-02-22 13:58:37 +01001251#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001252
Michal Vaskoe2713da2016-08-22 16:06:40 +02001253API int
1254nc_server_add_endpt(const char *name)
Michal Vasko9e036d52016-01-08 10:49:26 +01001255{
Michal Vasko3031aae2016-01-27 16:07:18 +01001256 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +01001257#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001258 uint16_t bind_ssh_idx;
1259#endif
1260#ifdef NC_ENABLED_TLS
1261 uint16_t bind_tls_idx;
Michal Vasko08a629a2016-02-02 12:20:47 +01001262#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001263
Michal Vasko45e53ae2016-04-07 11:46:03 +02001264 if (!name) {
1265 ERRARG("name");
1266 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001267 }
1268
Michal Vasko51e514d2016-02-02 15:51:52 +01001269 /* WRITE LOCK */
1270 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001271
1272 /* check name uniqueness */
1273 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001274 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001275 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001276 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +01001277 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001278 return -1;
1279 }
1280 }
1281
Michal Vasko3031aae2016-01-27 16:07:18 +01001282 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001283 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001284 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001285 ERRMEM;
1286 /* WRITE UNLOCK */
1287 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001288 return -1;
1289 }
1290 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1291
1292#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1293 server_opts.binds = nc_realloc(server_opts.binds, 2 * server_opts.endpt_count * sizeof *server_opts.binds);
1294 bind_ssh_idx = (server_opts.endpt_count - 1) * 2;
1295 bind_tls_idx = (server_opts.endpt_count - 1) * 2 + 1;
1296#else
1297 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
1298# ifdef NC_ENABLED_SSH
1299 bind_ssh_idx = server_opts.endpt_count - 1;
1300# endif
1301# ifdef NC_ENABLED_TLS
1302 bind_tls_idx = server_opts.endpt_count - 1;
1303# endif
1304#endif
1305 if (!server_opts.binds) {
1306 ERRMEM;
1307 /* WRITE UNLOCK */
1308 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001309 return -1;
1310 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001311
Radek Krejci53691be2016-02-22 13:58:37 +01001312#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001313 server_opts.binds[bind_ssh_idx].address = NULL;
1314 server_opts.binds[bind_ssh_idx].port = 0;
1315 server_opts.binds[bind_ssh_idx].sock = -1;
1316 server_opts.binds[bind_ssh_idx].ti = NC_TI_LIBSSH;
Michal Vasko08a629a2016-02-02 12:20:47 +01001317
Michal Vaskoe2713da2016-08-22 16:06:40 +02001318 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts = calloc(1, sizeof(struct nc_server_ssh_opts));
1319 if (!server_opts.endpts[server_opts.endpt_count - 1].ssh_opts) {
1320 ERRMEM;
1321 /* WRITE UNLOCK */
1322 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1323 return -1;
Michal Vasko3031aae2016-01-27 16:07:18 +01001324 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001325 /* set default values */
1326 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_methods =
1327 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1328 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_attempts = 3;
1329 server_opts.endpts[server_opts.endpt_count - 1].ssh_opts->auth_timeout = 10;
1330#endif
1331
1332#ifdef NC_ENABLED_TLS
1333 server_opts.binds[bind_tls_idx].address = NULL;
1334 server_opts.binds[bind_tls_idx].port = 0;
1335 server_opts.binds[bind_tls_idx].sock = -1;
1336 server_opts.binds[bind_tls_idx].ti = NC_TI_OPENSSL;
1337
1338 server_opts.endpts[server_opts.endpt_count - 1].tls_opts = calloc(1, sizeof(struct nc_server_tls_opts));
1339 if (!server_opts.endpts[server_opts.endpt_count - 1].tls_opts) {
1340 ERRMEM;
1341 /* WRITE UNLOCK */
1342 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1343 return -1;
1344 }
1345#endif
1346
Michal Vasko3031aae2016-01-27 16:07:18 +01001347 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001348
Michal Vasko3031aae2016-01-27 16:07:18 +01001349 /* WRITE UNLOCK */
1350 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001351
Michal Vasko9e036d52016-01-08 10:49:26 +01001352 return 0;
1353}
1354
Michal Vasko3031aae2016-01-27 16:07:18 +01001355int
Michal Vaskoda514772016-02-01 11:32:01 +01001356nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1357{
1358 struct nc_endpt *endpt;
1359 struct nc_bind *bind = NULL;
1360 uint16_t i;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001361 int sock = -1, set_addr;
Michal Vaskoda514772016-02-01 11:32:01 +01001362
Michal Vasko45e53ae2016-04-07 11:46:03 +02001363 if (!endpt_name) {
1364 ERRARG("endpt_name");
1365 return -1;
1366 } else if ((!address && !port) || (address && port)) {
1367 ERRARG("address and port");
1368 return -1;
1369 } else if (!ti) {
1370 ERRARG("ti");
Michal Vaskoda514772016-02-01 11:32:01 +01001371 return -1;
1372 }
1373
Michal Vaskoe2713da2016-08-22 16:06:40 +02001374 if (address) {
1375 set_addr = 1;
1376 } else {
1377 set_addr = 0;
1378 }
1379
Michal Vasko51e514d2016-02-02 15:51:52 +01001380 /* LOCK */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001381 endpt = nc_server_endpt_lock(endpt_name, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001382 if (!endpt) {
1383 return -1;
1384 }
1385
Michal Vaskoe2713da2016-08-22 16:06:40 +02001386#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1387 if (ti == NC_TI_LIBSSH) {
1388 bind = &server_opts.binds[2 * i];
1389 } else {
1390 bind = &server_opts.binds[2 * i + 1];
Michal Vaskoda514772016-02-01 11:32:01 +01001391 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001392#else
1393 bind = &server_opts.binds[i];
Michal Vasko5e391372016-08-22 16:20:58 +02001394 if (bind->ti != ti) {
Michal Vaskoda514772016-02-01 11:32:01 +01001395 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001396 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001397 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001398#endif
Michal Vaskoda514772016-02-01 11:32:01 +01001399
Michal Vaskoe2713da2016-08-22 16:06:40 +02001400 if (set_addr) {
1401 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001402 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001403 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001404 }
1405
Michal Vaskoe2713da2016-08-22 16:06:40 +02001406 /* we have all the information we need to create a listening socket */
1407 if (address && port) {
1408 /* create new socket, close the old one */
1409 sock = nc_sock_listen(address, port);
1410 if (sock == -1) {
1411 goto fail;
1412 }
1413
1414 if (bind->sock > -1) {
1415 close(bind->sock);
1416 }
1417 bind->sock = sock;
1418 } /* else we are just setting address or port */
1419
1420 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001421 lydict_remove(server_opts.ctx, bind->address);
1422 bind->address = lydict_insert(server_opts.ctx, address, 0);
1423 } else {
1424 bind->port = port;
1425 }
1426
Michal Vaskoe2713da2016-08-22 16:06:40 +02001427 if (sock > -1) {
1428#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1429 VRB("Listening on %s:%u for %s connections.", address, port, (ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
1430#elif defined(NC_ENABLED_SSH)
1431 VRB("Listening on %s:%u for SSH connections.", address, port);
1432#else
1433 VRB("Listening on %s:%u for TLS connections.", address, port);
1434#endif
1435 }
1436
Michal Vasko51e514d2016-02-02 15:51:52 +01001437 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001438 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001439 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001440
1441fail:
1442 /* UNLOCK */
1443 nc_server_endpt_unlock(endpt);
1444 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001445}
1446
Michal Vaskoe2713da2016-08-22 16:06:40 +02001447API int
1448nc_server_del_endpt(const char *name)
Michal Vasko9e036d52016-01-08 10:49:26 +01001449{
1450 uint32_t i;
1451 int ret = -1;
1452
Michal Vasko3031aae2016-01-27 16:07:18 +01001453 /* WRITE LOCK */
1454 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001455
Michal Vaskoe2713da2016-08-22 16:06:40 +02001456 if (!name) {
1457 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001458 for (i = 0; i < server_opts.endpt_count; ++i) {
1459 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko3031aae2016-01-27 16:07:18 +01001460 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
Radek Krejci53691be2016-02-22 13:58:37 +01001461#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001462 nc_server_ssh_clear_opts(server_opts.endpts[i].ssh_opts);
1463 free(server_opts.endpts[i].ssh_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001464#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001465#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001466 nc_server_tls_clear_opts(server_opts.endpts[i].tls_opts);
1467 free(server_opts.endpts[i].tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001468#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001469 ret = 0;
1470 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001471 free(server_opts.endpts);
1472 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001473
1474 /* remove all binds */
1475 for (i = 0; i < server_opts.endpt_count; ++i) {
1476 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1477 if (server_opts.binds[i].sock > -1) {
1478 close(server_opts.binds[i].sock);
1479 }
1480 }
1481#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1482 for (; i < 2 * server_opts.endpt_count; ++i) {
1483 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1484 if (server_opts.binds[i].sock > -1) {
1485 close(server_opts.binds[i].sock);
1486 }
1487 }
1488#endif
1489 free(server_opts.binds);
1490 server_opts.binds = NULL;
1491
Michal Vasko3031aae2016-01-27 16:07:18 +01001492 server_opts.endpt_count = 0;
1493
Michal Vasko1a38c862016-01-15 15:50:07 +01001494 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001495 /* remove one endpoint with bind(s) */
Michal Vasko3031aae2016-01-27 16:07:18 +01001496 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001497 if (!strcmp(server_opts.endpts[i].name, name)) {
1498 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001499 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko3031aae2016-01-27 16:07:18 +01001500 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
Radek Krejci53691be2016-02-22 13:58:37 +01001501#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001502 nc_server_ssh_clear_opts(server_opts.endpts[i].ssh_opts);
1503 free(server_opts.endpts[i].ssh_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001504#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001505#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001506 nc_server_tls_clear_opts(server_opts.endpts[i].tls_opts);
1507 free(server_opts.endpts[i].tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001508#endif
Michal Vasko1a38c862016-01-15 15:50:07 +01001509
Michal Vaskoe2713da2016-08-22 16:06:40 +02001510 /* remove bind(s) */
1511#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1512 i *= 2;
1513 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1514 if (server_opts.binds[i].sock > -1) {
1515 close(server_opts.binds[i].sock);
1516 }
1517 ++i;
1518#endif
1519 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1520 if (server_opts.binds[i].sock > -1) {
1521 close(server_opts.binds[i].sock);
1522 }
1523
1524 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001525 --server_opts.endpt_count;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001526#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1527 --i;
1528 i /= 2;
1529 if (i < server_opts.endpt_count) {
1530 memcpy(&server_opts.binds[2 * i], &server_opts.binds[2 * server_opts.endpt_count], 2 * sizeof *server_opts.binds);
1531 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1532 }
1533#else
Michal Vaskoc0256492016-02-02 12:19:06 +01001534 if (i < server_opts.endpt_count) {
1535 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1536 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001537 }
1538#endif
1539 else if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001540 free(server_opts.binds);
1541 server_opts.binds = NULL;
1542 free(server_opts.endpts);
1543 server_opts.endpts = NULL;
1544 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001545
1546 ret = 0;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001547 break;
Michal Vasko1a38c862016-01-15 15:50:07 +01001548 }
1549 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001550 }
1551
Michal Vasko3031aae2016-01-27 16:07:18 +01001552 /* WRITE UNLOCK */
1553 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001554
Michal Vasko9e036d52016-01-08 10:49:26 +01001555 return ret;
1556}
1557
Michal Vasko71090fc2016-05-24 16:37:28 +02001558API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001559nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001560{
Michal Vasko71090fc2016-05-24 16:37:28 +02001561 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001562 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001563 char *host = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001564 uint16_t port, endpt_idx, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001565
Michal Vasko45e53ae2016-04-07 11:46:03 +02001566 if (!server_opts.ctx) {
1567 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001568 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001569 } else if (!session) {
1570 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001571 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001572 }
1573
Michal Vasko51e514d2016-02-02 15:51:52 +01001574 /* we have to hold WRITE for the whole time, since there is not
1575 * a way of downgrading the lock to READ */
1576 /* WRITE LOCK */
1577 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1578
1579 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001580 ERR("No endpoints to accept sessions on.");
Michal Vasko51e514d2016-02-02 15:51:52 +01001581 /* WRITE UNLOCK */
1582 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001583 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001584 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001585
Michal Vasko94acafc2016-09-23 13:40:10 +02001586#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1587 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count * 2, timeout, &host, &port, &bind_idx);
1588#else
Michal Vaskoe2713da2016-08-22 16:06:40 +02001589 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko94acafc2016-09-23 13:40:10 +02001590#endif
Michal Vaskob48aa812016-01-18 14:13:09 +01001591
Michal Vasko50456e82016-02-02 12:16:08 +01001592 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001593 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001594 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001595 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001596 if (!ret) {
1597 return NC_MSG_WOULDBLOCK;
1598 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001599 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001600 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001601 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001602
Michal Vasko1a38c862016-01-15 15:50:07 +01001603 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001604 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001605 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001606 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001607 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001608 msgtype = NC_MSG_ERROR;
1609 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001610 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001611 (*session)->status = NC_STATUS_STARTING;
1612 (*session)->side = NC_SERVER;
1613 (*session)->ctx = server_opts.ctx;
1614 (*session)->flags = NC_SESSION_SHAREDCTX;
1615 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1616 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001617
1618 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001619 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1620 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001621 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001622 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001623 msgtype = NC_MSG_ERROR;
1624 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001625 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001626 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001627
Michal Vaskoe2713da2016-08-22 16:06:40 +02001628 endpt_idx = bind_idx;
1629 /* transform index as needed */
1630#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1631 if (server_opts.binds[bind_idx].ti == NC_TI_OPENSSL) {
1632 --endpt_idx;
1633 }
1634 endpt_idx /= 2;
1635#endif
Michal Vasko3031aae2016-01-27 16:07:18 +01001636
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001637 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001638#ifdef NC_ENABLED_SSH
Michal Vaskoe2713da2016-08-22 16:06:40 +02001639 if (server_opts.binds[bind_idx].ti == NC_TI_LIBSSH) {
1640 (*session)->data = server_opts.endpts[endpt_idx].ssh_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001641 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001642 if (ret < 0) {
1643 msgtype = NC_MSG_ERROR;
1644 goto cleanup;
1645 } else if (!ret) {
1646 msgtype = NC_MSG_WOULDBLOCK;
1647 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001648 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001649 } else
1650#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001651#ifdef NC_ENABLED_TLS
Michal Vaskoe2713da2016-08-22 16:06:40 +02001652 if (server_opts.binds[bind_idx].ti == NC_TI_OPENSSL) {
1653 (*session)->data = server_opts.endpts[endpt_idx].tls_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001654 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001655 if (ret < 0) {
1656 msgtype = NC_MSG_ERROR;
1657 goto cleanup;
1658 } else if (!ret) {
1659 msgtype = NC_MSG_WOULDBLOCK;
1660 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001661 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001662 } else
1663#endif
1664 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001665 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001666 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001667 msgtype = NC_MSG_ERROR;
1668 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001669 }
1670
Michal Vasko2cc4c682016-03-01 09:16:48 +01001671 (*session)->data = NULL;
1672
Michal Vasko51e514d2016-02-02 15:51:52 +01001673 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001674 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1675
Michal Vaskob48aa812016-01-18 14:13:09 +01001676 /* assign new SID atomically */
1677 /* LOCK */
1678 pthread_spin_lock(&server_opts.sid_lock);
1679 (*session)->id = server_opts.new_session_id++;
1680 /* UNLOCK */
1681 pthread_spin_unlock(&server_opts.sid_lock);
1682
Michal Vasko9e036d52016-01-08 10:49:26 +01001683 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001684 msgtype = nc_handshake(*session);
1685 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001686 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001687 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001688 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001689 }
Michal Vaskof802fdc2016-07-28 15:47:00 +02001690 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001691 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001692
Michal Vasko71090fc2016-05-24 16:37:28 +02001693 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001694
Michal Vasko71090fc2016-05-24 16:37:28 +02001695cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001696 /* WRITE UNLOCK */
1697 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1698
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001699 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001700 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001701 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001702}
1703
Michal Vasko71090fc2016-05-24 16:37:28 +02001704NC_MSG_TYPE
Michal Vasko8f5270d2016-02-29 16:22:25 +01001705nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01001706{
Michal Vasko71090fc2016-05-24 16:37:28 +02001707 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001708 int sock, ret;
1709
Michal Vasko45e53ae2016-04-07 11:46:03 +02001710 if (!host) {
1711 ERRARG("host");
Michal Vasko71090fc2016-05-24 16:37:28 +02001712 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001713 } else if (!port) {
1714 ERRARG("port");
Michal Vasko71090fc2016-05-24 16:37:28 +02001715 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001716 } else if (!ti) {
1717 ERRARG("ti");
Michal Vasko71090fc2016-05-24 16:37:28 +02001718 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001719 } else if (!session) {
1720 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001721 return NC_MSG_ERROR;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001722 }
1723
Michal Vaskob05053d2016-01-22 16:12:06 +01001724 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001725 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001726 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001727 }
1728
1729 *session = calloc(1, sizeof **session);
1730 if (!(*session)) {
1731 ERRMEM;
1732 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001733 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001734 }
1735 (*session)->status = NC_STATUS_STARTING;
1736 (*session)->side = NC_SERVER;
1737 (*session)->ctx = server_opts.ctx;
1738 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001739 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001740 (*session)->port = port;
1741
1742 /* transport lock */
1743 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1744 if (!(*session)->ti_lock) {
1745 ERRMEM;
1746 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001747 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001748 goto fail;
1749 }
1750 pthread_mutex_init((*session)->ti_lock, NULL);
1751
1752 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001753#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001754 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001755 /* OPTS LOCK */
1756 pthread_mutex_lock(&ssh_ch_opts_lock);
1757
Michal Vasko2cc4c682016-03-01 09:16:48 +01001758 (*session)->data = &ssh_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001759 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001760 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001761
1762 /* OPTS UNLOCK */
1763 pthread_mutex_unlock(&ssh_ch_opts_lock);
1764
Michal Vasko71090fc2016-05-24 16:37:28 +02001765 if (ret < 0) {
1766 msgtype = NC_MSG_ERROR;
1767 goto fail;
1768 } else if (!ret) {
1769 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001770 goto fail;
1771 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001772 } else
1773#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001774#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001775 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001776 /* OPTS LOCK */
1777 pthread_mutex_lock(&tls_ch_opts_lock);
1778
Michal Vasko2cc4c682016-03-01 09:16:48 +01001779 (*session)->data = &tls_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001780 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001781 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001782
1783 /* OPTS UNLOCK */
1784 pthread_mutex_unlock(&tls_ch_opts_lock);
1785
Michal Vasko71090fc2016-05-24 16:37:28 +02001786 if (ret < 0) {
1787 msgtype = NC_MSG_ERROR;
1788 goto fail;
1789 } else if (!ret) {
1790 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001791 goto fail;
1792 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001793 } else
1794#endif
1795 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001796 ERRINT;
1797 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001798 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001799 goto fail;
1800 }
1801
1802 /* assign new SID atomically */
1803 /* LOCK */
1804 pthread_spin_lock(&server_opts.sid_lock);
1805 (*session)->id = server_opts.new_session_id++;
1806 /* UNLOCK */
1807 pthread_spin_unlock(&server_opts.sid_lock);
1808
1809 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001810 msgtype = nc_handshake(*session);
1811 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01001812 goto fail;
1813 }
Michal Vaskof802fdc2016-07-28 15:47:00 +02001814 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001815 (*session)->status = NC_STATUS_RUNNING;
1816
Michal Vasko71090fc2016-05-24 16:37:28 +02001817 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001818
1819fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001820 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001821 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001822 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001823}
1824
Radek Krejci53691be2016-02-22 13:58:37 +01001825#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02001826
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001827API time_t
1828nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02001829{
1830 if (!session) {
1831 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001832 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02001833 }
1834
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001835 return session->session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02001836}