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 #if defined(WIN32)
38 #include <winsock2.h>
39 #else
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <netdb.h>
43 #endif
44 
45 #if !defined(__ANDROID__) && !defined(WIN32)
46 #include <ifaddrs.h>
47 #endif
48 
49 #ifndef NI_MAXHOST
50  #define NI_MAXHOST 1025
51 #endif
52 
53 namespace ros
54 {
55 
57 : only_localhost_allowed_(false)
58 {
59  char *ros_ip_env = NULL, *ros_hostname_env = NULL;
60  #ifdef _MSC_VER
61  _dupenv_s(&ros_ip_env, NULL, "ROS_IP");
62  _dupenv_s(&ros_hostname_env, NULL, "ROS_HOSTNAME");
63  #else
64  ros_ip_env = getenv("ROS_IP");
65  ros_hostname_env = getenv("ROS_HOSTNAME");
66  #endif
67  if (ros_hostname_env && !strcmp(ros_hostname_env, "localhost"))
69  else if (ros_ip_env && !strncmp(ros_ip_env, "127.", 4))
71  else if (ros_ip_env && !strcmp(ros_ip_env, "::1"))
73 
74  char our_hostname[256] = {0};
75  gethostname(our_hostname, sizeof(our_hostname)-1);
76  allowed_hosts_.push_back(std::string(our_hostname));
77  allowed_hosts_.push_back("localhost");
78 #if !defined(__ANDROID__) && !defined(WIN32)
79  // for ipv4 loopback, we'll explicitly search for 127.* in isHostAllowed()
80  // now we need to iterate all local interfaces and add their addresses
81  // from the getifaddrs manpage: (maybe something similar for windows ?)
82  ifaddrs *ifaddr;
83  if (-1 == getifaddrs(&ifaddr))
84  {
85  ROS_ERROR("getifaddr() failed");
86  return;
87  }
88  for (ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next)
89  {
90  if(NULL == ifa->ifa_addr)
91  continue; // ifa_addr can be NULL
92  int family = ifa->ifa_addr->sa_family;
93  if (family != AF_INET && family != AF_INET6)
94  continue; // we're only looking for IP addresses
95  char addr[NI_MAXHOST] = {0};
96  if (getnameinfo(ifa->ifa_addr,
97  (family == AF_INET) ? sizeof(sockaddr_in)
98  : sizeof(sockaddr_in6),
99  addr, NI_MAXHOST,
100  NULL, 0, NI_NUMERICHOST))
101  {
102  ROS_ERROR("getnameinfo() failed");
103  continue;
104  }
105  allowed_hosts_.push_back(std::string(addr));
106  }
107  freeifaddrs(ifaddr);
108 #endif
109 }
110 
111 bool Transport::isHostAllowed(const std::string &host) const
112 {
114  return true; // doesn't matter; we'll connect to anybody
115 
116  if (host.length() >= 4 && host.substr(0, 4) == std::string("127."))
117  return true; // ipv4 localhost
118  // now, loop through the list of valid hostnames and see if we find it
119  for (std::vector<std::string>::const_iterator it = allowed_hosts_.begin();
120  it != allowed_hosts_.end(); ++it)
121  {
122  if (host == *it)
123  return true; // hooray
124  }
125  ROS_WARN("ROS_HOSTNAME / ROS_IP is set to only allow local connections, so "
126  "a requested connection to '%s' is being rejected.", host.c_str());
127  return false; // sadness
128 }
129 
130 }
131 
ros
ros::Transport::allowed_hosts_
std::vector< std::string > allowed_hosts_
Definition: transport.h:151
NI_MAXHOST
#define NI_MAXHOST
Definition: transport.cpp:50
console.h
ROS_WARN
#define ROS_WARN(...)
ros::Transport::Transport
Transport()
Definition: transport.cpp:56
ros::Transport::isHostAllowed
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:111
ROS_ERROR
#define ROS_ERROR(...)
transport.h
ros::Transport::only_localhost_allowed_
bool only_localhost_allowed_
Definition: transport.h:150


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:01:44