39 #include <boost/lexical_cast.hpp> 55 bool splitURI(
const std::string& uri, std::string& host, uint32_t& port)
58 if (uri.substr(0, 7) == std::string(
"http://"))
60 else if (uri.substr(0, 9) == std::string(
"rosrpc://"))
63 std::string::size_type colon_pos = host.find_first_of(
":");
64 if (colon_pos == std::string::npos)
66 std::string port_str = host.substr(colon_pos+1);
67 std::string::size_type slash_pos = port_str.find_first_of(
"/");
68 if (slash_pos != std::string::npos)
69 port_str = port_str.erase(slash_pos);
70 port = atoi(port_str.c_str());
71 host = host.erase(colon_pos);
82 bool b = !strncmp(
"192.168", ip, 7) || !strncmp(
"10.", ip, 3) ||
83 !strncmp(
"169.254", ip, 7);
92 ROSCPP_LOG_DEBUG(
"determineIP: using value of ROS_HOSTNAME:%s:", ip_env.c_str());
93 if (ip_env.size() == 0)
95 ROS_WARN(
"invalid ROS_HOSTNAME (an empty string)");
102 ROSCPP_LOG_DEBUG(
"determineIP: using value of ROS_IP:%s:", ip_env.c_str());
103 if (ip_env.size() == 0)
105 ROS_WARN(
"invalid ROS_IP (an empty string)");
112 memset(host,0,
sizeof(host));
113 if(gethostname(host,
sizeof(host)-1) != 0)
115 ROS_ERROR(
"determineIP: gethostname failed");
118 else if(strlen(host) && strcmp(
"localhost", host))
120 return std::string(host);
125 #ifdef HAVE_IFADDRS_H 126 struct ifaddrs *ifa = NULL, *ifp = NULL;
128 if ((rc = getifaddrs(&ifp)) < 0)
130 ROS_FATAL(
"error in getifaddrs: [%s]", strerror(rc));
133 char preferred_ip[200] = {0};
134 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
140 if (ifa->ifa_addr->sa_family == AF_INET)
141 salen =
sizeof(
struct sockaddr_in);
142 else if (ifa->ifa_addr->sa_family == AF_INET6)
143 salen =
sizeof(
struct sockaddr_in6);
146 if (getnameinfo(ifa->ifa_addr, salen, ip_,
sizeof(ip_), NULL, 0,
149 ROSCPP_LOG_DEBUG(
"getnameinfo couldn't get the ip of interface [%s]", ifa->ifa_name);
154 if (!strcmp(
"127.0.0.1", ip_) || strchr(ip_,
':'))
156 if (ifa->ifa_addr->sa_family == AF_INET6 && !preferred_ip[0])
157 strcpy(preferred_ip, ip_);
159 strcpy(preferred_ip, ip_);
162 strcpy(preferred_ip, ip_);
165 if (!preferred_ip[0])
167 ROS_ERROR(
"Couldn't find a preferred IP via the getifaddrs() call; I'm assuming that your IP " 168 "address is 127.0.0.1. This should work for local processes, " 169 "but will almost certainly not work if you have remote processes." 170 "Report to the ROS development team to seek a fix.");
171 return std::string(
"127.0.0.1");
174 return std::string(preferred_ip);
178 ROS_ERROR(
"You don't have the getifaddrs() call; I'm assuming that your IP " 179 "address is 127.0.0.1. This should work for local processes, " 180 "but will almost certainly not work if you have remote processes." 181 "Report to the ROS development team to seek a fix.");
182 return std::string(
"127.0.0.1");
188 M_string::const_iterator it = remappings.find(
"__hostname");
189 if (it != remappings.end())
195 it = remappings.find(
"__ip");
196 if (it != remappings.end())
202 it = remappings.find(
"__tcpros_server_port");
203 if (it != remappings.end())
207 g_tcpros_server_port = boost::lexical_cast<uint16_t>(it->second);
209 catch (boost::bad_lexical_cast&)
211 throw ros::InvalidPortException(
"__tcpros_server_port [" + it->second +
"] was not specified as a number within the 0-65535 range");
Thrown when an invalid port is specified.
uint16_t g_tcpros_server_port
ROSCPP_DECL bool splitURI(const std::string &uri, std::string &host, uint32_t &port)
std::map< std::string, std::string > M_string
#define ROSCPP_LOG_DEBUG(...)
ROSCPP_DECL const std::string & getHost()
bool get_environment_variable(std::string &str, const char *environment_variable)
std::string determineHost()
ROSCPP_DECL uint16_t getTCPROSPort()
static bool isPrivateIP(const char *ip)
void init(const M_string &remappings)