blob: e9e06dceb7f99633675a34afb876c7c13aef39f8 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 server session manipulation functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#include <stdint.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010025#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028
Michal Vasko1a38c862016-01-15 15:50:07 +010029#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010030#include "session_server.h"
31
Michal Vaskob48aa812016-01-18 14:13:09 +010032struct nc_server_opts server_opts = {
Michal Vasko3031aae2016-01-27 16:07:18 +010033 .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010034};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010035
fanchanghu966f2de2016-07-21 02:28:57 -040036static nc_rpc_clb global_rpc_clb = NULL;
37
Michal Vasko3031aae2016-01-27 16:07:18 +010038extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010039extern pthread_mutex_t ssh_ch_opts_lock;
40
Michal Vasko3031aae2016-01-27 16:07:18 +010041extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010042extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010043
44struct nc_endpt *
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010045nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +010046{
47 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010048 struct nc_endpt *endpt = NULL;
49
50 /* READ LOCK */
51 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010052
53 for (i = 0; i < server_opts.endpt_count; ++i) {
54 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010055 endpt = &server_opts.endpts[i];
56 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010057 }
58 }
59
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010060 if (!endpt) {
61 ERR("Endpoint \"%s\" was not found.", name);
62 /* READ UNLOCK */
63 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
64 return NULL;
65 }
66
67 /* ENDPT LOCK */
68 pthread_mutex_lock(&endpt->endpt_lock);
69
70 return endpt;
71}
72
73void
74nc_server_endpt_unlock(struct nc_endpt *endpt)
75{
76 /* ENDPT UNLOCK */
77 pthread_mutex_unlock(&endpt->endpt_lock);
78
79 /* READ UNLOCK */
Michal Vasko27562ad2016-02-02 15:50:39 +010080 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010081}
Michal Vasko086311b2016-01-08 09:53:11 +010082
Michal Vasko1a38c862016-01-15 15:50:07 +010083API void
84nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
85{
Michal Vasko45e53ae2016-04-07 11:46:03 +020086 if (!session) {
87 ERRARG("session");
88 return;
89 } else if (!reason) {
90 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +010091 return;
92 }
93
94 session->term_reason = reason;
95}
96
Michal Vasko086311b2016-01-08 09:53:11 +010097int
Michal Vaskof05562c2016-01-20 12:06:43 +010098nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +010099{
100 const int optVal = 1;
101 const socklen_t optLen = sizeof(optVal);
102 int is_ipv4, sock;
103 struct sockaddr_storage saddr;
104
105 struct sockaddr_in *saddr4;
106 struct sockaddr_in6 *saddr6;
107
108
109 if (!strchr(address, ':')) {
110 is_ipv4 = 1;
111 } else {
112 is_ipv4 = 0;
113 }
114
115 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
116 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100117 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100118 goto fail;
119 }
120
121 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100122 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100123 goto fail;
124 }
125
126 bzero(&saddr, sizeof(struct sockaddr_storage));
127 if (is_ipv4) {
128 saddr4 = (struct sockaddr_in *)&saddr;
129
130 saddr4->sin_family = AF_INET;
131 saddr4->sin_port = htons(port);
132
133 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100134 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100135 goto fail;
136 }
137
138 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100139 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100140 goto fail;
141 }
142
143 } else {
144 saddr6 = (struct sockaddr_in6 *)&saddr;
145
146 saddr6->sin6_family = AF_INET6;
147 saddr6->sin6_port = htons(port);
148
149 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100150 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100151 goto fail;
152 }
153
154 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100155 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100156 goto fail;
157 }
158 }
159
Michal Vaskofb89d772016-01-08 12:25:35 +0100160 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100161 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100162 goto fail;
163 }
164
165 return sock;
166
167fail:
168 if (sock > -1) {
169 close(sock);
170 }
171
172 return -1;
173}
174
175int
Michal Vasko3031aae2016-01-27 16:07:18 +0100176nc_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 +0100177{
178 uint16_t i;
179 struct pollfd *pfd;
180 struct sockaddr_storage saddr;
181 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100182 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100183
184 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100185 if (!pfd) {
186 ERRMEM;
187 return -1;
188 }
189
Michal Vasko086311b2016-01-08 09:53:11 +0100190 for (i = 0; i < bind_count; ++i) {
191 pfd[i].fd = binds[i].sock;
192 pfd[i].events = POLLIN;
193 pfd[i].revents = 0;
194 }
195
196 /* poll for a new connection */
197 errno = 0;
198 ret = poll(pfd, bind_count, timeout);
199 if (!ret) {
200 /* we timeouted */
201 free(pfd);
202 return 0;
203 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100204 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100205 free(pfd);
206 return -1;
207 }
208
209 for (i = 0; i < bind_count; ++i) {
210 if (pfd[i].revents & POLLIN) {
211 sock = pfd[i].fd;
212 break;
213 }
214 }
215 free(pfd);
216
217 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100218 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100219 return -1;
220 }
221
222 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100223 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100224 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100225 return -1;
226 }
227
Michal Vasko0190bc32016-03-02 15:47:49 +0100228 /* make the socket non-blocking */
229 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
230 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100231 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100232 return -1;
233 }
234
Michal Vasko3031aae2016-01-27 16:07:18 +0100235 if (idx) {
236 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100237 }
238
Michal Vasko086311b2016-01-08 09:53:11 +0100239 /* host was requested */
240 if (host) {
241 if (saddr.ss_family == AF_INET) {
242 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100243 if (*host) {
244 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
245 ERR("inet_ntop failed (%s).", strerror(errno));
246 free(*host);
247 *host = NULL;
248 }
Michal Vasko086311b2016-01-08 09:53:11 +0100249
Michal Vasko4eb3c312016-03-01 14:09:37 +0100250 if (port) {
251 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
252 }
253 } else {
254 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100255 }
256 } else if (saddr.ss_family == AF_INET6) {
257 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100258 if (*host) {
259 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
260 ERR("inet_ntop failed (%s).", strerror(errno));
261 free(*host);
262 *host = NULL;
263 }
Michal Vasko086311b2016-01-08 09:53:11 +0100264
Michal Vasko4eb3c312016-03-01 14:09:37 +0100265 if (port) {
266 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
267 }
268 } else {
269 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100270 }
271 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100272 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100273 }
274 }
275
276 return ret;
277}
278
Michal Vasko05ba9df2016-01-13 14:40:27 +0100279static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100280nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100281{
282 const char *identifier = NULL, *version = NULL, *format = NULL;
283 char *model_data = NULL;
284 const struct lys_module *module;
285 struct nc_server_error *err;
286 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100287 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100288
289 LY_TREE_FOR(rpc->child, child) {
290 if (!strcmp(child->schema->name, "identifier")) {
291 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
292 } else if (!strcmp(child->schema->name, "version")) {
293 version = ((struct lyd_node_leaf_list *)child)->value_str;
294 } else if (!strcmp(child->schema->name, "format")) {
295 format = ((struct lyd_node_leaf_list *)child)->value_str;
296 }
297 }
298
299 /* check version */
300 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100301 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
302 nc_err_set_msg(err, "The requested version is not supported.", "en");
303 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100304 }
305
306 /* check and get module with the name identifier */
307 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
308 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200309 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
310 }
311 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100312 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
313 nc_err_set_msg(err, "The requested schema was not found.", "en");
314 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100315 }
316
317 /* check format */
318 if (!format || !strcmp(format, "yang")) {
319 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
320 } else if (!strcmp(format, "yin")) {
321 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
322 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100323 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
324 nc_err_set_msg(err, "The requested format is not supported.", "en");
325 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100326 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200327 if (!model_data) {
328 ERRINT;
329 return NULL;
330 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100331
Michal Vasko303245c2016-03-24 15:20:03 +0100332 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200333 if (!sdata) {
334 ERRINT;
335 free(model_data);
336 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100337 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200338
Michal Vaskob2583f12016-05-12 11:40:23 +0200339 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data, LYD_PATH_OPT_OUTPUT);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100340 if (!data) {
341 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200342 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343 return NULL;
344 }
Michal Vaskob2583f12016-05-12 11:40:23 +0200345 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100346
347 return nc_server_reply_data(data, NC_PARAMTYPE_FREE);
348}
349
350static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100351nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100352{
Michal Vasko428087d2016-01-14 16:04:28 +0100353 session->term_reason = NC_SESSION_TERM_CLOSED;
354 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100355}
356
Michal Vasko086311b2016-01-08 09:53:11 +0100357API int
358nc_server_init(struct ly_ctx *ctx)
359{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100360 const struct lys_node *rpc;
361
Michal Vasko086311b2016-01-08 09:53:11 +0100362 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200363 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100364 return -1;
365 }
366
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100367 nc_init();
368
Michal Vasko05ba9df2016-01-13 14:40:27 +0100369 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100370 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100371 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100372 lys_set_private(rpc, nc_clb_default_get_schema);
373 }
374
375 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100376 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100377 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100378 lys_set_private(rpc, nc_clb_default_close_session);
379 }
380
Michal Vasko086311b2016-01-08 09:53:11 +0100381 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100382
383 server_opts.new_session_id = 1;
384 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
385
Michal Vasko086311b2016-01-08 09:53:11 +0100386 return 0;
387}
388
Michal Vaskob48aa812016-01-18 14:13:09 +0100389API void
390nc_server_destroy(void)
391{
392 pthread_spin_destroy(&server_opts.sid_lock);
393
Radek Krejci53691be2016-02-22 13:58:37 +0100394#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3031aae2016-01-27 16:07:18 +0100395 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100396#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100397 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100398}
399
Michal Vasko086311b2016-01-08 09:53:11 +0100400API int
401nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
402{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200403 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
404 ERRARG("basic_mode");
405 return -1;
406 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
407 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100408 return -1;
409 }
410
411 server_opts.wd_basic_mode = basic_mode;
412 server_opts.wd_also_supported = also_supported;
413 return 0;
414}
415
Michal Vasko1a38c862016-01-15 15:50:07 +0100416API void
Michal Vasko55f03972016-04-13 08:56:01 +0200417nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
418{
419 if (!basic_mode && !also_supported) {
420 ERRARG("basic_mode and also_supported");
421 return;
422 }
423
424 if (basic_mode) {
425 *basic_mode = server_opts.wd_basic_mode;
426 }
427 if (also_supported) {
428 *also_supported = server_opts.wd_also_supported;
429 }
430}
431
432API void
Michal Vasko086311b2016-01-08 09:53:11 +0100433nc_server_set_capab_interleave(int interleave_support)
434{
435 if (interleave_support) {
436 server_opts.interleave_capab = 1;
437 } else {
438 server_opts.interleave_capab = 0;
439 }
Michal Vasko086311b2016-01-08 09:53:11 +0100440}
441
Michal Vasko55f03972016-04-13 08:56:01 +0200442API int
443nc_server_get_capab_interleave(void)
444{
445 return server_opts.interleave_capab;
446}
447
Michal Vasko1a38c862016-01-15 15:50:07 +0100448API void
Michal Vasko086311b2016-01-08 09:53:11 +0100449nc_server_set_hello_timeout(uint16_t hello_timeout)
450{
Michal Vasko086311b2016-01-08 09:53:11 +0100451 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100452}
453
Michal Vasko55f03972016-04-13 08:56:01 +0200454API uint16_t
455nc_server_get_hello_timeout(void)
456{
457 return server_opts.hello_timeout;
458}
459
Michal Vasko1a38c862016-01-15 15:50:07 +0100460API void
Michal Vasko086311b2016-01-08 09:53:11 +0100461nc_server_set_idle_timeout(uint16_t idle_timeout)
462{
Michal Vasko086311b2016-01-08 09:53:11 +0100463 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100464}
465
Michal Vasko55f03972016-04-13 08:56:01 +0200466API uint16_t
467nc_server_get_idle_timeout(void)
468{
469 return server_opts.idle_timeout;
470}
471
Michal Vasko71090fc2016-05-24 16:37:28 +0200472API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100473nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100474{
Michal Vasko71090fc2016-05-24 16:37:28 +0200475 NC_MSG_TYPE msgtype;
476
Michal Vasko45e53ae2016-04-07 11:46:03 +0200477 if (!server_opts.ctx) {
478 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200479 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200480 } else if (fdin < 0) {
481 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200482 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200483 } else if (fdout < 0) {
484 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200485 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200486 } else if (!username) {
487 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200488 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200489 } else if (!session) {
490 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200491 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100492 }
493
494 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100495 *session = calloc(1, sizeof **session);
496 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100497 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200498 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100499 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100500 (*session)->status = NC_STATUS_STARTING;
501 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100502
503 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100504 (*session)->ti_type = NC_TI_FD;
505 (*session)->ti.fd.in = fdin;
506 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100507
508 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100509 (*session)->flags = NC_SESSION_SHAREDCTX;
510 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100511
Michal Vaskob48aa812016-01-18 14:13:09 +0100512 /* assign new SID atomically */
513 pthread_spin_lock(&server_opts.sid_lock);
514 (*session)->id = server_opts.new_session_id++;
515 pthread_spin_unlock(&server_opts.sid_lock);
516
Michal Vasko086311b2016-01-08 09:53:11 +0100517 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200518 msgtype = nc_handshake(*session);
519 if (msgtype != NC_MSG_HELLO) {
520 nc_session_free(*session, NULL);
521 *session = NULL;
522 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100523 }
Michal Vaskof8352352016-05-24 09:11:36 +0200524 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100525 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100526
Michal Vasko71090fc2016-05-24 16:37:28 +0200527 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100528}
Michal Vasko9e036d52016-01-08 10:49:26 +0100529
Michal Vaskob30b99c2016-07-26 11:35:43 +0200530static void
531nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
532{
533 uint8_t i, found = 0;
534
535 for (i = 0; i < ps->queue_len; ++i) {
536 /* idx round buffer adjust */
537 if (ps->queue_begin + i == NC_PS_QUEUE_SIZE) {
538 i = -ps->queue_begin;
539 }
540
541 if (found) {
542 /* move the value back one place */
543 if (ps->queue[ps->queue_begin + i] == id) {
544 /* another equal value, simply cannot be */
545 ERRINT;
546 }
547
548 if (ps->queue_begin + i == 0) {
549 ps->queue[NC_PS_QUEUE_SIZE - 1] = ps->queue[ps->queue_begin + i];
550 } else {
551 ps->queue[ps->queue_begin + i - 1] = ps->queue[ps->queue_begin + i];
552 }
553 } else if (ps->queue[ps->queue_begin + i] == id) {
554 /* found our id, there can be no more equal valid values */
555 found = 1;
556 }
557 }
558
559 if (!found) {
560 ERRINT;
561 }
562 --ps->queue_len;
563}
564
Michal Vaskof04a52a2016-04-07 10:52:10 +0200565int
Michal Vaskob30b99c2016-07-26 11:35:43 +0200566nc_ps_lock(struct nc_pollsession *ps, uint8_t *id)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200567{
568 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200569 uint8_t queue_last;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200570 struct timespec ts;
571
Radek Krejci7ac16052016-07-15 11:48:18 +0200572 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200573 ts.tv_sec += NC_READ_TIMEOUT;
574
575 /* LOCK */
576 ret = pthread_mutex_timedlock(&ps->lock, &ts);
577 if (ret) {
578 ERR("Failed to lock a pollsession (%s).", strerror(ret));
579 return -1;
580 }
581
582 /* get a unique queue value (by adding 1 to the last added value, if any) */
583 if (ps->queue_len) {
584 queue_last = ps->queue_begin + ps->queue_len - 1;
585 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
586 queue_last -= NC_PS_QUEUE_SIZE;
587 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200588 *id = ps->queue[queue_last] + 1;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200589 } else {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200590 *id = 0;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200591 }
592
593 /* add ourselves into the queue */
594 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
595 ERR("Pollsession queue too small.");
596 return -1;
597 }
598 ++ps->queue_len;
599 queue_last = ps->queue_begin + ps->queue_len - 1;
600 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
601 queue_last -= NC_PS_QUEUE_SIZE;
602 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200603 ps->queue[queue_last] = *id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200604
605 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200606 while (ps->queue[ps->queue_begin] != *id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200607 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200608 ts.tv_sec += NC_READ_TIMEOUT;
609
610 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
611 if (ret) {
612 ERR("Failed to wait for a pollsession condition (%s).", strerror(ret));
613 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200614 nc_ps_queue_remove_id(ps, *id);
615 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200616 return -1;
617 }
618 }
619
Michal Vaskobe86fe32016-04-07 10:43:03 +0200620 /* UNLOCK */
621 pthread_mutex_unlock(&ps->lock);
622
623 return 0;
624}
625
Michal Vaskof04a52a2016-04-07 10:52:10 +0200626int
Michal Vaskob30b99c2016-07-26 11:35:43 +0200627nc_ps_unlock(struct nc_pollsession *ps, uint8_t id)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200628{
629 int ret;
630 struct timespec ts;
631
Radek Krejci7ac16052016-07-15 11:48:18 +0200632 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200633 ts.tv_sec += NC_READ_TIMEOUT;
634
635 /* LOCK */
636 ret = pthread_mutex_timedlock(&ps->lock, &ts);
637 if (ret) {
638 ERR("Failed to lock a pollsession (%s).", strerror(ret));
639 ret = -1;
640 }
641
Michal Vaskob30b99c2016-07-26 11:35:43 +0200642 /* we must be the first, it was our turn after all, right? */
643 if (ps->queue[ps->queue_begin] != id) {
644 ERRINT;
645 return -1;
646 }
647
Michal Vaskobe86fe32016-04-07 10:43:03 +0200648 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200649 nc_ps_queue_remove_id(ps, id);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200650
651 /* broadcast to all other threads that the queue moved */
652 pthread_cond_broadcast(&ps->cond);
653
Michal Vaskobe86fe32016-04-07 10:43:03 +0200654 /* UNLOCK */
655 if (!ret) {
656 pthread_mutex_unlock(&ps->lock);
657 }
658
659 return ret;
660}
661
Michal Vasko428087d2016-01-14 16:04:28 +0100662API struct nc_pollsession *
663nc_ps_new(void)
664{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100665 struct nc_pollsession *ps;
666
667 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100668 if (!ps) {
669 ERRMEM;
670 return NULL;
671 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200672 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100673 pthread_mutex_init(&ps->lock, NULL);
674
675 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100676}
677
678API void
679nc_ps_free(struct nc_pollsession *ps)
680{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100681 if (!ps) {
682 return;
683 }
684
Michal Vaskobe86fe32016-04-07 10:43:03 +0200685 if (ps->queue_len) {
686 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
687 }
688
Michal Vasko3a715132016-01-21 15:40:31 +0100689 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100690 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100691 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200692 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100693
Michal Vasko428087d2016-01-14 16:04:28 +0100694 free(ps);
695}
696
697API int
698nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
699{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200700 uint8_t q_id;
701
Michal Vasko45e53ae2016-04-07 11:46:03 +0200702 if (!ps) {
703 ERRARG("ps");
704 return -1;
705 } else if (!session) {
706 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100707 return -1;
708 }
709
Michal Vasko48a63ed2016-03-01 09:48:21 +0100710 /* LOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200711 if (nc_ps_lock(ps, &q_id)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200712 return -1;
713 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100714
Michal Vasko428087d2016-01-14 16:04:28 +0100715 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100716 ps->pfds = nc_realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
717 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
718 if (!ps->pfds || !ps->sessions) {
719 ERRMEM;
720 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200721 nc_ps_unlock(ps, q_id);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100722 return -1;
723 }
Michal Vasko428087d2016-01-14 16:04:28 +0100724
725 switch (session->ti_type) {
726 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100727 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100728 break;
729
Radek Krejci53691be2016-02-22 13:58:37 +0100730#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100731 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100732 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100733 break;
734#endif
735
Radek Krejci53691be2016-02-22 13:58:37 +0100736#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100737 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100738 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100739 break;
740#endif
741
742 default:
743 ERRINT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100744 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200745 nc_ps_unlock(ps, q_id);
Michal Vasko428087d2016-01-14 16:04:28 +0100746 return -1;
747 }
Michal Vasko3a715132016-01-21 15:40:31 +0100748 ps->pfds[ps->session_count - 1].events = POLLIN;
749 ps->pfds[ps->session_count - 1].revents = 0;
750 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100751
Michal Vasko48a63ed2016-03-01 09:48:21 +0100752 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200753 return nc_ps_unlock(ps, q_id);
Michal Vasko428087d2016-01-14 16:04:28 +0100754}
755
Michal Vasko48a63ed2016-03-01 09:48:21 +0100756static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100757_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100758{
759 uint16_t i;
760
Radek Krejcid5f978f2016-03-03 13:14:45 +0100761 if (index >= 0) {
762 i = (uint16_t)index;
763 goto remove;
764 }
Michal Vasko428087d2016-01-14 16:04:28 +0100765 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100766 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100767remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100768 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100769 if (i < ps->session_count) {
770 ps->sessions[i] = ps->sessions[ps->session_count];
771 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
772 } else if (!ps->session_count) {
773 free(ps->sessions);
774 ps->sessions = NULL;
775 free(ps->pfds);
776 ps->pfds = NULL;
777 }
Michal Vasko428087d2016-01-14 16:04:28 +0100778 return 0;
779 }
780 }
781
Michal Vaskof0537d82016-01-29 14:42:38 +0100782 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100783}
784
Michal Vasko48a63ed2016-03-01 09:48:21 +0100785API int
786nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
787{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200788 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200789 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100790
Michal Vasko45e53ae2016-04-07 11:46:03 +0200791 if (!ps) {
792 ERRARG("ps");
793 return -1;
794 } else if (!session) {
795 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100796 return -1;
797 }
798
799 /* LOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200800 if (nc_ps_lock(ps, &q_id)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200801 return -1;
802 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100803
Radek Krejcid5f978f2016-03-03 13:14:45 +0100804 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100805
806 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200807 ret2 = nc_ps_unlock(ps, q_id);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100808
Michal Vaskobe86fe32016-04-07 10:43:03 +0200809 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100810}
811
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100812API uint16_t
813nc_ps_session_count(struct nc_pollsession *ps)
814{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200815 uint8_t q_id;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100816 uint16_t count;
817
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100818 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200819 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100820 return 0;
821 }
822
Michal Vasko48a63ed2016-03-01 09:48:21 +0100823 /* LOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200824 if (nc_ps_lock(ps, &q_id)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200825 return -1;
826 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100827
828 count = ps->session_count;
829
830 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200831 nc_ps_unlock(ps, q_id);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100832
833 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100834}
835
Michal Vasko71090fc2016-05-24 16:37:28 +0200836/* must be called holding the session lock!
837 * returns: NC_PSPOLL_ERROR,
838 * NC_PSPOLL_BAD_RPC,
839 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
840 * NC_PSPOLL_RPC
841 */
842static int
Michal Vasko428087d2016-01-14 16:04:28 +0100843nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
844{
845 struct lyxml_elem *xml = NULL;
846 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200847 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200848 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100849
Michal Vasko45e53ae2016-04-07 11:46:03 +0200850 if (!session) {
851 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200852 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200853 } else if (!rpc) {
854 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200855 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100856 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100857 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200858 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100859 }
860
861 msgtype = nc_read_msg(session, &xml);
862
863 switch (msgtype) {
864 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200865 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100866 if (!*rpc) {
867 ERRMEM;
868 goto error;
869 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100870
Radek Krejcif93c7d42016-04-06 13:41:15 +0200871 ly_errno = LY_SUCCESS;
Michal Vasko428087d2016-01-14 16:04:28 +0100872 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100873 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200874 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200875 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200876 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200877 nc_server_reply_free(reply);
878 if (ret == -1) {
879 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200880 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200881 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
882 } else {
883 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100884 }
Michal Vasko428087d2016-01-14 16:04:28 +0100885 (*rpc)->root = xml;
886 break;
887 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100888 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200889 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100890 goto error;
891 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100892 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200893 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100894 goto error;
895 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100896 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200897 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100898 goto error;
899 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200900 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100901 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
902 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200903 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100904 break;
905 }
906
Michal Vasko71090fc2016-05-24 16:37:28 +0200907 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100908
909error:
910 /* cleanup */
911 lyxml_free(server_opts.ctx, xml);
912
Michal Vasko71090fc2016-05-24 16:37:28 +0200913 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100914}
915
fanchanghu966f2de2016-07-21 02:28:57 -0400916API void
917nc_set_global_rpc_clb(nc_rpc_clb clb)
918{
919 global_rpc_clb = clb;
920}
921
Michal Vasko71090fc2016-05-24 16:37:28 +0200922/* must be called holding the session lock!
923 * returns: NC_PSPOLL_ERROR,
924 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
925 * NC_PSPOLL_REPLY_ERROR,
926 * 0
927 */
928static int
Michal Vasko428087d2016-01-14 16:04:28 +0100929nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
930{
931 nc_rpc_clb clb;
932 struct nc_server_reply *reply;
Michal Vasko71090fc2016-05-24 16:37:28 +0200933 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +0100934
Michal Vasko4a827e52016-03-03 10:59:00 +0100935 if (!rpc) {
936 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200937 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +0100938 }
939
fanchanghu966f2de2016-07-21 02:28:57 -0400940 if (rpc->tree->schema->priv) {
941 clb = (nc_rpc_clb) rpc->tree->schema->priv;
Michal Vasko428087d2016-01-14 16:04:28 +0100942 reply = clb(rpc->tree, session);
fanchanghu966f2de2016-07-21 02:28:57 -0400943 } else {
944 if (global_rpc_clb) {
945 reply = global_rpc_clb(rpc->tree, session);
946 } else { /* no callback, reply with a not-implemented error */
947 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
948 }
Michal Vasko428087d2016-01-14 16:04:28 +0100949 }
950
951 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100952 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100953 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200954 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
955 if (reply->type == NC_RPL_ERROR) {
956 ret |= NC_PSPOLL_REPLY_ERROR;
957 }
958 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +0100959
Michal Vasko71090fc2016-05-24 16:37:28 +0200960 if (r == -1) {
961 ERR("Session %u: failed to write reply.", session->id);
962 ret |= NC_PSPOLL_ERROR;
963 }
Michal Vasko428087d2016-01-14 16:04:28 +0100964
965 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
966 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
967 session->status = NC_STATUS_INVALID;
968 }
969
Michal Vasko71090fc2016-05-24 16:37:28 +0200970 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100971}
972
973API int
Michal Vasko71090fc2016-05-24 16:37:28 +0200974nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +0100975{
976 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200977 uint8_t q_id;
Michal Vasko3512e402016-01-28 16:22:34 +0100978 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100979 time_t cur_time;
Michal Vasko71090fc2016-05-24 16:37:28 +0200980 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +0100981 struct nc_server_rpc *rpc = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +0100982
983 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200984 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +0200985 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100986 }
987
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100988 cur_time = time(NULL);
989
Michal Vasko48a63ed2016-03-01 09:48:21 +0100990 /* LOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200991 if (nc_ps_lock(ps, &q_id)) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200992 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200993 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100994
Michal Vasko428087d2016-01-14 16:04:28 +0100995 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100996 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
997 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200998 ret = NC_PSPOLL_ERROR;
999 if (session) {
1000 *session = ps->sessions[i];
1001 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001002 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001003 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001004
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001005 /* TODO invalidate only sessions without subscription */
Michal Vasko3a715132016-01-21 15:40:31 +01001006 if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) {
1007 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1008 ps->sessions[i]->status = NC_STATUS_INVALID;
1009 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001010 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1011 if (session) {
1012 *session = ps->sessions[i];
1013 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001014 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001015 }
1016
Michal Vasko3a715132016-01-21 15:40:31 +01001017 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +01001018 break;
1019 }
Michal Vasko428087d2016-01-14 16:04:28 +01001020 }
1021
Michal Vaskobd8ef262016-01-20 11:09:27 +01001022 if (i == ps->session_count) {
Radek Krejci53691be2016-02-22 13:58:37 +01001023#ifdef NC_ENABLED_SSH
Michal Vasko3a715132016-01-21 15:40:31 +01001024retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +01001025#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +01001026 /* no leftover event */
1027 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +01001028 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001029 if (ret < 0) {
1030 ERR("Poll failed (%s).", strerror(errno));
1031 ret = NC_PSPOLL_ERROR;
1032 goto finish;
1033 } else if (!ret) {
1034 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001035 goto finish;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001036 }
Michal Vasko428087d2016-01-14 16:04:28 +01001037 }
1038
Michal Vaskobd8ef262016-01-20 11:09:27 +01001039 /* find the first fd with POLLIN, we don't care if there are more now */
1040 for (; i < ps->session_count; ++i) {
Michal Vasko46eac552016-05-30 15:27:25 +02001041 if (ps->pfds[i].revents & (POLLHUP | POLLNVAL)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001042 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
1043 ps->sessions[i]->status = NC_STATUS_INVALID;
1044 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko71090fc2016-05-24 16:37:28 +02001045 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1046 if (session) {
1047 *session = ps->sessions[i];
1048 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001049 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001050 } else if (ps->pfds[i].revents & POLLERR) {
1051 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
1052 ps->sessions[i]->status = NC_STATUS_INVALID;
1053 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko71090fc2016-05-24 16:37:28 +02001054 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1055 if (session) {
1056 *session = ps->sessions[i];
1057 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001058 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001059 } else if (ps->pfds[i].revents & POLLIN) {
Radek Krejci53691be2016-02-22 13:58:37 +01001060#ifdef NC_ENABLED_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +01001061 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +01001062 uint16_t j;
1063
Michal Vasko96164bf2016-01-21 15:41:58 +01001064 /* things are not that simple with SSH... */
Michal Vasko62be1ce2016-03-03 13:24:52 +01001065 ret = nc_ssh_pollin(ps->sessions[i], timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +01001066
1067 /* clear POLLIN on sessions sharing this session's SSH session */
Michal Vasko71090fc2016-05-24 16:37:28 +02001068 if (ret & (NC_PSPOLL_RPC | NC_PSPOLL_SSH_MSG | NC_PSPOLL_SSH_CHANNEL)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001069 for (j = i + 1; j < ps->session_count; ++j) {
1070 if (ps->pfds[j].fd == ps->pfds[i].fd) {
1071 ps->pfds[j].revents = 0;
1072 }
1073 }
1074 }
1075
Michal Vasko71090fc2016-05-24 16:37:28 +02001076 /* SSH message only */
1077 if (!(ret & (NC_PSPOLL_RPC | NC_PSPOLL_PENDING))) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001078 ps->pfds[i].revents = 0;
Michal Vasko71090fc2016-05-24 16:37:28 +02001079 if (session) {
1080 *session = ps->sessions[i];
1081 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001082 goto finish;
Michal Vasko96164bf2016-01-21 15:41:58 +01001083
1084 /* event occurred on some other channel */
Michal Vasko71090fc2016-05-24 16:37:28 +02001085 } else if (ret & NC_PSPOLL_PENDING) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001086 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001087 if (i == ps->session_count - 1) {
1088 /* last session and it is not the right channel, ... */
Michal Vasko8c748832016-02-03 15:32:16 +01001089 if (!timeout) {
Michal Vasko428087d2016-01-14 16:04:28 +01001090 /* ... timeout is 0, so that is it */
Michal Vasko71090fc2016-05-24 16:37:28 +02001091 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001092 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001093 }
Michal Vasko8c748832016-02-03 15:32:16 +01001094 /* ... retry polling reasonable time apart ... */
1095 usleep(NC_TIMEOUT_STEP);
1096 if (timeout > 0) {
1097 /* ... and decrease timeout, if not -1 */
Michal Vasko7b38e232016-02-26 15:01:07 +01001098 timeout -= NC_TIMEOUT_STEP * 1000;
Michal Vasko8c748832016-02-03 15:32:16 +01001099 }
1100 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +01001101 }
1102 /* check other sessions */
1103 continue;
Michal Vasko428087d2016-01-14 16:04:28 +01001104 }
1105 }
Radek Krejci53691be2016-02-22 13:58:37 +01001106#endif /* NC_ENABLED_SSH */
Michal Vasko428087d2016-01-14 16:04:28 +01001107
Michal Vaskobd8ef262016-01-20 11:09:27 +01001108 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +01001109 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001110 break;
1111 }
1112 }
1113
1114 if (i == ps->session_count) {
1115 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001116 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001117 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001118 }
1119
1120 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001121 cur_session = ps->sessions[i];
1122 if (session) {
1123 *session = cur_session;
1124 }
Michal Vasko428087d2016-01-14 16:04:28 +01001125
Michal Vaskobd8ef262016-01-20 11:09:27 +01001126 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko71090fc2016-05-24 16:37:28 +02001127 ret = nc_timedlock(cur_session->ti_lock, timeout);
1128 if (ret < 0) {
1129 ret = NC_PSPOLL_ERROR;
1130 goto finish;
1131 } else if (!ret) {
1132 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001133 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001134 }
1135
Michal Vasko71090fc2016-05-24 16:37:28 +02001136 ret = nc_recv_rpc(cur_session, &rpc);
1137 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1138 pthread_mutex_unlock(cur_session->ti_lock);
1139 if (cur_session->status != NC_STATUS_RUNNING) {
1140 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001141 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001142 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001143 }
1144
Michal Vasko71090fc2016-05-24 16:37:28 +02001145 cur_session->last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001146
Michal Vasko428087d2016-01-14 16:04:28 +01001147 /* process RPC */
Michal Vasko71090fc2016-05-24 16:37:28 +02001148 ret |= nc_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001149
Michal Vasko71090fc2016-05-24 16:37:28 +02001150 pthread_mutex_unlock(cur_session->ti_lock);
1151 if (cur_session->status != NC_STATUS_RUNNING) {
1152 ret |= NC_PSPOLL_SESSION_TERM;
1153 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1154 ret |= NC_PSPOLL_SESSION_ERROR;
1155 }
Michal Vasko428087d2016-01-14 16:04:28 +01001156 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001157
Michal Vaskoca4a2422016-02-02 12:17:14 +01001158 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001159
1160 /* is there some other socket waiting? */
1161 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001162 if (ps->pfds[i].revents) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001163 ret |= NC_PSPOLL_PENDING;
1164 break;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001165 }
1166 }
1167
Michal Vasko48a63ed2016-03-01 09:48:21 +01001168finish:
1169 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +02001170 nc_ps_unlock(ps, q_id);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001171 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001172}
1173
Michal Vaskod09eae62016-02-01 10:32:52 +01001174API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001175nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001176{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001177 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001178 uint16_t i;
1179 struct nc_session *session;
1180
Michal Vasko9a25e932016-02-01 10:36:42 +01001181 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001182 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001183 return;
1184 }
1185
Michal Vasko48a63ed2016-03-01 09:48:21 +01001186 /* LOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +02001187 if (nc_ps_lock(ps, &q_id)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001188 return;
1189 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001190
Michal Vasko48a63ed2016-03-01 09:48:21 +01001191 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001192 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001193 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001194 }
1195 free(ps->sessions);
1196 ps->sessions = NULL;
1197 free(ps->pfds);
1198 ps->pfds = NULL;
1199 ps->session_count = 0;
1200 } else {
1201 for (i = 0; i < ps->session_count; ) {
1202 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1203 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001204 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001205 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001206 continue;
1207 }
1208
1209 ++i;
1210 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001211 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001212
1213 /* UNLOCK */
Michal Vaskob30b99c2016-07-26 11:35:43 +02001214 nc_ps_unlock(ps, q_id);
Michal Vaskod09eae62016-02-01 10:32:52 +01001215}
1216
Radek Krejci53691be2016-02-22 13:58:37 +01001217#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001218
Michal Vasko3031aae2016-01-27 16:07:18 +01001219int
1220nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001221{
1222 int sock;
Michal Vasko3031aae2016-01-27 16:07:18 +01001223 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +01001224#ifdef NC_ENABLED_SSH
Michal Vasko08a629a2016-02-02 12:20:47 +01001225 struct nc_server_ssh_opts *ssh_opts;
1226#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001227
Michal Vasko45e53ae2016-04-07 11:46:03 +02001228 if (!name) {
1229 ERRARG("name");
1230 return -1;
1231 } else if (!address) {
1232 ERRARG("address");
1233 return -1;
1234 } else if (!port) {
1235 ERRARG("port");
Michal Vasko9e036d52016-01-08 10:49:26 +01001236 return -1;
1237 }
1238
Michal Vasko51e514d2016-02-02 15:51:52 +01001239 /* WRITE LOCK */
1240 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001241
1242 /* check name uniqueness */
1243 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskod4c03a82016-02-08 15:27:26 +01001244 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001245 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001246 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +01001247 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001248 return -1;
1249 }
1250 }
1251
Michal Vasko9e036d52016-01-08 10:49:26 +01001252 sock = nc_sock_listen(address, port);
1253 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001254 /* WRITE UNLOCK */
1255 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001256 return -1;
1257 }
1258
Michal Vasko3031aae2016-01-27 16:07:18 +01001259 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001260 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
1261 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
1262 if (!server_opts.binds || !server_opts.endpts) {
1263 ERRMEM;
1264 /* WRITE UNLOCK */
1265 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko0f74da52016-03-03 08:52:52 +01001266 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001267 return -1;
1268 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001269
Michal Vasko3031aae2016-01-27 16:07:18 +01001270 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1271 server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0);
Michal Vasko3031aae2016-01-27 16:07:18 +01001272 server_opts.binds[server_opts.endpt_count - 1].port = port;
1273 server_opts.binds[server_opts.endpt_count - 1].sock = sock;
1274 server_opts.binds[server_opts.endpt_count - 1].ti = ti;
1275 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001276#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001277 case NC_TI_LIBSSH:
Michal Vasko08a629a2016-02-02 12:20:47 +01001278 ssh_opts = calloc(1, sizeof *ssh_opts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001279 if (!ssh_opts) {
1280 ERRMEM;
1281 /* WRITE UNLOCK */
1282 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1283 return -1;
1284 }
Michal Vasko08a629a2016-02-02 12:20:47 +01001285 /* set default values */
1286 ssh_opts->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1287 ssh_opts->auth_attempts = 3;
1288 ssh_opts->auth_timeout = 10;
1289
1290 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = ssh_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +01001291 break;
1292#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001293#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001294 case NC_TI_OPENSSL:
1295 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 +01001296 if (!server_opts.endpts[server_opts.endpt_count - 1].ti_opts) {
1297 ERRMEM;
1298 /* WRITE UNLOCK */
1299 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1300 return -1;
1301 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001302 break;
1303#endif
1304 default:
1305 ERRINT;
1306 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL;
1307 break;
1308 }
1309 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001310
Michal Vasko3031aae2016-01-27 16:07:18 +01001311 /* WRITE UNLOCK */
1312 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001313
Michal Vasko9e036d52016-01-08 10:49:26 +01001314 return 0;
1315}
1316
Michal Vasko3031aae2016-01-27 16:07:18 +01001317int
Michal Vaskoda514772016-02-01 11:32:01 +01001318nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1319{
1320 struct nc_endpt *endpt;
1321 struct nc_bind *bind = NULL;
1322 uint16_t i;
1323 int sock;
1324
Michal Vasko45e53ae2016-04-07 11:46:03 +02001325 if (!endpt_name) {
1326 ERRARG("endpt_name");
1327 return -1;
1328 } else if ((!address && !port) || (address && port)) {
1329 ERRARG("address and port");
1330 return -1;
1331 } else if (!ti) {
1332 ERRARG("ti");
Michal Vaskoda514772016-02-01 11:32:01 +01001333 return -1;
1334 }
1335
Michal Vasko51e514d2016-02-02 15:51:52 +01001336 /* LOCK */
Michal Vaskoda514772016-02-01 11:32:01 +01001337 endpt = nc_server_endpt_lock(endpt_name, ti);
1338 if (!endpt) {
1339 return -1;
1340 }
1341
1342 /* we need to learn the index, to get the bind :-/ */
1343 for (i = 0; i < server_opts.endpt_count; ++i) {
1344 if (&server_opts.endpts[i] == endpt) {
1345 bind = &server_opts.binds[i];
1346 }
1347 }
1348 if (!bind) {
1349 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001350 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001351 }
1352
1353 if (address) {
1354 sock = nc_sock_listen(address, bind->port);
1355 } else {
1356 sock = nc_sock_listen(bind->address, port);
1357 }
1358 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001359 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001360 }
1361
1362 /* close old socket, update parameters */
1363 close(bind->sock);
1364 bind->sock = sock;
1365 if (address) {
1366 lydict_remove(server_opts.ctx, bind->address);
1367 bind->address = lydict_insert(server_opts.ctx, address, 0);
1368 } else {
1369 bind->port = port;
1370 }
1371
Michal Vasko51e514d2016-02-02 15:51:52 +01001372 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001373 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001374 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001375
1376fail:
1377 /* UNLOCK */
1378 nc_server_endpt_unlock(endpt);
1379 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001380}
1381
1382int
Michal Vasko3031aae2016-01-27 16:07:18 +01001383nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001384{
1385 uint32_t i;
1386 int ret = -1;
1387
Michal Vasko3031aae2016-01-27 16:07:18 +01001388 /* WRITE LOCK */
1389 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001390
Michal Vasko3031aae2016-01-27 16:07:18 +01001391 if (!name && !ti) {
1392 /* remove all */
Michal Vasko3031aae2016-01-27 16:07:18 +01001393 for (i = 0; i < server_opts.endpt_count; ++i) {
1394 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001395 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko51e514d2016-02-02 15:51:52 +01001396
Michal Vasko3031aae2016-01-27 16:07:18 +01001397 close(server_opts.binds[i].sock);
1398 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1399 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001400#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001401 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001402 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001403 break;
1404#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001405#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001406 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001407 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001408 break;
1409#endif
1410 default:
1411 ERRINT;
1412 break;
1413 }
1414 free(server_opts.endpts[i].ti_opts);
Michal Vasko9e036d52016-01-08 10:49:26 +01001415
Michal Vasko9e036d52016-01-08 10:49:26 +01001416 ret = 0;
1417 }
Michal Vasko7ddc5702016-02-08 15:29:39 +01001418 free(server_opts.binds);
1419 server_opts.binds = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001420 free(server_opts.endpts);
1421 server_opts.endpts = NULL;
1422 server_opts.endpt_count = 0;
1423
Michal Vasko1a38c862016-01-15 15:50:07 +01001424 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001425 /* remove one name endpoint or all ti endpoints */
1426 for (i = 0; i < server_opts.endpt_count; ++i) {
1427 if ((server_opts.binds[i].ti == ti) &&
1428 (!name || !strcmp(server_opts.endpts[i].name, name))) {
1429
Michal Vasko3031aae2016-01-27 16:07:18 +01001430 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001431 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001432 close(server_opts.binds[i].sock);
1433 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1434 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001435#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001436 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001437 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001438 break;
1439#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001440#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001441 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001442 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001443 break;
1444#endif
1445 default:
1446 ERRINT;
1447 break;
1448 }
1449 free(server_opts.endpts[i].ti_opts);
Michal Vasko1a38c862016-01-15 15:50:07 +01001450
Michal Vasko3031aae2016-01-27 16:07:18 +01001451 --server_opts.endpt_count;
Michal Vaskoc0256492016-02-02 12:19:06 +01001452 if (i < server_opts.endpt_count) {
1453 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1454 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1455 } else if (!server_opts.endpt_count) {
1456 free(server_opts.binds);
1457 server_opts.binds = NULL;
1458 free(server_opts.endpts);
1459 server_opts.endpts = NULL;
1460 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001461
1462 ret = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001463
1464 if (name) {
1465 /* one name endpoint removed, they are unique, we're done */
1466 break;
1467 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001468 }
1469 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001470 }
1471
Michal Vasko3031aae2016-01-27 16:07:18 +01001472 /* WRITE UNLOCK */
1473 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001474
Michal Vasko9e036d52016-01-08 10:49:26 +01001475 return ret;
1476}
1477
Michal Vasko71090fc2016-05-24 16:37:28 +02001478API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001479nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001480{
Michal Vasko71090fc2016-05-24 16:37:28 +02001481 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001482 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001483 char *host = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001484 uint16_t port, idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001485
Michal Vasko45e53ae2016-04-07 11:46:03 +02001486 if (!server_opts.ctx) {
1487 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001488 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001489 } else if (!session) {
1490 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001491 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001492 }
1493
Michal Vasko51e514d2016-02-02 15:51:52 +01001494 /* we have to hold WRITE for the whole time, since there is not
1495 * a way of downgrading the lock to READ */
1496 /* WRITE LOCK */
1497 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1498
1499 if (!server_opts.endpt_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001500 ERRINIT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001501 /* WRITE UNLOCK */
1502 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001503 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001504 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001505
Michal Vasko3031aae2016-01-27 16:07:18 +01001506 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx);
Michal Vaskob48aa812016-01-18 14:13:09 +01001507
Michal Vasko50456e82016-02-02 12:16:08 +01001508 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001509 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001510 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001511 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001512 if (!ret) {
1513 return NC_MSG_WOULDBLOCK;
1514 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001515 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001516 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001517 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001518
Michal Vasko1a38c862016-01-15 15:50:07 +01001519 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001520 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001521 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001522 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001523 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001524 msgtype = NC_MSG_ERROR;
1525 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001526 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001527 (*session)->status = NC_STATUS_STARTING;
1528 (*session)->side = NC_SERVER;
1529 (*session)->ctx = server_opts.ctx;
1530 (*session)->flags = NC_SESSION_SHAREDCTX;
1531 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1532 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001533
1534 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001535 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1536 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001537 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001538 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001539 msgtype = NC_MSG_ERROR;
1540 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001541 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001542 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001543
Michal Vasko2cc4c682016-03-01 09:16:48 +01001544 (*session)->data = server_opts.endpts[idx].ti_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +01001545
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001546 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001547#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001548 if (server_opts.binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001549 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001550 if (ret < 0) {
1551 msgtype = NC_MSG_ERROR;
1552 goto cleanup;
1553 } else if (!ret) {
1554 msgtype = NC_MSG_WOULDBLOCK;
1555 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001556 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001557 } else
1558#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001559#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001560 if (server_opts.binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001561 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001562 if (ret < 0) {
1563 msgtype = NC_MSG_ERROR;
1564 goto cleanup;
1565 } else if (!ret) {
1566 msgtype = NC_MSG_WOULDBLOCK;
1567 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001568 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001569 } else
1570#endif
1571 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001572 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001573 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001574 msgtype = NC_MSG_ERROR;
1575 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001576 }
1577
Michal Vasko2cc4c682016-03-01 09:16:48 +01001578 (*session)->data = NULL;
1579
Michal Vasko51e514d2016-02-02 15:51:52 +01001580 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001581 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1582
Michal Vaskob48aa812016-01-18 14:13:09 +01001583 /* assign new SID atomically */
1584 /* LOCK */
1585 pthread_spin_lock(&server_opts.sid_lock);
1586 (*session)->id = server_opts.new_session_id++;
1587 /* UNLOCK */
1588 pthread_spin_unlock(&server_opts.sid_lock);
1589
Michal Vasko9e036d52016-01-08 10:49:26 +01001590 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001591 msgtype = nc_handshake(*session);
1592 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001593 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001594 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001595 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001596 }
Michal Vaskof8352352016-05-24 09:11:36 +02001597 (*session)->session_start = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001598 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001599
Michal Vasko71090fc2016-05-24 16:37:28 +02001600 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001601
Michal Vasko71090fc2016-05-24 16:37:28 +02001602cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001603 /* WRITE UNLOCK */
1604 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1605
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001606 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001607 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001608 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001609}
1610
Michal Vasko71090fc2016-05-24 16:37:28 +02001611NC_MSG_TYPE
Michal Vasko8f5270d2016-02-29 16:22:25 +01001612nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01001613{
Michal Vasko71090fc2016-05-24 16:37:28 +02001614 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001615 int sock, ret;
1616
Michal Vasko45e53ae2016-04-07 11:46:03 +02001617 if (!host) {
1618 ERRARG("host");
Michal Vasko71090fc2016-05-24 16:37:28 +02001619 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001620 } else if (!port) {
1621 ERRARG("port");
Michal Vasko71090fc2016-05-24 16:37:28 +02001622 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001623 } else if (!ti) {
1624 ERRARG("ti");
Michal Vasko71090fc2016-05-24 16:37:28 +02001625 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001626 } else if (!session) {
1627 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001628 return NC_MSG_ERROR;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001629 }
1630
Michal Vaskob05053d2016-01-22 16:12:06 +01001631 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001632 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001633 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001634 }
1635
1636 *session = calloc(1, sizeof **session);
1637 if (!(*session)) {
1638 ERRMEM;
1639 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001640 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001641 }
1642 (*session)->status = NC_STATUS_STARTING;
1643 (*session)->side = NC_SERVER;
1644 (*session)->ctx = server_opts.ctx;
1645 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001646 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001647 (*session)->port = port;
1648
1649 /* transport lock */
1650 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1651 if (!(*session)->ti_lock) {
1652 ERRMEM;
1653 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001654 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001655 goto fail;
1656 }
1657 pthread_mutex_init((*session)->ti_lock, NULL);
1658
1659 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001660#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001661 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001662 /* OPTS LOCK */
1663 pthread_mutex_lock(&ssh_ch_opts_lock);
1664
Michal Vasko2cc4c682016-03-01 09:16:48 +01001665 (*session)->data = &ssh_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001666 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001667 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001668
1669 /* OPTS UNLOCK */
1670 pthread_mutex_unlock(&ssh_ch_opts_lock);
1671
Michal Vasko71090fc2016-05-24 16:37:28 +02001672 if (ret < 0) {
1673 msgtype = NC_MSG_ERROR;
1674 goto fail;
1675 } else if (!ret) {
1676 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001677 goto fail;
1678 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001679 } else
1680#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001681#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001682 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001683 /* OPTS LOCK */
1684 pthread_mutex_lock(&tls_ch_opts_lock);
1685
Michal Vasko2cc4c682016-03-01 09:16:48 +01001686 (*session)->data = &tls_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001687 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001688 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001689
1690 /* OPTS UNLOCK */
1691 pthread_mutex_unlock(&tls_ch_opts_lock);
1692
Michal Vasko71090fc2016-05-24 16:37:28 +02001693 if (ret < 0) {
1694 msgtype = NC_MSG_ERROR;
1695 goto fail;
1696 } else if (!ret) {
1697 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001698 goto fail;
1699 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001700 } else
1701#endif
1702 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001703 ERRINT;
1704 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001705 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001706 goto fail;
1707 }
1708
1709 /* assign new SID atomically */
1710 /* LOCK */
1711 pthread_spin_lock(&server_opts.sid_lock);
1712 (*session)->id = server_opts.new_session_id++;
1713 /* UNLOCK */
1714 pthread_spin_unlock(&server_opts.sid_lock);
1715
1716 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001717 msgtype = nc_handshake(*session);
1718 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01001719 goto fail;
1720 }
Michal Vaskof8352352016-05-24 09:11:36 +02001721 (*session)->session_start = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001722 (*session)->status = NC_STATUS_RUNNING;
1723
Michal Vasko71090fc2016-05-24 16:37:28 +02001724 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001725
1726fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001727 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001728 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001729 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001730}
1731
Radek Krejci53691be2016-02-22 13:58:37 +01001732#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02001733
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001734API time_t
1735nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02001736{
1737 if (!session) {
1738 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001739 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02001740 }
1741
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001742 return session->session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02001743}