blob: 405452350aafb44ef96e1395ea9af47ca41607b9 [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 Vasko41adf392017-01-17 10:39:04 +0100927 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child,
928 LYD_OPT_RPC | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS | LYD_OPT_STRICT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100929 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200930 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200931 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200932 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200933 nc_server_reply_free(reply);
934 if (ret == -1) {
935 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200936 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200937 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
938 } else {
939 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100940 }
Michal Vasko428087d2016-01-14 16:04:28 +0100941 (*rpc)->root = xml;
942 break;
943 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100944 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200945 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100946 goto error;
947 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100948 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200949 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100950 goto error;
951 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100952 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200953 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100954 goto error;
955 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200956 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100957 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
958 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200959 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100960 break;
961 }
962
Michal Vasko71090fc2016-05-24 16:37:28 +0200963 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100964
965error:
966 /* cleanup */
967 lyxml_free(server_opts.ctx, xml);
968
Michal Vasko71090fc2016-05-24 16:37:28 +0200969 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100970}
971
fanchanghu966f2de2016-07-21 02:28:57 -0400972API void
973nc_set_global_rpc_clb(nc_rpc_clb clb)
974{
975 global_rpc_clb = clb;
976}
977
Radek Krejci93e80222016-10-03 13:34:25 +0200978API NC_MSG_TYPE
979nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout)
980{
981 NC_MSG_TYPE result = NC_MSG_NOTIF;
982 int ret;
983
984 /* check parameters */
985 if (!session) {
986 ERRARG("session");
987 return NC_MSG_ERROR;
988 } else if (!notif || !notif->tree || !notif->eventtime) {
989 ERRARG("notif");
990 return NC_MSG_ERROR;
991 }
992
993 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
994 ret = nc_timedlock(session->ti_lock, timeout, __func__);
995 if (ret < 0) {
996 return NC_MSG_ERROR;
997 } else if (!ret) {
998 return NC_MSG_WOULDBLOCK;
999 }
1000
1001 ret = nc_write_msg(session, NC_MSG_NOTIF, notif);
1002 if (ret == -1) {
1003 ERR("Session %u: failed to write notification.", session->id);
1004 result = NC_MSG_ERROR;
1005 }
1006 pthread_mutex_unlock(session->ti_lock);
1007
1008 return result;
1009}
1010
Michal Vasko71090fc2016-05-24 16:37:28 +02001011/* must be called holding the session lock!
1012 * returns: NC_PSPOLL_ERROR,
1013 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
1014 * NC_PSPOLL_REPLY_ERROR,
1015 * 0
1016 */
1017static int
Radek Krejci93e80222016-10-03 13:34:25 +02001018nc_server_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001019{
1020 nc_rpc_clb clb;
1021 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +02001022 struct lys_node *rpc_act = NULL;
1023 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +02001024 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +01001025
Michal Vasko4a827e52016-03-03 10:59:00 +01001026 if (!rpc) {
1027 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001028 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +01001029 }
1030
Michal Vasko90e8e692016-07-13 12:27:57 +02001031 if (rpc->tree->schema->nodetype == LYS_RPC) {
1032 /* RPC */
1033 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -04001034 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001035 /* action */
1036 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
1037 if (elem->schema->nodetype == LYS_ACTION) {
1038 rpc_act = elem->schema;
1039 break;
1040 }
1041 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -04001042 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001043 if (!rpc_act) {
1044 ERRINT;
1045 return NC_PSPOLL_ERROR;
1046 }
1047 }
1048
1049 if (!rpc_act->priv) {
1050 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +01001051 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +01001052 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001053 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +01001054 reply = clb(rpc->tree, session);
1055 }
1056
1057 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001058 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +01001059 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001060 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
1061 if (reply->type == NC_RPL_ERROR) {
1062 ret |= NC_PSPOLL_REPLY_ERROR;
1063 }
1064 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +01001065
Michal Vasko71090fc2016-05-24 16:37:28 +02001066 if (r == -1) {
1067 ERR("Session %u: failed to write reply.", session->id);
1068 ret |= NC_PSPOLL_ERROR;
1069 }
Michal Vasko428087d2016-01-14 16:04:28 +01001070
1071 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1072 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1073 session->status = NC_STATUS_INVALID;
1074 }
1075
Michal Vasko71090fc2016-05-24 16:37:28 +02001076 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001077}
1078
1079API int
Michal Vasko71090fc2016-05-24 16:37:28 +02001080nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +01001081{
Michal Vasko99f251b2017-01-11 11:31:46 +01001082 int ret, r, poll_ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +02001083 uint8_t q_id;
Michal Vasko99f251b2017-01-11 11:31:46 +01001084 uint16_t i, j;
1085 char msg[256];
1086 NC_SESSION_TERM_REASON term_reason;
1087 struct pollfd pfd;
1088 struct timespec begin_ts, cur_ts;
Michal Vasko71090fc2016-05-24 16:37:28 +02001089 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +01001090 struct nc_server_rpc *rpc = NULL;
Michal Vasko99f251b2017-01-11 11:31:46 +01001091#ifdef NC_ENABLED_SSH
1092 struct nc_session *new;
1093#endif
Michal Vasko428087d2016-01-14 16:04:28 +01001094
1095 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001096 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001097 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001098 }
1099
Michal Vasko99f251b2017-01-11 11:31:46 +01001100 nc_gettimespec(&begin_ts);
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001101
Michal Vasko48a63ed2016-03-01 09:48:21 +01001102 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001103 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001104 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001105 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001106
Michal Vasko99f251b2017-01-11 11:31:46 +01001107 /* check that all session are fine */
Michal Vasko428087d2016-01-14 16:04:28 +01001108 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001109 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1110 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001111 ret = NC_PSPOLL_ERROR;
1112 if (session) {
1113 *session = ps->sessions[i];
1114 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001115 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001116 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001117
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001118 /* TODO invalidate only sessions without subscription */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001119 if (!(ps->sessions[i]->flags & NC_SESSION_CALLHOME) && server_opts.idle_timeout
Michal Vasko99f251b2017-01-11 11:31:46 +01001120 && (begin_ts.tv_sec >= ps->sessions[i]->opts.server.last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001121 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1122 ps->sessions[i]->status = NC_STATUS_INVALID;
1123 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001124 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1125 if (session) {
1126 *session = ps->sessions[i];
1127 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001128 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001129 }
Michal Vasko428087d2016-01-14 16:04:28 +01001130 }
1131
Michal Vasko99f251b2017-01-11 11:31:46 +01001132 /* poll on all the sessions one-by-one */
1133 do {
1134 /* loop from i to j */
1135 if (ps->last_event_session == ps->session_count - 1) {
1136 i = j = 0;
1137 } else {
1138 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001139 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001140 do {
1141 cur_session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +01001142
Michal Vasko99f251b2017-01-11 11:31:46 +01001143 switch (cur_session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +01001144#ifdef NC_ENABLED_SSH
Michal Vasko99f251b2017-01-11 11:31:46 +01001145 case NC_TI_LIBSSH:
1146 r = ssh_channel_poll_timeout(cur_session->ti.libssh.channel, 0, 0);
1147 if (r < 1) {
1148 if (r == SSH_EOF) {
1149 sprintf(msg, "SSH channel unexpectedly closed");
1150 term_reason = NC_SESSION_TERM_DROPPED;
1151 poll_ret = -2;
1152 } else if (r == SSH_ERROR) {
1153 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(cur_session->ti.libssh.session));
1154 poll_ret = -1;
1155 } else {
1156 poll_ret = 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001157 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001158 break;
Michal Vasko96164bf2016-01-21 15:41:58 +01001159 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001160 /* we have some data, but it may be just an SSH message */
Michal Vasko96164bf2016-01-21 15:41:58 +01001161
Michal Vasko99f251b2017-01-11 11:31:46 +01001162 r = nc_timedlock(cur_session->ti_lock, timeout, __func__);
1163 if (r < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001164 if (session) {
Michal Vasko99f251b2017-01-11 11:31:46 +01001165 *session = cur_session;
Michal Vasko71090fc2016-05-24 16:37:28 +02001166 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001167 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001168 goto finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001169 } else if (!r) {
1170 if (session) {
1171 *session = cur_session;
Michal Vasko428087d2016-01-14 16:04:28 +01001172 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001173 ret = NC_PSPOLL_TIMEOUT;
1174 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001175 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001176 r = ssh_execute_message_callbacks(cur_session->ti.libssh.session);
1177 pthread_mutex_unlock(cur_session->ti_lock);
1178
1179 if (r != SSH_OK) {
1180 sprintf(msg, "failed to receive SSH messages (%s)", ssh_get_error(cur_session->ti.libssh.session));
1181 term_reason = NC_SESSION_TERM_OTHER;
1182 poll_ret = -1;
1183 } else if (cur_session->flags & NC_SESSION_SSH_NEW_MSG) {
1184 /* new SSH message */
1185 cur_session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1186 if (cur_session->ti.libssh.next) {
1187 for (new = cur_session->ti.libssh.next; new != cur_session; new = new->ti.libssh.next) {
1188 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel
1189 && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1190 /* new NETCONF SSH channel */
1191 if (session) {
1192 *session = cur_session;
1193 }
1194 ret = NC_PSPOLL_SSH_CHANNEL;
1195 goto finish;
1196 }
1197 }
1198 }
1199
1200 /* just some SSH message */
1201 if (session) {
1202 *session = cur_session;
1203 }
1204 ret = NC_PSPOLL_SSH_MSG;
1205 goto finish;
1206 } else {
1207 /* we have some application data */
1208 poll_ret = 1;
1209 }
1210 break;
1211#endif
1212#ifdef NC_ENABLED_TLS
1213 case NC_TI_OPENSSL:
1214 r = SSL_pending(cur_session->ti.tls);
1215 if (!r) {
1216 /* no data pending in the SSL buffer, poll fd */
1217 pfd.fd = SSL_get_rfd(cur_session->ti.tls);
1218 if (pfd.fd < 0) {
1219 ERRINT;
1220 ret = NC_PSPOLL_ERROR;
1221 goto finish;
1222 }
1223 pfd.events = POLLIN;
1224 pfd.revents = 0;
1225 r = poll(&pfd, 1, 0);
1226
1227 if (r < 0) {
1228 sprintf(msg, "poll failed (%s)", strerror(errno));
1229 poll_ret = -1;
1230 } else if (r > 0) {
1231 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1232 sprintf(msg, "communication socket unexpectedly closed");
1233 term_reason = NC_SESSION_TERM_DROPPED;
1234 poll_ret = -2;
1235 } else if (pfd.revents & POLLERR) {
1236 sprintf(msg, "communication socket error");
1237 term_reason = NC_SESSION_TERM_OTHER;
1238 poll_ret = -2;
1239 } else {
1240 poll_ret = 1;
1241 }
1242 } else {
1243 poll_ret = 0;
1244 }
1245 } else {
1246 poll_ret = 1;
1247 }
1248 break;
1249#endif
1250 case NC_TI_FD:
1251 pfd.fd = cur_session->ti.fd.in;
1252 pfd.events = POLLIN;
1253 pfd.revents = 0;
1254 r = poll(&pfd, 1, 0);
1255
1256 if (r < 0) {
1257 sprintf(msg, "poll failed (%s)", strerror(errno));
1258 poll_ret = 0;
1259 } else if (r > 0) {
1260 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1261 sprintf(msg, "communication socket unexpectedly closed");
1262 term_reason = NC_SESSION_TERM_DROPPED;
1263 poll_ret = -2;
1264 } else if (pfd.revents & POLLERR) {
1265 sprintf(msg, "communication socket error");
1266 term_reason = NC_SESSION_TERM_OTHER;
1267 poll_ret = -2;
1268 } else {
1269 poll_ret = 1;
1270 }
1271 } else {
1272 poll_ret = 0;
1273 }
1274 break;
1275 case NC_TI_NONE:
1276 ERRINT;
1277 ret = NC_PSPOLL_ERROR;
1278 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001279 }
Michal Vasko428087d2016-01-14 16:04:28 +01001280
Michal Vasko99f251b2017-01-11 11:31:46 +01001281 /* here: poll_ret == -2 - session error, session terminated,
1282 * poll_ret == -1 - generic error,
1283 * poll_ret == 0 - nothing to read,
1284 * poll_ret > 0 - data available */
1285 if (poll_ret == -2) {
1286 ERR("Session %u: %s.", cur_session->id, msg);
1287 cur_session->status = NC_STATUS_INVALID;
1288 cur_session->term_reason = term_reason;
1289 if (session) {
1290 *session = cur_session;
1291 }
1292 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1293 goto finish;
1294 } else if (poll_ret == -1) {
1295 ERR("Session %u: %s.", cur_session->id, msg);
1296 ret = NC_PSPOLL_ERROR;
1297 goto finish;
1298 } else if (poll_ret > 0) {
1299 break;
1300 }
1301
1302 /* next iteration */
1303 if (i == ps->session_count - 1) {
1304 i = 0;
1305 } else {
1306 ++i;
1307 }
1308 } while (i != j);
1309
1310 /* no event */
1311 if (!poll_ret && (timeout > -1)) {
1312 usleep(NC_TIMEOUT_STEP);
1313
1314 nc_gettimespec(&cur_ts);
1315 /* final timeout */
1316 if (nc_difftimespec(&begin_ts, &cur_ts) >= (unsigned)timeout) {
1317 ret = NC_PSPOLL_TIMEOUT;
1318 goto finish;
1319 }
Michal Vasko428087d2016-01-14 16:04:28 +01001320 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001321 } while (!poll_ret);
Michal Vasko428087d2016-01-14 16:04:28 +01001322
1323 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001324 if (session) {
1325 *session = cur_session;
1326 }
Michal Vasko99f251b2017-01-11 11:31:46 +01001327 ps->last_event_session = i;
Michal Vasko428087d2016-01-14 16:04:28 +01001328
Michal Vaskobd8ef262016-01-20 11:09:27 +01001329 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko99f251b2017-01-11 11:31:46 +01001330 r = nc_timedlock(cur_session->ti_lock, timeout, __func__);
1331 if (r < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001332 ret = NC_PSPOLL_ERROR;
1333 goto finish;
Michal Vasko99f251b2017-01-11 11:31:46 +01001334 } else if (!r) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001335 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001336 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001337 }
1338
Radek Krejci93e80222016-10-03 13:34:25 +02001339 ret = nc_server_recv_rpc(cur_session, &rpc);
Michal Vasko71090fc2016-05-24 16:37:28 +02001340 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1341 pthread_mutex_unlock(cur_session->ti_lock);
1342 if (cur_session->status != NC_STATUS_RUNNING) {
1343 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001344 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001345 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001346 }
1347
Michal Vasko2e6defd2016-10-07 15:48:15 +02001348 cur_session->opts.server.last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001349
Michal Vasko428087d2016-01-14 16:04:28 +01001350 /* process RPC */
Radek Krejci93e80222016-10-03 13:34:25 +02001351 ret |= nc_server_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001352
Michal Vasko71090fc2016-05-24 16:37:28 +02001353 pthread_mutex_unlock(cur_session->ti_lock);
1354 if (cur_session->status != NC_STATUS_RUNNING) {
1355 ret |= NC_PSPOLL_SESSION_TERM;
1356 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1357 ret |= NC_PSPOLL_SESSION_ERROR;
1358 }
Michal Vasko428087d2016-01-14 16:04:28 +01001359 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001360
Michal Vaskoca4a2422016-02-02 12:17:14 +01001361 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001362
Michal Vasko48a63ed2016-03-01 09:48:21 +01001363finish:
1364 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001365 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001366 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001367}
1368
Michal Vaskod09eae62016-02-01 10:32:52 +01001369API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001370nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001371{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001372 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001373 uint16_t i;
1374 struct nc_session *session;
1375
Michal Vasko9a25e932016-02-01 10:36:42 +01001376 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001377 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001378 return;
1379 }
1380
Michal Vasko48a63ed2016-03-01 09:48:21 +01001381 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001382 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001383 return;
1384 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001385
Michal Vasko48a63ed2016-03-01 09:48:21 +01001386 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001387 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001388 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001389 }
1390 free(ps->sessions);
1391 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001392 ps->session_count = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +01001393 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001394 } else {
1395 for (i = 0; i < ps->session_count; ) {
1396 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1397 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001398 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001399 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001400 continue;
1401 }
1402
1403 ++i;
1404 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001405 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001406
1407 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001408 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001409}
1410
Radek Krejci53691be2016-02-22 13:58:37 +01001411#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001412
Michal Vaskoe2713da2016-08-22 16:06:40 +02001413API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001414nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001415{
Michal Vasko3031aae2016-01-27 16:07:18 +01001416 uint16_t i;
Michal Vasko9e036d52016-01-08 10:49:26 +01001417
Michal Vasko45e53ae2016-04-07 11:46:03 +02001418 if (!name) {
1419 ERRARG("name");
1420 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001421 }
1422
Michal Vasko51e514d2016-02-02 15:51:52 +01001423 /* WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001424 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001425
1426 /* check name uniqueness */
1427 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001428 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001429 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001430 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001431 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001432 return -1;
1433 }
1434 }
1435
Michal Vasko3031aae2016-01-27 16:07:18 +01001436 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001437 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001438 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001439 ERRMEM;
1440 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001441 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001442 return -1;
1443 }
1444 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001445 server_opts.endpts[server_opts.endpt_count - 1].ti = ti;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001446
Michal Vaskoe2713da2016-08-22 16:06:40 +02001447 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001448 if (!server_opts.binds) {
1449 ERRMEM;
1450 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001451 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001452 return -1;
1453 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001454
Michal Vasko2e6defd2016-10-07 15:48:15 +02001455 server_opts.binds[server_opts.endpt_count - 1].address = NULL;
1456 server_opts.binds[server_opts.endpt_count - 1].port = 0;
1457 server_opts.binds[server_opts.endpt_count - 1].sock = -1;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001458 server_opts.binds[server_opts.endpt_count - 1].pollin = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001459
1460 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001461#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001462 case NC_TI_LIBSSH:
1463 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1464 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) {
1465 ERRMEM;
1466 /* WRITE UNLOCK */
1467 pthread_rwlock_unlock(&server_opts.endpt_lock);
1468 return -1;
1469 }
1470 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods =
1471 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1472 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3;
1473 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10;
1474 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001475#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001476#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001477 case NC_TI_OPENSSL:
1478 server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1479 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) {
1480 ERRMEM;
1481 /* WRITE UNLOCK */
1482 pthread_rwlock_unlock(&server_opts.endpt_lock);
1483 return -1;
1484 }
1485 break;
1486#endif
1487 default:
1488 ERRINT;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001489 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001490 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001491 return -1;
1492 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001493
Michal Vasko2e6defd2016-10-07 15:48:15 +02001494 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001495
Michal Vasko3031aae2016-01-27 16:07:18 +01001496 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001497 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001498
Michal Vasko9e036d52016-01-08 10:49:26 +01001499 return 0;
1500}
1501
Michal Vasko3031aae2016-01-27 16:07:18 +01001502int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001503nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port)
Michal Vaskoda514772016-02-01 11:32:01 +01001504{
1505 struct nc_endpt *endpt;
1506 struct nc_bind *bind = NULL;
1507 uint16_t i;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001508 int sock = -1, set_addr;
Michal Vaskoda514772016-02-01 11:32:01 +01001509
Michal Vasko45e53ae2016-04-07 11:46:03 +02001510 if (!endpt_name) {
1511 ERRARG("endpt_name");
1512 return -1;
1513 } else if ((!address && !port) || (address && port)) {
1514 ERRARG("address and port");
1515 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001516 }
1517
Michal Vaskoe2713da2016-08-22 16:06:40 +02001518 if (address) {
1519 set_addr = 1;
1520 } else {
1521 set_addr = 0;
1522 }
1523
Michal Vasko51e514d2016-02-02 15:51:52 +01001524 /* LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001525 endpt = nc_server_endpt_lock(endpt_name, 0, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001526 if (!endpt) {
1527 return -1;
1528 }
1529
Michal Vaskoe2713da2016-08-22 16:06:40 +02001530 bind = &server_opts.binds[i];
Michal Vaskoda514772016-02-01 11:32:01 +01001531
Michal Vaskoe2713da2016-08-22 16:06:40 +02001532 if (set_addr) {
1533 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001534 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001535 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001536 }
1537
Michal Vaskoe2713da2016-08-22 16:06:40 +02001538 /* we have all the information we need to create a listening socket */
1539 if (address && port) {
1540 /* create new socket, close the old one */
1541 sock = nc_sock_listen(address, port);
1542 if (sock == -1) {
1543 goto fail;
1544 }
1545
1546 if (bind->sock > -1) {
1547 close(bind->sock);
1548 }
1549 bind->sock = sock;
1550 } /* else we are just setting address or port */
1551
1552 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001553 lydict_remove(server_opts.ctx, bind->address);
1554 bind->address = lydict_insert(server_opts.ctx, address, 0);
1555 } else {
1556 bind->port = port;
1557 }
1558
Michal Vaskoe2713da2016-08-22 16:06:40 +02001559 if (sock > -1) {
1560#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001561 VRB("Listening on %s:%u for %s connections.", address, port, (endpt->ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001562#elif defined(NC_ENABLED_SSH)
1563 VRB("Listening on %s:%u for SSH connections.", address, port);
1564#else
1565 VRB("Listening on %s:%u for TLS connections.", address, port);
1566#endif
1567 }
1568
Michal Vasko51e514d2016-02-02 15:51:52 +01001569 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001570 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001571 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001572
1573fail:
1574 /* UNLOCK */
1575 nc_server_endpt_unlock(endpt);
1576 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001577}
1578
Michal Vaskoe2713da2016-08-22 16:06:40 +02001579API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001580nc_server_endpt_set_address(const char *endpt_name, const char *address)
1581{
1582 return nc_server_endpt_set_address_port(endpt_name, address, 0);
1583}
1584
1585API int
1586nc_server_endpt_set_port(const char *endpt_name, uint16_t port)
1587{
1588 return nc_server_endpt_set_address_port(endpt_name, NULL, port);
1589}
1590
1591API int
Michal Vasko59050372016-11-22 14:33:55 +01001592nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001593{
1594 uint32_t i;
1595 int ret = -1;
1596
Michal Vasko3031aae2016-01-27 16:07:18 +01001597 /* WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001598 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001599
Michal Vasko59050372016-11-22 14:33:55 +01001600 if (!name && !ti) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001601 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001602 for (i = 0; i < server_opts.endpt_count; ++i) {
1603 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001604 pthread_mutex_destroy(&server_opts.endpts[i].lock);
1605 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001606#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001607 case NC_TI_LIBSSH:
1608 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1609 free(server_opts.endpts[i].opts.ssh);
1610 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001611#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001612#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001613 case NC_TI_OPENSSL:
1614 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1615 free(server_opts.endpts[i].opts.tls);
1616 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001617#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001618 default:
1619 ERRINT;
1620 /* won't get here ...*/
1621 break;
1622 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001623 ret = 0;
1624 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001625 free(server_opts.endpts);
1626 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001627
1628 /* remove all binds */
1629 for (i = 0; i < server_opts.endpt_count; ++i) {
1630 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1631 if (server_opts.binds[i].sock > -1) {
1632 close(server_opts.binds[i].sock);
1633 }
1634 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001635 free(server_opts.binds);
1636 server_opts.binds = NULL;
1637
Michal Vasko3031aae2016-01-27 16:07:18 +01001638 server_opts.endpt_count = 0;
1639
Michal Vasko1a38c862016-01-15 15:50:07 +01001640 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001641 /* remove one endpoint with bind(s) or all endpoints using one transport protocol */
Michal Vasko3031aae2016-01-27 16:07:18 +01001642 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001643 if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001644 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001645 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001646 pthread_mutex_destroy(&server_opts.endpts[i].lock);
1647 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001648#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001649 case NC_TI_LIBSSH:
1650 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1651 free(server_opts.endpts[i].opts.ssh);
1652 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001653#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001654#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001655 case NC_TI_OPENSSL:
1656 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1657 free(server_opts.endpts[i].opts.tls);
1658 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001659#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001660 default:
1661 ERRINT;
1662 break;
1663 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001664
Michal Vaskoe2713da2016-08-22 16:06:40 +02001665 /* remove bind(s) */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001666 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1667 if (server_opts.binds[i].sock > -1) {
1668 close(server_opts.binds[i].sock);
1669 }
1670
1671 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001672 --server_opts.endpt_count;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001673 if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001674 free(server_opts.binds);
1675 server_opts.binds = NULL;
1676 free(server_opts.endpts);
1677 server_opts.endpts = NULL;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001678 } else if (i < server_opts.endpt_count) {
1679 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1680 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoc0256492016-02-02 12:19:06 +01001681 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001682
1683 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01001684 if (name) {
1685 break;
1686 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001687 }
1688 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001689 }
1690
Michal Vasko3031aae2016-01-27 16:07:18 +01001691 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001692 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001693
Michal Vasko9e036d52016-01-08 10:49:26 +01001694 return ret;
1695}
1696
Michal Vasko71090fc2016-05-24 16:37:28 +02001697API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001698nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001699{
Michal Vasko71090fc2016-05-24 16:37:28 +02001700 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001701 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001702 char *host = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001703 uint16_t port, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001704
Michal Vasko45e53ae2016-04-07 11:46:03 +02001705 if (!server_opts.ctx) {
1706 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001707 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001708 } else if (!session) {
1709 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001710 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001711 }
1712
Michal Vasko51e514d2016-02-02 15:51:52 +01001713 /* we have to hold WRITE for the whole time, since there is not
1714 * a way of downgrading the lock to READ */
1715 /* WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001716 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001717
1718 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001719 ERR("No endpoints to accept sessions on.");
Michal Vasko51e514d2016-02-02 15:51:52 +01001720 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001721 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001722 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001723 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001724
Michal Vaskoe2713da2016-08-22 16:06:40 +02001725 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001726 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001727 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001728 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001729 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001730 if (!ret) {
1731 return NC_MSG_WOULDBLOCK;
1732 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001733 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001734 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001735 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001736
Michal Vasko1a38c862016-01-15 15:50:07 +01001737 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001738 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001739 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001740 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001741 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001742 msgtype = NC_MSG_ERROR;
1743 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001744 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001745 (*session)->status = NC_STATUS_STARTING;
1746 (*session)->side = NC_SERVER;
1747 (*session)->ctx = server_opts.ctx;
1748 (*session)->flags = NC_SESSION_SHAREDCTX;
1749 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1750 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001751
1752 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001753 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1754 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001755 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001756 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001757 msgtype = NC_MSG_ERROR;
1758 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001759 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001760 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001761
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001762 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001763#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001764 if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) {
1765 (*session)->data = server_opts.endpts[bind_idx].opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01001766 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001767 if (ret < 0) {
1768 msgtype = NC_MSG_ERROR;
1769 goto cleanup;
1770 } else if (!ret) {
1771 msgtype = NC_MSG_WOULDBLOCK;
1772 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001773 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001774 } else
1775#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001776#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001777 if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) {
1778 (*session)->data = server_opts.endpts[bind_idx].opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01001779 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001780 if (ret < 0) {
1781 msgtype = NC_MSG_ERROR;
1782 goto cleanup;
1783 } else if (!ret) {
1784 msgtype = NC_MSG_WOULDBLOCK;
1785 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001786 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001787 } else
1788#endif
1789 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001790 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001791 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001792 msgtype = NC_MSG_ERROR;
1793 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001794 }
1795
Michal Vasko2cc4c682016-03-01 09:16:48 +01001796 (*session)->data = NULL;
1797
Michal Vasko51e514d2016-02-02 15:51:52 +01001798 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001799 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001800
Michal Vaskob48aa812016-01-18 14:13:09 +01001801 /* assign new SID atomically */
1802 /* LOCK */
1803 pthread_spin_lock(&server_opts.sid_lock);
1804 (*session)->id = server_opts.new_session_id++;
1805 /* UNLOCK */
1806 pthread_spin_unlock(&server_opts.sid_lock);
1807
Michal Vasko9e036d52016-01-08 10:49:26 +01001808 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001809 msgtype = nc_handshake(*session);
1810 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001811 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001812 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001813 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001814 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001815 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001816 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001817
Michal Vasko71090fc2016-05-24 16:37:28 +02001818 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001819
Michal Vasko71090fc2016-05-24 16:37:28 +02001820cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001821 /* WRITE UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001822 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001823
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001824 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001825 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001826 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001827}
1828
Michal Vasko2e6defd2016-10-07 15:48:15 +02001829API int
1830nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti)
1831{
1832 uint16_t i;
1833
1834 if (!name) {
1835 ERRARG("name");
1836 return -1;
1837 } else if (!ti) {
1838 ERRARG("ti");
1839 return -1;
1840 }
1841
1842 /* WRITE LOCK */
1843 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1844
1845 /* check name uniqueness */
1846 for (i = 0; i < server_opts.ch_client_count; ++i) {
1847 if (!strcmp(server_opts.ch_clients[i].name, name)) {
1848 ERR("Call Home client \"%s\" already exists.", name);
1849 /* WRITE UNLOCK */
1850 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1851 return -1;
1852 }
1853 }
1854
1855 ++server_opts.ch_client_count;
1856 server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients);
1857 if (!server_opts.ch_clients) {
1858 ERRMEM;
1859 /* WRITE UNLOCK */
1860 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1861 return -1;
1862 }
1863 server_opts.ch_clients[server_opts.ch_client_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1864 server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001865 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL;
1866 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpt_count = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001867
1868 switch (ti) {
1869#ifdef NC_ENABLED_SSH
1870 case NC_TI_LIBSSH:
1871 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1872 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) {
1873 ERRMEM;
1874 /* WRITE UNLOCK */
1875 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1876 return -1;
1877 }
1878 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods =
1879 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1880 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3;
1881 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10;
1882 break;
1883#endif
1884#ifdef NC_ENABLED_TLS
1885 case NC_TI_OPENSSL:
1886 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1887 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) {
1888 ERRMEM;
1889 /* WRITE UNLOCK */
1890 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1891 return -1;
1892 }
1893 break;
1894#endif
1895 default:
1896 ERRINT;
1897 /* WRITE UNLOCK */
1898 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1899 return -1;
1900 }
1901
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001902 server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0;
1903
Michal Vasko2e6defd2016-10-07 15:48:15 +02001904 /* set CH default options */
1905 server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED;
1906 server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3;
1907
1908 pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL);
1909
1910 /* WRITE UNLOCK */
1911 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1912
1913 return 0;
1914}
1915
1916API int
Michal Vasko59050372016-11-22 14:33:55 +01001917nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001918{
1919 uint16_t i, j;
1920 int ret = -1;
1921
1922 /* WRITE LOCK */
1923 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1924
Michal Vasko59050372016-11-22 14:33:55 +01001925 if (!name && !ti) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001926 /* remove all CH clients */
1927 for (i = 0; i < server_opts.ch_client_count; ++i) {
1928 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1929
1930 /* remove all endpoints */
1931 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
1932 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
1933 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
1934 }
1935 free(server_opts.ch_clients[i].ch_endpts);
1936
1937 switch (server_opts.ch_clients[i].ti) {
1938#ifdef NC_ENABLED_SSH
1939 case NC_TI_LIBSSH:
1940 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1941 free(server_opts.ch_clients[i].opts.ssh);
1942 break;
1943#endif
1944#ifdef NC_ENABLED_TLS
1945 case NC_TI_OPENSSL:
1946 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
1947 free(server_opts.ch_clients[i].opts.tls);
1948 break;
1949#endif
1950 default:
1951 ERRINT;
1952 /* won't get here ...*/
1953 break;
1954 }
1955
1956 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
1957
1958 ret = 0;
1959 }
1960 free(server_opts.ch_clients);
1961 server_opts.ch_clients = NULL;
1962
1963 server_opts.ch_client_count = 0;
1964
1965 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001966 /* remove one client with endpoint(s) or all clients using one protocol */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001967 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001968 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 +02001969 /* remove endpt */
1970 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1971
1972 switch (server_opts.ch_clients[i].ti) {
1973#ifdef NC_ENABLED_SSH
1974 case NC_TI_LIBSSH:
1975 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1976 free(server_opts.ch_clients[i].opts.ssh);
1977 break;
1978#endif
1979#ifdef NC_ENABLED_TLS
1980 case NC_TI_OPENSSL:
1981 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
1982 free(server_opts.ch_clients[i].opts.tls);
1983 break;
1984#endif
1985 default:
1986 ERRINT;
1987 break;
1988 }
1989
1990 /* remove all endpoints */
1991 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
1992 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
1993 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
1994 }
1995 free(server_opts.ch_clients[i].ch_endpts);
1996
1997 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
1998
1999 /* move last client and endpoint(s) to the empty space */
2000 --server_opts.ch_client_count;
2001 if (i < server_opts.ch_client_count) {
2002 memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count],
2003 sizeof *server_opts.ch_clients);
2004 } else if (!server_opts.ch_client_count) {
2005 free(server_opts.ch_clients);
2006 server_opts.ch_clients = NULL;
2007 }
2008
2009 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01002010 if (name) {
2011 break;
2012 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002013 }
2014 }
2015 }
2016
2017 /* WRITE UNLOCK */
2018 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2019
2020 return ret;
2021}
2022
2023API int
2024nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name)
2025{
2026 uint16_t i;
2027 struct nc_ch_client *client;
2028
2029 if (!client_name) {
2030 ERRARG("client_name");
2031 return -1;
2032 } else if (!endpt_name) {
2033 ERRARG("endpt_name");
2034 return -1;
2035 }
2036
2037 /* LOCK */
2038 client = nc_server_ch_client_lock(client_name, 0, NULL);
2039 if (!client) {
2040 return -1;
2041 }
2042
2043 for (i = 0; i < client->ch_endpt_count; ++i) {
2044 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2045 ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name);
2046 /* UNLOCK */
2047 nc_server_ch_client_unlock(client);
2048 return -1;
2049 }
2050 }
2051
2052 ++client->ch_endpt_count;
2053 client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts);
2054 if (!client->ch_endpts) {
2055 ERRMEM;
2056 /* UNLOCK */
2057 nc_server_ch_client_unlock(client);
2058 return -1;
2059 }
2060
2061 client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0);
2062 client->ch_endpts[client->ch_endpt_count - 1].address = NULL;
2063 client->ch_endpts[client->ch_endpt_count - 1].port = 0;
2064
2065 /* UNLOCK */
2066 nc_server_ch_client_unlock(client);
2067
2068 return 0;
2069}
2070
2071API int
2072nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name)
2073{
2074 uint16_t i;
2075 int ret = -1;
2076 struct nc_ch_client *client;
2077
2078 if (!client_name) {
2079 ERRARG("client_name");
2080 return -1;
2081 }
2082
2083 /* LOCK */
2084 client = nc_server_ch_client_lock(client_name, 0, NULL);
2085 if (!client) {
2086 return -1;
2087 }
2088
2089 if (!endpt_name) {
2090 /* remove all endpoints */
2091 for (i = 0; i < client->ch_endpt_count; ++i) {
2092 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2093 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2094 }
2095 free(client->ch_endpts);
2096 client->ch_endpts = NULL;
2097 client->ch_endpt_count = 0;
2098
2099 ret = 0;
2100 } else {
2101 for (i = 0; i < client->ch_endpt_count; ++i) {
2102 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2103 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2104 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002105
Michal Vasko4f921012016-10-20 14:07:45 +02002106 /* move last endpoint to the empty space */
2107 --client->ch_endpt_count;
2108 if (i < client->ch_endpt_count) {
2109 memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts);
2110 } else if (!server_opts.ch_client_count) {
2111 free(server_opts.ch_clients);
2112 server_opts.ch_clients = NULL;
2113 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002114
Michal Vasko4f921012016-10-20 14:07:45 +02002115 ret = 0;
2116 break;
2117 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002118 }
2119 }
2120
2121 /* UNLOCK */
2122 nc_server_ch_client_unlock(client);
2123
2124 return ret;
2125}
2126
2127API int
2128nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address)
2129{
2130 uint16_t i;
2131 int ret = -1;
2132 struct nc_ch_client *client;
2133
2134 if (!client_name) {
2135 ERRARG("client_name");
2136 return -1;
2137 } else if (!endpt_name) {
2138 ERRARG("endpt_name");
2139 return -1;
2140 } else if (!address) {
2141 ERRARG("address");
2142 return -1;
2143 }
2144
2145 /* LOCK */
2146 client = nc_server_ch_client_lock(client_name, 0, NULL);
2147 if (!client) {
2148 return -1;
2149 }
2150
2151 for (i = 0; i < client->ch_endpt_count; ++i) {
2152 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2153 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2154 client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0);
2155
2156 ret = 0;
2157 break;
2158 }
2159 }
2160
2161 /* UNLOCK */
2162 nc_server_ch_client_unlock(client);
2163
2164 if (ret == -1) {
2165 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2166 }
2167
2168 return ret;
2169}
2170
2171API int
2172nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port)
2173{
2174 uint16_t i;
2175 int ret = -1;
2176 struct nc_ch_client *client;
2177
2178 if (!client_name) {
2179 ERRARG("client_name");
2180 return -1;
2181 } else if (!endpt_name) {
2182 ERRARG("endpt_name");
2183 return -1;
2184 } else if (!port) {
2185 ERRARG("port");
2186 return -1;
2187 }
2188
2189 /* LOCK */
2190 client = nc_server_ch_client_lock(client_name, 0, NULL);
2191 if (!client) {
2192 return -1;
2193 }
2194
2195 for (i = 0; i < client->ch_endpt_count; ++i) {
2196 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2197 client->ch_endpts[i].port = port;
2198
2199 ret = 0;
2200 break;
2201 }
2202 }
2203
2204 /* UNLOCK */
2205 nc_server_ch_client_unlock(client);
2206
2207 if (ret == -1) {
2208 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2209 }
2210
2211 return ret;
2212}
2213
2214API int
2215nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type)
2216{
2217 struct nc_ch_client *client;
2218
2219 if (!client_name) {
2220 ERRARG("client_name");
2221 return -1;
2222 } else if (!conn_type) {
2223 ERRARG("conn_type");
2224 return -1;
2225 }
2226
2227 /* LOCK */
2228 client = nc_server_ch_client_lock(client_name, 0, NULL);
2229 if (!client) {
2230 return -1;
2231 }
2232
2233 if (client->conn_type != conn_type) {
2234 client->conn_type = conn_type;
2235
2236 /* set default options */
2237 switch (conn_type) {
2238 case NC_CH_PERSIST:
2239 client->conn.persist.idle_timeout = 86400;
2240 client->conn.persist.ka_max_wait = 30;
2241 client->conn.persist.ka_max_attempts = 3;
2242 break;
2243 case NC_CH_PERIOD:
2244 client->conn.period.idle_timeout = 300;
2245 client->conn.period.reconnect_timeout = 60;
2246 break;
2247 default:
2248 ERRINT;
2249 break;
2250 }
2251 }
2252
2253 /* UNLOCK */
2254 nc_server_ch_client_unlock(client);
2255
2256 return 0;
2257}
2258
2259API int
2260nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout)
2261{
2262 struct nc_ch_client *client;
2263
2264 if (!client_name) {
2265 ERRARG("client_name");
2266 return -1;
2267 }
2268
2269 /* LOCK */
2270 client = nc_server_ch_client_lock(client_name, 0, NULL);
2271 if (!client) {
2272 return -1;
2273 }
2274
2275 if (client->conn_type != NC_CH_PERSIST) {
2276 ERR("Call Home client \"%s\" is not of persistent connection type.");
2277 /* UNLOCK */
2278 nc_server_ch_client_unlock(client);
2279 return -1;
2280 }
2281
2282 client->conn.persist.idle_timeout = idle_timeout;
2283
2284 /* UNLOCK */
2285 nc_server_ch_client_unlock(client);
2286
2287 return 0;
2288}
2289
2290API int
2291nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait)
2292{
2293 struct nc_ch_client *client;
2294
2295 if (!client_name) {
2296 ERRARG("client_name");
2297 return -1;
2298 } else if (!max_wait) {
2299 ERRARG("max_wait");
2300 return -1;
2301 }
2302
2303 /* LOCK */
2304 client = nc_server_ch_client_lock(client_name, 0, NULL);
2305 if (!client) {
2306 return -1;
2307 }
2308
2309 if (client->conn_type != NC_CH_PERSIST) {
2310 ERR("Call Home client \"%s\" is not of persistent connection type.");
2311 /* UNLOCK */
2312 nc_server_ch_client_unlock(client);
2313 return -1;
2314 }
2315
2316 client->conn.persist.ka_max_wait = max_wait;
2317
2318 /* UNLOCK */
2319 nc_server_ch_client_unlock(client);
2320
2321 return 0;
2322}
2323
2324API int
2325nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts)
2326{
2327 struct nc_ch_client *client;
2328
2329 if (!client_name) {
2330 ERRARG("client_name");
2331 return -1;
2332 }
2333
2334 /* LOCK */
2335 client = nc_server_ch_client_lock(client_name, 0, NULL);
2336 if (!client) {
2337 return -1;
2338 }
2339
2340 if (client->conn_type != NC_CH_PERSIST) {
2341 ERR("Call Home client \"%s\" is not of persistent connection type.");
2342 /* UNLOCK */
2343 nc_server_ch_client_unlock(client);
2344 return -1;
2345 }
2346
2347 client->conn.persist.ka_max_attempts = max_attempts;
2348
2349 /* UNLOCK */
2350 nc_server_ch_client_unlock(client);
2351
2352 return 0;
2353}
2354
2355API int
2356nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout)
2357{
2358 struct nc_ch_client *client;
2359
2360 if (!client_name) {
2361 ERRARG("client_name");
2362 return -1;
2363 }
2364
2365 /* LOCK */
2366 client = nc_server_ch_client_lock(client_name, 0, NULL);
2367 if (!client) {
2368 return -1;
2369 }
2370
2371 if (client->conn_type != NC_CH_PERIOD) {
2372 ERR("Call Home client \"%s\" is not of periodic connection type.");
2373 /* UNLOCK */
2374 nc_server_ch_client_unlock(client);
2375 return -1;
2376 }
2377
2378 client->conn.period.idle_timeout = idle_timeout;
2379
2380 /* UNLOCK */
2381 nc_server_ch_client_unlock(client);
2382
2383 return 0;
2384}
2385
2386API int
2387nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout)
2388{
2389 struct nc_ch_client *client;
2390
2391 if (!client_name) {
2392 ERRARG("client_name");
2393 return -1;
2394 } else if (!reconnect_timeout) {
2395 ERRARG("reconnect_timeout");
2396 return -1;
2397 }
2398
2399 /* LOCK */
2400 client = nc_server_ch_client_lock(client_name, 0, NULL);
2401 if (!client) {
2402 return -1;
2403 }
2404
2405 if (client->conn_type != NC_CH_PERIOD) {
2406 ERR("Call Home client \"%s\" is not of periodic connection type.");
2407 /* UNLOCK */
2408 nc_server_ch_client_unlock(client);
2409 return -1;
2410 }
2411
2412 client->conn.period.reconnect_timeout = reconnect_timeout;
2413
2414 /* UNLOCK */
2415 nc_server_ch_client_unlock(client);
2416
2417 return 0;
2418}
2419
2420API int
2421nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with)
2422{
2423 struct nc_ch_client *client;
2424
2425 if (!client_name) {
2426 ERRARG("client_name");
2427 return -1;
2428 }
2429
2430 /* LOCK */
2431 client = nc_server_ch_client_lock(client_name, 0, NULL);
2432 if (!client) {
2433 return -1;
2434 }
2435
2436 client->start_with = start_with;
2437
2438 /* UNLOCK */
2439 nc_server_ch_client_unlock(client);
2440
2441 return 0;
2442}
2443
2444API int
2445nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts)
2446{
2447 struct nc_ch_client *client;
2448
2449 if (!client_name) {
2450 ERRARG("client_name");
2451 return -1;
2452 } else if (!max_attempts) {
2453 ERRARG("max_attempts");
2454 return -1;
2455 }
2456
2457 /* LOCK */
2458 client = nc_server_ch_client_lock(client_name, 0, NULL);
2459 if (!client) {
2460 return -1;
2461 }
2462
2463 client->max_attempts = max_attempts;
2464
2465 /* UNLOCK */
2466 nc_server_ch_client_unlock(client);
2467
2468 return 0;
2469}
2470
2471/* client lock is expected to be held */
2472static NC_MSG_TYPE
2473nc_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 +01002474{
Michal Vasko71090fc2016-05-24 16:37:28 +02002475 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002476 int sock, ret;
2477
Michal Vasko2e6defd2016-10-07 15:48:15 +02002478 sock = nc_sock_connect(endpt->address, endpt->port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01002479 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02002480 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002481 }
2482
2483 *session = calloc(1, sizeof **session);
2484 if (!(*session)) {
2485 ERRMEM;
2486 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002487 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002488 }
2489 (*session)->status = NC_STATUS_STARTING;
2490 (*session)->side = NC_SERVER;
2491 (*session)->ctx = server_opts.ctx;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002492 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002493 (*session)->host = lydict_insert(server_opts.ctx, endpt->address, 0);
2494 (*session)->port = endpt->port;
Michal Vaskob05053d2016-01-22 16:12:06 +01002495
2496 /* transport lock */
2497 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
2498 if (!(*session)->ti_lock) {
2499 ERRMEM;
2500 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002501 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002502 goto fail;
2503 }
2504 pthread_mutex_init((*session)->ti_lock, NULL);
2505
2506 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01002507#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002508 if (client->ti == NC_TI_LIBSSH) {
2509 (*session)->data = client->opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01002510 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002511 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002512
Michal Vasko71090fc2016-05-24 16:37:28 +02002513 if (ret < 0) {
2514 msgtype = NC_MSG_ERROR;
2515 goto fail;
2516 } else if (!ret) {
2517 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002518 goto fail;
2519 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002520 } else
2521#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002522#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002523 if (client->ti == NC_TI_OPENSSL) {
2524 (*session)->data = client->opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01002525 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002526 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002527
Michal Vasko71090fc2016-05-24 16:37:28 +02002528 if (ret < 0) {
2529 msgtype = NC_MSG_ERROR;
2530 goto fail;
2531 } else if (!ret) {
2532 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002533 goto fail;
2534 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002535 } else
2536#endif
2537 {
Michal Vaskob05053d2016-01-22 16:12:06 +01002538 ERRINT;
2539 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002540 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002541 goto fail;
2542 }
2543
2544 /* assign new SID atomically */
2545 /* LOCK */
2546 pthread_spin_lock(&server_opts.sid_lock);
2547 (*session)->id = server_opts.new_session_id++;
2548 /* UNLOCK */
2549 pthread_spin_unlock(&server_opts.sid_lock);
2550
2551 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02002552 msgtype = nc_handshake(*session);
2553 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01002554 goto fail;
2555 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002556 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002557 (*session)->status = NC_STATUS_RUNNING;
2558
Michal Vasko71090fc2016-05-24 16:37:28 +02002559 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002560
2561fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002562 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002563 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002564 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002565}
2566
Michal Vasko2e6defd2016-10-07 15:48:15 +02002567/* ms */
2568#define NC_CH_NO_ENDPT_WAIT 1000
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002569#define NC_CH_ENDPT_FAIL_WAIT 1000
Michal Vasko2e6defd2016-10-07 15:48:15 +02002570
2571struct nc_ch_client_thread_arg {
2572 char *client_name;
2573 void (*session_clb)(const char *client_name, struct nc_session *new_session);
2574};
2575
2576static struct nc_ch_client *
2577nc_server_ch_client_with_endpt_lock(const char *name)
2578{
2579 struct nc_ch_client *client;
2580
2581 while (1) {
2582 /* LOCK */
2583 client = nc_server_ch_client_lock(name, 0, NULL);
2584 if (!client) {
2585 return NULL;
2586 }
2587 if (client->ch_endpt_count) {
2588 return client;
2589 }
2590 /* no endpoints defined yet */
2591
2592 /* UNLOCK */
2593 nc_server_ch_client_unlock(client);
2594
2595 usleep(NC_CH_NO_ENDPT_WAIT * 1000);
2596 }
2597
2598 return NULL;
2599}
2600
2601static int
2602nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data)
2603{
2604 int ret;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002605 uint32_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002606 struct timespec ts;
2607 struct nc_ch_client *client;
2608
2609 /* session created, initialize condition */
2610 session->opts.server.ch_lock = malloc(sizeof *session->opts.server.ch_lock);
2611 session->opts.server.ch_cond = malloc(sizeof *session->opts.server.ch_cond);
2612 if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) {
2613 ERRMEM;
2614 nc_session_free(session, NULL);
2615 return -1;
2616 }
2617 pthread_mutex_init(session->opts.server.ch_lock, NULL);
2618 pthread_cond_init(session->opts.server.ch_cond, NULL);
2619
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002620 session->flags |= NC_SESSION_CALLHOME;
2621
Michal Vasko2e6defd2016-10-07 15:48:15 +02002622 /* CH LOCK */
2623 pthread_mutex_lock(session->opts.server.ch_lock);
2624
2625 /* give the session to the user */
2626 data->session_clb(data->client_name, session);
2627
2628 do {
2629 nc_gettimespec(&ts);
2630 ts.tv_nsec += NC_CH_NO_ENDPT_WAIT * 1000000L;
2631 if (ts.tv_nsec > 1000000000L) {
2632 ts.tv_sec += ts.tv_nsec / 1000000000L;
2633 ts.tv_nsec %= 1000000000L;
2634 }
2635
2636 ret = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts);
2637 if (ret && (ret != ETIMEDOUT)) {
2638 ERR("Pthread condition timedwait failed (%s).", strerror(ret));
2639 goto ch_client_remove;
2640 }
2641
2642 /* check whether the client was not removed */
2643 /* LOCK */
2644 client = nc_server_ch_client_lock(data->client_name, 0, NULL);
2645 if (!client) {
2646 /* client was removed, finish thread */
2647 VRB("Call Home client \"%s\" removed, but an established session will not be terminated.",
2648 data->client_name);
2649 goto ch_client_remove;
2650 }
2651
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002652 if (client->conn_type == NC_CH_PERSIST) {
2653 /* TODO keep-alives */
2654 idle_timeout = client->conn.persist.idle_timeout;
2655 } else {
2656 idle_timeout = client->conn.period.idle_timeout;
2657 }
2658
2659 /* TODO only for sessions without subscriptions */
2660 if (idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) {
2661 VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id);
2662 session->status = NC_STATUS_INVALID;
2663 session->term_reason = NC_SESSION_TERM_TIMEOUT;
2664 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002665
2666 /* UNLOCK */
2667 nc_server_ch_client_unlock(client);
2668
2669 } while (session->status == NC_STATUS_RUNNING);
2670
2671 /* CH UNLOCK */
2672 pthread_mutex_unlock(session->opts.server.ch_lock);
2673
2674 return 0;
2675
2676ch_client_remove:
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002677 /* make the session a standard one */
2678 pthread_cond_destroy(session->opts.server.ch_cond);
2679 free(session->opts.server.ch_cond);
2680 session->opts.server.ch_cond = NULL;
2681
2682 session->flags &= ~NC_SESSION_CALLHOME;
2683
Michal Vasko2e6defd2016-10-07 15:48:15 +02002684 /* CH UNLOCK */
2685 pthread_mutex_unlock(session->opts.server.ch_lock);
2686
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002687 pthread_mutex_destroy(session->opts.server.ch_lock);
2688 free(session->opts.server.ch_lock);
2689 session->opts.server.ch_lock = NULL;
2690
Michal Vasko2e6defd2016-10-07 15:48:15 +02002691 return 1;
2692}
2693
2694static void *
2695nc_ch_client_thread(void *arg)
2696{
2697 struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg;
2698 NC_MSG_TYPE msgtype;
2699 uint8_t cur_attempts = 0;
2700 uint16_t i;
2701 char *cur_endpt_name;
2702 struct nc_ch_endpt *cur_endpt;
2703 struct nc_session *session;
2704 struct nc_ch_client *client;
2705
2706 /* LOCK */
2707 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2708 if (!client) {
2709 goto cleanup;
2710 }
2711
2712 cur_endpt = &client->ch_endpts[0];
2713 cur_endpt_name = strdup(cur_endpt->name);
2714
Michal Vasko29af44b2016-10-13 10:59:55 +02002715 VRB("Call Home client \"%s\" connecting...", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002716 while (1) {
2717 msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session);
2718
2719 if (msgtype == NC_MSG_HELLO) {
2720 /* UNLOCK */
2721 nc_server_ch_client_unlock(client);
2722
Michal Vasko29af44b2016-10-13 10:59:55 +02002723 VRB("Call Home client \"%s\" session %u established.", data->client_name, session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002724 if (nc_server_ch_client_thread_session_cond_wait(session, data)) {
2725 goto cleanup;
2726 }
Michal Vasko29af44b2016-10-13 10:59:55 +02002727 VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002728
2729 /* LOCK */
2730 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2731 if (!client) {
2732 goto cleanup;
2733 }
2734
2735 /* session changed status -> it was disconnected for whatever reason,
2736 * persistent connection immediately tries to reconnect, periodic waits some first */
2737 if (client->conn_type == NC_CH_PERIOD) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002738 /* UNLOCK */
2739 nc_server_ch_client_unlock(client);
2740
2741 /* TODO wake up sometimes to check for new notifications */
2742 usleep(client->conn.period.reconnect_timeout * 60 * 1000000);
2743
2744 /* LOCK */
2745 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2746 if (!client) {
2747 goto cleanup;
2748 }
2749 }
2750
2751 /* set next endpoint to try */
2752 if (client->start_with == NC_CH_FIRST_LISTED) {
2753 cur_endpt = &client->ch_endpts[0];
2754 free(cur_endpt_name);
2755 cur_endpt_name = strdup(cur_endpt->name);
2756 } /* else we keep the current one */
2757 } else {
Michal Vasko6bb116b2016-10-26 13:53:46 +02002758 /* UNLOCK */
2759 nc_server_ch_client_unlock(client);
2760
Michal Vasko2e6defd2016-10-07 15:48:15 +02002761 /* session was not created */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002762 usleep(NC_CH_ENDPT_FAIL_WAIT * 1000);
2763
Michal Vasko6bb116b2016-10-26 13:53:46 +02002764 /* LOCK */
2765 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2766 if (!client) {
2767 goto cleanup;
2768 }
2769
Michal Vasko2e6defd2016-10-07 15:48:15 +02002770 ++cur_attempts;
2771 if (cur_attempts == client->max_attempts) {
2772 for (i = 0; i < client->ch_endpt_count; ++i) {
2773 if (!strcmp(client->ch_endpts[i].name, cur_endpt_name)) {
2774 break;
2775 }
2776 }
2777 if (i < client->ch_endpt_count - 1) {
2778 /* just go to the next endpoint */
2779 cur_endpt = &client->ch_endpts[i + 1];
2780 free(cur_endpt_name);
2781 cur_endpt_name = strdup(cur_endpt->name);
2782 } else {
2783 /* cur_endpoint was removed or is the last, either way start with the first one */
2784 cur_endpt = &client->ch_endpts[0];
2785 free(cur_endpt_name);
2786 cur_endpt_name = strdup(cur_endpt->name);
2787 }
2788
2789 cur_attempts = 0;
2790 } /* else we keep the current one */
2791 }
2792 }
2793
2794cleanup:
2795 VRB("Call Home client \"%s\" thread exit.", data->client_name);
2796
2797 free(data->client_name);
2798 free(data);
2799 return NULL;
2800}
2801
2802API int
2803nc_connect_ch_client_dispatch(const char *client_name,
2804 void (*session_clb)(const char *client_name, struct nc_session *new_session)) {
2805 int ret;
2806 pthread_t tid;
2807 struct nc_ch_client_thread_arg *arg;
2808
2809 if (!client_name) {
2810 ERRARG("client_name");
2811 return -1;
2812 } else if (!session_clb) {
2813 ERRARG("session_clb");
2814 return -1;
2815 }
2816
2817 arg = malloc(sizeof *arg);
2818 if (!arg) {
2819 ERRMEM;
2820 return -1;
2821 }
2822 arg->client_name = strdup(client_name);
2823 if (!arg->client_name) {
2824 ERRMEM;
2825 free(arg);
2826 return -1;
2827 }
2828 arg->session_clb = session_clb;
2829
2830 ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg);
2831 if (ret) {
2832 ERR("Creating a new thread failed (%s).", strerror(ret));
2833 free(arg->client_name);
2834 free(arg);
2835 return -1;
2836 }
2837 /* the thread now manages arg */
2838
2839 pthread_detach(tid);
2840
2841 return 0;
2842}
2843
Radek Krejci53691be2016-02-22 13:58:37 +01002844#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02002845
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002846API time_t
2847nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02002848{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002849 if (!session || (session->side != NC_SERVER)) {
Michal Vaskof8352352016-05-24 09:11:36 +02002850 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002851 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02002852 }
2853
Michal Vasko2e6defd2016-10-07 15:48:15 +02002854 return session->opts.server.session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02002855}