microRTPS: allow communications only in the localhost network when ROS_LOCALHOST_ONLY env variable is set

This commit is contained in:
TSC21 2021-05-01 11:05:16 +02:00 committed by Nuno Marques
parent 578e426e9e
commit 323ce797f8
3 changed files with 42 additions and 3 deletions

View File

@ -25,7 +25,7 @@ except AttributeError:
/****************************************************************************
*
* Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
* Copyright (c) 2018-2019 PX4 Development Team. All rights reserved.
* Copyright (c) 2018-2021 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -67,6 +67,7 @@ except AttributeError:
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/publisher/Publisher.h>
#include <fastrtps/attributes/PublisherAttributes.h>
#include <fastrtps/transport/UDPv4TransportDescriptor.h>
#include <fastrtps/Domain.h>
@ -99,6 +100,22 @@ bool @(topic)_Publisher::init(const std::string& ns)
std::string nodeName = ns;
nodeName.append("@(topic)_publisher");
PParam.rtps.setName(nodeName.c_str());
// Check if ROS_LOCALHOST_ONLY is set. This means that one wants to use
// only the localhost network for data sharing
const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY");
if (localhost_only && strcmp(localhost_only, "1") == 0) {
// Create a custom network transport descriptor to whitelist the localhost
auto localhostDescriptor = std::make_shared<UDPv4TransportDescriptor>();
localhostDescriptor->interfaceWhiteList.emplace_back("127.0.0.1");
// Disable the built-in Transport Layer
PParam.rtps.useBuiltinTransports = false;
// Add the descriptor as a custom user transport
PParam.rtps.userTransports.push_back(localhostDescriptor);
}
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
return false;

View File

@ -25,7 +25,7 @@ except AttributeError:
/****************************************************************************
*
* Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
* Copyright (c) 2018-2019 PX4 Development Team. All rights reserved.
* Copyright (c) 2018-2021 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -66,6 +66,7 @@ except AttributeError:
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/subscriber/Subscriber.h>
#include <fastrtps/attributes/SubscriberAttributes.h>
#include <fastrtps/transport/UDPv4TransportDescriptor.h>
#include <fastrtps/Domain.h>
@ -103,6 +104,22 @@ bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable* t_send
std::string nodeName = ns;
nodeName.append("@(topic)_subscriber");
PParam.rtps.setName(nodeName.c_str());
// Check if ROS_LOCALHOST_ONLY is set. This means that one wants to use
// only the localhost network for data sharing
const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY");
if (localhost_only && strcmp(localhost_only, "1") == 0) {
// Create a custom network transport descriptor to whitelist the localhost
auto localhostDescriptor = std::make_shared<UDPv4TransportDescriptor>();
localhostDescriptor->interfaceWhiteList.emplace_back("127.0.0.1");
// Disable the built-in Transport Layer
PParam.rtps.useBuiltinTransports = false;
// Add the descriptor as a custom user transport
PParam.rtps.userTransports.push_back(localhostDescriptor);
}
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
return false;

View File

@ -22,7 +22,7 @@ recv_topics = [(alias[idx] if alias[idx] else s.short_name) for idx, s in enumer
/****************************************************************************
*
* Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
* Copyright (c) 2018-2019 PX4 Development Team. All rights reserved.
* Copyright (c) 2018-2021 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -230,6 +230,11 @@ int main(int argc, char** argv)
printf("\033[0;37m--- MicroRTPS Agent ---\033[0m\n");
printf("[ micrortps_agent ]\tStarting link...\n");
const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY");
if (localhost_only && strcmp(localhost_only, "1") == 0) {
printf("[ micrortps_agent ]\tUsing only the localhost network for data sharing...\n");
}
switch (_options.transport)
{
case options::eTransports::UART: