network.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "config.h"
29 #include "ros/network.h"
30 #include "ros/file_log.h"
31 #include "ros/exceptions.h"
32 #include "ros/io.h" // cross-platform headers needed
33 #include <ros/console.h>
34 #include <ros/assert.h>
35 #ifdef HAVE_IFADDRS_H
36  #include <ifaddrs.h>
37 #endif
38 
39 #include <boost/lexical_cast.hpp>
40 
41 namespace ros
42 {
43 
44 namespace network
45 {
46 
47 std::string g_host;
48 uint16_t g_tcpros_server_port = 0;
49 
50 const std::string& getHost()
51 {
52  return g_host;
53 }
54 
55 bool splitURI(const std::string& uri, std::string& host, uint32_t& port)
56 {
57  // skip over the protocol if it's there
58  if (uri.substr(0, 7) == std::string("http://"))
59  host = uri.substr(7);
60  else if (uri.substr(0, 9) == std::string("rosrpc://"))
61  host = uri.substr(9);
62  // split out the port
63  std::string::size_type colon_pos = host.find_first_of(":");
64  if (colon_pos == std::string::npos)
65  return false;
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);
72  return true;
73 }
74 
75 uint16_t getTCPROSPort()
76 {
77  return g_tcpros_server_port;
78 }
79 
80 static bool isPrivateIP(const char *ip)
81 {
82  bool b = !strncmp("192.168", ip, 7) || !strncmp("10.", ip, 3) ||
83  !strncmp("169.254", ip, 7);
84  return b;
85 }
86 
87 std::string determineHost()
88 {
89  std::string ip_env;
90  // First, did the user set ROS_HOSTNAME?
91  if ( get_environment_variable(ip_env, "ROS_HOSTNAME")) {
92  ROSCPP_LOG_DEBUG( "determineIP: using value of ROS_HOSTNAME:%s:", ip_env.c_str());
93  if (ip_env.size() == 0)
94  {
95  ROS_WARN("invalid ROS_HOSTNAME (an empty string)");
96  }
97  return ip_env;
98  }
99 
100  // Second, did the user set ROS_IP?
101  if ( get_environment_variable(ip_env, "ROS_IP")) {
102  ROSCPP_LOG_DEBUG( "determineIP: using value of ROS_IP:%s:", ip_env.c_str());
103  if (ip_env.size() == 0)
104  {
105  ROS_WARN("invalid ROS_IP (an empty string)");
106  }
107  return ip_env;
108  }
109 
110  // Third, try the hostname
111  char host[1024];
112  memset(host,0,sizeof(host));
113  if(gethostname(host,sizeof(host)-1) != 0)
114  {
115  ROS_ERROR("determineIP: gethostname failed");
116  }
117  // We don't want localhost to be our ip
118  else if(strlen(host) && strcmp("localhost", host))
119  {
120  return std::string(host);
121  }
122 
123  // Fourth, fall back on interface search, which will yield an IP address
124 
125 #ifdef HAVE_IFADDRS_H
126  struct ifaddrs *ifa = NULL, *ifp = NULL;
127  int rc;
128  if ((rc = getifaddrs(&ifp)) < 0)
129  {
130  ROS_FATAL("error in getifaddrs: [%s]", strerror(rc));
131  ROS_BREAK();
132  }
133  char preferred_ip[200] = {0};
134  for (ifa = ifp; ifa; ifa = ifa->ifa_next)
135  {
136  char ip_[200];
137  socklen_t salen;
138  if (!ifa->ifa_addr)
139  continue; // evidently this interface has no ip address
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);
144  else
145  continue;
146  if (getnameinfo(ifa->ifa_addr, salen, ip_, sizeof(ip_), NULL, 0,
147  NI_NUMERICHOST) < 0)
148  {
149  ROSCPP_LOG_DEBUG( "getnameinfo couldn't get the ip of interface [%s]", ifa->ifa_name);
150  continue;
151  }
152  //ROS_INFO( "ip of interface [%s] is [%s]", ifa->ifa_name, ip);
153  // prefer non-private IPs over private IPs
154  if (!strcmp("127.0.0.1", ip_) || strchr(ip_,':'))
155  continue; // ignore loopback unless we have no other choice
156  if (ifa->ifa_addr->sa_family == AF_INET6 && !preferred_ip[0])
157  strcpy(preferred_ip, ip_);
158  else if (isPrivateIP(ip_) && !preferred_ip[0])
159  strcpy(preferred_ip, ip_);
160  else if (!isPrivateIP(ip_) &&
161  (isPrivateIP(preferred_ip) || !preferred_ip[0]))
162  strcpy(preferred_ip, ip_);
163  }
164  freeifaddrs(ifp);
165  if (!preferred_ip[0])
166  {
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");
172  }
173  ROSCPP_LOG_DEBUG( "preferred IP is guessed to be %s", preferred_ip);
174  return std::string(preferred_ip);
175 #else
176  // @todo Fix IP determination in the case where getifaddrs() isn't
177  // available.
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");
183 #endif
184 }
185 
186 void init(const M_string& remappings)
187 {
188  M_string::const_iterator it = remappings.find("__hostname");
189  if (it != remappings.end())
190  {
191  g_host = it->second;
192  }
193  else
194  {
195  it = remappings.find("__ip");
196  if (it != remappings.end())
197  {
198  g_host = it->second;
199  }
200  }
201 
202  it = remappings.find("__tcpros_server_port");
203  if (it != remappings.end())
204  {
205  try
206  {
207  g_tcpros_server_port = boost::lexical_cast<uint16_t>(it->second);
208  }
209  catch (boost::bad_lexical_cast&)
210  {
211  throw ros::InvalidPortException("__tcpros_server_port [" + it->second + "] was not specified as a number within the 0-65535 range");
212  }
213  }
214 
215  if (g_host.empty())
216  {
217  g_host = determineHost();
218  }
219 }
220 
221 } // namespace network
222 
223 } // namespace ros
#define ROS_FATAL(...)
Thrown when an invalid port is specified.
Definition: exceptions.h:85
uint16_t g_tcpros_server_port
Definition: network.cpp:48
ROSCPP_DECL bool splitURI(const std::string &uri, std::string &host, uint32_t &port)
Definition: network.cpp:55
std::string g_host
Definition: network.cpp:47
#define ROS_WARN(...)
std::map< std::string, std::string > M_string
#define ROSCPP_LOG_DEBUG(...)
Definition: file_log.h:35
ROSCPP_DECL const std::string & getHost()
Definition: network.cpp:50
bool get_environment_variable(std::string &str, const char *environment_variable)
#define ROS_BREAK()
std::string determineHost()
Definition: network.cpp:87
#define ROS_ERROR(...)
ROSCPP_DECL uint16_t getTCPROSPort()
Definition: network.cpp:75
static bool isPrivateIP(const char *ip)
Definition: network.cpp:80
void init(const M_string &remappings)
Definition: network.cpp:186


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54