blob: 5a400e625c8a7a8f62591c7cfc150001f828e4fc [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 server session manipulation functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <stdint.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010025#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028
Michal Vasko1a38c862016-01-15 15:50:07 +010029#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010030#include "session_server.h"
31
Michal Vaskob48aa812016-01-18 14:13:09 +010032struct nc_server_opts server_opts = {
Michal Vasko2e6defd2016-10-07 15:48:15 +020033 .endpt_lock = PTHREAD_RWLOCK_INITIALIZER,
34 .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010035};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010036
fanchanghu966f2de2016-07-21 02:28:57 -040037static nc_rpc_clb global_rpc_clb = NULL;
38
Michal Vasko3031aae2016-01-27 16:07:18 +010039struct nc_endpt *
Michal Vasko2e6defd2016-10-07 15:48:15 +020040nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010041{
42 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010043 struct nc_endpt *endpt = NULL;
44
45 /* READ LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020046 pthread_rwlock_rdlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010047
48 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +020049 if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010050 endpt = &server_opts.endpts[i];
51 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010052 }
53 }
54
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010055 if (!endpt) {
56 ERR("Endpoint \"%s\" was not found.", name);
57 /* READ UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020058 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010059 return NULL;
60 }
61
62 /* ENDPT LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020063 pthread_mutex_lock(&endpt->lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010064
Michal Vaskoe2713da2016-08-22 16:06:40 +020065 if (idx) {
66 *idx = i;
67 }
68
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010069 return endpt;
70}
71
72void
73nc_server_endpt_unlock(struct nc_endpt *endpt)
74{
75 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020076 pthread_mutex_unlock(&endpt->lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010077
78 /* READ UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020079 pthread_rwlock_unlock(&server_opts.endpt_lock);
80}
81
82struct nc_ch_client *
83nc_server_ch_client_lock(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
84{
85 uint16_t i;
86 struct nc_ch_client *client = NULL;
87
88 /* READ LOCK */
89 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
90
91 for (i = 0; i < server_opts.ch_client_count; ++i) {
92 if (!strcmp(server_opts.ch_clients[i].name, name) && (!ti || (server_opts.ch_clients[i].ti == ti))) {
93 client = &server_opts.ch_clients[i];
94 break;
95 }
96 }
97
98 if (!client) {
99 ERR("Call Home client \"%s\" was not found.", name);
100 /* READ UNLOCK */
101 pthread_rwlock_unlock(&server_opts.ch_client_lock);
102 return NULL;
103 }
104
105 /* CH CLIENT LOCK */
106 pthread_mutex_lock(&client->lock);
107
108 if (idx) {
109 *idx = i;
110 }
111
112 return client;
113}
114
115void
116nc_server_ch_client_unlock(struct nc_ch_client *client)
117{
118 /* CH CLIENT UNLOCK */
119 pthread_mutex_unlock(&client->lock);
120
121 /* READ UNLOCK */
122 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100123}
Michal Vasko086311b2016-01-08 09:53:11 +0100124
Michal Vasko1a38c862016-01-15 15:50:07 +0100125API void
126nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
127{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200128 if (!session) {
129 ERRARG("session");
130 return;
131 } else if (!reason) {
132 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +0100133 return;
134 }
135
136 session->term_reason = reason;
137}
138
Michal Vasko086311b2016-01-08 09:53:11 +0100139int
Michal Vaskof05562c2016-01-20 12:06:43 +0100140nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100141{
142 const int optVal = 1;
143 const socklen_t optLen = sizeof(optVal);
144 int is_ipv4, sock;
145 struct sockaddr_storage saddr;
146
147 struct sockaddr_in *saddr4;
148 struct sockaddr_in6 *saddr6;
149
150
151 if (!strchr(address, ':')) {
152 is_ipv4 = 1;
153 } else {
154 is_ipv4 = 0;
155 }
156
157 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
158 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100159 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100160 goto fail;
161 }
162
163 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100164 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100165 goto fail;
166 }
167
168 bzero(&saddr, sizeof(struct sockaddr_storage));
169 if (is_ipv4) {
170 saddr4 = (struct sockaddr_in *)&saddr;
171
172 saddr4->sin_family = AF_INET;
173 saddr4->sin_port = htons(port);
174
175 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100176 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100177 goto fail;
178 }
179
180 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100181 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100182 goto fail;
183 }
184
185 } else {
186 saddr6 = (struct sockaddr_in6 *)&saddr;
187
188 saddr6->sin6_family = AF_INET6;
189 saddr6->sin6_port = htons(port);
190
191 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100192 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100193 goto fail;
194 }
195
196 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100197 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100198 goto fail;
199 }
200 }
201
Michal Vaskofb89d772016-01-08 12:25:35 +0100202 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100203 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100204 goto fail;
205 }
206
207 return sock;
208
209fail:
210 if (sock > -1) {
211 close(sock);
212 }
213
214 return -1;
215}
216
217int
Michal Vasko3031aae2016-01-27 16:07:18 +0100218nc_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 +0100219{
220 uint16_t i;
221 struct pollfd *pfd;
222 struct sockaddr_storage saddr;
223 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100224 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100225
226 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100227 if (!pfd) {
228 ERRMEM;
229 return -1;
230 }
231
Michal Vasko0a3f3752016-10-13 14:58:38 +0200232 for (i = 0; i < bind_count; ++i) {
Michal Vasko94acafc2016-09-23 13:40:10 +0200233 if (binds[i].sock < 0) {
234 /* invalid socket */
235 --bind_count;
236 continue;
237 }
Michal Vasko0a3f3752016-10-13 14:58:38 +0200238 if (binds[i].pollin) {
239 binds[i].pollin = 0;
240 /* leftover pollin */
241 sock = binds[i].sock;
242 break;
243 }
Michal Vasko086311b2016-01-08 09:53:11 +0100244 pfd[i].fd = binds[i].sock;
245 pfd[i].events = POLLIN;
246 pfd[i].revents = 0;
247 }
248
Michal Vasko0a3f3752016-10-13 14:58:38 +0200249 if (sock == -1) {
250 /* poll for a new connection */
251 ret = poll(pfd, bind_count, timeout);
252 if (!ret) {
253 /* we timeouted */
254 free(pfd);
255 return 0;
256 } else if (ret == -1) {
257 ERR("Poll failed (%s).", strerror(errno));
258 free(pfd);
259 return -1;
260 }
Michal Vasko086311b2016-01-08 09:53:11 +0100261
Michal Vasko0a3f3752016-10-13 14:58:38 +0200262 for (i = 0; i < bind_count; ++i) {
263 if (pfd[i].revents & POLLIN) {
264 --ret;
265
266 if (!ret) {
267 /* the last socket with an event, use it */
268 sock = pfd[i].fd;
269 break;
270 } else {
271 /* just remember the event for next time */
272 binds[i].pollin = 1;
273 }
274 }
Michal Vasko086311b2016-01-08 09:53:11 +0100275 }
276 }
277 free(pfd);
278
279 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100280 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100281 return -1;
282 }
283
284 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100285 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100286 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100287 return -1;
288 }
Michal Vasko6ccb29d2016-10-13 15:00:27 +0200289 VRB("Accepted a connection on %s:%u.", binds[i].address, binds[i].port);
Michal Vasko086311b2016-01-08 09:53:11 +0100290
Michal Vasko0190bc32016-03-02 15:47:49 +0100291 /* make the socket non-blocking */
292 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
293 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100294 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100295 return -1;
296 }
297
Michal Vasko3031aae2016-01-27 16:07:18 +0100298 if (idx) {
299 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100300 }
301
Michal Vasko086311b2016-01-08 09:53:11 +0100302 /* host was requested */
303 if (host) {
304 if (saddr.ss_family == AF_INET) {
305 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100306 if (*host) {
307 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
308 ERR("inet_ntop failed (%s).", strerror(errno));
309 free(*host);
310 *host = NULL;
311 }
Michal Vasko086311b2016-01-08 09:53:11 +0100312
Michal Vasko4eb3c312016-03-01 14:09:37 +0100313 if (port) {
314 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
315 }
316 } else {
317 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100318 }
319 } else if (saddr.ss_family == AF_INET6) {
320 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100321 if (*host) {
322 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
323 ERR("inet_ntop failed (%s).", strerror(errno));
324 free(*host);
325 *host = NULL;
326 }
Michal Vasko086311b2016-01-08 09:53:11 +0100327
Michal Vasko4eb3c312016-03-01 14:09:37 +0100328 if (port) {
329 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
330 }
331 } else {
332 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100333 }
334 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100335 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100336 }
337 }
338
339 return ret;
340}
341
Michal Vasko05ba9df2016-01-13 14:40:27 +0100342static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100343nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100344{
345 const char *identifier = NULL, *version = NULL, *format = NULL;
346 char *model_data = NULL;
347 const struct lys_module *module;
348 struct nc_server_error *err;
349 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100350 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100351
352 LY_TREE_FOR(rpc->child, child) {
353 if (!strcmp(child->schema->name, "identifier")) {
354 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
355 } else if (!strcmp(child->schema->name, "version")) {
356 version = ((struct lyd_node_leaf_list *)child)->value_str;
357 } else if (!strcmp(child->schema->name, "format")) {
358 format = ((struct lyd_node_leaf_list *)child)->value_str;
359 }
360 }
361
362 /* check version */
363 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100364 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
365 nc_err_set_msg(err, "The requested version is not supported.", "en");
366 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100367 }
368
369 /* check and get module with the name identifier */
370 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
371 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200372 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
373 }
374 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100375 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
376 nc_err_set_msg(err, "The requested schema was not found.", "en");
377 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100378 }
379
380 /* check format */
Radek Krejci89c34452016-12-07 15:59:45 +0100381 if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100382 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
Radek Krejci89c34452016-12-07 15:59:45 +0100383 } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100384 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
385 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100386 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
387 nc_err_set_msg(err, "The requested format is not supported.", "en");
388 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100389 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200390 if (!model_data) {
391 ERRINT;
392 return NULL;
393 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100394
Michal Vasko303245c2016-03-24 15:20:03 +0100395 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200396 if (!sdata) {
397 ERRINT;
398 free(model_data);
399 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100400 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200401
Radek Krejci539efb62016-08-24 15:05:16 +0200402 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
403 LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
Michal Vasko3cb0b132017-01-03 14:59:51 +0100404 if (!data || lyd_validate(&data, LYD_OPT_RPCREPLY, NULL)) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100405 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200406 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100407 return NULL;
408 }
409
Radek Krejci36dfdb32016-09-01 16:56:35 +0200410 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100411}
412
413static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100414nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100415{
Michal Vasko428087d2016-01-14 16:04:28 +0100416 session->term_reason = NC_SESSION_TERM_CLOSED;
417 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100418}
419
Michal Vasko086311b2016-01-08 09:53:11 +0100420API int
421nc_server_init(struct ly_ctx *ctx)
422{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100423 const struct lys_node *rpc;
424
Michal Vasko086311b2016-01-08 09:53:11 +0100425 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200426 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100427 return -1;
428 }
429
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100430 nc_init();
431
Michal Vasko05ba9df2016-01-13 14:40:27 +0100432 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100433 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100434 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100435 lys_set_private(rpc, nc_clb_default_get_schema);
436 }
437
438 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100439 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100440 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100441 lys_set_private(rpc, nc_clb_default_close_session);
442 }
443
Michal Vasko086311b2016-01-08 09:53:11 +0100444 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100445
446 server_opts.new_session_id = 1;
447 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
448
Michal Vasko086311b2016-01-08 09:53:11 +0100449 return 0;
450}
451
Michal Vaskob48aa812016-01-18 14:13:09 +0100452API void
453nc_server_destroy(void)
454{
Radek Krejci658782b2016-12-04 22:04:55 +0100455 unsigned int i;
456
457 for (i = 0; i < server_opts.capabilities_count; i++) {
458 lydict_remove(server_opts.ctx, server_opts.capabilities[i]);
459 }
460 free(server_opts.capabilities);
Michal Vaskob48aa812016-01-18 14:13:09 +0100461 pthread_spin_destroy(&server_opts.sid_lock);
462
Radek Krejci53691be2016-02-22 13:58:37 +0100463#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko59050372016-11-22 14:33:55 +0100464 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100465#endif
Michal Vasko17dfda92016-12-01 14:06:16 +0100466#ifdef NC_ENABLED_SSH
467 nc_server_ssh_del_authkey(NULL, NULL, 0, NULL);
468#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100469 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100470}
471
Michal Vasko086311b2016-01-08 09:53:11 +0100472API int
473nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
474{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200475 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
476 ERRARG("basic_mode");
477 return -1;
478 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
479 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100480 return -1;
481 }
482
483 server_opts.wd_basic_mode = basic_mode;
484 server_opts.wd_also_supported = also_supported;
485 return 0;
486}
487
Michal Vasko1a38c862016-01-15 15:50:07 +0100488API void
Michal Vasko55f03972016-04-13 08:56:01 +0200489nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
490{
491 if (!basic_mode && !also_supported) {
492 ERRARG("basic_mode and also_supported");
493 return;
494 }
495
496 if (basic_mode) {
497 *basic_mode = server_opts.wd_basic_mode;
498 }
499 if (also_supported) {
500 *also_supported = server_opts.wd_also_supported;
501 }
502}
503
Michal Vasko55f03972016-04-13 08:56:01 +0200504API int
Radek Krejci658782b2016-12-04 22:04:55 +0100505nc_server_set_capability(const char *value)
Michal Vasko55f03972016-04-13 08:56:01 +0200506{
Radek Krejci658782b2016-12-04 22:04:55 +0100507 const char **new;
508
509 if (!value || !value[0]) {
510 ERRARG("value must not be empty");
511 return EXIT_FAILURE;
512 }
513
514 server_opts.capabilities_count++;
515 new = realloc(server_opts.capabilities, server_opts.capabilities_count * sizeof *server_opts.capabilities);
516 if (!new) {
517 ERRMEM;
518 return EXIT_FAILURE;
519 }
520 server_opts.capabilities = new;
521 server_opts.capabilities[server_opts.capabilities_count - 1] = lydict_insert(server_opts.ctx, value, 0);
522
523 return EXIT_SUCCESS;
Michal Vasko55f03972016-04-13 08:56:01 +0200524}
525
Michal Vasko1a38c862016-01-15 15:50:07 +0100526API void
Michal Vasko086311b2016-01-08 09:53:11 +0100527nc_server_set_hello_timeout(uint16_t hello_timeout)
528{
Michal Vasko086311b2016-01-08 09:53:11 +0100529 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100530}
531
Michal Vasko55f03972016-04-13 08:56:01 +0200532API uint16_t
533nc_server_get_hello_timeout(void)
534{
535 return server_opts.hello_timeout;
536}
537
Michal Vasko1a38c862016-01-15 15:50:07 +0100538API void
Michal Vasko086311b2016-01-08 09:53:11 +0100539nc_server_set_idle_timeout(uint16_t idle_timeout)
540{
Michal Vasko086311b2016-01-08 09:53:11 +0100541 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100542}
543
Michal Vasko55f03972016-04-13 08:56:01 +0200544API uint16_t
545nc_server_get_idle_timeout(void)
546{
547 return server_opts.idle_timeout;
548}
549
Michal Vasko71090fc2016-05-24 16:37:28 +0200550API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100551nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100552{
Michal Vasko71090fc2016-05-24 16:37:28 +0200553 NC_MSG_TYPE msgtype;
554
Michal Vasko45e53ae2016-04-07 11:46:03 +0200555 if (!server_opts.ctx) {
556 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200557 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200558 } else if (fdin < 0) {
559 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200560 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200561 } else if (fdout < 0) {
562 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200563 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200564 } else if (!username) {
565 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200566 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200567 } else if (!session) {
568 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200569 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100570 }
571
572 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100573 *session = calloc(1, sizeof **session);
574 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100575 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200576 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100577 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100578 (*session)->status = NC_STATUS_STARTING;
579 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100580
581 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100582 (*session)->ti_type = NC_TI_FD;
583 (*session)->ti.fd.in = fdin;
584 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100585
586 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100587 (*session)->flags = NC_SESSION_SHAREDCTX;
588 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100589
Michal Vaskob48aa812016-01-18 14:13:09 +0100590 /* assign new SID atomically */
591 pthread_spin_lock(&server_opts.sid_lock);
592 (*session)->id = server_opts.new_session_id++;
593 pthread_spin_unlock(&server_opts.sid_lock);
594
Michal Vasko086311b2016-01-08 09:53:11 +0100595 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200596 msgtype = nc_handshake(*session);
597 if (msgtype != NC_MSG_HELLO) {
598 nc_session_free(*session, NULL);
599 *session = NULL;
600 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100601 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200602 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100603 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100604
Michal Vasko71090fc2016-05-24 16:37:28 +0200605 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100606}
Michal Vasko9e036d52016-01-08 10:49:26 +0100607
Michal Vaskob30b99c2016-07-26 11:35:43 +0200608static void
609nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
610{
611 uint8_t i, found = 0;
612
613 for (i = 0; i < ps->queue_len; ++i) {
614 /* idx round buffer adjust */
615 if (ps->queue_begin + i == NC_PS_QUEUE_SIZE) {
616 i = -ps->queue_begin;
617 }
618
619 if (found) {
620 /* move the value back one place */
621 if (ps->queue[ps->queue_begin + i] == id) {
622 /* another equal value, simply cannot be */
623 ERRINT;
624 }
625
626 if (ps->queue_begin + i == 0) {
627 ps->queue[NC_PS_QUEUE_SIZE - 1] = ps->queue[ps->queue_begin + i];
628 } else {
629 ps->queue[ps->queue_begin + i - 1] = ps->queue[ps->queue_begin + i];
630 }
631 } else if (ps->queue[ps->queue_begin + i] == id) {
632 /* found our id, there can be no more equal valid values */
633 found = 1;
634 }
635 }
636
637 if (!found) {
638 ERRINT;
639 }
640 --ps->queue_len;
641}
642
Michal Vaskof04a52a2016-04-07 10:52:10 +0200643int
Michal Vasko26043172016-07-26 14:08:59 +0200644nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200645{
646 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200647 uint8_t queue_last;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200648 struct timespec ts;
649
Radek Krejci7ac16052016-07-15 11:48:18 +0200650 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200651 ts.tv_sec += NC_READ_TIMEOUT;
652
653 /* LOCK */
654 ret = pthread_mutex_timedlock(&ps->lock, &ts);
655 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200656 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200657 return -1;
658 }
659
660 /* get a unique queue value (by adding 1 to the last added value, if any) */
661 if (ps->queue_len) {
662 queue_last = ps->queue_begin + ps->queue_len - 1;
663 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
664 queue_last -= NC_PS_QUEUE_SIZE;
665 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200666 *id = ps->queue[queue_last] + 1;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200667 } else {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200668 *id = 0;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200669 }
670
671 /* add ourselves into the queue */
672 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko26043172016-07-26 14:08:59 +0200673 ERR("%s: pollsession queue too small.", func);
Michal Vasko0ea456b2016-07-26 12:23:24 +0200674 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200675 return -1;
676 }
677 ++ps->queue_len;
678 queue_last = ps->queue_begin + ps->queue_len - 1;
679 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
680 queue_last -= NC_PS_QUEUE_SIZE;
681 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200682 ps->queue[queue_last] = *id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200683
684 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200685 while (ps->queue[ps->queue_begin] != *id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200686 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200687 ts.tv_sec += NC_READ_TIMEOUT;
688
689 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
690 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200691 ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200692 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200693 nc_ps_queue_remove_id(ps, *id);
694 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200695 return -1;
696 }
697 }
698
Michal Vaskobe86fe32016-04-07 10:43:03 +0200699 /* UNLOCK */
700 pthread_mutex_unlock(&ps->lock);
701
702 return 0;
703}
704
Michal Vaskof04a52a2016-04-07 10:52:10 +0200705int
Michal Vasko26043172016-07-26 14:08:59 +0200706nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200707{
708 int ret;
709 struct timespec ts;
710
Radek Krejci7ac16052016-07-15 11:48:18 +0200711 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200712 ts.tv_sec += NC_READ_TIMEOUT;
713
714 /* LOCK */
715 ret = pthread_mutex_timedlock(&ps->lock, &ts);
716 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200717 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200718 ret = -1;
719 }
720
Michal Vaskob30b99c2016-07-26 11:35:43 +0200721 /* we must be the first, it was our turn after all, right? */
722 if (ps->queue[ps->queue_begin] != id) {
723 ERRINT;
Michal Vaskob1a094b2016-10-05 14:04:52 +0200724 /* UNLOCK */
725 if (!ret) {
726 pthread_mutex_unlock(&ps->lock);
727 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200728 return -1;
729 }
730
Michal Vaskobe86fe32016-04-07 10:43:03 +0200731 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200732 nc_ps_queue_remove_id(ps, id);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200733
734 /* broadcast to all other threads that the queue moved */
735 pthread_cond_broadcast(&ps->cond);
736
Michal Vaskobe86fe32016-04-07 10:43:03 +0200737 /* UNLOCK */
738 if (!ret) {
739 pthread_mutex_unlock(&ps->lock);
740 }
741
742 return ret;
743}
744
Michal Vasko428087d2016-01-14 16:04:28 +0100745API struct nc_pollsession *
746nc_ps_new(void)
747{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100748 struct nc_pollsession *ps;
749
750 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100751 if (!ps) {
752 ERRMEM;
753 return NULL;
754 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200755 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100756 pthread_mutex_init(&ps->lock, NULL);
757
758 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100759}
760
761API void
762nc_ps_free(struct nc_pollsession *ps)
763{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100764 if (!ps) {
765 return;
766 }
767
Michal Vaskobe86fe32016-04-07 10:43:03 +0200768 if (ps->queue_len) {
769 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
770 }
771
Michal Vasko428087d2016-01-14 16:04:28 +0100772 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100773 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200774 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100775
Michal Vasko428087d2016-01-14 16:04:28 +0100776 free(ps);
777}
778
779API int
780nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
781{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200782 uint8_t q_id;
783
Michal Vasko45e53ae2016-04-07 11:46:03 +0200784 if (!ps) {
785 ERRARG("ps");
786 return -1;
787 } else if (!session) {
788 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100789 return -1;
790 }
791
Michal Vasko48a63ed2016-03-01 09:48:21 +0100792 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200793 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200794 return -1;
795 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100796
Michal Vasko428087d2016-01-14 16:04:28 +0100797 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100798 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
Michal Vasko99f251b2017-01-11 11:31:46 +0100799 if (!ps->sessions) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100800 ERRMEM;
801 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200802 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100803 return -1;
804 }
Michal Vasko3a715132016-01-21 15:40:31 +0100805 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100806
Michal Vasko48a63ed2016-03-01 09:48:21 +0100807 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200808 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100809}
810
Michal Vasko48a63ed2016-03-01 09:48:21 +0100811static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100812_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100813{
814 uint16_t i;
815
Radek Krejcid5f978f2016-03-03 13:14:45 +0100816 if (index >= 0) {
817 i = (uint16_t)index;
818 goto remove;
819 }
Michal Vasko428087d2016-01-14 16:04:28 +0100820 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100821 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100822remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100823 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100824 if (i < ps->session_count) {
825 ps->sessions[i] = ps->sessions[ps->session_count];
Michal Vasko99f251b2017-01-11 11:31:46 +0100826 if (ps->last_event_session == i) {
827 ps->last_event_session = 0;
828 }
Michal Vasko58005732016-02-02 15:50:52 +0100829 } else if (!ps->session_count) {
830 free(ps->sessions);
831 ps->sessions = NULL;
Michal Vasko58005732016-02-02 15:50:52 +0100832 }
Michal Vasko428087d2016-01-14 16:04:28 +0100833 return 0;
834 }
835 }
836
Michal Vaskof0537d82016-01-29 14:42:38 +0100837 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100838}
839
Michal Vasko48a63ed2016-03-01 09:48:21 +0100840API int
841nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
842{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200843 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200844 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100845
Michal Vasko45e53ae2016-04-07 11:46:03 +0200846 if (!ps) {
847 ERRARG("ps");
848 return -1;
849 } else if (!session) {
850 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100851 return -1;
852 }
853
854 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200855 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200856 return -1;
857 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100858
Radek Krejcid5f978f2016-03-03 13:14:45 +0100859 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100860
861 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200862 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100863
Michal Vaskobe86fe32016-04-07 10:43:03 +0200864 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100865}
866
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100867API uint16_t
868nc_ps_session_count(struct nc_pollsession *ps)
869{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200870 uint8_t q_id;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100871 uint16_t count;
872
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100873 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200874 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100875 return 0;
876 }
877
Michal Vasko48a63ed2016-03-01 09:48:21 +0100878 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200879 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200880 return -1;
881 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100882
883 count = ps->session_count;
884
885 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200886 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100887
888 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100889}
890
Michal Vasko71090fc2016-05-24 16:37:28 +0200891/* must be called holding the session lock!
892 * returns: NC_PSPOLL_ERROR,
893 * NC_PSPOLL_BAD_RPC,
894 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
895 * NC_PSPOLL_RPC
896 */
897static int
Radek Krejci93e80222016-10-03 13:34:25 +0200898nc_server_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
Michal Vasko428087d2016-01-14 16:04:28 +0100899{
900 struct lyxml_elem *xml = NULL;
901 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200902 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200903 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100904
Michal Vasko45e53ae2016-04-07 11:46:03 +0200905 if (!session) {
906 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200907 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200908 } else if (!rpc) {
909 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200910 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100911 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100912 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200913 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100914 }
915
916 msgtype = nc_read_msg(session, &xml);
917
918 switch (msgtype) {
919 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200920 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100921 if (!*rpc) {
922 ERRMEM;
923 goto error;
924 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100925
Radek Krejcif93c7d42016-04-06 13:41:15 +0200926 ly_errno = LY_SUCCESS;
Michal Vasko0a5ae9a2016-11-15 11:54:47 +0100927 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_RPC | LYD_OPT_DESTRUCT | LYD_OPT_STRICT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100928 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200929 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200930 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200931 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200932 nc_server_reply_free(reply);
933 if (ret == -1) {
934 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200935 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200936 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
937 } else {
938 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100939 }
Michal Vasko428087d2016-01-14 16:04:28 +0100940 (*rpc)->root = xml;
941 break;
942 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100943 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200944 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100945 goto error;
946 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100947 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200948 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100949 goto error;
950 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100951 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200952 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100953 goto error;
954 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200955 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100956 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
957 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200958 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100959 break;
960 }
961
Michal Vasko71090fc2016-05-24 16:37:28 +0200962 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100963
964error:
965 /* cleanup */
966 lyxml_free(server_opts.ctx, xml);
967
Michal Vasko71090fc2016-05-24 16:37:28 +0200968 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100969}
970
fanchanghu966f2de2016-07-21 02:28:57 -0400971API void
972nc_set_global_rpc_clb(nc_rpc_clb clb)
973{
974 global_rpc_clb = clb;
975}
976
Radek Krejci93e80222016-10-03 13:34:25 +0200977API NC_MSG_TYPE
978nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout)
979{
980 NC_MSG_TYPE result = NC_MSG_NOTIF;
981 int ret;
982
983 /* check parameters */
984 if (!session) {
985 ERRARG("session");
986 return NC_MSG_ERROR;
987 } else if (!notif || !notif->tree || !notif->eventtime) {
988 ERRARG("notif");
989 return NC_MSG_ERROR;
990 }
991
992 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
993 ret = nc_timedlock(session->ti_lock, timeout, __func__);
994 if (ret < 0) {
995 return NC_MSG_ERROR;
996 } else if (!ret) {
997 return NC_MSG_WOULDBLOCK;
998 }
999
1000 ret = nc_write_msg(session, NC_MSG_NOTIF, notif);
1001 if (ret == -1) {
1002 ERR("Session %u: failed to write notification.", session->id);
1003 result = NC_MSG_ERROR;
1004 }
1005 pthread_mutex_unlock(session->ti_lock);
1006
1007 return result;
1008}
1009
Michal Vasko71090fc2016-05-24 16:37:28 +02001010/* must be called holding the session lock!
1011 * returns: NC_PSPOLL_ERROR,
1012 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
1013 * NC_PSPOLL_REPLY_ERROR,
1014 * 0
1015 */
1016static int
Radek Krejci93e80222016-10-03 13:34:25 +02001017nc_server_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001018{
1019 nc_rpc_clb clb;
1020 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +02001021 struct lys_node *rpc_act = NULL;
1022 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +02001023 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +01001024
Michal Vasko4a827e52016-03-03 10:59:00 +01001025 if (!rpc) {
1026 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001027 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +01001028 }
1029
Michal Vasko90e8e692016-07-13 12:27:57 +02001030 if (rpc->tree->schema->nodetype == LYS_RPC) {
1031 /* RPC */
1032 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -04001033 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001034 /* action */
1035 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
1036 if (elem->schema->nodetype == LYS_ACTION) {
1037 rpc_act = elem->schema;
1038 break;
1039 }
1040 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -04001041 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001042 if (!rpc_act) {
1043 ERRINT;
1044 return NC_PSPOLL_ERROR;
1045 }
1046 }
1047
1048 if (!rpc_act->priv) {
1049 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +01001050 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +01001051 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001052 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +01001053 reply = clb(rpc->tree, session);
1054 }
1055
1056 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001057 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +01001058 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001059 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
1060 if (reply->type == NC_RPL_ERROR) {
1061 ret |= NC_PSPOLL_REPLY_ERROR;
1062 }
1063 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +01001064
Michal Vasko71090fc2016-05-24 16:37:28 +02001065 if (r == -1) {
1066 ERR("Session %u: failed to write reply.", session->id);
1067 ret |= NC_PSPOLL_ERROR;
1068 }
Michal Vasko428087d2016-01-14 16:04:28 +01001069
1070 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1071 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1072 session->status = NC_STATUS_INVALID;
1073 }
1074
Michal Vasko71090fc2016-05-24 16:37:28 +02001075 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001076}
1077
1078API int
Michal Vasko71090fc2016-05-24 16:37:28 +02001079nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +01001080{
Michal Vasko99f251b2017-01-11 11:31:46 +01001081 int ret, r, poll_ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +02001082 uint8_t q_id;
Michal Vasko99f251b2017-01-11 11:31:46 +01001083 uint16_t i, j;
1084 char msg[256];
1085 NC_SESSION_TERM_REASON term_reason;
1086 struct pollfd pfd;
1087 struct timespec begin_ts, cur_ts;
Michal Vasko71090fc2016-05-24 16:37:28 +02001088 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +01001089 struct nc_server_rpc *rpc = NULL;
Michal Vasko99f251b2017-01-11 11:31:46 +01001090#ifdef NC_ENABLED_SSH
1091 struct nc_session *new;
1092#endif
Michal Vasko428087d2016-01-14 16:04:28 +01001093
1094 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001095 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001096 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001097 }
1098
Michal Vasko99f251b2017-01-11 11:31:46 +01001099 nc_gettimespec(&begin_ts);
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001100
Michal Vasko48a63ed2016-03-01 09:48:21 +01001101 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001102 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001103 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001104 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001105
Michal Vasko99f251b2017-01-11 11:31:46 +01001106 /* check that all session are fine */
Michal Vasko428087d2016-01-14 16:04:28 +01001107 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001108 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1109 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001110 ret = NC_PSPOLL_ERROR;
1111 if (session) {
1112 *session = ps->sessions[i];
1113 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001114 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001115 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001116
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001117 /* TODO invalidate only sessions without subscription */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001118 if (!(ps->sessions[i]->flags & NC_SESSION_CALLHOME) && server_opts.idle_timeout
Michal Vasko99f251b2017-01-11 11:31:46 +01001119 && (begin_ts.tv_sec >= ps->sessions[i]->opts.server.last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001120 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1121 ps->sessions[i]->status = NC_STATUS_INVALID;
1122 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001123 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1124 if (session) {
1125 *session = ps->sessions[i];
1126 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001127 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001128 }
Michal Vasko428087d2016-01-14 16:04:28 +01001129 }
1130
Michal Vasko99f251b2017-01-11 11:31:46 +01001131 /* poll on all the sessions one-by-one */
1132 do {
1133 /* loop from i to j */
1134 if (ps->last_event_session == ps->session_count - 1) {
1135 i = j = 0;
1136 } else {
1137 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001138 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001139 do {
1140 cur_session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +01001141
Michal Vasko99f251b2017-01-11 11:31:46 +01001142 switch (cur_session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +01001143#ifdef NC_ENABLED_SSH
Michal Vasko99f251b2017-01-11 11:31:46 +01001144 case NC_TI_LIBSSH:
1145 r = ssh_channel_poll_timeout(cur_session->ti.libssh.channel, 0, 0);
1146 if (r < 1) {
1147 if (r == SSH_EOF) {
1148 sprintf(msg, "SSH channel unexpectedly closed");
1149 term_reason = NC_SESSION_TERM_DROPPED;
1150 poll_ret = -2;
1151 } else if (r == SSH_ERROR) {
1152 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(cur_session->ti.libssh.session));
1153 poll_ret = -1;
1154 } else {
1155 poll_ret = 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001156 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001157 break;
Michal Vasko96164bf2016-01-21 15:41:58 +01001158 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001159 /* we have some data, but it may be just an SSH message */
Michal Vasko96164bf2016-01-21 15:41:58 +01001160
Michal Vasko99f251b2017-01-11 11:31:46 +01001161 r = nc_timedlock(cur_session->ti_lock, timeout, __func__);
1162 if (r < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001163 if (session) {
Michal Vasko99f251b2017-01-11 11:31:46 +01001164 *session = cur_session;
Michal Vasko71090fc2016-05-24 16:37:28 +02001165 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001166 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001167 goto finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001168 } else if (!r) {
1169 if (session) {
1170 *session = cur_session;
Michal Vasko428087d2016-01-14 16:04:28 +01001171 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001172 ret = NC_PSPOLL_TIMEOUT;
1173 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001174 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001175 r = ssh_execute_message_callbacks(cur_session->ti.libssh.session);
1176 pthread_mutex_unlock(cur_session->ti_lock);
1177
1178 if (r != SSH_OK) {
1179 sprintf(msg, "failed to receive SSH messages (%s)", ssh_get_error(cur_session->ti.libssh.session));
1180 term_reason = NC_SESSION_TERM_OTHER;
1181 poll_ret = -1;
1182 } else if (cur_session->flags & NC_SESSION_SSH_NEW_MSG) {
1183 /* new SSH message */
1184 cur_session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1185 if (cur_session->ti.libssh.next) {
1186 for (new = cur_session->ti.libssh.next; new != cur_session; new = new->ti.libssh.next) {
1187 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel
1188 && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1189 /* new NETCONF SSH channel */
1190 if (session) {
1191 *session = cur_session;
1192 }
1193 ret = NC_PSPOLL_SSH_CHANNEL;
1194 goto finish;
1195 }
1196 }
1197 }
1198
1199 /* just some SSH message */
1200 if (session) {
1201 *session = cur_session;
1202 }
1203 ret = NC_PSPOLL_SSH_MSG;
1204 goto finish;
1205 } else {
1206 /* we have some application data */
1207 poll_ret = 1;
1208 }
1209 break;
1210#endif
1211#ifdef NC_ENABLED_TLS
1212 case NC_TI_OPENSSL:
1213 r = SSL_pending(cur_session->ti.tls);
1214 if (!r) {
1215 /* no data pending in the SSL buffer, poll fd */
1216 pfd.fd = SSL_get_rfd(cur_session->ti.tls);
1217 if (pfd.fd < 0) {
1218 ERRINT;
1219 ret = NC_PSPOLL_ERROR;
1220 goto finish;
1221 }
1222 pfd.events = POLLIN;
1223 pfd.revents = 0;
1224 r = poll(&pfd, 1, 0);
1225
1226 if (r < 0) {
1227 sprintf(msg, "poll failed (%s)", strerror(errno));
1228 poll_ret = -1;
1229 } else if (r > 0) {
1230 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1231 sprintf(msg, "communication socket unexpectedly closed");
1232 term_reason = NC_SESSION_TERM_DROPPED;
1233 poll_ret = -2;
1234 } else if (pfd.revents & POLLERR) {
1235 sprintf(msg, "communication socket error");
1236 term_reason = NC_SESSION_TERM_OTHER;
1237 poll_ret = -2;
1238 } else {
1239 poll_ret = 1;
1240 }
1241 } else {
1242 poll_ret = 0;
1243 }
1244 } else {
1245 poll_ret = 1;
1246 }
1247 break;
1248#endif
1249 case NC_TI_FD:
1250 pfd.fd = cur_session->ti.fd.in;
1251 pfd.events = POLLIN;
1252 pfd.revents = 0;
1253 r = poll(&pfd, 1, 0);
1254
1255 if (r < 0) {
1256 sprintf(msg, "poll failed (%s)", strerror(errno));
1257 poll_ret = 0;
1258 } else if (r > 0) {
1259 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1260 sprintf(msg, "communication socket unexpectedly closed");
1261 term_reason = NC_SESSION_TERM_DROPPED;
1262 poll_ret = -2;
1263 } else if (pfd.revents & POLLERR) {
1264 sprintf(msg, "communication socket error");
1265 term_reason = NC_SESSION_TERM_OTHER;
1266 poll_ret = -2;
1267 } else {
1268 poll_ret = 1;
1269 }
1270 } else {
1271 poll_ret = 0;
1272 }
1273 break;
1274 case NC_TI_NONE:
1275 ERRINT;
1276 ret = NC_PSPOLL_ERROR;
1277 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001278 }
Michal Vasko428087d2016-01-14 16:04:28 +01001279
Michal Vasko99f251b2017-01-11 11:31:46 +01001280 /* here: poll_ret == -2 - session error, session terminated,
1281 * poll_ret == -1 - generic error,
1282 * poll_ret == 0 - nothing to read,
1283 * poll_ret > 0 - data available */
1284 if (poll_ret == -2) {
1285 ERR("Session %u: %s.", cur_session->id, msg);
1286 cur_session->status = NC_STATUS_INVALID;
1287 cur_session->term_reason = term_reason;
1288 if (session) {
1289 *session = cur_session;
1290 }
1291 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1292 goto finish;
1293 } else if (poll_ret == -1) {
1294 ERR("Session %u: %s.", cur_session->id, msg);
1295 ret = NC_PSPOLL_ERROR;
1296 goto finish;
1297 } else if (poll_ret > 0) {
1298 break;
1299 }
1300
1301 /* next iteration */
1302 if (i == ps->session_count - 1) {
1303 i = 0;
1304 } else {
1305 ++i;
1306 }
1307 } while (i != j);
1308
1309 /* no event */
1310 if (!poll_ret && (timeout > -1)) {
1311 usleep(NC_TIMEOUT_STEP);
1312
1313 nc_gettimespec(&cur_ts);
1314 /* final timeout */
1315 if (nc_difftimespec(&begin_ts, &cur_ts) >= (unsigned)timeout) {
1316 ret = NC_PSPOLL_TIMEOUT;
1317 goto finish;
1318 }
Michal Vasko428087d2016-01-14 16:04:28 +01001319 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001320 } while (!poll_ret);
Michal Vasko428087d2016-01-14 16:04:28 +01001321
1322 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001323 if (session) {
1324 *session = cur_session;
1325 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001326 ps->last_event_session = i;
Michal Vasko428087d2016-01-14 16:04:28 +01001327
Michal Vaskobd8ef262016-01-20 11:09:27 +01001328 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko99f251b2017-01-11 11:31:46 +01001329 r = nc_timedlock(cur_session->ti_lock, timeout, __func__);
1330 if (r < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001331 ret = NC_PSPOLL_ERROR;
1332 goto finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001333 } else if (!r) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001334 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001335 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001336 }
1337
Radek Krejci93e80222016-10-03 13:34:25 +02001338 ret = nc_server_recv_rpc(cur_session, &rpc);
Michal Vasko71090fc2016-05-24 16:37:28 +02001339 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1340 pthread_mutex_unlock(cur_session->ti_lock);
1341 if (cur_session->status != NC_STATUS_RUNNING) {
1342 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001343 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001344 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001345 }
1346
Michal Vasko2e6defd2016-10-07 15:48:15 +02001347 cur_session->opts.server.last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001348
Michal Vasko428087d2016-01-14 16:04:28 +01001349 /* process RPC */
Radek Krejci93e80222016-10-03 13:34:25 +02001350 ret |= nc_server_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001351
Michal Vasko71090fc2016-05-24 16:37:28 +02001352 pthread_mutex_unlock(cur_session->ti_lock);
1353 if (cur_session->status != NC_STATUS_RUNNING) {
1354 ret |= NC_PSPOLL_SESSION_TERM;
1355 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1356 ret |= NC_PSPOLL_SESSION_ERROR;
1357 }
Michal Vasko428087d2016-01-14 16:04:28 +01001358 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001359
Michal Vaskoca4a2422016-02-02 12:17:14 +01001360 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001361
Michal Vasko48a63ed2016-03-01 09:48:21 +01001362finish:
1363 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001364 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001365 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001366}
1367
Michal Vaskod09eae62016-02-01 10:32:52 +01001368API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001369nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001370{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001371 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001372 uint16_t i;
1373 struct nc_session *session;
1374
Michal Vasko9a25e932016-02-01 10:36:42 +01001375 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001376 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001377 return;
1378 }
1379
Michal Vasko48a63ed2016-03-01 09:48:21 +01001380 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001381 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001382 return;
1383 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001384
Michal Vasko48a63ed2016-03-01 09:48:21 +01001385 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001386 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001387 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001388 }
1389 free(ps->sessions);
1390 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001391 ps->session_count = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +01001392 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001393 } else {
1394 for (i = 0; i < ps->session_count; ) {
1395 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1396 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001397 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001398 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001399 continue;
1400 }
1401
1402 ++i;
1403 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001404 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001405
1406 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001407 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001408}
1409
Radek Krejci53691be2016-02-22 13:58:37 +01001410#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001411
Michal Vaskoe2713da2016-08-22 16:06:40 +02001412API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001413nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001414{
Michal Vasko3031aae2016-01-27 16:07:18 +01001415 uint16_t i;
Michal Vasko9e036d52016-01-08 10:49:26 +01001416
Michal Vasko45e53ae2016-04-07 11:46:03 +02001417 if (!name) {
1418 ERRARG("name");
1419 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001420 }
1421
Michal Vasko51e514d2016-02-02 15:51:52 +01001422 /* WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001423 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001424
1425 /* check name uniqueness */
1426 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001427 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001428 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001429 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001430 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001431 return -1;
1432 }
1433 }
1434
Michal Vasko3031aae2016-01-27 16:07:18 +01001435 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001436 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001437 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001438 ERRMEM;
1439 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001440 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001441 return -1;
1442 }
1443 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001444 server_opts.endpts[server_opts.endpt_count - 1].ti = ti;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001445
Michal Vaskoe2713da2016-08-22 16:06:40 +02001446 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001447 if (!server_opts.binds) {
1448 ERRMEM;
1449 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001450 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001451 return -1;
1452 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001453
Michal Vasko2e6defd2016-10-07 15:48:15 +02001454 server_opts.binds[server_opts.endpt_count - 1].address = NULL;
1455 server_opts.binds[server_opts.endpt_count - 1].port = 0;
1456 server_opts.binds[server_opts.endpt_count - 1].sock = -1;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001457 server_opts.binds[server_opts.endpt_count - 1].pollin = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001458
1459 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001460#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001461 case NC_TI_LIBSSH:
1462 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1463 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) {
1464 ERRMEM;
1465 /* WRITE UNLOCK */
1466 pthread_rwlock_unlock(&server_opts.endpt_lock);
1467 return -1;
1468 }
1469 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods =
1470 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1471 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3;
1472 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10;
1473 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001474#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001475#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001476 case NC_TI_OPENSSL:
1477 server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1478 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) {
1479 ERRMEM;
1480 /* WRITE UNLOCK */
1481 pthread_rwlock_unlock(&server_opts.endpt_lock);
1482 return -1;
1483 }
1484 break;
1485#endif
1486 default:
1487 ERRINT;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001488 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001489 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001490 return -1;
1491 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001492
Michal Vasko2e6defd2016-10-07 15:48:15 +02001493 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001494
Michal Vasko3031aae2016-01-27 16:07:18 +01001495 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001496 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001497
Michal Vasko9e036d52016-01-08 10:49:26 +01001498 return 0;
1499}
1500
Michal Vasko3031aae2016-01-27 16:07:18 +01001501int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001502nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port)
Michal Vaskoda514772016-02-01 11:32:01 +01001503{
1504 struct nc_endpt *endpt;
1505 struct nc_bind *bind = NULL;
1506 uint16_t i;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001507 int sock = -1, set_addr;
Michal Vaskoda514772016-02-01 11:32:01 +01001508
Michal Vasko45e53ae2016-04-07 11:46:03 +02001509 if (!endpt_name) {
1510 ERRARG("endpt_name");
1511 return -1;
1512 } else if ((!address && !port) || (address && port)) {
1513 ERRARG("address and port");
1514 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001515 }
1516
Michal Vaskoe2713da2016-08-22 16:06:40 +02001517 if (address) {
1518 set_addr = 1;
1519 } else {
1520 set_addr = 0;
1521 }
1522
Michal Vasko51e514d2016-02-02 15:51:52 +01001523 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001524 endpt = nc_server_endpt_lock(endpt_name, 0, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001525 if (!endpt) {
1526 return -1;
1527 }
1528
Michal Vaskoe2713da2016-08-22 16:06:40 +02001529 bind = &server_opts.binds[i];
Michal Vaskoda514772016-02-01 11:32:01 +01001530
Michal Vaskoe2713da2016-08-22 16:06:40 +02001531 if (set_addr) {
1532 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001533 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001534 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001535 }
1536
Michal Vaskoe2713da2016-08-22 16:06:40 +02001537 /* we have all the information we need to create a listening socket */
1538 if (address && port) {
1539 /* create new socket, close the old one */
1540 sock = nc_sock_listen(address, port);
1541 if (sock == -1) {
1542 goto fail;
1543 }
1544
1545 if (bind->sock > -1) {
1546 close(bind->sock);
1547 }
1548 bind->sock = sock;
1549 } /* else we are just setting address or port */
1550
1551 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001552 lydict_remove(server_opts.ctx, bind->address);
1553 bind->address = lydict_insert(server_opts.ctx, address, 0);
1554 } else {
1555 bind->port = port;
1556 }
1557
Michal Vaskoe2713da2016-08-22 16:06:40 +02001558 if (sock > -1) {
1559#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001560 VRB("Listening on %s:%u for %s connections.", address, port, (endpt->ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001561#elif defined(NC_ENABLED_SSH)
1562 VRB("Listening on %s:%u for SSH connections.", address, port);
1563#else
1564 VRB("Listening on %s:%u for TLS connections.", address, port);
1565#endif
1566 }
1567
Michal Vasko51e514d2016-02-02 15:51:52 +01001568 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001569 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001570 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001571
1572fail:
1573 /* UNLOCK */
1574 nc_server_endpt_unlock(endpt);
1575 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001576}
1577
Michal Vaskoe2713da2016-08-22 16:06:40 +02001578API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001579nc_server_endpt_set_address(const char *endpt_name, const char *address)
1580{
1581 return nc_server_endpt_set_address_port(endpt_name, address, 0);
1582}
1583
1584API int
1585nc_server_endpt_set_port(const char *endpt_name, uint16_t port)
1586{
1587 return nc_server_endpt_set_address_port(endpt_name, NULL, port);
1588}
1589
1590API int
Michal Vasko59050372016-11-22 14:33:55 +01001591nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001592{
1593 uint32_t i;
1594 int ret = -1;
1595
Michal Vasko3031aae2016-01-27 16:07:18 +01001596 /* WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001597 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001598
Michal Vasko59050372016-11-22 14:33:55 +01001599 if (!name && !ti) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001600 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001601 for (i = 0; i < server_opts.endpt_count; ++i) {
1602 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001603 pthread_mutex_destroy(&server_opts.endpts[i].lock);
1604 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001605#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001606 case NC_TI_LIBSSH:
1607 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1608 free(server_opts.endpts[i].opts.ssh);
1609 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001610#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001611#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001612 case NC_TI_OPENSSL:
1613 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1614 free(server_opts.endpts[i].opts.tls);
1615 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001616#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001617 default:
1618 ERRINT;
1619 /* won't get here ...*/
1620 break;
1621 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001622 ret = 0;
1623 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001624 free(server_opts.endpts);
1625 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001626
1627 /* remove all binds */
1628 for (i = 0; i < server_opts.endpt_count; ++i) {
1629 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1630 if (server_opts.binds[i].sock > -1) {
1631 close(server_opts.binds[i].sock);
1632 }
1633 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001634 free(server_opts.binds);
1635 server_opts.binds = NULL;
1636
Michal Vasko3031aae2016-01-27 16:07:18 +01001637 server_opts.endpt_count = 0;
1638
Michal Vasko1a38c862016-01-15 15:50:07 +01001639 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001640 /* remove one endpoint with bind(s) or all endpoints using one transport protocol */
Michal Vasko3031aae2016-01-27 16:07:18 +01001641 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001642 if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001643 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001644 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001645 pthread_mutex_destroy(&server_opts.endpts[i].lock);
1646 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001647#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001648 case NC_TI_LIBSSH:
1649 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1650 free(server_opts.endpts[i].opts.ssh);
1651 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001652#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001653#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001654 case NC_TI_OPENSSL:
1655 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1656 free(server_opts.endpts[i].opts.tls);
1657 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001658#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001659 default:
1660 ERRINT;
1661 break;
1662 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001663
Michal Vaskoe2713da2016-08-22 16:06:40 +02001664 /* remove bind(s) */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001665 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1666 if (server_opts.binds[i].sock > -1) {
1667 close(server_opts.binds[i].sock);
1668 }
1669
1670 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001671 --server_opts.endpt_count;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001672 if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001673 free(server_opts.binds);
1674 server_opts.binds = NULL;
1675 free(server_opts.endpts);
1676 server_opts.endpts = NULL;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001677 } else if (i < server_opts.endpt_count) {
1678 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1679 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoc0256492016-02-02 12:19:06 +01001680 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001681
1682 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01001683 if (name) {
1684 break;
1685 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001686 }
1687 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001688 }
1689
Michal Vasko3031aae2016-01-27 16:07:18 +01001690 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001691 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001692
Michal Vasko9e036d52016-01-08 10:49:26 +01001693 return ret;
1694}
1695
Michal Vasko71090fc2016-05-24 16:37:28 +02001696API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001697nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001698{
Michal Vasko71090fc2016-05-24 16:37:28 +02001699 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001700 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001701 char *host = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001702 uint16_t port, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001703
Michal Vasko45e53ae2016-04-07 11:46:03 +02001704 if (!server_opts.ctx) {
1705 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001706 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001707 } else if (!session) {
1708 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001709 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001710 }
1711
Michal Vasko51e514d2016-02-02 15:51:52 +01001712 /* we have to hold WRITE for the whole time, since there is not
1713 * a way of downgrading the lock to READ */
1714 /* WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001715 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001716
1717 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001718 ERR("No endpoints to accept sessions on.");
Michal Vasko51e514d2016-02-02 15:51:52 +01001719 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001720 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001721 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001722 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001723
Michal Vaskoe2713da2016-08-22 16:06:40 +02001724 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001725 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001726 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001727 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001728 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001729 if (!ret) {
1730 return NC_MSG_WOULDBLOCK;
1731 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001732 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001733 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001734 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001735
Michal Vasko1a38c862016-01-15 15:50:07 +01001736 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001737 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001738 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001739 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001740 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001741 msgtype = NC_MSG_ERROR;
1742 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001743 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001744 (*session)->status = NC_STATUS_STARTING;
1745 (*session)->side = NC_SERVER;
1746 (*session)->ctx = server_opts.ctx;
1747 (*session)->flags = NC_SESSION_SHAREDCTX;
1748 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1749 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001750
1751 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001752 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1753 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001754 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001755 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001756 msgtype = NC_MSG_ERROR;
1757 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001758 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001759 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001760
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001761 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001762#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001763 if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) {
1764 (*session)->data = server_opts.endpts[bind_idx].opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01001765 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001766 if (ret < 0) {
1767 msgtype = NC_MSG_ERROR;
1768 goto cleanup;
1769 } else if (!ret) {
1770 msgtype = NC_MSG_WOULDBLOCK;
1771 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001772 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001773 } else
1774#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001775#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001776 if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) {
1777 (*session)->data = server_opts.endpts[bind_idx].opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01001778 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001779 if (ret < 0) {
1780 msgtype = NC_MSG_ERROR;
1781 goto cleanup;
1782 } else if (!ret) {
1783 msgtype = NC_MSG_WOULDBLOCK;
1784 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001785 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001786 } else
1787#endif
1788 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001789 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001790 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001791 msgtype = NC_MSG_ERROR;
1792 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001793 }
1794
Michal Vasko2cc4c682016-03-01 09:16:48 +01001795 (*session)->data = NULL;
1796
Michal Vasko51e514d2016-02-02 15:51:52 +01001797 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001798 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001799
Michal Vaskob48aa812016-01-18 14:13:09 +01001800 /* assign new SID atomically */
1801 /* LOCK */
1802 pthread_spin_lock(&server_opts.sid_lock);
1803 (*session)->id = server_opts.new_session_id++;
1804 /* UNLOCK */
1805 pthread_spin_unlock(&server_opts.sid_lock);
1806
Michal Vasko9e036d52016-01-08 10:49:26 +01001807 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001808 msgtype = nc_handshake(*session);
1809 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001810 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001811 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001812 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001813 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001814 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001815 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001816
Michal Vasko71090fc2016-05-24 16:37:28 +02001817 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001818
Michal Vasko71090fc2016-05-24 16:37:28 +02001819cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001820 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001821 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001822
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001823 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001824 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001825 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001826}
1827
Michal Vasko2e6defd2016-10-07 15:48:15 +02001828API int
1829nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti)
1830{
1831 uint16_t i;
1832
1833 if (!name) {
1834 ERRARG("name");
1835 return -1;
1836 } else if (!ti) {
1837 ERRARG("ti");
1838 return -1;
1839 }
1840
1841 /* WRITE LOCK */
1842 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1843
1844 /* check name uniqueness */
1845 for (i = 0; i < server_opts.ch_client_count; ++i) {
1846 if (!strcmp(server_opts.ch_clients[i].name, name)) {
1847 ERR("Call Home client \"%s\" already exists.", name);
1848 /* WRITE UNLOCK */
1849 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1850 return -1;
1851 }
1852 }
1853
1854 ++server_opts.ch_client_count;
1855 server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients);
1856 if (!server_opts.ch_clients) {
1857 ERRMEM;
1858 /* WRITE UNLOCK */
1859 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1860 return -1;
1861 }
1862 server_opts.ch_clients[server_opts.ch_client_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1863 server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001864 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL;
1865 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpt_count = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001866
1867 switch (ti) {
1868#ifdef NC_ENABLED_SSH
1869 case NC_TI_LIBSSH:
1870 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1871 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) {
1872 ERRMEM;
1873 /* WRITE UNLOCK */
1874 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1875 return -1;
1876 }
1877 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods =
1878 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1879 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3;
1880 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10;
1881 break;
1882#endif
1883#ifdef NC_ENABLED_TLS
1884 case NC_TI_OPENSSL:
1885 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1886 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) {
1887 ERRMEM;
1888 /* WRITE UNLOCK */
1889 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1890 return -1;
1891 }
1892 break;
1893#endif
1894 default:
1895 ERRINT;
1896 /* WRITE UNLOCK */
1897 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1898 return -1;
1899 }
1900
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001901 server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0;
1902
Michal Vasko2e6defd2016-10-07 15:48:15 +02001903 /* set CH default options */
1904 server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED;
1905 server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3;
1906
1907 pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL);
1908
1909 /* WRITE UNLOCK */
1910 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1911
1912 return 0;
1913}
1914
1915API int
Michal Vasko59050372016-11-22 14:33:55 +01001916nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001917{
1918 uint16_t i, j;
1919 int ret = -1;
1920
1921 /* WRITE LOCK */
1922 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1923
Michal Vasko59050372016-11-22 14:33:55 +01001924 if (!name && !ti) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001925 /* remove all CH clients */
1926 for (i = 0; i < server_opts.ch_client_count; ++i) {
1927 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1928
1929 /* remove all endpoints */
1930 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
1931 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
1932 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
1933 }
1934 free(server_opts.ch_clients[i].ch_endpts);
1935
1936 switch (server_opts.ch_clients[i].ti) {
1937#ifdef NC_ENABLED_SSH
1938 case NC_TI_LIBSSH:
1939 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1940 free(server_opts.ch_clients[i].opts.ssh);
1941 break;
1942#endif
1943#ifdef NC_ENABLED_TLS
1944 case NC_TI_OPENSSL:
1945 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
1946 free(server_opts.ch_clients[i].opts.tls);
1947 break;
1948#endif
1949 default:
1950 ERRINT;
1951 /* won't get here ...*/
1952 break;
1953 }
1954
1955 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
1956
1957 ret = 0;
1958 }
1959 free(server_opts.ch_clients);
1960 server_opts.ch_clients = NULL;
1961
1962 server_opts.ch_client_count = 0;
1963
1964 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001965 /* remove one client with endpoint(s) or all clients using one protocol */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001966 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001967 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 +02001968 /* remove endpt */
1969 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1970
1971 switch (server_opts.ch_clients[i].ti) {
1972#ifdef NC_ENABLED_SSH
1973 case NC_TI_LIBSSH:
1974 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1975 free(server_opts.ch_clients[i].opts.ssh);
1976 break;
1977#endif
1978#ifdef NC_ENABLED_TLS
1979 case NC_TI_OPENSSL:
1980 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
1981 free(server_opts.ch_clients[i].opts.tls);
1982 break;
1983#endif
1984 default:
1985 ERRINT;
1986 break;
1987 }
1988
1989 /* remove all endpoints */
1990 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
1991 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
1992 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
1993 }
1994 free(server_opts.ch_clients[i].ch_endpts);
1995
1996 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
1997
1998 /* move last client and endpoint(s) to the empty space */
1999 --server_opts.ch_client_count;
2000 if (i < server_opts.ch_client_count) {
2001 memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count],
2002 sizeof *server_opts.ch_clients);
2003 } else if (!server_opts.ch_client_count) {
2004 free(server_opts.ch_clients);
2005 server_opts.ch_clients = NULL;
2006 }
2007
2008 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01002009 if (name) {
2010 break;
2011 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002012 }
2013 }
2014 }
2015
2016 /* WRITE UNLOCK */
2017 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2018
2019 return ret;
2020}
2021
2022API int
2023nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name)
2024{
2025 uint16_t i;
2026 struct nc_ch_client *client;
2027
2028 if (!client_name) {
2029 ERRARG("client_name");
2030 return -1;
2031 } else if (!endpt_name) {
2032 ERRARG("endpt_name");
2033 return -1;
2034 }
2035
2036 /* LOCK */
2037 client = nc_server_ch_client_lock(client_name, 0, NULL);
2038 if (!client) {
2039 return -1;
2040 }
2041
2042 for (i = 0; i < client->ch_endpt_count; ++i) {
2043 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2044 ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name);
2045 /* UNLOCK */
2046 nc_server_ch_client_unlock(client);
2047 return -1;
2048 }
2049 }
2050
2051 ++client->ch_endpt_count;
2052 client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts);
2053 if (!client->ch_endpts) {
2054 ERRMEM;
2055 /* UNLOCK */
2056 nc_server_ch_client_unlock(client);
2057 return -1;
2058 }
2059
2060 client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0);
2061 client->ch_endpts[client->ch_endpt_count - 1].address = NULL;
2062 client->ch_endpts[client->ch_endpt_count - 1].port = 0;
2063
2064 /* UNLOCK */
2065 nc_server_ch_client_unlock(client);
2066
2067 return 0;
2068}
2069
2070API int
2071nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name)
2072{
2073 uint16_t i;
2074 int ret = -1;
2075 struct nc_ch_client *client;
2076
2077 if (!client_name) {
2078 ERRARG("client_name");
2079 return -1;
2080 }
2081
2082 /* LOCK */
2083 client = nc_server_ch_client_lock(client_name, 0, NULL);
2084 if (!client) {
2085 return -1;
2086 }
2087
2088 if (!endpt_name) {
2089 /* remove all endpoints */
2090 for (i = 0; i < client->ch_endpt_count; ++i) {
2091 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2092 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2093 }
2094 free(client->ch_endpts);
2095 client->ch_endpts = NULL;
2096 client->ch_endpt_count = 0;
2097
2098 ret = 0;
2099 } else {
2100 for (i = 0; i < client->ch_endpt_count; ++i) {
2101 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2102 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2103 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002104
Michal Vasko4f921012016-10-20 14:07:45 +02002105 /* move last endpoint to the empty space */
2106 --client->ch_endpt_count;
2107 if (i < client->ch_endpt_count) {
2108 memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts);
2109 } else if (!server_opts.ch_client_count) {
2110 free(server_opts.ch_clients);
2111 server_opts.ch_clients = NULL;
2112 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002113
Michal Vasko4f921012016-10-20 14:07:45 +02002114 ret = 0;
2115 break;
2116 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002117 }
2118 }
2119
2120 /* UNLOCK */
2121 nc_server_ch_client_unlock(client);
2122
2123 return ret;
2124}
2125
2126API int
2127nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address)
2128{
2129 uint16_t i;
2130 int ret = -1;
2131 struct nc_ch_client *client;
2132
2133 if (!client_name) {
2134 ERRARG("client_name");
2135 return -1;
2136 } else if (!endpt_name) {
2137 ERRARG("endpt_name");
2138 return -1;
2139 } else if (!address) {
2140 ERRARG("address");
2141 return -1;
2142 }
2143
2144 /* LOCK */
2145 client = nc_server_ch_client_lock(client_name, 0, NULL);
2146 if (!client) {
2147 return -1;
2148 }
2149
2150 for (i = 0; i < client->ch_endpt_count; ++i) {
2151 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2152 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2153 client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0);
2154
2155 ret = 0;
2156 break;
2157 }
2158 }
2159
2160 /* UNLOCK */
2161 nc_server_ch_client_unlock(client);
2162
2163 if (ret == -1) {
2164 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2165 }
2166
2167 return ret;
2168}
2169
2170API int
2171nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port)
2172{
2173 uint16_t i;
2174 int ret = -1;
2175 struct nc_ch_client *client;
2176
2177 if (!client_name) {
2178 ERRARG("client_name");
2179 return -1;
2180 } else if (!endpt_name) {
2181 ERRARG("endpt_name");
2182 return -1;
2183 } else if (!port) {
2184 ERRARG("port");
2185 return -1;
2186 }
2187
2188 /* LOCK */
2189 client = nc_server_ch_client_lock(client_name, 0, NULL);
2190 if (!client) {
2191 return -1;
2192 }
2193
2194 for (i = 0; i < client->ch_endpt_count; ++i) {
2195 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2196 client->ch_endpts[i].port = port;
2197
2198 ret = 0;
2199 break;
2200 }
2201 }
2202
2203 /* UNLOCK */
2204 nc_server_ch_client_unlock(client);
2205
2206 if (ret == -1) {
2207 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2208 }
2209
2210 return ret;
2211}
2212
2213API int
2214nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type)
2215{
2216 struct nc_ch_client *client;
2217
2218 if (!client_name) {
2219 ERRARG("client_name");
2220 return -1;
2221 } else if (!conn_type) {
2222 ERRARG("conn_type");
2223 return -1;
2224 }
2225
2226 /* LOCK */
2227 client = nc_server_ch_client_lock(client_name, 0, NULL);
2228 if (!client) {
2229 return -1;
2230 }
2231
2232 if (client->conn_type != conn_type) {
2233 client->conn_type = conn_type;
2234
2235 /* set default options */
2236 switch (conn_type) {
2237 case NC_CH_PERSIST:
2238 client->conn.persist.idle_timeout = 86400;
2239 client->conn.persist.ka_max_wait = 30;
2240 client->conn.persist.ka_max_attempts = 3;
2241 break;
2242 case NC_CH_PERIOD:
2243 client->conn.period.idle_timeout = 300;
2244 client->conn.period.reconnect_timeout = 60;
2245 break;
2246 default:
2247 ERRINT;
2248 break;
2249 }
2250 }
2251
2252 /* UNLOCK */
2253 nc_server_ch_client_unlock(client);
2254
2255 return 0;
2256}
2257
2258API int
2259nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout)
2260{
2261 struct nc_ch_client *client;
2262
2263 if (!client_name) {
2264 ERRARG("client_name");
2265 return -1;
2266 }
2267
2268 /* LOCK */
2269 client = nc_server_ch_client_lock(client_name, 0, NULL);
2270 if (!client) {
2271 return -1;
2272 }
2273
2274 if (client->conn_type != NC_CH_PERSIST) {
2275 ERR("Call Home client \"%s\" is not of persistent connection type.");
2276 /* UNLOCK */
2277 nc_server_ch_client_unlock(client);
2278 return -1;
2279 }
2280
2281 client->conn.persist.idle_timeout = idle_timeout;
2282
2283 /* UNLOCK */
2284 nc_server_ch_client_unlock(client);
2285
2286 return 0;
2287}
2288
2289API int
2290nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait)
2291{
2292 struct nc_ch_client *client;
2293
2294 if (!client_name) {
2295 ERRARG("client_name");
2296 return -1;
2297 } else if (!max_wait) {
2298 ERRARG("max_wait");
2299 return -1;
2300 }
2301
2302 /* LOCK */
2303 client = nc_server_ch_client_lock(client_name, 0, NULL);
2304 if (!client) {
2305 return -1;
2306 }
2307
2308 if (client->conn_type != NC_CH_PERSIST) {
2309 ERR("Call Home client \"%s\" is not of persistent connection type.");
2310 /* UNLOCK */
2311 nc_server_ch_client_unlock(client);
2312 return -1;
2313 }
2314
2315 client->conn.persist.ka_max_wait = max_wait;
2316
2317 /* UNLOCK */
2318 nc_server_ch_client_unlock(client);
2319
2320 return 0;
2321}
2322
2323API int
2324nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts)
2325{
2326 struct nc_ch_client *client;
2327
2328 if (!client_name) {
2329 ERRARG("client_name");
2330 return -1;
2331 }
2332
2333 /* LOCK */
2334 client = nc_server_ch_client_lock(client_name, 0, NULL);
2335 if (!client) {
2336 return -1;
2337 }
2338
2339 if (client->conn_type != NC_CH_PERSIST) {
2340 ERR("Call Home client \"%s\" is not of persistent connection type.");
2341 /* UNLOCK */
2342 nc_server_ch_client_unlock(client);
2343 return -1;
2344 }
2345
2346 client->conn.persist.ka_max_attempts = max_attempts;
2347
2348 /* UNLOCK */
2349 nc_server_ch_client_unlock(client);
2350
2351 return 0;
2352}
2353
2354API int
2355nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout)
2356{
2357 struct nc_ch_client *client;
2358
2359 if (!client_name) {
2360 ERRARG("client_name");
2361 return -1;
2362 }
2363
2364 /* LOCK */
2365 client = nc_server_ch_client_lock(client_name, 0, NULL);
2366 if (!client) {
2367 return -1;
2368 }
2369
2370 if (client->conn_type != NC_CH_PERIOD) {
2371 ERR("Call Home client \"%s\" is not of periodic connection type.");
2372 /* UNLOCK */
2373 nc_server_ch_client_unlock(client);
2374 return -1;
2375 }
2376
2377 client->conn.period.idle_timeout = idle_timeout;
2378
2379 /* UNLOCK */
2380 nc_server_ch_client_unlock(client);
2381
2382 return 0;
2383}
2384
2385API int
2386nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout)
2387{
2388 struct nc_ch_client *client;
2389
2390 if (!client_name) {
2391 ERRARG("client_name");
2392 return -1;
2393 } else if (!reconnect_timeout) {
2394 ERRARG("reconnect_timeout");
2395 return -1;
2396 }
2397
2398 /* LOCK */
2399 client = nc_server_ch_client_lock(client_name, 0, NULL);
2400 if (!client) {
2401 return -1;
2402 }
2403
2404 if (client->conn_type != NC_CH_PERIOD) {
2405 ERR("Call Home client \"%s\" is not of periodic connection type.");
2406 /* UNLOCK */
2407 nc_server_ch_client_unlock(client);
2408 return -1;
2409 }
2410
2411 client->conn.period.reconnect_timeout = reconnect_timeout;
2412
2413 /* UNLOCK */
2414 nc_server_ch_client_unlock(client);
2415
2416 return 0;
2417}
2418
2419API int
2420nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with)
2421{
2422 struct nc_ch_client *client;
2423
2424 if (!client_name) {
2425 ERRARG("client_name");
2426 return -1;
2427 }
2428
2429 /* LOCK */
2430 client = nc_server_ch_client_lock(client_name, 0, NULL);
2431 if (!client) {
2432 return -1;
2433 }
2434
2435 client->start_with = start_with;
2436
2437 /* UNLOCK */
2438 nc_server_ch_client_unlock(client);
2439
2440 return 0;
2441}
2442
2443API int
2444nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts)
2445{
2446 struct nc_ch_client *client;
2447
2448 if (!client_name) {
2449 ERRARG("client_name");
2450 return -1;
2451 } else if (!max_attempts) {
2452 ERRARG("max_attempts");
2453 return -1;
2454 }
2455
2456 /* LOCK */
2457 client = nc_server_ch_client_lock(client_name, 0, NULL);
2458 if (!client) {
2459 return -1;
2460 }
2461
2462 client->max_attempts = max_attempts;
2463
2464 /* UNLOCK */
2465 nc_server_ch_client_unlock(client);
2466
2467 return 0;
2468}
2469
2470/* client lock is expected to be held */
2471static NC_MSG_TYPE
2472nc_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 +01002473{
Michal Vasko71090fc2016-05-24 16:37:28 +02002474 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002475 int sock, ret;
2476
Michal Vasko2e6defd2016-10-07 15:48:15 +02002477 sock = nc_sock_connect(endpt->address, endpt->port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01002478 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02002479 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002480 }
2481
2482 *session = calloc(1, sizeof **session);
2483 if (!(*session)) {
2484 ERRMEM;
2485 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002486 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002487 }
2488 (*session)->status = NC_STATUS_STARTING;
2489 (*session)->side = NC_SERVER;
2490 (*session)->ctx = server_opts.ctx;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002491 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002492 (*session)->host = lydict_insert(server_opts.ctx, endpt->address, 0);
2493 (*session)->port = endpt->port;
Michal Vaskob05053d2016-01-22 16:12:06 +01002494
2495 /* transport lock */
2496 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
2497 if (!(*session)->ti_lock) {
2498 ERRMEM;
2499 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002500 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002501 goto fail;
2502 }
2503 pthread_mutex_init((*session)->ti_lock, NULL);
2504
2505 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01002506#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002507 if (client->ti == NC_TI_LIBSSH) {
2508 (*session)->data = client->opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01002509 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002510 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002511
Michal Vasko71090fc2016-05-24 16:37:28 +02002512 if (ret < 0) {
2513 msgtype = NC_MSG_ERROR;
2514 goto fail;
2515 } else if (!ret) {
2516 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002517 goto fail;
2518 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002519 } else
2520#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002521#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002522 if (client->ti == NC_TI_OPENSSL) {
2523 (*session)->data = client->opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01002524 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002525 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002526
Michal Vasko71090fc2016-05-24 16:37:28 +02002527 if (ret < 0) {
2528 msgtype = NC_MSG_ERROR;
2529 goto fail;
2530 } else if (!ret) {
2531 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002532 goto fail;
2533 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002534 } else
2535#endif
2536 {
Michal Vaskob05053d2016-01-22 16:12:06 +01002537 ERRINT;
2538 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002539 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002540 goto fail;
2541 }
2542
2543 /* assign new SID atomically */
2544 /* LOCK */
2545 pthread_spin_lock(&server_opts.sid_lock);
2546 (*session)->id = server_opts.new_session_id++;
2547 /* UNLOCK */
2548 pthread_spin_unlock(&server_opts.sid_lock);
2549
2550 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02002551 msgtype = nc_handshake(*session);
2552 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01002553 goto fail;
2554 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002555 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002556 (*session)->status = NC_STATUS_RUNNING;
2557
Michal Vasko71090fc2016-05-24 16:37:28 +02002558 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002559
2560fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002561 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002562 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002563 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002564}
2565
Michal Vasko2e6defd2016-10-07 15:48:15 +02002566/* ms */
2567#define NC_CH_NO_ENDPT_WAIT 1000
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002568#define NC_CH_ENDPT_FAIL_WAIT 1000
Michal Vasko2e6defd2016-10-07 15:48:15 +02002569
2570struct nc_ch_client_thread_arg {
2571 char *client_name;
2572 void (*session_clb)(const char *client_name, struct nc_session *new_session);
2573};
2574
2575static struct nc_ch_client *
2576nc_server_ch_client_with_endpt_lock(const char *name)
2577{
2578 struct nc_ch_client *client;
2579
2580 while (1) {
2581 /* LOCK */
2582 client = nc_server_ch_client_lock(name, 0, NULL);
2583 if (!client) {
2584 return NULL;
2585 }
2586 if (client->ch_endpt_count) {
2587 return client;
2588 }
2589 /* no endpoints defined yet */
2590
2591 /* UNLOCK */
2592 nc_server_ch_client_unlock(client);
2593
2594 usleep(NC_CH_NO_ENDPT_WAIT * 1000);
2595 }
2596
2597 return NULL;
2598}
2599
2600static int
2601nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data)
2602{
2603 int ret;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002604 uint32_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002605 struct timespec ts;
2606 struct nc_ch_client *client;
2607
2608 /* session created, initialize condition */
2609 session->opts.server.ch_lock = malloc(sizeof *session->opts.server.ch_lock);
2610 session->opts.server.ch_cond = malloc(sizeof *session->opts.server.ch_cond);
2611 if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) {
2612 ERRMEM;
2613 nc_session_free(session, NULL);
2614 return -1;
2615 }
2616 pthread_mutex_init(session->opts.server.ch_lock, NULL);
2617 pthread_cond_init(session->opts.server.ch_cond, NULL);
2618
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002619 session->flags |= NC_SESSION_CALLHOME;
2620
Michal Vasko2e6defd2016-10-07 15:48:15 +02002621 /* CH LOCK */
2622 pthread_mutex_lock(session->opts.server.ch_lock);
2623
2624 /* give the session to the user */
2625 data->session_clb(data->client_name, session);
2626
2627 do {
2628 nc_gettimespec(&ts);
2629 ts.tv_nsec += NC_CH_NO_ENDPT_WAIT * 1000000L;
2630 if (ts.tv_nsec > 1000000000L) {
2631 ts.tv_sec += ts.tv_nsec / 1000000000L;
2632 ts.tv_nsec %= 1000000000L;
2633 }
2634
2635 ret = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts);
2636 if (ret && (ret != ETIMEDOUT)) {
2637 ERR("Pthread condition timedwait failed (%s).", strerror(ret));
2638 goto ch_client_remove;
2639 }
2640
2641 /* check whether the client was not removed */
2642 /* LOCK */
2643 client = nc_server_ch_client_lock(data->client_name, 0, NULL);
2644 if (!client) {
2645 /* client was removed, finish thread */
2646 VRB("Call Home client \"%s\" removed, but an established session will not be terminated.",
2647 data->client_name);
2648 goto ch_client_remove;
2649 }
2650
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002651 if (client->conn_type == NC_CH_PERSIST) {
2652 /* TODO keep-alives */
2653 idle_timeout = client->conn.persist.idle_timeout;
2654 } else {
2655 idle_timeout = client->conn.period.idle_timeout;
2656 }
2657
2658 /* TODO only for sessions without subscriptions */
2659 if (idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) {
2660 VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id);
2661 session->status = NC_STATUS_INVALID;
2662 session->term_reason = NC_SESSION_TERM_TIMEOUT;
2663 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002664
2665 /* UNLOCK */
2666 nc_server_ch_client_unlock(client);
2667
2668 } while (session->status == NC_STATUS_RUNNING);
2669
2670 /* CH UNLOCK */
2671 pthread_mutex_unlock(session->opts.server.ch_lock);
2672
2673 return 0;
2674
2675ch_client_remove:
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002676 /* make the session a standard one */
2677 pthread_cond_destroy(session->opts.server.ch_cond);
2678 free(session->opts.server.ch_cond);
2679 session->opts.server.ch_cond = NULL;
2680
2681 session->flags &= ~NC_SESSION_CALLHOME;
2682
Michal Vasko2e6defd2016-10-07 15:48:15 +02002683 /* CH UNLOCK */
2684 pthread_mutex_unlock(session->opts.server.ch_lock);
2685
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002686 pthread_mutex_destroy(session->opts.server.ch_lock);
2687 free(session->opts.server.ch_lock);
2688 session->opts.server.ch_lock = NULL;
2689
Michal Vasko2e6defd2016-10-07 15:48:15 +02002690 return 1;
2691}
2692
2693static void *
2694nc_ch_client_thread(void *arg)
2695{
2696 struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg;
2697 NC_MSG_TYPE msgtype;
2698 uint8_t cur_attempts = 0;
2699 uint16_t i;
2700 char *cur_endpt_name;
2701 struct nc_ch_endpt *cur_endpt;
2702 struct nc_session *session;
2703 struct nc_ch_client *client;
2704
2705 /* LOCK */
2706 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2707 if (!client) {
2708 goto cleanup;
2709 }
2710
2711 cur_endpt = &client->ch_endpts[0];
2712 cur_endpt_name = strdup(cur_endpt->name);
2713
Michal Vasko29af44b2016-10-13 10:59:55 +02002714 VRB("Call Home client \"%s\" connecting...", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002715 while (1) {
2716 msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session);
2717
2718 if (msgtype == NC_MSG_HELLO) {
2719 /* UNLOCK */
2720 nc_server_ch_client_unlock(client);
2721
Michal Vasko29af44b2016-10-13 10:59:55 +02002722 VRB("Call Home client \"%s\" session %u established.", data->client_name, session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002723 if (nc_server_ch_client_thread_session_cond_wait(session, data)) {
2724 goto cleanup;
2725 }
Michal Vasko29af44b2016-10-13 10:59:55 +02002726 VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002727
2728 /* LOCK */
2729 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2730 if (!client) {
2731 goto cleanup;
2732 }
2733
2734 /* session changed status -> it was disconnected for whatever reason,
2735 * persistent connection immediately tries to reconnect, periodic waits some first */
2736 if (client->conn_type == NC_CH_PERIOD) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002737 /* UNLOCK */
2738 nc_server_ch_client_unlock(client);
2739
2740 /* TODO wake up sometimes to check for new notifications */
2741 usleep(client->conn.period.reconnect_timeout * 60 * 1000000);
2742
2743 /* LOCK */
2744 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2745 if (!client) {
2746 goto cleanup;
2747 }
2748 }
2749
2750 /* set next endpoint to try */
2751 if (client->start_with == NC_CH_FIRST_LISTED) {
2752 cur_endpt = &client->ch_endpts[0];
2753 free(cur_endpt_name);
2754 cur_endpt_name = strdup(cur_endpt->name);
2755 } /* else we keep the current one */
2756 } else {
Michal Vasko6bb116b2016-10-26 13:53:46 +02002757 /* UNLOCK */
2758 nc_server_ch_client_unlock(client);
2759
Michal Vasko2e6defd2016-10-07 15:48:15 +02002760 /* session was not created */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002761 usleep(NC_CH_ENDPT_FAIL_WAIT * 1000);
2762
Michal Vasko6bb116b2016-10-26 13:53:46 +02002763 /* LOCK */
2764 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2765 if (!client) {
2766 goto cleanup;
2767 }
2768
Michal Vasko2e6defd2016-10-07 15:48:15 +02002769 ++cur_attempts;
2770 if (cur_attempts == client->max_attempts) {
2771 for (i = 0; i < client->ch_endpt_count; ++i) {
2772 if (!strcmp(client->ch_endpts[i].name, cur_endpt_name)) {
2773 break;
2774 }
2775 }
2776 if (i < client->ch_endpt_count - 1) {
2777 /* just go to the next endpoint */
2778 cur_endpt = &client->ch_endpts[i + 1];
2779 free(cur_endpt_name);
2780 cur_endpt_name = strdup(cur_endpt->name);
2781 } else {
2782 /* cur_endpoint was removed or is the last, either way start with the first one */
2783 cur_endpt = &client->ch_endpts[0];
2784 free(cur_endpt_name);
2785 cur_endpt_name = strdup(cur_endpt->name);
2786 }
2787
2788 cur_attempts = 0;
2789 } /* else we keep the current one */
2790 }
2791 }
2792
2793cleanup:
2794 VRB("Call Home client \"%s\" thread exit.", data->client_name);
2795
2796 free(data->client_name);
2797 free(data);
2798 return NULL;
2799}
2800
2801API int
2802nc_connect_ch_client_dispatch(const char *client_name,
2803 void (*session_clb)(const char *client_name, struct nc_session *new_session)) {
2804 int ret;
2805 pthread_t tid;
2806 struct nc_ch_client_thread_arg *arg;
2807
2808 if (!client_name) {
2809 ERRARG("client_name");
2810 return -1;
2811 } else if (!session_clb) {
2812 ERRARG("session_clb");
2813 return -1;
2814 }
2815
2816 arg = malloc(sizeof *arg);
2817 if (!arg) {
2818 ERRMEM;
2819 return -1;
2820 }
2821 arg->client_name = strdup(client_name);
2822 if (!arg->client_name) {
2823 ERRMEM;
2824 free(arg);
2825 return -1;
2826 }
2827 arg->session_clb = session_clb;
2828
2829 ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg);
2830 if (ret) {
2831 ERR("Creating a new thread failed (%s).", strerror(ret));
2832 free(arg->client_name);
2833 free(arg);
2834 return -1;
2835 }
2836 /* the thread now manages arg */
2837
2838 pthread_detach(tid);
2839
2840 return 0;
2841}
2842
Radek Krejci53691be2016-02-22 13:58:37 +01002843#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02002844
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002845API time_t
2846nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02002847{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002848 if (!session || (session->side != NC_SERVER)) {
Michal Vaskof8352352016-05-24 09:11:36 +02002849 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002850 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02002851 }
2852
Michal Vasko2e6defd2016-10-07 15:48:15 +02002853 return session->opts.server.session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02002854}