| 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 |
|
| 51 |
#ifndef INC_SRT_SOCKETCONFIG_H |
| 52 |
#define INC_SRT_SOCKETCONFIG_H |
| 53 |
|
| 54 |
#include "platform_sys.h" |
| 55 |
#ifdef SRT_ENABLE_BINDTODEVICE |
| 56 |
#include <linux/if.h> |
| 57 |
#endif |
| 58 |
#include <string> |
| 59 |
#include "haicrypt.h" |
| 60 |
#include "congctl.h" |
| 61 |
#include "packet.h" |
| 62 |
#include "handshake.h" |
| 63 |
#include "logger_defs.h" |
| 64 |
#include "packetfilter.h" |
| 65 |
|
| 66 |
// SRT Version constants |
| 67 |
#define SRT_VERSION_UNK 0 |
| 68 |
#define SRT_VERSION_MAJ1 0x010000 /* Version 1 major */ |
| 69 |
#define SRT_VERSION_MAJ(v) (0xFF0000 & (v)) /* Major number ensuring backward compatibility */ |
| 70 |
#define SRT_VERSION_MIN(v) (0x00FF00 & (v)) |
| 71 |
#define SRT_VERSION_PCH(v) (0x0000FF & (v)) |
| 72 |
|
| 73 |
static const int SRT_OHEAD_DEFAULT_P100 = 25; |
| 74 |
|
| 75 |
// NOTE: SRT_VERSION is primarily defined in the build file. |
| 76 |
extern const int32_t SRT_DEF_VERSION; |
| 77 |
|
| 78 |
namespace srt |
| 79 |
{ |
| 80 |
|
| 81 |
struct CSrtMuxerConfig |
| 82 |
{ |
| 83 |
static const int DEF_UDP_BUFFER_SIZE = 65536; |
| 84 |
|
| 85 |
int iIpTTL; |
| 86 |
int iIpToS; |
| 87 |
int iIpV6Only; // IPV6_V6ONLY option (-1 if not set) |
| 88 |
bool bReuseAddr; // reuse an exiting port or not, for UDP multiplexer |
| 89 |
|
| 90 |
#ifdef SRT_ENABLE_BINDTODEVICE |
| 91 |
std::string sBindToDevice; |
| 92 |
#endif |
| 93 |
int iUDPSndBufSize; // UDP sending buffer size |
| 94 |
int iUDPRcvBufSize; // UDP receiving buffer size |
| 95 |
|
| 96 |
// NOTE: this operator is not reversible. The syntax must use: |
| 97 |
// muxer_entry == socket_entry |
| 98 |
bool isCompatWith(const CSrtMuxerConfig& other) const |
| 99 |
{ |
| 100 |
#define CEQUAL(field) (field == other.field) |
| 101 |
return CEQUAL(iIpTTL) |
| 102 |
&& CEQUAL(iIpToS) |
| 103 |
&& CEQUAL(bReuseAddr) |
| 104 |
#ifdef SRT_ENABLE_BINDTODEVICE |
| 105 |
&& CEQUAL(sBindToDevice) |
| 106 |
#endif |
| 107 |
&& CEQUAL(iUDPSndBufSize) |
| 108 |
&& CEQUAL(iUDPRcvBufSize) |
| 109 |
&& (other.iIpV6Only == -1 || CEQUAL(iIpV6Only)) |
| 110 |
// NOTE: iIpV6Only is not regarded because |
| 111 |
// this matches only in case of IPv6 with "any" address. |
| 112 |
// And this aspect must be checked separately because here |
| 113 |
// this procedure has no access to neither the address, |
| 114 |
// nor the IP version (family). |
| 115 |
#undef CEQUAL |
| 116 |
&& true; |
| 117 |
} |
| 118 |
|
| 119 |
CSrtMuxerConfig() |
| 120 |
: iIpTTL(-1) /* IPv4 TTL or IPv6 HOPs [1..255] (-1:undefined) */ |
| 121 |
, iIpToS(-1) /* IPv4 Type of Service or IPv6 Traffic Class [0x00..0xff] (-1:undefined) */ |
| 122 |
, iIpV6Only(-1) |
| 123 |
, bReuseAddr(true) // This is default in SRT |
| 124 |
, iUDPSndBufSize(DEF_UDP_BUFFER_SIZE) |
| 125 |
, iUDPRcvBufSize(DEF_UDP_BUFFER_SIZE) |
| 126 |
{ |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
struct CSrtConfig; |
| 131 |
|
| 132 |
template <size_t SIZE> |
| 133 |
class StringStorage |
| 134 |
{ |
| 135 |
char stor[SIZE + 1]; |
| 136 |
uint16_t len; |
| 137 |
|
| 138 |
// NOTE: default copying allowed. |
| 139 |
|
| 140 |
public: |
| 141 |
StringStorage() |
| 142 |
: len(0) |
| 143 |
{ |
| 144 |
memset(stor, 0, sizeof stor); |
| 145 |
} |
| 146 |
|
| 147 |
StringStorage(const char* s, size_t length) |
| 148 |
: len(0) |
| 149 |
{ |
| 150 |
set(s, length); |
| 151 |
} |
| 152 |
|
| 153 |
bool set(const char* s, size_t length) |
| 154 |
{ |
| 155 |
if (length > SIZE) |
| 156 |
return false; |
| 157 |
|
| 158 |
memcpy(stor, s, length); |
| 159 |
stor[length] = 0; |
| 160 |
len = (int) length; |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
bool set(const std::string& s) |
| 165 |
{ |
| 166 |
return set(s.c_str(), s.size()); |
| 167 |
} |
| 168 |
|
| 169 |
size_t copy(char* s, size_t length) const |
| 170 |
{ |
| 171 |
if (!s) |
| 172 |
return 0; |
| 173 |
|
| 174 |
size_t copy_len = std::min((size_t)len, length); |
| 175 |
memcpy(s, stor, copy_len); |
| 176 |
return copy_len; |
| 177 |
} |
| 178 |
|
| 179 |
std::string str() const |
| 180 |
{ |
| 181 |
return len == 0 ? std::string() : std::string(stor, len); |
| 182 |
} |
| 183 |
|
| 184 |
const char* c_str() const |
| 185 |
{ |
| 186 |
return stor; |
| 187 |
} |
| 188 |
|
| 189 |
size_t size() const { return size_t(len); } |
| 190 |
bool empty() const { return len == 0; } |
| 191 |
}; |
| 192 |
|
| 193 |
struct CSrtConfig: CSrtMuxerConfig |
| 194 |
{ |
| 195 |
typedef srt::sync::steady_clock::time_point time_point; |
| 196 |
typedef srt::sync::steady_clock::duration duration; |
| 197 |
|
| 198 |
static const int |
| 199 |
DEF_MSS = 1500, |
| 200 |
DEF_FLIGHT_SIZE = 25600, |
| 201 |
DEF_BUFFER_SIZE = 8192, //Rcv buffer MUST NOT be bigger than Flight Flag size |
| 202 |
DEF_LINGER_S = 3*60, // 3 minutes |
| 203 |
DEF_CONNTIMEO_S = 3; // 3 seconds |
| 204 |
|
| 205 |
enum |
| 206 |
{ |
| 207 |
CIPHER_MODE_AUTO = 0, |
| 208 |
CIPHER_MODE_AES_CTR = 1, |
| 209 |
CIPHER_MODE_AES_GCM = 2 |
| 210 |
}; |
| 211 |
|
| 212 |
static const int COMM_RESPONSE_TIMEOUT_MS = 5 * 1000; // 5 seconds |
| 213 |
static const uint32_t COMM_DEF_MIN_STABILITY_TIMEOUT_MS = 60; // 60 ms |
| 214 |
|
| 215 |
// Minimum recv flight flag size is 32 packets |
| 216 |
static const int DEF_MIN_FLIGHT_PKT = 32; |
| 217 |
static const size_t MAX_SID_LENGTH = 512; |
| 218 |
static const size_t MAX_PFILTER_LENGTH = 64; |
| 219 |
static const size_t MAX_CONG_LENGTH = 16; |
| 220 |
|
| 221 |
int iMSS; // Maximum Segment Size, in bytes |
| 222 |
size_t zExpPayloadSize; // Expected average payload size (user option) |
| 223 |
|
| 224 |
// Options |
| 225 |
bool bSynSending; // Sending synchronization mode |
| 226 |
bool bSynRecving; // Receiving synchronization mode |
| 227 |
int iFlightFlagSize; // Maximum number of packets in flight from the peer side |
| 228 |
int iSndBufSize; // Maximum UDT sender buffer size |
| 229 |
int iRcvBufSize; // Maximum UDT receiver buffer size |
| 230 |
linger Linger; // Linger information on close |
| 231 |
bool bRendezvous; // Rendezvous connection mode |
| 232 |
|
| 233 |
duration tdConnTimeOut; // connect timeout in milliseconds |
| 234 |
bool bDriftTracer; |
| 235 |
int iSndTimeOut; // sending timeout in milliseconds |
| 236 |
int iRcvTimeOut; // receiving timeout in milliseconds |
| 237 |
int64_t llMaxBW; // maximum data transfer rate (threshold) |
| 238 |
#ifdef ENABLE_MAXREXMITBW |
| 239 |
int64_t llMaxRexmitBW; // maximum bandwidth limit for retransmissions (Bytes/s). |
| 240 |
#endif |
| 241 |
|
| 242 |
// These fields keep the options for encryption |
| 243 |
// (SRTO_PASSPHRASE, SRTO_PBKEYLEN). Crypto object is |
| 244 |
// created later and takes values from these. |
| 245 |
HaiCrypt_Secret CryptoSecret; |
| 246 |
int iSndCryptoKeyLen; |
| 247 |
|
| 248 |
// XXX Consider removing. The bDataSender stays here |
| 249 |
// in order to maintain the HS side selection in HSv4. |
| 250 |
bool bDataSender; |
| 251 |
|
| 252 |
bool bMessageAPI; |
| 253 |
bool bTSBPD; // Whether AGENT will do TSBPD Rx (whether peer does, is not agent's problem) |
| 254 |
int iRcvLatency; // Agent's Rx latency |
| 255 |
int iPeerLatency; // Peer's Rx latency for the traffic made by Agent's Tx. |
| 256 |
bool bTLPktDrop; // Whether Agent WILL DO TLPKTDROP on Rx. |
| 257 |
int iSndDropDelay; // Extra delay when deciding to snd-drop for TLPKTDROP, -1 to off |
| 258 |
bool bEnforcedEnc; // Off by default. When on, any connection other than nopw-nopw & pw1-pw1 is rejected. |
| 259 |
int iGroupConnect; // 1 - allow group connections |
| 260 |
int iPeerIdleTimeout_ms; // Timeout for hearing anything from the peer (ms). |
| 261 |
uint32_t uMinStabilityTimeout_ms; |
| 262 |
int iRetransmitAlgo; |
| 263 |
int iCryptoMode; // SRTO_CRYPTOMODE |
| 264 |
|
| 265 |
int64_t llInputBW; // Input stream rate (bytes/sec). 0: use internally estimated input bandwidth |
| 266 |
int64_t llMinInputBW; // Minimum input stream rate estimate (bytes/sec) |
| 267 |
int iOverheadBW; // Percent above input stream rate (applies if llMaxBW == 0) |
| 268 |
bool bRcvNakReport; // Enable Receiver Periodic NAK Reports |
| 269 |
int iMaxReorderTolerance; //< Maximum allowed value for dynamic reorder tolerance |
| 270 |
|
| 271 |
// For the use of CCryptoControl |
| 272 |
// HaiCrypt configuration |
| 273 |
unsigned int uKmRefreshRatePkt; |
| 274 |
unsigned int uKmPreAnnouncePkt; |
| 275 |
|
| 276 |
uint32_t uSrtVersion; |
| 277 |
uint32_t uMinimumPeerSrtVersion; |
| 278 |
|
| 279 |
StringStorage<MAX_CONG_LENGTH> sCongestion; |
| 280 |
StringStorage<MAX_PFILTER_LENGTH> sPacketFilterConfig; |
| 281 |
StringStorage<MAX_SID_LENGTH> sStreamName; |
| 282 |
|
| 283 |
// Shortcuts and utilities |
| 284 |
int32_t flightCapacity() |
| 285 |
{ |
| 286 |
return std::min(iRcvBufSize, iFlightFlagSize); |
| 287 |
} |
| 288 |
|
| 289 |
CSrtConfig() |
| 290 |
: iMSS(DEF_MSS) |
| 291 |
, zExpPayloadSize(SRT_LIVE_DEF_PLSIZE) |
| 292 |
, bSynSending(true) |
| 293 |
, bSynRecving(true) |
| 294 |
, iFlightFlagSize(DEF_FLIGHT_SIZE) |
| 295 |
, iSndBufSize(DEF_BUFFER_SIZE) |
| 296 |
, iRcvBufSize(DEF_BUFFER_SIZE) |
| 297 |
, bRendezvous(false) |
| 298 |
, tdConnTimeOut(srt::sync::seconds_from(DEF_CONNTIMEO_S)) |
| 299 |
, bDriftTracer(true) |
| 300 |
, iSndTimeOut(-1) |
| 301 |
, iRcvTimeOut(-1) |
| 302 |
, llMaxBW(-1) |
| 303 |
#ifdef ENABLE_MAXREXMITBW |
| 304 |
, llMaxRexmitBW(-1) |
| 305 |
#endif |
| 306 |
, bDataSender(false) |
| 307 |
, bMessageAPI(true) |
| 308 |
, bTSBPD(true) |
| 309 |
, iRcvLatency(SRT_LIVE_DEF_LATENCY_MS) |
| 310 |
, iPeerLatency(0) |
| 311 |
, bTLPktDrop(true) |
| 312 |
, iSndDropDelay(0) |
| 313 |
, bEnforcedEnc(true) |
| 314 |
, iGroupConnect(0) |
| 315 |
, iPeerIdleTimeout_ms(COMM_RESPONSE_TIMEOUT_MS) |
| 316 |
, uMinStabilityTimeout_ms(COMM_DEF_MIN_STABILITY_TIMEOUT_MS) |
| 317 |
, iRetransmitAlgo(1) |
| 318 |
, iCryptoMode(CIPHER_MODE_AUTO) |
| 319 |
, llInputBW(0) |
| 320 |
, llMinInputBW(0) |
| 321 |
, iOverheadBW(SRT_OHEAD_DEFAULT_P100) |
| 322 |
, bRcvNakReport(true) |
| 323 |
, iMaxReorderTolerance(0) // Sensible optimal value is 10, 0 preserves old behavior |
| 324 |
, uKmRefreshRatePkt(0) |
| 325 |
, uKmPreAnnouncePkt(0) |
| 326 |
, uSrtVersion(SRT_DEF_VERSION) |
| 327 |
, uMinimumPeerSrtVersion(SRT_VERSION_MAJ1) |
| 328 |
|
| 329 |
{ |
| 330 |
// Default UDT configurations |
| 331 |
iUDPRcvBufSize = iRcvBufSize * iMSS; |
| 332 |
|
| 333 |
// Linger: LIVE mode defaults, please refer to `SRTO_TRANSTYPE` option |
| 334 |
// for other modes. |
| 335 |
Linger.l_onoff = 0; |
| 336 |
Linger.l_linger = 0; |
| 337 |
CryptoSecret.len = 0; |
| 338 |
iSndCryptoKeyLen = 0; |
| 339 |
|
| 340 |
// Default congestion is "live". |
| 341 |
// Available builtin congestions: "file". |
| 342 |
// Others can be registered. |
| 343 |
sCongestion.set("live", 4); |
| 344 |
} |
| 345 |
|
| 346 |
~CSrtConfig() |
| 347 |
{ |
| 348 |
// Wipeout critical data |
| 349 |
memset(&CryptoSecret, 0, sizeof(CryptoSecret)); |
| 350 |
} |
| 351 |
|
| 352 |
int set(SRT_SOCKOPT optName, const void* val, int size); |
| 353 |
|
| 354 |
bool payloadSizeFits(size_t val, int ip_family, std::string& w_errmsg) ATR_NOTHROW; |
| 355 |
|
| 356 |
// This function returns the number of bytes that are allocated |
| 357 |
// for a single packet in the sender and receiver buffer. |
| 358 |
int bytesPerPkt() const { return iMSS - int(CPacket::UDP_HDR_SIZE); } |
| 359 |
}; |
| 360 |
|
| 361 |
template <typename T> |
| 362 |
inline T cast_optval(const void* optval) |
| 363 |
{ |
| 364 |
return *reinterpret_cast<const T*>(optval); |
| 365 |
} |
| 366 |
|
| 367 |
template <typename T> |
| 368 |
inline T cast_optval(const void* optval, int optlen) |
| 369 |
{ |
| 370 |
if (optlen > 0 && optlen != sizeof(T)) |
| 371 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 372 |
|
| 373 |
return cast_optval<T>(optval); |
| 374 |
} |
| 375 |
|
| 376 |
// This function is to make it possible for both C and C++ |
| 377 |
// API to accept both bool and int types for boolean options. |
| 378 |
// (it's not that C couldn't use <stdbool.h>, it's that people |
| 379 |
// often forget to use correct type). |
| 380 |
template <> |
| 381 |
inline bool cast_optval(const void* optval, int optlen) |
| 382 |
{ |
| 383 |
if (optlen == sizeof(bool)) |
| 384 |
{ |
| 385 |
return *reinterpret_cast<const bool*>(optval); |
| 386 |
} |
| 387 |
|
| 388 |
if (optlen == sizeof(int)) |
| 389 |
{ |
| 390 |
// 0!= is a windows warning-killer int-to-bool conversion |
| 391 |
return 0 != *reinterpret_cast<const int*>(optval); |
| 392 |
} |
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
int RcvBufferSizeOptionToValue(int optval, int flightflag, int mss); |
| 398 |
|
| 399 |
} // namespace srt |
| 400 |
|
| 401 |
struct SRT_SocketOptionObject |
| 402 |
{ |
| 403 |
struct SingleOption |
| 404 |
{ |
| 405 |
uint16_t option; |
| 406 |
uint16_t length; |
| 407 |
unsigned char storage[1]; // NOTE: Variable length object! |
| 408 |
}; |
| 409 |
|
| 410 |
std::vector<SingleOption*> options; |
| 411 |
|
| 412 |
SRT_SocketOptionObject() {} |
| 413 |
|
| 414 |
~SRT_SocketOptionObject() |
| 415 |
{ |
| 416 |
for (size_t i = 0; i < options.size(); ++i) |
| 417 |
{ |
| 418 |
// Convert back |
| 419 |
unsigned char* mem = reinterpret_cast<unsigned char*>(options[i]); |
| 420 |
delete[] mem; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
bool add(SRT_SOCKOPT optname, const void* optval, size_t optlen); |
| 425 |
}; |
| 426 |
|
| 427 |
#endif |