Skip to content

Commit

Permalink
[core] Fixed cookie contest
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed Apr 28, 2021
1 parent 291e010 commit 0b1925d
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3772,31 +3772,43 @@ void CUDT::cookieContest()
//
// The cookie contest must be repeated every time because it
// may change the state at some point.
int better_cookie = m_ConnReq.m_iCookie - m_ConnRes.m_iCookie;

if (better_cookie > 0)
{
m_SrtHsSide = HSD_INITIATOR;
//
// In SRT v1.4.3 and prior the below subtraction was performed in 32-bit arithmetic.
// The result of subtraction can overflow 32-bits.
// Example
// m_ConnReq.m_iCookie = -1480577720;
// m_ConnRes.m_iCookie = 811599203;
// int64_t llBetterCookie = -1480577720 - 811599203 = -2292176923 (FFFF FFFF 7760 27E5);
// int32_t iBetterCookie = 2002790373 (7760 27E5);
//
// Now 64-bit arithmetic is used to calculate the actual result of subtraction.
// The 31-st bit is then checked to check if the resulting is negative in 32-bit aritmetics.
// This way the old contest behavior is preserved, and potential compiler optimisations are avoided.
const int64_t contest = int64_t(m_ConnReq.m_iCookie) - int64_t(m_ConnRes.m_iCookie);

if ((contest & 0xFFFFFFFF) == 0)
{
// DRAW! The only way to continue would be to force the
// cookies to be regenerated and to start over. But it's
// not worth a shot - this is an extremely rare case.
// This can simply do reject so that it can be started again.

// Pretend then that the cookie contest wasn't done so that
// it's done again. Cookies are baked every time anew, however
// the successful initial contest remains valid no matter how
// cookies will change.

m_SrtHsSide = HSD_DRAW;
return;
}

if (better_cookie < 0)
if (contest & 0x80000000)
{
m_SrtHsSide = HSD_RESPONDER;
return;
}

// DRAW! The only way to continue would be to force the
// cookies to be regenerated and to start over. But it's
// not worth a shot - this is an extremely rare case.
// This can simply do reject so that it can be started again.

// Pretend then that the cookie contest wasn't done so that
// it's done again. Cookies are baked every time anew, however
// the successful initial contest remains valid no matter how
// cookies will change.

m_SrtHsSide = HSD_DRAW;
m_SrtHsSide = HSD_INITIATOR;
}

// This function should complete the data for KMX needed for an out-of-band
Expand Down

0 comments on commit 0b1925d

Please sign in to comment.