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