Commit cf36cd3a authored by Raphael Defosseux's avatar Raphael Defosseux

feat(fqdn): giving some time for FQDN resolution

Signed-off-by: default avatarRaphael Defosseux <raphael.defosseux@openairinterface.org>
parent 5d3fd80b
...@@ -23,36 +23,49 @@ ...@@ -23,36 +23,49 @@
#include "logger.hpp" #include "logger.hpp"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <iostream> #include <iostream>
#include <unistd.h>
#define MAX_NB_RESOLVE_TRIES 4
#define TIME_BETWEEN_TRIES 2
bool fqdn::resolve( bool fqdn::resolve(
const std::string& host_name, std::string& address, uint32_t& port, const std::string& host_name, std::string& address, uint32_t& port,
uint8_t& addr_type, const std::string& protocol) { uint8_t& addr_type, const std::string& protocol) {
try { int tries = 0;
boost::asio::io_context io_context = {}; while (tries < MAX_NB_RESOLVE_TRIES) {
try {
boost::asio::io_context io_context = {};
Logger::smf_app().debug("Resolving DNS Try #%u", tries);
boost::asio::ip::tcp::resolver resolver{io_context}; boost::asio::ip::tcp::resolver resolver{io_context};
boost::asio::ip::tcp::resolver::results_type endpoints = boost::asio::ip::tcp::resolver::results_type endpoints =
resolver.resolve(host_name, protocol); resolver.resolve(host_name, protocol);
addr_type = 0; // IPv4 by default addr_type = 0; // IPv4 by default
for (auto it = endpoints.cbegin(); it != endpoints.cend(); it++) { for (auto it = endpoints.cbegin(); it != endpoints.cend(); it++) {
// get the first Endpoint // get the first Endpoint
boost::asio::ip::tcp::endpoint endpoint = *it; boost::asio::ip::tcp::endpoint endpoint = *it;
address = endpoint.address().to_string(); address = endpoint.address().to_string();
port = endpoint.port(); port = endpoint.port();
Logger::smf_app().debug( Logger::smf_app().debug(
"Resolve a DNS (name %s, protocol %s): Ip Addr %s, port %u", "Resolve a DNS (name %s, protocol %s): Ip Addr %s, port %u",
host_name.c_str(), protocol.c_str(), address.c_str(), port); host_name.c_str(), protocol.c_str(), address.c_str(), port);
if (endpoint.address().is_v4()) if (endpoint.address().is_v4())
addr_type = 0; addr_type = 0;
else else
addr_type = 1; addr_type = 1;
return true; return true;
}
} catch (std::exception& e) {
tries++;
if (tries == MAX_NB_RESOLVE_TRIES) {
throw std::runtime_error(
"Cannot resolve a DNS name " + std::string(e.what()) + " after " +
std::to_string(tries) + " tries");
return false;
}
sleep(TIME_BETWEEN_TRIES);
} }
} catch (std::exception& e) {
throw std::runtime_error(
"Cannot resolve a DNS name " + std::string(e.what()));
return false;
} }
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment