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> 42 inline int getHostIPs(std::map<std::string, std::string>& addressMap)
44 struct ifaddrs *ifaddr, *ifa;
46 char host[NI_MAXHOST];
48 if (getifaddrs(&ifaddr) == -1)
54 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
56 if (ifa->ifa_addr == NULL)
61 if (ifa->ifa_addr->sa_family == AF_INET)
63 s = getnameinfo(ifa->ifa_addr,
sizeof(
struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
66 std::cout <<
"getnameinfo() failed: " << gai_strerror(s) << std::endl;
70 addressMap.insert(std::pair<std::string, std::string>(ifa->ifa_name, host));
79 inline std::string getInterfaceIP(std::string interface)
81 std::map<std::string, std::string> IPs;
83 if (IPs.count(interface))
85 return IPs[interface];
89 LOG(error) <<
"Could not find provided network interface: \"" <<
interface << "\"!, exiting.";
95 inline std::string getDefaultRouteNetworkInterface()
97 std::array<char, 128> buffer;
98 std::string interfaceName;
100 #ifdef __APPLE__ // MacOS 101 std::unique_ptr<FILE, decltype(pclose) *> file(popen(
"route -n get default | grep interface | cut -d \":\" -f 2",
"r"), pclose);
103 std::unique_ptr<FILE, decltype(pclose) *> file(popen(
"ip route | grep default | cut -d \" \" -f 5 | head -n 1",
"r"), pclose);
108 LOG(error) <<
"Could not detect default route network interface name - popen() failed!";
112 while (!feof(file.get()))
114 if (fgets(buffer.data(), 128, file.get()) != NULL)
116 interfaceName += buffer.data();
120 boost::algorithm::trim(interfaceName);
122 if (interfaceName ==
"")
124 LOG(error) <<
"Could not detect default route network interface name";
128 LOG(debug) <<
"Detected network interface name for the default route: " << interfaceName;
131 return interfaceName;
134 inline std::string getIpFromHostname(
const std::string& hostname)
137 namespace bai = boost::asio::ip;
138 boost::asio::io_service ios;
139 bai::tcp::resolver resolver(ios);
140 bai::tcp::resolver::query query(hostname,
"");
141 bai::tcp::resolver::iterator end;
143 auto it = std::find_if(
static_cast<bai::basic_resolver_iterator<bai::tcp>
>(resolver.resolve(query)), end, [](
const bai::tcp::endpoint& ep) {
144 return ep.address().is_v4();
148 std::stringstream ss;
149 ss << static_cast<bai::tcp::endpoint>(*it).address();
153 LOG(warn) <<
"could not find ipv4 address for hostname '" << hostname <<
"'";
156 }
catch (std::exception& e) {
157 LOG(error) <<
"could not resolve hostname '" << hostname <<
"', reason: " << e.what();
162 inline std::string getIpFromHostname(
const std::string& hostname, boost::asio::io_service& ios)
165 namespace bai = boost::asio::ip;
166 bai::tcp::resolver resolver(ios);
167 bai::tcp::resolver::query query(hostname,
"");
168 bai::tcp::resolver::iterator end;
170 auto it = std::find_if(
static_cast<bai::basic_resolver_iterator<bai::tcp>
>(resolver.resolve(query)), end, [](
const bai::tcp::endpoint& ep) {
171 return ep.address().is_v4();
175 std::stringstream ss;
176 ss << static_cast<bai::tcp::endpoint>(*it).address();
180 LOG(warn) <<
"could not find ipv4 address for hostname '" << hostname <<
"'";
183 }
catch (std::exception& e) {
184 LOG(error) <<
"could not resolve hostname '" << hostname <<
"', reason: " << e.what();
Definition: DeviceRunner.h:23