-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconncheck.c
2910 lines (2483 loc) · 101 KB
/
conncheck.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
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2006-2009 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
* Youness Alaoui, Collabora Ltd.
* Kai Vehmanen, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
/*
* Porting to C library using libevent.
* Jackie Dinh - 2016
*/
#include <sys/time.h>
#include "cice/agent.h"
#include "cice/base64.h"
#include "cice/component.h"
#include "cice/conncheck.h"
#include "cice/network.h"
#include "cice/types.h"
#include "cice/utils.h"
#include "cice/stun/usages/bind.h"
#include "cice/stun/usages/timer.h"
#include "cice/stun/usages/turn.h"
static int
priv_timer_expired (struct timeval *timer, struct timeval *now)
{
return (now->tv_sec == timer->tv_sec) ?
now->tv_usec >= timer->tv_usec :
now->tv_sec >= timer->tv_sec;
}
/*
* Sends a connectivity check over candidate pair 'pair'.
*
* @return zero on success, non-zero on error
*/
int conn_check_send(agent_t *agent, candidate_check_pair_t *pair)
{
/* note: following information is supplied:
* - username (for USERNAME attribute)
* - password (for MESSAGE-INTEGRITY)
* - priority (for PRIORITY)
* - ICE-CONTROLLED/ICE-CONTROLLING (for role conflicts)
* - USE-CANDIDATE (if sent by the controlling agent)
*/
uint8_t uname[ICE_STREAM_MAX_UNAME] = {0};
uint8_t *password = NULL;
stream_t *stream;
component_t *component;
uint32_t priority;
uint32_t uname_len;
size_t password_len;
uint32_t buffer_len;
int controlling = agent->controlling_mode;
int cand_use = controlling;
unsigned int timeout;
if ( agent == NULL || pair == NULL )
return ICE_ERR;
ICE_DEBUG("conn_check_send, controlling=%u",controlling);
if (agent_find_component (agent, pair->stream_id, pair->component_id,
&stream, &component) != ICE_OK)
return ICE_ERR;
uname_len = priv_create_username(agent, stream, pair->component_id,
pair->remote, pair->local, uname, sizeof (uname), 0);
password_len = priv_get_password(agent, stream, pair->remote, &password);
priority = peer_reflexive_candidate_priority(agent, pair->local);
if (password != NULL &&
(agent->compatibility == ICE_COMPATIBILITY_MSN ||
agent->compatibility == ICE_COMPATIBILITY_OC2007)) {
password = base64_decode ((const unsigned char *)password,password_len,&password_len);
}
{
char tmpbuf[INET6_ADDRSTRLEN];
char tmpbuf1[INET6_ADDRSTRLEN];
address_to_string (&pair->remote->addr, tmpbuf);
address_to_string (&pair->local->addr, tmpbuf1);
ICE_DEBUG("stun request, agent=%p,localaddr=%s, port=%u, remoteaddr=%s, port=%u, fd=%u, "
"foundation=%s ,cid=%u, tie=%llu, username=%s(%u), "
"password=%s(%lu), priority=%u", agent,
tmpbuf1, address_get_port (&pair->local->addr),
tmpbuf, address_get_port (&pair->remote->addr),
pair->sockptr->fd,
pair->foundation, pair->component_id,
(unsigned long long)agent->tie_breaker,
uname, uname_len, password, password_len,
priority);
}
if (cand_use) {
ICE_DEBUG("set pair nominated, pair=%p",pair);
pair->nominated = controlling;
}
if (uname_len > 0) {
buffer_len = stun_usage_ice_conncheck_create(&component->stun_agent,
&pair->stun_message, pair->stun_buffer, sizeof(pair->stun_buffer),
uname, uname_len, password, password_len,
cand_use, controlling, priority,
agent->tie_breaker,
pair->local->foundation,
agent_to_ice_compatibility (agent));
ICE_HEXDUMP(pair->stun_buffer, buffer_len,"stun");
ICE_DEBUG("conncheck created, agent=%p, bufflen=%d, buff=%p",
agent, buffer_len, pair->stun_message.buffer);
if (agent->compatibility == ICE_COMPATIBILITY_MSN ||
agent->compatibility == ICE_COMPATIBILITY_OC2007) {
free(password);
}
if (buffer_len > 0) {
ICE_DEBUG("FIXME: socket reliable, bufflen=%u",buffer_len);
//if (nice_socket_is_reliable(pair->sockptr)) {
// stun_timer_start_reliable(&pair->timer, STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT);
//} else {
stun_timer_start(&pair->timer,
priv_compute_conncheck_timer(agent, stream),
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
//}
/* TCP-ACTIVE candidate must create a new socket before sending
* by connecting to the peer. The new socket is stored in the candidate
* check pair, until we discover a new local peer reflexive */
ICE_DEBUG("pair info, fd=%d, transport=%u",pair->sockptr->fd,pair->local->transport);
if (pair->sockptr->fd == 0 && pair->local->transport == ICE_CANDIDATE_TRANSPORT_TCP_ACTIVE) {
stream_t *stream2 = NULL;
component_t *component2 = NULL;
//socket_t *new_socket;
if (agent_find_component(agent, pair->stream_id, pair->component_id,
&stream2, &component2) == ICE_OK) {
ICE_DEBUG("FIXME: create socket reliable");
/*new_socket = nice_tcp_active_socket_connect (pair->sockptr,
&pair->remote->addr);
if (new_socket) {
pair->sockptr = new_socket;
_priv_set_socket_tos (agent, pair->sockptr, stream2->tos);
component_attach_socket (component2, new_socket);
}*/
}
}
/* send the conncheck */
ICE_DEBUG("sending conncheck, buf=%p,bufflen=%u",pair->stun_buffer,buffer_len);
agent_socket_send(pair->sockptr, &pair->remote->addr,
(const char *)pair->stun_buffer, buffer_len);
timeout = stun_timer_remainder(&pair->timer);
/* note: convert from milli to microseconds for g_time_val_add() */
ICE_DEBUG("set timeout, timeout=%u",timeout);
gettimeofday(&pair->next_tick,NULL);
print_timeval(&pair->next_tick);
add_microseconds_to_timeval(&pair->next_tick, timeout * 1000);
print_timeval(&pair->next_tick);
/*g_get_current_time (&pair->next_tick);
g_time_val_add (&pair->next_tick, timeout * 1000);*/
} else {
ICE_DEBUG("Agent %p: buffer is empty, cancelling conncheck", agent);
pair->stun_message.buffer = NULL;
pair->stun_message.buffer_len = 0;
return -1;
}
} else {
ICE_DEBUG("Agent %p: no credentials found, cancelling conncheck", agent);
pair->stun_message.buffer = NULL;
pair->stun_message.buffer_len = 0;
return -1;
}
return 0;
}
/*
* Initiates a new connectivity check for a ICE candidate pair.
*
* @return TRUE on success, FALSE on error
*/
static int
priv_conn_check_initiate(agent_t *agent, candidate_check_pair_t *pair)
{
/* XXX: from ID-16 onwards, the checks should not be sent
* immediately, but be put into the "triggered queue",
* see "7.2.1.4 Triggered Checks"
*/
gettimeofday(&pair->next_tick,NULL);
add_microseconds_to_timeval(&pair->next_tick, agent->timer_ta * 1000);
pair->state = ICE_CHECK_IN_PROGRESS;
ICE_DEBUG("Agent %p : pair %p state IN_PROGRESS", agent, pair);
conn_check_send(agent, pair);
return ICE_OK;
}
/*
* Fills 'dest' with a username string for use in an outbound connectivity
* checks. No more than 'dest_len' characters (including terminating
* NULL) is ever written to the 'dest'.
*/
static
size_t priv_gen_username(agent_t *agent, uint32_t component_id,
char *remote, char *local, uint8_t *dest, uint32_t dest_len)
{
uint32_t len = 0;
size_t remote_len = strlen(remote);
size_t local_len = strlen(local);
if (remote_len > 0 && local_len > 0) {
if (agent->compatibility == ICE_COMPATIBILITY_RFC5245 &&
dest_len >= remote_len + local_len + 1) {
memcpy (dest, remote, remote_len);
len += remote_len;
memcpy (dest + len, ":", 1);
len++;
memcpy (dest + len, local, local_len);
len += local_len;
} else if ((agent->compatibility == ICE_COMPATIBILITY_WLM2009 ||
agent->compatibility == ICE_COMPATIBILITY_OC2007R2) &&
dest_len >= remote_len + local_len + 4 ) {
memcpy (dest, remote, remote_len);
len += remote_len;
memcpy (dest + len, ":", 1);
len++;
memcpy (dest + len, local, local_len);
len += local_len;
if (len % 4 != 0) {
memset (dest + len, 0, 4 - (len % 4));
len += 4 - (len % 4);
}
} else if (agent->compatibility == ICE_COMPATIBILITY_GOOGLE &&
dest_len >= remote_len + local_len) {
memcpy (dest, remote, remote_len);
len += remote_len;
memcpy (dest + len, local, local_len);
len += local_len;
} else if (agent->compatibility == ICE_COMPATIBILITY_MSN ||
agent->compatibility == ICE_COMPATIBILITY_OC2007) {
char component_str[10];
unsigned char *local_decoded = NULL;
unsigned char *remote_decoded = NULL;
size_t local_decoded_len;
size_t remote_decoded_len;
size_t total_len;
int padding;
snprintf(component_str, sizeof(component_str), "%d", component_id);
local_decoded = base64_decode((const unsigned char *)local,local_len,&local_decoded_len);
remote_decoded = base64_decode((const unsigned char *)remote,remote_len,&remote_decoded_len);
total_len = remote_decoded_len + local_decoded_len + 3 + 2*strlen (component_str);
padding = 4 - (total_len % 4);
if (dest_len >= total_len + padding) {
unsigned char pad_char[1] = {0};
int i;
memcpy (dest, remote_decoded, remote_decoded_len);
len += remote_decoded_len;
memcpy (dest + len, ":", 1);
len++;
memcpy (dest + len, component_str, strlen(component_str));
len += strlen (component_str);
memcpy (dest + len, ":", 1);
len++;
memcpy (dest + len, local_decoded, local_decoded_len);
len += local_decoded_len;
memcpy (dest + len, ":", 1);
len++;
memcpy (dest + len, component_str, strlen(component_str));;
len += strlen (component_str);
for (i = 0; i < padding; i++) {
memcpy (dest + len, pad_char, 1);
len++;
}
}
free(local_decoded);
free(remote_decoded);
}
}
return len;
}
/*
* Fills 'dest' with a username string for use in an outbound connectivity
* checks. No more than 'dest_len' characters (including terminating
* NULL) is ever written to the 'dest'.
*/
size_t priv_create_username(agent_t *agent, stream_t *stream,
uint32_t component_id, candidate_t *remote, candidate_t *local,
uint8_t *dest, uint32_t dest_len, int inbound)
{
ICE_DEBUG("FIXME: priv_create_username");
char *local_username = NULL;
char *remote_username = NULL;
if (remote && remote->username) {
remote_username = remote->username;
}
if (local && local->username) {
local_username = local->username;
}
if (stream) {
if (remote_username == NULL) {
remote_username = stream->remote_ufrag;
}
if (local_username == NULL) {
local_username = stream->local_ufrag;
}
}
if (local_username && remote_username) {
if (inbound) {
return priv_gen_username(agent, component_id,
local_username, remote_username, dest, dest_len);
} else {
return priv_gen_username(agent, component_id,
remote_username, local_username, dest, dest_len);
}
}
return 0;
}
void
print_list(candidate_check_pair_head_t *head) {
candidate_check_pair_t *p = NULL;
if (!head) return;
TAILQ_FOREACH(p,head,list) {
ICE_ERROR("pair info, priority=%lu,foundation=%s:%s (%p)",
p->priority,p->local->foundation,p->remote->foundation, p->remote);
}
return;
}
/*
* Enforces the upper limit for connectivity checks as described
* in ICE spec section 5.7.3 (ID-19). See also
* conn_check_add_for_candidate().
*/
static void
priv_limit_conn_check_list_size(candidate_check_pair_head_t *conncheck_list, uint32_t upper_limit)
{
uint32_t valid = 0;
uint32_t cancelled = 0;
candidate_check_pair_t *pair = NULL;
TAILQ_FOREACH(pair,conncheck_list,list) {
if (pair->state != ICE_CHECK_CANCELLED) {
valid++;
if (valid > upper_limit) {
pair->state = ICE_CHECK_CANCELLED;
cancelled++;
}
}
}
if (cancelled > 0) {
ICE_DEBUG("Agent : Pruned %d candidates. Conncheck list has %d elements"
" left. Maximum connchecks allowed : %d", cancelled, valid, upper_limit);
}
return;
}
static void
conn_check_insert(candidate_check_pair_head_t *head, candidate_check_pair_t *pair) {
candidate_check_pair_t *a = NULL;
if (!head || !pair)
return;
if (TAILQ_EMPTY(head)) {
TAILQ_INSERT_HEAD(head,pair,list);
return;
}
TAILQ_FOREACH(a,head,list) {
if (a->priority > pair->priority) {
TAILQ_INSERT_BEFORE(a,pair,list);
break;
}
}
return;
}
/*
* Creates a new connectivity check pair and adds it to
* the agent's list of checks.
*/
static void priv_add_new_check_pair (agent_t *agent, uint32_t stream_id, component_t *component,
candidate_t *local, candidate_t *remote, IceCheckState initial_state, int use_candidate)
{
stream_t *stream;
candidate_check_pair_t *pair;
if ( local == NULL || remote == NULL )
return;
stream = agent_find_stream(agent, stream_id);
if ( stream == NULL )
return;
pair = ICE_MALLOC(candidate_check_pair_t);
ICE_MEMZERO(pair,candidate_check_pair_t);
pair->agent = agent;
pair->stream_id = stream_id;
pair->component_id = component->id;;
pair->local = local;
pair->remote = remote;
if (remote->type == ICE_CANDIDATE_TYPE_PEER_REFLEXIVE)
pair->sockptr = (socket_t*) remote->sockptr;
else
pair->sockptr = (socket_t*) local->sockptr;
snprintf(pair->foundation, ICE_CANDIDATE_PAIR_MAX_FOUNDATION,
"%s:%s", local->foundation, remote->foundation);
pair->priority = agent_candidate_pair_priority(agent, local, remote);
pair->state = initial_state;
pair->nominated = use_candidate;
pair->controlling = agent->controlling_mode;
ICE_DEBUG("creating new pair, pair=%p, prio=%lu, rfoundation=%s(%p), state=%d",
pair, pair->priority, remote->foundation,remote, initial_state);
conn_check_insert(&stream->connchecks,pair);
//ICE_DEBUG("conncheck info, size=%u", list_size(&stream->connchecks.list));
//XXX: modify connchecks list inside TAILQ_FOREACH check function 'conn_check_remote_candidates_set'
ICE_DEBUG("added a new conncheck, agent=%p, pair=%p, foundation=%s, nominated=%u, stream_id=%u",
agent, pair, pair->foundation, pair->nominated, stream_id);
print_candidate(local,"local");
print_candidate(remote,"remote");
/* implement the hard upper limit for number of
checks (see sect 5.7.3 ICE ID-19): */
if (agent->compatibility == ICE_COMPATIBILITY_RFC5245) {
priv_limit_conn_check_list_size(&stream->connchecks, agent->max_conn_checks);
}
return;
}
IceCandidateTransport
conn_check_match_transport(IceCandidateTransport transport)
{
switch (transport) {
case ICE_CANDIDATE_TRANSPORT_TCP_ACTIVE:
return ICE_CANDIDATE_TRANSPORT_TCP_PASSIVE;
break;
case ICE_CANDIDATE_TRANSPORT_TCP_PASSIVE:
return ICE_CANDIDATE_TRANSPORT_TCP_ACTIVE;
break;
case ICE_CANDIDATE_TRANSPORT_TCP_SO:
case ICE_CANDIDATE_TRANSPORT_UDP:
default:
return transport;
break;
}
return transport;
}
static void priv_conn_check_add_for_candidate_pair_matched(agent_t *agent,
uint32_t stream_id, component_t *component, candidate_t *local,
candidate_t *remote, IceCheckState initial_state)
{
ICE_DEBUG("Adding check pair, agent=%p, local=%s, ltranpsort=%u, remote=%s(%p), rtransport=%u",
agent, local->foundation, local->transport, remote->foundation, remote, remote->transport);
priv_add_new_check_pair(agent, stream_id, component, local, remote, initial_state, 0);
if (component->state == ICE_COMPONENT_STATE_CONNECTED ||
component->state == ICE_COMPONENT_STATE_READY) {
agent_signal_component_state_change (agent,
stream_id, component->id, ICE_COMPONENT_STATE_CONNECTED);
} else {
agent_signal_component_state_change (agent,
stream_id, component->id, ICE_COMPONENT_STATE_CONNECTING);
}
return;
}
int
conn_check_add_for_candidate_pair(agent_t *agent,
uint32_t stream_id, component_t *component,
candidate_t *local, candidate_t *remote)
{
int ret = ICE_OK;
if ( local == NULL || remote == NULL )
return ICE_ERR;
/* note: do not create pairs where the local candidate is
* a srv-reflexive (ICE 5.7.3. "Pruning the pairs" ID-9) */
if ((agent->compatibility == ICE_COMPATIBILITY_RFC5245 ||
agent->compatibility == ICE_COMPATIBILITY_WLM2009 ||
agent->compatibility == ICE_COMPATIBILITY_OC2007R2) &&
local->type == ICE_CANDIDATE_TYPE_SERVER_REFLEXIVE) {
return ICE_ERR;
}
/* note: do not create pairs where local candidate has TCP passive transport
* (ice-tcp-13 6.2. "Forming the Check Lists") */
if (local->transport == ICE_CANDIDATE_TRANSPORT_TCP_PASSIVE) {
return ICE_ERR;
}
/* note: match pairs only if transport and address family are the same */
if (local->transport == conn_check_match_transport (remote->transport) &&
local->addr.s.addr.sa_family == remote->addr.s.addr.sa_family) {
priv_conn_check_add_for_candidate_pair_matched(agent, stream_id, component,
local, remote, ICE_CHECK_FROZEN);
ret = ICE_OK;
}
return ret;
}
/*
* Forms new candidate pairs by matching the new remote candidate
* 'remote_cand' with all existing local candidates of 'component'.
* Implements the logic described in ICE sect 5.7.1. "Forming Candidate
* Pairs" (ID-19).
*
* @param agent context
* @param component pointer to the component
* @param remote remote candidate to match with
*
* @return number of checks added, negative on fatal errors
*/
int
conn_check_add_for_candidate(agent_t *agent, uint32_t stream_id,
component_t *component, candidate_t *remote)
{
int added = 0;
int ret = 0;
candidate_t *local = NULL;
if ( remote == NULL || agent == NULL || component == NULL ) {
ICE_ERROR("null pointers, agent=%p,component=%p,remote=%p",agent,component,remote);
return ICE_ERR;
}
TAILQ_FOREACH(local,&component->local_candidates,list) {
ret = conn_check_add_for_candidate_pair(agent, stream_id, component, local, remote);
if (ret == ICE_OK) {
++added;
}
}
ICE_DEBUG("candidate info, added=%u, stream_id=%u",added,stream_id);
return added;
}
int
conn_check_add_for_local_candidate(agent_t *agent,
uint32_t stream_id, component_t *component, candidate_t *local)
{
int added = 0;
int ret = 0;
candidate_t *remote = NULL;
if (local == NULL)
return 0;
ICE_DEBUG("new candidate pairs, local=%p, added=%d",local, added);
TAILQ_FOREACH(remote,&component->remote_candidates,list) {
ICE_ERROR("remote candidate info, stream_id=%d, foundation=%s(%p)",
stream_id, remote->foundation, remote);
ret = conn_check_add_for_candidate_pair(agent, stream_id, component, local, remote);
ICE_DEBUG("check new pair, ret=%d",ret);
if (ret == ICE_OK) {
++added;
}
}
ICE_DEBUG("new candidate pairs, added=%d",added);
return added;
}
/*
* Timer callback that handles initiating and managing connectivity
* checks (paced by the Ta timer).
*
* This function is designed for the g_timeout_add() interface.
*
* @return will return FALSE when no more pending timers.
*/
static int
priv_conn_keepalive_tick_unlocked(agent_t *agent)
{
int errors = 0;
int ret = ICE_FALSE;
size_t buf_len = 0;
stream_t *stream = NULL;
//ICE_ERROR("conn keepalive tick, keepalive_conncheck=%u",agent->keepalive_conncheck);
// case 1: session established and media flowing
// (ref ICE sect 10 "Keepalives" ID-19)
TAILQ_FOREACH(stream,&agent->streams,list) {
component_t *component = NULL;
TAILQ_FOREACH(component,&stream->components,list) {
if (component->selected_pair.local != NULL) {
candidate_pair_t *p = &component->selected_pair;
// Disable keepalive checks on TCP candidates
ICE_DEBUG("pair info, pair=%p, local_transport=%u", p, p->local->transport);
if (p->local->transport != ICE_CANDIDATE_TRANSPORT_UDP)
continue;
if (agent->compatibility == ICE_COMPATIBILITY_GOOGLE ||
agent->keepalive_conncheck) {
uint32_t priority;
uint8_t uname[ICE_STREAM_MAX_UNAME];
size_t uname_len =
priv_create_username (agent, agent_find_stream (agent, stream->id),
component->id, p->remote, p->local, uname, sizeof (uname),
ICE_FALSE);
uint8_t *password = NULL;
size_t password_len = priv_get_password (agent,
agent_find_stream (agent, stream->id), p->remote, &password);
priority = peer_reflexive_candidate_priority (agent, p->local);
{
char tmpbuf[INET6_ADDRSTRLEN];
address_to_string (&p->remote->addr, tmpbuf);
ICE_ERROR("Agent %p : Keepalive STUN-CC REQ to '%s:%u', "
"socket=%u (c-id:%u), username='%.*s' (%lu), "
"password='%.*s' (%lu), priority=%u.", agent,
tmpbuf, address_get_port (&p->remote->addr),
((socket_t *)p->local->sockptr)->fd,
component->id, (int) uname_len, uname, uname_len,
(int) password_len, password, password_len, priority);
}
if (uname_len > 0) {
buf_len = stun_usage_ice_conncheck_create (&component->stun_agent,
&p->keepalive.stun_message, p->keepalive.stun_buffer,
sizeof(p->keepalive.stun_buffer),
uname, uname_len, password, password_len,
agent->controlling_mode, agent->controlling_mode, priority,
agent->tie_breaker,
NULL,
agent_to_ice_compatibility (agent));
ICE_ERROR("Agent %p: conncheck created %zd - %p",
agent, buf_len, p->keepalive.stun_message.buffer);
if (buf_len > 0) {
stun_timer_start (&p->keepalive.timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
agent->media_after_tick = ICE_FALSE;
// send the conncheck
ICE_ERROR("conncheck keepalive");
agent_socket_send((socket_t*)p->local->sockptr, &p->remote->addr,
(char *)p->keepalive.stun_buffer, buf_len);
p->keepalive.stream_id = stream->id;
p->keepalive.component_id = component->id;
p->keepalive.agent = agent;
agent_timeout_add_with_context (p->keepalive.agent,
NULL/*&p->keepalive.tick_source*/, "Pair keepalive",
stun_timer_remainder (&p->keepalive.timer),
NULL/*priv_conn_keepalive_retransmissions_tick*/, p);
} else {
++errors;
}
}
} else {
buf_len = stun_usage_bind_keepalive (&component->stun_agent,
&p->keepalive.stun_message, p->keepalive.stun_buffer,
sizeof(p->keepalive.stun_buffer));
if (buf_len > 0) {
agent_socket_send ((socket_t*)p->local->sockptr, &p->remote->addr,
(char *)p->keepalive.stun_buffer, buf_len);
ICE_DEBUG("Agent %p : stun_bind_keepalive for pair %p res %d.",
agent, p, (int) buf_len);
} else {
++errors;
}
}
}
}
}
// case 2: connectivity establishment ongoing
// (ref ICE sect 4.1.1.4 "Keeping Candidates Alive" ID-19)
TAILQ_FOREACH(stream,&agent->streams,list) {
component_t *component = NULL;
TAILQ_FOREACH(component,&stream->components,list) {
if (component->state < ICE_COMPONENT_STATE_READY &&
agent->stun_server_ip) {
address_t stun_server;
if (address_set_from_string (&stun_server, agent->stun_server_ip)) {
StunAgent stun_agent;
uint8_t stun_buffer[STUN_MAX_MESSAGE_SIZE_IPV6];
StunMessage stun_message;
size_t buffer_len = 0;
candidate_t *candidate = NULL;
address_set_port (&stun_server, agent->stun_server_port);
// FIXME: This will cause the stun response to arrive on the socket
// but the stun agent will not be able to parse it due to an invalid
// stun message since RFC3489 will not be compatible, and the response
// will be forwarded to the application as user data
stun_agent_init (&stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
STUN_COMPATIBILITY_RFC3489, 0);
buffer_len = stun_usage_bind_create (&stun_agent,
&stun_message, stun_buffer, sizeof(stun_buffer));
TAILQ_FOREACH(candidate,&component->local_candidates,list) {
if (candidate->type == ICE_CANDIDATE_TYPE_HOST &&
candidate->transport == ICE_CANDIDATE_TRANSPORT_UDP) {
// send the conncheck
ICE_DEBUG("Agent %p : resending STUN on %s to keep the "
"candidate alive.", agent, candidate->foundation);
agent_socket_send ((socket_t*)candidate->sockptr, &stun_server,
(char *)stun_buffer, buffer_len);
}
}
}
}
}
}
if (errors) {
ICE_ERROR("Agent %p : stopping keepalive timer", agent);
goto done;
}
ret = ICE_TRUE;
done:
return ret;
}
/*
* Changes the selected pair for the component if 'pair' is nominated
* and has higher priority than the currently selected pair. See
* ICE sect 11.1.1. "Procedures for Full Implementations" (ID-19).
*/
static int
priv_update_selected_pair(agent_t *agent, component_t *component, candidate_check_pair_t *pair)
{
candidate_pair_t cpair;
if ( agent == NULL || component == NULL )
return ICE_ERR;
if (pair->priority > component->selected_pair.priority
&& component_find_pair(component, agent, pair->local->foundation,
pair->remote->foundation, &cpair) == ICE_OK ) {
ICE_ERROR("changing SELECTED PAIR for component %u: %s:%s ,prio:%lu",
component->id, pair->local->foundation, pair->remote->foundation, pair->priority);
print_candidate(cpair.local,"local peer");
print_candidate(cpair.remote,"remote peer");
component_update_selected_pair(component, &cpair);
priv_conn_keepalive_tick_unlocked(agent);
agent_signal_new_selected_pair(agent, pair->stream_id, component->id,
pair->local, pair->remote);
}
return ICE_OK;
}
/*
* Implemented the pruning steps described in ICE sect 8.1.2
* "Updating States" (ID-19) after a pair has been nominated.
*
* @see priv_update_check_list_state_failed_components()
*/
static uint32_t
priv_prune_pending_checks(stream_t *stream, uint32_t component_id)
{
uint64_t highest_nominated_priority = 0;
uint32_t in_progress = 0;
candidate_check_pair_t *p = NULL;
ICE_DEBUG("Agent XXX: Finding highest priority, component=%d", component_id);
TAILQ_FOREACH(p,&stream->connchecks,list) {
if ( p->component_id == component_id && p->nominated == ICE_TRUE &&
(p->state == ICE_CHECK_SUCCEEDED || p->state == ICE_CHECK_DISCOVERED) ){
ICE_DEBUG("verify priority, priority=%llu", p->priority);
if (p->priority > highest_nominated_priority) {
highest_nominated_priority = p->priority;
}
}
}
ICE_DEBUG("Agent XXX: Pruning pending checks, highest_nominated_priority=%lu", highest_nominated_priority);
/* step: cancel all FROZEN and WAITING pairs for the component */
TAILQ_FOREACH(p,&stream->connchecks,list) {
if (p->component_id == component_id) {
if (p->state == ICE_CHECK_FROZEN || p->state == ICE_CHECK_WAITING) {
p->state = ICE_CHECK_CANCELLED;
ICE_DEBUG("Agent XXX : pair %p state CANCELED", p);
}
/* note: a SHOULD level req. in ICE 8.1.2. "Updating States" (ID-19) */
if (p->state == ICE_CHECK_IN_PROGRESS) {
if (highest_nominated_priority != 0 &&
p->priority < highest_nominated_priority) {
p->stun_message.buffer = NULL;
p->stun_message.buffer_len = 0;
p->state = ICE_CHECK_CANCELLED;
ICE_DEBUG("Agent XXX : pair %p state CANCELED", p);
} else {
/* We must keep the higher priority pairs running because if a udp
* packet was lost, we might end up using a bad candidate */
ICE_DEBUG("in-progress pair with higher priority, priority=%lu, nominated_pri=%lu"
, p->priority, highest_nominated_priority);
in_progress++;
}
}
}
}
ICE_DEBUG("Agent XXX: Pruning pending checks, in_progress=%lu", in_progress);
return in_progress;
}
/*
* Updates the check list state for a stream component.
*
* Implements the algorithm described in ICE sect 8.1.2
* "Updating States" (ID-19) as it applies to checks of
* a certain component. If there are any nominated pairs,
* ICE processing may be concluded, and component state is
* changed to READY.
*
* Sends a component state changesignal via 'agent'.
*/
static void
priv_update_check_list_state_for_ready(agent_t *agent, stream_t *stream, component_t *component)
{
candidate_check_pair_t *p = NULL;
int succeeded = 0, nominated = 0;
if ( component == NULL )
return;
// step: search for at least one nominated pair
TAILQ_FOREACH(p,&stream->connchecks,list) {
ICE_DEBUG("update check list, pair=%p, nominated=%u, state=%u, p-cid=%u, cid=%u, prio=%llu",
p, p->nominated, p->state, p->component_id, component->id, p->priority);
if (p->component_id == component->id) {
if (p->state == ICE_CHECK_SUCCEEDED ||
p->state == ICE_CHECK_DISCOVERED) {
++succeeded;
if (p->nominated == ICE_TRUE) {
++nominated;
}
}
}
}
ICE_DEBUG("check ready state, nominated=%u",nominated);
if (nominated > 0) {
// Only go to READY if no checks are left in progress. If there are
// any that are kept, then this function will be called again when the
// conncheck tick timer finishes them all
if (priv_prune_pending_checks(stream, component->id) == 0) {
agent_signal_component_state_change (agent, stream->id,
component->id, ICE_COMPONENT_STATE_READY);
}
}
ICE_DEBUG("conncheck list status, agent=%p, nominated=%u, succeeded=%u, cid=%u",
agent, nominated, succeeded, component->id);
return;
}
/*
* The remote party has signalled that the candidate pair
* described by 'component' and 'remotecand' is nominated
* for use.
*/
static void
priv_mark_pair_nominated(agent_t *agent, stream_t *stream,
component_t *component, candidate_t *remotecand)
{
candidate_check_pair_t *pair = NULL;
if (agent == NULL || stream == NULL || component == NULL )
return;
/* step: search for at least one nominated pair */
TAILQ_FOREACH(pair,&stream->connchecks,list) {
/* XXX: hmm, how to figure out to which local candidate the
* check was sent to? let's mark all matching pairs
* as nominated instead */
ICE_DEBUG("checking pair nominated, foundation=%s,remote=%p,remotecand=%p, prio=%lu",
pair->foundation, pair->remote, remotecand, pair->priority);
//ICE_ERROR("remote cand info, foundation=%s(%p)", pair->remote->foundation, pair->remote);
if (pair->remote == remotecand) {
ICE_DEBUG("marking pair as nominated, agent=%p, pair=%p, foundation=%s, state=%u",
agent, pair, pair->foundation, pair->state);
pair->nominated = 1;
if (pair->state == ICE_CHECK_SUCCEEDED ||
pair->state == ICE_CHECK_DISCOVERED) {
priv_update_selected_pair(agent, component, pair);
}
priv_update_check_list_state_for_ready(agent, stream, component);
}
}
return;
}
/*
* Schedules a triggered check after a successfully inbound
* connectivity check. Implements ICE sect 7.2.1.4 "Triggered Checks" (ID-19).
*