-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathTurnConnection.c
1228 lines (982 loc) · 53.2 KB
/
TurnConnection.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
/**
* Kinesis Video TurnConnection
*/
#define LOG_CLASS "TurnConnection"
#include "../Include_i.h"
extern StateMachineState TURN_CONNECTION_STATE_MACHINE_STATES[];
extern UINT32 TURN_CONNECTION_STATE_MACHINE_STATE_COUNT;
STATUS createTurnConnection(PIceServer pTurnServer, TIMER_QUEUE_HANDLE timerQueueHandle, TURN_CONNECTION_DATA_TRANSFER_MODE dataTransferMode,
KVS_SOCKET_PROTOCOL protocol, PTurnConnectionCallbacks pTurnConnectionCallbacks, PSocketConnection pTurnSocket,
PConnectionListener pConnectionListener, PTurnConnection* ppTurnConnection)
{
UNUSED_PARAM(dataTransferMode);
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PTurnConnection pTurnConnection = NULL;
CHAR turnStateMachineName[MAX_STATE_MACHINE_NAME_LENGTH];
CHK(pTurnServer != NULL && ppTurnConnection != NULL && pTurnSocket != NULL, STATUS_NULL_ARG);
CHK(IS_VALID_TIMER_QUEUE_HANDLE(timerQueueHandle), STATUS_INVALID_ARG);
CHK(pTurnServer->isTurn && !IS_EMPTY_STRING(pTurnServer->url) && !IS_EMPTY_STRING(pTurnServer->credential) &&
!IS_EMPTY_STRING(pTurnServer->username),
STATUS_INVALID_ARG);
pTurnConnection = (PTurnConnection) MEMCALLOC(
1, SIZEOF(TurnConnection) + DEFAULT_TURN_MESSAGE_RECV_CHANNEL_DATA_BUFFER_LEN * 2 + DEFAULT_TURN_MESSAGE_SEND_CHANNEL_DATA_BUFFER_LEN);
CHK(pTurnConnection != NULL, STATUS_NOT_ENOUGH_MEMORY);
pTurnConnection->lock = MUTEX_CREATE(TRUE);
pTurnConnection->sendLock = MUTEX_CREATE(FALSE);
pTurnConnection->freeAllocationCvar = CVAR_CREATE();
pTurnConnection->timerQueueHandle = timerQueueHandle;
pTurnConnection->turnServer = *pTurnServer;
pTurnConnection->state = TURN_STATE_NEW;
pTurnConnection->stateTimeoutTime = INVALID_TIMESTAMP_VALUE;
pTurnConnection->errorStatus = STATUS_SUCCESS;
pTurnConnection->timerCallbackId = MAX_UINT32;
pTurnConnection->pTurnPacket = NULL;
pTurnConnection->pTurnChannelBindPacket = NULL;
pTurnConnection->pConnectionListener = pConnectionListener;
pTurnConnection->dataTransferMode =
TURN_CONNECTION_DATA_TRANSFER_MODE_DATA_CHANNEL; // only TURN_CONNECTION_DATA_TRANSFER_MODE_DATA_CHANNEL for now
pTurnConnection->protocol = protocol;
pTurnConnection->relayAddressReported = FALSE;
pTurnConnection->pControlChannel = pTurnSocket;
ATOMIC_STORE_BOOL(&pTurnConnection->stopTurnConnection, FALSE);
ATOMIC_STORE_BOOL(&pTurnConnection->hasAllocation, FALSE);
ATOMIC_STORE_BOOL(&pTurnConnection->shutdownComplete, FALSE);
if (pTurnConnectionCallbacks != NULL) {
pTurnConnection->turnConnectionCallbacks = *pTurnConnectionCallbacks;
}
pTurnConnection->recvDataBufferSize = DEFAULT_TURN_MESSAGE_RECV_CHANNEL_DATA_BUFFER_LEN;
pTurnConnection->dataBufferSize = DEFAULT_TURN_MESSAGE_SEND_CHANNEL_DATA_BUFFER_LEN;
pTurnConnection->sendDataBuffer = (PBYTE) (pTurnConnection + 1);
pTurnConnection->recvDataBuffer = pTurnConnection->sendDataBuffer + pTurnConnection->dataBufferSize;
pTurnConnection->completeChannelDataBuffer =
pTurnConnection->sendDataBuffer + pTurnConnection->dataBufferSize + pTurnConnection->recvDataBufferSize;
pTurnConnection->currRecvDataLen = 0;
pTurnConnection->allocationExpirationTime = INVALID_TIMESTAMP_VALUE;
pTurnConnection->nextAllocationRefreshTime = 0;
pTurnConnection->currentTimerCallingPeriod = DEFAULT_TURN_TIMER_INTERVAL_BEFORE_READY;
SNPRINTF(turnStateMachineName, MAX_STATE_MACHINE_NAME_LENGTH, "%s-%p", TURN_STATE_MACHINE_NAME, (PVOID) pTurnConnection);
CHK_STATUS(createStateMachineWithName(TURN_CONNECTION_STATE_MACHINE_STATES, TURN_CONNECTION_STATE_MACHINE_STATE_COUNT, (UINT64) pTurnConnection,
turnConnectionGetTime, (UINT64) pTurnConnection, turnStateMachineName, &pTurnConnection->pStateMachine));
CleanUp:
CHK_LOG_ERR(retStatus);
if (STATUS_FAILED(retStatus) && pTurnConnection != NULL) {
freeTurnConnection(&pTurnConnection);
}
if (ppTurnConnection != NULL) {
*ppTurnConnection = pTurnConnection;
}
LEAVES();
return retStatus;
}
UINT64 turnConnectionGetTime(UINT64 customData)
{
UNUSED_PARAM(customData);
return GETTIME();
}
STATUS freeTurnConnection(PTurnConnection* ppTurnConnection)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PTurnConnection pTurnConnection = NULL;
PTurnPeer pTurnPeer = NULL;
SIZE_T timerCallbackId;
UINT32 i;
CHK(ppTurnConnection != NULL, STATUS_NULL_ARG);
// free is idempotent
CHK(*ppTurnConnection != NULL, retStatus);
pTurnConnection = *ppTurnConnection;
// Ensure we are not freeing everything without cancelling the timer
timerCallbackId = ATOMIC_EXCHANGE(&pTurnConnection->timerCallbackId, MAX_UINT32);
if (timerCallbackId != MAX_UINT32) {
CHK_LOG_ERR(timerQueueCancelTimer(pTurnConnection->timerQueueHandle, (UINT32) timerCallbackId, (UINT64) pTurnConnection));
}
// shutdown control channel
if (pTurnConnection->pControlChannel) {
CHK_LOG_ERR(connectionListenerRemoveConnection(pTurnConnection->pConnectionListener, pTurnConnection->pControlChannel));
CHK_LOG_ERR(freeSocketConnection(&pTurnConnection->pControlChannel));
}
// free transactionId store for each turn peer
for (i = 0; i < pTurnConnection->turnPeerCount; ++i) {
pTurnPeer = &pTurnConnection->turnPeerList[i];
freeTransactionIdStore(&pTurnPeer->pTransactionIdStore);
}
if (IS_VALID_MUTEX_VALUE(pTurnConnection->lock)) {
/* in case some thread is in the middle of a turn api call. */
MUTEX_LOCK(pTurnConnection->lock);
MUTEX_UNLOCK(pTurnConnection->lock);
MUTEX_FREE(pTurnConnection->lock);
}
if (IS_VALID_MUTEX_VALUE(pTurnConnection->sendLock)) {
/* in case some thread is in the middle of a turn api call. */
MUTEX_LOCK(pTurnConnection->sendLock);
MUTEX_UNLOCK(pTurnConnection->sendLock);
MUTEX_FREE(pTurnConnection->sendLock);
}
if (IS_VALID_CVAR_VALUE(pTurnConnection->freeAllocationCvar)) {
CVAR_FREE(pTurnConnection->freeAllocationCvar);
}
freeStateMachine(pTurnConnection->pStateMachine);
turnConnectionFreePreAllocatedPackets(pTurnConnection);
MEMFREE(pTurnConnection);
*ppTurnConnection = NULL;
CleanUp:
CHK_LOG_ERR(retStatus);
LEAVES();
return retStatus;
}
STATUS turnConnectionIncomingDataHandler(PTurnConnection pTurnConnection, PBYTE pBuffer, UINT32 bufferLen, PKvsIpAddress pSrc, PKvsIpAddress pDest,
PTurnChannelData channelDataList, PUINT32 pChannelDataCount)
{
ENTERS();
UNUSED_PARAM(pSrc);
UNUSED_PARAM(pDest);
STATUS retStatus = STATUS_SUCCESS;
UINT32 remainingDataSize = 0, processedDataLen, channelDataCount = 0, totalChannelDataCount = 0, channelDataListSize;
PBYTE pCurrent = NULL;
CHK(pTurnConnection != NULL && channelDataList != NULL && pChannelDataCount != NULL, STATUS_NULL_ARG);
CHK_WARN(bufferLen > 0 && pBuffer != NULL, retStatus, "Got empty buffer");
/* initially pChannelDataCount contains size of channelDataList */
channelDataListSize = *pChannelDataCount;
remainingDataSize = bufferLen;
pCurrent = pBuffer;
while (remainingDataSize > 0 && totalChannelDataCount < channelDataListSize) {
processedDataLen = 0;
channelDataCount = 0;
if (IS_STUN_PACKET(pCurrent)) {
processedDataLen = GET_STUN_PACKET_SIZE(pCurrent) + STUN_HEADER_LEN; /* size of entire STUN packet */
if (STUN_PACKET_IS_TYPE_ERROR(pCurrent)) {
CHK_STATUS(turnConnectionHandleStunError(pTurnConnection, pCurrent, processedDataLen));
} else {
CHK_STATUS(turnConnectionHandleStun(pTurnConnection, pCurrent, processedDataLen));
}
checkTurnConnectionStateMachine(pTurnConnection);
} else {
/* must be channel data if not stun */
CHK_STATUS(turnConnectionHandleChannelData(pTurnConnection, pCurrent, remainingDataSize, &channelDataList[totalChannelDataCount],
&channelDataCount, &processedDataLen));
}
CHK(remainingDataSize >= processedDataLen, STATUS_INVALID_ARG_LEN);
pCurrent += processedDataLen;
remainingDataSize -= processedDataLen;
/* channelDataCount will be either 1 or 0 */
totalChannelDataCount += channelDataCount;
}
*pChannelDataCount = totalChannelDataCount;
CleanUp:
CHK_LOG_ERR(retStatus);
LEAVES();
return retStatus;
}
STATUS turnConnectionHandleStun(PTurnConnection pTurnConnection, PBYTE pBuffer, UINT32 bufferLen)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
UINT16 stunPacketType = 0;
PStunAttributeHeader pStunAttr = NULL;
PStunAttributeAddress pStunAttributeAddress = NULL;
PStunAttributeLifetime pStunAttributeLifetime = NULL;
PStunPacket pStunPacket = NULL;
CHAR profileDebugStr[MAX_TURN_PROFILE_LOG_DESC_LEN];
CHAR ipAddrStr[KVS_IP_ADDRESS_STRING_BUFFER_LEN];
BOOL locked = FALSE;
ATOMIC_BOOL hasAllocation = FALSE;
PTurnPeer pTurnPeer = NULL;
UINT64 currentTime;
UINT32 i;
CHK(pTurnConnection != NULL, STATUS_NULL_ARG);
CHK(pBuffer != NULL && bufferLen > 0, STATUS_INVALID_ARG);
CHK(IS_STUN_PACKET(pBuffer) && !STUN_PACKET_IS_TYPE_ERROR(pBuffer), retStatus);
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
currentTime = GETTIME();
// only handling STUN response
stunPacketType = (UINT16) getInt16(*((PUINT16) pBuffer));
switch (stunPacketType) {
case STUN_PACKET_TYPE_ALLOCATE_SUCCESS_RESPONSE:
/* If shutdown has been initiated, ignore the allocation response */
CHK(!ATOMIC_LOAD(&pTurnConnection->stopTurnConnection), retStatus);
CHK_STATUS(deserializeStunPacket(pBuffer, bufferLen, pTurnConnection->longTermKey, KVS_MD5_DIGEST_LENGTH, &pStunPacket));
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_XOR_RELAYED_ADDRESS, &pStunAttr));
CHK_WARN(pStunAttr != NULL, retStatus, "No relay address attribute found in TURN allocate response. Dropping Packet");
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_LIFETIME, (PStunAttributeHeader*) &pStunAttributeLifetime));
CHK_WARN(pStunAttributeLifetime != NULL, retStatus, "Missing lifetime in Allocation response. Dropping Packet");
// convert lifetime to 100ns and store it
pTurnConnection->allocationExpirationTime = (pStunAttributeLifetime->lifetime * HUNDREDS_OF_NANOS_IN_A_SECOND) + currentTime;
pStunAttributeAddress = (PStunAttributeAddress) pStunAttr;
pTurnConnection->relayAddress = pStunAttributeAddress->address;
ATOMIC_STORE_BOOL(&pTurnConnection->hasAllocation, TRUE);
getIpAddrStr(&pTurnConnection->relayAddress, ipAddrStr, ARRAY_SIZE(ipAddrStr));
SNPRINTF(profileDebugStr, MAX_TURN_PROFILE_LOG_DESC_LEN, "%p - %s:%d - %s", (PVOID) pTurnConnection, ipAddrStr,
pTurnConnection->relayAddress.port, "TURN allocation");
DLOGD("[%p - %s:%d] TURN Allocation succeeded. Life time: %u seconds. Allocation expiration epoch %" PRIu64, pTurnConnection, ipAddrStr,
pTurnConnection->relayAddress.port, pStunAttributeLifetime->lifetime,
pTurnConnection->allocationExpirationTime / DEFAULT_TIME_UNIT_IN_NANOS);
PROFILE_WITH_START_TIME_OBJ(pTurnConnection->turnProfileDiagnostics.createAllocationStartTime,
pTurnConnection->turnProfileDiagnostics.createAllocationTime, profileDebugStr);
if (!pTurnConnection->relayAddressReported && pTurnConnection->turnConnectionCallbacks.relayAddressAvailableFn != NULL) {
pTurnConnection->relayAddressReported = TRUE;
// release lock early and report relay candidate
MUTEX_UNLOCK(pTurnConnection->lock);
locked = FALSE;
pTurnConnection->turnConnectionCallbacks.relayAddressAvailableFn(pTurnConnection->turnConnectionCallbacks.customData,
&pTurnConnection->relayAddress, pTurnConnection->pControlChannel);
}
break;
case STUN_PACKET_TYPE_REFRESH_SUCCESS_RESPONSE:
CHK_STATUS(deserializeStunPacket(pBuffer, bufferLen, pTurnConnection->longTermKey, KVS_MD5_DIGEST_LENGTH, &pStunPacket));
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_LIFETIME, (PStunAttributeHeader*) &pStunAttributeLifetime));
CHK_WARN(pStunAttributeLifetime != NULL, retStatus, "No lifetime attribute found in TURN refresh response. Dropping Packet");
if (pStunAttributeLifetime->lifetime == 0) {
hasAllocation = ATOMIC_EXCHANGE_BOOL(&pTurnConnection->hasAllocation, FALSE);
CHK(hasAllocation, retStatus);
DLOGD("TURN Allocation freed.");
CVAR_SIGNAL(pTurnConnection->freeAllocationCvar);
} else {
// convert lifetime to 100ns and store it
pTurnConnection->allocationExpirationTime = (pStunAttributeLifetime->lifetime * HUNDREDS_OF_NANOS_IN_A_SECOND) + currentTime;
DLOGD("Refreshed TURN allocation lifetime is %u seconds. Allocation expiration epoch %" PRIu64, pStunAttributeLifetime->lifetime,
pTurnConnection->allocationExpirationTime / DEFAULT_TIME_UNIT_IN_NANOS);
}
break;
case STUN_PACKET_TYPE_CREATE_PERMISSION_SUCCESS_RESPONSE:
for (i = 0; i < pTurnConnection->turnPeerCount; ++i) {
pTurnPeer = &pTurnConnection->turnPeerList[i];
if (transactionIdStoreHasId(pTurnPeer->pTransactionIdStore, pBuffer + STUN_PACKET_TRANSACTION_ID_OFFSET)) {
if (pTurnPeer->connectionState == TURN_PEER_CONN_STATE_CREATE_PERMISSION) {
pTurnPeer->connectionState = TURN_PEER_CONN_STATE_BIND_CHANNEL;
CHK_STATUS(getIpAddrStr(&pTurnPeer->address, ipAddrStr, ARRAY_SIZE(ipAddrStr)));
DLOGD("[%p] Create permission succeeded for peer %s:%d", pTurnConnection, ipAddrStr, pTurnPeer->address.port);
if (pTurnPeer->firstTimeCreatePermResponse) {
pTurnPeer->firstTimeCreatePermResponse = FALSE;
SNPRINTF(profileDebugStr, MAX_TURN_PROFILE_LOG_DESC_LEN, "%p - %s:%d - %s", (PVOID) pTurnConnection, ipAddrStr,
pTurnPeer->address.port, "TURN create permission");
PROFILE_WITH_START_TIME_OBJ(pTurnPeer->createPermissionStartTime, pTurnPeer->createPermissionTime, profileDebugStr);
}
}
pTurnPeer->permissionExpirationTime = TURN_PERMISSION_LIFETIME + currentTime;
}
}
break;
case STUN_PACKET_TYPE_CHANNEL_BIND_SUCCESS_RESPONSE:
for (i = 0; i < pTurnConnection->turnPeerCount; ++i) {
pTurnPeer = &pTurnConnection->turnPeerList[i];
if (pTurnPeer->connectionState == TURN_PEER_CONN_STATE_BIND_CHANNEL &&
transactionIdStoreHasId(pTurnPeer->pTransactionIdStore, pBuffer + STUN_PACKET_TRANSACTION_ID_OFFSET)) {
// pTurnPeer->ready means this peer is ready to receive data. pTurnPeer->connectionState could
// change after reaching TURN_PEER_CONN_STATE_READY due to refreshing permission and channel.
if (!pTurnPeer->ready) {
pTurnPeer->ready = TRUE;
}
pTurnPeer->connectionState = TURN_PEER_CONN_STATE_READY;
CHK_STATUS(getIpAddrStr(&pTurnPeer->address, ipAddrStr, ARRAY_SIZE(ipAddrStr)));
DLOGD("[%p] Channel bind succeeded with peer %s, port: %d, channel number %u", pTurnConnection, ipAddrStr,
pTurnPeer->address.port, pTurnPeer->channelNumber);
if (pTurnPeer->firstTimeBindChannelResponse) {
pTurnPeer->firstTimeBindChannelResponse = FALSE;
SNPRINTF(profileDebugStr, MAX_TURN_PROFILE_LOG_DESC_LEN, "%p - %s:%d:%u - %s", (PVOID) pTurnConnection, ipAddrStr,
pTurnPeer->address.port, pTurnPeer->channelNumber, "TURN bind channel");
PROFILE_WITH_START_TIME_OBJ(pTurnPeer->bindChannelStartTime, pTurnPeer->bindChannelTime, profileDebugStr);
}
break;
}
}
break;
case STUN_PACKET_TYPE_DATA_INDICATION:
DLOGD("Received data indication");
// no-ops for now, turn server has to use data channel if it is established. client is free to use either.
break;
default:
DLOGD("Drop unsupported TURN message with type 0x%02x", stunPacketType);
break;
}
CleanUp:
CHK_LOG_ERR(retStatus);
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}
if (pStunPacket != NULL) {
freeStunPacket(&pStunPacket);
}
LEAVES();
return retStatus;
}
STATUS turnConnectionHandleStunError(PTurnConnection pTurnConnection, PBYTE pBuffer, UINT32 bufferLen)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
UINT16 stunPacketType = 0;
PStunAttributeHeader pStunAttr = NULL;
PStunAttributeErrorCode pStunAttributeErrorCode = NULL;
PStunAttributeNonce pStunAttributeNonce = NULL;
PStunAttributeRealm pStunAttributeRealm = NULL;
PStunPacket pStunPacket = NULL;
BOOL locked = FALSE, iterate = TRUE;
PTurnPeer pTurnPeer = NULL;
CHAR profileDebugStr[MAX_TURN_PROFILE_LOG_DESC_LEN];
UINT32 i;
CHK(pTurnConnection != NULL, STATUS_NULL_ARG);
CHK(pBuffer != NULL && bufferLen > 0, STATUS_INVALID_ARG);
CHK(STUN_PACKET_IS_TYPE_ERROR(pBuffer), retStatus);
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
stunPacketType = (UINT16) getInt16(*((PUINT16) pBuffer));
/* we could get errors like expired nonce after sending the deallocation packet. The allocate would have been
* deallocated even if the response is error response, and if we try to deallocate again we would get invalid
* allocation error. Therefore if we get an error after we've sent the deallocation packet, consider the
* deallocation process finished. */
if (pTurnConnection->deallocatePacketSent) {
ATOMIC_STORE_BOOL(&pTurnConnection->hasAllocation, FALSE);
CHK(FALSE, retStatus);
}
if (pTurnConnection->credentialObtained) {
retStatus = deserializeStunPacket(pBuffer, bufferLen, pTurnConnection->longTermKey, KVS_MD5_DIGEST_LENGTH, &pStunPacket);
}
/* if deserializing with password didnt work, try deserialize without password again */
if (!pTurnConnection->credentialObtained || STATUS_FAILED(retStatus)) {
CHK_STATUS(deserializeStunPacket(pBuffer, bufferLen, NULL, 0, &pStunPacket));
retStatus = STATUS_SUCCESS;
}
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_ERROR_CODE, &pStunAttr));
CHK_WARN(pStunAttr != NULL, retStatus, "No error code attribute found in Stun Error response. Dropping Packet");
pStunAttributeErrorCode = (PStunAttributeErrorCode) pStunAttr;
switch (pStunAttributeErrorCode->errorCode) {
case STUN_ERROR_UNAUTHORIZED:
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_NONCE, &pStunAttr));
CHK_WARN(pStunAttr != NULL, retStatus, "No Nonce attribute found in Allocate Error response. Dropping Packet");
pStunAttributeNonce = (PStunAttributeNonce) pStunAttr;
CHK_WARN(pStunAttributeNonce->attribute.length <= STUN_MAX_NONCE_LEN, retStatus,
"Invalid Nonce found in Allocate Error response. Dropping Packet");
pTurnConnection->nonceLen = pStunAttributeNonce->attribute.length;
MEMCPY(pTurnConnection->turnNonce, pStunAttributeNonce->nonce, pTurnConnection->nonceLen);
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_REALM, &pStunAttr));
CHK_WARN(pStunAttr != NULL, retStatus, "No Realm attribute found in Allocate Error response. Dropping Packet");
pStunAttributeRealm = (PStunAttributeRealm) pStunAttr;
CHK_WARN(pStunAttributeRealm->attribute.length <= STUN_MAX_REALM_LEN, retStatus,
"Invalid Realm found in Allocate Error response. Dropping Packet");
// pStunAttributeRealm->attribute.length does not include null terminator and pStunAttributeRealm->realm is not null terminated
STRNCPY(pTurnConnection->turnRealm, pStunAttributeRealm->realm, pStunAttributeRealm->attribute.length);
pTurnConnection->turnRealm[pStunAttributeRealm->attribute.length] = '\0';
pTurnConnection->credentialObtained = TRUE;
SNPRINTF(profileDebugStr, MAX_TURN_PROFILE_LOG_DESC_LEN, "%p - %s", (PVOID) pTurnConnection, "TURN Get Credentials");
PROFILE_WITH_START_TIME_OBJ(pTurnConnection->turnProfileDiagnostics.getCredentialsStartTime,
pTurnConnection->turnProfileDiagnostics.getCredentialsTime, profileDebugStr);
CHK_STATUS(turnConnectionUpdateNonce(pTurnConnection));
break;
case STUN_ERROR_STALE_NONCE:
DLOGD("Updating stale nonce");
CHK_STATUS(getStunAttribute(pStunPacket, STUN_ATTRIBUTE_TYPE_NONCE, &pStunAttr));
CHK_WARN(pStunAttr != NULL, retStatus, "No Nonce attribute found in Refresh Error response. Dropping Packet");
pStunAttributeNonce = (PStunAttributeNonce) pStunAttr;
CHK_WARN(pStunAttributeNonce->attribute.length <= STUN_MAX_NONCE_LEN, retStatus,
"Invalid Nonce found in Refresh Error response. Dropping Packet");
pTurnConnection->nonceLen = pStunAttributeNonce->attribute.length;
MEMCPY(pTurnConnection->turnNonce, pStunAttributeNonce->nonce, pTurnConnection->nonceLen);
CHK_STATUS(turnConnectionUpdateNonce(pTurnConnection));
break;
default:
/* Remove peer for any other error */
DLOGW("Received STUN error response. Error type: 0x%02x, Error Code: %u. attribute len %u, Error detail: %s.", stunPacketType,
pStunAttributeErrorCode->errorCode, pStunAttributeErrorCode->attribute.length, pStunAttributeErrorCode->errorPhrase);
BOOL found = FALSE;
/* Find TurnPeer using transaction Id, then mark it as failed */
for (i = 0; iterate && i < pTurnConnection->turnPeerCount; ++i) {
pTurnPeer = &pTurnConnection->turnPeerList[i];
if (transactionIdStoreHasId(pTurnPeer->pTransactionIdStore, pBuffer + STUN_PACKET_TRANSACTION_ID_OFFSET)) {
CHAR ipAddr[KVS_IP_ADDRESS_STRING_BUFFER_LEN];
pTurnPeer->connectionState = TURN_PEER_CONN_STATE_FAILED;
iterate = FALSE;
found = TRUE;
getIpAddrStr(&pTurnPeer->address, ipAddr, ARRAY_SIZE(ipAddr));
DLOGD("remove turn peer with ip: %s:%u. family:%d", ipAddr, (UINT16) getInt16(pTurnPeer->address.port),
pTurnPeer->address.family);
}
}
if (found == FALSE) {
DLOGD("can not find any corresponding turn peer");
}
break;
}
CleanUp:
CHK_LOG_ERR(retStatus);
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}
if (pStunPacket != NULL) {
freeStunPacket(&pStunPacket);
}
LEAVES();
return retStatus;
}
STATUS turnConnectionHandleChannelData(PTurnConnection pTurnConnection, PBYTE pBuffer, UINT32 bufferLen, PTurnChannelData pChannelData,
PUINT32 pChannelDataCount, PUINT32 pProcessedDataLen)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
BOOL locked = FALSE;
UINT32 turnChannelDataCount = 0, hexStrLen = 0;
UINT16 channelNumber = 0;
PTurnPeer pTurnPeer = NULL;
PCHAR hexStr = NULL;
CHK(pTurnConnection != NULL && pChannelData != NULL && pChannelDataCount != NULL && pProcessedDataLen != NULL, STATUS_NULL_ARG);
CHK(pBuffer != NULL && bufferLen > 0, STATUS_INVALID_ARG);
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
if (pTurnConnection->protocol == KVS_SOCKET_PROTOCOL_UDP) {
channelNumber = (UINT16) getInt16(*(PINT16) pBuffer);
if ((pTurnPeer = turnConnectionGetPeerWithChannelNumber(pTurnConnection, channelNumber)) != NULL) {
/*
* Not expecting fragmented channel message in UDP mode.
* Data channel messages from UDP connection may or may not padded. Thus turnConnectionHandleChannelDataTcpMode wont
* be able to parse it.
*/
pChannelData->data = pBuffer + TURN_DATA_CHANNEL_SEND_OVERHEAD;
pChannelData->size = GET_STUN_PACKET_SIZE(pBuffer);
pChannelData->senderAddr = pTurnPeer->address;
turnChannelDataCount = 1;
if (pChannelData->size + TURN_DATA_CHANNEL_SEND_OVERHEAD < bufferLen) {
DLOGD("Not expecting multiple channel data messages in one UDP packet.");
}
} else {
CHK_STATUS(hexEncode(pBuffer, bufferLen, NULL, &hexStrLen));
hexStr = MEMCALLOC(1, hexStrLen * SIZEOF(CHAR));
CHK(hexStr != NULL, STATUS_NOT_ENOUGH_MEMORY);
CHK_STATUS(hexEncode(pBuffer, bufferLen, hexStr, &hexStrLen));
DLOGE("Turn connection does not have channel number, dumping payload: %s", hexStr);
turnChannelDataCount = 0;
SAFE_MEMFREE(hexStr);
}
*pProcessedDataLen = bufferLen;
} else {
CHK_STATUS(
turnConnectionHandleChannelDataTcpMode(pTurnConnection, pBuffer, bufferLen, pChannelData, &turnChannelDataCount, pProcessedDataLen));
}
*pChannelDataCount = turnChannelDataCount;
CleanUp:
CHK_LOG_ERR(retStatus);
if (hexStr != NULL) {
SAFE_MEMFREE(hexStr);
}
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}
LEAVES();
return retStatus;
}
/*
* turnConnectionHandleChannelDataTcpMode will process a single turn channel data item from pBuffer then return.
* If there is a complete channel data item in buffer, upon return *pTurnChannelDataCount will be 1, *pTurnChannelData
* will data details about the parsed channel data. *pProcessedDataLen will be the length of data processed.
*/
STATUS turnConnectionHandleChannelDataTcpMode(PTurnConnection pTurnConnection, PBYTE pBuffer, UINT32 bufferLen, PTurnChannelData pChannelData,
PUINT32 pTurnChannelDataCount, PUINT32 pProcessedDataLen)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
UINT32 bytesToCopy = 0, remainingMsgSize = 0, paddedChannelDataLen = 0, remainingBufLen = 0, channelDataCount = 0;
PBYTE pCurPos = NULL;
UINT16 channelNumber = 0;
PTurnPeer pTurnPeer = NULL;
CHK(pTurnConnection != NULL && pChannelData != NULL && pTurnChannelDataCount != NULL && pProcessedDataLen != NULL, STATUS_NULL_ARG);
CHK(pBuffer != NULL && bufferLen > 0, STATUS_INVALID_ARG);
pCurPos = pBuffer;
remainingBufLen = bufferLen;
/* process only one channel data and return. Because channel data can be intermixed with STUN packet.
* need to check remainingBufLen too because channel data could be incomplete. */
while (remainingBufLen != 0 && channelDataCount == 0) {
if (pTurnConnection->currRecvDataLen != 0) {
DLOGV("currRecvDataLen: %d", pTurnConnection->currRecvDataLen);
if (pTurnConnection->currRecvDataLen >= TURN_DATA_CHANNEL_SEND_OVERHEAD) {
/* pTurnConnection->recvDataBuffer always has channel data start */
paddedChannelDataLen = ROUND_UP((UINT32) getInt16(*(PINT16) (pTurnConnection->recvDataBuffer + SIZEOF(channelNumber))), 4);
remainingMsgSize = paddedChannelDataLen - (pTurnConnection->currRecvDataLen - TURN_DATA_CHANNEL_SEND_OVERHEAD);
bytesToCopy = MIN(remainingMsgSize, remainingBufLen);
remainingBufLen -= bytesToCopy;
if (bytesToCopy > (pTurnConnection->recvDataBufferSize - pTurnConnection->currRecvDataLen)) {
/* drop current message if it is longer than buffer size. */
pTurnConnection->currRecvDataLen = 0;
CHK(FALSE, STATUS_BUFFER_TOO_SMALL);
}
MEMCPY(pTurnConnection->recvDataBuffer + pTurnConnection->currRecvDataLen, pCurPos, bytesToCopy);
pTurnConnection->currRecvDataLen += bytesToCopy;
pCurPos += bytesToCopy;
CHECK_EXT(pTurnConnection->currRecvDataLen <= paddedChannelDataLen + TURN_DATA_CHANNEL_SEND_OVERHEAD,
"Should not store more than one channel data message in recvDataBuffer");
/*
* once assembled a complete channel data in recvDataBuffer, copy over to completeChannelDataBuffer to
* make room for subsequent partial channel data.
*/
if (pTurnConnection->currRecvDataLen == (paddedChannelDataLen + TURN_DATA_CHANNEL_SEND_OVERHEAD)) {
channelNumber = (UINT16) getInt16(*(PINT16) pTurnConnection->recvDataBuffer);
if ((pTurnPeer = turnConnectionGetPeerWithChannelNumber(pTurnConnection, channelNumber)) != NULL) {
MEMCPY(pTurnConnection->completeChannelDataBuffer, pTurnConnection->recvDataBuffer, pTurnConnection->currRecvDataLen);
pChannelData->data = pTurnConnection->completeChannelDataBuffer + TURN_DATA_CHANNEL_SEND_OVERHEAD;
pChannelData->size = GET_STUN_PACKET_SIZE(pTurnConnection->completeChannelDataBuffer);
pChannelData->senderAddr = pTurnPeer->address;
channelDataCount++;
}
pTurnConnection->currRecvDataLen = 0;
}
} else {
/* copy just enough to make a complete channel data header */
bytesToCopy = MIN(remainingMsgSize, TURN_DATA_CHANNEL_SEND_OVERHEAD - pTurnConnection->currRecvDataLen);
MEMCPY(pTurnConnection->recvDataBuffer + pTurnConnection->currRecvDataLen, pCurPos, bytesToCopy);
pTurnConnection->currRecvDataLen += bytesToCopy;
pCurPos += bytesToCopy;
}
} else {
/* new channel message start */
CHK(*pCurPos == TURN_DATA_CHANNEL_MSG_FIRST_BYTE, STATUS_TURN_MISSING_CHANNEL_DATA_HEADER);
paddedChannelDataLen = ROUND_UP((UINT32) getInt16(*(PINT16) (pCurPos + SIZEOF(UINT16))), 4);
if (remainingBufLen >= (paddedChannelDataLen + TURN_DATA_CHANNEL_SEND_OVERHEAD)) {
channelNumber = (UINT16) getInt16(*(PINT16) pCurPos);
if ((pTurnPeer = turnConnectionGetPeerWithChannelNumber(pTurnConnection, channelNumber)) != NULL) {
pChannelData->data = pCurPos + TURN_DATA_CHANNEL_SEND_OVERHEAD;
pChannelData->size = GET_STUN_PACKET_SIZE(pCurPos);
pChannelData->senderAddr = pTurnPeer->address;
channelDataCount++;
}
remainingBufLen -= (paddedChannelDataLen + TURN_DATA_CHANNEL_SEND_OVERHEAD);
pCurPos += (paddedChannelDataLen + TURN_DATA_CHANNEL_SEND_OVERHEAD);
} else {
CHK(pTurnConnection->currRecvDataLen == 0, STATUS_TURN_NEW_DATA_CHANNEL_MSG_HEADER_BEFORE_PREVIOUS_MSG_FINISH);
CHK(remainingBufLen <= (pTurnConnection->recvDataBufferSize), STATUS_BUFFER_TOO_SMALL);
MEMCPY(pTurnConnection->recvDataBuffer, pCurPos, remainingBufLen);
pTurnConnection->currRecvDataLen += remainingBufLen;
pCurPos += remainingBufLen;
remainingBufLen = 0;
}
}
}
/* return actual channel data count */
*pTurnChannelDataCount = channelDataCount;
*pProcessedDataLen = bufferLen - remainingBufLen;
CleanUp:
CHK_LOG_ERR(retStatus);
LEAVES();
return retStatus;
}
STATUS turnConnectionAddPeer(PTurnConnection pTurnConnection, PKvsIpAddress pPeerAddress)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PTurnPeer pTurnPeer = NULL;
BOOL locked = FALSE;
CHK(pTurnConnection != NULL && pPeerAddress != NULL, STATUS_NULL_ARG);
CHK(pTurnConnection->turnServer.ipAddress.family == pPeerAddress->family, STATUS_INVALID_ARG);
CHK_WARN(IS_IPV4_ADDR(pPeerAddress), retStatus, "Drop IPv6 turn peer because only IPv4 turn peer is supported right now");
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
/* check for duplicate */
CHK(turnConnectionGetPeerWithIp(pTurnConnection, pPeerAddress) == NULL, retStatus);
CHK_WARN(pTurnConnection->turnPeerCount < DEFAULT_TURN_MAX_PEER_COUNT, STATUS_INVALID_OPERATION, "Add peer failed. Max peer count reached");
pTurnPeer = &pTurnConnection->turnPeerList[pTurnConnection->turnPeerCount++];
pTurnPeer->connectionState = TURN_PEER_CONN_STATE_CREATE_PERMISSION;
pTurnPeer->address = *pPeerAddress;
pTurnPeer->xorAddress = *pPeerAddress;
/* safe to down cast because DEFAULT_TURN_MAX_PEER_COUNT is enforced */
pTurnPeer->channelNumber = (UINT16) pTurnConnection->turnPeerCount + TURN_CHANNEL_BIND_CHANNEL_NUMBER_BASE;
pTurnPeer->permissionExpirationTime = INVALID_TIMESTAMP_VALUE;
pTurnPeer->ready = FALSE;
pTurnPeer->firstTimeCreatePermReq = TRUE;
pTurnPeer->firstTimeBindChannelReq = TRUE;
pTurnPeer->firstTimeCreatePermResponse = TRUE;
pTurnPeer->firstTimeBindChannelResponse = TRUE;
CHK_STATUS(xorIpAddress(&pTurnPeer->xorAddress, NULL)); /* only work for IPv4 for now */
CHK_STATUS(createTransactionIdStore(DEFAULT_MAX_STORED_TRANSACTION_ID_COUNT, &pTurnPeer->pTransactionIdStore));
pTurnPeer = NULL;
CleanUp:
if (STATUS_FAILED(retStatus) && pTurnPeer != NULL) {
freeTransactionIdStore(&pTurnPeer->pTransactionIdStore);
pTurnConnection->turnPeerCount--;
}
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}
LEAVES();
return retStatus;
}
STATUS turnConnectionSendData(PTurnConnection pTurnConnection, PBYTE pBuf, UINT32 bufLen, PKvsIpAddress pDestIp)
{
STATUS retStatus = STATUS_SUCCESS;
PTurnPeer pSendPeer = NULL;
UINT32 paddedDataLen = 0;
CHAR ipAddrStr[KVS_IP_ADDRESS_STRING_BUFFER_LEN];
BOOL locked = FALSE;
BOOL sendLocked = FALSE;
CHK(pTurnConnection != NULL && pDestIp != NULL, STATUS_NULL_ARG);
CHK(pBuf != NULL && bufLen > 0, STATUS_INVALID_ARG);
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
if (!(pTurnConnection->state == TURN_STATE_CREATE_PERMISSION || pTurnConnection->state == TURN_STATE_BIND_CHANNEL ||
pTurnConnection->state == TURN_STATE_READY)) {
DLOGV("TurnConnection not ready to send data");
// If turn is not ready yet. Drop the send since ice will retry.
CHK(FALSE, retStatus);
}
pSendPeer = turnConnectionGetPeerWithIp(pTurnConnection, pDestIp);
CHK_STATUS(getIpAddrStr(pDestIp, ipAddrStr, ARRAY_SIZE(ipAddrStr)));
if (pSendPeer == NULL) {
DLOGV("Unable to send data through turn because peer with address %s:%u is not found", ipAddrStr, KVS_GET_IP_ADDRESS_PORT(pDestIp));
CHK(FALSE, retStatus);
} else if (pSendPeer->connectionState == TURN_PEER_CONN_STATE_FAILED) {
CHK(FALSE, STATUS_TURN_CONNECTION_PEER_NOT_USABLE);
} else if (!pSendPeer->ready) {
DLOGV("Unable to send data through turn because turn channel is not established with peer with address %s:%u", ipAddrStr,
KVS_GET_IP_ADDRESS_PORT(pDestIp));
CHK(FALSE, retStatus);
}
MUTEX_UNLOCK(pTurnConnection->lock);
locked = FALSE;
/* need to serialize send because every send load data into the same buffer pTurnConnection->sendDataBuffer */
MUTEX_LOCK(pTurnConnection->sendLock);
sendLocked = TRUE;
CHK(pTurnConnection->dataBufferSize - TURN_DATA_CHANNEL_SEND_OVERHEAD >= bufLen, STATUS_BUFFER_TOO_SMALL);
paddedDataLen = (UINT32) ROUND_UP(TURN_DATA_CHANNEL_SEND_OVERHEAD + bufLen, 4);
/* generate data channel TURN message */
putInt16((PINT16) (pTurnConnection->sendDataBuffer), pSendPeer->channelNumber);
putInt16((PINT16) (pTurnConnection->sendDataBuffer + 2), (UINT16) bufLen);
MEMCPY(pTurnConnection->sendDataBuffer + TURN_DATA_CHANNEL_SEND_OVERHEAD, pBuf, bufLen);
retStatus = iceUtilsSendData(pTurnConnection->sendDataBuffer, paddedDataLen, &pTurnConnection->turnServer.ipAddress,
pTurnConnection->pControlChannel, NULL, FALSE);
if (STATUS_FAILED(retStatus)) {
DLOGW("iceUtilsSendData failed with 0x%08x", retStatus);
if (retStatus != STATUS_SOCKET_CONNECTION_CLOSED_ALREADY) {
retStatus = STATUS_SUCCESS;
}
}
CleanUp:
CHK_LOG_ERR(retStatus);
if (sendLocked) {
MUTEX_UNLOCK(pTurnConnection->sendLock);
}
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}
return retStatus;
}
STATUS turnConnectionStart(PTurnConnection pTurnConnection)
{
STATUS retStatus = STATUS_SUCCESS;
BOOL locked = FALSE;
SIZE_T timerCallbackId;
CHK(pTurnConnection != NULL, STATUS_NULL_ARG);
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
/* only execute when turnConnection is in TURN_STATE_NEW */
CHK(pTurnConnection->state == TURN_STATE_NEW, retStatus);
MUTEX_UNLOCK(pTurnConnection->lock);
locked = FALSE;
timerCallbackId = ATOMIC_EXCHANGE(&pTurnConnection->timerCallbackId, MAX_UINT32);
if (timerCallbackId != MAX_UINT32) {
CHK_STATUS(timerQueueCancelTimer(pTurnConnection->timerQueueHandle, (UINT32) timerCallbackId, (UINT64) pTurnConnection));
}
/* schedule the timer, which will drive the state machine. */
CHK_STATUS(timerQueueAddTimer(pTurnConnection->timerQueueHandle, KVS_ICE_DEFAULT_TIMER_START_DELAY, pTurnConnection->currentTimerCallingPeriod,
turnConnectionTimerCallback, (UINT64) pTurnConnection, (PUINT32) &timerCallbackId));
ATOMIC_STORE(&pTurnConnection->timerCallbackId, timerCallbackId);
CleanUp:
CHK_LOG_ERR(retStatus);
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}
return retStatus;
}
STATUS turnConnectionRefreshAllocation(PTurnConnection pTurnConnection)
{
STATUS retStatus = STATUS_SUCCESS;
UINT64 currTime = 0;
PStunAttributeLifetime pStunAttributeLifetime = NULL;
CHK(pTurnConnection != NULL, STATUS_NULL_ARG);
currTime = GETTIME();
// return early if we are not in grace period yet or if we just sent refresh earlier
CHK(IS_VALID_TIMESTAMP(pTurnConnection->allocationExpirationTime) &&
currTime + DEFAULT_TURN_ALLOCATION_REFRESH_GRACE_PERIOD >= pTurnConnection->allocationExpirationTime &&
currTime >= pTurnConnection->nextAllocationRefreshTime,
retStatus);
DLOGD("Refresh turn allocation");
CHK_STATUS(getStunAttribute(pTurnConnection->pTurnAllocationRefreshPacket, STUN_ATTRIBUTE_TYPE_LIFETIME,
(PStunAttributeHeader*) &pStunAttributeLifetime));
CHK(pStunAttributeLifetime != NULL, STATUS_INTERNAL_ERROR);
pStunAttributeLifetime->lifetime = DEFAULT_TURN_ALLOCATION_LIFETIME_SECONDS;
CHK_STATUS(iceUtilsSendStunPacket(pTurnConnection->pTurnAllocationRefreshPacket, pTurnConnection->longTermKey,
ARRAY_SIZE(pTurnConnection->longTermKey), &pTurnConnection->turnServer.ipAddress,
pTurnConnection->pControlChannel, NULL, FALSE));
pTurnConnection->nextAllocationRefreshTime = currTime + DEFAULT_TURN_SEND_REFRESH_INVERVAL;
CleanUp:
CHK_LOG_ERR(retStatus);
return retStatus;
}
STATUS turnConnectionRefreshPermission(PTurnConnection pTurnConnection, PBOOL pNeedRefresh)
{
STATUS retStatus = STATUS_SUCCESS;
UINT64 currTime = 0;
PTurnPeer pTurnPeer = NULL;
BOOL needRefresh = FALSE;
CHAR ipAddr[KVS_IP_ADDRESS_STRING_BUFFER_LEN];
UINT32 i;
CHK(pTurnConnection != NULL && pNeedRefresh != NULL, STATUS_NULL_ARG);
currTime = GETTIME();
// refresh all peers whenever one of them expire is close to expiration
for (i = 0; i < pTurnConnection->turnPeerCount; ++i) {
pTurnPeer = &pTurnConnection->turnPeerList[i];
if (IS_VALID_TIMESTAMP(pTurnPeer->permissionExpirationTime) &&
currTime + DEFAULT_TURN_PERMISSION_REFRESH_GRACE_PERIOD >= pTurnPeer->permissionExpirationTime) {
getIpAddrStr(&pTurnPeer->address, ipAddr, ARRAY_SIZE(ipAddr));
DLOGD("[%p] Refreshing turn permission for %s:%d", pTurnConnection, ipAddr, pTurnPeer->address.port);
needRefresh = TRUE;
}
}
CleanUp:
if (pNeedRefresh != NULL) {
*pNeedRefresh = needRefresh;
}
CHK_LOG_ERR(retStatus);
return retStatus;
}
STATUS turnConnectionFreePreAllocatedPackets(PTurnConnection pTurnConnection)
{
STATUS retStatus = STATUS_SUCCESS;
CHK(pTurnConnection != NULL, STATUS_NULL_ARG);
if (pTurnConnection->pTurnPacket != NULL) {
CHK_STATUS(freeStunPacket(&pTurnConnection->pTurnPacket));
}
if (pTurnConnection->pTurnChannelBindPacket != NULL) {
CHK_STATUS(freeStunPacket(&pTurnConnection->pTurnChannelBindPacket));
}
if (pTurnConnection->pTurnCreatePermissionPacket != NULL) {
CHK_STATUS(freeStunPacket(&pTurnConnection->pTurnCreatePermissionPacket));
}
if (pTurnConnection->pTurnAllocationRefreshPacket != NULL) {
CHK_STATUS(freeStunPacket(&pTurnConnection->pTurnAllocationRefreshPacket));
}
CleanUp:
CHK_LOG_ERR(retStatus);
return retStatus;
}
STATUS turnConnectionUpdateNonce(PTurnConnection pTurnConnection)
{
STATUS retStatus = STATUS_SUCCESS;
// assume holding pTurnConnection->lock
// update nonce for pre-created packets
if (pTurnConnection->pTurnPacket != NULL) {
CHK_STATUS(updateStunNonceAttribute(pTurnConnection->pTurnPacket, pTurnConnection->turnNonce, pTurnConnection->nonceLen));
}
if (pTurnConnection->pTurnAllocationRefreshPacket != NULL) {
CHK_STATUS(updateStunNonceAttribute(pTurnConnection->pTurnAllocationRefreshPacket, pTurnConnection->turnNonce, pTurnConnection->nonceLen));
}
if (pTurnConnection->pTurnChannelBindPacket != NULL) {
CHK_STATUS(updateStunNonceAttribute(pTurnConnection->pTurnChannelBindPacket, pTurnConnection->turnNonce, pTurnConnection->nonceLen));
}
if (pTurnConnection->pTurnCreatePermissionPacket != NULL) {
CHK_STATUS(updateStunNonceAttribute(pTurnConnection->pTurnCreatePermissionPacket, pTurnConnection->turnNonce, pTurnConnection->nonceLen));
}
CleanUp:
CHK_LOG_ERR(retStatus);
return retStatus;
}
STATUS turnConnectionShutdown(PTurnConnection pTurnConnection, UINT64 waitUntilAllocationFreedTimeout)
{
STATUS retStatus = STATUS_SUCCESS;
UINT64 currentTime = 0, timeoutTime = 0;
BOOL locked = FALSE;
CHK(pTurnConnection != NULL, STATUS_NULL_ARG);
ATOMIC_STORE(&pTurnConnection->stopTurnConnection, TRUE);
MUTEX_LOCK(pTurnConnection->lock);
locked = TRUE;
CHK(ATOMIC_LOAD_BOOL(&pTurnConnection->hasAllocation), retStatus);
if (waitUntilAllocationFreedTimeout > 0) {
currentTime = GETTIME();
timeoutTime = currentTime + waitUntilAllocationFreedTimeout;
while (ATOMIC_LOAD_BOOL(&pTurnConnection->hasAllocation) && currentTime < timeoutTime) {
CVAR_WAIT(pTurnConnection->freeAllocationCvar, pTurnConnection->lock, waitUntilAllocationFreedTimeout);
currentTime = GETTIME();
}
if (ATOMIC_LOAD_BOOL(&pTurnConnection->hasAllocation)) {
DLOGD("Failed to free turn allocation within timeout of %" PRIu64 " milliseconds",
waitUntilAllocationFreedTimeout / HUNDREDS_OF_NANOS_IN_A_MILLISECOND);
}
}
CleanUp:
CHK_LOG_ERR(retStatus);
if (locked) {
MUTEX_UNLOCK(pTurnConnection->lock);
}