Skip to content
API Documentation
BastionGuard™
dns_cache.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
21
22#include "dns_cache.hpp"
23#include <chrono>
24#include <iostream>
25
27 static DNSCache inst;
28 return inst;
29}
30
31bool DNSCache::lookup(const std::string &host, std::vector<std::string> &out) {
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}
45
46std::string DNSCache::lookup_one(const std::string &host) {
47 std::vector<std::string> addrs;
48 if (lookup(host, addrs) && !addrs.empty())
49 return addrs.front();
50 return {};
51}
52
53void DNSCache::store(const std::string &host,
54 const std::vector<std::string> &addrs,
55 int ttl_seconds) {
56 if (host.empty() || addrs.empty()) return;
57
58 DNSResult r;
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}
67
68void DNSCache::set_default_ttl(int seconds) {
69 std::lock_guard<std::mutex> lk(mu_);
70 default_ttl_ = (seconds > 0) ? seconds : 60;
71}
72
74 std::lock_guard<std::mutex> lk(mu_);
75 map_.clear();
76}
77
78size_t DNSCache::size() const {
79 std::lock_guard<std::mutex> lk(mu_);
80 return map_.size();
81}
DNSCache()=default
std::unordered_map< std::string, DNSResult > map_
Definition dns_cache.hpp:52
void set_default_ttl(int seconds)
Definition dns_cache.cpp:68
std::mutex mu_
Definition dns_cache.hpp:51
int default_ttl_
Definition dns_cache.hpp:53
static DNSCache & instance()
Definition dns_cache.cpp:26
bool lookup(const std::string &host, std::vector< std::string > &out)
Definition dns_cache.cpp:31
void clear()
Definition dns_cache.cpp:73
void store(const std::string &host, const std::vector< std::string > &addrs, int ttl_seconds=-1)
Definition dns_cache.cpp:53
size_t size() const
Definition dns_cache.cpp:78
std::string lookup_one(const std::string &host)
Definition dns_cache.cpp:46