Amazon Kinesis Webrtc C SDK
TurnConnection.h
Go to the documentation of this file.
1 /*******************************************
2 TurnConnection internal include file
3 *******************************************/
4 #ifndef __KINESIS_VIDEO_WEBRTC_CLIENT_TURN_CONNECTION__
5 #define __KINESIS_VIDEO_WEBRTC_CLIENT_TURN_CONNECTION__
6 
7 #pragma once
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 // https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
14 #define TURN_REQUEST_TRANSPORT_UDP 17
15 #define TURN_REQUEST_TRANSPORT_TCP 6
16 #define DEFAULT_TURN_ALLOCATION_LIFETIME_SECONDS 600
17 // required by rfc5766 to be 300s
18 #define TURN_PERMISSION_LIFETIME (300 * HUNDREDS_OF_NANOS_IN_A_SECOND)
19 #define DEFAULT_TURN_TIMER_INTERVAL_BEFORE_READY (50 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND)
20 #define DEFAULT_TURN_TIMER_INTERVAL_AFTER_READY (1 * HUNDREDS_OF_NANOS_IN_A_SECOND)
21 #define DEFAULT_TURN_SEND_REFRESH_INVERVAL (1 * HUNDREDS_OF_NANOS_IN_A_SECOND)
22 
23 // turn state timeouts
24 #define DEFAULT_TURN_GET_CREDENTIAL_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
25 #define DEFAULT_TURN_ALLOCATION_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
26 #define DEFAULT_TURN_CREATE_PERMISSION_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
27 #define DEFAULT_TURN_BIND_CHANNEL_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
28 #define DEFAULT_TURN_CLEAN_UP_TIMEOUT (10 * HUNDREDS_OF_NANOS_IN_A_SECOND)
29 
30 #define DEFAULT_TURN_ALLOCATION_REFRESH_GRACE_PERIOD (30 * HUNDREDS_OF_NANOS_IN_A_SECOND)
31 #define DEFAULT_TURN_PERMISSION_REFRESH_GRACE_PERIOD (30 * HUNDREDS_OF_NANOS_IN_A_SECOND)
32 
33 #define MAX_TURN_CHANNEL_DATA_MESSAGE_SIZE 4 + 65536 /* header + data */
34 #define DEFAULT_TURN_MESSAGE_SEND_CHANNEL_DATA_BUFFER_LEN MAX_TURN_CHANNEL_DATA_MESSAGE_SIZE
35 #define DEFAULT_TURN_MESSAGE_RECV_CHANNEL_DATA_BUFFER_LEN MAX_TURN_CHANNEL_DATA_MESSAGE_SIZE
36 #define DEFAULT_TURN_CHANNEL_DATA_BUFFER_SIZE 512
37 #define DEFAULT_TURN_MAX_PEER_COUNT 32
38 #define MAX_TURN_PROFILE_LOG_DESC_LEN 256
39 
40 // all turn channel numbers must be greater than 0x4000 and less than 0x7FFF
41 #define TURN_CHANNEL_BIND_CHANNEL_NUMBER_BASE (UINT16) 0x4000
42 
43 // 2 byte channel number 2 data byte size
44 #define TURN_DATA_CHANNEL_SEND_OVERHEAD 4
45 #define TURN_DATA_CHANNEL_MSG_FIRST_BYTE 0x40
46 
47 #define TURN_STATE_MACHINE_NAME (PCHAR) "TURN"
48 
49 #define TURN_STATE_NEW_STR (PCHAR) "TURN_STATE_NEW"
50 #define TURN_STATE_CHECK_SOCKET_CONNECTION_STR (PCHAR) "TURN_STATE_CHECK_SOCKET_CONNECTION"
51 #define TURN_STATE_GET_CREDENTIALS_STR (PCHAR) "TURN_STATE_GET_CREDENTIALS"
52 #define TURN_STATE_ALLOCATION_STR (PCHAR) "TURN_STATE_ALLOCATION"
53 #define TURN_STATE_CREATE_PERMISSION_STR (PCHAR) "TURN_STATE_CREATE_PERMISSION"
54 #define TURN_STATE_BIND_CHANNEL_STR (PCHAR) "TURN_STATE_BIND_CHANNEL"
55 #define TURN_STATE_READY_STR (PCHAR) "TURN_STATE_READY"
56 #define TURN_STATE_CLEAN_UP_STR (PCHAR) "TURN_STATE_CLEAN_UP"
57 #define TURN_STATE_FAILED_STR (PCHAR) "TURN_STATE_FAILED"
58 #define TURN_STATE_UNKNOWN_STR (PCHAR) "TURN_STATE_UNKNOWN"
59 
61 typedef STATUS (*TurnStateFailedFunc)(PSocketConnection, UINT64);
62 
63 typedef enum {
69 
70 typedef enum {
74 
75 typedef struct {
76  PBYTE data;
77  UINT32 size;
80 
81 typedef struct {
82  UINT64 customData;
86 
87 typedef struct {
90  /*
91  * Steps to create a turn channel for a peer:
92  * - create permission
93  * - channel bind
94  * - ready to send data
95  */
98  UINT16 channelNumber;
100  BOOL ready;
110 
111 typedef struct {
117 
118 typedef struct __TurnConnection TurnConnection;
120  volatile ATOMIC_BOOL stopTurnConnection;
121  /* shutdown is complete when turn socket is closed */
122  volatile ATOMIC_BOOL shutdownComplete;
123  volatile ATOMIC_BOOL hasAllocation;
124  volatile SIZE_T timerCallbackId;
125 
126  // realm attribute in Allocation response
129  UINT16 nonceLen;
130  BYTE longTermKey[KVS_MD5_DIGEST_LENGTH];
133 
135 
138 
139  TIMER_QUEUE_HANDLE timerQueueHandle;
140 
142 
143  MUTEX lock;
144  MUTEX sendLock;
146 
147  UINT64 state;
148 
150 
151  STATUS errorStatus;
152 
157 
159 
161 
163 
166 
168 
171 
175  // when a complete channel data have been assembled in recvDataBuffer, move it to completeChannelDataBuffer
176  // to make room for subsequent partial channel data.
178 
181 
185  PStateMachine pStateMachine;
186 };
188 
201 
202 // used for state machine
203 UINT64 turnConnectionGetTime(UINT64);
204 
206 STATUS turnConnectionTimerCallback(UINT32, UINT64, UINT64);
207 STATUS turnConnectionGetLongTermKey(PCHAR, PCHAR, PCHAR, PBYTE, UINT32);
208 STATUS turnConnectionPackageTurnAllocationRequest(PCHAR, PCHAR, PBYTE, UINT16, UINT32, PStunPacket*);
209 
211 
212 STATUS turnConnectionHandleStun(PTurnConnection, PBYTE, UINT32);
213 STATUS turnConnectionHandleStunError(PTurnConnection, PBYTE, UINT32);
214 STATUS turnConnectionHandleChannelData(PTurnConnection, PBYTE, UINT32, PTurnChannelData, PUINT32, PUINT32);
215 STATUS turnConnectionHandleChannelDataTcpMode(PTurnConnection, PBYTE, UINT32, PTurnChannelData, PUINT32, PUINT32);
217 
220 
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 #endif /* __KINESIS_VIDEO_WEBRTC_CLIENT_TURN_CONNECTION__ */
#define STUN_MAX_NONCE_LEN
Definition: Include_i.h:81
#define STUN_MAX_REALM_LEN
Definition: Include_i.h:78
struct KvsIpAddress * PKvsIpAddress
KVS_SOCKET_PROTOCOL
Definition: Network.h:48
struct __SocketConnection * PSocketConnection
Definition: SocketConnection.h:48
STATUS turnConnectionGetLongTermKey(PCHAR, PCHAR, PCHAR, PBYTE, UINT32)
Definition: TurnConnection.c:1134
STATUS turnConnectionFreePreAllocatedPackets(PTurnConnection)
Definition: TurnConnection.c:907
STATUS turnConnectionAddPeer(PTurnConnection, PKvsIpAddress)
Definition: TurnConnection.c:669
STATUS(* RelayAddressAvailableFunc)(UINT64, PKvsIpAddress, PSocketConnection)
Definition: TurnConnection.h:60
TURN_CONNECTION_DATA_TRANSFER_MODE
Definition: TurnConnection.h:70
@ TURN_CONNECTION_DATA_TRANSFER_MODE_SEND_INDIDATION
Definition: TurnConnection.h:71
@ TURN_CONNECTION_DATA_TRANSFER_MODE_DATA_CHANNEL
Definition: TurnConnection.h:72
UINT64 turnConnectionGetTime(UINT64)
Definition: TurnConnection.c:85
TURN_PEER_CONNECTION_STATE
Definition: TurnConnection.h:63
@ TURN_PEER_CONN_STATE_FAILED
Definition: TurnConnection.h:67
@ TURN_PEER_CONN_STATE_BIND_CHANNEL
Definition: TurnConnection.h:65
@ TURN_PEER_CONN_STATE_READY
Definition: TurnConnection.h:66
@ TURN_PEER_CONN_STATE_CREATE_PERMISSION
Definition: TurnConnection.h:64
STATUS turnConnectionHandleChannelData(PTurnConnection, PBYTE, UINT32, PTurnChannelData, PUINT32, PUINT32)
Definition: TurnConnection.c:492
STATUS turnConnectionShutdown(PTurnConnection, UINT64)
Definition: TurnConnection.c:964
STATUS freeTurnConnection(PTurnConnection *)
Definition: TurnConnection.c:91
STATUS createTurnConnection(PIceServer, TIMER_QUEUE_HANDLE, TURN_CONNECTION_DATA_TRANSFER_MODE, KVS_SOCKET_PROTOCOL, PTurnConnectionCallbacks, PSocketConnection, PConnectionListener, PTurnConnection *)
Definition: TurnConnection.c:10
STATUS turnConnectionTimerCallback(UINT32, UINT64, UINT64)
Definition: TurnConnection.c:1099
STATUS turnConnectionStart(PTurnConnection)
Definition: TurnConnection.c:798
STATUS turnConnectionIncomingDataHandler(PTurnConnection, PBYTE, UINT32, PKvsIpAddress, PKvsIpAddress, PTurnChannelData, PUINT32)
Definition: TurnConnection.c:157
struct TurnProfileDiagnostics * PTurnProfileDiagnostics
BOOL turnConnectionGetRelayAddress(PTurnConnection, PKvsIpAddress)
Definition: TurnConnection.c:1013
STATUS(* TurnStateFailedFunc)(PSocketConnection, UINT64)
Definition: TurnConnection.h:61
VOID turnConnectionFatalError(PTurnConnection, STATUS)
Definition: TurnConnection.c:1218
STATUS turnConnectionRefreshPermission(PTurnConnection, PBOOL)
Definition: TurnConnection.c:873
PTurnPeer turnConnectionGetPeerWithIp(PTurnConnection, PKvsIpAddress)
Definition: TurnConnection.c:1204
STATUS checkTurnPeerConnections(PTurnConnection)
Definition: TurnConnection.c:1027
STATUS turnConnectionHandleStun(PTurnConnection, PBYTE, UINT32)
Definition: TurnConnection.c:207
struct TurnConnectionCallbacks * PTurnConnectionCallbacks
STATUS turnConnectionSendData(PTurnConnection, PBYTE, UINT32, PKvsIpAddress)
Definition: TurnConnection.c:720
struct TurnPeer * PTurnPeer
STATUS turnConnectionPackageTurnAllocationRequest(PCHAR, PCHAR, PBYTE, UINT16, UINT32, PStunPacket *)
Definition: TurnConnection.c:1155
STATUS turnConnectionHandleChannelDataTcpMode(PTurnConnection, PBYTE, UINT32, PTurnChannelData, PUINT32, PUINT32)
Definition: TurnConnection.c:566
PTurnPeer turnConnectionGetPeerWithChannelNumber(PTurnConnection, UINT16)
Definition: TurnConnection.c:1190
STATUS turnConnectionRefreshAllocation(PTurnConnection)
Definition: TurnConnection.c:837
STATUS turnConnectionUpdateNonce(PTurnConnection)
Definition: TurnConnection.c:935
struct TurnChannelData * PTurnChannelData
struct __TurnConnection * PTurnConnection
Definition: TurnConnection.h:187
STATUS turnConnectionHandleStunError(PTurnConnection, PBYTE, UINT32)
Definition: TurnConnection.c:368
BOOL turnConnectionIsShutdownComplete(PTurnConnection)
Definition: TurnConnection.c:1004
#define DEFAULT_TURN_MAX_PEER_COUNT
Definition: TurnConnection.h:37
Definition: ConnectionListener.h:19
Definition: IceUtils.h:53
Definition: Include_i.h:99
Definition: Stun.h:357
Definition: IceUtils.h:31
Definition: TurnConnection.h:75
UINT32 size
Definition: TurnConnection.h:77
PBYTE data
Definition: TurnConnection.h:76
KvsIpAddress senderAddr
Definition: TurnConnection.h:78
Definition: TurnConnection.h:81
RelayAddressAvailableFunc relayAddressAvailableFn
Definition: TurnConnection.h:83
UINT64 customData
Definition: TurnConnection.h:82
TurnStateFailedFunc turnStateFailedFn
Definition: TurnConnection.h:84
Definition: TurnConnection.h:87
TURN_PEER_CONNECTION_STATE connectionState
Definition: TurnConnection.h:96
UINT64 bindChannelTime
Definition: TurnConnection.h:108
KvsIpAddress xorAddress
Definition: TurnConnection.h:89
UINT16 channelNumber
Definition: TurnConnection.h:98
UINT64 createPermissionTime
Definition: TurnConnection.h:104
PTransactionIdStore pTransactionIdStore
Definition: TurnConnection.h:97
KvsIpAddress address
Definition: TurnConnection.h:88
BOOL ready
Definition: TurnConnection.h:100
UINT64 bindChannelStartTime
Definition: TurnConnection.h:107
BOOL firstTimeCreatePermReq
Definition: TurnConnection.h:101
UINT64 createPermissionStartTime
Definition: TurnConnection.h:103
BOOL firstTimeCreatePermResponse
Definition: TurnConnection.h:102
UINT64 permissionExpirationTime
Definition: TurnConnection.h:99
BOOL firstTimeBindChannelResponse
Definition: TurnConnection.h:106
BOOL firstTimeBindChannelReq
Definition: TurnConnection.h:105
Definition: TurnConnection.h:111
UINT64 getCredentialsStartTime
Definition: TurnConnection.h:112
UINT64 createAllocationStartTime
Definition: TurnConnection.h:114
UINT64 getCredentialsTime
Definition: TurnConnection.h:113
UINT64 createAllocationTime
Definition: TurnConnection.h:115
Definition: SocketConnection.h:25
Definition: TurnConnection.h:119
MUTEX sendLock
Definition: TurnConnection.h:144
UINT64 nextAllocationRefreshTime
Definition: TurnConnection.h:180
TurnConnectionCallbacks turnConnectionCallbacks
Definition: TurnConnection.h:167
PStateMachine pStateMachine
Definition: TurnConnection.h:185
PSocketConnection pControlChannel
Definition: TurnConnection.h:134
PStunPacket pTurnPacket
Definition: TurnConnection.h:153
UINT64 stateTimeoutTime
Definition: TurnConnection.h:149
MUTEX lock
Definition: TurnConnection.h:143
volatile ATOMIC_BOOL shutdownComplete
Definition: TurnConnection.h:122
UINT64 allocationExpirationTime
Definition: TurnConnection.h:179
BYTE turnNonce[STUN_MAX_NONCE_LEN]
Definition: TurnConnection.h:128
PStunPacket pTurnAllocationRefreshPacket
Definition: TurnConnection.h:156
PStunPacket pTurnChannelBindPacket
Definition: TurnConnection.h:155
STATUS errorStatus
Definition: TurnConnection.h:151
UINT64 currentTimerCallingPeriod
Definition: TurnConnection.h:182
CHAR turnRealm[STUN_MAX_REALM_LEN+1]
Definition: TurnConnection.h:127
PBYTE recvDataBuffer
Definition: TurnConnection.h:172
BYTE longTermKey[KVS_MD5_DIGEST_LENGTH]
Definition: TurnConnection.h:130
PBYTE completeChannelDataBuffer
Definition: TurnConnection.h:177
KvsIpAddress relayAddress
Definition: TurnConnection.h:160
volatile SIZE_T timerCallbackId
Definition: TurnConnection.h:124
PConnectionListener pConnectionListener
Definition: TurnConnection.h:162
UINT16 nonceLen
Definition: TurnConnection.h:129
BOOL relayAddressReported
Definition: TurnConnection.h:132
UINT32 recvDataBufferSize
Definition: TurnConnection.h:173
TURN_CONNECTION_DATA_TRANSFER_MODE dataTransferMode
Definition: TurnConnection.h:164
TurnPeer turnPeerList[32]
Definition: TurnConnection.h:136
UINT32 currRecvDataLen
Definition: TurnConnection.h:174
PStunPacket pTurnCreatePermissionPacket
Definition: TurnConnection.h:154
UINT32 dataBufferSize
Definition: TurnConnection.h:170
TIMER_QUEUE_HANDLE timerQueueHandle
Definition: TurnConnection.h:139
KvsIpAddress hostAddress
Definition: TurnConnection.h:158
CVAR freeAllocationCvar
Definition: TurnConnection.h:145
IceServer turnServer
Definition: TurnConnection.h:141
volatile ATOMIC_BOOL stopTurnConnection
Definition: TurnConnection.h:120
KVS_SOCKET_PROTOCOL protocol
Definition: TurnConnection.h:165
TurnProfileDiagnostics turnProfileDiagnostics
Definition: TurnConnection.h:184
BOOL credentialObtained
Definition: TurnConnection.h:131
BOOL deallocatePacketSent
Definition: TurnConnection.h:183
UINT32 turnPeerCount
Definition: TurnConnection.h:137
PBYTE sendDataBuffer
Definition: TurnConnection.h:169
UINT64 state
Definition: TurnConnection.h:147
volatile ATOMIC_BOOL hasAllocation
Definition: TurnConnection.h:123