diff --git a/src/cpp/rtps/transport/TCPTransportInterface.cpp b/src/cpp/rtps/transport/TCPTransportInterface.cpp index 3145c063cd9..e95b21dd1cd 100644 --- a/src/cpp/rtps/transport/TCPTransportInterface.cpp +++ b/src/cpp/rtps/transport/TCPTransportInterface.cpp @@ -298,11 +298,16 @@ 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); + } #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); @@ -311,7 +316,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 aa5025fe695..510932bddb5 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