socketconfig.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are
  15. met:
  16. * Redistributions of source code must retain the above
  17. copyright notice, this list of conditions and the
  18. following disclaimer.
  19. * Redistributions in binary form must reproduce the
  20. above copyright notice, this list of conditions
  21. and the following disclaimer in the documentation
  22. and/or other materials provided with the distribution.
  23. * Neither the name of the University of Illinois
  24. nor the names of its contributors may be used to
  25. endorse or promote products derived from this
  26. software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *****************************************************************************/
  39. /*****************************************************************************
  40. written by
  41. Haivision Systems Inc.
  42. *****************************************************************************/
  43. #ifndef INC_SRT_SOCKETCONFIG_H
  44. #define INC_SRT_SOCKETCONFIG_H
  45. #include "platform_sys.h"
  46. #ifdef SRT_ENABLE_BINDTODEVICE
  47. #include <linux/if.h>
  48. #endif
  49. #include <string>
  50. #include "haicrypt.h"
  51. #include "congctl.h"
  52. #include "packet.h"
  53. #include "handshake.h"
  54. #include "logger_defs.h"
  55. #include "packetfilter.h"
  56. // SRT Version constants
  57. #define SRT_VERSION_UNK 0
  58. #define SRT_VERSION_MAJ1 0x010000 /* Version 1 major */
  59. #define SRT_VERSION_MAJ(v) (0xFF0000 & (v)) /* Major number ensuring backward compatibility */
  60. #define SRT_VERSION_MIN(v) (0x00FF00 & (v))
  61. #define SRT_VERSION_PCH(v) (0x0000FF & (v))
  62. // NOTE: SRT_VERSION is primarily defined in the build file.
  63. extern const int32_t SRT_DEF_VERSION;
  64. namespace srt
  65. {
  66. struct CSrtMuxerConfig
  67. {
  68. static const int DEF_UDP_BUFFER_SIZE = 65536;
  69. int iIpTTL;
  70. int iIpToS;
  71. int iIpV6Only; // IPV6_V6ONLY option (-1 if not set)
  72. bool bReuseAddr; // reuse an exiting port or not, for UDP multiplexer
  73. #ifdef SRT_ENABLE_BINDTODEVICE
  74. std::string sBindToDevice;
  75. #endif
  76. int iUDPSndBufSize; // UDP sending buffer size
  77. int iUDPRcvBufSize; // UDP receiving buffer size
  78. // NOTE: this operator is not reversable. The syntax must use:
  79. // muxer_entry == socket_entry
  80. bool isCompatWith(const CSrtMuxerConfig& other) const
  81. {
  82. #define CEQUAL(field) (field == other.field)
  83. return CEQUAL(iIpTTL)
  84. && CEQUAL(iIpToS)
  85. && CEQUAL(bReuseAddr)
  86. #ifdef SRT_ENABLE_BINDTODEVICE
  87. && CEQUAL(sBindToDevice)
  88. #endif
  89. && CEQUAL(iUDPSndBufSize)
  90. && CEQUAL(iUDPRcvBufSize)
  91. && (other.iIpV6Only == -1 || CEQUAL(iIpV6Only))
  92. // NOTE: iIpV6Only is not regarded because
  93. // this matches only in case of IPv6 with "any" address.
  94. // And this aspect must be checked separately because here
  95. // this procedure has no access to neither the address,
  96. // nor the IP version (family).
  97. #undef CEQUAL
  98. && true;
  99. }
  100. CSrtMuxerConfig()
  101. : iIpTTL(-1) /* IPv4 TTL or IPv6 HOPs [1..255] (-1:undefined) */
  102. , iIpToS(-1) /* IPv4 Type of Service or IPv6 Traffic Class [0x00..0xff] (-1:undefined) */
  103. , iIpV6Only(-1)
  104. , bReuseAddr(true) // This is default in SRT
  105. , iUDPSndBufSize(DEF_UDP_BUFFER_SIZE)
  106. , iUDPRcvBufSize(DEF_UDP_BUFFER_SIZE)
  107. {
  108. }
  109. };
  110. struct CSrtConfig;
  111. template <size_t SIZE>
  112. class StringStorage
  113. {
  114. char stor[SIZE + 1];
  115. uint16_t len;
  116. // NOTE: default copying allowed.
  117. public:
  118. StringStorage()
  119. : len(0)
  120. {
  121. memset(stor, 0, sizeof stor);
  122. }
  123. bool set(const char* s, size_t length)
  124. {
  125. if (length > SIZE)
  126. return false;
  127. memcpy(stor, s, length);
  128. stor[length] = 0;
  129. len = (int) length;
  130. return true;
  131. }
  132. bool set(const std::string& s)
  133. {
  134. return set(s.c_str(), s.size());
  135. }
  136. size_t copy(char* s, size_t length) const
  137. {
  138. if (!s)
  139. return 0;
  140. size_t copy_len = std::min((size_t)len, length);
  141. memcpy(s, stor, copy_len);
  142. return copy_len;
  143. }
  144. std::string str() const
  145. {
  146. return len == 0 ? std::string() : std::string(stor);
  147. }
  148. const char* c_str() const
  149. {
  150. return stor;
  151. }
  152. size_t size() const { return size_t(len); }
  153. bool empty() const { return len == 0; }
  154. };
  155. struct CSrtConfig: CSrtMuxerConfig
  156. {
  157. typedef srt::sync::steady_clock::time_point time_point;
  158. typedef srt::sync::steady_clock::duration duration;
  159. static const int
  160. DEF_MSS = 1500,
  161. DEF_FLIGHT_SIZE = 25600,
  162. DEF_BUFFER_SIZE = 8192, //Rcv buffer MUST NOT be bigger than Flight Flag size
  163. DEF_LINGER_S = 3*60, // 3 minutes
  164. DEF_CONNTIMEO_S = 3; // 3 seconds
  165. enum
  166. {
  167. CIPHER_MODE_AUTO = 0,
  168. CIPHER_MODE_AES_CTR = 1,
  169. CIPHER_MODE_AES_GCM = 2
  170. };
  171. static const int COMM_RESPONSE_TIMEOUT_MS = 5 * 1000; // 5 seconds
  172. static const uint32_t COMM_DEF_MIN_STABILITY_TIMEOUT_MS = 60; // 60 ms
  173. // Mimimum recv flight flag size is 32 packets
  174. static const int DEF_MIN_FLIGHT_PKT = 32;
  175. static const size_t MAX_SID_LENGTH = 512;
  176. static const size_t MAX_PFILTER_LENGTH = 64;
  177. static const size_t MAX_CONG_LENGTH = 16;
  178. int iMSS; // Maximum Segment Size, in bytes
  179. size_t zExpPayloadSize; // Expected average payload size (user option)
  180. // Options
  181. bool bSynSending; // Sending synchronization mode
  182. bool bSynRecving; // Receiving synchronization mode
  183. int iFlightFlagSize; // Maximum number of packets in flight from the peer side
  184. int iSndBufSize; // Maximum UDT sender buffer size
  185. int iRcvBufSize; // Maximum UDT receiver buffer size
  186. linger Linger; // Linger information on close
  187. bool bRendezvous; // Rendezvous connection mode
  188. duration tdConnTimeOut; // connect timeout in milliseconds
  189. bool bDriftTracer;
  190. int iSndTimeOut; // sending timeout in milliseconds
  191. int iRcvTimeOut; // receiving timeout in milliseconds
  192. int64_t llMaxBW; // maximum data transfer rate (threshold)
  193. #ifdef ENABLE_MAXREXMITBW
  194. int64_t llMaxRexmitBW; // maximum bandwidth limit for retransmissions (Bytes/s).
  195. #endif
  196. // These fields keep the options for encryption
  197. // (SRTO_PASSPHRASE, SRTO_PBKEYLEN). Crypto object is
  198. // created later and takes values from these.
  199. HaiCrypt_Secret CryptoSecret;
  200. int iSndCryptoKeyLen;
  201. // XXX Consider removing. The bDataSender stays here
  202. // in order to maintain the HS side selection in HSv4.
  203. bool bDataSender;
  204. bool bMessageAPI;
  205. bool bTSBPD; // Whether AGENT will do TSBPD Rx (whether peer does, is not agent's problem)
  206. int iRcvLatency; // Agent's Rx latency
  207. int iPeerLatency; // Peer's Rx latency for the traffic made by Agent's Tx.
  208. bool bTLPktDrop; // Whether Agent WILL DO TLPKTDROP on Rx.
  209. int iSndDropDelay; // Extra delay when deciding to snd-drop for TLPKTDROP, -1 to off
  210. bool bEnforcedEnc; // Off by default. When on, any connection other than nopw-nopw & pw1-pw1 is rejected.
  211. int iGroupConnect; // 1 - allow group connections
  212. int iPeerIdleTimeout_ms; // Timeout for hearing anything from the peer (ms).
  213. uint32_t uMinStabilityTimeout_ms;
  214. int iRetransmitAlgo;
  215. int iCryptoMode; // SRTO_CRYPTOMODE
  216. int64_t llInputBW; // Input stream rate (bytes/sec). 0: use internally estimated input bandwidth
  217. int64_t llMinInputBW; // Minimum input stream rate estimate (bytes/sec)
  218. int iOverheadBW; // Percent above input stream rate (applies if llMaxBW == 0)
  219. bool bRcvNakReport; // Enable Receiver Periodic NAK Reports
  220. int iMaxReorderTolerance; //< Maximum allowed value for dynamic reorder tolerance
  221. // For the use of CCryptoControl
  222. // HaiCrypt configuration
  223. unsigned int uKmRefreshRatePkt;
  224. unsigned int uKmPreAnnouncePkt;
  225. uint32_t uSrtVersion;
  226. uint32_t uMinimumPeerSrtVersion;
  227. StringStorage<MAX_CONG_LENGTH> sCongestion;
  228. StringStorage<MAX_PFILTER_LENGTH> sPacketFilterConfig;
  229. StringStorage<MAX_SID_LENGTH> sStreamName;
  230. // Shortcuts and utilities
  231. int32_t flightCapacity()
  232. {
  233. return std::min(iRcvBufSize, iFlightFlagSize);
  234. }
  235. CSrtConfig()
  236. : iMSS(DEF_MSS)
  237. , zExpPayloadSize(SRT_LIVE_DEF_PLSIZE)
  238. , bSynSending(true)
  239. , bSynRecving(true)
  240. , iFlightFlagSize(DEF_FLIGHT_SIZE)
  241. , iSndBufSize(DEF_BUFFER_SIZE)
  242. , iRcvBufSize(DEF_BUFFER_SIZE)
  243. , bRendezvous(false)
  244. , tdConnTimeOut(srt::sync::seconds_from(DEF_CONNTIMEO_S))
  245. , bDriftTracer(true)
  246. , iSndTimeOut(-1)
  247. , iRcvTimeOut(-1)
  248. , llMaxBW(-1)
  249. #ifdef ENABLE_MAXREXMITBW
  250. , llMaxRexmitBW(-1)
  251. #endif
  252. , bDataSender(false)
  253. , bMessageAPI(true)
  254. , bTSBPD(true)
  255. , iRcvLatency(SRT_LIVE_DEF_LATENCY_MS)
  256. , iPeerLatency(0)
  257. , bTLPktDrop(true)
  258. , iSndDropDelay(0)
  259. , bEnforcedEnc(true)
  260. , iGroupConnect(0)
  261. , iPeerIdleTimeout_ms(COMM_RESPONSE_TIMEOUT_MS)
  262. , uMinStabilityTimeout_ms(COMM_DEF_MIN_STABILITY_TIMEOUT_MS)
  263. , iRetransmitAlgo(1)
  264. , iCryptoMode(CIPHER_MODE_AUTO)
  265. , llInputBW(0)
  266. , llMinInputBW(0)
  267. , iOverheadBW(25)
  268. , bRcvNakReport(true)
  269. , iMaxReorderTolerance(0) // Sensible optimal value is 10, 0 preserves old behavior
  270. , uKmRefreshRatePkt(0)
  271. , uKmPreAnnouncePkt(0)
  272. , uSrtVersion(SRT_DEF_VERSION)
  273. , uMinimumPeerSrtVersion(SRT_VERSION_MAJ1)
  274. {
  275. // Default UDT configurations
  276. iUDPRcvBufSize = iRcvBufSize * iMSS;
  277. // Linger: LIVE mode defaults, please refer to `SRTO_TRANSTYPE` option
  278. // for other modes.
  279. Linger.l_onoff = 0;
  280. Linger.l_linger = 0;
  281. CryptoSecret.len = 0;
  282. iSndCryptoKeyLen = 0;
  283. // Default congestion is "live".
  284. // Available builtin congestions: "file".
  285. // Others can be registerred.
  286. sCongestion.set("live", 4);
  287. }
  288. ~CSrtConfig()
  289. {
  290. // Wipeout critical data
  291. memset(&CryptoSecret, 0, sizeof(CryptoSecret));
  292. }
  293. int set(SRT_SOCKOPT optName, const void* val, int size);
  294. };
  295. template <typename T>
  296. inline T cast_optval(const void* optval)
  297. {
  298. return *reinterpret_cast<const T*>(optval);
  299. }
  300. template <typename T>
  301. inline T cast_optval(const void* optval, int optlen)
  302. {
  303. if (optlen > 0 && optlen != sizeof(T))
  304. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  305. return cast_optval<T>(optval);
  306. }
  307. // This function is to make it possible for both C and C++
  308. // API to accept both bool and int types for boolean options.
  309. // (it's not that C couldn't use <stdbool.h>, it's that people
  310. // often forget to use correct type).
  311. template <>
  312. inline bool cast_optval(const void* optval, int optlen)
  313. {
  314. if (optlen == sizeof(bool))
  315. {
  316. return *reinterpret_cast<const bool*>(optval);
  317. }
  318. if (optlen == sizeof(int))
  319. {
  320. // 0!= is a windows warning-killer int-to-bool conversion
  321. return 0 != *reinterpret_cast<const int*>(optval);
  322. }
  323. return false;
  324. }
  325. } // namespace srt
  326. struct SRT_SocketOptionObject
  327. {
  328. struct SingleOption
  329. {
  330. uint16_t option;
  331. uint16_t length;
  332. unsigned char storage[1]; // NOTE: Variable length object!
  333. };
  334. std::vector<SingleOption*> options;
  335. SRT_SocketOptionObject() {}
  336. ~SRT_SocketOptionObject()
  337. {
  338. for (size_t i = 0; i < options.size(); ++i)
  339. {
  340. // Convert back
  341. unsigned char* mem = reinterpret_cast<unsigned char*>(options[i]);
  342. delete[] mem;
  343. }
  344. }
  345. bool add(SRT_SOCKOPT optname, const void* optval, size_t optlen);
  346. };
  347. #endif