-
Notifications
You must be signed in to change notification settings - Fork 144
/
session_server.c
3112 lines (2655 loc) · 87.8 KB
/
session_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file session_server.c
* \author Michal Vasko <mvasko@cesnet.cz>
* \brief libnetconf2 server session manipulation functions
*
* Copyright (c) 2015 - 2017 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE /* signals, threads */
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <time.h>
#include <signal.h>
#include "libnetconf.h"
#include "session_server.h"
struct nc_server_opts server_opts = {
#ifdef NC_ENABLED_SSH
.authkey_lock = PTHREAD_MUTEX_INITIALIZER,
#endif
.bind_lock = PTHREAD_MUTEX_INITIALIZER,
.endpt_lock = PTHREAD_RWLOCK_INITIALIZER,
.ch_client_lock = PTHREAD_RWLOCK_INITIALIZER
};
static nc_rpc_clb global_rpc_clb = NULL;
struct nc_endpt *
nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
{
uint16_t i;
struct nc_endpt *endpt = NULL;
/* WRITE LOCK */
pthread_rwlock_wrlock(&server_opts.endpt_lock);
for (i = 0; i < server_opts.endpt_count; ++i) {
if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) {
endpt = &server_opts.endpts[i];
break;
}
}
if (!endpt) {
ERR("Endpoint \"%s\" was not found.", name);
/* UNLOCK */
pthread_rwlock_unlock(&server_opts.endpt_lock);
return NULL;
}
if (idx) {
*idx = i;
}
return endpt;
}
struct nc_ch_client *
nc_server_ch_client_lock(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
{
uint16_t i;
struct nc_ch_client *client = NULL;
/* READ LOCK */
pthread_rwlock_rdlock(&server_opts.ch_client_lock);
for (i = 0; i < server_opts.ch_client_count; ++i) {
if (!strcmp(server_opts.ch_clients[i].name, name) && (!ti || (server_opts.ch_clients[i].ti == ti))) {
client = &server_opts.ch_clients[i];
break;
}
}
if (!client) {
ERR("Call Home client \"%s\" was not found.", name);
/* READ UNLOCK */
pthread_rwlock_unlock(&server_opts.ch_client_lock);
return NULL;
}
/* CH CLIENT LOCK */
pthread_mutex_lock(&client->lock);
if (idx) {
*idx = i;
}
return client;
}
void
nc_server_ch_client_unlock(struct nc_ch_client *client)
{
/* CH CLIENT UNLOCK */
pthread_mutex_unlock(&client->lock);
/* READ UNLOCK */
pthread_rwlock_unlock(&server_opts.ch_client_lock);
}
API void
nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
{
if (!session) {
ERRARG("session");
return;
} else if (!reason) {
ERRARG("reason");
return;
}
if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) {
session->killed_by = 0;
}
session->term_reason = reason;
}
API void
nc_session_set_killed_by(struct nc_session *session, uint32_t sid)
{
if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) {
ERRARG("session");
return;
} else if (!sid) {
ERRARG("sid");
return;
}
session->killed_by = sid;
}
API void
nc_session_set_status(struct nc_session *session, NC_STATUS status)
{
if (!session) {
ERRARG("session");
return;
} else if (!status) {
ERRARG("status");
return;
}
session->status = status;
}
int
nc_sock_listen(const char *address, uint16_t port)
{
int opt;
int is_ipv4, sock;
struct sockaddr_storage saddr;
struct sockaddr_in *saddr4;
struct sockaddr_in6 *saddr6;
if (!strchr(address, ':')) {
is_ipv4 = 1;
} else {
is_ipv4 = 0;
}
sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
if (sock == -1) {
ERR("Failed to create socket (%s).", strerror(errno));
goto fail;
}
opt = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
ERR("Could not set SO_REUSEADDR socket option (%s).", strerror(errno));
goto fail;
}
opt = 1;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
goto fail;
}
bzero(&saddr, sizeof(struct sockaddr_storage));
if (is_ipv4) {
saddr4 = (struct sockaddr_in *)&saddr;
saddr4->sin_family = AF_INET;
saddr4->sin_port = htons(port);
if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
ERR("Failed to convert IPv4 address \"%s\".", address);
goto fail;
}
if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
goto fail;
}
} else {
saddr6 = (struct sockaddr_in6 *)&saddr;
saddr6->sin6_family = AF_INET6;
saddr6->sin6_port = htons(port);
if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
ERR("Failed to convert IPv6 address \"%s\".", address);
goto fail;
}
if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
goto fail;
}
}
if (listen(sock, NC_REVERSE_QUEUE) == -1) {
ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
goto fail;
}
return sock;
fail:
if (sock > -1) {
close(sock);
}
return -1;
}
int
nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx)
{
sigset_t sigmask, origmask;
uint16_t i, j, pfd_count;
struct pollfd *pfd;
struct sockaddr_storage saddr;
socklen_t saddr_len = sizeof(saddr);
int ret, sock = -1, flags;
pfd = malloc(bind_count * sizeof *pfd);
if (!pfd) {
ERRMEM;
return -1;
}
for (i = 0, pfd_count = 0; i < bind_count; ++i) {
if (binds[i].sock < 0) {
/* invalid socket */
continue;
}
if (binds[i].pollin) {
binds[i].pollin = 0;
/* leftover pollin */
sock = binds[i].sock;
break;
}
pfd[pfd_count].fd = binds[i].sock;
pfd[pfd_count].events = POLLIN;
pfd[pfd_count].revents = 0;
++pfd_count;
}
if (sock == -1) {
/* poll for a new connection */
sigfillset(&sigmask);
pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
ret = poll(pfd, pfd_count, timeout);
pthread_sigmask(SIG_SETMASK, &origmask, NULL);
if (!ret) {
/* we timeouted */
free(pfd);
return 0;
} else if (ret == -1) {
ERR("Poll failed (%s).", strerror(errno));
free(pfd);
return -1;
}
for (i = 0, j = 0; j < pfd_count; ++i, ++j) {
/* adjust i so that indices in binds and pfd always match */
while (binds[i].sock != pfd[j].fd) {
++i;
}
if (pfd[j].revents & POLLIN) {
--ret;
if (!ret) {
/* the last socket with an event, use it */
sock = pfd[j].fd;
break;
} else {
/* just remember the event for next time */
binds[i].pollin = 1;
}
}
}
}
free(pfd);
if (sock == -1) {
ERRINT;
return -1;
}
ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
if (ret < 0) {
ERR("Accept failed (%s).", strerror(errno));
return -1;
}
VRB("Accepted a connection on %s:%u.", binds[i].address, binds[i].port);
/* make the socket non-blocking */
if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
ERR("Fcntl failed (%s).", strerror(errno));
close(ret);
return -1;
}
/* enable keep-alive */
#ifdef TCP_KEEPIDLE
flags = 1;
if (setsockopt(ret, IPPROTO_TCP, TCP_KEEPIDLE, &flags, sizeof flags) == -1) {
ERR("Setsockopt failed (%s).", strerror(errno));
close(ret);
return -1;
}
#endif
#ifdef TCP_KEEPINTVL
flags = 5;
if (setsockopt(ret, IPPROTO_TCP, TCP_KEEPINTVL, &flags, sizeof flags) == -1) {
ERR("Setsockopt failed (%s).", strerror(errno));
close(ret);
return -1;
}
#endif
#ifdef TCP_KEEPCNT
flags = 10;
if (setsockopt(ret, IPPROTO_TCP, TCP_KEEPCNT, &flags, sizeof flags) == -1) {
ERR("Setsockopt failed (%s).", strerror(errno));
close(ret);
return -1;
}
#endif
if (idx) {
*idx = i;
}
/* host was requested */
if (host) {
if (saddr.ss_family == AF_INET) {
*host = malloc(INET_ADDRSTRLEN);
if (*host) {
if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, INET_ADDRSTRLEN)) {
ERR("inet_ntop failed (%s).", strerror(errno));
free(*host);
*host = NULL;
}
if (port) {
*port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
}
} else {
ERRMEM;
}
} else if (saddr.ss_family == AF_INET6) {
*host = malloc(INET6_ADDRSTRLEN);
if (*host) {
if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, INET6_ADDRSTRLEN)) {
ERR("inet_ntop failed (%s).", strerror(errno));
free(*host);
*host = NULL;
}
if (port) {
*port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
}
} else {
ERRMEM;
}
} else {
ERR("Source host of an unknown protocol family.");
}
}
return ret;
}
static struct nc_server_reply *
nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
{
const char *identifier = NULL, *version = NULL, *format = NULL;
char *model_data = NULL;
const struct lys_module *module;
struct nc_server_error *err;
struct lyd_node *child, *data = NULL;
const struct lys_node *sdata = NULL;
LY_TREE_FOR(rpc->child, child) {
if (!strcmp(child->schema->name, "identifier")) {
identifier = ((struct lyd_node_leaf_list *)child)->value_str;
} else if (!strcmp(child->schema->name, "version")) {
version = ((struct lyd_node_leaf_list *)child)->value_str;
if (version && version[0] == '\0') {
version = NULL;
}
} else if (!strcmp(child->schema->name, "format")) {
format = ((struct lyd_node_leaf_list *)child)->value_str;
}
}
/* check version */
if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
nc_err_set_msg(err, "The requested version is not supported.", "en");
return nc_server_reply_err(err);
}
/* check and get module with the name identifier */
module = ly_ctx_get_module(server_opts.ctx, identifier, version, 0);
if (!module) {
module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
}
if (!module) {
err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
nc_err_set_msg(err, "The requested schema was not found.", "en");
return nc_server_reply_err(err);
}
/* check format */
if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL, 0, 0);
} else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL, 0, 0);
} else {
err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
nc_err_set_msg(err, "The requested format is not supported.", "en");
return nc_server_reply_err(err);
}
if (!model_data) {
ERRINT;
return NULL;
}
sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/data", 1);
if (!sdata) {
ERRINT;
free(model_data);
return NULL;
}
data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
if (!data || lyd_validate(&data, LYD_OPT_RPCREPLY, NULL)) {
ERRINT;
free(model_data);
return NULL;
}
return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
}
static struct nc_server_reply *
nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
{
session->term_reason = NC_SESSION_TERM_CLOSED;
return nc_server_reply_ok();
}
API int
nc_server_init(struct ly_ctx *ctx)
{
const struct lys_node *rpc;
pthread_rwlockattr_t attr;
if (!ctx) {
ERRARG("ctx");
return -1;
}
nc_init();
/* set default <get-schema> callback if not specified */
rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0);
if (rpc && !rpc->priv) {
lys_set_private(rpc, nc_clb_default_get_schema);
}
/* set default <close-session> callback if not specififed */
rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session", 0);
if (rpc && !rpc->priv) {
lys_set_private(rpc, nc_clb_default_close_session);
}
server_opts.ctx = ctx;
server_opts.new_session_id = 1;
server_opts.new_client_id = 1;
errno=0;
if (pthread_rwlockattr_init(&attr) == 0) {
if (pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) == 0) {
if (pthread_rwlock_init(&server_opts.endpt_lock, &attr) != 0) {
ERR("%s: failed to init rwlock(%s).", __FUNCTION__, strerror(errno));
}
if (pthread_rwlock_init(&server_opts.ch_client_lock, &attr) != 0) {
ERR("%s: failed to init rwlock(%s).", __FUNCTION__, strerror(errno));
}
} else {
ERR("%s: failed set attribute (%s).", __FUNCTION__, strerror(errno));
}
pthread_rwlockattr_destroy(&attr);
} else {
ERR("%s: failed init attribute (%s).", __FUNCTION__, strerror(errno));
}
return 0;
}
API void
nc_server_destroy(void)
{
unsigned int i;
for (i = 0; i < server_opts.capabilities_count; i++) {
lydict_remove(server_opts.ctx, server_opts.capabilities[i]);
}
free(server_opts.capabilities);
server_opts.capabilities = NULL;
server_opts.capabilities_count = 0;
#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
nc_server_del_endpt(NULL, 0);
#endif
#ifdef NC_ENABLED_SSH
if (server_opts.passwd_auth_data && server_opts.passwd_auth_data_free) {
server_opts.passwd_auth_data_free(server_opts.passwd_auth_data);
}
server_opts.passwd_auth_data = NULL;
server_opts.passwd_auth_data_free = NULL;
nc_server_ssh_del_authkey(NULL, NULL, 0, NULL);
if (server_opts.hostkey_data && server_opts.hostkey_data_free) {
server_opts.hostkey_data_free(server_opts.hostkey_data);
}
server_opts.hostkey_data = NULL;
server_opts.hostkey_data_free = NULL;
#endif
#ifdef NC_ENABLED_TLS
if (server_opts.server_cert_data && server_opts.server_cert_data_free) {
server_opts.server_cert_data_free(server_opts.server_cert_data);
}
server_opts.server_cert_data = NULL;
server_opts.server_cert_data_free = NULL;
if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) {
server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data);
}
server_opts.trusted_cert_list_data = NULL;
server_opts.trusted_cert_list_data_free = NULL;
#endif
nc_destroy();
}
API int
nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
{
if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
ERRARG("basic_mode");
return -1;
} else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
ERRARG("also_supported");
return -1;
}
server_opts.wd_basic_mode = basic_mode;
server_opts.wd_also_supported = also_supported;
return 0;
}
API void
nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
{
if (!basic_mode && !also_supported) {
ERRARG("basic_mode and also_supported");
return;
}
if (basic_mode) {
*basic_mode = server_opts.wd_basic_mode;
}
if (also_supported) {
*also_supported = server_opts.wd_also_supported;
}
}
API int
nc_server_set_capability(const char *value)
{
const char **new;
if (!value || !value[0]) {
ERRARG("value must not be empty");
return EXIT_FAILURE;
}
server_opts.capabilities_count++;
new = realloc(server_opts.capabilities, server_opts.capabilities_count * sizeof *server_opts.capabilities);
if (!new) {
ERRMEM;
return EXIT_FAILURE;
}
server_opts.capabilities = new;
server_opts.capabilities[server_opts.capabilities_count - 1] = lydict_insert(server_opts.ctx, value, 0);
return EXIT_SUCCESS;
}
API void
nc_server_set_hello_timeout(uint16_t hello_timeout)
{
server_opts.hello_timeout = hello_timeout;
}
API uint16_t
nc_server_get_hello_timeout(void)
{
return server_opts.hello_timeout;
}
API void
nc_server_set_idle_timeout(uint16_t idle_timeout)
{
server_opts.idle_timeout = idle_timeout;
}
API uint16_t
nc_server_get_idle_timeout(void)
{
return server_opts.idle_timeout;
}
API NC_MSG_TYPE
nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
{
NC_MSG_TYPE msgtype;
struct timespec ts_cur;
if (!server_opts.ctx) {
ERRINIT;
return NC_MSG_ERROR;
} else if (fdin < 0) {
ERRARG("fdin");
return NC_MSG_ERROR;
} else if (fdout < 0) {
ERRARG("fdout");
return NC_MSG_ERROR;
} else if (!username) {
ERRARG("username");
return NC_MSG_ERROR;
} else if (!session) {
ERRARG("session");
return NC_MSG_ERROR;
}
/* prepare session structure */
*session = nc_new_session(NC_SERVER, 0);
if (!(*session)) {
ERRMEM;
return NC_MSG_ERROR;
}
(*session)->status = NC_STATUS_STARTING;
/* transport specific data */
(*session)->ti_type = NC_TI_FD;
(*session)->ti.fd.in = fdin;
(*session)->ti.fd.out = fdout;
/* assign context (dicionary needed for handshake) */
(*session)->flags = NC_SESSION_SHAREDCTX;
(*session)->ctx = server_opts.ctx;
/* assign new SID atomically */
(*session)->id = atomic_fetch_add(&server_opts.new_session_id, 1);
/* NETCONF handshake */
msgtype = nc_handshake_io(*session);
if (msgtype != NC_MSG_HELLO) {
nc_session_free(*session, NULL);
*session = NULL;
return msgtype;
}
nc_gettimespec_mono(&ts_cur);
(*session)->opts.server.last_rpc = ts_cur.tv_sec;
nc_gettimespec_real(&ts_cur);
(*session)->opts.server.session_start = ts_cur.tv_sec;
(*session)->status = NC_STATUS_RUNNING;
return msgtype;
}
static void
nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id)
{
uint8_t q_last;
if (ps->queue_len == NC_PS_QUEUE_SIZE) {
ERRINT;
return;
}
/* get a unique queue value (by adding 1 to the last added value, if any) */
if (ps->queue_len) {
q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE;
*id = ps->queue[q_last] + 1;
} else {
*id = 0;
}
/* add the id into the queue */
++ps->queue_len;
q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE;
ps->queue[q_last] = *id;
}
static void
nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
{
uint8_t i, q_idx, found = 0;
for (i = 0; i < ps->queue_len; ++i) {
/* get the actual queue idx */
q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE;
if (found) {
if (ps->queue[q_idx] == id) {
/* another equal value, simply cannot be */
ERRINT;
}
if (found == 2) {
/* move the following values */
ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx];
}
} else if (ps->queue[q_idx] == id) {
/* found our id, there can be no more equal valid values */
if (i == 0) {
found = 1;
} else {
/* this is not okay, our id is in the middle of the queue */
found = 2;
}
}
}
if (!found) {
ERRINT;
return;
}
--ps->queue_len;
if (found == 1) {
/* remove the id by moving the queue, otherwise all the values in the queue were moved */
ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE;
}
}
int
nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
{
int ret;
struct timespec ts;
nc_gettimespec_real(&ts);
nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
/* LOCK */
ret = pthread_mutex_timedlock(&ps->lock, &ts);
if (ret) {
ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
return -1;
}
/* check that the queue is long enough */
if (ps->queue_len == NC_PS_QUEUE_SIZE) {
ERR("%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE);
pthread_mutex_unlock(&ps->lock);
return -1;
}
/* add ourselves into the queue */
nc_ps_queue_add_id(ps, id);
/* is it our turn? */
while (ps->queue[ps->queue_begin] != *id) {
nc_gettimespec_real(&ts);
nc_addtimespec(&ts, NC_PS_QUEUE_TIMEOUT);
ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
if (ret) {
ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
/* remove ourselves from the queue */
nc_ps_queue_remove_id(ps, *id);
pthread_mutex_unlock(&ps->lock);
return -1;
}
}
/* UNLOCK */
pthread_mutex_unlock(&ps->lock);
return 0;
}
int
nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
{
int ret;
struct timespec ts;
nc_gettimespec_real(&ts);
nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
/* LOCK */
ret = pthread_mutex_timedlock(&ps->lock, &ts);
if (ret) {
ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
ret = -1;
}
/* we must be the first, it was our turn after all, right? */
if (ps->queue[ps->queue_begin] != id) {
ERRINT;
/* UNLOCK */
if (!ret) {
pthread_mutex_unlock(&ps->lock);
}
return -1;
}
/* remove ourselves from the queue */
nc_ps_queue_remove_id(ps, id);
/* broadcast to all other threads that the queue moved */
pthread_cond_broadcast(&ps->cond);
/* UNLOCK */
if (!ret) {
pthread_mutex_unlock(&ps->lock);
}
return ret;
}
API struct nc_pollsession *
nc_ps_new(void)
{
struct nc_pollsession *ps;
ps = calloc(1, sizeof(struct nc_pollsession));
if (!ps) {
ERRMEM;
return NULL;
}
pthread_cond_init(&ps->cond, NULL);
pthread_mutex_init(&ps->lock, NULL);
return ps;
}
API void
nc_ps_free(struct nc_pollsession *ps)
{
uint16_t i;
if (!ps) {
return;
}
if (ps->queue_len) {
ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
}
for (i = 0; i < ps->session_count; i++) {
free(ps->sessions[i]);
}
free(ps->sessions);
pthread_mutex_destroy(&ps->lock);
pthread_cond_destroy(&ps->cond);
free(ps);
}
API int
nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
{
uint8_t q_id;
if (!ps) {
ERRARG("ps");
return -1;
} else if (!session) {
ERRARG("session");
return -1;
}
/* LOCK */
if (nc_ps_lock(ps, &q_id, __func__)) {
return -1;
}
++ps->session_count;
ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
if (!ps->sessions) {
ERRMEM;
/* UNLOCK */
nc_ps_unlock(ps, q_id, __func__);
return -1;
}
ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions);
if (!ps->sessions[ps->session_count - 1]) {
ERRMEM;
--ps->session_count;
/* UNLOCK */
nc_ps_unlock(ps, q_id, __func__);
return -1;
}
ps->sessions[ps->session_count - 1]->session = session;
ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE;
/* UNLOCK */
return nc_ps_unlock(ps, q_id, __func__);
}
static int
_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
{
uint16_t i;
if (index >= 0) {
i = (uint16_t)index;
goto remove;
}
for (i = 0; i < ps->session_count; ++i) {
if (ps->sessions[i]->session == session) {
remove:
--ps->session_count;
if (i <= ps->session_count) {
free(ps->sessions[i]);
ps->sessions[i] = ps->sessions[ps->session_count];
}
if (!ps->session_count) {
free(ps->sessions);
ps->sessions = NULL;
}
ps->last_event_session = 0;
return 0;
}
}
return -1;
}
API int
nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
{
uint8_t q_id;
int ret, ret2;
if (!ps) {
ERRARG("ps");
return -1;
} else if (!session) {
ERRARG("session");
return -1;
}
/* LOCK */
if (nc_ps_lock(ps, &q_id, __func__)) {