Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions srtcore/buffer_rcv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ void CRcvBuffer::insertAt(int offset, int pos, CUnit* unit)
updateNonreadPos();
}

void CRcvBuffer::dropAt(int offset, int pos)
{
SRT_ASSERT(offset >= 0 && offset < (int)capacity());
SRT_ASSERT(pos == (m_iStartPos + offset) % int(m_szSize));
SRT_ASSERT(m_entries[pos].status == EntryState_Empty);
SRT_ASSERT(m_entries[pos].pUnit == NULL);

if (offset >= m_iMaxPosOff)
m_iMaxPosOff = offset + 1;

m_entries[pos].status = EntryState_Drop;

// Same as in dropMessage(): check if the dropped cell is before m_iFirstNonreadPos.
const bool needUpdateNonreadPos = (offset <= getRcvDataSize());
releaseNextFillerEntries();
if (needUpdateNonreadPos)
{
m_iFirstNonreadPos = m_iStartPos;
updateNonreadPos();
}
}

std::pair<int, int> CRcvBuffer::dropUpTo(int32_t seqno)
{
IF_RCVBUF_DEBUG(ScopedLog scoped_log);
Expand Down
4 changes: 4 additions & 0 deletions srtcore/buffer_rcv.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class CRcvBuffer
/// Insert a unit into a free cell found by findInsertPos(), without verification.
void insertAt(int offset, int pos, CUnit* unit);

/// Mark a free cell found by findInsertPos() as dropped, as if a packet had been
/// inserted there and then dropped. This allows the acknowledgement to move past it.
void dropAt(int offset, int pos);

/// Drop packets in the receiver buffer from the current position up to the seqno (excluding seqno).
/// @param [in] seqno drop units up to this sequence number
/// @return number of dropped (missing) and discarded (available) packets as a pair(dropped, discarded).
Expand Down
48 changes: 38 additions & 10 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10974,8 +10974,12 @@ int srt::CUDT::handleSocketPacketReception(const vector<CUnit*>& incoming, bool&
// never been received, so that it is recovered by the regular loss handling
// (retransmission or packet filter) (see #2626 and #3392).
// The cell is located once: the buffer can't change while m_RcvBufferLock is held.
// An exception is a live stream when the key is still pending (SRT_KM_S_SECURING):
// a pre-1.3.0 (HSv4) peer sends its key only after it receives an ACK, so the packet
// is treated as received and dropped, the same way as in 1.5.7, for the ACK to advance.
bool decrypt_successful = false;
bool rejected = false;
bool drop_undecrypted = false;
const char* reject_reason SRT_ATR_UNUSED = "";
int buf_offset, buf_pos;
const int cell_status = m_pRcvBuffer->findInsertPos(rpkt.seqno(), (buf_offset), (buf_pos));
Expand All @@ -10990,8 +10994,19 @@ int srt::CUDT::handleSocketPacketReception(const vector<CUnit*>& incoming, bool&
u->m_Packet.setRexmitFlag(retransmitted); // Recover the flag.

decrypt_successful = (rc == ENCS_CLEAR);
rejected = !decrypt_successful;
reject_reason = "Decryption failed";
if (!decrypt_successful)
{
if (m_bTsbPd && m_pCryptoControl && m_pCryptoControl->m_RcvKmState == SRT_KM_S_SECURING)
{
drop_undecrypted = true;
reject_reason = "Decryption key not yet received";
}
else
{
rejected = true;
reject_reason = "Decryption failed";
}
}
}
else if (m_pCryptoControl && m_pCryptoControl->m_RcvKmState != SRT_KM_S_UNSECURED)
{
Expand All @@ -11008,23 +11023,36 @@ int srt::CUDT::handleSocketPacketReception(const vector<CUnit*>& incoming, bool&
IF_HEAVY_LOGGING(exc_type = "UNACKED");
adding_successful = false;
}
else if (rejected)
else if (rejected || drop_undecrypted)
{
adding_successful = false;
IF_HEAVY_LOGGING(exc_type = "UNDECRYPTED");

// The packet filter has already seen this packet: make it forget it.
// The packet is left unmodified by a failed decryption.
if (m_PacketFilter)
m_PacketFilter.forget(rpkt, (w_filter_loss_seqs));
if (drop_undecrypted)
{
IF_HEAVY_LOGGING(exc_type = "DROPPED");
m_pRcvBuffer->dropAt(buf_offset, buf_pos);
w_new_inserted = true;
}
else
{
IF_HEAVY_LOGGING(exc_type = "UNDECRYPTED");

// The packet filter has already seen this packet: make it forget it.
// The packet is left unmodified by a failed decryption.
if (m_PacketFilter)
m_PacketFilter.forget(rpkt, (w_filter_loss_seqs));
}

const steady_clock::time_point tnow = steady_clock::now();
ScopedLock lg(m_StatsLock);
m_stats.rcvr.undecrypted.count(stats::BytesPackets(rpkt.getLength(), 1));
if (drop_undecrypted)
m_stats.rcvr.dropped.count(stats::BytesPackets(rpkt.getLength(), 1));
string why;
if (frequentLogAllowed(FREQLOGFA_ENCRYPTION_FAILURE, tnow, (why)))
{
LOGC(qrlog.Warn, log << CONID() << reject_reason << " (seqno %" << u->m_Packet.getSeqNo() << "), ignored"
LOGC(qrlog.Warn, log << CONID() << reject_reason << " (seqno %" << u->m_Packet.getSeqNo() << "), "
<< (drop_undecrypted ? "dropped" : "ignored")
<< ". pktRcvUndecryptTotal=" << m_stats.rcvr.undecrypted.total.count() << "." << why);
}
#if SRT_ENABLE_FREQUENT_LOG_TRACE
Expand Down Expand Up @@ -11091,7 +11119,7 @@ int srt::CUDT::handleSocketPacketReception(const vector<CUnit*>& incoming, bool&

// Decryption should have made the crypto flags EK_NOENC.
// Otherwise it's an error.
if (adding_successful)
if (adding_successful || drop_undecrypted)
{
HLOGC(qrlog.Debug,
log << CONID()
Expand Down
30 changes: 30 additions & 0 deletions test/test_buffer_rcv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,36 @@ TEST_F(CRcvBufferReadMsg, InsertAt)
EXPECT_TRUE(verifyPayload(buff.data(), m_payload_sz, m_init_seqno));
}

TEST_F(CRcvBufferReadMsg, DropAt)
{
auto& rcv_buffer = *m_rcv_buffer.get();
const int32_t seq1 = CSeqNo::incseq(m_init_seqno);
const int32_t seq2 = CSeqNo::incseq(seq1);
int offset = -1, pos = -1;

// Dropping a free cell at the head moves the buffer (and the ACK point) past it.
ASSERT_EQ(rcv_buffer.findInsertPos(m_init_seqno, offset, pos), 0);
rcv_buffer.dropAt(offset, pos);
EXPECT_EQ(rcv_buffer.getStartSeqNo(), seq1);
EXPECT_EQ(rcv_buffer.getFirstNonreadSeqNo(), seq1);
EXPECT_TRUE(rcv_buffer.empty());

// Dropping a cell after a gap keeps it occupied until the gap is filled and read.
ASSERT_EQ(rcv_buffer.findInsertPos(seq2, offset, pos), 0);
rcv_buffer.dropAt(offset, pos);
EXPECT_EQ(rcv_buffer.getStartSeqNo(), seq1);
EXPECT_EQ(rcv_buffer.getFirstNonreadSeqNo(), seq1);
EXPECT_EQ(rcv_buffer.findInsertPos(seq2, offset, pos), -1);

EXPECT_EQ(addMessage(1, 2, seq1), 0);
array<char, m_payload_sz> buff;
EXPECT_EQ(readMessage(buff.data(), buff.size()), int(m_payload_sz));
EXPECT_TRUE(verifyPayload(buff.data(), m_payload_sz, seq1));
EXPECT_EQ(rcv_buffer.getStartSeqNo(), CSeqNo::incseq(seq2));
EXPECT_TRUE(rcv_buffer.empty());
EXPECT_EQ(m_unit_queue->size(), m_unit_queue->capacity());
}

TEST_F(CRcvBufferReadMsg, PacketDropBySeqNo)
{
// Add two packets.
Expand Down
Loading