From 453b20781c74216fc04539a94d1c0f939e8e4a30 Mon Sep 17 00:00:00 2001 From: Brian Ezequiel Marchi Date: Fri, 11 Oct 2019 12:30:11 -0300 Subject: [PATCH 1/5] Add function to check for allowed hosts from env var Signed-off-by: Brian Ezequiel Marchi --- rmw/CMakeLists.txt | 1 + rmw/include/rmw/host.h | 52 ++++++++++++++++++++++++++++++++++++++++++ rmw/src/host.c | 50 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 rmw/include/rmw/host.h create mode 100644 rmw/src/host.c diff --git a/rmw/CMakeLists.txt b/rmw/CMakeLists.txt index 632e32dd..f6d76fe8 100644 --- a/rmw/CMakeLists.txt +++ b/rmw/CMakeLists.txt @@ -28,6 +28,7 @@ set(rmw_sources "src/allocators.c" "src/convert_rcutils_ret_to_rmw_ret.c" "src/event.c" + "src/host.c" "src/init.c" "src/init_options.c" "src/names_and_types.c" diff --git a/rmw/include/rmw/host.h b/rmw/include/rmw/host.h new file mode 100644 index 00000000..c958c5de --- /dev/null +++ b/rmw/include/rmw/host.h @@ -0,0 +1,52 @@ +// 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 RMW__IMPL__CPP__HOST_H +#define RMW__IMPL__CPP__HOST_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "rmw/macros.h" +#include "rmw/types.h" + +#define RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY 0 +#define RMW_LOCAL_HOST_ENABLED 1 +#define RMW_INVALID_ALLOWED_HOSTS 2 + + +/// Determine if the user provided a list of hosts to communicate to in the +/** + * Checks the allowed hosts declared by the user in the environment variable ROS_ALLOWED_HOSTS. + * localhost is the only host supported besides using all network interfaces. Allocates memory for + * the list of hosts if it's not empty and supported. + * \param[out] allowed_hosts string that contains the list of allowed hosts. + * \returns `RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY` if the env var is not defined or is empty. + * \returns `RMW_LOCAL_HOST_ENABLED` if the environment variable is localhost + * \returns `RMW_INVALID_ALLOWED_HOSTS` when the user provides a non empty string + * different than localhost. + */ +RMW_PUBLIC +RMW_WARN_UNUSED +rmw_ret_t +rmw_allowed_hosts( + char * allowed_hosts); + +#ifdef __cplusplus +} +#endif + +#endif // RMW__IMPL__CPP__HOST_H diff --git a/rmw/src/host.c b/rmw/src/host.c new file mode 100644 index 00000000..21abfedc --- /dev/null +++ b/rmw/src/host.c @@ -0,0 +1,50 @@ +// 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 + +rmw_ret_t +rmw_allowed_hosts( + char * allowed_hosts) +{ + rmw_ret_t ret = RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY; + const char * ros_host_env = "ROS_ALLOWED_HOSTS"; +#ifndef _WIN32 + const char * ros_host_env_val = getenv(ros_host_env); + if (ros_host_env_val != NULL && strlen(ros_host_env_val) > 0) { + allowed_hosts = malloc(strlen(ros_host_env_val)+1); + strcpy(allowed_hosts, ros_host_env_val); + } +#else + size_t ros_host_env_val_size; + _dupenv_s(&allowed_hosts, &ros_host_env_val_size, ros_host_env); +#endif + if (allowed_hosts != NULL && strcmp(allowed_hosts, "") != 0) { + if (strcmp(allowed_hosts, "localhost") != 0) { + ret = RMW_INVALID_ALLOWED_HOSTS; + free(allowed_hosts); + } else { + ret = RMW_LOCAL_HOST_ENABLED; + } + } else { + ret = RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY; + if (allowed_hosts != NULL) { + free(allowed_hosts); + } + } + return ret; +} From 56fd4c5764eee79d1c729a9cadf54766c37042ec Mon Sep 17 00:00:00 2001 From: Brian Ezequiel Marchi Date: Tue, 15 Oct 2019 11:39:10 -0300 Subject: [PATCH 2/5] Change the use and name of the local host env variable Signed-off-by: Brian Ezequiel Marchi --- rmw/include/rmw/host.h | 23 +++++++---------------- rmw/src/host.c | 37 ++++++++----------------------------- 2 files changed, 15 insertions(+), 45 deletions(-) diff --git a/rmw/include/rmw/host.h b/rmw/include/rmw/host.h index c958c5de..d72ca477 100644 --- a/rmw/include/rmw/host.h +++ b/rmw/include/rmw/host.h @@ -23,27 +23,18 @@ extern "C" #include "rmw/macros.h" #include "rmw/types.h" -#define RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY 0 -#define RMW_LOCAL_HOST_ENABLED 1 -#define RMW_INVALID_ALLOWED_HOSTS 2 +const char * RMW_LOCAL_HOST_ENV_VAR = "ROS_LOCALHOST_ONLY"; - -/// Determine if the user provided a list of hosts to communicate to in the +/// Determine if the user wants to communicate using loopback only. /** - * Checks the allowed hosts declared by the user in the environment variable ROS_ALLOWED_HOSTS. - * localhost is the only host supported besides using all network interfaces. Allocates memory for - * the list of hosts if it's not empty and supported. - * \param[out] allowed_hosts string that contains the list of allowed hosts. - * \returns `RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY` if the env var is not defined or is empty. - * \returns `RMW_LOCAL_HOST_ENABLED` if the environment variable is localhost - * \returns `RMW_INVALID_ALLOWED_HOSTS` when the user provides a non empty string - * different than localhost. + * Checks if localhost should be used for network communication checking ROS_LOCALHOST_ONLY env + * variable + * \returns true if ROS_LOCALHOST_ONLY is 1 or true, false otherwise. */ RMW_PUBLIC RMW_WARN_UNUSED -rmw_ret_t -rmw_allowed_hosts( - char * allowed_hosts); +bool +rmw_local_host_only(); #ifdef __cplusplus } diff --git a/rmw/src/host.c b/rmw/src/host.c index 21abfedc..10a32338 100644 --- a/rmw/src/host.c +++ b/rmw/src/host.c @@ -14,37 +14,16 @@ #include +#include "rcutils/get_env.h" + #include #include -rmw_ret_t -rmw_allowed_hosts( - char * allowed_hosts) +bool +rmw_local_host_only() { - rmw_ret_t ret = RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY; - const char * ros_host_env = "ROS_ALLOWED_HOSTS"; -#ifndef _WIN32 - const char * ros_host_env_val = getenv(ros_host_env); - if (ros_host_env_val != NULL && strlen(ros_host_env_val) > 0) { - allowed_hosts = malloc(strlen(ros_host_env_val)+1); - strcpy(allowed_hosts, ros_host_env_val); - } -#else - size_t ros_host_env_val_size; - _dupenv_s(&allowed_hosts, &ros_host_env_val_size, ros_host_env); -#endif - if (allowed_hosts != NULL && strcmp(allowed_hosts, "") != 0) { - if (strcmp(allowed_hosts, "localhost") != 0) { - ret = RMW_INVALID_ALLOWED_HOSTS; - free(allowed_hosts); - } else { - ret = RMW_LOCAL_HOST_ENABLED; - } - } else { - ret = RMW_ENV_VAR_NOT_DEFINED_OR_EMPTY; - if (allowed_hosts != NULL) { - free(allowed_hosts); - } - } - return ret; + const char * ros_local_host_env_val = NULL; + return rcutils_get_env(RMW_LOCAL_HOST_ENV_VAR, &ros_local_host_env_val) == NULL && + ros_local_host_env_val != NULL && + (strcmp(ros_local_host_env_val, "true") == 0 || strcmp(ros_local_host_env_val, "1") == 0); } From 027b568c17dcc65c61b12d2e582e14b23683c30c Mon Sep 17 00:00:00 2001 From: Brian Ezequiel Marchi Date: Tue, 15 Oct 2019 16:52:20 -0300 Subject: [PATCH 3/5] Rename host file and functions Signed-off-by: Brian Ezequiel Marchi --- rmw/CMakeLists.txt | 2 +- rmw/include/rmw/{host.h => localhost.h} | 6 +++--- rmw/src/{host.c => localhost.c} | 11 +++++------ 3 files changed, 9 insertions(+), 10 deletions(-) rename rmw/include/rmw/{host.h => localhost.h} (87%) rename rmw/src/{host.c => localhost.c} (69%) diff --git a/rmw/CMakeLists.txt b/rmw/CMakeLists.txt index f6d76fe8..e521d44f 100644 --- a/rmw/CMakeLists.txt +++ b/rmw/CMakeLists.txt @@ -28,7 +28,7 @@ set(rmw_sources "src/allocators.c" "src/convert_rcutils_ret_to_rmw_ret.c" "src/event.c" - "src/host.c" + "src/localhost.c" "src/init.c" "src/init_options.c" "src/names_and_types.c" diff --git a/rmw/include/rmw/host.h b/rmw/include/rmw/localhost.h similarity index 87% rename from rmw/include/rmw/host.h rename to rmw/include/rmw/localhost.h index d72ca477..948bfb80 100644 --- a/rmw/include/rmw/host.h +++ b/rmw/include/rmw/localhost.h @@ -23,18 +23,18 @@ extern "C" #include "rmw/macros.h" #include "rmw/types.h" -const char * RMW_LOCAL_HOST_ENV_VAR = "ROS_LOCALHOST_ONLY"; +const char * RMW_LOCALHOST_ENV_VAR = "ROS_LOCALHOST_ONLY"; /// 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 1 or true, false otherwise. + * \returns true if ROS_LOCALHOST_ONLY is set and is 1, false otherwise. */ RMW_PUBLIC RMW_WARN_UNUSED bool -rmw_local_host_only(); +rmw_localhost_only(); #ifdef __cplusplus } diff --git a/rmw/src/host.c b/rmw/src/localhost.c similarity index 69% rename from rmw/src/host.c rename to rmw/src/localhost.c index 10a32338..1f5255f5 100644 --- a/rmw/src/host.c +++ b/rmw/src/localhost.c @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include #include "rcutils/get_env.h" @@ -20,10 +20,9 @@ #include bool -rmw_local_host_only() +rmw_localhost_only() { - const char * ros_local_host_env_val = NULL; - return rcutils_get_env(RMW_LOCAL_HOST_ENV_VAR, &ros_local_host_env_val) == NULL && - ros_local_host_env_val != NULL && - (strcmp(ros_local_host_env_val, "true") == 0 || strcmp(ros_local_host_env_val, "1") == 0); + const char * ros_local_host_env_val = NULL; + return rcutils_get_env(RMW_LOCALHOST_ENV_VAR, &ros_local_host_env_val) == NULL && + ros_local_host_env_val != NULL && strcmp(ros_local_host_env_val, "1") == 0; } From 0f2e1363bff16a735e499721ba62a55fe9b8dfdc Mon Sep 17 00:00:00 2001 From: Brian Ezequiel Marchi Date: Wed, 16 Oct 2019 10:27:08 -0300 Subject: [PATCH 4/5] Change rmw_create_node signature and add loopback bool Signed-off-by: Brian Ezequiel Marchi --- rmw/include/rmw/rmw.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rmw/include/rmw/rmw.h b/rmw/include/rmw/rmw.h index 1335dcbc..f8a2f51a 100644 --- a/rmw/include/rmw/rmw.h +++ b/rmw/include/rmw/rmw.h @@ -160,6 +160,8 @@ rmw_get_serialization_format(void); * \param[in] namespace_ the node namespace * \param[in] domain_id the id of the domain that the node should join * \param[in] security_options the security configurations for the node + * \param[in] localhost_only whenever to use loopback only for communication or default + * network interfaces. * \return rmw node handle or `NULL` if there was an error */ RMW_PUBLIC @@ -170,7 +172,8 @@ rmw_create_node( const char * name, const char * namespace_, size_t domain_id, - const rmw_node_security_options_t * security_options); + const rmw_node_security_options_t * security_options, + bool localhost_only); /// Finalize a given node handle, reclaim the resources, and deallocate the node handle. /** From adb0ec3a133497fa071fcddf3cabc24b3080de82 Mon Sep 17 00:00:00 2001 From: Brian Ezequiel Marchi Date: Wed, 16 Oct 2019 16:25:32 -0300 Subject: [PATCH 5/5] Move localhost file to rcl Signed-off-by: Brian Ezequiel Marchi --- rmw/CMakeLists.txt | 1 - rmw/include/rmw/localhost.h | 43 ------------------------------------- rmw/src/localhost.c | 28 ------------------------ 3 files changed, 72 deletions(-) delete mode 100644 rmw/include/rmw/localhost.h delete mode 100644 rmw/src/localhost.c diff --git a/rmw/CMakeLists.txt b/rmw/CMakeLists.txt index e521d44f..632e32dd 100644 --- a/rmw/CMakeLists.txt +++ b/rmw/CMakeLists.txt @@ -28,7 +28,6 @@ set(rmw_sources "src/allocators.c" "src/convert_rcutils_ret_to_rmw_ret.c" "src/event.c" - "src/localhost.c" "src/init.c" "src/init_options.c" "src/names_and_types.c" diff --git a/rmw/include/rmw/localhost.h b/rmw/include/rmw/localhost.h deleted file mode 100644 index 948bfb80..00000000 --- a/rmw/include/rmw/localhost.h +++ /dev/null @@ -1,43 +0,0 @@ -// 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 RMW__IMPL__CPP__HOST_H -#define RMW__IMPL__CPP__HOST_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "rmw/macros.h" -#include "rmw/types.h" - -const char * RMW_LOCALHOST_ENV_VAR = "ROS_LOCALHOST_ONLY"; - -/// 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. - */ -RMW_PUBLIC -RMW_WARN_UNUSED -bool -rmw_localhost_only(); - -#ifdef __cplusplus -} -#endif - -#endif // RMW__IMPL__CPP__HOST_H diff --git a/rmw/src/localhost.c b/rmw/src/localhost.c deleted file mode 100644 index 1f5255f5..00000000 --- a/rmw/src/localhost.c +++ /dev/null @@ -1,28 +0,0 @@ -// 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 "rcutils/get_env.h" - -#include -#include - -bool -rmw_localhost_only() -{ - const char * ros_local_host_env_val = NULL; - return rcutils_get_env(RMW_LOCALHOST_ENV_VAR, &ros_local_host_env_val) == NULL && - ros_local_host_env_val != NULL && strcmp(ros_local_host_env_val, "1") == 0; -}