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 1 commit
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"
"src/init.c"
"src/init_options.c"
"src/names_and_types.c"
Expand Down
52 changes: 52 additions & 0 deletions rmw/include/rmw/host.h
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions rmw/src/host.c
Original file line number Diff line number Diff line change
@@ -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 <rmw/host.h>

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

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;
}