forked from sipwise/rtpengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathice.c
1611 lines (1311 loc) · 43.9 KB
/
ice.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 "ice.h"
#include <glib.h>
#include <sys/time.h>
#include <unistd.h>
#include "str.h"
#include "call.h"
#include "helpers.h"
#include "log.h"
#include "obj.h"
#include "stun.h"
#include "poller.h"
#include "log_funcs.h"
#include "timerthread.h"
#include "call_interfaces.h"
#if __DEBUG
#define ICE_DEBUG 1
#else
#define ICE_DEBUG 0
#endif
#if ICE_DEBUG
#define __DBG(x...) ilogs(ice, LOG_DEBUG, x)
#else
#define __DBG(x...) ilogs(internals, LOG_DEBUG, x)
#endif
#define PAIR_FORMAT STR_FORMAT_M ":" STR_FORMAT_M ":%lu"
#define PAIR_FMT(p) \
STR_FMT_M(&(p)->local_intf->ice_foundation), \
STR_FMT_M(&(p)->remote_candidate->foundation), \
(p)->remote_candidate->component_id
struct fragment_key {
str call_id;
str from_tag;
};
struct sdp_fragment {
ng_buffer *ngbuf;
struct timeval received;
sdp_streams_q streams;
sdp_ng_flags flags;
};
static void __ice_agent_free(void *p);
static void create_random_ice_string(call_t *call, str *s, int len);
static void __do_ice_checks(struct ice_agent *ag);
static struct ice_candidate_pair *__pair_lookup(struct ice_agent *, struct ice_candidate *cand,
const struct local_intf *ifa);
static void __recalc_pair_prios(struct ice_agent *ag);
static void __role_change(struct ice_agent *ag, int new_controlling);
static void __get_complete_components(candidate_pair_q *out, struct ice_agent *ag, GTree *t, unsigned int);
static void __agent_schedule(struct ice_agent *ag, unsigned long);
static void __agent_schedule_abs(struct ice_agent *ag, const struct timeval *tv);
static void __agent_deschedule(struct ice_agent *ag);
static void __ice_agent_free_components(struct ice_agent *ag);
static void __agent_shutdown(struct ice_agent *ag);
static void ice_agents_timer_run(void *);
static uint64_t tie_breaker;
static struct timerthread ice_agents_timer_thread;
static const char ice_chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const unsigned int ice_type_preferences[] = {
[ICT_UNKNOWN] = 0,
[ICT_HOST] = 126,
[ICT_SRFLX] = 100,
[ICT_PRFLX] = 110,
[ICT_RELAY] = 0,
};
const char * const ice_type_strings[] = {
[ICT_UNKNOWN] = "unknown",
[ICT_HOST] = "host",
[ICT_SRFLX] = "srflx",
[ICT_PRFLX] = "prflx",
[ICT_RELAY] = "relay",
};
static unsigned int frag_key_hash(const void *A);
static int frag_key_eq(const void *A, const void *B);
static void fragment_key_free(struct fragment_key *);
TYPED_GQUEUE(fragment, struct sdp_fragment)
TYPED_GHASHTABLE(fragments_ht, struct fragment_key, fragment_q, frag_key_hash, frag_key_eq,
fragment_key_free, NULL)
TYPED_GHASHTABLE_LOOKUP_INSERT(fragments_ht, fragment_key_free, fragment_q_new)
static mutex_t sdp_fragments_lock;
static fragments_ht sdp_fragments;
static void ice_update_media_streams(struct call_monologue *ml, sdp_streams_q *streams) {
for (__auto_type l = streams->head; l; l = l->next) {
struct stream_params *sp = l->data;
struct call_media *media = NULL;
if (sp->media_id.len)
media = g_hash_table_lookup(ml->media_ids, &sp->media_id);
else if (sp->index > 0) {
unsigned int arr_idx = sp->index - 1;
if (arr_idx < ml->medias->len)
media = ml->medias->pdata[arr_idx];
}
if (!media) {
ilogs(ice, LOG_WARN, "No matching media for trickle ICE update found");
continue;
}
if (!media->ice_agent) {
ilogs(ice, LOG_WARN, "Media for trickle ICE update is not ICE-enabled");
continue;
}
if (!MEDIA_ISSET(media, TRICKLE_ICE)) {
ilogs(ice, LOG_WARN, "Media for trickle ICE update is not trickle-ICE-enabled");
continue;
}
ice_update(media->ice_agent, sp, false);
}
}
static unsigned int frag_key_hash(const void *A) {
const struct fragment_key *a = A;
return str_hash(&a->call_id) ^ str_hash(&a->from_tag);
}
static int frag_key_eq(const void *A, const void *B) {
const struct fragment_key *a = A;
const struct fragment_key *b = B;
return str_equal(&a->call_id, &b->call_id)
&& str_equal(&a->from_tag, &b->from_tag);
}
static void fragment_free(struct sdp_fragment *frag) {
sdp_streams_clear(&frag->streams);
call_ng_free_flags(&frag->flags);
obj_put(frag->ngbuf);
g_slice_free1(sizeof(*frag), frag);
}
static void fragment_key_free(struct fragment_key *k) {
g_free(k->call_id.s);
g_free(k->from_tag.s);
g_slice_free1(sizeof(*k), k);
}
static void queue_sdp_fragment(ng_buffer *ngbuf, sdp_streams_q *streams, sdp_ng_flags *flags) {
ilog(LOG_DEBUG, "Queuing up SDP fragment for " STR_FORMAT_M "/" STR_FORMAT_M,
STR_FMT_M(&flags->call_id), STR_FMT_M(&flags->from_tag));
struct fragment_key *k = g_slice_alloc0(sizeof(*k));
str_init_dup_str(&k->call_id, &flags->call_id);
str_init_dup_str(&k->from_tag, &flags->from_tag);
struct sdp_fragment *frag = g_slice_alloc0(sizeof(*frag));
frag->received = rtpe_now;
frag->ngbuf = obj_get(ngbuf);
frag->streams = *streams;
frag->flags = *flags;
t_queue_init(streams);
ZERO(*flags);
mutex_lock(&sdp_fragments_lock);
fragment_q *frags = fragments_ht_lookup_insert(sdp_fragments, k);
t_queue_push_tail(frags, frag);
mutex_unlock(&sdp_fragments_lock);
}
bool trickle_ice_update(ng_buffer *ngbuf, call_t *call, sdp_ng_flags *flags,
sdp_streams_q *streams)
{
if (!flags->fragment)
return false;
if (!call) {
queue_sdp_fragment(ngbuf, streams, flags);
return true;
}
struct call_monologue *ml = call_get_monologue(call, &flags->from_tag);
if (!ml) {
queue_sdp_fragment(ngbuf, streams, flags);
return true;
}
ice_update_media_streams(ml, streams);
return true;
}
#define MAX_FRAG_AGE 3000000
void dequeue_sdp_fragments(struct call_monologue *monologue) {
struct fragment_key k;
ZERO(k);
k.call_id = monologue->call->callid;
k.from_tag = monologue->tag;
fragment_q *frags = NULL;
{
LOCK(&sdp_fragments_lock);
t_hash_table_steal_extended(sdp_fragments, &k, NULL, &frags);
if (!frags)
return;
// we own the queue now
}
struct sdp_fragment *frag;
while ((frag = t_queue_pop_head(frags))) {
if (timeval_diff(&rtpe_now, &frag->received) > MAX_FRAG_AGE)
goto next;
ilog(LOG_DEBUG, "Dequeuing SDP fragment for " STR_FORMAT_M "/" STR_FORMAT_M,
STR_FMT_M(&k.call_id), STR_FMT_M(&k.from_tag));
ice_update_media_streams(monologue, &frag->streams);
next:
fragment_free(frag);
}
t_queue_free(frags);
}
static gboolean fragment_check_cleanup(struct fragment_key *key, fragment_q *frags, void *p) {
bool all = GPOINTER_TO_INT(p);
if (!key || !frags)
return TRUE;
while (frags->length) {
struct sdp_fragment *frag = frags->head->data;
if (!all && timeval_diff(&rtpe_now, &frag->received) <= MAX_FRAG_AGE)
break;
t_queue_pop_head(frags);
fragment_free(frag);
}
if (!frags->length) {
t_queue_free(frags);
return TRUE;
}
return FALSE;
}
static void fragments_cleanup(bool all) {
mutex_lock(&sdp_fragments_lock);
t_hash_table_foreach_remove(sdp_fragments, fragment_check_cleanup, GINT_TO_POINTER(all));
mutex_unlock(&sdp_fragments_lock);
}
enum ice_candidate_type ice_candidate_type(const str *s) {
int i;
for (i = 1; i < G_N_ELEMENTS(ice_type_strings); i++) {
if (!str_cmp(s, ice_type_strings[i]))
return i;
}
return ICT_UNKNOWN;
}
bool ice_has_related(enum ice_candidate_type t) {
if (t == ICT_HOST)
return false;
/* ignoring ICT_UNKNOWN */
return true;
}
static uint64_t __ice_pair_priority(const struct local_intf *ifa, struct ice_candidate *cand,
int controlling)
{
uint64_t g, d;
g = ice_priority(ICT_HOST, ifa->unique_id, cand->component_id);
d = cand->priority;
if (!controlling) {
uint64_t t = g;
g = d;
d = t;
}
return (MIN(g,d) << 32) + (MAX(g,d) << 1) + (g > d ? 1 : 0);
}
static void __do_ice_pair_priority(struct ice_candidate_pair *pair) {
pair->pair_priority = __ice_pair_priority(pair->local_intf, pair->remote_candidate,
AGENT_ISSET(pair->agent, CONTROLLING));
}
static void __new_stun_transaction(struct ice_candidate_pair *pair) {
struct ice_agent *ag = pair->agent;
t_hash_table_remove(ag->transaction_hash, pair->stun_transaction);
random_string((void *) pair->stun_transaction, sizeof(pair->stun_transaction));
t_hash_table_insert(ag->transaction_hash, pair->stun_transaction, pair);
}
/* agent must be locked */
static void __all_pairs_list(struct ice_agent *ag) {
t_queue_clear(&ag->all_pairs_list);
g_tree_get_values(&ag->all_pairs_list.q, ag->all_pairs);
}
static void __tree_coll_callback(void *oo, void *nn) {
struct ice_candidate_pair *o = oo, *n = nn;
ilogs(ice, LOG_WARN | LOG_FLAG_LIMIT, "Priority collision between candidate pairs " PAIR_FORMAT " and "
PAIR_FORMAT " - ICE will likely fail",
PAIR_FMT(o), PAIR_FMT(n));
}
/* agent must be locked */
static struct ice_candidate_pair *__pair_candidate(stream_fd *sfd, struct ice_agent *ag,
struct ice_candidate *cand)
{
struct ice_candidate_pair *pair;
if (sfd->socket.family != cand->endpoint.address.family)
return NULL;
pair = g_slice_alloc0(sizeof(*pair));
pair->agent = ag;
pair->remote_candidate = cand;
pair->local_intf = sfd->local_intf;
pair->sfd = sfd;
if (cand->component_id != 1)
PAIR_SET(pair, FROZEN);
__do_ice_pair_priority(pair);
__new_stun_transaction(pair);
t_queue_push_tail(&ag->candidate_pairs, pair);
t_hash_table_insert(ag->pair_hash, pair, pair);
g_tree_insert_coll(ag->all_pairs, pair, pair, __tree_coll_callback);
ilogs(ice, LOG_DEBUG, "Created candidate pair "PAIR_FORMAT" between %s and %s%s%s, type %s", PAIR_FMT(pair),
sockaddr_print_buf(&sfd->socket.local.address),
FMT_M(endpoint_print_buf(&cand->endpoint)),
ice_candidate_type_str(cand->type));
return pair;
}
static unsigned int __pair_hash(const void *p) {
const struct ice_candidate_pair *pair = p;
return g_direct_hash(pair->local_intf) ^ g_direct_hash(pair->remote_candidate);
}
static int __pair_equal(const void *a, const void *b) {
const struct ice_candidate_pair *A = a, *B = b;
return A->local_intf == B->local_intf
&& A->remote_candidate == B->remote_candidate;
}
static unsigned int __cand_hash(const void *p) {
const struct ice_candidate *cand = p;
return endpoint_hash(&cand->endpoint) ^ cand->component_id;
}
static int __cand_equal(const void *a, const void *b) {
const struct ice_candidate *A = a, *B = b;
return endpoint_eq(&A->endpoint, &B->endpoint)
&& A->component_id == B->component_id;
}
static unsigned int __found_hash(const void *p) {
const struct ice_candidate *cand = p;
return str_hash(&cand->foundation) ^ cand->component_id;
}
static int __found_equal(const void *a, const void *b) {
const struct ice_candidate *A = a, *B = b;
return str_equal(&A->foundation, &B->foundation)
&& A->component_id == B->component_id;
}
static unsigned int __trans_hash(const void *p) {
const uint32_t *tp = p;
return tp[0] ^ tp[1] ^ tp[2];
}
static int __trans_equal(const void *a, const void *b) {
const uint32_t *A = a, *B = b;
return A[0] == B[0] && A[1] == B[1] && A[2] == B[2];
}
static int __pair_prio_cmp(const void *a, const void *b) {
const struct ice_candidate_pair *A = a, *B = b;
/* highest priority first */
if (A->pair_priority < B->pair_priority)
return 1;
if (A->pair_priority > B->pair_priority)
return -1;
/* lowest component first */
if (A->remote_candidate->component_id < B->remote_candidate->component_id)
return -1;
if (A->remote_candidate->component_id > B->remote_candidate->component_id)
return 1;
/* highest local preference first, which is lowest unique_id first */
if (A->local_intf->unique_id < B->local_intf->unique_id)
return -1;
if (A->local_intf->unique_id > B->local_intf->unique_id)
return 1;
return 0;
}
TYPED_GHASHTABLE_IMPL(candidate_ht, __cand_hash, __cand_equal, NULL, NULL)
TYPED_GHASHTABLE_IMPL(candidate_pair_ht, __pair_hash, __pair_equal, NULL, NULL)
TYPED_GHASHTABLE_IMPL(foundation_ht, __found_hash, __found_equal, NULL, NULL)
TYPED_GHASHTABLE_IMPL(priority_ht, g_direct_hash, g_direct_equal, NULL, NULL)
TYPED_GHASHTABLE_IMPL(transaction_ht, __trans_hash, __trans_equal, NULL, NULL)
static void __ice_agent_initialize(struct ice_agent *ag) {
struct call_media *media = ag->media;
call_t *call = ag->call;
ag->candidate_hash = candidate_ht_new();
ag->cand_prio_hash = priority_ht_new();
ag->pair_hash = candidate_pair_ht_new();
ag->transaction_hash = transaction_ht_new();
ag->foundation_hash = foundation_ht_new();
atomic64_set_na(&ag->agent_flags, 0);
bf_copy(&ag->agent_flags, ICE_AGENT_CONTROLLING, &media->media_flags, MEDIA_FLAG_ICE_CONTROLLING);
bf_copy(&ag->agent_flags, ICE_AGENT_LITE_SELF, &media->media_flags, MEDIA_FLAG_ICE_LITE_SELF);
ag->logical_intf = media->logical_intf;
ag->desired_family = media->desired_family;
ag->nominated_pairs = g_tree_new(__pair_prio_cmp);
ag->valid_pairs = g_tree_new(__pair_prio_cmp);
ag->succeeded_pairs = g_tree_new(__pair_prio_cmp);
ag->all_pairs = g_tree_new(__pair_prio_cmp);
create_random_ice_string(call, &ag->ufrag[1], 8);
create_random_ice_string(call, &ag->pwd[1], 26);
atomic64_set(&ag->last_activity, rtpe_now.tv_sec);
}
static struct ice_agent *__ice_agent_new(struct call_media *media) {
struct ice_agent *ag;
call_t *call = media->call;
ag = obj_alloc0("ice_agent", sizeof(*ag), __ice_agent_free);
ag->tt_obj.tt = &ice_agents_timer_thread;
ag->call = obj_get(call);
ag->media = media;
mutex_init(&ag->lock);
__ice_agent_initialize(ag);
return ag;
}
/* called with the call lock held in W */
void ice_agent_init(struct ice_agent **agp, struct call_media *media) {
struct ice_agent *ag;
if (*agp)
ag = *agp;
else
*agp = ag = __ice_agent_new(media);
}
static int __copy_cand(call_t *call, struct ice_candidate *dst, const struct ice_candidate *src) {
int eq = (dst->priority == src->priority);
*dst = *src;
call_str_cpy(call, &dst->foundation, &src->foundation);
return eq ? 0 : 1;
}
static void __ice_reset(struct ice_agent *ag) {
__agent_deschedule(ag);
AGENT_CLEAR3(ag, COMPLETED, NOMINATING, USABLE);
__ice_agent_free_components(ag);
ZERO(ag->active_components);
ZERO(ag->start_nominating);
ZERO(ag->tt_obj.last_run);
__ice_agent_initialize(ag);
}
/* if the other side did a restart */
static void __ice_restart(struct ice_agent *ag) {
ilogs(ice, LOG_DEBUG, "ICE restart detected, resetting ICE agent");
ag->ufrag[0] = STR_NULL;
ag->pwd[0] = STR_NULL;
ag->ufrag[1] = STR_NULL;
ag->pwd[1] = STR_NULL;
__ice_reset(ag);
}
/* if we're doing a restart */
void ice_restart(struct ice_agent *ag) {
ilogs(ice, LOG_DEBUG, "Restarting ICE and resetting ICE agent");
ag->ufrag[1] = STR_NULL;
ag->pwd[1] = STR_NULL;
__ice_reset(ag);
}
/* called with the call lock held in W, hence agent doesn't need to be locked */
void ice_update(struct ice_agent *ag, struct stream_params *sp, bool allow_reset) {
struct ice_candidate *cand, *dup;
struct call_media *media;
call_t *call;
int recalc = 0;
unsigned int comps;
struct packet_stream *components[MAX_COMPONENTS], *ps;
candidate_q *candidates;
stream_fd *sfd;
if (!ag)
return;
log_info_ice_agent(ag);
atomic64_set(&ag->last_activity, rtpe_now.tv_sec);
media = ag->media;
call = media->call;
__role_change(ag, MEDIA_ISSET(media, ICE_CONTROLLING));
if (sp) {
if (ice_is_restart(ag, sp)) {
if (!allow_reset)
ilog(LOG_WARN, "ICE restart detected, but reset not allowed at this point");
else
__ice_restart(ag);
}
/* update remote info */
if (sp->ice_ufrag.s)
call_str_cpy(call, &ag->ufrag[0], &sp->ice_ufrag);
if (sp->ice_pwd.s)
call_str_cpy(call, &ag->pwd[0], &sp->ice_pwd);
candidates = &sp->ice_candidates;
}
else /* this is a dummy update in case rtcp-mux has changed */
candidates = &ag->remote_candidates;
/* get our component streams */
ZERO(components);
comps = 0;
for (__auto_type l = media->streams.head; l; l = l->next)
components[comps++] = l->data;
if (comps == 2 && (MEDIA_ISSET(media, RTCP_MUX) || !proto_is_rtp(media->protocol)))
components[1] = NULL;
comps = 0;
for (__auto_type l = candidates->head; l; l = l->next) {
if (ag->remote_candidates.length >= MAX_ICE_CANDIDATES) {
ilogs(ice, LOG_WARNING, "Maxmimum number of ICE candidates exceeded");
break;
}
cand = l->data;
/* skip invalid */
if (!cand->component_id || cand->component_id > G_N_ELEMENTS(components))
continue;
ps = components[cand->component_id - 1];
if (ps) /* only count active components */
comps = MAX(comps, cand->component_id);
dup = t_hash_table_lookup(ag->candidate_hash, cand);
if (!sp && dup) /* this isn't a real update, so only check pairings */
goto pair;
/* check for duplicates */
if (dup) {
/* if this is peer reflexive, we've learned it through STUN.
* otherwise it's simply one we've seen before. */
if (dup->type == ICT_PRFLX) {
ilogs(ice, LOG_DEBUG, "Replacing previously learned prflx ICE candidate with "
STR_FORMAT_M ":%lu", STR_FMT_M(&cand->foundation),
cand->component_id);
}
else {
/* if the new one has higher priority then the old one, then we
* update it, otherwise we just drop it */
if (cand->priority <= dup->priority) {
ilogs(ice, LOG_DEBUG, "Dropping new ICE candidate " STR_FORMAT_M
" in favour of "
STR_FORMAT_M ":%lu",
STR_FMT_M(&cand->foundation),
STR_FMT_M(&dup->foundation), cand->component_id);
continue;
}
ilogs(ice, LOG_DEBUG, "Replacing known ICE candidate " STR_FORMAT_M " with higher "
"priority "
STR_FORMAT_M ":%lu",
STR_FMT_M(&dup->foundation),
STR_FMT_M(&cand->foundation), cand->component_id);
}
/* priority and foundation may change */
t_hash_table_remove(ag->foundation_hash, dup);
recalc += __copy_cand(call, dup, cand);
}
else {
ilogs(ice, LOG_DEBUG, "Learning new ICE candidate " STR_FORMAT_M ":%lu",
STR_FMT_M(&cand->foundation), cand->component_id);
dup = g_slice_alloc(sizeof(*dup));
__copy_cand(call, dup, cand);
t_hash_table_insert(ag->candidate_hash, dup, dup);
t_hash_table_insert(ag->cand_prio_hash, GUINT_TO_POINTER(dup->priority), dup);
t_queue_push_tail(&ag->remote_candidates, dup);
}
t_hash_table_insert(ag->foundation_hash, dup, dup);
pair:
if (!ps)
continue;
for (__auto_type k = ps->sfds.head; k; k = k->next) {
sfd = k->data;
/* skip duplicates here also */
if (__pair_lookup(ag, dup, sfd->local_intf))
continue;
__pair_candidate(sfd, ag, dup);
}
}
if (comps)
ag->active_components = comps;
if (!ag->active_components) {
/* determine components for tricke-ice case */
comps = 2;
if (!components[1])
comps = 1;
ag->active_components = comps;
}
/* if we're here, we can start our ICE checks */
if (recalc)
__recalc_pair_prios(ag);
else
__all_pairs_list(ag);
if (comps)
__do_ice_checks(ag);
else
__agent_shutdown(ag);
log_info_pop();
}
static void ice_candidate_free(struct ice_candidate *p) {
g_slice_free1(sizeof(*p), p);
}
void ice_candidates_free(candidate_q *q) {
t_queue_clear_full(q, ice_candidate_free);
}
static void ice_candidate_pair_free(struct ice_candidate_pair *p) {
g_slice_free1(sizeof(struct ice_candidate_pair), p);
}
static void ice_candidate_pairs_free(candidate_pair_q *q) {
t_queue_clear_full(q, ice_candidate_pair_free);
}
/* call must be locked */
void ice_shutdown(struct ice_agent **agp) {
struct ice_agent *ag;
if (!agp) {
ilogs(ice, LOG_ERR, "ice agp is NULL");
return ;
}
ag = *agp;
if (!ag)
return;
__agent_deschedule(ag);
*agp = NULL;
obj_put(&ag->tt_obj);
}
static void __ice_agent_free_components(struct ice_agent *ag) {
if (!ag) {
ilogs(ice, LOG_ERR, "ice ag is NULL");
return;
}
t_queue_clear(&ag->triggered);
t_hash_table_destroy(ag->candidate_hash);
t_hash_table_destroy(ag->cand_prio_hash);
t_hash_table_destroy(ag->pair_hash);
t_hash_table_destroy(ag->transaction_hash);
t_hash_table_destroy(ag->foundation_hash);
g_tree_destroy(ag->all_pairs);
t_queue_clear(&ag->all_pairs_list);
g_tree_destroy(ag->nominated_pairs);
g_tree_destroy(ag->succeeded_pairs);
g_tree_destroy(ag->valid_pairs);
ice_candidates_free(&ag->remote_candidates);
ice_candidate_pairs_free(&ag->candidate_pairs);
}
static void __ice_agent_free(void *p) {
struct ice_agent *ag = p;
if (!ag) {
ilogs(ice, LOG_ERR, "ice ag is NULL");
return;
}
__DBG("freeing ice_agent");
__ice_agent_free_components(ag);
mutex_destroy(&ag->lock);
obj_put(ag->call);
}
static void __agent_schedule(struct ice_agent *ag, unsigned long usec) {
struct timeval nxt;
nxt = rtpe_now;
timeval_add_usec(&nxt, usec);
__agent_schedule_abs(ag, &nxt);
}
static void __agent_schedule_abs(struct ice_agent *ag, const struct timeval *tv) {
struct timeval nxt;
long long diff;
if (!ag)
return;
nxt = *tv;
mutex_lock(&ice_agents_timer_thread.lock);
if (ag->tt_obj.last_run.tv_sec) {
/* make sure we don't run more often than we should */
diff = timeval_diff(&nxt, &ag->tt_obj.last_run);
if (diff < TIMER_RUN_INTERVAL * 1000)
timeval_add_usec(&nxt, TIMER_RUN_INTERVAL * 1000 - diff);
}
timerthread_obj_schedule_abs_nl(&ag->tt_obj, &nxt);
mutex_unlock(&ice_agents_timer_thread.lock);
}
static void __agent_deschedule(struct ice_agent *ag) {
if (ag)
timerthread_obj_deschedule(&ag->tt_obj);
}
void ice_init(void) {
random_string((void *) &tie_breaker, sizeof(tie_breaker));
timerthread_init(&ice_agents_timer_thread, ice_agents_timer_run);
sdp_fragments = fragments_ht_new();
mutex_init(&sdp_fragments_lock);
}
void ice_free(void) {
timerthread_free(&ice_agents_timer_thread);
fragments_cleanup(true);
t_hash_table_destroy_ptr(&sdp_fragments);
mutex_destroy(&sdp_fragments_lock);
}
enum thread_looper_action ice_slow_timer(void) {
fragments_cleanup(false);
return TLA_CONTINUE;
}
static void __fail_pair(struct ice_candidate_pair *pair) {
ilogs(ice, LOG_DEBUG, "Setting ICE candidate pair "PAIR_FORMAT" as failed", PAIR_FMT(pair));
PAIR_SET(pair, FAILED);
PAIR_CLEAR(pair, IN_PROGRESS);
}
/* agent must NOT be locked, but call must be locked in R */
static void __do_ice_check(struct ice_candidate_pair *pair) {
stream_fd *sfd = pair->sfd;
struct ice_agent *ag = pair->agent;
uint32_t prio, transact[3];
if (AGENT_ISSET(ag, LITE_SELF))
PAIR_SET(pair, SUCCEEDED);
if (PAIR_ISSET(pair, SUCCEEDED) && !PAIR_ISSET(pair, TO_USE))
return;
if (!ag->pwd[0].s)
return;
prio = ice_priority(ICT_PRFLX, pair->local_intf->unique_id,
pair->remote_candidate->component_id);
mutex_lock(&ag->lock);
pair->retransmit = rtpe_now;
if (!PAIR_SET(pair, IN_PROGRESS)) {
PAIR_CLEAR2(pair, FROZEN, FAILED);
pair->retransmit_ms = STUN_RETRANSMIT_INTERVAL;
pair->retransmits = 0;
}
else if (pair->retransmits > STUN_MAX_RETRANSMITS) {
__fail_pair(pair);
mutex_unlock(&ag->lock);
return;
}
else {
pair->retransmit_ms *= 2;
pair->retransmits++;
}
timeval_add_usec(&pair->retransmit, pair->retransmit_ms * 1000);
__agent_schedule_abs(pair->agent, &pair->retransmit);
memcpy(transact, pair->stun_transaction, sizeof(transact));
pair->was_controlling = AGENT_ISSET(ag, CONTROLLING);
pair->was_nominated = PAIR_ISSET(pair, TO_USE);
mutex_unlock(&ag->lock);
ilogs(ice, LOG_DEBUG, "Sending %sICE/STUN request for candidate pair "PAIR_FORMAT" from %s to %s%s%s",
PAIR_ISSET(pair, TO_USE) ? "nominating " : "",
PAIR_FMT(pair), sockaddr_print_buf(&pair->local_intf->spec->local_address.addr),
FMT_M(endpoint_print_buf(&pair->remote_candidate->endpoint)));
stun_binding_request(&pair->remote_candidate->endpoint, transact, &ag->pwd[0], ag->ufrag,
AGENT_ISSET(ag, CONTROLLING), tie_breaker,
prio, &sfd->socket,
PAIR_ISSET(pair, TO_USE));
}
static int __component_find(const void *a, const void *b) {
const struct ice_candidate_pair *A = a;
unsigned int comp = GPOINTER_TO_UINT(b);
if (A->remote_candidate->component_id == comp)
return TRUE;
return FALSE;
}
static struct ice_candidate_pair *__get_pair_by_component(GTree *t, unsigned int component) {
return g_tree_find_first(t, __component_find, GUINT_TO_POINTER(component));
}
static void __get_pairs_by_component(candidate_pair_q *out, GTree *t, unsigned int component) {
g_tree_find_all(&out->q, t, __component_find, GUINT_TO_POINTER(component));
}
static void __get_complete_succeeded_pairs(candidate_pair_q *out, struct ice_agent *ag) {
__get_complete_components(out, ag, ag->succeeded_pairs, ICE_PAIR_SUCCEEDED);
}
static void __get_complete_valid_pairs(candidate_pair_q *out, struct ice_agent *ag) {
__get_complete_components(out, ag, ag->valid_pairs, ICE_PAIR_VALID);
}
static void __nominate_pairs(struct ice_agent *ag) {
candidate_pair_q complete;
struct ice_candidate_pair *pair;
ilogs(ice, LOG_DEBUG, "Start nominating ICE pairs");
AGENT_SET(ag, NOMINATING);
ZERO(ag->start_nominating);
__get_complete_succeeded_pairs(&complete, ag);
for (__auto_type l = complete.head; l; l = l->next) {
pair = l->data;
ilogs(ice, LOG_DEBUG, "Nominating ICE pair "PAIR_FORMAT, PAIR_FMT(pair));
PAIR_CLEAR(pair, IN_PROGRESS);
PAIR_SET2(pair, NOMINATED, TO_USE);
pair->retransmits = 0;
__new_stun_transaction(pair);
t_queue_push_tail(&ag->triggered, pair);
}
t_queue_clear(&complete);
}
/* call must be locked R or W, agent must not be locked */
static void __do_ice_checks(struct ice_agent *ag) {
struct ice_candidate_pair *pair, *highest = NULL, *frozen = NULL, *valid;
stream_fd *sfd;
GQueue retransmits = G_QUEUE_INIT;
struct timeval next_run = {0,0};
int have_more = 0;
if (!ag) {
ilogs(ice, LOG_ERR, "ice ag is NULL");
return;
}
if (!ag->pwd[0].s)
return;
atomic64_set(&ag->last_activity, rtpe_now.tv_sec);
__DBG("running checks, call "STR_FORMAT" tag "STR_FORMAT"", STR_FMT(&ag->call->callid),
STR_FMT(&ag->media->monologue->tag));
mutex_lock(&ag->lock);
/* check if we're done and should start nominating pairs */
if (AGENT_ISSET(ag, CONTROLLING) && !AGENT_ISSET(ag, NOMINATING) && ag->start_nominating.tv_sec) {
if (timeval_cmp(&rtpe_now, &ag->start_nominating) >= 0)
__nominate_pairs(ag);
timeval_lowest(&next_run, &ag->start_nominating);
}
/* triggered checks are preferred */
pair = t_queue_pop_head(&ag->triggered);
if (pair) {
__DBG("running triggered check on " PAIR_FORMAT, PAIR_FMT(pair));
PAIR_CLEAR(pair, TRIGGERED);
next_run = rtpe_now;
goto check;
}
/* find the highest-priority non-frozen non-in-progress pair */
for (__auto_type l = ag->all_pairs_list.head; l; l = l->next) {
pair = l->data;
__DBG("considering checking " PAIR_FORMAT, PAIR_FMT(pair));
/* skip dead streams */
sfd = pair->sfd;
if (!sfd || !sfd->stream || !sfd->stream->selected_sfd)
continue;
if (PAIR_ISSET(pair, FAILED))
continue;
if (PAIR_ISSET(pair, SUCCEEDED) && !PAIR_ISSET(pair, TO_USE))
continue;
valid = __get_pair_by_component(ag->valid_pairs, pair->remote_candidate->component_id);
if (PAIR_ISSET(pair, IN_PROGRESS)) {
/* handle retransmits */
/* but only if our priority is lower than any valid pair */
if (valid && valid->pair_priority > pair->pair_priority)
continue;
if (timeval_cmp(&pair->retransmit, &rtpe_now) <= 0)
g_queue_push_tail(&retransmits, pair); /* can't run check directly
due to locks */
else
timeval_lowest(&next_run, &pair->retransmit);
continue;
}
/* don't do anything else if we already have a valid pair */
if (valid)
continue;
/* or if we're in or past the final phase */
if (AGENT_ISSET2(ag, NOMINATING, COMPLETED))
continue;
have_more = 1;
/* remember the first frozen pair in case we find nothing else */
if (PAIR_ISSET(pair, FROZEN)) {
__DBG("pair " PAIR_FORMAT " is frozen", PAIR_FMT(pair));
if (!frozen)
frozen = pair;
continue;
}
if (!highest)
highest = pair;
}
if (highest) {
pair = highest;
__DBG("checking highest priority pair " PAIR_FORMAT, PAIR_FMT(pair));
}
else if (frozen) {
pair = frozen;
__DBG("checking highest priority frozen pair " PAIR_FORMAT, PAIR_FMT(pair));
}
else
pair = NULL;
check:
mutex_unlock(&ag->lock);
if (pair)
__do_ice_check(pair);
while ((pair = g_queue_pop_head(&retransmits)))
__do_ice_check(pair);
/* determine when to run next */
if (have_more)
__agent_schedule(ag, 0);
else if (next_run.tv_sec)
__agent_schedule_abs(ag, &next_run); /* for retransmits */