From 9fc686ba67763f6051703bbad6609d2213817681 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 8 Mar 2024 23:24:10 +0100 Subject: [PATCH] TCP Client&Server Participant Decision-Making (#4277) (backport #4297) (#4413) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * TCP Client&Server Participant Decision-Making (#4277) * Refs #20180: Spelling fix Signed-off-by: cferreiragonz * Refs #20180: Create function to check pending_logical_ports Signed-off-by: cferreiragonz * Refs #20180: Add new TCP Connection type Signed-off-by: cferreiragonz * Refs #20180: Add new method to enable TCP channel after connection in LARGE DATA Signed-off-by: cferreiragonz * Refs #20180: Refactor OpenOutputChannel method Signed-off-by: cferreiragonz * Refs #20180: Include LARGE DATA logic in SocketAccepted method Signed-off-by: cferreiragonz * Refs #20180: Include LARGE DATA logic in processBindConnectionRequest method Signed-off-by: cferreiragonz * Refs #20180: Add new TCP connection and methods SECURITY Signed-off-by: cferreiragonz * Refs #20180: Add SECURITY support to OpenOutputChannel Signed-off-by: cferreiragonz * Refs #20180: Include LARGE DATA logic in SecureSocketAccepted method Signed-off-by: cferreiragonz * Refs #20180: Bugfix setting environment variable in tests Signed-off-by: cferreiragonz * Refs #20180: Add multicast IPv6 to LARGE_DATA locator Signed-off-by: cferreiragonz * Refs #20180: Add TCP test in LARGE_DATA Topology Signed-off-by: cferreiragonz * Refs #20180: Clean code & Uncrustify Signed-off-by: cferreiragonz * Refs #20180: Modified participant populated test Signed-off-by: Jesus Perez * Refs #20180: Add new api to update channels Signed-off-by: cferreiragonz * Refs #20180: Refactor OpenOutputChannel with new channel map Signed-off-by: cferreiragonz * Refs #20180: Refactor bind_socket with new channel map Signed-off-by: cferreiragonz * Refs #20180: Refactor SocketAccepted with new channel map (Revert to old behavior) Signed-off-by: cferreiragonz * Refs #20180: Refactor CloseOutputChannel to support cases with new channel map Signed-off-by: cferreiragonz * Refs #20180: Update Secure channel logic with new channel map Signed-off-by: cferreiragonz * Refs #20180: Use same IPv6 as defaultMetatrafficMulticast Signed-off-by: cferreiragonz * Refs #20180: Refactor using locator inside sender resource instead of channel Signed-off-by: cferreiragonz * Refs #20180: Data races associated Signed-off-by: cferreiragonz * Refs #20180: Apply suggestions to tests Signed-off-by: Jesus Perez * Refs #20180: Add OpenLogicalPortRequest scenario before Bind Response is processed Signed-off-by: cferreiragonz * Refs #20180. Remove unnecessary methods. Signed-off-by: Miguel Company * Refs #20180. Early exit when logical_port is 0. Signed-off-by: Miguel Company * Refs #20180. Refactor to avoid lambda. Signed-off-by: Miguel Company * Refs #20180. Minor changes. Signed-off-by: Miguel Company * Refs #20180. Simplify with WanToLanLocator. Signed-off-by: Miguel Company * Refs #20180: Apply suggestions Signed-off-by: cferreiragonz * Revert "Refs #20180: Add new TCP Connection type" This reverts commit 4b4eb4153553cfdbf007896ff524d3e904f722c9. Signed-off-by: cferreiragonz * Refs #20180: Remove unnecessary lock Signed-off-by: cferreiragonz * Revert "Refs #20180: Add new TCP connection and methods SECURITY" This reverts commit c69d92e56dcfab3c3c7287145a7a93e57947d3cb. Signed-off-by: cferreiragonz * Refs #20180: Fix test (file descriptors limit) Signed-off-by: Jesus Perez * Refs #20180: Uncrustify Signed-off-by: Jesus Perez * Refs #20180. Fix port comparison. Signed-off-by: Miguel Company Co-authored-by: Carlos Ferreira González * Refs #20180: Fix double lock and update old test with current behavior Signed-off-by: Jesus Perez * Refs #20180: Apply suggestions Signed-off-by: Jesus Perez * Refs #20180: Fix windows test build Signed-off-by: Jesus Perez * Refs #20180: Apply test suggestions Signed-off-by: Jesus Perez * Refs #20180: Bugfix with cv in receiver_resources_ destruction Signed-off-by: cferreiragonz * Refs #20180. Delete participant on failing ParticipantTests.ParticipantCreationWithBuiltinTransport Signed-off-by: Miguel Company * Refs #20180. Do not use non_blocking_send on TCP transport for LARGE_DATA. Signed-off-by: Miguel Company --------- Signed-off-by: cferreiragonz Signed-off-by: Jesus Perez Signed-off-by: Miguel Company Co-authored-by: Jesus Perez Co-authored-by: Miguel Company (cherry picked from commit 709b1406b0a40876a98bcb66147c61dd98aadc1d) # Conflicts: # test/blackbox/common/BlackboxTestsTransportTCP.cpp * Fix Conflicts Signed-off-by: cferreiragonz * Enabling multiple interfaces through whitelist in TCP servers (#4297) * Refs #20238: Add whitelist test in TCPv4 Signed-off-by: cferreiragonz * Refs #20238: Add whitelist test in TCPv6 Signed-off-by: cferreiragonz * Refs #20238: Adding interface to locator if whitelist is not empty Signed-off-by: cferreiragonz * Refs #20238: Uncrustify Signed-off-by: cferreiragonz * Refs #20238: Fix build in Windows Signed-off-by: cferreiragonz * Refs #20238: Apply suggestions Signed-off-by: cferreiragonz * Refs #20238: Remove repeated addition of listener port (added by default) Signed-off-by: cferreiragonz --------- Signed-off-by: cferreiragonz (cherry picked from commit 5f65a65196dff2453ff86df711db018fefed1e54) * Fix conflicts Signed-off-by: cferreiragonz --------- Signed-off-by: cferreiragonz Co-authored-by: Carlos Ferreira González --- .../rtps/transport/TCPTransportInterface.cpp | 13 ++++- test/unittest/transport/TCPv4Tests.cpp | 58 +++++++++++++++++++ .../transport/mock/MockTCPv4Transport.h | 10 ++++ .../transport/mock/MockTCPv6Transport.h | 10 ++++ 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/cpp/rtps/transport/TCPTransportInterface.cpp b/src/cpp/rtps/transport/TCPTransportInterface.cpp index 5c6cd4d4611..a2912dc11f8 100644 --- a/src/cpp/rtps/transport/TCPTransportInterface.cpp +++ b/src/cpp/rtps/transport/TCPTransportInterface.cpp @@ -299,11 +299,20 @@ uint16_t TCPTransportInterface::create_acceptor_socket( std::vector vInterfaces = get_binding_interfaces_list(); for (std::string& sInterface : vInterfaces) { + Locator loc = locator; + if (loc.kind == LOCATOR_KIND_TCPv4) + { + IPLocator::setIPv4(loc, sInterface); + } + else if (loc.kind == LOCATOR_KIND_TCPv6) + { + IPLocator::setIPv6(loc, sInterface); + } #if TLS_FOUND if (configuration()->apply_security) { std::shared_ptr acceptor = - std::make_shared(io_service_, sInterface, locator); + std::make_shared(io_service_, sInterface, loc); acceptors_[acceptor->locator()] = acceptor; acceptor->accept(this, ssl_context_); final_port = static_cast(acceptor->locator().port); @@ -312,7 +321,7 @@ uint16_t TCPTransportInterface::create_acceptor_socket( #endif // if TLS_FOUND { std::shared_ptr acceptor = - std::make_shared(io_service_, sInterface, locator); + std::make_shared(io_service_, sInterface, loc); acceptors_[acceptor->locator()] = acceptor; acceptor->accept(this); final_port = static_cast(acceptor->locator().port); diff --git a/test/unittest/transport/TCPv4Tests.cpp b/test/unittest/transport/TCPv4Tests.cpp index 8ec3f8748c4..91c68be0843 100644 --- a/test/unittest/transport/TCPv4Tests.cpp +++ b/test/unittest/transport/TCPv4Tests.cpp @@ -463,6 +463,64 @@ TEST_F(TCPv4Tests, send_and_receive_between_allowed_interfaces_ports) } } +static void GetIP4s( + std::vector& interfaces) +{ + IPFinder::getIPs(&interfaces, false); + auto new_end = remove_if(interfaces.begin(), + interfaces.end(), + [](IPFinder::info_IP ip) + { + return ip.type != IPFinder::IP4 && ip.type != IPFinder::IP4_LOCAL; + }); + interfaces.erase(new_end, interfaces.end()); + std::for_each(interfaces.begin(), interfaces.end(), [](IPFinder::info_IP& loc) + { + loc.locator.kind = LOCATOR_KIND_TCPv4; + }); +} + +TEST_F(TCPv4Tests, check_TCPv4_interface_whitelist_initialization) +{ + std::vector interfaces; + + GetIP4s(interfaces); + + std::vector mock_interfaces; + for (auto& ip : interfaces) + { + mock_interfaces.push_back(ip.name); + } + // Add manually localhost to test adding multiple interfaces + mock_interfaces.push_back("127.0.0.1"); + + for (auto& ip : mock_interfaces) + { + descriptor.interfaceWhiteList.emplace_back(ip); + } + MockTCPv4Transport transportUnderTest(descriptor); + transportUnderTest.init(); + + // Check that the transport whitelist and the acceptors map is the same size as the mock_interfaces + ASSERT_EQ(transportUnderTest.get_interface_whitelist().size(), descriptor.interfaceWhiteList.size()); + ASSERT_EQ(transportUnderTest.get_acceptors_map().size(), descriptor.interfaceWhiteList.size()); + + // Check that every interface is in the whitelist + auto check_whitelist = transportUnderTest.get_interface_whitelist(); + for (auto& ip : mock_interfaces) + { + ASSERT_NE(std::find(check_whitelist.begin(), check_whitelist.end(), asio::ip::address_v4::from_string( + ip)), check_whitelist.end()); + } + + // Check that every interface is in the acceptors map + for (const auto& test : transportUnderTest.get_acceptors_map()) + { + ASSERT_NE(std::find(mock_interfaces.begin(), mock_interfaces.end(), IPLocator::toIPv4string( + test.first)), mock_interfaces.end()); + } +} + #if TLS_FOUND TEST_F(TCPv4Tests, send_and_receive_between_secure_ports_client_verifies) { diff --git a/test/unittest/transport/mock/MockTCPv4Transport.h b/test/unittest/transport/mock/MockTCPv4Transport.h index 6a1e7300b86..f5aff8b452b 100644 --- a/test/unittest/transport/mock/MockTCPv4Transport.h +++ b/test/unittest/transport/mock/MockTCPv4Transport.h @@ -46,6 +46,16 @@ class MockTCPv4Transport : public TCPv4Transport return unbound_channel_resources_; } + const std::vector& get_interface_whitelist() const + { + return interface_whitelist_; + } + + const std::map>& get_acceptors_map() const + { + return acceptors_; + } + }; } // namespace rtps diff --git a/test/unittest/transport/mock/MockTCPv6Transport.h b/test/unittest/transport/mock/MockTCPv6Transport.h index 37b8d7c02d3..33f9155e2dc 100644 --- a/test/unittest/transport/mock/MockTCPv6Transport.h +++ b/test/unittest/transport/mock/MockTCPv6Transport.h @@ -46,6 +46,16 @@ class MockTCPv6Transport : public TCPv6Transport return unbound_channel_resources_; } + const std::vector& get_interface_whitelist() const + { + return interface_whitelist_; + } + + const std::map>& get_acceptors_map() const + { + return acceptors_; + } + }; } // namespace rtps