Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Added rejection handshake sent to the peer in rendezvous mode #2667

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
58 changes: 57 additions & 1 deletion srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,9 @@ bool srt::CUDT::interpretSrtHandshake(const CHandShake& hs,
{
// Cryptographic modes mismatch. Not acceptable at all.
m_RejectReason = SRT_REJ_CRYPTO;
LOGC(cnlog.Error,
log << CONID()
<< "interpretSrtHandshake: KMREQ result: Bad crypto mode - rejecting");
return false;
}
#endif
Expand Down Expand Up @@ -3682,11 +3685,23 @@ void srt::CUDT::startConnect(const sockaddr_any& serv_addr, int32_t forced_isn)
cst = processRendezvous(&response, serv_addr, RST_OK, (reqpkt));
if (cst == CONN_CONTINUE)
continue;
break;

// Just in case it wasn't set, set this as a fallback
if (m_RejectReason == SRT_REJ_UNKNOWN)
m_RejectReason = SRT_REJ_ROGUE;

// rejection or erroneous code.
reqpkt.setLength(m_iMaxSRTPayloadSize);
reqpkt.setControl(UMSG_HANDSHAKE);
sendRendezvousRejection(serv_addr, (reqpkt));
}

if (cst == CONN_REJECT)
{
HLOGC(cnlog.Debug,
log << CONID() << "startConnect: REJECTED by processConnectResponse - sending SHUTDOWN");
sendCtrl(UMSG_SHUTDOWN);
}

if (cst != CONN_CONTINUE && cst != CONN_CONFUSED)
break; // --> OUTSIDE-LOOP
Expand Down Expand Up @@ -3873,6 +3888,11 @@ bool srt::CUDT::processAsyncConnectRequest(EReadStatus rst,
LOGC(cnlog.Warn,
log << CONID()
<< "processAsyncConnectRequest: REJECT reported from processRendezvous, not processing further.");

if (m_RejectReason == SRT_REJ_UNKNOWN)
m_RejectReason = SRT_REJ_ROGUE;

sendRendezvousRejection(serv_addr, (request));
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
status = false;
}
}
Expand Down Expand Up @@ -3925,6 +3945,24 @@ bool srt::CUDT::processAsyncConnectRequest(EReadStatus rst,
return status;
}

void srt::CUDT::sendRendezvousRejection(const sockaddr_any& serv_addr, CPacket& r_rsppkt)
{
// We can reuse m_ConnReq because we are about to abandon the connection process.
m_ConnReq.m_iReqType = URQFailure(m_RejectReason);

// Assumed that r_rsppkt refers to a packet object that was already prepared
// to be used for storing the handshake there.
size_t size = r_rsppkt.getLength();
m_ConnReq.store_to((r_rsppkt.m_pcData), (size));
r_rsppkt.setLength(size);

HLOGC(cnlog.Debug, log << CONID() << "sendRendezvousRejection: using code=" << m_ConnReq.m_iReqType
<< " for reject reason code " << m_RejectReason << " (" << srt_rejectreason_str(m_RejectReason) << ")");

setPacketTS(r_rsppkt, steady_clock::now());
m_pSndQueue->sendto(serv_addr, r_rsppkt, m_SourceAddr);
}

void srt::CUDT::cookieContest()
{
if (m_SrtHsSide != HSD_DRAW)
Expand Down Expand Up @@ -4427,7 +4465,25 @@ EConnectStatus srt::CUDT::processConnectResponse(const CPacket& response, CUDTEx
<< "processConnectResponse: CONFUSED: expected UMSG_HANDSHAKE as connection not yet established, "
"got: "
<< MessageTypeStr(response.getType(), response.getExtendedType()));

if (response.getType() == UMSG_SHUTDOWN)
{
LOGC(cnlog.Error,
log << CONID() << "processConnectResponse: UMSG_SHUTDOWN received, rejecting connection.");
return CONN_REJECT;
}
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
}

if (m_config.bRendezvous)
{
// In rendezvous mode we expect that both sides are known
// to the service operator (unlike a listener, which may
// operate connections from unknown sources). This means that
// the connection process should be terminated anyway, on
// whichever side it would happen.
return CONN_REJECT;
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
}

return CONN_CONFUSED;
}

Expand Down
1 change: 1 addition & 0 deletions srtcore/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class CUDT
/// @param rst Current read status to know if the HS packet was freshly received from the peer, or this is only a periodic update (RST_AGAIN)
SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
EConnectStatus processRendezvous(const CPacket* response, const sockaddr_any& serv_addr, EReadStatus, CPacket& reqpkt);
void sendRendezvousRejection(const sockaddr_any& serv_addr, CPacket& request);

/// Create the CryptoControl object based on the HS packet. Allocates sender and receiver buffers and loss lists.
SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
Expand Down
3 changes: 3 additions & 0 deletions srtcore/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ std::string KmStateStr(SRT_KM_STATE state)
TAKE(SECURING);
TAKE(NOSECRET);
TAKE(BADSECRET);
#ifdef ENABLE_AEAD_API_PREVIEW
TAKE(BADCRYPTOMODE);
#endif
#undef TAKE
default:
{
Expand Down
7 changes: 4 additions & 3 deletions testing/testmedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FileTarget: public virtual Target
{
ofile.write(data.payload.data(), data.payload.size());
#ifdef PLEASE_LOG
applog.Debug() << "FileTarget::Write: " << data.size() << " written to a file";
applog.Debug() << "FileTarget::Write: " << data.payload.size() << " written to a file";
#endif
}

Expand Down Expand Up @@ -1297,7 +1297,8 @@ void SrtCommon::ConnectClient(string host, int port)
{
int reason = srt_getrejectreason(m_sock);
#if PLEASE_LOG
LOGP(applog.Error, "ERROR reported by srt_connect - closing socket @", m_sock);
LOGP(applog.Error, "ERROR reported by srt_connect - closing socket @", m_sock,
" reject reason: ", reason, ": ", srt_rejectreason_str(reason));
#endif
if (transmit_retry_connect && (transmit_retry_always || reason == SRT_REJ_TIMEOUT))
{
Expand Down Expand Up @@ -2360,7 +2361,7 @@ MediaPacket SrtSource::Read(size_t chunk)
#if PLEASE_LOG
extern srt_logging::Logger applog;
LOGC(applog.Debug, log << "recv: #" << mctrl.msgno << " %" << mctrl.pktseq << " "
<< BufferStamp(data.data(), stat) << " BELATED: " << ((CTimer::getTime()-mctrl.srctime)/1000.0) << "ms");
<< BufferStamp(data.data(), stat) << " BELATED: " << ((srt_time_now()-mctrl.srctime)/1000.0) << "ms");
#endif

Verb() << "(#" << mctrl.msgno << " %" << mctrl.pktseq << " " << BufferStamp(data.data(), stat) << ") " << VerbNoEOL;
Expand Down