transport.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014, Open Source Robotics Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
36 #include "ros/console.h"
37 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #include <netdb.h>
40 
41 #if !defined(__ANDROID__)
42 #include <ifaddrs.h>
43 #endif
44 
45 #ifndef NI_MAXHOST
46  #define NI_MAXHOST 1025
47 #endif
48 
49 namespace ros
50 {
51 
53 : only_localhost_allowed_(false)
54 {
55  char *ros_ip_env = NULL, *ros_hostname_env = NULL;
56  #ifdef _MSC_VER
57  _dupenv_s(&ros_ip_env, NULL, "ROS_IP");
58  _dupenv_s(&ros_hostname_env, NULL, "ROS_HOSTNAME");
59  #else
60  ros_ip_env = getenv("ROS_IP");
61  ros_hostname_env = getenv("ROS_HOSTNAME");
62  #endif
63  if (ros_hostname_env && !strcmp(ros_hostname_env, "localhost"))
65  else if (ros_ip_env && !strncmp(ros_ip_env, "127.", 4))
67  else if (ros_ip_env && !strcmp(ros_ip_env, "::1"))
69 
70  char our_hostname[256] = {0};
71  gethostname(our_hostname, sizeof(our_hostname)-1);
72  allowed_hosts_.push_back(std::string(our_hostname));
73  allowed_hosts_.push_back("localhost");
74 #if !defined(__ANDROID__)
75  // for ipv4 loopback, we'll explicitly search for 127.* in isHostAllowed()
76  // now we need to iterate all local interfaces and add their addresses
77  // from the getifaddrs manpage: (maybe something similar for windows ?)
78  ifaddrs *ifaddr;
79  if (-1 == getifaddrs(&ifaddr))
80  {
81  ROS_ERROR("getifaddr() failed");
82  return;
83  }
84  for (ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next)
85  {
86  if(NULL == ifa->ifa_addr)
87  continue; // ifa_addr can be NULL
88  int family = ifa->ifa_addr->sa_family;
89  if (family != AF_INET && family != AF_INET6)
90  continue; // we're only looking for IP addresses
91  char addr[NI_MAXHOST] = {0};
92  if (getnameinfo(ifa->ifa_addr,
93  (family == AF_INET) ? sizeof(sockaddr_in)
94  : sizeof(sockaddr_in6),
95  addr, NI_MAXHOST,
96  NULL, 0, NI_NUMERICHOST))
97  {
98  ROS_ERROR("getnameinfo() failed");
99  continue;
100  }
101  allowed_hosts_.push_back(std::string(addr));
102  }
103  freeifaddrs(ifaddr);
104 #endif
105 }
106 
107 bool Transport::isHostAllowed(const std::string &host) const
108 {
110  return true; // doesn't matter; we'll connect to anybody
111 
112  if (host.length() >= 4 && host.substr(0, 4) == std::string("127."))
113  return true; // ipv4 localhost
114  // now, loop through the list of valid hostnames and see if we find it
115  for (std::vector<std::string>::const_iterator it = allowed_hosts_.begin();
116  it != allowed_hosts_.end(); ++it)
117  {
118  if (host == *it)
119  return true; // hooray
120  }
121  ROS_WARN("ROS_HOSTNAME / ROS_IP is set to only allow local connections, so "
122  "a requested connection to '%s' is being rejected.", host.c_str());
123  return false; // sadness
124 }
125 
126 }
127 
#define ROS_WARN(...)
#define NI_MAXHOST
Definition: transport.cpp:46
bool only_localhost_allowed_
Definition: transport.h:150
std::vector< std::string > allowed_hosts_
Definition: transport.h:151
bool isHostAllowed(const std::string &host) const
returns true if the transport is allowed to connect to the host passed to it.
Definition: transport.cpp:107
#define ROS_ERROR(...)


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54