From a073507182d1a28ffdeb003609157a0ea38a1029 Mon Sep 17 00:00:00 2001 From: Brian Marchi Date: Fri, 18 Oct 2019 19:30:22 -0300 Subject: [PATCH] Add localhost option to node creation (#520) * Add localhost option to node creation Signed-off-by: Brian Ezequiel Marchi * Bring localhost function from rmw Signed-off-by: Brian Ezequiel Marchi * Satisfy uncrustify and address pr comments Signed-off-by: Brian Ezequiel Marchi --- rcl/CMakeLists.txt | 1 + rcl/include/rcl/localhost.h | 42 +++++++++++++++++++++++++++++++++++++ rcl/src/rcl/localhost.c | 30 ++++++++++++++++++++++++++ rcl/src/rcl/node.c | 3 ++- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 rcl/include/rcl/localhost.h create mode 100644 rcl/src/rcl/localhost.c diff --git a/rcl/CMakeLists.txt b/rcl/CMakeLists.txt index e60184371..cca35b24b 100644 --- a/rcl/CMakeLists.txt +++ b/rcl/CMakeLists.txt @@ -45,6 +45,7 @@ set(${PROJECT_NAME}_sources src/rcl/init_options.c src/rcl/lexer.c src/rcl/lexer_lookahead.c + src/rcl/localhost.c src/rcl/logging_rosout.c src/rcl/logging.c src/rcl/node.c diff --git a/rcl/include/rcl/localhost.h b/rcl/include/rcl/localhost.h new file mode 100644 index 000000000..be41615d8 --- /dev/null +++ b/rcl/include/rcl/localhost.h @@ -0,0 +1,42 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RCL__LOCALHOST_H_ +#define RCL__LOCALHOST_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "rcl/types.h" +#include "rcl/visibility_control.h" + +extern const char * const RCL_LOCALHOST_ENV_VAR; + +/// Determine if the user wants to communicate using loopback only. +/** + * Checks if localhost should be used for network communication checking ROS_LOCALHOST_ONLY env + * variable + * \returns true if ROS_LOCALHOST_ONLY is set and is 1, false otherwise. + */ +RCL_PUBLIC +bool +rcl_localhost_only(); + +#ifdef __cplusplus +} +#endif + +#endif // RCL__LOCALHOST_H_ diff --git a/rcl/src/rcl/localhost.c b/rcl/src/rcl/localhost.c new file mode 100644 index 000000000..1d5945ad3 --- /dev/null +++ b/rcl/src/rcl/localhost.c @@ -0,0 +1,30 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +#include "rcutils/get_env.h" + +const char * const RCL_LOCALHOST_ENV_VAR = "ROS_LOCALHOST_ONLY"; + +bool +rcl_localhost_only() +{ + const char * ros_local_host_env_val = NULL; + return rcutils_get_env(RCL_LOCALHOST_ENV_VAR, &ros_local_host_env_val) == NULL && + ros_local_host_env_val != NULL && strcmp(ros_local_host_env_val, "1") == 0; +} diff --git a/rcl/src/rcl/node.c b/rcl/src/rcl/node.c index 65ced0db4..5522c790f 100644 --- a/rcl/src/rcl/node.c +++ b/rcl/src/rcl/node.c @@ -26,6 +26,7 @@ extern "C" #include "rcl/arguments.h" #include "rcl/error_handling.h" +#include "rcl/localhost.h" #include "rcl/logging.h" #include "rcl/logging_rosout.h" #include "rcl/rcl.h" @@ -329,7 +330,7 @@ rcl_node_init( } node->impl->rmw_node_handle = rmw_create_node( &(node->context->impl->rmw_context), - name, local_namespace_, domain_id, &node_security_options); + name, local_namespace_, domain_id, &node_security_options, rcl_localhost_only()); RCL_CHECK_FOR_NULL_WITH_MSG( node->impl->rmw_node_handle, rmw_get_error_string().str, goto fail);