fix(tools): No longer use removed query API

Deprecated via 74fe2b8e14
and removed via e916bdfb1a
in Boost 1.87 or Asio 1.33.
This commit is contained in:
Dennis Klein 2025-01-07 18:50:36 +01:00
parent 41c6daaabb
commit 9297efb900
No known key found for this signature in database
GPG Key ID: 08E62D23FA0ECBBC
3 changed files with 31 additions and 33 deletions

View File

@ -1,5 +1,5 @@
/******************************************************************************** /********************************************************************************
* Copyright (C) 2017-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * Copyright (C) 2017-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* * * *
* This software is distributed under the terms of the * * This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, * * GNU Lesser General Public Licence (LGPL) version 3, *
@ -8,12 +8,12 @@
#include <fairlogger/Logger.h> #include <fairlogger/Logger.h>
#include <fairmq/tools/Network.h> #include <fairmq/tools/Network.h>
#include <fairmq/tools/Strings.h>
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
#endif #endif
#include <algorithm>
#include <array> #include <array>
#include <boost/algorithm/string.hpp> // trim #include <boost/algorithm/string.hpp> // trim
#include <boost/asio.hpp> #include <boost/asio.hpp>
@ -158,33 +158,22 @@ string getDefaultRouteNetworkInterface()
} }
string getIpFromHostname(const string& hostname) string getIpFromHostname(const string& hostname)
{
boost::asio::io_context ioc;
using namespace boost::asio::ip;
try { try {
tcp::resolver resolver(ioc); boost::asio::io_context ioc;
tcp::resolver::query query(hostname, ""); boost::asio::ip::tcp::resolver resolver(ioc);
tcp::resolver::iterator end;
auto it = find_if(static_cast<basic_resolver_iterator<tcp>>(resolver.resolve(query)), auto const result = resolver.resolve(boost::asio::ip::tcp::v4(), hostname, "");
end,
[](const tcp::endpoint& ep) { return ep.address().is_v4(); });
if (it != end) {
stringstream ss;
ss << static_cast<tcp::endpoint>(*it).address();
return ss.str();
}
if (result.empty()) {
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'"; 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 ""; return "";
} }
return ToString(result.begin()->endpoint().address());
}
catch (std::exception const& ex)
{
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << ex.what();
return "";
} }
} // namespace fair::mq::tools } // namespace fair::mq::tools

View File

@ -253,7 +253,7 @@ add_testsuite(Tools
LINKS FairMQ LINKS FairMQ
INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
TIMEOUT 20 TIMEOUT 5
${environment} ${environment}
) )

View File

@ -1,28 +1,37 @@
/******************************************************************************** /********************************************************************************
* Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * Copyright (C) 2018-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* * * *
* This software is distributed under the terms of the * * This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, * * GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
#include <gtest/gtest.h>
#include <fairmq/tools/Network.h> #include <fairmq/tools/Network.h>
#include <gtest/gtest.h>
#include <string> #include <string>
namespace namespace
{ {
using namespace std; TEST(Tools, NetworkDefaultIP)
using namespace fair::mq;
TEST(Tools, Network)
{ {
string interface = fair::mq::tools::getDefaultRouteNetworkInterface(); auto const interface = fair::mq::tools::getDefaultRouteNetworkInterface();
EXPECT_NE(interface, ""); EXPECT_NE(interface, "");
string interfaceIP = fair::mq::tools::getInterfaceIP(interface); auto const interfaceIP = fair::mq::tools::getInterfaceIP(interface);
EXPECT_NE(interfaceIP, ""); EXPECT_NE(interfaceIP, "");
} }
TEST(Tools, NetworkIPv4Localhost)
{
auto const ip = fair::mq::tools::getIpFromHostname("localhost");
EXPECT_FALSE(ip.empty());
EXPECT_EQ(ip, "127.0.0.1");
}
TEST(Tools, NetworkInvalidHostname)
{
auto const ip = fair::mq::tools::getIpFromHostname("non.existent.domain.invalid");
EXPECT_TRUE(ip.empty());
}
} /* namespace */ } /* namespace */