9 #ifndef FAIR_MQ_TOOLS_NETWORK_H 10 #define FAIR_MQ_TOOLS_NETWORK_H 13 #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST 16 #include "FairMQLogger.h" 18 #include <sys/socket.h> 19 #include <sys/types.h> 24 #include <boost/algorithm/string.hpp> 25 #include <boost/asio.hpp> 41 inline int getHostIPs(std::map<std::string, std::string>& addressMap)
43 struct ifaddrs *ifaddr, *ifa;
45 char host[NI_MAXHOST];
47 if (getifaddrs(&ifaddr) == -1)
53 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
55 if (ifa->ifa_addr == NULL)
60 if (ifa->ifa_addr->sa_family == AF_INET)
62 s = getnameinfo(ifa->ifa_addr,
sizeof(
struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
65 std::cout <<
"getnameinfo() failed: " << gai_strerror(s) << std::endl;
69 addressMap.insert(std::pair<std::string, std::string>(ifa->ifa_name, host));
78 inline std::string getInterfaceIP(std::string interface)
80 std::map<std::string, std::string> IPs;
82 if (IPs.count(interface))
84 return IPs[interface];
88 LOG(error) <<
"Could not find provided network interface: \"" <<
interface << "\"!, exiting.";
94 inline std::string getDefaultRouteNetworkInterface()
96 std::array<char, 128> buffer;
97 std::string interfaceName;
99 #ifdef __APPLE__ // MacOS 100 std::unique_ptr<FILE, decltype(pclose) *> file(popen(
"route -n get default | grep interface | cut -d \":\" -f 2",
"r"), pclose);
102 std::unique_ptr<FILE, decltype(pclose) *> file(popen(
"ip route | grep default | cut -d \" \" -f 5 | head -n 1",
"r"), pclose);
107 LOG(error) <<
"Could not detect default route network interface name - popen() failed!";
111 while (!feof(file.get()))
113 if (fgets(buffer.data(), 128, file.get()) != NULL)
115 interfaceName += buffer.data();
119 boost::algorithm::trim(interfaceName);
121 if (interfaceName ==
"")
123 LOG(error) <<
"Could not detect default route network interface name";
127 LOG(debug) <<
"Detected network interface name for the default route: " << interfaceName;
130 return interfaceName;
133 inline std::string getIpFromHostname(
const std::string& hostname)
136 boost::asio::io_service ios;
137 boost::asio::ip::tcp::resolver resolver(ios);
138 boost::asio::ip::tcp::resolver::query query(hostname,
"");
139 boost::asio::ip::tcp::resolver::iterator end;
141 auto it = std::find_if(resolver.resolve(query), end, [](
const boost::asio::ip::tcp::endpoint& ep) {
142 return ep.address().is_v4();
146 std::stringstream ss;
147 ss << static_cast<boost::asio::ip::tcp::endpoint>(*it).address();
151 LOG(warn) <<
"could not find ipv4 address for hostname '" << hostname <<
"'";
154 }
catch (std::exception& e) {
155 LOG(error) <<
"could not resolve hostname '" << hostname <<
"', reason: " << e.what();
160 inline std::string getIpFromHostname(
const std::string& hostname, boost::asio::io_service& ios)
163 boost::asio::ip::tcp::resolver resolver(ios);
164 boost::asio::ip::tcp::resolver::query query(hostname,
"");
165 boost::asio::ip::tcp::resolver::iterator end;
167 auto it = std::find_if(resolver.resolve(query), end, [](
const boost::asio::ip::tcp::endpoint& ep) {
168 return ep.address().is_v4();
172 std::stringstream ss;
173 ss << static_cast<boost::asio::ip::tcp::endpoint>(*it).address();
177 LOG(warn) <<
"could not find ipv4 address for hostname '" << hostname <<
"'";
180 }
catch (std::exception& e) {
181 LOG(error) <<
"could not resolve hostname '" << hostname <<
"', reason: " << e.what();
Definition: DeviceRunner.h:23