socketconfig.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. #include <utility>
  44. #include "srt.h"
  45. #include "socketconfig.h"
  46. using namespace srt;
  47. extern const int32_t SRT_DEF_VERSION = SrtParseVersion(SRT_VERSION);
  48. namespace {
  49. typedef void setter_function(CSrtConfig& co, const void* optval, int optlen);
  50. template<SRT_SOCKOPT name>
  51. struct CSrtConfigSetter
  52. {
  53. static setter_function set;
  54. };
  55. template<>
  56. struct CSrtConfigSetter<SRTO_MSS>
  57. {
  58. static void set(CSrtConfig& co, const void* optval, int optlen)
  59. {
  60. const int ival = cast_optval<int>(optval, optlen);
  61. if (ival < int(CPacket::UDP_HDR_SIZE + CHandShake::m_iContentSize))
  62. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  63. co.iMSS = ival;
  64. // Packet size cannot be greater than UDP buffer size
  65. if (co.iMSS > co.iUDPSndBufSize)
  66. co.iMSS = co.iUDPSndBufSize;
  67. if (co.iMSS > co.iUDPRcvBufSize)
  68. co.iMSS = co.iUDPRcvBufSize;
  69. }
  70. };
  71. template<>
  72. struct CSrtConfigSetter<SRTO_FC>
  73. {
  74. static void set(CSrtConfig& co, const void* optval, int optlen)
  75. {
  76. using namespace srt_logging;
  77. const int fc = cast_optval<int>(optval, optlen);
  78. if (fc < co.DEF_MIN_FLIGHT_PKT)
  79. {
  80. LOGC(kmlog.Error, log << "SRTO_FC: minimum allowed value is 32 (provided: " << fc << ")");
  81. throw CUDTException(MJ_NOTSUP, MN_INVAL);
  82. }
  83. co.iFlightFlagSize = fc;
  84. }
  85. };
  86. template<>
  87. struct CSrtConfigSetter<SRTO_SNDBUF>
  88. {
  89. static void set(CSrtConfig& co, const void* optval, int optlen)
  90. {
  91. int bs = cast_optval<int>(optval, optlen);
  92. if (bs <= 0)
  93. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  94. co.iSndBufSize = bs / (co.iMSS - CPacket::UDP_HDR_SIZE);
  95. }
  96. };
  97. template<>
  98. struct CSrtConfigSetter<SRTO_RCVBUF>
  99. {
  100. static void set(CSrtConfig& co, const void* optval, int optlen)
  101. {
  102. const int val = cast_optval<int>(optval, optlen);
  103. if (val <= 0)
  104. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  105. // Mimimum recv buffer size is 32 packets
  106. const int mssin_size = co.iMSS - CPacket::UDP_HDR_SIZE;
  107. if (val > mssin_size * co.DEF_MIN_FLIGHT_PKT)
  108. co.iRcvBufSize = val / mssin_size;
  109. else
  110. co.iRcvBufSize = co.DEF_MIN_FLIGHT_PKT;
  111. // recv buffer MUST not be greater than FC size
  112. if (co.iRcvBufSize > co.iFlightFlagSize)
  113. co.iRcvBufSize = co.iFlightFlagSize;
  114. }
  115. };
  116. template<>
  117. struct CSrtConfigSetter<SRTO_LINGER>
  118. {
  119. static void set(CSrtConfig& co, const void* optval, int optlen)
  120. {
  121. co.Linger = cast_optval<linger>(optval, optlen);
  122. }
  123. };
  124. template<>
  125. struct CSrtConfigSetter<SRTO_UDP_SNDBUF>
  126. {
  127. static void set(CSrtConfig& co, const void* optval, int optlen)
  128. {
  129. co.iUDPSndBufSize = std::max(co.iMSS, cast_optval<int>(optval, optlen));
  130. }
  131. };
  132. template<>
  133. struct CSrtConfigSetter<SRTO_UDP_RCVBUF>
  134. {
  135. static void set(CSrtConfig& co, const void* optval, int optlen)
  136. {
  137. co.iUDPRcvBufSize = std::max(co.iMSS, cast_optval<int>(optval, optlen));
  138. }
  139. };
  140. template<>
  141. struct CSrtConfigSetter<SRTO_RENDEZVOUS>
  142. {
  143. static void set(CSrtConfig& co, const void* optval, int optlen)
  144. {
  145. co.bRendezvous = cast_optval<bool>(optval, optlen);
  146. }
  147. };
  148. template<>
  149. struct CSrtConfigSetter<SRTO_SNDTIMEO>
  150. {
  151. static void set(CSrtConfig& co, const void* optval, int optlen)
  152. {
  153. const int val = cast_optval<int>(optval, optlen);
  154. if (val < -1)
  155. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  156. co.iSndTimeOut = val;
  157. }
  158. };
  159. template<>
  160. struct CSrtConfigSetter<SRTO_RCVTIMEO>
  161. {
  162. static void set(CSrtConfig& co, const void* optval, int optlen)
  163. {
  164. const int val = cast_optval<int>(optval, optlen);
  165. if (val < -1)
  166. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  167. co.iRcvTimeOut = val;
  168. }
  169. };
  170. template<>
  171. struct CSrtConfigSetter<SRTO_SNDSYN>
  172. {
  173. static void set(CSrtConfig& co, const void* optval, int optlen)
  174. {
  175. co.bSynSending = cast_optval<bool>(optval, optlen);
  176. }
  177. };
  178. template<>
  179. struct CSrtConfigSetter<SRTO_RCVSYN>
  180. {
  181. static void set(CSrtConfig& co, const void* optval, int optlen)
  182. {
  183. co.bSynRecving = cast_optval<bool>(optval, optlen);
  184. }
  185. };
  186. template<>
  187. struct CSrtConfigSetter<SRTO_REUSEADDR>
  188. {
  189. static void set(CSrtConfig& co, const void* optval, int optlen)
  190. {
  191. co.bReuseAddr = cast_optval<bool>(optval, optlen);
  192. }
  193. };
  194. template<>
  195. struct CSrtConfigSetter<SRTO_MAXBW>
  196. {
  197. static void set(CSrtConfig& co, const void* optval, int optlen)
  198. {
  199. const int64_t val = cast_optval<int64_t>(optval, optlen);
  200. if (val < -1)
  201. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  202. co.llMaxBW = val;
  203. }
  204. };
  205. #ifdef ENABLE_MAXREXMITBW
  206. template<>
  207. struct CSrtConfigSetter<SRTO_MAXREXMITBW>
  208. {
  209. static void set(CSrtConfig& co, const void* optval, int optlen)
  210. {
  211. const int64_t val = cast_optval<int64_t>(optval, optlen);
  212. if (val < -1)
  213. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  214. co.llMaxRexmitBW = val;
  215. }
  216. };
  217. #endif
  218. template<>
  219. struct CSrtConfigSetter<SRTO_IPTTL>
  220. {
  221. static void set(CSrtConfig& co, const void* optval, int optlen)
  222. {
  223. int val = cast_optval<int>(optval, optlen);
  224. if (!(val == -1) && !((val >= 1) && (val <= 255)))
  225. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  226. co.iIpTTL = cast_optval<int>(optval);
  227. }
  228. };
  229. template<>
  230. struct CSrtConfigSetter<SRTO_IPTOS>
  231. {
  232. static void set(CSrtConfig& co, const void* optval, int optlen)
  233. {
  234. co.iIpToS = cast_optval<int>(optval, optlen);
  235. }
  236. };
  237. template<>
  238. struct CSrtConfigSetter<SRTO_BINDTODEVICE>
  239. {
  240. static void set(CSrtConfig& co, const void* optval, int optlen)
  241. {
  242. using namespace srt_logging;
  243. #ifdef SRT_ENABLE_BINDTODEVICE
  244. using namespace std;
  245. string val;
  246. if (optlen == -1)
  247. val = (const char *)optval;
  248. else
  249. val.assign((const char *)optval, optlen);
  250. if (val.size() >= IFNAMSIZ)
  251. {
  252. LOGC(kmlog.Error, log << "SRTO_BINDTODEVICE: device name too long (max: IFNAMSIZ=" << IFNAMSIZ << ")");
  253. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  254. }
  255. co.sBindToDevice = val;
  256. #else
  257. (void)co; // prevent warning
  258. (void)optval;
  259. (void)optlen;
  260. LOGC(kmlog.Error, log << "SRTO_BINDTODEVICE is not supported on that platform");
  261. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  262. #endif
  263. }
  264. };
  265. template<>
  266. struct CSrtConfigSetter<SRTO_INPUTBW>
  267. {
  268. static void set(CSrtConfig& co, const void* optval, int optlen)
  269. {
  270. const int64_t val = cast_optval<int64_t>(optval, optlen);
  271. if (val < 0)
  272. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  273. co.llInputBW = val;
  274. }
  275. };
  276. template<>
  277. struct CSrtConfigSetter<SRTO_MININPUTBW>
  278. {
  279. static void set(CSrtConfig& co, const void* optval, int optlen)
  280. {
  281. const int64_t val = cast_optval<int64_t>(optval, optlen);
  282. if (val < 0)
  283. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  284. co.llMinInputBW = val;
  285. }
  286. };
  287. template<>
  288. struct CSrtConfigSetter<SRTO_OHEADBW>
  289. {
  290. static void set(CSrtConfig& co, const void* optval, int optlen)
  291. {
  292. const int32_t val = cast_optval<int32_t>(optval, optlen);
  293. if (val < 5 || val > 100)
  294. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  295. co.iOverheadBW = val;
  296. }
  297. };
  298. template<>
  299. struct CSrtConfigSetter<SRTO_SENDER>
  300. {
  301. static void set(CSrtConfig& co, const void* optval, int optlen)
  302. {
  303. co.bDataSender = cast_optval<bool>(optval, optlen);
  304. }
  305. };
  306. template<>
  307. struct CSrtConfigSetter<SRTO_TSBPDMODE>
  308. {
  309. static void set(CSrtConfig& co, const void* optval, int optlen)
  310. {
  311. const bool val = cast_optval<bool>(optval, optlen);
  312. #ifdef SRT_ENABLE_ENCRYPTION
  313. if (val == false && co.iCryptoMode == CSrtConfig::CIPHER_MODE_AES_GCM)
  314. {
  315. using namespace srt_logging;
  316. LOGC(aclog.Error, log << "Can't disable TSBPD as long as AES GCM is enabled.");
  317. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  318. }
  319. #endif
  320. co.bTSBPD = val;
  321. }
  322. };
  323. template<>
  324. struct CSrtConfigSetter<SRTO_LATENCY>
  325. {
  326. static void set(CSrtConfig& co, const void* optval, int optlen)
  327. {
  328. const int val = cast_optval<int>(optval, optlen);
  329. if (val < 0)
  330. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  331. co.iRcvLatency = val;
  332. co.iPeerLatency = val;
  333. }
  334. };
  335. template<>
  336. struct CSrtConfigSetter<SRTO_RCVLATENCY>
  337. {
  338. static void set(CSrtConfig& co, const void* optval, int optlen)
  339. {
  340. const int val = cast_optval<int>(optval, optlen);
  341. if (val < 0)
  342. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  343. co.iRcvLatency = val;
  344. }
  345. };
  346. template<>
  347. struct CSrtConfigSetter<SRTO_PEERLATENCY>
  348. {
  349. static void set(CSrtConfig& co, const void* optval, int optlen)
  350. {
  351. const int val = cast_optval<int>(optval, optlen);
  352. if (val < 0)
  353. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  354. co.iPeerLatency = val;
  355. }
  356. };
  357. template<>
  358. struct CSrtConfigSetter<SRTO_TLPKTDROP>
  359. {
  360. static void set(CSrtConfig& co, const void* optval, int optlen)
  361. {
  362. co.bTLPktDrop = cast_optval<bool>(optval, optlen);
  363. }
  364. };
  365. template<>
  366. struct CSrtConfigSetter<SRTO_SNDDROPDELAY>
  367. {
  368. static void set(CSrtConfig& co, const void* optval, int optlen)
  369. {
  370. const int val = cast_optval<int>(optval, optlen);
  371. if (val < -1)
  372. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  373. co.iSndDropDelay = val;
  374. }
  375. };
  376. template<>
  377. struct CSrtConfigSetter<SRTO_PASSPHRASE>
  378. {
  379. static void set(CSrtConfig& co, const void* optval, int optlen)
  380. {
  381. using namespace srt_logging;
  382. #ifdef SRT_ENABLE_ENCRYPTION
  383. // Password must be 10-80 characters.
  384. // Or it can be empty to clear the password.
  385. if ((optlen != 0) && (optlen < 10 || optlen > HAICRYPT_SECRET_MAX_SZ))
  386. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  387. memset(&co.CryptoSecret, 0, sizeof(co.CryptoSecret));
  388. co.CryptoSecret.typ = HAICRYPT_SECTYP_PASSPHRASE;
  389. co.CryptoSecret.len = (optlen <= (int)sizeof(co.CryptoSecret.str) ? optlen : (int)sizeof(co.CryptoSecret.str));
  390. memcpy((co.CryptoSecret.str), optval, co.CryptoSecret.len);
  391. #else
  392. (void)co; // prevent warning
  393. (void)optval;
  394. if (optlen == 0)
  395. return; // Allow to set empty passphrase if no encryption supported.
  396. LOGC(aclog.Error, log << "SRTO_PASSPHRASE: encryption not enabled at compile time");
  397. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  398. #endif
  399. }
  400. };
  401. template<>
  402. struct CSrtConfigSetter<SRTO_PBKEYLEN>
  403. {
  404. static void set(CSrtConfig& co, const void* optval, int optlen)
  405. {
  406. using namespace srt_logging;
  407. #ifdef SRT_ENABLE_ENCRYPTION
  408. const int v = cast_optval<int>(optval, optlen);
  409. int const allowed[4] = {
  410. 0, // Default value, if this results for initiator, defaults to 16. See below.
  411. 16, // AES-128
  412. 24, // AES-192
  413. 32 // AES-256
  414. };
  415. const int *const allowed_end = allowed + 4;
  416. if (std::find(allowed, allowed_end, v) == allowed_end)
  417. {
  418. LOGC(aclog.Error,
  419. log << "Invalid value for option SRTO_PBKEYLEN: " << v << "; allowed are: 0, 16, 24, 32");
  420. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  421. }
  422. // Note: This works a little different in HSv4 and HSv5.
  423. // HSv4:
  424. // The party that is set SRTO_SENDER will send KMREQ, and it will
  425. // use default value 16, if SRTO_PBKEYLEN is the default value 0.
  426. // The responder that receives KMRSP has nothing to say about
  427. // PBKEYLEN anyway and it will take the length of the key from
  428. // the initiator (sender) as a good deal.
  429. //
  430. // HSv5:
  431. // The initiator (independently on the sender) will send KMREQ,
  432. // and as it should be the sender to decide about the PBKEYLEN.
  433. // Your application should do the following then:
  434. // 1. The sender should set PBKEYLEN to the required value.
  435. // 2. If the sender is initiator, it will create the key using
  436. // its preset PBKEYLEN (or default 16, if not set) and the
  437. // receiver-responder will take it as a good deal.
  438. // 3. Leave the PBKEYLEN value on the receiver as default 0.
  439. // 4. If sender is responder, it should then advertise the PBKEYLEN
  440. // value in the initial handshake messages (URQ_INDUCTION if
  441. // listener, and both URQ_WAVEAHAND and URQ_CONCLUSION in case
  442. // of rendezvous, as it is the matter of luck who of them will
  443. // eventually become the initiator). This way the receiver
  444. // being an initiator will set iSndCryptoKeyLen before setting
  445. // up KMREQ for sending to the sender-responder.
  446. //
  447. // Note that in HSv5 if both sides set PBKEYLEN, the responder
  448. // wins, unless the initiator is a sender (the effective PBKEYLEN
  449. // will be the one advertised by the responder). If none sets,
  450. // PBKEYLEN will default to 16.
  451. co.iSndCryptoKeyLen = v;
  452. #else
  453. (void)co; // prevent warning
  454. (void)optval;
  455. (void)optlen;
  456. LOGC(aclog.Error, log << "SRTO_PBKEYLEN: encryption not enabled at compile time");
  457. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  458. #endif
  459. }
  460. };
  461. template<>
  462. struct CSrtConfigSetter<SRTO_NAKREPORT>
  463. {
  464. static void set(CSrtConfig& co, const void* optval, int optlen)
  465. {
  466. co.bRcvNakReport = cast_optval<bool>(optval, optlen);
  467. }
  468. };
  469. template<>
  470. struct CSrtConfigSetter<SRTO_CONNTIMEO>
  471. {
  472. static void set(CSrtConfig& co, const void* optval, int optlen)
  473. {
  474. const int val = cast_optval<int>(optval, optlen);
  475. if (val < 0)
  476. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  477. using namespace srt::sync;
  478. co.tdConnTimeOut = milliseconds_from(val);
  479. }
  480. };
  481. template<>
  482. struct CSrtConfigSetter<SRTO_DRIFTTRACER>
  483. {
  484. static void set(CSrtConfig& co, const void* optval, int optlen)
  485. {
  486. co.bDriftTracer = cast_optval<bool>(optval, optlen);
  487. }
  488. };
  489. template<>
  490. struct CSrtConfigSetter<SRTO_LOSSMAXTTL>
  491. {
  492. static void set(CSrtConfig& co, const void* optval, int optlen)
  493. {
  494. co.iMaxReorderTolerance = cast_optval<int>(optval, optlen);
  495. }
  496. };
  497. template<>
  498. struct CSrtConfigSetter<SRTO_VERSION>
  499. {
  500. static void set(CSrtConfig& co, const void* optval, int optlen)
  501. {
  502. co.uSrtVersion = cast_optval<uint32_t>(optval, optlen);
  503. }
  504. };
  505. template<>
  506. struct CSrtConfigSetter<SRTO_MINVERSION>
  507. {
  508. static void set(CSrtConfig& co, const void* optval, int optlen)
  509. {
  510. co.uMinimumPeerSrtVersion = cast_optval<uint32_t>(optval, optlen);
  511. }
  512. };
  513. template<>
  514. struct CSrtConfigSetter<SRTO_STREAMID>
  515. {
  516. static void set(CSrtConfig& co, const void* optval, int optlen)
  517. {
  518. if (size_t(optlen) > CSrtConfig::MAX_SID_LENGTH)
  519. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  520. co.sStreamName.set((const char*)optval, optlen);
  521. }
  522. };
  523. template<>
  524. struct CSrtConfigSetter<SRTO_CONGESTION>
  525. {
  526. static void set(CSrtConfig& co, const void* optval, int optlen)
  527. {
  528. std::string val;
  529. if (optlen == -1)
  530. val = (const char*)optval;
  531. else
  532. val.assign((const char*)optval, optlen);
  533. // Translate alias
  534. if (val == "vod")
  535. val = "file";
  536. bool res = SrtCongestion::exists(val);
  537. if (!res)
  538. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  539. co.sCongestion.set(val);
  540. }
  541. };
  542. template<>
  543. struct CSrtConfigSetter<SRTO_MESSAGEAPI>
  544. {
  545. static void set(CSrtConfig& co, const void* optval, int optlen)
  546. {
  547. co.bMessageAPI = cast_optval<bool>(optval, optlen);
  548. }
  549. };
  550. template<>
  551. struct CSrtConfigSetter<SRTO_PAYLOADSIZE>
  552. {
  553. static void set(CSrtConfig& co, const void* optval, int optlen)
  554. {
  555. using namespace srt_logging;
  556. const int val = cast_optval<int>(optval, optlen);
  557. if (val < 0)
  558. {
  559. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  560. }
  561. if (val > SRT_LIVE_MAX_PLSIZE)
  562. {
  563. LOGC(aclog.Error, log << "SRTO_PAYLOADSIZE: value exceeds " << SRT_LIVE_MAX_PLSIZE << ", maximum payload per MTU.");
  564. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  565. }
  566. if (!co.sPacketFilterConfig.empty())
  567. {
  568. // This means that the filter might have been installed before,
  569. // and the fix to the maximum payload size was already applied.
  570. // This needs to be checked now.
  571. SrtFilterConfig fc;
  572. if (!ParseFilterConfig(co.sPacketFilterConfig.str(), fc))
  573. {
  574. // Break silently. This should not happen
  575. LOGC(aclog.Error, log << "SRTO_PAYLOADSIZE: IPE: failing filter configuration installed");
  576. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  577. }
  578. const size_t efc_max_payload_size = SRT_LIVE_MAX_PLSIZE - fc.extra_size;
  579. if (size_t(val) > efc_max_payload_size)
  580. {
  581. LOGC(aclog.Error,
  582. log << "SRTO_PAYLOADSIZE: value exceeds " << SRT_LIVE_MAX_PLSIZE << " bytes decreased by " << fc.extra_size
  583. << " required for packet filter header");
  584. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  585. }
  586. }
  587. // Not checking AUTO to allow defaul 1456 bytes.
  588. if ((co.iCryptoMode == CSrtConfig::CIPHER_MODE_AES_GCM)
  589. && (val > (SRT_LIVE_MAX_PLSIZE - HAICRYPT_AUTHTAG_MAX)))
  590. {
  591. LOGC(aclog.Error,
  592. log << "SRTO_PAYLOADSIZE: value exceeds " << SRT_LIVE_MAX_PLSIZE << " bytes decreased by " << HAICRYPT_AUTHTAG_MAX
  593. << " required for AES-GCM.");
  594. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  595. }
  596. co.zExpPayloadSize = val;
  597. }
  598. };
  599. template<>
  600. struct CSrtConfigSetter<SRTO_TRANSTYPE>
  601. {
  602. static void set(CSrtConfig& co, const void* optval, int optlen)
  603. {
  604. // XXX Note that here the configuration for SRTT_LIVE
  605. // is the same as DEFAULT VALUES for these fields set
  606. // in CUDT::CUDT.
  607. switch (cast_optval<SRT_TRANSTYPE>(optval, optlen))
  608. {
  609. case SRTT_LIVE:
  610. // Default live options:
  611. // - tsbpd: on
  612. // - latency: 120ms
  613. // - linger: off
  614. // - congctl: live
  615. // - extraction method: message (reading call extracts one message)
  616. co.bTSBPD = true;
  617. co.iRcvLatency = SRT_LIVE_DEF_LATENCY_MS;
  618. co.iPeerLatency = 0;
  619. co.bTLPktDrop = true;
  620. co.iSndDropDelay = 0;
  621. co.bMessageAPI = true;
  622. co.bRcvNakReport = true;
  623. co.iRetransmitAlgo = 1;
  624. co.zExpPayloadSize = SRT_LIVE_DEF_PLSIZE;
  625. co.Linger.l_onoff = 0;
  626. co.Linger.l_linger = 0;
  627. co.sCongestion.set("live", 4);
  628. break;
  629. case SRTT_FILE:
  630. // File transfer mode:
  631. // - tsbpd: off
  632. // - latency: 0
  633. // - linger: on
  634. // - congctl: file (original UDT congestion control)
  635. // - extraction method: stream (reading call extracts as many bytes as available and fits in buffer)
  636. co.bTSBPD = false;
  637. co.iRcvLatency = 0;
  638. co.iPeerLatency = 0;
  639. co.bTLPktDrop = false;
  640. co.iSndDropDelay = -1;
  641. co.bMessageAPI = false;
  642. co.bRcvNakReport = false;
  643. co.iRetransmitAlgo = 0;
  644. co.zExpPayloadSize = 0; // use maximum
  645. co.Linger.l_onoff = 1;
  646. co.Linger.l_linger = CSrtConfig::DEF_LINGER_S;
  647. co.sCongestion.set("file", 4);
  648. break;
  649. default:
  650. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  651. }
  652. }
  653. };
  654. #if ENABLE_BONDING
  655. template<>
  656. struct CSrtConfigSetter<SRTO_GROUPCONNECT>
  657. {
  658. static void set(CSrtConfig& co, const void* optval, int optlen)
  659. {
  660. co.iGroupConnect = cast_optval<int>(optval, optlen);
  661. }
  662. };
  663. #endif
  664. template<>
  665. struct CSrtConfigSetter<SRTO_KMREFRESHRATE>
  666. {
  667. static void set(CSrtConfig& co, const void* optval, int optlen)
  668. {
  669. using namespace srt_logging;
  670. const int val = cast_optval<int>(optval, optlen);
  671. if (val < 0)
  672. {
  673. LOGC(aclog.Error,
  674. log << "SRTO_KMREFRESHRATE=" << val << " can't be negative");
  675. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  676. }
  677. // Changing the KMREFRESHRATE sets KMPREANNOUNCE to the maximum allowed value
  678. co.uKmRefreshRatePkt = (unsigned) val;
  679. if (co.uKmPreAnnouncePkt == 0 && co.uKmRefreshRatePkt == 0)
  680. return; // Both values are default
  681. const unsigned km_preanno = co.uKmPreAnnouncePkt == 0 ? HAICRYPT_DEF_KM_PRE_ANNOUNCE : co.uKmPreAnnouncePkt;
  682. const unsigned km_refresh = co.uKmRefreshRatePkt == 0 ? HAICRYPT_DEF_KM_REFRESH_RATE : co.uKmRefreshRatePkt;
  683. if (co.uKmPreAnnouncePkt == 0 || km_preanno > (km_refresh - 1) / 2)
  684. {
  685. co.uKmPreAnnouncePkt = (km_refresh - 1) / 2;
  686. LOGC(aclog.Warn,
  687. log << "SRTO_KMREFRESHRATE=0x" << std::hex << km_refresh << ": setting SRTO_KMPREANNOUNCE=0x"
  688. << std::hex << co.uKmPreAnnouncePkt);
  689. }
  690. }
  691. };
  692. template<>
  693. struct CSrtConfigSetter<SRTO_KMPREANNOUNCE>
  694. {
  695. static void set(CSrtConfig& co, const void* optval, int optlen)
  696. {
  697. using namespace srt_logging;
  698. const int val = cast_optval<int>(optval, optlen);
  699. if (val < 0)
  700. {
  701. LOGC(aclog.Error,
  702. log << "SRTO_KMPREANNOUNCE=" << val << " can't be negative");
  703. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  704. }
  705. const unsigned km_preanno = val == 0 ? HAICRYPT_DEF_KM_PRE_ANNOUNCE : val;
  706. const unsigned kmref = co.uKmRefreshRatePkt == 0 ? HAICRYPT_DEF_KM_REFRESH_RATE : co.uKmRefreshRatePkt;
  707. if (km_preanno > (kmref - 1) / 2)
  708. {
  709. LOGC(aclog.Error,
  710. log << "SRTO_KMPREANNOUNCE=0x" << std::hex << km_preanno << " exceeds KmRefresh/2, 0x" << ((kmref - 1) / 2)
  711. << " - OPTION REJECTED.");
  712. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  713. }
  714. co.uKmPreAnnouncePkt = val;
  715. }
  716. };
  717. template<>
  718. struct CSrtConfigSetter<SRTO_ENFORCEDENCRYPTION>
  719. {
  720. static void set(CSrtConfig& co, const void* optval, int optlen)
  721. {
  722. co.bEnforcedEnc = cast_optval<bool>(optval, optlen);
  723. }
  724. };
  725. template<>
  726. struct CSrtConfigSetter<SRTO_PEERIDLETIMEO>
  727. {
  728. static void set(CSrtConfig& co, const void* optval, int optlen)
  729. {
  730. const int val = cast_optval<int>(optval, optlen);
  731. if (val < 0)
  732. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  733. co.iPeerIdleTimeout_ms = val;
  734. }
  735. };
  736. template<>
  737. struct CSrtConfigSetter<SRTO_IPV6ONLY>
  738. {
  739. static void set(CSrtConfig& co, const void* optval, int optlen)
  740. {
  741. co.iIpV6Only = cast_optval<int>(optval, optlen);
  742. }
  743. };
  744. template<>
  745. struct CSrtConfigSetter<SRTO_PACKETFILTER>
  746. {
  747. static void set(CSrtConfig& co, const void* optval, int optlen)
  748. {
  749. using namespace srt_logging;
  750. std::string arg((const char*)optval, optlen);
  751. // Parse the configuration string prematurely
  752. SrtFilterConfig fc;
  753. PacketFilter::Factory* fax = 0;
  754. if (!ParseFilterConfig(arg, (fc), (&fax)))
  755. {
  756. LOGC(aclog.Error,
  757. log << "SRTO_PACKETFILTER: Incorrect syntax. Use: FILTERTYPE[,KEY:VALUE...]. "
  758. "FILTERTYPE ("
  759. << fc.type << ") must be installed (or builtin)");
  760. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  761. }
  762. std::string error;
  763. if (!fax->verifyConfig(fc, (error)))
  764. {
  765. LOGC(aclog.Error, log << "SRTO_PACKETFILTER: Incorrect config: " << error);
  766. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  767. }
  768. size_t efc_max_payload_size = SRT_LIVE_MAX_PLSIZE - fc.extra_size;
  769. if (co.zExpPayloadSize > efc_max_payload_size)
  770. {
  771. LOGC(aclog.Warn,
  772. log << "Due to filter-required extra " << fc.extra_size << " bytes, SRTO_PAYLOADSIZE fixed to "
  773. << efc_max_payload_size << " bytes");
  774. co.zExpPayloadSize = efc_max_payload_size;
  775. }
  776. co.sPacketFilterConfig.set(arg);
  777. }
  778. };
  779. #if ENABLE_BONDING
  780. template<>
  781. struct CSrtConfigSetter<SRTO_GROUPMINSTABLETIMEO>
  782. {
  783. static void set(CSrtConfig& co, const void* optval, int optlen)
  784. {
  785. using namespace srt_logging;
  786. // This option is meaningless for the socket itself.
  787. // It's set here just for the sake of setting it on a listener
  788. // socket so that it is then applied on the group when a
  789. // group connection is configured.
  790. const int val_ms = cast_optval<int>(optval, optlen);
  791. const int min_timeo_ms = (int) CSrtConfig::COMM_DEF_MIN_STABILITY_TIMEOUT_MS;
  792. if (val_ms < min_timeo_ms)
  793. {
  794. LOGC(qmlog.Error,
  795. log << "group option: SRTO_GROUPMINSTABLETIMEO min allowed value is "
  796. << min_timeo_ms << " ms.");
  797. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  798. }
  799. const int idletmo_ms = co.iPeerIdleTimeout_ms;
  800. if (val_ms > idletmo_ms)
  801. {
  802. LOGC(aclog.Error, log << "group option: SRTO_GROUPMINSTABLETIMEO(" << val_ms
  803. << ") exceeds SRTO_PEERIDLETIMEO(" << idletmo_ms << ")");
  804. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  805. }
  806. co.uMinStabilityTimeout_ms = val_ms;
  807. LOGC(smlog.Error, log << "SRTO_GROUPMINSTABLETIMEO set " << val_ms);
  808. }
  809. };
  810. #endif
  811. template<>
  812. struct CSrtConfigSetter<SRTO_RETRANSMITALGO>
  813. {
  814. static void set(CSrtConfig& co, const void* optval, int optlen)
  815. {
  816. const int val = cast_optval<int>(optval, optlen);
  817. if (val < 0 || val > 1)
  818. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  819. co.iRetransmitAlgo = val;
  820. }
  821. };
  822. #ifdef ENABLE_AEAD_API_PREVIEW
  823. template<>
  824. struct CSrtConfigSetter<SRTO_CRYPTOMODE>
  825. {
  826. static void set(CSrtConfig& co, const void* optval, int optlen)
  827. {
  828. using namespace srt_logging;
  829. const int val = cast_optval<int>(optval, optlen);
  830. #ifdef SRT_ENABLE_ENCRYPTION
  831. if (val < CSrtConfig::CIPHER_MODE_AUTO || val > CSrtConfig::CIPHER_MODE_AES_GCM)
  832. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  833. if (val == CSrtConfig::CIPHER_MODE_AES_GCM && !HaiCrypt_IsAESGCM_Supported())
  834. {
  835. LOGC(aclog.Error, log << "AES GCM is not supported by the crypto provider.");
  836. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  837. }
  838. if (val == CSrtConfig::CIPHER_MODE_AES_GCM && !co.bTSBPD)
  839. {
  840. LOGC(aclog.Error, log << "Enable TSBPD to use AES GCM.");
  841. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  842. }
  843. co.iCryptoMode = val;
  844. #else
  845. LOGC(aclog.Error, log << "SRT was built without crypto module.");
  846. throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
  847. #endif
  848. }
  849. };
  850. #endif
  851. int dispatchSet(SRT_SOCKOPT optName, CSrtConfig& co, const void* optval, int optlen)
  852. {
  853. switch (optName)
  854. {
  855. #define DISPATCH(optname) case optname: CSrtConfigSetter<optname>::set(co, optval, optlen); return 0;
  856. DISPATCH(SRTO_MSS);
  857. DISPATCH(SRTO_FC);
  858. DISPATCH(SRTO_SNDBUF);
  859. DISPATCH(SRTO_RCVBUF);
  860. DISPATCH(SRTO_LINGER);
  861. DISPATCH(SRTO_UDP_SNDBUF);
  862. DISPATCH(SRTO_UDP_RCVBUF);
  863. DISPATCH(SRTO_RENDEZVOUS);
  864. DISPATCH(SRTO_SNDTIMEO);
  865. DISPATCH(SRTO_RCVTIMEO);
  866. DISPATCH(SRTO_SNDSYN);
  867. DISPATCH(SRTO_RCVSYN);
  868. DISPATCH(SRTO_REUSEADDR);
  869. DISPATCH(SRTO_MAXBW);
  870. DISPATCH(SRTO_IPTTL);
  871. DISPATCH(SRTO_IPTOS);
  872. DISPATCH(SRTO_BINDTODEVICE);
  873. DISPATCH(SRTO_INPUTBW);
  874. DISPATCH(SRTO_MININPUTBW);
  875. DISPATCH(SRTO_OHEADBW);
  876. DISPATCH(SRTO_SENDER);
  877. DISPATCH(SRTO_TSBPDMODE);
  878. DISPATCH(SRTO_LATENCY);
  879. DISPATCH(SRTO_RCVLATENCY);
  880. DISPATCH(SRTO_PEERLATENCY);
  881. DISPATCH(SRTO_TLPKTDROP);
  882. DISPATCH(SRTO_SNDDROPDELAY);
  883. DISPATCH(SRTO_PASSPHRASE);
  884. DISPATCH(SRTO_PBKEYLEN);
  885. DISPATCH(SRTO_NAKREPORT);
  886. DISPATCH(SRTO_CONNTIMEO);
  887. DISPATCH(SRTO_DRIFTTRACER);
  888. DISPATCH(SRTO_LOSSMAXTTL);
  889. DISPATCH(SRTO_VERSION);
  890. DISPATCH(SRTO_MINVERSION);
  891. DISPATCH(SRTO_STREAMID);
  892. DISPATCH(SRTO_CONGESTION);
  893. DISPATCH(SRTO_MESSAGEAPI);
  894. DISPATCH(SRTO_PAYLOADSIZE);
  895. DISPATCH(SRTO_TRANSTYPE);
  896. #if ENABLE_BONDING
  897. DISPATCH(SRTO_GROUPCONNECT);
  898. DISPATCH(SRTO_GROUPMINSTABLETIMEO);
  899. #endif
  900. DISPATCH(SRTO_KMREFRESHRATE);
  901. DISPATCH(SRTO_KMPREANNOUNCE);
  902. DISPATCH(SRTO_ENFORCEDENCRYPTION);
  903. DISPATCH(SRTO_PEERIDLETIMEO);
  904. DISPATCH(SRTO_IPV6ONLY);
  905. DISPATCH(SRTO_PACKETFILTER);
  906. DISPATCH(SRTO_RETRANSMITALGO);
  907. #ifdef ENABLE_AEAD_API_PREVIEW
  908. DISPATCH(SRTO_CRYPTOMODE);
  909. #endif
  910. #ifdef ENABLE_MAXREXMITBW
  911. DISPATCH(SRTO_MAXREXMITBW);
  912. #endif
  913. #undef DISPATCH
  914. default:
  915. return -1;
  916. }
  917. }
  918. } // anonymous namespace
  919. int CSrtConfig::set(SRT_SOCKOPT optName, const void* optval, int optlen)
  920. {
  921. return dispatchSet(optName, *this, optval, optlen);
  922. }
  923. #if ENABLE_BONDING
  924. bool SRT_SocketOptionObject::add(SRT_SOCKOPT optname, const void* optval, size_t optlen)
  925. {
  926. // Check first if this option is allowed to be set
  927. // as on a member socket.
  928. switch (optname)
  929. {
  930. case SRTO_BINDTODEVICE:
  931. case SRTO_CONNTIMEO:
  932. case SRTO_DRIFTTRACER:
  933. //SRTO_FC - not allowed to be different among group members
  934. case SRTO_GROUPMINSTABLETIMEO:
  935. //SRTO_INPUTBW - per transmission setting
  936. case SRTO_IPTOS:
  937. case SRTO_IPTTL:
  938. case SRTO_KMREFRESHRATE:
  939. case SRTO_KMPREANNOUNCE:
  940. //SRTO_LATENCY - per transmission setting
  941. //SRTO_LINGER - not for managed sockets
  942. case SRTO_LOSSMAXTTL:
  943. //SRTO_MAXBW - per transmission setting
  944. //SRTO_MESSAGEAPI - groups are live mode only
  945. //SRTO_MINVERSION - per group connection setting
  946. case SRTO_NAKREPORT:
  947. //SRTO_OHEADBW - per transmission setting
  948. //SRTO_PACKETFILTER - per transmission setting
  949. //SRTO_PASSPHRASE - per group connection setting
  950. //SRTO_PASSPHRASE - per transmission setting
  951. //SRTO_PBKEYLEN - per group connection setting
  952. case SRTO_PEERIDLETIMEO:
  953. case SRTO_RCVBUF:
  954. //SRTO_RCVSYN - must be always false in groups
  955. //SRTO_RCVTIMEO - must be always -1 in groups
  956. case SRTO_SNDBUF:
  957. case SRTO_SNDDROPDELAY:
  958. //SRTO_TLPKTDROP - per transmission setting
  959. //SRTO_TSBPDMODE - per transmission setting
  960. case SRTO_UDP_RCVBUF:
  961. case SRTO_UDP_SNDBUF:
  962. break;
  963. default:
  964. // Other options are not allowed
  965. return false;
  966. }
  967. // Header size will get the size likely aligned, but it won't
  968. // hurt if the memory size will be up to 4 bytes more than
  969. // needed - and it's better to not risk that alighment rules
  970. // will make these calculations result in less space than needed.
  971. const size_t headersize = sizeof(SingleOption);
  972. const size_t payload = std::min(sizeof(uint32_t), optlen);
  973. unsigned char* mem = new unsigned char[headersize + payload];
  974. SingleOption* option = reinterpret_cast<SingleOption*>(mem);
  975. option->option = optname;
  976. option->length = (uint16_t) optlen;
  977. memcpy(option->storage, optval, optlen);
  978. options.push_back(option);
  979. return true;
  980. }
  981. #endif