37 #include <sys/socket.h>
40 #include <boost/lexical_cast.hpp>
56 bool splitURI(
const std::string& uri, std::string& host, uint32_t& port)
59 if (uri.substr(0, 7) == std::string(
"http://"))
61 else if (uri.substr(0, 9) == std::string(
"rosrpc://"))
64 std::string::size_type colon_pos = host.find_first_of(
":");
65 if (colon_pos == std::string::npos)
67 std::string port_str = host.substr(colon_pos+1);
68 std::string::size_type slash_pos = port_str.find_first_of(
"/");
69 if (slash_pos != std::string::npos)
70 port_str = port_str.erase(slash_pos);
71 port = atoi(port_str.c_str());
72 host = host.erase(colon_pos);
83 bool b = !strncmp(
"192.168", ip, 7) || !strncmp(
"10.", ip, 3) ||
84 !strncmp(
"169.254", ip, 7);
93 ROSCPP_LOG_DEBUG(
"determineIP: using value of ROS_HOSTNAME:%s:", ip_env.c_str());
94 if (ip_env.size() == 0)
96 ROS_WARN(
"invalid ROS_HOSTNAME (an empty string)");
103 ROSCPP_LOG_DEBUG(
"determineIP: using value of ROS_IP:%s:", ip_env.c_str());
104 if (ip_env.size() == 0)
106 ROS_WARN(
"invalid ROS_IP (an empty string)");
113 memset(host,0,
sizeof(host));
114 if(gethostname(host,
sizeof(host)-1) != 0)
116 ROS_ERROR(
"determineIP: gethostname failed");
119 else if(strlen(host) && strcmp(
"localhost", host))
121 return std::string(host);
126 #ifdef HAVE_IFADDRS_H
127 struct ifaddrs *ifa = NULL, *ifp = NULL;
129 if ((rc = getifaddrs(&ifp)) < 0)
131 ROS_FATAL(
"error in getifaddrs: [%s]", strerror(rc));
134 char preferred_ip[200] = {0};
135 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
141 if (ifa->ifa_addr->sa_family == AF_INET)
142 salen =
sizeof(
struct sockaddr_in);
143 else if (ifa->ifa_addr->sa_family == AF_INET6)
144 salen =
sizeof(
struct sockaddr_in6);
147 if (getnameinfo(ifa->ifa_addr, salen, ip_,
sizeof(ip_), NULL, 0,
150 ROSCPP_LOG_DEBUG(
"getnameinfo couldn't get the ip of interface [%s]", ifa->ifa_name);
155 if (!strcmp(
"127.0.0.1", ip_) || strchr(ip_,
':'))
157 if (ifa->ifa_addr->sa_family == AF_INET6 && !preferred_ip[0])
158 strcpy(preferred_ip, ip_);
160 strcpy(preferred_ip, ip_);
163 strcpy(preferred_ip, ip_);
166 if (!preferred_ip[0])
168 ROS_ERROR(
"Couldn't find a preferred IP via the getifaddrs() call; I'm assuming that your IP "
169 "address is 127.0.0.1. This should work for local processes, "
170 "but will almost certainly not work if you have remote processes."
171 "Report to the ROS development team to seek a fix.");
172 return std::string(
"127.0.0.1");
175 return std::string(preferred_ip);
179 ROS_ERROR(
"You don't have the getifaddrs() call; I'm assuming that your IP "
180 "address is 127.0.0.1. This should work for local processes, "
181 "but will almost certainly not work if you have remote processes."
182 "Report to the ROS development team to seek a fix.");
183 return std::string(
"127.0.0.1");
189 M_string::const_iterator it = remappings.find(
"__hostname");
190 if (it != remappings.end())
196 it = remappings.find(
"__ip");
197 if (it != remappings.end())
203 it = remappings.find(
"__tcpros_server_port");
204 if (it != remappings.end())
210 catch (boost::bad_lexical_cast&)
212 throw ros::InvalidPortException(
"__tcpros_server_port [" + it->second +
"] was not specified as a number within the 0-65535 range");