Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20238] Enabling multiple interfaces through whitelist in TCP servers #4297

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/cpp/rtps/transport/TCPTransportInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void TCPTransportInterface::calculate_crc(
}

uint16_t TCPTransportInterface::create_acceptor_socket(
const Locator& locator)
Locator& locator)
MiguelCompany marked this conversation as resolved.
Show resolved Hide resolved
{
uint16_t final_port = 0;
try
Expand Down Expand Up @@ -348,6 +348,14 @@ uint16_t TCPTransportInterface::create_acceptor_socket(
std::vector<std::string> vInterfaces = get_binding_interfaces_list();
for (std::string& sInterface : vInterfaces)
{
if (locator.kind == LOCATOR_KIND_TCPv4)
{
IPLocator::setIPv4(locator, sInterface);
}
else if (locator.kind == LOCATOR_KIND_TCPv6)
{
IPLocator::setIPv6(locator, sInterface);
}
#if TLS_FOUND
if (configuration()->apply_security)
{
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/rtps/transport/TCPTransportInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class TCPTransportInterface : public TransportInterface

//! Creates a TCP acceptor to wait for incoming connections by the given locator.
uint16_t create_acceptor_socket(
const Locator& locator);
Locator& locator);

virtual void get_ips(
std::vector<fastrtps::rtps::IPFinder::info_IP>& loc_names,
Expand Down
41 changes: 40 additions & 1 deletion test/unittest/transport/TCPv4Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,49 @@ TEST_F(TCPv4Tests, send_and_receive_between_allowed_interfaces_ports_by_name)
senderThread->join();
ASSERT_TRUE(bOk);
}
}

TEST_F(TCPv4Tests, check_TCPv4_interface_whitelist_initialization)
{
std::vector<IPFinder::info_IP> interfaces;

}
GetIP4s(interfaces);

std::vector<std::string> 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);
}
descriptor.add_listener_port(g_default_port);
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)
Expand Down
64 changes: 64 additions & 0 deletions test/unittest/transport/TCPv6Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,70 @@ TEST_F(TCPv6Tests, autofill_port)
EXPECT_TRUE(transportUnderTest_multiple_autofill.configuration()->listening_ports.size() == 3);
}

static void GetIP6s(
std::vector<IPFinder::info_IP>& interfaces)
{
IPFinder::getIPs(&interfaces, false);
auto new_end = remove_if(interfaces.begin(),
interfaces.end(),
[](IPFinder::info_IP ip)
{
return ip.type != IPFinder::IP6 && ip.type != IPFinder::IP6_LOCAL;
});
interfaces.erase(new_end, interfaces.end());
std::for_each(interfaces.begin(), interfaces.end(), [](IPFinder::info_IP& loc)
{
loc.locator.kind = LOCATOR_KIND_TCPv6;
});
}

TEST_F(TCPv6Tests, check_TCPv6_interface_whitelist_initialization)
{
std::vector<IPFinder::info_IP> interfaces;

GetIP6s(interfaces);

// asio::ip::addres_v6 appends the interface name to the IP address, but the locator does not
// Create two different vectors to compare them
std::vector<std::string> asio_interfaces;
std::vector<std::string> locator_interfaces;
for (auto& ip : interfaces)
{
asio_interfaces.push_back(ip.name);
locator_interfaces.push_back(IPLocator::toIPv6string(ip.locator));
}
// Add manually localhost to test adding multiple interfaces
asio_interfaces.push_back("::1");
locator_interfaces.push_back("::1");

for (auto& ip : locator_interfaces)
{
descriptor.interfaceWhiteList.emplace_back(ip);
}
descriptor.add_listener_port(g_default_port);
MockTCPv6Transport transportUnderTest(descriptor);
transportUnderTest.init();

// Check that the transport whitelist and the acceptors map is the same size as the locator_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 : asio_interfaces)
{
ASSERT_NE(std::find(check_whitelist.begin(), check_whitelist.end(), asio::ip::address_v6::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(locator_interfaces.begin(), locator_interfaces.end(), IPLocator::toIPv6string(
test.first)), locator_interfaces.end());
}
}

// This test verifies server's channel resources mapping keys uniqueness, where keys are clients locators.
// Clients typically communicated its PID as its locator port. When having several clients in the same
// process this lead to overwriting server's channel resources map elements.
Expand Down
10 changes: 10 additions & 0 deletions test/unittest/transport/mock/MockTCPv4Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class MockTCPv4Transport : public TCPv4Transport
return unbound_channel_resources_;
}

const std::vector<asio::ip::address_v4>& get_interface_whitelist() const
{
return interface_whitelist_;
}

const std::map<Locator_t, std::shared_ptr<fastdds::rtps::TCPAcceptor>>& get_acceptors_map() const
{
return acceptors_;
}

};

} // namespace rtps
Expand Down
10 changes: 10 additions & 0 deletions test/unittest/transport/mock/MockTCPv6Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class MockTCPv6Transport : public TCPv6Transport
return unbound_channel_resources_;
}

const std::vector<asio::ip::address_v6>& get_interface_whitelist() const
{
return interface_whitelist_;
}

const std::map<Locator_t, std::shared_ptr<fastdds::rtps::TCPAcceptor>>& get_acceptors_map() const
{
return acceptors_;
}

};

} // namespace rtps
Expand Down
Loading