diff --git a/src/inet/tests/TestInetEndPoint.cpp b/src/inet/tests/TestInetEndPoint.cpp index 10209c0ba9b50f..426f749f09de21 100644 --- a/src/inet/tests/TestInetEndPoint.cpp +++ b/src/inet/tests/TestInetEndPoint.cpp @@ -256,9 +256,11 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal) InterfaceId intId; // EndPoint - UDPEndPoint * testUDPEP = nullptr; + UDPEndPoint * testUDPEP = nullptr; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT TCPEndPoint * testTCPEP1 = nullptr; - PacketBufferHandle buf = PacketBufferHandle::New(PacketBuffer::kMaxSize); +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT + PacketBufferHandle buf = PacketBufferHandle::New(PacketBuffer::kMaxSize); // init all the EndPoints SYSTEM_STATS_RESET(System::Stats::kInetLayer_NumUDPEps); @@ -266,10 +268,12 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal) ASSERT_EQ(err, CHIP_NO_ERROR); EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumUDPEps, 1)); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT SYSTEM_STATS_RESET(System::Stats::kInetLayer_NumTCPEps); err = gTCP.NewEndPoint(&testTCPEP1); ASSERT_EQ(err, CHIP_NO_ERROR); - EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumUDPEps, 1)); + EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumTCPEps, 1)); +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT err = InterfaceId::Null().GetLinkLocalAddr(&addr); @@ -323,6 +327,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal) testUDPEP->Free(); EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumUDPEps, 0)); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT // TcpEndPoint special cases to cover the error branch err = testTCPEP1->GetPeerInfo(nullptr, nullptr); EXPECT_EQ(err, CHIP_ERROR_INCORRECT_STATE); @@ -362,6 +367,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal) testTCPEP1->Free(); EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumTCPEps, 0)); EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(System::Stats::kInetLayer_NumTCPEps, 1)); +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT } #if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP @@ -369,7 +375,9 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal) TEST_F(TestInetEndPoint, TestInetEndPointLimit) { UDPEndPoint * testUDPEP[INET_CONFIG_NUM_UDP_ENDPOINTS + 1] = { nullptr }; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT TCPEndPoint * testTCPEP[INET_CONFIG_NUM_TCP_ENDPOINTS + 1] = { nullptr }; +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT CHIP_ERROR err = CHIP_NO_ERROR; @@ -388,6 +396,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointLimit) const int udpHighWaterMark = udpCount; EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(System::Stats::kInetLayer_NumUDPEps, udpHighWaterMark)); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT int tcpCount = 0; SYSTEM_STATS_RESET(System::Stats::kInetLayer_NumTCPEps); for (int i = INET_CONFIG_NUM_TCP_ENDPOINTS; i >= 0; --i) @@ -402,6 +411,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointLimit) } const int tcpHighWaterMark = tcpCount; EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(System::Stats::kInetLayer_NumTCPEps, tcpHighWaterMark)); +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT #if CHIP_SYSTEM_CONFIG_NUM_TIMERS // Verify same aComplete and aAppState args do not exhaust timer pool diff --git a/src/inet/tests/inet-layer-test-tool.cpp b/src/inet/tests/inet-layer-test-tool.cpp index e276600fa10108..0c1158a9ed25ed 100644 --- a/src/inet/tests/inet-layer-test-tool.cpp +++ b/src/inet/tests/inet-layer-test-tool.cpp @@ -88,11 +88,14 @@ static const uint32_t kExpectedTxSizeDefault = kExpectedRxSizeDefault; static const uint32_t kOptFlagsDefault = (kOptFlagUseIPv6 | kOptFlagUseUDPIP); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT static TCPEndPoint * sTCPIPEndPoint = nullptr; // Used for connect/send/receive static TCPEndPoint * sTCPIPListenEndPoint = nullptr; // Used for accept/listen -static UDPEndPoint * sUDPIPEndPoint = nullptr; +static const uint16_t kTCPPort = kUDPPort; +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT + +static UDPEndPoint * sUDPIPEndPoint = nullptr; -static const uint16_t kTCPPort = kUDPPort; // clang-format off static TestState sTestState = { @@ -184,6 +187,7 @@ static OptionSet * sToolOptionSets[] = namespace chip { namespace Inet { +#if INET_CONFIG_ENABLE_TCP_ENDPOINT class TCPTest { public: @@ -193,6 +197,7 @@ class TCPTest return endPoint->mState == TCPEndPoint::State::kConnected || endPoint->mState == TCPEndPoint::State::kReceiveShutdown; } }; +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT } // namespace Inet } // namespace chip @@ -502,6 +507,7 @@ static bool HandleDataReceived(const PacketBufferHandle & aBuffer, bool aCheckBu // TCP Endpoint Callbacks +#if INET_CONFIG_ENABLE_TCP_ENDPOINT void HandleTCPConnectionComplete(TCPEndPoint * aEndPoint, CHIP_ERROR aError) { CHIP_ERROR lStatus; @@ -636,6 +642,7 @@ static void HandleTCPConnectionReceived(TCPEndPoint * aListenEndPoint, TCPEndPoi sTCPIPEndPoint = aConnectEndPoint; } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT // UDP Endpoint Callbacks @@ -673,11 +680,13 @@ static bool IsTransportReadyForSend() return (sUDPIPEndPoint != nullptr); } +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if ((gOptFlags & kOptFlagUseTCPIP) == kOptFlagUseTCPIP) { return (sTCPIPEndPoint != nullptr) && (sTCPIPEndPoint->PendingSendLength() == 0) && TCPTest::StateIsConnectedOrReceiveShutdown(sTCPIPEndPoint); } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT return false; } @@ -686,6 +695,7 @@ static CHIP_ERROR PrepareTransportForSend() { CHIP_ERROR lStatus = CHIP_NO_ERROR; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (gOptFlags & kOptFlagUseTCPIP) { if (sTCPIPEndPoint == nullptr) @@ -702,6 +712,7 @@ static CHIP_ERROR PrepareTransportForSend() INET_FAIL_ERROR(lStatus, "TCPEndPoint::Connect failed"); } } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT return (lStatus); } @@ -722,6 +733,7 @@ static CHIP_ERROR DriveSendForDestination(const IPAddress & aAddress, uint16_t a ReturnErrorOnFailure(sUDPIPEndPoint->SendTo(aAddress, kUDPPort, std::move(lBuffer))); } +#if INET_CONFIG_ENABLE_TCP_ENDPOINT else if ((gOptFlags & kOptFlagUseTCPIP) == kOptFlagUseTCPIP) { const uint32_t lFirstValue = sTestState.mStats.mTransmit.mActual; @@ -737,6 +749,7 @@ static CHIP_ERROR DriveSendForDestination(const IPAddress & aAddress, uint16_t a ReturnErrorOnFailure(sTCPIPEndPoint->Send(std::move(lBuffer))); } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT return CHIP_NO_ERROR; } @@ -838,6 +851,7 @@ static void StartTest() lStatus = sUDPIPEndPoint->Listen(HandleUDPMessageReceived, HandleUDPReceiveError); INET_FAIL_ERROR(lStatus, "UDPEndPoint::Listen failed"); } +#if INET_CONFIG_ENABLE_TCP_ENDPOINT else if (gOptFlags & kOptFlagUseTCPIP) { const uint16_t lConnectionBacklogMax = 1; @@ -855,6 +869,7 @@ static void StartTest() lStatus = sTCPIPListenEndPoint->Listen(lConnectionBacklogMax); INET_FAIL_ERROR(lStatus, "TCPEndPoint::Listen failed"); } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT } if (Common::IsReceiver()) @@ -870,6 +885,7 @@ static void CleanupTest() // Release the resources associated with the allocated end points. +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (sTCPIPEndPoint != nullptr) { sTCPIPEndPoint->Close(); @@ -881,6 +897,7 @@ static void CleanupTest() sTCPIPListenEndPoint->Shutdown(); sTCPIPListenEndPoint->Free(); } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT if (sUDPIPEndPoint != nullptr) { diff --git a/src/messaging/tests/echo/echo_requester.cpp b/src/messaging/tests/echo/echo_requester.cpp index b06edb359b989f..c3aa3f6085c6df 100644 --- a/src/messaging/tests/echo/echo_requester.cpp +++ b/src/messaging/tests/echo/echo_requester.cpp @@ -51,9 +51,6 @@ namespace { // Max value for the number of EchoRequests sent. constexpr size_t kMaxEchoCount = 3; -// Max value for the number of tcp connect attempts. -constexpr size_t kMaxTCPConnectAttempts = 3; - // The CHIP Echo interval time. constexpr chip::System::Clock::Timeout gEchoInterval = chip::System::Clock::Seconds16(1); @@ -63,15 +60,17 @@ constexpr chip::FabricIndex gFabricIndex = 0; chip::Protocols::Echo::EchoClient gEchoClient; chip::TransportMgr gUDPManager; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT +// Max value for the number of tcp connect attempts. +constexpr size_t kMaxTCPConnectAttempts = 3; + chip::TransportMgr> gTCPManager; -chip::Inet::IPAddress gDestAddr; -chip::SessionHolder gSession; chip::Transport::AppTCPConnectionCallbackCtxt gAppTCPConnCbCtxt; chip::Transport::ActiveTCPConnectionState * gActiveTCPConnState = nullptr; -// The last time a CHIP Echo was attempted to be sent. -chip::System::Clock::Timestamp gLastEchoTime = chip::System::Clock::kZero; +static void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr); +static void HandleConnectionClosed(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr); // True, if client is still connecting to the server, false otherwise. static bool gClientConInProgress = false; @@ -79,6 +78,16 @@ static bool gClientConInProgress = false; // True, once client connection to server is established. static bool gClientConEstablished = false; +bool gUseTCP = false; +uint64_t gTCPConnAttemptCount = 0; +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT + +chip::Inet::IPAddress gDestAddr; +chip::SessionHolder gSession; + +// The last time a CHIP Echo was attempted to be sent. +chip::System::Clock::Timestamp gLastEchoTime = chip::System::Clock::kZero; + // The handle to the TCP connection to the peer. // static chip::Transport::ActiveTCPConnectionState * gCon = nullptr; @@ -88,16 +97,9 @@ uint64_t gEchoCount = 0; // Count of the number of EchoResponses received. uint64_t gEchoRespCount = 0; -bool gUseTCP = false; - -uint64_t gTCPConnAttemptCount = 0; - CHIP_ERROR SendEchoRequest(); void EchoTimerHandler(chip::System::Layer * systemLayer, void * appState); -static void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr); -static void HandleConnectionClosed(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr); - void Shutdown() { chip::DeviceLayer::SystemLayer().CancelTimer(EchoTimerHandler, nullptr); @@ -170,15 +172,17 @@ CHIP_ERROR SendEchoRequest() return err; } -CHIP_ERROR EstablishSecureSession(chip::Transport::ActiveTCPConnectionState * conn = nullptr) +CHIP_ERROR EstablishSecureSession() { char peerAddrBuf[chip::Transport::PeerAddress::kMaxToStringSize]; chip::Transport::PeerAddress peerAddr; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (gUseTCP) { peerAddr = chip::Transport::PeerAddress::TCP(gDestAddr, CHIP_PORT); } else +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT { peerAddr = chip::Transport::PeerAddress::UDP(gDestAddr, CHIP_PORT, chip::Inet::InterfaceId::Null()); } @@ -195,11 +199,13 @@ CHIP_ERROR EstablishSecureSession(chip::Transport::ActiveTCPConnectionState * co } else { +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (gUseTCP) { - printf("Associating secure session with connection %p\n", conn); - gSession.Get().Value()->AsSecureSession()->SetTCPConnection(conn); + printf("Associating secure session with connection %p\n", gActiveTCPConnState); + gSession.Get().Value()->AsSecureSession()->SetTCPConnection(gActiveTCPConnState); } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT printf("Successfully established secure session with peer at %s\n", peerAddrBuf); } @@ -207,6 +213,7 @@ CHIP_ERROR EstablishSecureSession(chip::Transport::ActiveTCPConnectionState * co return err; } +#if INET_CONFIG_ENABLE_TCP_ENDPOINT void CloseConnection() { char peerAddrBuf[chip::Transport::PeerAddress::kMaxToStringSize]; @@ -225,7 +232,7 @@ void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState * { chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); - if (err != CHIP_NO_ERROR) + if (err != CHIP_NO_ERROR || conn != gActiveTCPConnState) { printf("Connection FAILED with err: %s\n", chip::ErrorStr(err)); @@ -235,7 +242,7 @@ void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState * return; } - err = EstablishSecureSession(conn); + err = EstablishSecureSession(); if (err != CHIP_NO_ERROR) { printf("Secure session FAILED with err: %s\n", chip::ErrorStr(err)); @@ -281,6 +288,7 @@ void EstablishTCPConnection() gClientConInProgress = true; } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT void HandleEchoResponseReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload) { @@ -313,10 +321,12 @@ int main(int argc, char * argv[]) ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); } +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if ((argc == 3) && (strcmp(argv[2], "--tcp") == 0)) { gUseTCP = true; } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT if (!chip::Inet::IPAddress::FromString(argv[1], gDestAddr)) { @@ -332,6 +342,7 @@ int main(int argc, char * argv[]) InitializeChip(); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (gUseTCP) { err = gTCPManager.Init(chip::Transport::TcpListenParameters(chip::DeviceLayer::TCPEndPointManager()) @@ -348,6 +359,7 @@ int main(int argc, char * argv[]) gAppTCPConnCbCtxt.connClosedCb = HandleConnectionClosed; } else +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT { err = gUDPManager.Init(chip::Transport::UdpListenParameters(chip::DeviceLayer::UDPEndPointManager()) .SetAddressType(chip::Inet::IPAddressType::kIPv6) @@ -365,13 +377,14 @@ int main(int argc, char * argv[]) err = gMessageCounterManager.Init(&gExchangeManager); SuccessOrExit(err); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (gUseTCP) { while (!gClientConEstablished) { // For TCP transport, attempt to establish the connection to the CHIP echo responder. - // On Connection completion, call EstablishSecureSession(conn); + // On Connection completion, call EstablishSecureSession(); EstablishTCPConnection(); chip::DeviceLayer::PlatformMgr().RunEventLoop(); @@ -383,9 +396,10 @@ int main(int argc, char * argv[]) } } else +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT { // Start the CHIP session to the CHIP echo responder. - err = EstablishSecureSession(nullptr); + err = EstablishSecureSession(); SuccessOrExit(err); } @@ -402,16 +416,22 @@ int main(int argc, char * argv[]) gUDPManager.Close(); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (gUseTCP) { gTCPManager.TCPDisconnect(chip::Transport::PeerAddress::TCP(gDestAddr)); } gTCPManager.Close(); +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT Shutdown(); exit: - if ((err != CHIP_NO_ERROR) || (gEchoRespCount != kMaxEchoCount) || (gTCPConnAttemptCount > kMaxTCPConnectAttempts)) + if ((err != CHIP_NO_ERROR) || (gEchoRespCount != kMaxEchoCount) +#if INET_CONFIG_ENABLE_TCP_ENDPOINT + || (gTCPConnAttemptCount > kMaxTCPConnectAttempts) +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT + ) { printf("ChipEchoClient failed: %s\n", chip::ErrorStr(err)); exit(EXIT_FAILURE); diff --git a/src/messaging/tests/echo/echo_responder.cpp b/src/messaging/tests/echo/echo_responder.cpp index 0cd2366a1f6c2b..46550635845a9b 100644 --- a/src/messaging/tests/echo/echo_responder.cpp +++ b/src/messaging/tests/echo/echo_responder.cpp @@ -45,7 +45,9 @@ namespace { // The EchoServer object. chip::Protocols::Echo::EchoServer gEchoServer; chip::TransportMgr gUDPManager; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT chip::TransportMgr> gTCPManager; +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT chip::SessionHolder gSession; // Callback handler when a CHIP EchoRequest is received. @@ -60,7 +62,9 @@ int main(int argc, char * argv[]) { CHIP_ERROR err = CHIP_NO_ERROR; chip::Transport::PeerAddress peer(chip::Transport::Type::kUndefined); - bool useTCP = false; +#if INET_CONFIG_ENABLE_TCP_ENDPOINT + bool useTCP = false; +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT bool disableEcho = false; const chip::FabricIndex gFabricIndex = 0; @@ -71,10 +75,12 @@ int main(int argc, char * argv[]) ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); } +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if ((argc == 2) && (strcmp(argv[1], "--tcp") == 0)) { useTCP = true; } +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT if ((argc == 2) && (strcmp(argv[1], "--disable") == 0)) { @@ -83,6 +89,7 @@ int main(int argc, char * argv[]) InitializeChip(); +#if INET_CONFIG_ENABLE_TCP_ENDPOINT if (useTCP) { err = gTCPManager.Init(chip::Transport::TcpListenParameters(chip::DeviceLayer::TCPEndPointManager()) @@ -94,6 +101,7 @@ int main(int argc, char * argv[]) SuccessOrExit(err); } else +#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT { err = gUDPManager.Init(chip::Transport::UdpListenParameters(chip::DeviceLayer::UDPEndPointManager()) .SetAddressType(chip::Inet::IPAddressType::kIPv6)); diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp index 17065e319cf3bd..d9582479458617 100644 --- a/src/transport/SessionManager.cpp +++ b/src/transport/SessionManager.cpp @@ -1269,18 +1269,18 @@ Optional SessionManager::FindSecureSessionForNode(ScopedNodeId pe if (session->IsActiveSession() && session->GetPeer() == peerNodeId && (!type.HasValue() || type.Value() == session->GetSecureSessionType())) { -#if INET_CONFIG_ENABLE_TCP_ENDPOINT - if ((transportPayloadCapability == TransportPayloadCapability::kMRPOrTCPCompatiblePayload || - transportPayloadCapability == TransportPayloadCapability::kLargePayload) && - session->GetTCPConnection() != nullptr) + if (transportPayloadCapability == TransportPayloadCapability::kMRPOrTCPCompatiblePayload || + transportPayloadCapability == TransportPayloadCapability::kLargePayload) { +#if INET_CONFIG_ENABLE_TCP_ENDPOINT // Set up a TCP transport based session as standby - if ((tcpSession == nullptr) || (tcpSession->GetLastActivityTime() < session->GetLastActivityTime())) + if ((tcpSession == nullptr || tcpSession->GetLastActivityTime() < session->GetLastActivityTime()) && + session->GetTCPConnection() != nullptr) { tcpSession = session; } - } #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT + } if ((mrpSession == nullptr) || (mrpSession->GetLastActivityTime() < session->GetLastActivityTime())) {