Skip to content

Commit aea49ed

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 aea49ed

12 files changed

+666
-131
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

+57-2
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,18 @@ CHIP_ERROR CASESession::Init(SessionManager & sessionManager, Credentials::Certi
444444
ReturnErrorOnFailure(mCommissioningHash.Begin());
445445

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

536557
exit:
537558
if (err != CHIP_NO_ERROR)
@@ -637,6 +658,40 @@ CHIP_ERROR CASESession::RecoverInitiatorIpk()
637658
return CHIP_NO_ERROR;
638659
}
639660

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

+67
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,66 @@ void SessionManager::OnMessageReceived(const PeerAddress & peerAddress, System::
582589
}
583590
}
584591

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

src/transport/SessionManager.h

+27
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl
407407
TransportMgrBase * GetTransportManager() const { return mTransportMgr; }
408408
Transport::SecureSessionTable & GetSecureSessions() { return mSecureSessions; }
409409

410+
410411
/**
411412
* @brief
412413
* Handle received secure message. Implements TransportMgrDelegate
@@ -416,6 +417,26 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl
416417
*/
417418
void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf) override;
418419

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

501+
#if CHIP_CONFIG_TCP_SUPPORT_ENABLED
502+
OnConnectionCompleteCallback mConnCompleteCb = nullptr;
503+
OnConnectionClosedCallback mConnClosedCb = nullptr;
504+
void * mConnContext = nullptr;
505+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
506+
480507
SessionMessageDelegate * mCB = nullptr;
481508

482509
TransportMgrBase * mTransportMgr = nullptr;

src/transport/TransportMgr.h

+14
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ 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+
66+
virtual void HandleConnectionClosed(Inet::TCPEndPoint * conObj, CHIP_ERROR conErr) {};
67+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
5468
};
5569

5670
template <typename... TransportTypes>

src/transport/TransportMgrBase.cpp

+46
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,41 @@ 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+
}
130+
#endif // CHIP_CONFIG_TCP_SUPPORT_ENABLED
131+
86132
} // 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)