Skip to content

Commit b65cdb6

Browse files
MiguelCompanyfujitatomoya
authored andcommitted
Use interface whitelist for localhost only (ros2#476)
* Use interface whitelist for localhost only Signed-off-by: Miguel Company <[email protected]>
1 parent 684b10b commit b65cdb6

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

rmw_fastrtps_shared_cpp/src/participant.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <limits.h>
1717
#include <string>
18+
#include <memory>
1819

1920
#include "fastrtps/config.h"
2021
#include "fastrtps/Domain.h"
@@ -33,6 +34,7 @@
3334
#include "fastrtps/subscriber/Subscriber.h"
3435
#include "fastrtps/subscriber/SubscriberListener.h"
3536
#include "fastrtps/subscriber/SampleInfo.h"
37+
#include "fastrtps/transport/UDPv4TransportDescriptor.h"
3638

3739
#include "rcutils/filesystem.h"
3840
#include "rcutils/get_env.h"
@@ -50,6 +52,12 @@ using Locator_t = eprosima::fastrtps::rtps::Locator_t;
5052
using Participant = eprosima::fastrtps::Participant;
5153
using ParticipantAttributes = eprosima::fastrtps::ParticipantAttributes;
5254
using StatefulReader = eprosima::fastrtps::rtps::StatefulReader;
55+
using UDPv4TransportDescriptor = eprosima::fastrtps::rtps::UDPv4TransportDescriptor;
56+
57+
#if FASTRTPS_VERSION_MAJOR >= 2
58+
#include "fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h"
59+
using SharedMemTransportDescriptor = eprosima::fastdds::rtps::SharedMemTransportDescriptor;
60+
#endif
5361

5462
#if HAVE_SECURITY
5563
static
@@ -154,14 +162,19 @@ rmw_fastrtps_shared_cpp::create_participant(
154162
Domain::getDefaultParticipantAttributes(participantAttrs);
155163

156164
if (localhost_only) {
157-
Locator_t local_network_interface_locator;
158-
static const std::string local_ip_name("127.0.0.1");
159-
local_network_interface_locator.kind = 1;
160-
local_network_interface_locator.port = 0;
161-
IPLocator::setIPv4(local_network_interface_locator, local_ip_name);
162-
participantAttrs.rtps.builtin.metatrafficUnicastLocatorList.push_back(
163-
local_network_interface_locator);
164-
participantAttrs.rtps.builtin.initialPeersList.push_back(local_network_interface_locator);
165+
// In order to use the interface white list, we need to disable the default transport config
166+
participantAttrs.rtps.useBuiltinTransports = false;
167+
168+
// Add a UDPv4 transport with only localhost enabled
169+
auto udp_transport = std::make_shared<UDPv4TransportDescriptor>();
170+
udp_transport->interfaceWhiteList.emplace_back("127.0.0.1");
171+
participantAttrs.rtps.userTransports.push_back(udp_transport);
172+
173+
// Add SHM transport if available
174+
#if FASTRTPS_VERSION_MAJOR >= 2
175+
auto shm_transport = std::make_shared<SharedMemTransportDescriptor>();
176+
participantAttrs.rtps.userTransports.push_back(shm_transport);
177+
#endif
165178
}
166179

167180
// No custom handling of RMW_DEFAULT_DOMAIN_ID. Simply use a reasonable domain id.

0 commit comments

Comments
 (0)