Skip to content

Commit

Permalink
[core] Added rejection handshake sent to the peer in rendezvous mode (#…
Browse files Browse the repository at this point in the history
…2667).

Fixed: send SHUTDOWN also in blocking mode.
Added fallback ROGUE reject reason case it isn't set.
  • Loading branch information
ethouris authored Feb 17, 2023
1 parent 46d55c6 commit 1cffd2f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
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));
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;
}
}

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;
}

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 @@ -1316,7 +1316,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 @@ -2379,7 +2380,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

0 comments on commit 1cffd2f

Please sign in to comment.