Skip to content

Commit

Permalink
[core] Fixed wrong reaction on failure KMREQ and unsafe construction …
Browse files Browse the repository at this point in the history
…of EventVariant (#1666)
  • Loading branch information
ethouris committed Nov 20, 2020
1 parent 411264f commit d91e66f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
46 changes: 23 additions & 23 deletions srtcore/common.h
Expand Up @@ -317,7 +317,7 @@ struct EventVariant
enum Type {UNDEFINED, PACKET, ARRAY, ACK, STAGE, INIT} type;
union U
{
CPacket* packet;
const CPacket* packet;
int32_t ack;
struct
{
Expand All @@ -328,36 +328,36 @@ struct EventVariant
EInitEvent init;
} u;

EventVariant()
{
type = UNDEFINED;
memset(&u, 0, sizeof u);
}

template<Type t>
struct VariantFor;

template <Type tp, typename Arg>
void Assign(Arg arg)
{
type = tp;
(u.*(VariantFor<tp>::field())) = arg;
//(u.*field) = arg;
}

void operator=(CPacket* arg) { Assign<PACKET>(arg); };
void operator=(int32_t arg) { Assign<ACK>(arg); };
void operator=(ECheckTimerStage arg) { Assign<STAGE>(arg); };
void operator=(EInitEvent arg) { Assign<INIT>(arg); };

// Note: UNDEFINED and ARRAY don't have assignment operator.
// For ARRAY you'll use 'set' function. For UNDEFINED there's nothing.

explicit EventVariant(const CPacket* arg)
{
type = PACKET;
u.packet = arg;
}

explicit EventVariant(int32_t arg)
{
type = ACK;
u.ack = arg;
}

template <class T>
EventVariant(const T arg)
explicit EventVariant(ECheckTimerStage arg)
{
*this = arg;
type = STAGE;
u.stage = arg;
}

explicit EventVariant(EInitEvent arg)
{
type = INIT;
u.init = arg;
}

const int32_t* get_ptr() const
Expand Down Expand Up @@ -422,10 +422,10 @@ class EventArgType;


// The 'type' field wouldn't be even necessary if we

// use a full-templated version. TBD.
template<> struct EventVariant::VariantFor<EventVariant::PACKET>
{
typedef CPacket* type;
typedef const CPacket* type;
static type U::*field() {return &U::packet;}
};

Expand Down
25 changes: 13 additions & 12 deletions srtcore/core.cpp
Expand Up @@ -490,7 +490,7 @@ void CUDT::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
// When not connected, this will do nothing, however this
// event will be repeated just after connecting anyway.
if (m_bConnected)
updateCC(TEV_INIT, TEV_INIT_RESET);
updateCC(TEV_INIT, EventVariant(TEV_INIT_RESET));
break;

case SRTO_IPTTL:
Expand Down Expand Up @@ -534,7 +534,7 @@ void CUDT::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
// (only if connected; if not, then the value
// from m_iOverheadBW will be used initially)
if (m_bConnected)
updateCC(TEV_INIT, TEV_INIT_INPUTBW);
updateCC(TEV_INIT, EventVariant(TEV_INIT_INPUTBW));
break;

case SRTO_OHEADBW:
Expand All @@ -546,7 +546,7 @@ void CUDT::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
// (only if connected; if not, then the value
// from m_iOverheadBW will be used initially)
if (m_bConnected)
updateCC(TEV_INIT, TEV_INIT_OHEADBW);
updateCC(TEV_INIT, EventVariant(TEV_INIT_OHEADBW));
break;

case SRTO_SENDER:
Expand Down Expand Up @@ -2514,7 +2514,8 @@ bool CUDT::processSrtMsg(const CPacket *ctrlpkt)
LOGC(cnlog.Warn,
log << "KMREQ FAILURE: " << KmStateStr(SRT_KM_STATE(srtdata_out[0]))
<< " - rejecting per enforced encryption");
return false;
res = SRT_CMD_NONE;
break;
}
HLOGC(cnlog.Debug,
log << "MKREQ -> KMRSP FAILURE state: " << KmStateStr(SRT_KM_STATE(srtdata_out[0])));
Expand Down Expand Up @@ -6137,7 +6138,7 @@ SRT_REJECT_REASON CUDT::setupCC()
<< " rcvrate=" << m_iDeliveryRate << "p/s (" << m_iByteDeliveryRate << "B/S)"
<< " rtt=" << m_iRTT << " bw=" << m_iBandwidth);

if (!updateCC(TEV_INIT, TEV_INIT_RESET))
if (!updateCC(TEV_INIT, EventVariant(TEV_INIT_RESET)))
{
LOGC(rslog.Error, log << "setupCC: IPE: resrouces not yet initialized!");
return SRT_REJ_IPE;
Expand Down Expand Up @@ -8540,7 +8541,7 @@ void CUDT::processCtrlAck(const CPacket &ctrlpkt, const steady_clock::time_point
}

checkSndTimers(REGEN_KM);
updateCC(TEV_ACK, ackdata_seqno);
updateCC(TEV_ACK, EventVariant(ackdata_seqno));

enterCS(m_StatsLock);
++m_stats.recvACK;
Expand Down Expand Up @@ -8732,7 +8733,7 @@ void CUDT::processCtrl(const CPacket &ctrlpkt)
m_iRTTVar = avg_iir<4>(m_iRTTVar, abs(rtt - m_iRTT));
m_iRTT = avg_iir<8>(m_iRTT, rtt);

updateCC(TEV_ACKACK, ack);
updateCC(TEV_ACKACK, EventVariant(ack));

// This function will put a lock on m_RecvLock by itself, as needed.
// It must be done inside because this function reads the current time
Expand Down Expand Up @@ -8986,7 +8987,7 @@ void CUDT::processCtrl(const CPacket &ctrlpkt)
}
else
{
updateCC(TEV_CUSTOM, &ctrlpkt);
updateCC(TEV_CUSTOM, EventVariant(&ctrlpkt));
}
}
break;
Expand Down Expand Up @@ -9444,7 +9445,7 @@ std::pair<int, steady_clock::time_point> CUDT::packData(CPacket& w_packet)
// the CSndQueue::worker thread. All others are reported from
// CRcvQueue::worker. If you connect to this signal, make sure
// that you are aware of prospective simultaneous access.
updateCC(TEV_SEND, &w_packet);
updateCC(TEV_SEND, EventVariant(&w_packet));

// XXX This was a blocked code also originally in UDT. Probably not required.
// Left untouched for historical reasons.
Expand Down Expand Up @@ -9684,7 +9685,7 @@ int CUDT::processData(CUnit* in_unit)
}
#endif

updateCC(TEV_RECEIVE, &packet);
updateCC(TEV_RECEIVE, EventVariant(&packet));
++m_iPktCount;

const int pktsz = packet.getLength();
Expand Down Expand Up @@ -11118,7 +11119,7 @@ void CUDT::checkRexmitTimer(const steady_clock::time_point& currtime)

checkSndTimers(DONT_REGEN_KM);
const ECheckTimerStage stage = is_fastrexmit ? TEV_CHT_FASTREXMIT : TEV_CHT_REXMIT;
updateCC(TEV_CHECKTIMER, stage);
updateCC(TEV_CHECKTIMER, EventVariant(stage));

// immediately restart transmission
m_pSndQueue->m_pSndUList->update(this, CSndUList::DO_RESCHEDULE);
Expand All @@ -11127,7 +11128,7 @@ void CUDT::checkRexmitTimer(const steady_clock::time_point& currtime)
void CUDT::checkTimers()
{
// update CC parameters
updateCC(TEV_CHECKTIMER, TEV_CHT_INIT);
updateCC(TEV_CHECKTIMER, EventVariant(TEV_CHT_INIT));

const steady_clock::time_point currtime = steady_clock::now();

Expand Down

0 comments on commit d91e66f

Please sign in to comment.