Skip to content

Commit 9e68e1f

Browse files
Add localhost capability only to UDP & TCP transports (#5864) (#5868)
* Refs #23213: Add regression test * Refs #23213: Only add localhost capability to UDP & TCP transports * Refs #23213: Update failing test --------- (cherry picked from commit 401ccad) Signed-off-by: Juan Lopez Fernandez <[email protected]> Co-authored-by: juanlofer-eprosima <[email protected]>
1 parent 8f66c94 commit 9e68e1f

File tree

16 files changed

+192
-35
lines changed

16 files changed

+192
-35
lines changed

include/fastdds/rtps/transport/TransportInterface.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class FASTDDS_EXPORTED_API TransportInterface
319319
//! Must report whether localhost locator is allowed
320320
virtual bool is_localhost_allowed() const
321321
{
322-
return true;
322+
return false;
323323
}
324324

325325
//! Returns netmask filter information (transport's netmask filter kind and allowlist)

src/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ set(${PROJECT_NAME}_source_files
180180
rtps/messages/submessages/GapMsg.hpp
181181
rtps/messages/submessages/HeartbeatMsg.hpp
182182
rtps/network/NetworkBuffer.cpp
183+
rtps/network/NetworkConfiguration.cpp
183184
rtps/network/NetworkFactory.cpp
184185
rtps/network/ReceiverResource.cpp
185186
rtps/network/utils/external_locators.cpp

src/cpp/rtps/builtin/data/NetworkConfiguration.hpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/cpp/rtps/builtin/discovery/participant/PDPSimple.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
#include <fastdds/builtin/type_lookup_service/TypeLookupManager.hpp>
3232
#include <rtps/builtin/BuiltinProtocols.h>
33-
#include <rtps/builtin/data/NetworkConfiguration.hpp>
3433
#include <rtps/builtin/data/ParticipantProxyData.hpp>
3534
#include <rtps/builtin/data/ReaderProxyData.hpp>
3635
#include <rtps/builtin/data/WriterProxyData.hpp>
@@ -42,6 +41,7 @@
4241
#include <rtps/builtin/discovery/participant/simple/SimplePDPEndpointsSecure.hpp>
4342
#include <rtps/builtin/liveliness/WLP.hpp>
4443
#include <rtps/history/TopicPayloadPoolRegistry.hpp>
44+
#include <rtps/network/NetworkConfiguration.hpp>
4545
#include <rtps/participant/RTPSParticipantImpl.hpp>
4646
#include <rtps/reader/BaseReader.hpp>
4747
#include <rtps/reader/StatefulReader.hpp>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <cassert>
16+
17+
#include <fastdds/rtps/common/Locator.hpp>
18+
19+
#include <rtps/network/NetworkConfiguration.hpp>
20+
21+
namespace eprosima {
22+
namespace fastdds {
23+
namespace rtps {
24+
namespace network {
25+
26+
void add_localhost_capability(
27+
int32_t kind,
28+
NetworkConfigSet_t& network_config)
29+
{
30+
// Only add localhost capability for transports that support it
31+
if (kind == LOCATOR_KIND_UDPv4 || kind == LOCATOR_KIND_UDPv6 ||
32+
kind == LOCATOR_KIND_TCPv4 || kind == LOCATOR_KIND_TCPv6)
33+
{
34+
// Ensure the kind is a power of two to perform safe bitwise operations
35+
assert(kind > 0 && (kind & (kind - 1)) == 0);
36+
37+
network_config |= kind;
38+
}
39+
}
40+
41+
} // namespace network
42+
} // namespace rtps
43+
} // namespace fastdds
44+
} // namespace eprosima
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file NetworkConfiguration.hpp
17+
*/
18+
19+
#ifndef FASTDDS_RTPS_NETWORK__NETWORKCONFIGURATION_HPP
20+
#define FASTDDS_RTPS_NETWORK__NETWORKCONFIGURATION_HPP
21+
22+
#include <fastdds/rtps/common/Types.hpp>
23+
24+
#define DISC_NETWORK_CONFIGURATION_LISTENING_LOCALHOST_ALL (0x0000000F)
25+
26+
namespace eprosima {
27+
namespace fastdds {
28+
namespace rtps {
29+
namespace network {
30+
31+
/**
32+
* @brief Add the capability to use localhost for the given transport kind.
33+
*
34+
* This function adds the capability to use localhost for the given transport kind
35+
* in the provided network configuration.
36+
*
37+
* @param kind The transport kind to add the localhost capability for.
38+
* @param network_config The network configuration to modify.
39+
*/
40+
void add_localhost_capability(
41+
int32_t kind,
42+
NetworkConfigSet_t& network_config);
43+
44+
} // namespace network
45+
} // namespace rtps
46+
} // namespace fastdds
47+
} // namespace eprosima
48+
49+
#endif // FASTDDS_RTPS_NETWORK__NETWORKCONFIGURATION_HPP

src/cpp/rtps/network/NetworkFactory.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <fastdds/utils/IPFinder.hpp>
2525
#include <fastdds/utils/IPLocator.hpp>
2626

27+
#include <rtps/network/NetworkConfiguration.hpp>
2728
#include <rtps/transport/TCPTransportInterface.h>
2829

2930
using namespace std;
@@ -166,7 +167,9 @@ bool NetworkFactory::RegisterTransport(
166167

167168
if (is_localhost_allowed)
168169
{
169-
network_configuration_ |= kind;
170+
network::add_localhost_capability(
171+
kind,
172+
network_configuration_);
170173
}
171174
}
172175
}

test/blackbox/common/BlackboxTestsNetworkConf.cpp

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ TEST_P(NetworkConfig, PubSubInterfaceWhitelistLocalhost)
361361
void interface_whitelist_test(
362362
const std::vector<IPFinder::info_IP>& pub_interfaces,
363363
const std::vector<IPFinder::info_IP>& sub_interfaces,
364-
bool interface_name = false)
364+
bool interface_name = false,
365+
bool add_shm_descriptor = false)
365366
{
366367
PubSubReader<HelloWorldPubSubType> reader(TEST_TOPIC_NAME);
367368
PubSubWriter<HelloWorldPubSubType> writer(TEST_TOPIC_NAME);
@@ -378,7 +379,7 @@ void interface_whitelist_test(
378379
pub_udp_descriptor = std::make_shared<UDPv6TransportDescriptor>();
379380
}
380381

381-
// include the interfaces in the transport descriptor
382+
// include the interfaces in the UDP transport descriptor
382383
for (const auto& network_interface : pub_interfaces)
383384
{
384385
if (!interface_name)
@@ -391,10 +392,26 @@ void interface_whitelist_test(
391392
}
392393
}
393394

394-
// Set the transport descriptor WITH interfaces in the writer
395+
std::shared_ptr<SharedMemTransportDescriptor> pub_shm_descriptor;
396+
397+
if (add_shm_descriptor)
398+
{
399+
pub_shm_descriptor = std::make_shared<SharedMemTransportDescriptor>();
400+
}
401+
402+
// Set the UDP transport descriptor WITH interfaces in the writer
395403
writer.reliability(eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS).history_depth(10).
396404
disable_builtin_transport().
397-
add_user_transport_to_pparams(pub_udp_descriptor).init();
405+
add_user_transport_to_pparams(pub_udp_descriptor);
406+
407+
// If shared memory is requested, add it as well
408+
if (add_shm_descriptor)
409+
{
410+
writer.add_user_transport_to_pparams(pub_shm_descriptor);
411+
}
412+
413+
// Initialize the writer
414+
writer.init();
398415

399416
ASSERT_TRUE(writer.isInitialized());
400417

@@ -409,7 +426,7 @@ void interface_whitelist_test(
409426
sub_udp_descriptor = std::make_shared<UDPv6TransportDescriptor>();
410427
}
411428

412-
// include the interfaces in the transport descriptor
429+
// include the interfaces in the UDP transport descriptor
413430
for (const auto& network_interface : sub_interfaces)
414431
{
415432
if (!interface_name)
@@ -422,10 +439,26 @@ void interface_whitelist_test(
422439
}
423440
}
424441

425-
// Set the transport descriptor WITH interfaces in the reader
442+
std::shared_ptr<SharedMemTransportDescriptor> sub_shm_descriptor;
443+
444+
if (add_shm_descriptor)
445+
{
446+
sub_shm_descriptor = std::make_shared<SharedMemTransportDescriptor>();
447+
}
448+
449+
// Set the UDP transport descriptor WITH interfaces in the reader
426450
reader.reliability(eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS).history_depth(10).
427451
disable_builtin_transport().
428-
add_user_transport_to_pparams(sub_udp_descriptor).init();
452+
add_user_transport_to_pparams(sub_udp_descriptor);
453+
454+
// If shared memory is requested, add it as well
455+
if (add_shm_descriptor)
456+
{
457+
reader.add_user_transport_to_pparams(sub_shm_descriptor);
458+
}
459+
460+
// Initialize the reader
461+
reader.init();
429462

430463
ASSERT_TRUE(reader.isInitialized());
431464

@@ -622,6 +655,44 @@ TEST_P(NetworkConfig, PubSubAsymmetricInterfaceWhitelistAllExceptLocalhost)
622655
}
623656
}
624657

658+
// Regression test for redmine issue #23213 to check in UDP that setting the interface whitelist in one
659+
// of the endpoints, but not in the other, connection is established anyways.
660+
// All available interfaces except loopback case.
661+
// This test verifies that adding a SHM transport descriptor does not affect the communication in this concrete case.
662+
TEST_P(NetworkConfig, PubSubAsymmetricInterfaceWhitelistAllExceptLocalhostSHM)
663+
{
664+
std::vector<IPFinder::info_IP> no_interfaces;
665+
666+
std::vector<IPFinder::info_IP> all_interfaces_except_localhost;
667+
use_udpv4 ? GetIP4s(all_interfaces_except_localhost, false) : GetIP6s(all_interfaces_except_localhost, false);
668+
669+
{
670+
// IP address
671+
{
672+
// Whitelist only in publisher
673+
interface_whitelist_test(all_interfaces_except_localhost, no_interfaces, false, true);
674+
}
675+
676+
{
677+
// Whitelist only in subscriber
678+
interface_whitelist_test(no_interfaces, all_interfaces_except_localhost, false, true);
679+
}
680+
}
681+
682+
{
683+
// Interface name
684+
{
685+
// Whitelist only in publisher
686+
interface_whitelist_test(all_interfaces_except_localhost, no_interfaces, true, true);
687+
}
688+
689+
{
690+
// Whitelist only in subscriber
691+
interface_whitelist_test(no_interfaces, all_interfaces_except_localhost, true, true);
692+
}
693+
}
694+
}
695+
625696
TEST_P(NetworkConfig, SubGetListeningLocators)
626697
{
627698
PubSubReader<HelloWorldPubSubType> reader(TEST_TOPIC_NAME);

test/blackbox/common/DDSBlackboxTestsMonitorService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2944,7 +2944,7 @@ TEST(DDSMonitorServiceTest, monitor_service_proxy_optional_qos)
29442944
expected_participant_builtin_topic_data.wire_protocol->prefix =
29452945
expected_participant_builtin_topic_data.guid.guidPrefix;
29462946
expected_participant_builtin_topic_data.wire_protocol->participant_id = 0;
2947-
expected_participant_builtin_topic_data.wire_protocol->builtin.network_configuration = LOCATOR_KIND_SHM;
2947+
expected_participant_builtin_topic_data.wire_protocol->builtin.network_configuration = LOCATOR_KIND_UDPv4;
29482948

29492949
validator->register_remote_participant_builtin_topic_data(expected_participant_builtin_topic_data);
29502950

test/unittest/dds/publisher/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ set(DATAWRITERTESTS_SOURCE DataWriterTests.cpp
139139
${PROJECT_SOURCE_DIR}/src/cpp/rtps/messages/RTPSMessageGroup.cpp
140140
${PROJECT_SOURCE_DIR}/src/cpp/rtps/messages/SendBuffersManager.cpp
141141
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/NetworkBuffer.cpp
142+
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/NetworkConfiguration.cpp
142143
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/NetworkFactory.cpp
143144
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/ReceiverResource.cpp
144145
${PROJECT_SOURCE_DIR}/src/cpp/rtps/network/utils/external_locators.cpp

0 commit comments

Comments
 (0)