Skip to content

Commit

Permalink
fix(tools): No longer use deprecated query API
Browse files Browse the repository at this point in the history
Deprecated via chriskohlhoff/asio@74fe2b8
and removed via chriskohlhoff/asio@13f0683
in Boost 1.87 or Asio 1.33.
  • Loading branch information
dennisklein committed Jan 7, 2025
1 parent 41c6daa commit 9437c91
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions fairmq/tools/Network.cxx
Original file line number Diff line number Diff line change
@@ -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, *
Expand All @@ -8,12 +8,12 @@

#include <fairlogger/Logger.h>
#include <fairmq/tools/Network.h>
#include <fairmq/tools/Strings.h>

#ifndef _GNU_SOURCE
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
#endif

#include <algorithm>
#include <array>
#include <boost/algorithm/string.hpp> // trim
#include <boost/asio.hpp>
Expand Down Expand Up @@ -161,26 +161,16 @@ string getIpFromHostname(const string& hostname)
{
boost::asio::io_context 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<basic_resolver_iterator<tcp>>(resolver.resolve(query)),
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();
}
boost::asio::ip::tcp::resolver resolver(ioc);
auto result = resolver.resolve(boost::asio::ip::tcp::v4(), hostname, "");

LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
if (result.empty()) {
LOG(warn) << "could not find ipv4 address for hostname '" << hostname << "'";
return "";
}

return "";
return ToString(result.begin()->endpoint().address());
} catch (exception& e) {
LOG(error) << "could not resolve hostname '" << hostname << "', reason: " << e.what();
return "";
Expand Down

0 comments on commit 9437c91

Please sign in to comment.