• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 18743945403

23 Oct 2025 09:29AM UTC coverage: 65.845% (+0.02%) from 65.829%
18743945403

Pull #16356

github

web-flow
Merge 8a2027ef1 into efa3637e8
Pull Request #16356: auth 5.0: backport "pdnsutil: fix b2b-migrate to from sql to non-sql"

42073 of 92452 branches covered (45.51%)

Branch coverage included in aggregate %.

128008 of 165855 relevant lines covered (77.18%)

6379935.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/pdns/recursordist/nod.hh
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

24
#include <mutex>
25
#include <thread>
26
#include <boost/filesystem.hpp>
27

28
#include "dnsname.hh"
29
#include "lock.hh"
30
#include "stable-bloom.hh"
31

32
namespace nod
33
{
34
const float c_fp_rate = 0.01;
35
const size_t c_num_cells = 67108864;
36
const uint8_t c_num_dec = 10;
37
const unsigned int snapshot_interval_default = 600;
38
const std::string bf_suffix = "bf";
39
const std::string sbf_prefix = "sbf";
40

41
// Theses classes are not designed to be shared between threads
42
// Use a new instance per-thread, e.g. using thread local storage
43
// Synchronization (at the class level) is still needed for reading from
44
// and writing to the cache dir
45
// Synchronization (at the instance level) is needed when snapshotting
46
class PersistentSBF
47
{
48
public:
49
  PersistentSBF() :
50
    d_sbf(bf::stableBF(c_fp_rate, c_num_cells, c_num_dec)) {}
3✔
51
  PersistentSBF(uint32_t num_cells) :
52
    d_sbf(bf::stableBF(c_fp_rate, num_cells, c_num_dec)) {}
6✔
53
  bool init(bool ignore_pid = false);
54
  void setPrefix(const std::string& prefix) { d_prefix = prefix; } // Added to filenames in cachedir
9✔
55
  void setCacheDir(const std::string& cachedir);
56
  bool snapshotCurrent(std::thread::id tid); // Write the current file out to disk
57
  void add(const std::string& data)
58
  {
1✔
59
    d_sbf.lock()->add(data);
1✔
60
  }
1✔
61
  bool test(const std::string& data) { return d_sbf.lock()->test(data); }
×
62
  bool testAndAdd(const std::string& data)
63
  {
1,000,018✔
64
    return d_sbf.lock()->testAndAdd(data);
1,000,018✔
65
  }
1,000,018✔
66

67
private:
68
  void remove_tmp_files(const boost::filesystem::path&, std::scoped_lock<std::mutex>&);
69

70
  LockGuarded<bf::stableBF> d_sbf; // Stable Bloom Filter
71
  std::string d_cachedir;
72
  std::string d_prefix = sbf_prefix;
73
  // One mutex for all instances of this class, used to avoid multiple init() calls happening
74
  // simulateneously.  The snapshot code is thread safe wrt file operations, so it does not need to
75
  // acquire this mutex, assuming the init() code never runs simulatenously with the snapshot code.
76
  static std::mutex d_cachedir_mutex;
77
};
78

79
class NODDB
80
{
81
public:
82
  NODDB() = default;
3✔
83
  NODDB(uint32_t num_cells) :
84
    d_psbf{num_cells} {}
3✔
85
  // Set ignore_pid to true if you don't mind loading files
86
  // created by the current process
87
  bool init(bool ignore_pid = false)
88
  {
6✔
89
    d_psbf.setPrefix("nod");
6✔
90
    return d_psbf.init(ignore_pid);
6✔
91
  }
6✔
92
  bool isNewDomain(const std::string& domain); // Returns true if newly observed domain
93
  bool isNewDomain(const DNSName& dname); // As above
94
  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)
95
  bool isNewDomainWithParent(const DNSName& dname, std::string& observed); // As above
96
  void addDomain(const DNSName& dname); // You need to add this to refresh frequently used domains
97
  void addDomain(const std::string& domain); // As above
98
  void setSnapshotInterval(unsigned int secs) { d_snapshot_interval = secs; }
3✔
99
  void setCacheDir(const std::string& cachedir) { d_psbf.setCacheDir(cachedir); }
7✔
100
  bool snapshotCurrent(std::thread::id tid) { return d_psbf.snapshotCurrent(tid); }
1✔
101
  void housekeepingThread(std::thread::id tid);
102

103
private:
104
  PersistentSBF d_psbf;
105
  unsigned int d_snapshot_interval{snapshot_interval_default}; // Number seconds between snapshots
106
};
107

108
class UniqueResponseDB
109
{
110
public:
111
  UniqueResponseDB() = default;
112
  UniqueResponseDB(uint32_t num_cells) :
113
    d_psbf{num_cells} {}
3✔
114
  bool init(bool ignore_pid = false)
115
  {
3✔
116
    d_psbf.setPrefix("udr");
3✔
117
    return d_psbf.init(ignore_pid);
3✔
118
  }
3✔
119
  bool isUniqueResponse(const std::string& response);
120
  void addResponse(const std::string& response);
121
  void setSnapshotInterval(unsigned int secs) { d_snapshot_interval = secs; }
3✔
122
  void setCacheDir(const std::string& cachedir) { d_psbf.setCacheDir(cachedir); }
3✔
123
  bool snapshotCurrent(std::thread::id tid) { return d_psbf.snapshotCurrent(tid); }
×
124
  void housekeepingThread(std::thread::id tid);
125

126
private:
127
  PersistentSBF d_psbf;
128
  unsigned int d_snapshot_interval{snapshot_interval_default}; // Number seconds between snapshots
129
};
130

131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc