Skip to content

Commit 1cffd2f

Browse files
authored
[core] Added rejection handshake sent to the peer in rendezvous mode (#2667).
Fixed: send SHUTDOWN also in blocking mode. Added fallback ROGUE reject reason case it isn't set.
1 parent 46d55c6 commit 1cffd2f

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

srtcore/core.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,9 @@ bool srt::CUDT::interpretSrtHandshake(const CHandShake& hs,
26292629
{
26302630
// Cryptographic modes mismatch. Not acceptable at all.
26312631
m_RejectReason = SRT_REJ_CRYPTO;
2632+
LOGC(cnlog.Error,
2633+
log << CONID()
2634+
<< "interpretSrtHandshake: KMREQ result: Bad crypto mode - rejecting");
26322635
return false;
26332636
}
26342637
#endif
@@ -3682,11 +3685,23 @@ void srt::CUDT::startConnect(const sockaddr_any& serv_addr, int32_t forced_isn)
36823685
cst = processRendezvous(&response, serv_addr, RST_OK, (reqpkt));
36833686
if (cst == CONN_CONTINUE)
36843687
continue;
3685-
break;
3688+
3689+
// Just in case it wasn't set, set this as a fallback
3690+
if (m_RejectReason == SRT_REJ_UNKNOWN)
3691+
m_RejectReason = SRT_REJ_ROGUE;
3692+
3693+
// rejection or erroneous code.
3694+
reqpkt.setLength(m_iMaxSRTPayloadSize);
3695+
reqpkt.setControl(UMSG_HANDSHAKE);
3696+
sendRendezvousRejection(serv_addr, (reqpkt));
36863697
}
36873698

36883699
if (cst == CONN_REJECT)
3700+
{
3701+
HLOGC(cnlog.Debug,
3702+
log << CONID() << "startConnect: REJECTED by processConnectResponse - sending SHUTDOWN");
36893703
sendCtrl(UMSG_SHUTDOWN);
3704+
}
36903705

36913706
if (cst != CONN_CONTINUE && cst != CONN_CONFUSED)
36923707
break; // --> OUTSIDE-LOOP
@@ -3873,6 +3888,11 @@ bool srt::CUDT::processAsyncConnectRequest(EReadStatus rst,
38733888
LOGC(cnlog.Warn,
38743889
log << CONID()
38753890
<< "processAsyncConnectRequest: REJECT reported from processRendezvous, not processing further.");
3891+
3892+
if (m_RejectReason == SRT_REJ_UNKNOWN)
3893+
m_RejectReason = SRT_REJ_ROGUE;
3894+
3895+
sendRendezvousRejection(serv_addr, (request));
38763896
status = false;
38773897
}
38783898
}
@@ -3925,6 +3945,24 @@ bool srt::CUDT::processAsyncConnectRequest(EReadStatus rst,
39253945
return status;
39263946
}
39273947

3948+
void srt::CUDT::sendRendezvousRejection(const sockaddr_any& serv_addr, CPacket& r_rsppkt)
3949+
{
3950+
// We can reuse m_ConnReq because we are about to abandon the connection process.
3951+
m_ConnReq.m_iReqType = URQFailure(m_RejectReason);
3952+
3953+
// Assumed that r_rsppkt refers to a packet object that was already prepared
3954+
// to be used for storing the handshake there.
3955+
size_t size = r_rsppkt.getLength();
3956+
m_ConnReq.store_to((r_rsppkt.m_pcData), (size));
3957+
r_rsppkt.setLength(size);
3958+
3959+
HLOGC(cnlog.Debug, log << CONID() << "sendRendezvousRejection: using code=" << m_ConnReq.m_iReqType
3960+
<< " for reject reason code " << m_RejectReason << " (" << srt_rejectreason_str(m_RejectReason) << ")");
3961+
3962+
setPacketTS(r_rsppkt, steady_clock::now());
3963+
m_pSndQueue->sendto(serv_addr, r_rsppkt, m_SourceAddr);
3964+
}
3965+
39283966
void srt::CUDT::cookieContest()
39293967
{
39303968
if (m_SrtHsSide != HSD_DRAW)
@@ -4427,7 +4465,25 @@ EConnectStatus srt::CUDT::processConnectResponse(const CPacket& response, CUDTEx
44274465
<< "processConnectResponse: CONFUSED: expected UMSG_HANDSHAKE as connection not yet established, "
44284466
"got: "
44294467
<< MessageTypeStr(response.getType(), response.getExtendedType()));
4468+
4469+
if (response.getType() == UMSG_SHUTDOWN)
4470+
{
4471+
LOGC(cnlog.Error,
4472+
log << CONID() << "processConnectResponse: UMSG_SHUTDOWN received, rejecting connection.");
4473+
return CONN_REJECT;
4474+
}
44304475
}
4476+
4477+
if (m_config.bRendezvous)
4478+
{
4479+
// In rendezvous mode we expect that both sides are known
4480+
// to the service operator (unlike a listener, which may
4481+
// operate connections from unknown sources). This means that
4482+
// the connection process should be terminated anyway, on
4483+
// whichever side it would happen.
4484+
return CONN_REJECT;
4485+
}
4486+
44314487
return CONN_CONFUSED;
44324488
}
44334489

srtcore/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ class CUDT
485485
/// @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)
486486
SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
487487
EConnectStatus processRendezvous(const CPacket* response, const sockaddr_any& serv_addr, EReadStatus, CPacket& reqpkt);
488+
void sendRendezvousRejection(const sockaddr_any& serv_addr, CPacket& request);
488489

489490
/// Create the CryptoControl object based on the HS packet. Allocates sender and receiver buffers and loss lists.
490491
SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)

srtcore/crypto.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ std::string KmStateStr(SRT_KM_STATE state)
5757
TAKE(SECURING);
5858
TAKE(NOSECRET);
5959
TAKE(BADSECRET);
60+
#ifdef ENABLE_AEAD_API_PREVIEW
61+
TAKE(BADCRYPTOMODE);
62+
#endif
6063
#undef TAKE
6164
default:
6265
{

testing/testmedia.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class FileTarget: public virtual Target
144144
{
145145
ofile.write(data.payload.data(), data.payload.size());
146146
#ifdef PLEASE_LOG
147-
applog.Debug() << "FileTarget::Write: " << data.size() << " written to a file";
147+
applog.Debug() << "FileTarget::Write: " << data.payload.size() << " written to a file";
148148
#endif
149149
}
150150

@@ -1316,7 +1316,8 @@ void SrtCommon::ConnectClient(string host, int port)
13161316
{
13171317
int reason = srt_getrejectreason(m_sock);
13181318
#if PLEASE_LOG
1319-
LOGP(applog.Error, "ERROR reported by srt_connect - closing socket @", m_sock);
1319+
LOGP(applog.Error, "ERROR reported by srt_connect - closing socket @", m_sock,
1320+
" reject reason: ", reason, ": ", srt_rejectreason_str(reason));
13201321
#endif
13211322
if (transmit_retry_connect && (transmit_retry_always || reason == SRT_REJ_TIMEOUT))
13221323
{
@@ -2379,7 +2380,7 @@ MediaPacket SrtSource::Read(size_t chunk)
23792380
#if PLEASE_LOG
23802381
extern srt_logging::Logger applog;
23812382
LOGC(applog.Debug, log << "recv: #" << mctrl.msgno << " %" << mctrl.pktseq << " "
2382-
<< BufferStamp(data.data(), stat) << " BELATED: " << ((CTimer::getTime()-mctrl.srctime)/1000.0) << "ms");
2383+
<< BufferStamp(data.data(), stat) << " BELATED: " << ((srt_time_now()-mctrl.srctime)/1000.0) << "ms");
23832384
#endif
23842385

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

0 commit comments

Comments
 (0)