]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/nod.hh
updated KSK and ZSK Rollover procedures, small fixes in Algorithm Rollover procedure
[thirdparty/pdns.git] / pdns / nod.hh
CommitLineData
af1377b7
NC
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#pragma once
23#include <atomic>
24#include <mutex>
343ae951 25#include <thread>
852d4e70 26#include <boost/filesystem.hpp>
af1377b7 27#include "dnsname.hh"
e837d8d8 28#include "lock.hh"
af1377b7
NC
29#include "stable-bloom.hh"
30
31namespace nod {
b78727c6
NC
32 const float c_fp_rate = 0.01;
33 const size_t c_num_cells = 67108864;
34 const uint8_t c_num_dec = 10;
af1377b7
NC
35 const unsigned int snapshot_interval_default = 600;
36 const std::string bf_suffix = "bf";
343ae951 37 const std::string sbf_prefix = "sbf";
af1377b7 38
343ae951 39 // Theses classes are not designed to be shared between threads
af1377b7
NC
40 // Use a new instance per-thread, e.g. using thread local storage
41 // Synchronization (at the class level) is still needed for reading from
42 // and writing to the cache dir
43 // Synchronization (at the instance level) is needed when snapshotting
343ae951
NC
44 class PersistentSBF {
45 public:
e837d8d8
RG
46 PersistentSBF() : d_sbf(bf::stableBF(c_fp_rate, c_num_cells, c_num_dec)) {}
47 PersistentSBF(uint32_t num_cells) : d_sbf(bf::stableBF(c_fp_rate, num_cells, c_num_dec)) {}
343ae951
NC
48 bool init(bool ignore_pid=false);
49 void setPrefix(const std::string& prefix) { d_prefix = prefix; } // Added to filenames in cachedir
50 void setCacheDir(const std::string& cachedir);
51 bool snapshotCurrent(std::thread::id tid); // Write the current file out to disk
52 void add(const std::string& data) {
53 // The only time this should block is when snapshotting
e837d8d8 54 d_sbf.lock()->add(data);
343ae951 55 }
e837d8d8 56 bool test(const std::string& data) { return d_sbf.lock()->test(data); }
343ae951
NC
57 bool testAndAdd(const std::string& data) {
58 // The only time this should block is when snapshotting
e837d8d8 59 return d_sbf.lock()->testAndAdd(data);
343ae951
NC
60 }
61 private:
852d4e70 62 void remove_tmp_files(const boost::filesystem::path&, std::lock_guard<std::mutex>&);
e672ad6d 63
343ae951 64 bool d_init{false};
e837d8d8 65 LockGuarded<bf::stableBF> d_sbf; // Stable Bloom Filter
343ae951
NC
66 std::string d_cachedir;
67 std::string d_prefix = sbf_prefix;
343ae951
NC
68 static std::mutex d_cachedir_mutex; // One mutex for all instances of this class
69 };
70
af1377b7
NC
71 class NODDB {
72 public:
b78727c6
NC
73 NODDB() : d_psbf{} {}
74 NODDB(uint32_t num_cells) : d_psbf{num_cells} {}
af1377b7
NC
75 // Set ignore_pid to true if you don't mind loading files
76 // created by the current process
343ae951
NC
77 bool init(bool ignore_pid=false) {
78 d_psbf.setPrefix("nod");
79 return d_psbf.init(ignore_pid);
80 }
af1377b7
NC
81 bool isNewDomain(const std::string& domain); // Returns true if newly observed domain
82 bool isNewDomain(const DNSName& dname); // As above
83 bool isNewDomainWithParent(const std::string& domain, std::string& observed); // Returns true if newly observed domain, in which case "observed" contains the parent domain which *was* observed (or "" if domain is . or no parent domains observed)
84 bool isNewDomainWithParent(const DNSName& dname, std::string& observed); // As above
85 void addDomain(const DNSName& dname); // You need to add this to refresh frequently used domains
86 void addDomain(const std::string& domain); // As above
87 void setSnapshotInterval(unsigned int secs) { d_snapshot_interval = secs; }
343ae951
NC
88 void setCacheDir(const std::string& cachedir) { d_psbf.setCacheDir(cachedir); }
89 bool snapshotCurrent(std::thread::id tid) { return d_psbf.snapshotCurrent(tid); }
90 static void startHousekeepingThread(std::shared_ptr<NODDB> noddbp, std::thread::id tid) {
91 noddbp->housekeepingThread(tid);
af1377b7
NC
92 }
93 private:
343ae951
NC
94 PersistentSBF d_psbf;
95 unsigned int d_snapshot_interval{snapshot_interval_default}; // Number seconds between snapshots
96 void housekeepingThread(std::thread::id tid);
97 };
98
99 class UniqueResponseDB {
100 public:
b78727c6
NC
101 UniqueResponseDB() : d_psbf{} {}
102 UniqueResponseDB(uint32_t num_cells) : d_psbf{num_cells} {}
343ae951
NC
103 bool init(bool ignore_pid=false) {
104 d_psbf.setPrefix("udr");
105 return d_psbf.init(ignore_pid);
106 }
107 bool isUniqueResponse(const std::string& response);
108 void addResponse(const std::string& response);
109 void setSnapshotInterval(unsigned int secs) { d_snapshot_interval = secs; }
110 void setCacheDir(const std::string& cachedir) { d_psbf.setCacheDir(cachedir); }
111 bool snapshotCurrent(std::thread::id tid) { return d_psbf.snapshotCurrent(tid); }
112 static void startHousekeepingThread(std::shared_ptr<UniqueResponseDB> udrdbp, std::thread::id tid) {
113 udrdbp->housekeepingThread(tid);
114 }
115 private:
116 PersistentSBF d_psbf;
117 unsigned int d_snapshot_interval{snapshot_interval_default}; // Number seconds between snapshots
118 void housekeepingThread(std::thread::id tid);
af1377b7
NC
119 };
120
121}