From c80f97b338365c379b8df6ef75ec0baa132fe856 Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Tue, 7 Jan 2025 18:50:36 +0100 Subject: [PATCH] fix(tools): No longer use removed `query` API Deprecated via https://github.com/chriskohlhoff/asio/commit/74fe2b8e1474f3bdac9f509c15aa095da0562e2d and removed via https://github.com/chriskohlhoff/asio/commit/e916bdfb1ab64d68ba556e0040a3e86a747b74a7 in Boost 1.87 or Asio 1.33. --- fairmq/tools/Network.cxx | 35 ++++++++++++----------------------- test/CMakeLists.txt | 2 +- test/tools/_network.cxx | 27 ++++++++++++++++++--------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/fairmq/tools/Network.cxx b/fairmq/tools/Network.cxx index a5e9e4bb..7922f2c9 100644 --- a/fairmq/tools/Network.cxx +++ b/fairmq/tools/Network.cxx @@ -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 * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -8,12 +8,12 @@ #include #include +#include #ifndef _GNU_SOURCE #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #endif -#include #include #include // trim #include @@ -158,33 +158,22 @@ string getDefaultRouteNetworkInterface() } string getIpFromHostname(const string& hostname) -{ +try { boost::asio::io_context ioc; + boost::asio::ip::tcp::resolver resolver(ioc); - using namespace boost::asio::ip; - - try { - tcp::resolver resolver(ioc); - tcp::resolver::query query(hostname, ""); - tcp::resolver::iterator end; - - auto it = find_if(static_cast>(resolver.resolve(query)), - end, - [](const tcp::endpoint& ep) { return ep.address().is_v4(); }); - - if (it != end) { - stringstream ss; - ss << static_cast(*it).address(); - return ss.str(); - } + auto const result = resolver.resolve(boost::asio::ip::tcp::v4(), hostname, ""); + if (result.empty()) { 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 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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a306dcae..a3dd6547 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -253,7 +253,7 @@ add_testsuite(Tools LINKS FairMQ INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} - TIMEOUT 20 + TIMEOUT 5 ${environment} ) diff --git a/test/tools/_network.cxx b/test/tools/_network.cxx index 838c40de..e63d73c7 100644 --- a/test/tools/_network.cxx +++ b/test/tools/_network.cxx @@ -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 * * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#include #include - +#include #include namespace { -using namespace std; -using namespace fair::mq; - -TEST(Tools, Network) +TEST(Tools, NetworkDefaultIP) { - string interface = fair::mq::tools::getDefaultRouteNetworkInterface(); + auto const interface = fair::mq::tools::getDefaultRouteNetworkInterface(); EXPECT_NE(interface, ""); - string interfaceIP = fair::mq::tools::getInterfaceIP(interface); + auto const interfaceIP = fair::mq::tools::getInterfaceIP(interface); 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 */