diff --git a/msg/templates/urtps/Publisher.cpp.em b/msg/templates/urtps/Publisher.cpp.em index 8454b52221..e24c03417f 100644 --- a/msg/templates/urtps/Publisher.cpp.em +++ b/msg/templates/urtps/Publisher.cpp.em @@ -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 #include #include +#include #include @@ -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(); + 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; diff --git a/msg/templates/urtps/Subscriber.cpp.em b/msg/templates/urtps/Subscriber.cpp.em index 4cb048d74e..49f3674bb8 100644 --- a/msg/templates/urtps/Subscriber.cpp.em +++ b/msg/templates/urtps/Subscriber.cpp.em @@ -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 #include #include +#include #include @@ -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(); + 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; diff --git a/msg/templates/urtps/microRTPS_agent.cpp.em b/msg/templates/urtps/microRTPS_agent.cpp.em index 911357c224..88740101a4 100644 --- a/msg/templates/urtps/microRTPS_agent.cpp.em +++ b/msg/templates/urtps/microRTPS_agent.cpp.em @@ -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: