Skip to content

Commit 4ad2876

Browse files
committed
TCP ConnectToPeer() API in Transport Manager
Currently, connecting to a peer is coupled with sending a message to the peer. This decouples the two and creates a clear API for connecting to a peer address. Goes along with the existing Disconnect() API. This would be essential during activation of retained sessions by solely connecting to the peer and associating with the retained session. Surface Connection completion and Closure callbacks and hook them through SessionManager(TransportMgr delegate) and CASESession. Modify ActiveConnectionState in TCPBase to hold state for each connection, so that it is able to handle the various control flow paths.
1 parent 42c3af2 commit 4ad2876

12 files changed

+650
-134
lines changed

src/lib/core/CHIPConfig.h

+14
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,20 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
16001600
#define CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED 0
16011601
#endif
16021602

1603+
/**
1604+
* @def CHIP_CONFIG_TCP_SUPPORT_ENABLED
1605+
*
1606+
* @brief
1607+
* Enable (1) or disable (0) support for TCP as a transport protocol for
1608+
* operational communications.
1609+
*
1610+
* When enabled, the node would advertise this support via DNS-SD advertisements to allow peers
1611+
* to know and select the appropriate transport for session establishment.
1612+
*/
1613+
#ifndef CHIP_CONFIG_TCP_SUPPORT_ENABLED
1614+
#define CHIP_CONFIG_TCP_SUPPORT_ENABLED (INET_CONFIG_ENABLE_TCP_ENDPOINT)
1615+
#endif
1616+
16031617
/**
16041618
* @}
16051619
*/

src/protocols/secure_channel/CASESession.cpp

+56-3
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,19 @@ CHIP_ERROR CASESession::Init(SessionManager & sessionManager, Credentials::Certi
443443

444444
ReturnErrorOnFailure(mCommissioningHash.Begin());
445445

446-
mDelegate = delegate;
446+
mDelegate = delegate;
447+
mSessionManager = &sessionManager;
448+
447449
ReturnErrorOnFailure(AllocateSecureSession(sessionManager, sessionEvictionHint));
448450

449451
mValidContext.Reset();
450452
mValidContext.mRequiredKeyUsages.Set(KeyUsageFlags::kDigitalSignature);
451453
mValidContext.mRequiredKeyPurposes.Set(KeyPurposeFlags::kServerAuth);
452454
mValidContext.mValidityPolicy = policy;
453455

456+
// Set callbacks for connection in sessionmanager
457+
sessionManager.SetConnectionCallbacks(HandleConnectionComplete, HandleConnectionClosed, this);
458+
454459
return CHIP_NO_ERROR;
455460
}
456461

@@ -530,8 +535,23 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
530535
ChipLogProgress(SecureChannel, "Initiating session on local FabricIndex %u from 0x" ChipLogFormatX64 " -> 0x" ChipLogFormatX64,
531536
static_cast<unsigned>(mFabricIndex), ChipLogValueX64(mLocalNodeId), ChipLogValueX64(mPeerNodeId));
532537

533-
err = SendSigma1();
534-
SuccessOrExit(err);
538+
// TODO: Need to look into unifying this call to invoke ConnectToPeer() for
539+
// all transports, including MRP.
540+
// For UDP based transports, this call would short-circuit back into
541+
// HandleConnectionComplete() callback.
542+
// Currently, TestCASESession is organized such that it requires synchronous
543+
// calls back from EstablishSession() over UDP. So, bifurcate TCP and MRP
544+
// calls here, for now.
545+
if (mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
546+
{
547+
err = sessionManager.ConnectToPeer(mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress());
548+
SuccessOrExit(err);
549+
}
550+
else
551+
{
552+
err = SendSigma1();
553+
SuccessOrExit(err);
554+
}
535555

536556
exit:
537557
if (err != CHIP_NO_ERROR)
@@ -637,6 +657,39 @@ CHIP_ERROR CASESession::RecoverInitiatorIpk()
637657
return CHIP_NO_ERROR;
638658
}
639659

660+
void CASESession::HandleConnectionComplete(void * context, Inet::TCPEndPoint * conObj, CHIP_ERROR conErr)
661+
{
662+
CHIP_ERROR err = CHIP_NO_ERROR;
663+
664+
VerifyOrReturn(context != nullptr);
665+
// VerifyOrReturn(peerAddr.GetTransportType() == Transport::Type::kTcp);
666+
667+
CASESession * caseSession = reinterpret_cast<CASESession *>(context);
668+
669+
// Send Sigma1 after connection is established for sessions over TCP
670+
err = caseSession->SendSigma1();
671+
if (err != CHIP_NO_ERROR)
672+
{
673+
caseSession->Clear();
674+
}
675+
}
676+
677+
void CASESession::HandleConnectionClosed(void * context, Inet::TCPEndPoint * conObj, CHIP_ERROR conErr)
678+
{
679+
// CHIP_ERROR err = CHIP_NO_ERROR;
680+
681+
VerifyOrReturn(context != nullptr);
682+
CASESession * caseSession = reinterpret_cast<CASESession *>(context);
683+
if (conErr == CHIP_NO_ERROR)
684+
{
685+
caseSession->Clear();
686+
}
687+
else
688+
{
689+
// Connection establishment failed
690+
}
691+
}
692+
640693
CHIP_ERROR CASESession::SendSigma1()
641694
{
642695
MATTER_TRACE_SCOPE("SendSigma1", "CASESession");

src/protocols/secure_channel/CASESession.h

+6
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
283283

284284
void InvalidateIfPendingEstablishmentOnFabric(FabricIndex fabricIndex);
285285

286+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
287+
static void HandleConnectionComplete(void * context, Inet::TCPEndPoint * conObj, CHIP_ERROR conErr);
288+
static void HandleConnectionClosed(void * context, Inet::TCPEndPoint * conObj, CHIP_ERROR conErr);
289+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
290+
286291
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
287292
void SetStopSigmaHandshakeAt(Optional<State> state) { mStopHandshakeAtState = state; }
288293
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
@@ -298,6 +303,7 @@ class DLL_EXPORT CASESession : public Messaging::UnsolicitedMessageHandler,
298303
uint8_t mIPK[kIPKSize];
299304

300305
SessionResumptionStorage * mSessionResumptionStorage = nullptr;
306+
SessionManager * mSessionManager = nullptr;
301307

302308
FabricTable * mFabricsTable = nullptr;
303309
FabricIndex mFabricIndex = kUndefinedFabricIndex;

src/transport/SessionManager.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ CHIP_ERROR SessionManager::Init(System::Layer * systemLayer, TransportMgrBase *
108108

109109
mTransportMgr->SetSessionManager(this);
110110

111+
mTransportMgr->SetSystemLayer(systemLayer);
112+
113+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
114+
mConnCompleteCb = nullptr;
115+
mConnClosedCb = nullptr;
116+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
117+
111118
return CHIP_NO_ERROR;
112119
}
113120

@@ -582,6 +589,65 @@ void SessionManager::OnMessageReceived(const PeerAddress & peerAddress, System::
582589
}
583590
}
584591

592+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
593+
void SessionManager::SetConnectionCallbacks(OnConnectionCompleteCallback connCompleteCb, OnConnectionClosedCallback connClosedCb,
594+
void * connContext)
595+
{
596+
mConnCompleteCb = connCompleteCb;
597+
mConnClosedCb = connClosedCb;
598+
mConnContext = connContext;
599+
}
600+
601+
void SessionManager::HandleConnectionComplete(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr)
602+
{
603+
if (mConnCompleteCb != nullptr)
604+
{
605+
ChipLogProgress(Inet, "Calling Connection Complete callback");
606+
mConnCompleteCb(mConnContext, conObj, conErr);
607+
}
608+
else
609+
{
610+
ChipLogProgress(Inet, "Connection Complete callback missing");
611+
}
612+
}
613+
614+
void SessionManager::HandleConnectionClosed(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr)
615+
{
616+
if (mConnClosedCb != nullptr)
617+
{
618+
mConnClosedCb(mConnContext, conObj, conErr);
619+
}
620+
else
621+
{
622+
ChipLogProgress(Inet, "Connection Closed callback missing");
623+
}
624+
}
625+
626+
CHIP_ERROR SessionManager::ConnectToPeer(const PeerAddress & peerAddress)
627+
{
628+
if (mTransportMgr != nullptr)
629+
{
630+
ChipLogProgress(Inet, "Connecting to peer ");
631+
return mTransportMgr->ConnectToPeer(peerAddress);
632+
}
633+
634+
ChipLogError(Inet, "The transport manager is not initialized. Unable to connect to peer");
635+
636+
return CHIP_ERROR_INCORRECT_STATE;
637+
}
638+
639+
CHIP_ERROR SessionManager::Disconnect(const PeerAddress & peerAddress)
640+
{
641+
if (mTransportMgr != nullptr)
642+
{
643+
ChipLogProgress(Inet, "Disconnecting from peer ");
644+
mTransportMgr->Disconnect(peerAddress);
645+
}
646+
647+
return CHIP_NO_ERROR;
648+
}
649+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
650+
585651
void SessionManager::UnauthenticatedMessageDispatch(const PacketHeader & partialPacketHeader,
586652
const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg)
587653
{

src/transport/SessionManager.h

+25
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,25 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl
416416
*/
417417
void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf) override;
418418

419+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
420+
CHIP_ERROR ConnectToPeer(const Transport::PeerAddress & peerAddress);
421+
422+
CHIP_ERROR Disconnect(const Transport::PeerAddress & peerAddress);
423+
424+
void HandleConnectionComplete(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr) override;
425+
426+
void HandleConnectionClosed(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr) override;
427+
428+
// Functors for callbacks into higher layers
429+
using OnConnectionCompleteCallback = void (*)(void * appContext, Inet::TCPEndPoint * conObj, CHIP_ERROR conErr);
430+
431+
using OnConnectionClosedCallback = void (*)(void * appContext, Inet::TCPEndPoint * conObj, CHIP_ERROR conErr);
432+
433+
// Set the higher layer callbacks
434+
void SetConnectionCallbacks(OnConnectionCompleteCallback connCompleteCb, OnConnectionClosedCallback connClosedCb,
435+
void * connContext);
436+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
437+
419438
Optional<SessionHandle> CreateUnauthenticatedSession(const Transport::PeerAddress & peerAddress,
420439
const ReliableMessageProtocolConfig & config)
421440
{
@@ -477,6 +496,12 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl
477496
State mState; // < Initialization state of the object
478497
chip::Transport::GroupOutgoingCounters mGroupClientCounter;
479498

499+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
500+
OnConnectionCompleteCallback mConnCompleteCb = nullptr;
501+
OnConnectionClosedCallback mConnClosedCb = nullptr;
502+
void * mConnContext = nullptr;
503+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
504+
480505
SessionMessageDelegate * mCB = nullptr;
481506

482507
TransportMgrBase * mTransportMgr = nullptr;

src/transport/TransportMgr.h

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ class TransportMgrDelegate
5151
* @param msgBuf the buffer containing a full CHIP message (except for the optional length field).
5252
*/
5353
virtual void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf) = 0;
54+
55+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
56+
/**
57+
* @brief
58+
* Handle connection completion.
59+
*
60+
* @param conObj the connection object
61+
* @param conErr the connection error on the attempt.
62+
*/
63+
virtual void HandleConnectionComplete(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr){};
64+
65+
virtual void HandleConnectionClosed(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr){};
66+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
5467
};
5568

5669
template <typename... TransportTypes>

src/transport/TransportMgrBase.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <platform/LockTracker.h>
2121
#include <transport/TransportMgr.h>
2222
#include <transport/raw/Base.h>
23+
#include <transport/raw/TCP.h>
2324

2425
namespace chip {
2526

@@ -28,10 +29,17 @@ CHIP_ERROR TransportMgrBase::SendMessage(const Transport::PeerAddress & address,
2829
return mTransport->SendMessage(address, std::move(msgBuf));
2930
}
3031

32+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
33+
CHIP_ERROR TransportMgrBase::ConnectToPeer(const Transport::PeerAddress & address)
34+
{
35+
return mTransport->ConnectToPeer(address);
36+
}
37+
3138
void TransportMgrBase::Disconnect(const Transport::PeerAddress & address)
3239
{
3340
mTransport->Disconnect(address);
3441
}
42+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
3543

3644
CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
3745
{
@@ -41,6 +49,7 @@ CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
4149
}
4250
mTransport = transport;
4351
mTransport->SetDelegate(this);
52+
4453
ChipLogDetail(Inet, "TransportMgr initialized");
4554
return CHIP_NO_ERROR;
4655
}
@@ -83,4 +92,40 @@ void TransportMgrBase::HandleMessageReceived(const Transport::PeerAddress & peer
8392
}
8493
}
8594

95+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
96+
void TransportMgrBase::HandleConnectionComplete(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr)
97+
{
98+
if (mSessionManager != nullptr)
99+
{
100+
mSessionManager->HandleConnectionComplete(conObj, conErr);
101+
}
102+
else
103+
{
104+
Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conObj->mAppState);
105+
106+
// Close connection here
107+
if (tcp)
108+
{
109+
tcp->Disconnect(conObj);
110+
}
111+
}
112+
}
113+
114+
void TransportMgrBase::HandleConnectionClosed(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr)
115+
{
116+
if (mSessionManager != nullptr)
117+
{
118+
mSessionManager->HandleConnectionClosed(conObj, conErr);
119+
}
120+
else
121+
{
122+
Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conObj->mAppState);
123+
if (tcp)
124+
{
125+
tcp->Disconnect(conObj);
126+
}
127+
}
128+
}
129+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
130+
86131
} // namespace chip

src/transport/TransportMgrBase.h

+12
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ class TransportMgrBase : public Transport::RawTransportDelegate
3939

4040
void Close();
4141

42+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
43+
CHIP_ERROR ConnectToPeer(const Transport::PeerAddress & address);
44+
4245
void Disconnect(const Transport::PeerAddress & address);
4346

47+
void HandleConnectionComplete(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr) override;
48+
49+
void HandleConnectionClosed(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr) override;
50+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
51+
52+
void SetSystemLayer(System::Layer * systemLayer) { mTransport->SetSystemLayer(systemLayer); }
53+
4454
void SetSessionManager(TransportMgrDelegate * sessionManager) { mSessionManager = sessionManager; }
4555

56+
TransportMgrDelegate * GetSessionManager() { return mSessionManager; };
57+
4658
CHIP_ERROR MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join);
4759

4860
void HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg) override;

0 commit comments

Comments
 (0)