40 #include <netinet/in.h> 41 #include <sys/socket.h> 45 #if !defined(__ANDROID__) && !defined(WIN32) 50 #define NI_MAXHOST 1025 57 : only_localhost_allowed_(false)
59 char *ros_ip_env = NULL, *ros_hostname_env = NULL;
61 _dupenv_s(&ros_ip_env, NULL,
"ROS_IP");
62 _dupenv_s(&ros_hostname_env, NULL,
"ROS_HOSTNAME");
64 ros_ip_env = getenv(
"ROS_IP");
65 ros_hostname_env = getenv(
"ROS_HOSTNAME");
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"))
74 char our_hostname[256] = {0};
75 gethostname(our_hostname,
sizeof(our_hostname)-1);
78 #if !defined(__ANDROID__) && !defined(WIN32) 83 if (-1 == getifaddrs(&ifaddr))
88 for (ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next)
90 if(NULL == ifa->ifa_addr)
92 int family = ifa->ifa_addr->sa_family;
93 if (family != AF_INET && family != AF_INET6)
96 if (getnameinfo(ifa->ifa_addr,
97 (family == AF_INET) ?
sizeof(sockaddr_in)
98 :
sizeof(sockaddr_in6),
100 NULL, 0, NI_NUMERICHOST))
116 if (host.length() >= 4 && host.substr(0, 4) == std::string(
"127."))
119 for (std::vector<std::string>::const_iterator it =
allowed_hosts_.begin();
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());
bool only_localhost_allowed_
std::vector< std::string > allowed_hosts_
bool isHostAllowed(const std::string &host) const
returns true if the transport is allowed to connect to the host passed to it.