Add localhost option to node creation (#520)

* Add localhost option to node creation

Signed-off-by: Brian Ezequiel Marchi <brian.marchi65@gmail.com>

* Bring localhost function from rmw

Signed-off-by: Brian Ezequiel Marchi <brian.marchi65@gmail.com>

* Satisfy uncrustify and address pr comments

Signed-off-by: Brian Ezequiel Marchi <brian.marchi65@gmail.com>
This commit is contained in:
Brian Marchi 2019-10-18 19:30:22 -03:00 committed by Michel Hidalgo
parent 60bdbd26dc
commit a073507182
4 changed files with 75 additions and 1 deletions

View file

@ -45,6 +45,7 @@ set(${PROJECT_NAME}_sources
src/rcl/init_options.c src/rcl/init_options.c
src/rcl/lexer.c src/rcl/lexer.c
src/rcl/lexer_lookahead.c src/rcl/lexer_lookahead.c
src/rcl/localhost.c
src/rcl/logging_rosout.c src/rcl/logging_rosout.c
src/rcl/logging.c src/rcl/logging.c
src/rcl/node.c src/rcl/node.c

View file

@ -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_

30
rcl/src/rcl/localhost.c Normal file
View file

@ -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 <rcl/localhost.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View file

@ -26,6 +26,7 @@ extern "C"
#include "rcl/arguments.h" #include "rcl/arguments.h"
#include "rcl/error_handling.h" #include "rcl/error_handling.h"
#include "rcl/localhost.h"
#include "rcl/logging.h" #include "rcl/logging.h"
#include "rcl/logging_rosout.h" #include "rcl/logging_rosout.h"
#include "rcl/rcl.h" #include "rcl/rcl.h"
@ -329,7 +330,7 @@ rcl_node_init(
} }
node->impl->rmw_node_handle = rmw_create_node( node->impl->rmw_node_handle = rmw_create_node(
&(node->context->impl->rmw_context), &(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( RCL_CHECK_FOR_NULL_WITH_MSG(
node->impl->rmw_node_handle, rmw_get_error_string().str, goto fail); node->impl->rmw_node_handle, rmw_get_error_string().str, goto fail);