Skip to content
API Documentation
BastionGuard™
AsyncDNSResolver Class Reference

#include <async_dns_resolver.hpp>

Classes

struct  Entry

Public Member Functions

std::future< std::string > resolve_async (const std::string &host)
std::string resolve_cached (const std::string &host)

Static Public Member Functions

static AsyncDNSResolver & instance ()

Private Member Functions

 AsyncDNSResolver ()=default

Static Private Member Functions

static uint64_t now_ms ()

Private Attributes

std::unordered_map< std::string, Entry > cache_
std::unordered_map< std::string, std::shared_ptr< std::promise< std::string > > > pending_
std::mutex mutex_

Static Private Attributes

static constexpr uint64_t TTL_MS = 5 * 60 * 1000

Detailed Description

Definition at line 31 of file async_dns_resolver.hpp.

Constructor & Destructor Documentation

◆ AsyncDNSResolver()

AsyncDNSResolver::AsyncDNSResolver ( )
privatedefault

Referenced by instance().

Member Function Documentation

◆ instance()

AsyncDNSResolver & AsyncDNSResolver::instance ( )
inlinestatic

Definition at line 33 of file async_dns_resolver.hpp.

33 {
34 static AsyncDNSResolver inst;
35 return inst;
36 }
AsyncDNSResolver()=default

References AsyncDNSResolver().

Referenced by WebProbe::check_url().

◆ now_ms()

uint64_t AsyncDNSResolver::now_ms ( )
inlinestaticprivate

Definition at line 99 of file async_dns_resolver.hpp.

99 {
100 using namespace std::chrono;
101 return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
102 }

Referenced by resolve_async(), and resolve_cached().

◆ resolve_async()

std::future< std::string > AsyncDNSResolver::resolve_async ( const std::string & host)
inline

Definition at line 38 of file async_dns_resolver.hpp.

38 {
39 {
40 std::lock_guard<std::mutex> lk(mutex_);
41 auto itc = cache_.find(host);
42 if (itc != cache_.end() && now_ms() < itc->second.expires) {
43 std::promise<std::string> p;
44 p.set_value(itc->second.ip);
45 return p.get_future();
46 }
47 auto itp = pending_.find(host);
48 if (itp != pending_.end()) {
49 return itp->second->get_future();
50 }
51 }
52
53 auto prom = std::make_shared<std::promise<std::string>>();
54 {
55 std::lock_guard<std::mutex> lk(mutex_);
56 pending_[host] = prom;
57 }
58
59 std::thread([this, host, prom]() {
60 std::string ip;
61 struct addrinfo hints{}, *res = nullptr;
62 hints.ai_family = AF_INET;
63 hints.ai_socktype = SOCK_STREAM;
64 int rc = getaddrinfo(host.c_str(), nullptr, &hints, &res);
65 if (rc == 0 && res) {
66 char buf[INET_ADDRSTRLEN] = {0};
67 if (res->ai_family == AF_INET) {
68 auto *a = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
69 inet_ntop(AF_INET, &a->sin_addr, buf, sizeof(buf));
70 ip = buf;
71 }
72 freeaddrinfo(res);
73 }
74 {
75 std::lock_guard<std::mutex> lk(mutex_);
76 cache_[host] = { ip, now_ms() + TTL_MS };
77 pending_.erase(host);
78 }
79 prom->set_value(ip);
80 }).detach();
81
82 return prom->get_future();
83 }
#define AF_INET
Definition bsc_ebpf.c:56
std::unordered_map< std::string, std::shared_ptr< std::promise< std::string > > > pending_
static uint64_t now_ms()
static constexpr uint64_t TTL_MS
std::unordered_map< std::string, Entry > cache_

References AF_INET, cache_, mutex_, now_ms(), pending_, and TTL_MS.

◆ resolve_cached()

std::string AsyncDNSResolver::resolve_cached ( const std::string & host)
inline

Definition at line 85 of file async_dns_resolver.hpp.

85 {
86 std::lock_guard<std::mutex> lk(mutex_);
87 auto it = cache_.find(host);
88 if (it != cache_.end() && now_ms() < it->second.expires) return it->second.ip;
89 return {};
90 }

References cache_, mutex_, and now_ms().

Referenced by WebProbe::check_url().

Member Data Documentation

◆ cache_

std::unordered_map<std::string, Entry> AsyncDNSResolver::cache_
private

Definition at line 95 of file async_dns_resolver.hpp.

Referenced by resolve_async(), and resolve_cached().

◆ mutex_

std::mutex AsyncDNSResolver::mutex_
private

Definition at line 97 of file async_dns_resolver.hpp.

Referenced by resolve_async(), and resolve_cached().

◆ pending_

std::unordered_map<std::string, std::shared_ptr<std::promise<std::string> > > AsyncDNSResolver::pending_
private

Definition at line 96 of file async_dns_resolver.hpp.

Referenced by resolve_async().

◆ TTL_MS

uint64_t AsyncDNSResolver::TTL_MS = 5 * 60 * 1000
staticconstexprprivate

Definition at line 94 of file async_dns_resolver.hpp.

Referenced by resolve_async().


The documentation for this class was generated from the following file: