Amazon Kinesis Webrtc C SDK
Stun.h
Go to the documentation of this file.
1 /*******************************************
2 StunPackager internal include file
3 *******************************************/
4 #ifndef __KINESIS_VIDEO_WEBRTC_CLIENT_STUN_PACKAGER__
5 #define __KINESIS_VIDEO_WEBRTC_CLIENT_STUN_PACKAGER__
6 
7 #pragma once
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
21 #define STUN_HEADER_LEN (UINT16) 20
22 #define STUN_HEADER_TYPE_LEN (UINT16) 2
23 #define STUN_HEADER_DATA_LEN (UINT16) 2
24 #define STUN_HEADER_MAGIC_COOKIE (UINT32) 0x2112A442
25 #define STUN_HEADER_MAGIC_COOKIE_LE (UINT32) 0x42A41221
26 #define STUN_HEADER_MAGIC_COOKIE_LEN SIZEOF(STUN_HEADER_MAGIC_COOKIE)
27 #define STUN_HEADER_TRANSACTION_ID_LEN (UINT16) 12
28 
35 #define STUN_ATTRIBUTE_HEADER_TYPE_LEN (UINT16) 2
36 #define STUN_ATTRIBUTE_HEADER_DATA_LEN (UINT16) 2
37 #define STUN_ATTRIBUTE_HEADER_LEN (UINT16)(STUN_ATTRIBUTE_HEADER_TYPE_LEN + STUN_ATTRIBUTE_HEADER_DATA_LEN)
38 
39 #define STUN_ATTRIBUTE_ADDRESS_FAMILY_LEN (UINT16) 2
40 #define STUN_ATTRIBUTE_ADDRESS_PORT_LEN (UINT16) 2
41 #define STUN_ATTRIBUTE_ADDRESS_HEADER_LEN (UINT16)(STUN_ATTRIBUTE_ADDRESS_FAMILY_LEN + STUN_ATTRIBUTE_ADDRESS_PORT_LEN)
42 
46 #define STUN_ATTRIBUTE_FINGERPRINT_LEN (UINT16) 4
47 
51 #define STUN_ATTRIBUTE_PRIORITY_LEN (UINT16) 4
52 
56 #define STUN_ATTRIBUTE_LIFETIME_LEN (UINT16) 4
57 
58 #define STUN_ATTRIBUTE_CHANNEL_NUMBER_LEN (UINT16) 4
59 
63 #define STUN_ATTRIBUTE_CHANGE_REQUEST_FLAG_LEN (UINT16) 4
64 
68 #define STUN_ATTRIBUTE_ICE_CONTROL_LEN (UINT16) 8
69 
73 #define STUN_ATTRIBUTE_REQUESTED_TRANSPORT_PROTOCOL_LEN (UINT16) 4
74 
78 #define STUN_ATTRIBUTE_FLAG_LEN (UINT16) 0
79 
83 #define STUN_TRANSACTION_ID_LEN (UINT16) 12
84 
88 #define STUN_HMAC_VALUE_LEN KVS_SHA1_DIGEST_LENGTH
89 
93 #define STUN_ATTRIBUTE_MAX_COUNT 20
94 
98 #define STUN_PACKET_ALLOCATION_SIZE 2048
99 
100 #define STUN_SEND_INDICATION_OVERHEAD_SIZE 36
101 #define STUN_SEND_INDICATION_APPLICATION_DATA_OFFSET 36
102 #define STUN_SEND_INDICATION_APPLICATION_DATA_LEN_OFFSET 34
103 #define STUN_SEND_INDICATION_XOR_PEER_ADDRESS_OFFSET 28
104 #define STUN_SEND_INDICATION_XOR_PEER_ADDRESS_PORT_OFFSET 26
105 
110 #define STUN_FINGERPRINT_ATTRIBUTE_XOR_VALUE (UINT32) 0x5354554e
111 
112 #define STUN_ERROR_CODE_PACKET_ERROR_CLASS_OFFSET 2
113 #define STUN_ERROR_CODE_PACKET_ERROR_CODE_OFFSET 3
114 #define STUN_ERROR_CODE_PACKET_ERROR_PHRASE_OFFSET 4
115 #define STUN_PACKET_TRANSACTION_ID_OFFSET 8
116 
120 #define STUN_ATTRIBUTE_CHANGE_REQUEST_FLAG_CHANGE_IP 4
121 #define STUN_ATTRIBUTE_CHANGE_REQUEST_FLAG_CHANGE_PORT 2
122 
127 #define GET_STUN_ERROR_CODE(pClass, pCode) ((UINT16) ((*(PUINT8) (pClass)) * 100 + *(PUINT8) (pCode)))
128 
132 #define PACKAGE_STUN_ATTR_HEADER(pBuf, type, dataLen) \
133  putInt16((PINT16) (pBuf), (UINT16) (type)); \
134  putInt16((PINT16) ((pBuf) + STUN_ATTRIBUTE_HEADER_TYPE_LEN), (UINT16) (dataLen));
135 
139 typedef enum {
142  STUN_PACKET_TYPE_ALLOCATE = (UINT16) 0x0003,
143  STUN_PACKET_TYPE_REFRESH = (UINT16) 0x0004,
144  STUN_PACKET_TYPE_SEND = (UINT16) 0x0006,
145  STUN_PACKET_TYPE_DATA = (UINT16) 0x0007,
164 
165 /*
166  * Taking a PBYTE pointing to a buffer containing stun packet, return whether the stun packet is error packet or not
167  */
168 #define STUN_PACKET_IS_TYPE_ERROR(pPacketBuffer) \
169  ((getInt16(*(PINT16) pPacketBuffer) == STUN_PACKET_TYPE_BINDING_RESPONSE_ERROR) || \
170  (getInt16(*(PINT16) pPacketBuffer) == STUN_PACKET_TYPE_SHARED_SECRET_ERROR_RESPONSE) || \
171  (getInt16(*(PINT16) pPacketBuffer) == STUN_PACKET_TYPE_ALLOCATE_ERROR_RESPONSE) || \
172  (getInt16(*(PINT16) pPacketBuffer) == STUN_PACKET_TYPE_REFRESH_ERROR_RESPONSE) || \
173  (getInt16(*(PINT16) pPacketBuffer) == STUN_PACKET_TYPE_CREATE_PERMISSION_ERROR_RESPONSE) || \
174  (getInt16(*(PINT16) pPacketBuffer) == STUN_PACKET_TYPE_CHANNEL_BIND_ERROR_RESPONSE))
175 
179 typedef enum {
180  STUN_ERROR_UNAUTHORIZED = (UINT16) 401,
181  STUN_ERROR_STALE_NONCE = (UINT16) 438,
183 
187 typedef enum {
193  STUN_ATTRIBUTE_TYPE_USERNAME = (UINT16) 0x0006,
194  STUN_ATTRIBUTE_TYPE_PASSWORD = (UINT16) 0x0007,
200  STUN_ATTRIBUTE_TYPE_PRIORITY = (UINT16) 0x0024,
206  STUN_ATTRIBUTE_TYPE_LIFETIME = (UINT16) 0x000D,
208  STUN_ATTRIBUTE_TYPE_DATA = (UINT16) 0x0013,
209  STUN_ATTRIBUTE_TYPE_REALM = (UINT16) 0x0014,
210  STUN_ATTRIBUTE_TYPE_NONCE = (UINT16) 0x0015,
212  STUN_ATTRIBUTE_TYPE_EVEN_PORT = (UINT16) 0x0018,
216 
218 
229 typedef struct {
232  UINT32 magicCookie;
233  BYTE transactionId[STUN_TRANSACTION_ID_LEN];
235 
236 typedef struct {
237  // Type of the STUN attribute
238  UINT16 type;
239 
240  // Length of the value
241  UINT16 length;
243 
244 typedef struct {
248 
249 typedef struct {
250  // Encapsulating the attribute header
252 
253  // Padded with 0 - 3 bytes to be 32-bit aligned
254  UINT16 paddedLength;
255 
256  // NOTE: User name which might or might not be NULL terminated will follow the attribute header
257  // NOTE: This will contain the padded bits as well
258  PCHAR userName;
260 
261 typedef struct {
265 
266 typedef struct {
268  UINT32 priority;
270 
271 typedef struct {
274 
275 typedef struct {
277  BYTE messageIntegrity[STUN_HMAC_VALUE_LEN];
279 
280 typedef struct {
282  UINT32 lifetime;
284 
285 typedef struct {
287  BYTE protocol[4];
289 
290 typedef struct {
291  // Encapsulating the attribute header
293 
294  // Padded with 0 - 3 bytes to be 32-bit aligned
295  UINT16 paddedLength;
296 
297  // NOTE: User name which might or might not be NULL terminated will follow the attribute header
298  // NOTE: This will contain the padded bits as well
299  PCHAR realm;
301 
302 typedef struct {
304 
305  // Padded with 0 - 3 bytes to be 32-bit aligned
306  UINT16 paddedLength;
307 
308  PBYTE nonce;
310 
311 typedef struct {
313 
314  UINT16 errorCode;
315 
316  // Padded with 0 - 3 bytes to be 32-bit aligned
317  UINT16 paddedLength;
318 
319  PCHAR errorPhrase;
321 
322 typedef struct {
324 
325  UINT64 tieBreaker;
327 
328 typedef struct {
330 
331  // Padded with 0 - 3 bytes to be multiple of 4
332  UINT16 paddedLength;
333 
334  PBYTE data;
336 
337 typedef struct {
339 
341 
342  UINT16 reserve;
344 
345 typedef struct {
347 
348  /* only two bit of changeFlag is used. 0x00000002 means change ip. 0x00000004 means change port */
349  UINT32 changeFlag;
351 
357 typedef struct {
358  // Stun header
360 
361  // Number of attributes in the list
363 
364  // The entire structure allocation size
366 
367  // Stun attributes
370 
371 STATUS serializeStunPacket(PStunPacket, PBYTE, UINT32, BOOL, BOOL, PBYTE, PUINT32);
372 STATUS deserializeStunPacket(PBYTE, UINT32, PBYTE, UINT32, PStunPacket*);
373 STATUS freeStunPacket(PStunPacket*);
382 STATUS appendStunNonceAttribute(PStunPacket, PBYTE, UINT16);
383 STATUS updateStunNonceAttribute(PStunPacket, PBYTE, UINT16);
384 STATUS appendStunErrorCodeAttribute(PStunPacket, PCHAR, UINT16);
386 STATUS appendStunDataAttribute(PStunPacket, PBYTE, UINT16);
389 
396 
400 STATUS xorIpAddress(PKvsIpAddress, PBYTE);
401 //
402 // Internal functions
403 //
407 
408 #ifdef __cplusplus
409 }
410 #endif
411 #endif /* __KINESIS_VIDEO_WEBRTC_CLIENT_STUN_PACKAGER__ */
struct StunAttributeFingerprint * PStunAttributeFingerprint
STUN_ERROR_CODE
Definition: Stun.h:179
@ STUN_ERROR_STALE_NONCE
Definition: Stun.h:181
@ STUN_ERROR_UNAUTHORIZED
Definition: Stun.h:180
STATUS appendStunUsernameAttribute(PStunPacket, PCHAR)
Definition: Stun.c:1159
STATUS appendStunChannelNumberAttribute(PStunPacket, UINT16)
Definition: Stun.c:1650
STUN_ATTRIBUTE_TYPE
Definition: Stun.h:187
@ STUN_ATTRIBUTE_TYPE_ICE_CONTROLLING
Definition: Stun.h:204
@ STUN_ATTRIBUTE_TYPE_XOR_MAPPED_ADDRESS
Definition: Stun.h:199
@ STUN_ATTRIBUTE_TYPE_USE_CANDIDATE
Definition: Stun.h:201
@ STUN_ATTRIBUTE_TYPE_DONT_FRAGMENT
Definition: Stun.h:214
@ STUN_ATTRIBUTE_TYPE_EVEN_PORT
Definition: Stun.h:212
@ STUN_ATTRIBUTE_TYPE_ERROR_CODE
Definition: Stun.h:196
@ STUN_ATTRIBUTE_TYPE_NONCE
Definition: Stun.h:210
@ STUN_ATTRIBUTE_TYPE_PASSWORD
Definition: Stun.h:194
@ STUN_ATTRIBUTE_TYPE_RESPONSE_ADDRESS
Definition: Stun.h:189
@ STUN_ATTRIBUTE_TYPE_REFLECTED_FROM
Definition: Stun.h:198
@ STUN_ATTRIBUTE_TYPE_REALM
Definition: Stun.h:209
@ STUN_ATTRIBUTE_TYPE_CHANGED_ADDRESS
Definition: Stun.h:192
@ STUN_ATTRIBUTE_TYPE_UNKNOWN_ATTRIBUTES
Definition: Stun.h:197
@ STUN_ATTRIBUTE_TYPE_LIFETIME
Definition: Stun.h:206
@ STUN_ATTRIBUTE_TYPE_REQUESTED_TRANSPORT
Definition: Stun.h:213
@ STUN_ATTRIBUTE_TYPE_XOR_RELAYED_ADDRESS
Definition: Stun.h:211
@ STUN_ATTRIBUTE_TYPE_DATA
Definition: Stun.h:208
@ STUN_ATTRIBUTE_TYPE_CHANNEL_NUMBER
Definition: Stun.h:205
@ STUN_ATTRIBUTE_TYPE_XOR_PEER_ADDRESS
Definition: Stun.h:207
@ STUN_ATTRIBUTE_TYPE_USERNAME
Definition: Stun.h:193
@ STUN_ATTRIBUTE_TYPE_MAPPED_ADDRESS
Definition: Stun.h:188
@ STUN_ATTRIBUTE_TYPE_SOURCE_ADDRESS
Definition: Stun.h:191
@ STUN_ATTRIBUTE_TYPE_PRIORITY
Definition: Stun.h:200
@ STUN_ATTRIBUTE_TYPE_RESERVATION_TOKEN
Definition: Stun.h:215
@ STUN_ATTRIBUTE_TYPE_MESSAGE_INTEGRITY
Definition: Stun.h:195
@ STUN_ATTRIBUTE_TYPE_FINGERPRINT
Definition: Stun.h:202
@ STUN_ATTRIBUTE_TYPE_CHANGE_REQUEST
Definition: Stun.h:190
@ STUN_ATTRIBUTE_TYPE_ICE_CONTROLLED
Definition: Stun.h:203
struct StunAttributeRequestedTransport * PStunAttributeRequestedTransport
STATUS xorIpAddress(PKvsIpAddress, PBYTE)
Definition: Stun.c:1229
STATUS freeStunPacket(PStunPacket *)
Definition: Stun.c:1066
struct StunAttributeIceControl * PStunAttributeIceControl
STATUS appendStunIceControllAttribute(PStunPacket, STUN_ATTRIBUTE_TYPE, UINT64)
Definition: Stun.c:1574
struct StunAttributeAddress * PStunAttributeAddress
STATUS appendStunRealmAttribute(PStunPacket, PCHAR)
Definition: Stun.c:1416
struct StunAttributeData * PStunAttributeData
struct StunAttributeFlag * PStunAttributeFlag
struct StunAttributeNonce * PStunAttributeNonce
struct StunAttributeHeader * PStunAttributeHeader
struct StunAttributePriority * PStunAttributePriority
STATUS serializeStunPacket(PStunPacket, PBYTE, UINT32, BOOL, BOOL, PBYTE, PUINT32)
Definition: Stun.c:70
STATUS appendStunFlagAttribute(PStunPacket, STUN_ATTRIBUTE_TYPE)
Definition: Stun.c:1291
struct StunAttributeChangeRequest * PStunAttributeChangeRequest
STATUS appendStunNonceAttribute(PStunPacket, PBYTE, UINT16)
Definition: Stun.c:1459
#define STUN_HMAC_VALUE_LEN
Definition: Stun.h:88
STATUS updateStunNonceAttribute(PStunPacket, PBYTE, UINT16)
Definition: Stun.c:1501
STATUS getFirstAvailableStunAttribute(PStunPacket, PStunAttributeHeader *)
Definition: Stun.c:1742
struct StunPacket * PStunPacket
struct StunHeader * PStunHeader
UINT16 getPackagedStunAttributeSize(PStunAttributeHeader)
Definition: Stun.c:1683
STATUS appendStunErrorCodeAttribute(PStunPacket, PCHAR, UINT16)
Definition: Stun.c:1529
STATUS getStunAttribute(PStunPacket, STUN_ATTRIBUTE_TYPE, PStunAttributeHeader *)
Definition: Stun.c:1204
struct StunAttributeMessageIntegrity * PStunAttributeMessageIntegrity
STATUS appendStunAddressAttribute(PStunPacket, STUN_ATTRIBUTE_TYPE, PKvsIpAddress)
Definition: Stun.c:1126
STATUS appendStunChangeRequestAttribute(PStunPacket, UINT32)
Definition: Stun.c:1351
STATUS appendStunPriorityAttribute(PStunPacket, UINT32)
Definition: Stun.c:1259
struct StunAttributeRealm * PStunAttributeRealm
STUN_PACKET_TYPE
Definition: Stun.h:139
@ STUN_PACKET_TYPE_REFRESH_SUCCESS_RESPONSE
Definition: Stun.h:154
@ STUN_PACKET_TYPE_ALLOCATE_ERROR_RESPONSE
Definition: Stun.h:159
@ STUN_PACKET_TYPE_BINDING_INDICATION
Definition: Stun.h:148
@ STUN_PACKET_TYPE_SEND_INDICATION
Definition: Stun.h:149
@ STUN_PACKET_TYPE_BINDING_RESPONSE_ERROR
Definition: Stun.h:157
@ STUN_PACKET_TYPE_CREATE_PERMISSION_ERROR_RESPONSE
Definition: Stun.h:161
@ STUN_PACKET_TYPE_ALLOCATE_SUCCESS_RESPONSE
Definition: Stun.h:153
@ STUN_PACKET_TYPE_SHARED_SECRET_RESPONSE
Definition: Stun.h:152
@ STUN_PACKET_TYPE_SEND
Definition: Stun.h:144
@ STUN_PACKET_TYPE_SHARED_SECRET_REQUEST
Definition: Stun.h:141
@ STUN_PACKET_TYPE_DATA_INDICATION
Definition: Stun.h:150
@ STUN_PACKET_TYPE_ALLOCATE
Definition: Stun.h:142
@ STUN_PACKET_TYPE_DATA
Definition: Stun.h:145
@ STUN_PACKET_TYPE_REFRESH
Definition: Stun.h:143
@ STUN_PACKET_TYPE_SHARED_SECRET_ERROR_RESPONSE
Definition: Stun.h:158
@ STUN_PACKET_TYPE_CHANNEL_BIND_ERROR_RESPONSE
Definition: Stun.h:162
@ STUN_PACKET_TYPE_BINDING_REQUEST
Definition: Stun.h:140
@ STUN_PACKET_TYPE_CREATE_PERMISSION
Definition: Stun.h:146
@ STUN_PACKET_TYPE_CHANNEL_BIND_REQUEST
Definition: Stun.h:147
@ STUN_PACKET_TYPE_BINDING_RESPONSE_SUCCESS
Definition: Stun.h:151
@ STUN_PACKET_TYPE_CREATE_PERMISSION_SUCCESS_RESPONSE
Definition: Stun.h:155
@ STUN_PACKET_TYPE_CHANNEL_BIND_SUCCESS_RESPONSE
Definition: Stun.h:156
@ STUN_PACKET_TYPE_REFRESH_ERROR_RESPONSE
Definition: Stun.h:160
STATUS stunPackageIpAddr(PStunHeader, STUN_ATTRIBUTE_TYPE, PKvsIpAddress, PBYTE, PUINT32)
Definition: Stun.c:4
STATUS appendStunDataAttribute(PStunPacket, PBYTE, UINT16)
Definition: Stun.c:1608
struct StunAttributeChannelNumber * PStunAttributeChannelNumber
STATUS createStunPacket(STUN_PACKET_TYPE, PBYTE, PStunPacket *)
Definition: Stun.c:1081
struct StunAttributeErrorCode * PStunAttributeErrorCode
STATUS appendStunRequestedTransportAttribute(PStunPacket, UINT8)
Definition: Stun.c:1383
struct StunAttributeUsername * PStunAttributeUsername
STATUS deserializeStunPacket(PBYTE, UINT32, PBYTE, UINT32, PStunPacket *)
Definition: Stun.c:517
struct StunAttributeLifetime * PStunAttributeLifetime
#define STUN_TRANSACTION_ID_LEN
Definition: Stun.h:83
STATUS appendStunLifetimeAttribute(PStunPacket, UINT32)
Definition: Stun.c:1319
Definition: Include_i.h:99
Definition: Stun.h:244
StunAttributeHeader attribute
Definition: Stun.h:245
KvsIpAddress address
Definition: Stun.h:246
Definition: Stun.h:345
StunAttributeHeader attribute
Definition: Stun.h:346
UINT32 changeFlag
Definition: Stun.h:349
Definition: Stun.h:337
StunAttributeHeader attribute
Definition: Stun.h:338
UINT16 channelNumber
Definition: Stun.h:340
UINT16 reserve
Definition: Stun.h:342
Definition: Stun.h:328
PBYTE data
Definition: Stun.h:334
UINT16 paddedLength
Definition: Stun.h:332
StunAttributeHeader attribute
Definition: Stun.h:329
Definition: Stun.h:311
UINT16 paddedLength
Definition: Stun.h:317
StunAttributeHeader attribute
Definition: Stun.h:312
PCHAR errorPhrase
Definition: Stun.h:319
UINT16 errorCode
Definition: Stun.h:314
Definition: Stun.h:261
StunAttributeHeader attribute
Definition: Stun.h:262
UINT32 crc32Fingerprint
Definition: Stun.h:263
Definition: Stun.h:271
StunAttributeHeader attribute
Definition: Stun.h:272
Definition: Stun.h:236
UINT16 type
Definition: Stun.h:238
UINT16 length
Definition: Stun.h:241
Definition: Stun.h:322
UINT64 tieBreaker
Definition: Stun.h:325
StunAttributeHeader attribute
Definition: Stun.h:323
Definition: Stun.h:280
UINT32 lifetime
Definition: Stun.h:282
StunAttributeHeader attribute
Definition: Stun.h:281
Definition: Stun.h:275
StunAttributeHeader attribute
Definition: Stun.h:276
Definition: Stun.h:302
UINT16 paddedLength
Definition: Stun.h:306
PBYTE nonce
Definition: Stun.h:308
StunAttributeHeader attribute
Definition: Stun.h:303
Definition: Stun.h:266
StunAttributeHeader attribute
Definition: Stun.h:267
UINT32 priority
Definition: Stun.h:268
Definition: Stun.h:290
PCHAR realm
Definition: Stun.h:299
UINT16 paddedLength
Definition: Stun.h:295
StunAttributeHeader attribute
Definition: Stun.h:292
Definition: Stun.h:285
StunAttributeHeader attribute
Definition: Stun.h:286
Definition: Stun.h:249
PCHAR userName
Definition: Stun.h:258
StunAttributeHeader attribute
Definition: Stun.h:251
UINT16 paddedLength
Definition: Stun.h:254
Definition: Stun.h:229
UINT16 messageLength
Definition: Stun.h:231
UINT16 stunMessageType
Definition: Stun.h:230
UINT32 magicCookie
Definition: Stun.h:232
Definition: Stun.h:357
StunHeader header
Definition: Stun.h:359
UINT32 allocationSize
Definition: Stun.h:365
UINT32 attributesCount
Definition: Stun.h:362
PStunAttributeHeader * attributeList
Definition: Stun.h:368