From fa256737c60ac09acc4637f31a53727c145275db Mon Sep 17 00:00:00 2001 From: Pradip De Date: Wed, 3 Jul 2024 15:51:33 -0700 Subject: [PATCH] Minor fixes in TCP Disconnect to simplify the logic. (#34142) Use the available IPAddress and port info in the member PeerAddress in the ActiveConnection object to compare with the passed PeerAddress instead of calling GetPeerInfo() from within TCPEndPoint to fetch those. Log the connection closure in the CloseConnectionInternal function which is also called by CloseActiveConnections() from the TCPBase destructor. --- src/transport/raw/TCP.cpp | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/transport/raw/TCP.cpp b/src/transport/raw/TCP.cpp index 3c3e6da15c2b58..8a15f754d54027 100644 --- a/src/transport/raw/TCP.cpp +++ b/src/transport/raw/TCP.cpp @@ -398,6 +398,10 @@ void TCPBase::CloseConnectionInternal(ActiveTCPConnectionState * connection, CHI if (connection->mConnectionState != TCPState::kClosed && connection->mEndPoint) { + char addrStr[Transport::PeerAddress::kMaxToStringSize]; + connection->mPeerAddr.ToString(addrStr); + ChipLogProgress(Inet, "Closing connection with peer %s.", addrStr); + if (err == CHIP_NO_ERROR) { connection->mEndPoint->Close(); @@ -616,36 +620,20 @@ CHIP_ERROR TCPBase::TCPConnect(const PeerAddress & address, Transport::AppTCPCon void TCPBase::TCPDisconnect(const PeerAddress & address) { - CHIP_ERROR err = CHIP_NO_ERROR; // Closes an existing connection for (size_t i = 0; i < mActiveConnectionsSize; i++) { if (mActiveConnections[i].IsConnected()) { - Inet::IPAddress ipAddress; - uint16_t port; - Inet::InterfaceId interfaceId; - - err = mActiveConnections[i].mEndPoint->GetPeerInfo(&ipAddress, &port); - if (err != CHIP_NO_ERROR) - { - ChipLogError(Inet, "TCPDisconnect: GetPeerInfo error: %" CHIP_ERROR_FORMAT, err.Format()); - return; - } - - err = mActiveConnections[i].mEndPoint->GetInterfaceId(&interfaceId); - if (err != CHIP_NO_ERROR) + const Inet::IPAddress & ipAddress = mActiveConnections[i].mPeerAddr.GetIPAddress(); + uint16_t port = mActiveConnections[i].mPeerAddr.GetPort(); + + // Ignoring the InterfaceID in the check as it may not have been provided in + // the PeerAddress during connection establishment. The IPAddress and Port + // are the necessary and sufficient set of parameters for searching + // through the connections. + if (ipAddress == address.GetIPAddress() && port == address.GetPort() && address.GetTransportType() == Type::kTcp) { - ChipLogError(Inet, "TCPDisconnect: GetInterfaceId error: %" CHIP_ERROR_FORMAT, err.Format()); - return; - } - // if (address == PeerAddress::TCP(ipAddress, port, interfaceId)) - if (ipAddress == address.GetIPAddress() && port == address.GetPort()) - { - char addrStr[Transport::PeerAddress::kMaxToStringSize]; - address.ToString(addrStr); - ChipLogProgress(Inet, "Disconnecting with peer %s.", addrStr); - // NOTE: this leaves the socket in TIME_WAIT. // Calling Abort() would clean it since SO_LINGER would be set to 0, // however this seems not to be useful.