Skip to content

Commit 138fb4f

Browse files
authored
Mark session for eviction on TCP Client side when disconnecting with peer. (project-chip#34451)
The TCP server on receiving a TCPDisconnect from the client marks the corresponding secure session for eviction. The TCP client should also mark its session for eviction when proactively closing the connection with peer.
1 parent 8e32ce7 commit 138fb4f

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/transport/SessionManager.cpp

+30-21
Original file line numberDiff line numberDiff line change
@@ -714,27 +714,7 @@ void SessionManager::HandleConnectionClosed(Transport::ActiveTCPConnectionState
714714
{
715715
VerifyOrReturn(conn != nullptr);
716716

717-
// Mark the corresponding secure sessions as defunct
718-
mSecureSessions.ForEachSession([&](auto session) {
719-
if (session->IsActiveSession() && session->GetTCPConnection() == conn)
720-
{
721-
SessionHandle handle(*session);
722-
// Notify the SessionConnection delegate of the connection
723-
// closure.
724-
if (mConnDelegate != nullptr)
725-
{
726-
mConnDelegate->OnTCPConnectionClosed(handle, conErr);
727-
}
728-
729-
// Dis-associate the connection from session by setting it to a
730-
// nullptr.
731-
session->SetTCPConnection(nullptr);
732-
// Mark session as defunct
733-
session->MarkAsDefunct();
734-
}
735-
736-
return Loop::Continue;
737-
});
717+
MarkSecureSessionOverTCPForEviction(conn, conErr);
738718

739719
// TODO: A mechanism to mark an unauthenticated session as unusable when
740720
// the underlying connection is broken. Issue #32323
@@ -785,6 +765,8 @@ void SessionManager::TCPDisconnect(Transport::ActiveTCPConnectionState * conn, b
785765
conn->mPeerAddr.ToString(peerAddrBuf);
786766
ChipLogProgress(Inet, "Disconnecting TCP connection from peer at %s.", peerAddrBuf);
787767
mTransportMgr->TCPDisconnect(conn, shouldAbort);
768+
769+
MarkSecureSessionOverTCPForEviction(conn, CHIP_NO_ERROR);
788770
}
789771
}
790772
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
@@ -1336,6 +1318,33 @@ Optional<SessionHandle> SessionManager::FindSecureSessionForNode(ScopedNodeId pe
13361318
return mrpSession != nullptr ? MakeOptional<SessionHandle>(*mrpSession) : Optional<SessionHandle>::Missing();
13371319
}
13381320

1321+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
1322+
void SessionManager::MarkSecureSessionOverTCPForEviction(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
1323+
{
1324+
// Mark the corresponding secure sessions for eviction
1325+
mSecureSessions.ForEachSession([&](auto session) {
1326+
if (session->IsActiveSession() && session->GetTCPConnection() == conn)
1327+
{
1328+
SessionHandle handle(*session);
1329+
// Notify the SessionConnection delegate of the connection
1330+
// closure.
1331+
if (mConnDelegate != nullptr)
1332+
{
1333+
mConnDelegate->OnTCPConnectionClosed(handle, conErr);
1334+
}
1335+
1336+
// Dis-associate the connection from session by setting it to a
1337+
// nullptr.
1338+
session->SetTCPConnection(nullptr);
1339+
// Mark session for eviction.
1340+
session->MarkForEviction();
1341+
}
1342+
1343+
return Loop::Continue;
1344+
});
1345+
}
1346+
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
1347+
13391348
/**
13401349
* Provides a means to get diagnostic information such as number of sessions.
13411350
*/

src/transport/SessionManager.h

+4
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl
616616

617617
void OnReceiveError(CHIP_ERROR error, const Transport::PeerAddress & source);
618618

619+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
620+
void MarkSecureSessionOverTCPForEviction(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
621+
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
622+
619623
static bool IsControlMessage(PayloadHeader & payloadHeader)
620624
{
621625
return payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq) ||

0 commit comments

Comments
 (0)