blob: fa1ad85daeb0fd15b23a758200423cb0318406ef [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 server session manipulation functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <stdint.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010025#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028
Michal Vasko1a38c862016-01-15 15:50:07 +010029#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010030#include "session_server.h"
31
Michal Vaskob48aa812016-01-18 14:13:09 +010032struct nc_server_opts server_opts = {
Michal Vasko3031aae2016-01-27 16:07:18 +010033 .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010034};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010035
Michal Vasko3031aae2016-01-27 16:07:18 +010036extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010037extern pthread_mutex_t ssh_ch_opts_lock;
38
Michal Vasko3031aae2016-01-27 16:07:18 +010039extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010040extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010041
42struct nc_endpt *
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010043nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +010044{
45 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010046 struct nc_endpt *endpt = NULL;
47
48 /* READ LOCK */
49 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010050
51 for (i = 0; i < server_opts.endpt_count; ++i) {
52 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010053 endpt = &server_opts.endpts[i];
54 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010055 }
56 }
57
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010058 if (!endpt) {
59 ERR("Endpoint \"%s\" was not found.", name);
60 /* READ UNLOCK */
61 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
62 return NULL;
63 }
64
65 /* ENDPT LOCK */
66 pthread_mutex_lock(&endpt->endpt_lock);
67
68 return endpt;
69}
70
71void
72nc_server_endpt_unlock(struct nc_endpt *endpt)
73{
74 /* ENDPT UNLOCK */
75 pthread_mutex_unlock(&endpt->endpt_lock);
76
77 /* READ UNLOCK */
Michal Vasko27562ad2016-02-02 15:50:39 +010078 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010079}
Michal Vasko086311b2016-01-08 09:53:11 +010080
Michal Vasko1a38c862016-01-15 15:50:07 +010081API void
82nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
83{
84 if (!session || !reason) {
85 ERRARG;
86 return;
87 }
88
89 session->term_reason = reason;
90}
91
Michal Vasko086311b2016-01-08 09:53:11 +010092int
Michal Vaskof05562c2016-01-20 12:06:43 +010093nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +010094{
95 const int optVal = 1;
96 const socklen_t optLen = sizeof(optVal);
97 int is_ipv4, sock;
98 struct sockaddr_storage saddr;
99
100 struct sockaddr_in *saddr4;
101 struct sockaddr_in6 *saddr6;
102
103
104 if (!strchr(address, ':')) {
105 is_ipv4 = 1;
106 } else {
107 is_ipv4 = 0;
108 }
109
110 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
111 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100112 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100113 goto fail;
114 }
115
116 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100117 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100118 goto fail;
119 }
120
121 bzero(&saddr, sizeof(struct sockaddr_storage));
122 if (is_ipv4) {
123 saddr4 = (struct sockaddr_in *)&saddr;
124
125 saddr4->sin_family = AF_INET;
126 saddr4->sin_port = htons(port);
127
128 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100129 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100130 goto fail;
131 }
132
133 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100134 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100135 goto fail;
136 }
137
138 } else {
139 saddr6 = (struct sockaddr_in6 *)&saddr;
140
141 saddr6->sin6_family = AF_INET6;
142 saddr6->sin6_port = htons(port);
143
144 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100145 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100146 goto fail;
147 }
148
149 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100150 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100151 goto fail;
152 }
153 }
154
Michal Vaskofb89d772016-01-08 12:25:35 +0100155 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100156 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100157 goto fail;
158 }
159
160 return sock;
161
162fail:
163 if (sock > -1) {
164 close(sock);
165 }
166
167 return -1;
168}
169
170int
Michal Vasko3031aae2016-01-27 16:07:18 +0100171nc_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 +0100172{
173 uint16_t i;
174 struct pollfd *pfd;
175 struct sockaddr_storage saddr;
176 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100177 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100178
179 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100180 if (!pfd) {
181 ERRMEM;
182 return -1;
183 }
184
Michal Vasko086311b2016-01-08 09:53:11 +0100185 for (i = 0; i < bind_count; ++i) {
186 pfd[i].fd = binds[i].sock;
187 pfd[i].events = POLLIN;
188 pfd[i].revents = 0;
189 }
190
191 /* poll for a new connection */
192 errno = 0;
193 ret = poll(pfd, bind_count, timeout);
194 if (!ret) {
195 /* we timeouted */
196 free(pfd);
197 return 0;
198 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100199 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100200 free(pfd);
201 return -1;
202 }
203
204 for (i = 0; i < bind_count; ++i) {
205 if (pfd[i].revents & POLLIN) {
206 sock = pfd[i].fd;
207 break;
208 }
209 }
210 free(pfd);
211
212 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100213 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100214 return -1;
215 }
216
217 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100218 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100219 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100220 return -1;
221 }
222
Michal Vasko0190bc32016-03-02 15:47:49 +0100223 /* make the socket non-blocking */
224 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
225 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100226 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100227 return -1;
228 }
229
Michal Vasko3031aae2016-01-27 16:07:18 +0100230 if (idx) {
231 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100232 }
233
Michal Vasko086311b2016-01-08 09:53:11 +0100234 /* host was requested */
235 if (host) {
236 if (saddr.ss_family == AF_INET) {
237 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100238 if (*host) {
239 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
240 ERR("inet_ntop failed (%s).", strerror(errno));
241 free(*host);
242 *host = NULL;
243 }
Michal Vasko086311b2016-01-08 09:53:11 +0100244
Michal Vasko4eb3c312016-03-01 14:09:37 +0100245 if (port) {
246 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
247 }
248 } else {
249 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100250 }
251 } else if (saddr.ss_family == AF_INET6) {
252 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100253 if (*host) {
254 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
255 ERR("inet_ntop failed (%s).", strerror(errno));
256 free(*host);
257 *host = NULL;
258 }
Michal Vasko086311b2016-01-08 09:53:11 +0100259
Michal Vasko4eb3c312016-03-01 14:09:37 +0100260 if (port) {
261 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
262 }
263 } else {
264 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100265 }
266 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100267 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100268 }
269 }
270
271 return ret;
272}
273
Michal Vasko05ba9df2016-01-13 14:40:27 +0100274static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100275nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100276{
277 const char *identifier = NULL, *version = NULL, *format = NULL;
278 char *model_data = NULL;
279 const struct lys_module *module;
280 struct nc_server_error *err;
281 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100282 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100283
284 LY_TREE_FOR(rpc->child, child) {
285 if (!strcmp(child->schema->name, "identifier")) {
286 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
287 } else if (!strcmp(child->schema->name, "version")) {
288 version = ((struct lyd_node_leaf_list *)child)->value_str;
289 } else if (!strcmp(child->schema->name, "format")) {
290 format = ((struct lyd_node_leaf_list *)child)->value_str;
291 }
292 }
293
294 /* check version */
295 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100296 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
297 nc_err_set_msg(err, "The requested version is not supported.", "en");
298 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100299 }
300
301 /* check and get module with the name identifier */
302 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
303 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100304 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
305 nc_err_set_msg(err, "The requested schema was not found.", "en");
306 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100307 }
308
309 /* check format */
310 if (!format || !strcmp(format, "yang")) {
311 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
312 } else if (!strcmp(format, "yin")) {
313 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
314 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100315 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
316 nc_err_set_msg(err, "The requested format is not supported.", "en");
317 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100318 }
319
Michal Vasko303245c2016-03-24 15:20:03 +0100320 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vasko0473c4c2016-01-19 10:40:06 +0100321 if (model_data && sdata) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100322 data = lyd_output_new_anyxml(sdata, model_data);
323 }
324 free(model_data);
325 if (!data) {
326 ERRINT;
327 return NULL;
328 }
329
330 return nc_server_reply_data(data, NC_PARAMTYPE_FREE);
331}
332
333static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100334nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100335{
Michal Vasko428087d2016-01-14 16:04:28 +0100336 session->term_reason = NC_SESSION_TERM_CLOSED;
337 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100338}
339
Michal Vasko086311b2016-01-08 09:53:11 +0100340API int
341nc_server_init(struct ly_ctx *ctx)
342{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343 const struct lys_node *rpc;
344
Michal Vasko086311b2016-01-08 09:53:11 +0100345 if (!ctx) {
346 ERRARG;
347 return -1;
348 }
349
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100350 nc_init();
351
Michal Vasko05ba9df2016-01-13 14:40:27 +0100352 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100353 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100354 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100355 lys_set_private(rpc, nc_clb_default_get_schema);
356 }
357
358 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100359 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100360 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100361 lys_set_private(rpc, nc_clb_default_close_session);
362 }
363
Michal Vasko086311b2016-01-08 09:53:11 +0100364 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100365
366 server_opts.new_session_id = 1;
367 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
368
Michal Vasko086311b2016-01-08 09:53:11 +0100369 return 0;
370}
371
Michal Vaskob48aa812016-01-18 14:13:09 +0100372API void
373nc_server_destroy(void)
374{
375 pthread_spin_destroy(&server_opts.sid_lock);
376
Radek Krejci53691be2016-02-22 13:58:37 +0100377#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3031aae2016-01-27 16:07:18 +0100378 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100379#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100380 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100381}
382
Michal Vasko086311b2016-01-08 09:53:11 +0100383API int
384nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
385{
386 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)
387 || (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT)))) {
388 ERRARG;
389 return -1;
390 }
391
392 server_opts.wd_basic_mode = basic_mode;
393 server_opts.wd_also_supported = also_supported;
394 return 0;
395}
396
Michal Vasko1a38c862016-01-15 15:50:07 +0100397API void
Michal Vasko086311b2016-01-08 09:53:11 +0100398nc_server_set_capab_interleave(int interleave_support)
399{
400 if (interleave_support) {
401 server_opts.interleave_capab = 1;
402 } else {
403 server_opts.interleave_capab = 0;
404 }
Michal Vasko086311b2016-01-08 09:53:11 +0100405}
406
Michal Vasko1a38c862016-01-15 15:50:07 +0100407API void
Michal Vasko086311b2016-01-08 09:53:11 +0100408nc_server_set_hello_timeout(uint16_t hello_timeout)
409{
Michal Vasko086311b2016-01-08 09:53:11 +0100410 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100411}
412
Michal Vasko1a38c862016-01-15 15:50:07 +0100413API void
Michal Vasko086311b2016-01-08 09:53:11 +0100414nc_server_set_idle_timeout(uint16_t idle_timeout)
415{
Michal Vasko086311b2016-01-08 09:53:11 +0100416 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100417}
418
419API int
Michal Vasko1a38c862016-01-15 15:50:07 +0100420nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100421{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100422 if (!server_opts.ctx || (fdin < 0) || (fdout < 0) || !username || !session) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100423 ERRARG;
424 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100425 }
426
427 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100428 *session = calloc(1, sizeof **session);
429 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100430 ERRMEM;
Michal Vasko1a38c862016-01-15 15:50:07 +0100431 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100432 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100433 (*session)->status = NC_STATUS_STARTING;
434 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100435
436 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100437 (*session)->ti_type = NC_TI_FD;
438 (*session)->ti.fd.in = fdin;
439 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100440
441 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100442 (*session)->flags = NC_SESSION_SHAREDCTX;
443 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100444
Michal Vaskob48aa812016-01-18 14:13:09 +0100445 /* assign new SID atomically */
446 pthread_spin_lock(&server_opts.sid_lock);
447 (*session)->id = server_opts.new_session_id++;
448 pthread_spin_unlock(&server_opts.sid_lock);
449
Michal Vasko086311b2016-01-08 09:53:11 +0100450 /* NETCONF handshake */
Michal Vasko1a38c862016-01-15 15:50:07 +0100451 if (nc_handshake(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100452 goto fail;
453 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100454 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100455 (*session)->last_rpc = time(NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100456
Michal Vasko1a38c862016-01-15 15:50:07 +0100457 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100458
459fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100460 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100461 *session = NULL;
462 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100463}
Michal Vasko9e036d52016-01-08 10:49:26 +0100464
Michal Vasko428087d2016-01-14 16:04:28 +0100465API struct nc_pollsession *
466nc_ps_new(void)
467{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100468 struct nc_pollsession *ps;
469
470 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100471 if (!ps) {
472 ERRMEM;
473 return NULL;
474 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100475 pthread_mutex_init(&ps->lock, NULL);
476
477 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100478}
479
480API void
481nc_ps_free(struct nc_pollsession *ps)
482{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100483 if (!ps) {
484 return;
485 }
486
Michal Vasko3a715132016-01-21 15:40:31 +0100487 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100488 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100489 pthread_mutex_destroy(&ps->lock);
490
Michal Vasko428087d2016-01-14 16:04:28 +0100491 free(ps);
492}
493
494API int
495nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
496{
497 if (!ps || !session) {
498 ERRARG;
499 return -1;
500 }
501
Michal Vasko48a63ed2016-03-01 09:48:21 +0100502 /* LOCK */
503 pthread_mutex_lock(&ps->lock);
504
Michal Vasko428087d2016-01-14 16:04:28 +0100505 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100506 ps->pfds = nc_realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
507 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
508 if (!ps->pfds || !ps->sessions) {
509 ERRMEM;
510 /* UNLOCK */
511 pthread_mutex_unlock(&ps->lock);
512 return -1;
513 }
Michal Vasko428087d2016-01-14 16:04:28 +0100514
515 switch (session->ti_type) {
516 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100517 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100518 break;
519
Radek Krejci53691be2016-02-22 13:58:37 +0100520#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100521 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100522 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100523 break;
524#endif
525
Radek Krejci53691be2016-02-22 13:58:37 +0100526#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100527 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100528 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100529 break;
530#endif
531
532 default:
533 ERRINT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100534 /* UNLOCK */
535 pthread_mutex_unlock(&ps->lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100536 return -1;
537 }
Michal Vasko3a715132016-01-21 15:40:31 +0100538 ps->pfds[ps->session_count - 1].events = POLLIN;
539 ps->pfds[ps->session_count - 1].revents = 0;
540 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100541
Michal Vasko48a63ed2016-03-01 09:48:21 +0100542 /* UNLOCK */
543 pthread_mutex_unlock(&ps->lock);
544
Michal Vasko428087d2016-01-14 16:04:28 +0100545 return 0;
546}
547
Michal Vasko48a63ed2016-03-01 09:48:21 +0100548static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100549_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100550{
551 uint16_t i;
552
Radek Krejcid5f978f2016-03-03 13:14:45 +0100553 if (index >= 0) {
554 i = (uint16_t)index;
555 goto remove;
556 }
Michal Vasko428087d2016-01-14 16:04:28 +0100557 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100558 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100559remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100560 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100561 if (i < ps->session_count) {
562 ps->sessions[i] = ps->sessions[ps->session_count];
563 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
564 } else if (!ps->session_count) {
565 free(ps->sessions);
566 ps->sessions = NULL;
567 free(ps->pfds);
568 ps->pfds = NULL;
569 }
Michal Vasko428087d2016-01-14 16:04:28 +0100570 return 0;
571 }
572 }
573
Michal Vaskof0537d82016-01-29 14:42:38 +0100574 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100575}
576
Michal Vasko48a63ed2016-03-01 09:48:21 +0100577API int
578nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
579{
580 int ret;
581
582 if (!ps || !session) {
583 ERRARG;
584 return -1;
585 }
586
587 /* LOCK */
588 pthread_mutex_lock(&ps->lock);
589
Radek Krejcid5f978f2016-03-03 13:14:45 +0100590 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100591
592 /* UNLOCK */
593 pthread_mutex_unlock(&ps->lock);
594
595 return ret;
596}
597
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100598API uint16_t
599nc_ps_session_count(struct nc_pollsession *ps)
600{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100601 uint16_t count;
602
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100603 if (!ps) {
604 ERRARG;
605 return 0;
606 }
607
Michal Vasko48a63ed2016-03-01 09:48:21 +0100608 /* LOCK */
609 pthread_mutex_lock(&ps->lock);
610
611 count = ps->session_count;
612
613 /* UNLOCK */
614 pthread_mutex_unlock(&ps->lock);
615
616 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100617}
618
Michal Vasko428087d2016-01-14 16:04:28 +0100619/* must be called holding the session lock! */
620static NC_MSG_TYPE
621nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
622{
623 struct lyxml_elem *xml = NULL;
624 NC_MSG_TYPE msgtype;
625
626 if (!session || !rpc) {
627 ERRARG;
628 return NC_MSG_ERROR;
629 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100630 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100631 return NC_MSG_ERROR;
632 }
633
634 msgtype = nc_read_msg(session, &xml);
635
636 switch (msgtype) {
637 case NC_MSG_RPC:
638 *rpc = malloc(sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100639 if (!*rpc) {
640 ERRMEM;
641 goto error;
642 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100643
Michal Vasko428087d2016-01-14 16:04:28 +0100644 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100645 if (!(*rpc)->tree) {
646 ERR("Session %u: received message failed to be parsed into a known RPC.", session->id);
647 msgtype = NC_MSG_NONE;
648 }
Michal Vasko428087d2016-01-14 16:04:28 +0100649 (*rpc)->root = xml;
650 break;
651 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100652 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100653 goto error;
654 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100655 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100656 goto error;
657 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100658 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100659 goto error;
660 default:
661 /* NC_MSG_ERROR - pass it out;
662 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
663 */
664 break;
665 }
666
667 return msgtype;
668
669error:
670 /* cleanup */
671 lyxml_free(server_opts.ctx, xml);
672
673 return NC_MSG_ERROR;
674}
675
676/* must be called holding the session lock! */
677static NC_MSG_TYPE
678nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
679{
680 nc_rpc_clb clb;
681 struct nc_server_reply *reply;
682 int ret;
683
Michal Vasko4a827e52016-03-03 10:59:00 +0100684 if (!rpc) {
685 ERRINT;
686 return NC_MSG_ERROR;
687 }
688
Michal Vasko428087d2016-01-14 16:04:28 +0100689 /* no callback, reply with a not-implemented error */
Michal Vaskofd100c92016-03-01 15:23:46 +0100690 if (!rpc->tree || !rpc->tree->schema->priv) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100691 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100692 } else {
Michal Vaskofd100c92016-03-01 15:23:46 +0100693 clb = (nc_rpc_clb)rpc->tree->schema->priv;
Michal Vasko428087d2016-01-14 16:04:28 +0100694 reply = clb(rpc->tree, session);
695 }
696
697 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100698 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100699 }
700
701 ret = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
702
703 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
704 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
705 session->status = NC_STATUS_INVALID;
706 }
707
708 if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100709 ERR("Session %u: failed to write reply.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100710 nc_server_reply_free(reply);
711 return NC_MSG_ERROR;
712 }
713 nc_server_reply_free(reply);
714
715 return NC_MSG_REPLY;
716}
717
718API int
719nc_ps_poll(struct nc_pollsession *ps, int timeout)
720{
721 int ret;
Michal Vasko3512e402016-01-28 16:22:34 +0100722 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100723 time_t cur_time;
Michal Vasko428087d2016-01-14 16:04:28 +0100724 NC_MSG_TYPE msgtype;
725 struct nc_session *session;
Michal Vasko4a827e52016-03-03 10:59:00 +0100726 struct nc_server_rpc *rpc = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +0100727
728 if (!ps || !ps->session_count) {
729 ERRARG;
730 return -1;
731 }
732
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100733 cur_time = time(NULL);
734
Michal Vasko48a63ed2016-03-01 09:48:21 +0100735 /* LOCK */
736 pthread_mutex_lock(&ps->lock);
737
Michal Vasko428087d2016-01-14 16:04:28 +0100738 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100739 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
740 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100741 ret = -1;
742 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100743 }
Michal Vaskobd8ef262016-01-20 11:09:27 +0100744
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100745 /* TODO invalidate only sessions without subscription */
Michal Vasko3a715132016-01-21 15:40:31 +0100746 if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) {
747 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
748 ps->sessions[i]->status = NC_STATUS_INVALID;
749 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100750 ret = 3;
751 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100752 }
753
Michal Vasko3a715132016-01-21 15:40:31 +0100754 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100755 break;
756 }
Michal Vasko428087d2016-01-14 16:04:28 +0100757 }
758
Michal Vaskobd8ef262016-01-20 11:09:27 +0100759 if (i == ps->session_count) {
Radek Krejci53691be2016-02-22 13:58:37 +0100760#ifdef NC_ENABLED_SSH
Michal Vasko3a715132016-01-21 15:40:31 +0100761retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +0100762#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +0100763 /* no leftover event */
764 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +0100765 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vaskobd8ef262016-01-20 11:09:27 +0100766 if (ret < 1) {
Michal Vasko48a63ed2016-03-01 09:48:21 +0100767 goto finish;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100768 }
Michal Vasko428087d2016-01-14 16:04:28 +0100769 }
770
Michal Vaskobd8ef262016-01-20 11:09:27 +0100771 /* find the first fd with POLLIN, we don't care if there are more now */
772 for (; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100773 if (ps->pfds[i].revents & POLLHUP) {
774 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
775 ps->sessions[i]->status = NC_STATUS_INVALID;
776 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100777 ret = 3;
778 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +0100779 } else if (ps->pfds[i].revents & POLLERR) {
780 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
781 ps->sessions[i]->status = NC_STATUS_INVALID;
782 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100783 ret = 3;
784 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +0100785 } else if (ps->pfds[i].revents & POLLIN) {
Radek Krejci53691be2016-02-22 13:58:37 +0100786#ifdef NC_ENABLED_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +0100787 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +0100788 uint16_t j;
789
Michal Vasko96164bf2016-01-21 15:41:58 +0100790 /* things are not that simple with SSH... */
Michal Vasko62be1ce2016-03-03 13:24:52 +0100791 ret = nc_ssh_pollin(ps->sessions[i], timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100792
793 /* clear POLLIN on sessions sharing this session's SSH session */
794 if ((ret == 1) || (ret >= 4)) {
795 for (j = i + 1; j < ps->session_count; ++j) {
796 if (ps->pfds[j].fd == ps->pfds[i].fd) {
797 ps->pfds[j].revents = 0;
798 }
799 }
800 }
801
802 /* actual event happened */
803 if ((ret <= 0) || (ret >= 3)) {
804 ps->pfds[i].revents = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100805 goto finish;
Michal Vasko96164bf2016-01-21 15:41:58 +0100806
807 /* event occurred on some other channel */
808 } else if (ret == 2) {
809 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100810 if (i == ps->session_count - 1) {
811 /* last session and it is not the right channel, ... */
Michal Vasko8c748832016-02-03 15:32:16 +0100812 if (!timeout) {
Michal Vasko428087d2016-01-14 16:04:28 +0100813 /* ... timeout is 0, so that is it */
Michal Vasko48a63ed2016-03-01 09:48:21 +0100814 ret = 0;
815 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100816 }
Michal Vasko8c748832016-02-03 15:32:16 +0100817 /* ... retry polling reasonable time apart ... */
818 usleep(NC_TIMEOUT_STEP);
819 if (timeout > 0) {
820 /* ... and decrease timeout, if not -1 */
Michal Vasko7b38e232016-02-26 15:01:07 +0100821 timeout -= NC_TIMEOUT_STEP * 1000;
Michal Vasko8c748832016-02-03 15:32:16 +0100822 }
823 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +0100824 }
825 /* check other sessions */
826 continue;
Michal Vasko428087d2016-01-14 16:04:28 +0100827 }
828 }
Radek Krejci53691be2016-02-22 13:58:37 +0100829#endif /* NC_ENABLED_SSH */
Michal Vasko428087d2016-01-14 16:04:28 +0100830
Michal Vaskobd8ef262016-01-20 11:09:27 +0100831 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +0100832 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100833 break;
834 }
835 }
836
837 if (i == ps->session_count) {
838 ERRINT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100839 ret = -1;
840 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100841 }
842
843 /* this is the session with some data available for reading */
Michal Vasko3a715132016-01-21 15:40:31 +0100844 session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +0100845
Michal Vaskobd8ef262016-01-20 11:09:27 +0100846 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko62be1ce2016-03-03 13:24:52 +0100847 ret = nc_timedlock(session->ti_lock, timeout);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100848 if (ret != 1) {
849 /* error or timeout */
Michal Vasko48a63ed2016-03-01 09:48:21 +0100850 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100851 }
852
853 msgtype = nc_recv_rpc(session, &rpc);
854 if (msgtype == NC_MSG_ERROR) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100855 pthread_mutex_unlock(session->ti_lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100856 if (session->status != NC_STATUS_RUNNING) {
Michal Vasko48a63ed2016-03-01 09:48:21 +0100857 ret = 3;
858 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100859 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100860 ret = -1;
861 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100862 }
863
Michal Vaskoca4a2422016-02-02 12:17:14 +0100864 /* NC_MSG_NONE is not a real (known) RPC */
865 if (msgtype == NC_MSG_RPC) {
866 session->last_rpc = time(NULL);
867 }
868
Michal Vasko428087d2016-01-14 16:04:28 +0100869 /* process RPC */
870 msgtype = nc_send_reply(session, rpc);
871
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100872 pthread_mutex_unlock(session->ti_lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100873
874 if (msgtype == NC_MSG_ERROR) {
Michal Vaskoca4a2422016-02-02 12:17:14 +0100875 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100876 ret = -1;
877 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100878 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100879 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +0100880
Michal Vaskobd8b4e12016-01-22 16:11:20 +0100881 /* status change takes precedence over leftover events (return 2) */
882 if (session->status != NC_STATUS_RUNNING) {
Michal Vasko48a63ed2016-03-01 09:48:21 +0100883 ret = 3;
884 goto finish;
Michal Vaskobd8b4e12016-01-22 16:11:20 +0100885 }
886
Michal Vaskobd8ef262016-01-20 11:09:27 +0100887 /* is there some other socket waiting? */
888 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100889 if (ps->pfds[i].revents) {
Michal Vasko48a63ed2016-03-01 09:48:21 +0100890 ret = 2;
891 goto finish;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100892 }
893 }
894
Michal Vasko48a63ed2016-03-01 09:48:21 +0100895 ret = 1;
896
897finish:
898 /* UNLOCK */
899 pthread_mutex_unlock(&ps->lock);
900 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100901}
902
Michal Vaskod09eae62016-02-01 10:32:52 +0100903API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100904nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +0100905{
906 uint16_t i;
907 struct nc_session *session;
908
Michal Vasko9a25e932016-02-01 10:36:42 +0100909 if (!ps) {
910 ERRARG;
911 return;
912 }
913
Michal Vasko48a63ed2016-03-01 09:48:21 +0100914 /* LOCK */
915 pthread_mutex_lock(&ps->lock);
Michal Vaskod09eae62016-02-01 10:32:52 +0100916
Michal Vasko48a63ed2016-03-01 09:48:21 +0100917 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +0100918 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100919 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100920 }
921 free(ps->sessions);
922 ps->sessions = NULL;
923 free(ps->pfds);
924 ps->pfds = NULL;
925 ps->session_count = 0;
926 } else {
927 for (i = 0; i < ps->session_count; ) {
928 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
929 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +0100930 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100931 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100932 continue;
933 }
934
935 ++i;
936 }
Michal Vaskod09eae62016-02-01 10:32:52 +0100937 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100938
939 /* UNLOCK */
940 pthread_mutex_unlock(&ps->lock);
Michal Vaskod09eae62016-02-01 10:32:52 +0100941}
942
Radek Krejci53691be2016-02-22 13:58:37 +0100943#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100944
Michal Vasko3031aae2016-01-27 16:07:18 +0100945int
946nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +0100947{
948 int sock;
Michal Vasko3031aae2016-01-27 16:07:18 +0100949 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +0100950#ifdef NC_ENABLED_SSH
Michal Vasko08a629a2016-02-02 12:20:47 +0100951 struct nc_server_ssh_opts *ssh_opts;
952#endif
Michal Vasko9e036d52016-01-08 10:49:26 +0100953
Michal Vasko3031aae2016-01-27 16:07:18 +0100954 if (!name || !address || !port) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100955 ERRARG;
956 return -1;
957 }
958
Michal Vasko51e514d2016-02-02 15:51:52 +0100959 /* WRITE LOCK */
960 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100961
962 /* check name uniqueness */
963 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskod4c03a82016-02-08 15:27:26 +0100964 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100965 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +0100966 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +0100967 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100968 return -1;
969 }
970 }
971
Michal Vasko9e036d52016-01-08 10:49:26 +0100972 sock = nc_sock_listen(address, port);
973 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +0100974 /* WRITE UNLOCK */
975 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +0100976 return -1;
977 }
978
Michal Vasko3031aae2016-01-27 16:07:18 +0100979 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100980 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
981 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
982 if (!server_opts.binds || !server_opts.endpts) {
983 ERRMEM;
984 /* WRITE UNLOCK */
985 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko0f74da52016-03-03 08:52:52 +0100986 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100987 return -1;
988 }
Michal Vasko9e036d52016-01-08 10:49:26 +0100989
Michal Vasko3031aae2016-01-27 16:07:18 +0100990 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
991 server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0);
Michal Vasko3031aae2016-01-27 16:07:18 +0100992 server_opts.binds[server_opts.endpt_count - 1].port = port;
993 server_opts.binds[server_opts.endpt_count - 1].sock = sock;
994 server_opts.binds[server_opts.endpt_count - 1].ti = ti;
995 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +0100996#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100997 case NC_TI_LIBSSH:
Michal Vasko08a629a2016-02-02 12:20:47 +0100998 ssh_opts = calloc(1, sizeof *ssh_opts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100999 if (!ssh_opts) {
1000 ERRMEM;
1001 /* WRITE UNLOCK */
1002 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1003 return -1;
1004 }
Michal Vasko08a629a2016-02-02 12:20:47 +01001005 /* set default values */
1006 ssh_opts->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1007 ssh_opts->auth_attempts = 3;
1008 ssh_opts->auth_timeout = 10;
1009
1010 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = ssh_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +01001011 break;
1012#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001013#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 case NC_TI_OPENSSL:
1015 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_tls_opts));
Michal Vasko4eb3c312016-03-01 14:09:37 +01001016 if (!server_opts.endpts[server_opts.endpt_count - 1].ti_opts) {
1017 ERRMEM;
1018 /* WRITE UNLOCK */
1019 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1020 return -1;
1021 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001022 break;
1023#endif
1024 default:
1025 ERRINT;
1026 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL;
1027 break;
1028 }
1029 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001030
Michal Vasko3031aae2016-01-27 16:07:18 +01001031 /* WRITE UNLOCK */
1032 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001033
Michal Vasko9e036d52016-01-08 10:49:26 +01001034 return 0;
1035}
1036
Michal Vasko3031aae2016-01-27 16:07:18 +01001037int
Michal Vaskoda514772016-02-01 11:32:01 +01001038nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1039{
1040 struct nc_endpt *endpt;
1041 struct nc_bind *bind = NULL;
1042 uint16_t i;
1043 int sock;
1044
1045 if (!endpt_name || (!address && !port) || (address && port) || !ti) {
1046 ERRARG;
1047 return -1;
1048 }
1049
Michal Vasko51e514d2016-02-02 15:51:52 +01001050 /* LOCK */
Michal Vaskoda514772016-02-01 11:32:01 +01001051 endpt = nc_server_endpt_lock(endpt_name, ti);
1052 if (!endpt) {
1053 return -1;
1054 }
1055
1056 /* we need to learn the index, to get the bind :-/ */
1057 for (i = 0; i < server_opts.endpt_count; ++i) {
1058 if (&server_opts.endpts[i] == endpt) {
1059 bind = &server_opts.binds[i];
1060 }
1061 }
1062 if (!bind) {
1063 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001064 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001065 }
1066
1067 if (address) {
1068 sock = nc_sock_listen(address, bind->port);
1069 } else {
1070 sock = nc_sock_listen(bind->address, port);
1071 }
1072 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001073 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001074 }
1075
1076 /* close old socket, update parameters */
1077 close(bind->sock);
1078 bind->sock = sock;
1079 if (address) {
1080 lydict_remove(server_opts.ctx, bind->address);
1081 bind->address = lydict_insert(server_opts.ctx, address, 0);
1082 } else {
1083 bind->port = port;
1084 }
1085
Michal Vasko51e514d2016-02-02 15:51:52 +01001086 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001087 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001088 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001089
1090fail:
1091 /* UNLOCK */
1092 nc_server_endpt_unlock(endpt);
1093 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001094}
1095
1096int
Michal Vasko3031aae2016-01-27 16:07:18 +01001097nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001098{
1099 uint32_t i;
1100 int ret = -1;
1101
Michal Vasko3031aae2016-01-27 16:07:18 +01001102 /* WRITE LOCK */
1103 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001104
Michal Vasko3031aae2016-01-27 16:07:18 +01001105 if (!name && !ti) {
1106 /* remove all */
Michal Vasko3031aae2016-01-27 16:07:18 +01001107 for (i = 0; i < server_opts.endpt_count; ++i) {
1108 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001109 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko51e514d2016-02-02 15:51:52 +01001110
Michal Vasko3031aae2016-01-27 16:07:18 +01001111 close(server_opts.binds[i].sock);
1112 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1113 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001114#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001115 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001116 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001117 break;
1118#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001119#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001120 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001121 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001122 break;
1123#endif
1124 default:
1125 ERRINT;
1126 break;
1127 }
1128 free(server_opts.endpts[i].ti_opts);
Michal Vasko9e036d52016-01-08 10:49:26 +01001129
Michal Vasko9e036d52016-01-08 10:49:26 +01001130 ret = 0;
1131 }
Michal Vasko7ddc5702016-02-08 15:29:39 +01001132 free(server_opts.binds);
1133 server_opts.binds = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001134 free(server_opts.endpts);
1135 server_opts.endpts = NULL;
1136 server_opts.endpt_count = 0;
1137
Michal Vasko1a38c862016-01-15 15:50:07 +01001138 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001139 /* remove one name endpoint or all ti endpoints */
1140 for (i = 0; i < server_opts.endpt_count; ++i) {
1141 if ((server_opts.binds[i].ti == ti) &&
1142 (!name || !strcmp(server_opts.endpts[i].name, name))) {
1143
Michal Vasko3031aae2016-01-27 16:07:18 +01001144 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001145 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001146 close(server_opts.binds[i].sock);
1147 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1148 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001149#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001150 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001151 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001152 break;
1153#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001154#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001155 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001156 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001157 break;
1158#endif
1159 default:
1160 ERRINT;
1161 break;
1162 }
1163 free(server_opts.endpts[i].ti_opts);
Michal Vasko1a38c862016-01-15 15:50:07 +01001164
Michal Vasko3031aae2016-01-27 16:07:18 +01001165 --server_opts.endpt_count;
Michal Vaskoc0256492016-02-02 12:19:06 +01001166 if (i < server_opts.endpt_count) {
1167 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1168 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1169 } else if (!server_opts.endpt_count) {
1170 free(server_opts.binds);
1171 server_opts.binds = NULL;
1172 free(server_opts.endpts);
1173 server_opts.endpts = NULL;
1174 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001175
1176 ret = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001177
1178 if (name) {
1179 /* one name endpoint removed, they are unique, we're done */
1180 break;
1181 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001182 }
1183 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001184 }
1185
Michal Vasko3031aae2016-01-27 16:07:18 +01001186 /* WRITE UNLOCK */
1187 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001188
Michal Vasko9e036d52016-01-08 10:49:26 +01001189 return ret;
1190}
1191
Michal Vasko1a38c862016-01-15 15:50:07 +01001192API int
1193nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001194{
Michal Vasko1a38c862016-01-15 15:50:07 +01001195 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001196 char *host = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001197 uint16_t port, idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001198
Michal Vasko51e514d2016-02-02 15:51:52 +01001199 if (!server_opts.ctx || !session) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001200 ERRARG;
Michal Vasko1a38c862016-01-15 15:50:07 +01001201 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001202 }
1203
Michal Vasko51e514d2016-02-02 15:51:52 +01001204 /* we have to hold WRITE for the whole time, since there is not
1205 * a way of downgrading the lock to READ */
1206 /* WRITE LOCK */
1207 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1208
1209 if (!server_opts.endpt_count) {
1210 ERRARG;
1211 /* WRITE UNLOCK */
1212 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1213 return -1;
1214 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001215
Michal Vasko3031aae2016-01-27 16:07:18 +01001216 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx);
Michal Vaskob48aa812016-01-18 14:13:09 +01001217
Michal Vasko50456e82016-02-02 12:16:08 +01001218 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001219 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001220 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001221 free(host);
Michal Vaskob48aa812016-01-18 14:13:09 +01001222 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001223 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001224 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001225
Michal Vasko1a38c862016-01-15 15:50:07 +01001226 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001227 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001228 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001229 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001230 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001231 ret = -1;
1232 goto fail;
Michal Vasko9e036d52016-01-08 10:49:26 +01001233 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001234 (*session)->status = NC_STATUS_STARTING;
1235 (*session)->side = NC_SERVER;
1236 (*session)->ctx = server_opts.ctx;
1237 (*session)->flags = NC_SESSION_SHAREDCTX;
1238 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1239 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001240
1241 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001242 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1243 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001244 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001245 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001246 ret = -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001247 goto fail;
1248 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001249 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001250
Michal Vasko2cc4c682016-03-01 09:16:48 +01001251 (*session)->data = server_opts.endpts[idx].ti_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +01001252
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001253 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001254#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001255 if (server_opts.binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001256 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001257 if (ret < 1) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001258 goto fail;
1259 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001260 } else
1261#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001262#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001263 if (server_opts.binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001264 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001265 if (ret < 1) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001266 goto fail;
1267 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001268 } else
1269#endif
1270 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001271 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001272 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001273 ret = -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001274 goto fail;
1275 }
1276
Michal Vasko2cc4c682016-03-01 09:16:48 +01001277 (*session)->data = NULL;
1278
Michal Vasko51e514d2016-02-02 15:51:52 +01001279 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001280 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1281
Michal Vaskob48aa812016-01-18 14:13:09 +01001282 /* assign new SID atomically */
1283 /* LOCK */
1284 pthread_spin_lock(&server_opts.sid_lock);
1285 (*session)->id = server_opts.new_session_id++;
1286 /* UNLOCK */
1287 pthread_spin_unlock(&server_opts.sid_lock);
1288
Michal Vasko9e036d52016-01-08 10:49:26 +01001289 /* NETCONF handshake */
Michal Vasko1a38c862016-01-15 15:50:07 +01001290 if (nc_handshake(*session)) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001291 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001292 *session = NULL;
1293 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001294 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001295 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001296
Michal Vasko1a38c862016-01-15 15:50:07 +01001297 return 1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001298
1299fail:
Michal Vasko3031aae2016-01-27 16:07:18 +01001300 /* WRITE UNLOCK */
1301 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1302
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001303 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001304 *session = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001305 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001306}
1307
Michal Vasko3031aae2016-01-27 16:07:18 +01001308int
Michal Vasko8f5270d2016-02-29 16:22:25 +01001309nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01001310{
1311 int sock, ret;
1312
Michal Vaskoc61c4492016-01-25 11:13:34 +01001313 if (!host || !port || !ti || !session) {
1314 ERRARG;
1315 return -1;
1316 }
1317
Michal Vaskob05053d2016-01-22 16:12:06 +01001318 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001319 if (sock < 0) {
1320 return -1;
Michal Vaskob05053d2016-01-22 16:12:06 +01001321 }
1322
1323 *session = calloc(1, sizeof **session);
1324 if (!(*session)) {
1325 ERRMEM;
1326 close(sock);
1327 return -1;
1328 }
1329 (*session)->status = NC_STATUS_STARTING;
1330 (*session)->side = NC_SERVER;
1331 (*session)->ctx = server_opts.ctx;
1332 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001333 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001334 (*session)->port = port;
1335
1336 /* transport lock */
1337 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1338 if (!(*session)->ti_lock) {
1339 ERRMEM;
1340 close(sock);
1341 ret = -1;
1342 goto fail;
1343 }
1344 pthread_mutex_init((*session)->ti_lock, NULL);
1345
1346 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001347#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001348 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001349 /* OPTS LOCK */
1350 pthread_mutex_lock(&ssh_ch_opts_lock);
1351
Michal Vasko2cc4c682016-03-01 09:16:48 +01001352 (*session)->data = &ssh_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001353 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001354 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001355
1356 /* OPTS UNLOCK */
1357 pthread_mutex_unlock(&ssh_ch_opts_lock);
1358
Michal Vaskob05053d2016-01-22 16:12:06 +01001359 if (ret < 1) {
1360 goto fail;
1361 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001362 } else
1363#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001364#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001365 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001366 /* OPTS LOCK */
1367 pthread_mutex_lock(&tls_ch_opts_lock);
1368
Michal Vasko2cc4c682016-03-01 09:16:48 +01001369 (*session)->data = &tls_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001370 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001371 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001372
1373 /* OPTS UNLOCK */
1374 pthread_mutex_unlock(&tls_ch_opts_lock);
1375
Michal Vaskob05053d2016-01-22 16:12:06 +01001376 if (ret < 1) {
1377 goto fail;
1378 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001379 } else
1380#endif
1381 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001382 ERRINT;
1383 close(sock);
1384 ret = -1;
1385 goto fail;
1386 }
1387
1388 /* assign new SID atomically */
1389 /* LOCK */
1390 pthread_spin_lock(&server_opts.sid_lock);
1391 (*session)->id = server_opts.new_session_id++;
1392 /* UNLOCK */
1393 pthread_spin_unlock(&server_opts.sid_lock);
1394
1395 /* NETCONF handshake */
1396 if (nc_handshake(*session)) {
1397 ret = -1;
1398 goto fail;
1399 }
1400 (*session)->status = NC_STATUS_RUNNING;
1401
1402 return 1;
1403
1404fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001405 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001406 *session = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001407 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +01001408}
1409
Radek Krejci53691be2016-02-22 13:58:37 +01001410#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */