-
Notifications
You must be signed in to change notification settings - Fork 912
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
implement ROS_IP=localhost behavior for roscpp, and for outbound rospy connections #452
Closed
Closed
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8bfa94f
check ROS_IP for localhost and do the right thing when binding
codebot af94ba8
fix byte ordering in INADDR_LOOPBACK for ipv4
codebot 2529a4f
ensure that when ROS_IP is localhost, nodes will not start outbound c…
codebot 3c6a4cb
also verify host for outbound UDPROS connections
codebot 3d41e2c
tweak rospy warning logic so that ROS_IP=localhost does not cause war…
codebot f2fc36f
allow numeric hostnames for localhost on localhost-only connections
codebot 3bfaf6a
rospy tweak to cause tcpros to check outbound connections if ROS_IP=l…
codebot 18ee16c
refactor to reduce repetition. iterate through local interfaces and c…
codebot 0097b24
identify ipv4 127.* stub to imply local-only connections
codebot 0b806b6
refactor bind code to reduce repetition
codebot 67cdd40
only allow ROS_HOSTNAME (not ROS_IP) to be set to localhost in rospy
codebot e4c5a28
const correctness for Transport() functions
codebot 76c478f
finish const correctness of Tranport() function implementation, and f…
codebot 4a29e84
remove trailing newline
codebot 3f51fc6
only look for ROS_HOSTNAME=localhost; only look for ROS_IP to be 127.…
codebot be21c4e
add ipv6 loopback address check. revert whitespace.
codebot df99e1f
fix comment to refer to ROS_HOSTNAME instead of ROS_IP
codebot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2014, Open Source Robotics Foundation, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* * Neither the name of Willow Garage, Inc. nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include "ros/transport/transport.h" | ||
#include "ros/console.h" | ||
#include <ifaddrs.h> | ||
#include <netinet/in.h> | ||
#include <sys/socket.h> | ||
#include <netdb.h> | ||
|
||
#ifndef NI_MAXHOST | ||
#define NI_MAXHOST 1025 | ||
#endif | ||
|
||
namespace ros | ||
{ | ||
|
||
Transport::Transport() | ||
: only_localhost_allowed_(false) | ||
{ | ||
char *ros_ip_env = NULL, *ros_hostname_env = NULL; | ||
#ifdef _MSC_VER | ||
_dupenv_s(&ros_ip_env, NULL, "ROS_IP"); | ||
_dupenv_s(&ros_hostname_env, NULL, "ROS_HOSTNAME"); | ||
#else | ||
ros_ip_env = getenv("ROS_IP"); | ||
ros_hostname_env = getenv("ROS_HOSTNAME"); | ||
#endif | ||
if (ros_hostname_env && !strcmp(ros_hostname_env, "localhost")) | ||
only_localhost_allowed_ = true; | ||
else if (ros_ip_env && !strncmp(ros_ip_env, "127.", 4)) | ||
only_localhost_allowed_ = true; | ||
|
||
char our_hostname[256] = {0}; | ||
gethostname(our_hostname, sizeof(our_hostname)-1); | ||
allowed_hosts_.push_back(std::string(our_hostname)); | ||
allowed_hosts_.push_back("localhost"); | ||
// for ipv4 loopback, we'll explicitly search for 127.* in isHostAllowed() | ||
// now we need to iterate all local interfaces and add their addresses | ||
// from the getifaddrs manpage: (maybe something similar for windows ?) | ||
ifaddrs *ifaddr; | ||
if (-1 == getifaddrs(&ifaddr)) | ||
{ | ||
ROS_ERROR("getifaddr() failed"); | ||
return; | ||
} | ||
for (ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next) | ||
{ | ||
int family = ifa->ifa_addr->sa_family; | ||
if (family != AF_INET && family != AF_INET6) | ||
continue; // we're only looking for IP addresses | ||
char addr[NI_MAXHOST] = {0}; | ||
if (getnameinfo(ifa->ifa_addr, | ||
(family == AF_INET) ? sizeof(sockaddr_in) | ||
: sizeof(sockaddr_in6), | ||
addr, NI_MAXHOST, | ||
NULL, 0, NI_NUMERICHOST)) | ||
{ | ||
ROS_ERROR("getnameinfo() failed"); | ||
continue; | ||
} | ||
allowed_hosts_.push_back(std::string(addr)); | ||
} | ||
} | ||
|
||
bool Transport::isHostAllowed(const std::string &host) const | ||
{ | ||
if (!only_localhost_allowed_) | ||
return true; // doesn't matter; we'll connect to anybody | ||
|
||
if (host.length() >= 4 && host.substr(0, 4) == std::string("127.")) | ||
return true; // ipv4 localhost | ||
// now, loop through the list of valid hostnames and see if we find it | ||
for (std::vector<std::string>::const_iterator it = allowed_hosts_.begin(); | ||
it != allowed_hosts_.end(); ++it) | ||
{ | ||
if (host == *it) | ||
return true; // hooray | ||
} | ||
ROS_WARN("ROS_HOSTNAME / ROS_IP is set to only allow local connections, so " | ||
"a requested connection to '%s' is being rejected.", host.c_str()); | ||
return false; // sadness | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -518,6 +518,16 @@ def connect(self, dest_addr, dest_port, endpoint_id, timeout=None): | |
@type timeout: float | ||
@raise TransportInitError: if unable to create connection | ||
""" | ||
# first make sure that if ROS_IP=localhost, we will not attempt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, for bother about more details... but the comment should refer to ROS_HOSTNAME. |
||
# to connect to anything other than localhost | ||
if ("ROS_HOSTNAME" in os.environ) and (os.environ["ROS_HOSTNAME"] == "localhost"): | ||
if not rosgraph.network.is_local_address(dest_addr): | ||
msg = "attempted to connect to non-local host [%s] from a node launched with ROS_HOSTNAME=localhost" % (dest_addr) | ||
logwarn(msg) | ||
self.close() | ||
raise TransportInitError(msg) # bubble up | ||
|
||
# now we can proceed with trying to connect. | ||
try: | ||
self.endpoint_id = endpoint_id | ||
self.dest_address = (dest_addr, dest_port) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line also needs to work with IPv6 addresses.