Skip to content

Commit

Permalink
Added socket options to tweak KM refresh parameters, fixed uninitiali…
Browse files Browse the repository at this point in the history
…zed var (#350)
  • Loading branch information
ethouris authored and rndi committed Apr 20, 2018
1 parent 09212be commit b851180
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
4 changes: 3 additions & 1 deletion apps/socketoptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ const SocketOption srt_options [] {
{ "smoother", 0, SRTO_SMOOTHER, SocketOption::PRE, SocketOption::STRING, nullptr},
{ "messageapi", 0, SRTO_MESSAGEAPI, SocketOption::PRE, SocketOption::BOOL, nullptr},
{ "payloadsize", 0, SRTO_PAYLOADSIZE, SocketOption::PRE, SocketOption::INT, nullptr},
{ "transtype", 0, SRTO_TRANSTYPE, SocketOption::PRE, SocketOption::ENUM, &enummap_transtype }
{ "transtype", 0, SRTO_TRANSTYPE, SocketOption::PRE, SocketOption::ENUM, &enummap_transtype },
{ "kmrefreshrate", 0, SRTO_KMREFRESHRATE, SocketOption::PRE, SocketOption::INT, nullptr },
{ "kmpreannounce", 0, SRTO_KMPREANNOUNCE, SocketOption::PRE, SocketOption::INT, nullptr }
};
}

Expand Down
40 changes: 40 additions & 0 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ void CUDT::construct()
m_bBroken = false;
m_bPeerHealth = true;
m_ullLingerExpiration = 0;
m_llLastReqTime = 0;

m_lSrtVersion = SRT_DEF_VERSION;
m_lPeerSrtVersion = 0; // not defined until connected.
Expand All @@ -192,6 +193,9 @@ void CUDT::construct()
m_bTsbPd = false;
m_bPeerTLPktDrop = false;

m_uKmRefreshRatePkt = 0;
m_uKmPreAnnouncePkt = 0;

// Initilize mutex and condition variables
initSynch();
}
Expand Down Expand Up @@ -305,6 +309,9 @@ CUDT::CUDT(const CUDT& ancestor)
m_CryptoSecret = ancestor.m_CryptoSecret;
m_iSndCryptoKeyLen = ancestor.m_iSndCryptoKeyLen;

m_uKmRefreshRatePkt = ancestor.m_uKmRefreshRatePkt;
m_uKmPreAnnouncePkt = ancestor.m_uKmPreAnnouncePkt;

m_pCache = ancestor.m_pCache;

// Smoother's copy constructor copies the selection,
Expand Down Expand Up @@ -766,6 +773,39 @@ void CUDT::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
}
break;

case SRTO_KMREFRESHRATE:
if (m_bConnected)
throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);

// If you first change the KMREFRESHRATE, KMPREANNOUNCE
// will be set to the maximum allowed value
m_uKmRefreshRatePkt = *(int*)optval;
if (m_uKmPreAnnouncePkt == 0 || m_uKmPreAnnouncePkt > (m_uKmRefreshRatePkt-1)/2)
{
m_uKmPreAnnouncePkt = (m_uKmRefreshRatePkt-1)/2;
LOGC(mglog.Warn, log << "SRTO_KMREFRESHRATE=0x"
<< hex << m_uKmRefreshRatePkt << ": setting SRTO_KMPREANNOUNCE=0x"
<< hex << m_uKmPreAnnouncePkt);
}
break;

case SRTO_KMPREANNOUNCE:
if (m_bConnected)
throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
{
int val = *(int*)optval;
int kmref = m_uKmRefreshRatePkt == 0 ? HAICRYPT_DEF_KM_REFRESH_RATE : m_uKmRefreshRatePkt;
if (val > (kmref-1)/2)
{
LOGC(mglog.Error, log << "SRTO_KMPREANNOUNCE=0x" << hex << val
<< " exceeds KmRefresh/2, 0x" << ((kmref-1)/2) << " - OPTION REJECTED.");
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
}

m_uKmPreAnnouncePkt = val;
}
break;

default:
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
}
Expand Down
6 changes: 6 additions & 0 deletions srtcore/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ class CUDT

void checkTimers();

public: // For the use of CCryptoControl
// HaiCrypt configuration
unsigned int m_uKmRefreshRatePkt;
unsigned int m_uKmPreAnnouncePkt;


private: // for UDP multiplexer
CSndQueue* m_pSndQueue; // packet sending queue
CRcvQueue* m_pRcvQueue; // packet receiving queue
Expand Down
11 changes: 8 additions & 3 deletions srtcore/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ m_SocketID(id),
m_iSndKmKeyLen(0),
m_iRcvKmKeyLen(0),
m_SndKmState(SRT_KM_S_UNSECURED),
m_RcvKmState(SRT_KM_S_UNSECURED)
m_RcvKmState(SRT_KM_S_UNSECURED),
m_KmRefreshRatePkt(0),
m_KmPreAnnouncePkt(0)
{

m_KmSecret.len = 0;
Expand Down Expand Up @@ -475,6 +477,9 @@ bool CCryptoControl::init(HandshakeSide side, bool bidirectional)
// Set security-pending state, if a password was set.
m_SndKmState = (m_iSndKmKeyLen > 0) ? SRT_KM_S_SECURING : SRT_KM_S_UNSECURED;

m_KmPreAnnouncePkt = m_parent->m_uKmPreAnnouncePkt;
m_KmRefreshRatePkt = m_parent->m_uKmRefreshRatePkt;

if ( side == HSD_INITIATOR )
{
if (hasPassphrase())
Expand Down Expand Up @@ -585,8 +590,8 @@ bool CCryptoControl::createCryptoCtx(ref_t<HaiCrypt_Handle> hCrypto, size_t keyl
crypto_cfg.key_len = (size_t)keylen;
crypto_cfg.data_max_len = HAICRYPT_DEF_DATA_MAX_LENGTH; //MTU
crypto_cfg.km_tx_period_ms = 0;//No HaiCrypt KM inject period, handled in SRT;
crypto_cfg.km_refresh_rate_pkt = HAICRYPT_DEF_KM_REFRESH_RATE;
crypto_cfg.km_pre_announce_pkt = SRT_CRYPT_KM_PRE_ANNOUNCE;
crypto_cfg.km_refresh_rate_pkt = m_KmRefreshRatePkt == 0 ? HAICRYPT_DEF_KM_REFRESH_RATE : m_KmRefreshRatePkt;
crypto_cfg.km_pre_announce_pkt = m_KmPreAnnouncePkt == 0 ? SRT_CRYPT_KM_PRE_ANNOUNCE : m_KmPreAnnouncePkt;
crypto_cfg.secret = m_KmSecret;
//memcpy(&crypto_cfg.secret, &m_KmSecret, sizeof(crypto_cfg.secret));

Expand Down
5 changes: 5 additions & 0 deletions srtcore/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class CCryptoControl
SRT_KM_STATE m_RcvKmState; //Receiver Km State (informed by peer)

private:
// Partial haicrypt configuration, consider
// putting the whole HaiCrypt_Cfg object here.
int m_KmRefreshRatePkt;
int m_KmPreAnnouncePkt;

HaiCrypt_Secret m_KmSecret; //Key material shared secret
// Sender
uint64_t m_SndKmLastTime;
Expand Down
4 changes: 3 additions & 1 deletion srtcore/srt.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ typedef enum SRT_SOCKOPT {
SRTO_SMOOTHER, // Smoother selection (congestion control algorithm)
SRTO_MESSAGEAPI,
SRTO_PAYLOADSIZE,
SRTO_TRANSTYPE // Transmission type (set of options required for given transmission type)
SRTO_TRANSTYPE, // Transmission type (set of options required for given transmission type)
SRTO_KMREFRESHRATE,
SRTO_KMPREANNOUNCE
} SRT_SOCKOPT;

// DEPRECATED OPTIONS:
Expand Down

0 comments on commit b851180

Please sign in to comment.