blob: 1df740d9e1fe7f40fb19c933be0c8e4050ac64ce [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 *
Michal Vasko18aeb5d2017-02-17 09:23:56 +01006 * Copyright (c) 2015 - 2017 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01007 *
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 */
Michal Vaskoade892d2017-02-22 13:40:35 +010014#define _POSIX_SOUCE /* signals */
Michal Vasko086311b2016-01-08 09:53:11 +010015
16#include <stdint.h>
17#include <stdlib.h>
18#include <errno.h>
19#include <string.h>
20#include <poll.h>
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010026#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010027#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010028#include <time.h>
Michal Vaskoade892d2017-02-22 13:40:35 +010029#include <signal.h>
Michal Vasko086311b2016-01-08 09:53:11 +010030
Michal Vasko1a38c862016-01-15 15:50:07 +010031#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "session_server.h"
33
Michal Vaskob48aa812016-01-18 14:13:09 +010034struct nc_server_opts server_opts = {
Michal Vaskoade892d2017-02-22 13:40:35 +010035#ifdef NC_ENABLED_SSH
36 .authkey_lock = PTHREAD_MUTEX_INITIALIZER,
37#endif
38 .bind_lock = PTHREAD_MUTEX_INITIALIZER,
Michal Vasko2e6defd2016-10-07 15:48:15 +020039 .endpt_lock = PTHREAD_RWLOCK_INITIALIZER,
40 .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010041};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010042
fanchanghu966f2de2016-07-21 02:28:57 -040043static nc_rpc_clb global_rpc_clb = NULL;
44
Michal Vasko3031aae2016-01-27 16:07:18 +010045struct nc_endpt *
Michal Vaskoade892d2017-02-22 13:40:35 +010046nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010047{
48 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010049 struct nc_endpt *endpt = NULL;
50
Michal Vaskoade892d2017-02-22 13:40:35 +010051 /* WRITE LOCK */
52 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010053
54 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +020055 if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010056 endpt = &server_opts.endpts[i];
57 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010058 }
59 }
60
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010061 if (!endpt) {
62 ERR("Endpoint \"%s\" was not found.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +010063 /* UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020064 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010065 return NULL;
66 }
67
Michal Vaskoe2713da2016-08-22 16:06:40 +020068 if (idx) {
69 *idx = i;
70 }
71
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010072 return endpt;
73}
74
Michal Vasko2e6defd2016-10-07 15:48:15 +020075struct nc_ch_client *
76nc_server_ch_client_lock(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
77{
78 uint16_t i;
79 struct nc_ch_client *client = NULL;
80
81 /* READ LOCK */
82 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
83
84 for (i = 0; i < server_opts.ch_client_count; ++i) {
85 if (!strcmp(server_opts.ch_clients[i].name, name) && (!ti || (server_opts.ch_clients[i].ti == ti))) {
86 client = &server_opts.ch_clients[i];
87 break;
88 }
89 }
90
91 if (!client) {
92 ERR("Call Home client \"%s\" was not found.", name);
93 /* READ UNLOCK */
94 pthread_rwlock_unlock(&server_opts.ch_client_lock);
95 return NULL;
96 }
97
98 /* CH CLIENT LOCK */
99 pthread_mutex_lock(&client->lock);
100
101 if (idx) {
102 *idx = i;
103 }
104
105 return client;
106}
107
108void
109nc_server_ch_client_unlock(struct nc_ch_client *client)
110{
111 /* CH CLIENT UNLOCK */
112 pthread_mutex_unlock(&client->lock);
113
114 /* READ UNLOCK */
115 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100116}
Michal Vasko086311b2016-01-08 09:53:11 +0100117
Michal Vasko1a38c862016-01-15 15:50:07 +0100118API void
119nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
120{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200121 if (!session) {
122 ERRARG("session");
123 return;
124 } else if (!reason) {
125 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +0100126 return;
127 }
128
129 session->term_reason = reason;
130}
131
Michal Vasko086311b2016-01-08 09:53:11 +0100132int
Michal Vaskof05562c2016-01-20 12:06:43 +0100133nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100134{
135 const int optVal = 1;
136 const socklen_t optLen = sizeof(optVal);
137 int is_ipv4, sock;
138 struct sockaddr_storage saddr;
139
140 struct sockaddr_in *saddr4;
141 struct sockaddr_in6 *saddr6;
142
143
144 if (!strchr(address, ':')) {
145 is_ipv4 = 1;
146 } else {
147 is_ipv4 = 0;
148 }
149
150 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
151 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100152 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100153 goto fail;
154 }
155
156 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100157 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100158 goto fail;
159 }
160
161 bzero(&saddr, sizeof(struct sockaddr_storage));
162 if (is_ipv4) {
163 saddr4 = (struct sockaddr_in *)&saddr;
164
165 saddr4->sin_family = AF_INET;
166 saddr4->sin_port = htons(port);
167
168 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100169 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100170 goto fail;
171 }
172
173 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100174 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100175 goto fail;
176 }
177
178 } else {
179 saddr6 = (struct sockaddr_in6 *)&saddr;
180
181 saddr6->sin6_family = AF_INET6;
182 saddr6->sin6_port = htons(port);
183
184 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100185 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100186 goto fail;
187 }
188
189 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100190 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100191 goto fail;
192 }
193 }
194
Michal Vaskofb89d772016-01-08 12:25:35 +0100195 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100196 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100197 goto fail;
198 }
199
200 return sock;
201
202fail:
203 if (sock > -1) {
204 close(sock);
205 }
206
207 return -1;
208}
209
210int
Michal Vasko3031aae2016-01-27 16:07:18 +0100211nc_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 +0100212{
Michal Vaskof54cd352017-02-22 13:42:02 +0100213 sigset_t sigmask, origmask;
Michal Vaskoac2f6182017-01-30 14:32:03 +0100214 uint16_t i, j, pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100215 struct pollfd *pfd;
216 struct sockaddr_storage saddr;
217 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100218 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100219
220 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100221 if (!pfd) {
222 ERRMEM;
223 return -1;
224 }
225
Michal Vaskoac2f6182017-01-30 14:32:03 +0100226 for (i = 0, pfd_count = 0; i < bind_count; ++i) {
Michal Vasko94acafc2016-09-23 13:40:10 +0200227 if (binds[i].sock < 0) {
228 /* invalid socket */
Michal Vasko94acafc2016-09-23 13:40:10 +0200229 continue;
230 }
Michal Vasko0a3f3752016-10-13 14:58:38 +0200231 if (binds[i].pollin) {
232 binds[i].pollin = 0;
233 /* leftover pollin */
234 sock = binds[i].sock;
235 break;
236 }
Michal Vaskoac2f6182017-01-30 14:32:03 +0100237 pfd[pfd_count].fd = binds[i].sock;
238 pfd[pfd_count].events = POLLIN;
239 pfd[pfd_count].revents = 0;
240
241 ++pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100242 }
243
Michal Vasko0a3f3752016-10-13 14:58:38 +0200244 if (sock == -1) {
245 /* poll for a new connection */
Michal Vaskof54cd352017-02-22 13:42:02 +0100246 sigfillset(&sigmask);
247 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vaskoac2f6182017-01-30 14:32:03 +0100248 ret = poll(pfd, pfd_count, timeout);
Michal Vaskof54cd352017-02-22 13:42:02 +0100249 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
250
Michal Vasko0a3f3752016-10-13 14:58:38 +0200251 if (!ret) {
252 /* we timeouted */
253 free(pfd);
254 return 0;
255 } else if (ret == -1) {
256 ERR("Poll failed (%s).", strerror(errno));
257 free(pfd);
258 return -1;
259 }
Michal Vasko086311b2016-01-08 09:53:11 +0100260
Michal Vaskoac2f6182017-01-30 14:32:03 +0100261 for (i = 0, j = 0; j < pfd_count; ++i, ++j) {
262 /* adjust i so that indices in binds and pfd always match */
263 while (binds[i].sock != pfd[j].fd) {
264 ++i;
265 }
266
267 if (pfd[j].revents & POLLIN) {
Michal Vasko0a3f3752016-10-13 14:58:38 +0200268 --ret;
269
270 if (!ret) {
271 /* the last socket with an event, use it */
Michal Vaskoac2f6182017-01-30 14:32:03 +0100272 sock = pfd[j].fd;
Michal Vasko0a3f3752016-10-13 14:58:38 +0200273 break;
274 } else {
275 /* just remember the event for next time */
276 binds[i].pollin = 1;
277 }
278 }
Michal Vasko086311b2016-01-08 09:53:11 +0100279 }
280 }
281 free(pfd);
282
283 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100284 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100285 return -1;
286 }
287
288 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100289 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100290 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100291 return -1;
292 }
Michal Vasko6ccb29d2016-10-13 15:00:27 +0200293 VRB("Accepted a connection on %s:%u.", binds[i].address, binds[i].port);
Michal Vasko086311b2016-01-08 09:53:11 +0100294
Michal Vasko0190bc32016-03-02 15:47:49 +0100295 /* make the socket non-blocking */
296 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
297 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100298 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100299 return -1;
300 }
301
Michal Vasko3031aae2016-01-27 16:07:18 +0100302 if (idx) {
303 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100304 }
305
Michal Vasko086311b2016-01-08 09:53:11 +0100306 /* host was requested */
307 if (host) {
308 if (saddr.ss_family == AF_INET) {
309 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100310 if (*host) {
311 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
312 ERR("inet_ntop failed (%s).", strerror(errno));
313 free(*host);
314 *host = NULL;
315 }
Michal Vasko086311b2016-01-08 09:53:11 +0100316
Michal Vasko4eb3c312016-03-01 14:09:37 +0100317 if (port) {
318 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
319 }
320 } else {
321 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100322 }
323 } else if (saddr.ss_family == AF_INET6) {
324 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100325 if (*host) {
326 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
327 ERR("inet_ntop failed (%s).", strerror(errno));
328 free(*host);
329 *host = NULL;
330 }
Michal Vasko086311b2016-01-08 09:53:11 +0100331
Michal Vasko4eb3c312016-03-01 14:09:37 +0100332 if (port) {
333 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
334 }
335 } else {
336 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100337 }
338 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100339 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100340 }
341 }
342
343 return ret;
344}
345
Michal Vasko05ba9df2016-01-13 14:40:27 +0100346static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100347nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100348{
349 const char *identifier = NULL, *version = NULL, *format = NULL;
350 char *model_data = NULL;
351 const struct lys_module *module;
352 struct nc_server_error *err;
353 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100354 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100355
356 LY_TREE_FOR(rpc->child, child) {
357 if (!strcmp(child->schema->name, "identifier")) {
358 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
359 } else if (!strcmp(child->schema->name, "version")) {
360 version = ((struct lyd_node_leaf_list *)child)->value_str;
361 } else if (!strcmp(child->schema->name, "format")) {
362 format = ((struct lyd_node_leaf_list *)child)->value_str;
363 }
364 }
365
366 /* check version */
367 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100368 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
369 nc_err_set_msg(err, "The requested version is not supported.", "en");
370 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100371 }
372
373 /* check and get module with the name identifier */
374 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
375 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200376 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
377 }
378 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100379 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
380 nc_err_set_msg(err, "The requested schema was not found.", "en");
381 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100382 }
383
384 /* check format */
Radek Krejci89c34452016-12-07 15:59:45 +0100385 if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100386 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
Radek Krejci89c34452016-12-07 15:59:45 +0100387 } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100388 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
389 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100390 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
391 nc_err_set_msg(err, "The requested format is not supported.", "en");
392 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100393 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200394 if (!model_data) {
395 ERRINT;
396 return NULL;
397 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100398
Michal Vasko303245c2016-03-24 15:20:03 +0100399 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200400 if (!sdata) {
401 ERRINT;
402 free(model_data);
403 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100404 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200405
Radek Krejci539efb62016-08-24 15:05:16 +0200406 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
407 LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
Michal Vasko3cb0b132017-01-03 14:59:51 +0100408 if (!data || lyd_validate(&data, LYD_OPT_RPCREPLY, NULL)) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100409 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200410 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100411 return NULL;
412 }
413
Radek Krejci36dfdb32016-09-01 16:56:35 +0200414 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100415}
416
417static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100418nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100419{
Michal Vasko428087d2016-01-14 16:04:28 +0100420 session->term_reason = NC_SESSION_TERM_CLOSED;
421 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100422}
423
Michal Vasko086311b2016-01-08 09:53:11 +0100424API int
425nc_server_init(struct ly_ctx *ctx)
426{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100427 const struct lys_node *rpc;
428
Michal Vasko086311b2016-01-08 09:53:11 +0100429 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200430 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100431 return -1;
432 }
433
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100434 nc_init();
435
Michal Vasko05ba9df2016-01-13 14:40:27 +0100436 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100437 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100438 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100439 lys_set_private(rpc, nc_clb_default_get_schema);
440 }
441
442 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100443 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100444 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100445 lys_set_private(rpc, nc_clb_default_close_session);
446 }
447
Michal Vasko086311b2016-01-08 09:53:11 +0100448 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100449
450 server_opts.new_session_id = 1;
451 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
452
Michal Vasko086311b2016-01-08 09:53:11 +0100453 return 0;
454}
455
Michal Vaskob48aa812016-01-18 14:13:09 +0100456API void
457nc_server_destroy(void)
458{
Radek Krejci658782b2016-12-04 22:04:55 +0100459 unsigned int i;
460
461 for (i = 0; i < server_opts.capabilities_count; i++) {
462 lydict_remove(server_opts.ctx, server_opts.capabilities[i]);
463 }
464 free(server_opts.capabilities);
Michal Vaskob48aa812016-01-18 14:13:09 +0100465 pthread_spin_destroy(&server_opts.sid_lock);
466
Radek Krejci53691be2016-02-22 13:58:37 +0100467#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko59050372016-11-22 14:33:55 +0100468 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100469#endif
Michal Vasko17dfda92016-12-01 14:06:16 +0100470#ifdef NC_ENABLED_SSH
471 nc_server_ssh_del_authkey(NULL, NULL, 0, NULL);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100472
473 if (server_opts.hostkey_data && server_opts.hostkey_data_free) {
474 server_opts.hostkey_data_free(server_opts.hostkey_data);
475 }
476#endif
477#ifdef NC_ENABLED_TLS
478 if (server_opts.server_cert_data && server_opts.server_cert_data_free) {
479 server_opts.server_cert_data_free(server_opts.server_cert_data);
480 }
481 if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) {
482 server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data);
483 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100484#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100485 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100486}
487
Michal Vasko086311b2016-01-08 09:53:11 +0100488API int
489nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
490{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200491 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
492 ERRARG("basic_mode");
493 return -1;
494 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
495 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100496 return -1;
497 }
498
499 server_opts.wd_basic_mode = basic_mode;
500 server_opts.wd_also_supported = also_supported;
501 return 0;
502}
503
Michal Vasko1a38c862016-01-15 15:50:07 +0100504API void
Michal Vasko55f03972016-04-13 08:56:01 +0200505nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
506{
507 if (!basic_mode && !also_supported) {
508 ERRARG("basic_mode and also_supported");
509 return;
510 }
511
512 if (basic_mode) {
513 *basic_mode = server_opts.wd_basic_mode;
514 }
515 if (also_supported) {
516 *also_supported = server_opts.wd_also_supported;
517 }
518}
519
Michal Vasko55f03972016-04-13 08:56:01 +0200520API int
Radek Krejci658782b2016-12-04 22:04:55 +0100521nc_server_set_capability(const char *value)
Michal Vasko55f03972016-04-13 08:56:01 +0200522{
Radek Krejci658782b2016-12-04 22:04:55 +0100523 const char **new;
524
525 if (!value || !value[0]) {
526 ERRARG("value must not be empty");
527 return EXIT_FAILURE;
528 }
529
530 server_opts.capabilities_count++;
531 new = realloc(server_opts.capabilities, server_opts.capabilities_count * sizeof *server_opts.capabilities);
532 if (!new) {
533 ERRMEM;
534 return EXIT_FAILURE;
535 }
536 server_opts.capabilities = new;
537 server_opts.capabilities[server_opts.capabilities_count - 1] = lydict_insert(server_opts.ctx, value, 0);
538
539 return EXIT_SUCCESS;
Michal Vasko55f03972016-04-13 08:56:01 +0200540}
541
Michal Vasko1a38c862016-01-15 15:50:07 +0100542API void
Michal Vasko086311b2016-01-08 09:53:11 +0100543nc_server_set_hello_timeout(uint16_t hello_timeout)
544{
Michal Vasko086311b2016-01-08 09:53:11 +0100545 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100546}
547
Michal Vasko55f03972016-04-13 08:56:01 +0200548API uint16_t
549nc_server_get_hello_timeout(void)
550{
551 return server_opts.hello_timeout;
552}
553
Michal Vasko1a38c862016-01-15 15:50:07 +0100554API void
Michal Vasko086311b2016-01-08 09:53:11 +0100555nc_server_set_idle_timeout(uint16_t idle_timeout)
556{
Michal Vasko086311b2016-01-08 09:53:11 +0100557 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100558}
559
Michal Vasko55f03972016-04-13 08:56:01 +0200560API uint16_t
561nc_server_get_idle_timeout(void)
562{
563 return server_opts.idle_timeout;
564}
565
Michal Vasko71090fc2016-05-24 16:37:28 +0200566API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100567nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100568{
Michal Vasko71090fc2016-05-24 16:37:28 +0200569 NC_MSG_TYPE msgtype;
570
Michal Vasko45e53ae2016-04-07 11:46:03 +0200571 if (!server_opts.ctx) {
572 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200573 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200574 } else if (fdin < 0) {
575 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200576 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200577 } else if (fdout < 0) {
578 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200579 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200580 } else if (!username) {
581 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200582 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200583 } else if (!session) {
584 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200585 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100586 }
587
588 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100589 *session = nc_new_session(0);
Michal Vasko1a38c862016-01-15 15:50:07 +0100590 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100591 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200592 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100593 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100594 (*session)->status = NC_STATUS_STARTING;
595 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100596
Michal Vaskoade892d2017-02-22 13:40:35 +0100597 /* transport lock */
598 pthread_mutex_init((*session)->ti_lock, NULL);
599 pthread_cond_init((*session)->ti_cond, NULL);
600 *(*session)->ti_inuse = 0;
601
Michal Vasko086311b2016-01-08 09:53:11 +0100602 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100603 (*session)->ti_type = NC_TI_FD;
604 (*session)->ti.fd.in = fdin;
605 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100606
607 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100608 (*session)->flags = NC_SESSION_SHAREDCTX;
609 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100610
Michal Vaskob48aa812016-01-18 14:13:09 +0100611 /* assign new SID atomically */
612 pthread_spin_lock(&server_opts.sid_lock);
613 (*session)->id = server_opts.new_session_id++;
614 pthread_spin_unlock(&server_opts.sid_lock);
615
Michal Vasko086311b2016-01-08 09:53:11 +0100616 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200617 msgtype = nc_handshake(*session);
618 if (msgtype != NC_MSG_HELLO) {
619 nc_session_free(*session, NULL);
620 *session = NULL;
621 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100622 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200623 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100624 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100625
Michal Vasko71090fc2016-05-24 16:37:28 +0200626 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100627}
Michal Vasko9e036d52016-01-08 10:49:26 +0100628
Michal Vaskob30b99c2016-07-26 11:35:43 +0200629static void
630nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
631{
632 uint8_t i, found = 0;
633
634 for (i = 0; i < ps->queue_len; ++i) {
635 /* idx round buffer adjust */
636 if (ps->queue_begin + i == NC_PS_QUEUE_SIZE) {
637 i = -ps->queue_begin;
638 }
639
640 if (found) {
641 /* move the value back one place */
642 if (ps->queue[ps->queue_begin + i] == id) {
643 /* another equal value, simply cannot be */
644 ERRINT;
645 }
646
647 if (ps->queue_begin + i == 0) {
648 ps->queue[NC_PS_QUEUE_SIZE - 1] = ps->queue[ps->queue_begin + i];
649 } else {
650 ps->queue[ps->queue_begin + i - 1] = ps->queue[ps->queue_begin + i];
651 }
652 } else if (ps->queue[ps->queue_begin + i] == id) {
653 /* found our id, there can be no more equal valid values */
654 found = 1;
655 }
656 }
657
658 if (!found) {
659 ERRINT;
660 }
661 --ps->queue_len;
662}
663
Michal Vaskof04a52a2016-04-07 10:52:10 +0200664int
Michal Vasko26043172016-07-26 14:08:59 +0200665nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200666{
667 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200668 uint8_t queue_last;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200669 struct timespec ts;
670
Radek Krejci7ac16052016-07-15 11:48:18 +0200671 nc_gettimespec(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100672 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200673
674 /* LOCK */
675 ret = pthread_mutex_timedlock(&ps->lock, &ts);
676 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200677 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200678 return -1;
679 }
680
681 /* get a unique queue value (by adding 1 to the last added value, if any) */
682 if (ps->queue_len) {
683 queue_last = ps->queue_begin + ps->queue_len - 1;
684 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
685 queue_last -= NC_PS_QUEUE_SIZE;
686 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200687 *id = ps->queue[queue_last] + 1;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200688 } else {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200689 *id = 0;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200690 }
691
692 /* add ourselves into the queue */
693 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko26043172016-07-26 14:08:59 +0200694 ERR("%s: pollsession queue too small.", func);
Michal Vasko0ea456b2016-07-26 12:23:24 +0200695 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200696 return -1;
697 }
698 ++ps->queue_len;
699 queue_last = ps->queue_begin + ps->queue_len - 1;
700 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
701 queue_last -= NC_PS_QUEUE_SIZE;
702 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200703 ps->queue[queue_last] = *id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200704
705 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200706 while (ps->queue[ps->queue_begin] != *id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200707 nc_gettimespec(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100708 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200709
710 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
711 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200712 ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200713 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200714 nc_ps_queue_remove_id(ps, *id);
715 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200716 return -1;
717 }
718 }
719
Michal Vaskobe86fe32016-04-07 10:43:03 +0200720 /* UNLOCK */
721 pthread_mutex_unlock(&ps->lock);
722
723 return 0;
724}
725
Michal Vaskof04a52a2016-04-07 10:52:10 +0200726int
Michal Vasko26043172016-07-26 14:08:59 +0200727nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200728{
729 int ret;
730 struct timespec ts;
731
Radek Krejci7ac16052016-07-15 11:48:18 +0200732 nc_gettimespec(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100733 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200734
735 /* LOCK */
736 ret = pthread_mutex_timedlock(&ps->lock, &ts);
737 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200738 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200739 ret = -1;
740 }
741
Michal Vaskob30b99c2016-07-26 11:35:43 +0200742 /* we must be the first, it was our turn after all, right? */
743 if (ps->queue[ps->queue_begin] != id) {
744 ERRINT;
Michal Vaskob1a094b2016-10-05 14:04:52 +0200745 /* UNLOCK */
746 if (!ret) {
747 pthread_mutex_unlock(&ps->lock);
748 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200749 return -1;
750 }
751
Michal Vaskobe86fe32016-04-07 10:43:03 +0200752 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200753 nc_ps_queue_remove_id(ps, id);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200754
755 /* broadcast to all other threads that the queue moved */
756 pthread_cond_broadcast(&ps->cond);
757
Michal Vaskobe86fe32016-04-07 10:43:03 +0200758 /* UNLOCK */
759 if (!ret) {
760 pthread_mutex_unlock(&ps->lock);
761 }
762
763 return ret;
764}
765
Michal Vasko428087d2016-01-14 16:04:28 +0100766API struct nc_pollsession *
767nc_ps_new(void)
768{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100769 struct nc_pollsession *ps;
770
771 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100772 if (!ps) {
773 ERRMEM;
774 return NULL;
775 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200776 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100777 pthread_mutex_init(&ps->lock, NULL);
778
779 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100780}
781
782API void
783nc_ps_free(struct nc_pollsession *ps)
784{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100785 if (!ps) {
786 return;
787 }
788
Michal Vaskobe86fe32016-04-07 10:43:03 +0200789 if (ps->queue_len) {
790 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
791 }
792
Michal Vasko428087d2016-01-14 16:04:28 +0100793 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100794 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200795 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100796
Michal Vasko428087d2016-01-14 16:04:28 +0100797 free(ps);
798}
799
800API int
801nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
802{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200803 uint8_t q_id;
804
Michal Vasko45e53ae2016-04-07 11:46:03 +0200805 if (!ps) {
806 ERRARG("ps");
807 return -1;
808 } else if (!session) {
809 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100810 return -1;
811 }
812
Michal Vasko48a63ed2016-03-01 09:48:21 +0100813 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200814 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200815 return -1;
816 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100817
Michal Vasko428087d2016-01-14 16:04:28 +0100818 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100819 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
Michal Vasko99f251b2017-01-11 11:31:46 +0100820 if (!ps->sessions) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100821 ERRMEM;
822 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200823 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100824 return -1;
825 }
Michal Vasko3a715132016-01-21 15:40:31 +0100826 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100827
Michal Vasko48a63ed2016-03-01 09:48:21 +0100828 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200829 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100830}
831
Michal Vasko48a63ed2016-03-01 09:48:21 +0100832static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100833_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100834{
835 uint16_t i;
836
Radek Krejcid5f978f2016-03-03 13:14:45 +0100837 if (index >= 0) {
838 i = (uint16_t)index;
839 goto remove;
840 }
Michal Vasko428087d2016-01-14 16:04:28 +0100841 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100842 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100843remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100844 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100845 if (i < ps->session_count) {
846 ps->sessions[i] = ps->sessions[ps->session_count];
Michal Vasko58005732016-02-02 15:50:52 +0100847 } else if (!ps->session_count) {
848 free(ps->sessions);
849 ps->sessions = NULL;
Michal Vasko58005732016-02-02 15:50:52 +0100850 }
Michal Vasko11d2f6a2017-02-02 11:15:06 +0100851 ps->last_event_session = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100852 return 0;
853 }
854 }
855
Michal Vaskof0537d82016-01-29 14:42:38 +0100856 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100857}
858
Michal Vasko48a63ed2016-03-01 09:48:21 +0100859API int
860nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
861{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200862 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200863 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100864
Michal Vasko45e53ae2016-04-07 11:46:03 +0200865 if (!ps) {
866 ERRARG("ps");
867 return -1;
868 } else if (!session) {
869 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100870 return -1;
871 }
872
873 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200874 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200875 return -1;
876 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100877
Radek Krejcid5f978f2016-03-03 13:14:45 +0100878 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100879
880 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200881 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100882
Michal Vaskobe86fe32016-04-07 10:43:03 +0200883 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100884}
885
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100886API uint16_t
887nc_ps_session_count(struct nc_pollsession *ps)
888{
889 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200890 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100891 return 0;
892 }
893
Michal Vaskof4462fd2017-02-15 14:29:05 +0100894 return ps->session_count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100895}
896
Michal Vasko71090fc2016-05-24 16:37:28 +0200897/* must be called holding the session lock!
898 * returns: NC_PSPOLL_ERROR,
899 * NC_PSPOLL_BAD_RPC,
900 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
901 * NC_PSPOLL_RPC
902 */
903static int
Radek Krejci93e80222016-10-03 13:34:25 +0200904nc_server_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
Michal Vasko428087d2016-01-14 16:04:28 +0100905{
906 struct lyxml_elem *xml = NULL;
907 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200908 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200909 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100910
Michal Vasko45e53ae2016-04-07 11:46:03 +0200911 if (!session) {
912 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200913 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200914 } else if (!rpc) {
915 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200916 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100917 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100918 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200919 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100920 }
921
922 msgtype = nc_read_msg(session, &xml);
923
924 switch (msgtype) {
925 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200926 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100927 if (!*rpc) {
928 ERRMEM;
929 goto error;
930 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100931
Radek Krejcif93c7d42016-04-06 13:41:15 +0200932 ly_errno = LY_SUCCESS;
Michal Vasko41adf392017-01-17 10:39:04 +0100933 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child,
934 LYD_OPT_RPC | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS | LYD_OPT_STRICT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100935 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200936 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200937 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200938 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200939 nc_server_reply_free(reply);
940 if (ret == -1) {
941 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200942 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200943 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
944 } else {
945 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100946 }
Michal Vasko428087d2016-01-14 16:04:28 +0100947 (*rpc)->root = xml;
948 break;
949 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100950 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200951 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100952 goto error;
953 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100954 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200955 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100956 goto error;
957 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100958 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200959 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100960 goto error;
961 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200962 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100963 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
964 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200965 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100966 break;
967 }
968
Michal Vasko71090fc2016-05-24 16:37:28 +0200969 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100970
971error:
972 /* cleanup */
973 lyxml_free(server_opts.ctx, xml);
974
Michal Vasko71090fc2016-05-24 16:37:28 +0200975 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100976}
977
fanchanghu966f2de2016-07-21 02:28:57 -0400978API void
979nc_set_global_rpc_clb(nc_rpc_clb clb)
980{
981 global_rpc_clb = clb;
982}
983
Radek Krejci93e80222016-10-03 13:34:25 +0200984API NC_MSG_TYPE
985nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout)
986{
987 NC_MSG_TYPE result = NC_MSG_NOTIF;
988 int ret;
989
990 /* check parameters */
Michal Vasko3486a7c2017-03-03 13:28:07 +0100991 if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) {
Radek Krejci93e80222016-10-03 13:34:25 +0200992 ERRARG("session");
993 return NC_MSG_ERROR;
994 } else if (!notif || !notif->tree || !notif->eventtime) {
995 ERRARG("notif");
996 return NC_MSG_ERROR;
997 }
998
999 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vaskoade892d2017-02-22 13:40:35 +01001000 ret = nc_session_lock(session, timeout, __func__);
Radek Krejci93e80222016-10-03 13:34:25 +02001001 if (ret < 0) {
1002 return NC_MSG_ERROR;
1003 } else if (!ret) {
1004 return NC_MSG_WOULDBLOCK;
1005 }
1006
1007 ret = nc_write_msg(session, NC_MSG_NOTIF, notif);
1008 if (ret == -1) {
1009 ERR("Session %u: failed to write notification.", session->id);
1010 result = NC_MSG_ERROR;
1011 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001012
1013 nc_session_unlock(session, timeout, __func__);
Radek Krejci93e80222016-10-03 13:34:25 +02001014
1015 return result;
1016}
1017
Michal Vasko71090fc2016-05-24 16:37:28 +02001018/* must be called holding the session lock!
1019 * returns: NC_PSPOLL_ERROR,
1020 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
1021 * NC_PSPOLL_REPLY_ERROR,
1022 * 0
1023 */
1024static int
Radek Krejci93e80222016-10-03 13:34:25 +02001025nc_server_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001026{
1027 nc_rpc_clb clb;
1028 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +02001029 struct lys_node *rpc_act = NULL;
1030 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +02001031 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +01001032
Michal Vasko4a827e52016-03-03 10:59:00 +01001033 if (!rpc) {
1034 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001035 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +01001036 }
1037
Michal Vasko90e8e692016-07-13 12:27:57 +02001038 if (rpc->tree->schema->nodetype == LYS_RPC) {
1039 /* RPC */
1040 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -04001041 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001042 /* action */
1043 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
1044 if (elem->schema->nodetype == LYS_ACTION) {
1045 rpc_act = elem->schema;
1046 break;
1047 }
1048 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -04001049 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001050 if (!rpc_act) {
1051 ERRINT;
1052 return NC_PSPOLL_ERROR;
1053 }
1054 }
1055
1056 if (!rpc_act->priv) {
1057 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +01001058 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +01001059 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001060 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +01001061 reply = clb(rpc->tree, session);
1062 }
1063
1064 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001065 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +01001066 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001067 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
1068 if (reply->type == NC_RPL_ERROR) {
1069 ret |= NC_PSPOLL_REPLY_ERROR;
1070 }
1071 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +01001072
Michal Vasko71090fc2016-05-24 16:37:28 +02001073 if (r == -1) {
1074 ERR("Session %u: failed to write reply.", session->id);
1075 ret |= NC_PSPOLL_ERROR;
1076 }
Michal Vasko428087d2016-01-14 16:04:28 +01001077
1078 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1079 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1080 session->status = NC_STATUS_INVALID;
1081 }
1082
Michal Vasko71090fc2016-05-24 16:37:28 +02001083 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001084}
1085
1086API int
Michal Vasko71090fc2016-05-24 16:37:28 +02001087nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +01001088{
Michal Vasko99f251b2017-01-11 11:31:46 +01001089 int ret, r, poll_ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +02001090 uint8_t q_id;
Michal Vasko99f251b2017-01-11 11:31:46 +01001091 uint16_t i, j;
1092 char msg[256];
1093 NC_SESSION_TERM_REASON term_reason;
1094 struct pollfd pfd;
Michal Vasko36c7be82017-02-22 13:37:59 +01001095 struct timespec ts_timeout, ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001096 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +01001097 struct nc_server_rpc *rpc = NULL;
Michal Vasko99f251b2017-01-11 11:31:46 +01001098#ifdef NC_ENABLED_SSH
1099 struct nc_session *new;
1100#endif
Michal Vasko428087d2016-01-14 16:04:28 +01001101
Michal Vasko30a5d6b2017-02-15 14:29:39 +01001102 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001103 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001104 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001105 }
1106
Michal Vaskoade892d2017-02-22 13:40:35 +01001107 /* PS LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001108 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001109 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001110 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001111
Michal Vaskoade892d2017-02-22 13:40:35 +01001112 if (!ps->session_count) {
1113 ret = NC_PSPOLL_NOSESSIONS;
1114 goto ps_unlock_finish;
1115 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001116
Michal Vasko36c7be82017-02-22 13:37:59 +01001117 /* check timeout of all the sessions */
1118 nc_gettimespec(&ts_cur);
1119 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001120 if (!(ps->sessions[i]->flags & NC_SESSION_CALLHOME) && !ps->sessions[i]->opts.server.ntf_status
1121 && server_opts.idle_timeout
Michal Vasko36c7be82017-02-22 13:37:59 +01001122 && (ts_cur.tv_sec >= ps->sessions[i]->opts.server.last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001123 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1124 ps->sessions[i]->status = NC_STATUS_INVALID;
1125 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001126 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1127 if (session) {
1128 *session = ps->sessions[i];
1129 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001130 goto ps_unlock_finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001131 }
Michal Vasko428087d2016-01-14 16:04:28 +01001132 }
1133
Michal Vasko36c7be82017-02-22 13:37:59 +01001134 if (timeout > -1) {
1135 nc_gettimespec(&ts_timeout);
1136 nc_addtimespec(&ts_timeout, timeout);
1137 }
1138
1139 /* poll all the sessions one-by-one */
Michal Vasko99f251b2017-01-11 11:31:46 +01001140 do {
1141 /* loop from i to j */
1142 if (ps->last_event_session == ps->session_count - 1) {
1143 i = j = 0;
1144 } else {
1145 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001146 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001147 do {
1148 cur_session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +01001149
Michal Vaskoade892d2017-02-22 13:40:35 +01001150 /* SESSION LOCK */
1151 if ((cur_session->status == NC_STATUS_RUNNING)
1152 && !*cur_session->ti_inuse && ((r = nc_session_lock(cur_session, 0, __func__)))) {
1153 /* we go here if we successfully lock the session or there was an error, on timeout we simply skip it */
1154 if (r == -1) {
1155 ret = NC_PSPOLL_ERROR;
1156 goto ps_unlock_finish;
1157 }
1158 /* damn race condition */
1159 if (cur_session->status != NC_STATUS_RUNNING) {
1160 /* SESSION UNLOCK */
1161 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1162 goto next_iteration;
1163 }
1164
1165 switch (cur_session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +01001166#ifdef NC_ENABLED_SSH
Michal Vaskoade892d2017-02-22 13:40:35 +01001167 case NC_TI_LIBSSH:
1168 r = ssh_channel_poll_timeout(cur_session->ti.libssh.channel, 0, 0);
1169 if (r < 1) {
1170 if (r == SSH_EOF) {
1171 sprintf(msg, "SSH channel unexpected EOF");
1172 term_reason = NC_SESSION_TERM_DROPPED;
1173 poll_ret = -2;
1174 } else if (r == SSH_ERROR) {
1175 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(cur_session->ti.libssh.session));
1176 term_reason = NC_SESSION_TERM_OTHER;
1177 poll_ret = -2;
1178 } else {
1179 poll_ret = 0;
1180 }
1181 break;
1182 }
1183
1184 /* we have some data, but it may be just an SSH message */
1185 r = ssh_execute_message_callbacks(cur_session->ti.libssh.session);
1186 if (r != SSH_OK) {
1187 sprintf(msg, "failed to receive SSH messages (%s)", ssh_get_error(cur_session->ti.libssh.session));
Michal Vasko76be6752017-01-19 12:14:48 +01001188 term_reason = NC_SESSION_TERM_OTHER;
1189 poll_ret = -2;
Michal Vaskoade892d2017-02-22 13:40:35 +01001190 } else if (cur_session->flags & NC_SESSION_SSH_NEW_MSG) {
1191 /* new SSH message */
1192 cur_session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1193 if (cur_session->ti.libssh.next) {
1194 for (new = cur_session->ti.libssh.next; new != cur_session; new = new->ti.libssh.next) {
1195 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel
1196 && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1197 /* new NETCONF SSH channel */
1198 if (session) {
1199 *session = cur_session;
1200 }
1201 ret = NC_PSPOLL_SSH_CHANNEL;
1202 goto session_ps_unlock_finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001203 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001204 }
1205 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001206
Michal Vaskoade892d2017-02-22 13:40:35 +01001207 /* just some SSH message */
1208 if (session) {
1209 *session = cur_session;
1210 }
1211 ret = NC_PSPOLL_SSH_MSG;
1212 goto session_ps_unlock_finish;
1213 } else {
1214 /* we have some application data */
1215 poll_ret = 1;
Michal Vasko99f251b2017-01-11 11:31:46 +01001216 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001217 break;
Michal Vasko99f251b2017-01-11 11:31:46 +01001218#endif
1219#ifdef NC_ENABLED_TLS
Michal Vaskoade892d2017-02-22 13:40:35 +01001220 case NC_TI_OPENSSL:
1221 r = SSL_pending(cur_session->ti.tls);
1222 if (!r) {
1223 /* no data pending in the SSL buffer, poll fd */
1224 pfd.fd = SSL_get_rfd(cur_session->ti.tls);
1225 if (pfd.fd < 0) {
1226 ERRINT;
1227 ret = NC_PSPOLL_ERROR;
1228 goto session_ps_unlock_finish;
1229 }
1230 pfd.events = POLLIN;
1231 pfd.revents = 0;
1232 r = poll(&pfd, 1, 0);
1233
1234 if ((r < 0) && (errno != EINTR)) {
1235 sprintf(msg, "poll failed (%s)", strerror(errno));
1236 poll_ret = -1;
1237 } else if (r > 0) {
1238 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1239 sprintf(msg, "communication socket unexpectedly closed");
1240 term_reason = NC_SESSION_TERM_DROPPED;
1241 poll_ret = -2;
1242 } else if (pfd.revents & POLLERR) {
1243 sprintf(msg, "communication socket error");
1244 term_reason = NC_SESSION_TERM_OTHER;
1245 poll_ret = -2;
1246 } else {
1247 poll_ret = 1;
1248 }
1249 } else {
1250 poll_ret = 0;
1251 }
1252 } else {
1253 poll_ret = 1;
Michal Vasko99f251b2017-01-11 11:31:46 +01001254 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001255 break;
1256#endif
1257 case NC_TI_FD:
1258 pfd.fd = cur_session->ti.fd.in;
Michal Vasko99f251b2017-01-11 11:31:46 +01001259 pfd.events = POLLIN;
1260 pfd.revents = 0;
1261 r = poll(&pfd, 1, 0);
1262
Michal Vaskoade892d2017-02-22 13:40:35 +01001263 if ((r < 0) && (errno != EINTR)) {
Michal Vasko99f251b2017-01-11 11:31:46 +01001264 sprintf(msg, "poll failed (%s)", strerror(errno));
1265 poll_ret = -1;
1266 } else if (r > 0) {
1267 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1268 sprintf(msg, "communication socket unexpectedly closed");
1269 term_reason = NC_SESSION_TERM_DROPPED;
1270 poll_ret = -2;
1271 } else if (pfd.revents & POLLERR) {
1272 sprintf(msg, "communication socket error");
1273 term_reason = NC_SESSION_TERM_OTHER;
1274 poll_ret = -2;
1275 } else {
1276 poll_ret = 1;
1277 }
1278 } else {
1279 poll_ret = 0;
1280 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001281 break;
1282 case NC_TI_NONE:
1283 ERRINT;
1284 ret = NC_PSPOLL_ERROR;
1285 goto session_ps_unlock_finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001286 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001287
Michal Vaskoade892d2017-02-22 13:40:35 +01001288 /* here: poll_ret == -2 - session error, session terminated,
1289 * poll_ret == -1 - generic error,
1290 * poll_ret == 0 - nothing to read,
1291 * poll_ret > 0 - data available
1292 */
1293 if (poll_ret == -2) {
1294 ERR("Session %u: %s.", cur_session->id, msg);
1295 cur_session->status = NC_STATUS_INVALID;
1296 cur_session->term_reason = term_reason;
1297 if (session) {
1298 *session = cur_session;
Michal Vasko99f251b2017-01-11 11:31:46 +01001299 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001300 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1301 goto session_ps_unlock_finish;
1302 } else if (poll_ret == -1) {
1303 ERR("Session %u: %s.", cur_session->id, msg);
1304 ret = NC_PSPOLL_ERROR;
1305 goto session_ps_unlock_finish;
1306 } else if (poll_ret > 0) {
1307 break;
Michal Vasko99f251b2017-01-11 11:31:46 +01001308 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001309
1310 /* SESSION UNLOCK */
1311 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1312 } else {
1313 /* timeout */
1314 poll_ret = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001315 }
Michal Vasko428087d2016-01-14 16:04:28 +01001316
Michal Vaskoade892d2017-02-22 13:40:35 +01001317next_iteration:
Michal Vasko99f251b2017-01-11 11:31:46 +01001318 if (i == ps->session_count - 1) {
1319 i = 0;
1320 } else {
1321 ++i;
1322 }
1323 } while (i != j);
1324
Michal Vaskoade892d2017-02-22 13:40:35 +01001325 /* no event, no session remains locked */
Michal Vasko99f251b2017-01-11 11:31:46 +01001326 if (!poll_ret && (timeout > -1)) {
1327 usleep(NC_TIMEOUT_STEP);
1328
Michal Vaskoade892d2017-02-22 13:40:35 +01001329 nc_gettimespec(&ts_cur);
Michal Vasko99f251b2017-01-11 11:31:46 +01001330 /* final timeout */
Michal Vaskoade892d2017-02-22 13:40:35 +01001331 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko99f251b2017-01-11 11:31:46 +01001332 ret = NC_PSPOLL_TIMEOUT;
Michal Vaskoade892d2017-02-22 13:40:35 +01001333 goto ps_unlock_finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001334 }
Michal Vasko428087d2016-01-14 16:04:28 +01001335 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001336 } while (!poll_ret);
Michal Vasko428087d2016-01-14 16:04:28 +01001337
Michal Vaskoade892d2017-02-22 13:40:35 +01001338 /* this is the session with some data available for reading, it is still locked */
Michal Vasko71090fc2016-05-24 16:37:28 +02001339 if (session) {
1340 *session = cur_session;
1341 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001342 ps->last_event_session = i;
Michal Vasko428087d2016-01-14 16:04:28 +01001343
Michal Vaskoade892d2017-02-22 13:40:35 +01001344 /* PS UNLOCK */
1345 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001346
Radek Krejci93e80222016-10-03 13:34:25 +02001347 ret = nc_server_recv_rpc(cur_session, &rpc);
Michal Vasko71090fc2016-05-24 16:37:28 +02001348 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001349 if (cur_session->status != NC_STATUS_RUNNING) {
1350 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001351 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001352 goto session_unlock_finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001353 }
1354
Michal Vasko2e6defd2016-10-07 15:48:15 +02001355 cur_session->opts.server.last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001356
Michal Vaskoade892d2017-02-22 13:40:35 +01001357 /* process RPC, not needed afterwards */
Radek Krejci93e80222016-10-03 13:34:25 +02001358 ret |= nc_server_send_reply(cur_session, rpc);
Michal Vaskoade892d2017-02-22 13:40:35 +01001359 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vasko428087d2016-01-14 16:04:28 +01001360
Michal Vasko71090fc2016-05-24 16:37:28 +02001361 if (cur_session->status != NC_STATUS_RUNNING) {
1362 ret |= NC_PSPOLL_SESSION_TERM;
1363 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1364 ret |= NC_PSPOLL_SESSION_ERROR;
1365 }
Michal Vasko428087d2016-01-14 16:04:28 +01001366 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001367
Michal Vaskoade892d2017-02-22 13:40:35 +01001368session_unlock_finish:
1369 /* SESSION UNLOCK */
1370 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1371 return ret;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001372
Michal Vaskoade892d2017-02-22 13:40:35 +01001373session_ps_unlock_finish:
1374 /* SESSION UNLOCK */
1375 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1376
1377ps_unlock_finish:
1378 /* PS UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001379 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001380 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001381}
1382
Michal Vaskod09eae62016-02-01 10:32:52 +01001383API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001384nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001385{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001386 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001387 uint16_t i;
1388 struct nc_session *session;
1389
Michal Vasko9a25e932016-02-01 10:36:42 +01001390 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001391 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001392 return;
1393 }
1394
Michal Vasko48a63ed2016-03-01 09:48:21 +01001395 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001396 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001397 return;
1398 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001399
Michal Vasko48a63ed2016-03-01 09:48:21 +01001400 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001401 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001402 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001403 }
1404 free(ps->sessions);
1405 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001406 ps->session_count = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +01001407 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001408 } else {
1409 for (i = 0; i < ps->session_count; ) {
1410 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1411 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001412 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001413 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001414 continue;
1415 }
1416
1417 ++i;
1418 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001419 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001420
1421 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001422 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001423}
1424
Radek Krejci53691be2016-02-22 13:58:37 +01001425#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001426
Michal Vaskoe2713da2016-08-22 16:06:40 +02001427API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001428nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001429{
Michal Vasko3031aae2016-01-27 16:07:18 +01001430 uint16_t i;
Michal Vaskoade892d2017-02-22 13:40:35 +01001431 int ret = 0;
Michal Vasko9e036d52016-01-08 10:49:26 +01001432
Michal Vasko45e53ae2016-04-07 11:46:03 +02001433 if (!name) {
1434 ERRARG("name");
1435 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001436 }
1437
Michal Vaskoade892d2017-02-22 13:40:35 +01001438 /* BIND LOCK */
1439 pthread_mutex_lock(&server_opts.bind_lock);
1440
1441 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001442 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001443
1444 /* check name uniqueness */
1445 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001446 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001447 ERR("Endpoint \"%s\" already exists.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +01001448 ret = -1;
1449 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001450 }
1451 }
1452
Michal Vasko3031aae2016-01-27 16:07:18 +01001453 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001454 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001455 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001456 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001457 ret = -1;
1458 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001459 }
1460 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001461 server_opts.endpts[server_opts.endpt_count - 1].ti = ti;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001462
Michal Vaskoe2713da2016-08-22 16:06:40 +02001463 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001464 if (!server_opts.binds) {
1465 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001466 ret = -1;
1467 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001468 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001469
Michal Vasko2e6defd2016-10-07 15:48:15 +02001470 server_opts.binds[server_opts.endpt_count - 1].address = NULL;
1471 server_opts.binds[server_opts.endpt_count - 1].port = 0;
1472 server_opts.binds[server_opts.endpt_count - 1].sock = -1;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001473 server_opts.binds[server_opts.endpt_count - 1].pollin = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001474
1475 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001476#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001477 case NC_TI_LIBSSH:
1478 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1479 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) {
1480 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001481 ret = -1;
1482 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001483 }
1484 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods =
1485 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1486 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3;
1487 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10;
1488 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001489#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001490#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001491 case NC_TI_OPENSSL:
1492 server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1493 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) {
1494 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001495 ret = -1;
1496 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001497 }
1498 break;
1499#endif
1500 default:
1501 ERRINT;
Michal Vaskoade892d2017-02-22 13:40:35 +01001502 ret = -1;
1503 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001504 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001505
Michal Vaskoade892d2017-02-22 13:40:35 +01001506cleanup:
1507 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001508 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001509
Michal Vaskoade892d2017-02-22 13:40:35 +01001510 /* BIND UNLOCK */
1511 pthread_mutex_unlock(&server_opts.bind_lock);
1512
1513 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001514}
1515
Michal Vasko3031aae2016-01-27 16:07:18 +01001516int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001517nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port)
Michal Vaskoda514772016-02-01 11:32:01 +01001518{
1519 struct nc_endpt *endpt;
1520 struct nc_bind *bind = NULL;
1521 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001522 int sock = -1, set_addr, ret = 0;
Michal Vaskoda514772016-02-01 11:32:01 +01001523
Michal Vasko45e53ae2016-04-07 11:46:03 +02001524 if (!endpt_name) {
1525 ERRARG("endpt_name");
1526 return -1;
1527 } else if ((!address && !port) || (address && port)) {
1528 ERRARG("address and port");
1529 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001530 }
1531
Michal Vaskoe2713da2016-08-22 16:06:40 +02001532 if (address) {
1533 set_addr = 1;
1534 } else {
1535 set_addr = 0;
1536 }
1537
Michal Vaskoade892d2017-02-22 13:40:35 +01001538 /* BIND LOCK */
1539 pthread_mutex_lock(&server_opts.bind_lock);
1540
1541 /* ENDPT LOCK */
1542 endpt = nc_server_endpt_lock_get(endpt_name, 0, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001543 if (!endpt) {
1544 return -1;
1545 }
1546
Michal Vaskoe2713da2016-08-22 16:06:40 +02001547 bind = &server_opts.binds[i];
Michal Vaskoda514772016-02-01 11:32:01 +01001548
Michal Vaskoe2713da2016-08-22 16:06:40 +02001549 if (set_addr) {
1550 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001551 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001552 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001553 }
1554
Michal Vaskoe2713da2016-08-22 16:06:40 +02001555 /* we have all the information we need to create a listening socket */
1556 if (address && port) {
1557 /* create new socket, close the old one */
1558 sock = nc_sock_listen(address, port);
1559 if (sock == -1) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001560 ret = -1;
1561 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001562 }
1563
1564 if (bind->sock > -1) {
1565 close(bind->sock);
1566 }
1567 bind->sock = sock;
1568 } /* else we are just setting address or port */
1569
1570 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001571 lydict_remove(server_opts.ctx, bind->address);
1572 bind->address = lydict_insert(server_opts.ctx, address, 0);
1573 } else {
1574 bind->port = port;
1575 }
1576
Michal Vaskoe2713da2016-08-22 16:06:40 +02001577 if (sock > -1) {
1578#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001579 VRB("Listening on %s:%u for %s connections.", address, port, (endpt->ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001580#elif defined(NC_ENABLED_SSH)
1581 VRB("Listening on %s:%u for SSH connections.", address, port);
1582#else
1583 VRB("Listening on %s:%u for TLS connections.", address, port);
1584#endif
1585 }
1586
Michal Vasko4c1fb492017-01-30 14:31:07 +01001587cleanup:
Michal Vaskoade892d2017-02-22 13:40:35 +01001588 /* ENDPT UNLOCK */
1589 pthread_rwlock_unlock(&server_opts.endpt_lock);
1590
1591 /* BIND UNLOCK */
1592 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001593
Michal Vasko4c1fb492017-01-30 14:31:07 +01001594 return ret;
Michal Vaskoda514772016-02-01 11:32:01 +01001595}
1596
Michal Vaskoe2713da2016-08-22 16:06:40 +02001597API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001598nc_server_endpt_set_address(const char *endpt_name, const char *address)
1599{
1600 return nc_server_endpt_set_address_port(endpt_name, address, 0);
1601}
1602
1603API int
1604nc_server_endpt_set_port(const char *endpt_name, uint16_t port)
1605{
1606 return nc_server_endpt_set_address_port(endpt_name, NULL, port);
1607}
1608
1609API int
Michal Vasko59050372016-11-22 14:33:55 +01001610nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001611{
1612 uint32_t i;
1613 int ret = -1;
1614
Michal Vaskoade892d2017-02-22 13:40:35 +01001615 /* BIND LOCK */
1616 pthread_mutex_lock(&server_opts.bind_lock);
1617
1618 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001619 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001620
Michal Vasko59050372016-11-22 14:33:55 +01001621 if (!name && !ti) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001622 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001623 for (i = 0; i < server_opts.endpt_count; ++i) {
1624 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001625 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001626#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001627 case NC_TI_LIBSSH:
1628 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1629 free(server_opts.endpts[i].opts.ssh);
1630 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001631#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001632#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001633 case NC_TI_OPENSSL:
1634 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1635 free(server_opts.endpts[i].opts.tls);
1636 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001637#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001638 default:
1639 ERRINT;
1640 /* won't get here ...*/
1641 break;
1642 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001643 ret = 0;
1644 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001645 free(server_opts.endpts);
1646 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001647
1648 /* remove all binds */
1649 for (i = 0; i < server_opts.endpt_count; ++i) {
1650 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1651 if (server_opts.binds[i].sock > -1) {
1652 close(server_opts.binds[i].sock);
1653 }
1654 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001655 free(server_opts.binds);
1656 server_opts.binds = NULL;
1657
Michal Vasko3031aae2016-01-27 16:07:18 +01001658 server_opts.endpt_count = 0;
1659
Michal Vasko1a38c862016-01-15 15:50:07 +01001660 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001661 /* remove one endpoint with bind(s) or all endpoints using one transport protocol */
Michal Vasko3031aae2016-01-27 16:07:18 +01001662 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001663 if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001664 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001665 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001666 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001667#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001668 case NC_TI_LIBSSH:
1669 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1670 free(server_opts.endpts[i].opts.ssh);
1671 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001672#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001673#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001674 case NC_TI_OPENSSL:
1675 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1676 free(server_opts.endpts[i].opts.tls);
1677 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001678#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001679 default:
1680 ERRINT;
1681 break;
1682 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001683
Michal Vaskoe2713da2016-08-22 16:06:40 +02001684 /* remove bind(s) */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001685 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1686 if (server_opts.binds[i].sock > -1) {
1687 close(server_opts.binds[i].sock);
1688 }
1689
1690 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001691 --server_opts.endpt_count;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001692 if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001693 free(server_opts.binds);
1694 server_opts.binds = NULL;
1695 free(server_opts.endpts);
1696 server_opts.endpts = NULL;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001697 } else if (i < server_opts.endpt_count) {
1698 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1699 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoc0256492016-02-02 12:19:06 +01001700 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001701
1702 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01001703 if (name) {
1704 break;
1705 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001706 }
1707 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001708 }
1709
Michal Vaskoade892d2017-02-22 13:40:35 +01001710 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001711 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001712
Michal Vaskoade892d2017-02-22 13:40:35 +01001713 /* BIND UNLOCK */
1714 pthread_mutex_unlock(&server_opts.bind_lock);
1715
Michal Vasko9e036d52016-01-08 10:49:26 +01001716 return ret;
1717}
1718
Michal Vasko71090fc2016-05-24 16:37:28 +02001719API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001720nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001721{
Michal Vasko71090fc2016-05-24 16:37:28 +02001722 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001723 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001724 char *host = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001725 uint16_t port, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001726
Michal Vasko45e53ae2016-04-07 11:46:03 +02001727 if (!server_opts.ctx) {
1728 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001729 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001730 } else if (!session) {
1731 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001732 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001733 }
1734
Michal Vaskoade892d2017-02-22 13:40:35 +01001735 /* BIND LOCK */
1736 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001737
1738 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001739 ERR("No endpoints to accept sessions on.");
Michal Vaskoade892d2017-02-22 13:40:35 +01001740 /* BIND UNLOCK */
1741 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001742 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001743 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001744
Michal Vaskoe2713da2016-08-22 16:06:40 +02001745 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001746 if (ret < 1) {
Michal Vaskoade892d2017-02-22 13:40:35 +01001747 /* BIND UNLOCK */
1748 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001749 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001750 if (!ret) {
1751 return NC_MSG_WOULDBLOCK;
1752 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001753 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001754 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001755
1756 /* switch bind_lock for endpt_lock, so that another thread can accept another session */
1757 /* ENDPT READ LOCK */
1758 pthread_rwlock_rdlock(&server_opts.endpt_lock);
1759
1760 /* BIND UNLOCK */
1761 pthread_mutex_unlock(&server_opts.bind_lock);
1762
Michal Vaskob48aa812016-01-18 14:13:09 +01001763 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001764
Michal Vaskoade892d2017-02-22 13:40:35 +01001765 *session = nc_new_session(0);
Michal Vasko686aa312016-01-21 15:58:18 +01001766 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001767 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001768 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001769 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001770 msgtype = NC_MSG_ERROR;
1771 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001772 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001773 (*session)->status = NC_STATUS_STARTING;
1774 (*session)->side = NC_SERVER;
1775 (*session)->ctx = server_opts.ctx;
1776 (*session)->flags = NC_SESSION_SHAREDCTX;
1777 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1778 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001779
1780 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001781 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +01001782 pthread_cond_init((*session)->ti_cond, NULL);
1783 *(*session)->ti_inuse = 0;
Michal Vasko9e036d52016-01-08 10:49:26 +01001784
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001785 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001786#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001787 if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) {
1788 (*session)->data = server_opts.endpts[bind_idx].opts.ssh;
Michal Vaskob70c8b82017-03-17 09:09:29 +01001789 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02001790 if (ret < 0) {
1791 msgtype = NC_MSG_ERROR;
1792 goto cleanup;
1793 } else if (!ret) {
1794 msgtype = NC_MSG_WOULDBLOCK;
1795 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001796 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001797 } else
1798#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001799#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001800 if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) {
1801 (*session)->data = server_opts.endpts[bind_idx].opts.tls;
Michal Vaskob70c8b82017-03-17 09:09:29 +01001802 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02001803 if (ret < 0) {
1804 msgtype = NC_MSG_ERROR;
1805 goto cleanup;
1806 } else if (!ret) {
1807 msgtype = NC_MSG_WOULDBLOCK;
1808 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001809 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001810 } else
1811#endif
1812 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001813 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001814 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001815 msgtype = NC_MSG_ERROR;
1816 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001817 }
1818
Michal Vasko2cc4c682016-03-01 09:16:48 +01001819 (*session)->data = NULL;
1820
Michal Vaskoade892d2017-02-22 13:40:35 +01001821 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001822 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001823
Michal Vaskob48aa812016-01-18 14:13:09 +01001824 /* assign new SID atomically */
1825 /* LOCK */
1826 pthread_spin_lock(&server_opts.sid_lock);
1827 (*session)->id = server_opts.new_session_id++;
1828 /* UNLOCK */
1829 pthread_spin_unlock(&server_opts.sid_lock);
1830
Michal Vasko9e036d52016-01-08 10:49:26 +01001831 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001832 msgtype = nc_handshake(*session);
1833 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001834 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001835 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001836 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001837 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001838 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001839 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001840
Michal Vasko71090fc2016-05-24 16:37:28 +02001841 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001842
Michal Vasko71090fc2016-05-24 16:37:28 +02001843cleanup:
Michal Vaskoade892d2017-02-22 13:40:35 +01001844 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001845 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001846
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001847 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001848 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001849 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001850}
1851
Michal Vasko2e6defd2016-10-07 15:48:15 +02001852API int
1853nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti)
1854{
1855 uint16_t i;
1856
1857 if (!name) {
1858 ERRARG("name");
1859 return -1;
1860 } else if (!ti) {
1861 ERRARG("ti");
1862 return -1;
1863 }
1864
1865 /* WRITE LOCK */
1866 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1867
1868 /* check name uniqueness */
1869 for (i = 0; i < server_opts.ch_client_count; ++i) {
1870 if (!strcmp(server_opts.ch_clients[i].name, name)) {
1871 ERR("Call Home client \"%s\" already exists.", name);
1872 /* WRITE UNLOCK */
1873 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1874 return -1;
1875 }
1876 }
1877
1878 ++server_opts.ch_client_count;
1879 server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients);
1880 if (!server_opts.ch_clients) {
1881 ERRMEM;
1882 /* WRITE UNLOCK */
1883 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1884 return -1;
1885 }
1886 server_opts.ch_clients[server_opts.ch_client_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1887 server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001888 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL;
1889 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpt_count = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001890
1891 switch (ti) {
1892#ifdef NC_ENABLED_SSH
1893 case NC_TI_LIBSSH:
1894 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1895 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) {
1896 ERRMEM;
1897 /* WRITE UNLOCK */
1898 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1899 return -1;
1900 }
1901 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods =
1902 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1903 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3;
1904 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10;
1905 break;
1906#endif
1907#ifdef NC_ENABLED_TLS
1908 case NC_TI_OPENSSL:
1909 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1910 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) {
1911 ERRMEM;
1912 /* WRITE UNLOCK */
1913 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1914 return -1;
1915 }
1916 break;
1917#endif
1918 default:
1919 ERRINT;
1920 /* WRITE UNLOCK */
1921 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1922 return -1;
1923 }
1924
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001925 server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0;
1926
Michal Vasko2e6defd2016-10-07 15:48:15 +02001927 /* set CH default options */
1928 server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED;
1929 server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3;
1930
1931 pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL);
1932
1933 /* WRITE UNLOCK */
1934 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1935
1936 return 0;
1937}
1938
1939API int
Michal Vasko59050372016-11-22 14:33:55 +01001940nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001941{
1942 uint16_t i, j;
1943 int ret = -1;
1944
1945 /* WRITE LOCK */
1946 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1947
Michal Vasko59050372016-11-22 14:33:55 +01001948 if (!name && !ti) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001949 /* remove all CH clients */
1950 for (i = 0; i < server_opts.ch_client_count; ++i) {
1951 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1952
1953 /* remove all endpoints */
1954 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
1955 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
1956 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
1957 }
1958 free(server_opts.ch_clients[i].ch_endpts);
1959
1960 switch (server_opts.ch_clients[i].ti) {
1961#ifdef NC_ENABLED_SSH
1962 case NC_TI_LIBSSH:
1963 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1964 free(server_opts.ch_clients[i].opts.ssh);
1965 break;
1966#endif
1967#ifdef NC_ENABLED_TLS
1968 case NC_TI_OPENSSL:
1969 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
1970 free(server_opts.ch_clients[i].opts.tls);
1971 break;
1972#endif
1973 default:
1974 ERRINT;
1975 /* won't get here ...*/
1976 break;
1977 }
1978
1979 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
1980
1981 ret = 0;
1982 }
1983 free(server_opts.ch_clients);
1984 server_opts.ch_clients = NULL;
1985
1986 server_opts.ch_client_count = 0;
1987
1988 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001989 /* remove one client with endpoint(s) or all clients using one protocol */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001990 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001991 if ((name && !strcmp(server_opts.ch_clients[i].name, name)) || (!name && (server_opts.ch_clients[i].ti == ti))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001992 /* remove endpt */
1993 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1994
1995 switch (server_opts.ch_clients[i].ti) {
1996#ifdef NC_ENABLED_SSH
1997 case NC_TI_LIBSSH:
1998 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1999 free(server_opts.ch_clients[i].opts.ssh);
2000 break;
2001#endif
2002#ifdef NC_ENABLED_TLS
2003 case NC_TI_OPENSSL:
2004 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
2005 free(server_opts.ch_clients[i].opts.tls);
2006 break;
2007#endif
2008 default:
2009 ERRINT;
2010 break;
2011 }
2012
2013 /* remove all endpoints */
2014 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
2015 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
2016 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
2017 }
2018 free(server_opts.ch_clients[i].ch_endpts);
2019
2020 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
2021
2022 /* move last client and endpoint(s) to the empty space */
2023 --server_opts.ch_client_count;
2024 if (i < server_opts.ch_client_count) {
2025 memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count],
2026 sizeof *server_opts.ch_clients);
2027 } else if (!server_opts.ch_client_count) {
2028 free(server_opts.ch_clients);
2029 server_opts.ch_clients = NULL;
2030 }
2031
2032 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01002033 if (name) {
2034 break;
2035 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002036 }
2037 }
2038 }
2039
2040 /* WRITE UNLOCK */
2041 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2042
2043 return ret;
2044}
2045
2046API int
2047nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name)
2048{
2049 uint16_t i;
2050 struct nc_ch_client *client;
2051
2052 if (!client_name) {
2053 ERRARG("client_name");
2054 return -1;
2055 } else if (!endpt_name) {
2056 ERRARG("endpt_name");
2057 return -1;
2058 }
2059
2060 /* LOCK */
2061 client = nc_server_ch_client_lock(client_name, 0, NULL);
2062 if (!client) {
2063 return -1;
2064 }
2065
2066 for (i = 0; i < client->ch_endpt_count; ++i) {
2067 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2068 ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name);
2069 /* UNLOCK */
2070 nc_server_ch_client_unlock(client);
2071 return -1;
2072 }
2073 }
2074
2075 ++client->ch_endpt_count;
2076 client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts);
2077 if (!client->ch_endpts) {
2078 ERRMEM;
2079 /* UNLOCK */
2080 nc_server_ch_client_unlock(client);
2081 return -1;
2082 }
2083
2084 client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0);
2085 client->ch_endpts[client->ch_endpt_count - 1].address = NULL;
2086 client->ch_endpts[client->ch_endpt_count - 1].port = 0;
2087
2088 /* UNLOCK */
2089 nc_server_ch_client_unlock(client);
2090
2091 return 0;
2092}
2093
2094API int
2095nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name)
2096{
2097 uint16_t i;
2098 int ret = -1;
2099 struct nc_ch_client *client;
2100
2101 if (!client_name) {
2102 ERRARG("client_name");
2103 return -1;
2104 }
2105
2106 /* LOCK */
2107 client = nc_server_ch_client_lock(client_name, 0, NULL);
2108 if (!client) {
2109 return -1;
2110 }
2111
2112 if (!endpt_name) {
2113 /* remove all endpoints */
2114 for (i = 0; i < client->ch_endpt_count; ++i) {
2115 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2116 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2117 }
2118 free(client->ch_endpts);
2119 client->ch_endpts = NULL;
2120 client->ch_endpt_count = 0;
2121
2122 ret = 0;
2123 } else {
2124 for (i = 0; i < client->ch_endpt_count; ++i) {
2125 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2126 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2127 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002128
Michal Vasko4f921012016-10-20 14:07:45 +02002129 /* move last endpoint to the empty space */
2130 --client->ch_endpt_count;
2131 if (i < client->ch_endpt_count) {
2132 memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts);
2133 } else if (!server_opts.ch_client_count) {
2134 free(server_opts.ch_clients);
2135 server_opts.ch_clients = NULL;
2136 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002137
Michal Vasko4f921012016-10-20 14:07:45 +02002138 ret = 0;
2139 break;
2140 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002141 }
2142 }
2143
2144 /* UNLOCK */
2145 nc_server_ch_client_unlock(client);
2146
2147 return ret;
2148}
2149
2150API int
2151nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address)
2152{
2153 uint16_t i;
2154 int ret = -1;
2155 struct nc_ch_client *client;
2156
2157 if (!client_name) {
2158 ERRARG("client_name");
2159 return -1;
2160 } else if (!endpt_name) {
2161 ERRARG("endpt_name");
2162 return -1;
2163 } else if (!address) {
2164 ERRARG("address");
2165 return -1;
2166 }
2167
2168 /* LOCK */
2169 client = nc_server_ch_client_lock(client_name, 0, NULL);
2170 if (!client) {
2171 return -1;
2172 }
2173
2174 for (i = 0; i < client->ch_endpt_count; ++i) {
2175 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2176 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2177 client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0);
2178
2179 ret = 0;
2180 break;
2181 }
2182 }
2183
2184 /* UNLOCK */
2185 nc_server_ch_client_unlock(client);
2186
2187 if (ret == -1) {
2188 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2189 }
2190
2191 return ret;
2192}
2193
2194API int
2195nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port)
2196{
2197 uint16_t i;
2198 int ret = -1;
2199 struct nc_ch_client *client;
2200
2201 if (!client_name) {
2202 ERRARG("client_name");
2203 return -1;
2204 } else if (!endpt_name) {
2205 ERRARG("endpt_name");
2206 return -1;
2207 } else if (!port) {
2208 ERRARG("port");
2209 return -1;
2210 }
2211
2212 /* LOCK */
2213 client = nc_server_ch_client_lock(client_name, 0, NULL);
2214 if (!client) {
2215 return -1;
2216 }
2217
2218 for (i = 0; i < client->ch_endpt_count; ++i) {
2219 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2220 client->ch_endpts[i].port = port;
2221
2222 ret = 0;
2223 break;
2224 }
2225 }
2226
2227 /* UNLOCK */
2228 nc_server_ch_client_unlock(client);
2229
2230 if (ret == -1) {
2231 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2232 }
2233
2234 return ret;
2235}
2236
2237API int
2238nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type)
2239{
2240 struct nc_ch_client *client;
2241
2242 if (!client_name) {
2243 ERRARG("client_name");
2244 return -1;
2245 } else if (!conn_type) {
2246 ERRARG("conn_type");
2247 return -1;
2248 }
2249
2250 /* LOCK */
2251 client = nc_server_ch_client_lock(client_name, 0, NULL);
2252 if (!client) {
2253 return -1;
2254 }
2255
2256 if (client->conn_type != conn_type) {
2257 client->conn_type = conn_type;
2258
2259 /* set default options */
2260 switch (conn_type) {
2261 case NC_CH_PERSIST:
2262 client->conn.persist.idle_timeout = 86400;
2263 client->conn.persist.ka_max_wait = 30;
2264 client->conn.persist.ka_max_attempts = 3;
2265 break;
2266 case NC_CH_PERIOD:
2267 client->conn.period.idle_timeout = 300;
2268 client->conn.period.reconnect_timeout = 60;
2269 break;
2270 default:
2271 ERRINT;
2272 break;
2273 }
2274 }
2275
2276 /* UNLOCK */
2277 nc_server_ch_client_unlock(client);
2278
2279 return 0;
2280}
2281
2282API int
2283nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout)
2284{
2285 struct nc_ch_client *client;
2286
2287 if (!client_name) {
2288 ERRARG("client_name");
2289 return -1;
2290 }
2291
2292 /* LOCK */
2293 client = nc_server_ch_client_lock(client_name, 0, NULL);
2294 if (!client) {
2295 return -1;
2296 }
2297
2298 if (client->conn_type != NC_CH_PERSIST) {
2299 ERR("Call Home client \"%s\" is not of persistent connection type.");
2300 /* UNLOCK */
2301 nc_server_ch_client_unlock(client);
2302 return -1;
2303 }
2304
2305 client->conn.persist.idle_timeout = idle_timeout;
2306
2307 /* UNLOCK */
2308 nc_server_ch_client_unlock(client);
2309
2310 return 0;
2311}
2312
2313API int
2314nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait)
2315{
2316 struct nc_ch_client *client;
2317
2318 if (!client_name) {
2319 ERRARG("client_name");
2320 return -1;
2321 } else if (!max_wait) {
2322 ERRARG("max_wait");
2323 return -1;
2324 }
2325
2326 /* LOCK */
2327 client = nc_server_ch_client_lock(client_name, 0, NULL);
2328 if (!client) {
2329 return -1;
2330 }
2331
2332 if (client->conn_type != NC_CH_PERSIST) {
2333 ERR("Call Home client \"%s\" is not of persistent connection type.");
2334 /* UNLOCK */
2335 nc_server_ch_client_unlock(client);
2336 return -1;
2337 }
2338
2339 client->conn.persist.ka_max_wait = max_wait;
2340
2341 /* UNLOCK */
2342 nc_server_ch_client_unlock(client);
2343
2344 return 0;
2345}
2346
2347API int
2348nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts)
2349{
2350 struct nc_ch_client *client;
2351
2352 if (!client_name) {
2353 ERRARG("client_name");
2354 return -1;
2355 }
2356
2357 /* LOCK */
2358 client = nc_server_ch_client_lock(client_name, 0, NULL);
2359 if (!client) {
2360 return -1;
2361 }
2362
2363 if (client->conn_type != NC_CH_PERSIST) {
2364 ERR("Call Home client \"%s\" is not of persistent connection type.");
2365 /* UNLOCK */
2366 nc_server_ch_client_unlock(client);
2367 return -1;
2368 }
2369
2370 client->conn.persist.ka_max_attempts = max_attempts;
2371
2372 /* UNLOCK */
2373 nc_server_ch_client_unlock(client);
2374
2375 return 0;
2376}
2377
2378API int
2379nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout)
2380{
2381 struct nc_ch_client *client;
2382
2383 if (!client_name) {
2384 ERRARG("client_name");
2385 return -1;
2386 }
2387
2388 /* LOCK */
2389 client = nc_server_ch_client_lock(client_name, 0, NULL);
2390 if (!client) {
2391 return -1;
2392 }
2393
2394 if (client->conn_type != NC_CH_PERIOD) {
2395 ERR("Call Home client \"%s\" is not of periodic connection type.");
2396 /* UNLOCK */
2397 nc_server_ch_client_unlock(client);
2398 return -1;
2399 }
2400
2401 client->conn.period.idle_timeout = idle_timeout;
2402
2403 /* UNLOCK */
2404 nc_server_ch_client_unlock(client);
2405
2406 return 0;
2407}
2408
2409API int
2410nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout)
2411{
2412 struct nc_ch_client *client;
2413
2414 if (!client_name) {
2415 ERRARG("client_name");
2416 return -1;
2417 } else if (!reconnect_timeout) {
2418 ERRARG("reconnect_timeout");
2419 return -1;
2420 }
2421
2422 /* LOCK */
2423 client = nc_server_ch_client_lock(client_name, 0, NULL);
2424 if (!client) {
2425 return -1;
2426 }
2427
2428 if (client->conn_type != NC_CH_PERIOD) {
2429 ERR("Call Home client \"%s\" is not of periodic connection type.");
2430 /* UNLOCK */
2431 nc_server_ch_client_unlock(client);
2432 return -1;
2433 }
2434
2435 client->conn.period.reconnect_timeout = reconnect_timeout;
2436
2437 /* UNLOCK */
2438 nc_server_ch_client_unlock(client);
2439
2440 return 0;
2441}
2442
2443API int
2444nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with)
2445{
2446 struct nc_ch_client *client;
2447
2448 if (!client_name) {
2449 ERRARG("client_name");
2450 return -1;
2451 }
2452
2453 /* LOCK */
2454 client = nc_server_ch_client_lock(client_name, 0, NULL);
2455 if (!client) {
2456 return -1;
2457 }
2458
2459 client->start_with = start_with;
2460
2461 /* UNLOCK */
2462 nc_server_ch_client_unlock(client);
2463
2464 return 0;
2465}
2466
2467API int
2468nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts)
2469{
2470 struct nc_ch_client *client;
2471
2472 if (!client_name) {
2473 ERRARG("client_name");
2474 return -1;
2475 } else if (!max_attempts) {
2476 ERRARG("max_attempts");
2477 return -1;
2478 }
2479
2480 /* LOCK */
2481 client = nc_server_ch_client_lock(client_name, 0, NULL);
2482 if (!client) {
2483 return -1;
2484 }
2485
2486 client->max_attempts = max_attempts;
2487
2488 /* UNLOCK */
2489 nc_server_ch_client_unlock(client);
2490
2491 return 0;
2492}
2493
2494/* client lock is expected to be held */
2495static NC_MSG_TYPE
2496nc_connect_ch_client_endpt(struct nc_ch_client *client, struct nc_ch_endpt *endpt, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01002497{
Michal Vasko71090fc2016-05-24 16:37:28 +02002498 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002499 int sock, ret;
2500
Michal Vasko2e6defd2016-10-07 15:48:15 +02002501 sock = nc_sock_connect(endpt->address, endpt->port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01002502 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02002503 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002504 }
2505
Michal Vaskoade892d2017-02-22 13:40:35 +01002506 *session = nc_new_session(0);
Michal Vaskob05053d2016-01-22 16:12:06 +01002507 if (!(*session)) {
2508 ERRMEM;
2509 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002510 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002511 }
2512 (*session)->status = NC_STATUS_STARTING;
2513 (*session)->side = NC_SERVER;
2514 (*session)->ctx = server_opts.ctx;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002515 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002516 (*session)->host = lydict_insert(server_opts.ctx, endpt->address, 0);
2517 (*session)->port = endpt->port;
Michal Vaskob05053d2016-01-22 16:12:06 +01002518
2519 /* transport lock */
Michal Vaskob05053d2016-01-22 16:12:06 +01002520 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +01002521 pthread_cond_init((*session)->ti_cond, NULL);
2522 *(*session)->ti_inuse = 0;
Michal Vaskob05053d2016-01-22 16:12:06 +01002523
2524 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01002525#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002526 if (client->ti == NC_TI_LIBSSH) {
2527 (*session)->data = client->opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01002528 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002529 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002530
Michal Vasko71090fc2016-05-24 16:37:28 +02002531 if (ret < 0) {
2532 msgtype = NC_MSG_ERROR;
2533 goto fail;
2534 } else if (!ret) {
2535 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002536 goto fail;
2537 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002538 } else
2539#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002540#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002541 if (client->ti == NC_TI_OPENSSL) {
2542 (*session)->data = client->opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01002543 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002544 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002545
Michal Vasko71090fc2016-05-24 16:37:28 +02002546 if (ret < 0) {
2547 msgtype = NC_MSG_ERROR;
2548 goto fail;
2549 } else if (!ret) {
2550 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002551 goto fail;
2552 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002553 } else
2554#endif
2555 {
Michal Vaskob05053d2016-01-22 16:12:06 +01002556 ERRINT;
2557 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002558 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002559 goto fail;
2560 }
2561
2562 /* assign new SID atomically */
2563 /* LOCK */
2564 pthread_spin_lock(&server_opts.sid_lock);
2565 (*session)->id = server_opts.new_session_id++;
2566 /* UNLOCK */
2567 pthread_spin_unlock(&server_opts.sid_lock);
2568
2569 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02002570 msgtype = nc_handshake(*session);
2571 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01002572 goto fail;
2573 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002574 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002575 (*session)->status = NC_STATUS_RUNNING;
2576
Michal Vasko71090fc2016-05-24 16:37:28 +02002577 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002578
2579fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002580 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002581 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002582 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002583}
2584
Michal Vasko2e6defd2016-10-07 15:48:15 +02002585struct nc_ch_client_thread_arg {
2586 char *client_name;
2587 void (*session_clb)(const char *client_name, struct nc_session *new_session);
2588};
2589
2590static struct nc_ch_client *
2591nc_server_ch_client_with_endpt_lock(const char *name)
2592{
2593 struct nc_ch_client *client;
2594
2595 while (1) {
2596 /* LOCK */
2597 client = nc_server_ch_client_lock(name, 0, NULL);
2598 if (!client) {
2599 return NULL;
2600 }
2601 if (client->ch_endpt_count) {
2602 return client;
2603 }
2604 /* no endpoints defined yet */
2605
2606 /* UNLOCK */
2607 nc_server_ch_client_unlock(client);
2608
2609 usleep(NC_CH_NO_ENDPT_WAIT * 1000);
2610 }
2611
2612 return NULL;
2613}
2614
2615static int
2616nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data)
2617{
2618 int ret;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002619 uint32_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002620 struct timespec ts;
2621 struct nc_ch_client *client;
2622
2623 /* session created, initialize condition */
2624 session->opts.server.ch_lock = malloc(sizeof *session->opts.server.ch_lock);
2625 session->opts.server.ch_cond = malloc(sizeof *session->opts.server.ch_cond);
2626 if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) {
2627 ERRMEM;
2628 nc_session_free(session, NULL);
2629 return -1;
2630 }
2631 pthread_mutex_init(session->opts.server.ch_lock, NULL);
2632 pthread_cond_init(session->opts.server.ch_cond, NULL);
2633
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002634 session->flags |= NC_SESSION_CALLHOME;
2635
Michal Vasko2e6defd2016-10-07 15:48:15 +02002636 /* CH LOCK */
2637 pthread_mutex_lock(session->opts.server.ch_lock);
2638
2639 /* give the session to the user */
2640 data->session_clb(data->client_name, session);
2641
2642 do {
2643 nc_gettimespec(&ts);
Michal Vaskoe39ae6b2017-03-14 09:03:46 +01002644 nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002645
2646 ret = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts);
2647 if (ret && (ret != ETIMEDOUT)) {
2648 ERR("Pthread condition timedwait failed (%s).", strerror(ret));
2649 goto ch_client_remove;
2650 }
2651
2652 /* check whether the client was not removed */
2653 /* LOCK */
2654 client = nc_server_ch_client_lock(data->client_name, 0, NULL);
2655 if (!client) {
2656 /* client was removed, finish thread */
2657 VRB("Call Home client \"%s\" removed, but an established session will not be terminated.",
2658 data->client_name);
2659 goto ch_client_remove;
2660 }
2661
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002662 if (client->conn_type == NC_CH_PERSIST) {
2663 /* TODO keep-alives */
2664 idle_timeout = client->conn.persist.idle_timeout;
2665 } else {
2666 idle_timeout = client->conn.period.idle_timeout;
2667 }
2668
Michal Vasko3486a7c2017-03-03 13:28:07 +01002669 if (!session->opts.server.ntf_status && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) {
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002670 VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id);
2671 session->status = NC_STATUS_INVALID;
2672 session->term_reason = NC_SESSION_TERM_TIMEOUT;
2673 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002674
2675 /* UNLOCK */
2676 nc_server_ch_client_unlock(client);
2677
2678 } while (session->status == NC_STATUS_RUNNING);
2679
2680 /* CH UNLOCK */
2681 pthread_mutex_unlock(session->opts.server.ch_lock);
2682
2683 return 0;
2684
2685ch_client_remove:
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002686 /* make the session a standard one */
2687 pthread_cond_destroy(session->opts.server.ch_cond);
2688 free(session->opts.server.ch_cond);
2689 session->opts.server.ch_cond = NULL;
2690
2691 session->flags &= ~NC_SESSION_CALLHOME;
2692
Michal Vasko2e6defd2016-10-07 15:48:15 +02002693 /* CH UNLOCK */
2694 pthread_mutex_unlock(session->opts.server.ch_lock);
2695
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002696 pthread_mutex_destroy(session->opts.server.ch_lock);
2697 free(session->opts.server.ch_lock);
2698 session->opts.server.ch_lock = NULL;
2699
Michal Vasko2e6defd2016-10-07 15:48:15 +02002700 return 1;
2701}
2702
2703static void *
2704nc_ch_client_thread(void *arg)
2705{
2706 struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg;
2707 NC_MSG_TYPE msgtype;
2708 uint8_t cur_attempts = 0;
2709 uint16_t i;
2710 char *cur_endpt_name;
2711 struct nc_ch_endpt *cur_endpt;
2712 struct nc_session *session;
2713 struct nc_ch_client *client;
2714
2715 /* LOCK */
2716 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2717 if (!client) {
2718 goto cleanup;
2719 }
2720
2721 cur_endpt = &client->ch_endpts[0];
2722 cur_endpt_name = strdup(cur_endpt->name);
2723
Michal Vasko29af44b2016-10-13 10:59:55 +02002724 VRB("Call Home client \"%s\" connecting...", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002725 while (1) {
2726 msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session);
2727
2728 if (msgtype == NC_MSG_HELLO) {
2729 /* UNLOCK */
2730 nc_server_ch_client_unlock(client);
2731
Michal Vasko29af44b2016-10-13 10:59:55 +02002732 VRB("Call Home client \"%s\" session %u established.", data->client_name, session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002733 if (nc_server_ch_client_thread_session_cond_wait(session, data)) {
2734 goto cleanup;
2735 }
Michal Vasko29af44b2016-10-13 10:59:55 +02002736 VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002737
2738 /* LOCK */
2739 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2740 if (!client) {
2741 goto cleanup;
2742 }
2743
2744 /* session changed status -> it was disconnected for whatever reason,
2745 * persistent connection immediately tries to reconnect, periodic waits some first */
2746 if (client->conn_type == NC_CH_PERIOD) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002747 /* UNLOCK */
2748 nc_server_ch_client_unlock(client);
2749
2750 /* TODO wake up sometimes to check for new notifications */
2751 usleep(client->conn.period.reconnect_timeout * 60 * 1000000);
2752
2753 /* LOCK */
2754 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2755 if (!client) {
2756 goto cleanup;
2757 }
2758 }
2759
2760 /* set next endpoint to try */
2761 if (client->start_with == NC_CH_FIRST_LISTED) {
2762 cur_endpt = &client->ch_endpts[0];
2763 free(cur_endpt_name);
2764 cur_endpt_name = strdup(cur_endpt->name);
2765 } /* else we keep the current one */
2766 } else {
Michal Vasko6bb116b2016-10-26 13:53:46 +02002767 /* UNLOCK */
2768 nc_server_ch_client_unlock(client);
2769
Michal Vasko2e6defd2016-10-07 15:48:15 +02002770 /* session was not created */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002771 usleep(NC_CH_ENDPT_FAIL_WAIT * 1000);
2772
Michal Vasko6bb116b2016-10-26 13:53:46 +02002773 /* LOCK */
2774 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2775 if (!client) {
2776 goto cleanup;
2777 }
2778
Michal Vasko2e6defd2016-10-07 15:48:15 +02002779 ++cur_attempts;
2780 if (cur_attempts == client->max_attempts) {
2781 for (i = 0; i < client->ch_endpt_count; ++i) {
2782 if (!strcmp(client->ch_endpts[i].name, cur_endpt_name)) {
2783 break;
2784 }
2785 }
2786 if (i < client->ch_endpt_count - 1) {
2787 /* just go to the next endpoint */
2788 cur_endpt = &client->ch_endpts[i + 1];
2789 free(cur_endpt_name);
2790 cur_endpt_name = strdup(cur_endpt->name);
2791 } else {
2792 /* cur_endpoint was removed or is the last, either way start with the first one */
2793 cur_endpt = &client->ch_endpts[0];
2794 free(cur_endpt_name);
2795 cur_endpt_name = strdup(cur_endpt->name);
2796 }
2797
2798 cur_attempts = 0;
2799 } /* else we keep the current one */
2800 }
2801 }
2802
2803cleanup:
2804 VRB("Call Home client \"%s\" thread exit.", data->client_name);
2805
2806 free(data->client_name);
2807 free(data);
2808 return NULL;
2809}
2810
2811API int
2812nc_connect_ch_client_dispatch(const char *client_name,
2813 void (*session_clb)(const char *client_name, struct nc_session *new_session)) {
2814 int ret;
2815 pthread_t tid;
2816 struct nc_ch_client_thread_arg *arg;
2817
2818 if (!client_name) {
2819 ERRARG("client_name");
2820 return -1;
2821 } else if (!session_clb) {
2822 ERRARG("session_clb");
2823 return -1;
2824 }
2825
2826 arg = malloc(sizeof *arg);
2827 if (!arg) {
2828 ERRMEM;
2829 return -1;
2830 }
2831 arg->client_name = strdup(client_name);
2832 if (!arg->client_name) {
2833 ERRMEM;
2834 free(arg);
2835 return -1;
2836 }
2837 arg->session_clb = session_clb;
2838
2839 ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg);
2840 if (ret) {
2841 ERR("Creating a new thread failed (%s).", strerror(ret));
2842 free(arg->client_name);
2843 free(arg);
2844 return -1;
2845 }
2846 /* the thread now manages arg */
2847
2848 pthread_detach(tid);
2849
2850 return 0;
2851}
2852
Radek Krejci53691be2016-02-22 13:58:37 +01002853#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02002854
Michal Vaskoe8e07702017-03-15 10:19:30 +01002855API int
2856nc_server_endpt_count(void)
2857{
2858 return server_opts.endpt_count;
2859}
2860
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002861API time_t
2862nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02002863{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002864 if (!session || (session->side != NC_SERVER)) {
Michal Vaskof8352352016-05-24 09:11:36 +02002865 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002866 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02002867 }
2868
Michal Vasko2e6defd2016-10-07 15:48:15 +02002869 return session->opts.server.session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02002870}
Michal Vasko3486a7c2017-03-03 13:28:07 +01002871
2872API void
2873nc_session_set_notif_status(struct nc_session *session, int notif_status)
2874{
2875 if (!session || (session->side != NC_SERVER)) {
2876 ERRARG("session");
2877 return;
2878 }
2879
2880 session->opts.server.ntf_status = (notif_status ? 1 : 0);
2881}
2882
2883API int
2884nc_session_get_notif_status(const struct nc_session *session)
2885{
2886 if (!session || (session->side != NC_SERVER)) {
2887 ERRARG("session");
2888 return 0;
2889 }
2890
2891 return session->opts.server.ntf_status;
2892}