Add test for interface IP detection tools

This commit is contained in:
Alexey Rybalchenko 2018-11-01 11:23:00 +01:00 committed by Dennis Klein
parent 5e4876c947
commit 0b199e779a
4 changed files with 70 additions and 60 deletions

View File

@ -28,6 +28,7 @@
#include <iostream> #include <iostream>
#include <array> #include <array>
#include <exception> #include <exception>
#include <stdexcept>
#include <algorithm> #include <algorithm>
using namespace std; using namespace std;
@ -40,54 +41,52 @@ namespace tools
{ {
// returns a map with network interface names as keys and their IP addresses as values // returns a map with network interface names as keys and their IP addresses as values
int getHostIPs(map<string, string>& addressMap) map<string, string> getHostIPs()
{ {
map<string, string> addressMap;
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
int s; int s;
char host[NI_MAXHOST]; char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) if (getifaddrs(&ifaddr) == -1) {
{
perror("getifaddrs"); perror("getifaddrs");
return -1; throw runtime_error("getifaddrs failed");
} }
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
{ if (ifa->ifa_addr == nullptr) {
if (ifa->ifa_addr == NULL)
{
continue; continue;
} }
if (ifa->ifa_addr->sa_family == AF_INET) if (ifa->ifa_addr->sa_family == AF_INET) {
{ s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (s != 0) {
if (s != 0)
{
cout << "getnameinfo() failed: " << gai_strerror(s) << endl; cout << "getnameinfo() failed: " << gai_strerror(s) << endl;
return -1; throw runtime_error("getnameinfo() failed");
} }
addressMap.insert(pair<string, string>(ifa->ifa_name, host)); addressMap.insert(pair<string, string>(ifa->ifa_name, host));
} }
} }
freeifaddrs(ifaddr); freeifaddrs(ifaddr);
return 0; return addressMap;
} }
// get IP address of a given interface name // get IP address of a given interface name
string getInterfaceIP(const string& interface) string getInterfaceIP(const string& interface)
{ {
map<string, string> IPs; try {
getHostIPs(IPs); auto IPs = getHostIPs();
if (IPs.count(interface)) if (IPs.count(interface)) {
{ return IPs[interface];
return IPs[interface]; } else {
} LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
else return "";
{ }
LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting."; } catch (runtime_error& re) {
cout << "could not get interface IP: " << re.what();
return ""; return "";
} }
} }
@ -104,28 +103,22 @@ string getDefaultRouteNetworkInterface()
unique_ptr<FILE, decltype(pclose) *> file(popen("ip route | grep default | cut -d \" \" -f 5 | head -n 1", "r"), pclose); unique_ptr<FILE, decltype(pclose) *> file(popen("ip route | grep default | cut -d \" \" -f 5 | head -n 1", "r"), pclose);
#endif #endif
if (!file) if (!file) {
{
LOG(error) << "Could not detect default route network interface name - popen() failed!"; LOG(error) << "Could not detect default route network interface name - popen() failed!";
return ""; return "";
} }
while (!feof(file.get())) while (!feof(file.get())) {
{ if (fgets(buffer.data(), 128, file.get()) != nullptr) {
if (fgets(buffer.data(), 128, file.get()) != NULL)
{
interfaceName += buffer.data(); interfaceName += buffer.data();
} }
} }
boost::algorithm::trim(interfaceName); boost::algorithm::trim(interfaceName);
if (interfaceName == "") if (interfaceName == "") {
{
LOG(error) << "Could not detect default route network interface name"; LOG(error) << "Could not detect default route network interface name";
} } else {
else
{
LOG(debug) << "Detected network interface name for the default route: " << interfaceName; LOG(debug) << "Detected network interface name for the default route: " << interfaceName;
} }
@ -134,30 +127,8 @@ string getDefaultRouteNetworkInterface()
string getIpFromHostname(const string& hostname) string getIpFromHostname(const string& hostname)
{ {
try { boost::asio::io_service ios;
namespace bai = boost::asio::ip; return getIpFromHostname(hostname, ios);
boost::asio::io_service ios;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::query query(hostname, "");
bai::tcp::resolver::iterator end;
auto it = find_if(static_cast<bai::basic_resolver_iterator<bai::tcp>>(resolver.resolve(query)), end, [](const bai::tcp::endpoint& ep) {
return ep.address().is_v4();
});
if (it != end) {
stringstream ss;
ss << static_cast<bai::tcp::endpoint>(*it).address();
return ss.str();
}
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
} catch (exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
}
} }
string getIpFromHostname(const string& hostname, boost::asio::io_service& ios) string getIpFromHostname(const string& hostname, boost::asio::io_service& ios)

View File

@ -32,7 +32,7 @@ namespace tools
{ {
// returns a map with network interface names as keys and their IP addresses as values // returns a map with network interface names as keys and their IP addresses as values
int getHostIPs(std::map<std::string, std::string>& addressMap); std::map<std::string, std::string> getHostIPs();
// get IP address of a given interface name // get IP address of a given interface name
std::string getInterfaceIP(const std::string& interface); std::string getInterfaceIP(const std::string& interface);

View File

@ -198,6 +198,17 @@ add_testsuite(FairMQ.StateMachine
TIMEOUT 10 TIMEOUT 10
) )
add_testsuite(FairMQ.Tools
SOURCES
${CMAKE_CURRENT_BINARY_DIR}/runner.cxx
tools/_network.cxx
LINKS FairMQ
INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
TIMEOUT 10
)
add_testsuite(FairMQ.Transport add_testsuite(FairMQ.Transport
SOURCES SOURCES
${CMAKE_CURRENT_BINARY_DIR}/runner.cxx ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx

28
test/tools/_network.cxx Normal file
View File

@ -0,0 +1,28 @@
/********************************************************************************
* Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <gtest/gtest.h>
#include <fairmq/Tools.h>
#include <string>
namespace
{
using namespace std;
using namespace fair::mq;
TEST(Tools, Network)
{
string interface = fair::mq::tools::getDefaultRouteNetworkInterface();
EXPECT_NE(interface, "");
string interfaceIP = fair::mq::tools::getInterfaceIP(interface);
EXPECT_NE(interfaceIP, "");
}
} /* namespace */