From b88ddd05c5b883ffd21b0a412cdcd6950a7da696 Mon Sep 17 00:00:00 2001 From: Pradip De Date: Tue, 3 Oct 2023 14:49:24 -0700 Subject: [PATCH 1/2] Increment used endpoint count during incoming connection. Due to this issue, subsequent incoming connections were failing to connect as the value was getting decremented from zero resulting in a check failing. Also, remove duplicate code for freeing an active connection and move it into a separate function. --- src/transport/raw/TCP.cpp | 36 ++++++++++++++++++------------------ src/transport/raw/TCP.h | 4 ++++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/transport/raw/TCP.cpp b/src/transport/raw/TCP.cpp index 8e6cae72da4f36..a1b1df1fe45a2d 100644 --- a/src/transport/raw/TCP.cpp +++ b/src/transport/raw/TCP.cpp @@ -325,6 +325,18 @@ CHIP_ERROR TCPBase::ProcessSingleMessage(const PeerAddress & peerAddress, Active return CHIP_NO_ERROR; } +void TCPBase::ReleaseActiveConnection(Inet::TCPEndPoint * endPoint) +{ + for (size_t i = 0; i < mActiveConnectionsSize; i++) + { + if (mActiveConnections[i].mEndPoint == endPoint) + { + mActiveConnections[i].Free(); + mUsedEndPointCount--; + } + } +} + CHIP_ERROR TCPBase::OnTcpReceive(Inet::TCPEndPoint * endPoint, System::PacketBufferHandle && buffer) { Inet::IPAddress ipAddress; @@ -425,15 +437,8 @@ void TCPBase::OnConnectionClosed(Inet::TCPEndPoint * endPoint, CHIP_ERROR err) ChipLogProgress(Inet, "Connection closed."); - for (size_t i = 0; i < tcp->mActiveConnectionsSize; i++) - { - if (tcp->mActiveConnections[i].mEndPoint == endPoint) - { - ChipLogProgress(Inet, "Freeing closed connection."); - tcp->mActiveConnections[i].Free(); - tcp->mUsedEndPointCount--; - } - } + ChipLogProgress(Inet, "Freeing closed connection."); + tcp->ReleaseActiveConnection(endPoint); } void TCPBase::OnConnectionReceived(Inet::TCPEndPoint * listenEndPoint, Inet::TCPEndPoint * endPoint, @@ -449,6 +454,7 @@ void TCPBase::OnConnectionReceived(Inet::TCPEndPoint * listenEndPoint, Inet::TCP if (!tcp->mActiveConnections[i].InUse()) { tcp->mActiveConnections[i].Init(endPoint); + tcp->mUsedEndPointCount++; break; } } @@ -502,15 +508,9 @@ void TCPBase::OnPeerClosed(Inet::TCPEndPoint * endPoint) { TCPBase * tcp = reinterpret_cast(endPoint->mAppState); - for (size_t i = 0; i < tcp->mActiveConnectionsSize; i++) - { - if (tcp->mActiveConnections[i].mEndPoint == endPoint) - { - ChipLogProgress(Inet, "Freeing connection: connection closed by peer"); - tcp->mActiveConnections[i].Free(); - tcp->mUsedEndPointCount--; - } - } + ChipLogProgress(Inet, "Freeing connection: connection closed by peer"); + + tcp->ReleaseActiveConnection(endPoint); } bool TCPBase::HasActiveConnections() const diff --git a/src/transport/raw/TCP.h b/src/transport/raw/TCP.h index 8f90a4242b187f..15341db37c5f41 100644 --- a/src/transport/raw/TCP.h +++ b/src/transport/raw/TCP.h @@ -225,6 +225,10 @@ class DLL_EXPORT TCPBase : public Base */ CHIP_ERROR ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint16_t messageSize); + // Release an active connection(corresponding to the passed TCPEndPoint) + // from the pool. + void ReleaseActiveConnection(Inet::TCPEndPoint * endPoint); + // Callback handler for TCPEndPoint. TCP message receive handler. // @see TCPEndpoint::OnDataReceivedFunct static CHIP_ERROR OnTcpReceive(Inet::TCPEndPoint * endPoint, System::PacketBufferHandle && buffer); From 51aed445f5f54aa2ce14d724df4361e17ed63f52 Mon Sep 17 00:00:00 2001 From: Pradip De Date: Thu, 5 Oct 2023 10:06:37 -0700 Subject: [PATCH 2/2] Update src/transport/raw/TCP.h Co-authored-by: Boris Zbarsky --- src/transport/raw/TCP.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/raw/TCP.h b/src/transport/raw/TCP.h index 15341db37c5f41..d9f78be1771b0f 100644 --- a/src/transport/raw/TCP.h +++ b/src/transport/raw/TCP.h @@ -225,7 +225,7 @@ class DLL_EXPORT TCPBase : public Base */ CHIP_ERROR ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint16_t messageSize); - // Release an active connection(corresponding to the passed TCPEndPoint) + // Release an active connection (corresponding to the passed TCPEndPoint) // from the pool. void ReleaseActiveConnection(Inet::TCPEndPoint * endPoint);