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

PowerDNS / pdns / 12595591960

03 Jan 2025 09:27AM UTC coverage: 62.774% (+2.5%) from 60.245%
12595591960

Pull #15008

github

web-flow
Merge c2a2749d3 into 788f396a7
Pull Request #15008: Do not follow CNAME records for ANY or CNAME queries

30393 of 78644 branches covered (38.65%)

Branch coverage included in aggregate %.

105822 of 138350 relevant lines covered (76.49%)

4613078.44 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
#include <atomic>
24
#include <mutex>
25
#include <thread>
26
#include <boost/filesystem.hpp>
27
#include "dnsname.hh"
28
#include "lock.hh"
29
#include "stable-bloom.hh"
30

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

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

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

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

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

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

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

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

130
}
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