Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "ros/transport/transport.h"
00036 #include "ros/console.h"
00037 #include <netinet/in.h>
00038 #include <sys/socket.h>
00039 #include <netdb.h>
00040
00041 #if !defined(__ANDROID__)
00042 #include <ifaddrs.h>
00043 #endif
00044
00045 #ifndef NI_MAXHOST
00046 #define NI_MAXHOST 1025
00047 #endif
00048
00049 namespace ros
00050 {
00051
00052 Transport::Transport()
00053 : only_localhost_allowed_(false)
00054 {
00055 char *ros_ip_env = NULL, *ros_hostname_env = NULL;
00056 #ifdef _MSC_VER
00057 _dupenv_s(&ros_ip_env, NULL, "ROS_IP");
00058 _dupenv_s(&ros_hostname_env, NULL, "ROS_HOSTNAME");
00059 #else
00060 ros_ip_env = getenv("ROS_IP");
00061 ros_hostname_env = getenv("ROS_HOSTNAME");
00062 #endif
00063 if (ros_hostname_env && !strcmp(ros_hostname_env, "localhost"))
00064 only_localhost_allowed_ = true;
00065 else if (ros_ip_env && !strncmp(ros_ip_env, "127.", 4))
00066 only_localhost_allowed_ = true;
00067 else if (ros_ip_env && !strcmp(ros_ip_env, "::1"))
00068 only_localhost_allowed_ = true;
00069
00070 char our_hostname[256] = {0};
00071 gethostname(our_hostname, sizeof(our_hostname)-1);
00072 allowed_hosts_.push_back(std::string(our_hostname));
00073 allowed_hosts_.push_back("localhost");
00074 #if !defined(__ANDROID__)
00075
00076
00077
00078 ifaddrs *ifaddr;
00079 if (-1 == getifaddrs(&ifaddr))
00080 {
00081 ROS_ERROR("getifaddr() failed");
00082 return;
00083 }
00084 for (ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next)
00085 {
00086 if(NULL == ifa->ifa_addr)
00087 continue;
00088 int family = ifa->ifa_addr->sa_family;
00089 if (family != AF_INET && family != AF_INET6)
00090 continue;
00091 char addr[NI_MAXHOST] = {0};
00092 if (getnameinfo(ifa->ifa_addr,
00093 (family == AF_INET) ? sizeof(sockaddr_in)
00094 : sizeof(sockaddr_in6),
00095 addr, NI_MAXHOST,
00096 NULL, 0, NI_NUMERICHOST))
00097 {
00098 ROS_ERROR("getnameinfo() failed");
00099 continue;
00100 }
00101 allowed_hosts_.push_back(std::string(addr));
00102 }
00103 freeifaddrs(ifaddr);
00104 #endif
00105 }
00106
00107 bool Transport::isHostAllowed(const std::string &host) const
00108 {
00109 if (!only_localhost_allowed_)
00110 return true;
00111
00112 if (host.length() >= 4 && host.substr(0, 4) == std::string("127."))
00113 return true;
00114
00115 for (std::vector<std::string>::const_iterator it = allowed_hosts_.begin();
00116 it != allowed_hosts_.end(); ++it)
00117 {
00118 if (host == *it)
00119 return true;
00120 }
00121 ROS_WARN("ROS_HOSTNAME / ROS_IP is set to only allow local connections, so "
00122 "a requested connection to '%s' is being rejected.", host.c_str());
00123 return false;
00124 }
00125
00126 }
00127
roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Tue Mar 7 2017 03:44:47