Skip to content

Commit c3df536

Browse files
committed
Mark session defunct on TCP Client side when disconnecting with peer.
The TCP server on receiving a TCPDisconnect from the client marks the corresponding secure session as defunct. The TCP client should also mark its session defunct when proactively closing the connection with peer.
1 parent 47cec4e commit c3df536

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+
MarkSecureSessionDefunctForConnection(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+
MarkSecureSessionDefunctForConnection(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::MarkSecureSessionDefunctForConnection(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
1323+
{
1324+
// Mark the corresponding secure sessions as defunct
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 as defunct
1340+
session->MarkAsDefunct();
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
@@ -621,6 +621,10 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl
621621
return payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq) ||
622622
payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncRsp);
623623
}
624+
625+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
626+
void MarkSecureSessionDefunctForConnection(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
627+
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
624628
};
625629

626630
namespace MessagePacketBuffer {

0 commit comments

Comments
 (0)