Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localhost option to node creation #520

Merged
merged 3 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
Expand Down
42 changes: 42 additions & 0 deletions rcl/include/rcl/localhost.h
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions rcl/src/rcl/localhost.c
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion rcl/src/rcl/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -328,7 +329,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);
Expand Down