Skip to content
API Documentation
BastionGuard™
dns_cache.hpp
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#pragma once
22#include <string>
23#include <vector>
24#include <unordered_map>
25#include <mutex>
26#include <chrono>
27
28class DNSCache {
29public:
30 static DNSCache &instance();
31
32 bool lookup(const std::string &host, std::vector<std::string> &out);
33 std::string lookup_one(const std::string &host);
34
35 void store(const std::string &host, const std::vector<std::string> &addrs, int ttl_seconds = -1);
36 void set_default_ttl(int seconds);
37 void clear();
38
39 size_t size() const;
40
41private:
42 struct DNSResult {
43 std::vector<std::string> addrs;
44 std::chrono::steady_clock::time_point expires;
45 };
46
47 DNSCache() = default;
48 DNSCache(const DNSCache&) = delete;
49 DNSCache& operator=(const DNSCache&) = delete;
50
51 mutable std::mutex mu_;
52 std::unordered_map<std::string, DNSResult> map_;
53 int default_ttl_ = 60;
54};
DNSCache & operator=(const DNSCache &)=delete
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
DNSCache(const DNSCache &)=delete
size_t size() const
Definition dns_cache.cpp:78
std::string lookup_one(const std::string &host)
Definition dns_cache.cpp:46
std::chrono::steady_clock::time_point expires
Definition dns_cache.hpp:44
std::vector< std::string > addrs
Definition dns_cache.hpp:43