@@ -6,6 +6,8 @@
|
|||||||
#include <network/dns_handler.hpp>
|
#include <network/dns_handler.hpp>
|
||||||
#include <network/poller.hpp>
|
#include <network/poller.hpp>
|
||||||
|
|
||||||
|
#include <utils/timed_events.hpp>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
@@ -30,7 +32,13 @@ DNSHandler::DNSHandler()
|
|||||||
int ares_error;
|
int ares_error;
|
||||||
if ((ares_error = ::ares_library_init(ARES_LIB_INIT_ALL)) != 0)
|
if ((ares_error = ::ares_library_init(ARES_LIB_INIT_ALL)) != 0)
|
||||||
throw std::runtime_error("Failed to initialize c-ares lib: "s + ares_strerror(ares_error));
|
throw std::runtime_error("Failed to initialize c-ares lib: "s + ares_strerror(ares_error));
|
||||||
if ((ares_error = ::ares_init(&this->channel)) != ARES_SUCCESS)
|
struct ares_options options = {};
|
||||||
|
// The default timeout values are way too high
|
||||||
|
options.timeout = 1000;
|
||||||
|
options.tries = 3;
|
||||||
|
if ((ares_error = ::ares_init_options(&this->channel,
|
||||||
|
&options,
|
||||||
|
ARES_OPT_TIMEOUTMS|ARES_OPT_TRIES)) != ARES_SUCCESS)
|
||||||
throw std::runtime_error("Failed to initialize c-ares channel: "s + ares_strerror(ares_error));
|
throw std::runtime_error("Failed to initialize c-ares channel: "s + ares_strerror(ares_error));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +107,8 @@ void DNSHandler::watch_dns_sockets(std::shared_ptr<Poller>& poller)
|
|||||||
{ // If not found, create it because we need to watch it
|
{ // If not found, create it because we need to watch it
|
||||||
if (it == this->socket_handlers.end())
|
if (it == this->socket_handlers.end())
|
||||||
{
|
{
|
||||||
this->socket_handlers.emplace_front(std::make_unique<DNSSocketHandler>(poller, i));
|
this->socket_handlers.emplace(this->socket_handlers.begin(),
|
||||||
|
std::make_unique<DNSSocketHandler>(poller, i));
|
||||||
it = this->socket_handlers.begin();
|
it = this->socket_handlers.begin();
|
||||||
}
|
}
|
||||||
poller->add_socket_handler(it->get());
|
poller->add_socket_handler(it->get());
|
||||||
@@ -107,6 +116,23 @@ void DNSHandler::watch_dns_sockets(std::shared_ptr<Poller>& poller)
|
|||||||
poller->watch_send_events(it->get());
|
poller->watch_send_events(it->get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Cancel previous timer, if any.
|
||||||
|
TimedEventsManager::instance().cancel("DNS timeout");
|
||||||
|
struct timeval tv;
|
||||||
|
struct timeval* tvp;
|
||||||
|
tvp = ::ares_timeout(this->channel, NULL, &tv);
|
||||||
|
if (tvp)
|
||||||
|
{
|
||||||
|
auto future_time = std::chrono::steady_clock::now() + std::chrono::seconds(tvp->tv_sec) + \
|
||||||
|
std::chrono::microseconds(tvp->tv_usec);
|
||||||
|
TimedEventsManager::instance().add_event(TimedEvent(std::move(future_time),
|
||||||
|
[this]()
|
||||||
|
{
|
||||||
|
for (auto& dns_socket_handler: this->socket_handlers)
|
||||||
|
dns_socket_handler->on_recv();
|
||||||
|
},
|
||||||
|
"DNS timeout"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* CARES_FOUND */
|
#endif /* CARES_FOUND */
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class DNSSocketHandler;
|
|||||||
# include <ares.h>
|
# include <ares.h>
|
||||||
# include <memory>
|
# include <memory>
|
||||||
# include <string>
|
# include <string>
|
||||||
# include <list>
|
# include <vector>
|
||||||
|
|
||||||
void on_hostname4_resolved(void* arg, int status, int, struct hostent* hostent);
|
void on_hostname4_resolved(void* arg, int status, int, struct hostent* hostent);
|
||||||
void on_hostname6_resolved(void* arg, int status, int, struct hostent* hostent);
|
void on_hostname6_resolved(void* arg, int status, int, struct hostent* hostent);
|
||||||
@@ -49,7 +49,7 @@ private:
|
|||||||
* call to ares_fds. DNSSocketHandlers are added to it or removed from it
|
* call to ares_fds. DNSSocketHandlers are added to it or removed from it
|
||||||
* in the watch_dns_sockets() method
|
* in the watch_dns_sockets() method
|
||||||
*/
|
*/
|
||||||
std::list<std::unique_ptr<DNSSocketHandler>> socket_handlers;
|
std::vector<std::unique_ptr<DNSSocketHandler>> socket_handlers;
|
||||||
ares_channel channel;
|
ares_channel channel;
|
||||||
|
|
||||||
DNSHandler(const DNSHandler&) = delete;
|
DNSHandler(const DNSHandler&) = delete;
|
||||||
|
|||||||
Reference in New Issue
Block a user