Skip to content

Commit

Permalink
Exchange handles SecureSession closing
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Nov 18, 2020
1 parent 53ee104 commit 5298873
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
DispatchMessage(packetHeader, payloadHeader, msgBuf);
}

void ExchangeManager::OnConnectionExpired(Transport::PeerConnectionState * state, SecureSessionMgrBase * mgr)
{
// Search for an existing exchange that the message applies to. If a match is found...
ExchangeContext * ec = ContextPool;
for (int i = 0; i < CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS; i++, ec++)
{
if (ec->GetReferenceCount() > 0 && ec->mPeerNodeId == state->GetPeerNodeId())
{
ec->Close();
}
}
}

void ExchangeManager::IncrementContextsInUse()
{
mContextsInUse++;
Expand Down
2 changes: 2 additions & 0 deletions src/messaging/ExchangeMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate
void OnMessageReceived(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
Transport::PeerConnectionState * state, System::PacketBuffer * msgBuf,
SecureSessionMgrBase * msgLayer) override;

void OnConnectionExpired(Transport::PeerConnectionState * state, SecureSessionMgrBase * mgr) override;
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/transport/PeerConnections.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class PeerConnections
*
*/
template <class T>
void SetConnectionExpiredHandler(void (*handler)(const PeerConnectionState &, T *), T * param)
void SetConnectionExpiredHandler(void (*handler)(PeerConnectionState &, T *), T * param)
{
mConnectionExpiredArgument = param;
OnConnectionExpired = reinterpret_cast<ConnectionExpiredHandler>(handler);
Expand Down
7 changes: 6 additions & 1 deletion src/transport/SecureSessionMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,18 @@ void SecureSessionMgrBase::HandleDataReceived(const PacketHeader & packetHeader,
}
}

void SecureSessionMgrBase::HandleConnectionExpired(const Transport::PeerConnectionState & state, SecureSessionMgrBase * mgr)
void SecureSessionMgrBase::HandleConnectionExpired(Transport::PeerConnectionState & state, SecureSessionMgrBase * mgr)
{
char addr[Transport::PeerAddress::kMaxToStringSize];
state.GetPeerAddress().ToString(addr, sizeof(addr));

ChipLogDetail(Inet, "Connection from '%s' expired", addr);

if (mgr->mCB != nullptr)
{
mgr->mCB->OnConnectionExpired(&state, mgr);
}

mgr->mTransport->Disconnect(state.GetPeerAddress());
}

Expand Down
11 changes: 10 additions & 1 deletion src/transport/SecureSessionMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ class DLL_EXPORT SecureSessionMgrDelegate
*/
virtual void OnNewConnection(Transport::PeerConnectionState * state, SecureSessionMgrBase * mgr) {}

/**
* @brief
* Called when a new connection is closing
*
* @param state connection state
* @param mgr A pointer to the SecureSessionMgr
*/
virtual void OnConnectionExpired(Transport::PeerConnectionState * state, SecureSessionMgrBase * mgr) {}

/**
* @brief
* Called when the peer address is resolved from NodeID.
Expand Down Expand Up @@ -184,7 +193,7 @@ class DLL_EXPORT SecureSessionMgrBase : public Mdns::ResolveDelegate
/**
* Called when a specific connection expires.
*/
static void HandleConnectionExpired(const Transport::PeerConnectionState & state, SecureSessionMgrBase * mgr);
static void HandleConnectionExpired(Transport::PeerConnectionState & state, SecureSessionMgrBase * mgr);

/**
* Callback for timer expiry check
Expand Down
2 changes: 1 addition & 1 deletion src/transport/tests/TestPeerConnections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct ExpiredCallInfo
PeerAddress lastCallPeerAddress = PeerAddress::Uninitialized();
};

void OnConnectionExpired(const PeerConnectionState & state, ExpiredCallInfo * info)
void OnConnectionExpired(PeerConnectionState & state, ExpiredCallInfo * info)
{
info->callCount++;
info->lastCallNodeId = state.GetPeerNodeId();
Expand Down

0 comments on commit 5298873

Please sign in to comment.