forked from NEAT-project/neat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
neat_core.c
7397 lines (6314 loc) · 255 KB
/
neat_core.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
#include <sys/types.h>
#include <netinet/in.h>
#if defined(USRSCTP_SUPPORT)
#include <usrsctp.h>
#else
#if defined(HAVE_NETINET_SCTP_H) && !defined(USRSCTP_SUPPORT)
#ifdef __linux__
#include <netinet/sctp.h>
#else // __linux__
#include <netinet/sctp.h>
#include <netinet/udplite.h>
#endif // __linux__
#endif // defined(HAVE_NETINET_SCTP_H) && !defined(USRSCTP_SUPPORT)
#endif
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <uv.h>
#include <errno.h>
#include <ifaddrs.h>
#ifdef __linux__
#include <net/if.h>
#ifndef USRSCTP_SUPPORT
#include <sys/socket.h>
#endif // USRSCTP_SUPPORT
#endif // __linux__
#include "neat.h"
#include "neat_internal.h"
#include "neat_core.h"
#include "neat_queue.h"
#include "neat_addr.h"
#include "neat_queue.h"
#include "neat_stat.h"
#include "neat_resolver_helpers.h"
#include "neat_json_helpers.h"
#include "neat_unix_json_socket.h"
#include "neat_pm_socket.h"
#if defined(USRSCTP_SUPPORT)
#include "neat_usrsctp_internal.h"
#include <usrsctp.h>
#endif // defined(USRSCTP_SUPPORT)
#ifdef __linux__
#include "neat_linux_internal.h"
#endif // __linux__
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include "neat_bsd_internal.h"
#endif
static void nt_update_poll_handle(neat_ctx *ctx, neat_flow *flow, uv_poll_t *handle);
static neat_error_code nt_write_flush(struct neat_ctx *ctx, struct neat_flow *flow);
static int nt_listen_via_kernel(struct neat_ctx *ctx, struct neat_flow *flow,
struct neat_pollable_socket *listen_socket);
#if defined(USRSCTP_SUPPORT)
static int nt_connect_via_usrsctp(struct neat_he_candidate *candidate);
static int nt_listen_via_usrsctp(struct neat_ctx *ctx, struct neat_flow *flow,
struct neat_pollable_socket *listen_socket);
static int nt_close_via_usrsctp(struct neat_ctx *ctx, struct neat_flow *flow);
static int nt_shutdown_via_usrsctp(struct neat_ctx *ctx, struct neat_flow *flow);
static void handle_upcall(struct socket *s, void *arg, int flags);
static void handle_connect(struct socket *s, void *arg, int flags);
static void nt_sctp_init_events(struct socket *sock);
#else // defined(USRSCTP_SUPPORT)
static void nt_sctp_init_events(int sock);
#endif // defined(USRSCTP_SUPPORT)
#ifdef SCTP_MULTISTREAMING
static neat_flow *nt_sctp_get_flow_by_sid(struct neat_pollable_socket *socket, uint16_t sid);
static void nt_sctp_reset_stream(struct neat_pollable_socket *socket, uint16_t sid);
static void nt_hook_mulitstream_flows(neat_flow *flow);
#ifdef SCTP_RESET_STREAMS
static void nt_sctp_handle_reset_stream(struct neat_pollable_socket *socket, struct sctp_stream_reset_event *notfn);
#endif // SCTP_RESET_STREAMS
#endif // SCTP_MULTISTREAMING
static void nt_free_flow(struct neat_flow *flow);
static int nt_prepare_sctp_socket(struct neat_ctx* ctx, struct neat_pollable_socket* pollable_socket);
static neat_flow * do_accept(neat_ctx *ctx, neat_flow *flow, struct neat_pollable_socket *socket);
neat_flow * nt_find_flow(neat_ctx *, struct sockaddr_storage *, struct sockaddr_storage *);
static void io_all_written(neat_ctx *ctx, neat_flow *flow, uint16_t stream_id);
#define TAG_STRING(tag) [tag] = #tag
const char *neat_tag_name[NEAT_TAG_LAST] = {
TAG_STRING(NEAT_TAG_STREAM_ID),
TAG_STRING(NEAT_TAG_STREAM_COUNT),
TAG_STRING(NEAT_TAG_LOCAL_NAME),
TAG_STRING(NEAT_TAG_LOCAL_ADDRESS),
TAG_STRING(NEAT_TAG_SERVICE_NAME),
TAG_STRING(NEAT_TAG_CONTEXT),
TAG_STRING(NEAT_TAG_PARTIAL_RELIABILITY_METHOD),
TAG_STRING(NEAT_TAG_PARTIAL_RELIABILITY_VALUE),
TAG_STRING(NEAT_TAG_PARTIAL_MESSAGE_RECEIVED),
TAG_STRING(NEAT_TAG_PARTIAL_SEQNUM),
TAG_STRING(NEAT_TAG_UNORDERED),
TAG_STRING(NEAT_TAG_UNORDERED_SEQNUM),
TAG_STRING(NEAT_TAG_DESTINATION_IP_ADDRESS),
TAG_STRING(NEAT_TAG_PRIORITY),
TAG_STRING(NEAT_TAG_FLOW_GROUP),
TAG_STRING(NEAT_TAG_CC_ALGORITHM),
TAG_STRING(NEAT_TAG_TRANSPORT_STACK),
TAG_STRING(NEAT_TAG_CHANNEL_NAME)
};
#define MIN(a,b) (((a)<(b))?(a):(b))
//Intiailize the OS-independent part of the context, and call the OS-dependent
//init function
struct neat_ctx *
neat_init_ctx()
{
struct neat_ctx *nc;
struct neat_ctx *ctx = NULL;
nc = calloc(1, sizeof(struct neat_ctx));
if (!nc) {
return NULL;
}
nc->loop = calloc(1, sizeof(uv_loop_t));
nc->pvd = NULL;
if (nc->loop == NULL) {
free(nc);
return NULL;
}
nc->error = NEAT_OK;
nc->log_level = NEAT_LOG_DEBUG;
nt_log_init(nc);
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
// TODO: Disable these checks for non-debug builds
if (sizeof(neat_tag_name) / sizeof(neat_tag_name[0]) != NEAT_TAG_LAST) {
nt_log(nc, NEAT_LOG_DEBUG,
"Warning: Expected %d tag names, but found %d tag names",
NEAT_TAG_LAST,
sizeof(neat_tag_name) / sizeof(*neat_tag_name));
}
for (int i = 0; i < NEAT_TAG_LAST; ++i) {
if (neat_tag_name[i] == NULL) {
nt_log(nc, NEAT_LOG_DEBUG, "Warning: Missing one or more tag names (index %d)", i);
break;
}
}
uv_loop_init(nc->loop);
LIST_INIT(&(nc->src_addrs));
LIST_INIT(&(nc->flows));
uv_timer_init(nc->loop, &(nc->addr_lifetime_handle));
nc->addr_lifetime_handle.data = nc;
uv_timer_start(&(nc->addr_lifetime_handle),
nt_addr_lifetime_timeout_cb,
1000 * NEAT_ADDRESS_LIFETIME_TIMEOUT,
1000 * NEAT_ADDRESS_LIFETIME_TIMEOUT);
nt_security_init(nc);
#if defined(USRSCTP_SUPPORT)
if (usr_intern.num_ctx == 0) {
nt_usrsctp_init(nc);
}
nt_usrsctp_init_ctx(nc);
#endif
#if defined(__linux__)
ctx = nt_linux_init_ctx(nc);
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
ctx = nt_bsd_init_ctx(nc);
#else
uv_loop_close(nc->loop);
#endif
if (!ctx) {
free(nc->loop);
free(nc);
}
#if defined(USRSCTP_SUPPORT)
usr_intern.num_ctx++;
#endif
return ctx;
}
//Start the internal NEAT event loop
//TODO: Add support for embedding libuv loops in other event loops
neat_error_code
neat_start_event_loop(struct neat_ctx *nc, neat_run_mode run_mode)
{
if (run_mode == NEAT_RUN_DEFAULT)
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
uv_run(nc->loop, (uv_run_mode) run_mode);
uv_loop_close(nc->loop);
return nc->error;
}
void
neat_stop_event_loop(struct neat_ctx *nc)
{
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
if (nc && nc->loop) {
uv_stop(nc->loop);
}
}
int
neat_get_backend_fd(struct neat_ctx *nc)
{
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
return uv_backend_fd(nc->loop);
}
uv_loop_t
*neat_get_event_loop(struct neat_ctx *ctx)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
assert(ctx);
return ctx->loop;
}
/*
* Terminate a NEAT context upon error.
* Errors are reported back to the user through neat_start_event_loop.
*/
void
nt_ctx_fail_on_error(struct neat_ctx *nc, neat_error_code error)
{
if (error == NEAT_OK)
return;
nc->error = error;
neat_stop_event_loop(nc);
}
int
neat_get_backend_timeout(struct neat_ctx *nc)
{
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
return uv_backend_timeout(nc->loop);
}
static void
nt_walk_cb(uv_handle_t *handle, void *ctx)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
// Bug fix (karlgrin, 170323).
if ((handle == NULL) || (handle->data == NULL))
return;
//HACK: Can't stop the IDLE handle used by resolver. Should probably do
//something more advanced in case we use other idle handles
if (handle->type == UV_IDLE) {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - handle->type == UV_IDLE - skipping", __func__);
return;
}
if (!uv_is_closing(handle)) {
// If this assert triggers, then some handle is not being closed
// correctly. A handle with a data pointer should already be closed
// before this point since the next uv_close operation will not free
// any handles. In other words, you have a memory leak.
assert(handle->data);
nt_log(ctx, NEAT_LOG_DEBUG, "%s - closing handle", __func__);
uv_close(handle, NULL);
}
}
//Free any resource used by the context. Loop must be stopped before this is
//called
//TODO: Consider adding callback, like for resolver
void
neat_free_ctx(struct neat_ctx *nc)
{
struct neat_flow *flow, *prev_flow = NULL;
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
if (!nc) {
return;
}
if (nc->resolver) {
nt_resolver_release(nc->resolver);
}
while (!LIST_EMPTY(&nc->flows)) {
flow = LIST_FIRST(&nc->flows);
/*
* If this assert triggers, it means that a call to neat_free_flow did
* not remove the flow pointed to by f from the list of flows. The
* assert is present because clang-analyzer somehow doesn't see the fact
* that the list is changed in nt_free_flow().
*/
assert(flow != prev_flow);
if (flow == prev_flow) {
nt_log(nc, NEAT_LOG_ERROR, "%s - nt_free_flow() failed", __func__);
}
/* NEAT is shutting down. Make sure that close callback is called here,
* since there is no main loop any more. */
if (!flow->socket->multistream
#ifdef SCTP_MULTISTREAMING
|| flow->socket->sctp_streams_used == 0
#endif
) {
flow->closefx(flow->ctx, flow);
}
if (flow->operations.on_close) {
//READYCALLBACKSTRUCT;
flow->operations.on_close(&flow->operations);
}
nt_free_flow(flow);
prev_flow = flow;
}
//uv_run(nc->loop, UV_RUN_NOWAIT);
uv_walk(nc->loop, nt_walk_cb, nc);
//Let all close handles run
uv_run(nc->loop, UV_RUN_DEFAULT);
uv_loop_close(nc->loop);
nt_addr_free_src_list(nc);
if (nc->cleanup) {
nc->cleanup(nc);
}
if (nc->event_cbs) {
free(nc->event_cbs);
}
if (nc->pvd) {
neat_pvd_release(nc->pvd);
free(nc->pvd);
}
free(nc->loop);
nt_security_close(nc);
nt_log_close(nc);
free(nc);
#if defined(USRSCTP_SUPPORT)
usr_intern.num_ctx--;
#endif
}
//The three functions that deal with the NEAT callback API. Nothing very
//interesting, register a callback, run all callbacks and remove callbacks
uint8_t nt_add_event_cb(struct neat_ctx *nc, uint8_t event_type,
struct neat_event_cb *cb)
{
uint8_t i = 0;
struct neat_event_cbs *cb_list_head;
struct neat_event_cb *cb_itr;
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
if (event_type > NEAT_MAX_EVENT)
return RETVAL_FAILURE;
//Do not initialize callback array before we have to, in case no-one will
//use the callback API
if (!nc->event_cbs) {
nc->event_cbs = calloc(NEAT_MAX_EVENT + 1,
sizeof(struct neat_event_cbs));
if (!nc->event_cbs)
return RETVAL_FAILURE;
for (i = 0; i < NEAT_MAX_EVENT; i++)
LIST_INIT(&(nc->event_cbs[i]));
}
cb_list_head = &(nc->event_cbs[event_type]);
for (cb_itr = cb_list_head->lh_first; cb_itr != NULL;
cb_itr = cb_itr->next_cb.le_next) {
if (cb_itr == cb) {
//TODO: Debug level
nt_log(nc, NEAT_LOG_INFO, "%s - Callback for %u has already been added", __func__, event_type);
return RETVAL_FAILURE;
}
}
//TODO: Debug level
nt_log(nc, NEAT_LOG_INFO, "%s - Added new callback for event type %u", __func__, event_type);
LIST_INSERT_HEAD(cb_list_head, cb, next_cb);
return RETVAL_SUCCESS;
}
uint8_t nt_remove_event_cb(struct neat_ctx *nc, uint8_t event_type,
struct neat_event_cb *cb)
{
struct neat_event_cbs *cb_list_head = NULL;
struct neat_event_cb *cb_itr = NULL;
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
if (event_type > NEAT_MAX_EVENT || !nc->event_cbs)
return RETVAL_FAILURE;
cb_list_head = &(nc->event_cbs[event_type]);
for (cb_itr = cb_list_head->lh_first; cb_itr != NULL;
cb_itr = cb_itr->next_cb.le_next) {
if (cb_itr == cb)
break;
}
if (cb_itr) {
//TODO: Debug level print
nt_log(nc, NEAT_LOG_INFO, "%s - Removed callback for type %u", __func__, event_type);
LIST_REMOVE(cb_itr, next_cb);
}
return RETVAL_SUCCESS;
}
void
nt_run_event_cb(struct neat_ctx *nc, uint8_t event_type, void *data)
{
struct neat_event_cbs *cb_list_head = NULL;
struct neat_event_cb *cb_itr = NULL;
nt_log(nc, NEAT_LOG_DEBUG, "%s", __func__);
if (event_type > NEAT_MAX_EVENT ||
!nc->event_cbs)
return;
cb_list_head = &(nc->event_cbs[event_type]);
for (cb_itr = cb_list_head->lh_first; cb_itr != NULL;
cb_itr = cb_itr->next_cb.le_next)
cb_itr->event_cb(nc, cb_itr->data, data);
}
struct neat_iofilter *
insert_neat_iofilter(neat_ctx *ctx, neat_flow *flow)
{
struct neat_iofilter *filter = calloc(1, sizeof (struct neat_iofilter));
if (filter) {
filter->next = flow->iofilters;
flow->iofilters = filter;
}
return filter;
}
static void free_iofilters(struct neat_iofilter *filter)
{
if (!filter) {
return;
}
free_iofilters(filter->next);
if (filter->dtor) {
filter->dtor(filter);
}
free (filter);
}
static void free_dtlsdata(struct neat_dtls_data *dtls)
{
if (!dtls) {
return;
}
if (dtls->dtor) {
dtls->dtor(dtls);
}
free (dtls);
dtls = NULL;
}
static void
on_handle_closed(uv_handle_t *handle)
{
//nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
free(handle);
}
static void
on_handle_closed_candidate(uv_handle_t *handle)
{
struct neat_he_candidate *candidate = (struct neat_he_candidate *)handle->data;
close(candidate->pollable_socket->fd);
if (candidate->pollable_socket->dtls_data) {
free_dtlsdata(candidate->pollable_socket->dtls_data);
}
free(candidate->pollable_socket);
free(candidate->if_name);
json_decref(candidate->properties);
free(candidate);
free(handle);
}
void
nt_free_candidate(struct neat_ctx *ctx, struct neat_he_candidate *candidate)
{
struct neat_he_sockopt *sockopt;
struct neat_he_sockopt *tmp;
struct linger so_linger;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (candidate == NULL) {
return;
}
// Enable SO_LINGER so that the following close will abort the connection.
// It is desired to abort any unused connections.
so_linger.l_onoff = 1;
so_linger.l_linger = 0;
if (candidate->prio_timer != NULL) {
uv_timer_stop(candidate->prio_timer);
uv_close((uv_handle_t *) candidate->prio_timer, on_handle_closed);
}
free(candidate->pollable_socket->dst_address);
free(candidate->pollable_socket->src_address);
if (!TAILQ_EMPTY(&(candidate->sock_opts))) {
TAILQ_FOREACH_SAFE(sockopt, (&candidate->sock_opts), next, tmp) {
if (sockopt->type == NEAT_SOCKOPT_STRING) {
free(sockopt->value.s_val);
}
TAILQ_REMOVE((&candidate->sock_opts), sockopt, next);
}
}
// We should close the handle of this candidate asynchronously, but only if
// this handle is not being used by the flow.
if (candidate->pollable_socket->handle != NULL) {
if (candidate->pollable_socket->handle == candidate->pollable_socket->flow->socket->handle) {
nt_log(ctx, NEAT_LOG_DEBUG,"%s: Handle used by flow, flow should release it", __func__);
} else {
if (candidate->pollable_socket->fd == -1) {
nt_log(ctx, NEAT_LOG_DEBUG,"%s: Candidate does not use a socket", __func__);
#if defined(USRSCTP_SUPPORT)
if (candidate->pollable_socket->usrsctp_socket != NULL) {
if (usrsctp_setsockopt(candidate->pollable_socket->usrsctp_socket, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger)) < 0) {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - usrsctp_setsockopt(SO_LINGER) failed", __func__);
}
usrsctp_close(candidate->pollable_socket->usrsctp_socket);
}
#endif
free(candidate->pollable_socket->handle);
} else if (!uv_is_closing((uv_handle_t*)candidate->pollable_socket->handle)) {
nt_log(ctx, NEAT_LOG_DEBUG,"%s: Release candidate after closing (%d)", __func__,
candidate->pollable_socket->fd);
if (setsockopt(candidate->pollable_socket->fd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger)) < 0) {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - setsockopt(SO_LINGER) failed", __func__);
}
candidate->pollable_socket->handle->data = candidate;
uv_close((uv_handle_t*)candidate->pollable_socket->handle, on_handle_closed_candidate);
return;
} else {
nt_log(ctx, NEAT_LOG_DEBUG,"%s: Candidate handle is already closing", __func__);
}
}
}
if (candidate->pollable_socket->dtls_data) {
free (candidate->pollable_socket->dtls_data->userData);
candidate->pollable_socket->dtls_data->userData = NULL;
free (candidate->pollable_socket->dtls_data);
candidate->pollable_socket->dtls_data = NULL;
}
free(candidate->pollable_socket);
free(candidate->if_name);
json_decref(candidate->properties);
free(candidate);
}
void
nt_free_candidates(struct neat_ctx *ctx, struct neat_he_candidates *candidates)
{
struct neat_he_candidate *candidate;
struct neat_he_candidate *tmp;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (candidates == NULL) {
return;
}
TAILQ_FOREACH_SAFE(candidate, candidates, next, tmp) {
nt_free_candidate(ctx, candidate);
}
free(candidates);
}
static void
synchronous_free(neat_flow *flow)
{
nt_log(flow->ctx, NEAT_LOG_DEBUG, "%s", __func__);
assert(flow);
assert(flow->socket);
free((char *)flow->name);
free((char *)flow->server_pem);
free((char *)flow->cert_pem);
free((char *)flow->key_pem);
if (flow->cc_algorithm) {
free((char*)flow->cc_algorithm);
}
if (flow->resolver_results) {
nt_log(flow->ctx, NEAT_LOG_DEBUG, "%s - neat_resolver_free_results", __func__);
nt_resolver_free_results(flow->resolver_results);
} else {
nt_log(flow->ctx, NEAT_LOG_DEBUG, "%s - NOT neat_resolver_free_results", __func__);
}
json_decref(flow->properties);
free_iofilters(flow->iofilters);
free_dtlsdata(flow->socket->dtls_data);
free(flow->readBuffer);
if (!flow->socket->multistream
#ifdef SCTP_MULTISTREAMING
|| flow->socket->sctp_streams_used == 0
#endif
) {
free(flow->socket->handle);
#if defined(USRSCTP_SUPPORT)
if (nt_base_stack(flow->socket->stack) == NEAT_STACK_SCTP) {
if (flow->socket->usrsctp_socket) {
usrsctp_close(flow->socket->usrsctp_socket);
}
}
#endif
free(flow->socket);
}
free(flow);
}
static void
socket_handle_free_cb(uv_handle_t *handle)
{
struct neat_pollable_socket *pollable_socket = handle->data;
#ifdef SCTP_MULTISTREAMING
struct neat_flow *flow = NULL;
struct neat_flow *next_flow = NULL;
#endif
assert(pollable_socket);
if (pollable_socket->multistream) {
#ifdef SCTP_MULTISTREAMING
#if 0
while (!LIST_EMPTY(&pollable_socket->sctp_multistream_flows)) {
flow = LIST_FIRST(&pollable_socket->sctp_multistream_flows);
assert(flow);
LIST_REMOVE(flow, multistream_next_flow);
synchronous_free(flow);
}
#else
LIST_FOREACH_SAFE(flow, &(pollable_socket->sctp_multistream_flows), multistream_next_flow, next_flow) {
LIST_REMOVE(flow, multistream_next_flow);
synchronous_free(flow);
}
#endif
//assert(pollable_socket->sctp_streams_used == 0);
//free(pollable_socket->handle);
//free(pollable_socket);
#else
assert(false);
#endif
} else {
synchronous_free(pollable_socket->flow);
}
}
static void
listen_socket_handle_free_cb(uv_handle_t *handle)
{
struct neat_pollable_socket *pollable_socket = handle->data;
//struct neat_ctx *ctx = pollable_socket->flow->ctx;
//nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
free(pollable_socket);
free(handle);
}
static int
nt_close_socket(struct neat_ctx *ctx, struct neat_flow *flow)
{
struct neat_pollable_socket *listening_socket;
struct neat_pollable_socket *listening_socket_temp;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
#if defined(USRSCTP_SUPPORT)
if (nt_base_stack(flow->socket->stack) == NEAT_STACK_SCTP) {
nt_close_via_usrsctp(flow->ctx, flow);
return 0;
}
#endif
// close all listening sockets
TAILQ_FOREACH_SAFE(listening_socket, &(flow->listen_sockets), next, listening_socket_temp) {
assert(listening_socket->fd > 0);
close(listening_socket->fd);
}
// close active socket
close(flow->socket->fd);
return 0;
}
void
nt_free_flow(neat_flow *flow)
{
struct neat_ctx *ctx = flow->ctx;
struct neat_pollable_socket *listen_socket;
struct neat_pollable_socket *listen_socket_temp;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
nt_log(ctx, NEAT_LOG_INFO, "%s - removing %p", __func__, flow);
LIST_REMOVE(flow, next_flow);
#if defined(USRSCTP_SUPPORT)
if (nt_base_stack(flow->socket->stack) == NEAT_STACK_SCTP) {
synchronous_free(flow);
return;
}
#endif
nt_free_candidates(ctx, flow->candidate_list);
flow->candidate_list = NULL;
// close all listening sockets
TAILQ_FOREACH_SAFE(listen_socket, &(flow->listen_sockets), next, listen_socket_temp) {
if (!uv_is_closing((uv_handle_t *)listen_socket->handle)) {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - closing listening handle and waiting for listen_socket_handle_free_cb", __func__);
uv_close((uv_handle_t *)(listen_socket->handle), listen_socket_handle_free_cb);
} else {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - listen handle is already closing", __func__);
}
}
#ifdef SCTP_MULTISTREAMING
if (flow->socket->multistream) {
LIST_REMOVE(flow, multistream_next_flow);
}
#endif
// close handles for active flow
if (flow->socket->handle != NULL && flow->socket->handle->type != UV_UNKNOWN_HANDLE
#ifdef SCTP_MULTISTREAMING
&& (!flow->socket->multistream || flow->socket->sctp_streams_used == 0)
#endif
) {
if (!uv_is_closing((uv_handle_t *)flow->socket->handle)) {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - closing handle and waiting for socket_handle_free_cb", __func__);
uv_close((uv_handle_t *)(flow->socket->handle), socket_handle_free_cb);
} else {
nt_log(ctx, NEAT_LOG_DEBUG, "%s - handle is already closing", __func__);
}
} else {
synchronous_free(flow);
}
}
neat_error_code
neat_set_property(neat_ctx *ctx, neat_flow *flow, const char *properties)
{
json_t *prop, *props;
json_t *val;
json_error_t error;
const char *key;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
nt_log(ctx, NEAT_LOG_DEBUG, "%s - %s", __func__, properties);
if (strlen(properties) != 0) {
props = json_loads(properties, 0, &error);
if (props == NULL) {
nt_log(ctx, NEAT_LOG_DEBUG, "Error in property string, line %d col %d",
error.line, error.position);
nt_log(ctx, NEAT_LOG_DEBUG, "%s", error.text);
return NEAT_ERROR_BAD_ARGUMENT;
}
json_object_foreach(props, key, prop) {
if (strcmp(key, "transport") == 0) {
val = json_object_get(prop, "value");
assert(val);
if (json_typeof(val) == JSON_STRING) {
if (strcmp(json_string_value(val), "WEBRTC") == 0) {
flow->webrtcEnabled = true;
}
}
}
// This step is not strictly required, but informs of overwritten keys
if (json_object_del(flow->properties, key) == 0) {
nt_log(ctx, NEAT_LOG_DEBUG, "Existing property %s was overwritten!", key);
}
json_object_set(flow->properties, key, prop);
}
json_decref(props);
} else {
nt_log(ctx, NEAT_LOG_DEBUG, "User did not specify any properties!");
}
#if 0
char *buffer = json_dumps(flow->properties, JSON_INDENT(2));
nt_log(ctx, NEAT_LOG_DEBUG, "Flow properties are now:\n%s\n", buffer);
free(buffer);
#endif
return NEAT_OK;
}
neat_error_code
neat_get_property(neat_ctx *ctx, neat_flow *flow, const char* name, void *ptr, size_t *size)
{
json_t *prop;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (flow->properties == NULL) {
nt_log(ctx, NEAT_LOG_DEBUG, "Flow has no properties (properties == NULL)");
return NEAT_ERROR_UNABLE;
}
prop = json_object_get(flow->properties, name);
if (prop == NULL) {
nt_log(ctx, NEAT_LOG_DEBUG, "Flow has no property named %s");
return NEAT_ERROR_UNABLE;
}
prop = json_object_get(prop, "value");
if (prop == NULL) {
nt_log(ctx, NEAT_LOG_DEBUG, "Flow has property %s, but it contains no \"value\" key!");
return NEAT_ERROR_UNABLE;
}
switch (json_typeof(prop)) {
case JSON_STRING:
{
size_t str_len = json_string_length(prop);
if (str_len + 1 > *size) {
*size = str_len + 1;
return NEAT_ERROR_MESSAGE_TOO_BIG;
}
strncpy(ptr, json_string_value(prop), *size);
*size = str_len;
break;
}
case JSON_INTEGER:
{
if (sizeof(json_int_t) > *size) {
*size = sizeof(json_int_t);
return NEAT_ERROR_MESSAGE_TOO_BIG;
}
*((json_int_t*)ptr) = json_integer_value(prop);
*size = sizeof(json_int_t);
break;
}
case JSON_TRUE:
case JSON_FALSE:
{
if (sizeof(json_int_t) > *size) {
*size = sizeof(json_int_t);
return NEAT_ERROR_MESSAGE_TOO_BIG;
}
*((json_int_t*)ptr) = json_is_true(prop);
*size = sizeof(json_int_t);
break;
}
default:
return NEAT_ERROR_UNABLE;
}
return NEAT_OK;
}
int
nt_get_stack(neat_ctx* mgr, neat_flow* flow)
{
return flow->socket->stack;
}
neat_error_code
neat_set_operations(neat_ctx *ctx, neat_flow *flow, struct neat_flow_operations *ops)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (&flow->operations != ops) {
int transport_protocol = flow->operations.transport_protocol;
// only copy when not trying to set the same ops again
flow->operations = *ops;
flow->operations.transport_protocol = transport_protocol;
}
if (flow->socket == NULL) {
return NEAT_OK;
}
#if defined(USRSCTP_SUPPORT)
if (nt_base_stack(flow->socket->stack) == NEAT_STACK_SCTP) {
// handle_upcall(flow->socket->usrsctp_socket, flow->socket, 0);
return NEAT_OK;
}
#endif
nt_update_poll_handle(ctx, flow, flow->socket->handle);
return NEAT_OK;
}
/* Return statistics about the flow in JSON format
NB - the memory allocated for the return string must be freed
by the caller */
neat_error_code
neat_get_stats(neat_ctx *ctx, char **json_stats)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
nt_stats_build_json(ctx, json_stats);
return NEAT_OK;
}
#define READYCALLBACKSTRUCT \
flow->operations.status = code;\
flow->operations.stream_id = stream_id;\
flow->operations.ctx = ctx;\
flow->operations.flow = flow;
void
nt_io_error(neat_ctx *ctx, neat_flow *flow, neat_error_code code)
{
const int stream_id = NEAT_INVALID_STREAM;
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
if (!flow) {
nt_log(ctx, NEAT_LOG_ERROR, "Called nt_io_error() with invalid flow");
return;
}
if (!flow->operations.on_error) {
return;
}
READYCALLBACKSTRUCT;
flow->operations.on_error(&flow->operations);
}
static void
io_connected(neat_ctx *ctx, neat_flow *flow, neat_error_code code)
{
nt_log(ctx, NEAT_LOG_DEBUG, "%s", __func__);
const int stream_id = NEAT_INVALID_STREAM;
#if defined(IPPROTO_SCTP) && defined(SCTP_STATUS) && !defined(USRSCTP_SUPPORT)