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 function to enable localhost communication only from env var #190

Merged
merged 5 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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 rmw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(rmw_sources
"src/allocators.c"
"src/convert_rcutils_ret_to_rmw_ret.c"
"src/event.c"
"src/host.c"
BMarchi marked this conversation as resolved.
Show resolved Hide resolved
"src/init.c"
"src/init_options.c"
"src/names_and_types.c"
Expand Down
43 changes: 43 additions & 0 deletions rmw/include/rmw/host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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_LOCAL_HOST_ENV_VAR = "ROS_LOCALHOST_ONLY";
BMarchi marked this conversation as resolved.
Show resolved Hide resolved

/// 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.
BMarchi marked this conversation as resolved.
Show resolved Hide resolved
*/
RMW_PUBLIC
RMW_WARN_UNUSED
bool
rmw_local_host_only();

#ifdef __cplusplus
}
#endif

#endif // RMW__IMPL__CPP__HOST_H
29 changes: 29 additions & 0 deletions rmw/src/host.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 <rmw/host.h>

#include "rcutils/get_env.h"

#include <stdlib.h>
#include <string.h>

bool
rmw_local_host_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);
BMarchi marked this conversation as resolved.
Show resolved Hide resolved
}