Amazon Kinesis Webrtc C SDK
 
Loading...
Searching...
No Matches
Include_i.h
Go to the documentation of this file.
1/*******************************************
2Main internal include file
3*******************************************/
4#ifndef __KINESIS_VIDEO_WEBRTC_CLIENT_INCLUDE_I__
5#define __KINESIS_VIDEO_WEBRTC_CLIENT_INCLUDE_I__
6
7#pragma once
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#ifdef _WIN32
14#include <winsock2.h>
15#include <iphlpapi.h>
16#include <ws2tcpip.h>
17
18#ifndef MSG_NOSIGNAL
19#define MSG_NOSIGNAL 0
20#endif
21
22#endif
23
25// Project include files
28
29#ifdef KVS_USE_OPENSSL
30#include <openssl/bio.h>
31#include <openssl/err.h>
32#include <openssl/hmac.h>
33#include <openssl/md5.h>
34#include <openssl/rand.h>
35#include <openssl/sha.h>
36#include <openssl/ssl.h>
37#elif KVS_USE_MBEDTLS
38/* mbedTLS 4 moved legacy entropy/CTR-DRBG/hash/RSA/ECP/bignum headers under
39 * mbedtls/private/ and gates their prototypes behind
40 * MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS. The symbols still ship in libmbedcrypto,
41 * so we declare the macro before mbedtls/ssl.h to re-expose them across all
42 * includes below. Note: the mbedtls/private/ headers are explicitly unsupported upstream
43 * and may break across v4.x point releases. TODO: migrate to PSA Crypto APIs
44 * (psa_crypto_init + psa_generate_random + psa_hash_compute + psa_mac_compute)
45 * in a follow-up so we can drop this opt-out. */
46/* Nested guard: a bare __has_include(...) in an #if is a syntax error on
47 * pre-C23 / older compilers (e.g. GCC < 5) — the && does not stop the parse.
48 * #ifdef __has_include is safe everywhere; only use the operator when present. */
49#ifdef __has_include
50#if __has_include(<mbedtls/build_info.h>)
51#include <mbedtls/build_info.h>
52#else
53#include <mbedtls/version.h>
54#endif
55#else
56#include <mbedtls/version.h>
57#endif
58#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_MAJOR >= 4
59#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
60#endif
61#include <mbedtls/ssl.h>
62#include <mbedtls/error.h>
63#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_MAJOR >= 4
64#include <mbedtls/private/entropy.h>
65#include <mbedtls/private/ctr_drbg.h>
66#include <mbedtls/md.h>
67#include <mbedtls/pk.h>
68#include <mbedtls/private/rsa.h>
69#include <mbedtls/private/ecp.h>
70#include <mbedtls/private/bignum.h>
71#else
72#include <mbedtls/entropy.h>
73#include <mbedtls/ctr_drbg.h>
74#if MBEDTLS_VERSION_NUMBER < 0x03000000
75#include <mbedtls/certs.h>
76#endif
77#include <mbedtls/md.h>
78#endif
79
80// Only mbedTLS 4 can take the RNG from PSA; before it the sessions seed their own CTR-DRBG from the entropy module
81#if MBEDTLS_VERSION_MAJOR < 4 && !(defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C))
82#error "Building without MBEDTLS_ENTROPY_C/MBEDTLS_CTR_DRBG_C requires mbedTLS 4"
83#endif
84#endif
85
86#ifdef USE_LIBSRTP3
87#include <srtp3/srtp.h>
88#else
89#include <srtp2/srtp.h>
90#endif
91
92// INET/INET6 MUST be defined before usrsctp
93// If removed will cause corruption that is hard to determine at runtime
94#define INET 1
95#define INET6 1
96#include <usrsctp.h>
97
98#if !defined __WINDOWS_BUILD__
99#include <signal.h>
100#include <sys/types.h>
101#ifdef HAVE_IFADDRS_H
102#include <ifaddrs.h>
103#endif
104#include <sys/types.h>
105#include <sys/socket.h>
106#include <netdb.h>
107#include <net/if.h>
108#include <arpa/inet.h>
109#include <fcntl.h>
110#include <netinet/tcp.h>
111#ifdef HAVE_POLL_H
112#include <poll.h>
113#endif
114#endif
115
116// Max uFrag and uPwd length as documented in https://tools.ietf.org/html/rfc5245#section-15.4
117#define ICE_MAX_UFRAG_LEN 256
118#define ICE_MAX_UPWD_LEN 256
119
120// Max stun username attribute len: https://tools.ietf.org/html/rfc5389#section-15.3
121#define STUN_MAX_USERNAME_LEN (UINT16) 512
122
123// https://tools.ietf.org/html/rfc5389#section-15.7
124#define STUN_MAX_REALM_LEN (UINT16) 128
125
126// https://tools.ietf.org/html/rfc5389#section-15.8
127#define STUN_MAX_NONCE_LEN (UINT16) 128
128
129// https://tools.ietf.org/html/rfc5389#section-15.6
130#define STUN_MAX_ERROR_PHRASE_LEN (UINT16) 128
131
132// Byte sizes of the IP addresses
133#define IPV6_ADDRESS_LENGTH (UINT16) 16
134#define IPV4_ADDRESS_LENGTH (UINT16) 4
135
136#define CERTIFICATE_FINGERPRINT_LENGTH 160
137
138#define MAX_UDP_PACKET_SIZE 65507
139
140typedef enum {
141 KVS_IP_FAMILY_TYPE_NOT_SET = (UINT16) 0x0000, // Sentinel value for not yet set IP address.
142 KVS_IP_FAMILY_TYPE_IPV4 = (UINT16) 0x0001,
143 KVS_IP_FAMILY_TYPE_IPV6 = (UINT16) 0x0002,
145
146typedef struct {
147 UINT16 family;
148 UINT16 port; // port is stored in network byte order
149 BYTE address[IPV6_ADDRESS_LENGTH]; // address is stored in network byte order
152
153// This structure stores both an IPv4 and IPv6 address (if applicable).
158
159static inline BOOL IS_IPV4_ADDR(const PKvsIpAddress pAddress)
160{
161 return pAddress != NULL && pAddress->family == KVS_IP_FAMILY_TYPE_IPV4;
162}
163
164static inline BOOL IS_IPV6_ADDR(const PKvsIpAddress pAddress)
165{
166 return pAddress != NULL && pAddress->family == KVS_IP_FAMILY_TYPE_IPV6;
167}
168
169// Returns TRUE for IP addresses that are not globally reachable, per the IANA Special-Purpose
170// Address Registries (RFC 6890 and the RFCs noted per range). A public TURN server (e.g. the
171// KVS TURN service) cannot relay to these and rejects CreatePermission for them with
172// "403 Forbidden IP". Address bytes are in network byte order. Both IPv4 and IPv6 are covered.
173static inline BOOL IS_NON_ROUTABLE_ADDR(const PKvsIpAddress pAddress)
174{
175 BYTE b0, b1, b2;
176
177 if (pAddress == NULL) {
178 return FALSE;
179 }
180
181 // IPv6 non-globally-reachable ranges (address is 16 bytes, network byte order).
182 if (IS_IPV6_ADDR(pAddress)) {
183 PBYTE a = pAddress->address;
184 UINT32 i;
185 BOOL allZeroButLast = TRUE;
186
187 if (a[0] == 0xfe && (a[1] & 0xc0) == 0x80) {
188 return TRUE; // fe80::/10 link-local unicast (RFC 4291)
189 }
190 if ((a[0] & 0xfe) == 0xfc) {
191 return TRUE; // fc00::/7 unique local address / ULA (RFC 4193)
192 }
193 if (a[0] == 0x20 && a[1] == 0x01 && a[2] == 0x0d && a[3] == 0xb8) {
194 return TRUE; // 2001:db8::/32 documentation (RFC 3849)
195 }
196 for (i = 0; i < 15; ++i) {
197 if (a[i] != 0) {
198 allZeroButLast = FALSE;
199 break;
200 }
201 }
202 if (allZeroButLast && (a[15] == 0 || a[15] == 1)) {
203 return TRUE; // ::/128 unspecified and ::1/128 loopback (RFC 4291)
204 }
205
206 return FALSE;
207 }
208
209 // Only IPv4 ranges remain (IS_IPV4_ADDR also handles the NULL check).
210 if (!IS_IPV4_ADDR(pAddress)) {
211 return FALSE;
212 }
213
214 b0 = pAddress->address[0];
215 b1 = pAddress->address[1];
216 b2 = pAddress->address[2];
217
218 if (b0 == 0) {
219 return TRUE; // 0.0.0.0/8 "this host on this network" (RFC 1122)
220 }
221 if (b0 == 10) {
222 return TRUE; // 10.0.0.0/8 private-use (RFC 1918)
223 }
224 if (b0 == 100 && b1 >= 64 && b1 <= 127) {
225 return TRUE; // 100.64.0.0/10 shared address space / CGNAT (RFC 6598)
226 }
227 if (b0 == 127) {
228 return TRUE; // 127.0.0.0/8 loopback (RFC 1122)
229 }
230 if (b0 == 169 && b1 == 254) {
231 return TRUE; // 169.254.0.0/16 link-local (RFC 3927)
232 }
233 if (b0 == 172 && b1 >= 16 && b1 <= 31) {
234 return TRUE; // 172.16.0.0/12 private-use (RFC 1918)
235 }
236 if (b0 == 192 && b1 == 0 && b2 == 0) {
237 return TRUE; // 192.0.0.0/24 IETF protocol assignments (RFC 6890)
238 }
239 if (b0 == 192 && b1 == 0 && b2 == 2) {
240 return TRUE; // 192.0.2.0/24 documentation TEST-NET-1 (RFC 5737)
241 }
242 if (b0 == 192 && b1 == 88 && b2 == 99) {
243 return TRUE; // 192.88.99.0/24 6to4 relay anycast, deprecated (RFC 7526)
244 }
245 if (b0 == 192 && b1 == 168) {
246 return TRUE; // 192.168.0.0/16 private-use (RFC 1918)
247 }
248 if (b0 == 198 && (b1 == 18 || b1 == 19)) {
249 return TRUE; // 198.18.0.0/15 benchmarking (RFC 2544)
250 }
251 if (b0 == 198 && b1 == 51 && b2 == 100) {
252 return TRUE; // 198.51.100.0/24 documentation TEST-NET-2 (RFC 5737)
253 }
254 if (b0 == 203 && b1 == 0 && b2 == 113) {
255 return TRUE; // 203.0.113.0/24 documentation TEST-NET-3 (RFC 5737)
256 }
257 if (b0 >= 240) {
258 return TRUE; // 240.0.0.0/4 reserved / future use, incl. 255.255.255.255 (RFC 1112)
259 }
260
261 return FALSE;
262}
263
264// Used for ensuring alignment
265#define ALIGN_UP_TO_MACHINE_WORD(x) ROUND_UP((x), SIZEOF(SIZE_T))
266
267typedef STATUS (*IceServerSetIpFunc)(UINT64, PCHAR, PDualKvsIpAddresses);
268STATUS getIpAddrStr(PKvsIpAddress pKvsIpAddress, PCHAR pBuffer, UINT32 bufferLen);
269
271// Project forward declarations
273struct __TurnConnection;
274struct __SocketConnection;
275STATUS generateJSONSafeString(PCHAR, UINT32);
276
278// Project internal includes
281#include "Crypto/IOBuffer.h"
282#include "Crypto/Crypto.h"
283#include "Crypto/Dtls.h"
284#include "Crypto/Tls.h"
285#include "Ice/Network.h"
286#include "Ice/SocketConnection.h"
288#include "Stun/Stun.h"
289#include "Ice/IceUtils.h"
290#include "Sdp/Sdp.h"
291#include "Ice/IceAgent.h"
292#include "Ice/TurnConnection.h"
296#include "Srtp/SrtpSession.h"
297#include "Sctp/Sctp.h"
298#include "Signaling/FileCache.h"
299#include "Signaling/Signaling.h"
303#include "Rtp/RtpPacket.h"
304#include "Rtcp/RtcpPacket.h"
305#include "Rtcp/RollingBuffer.h"
311#include "PeerConnection/Rtp.h"
312#include "PeerConnection/Rtcp.h"
319#include "Metrics/Metrics.h"
320
322// Project internal defines
324
326// Project internal functions
328
329#define KVS_CONVERT_TIMESCALE(pts, from_timescale, to_timescale) (pts * to_timescale / from_timescale)
330
331#ifdef __cplusplus
332}
333#endif
334#endif /* __KINESIS_VIDEO_WEBRTC_CLIENT_INCLUDE_I__ */
KVS_IP_FAMILY_TYPE
Definition Include_i.h:140
@ KVS_IP_FAMILY_TYPE_IPV4
Definition Include_i.h:142
@ KVS_IP_FAMILY_TYPE_NOT_SET
Definition Include_i.h:141
@ KVS_IP_FAMILY_TYPE_IPV6
Definition Include_i.h:143
#define IPV6_ADDRESS_LENGTH
Definition Include_i.h:133
STATUS generateJSONSafeString(PCHAR, UINT32)
Definition PeerConnection.c:715
struct DualKvsIpAddresses * PDualKvsIpAddresses
struct KvsIpAddress * PKvsIpAddress
STATUS getIpAddrStr(PKvsIpAddress pKvsIpAddress, PCHAR pBuffer, UINT32 bufferLen)
Definition Network.c:636
STATUS(* IceServerSetIpFunc)(UINT64, PCHAR, PDualKvsIpAddresses)
Definition Include_i.h:267
Definition Include_i.h:154
KvsIpAddress ipv4Address
Definition Include_i.h:155
KvsIpAddress ipv6Address
Definition Include_i.h:156
Definition Include_i.h:146
UINT16 port
Definition Include_i.h:148
BYTE address[(UINT16) 16]
Definition Include_i.h:149
BOOL isPointToPoint
Definition Include_i.h:150
UINT16 family
Definition Include_i.h:147
Definition SocketConnection.h:25
Definition TurnConnection.h:119