Skip to content
API Documentation
BastionGuard™
DNSCache Class Reference

#include <dns_cache.hpp>

Classes

struct  DNSResult

Public Member Functions

bool lookup (const std::string &host, std::vector< std::string > &out)
std::string lookup_one (const std::string &host)
void store (const std::string &host, const std::vector< std::string > &addrs, int ttl_seconds=-1)
void set_default_ttl (int seconds)
void clear ()
size_t size () const

Static Public Member Functions

static DNSCache & instance ()

Private Member Functions

 DNSCache ()=default
 DNSCache (const DNSCache &)=delete
DNSCache & operator= (const DNSCache &)=delete

Private Attributes

std::mutex mu_
std::unordered_map< std::string, DNSResult > map_
int default_ttl_ = 60

Detailed Description

Definition at line 28 of file dns_cache.hpp.

Constructor & Destructor Documentation

◆ DNSCache() [1/2]

DNSCache::DNSCache ( )
privatedefault

Referenced by DNSCache(), instance(), and operator=().

◆ DNSCache() [2/2]

DNSCache::DNSCache ( const DNSCache & )
privatedelete

References DNSCache().

Member Function Documentation

◆ clear()

void DNSCache::clear ( )

Definition at line 73 of file dns_cache.cpp.

73 {
74 std::lock_guard<std::mutex> lk(mu_);
75 map_.clear();
76}
std::unordered_map< std::string, DNSResult > map_
Definition dns_cache.hpp:52
std::mutex mu_
Definition dns_cache.hpp:51

References map_, and mu_.

◆ instance()

DNSCache & DNSCache::instance ( )
static

Definition at line 26 of file dns_cache.cpp.

26 {
27 static DNSCache inst;
28 return inst;
29}
DNSCache()=default

References DNSCache().

Referenced by resolve_with_cache().

◆ lookup()

bool DNSCache::lookup ( const std::string & host,
std::vector< std::string > & out )

Definition at line 31 of file dns_cache.cpp.

31 {
32 std::lock_guard<std::mutex> lk(mu_);
33 auto it = map_.find(host);
34 if (it == map_.end()) return false;
35
36 auto now = std::chrono::steady_clock::now();
37 if (now > it->second.expires) {
38 map_.erase(it);
39 return false;
40 }
41
42 out = it->second.addrs;
43 return true;
44}

References map_, and mu_.

Referenced by lookup_one().

◆ lookup_one()

std::string DNSCache::lookup_one ( const std::string & host)

Definition at line 46 of file dns_cache.cpp.

46 {
47 std::vector<std::string> addrs;
48 if (lookup(host, addrs) && !addrs.empty())
49 return addrs.front();
50 return {};
51}
bool lookup(const std::string &host, std::vector< std::string > &out)
Definition dns_cache.cpp:31

References lookup().

◆ operator=()

DNSCache & DNSCache::operator= ( const DNSCache & )
privatedelete

References DNSCache().

◆ set_default_ttl()

void DNSCache::set_default_ttl ( int seconds)

Definition at line 68 of file dns_cache.cpp.

68 {
69 std::lock_guard<std::mutex> lk(mu_);
70 default_ttl_ = (seconds > 0) ? seconds : 60;
71}
int default_ttl_
Definition dns_cache.hpp:53

References default_ttl_, and mu_.

◆ size()

size_t DNSCache::size ( ) const

Definition at line 78 of file dns_cache.cpp.

78 {
79 std::lock_guard<std::mutex> lk(mu_);
80 return map_.size();
81}

References map_, and mu_.

◆ store()

void DNSCache::store ( const std::string & host,
const std::vector< std::string > & addrs,
int ttl_seconds = -1 )

Definition at line 53 of file dns_cache.cpp.

55 {
56 if (host.empty() || addrs.empty()) return;
57
59 r.addrs = addrs;
60 int ttl = (ttl_seconds > 0) ? ttl_seconds : default_ttl_;
61 auto now = std::chrono::steady_clock::now();
62 r.expires = now + std::chrono::seconds(ttl);
63
64 std::lock_guard<std::mutex> lk(mu_);
65 map_[host] = std::move(r);
66}

References default_ttl_, map_, and mu_.

Referenced by resolve_with_cache().

Member Data Documentation

◆ default_ttl_

int DNSCache::default_ttl_ = 60
private

Definition at line 53 of file dns_cache.hpp.

Referenced by set_default_ttl(), and store().

◆ map_

std::unordered_map<std::string, DNSResult> DNSCache::map_
private

Definition at line 52 of file dns_cache.hpp.

Referenced by clear(), lookup(), size(), and store().

◆ mu_

std::mutex DNSCache::mu_
mutableprivate

Definition at line 51 of file dns_cache.hpp.

Referenced by clear(), lookup(), set_default_ttl(), size(), and store().


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