]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursor_cache.hh
Merge pull request #5523 from rubenk/fix-typos-in-logmessage
[thirdparty/pdns.git] / pdns / recursor_cache.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 #ifndef RECURSOR_CACHE_HH
23 #define RECURSOR_CACHE_HH
24 #include <string>
25 #include <set>
26 #include "dns.hh"
27 #include "qtype.hh"
28 #include "misc.hh"
29 #include "dnsname.hh"
30 #include <iostream>
31 #include "dnsrecords.hh"
32 #include <boost/utility.hpp>
33 #undef L
34 #include <boost/multi_index_container.hpp>
35 #include <boost/multi_index/ordered_index.hpp>
36 #include <boost/multi_index/hashed_index.hpp>
37 #include <boost/tuple/tuple_comparison.hpp>
38 #include <boost/multi_index/key_extractors.hpp>
39 #include <boost/multi_index/sequenced_index.hpp>
40 #include <boost/version.hpp>
41 #include "iputils.hh"
42 #include "validate.hh"
43 #undef max
44
45 #define L theL()
46 #include "namespaces.hh"
47 using namespace ::boost::multi_index;
48
49 class MemRecursorCache : public boost::noncopyable // : public RecursorCache
50 {
51 public:
52 MemRecursorCache() : d_cachecachevalid(false)
53 {
54 cacheHits = cacheMisses = 0;
55 }
56 unsigned int size();
57 unsigned int bytes();
58 int32_t get(time_t, const DNSName &qname, const QType& qt, bool requireAuth, vector<DNSRecord>* res, const ComboAddress& who, vector<std::shared_ptr<RRSIGRecordContent>>* signatures=nullptr, std::vector<std::shared_ptr<DNSRecord>>* authorityRecs=nullptr, bool* variable=nullptr, vState* state=nullptr, bool* wasAuth=nullptr);
59
60 void replace(time_t, const DNSName &qname, const QType& qt, const vector<DNSRecord>& content, const vector<shared_ptr<RRSIGRecordContent>>& signatures, const std::vector<std::shared_ptr<DNSRecord>>& authorityRecs, bool auth, boost::optional<Netmask> ednsmask=boost::none, vState state=Indeterminate);
61
62 void doPrune(void);
63 void doPrune(unsigned int keep);
64 uint64_t doDump(int fd);
65
66 int doWipeCache(const DNSName& name, bool sub, uint16_t qtype=0xffff);
67 bool doAgeCache(time_t now, const DNSName& name, uint16_t qtype, uint32_t newTTL);
68 bool updateValidationStatus(time_t now, const DNSName &qname, const QType& qt, const ComboAddress& who, bool requireAuth, vState newState);
69
70 uint64_t cacheHits, cacheMisses;
71
72 private:
73
74 struct CacheEntry
75 {
76 CacheEntry(const boost::tuple<DNSName, uint16_t, Netmask>& key, const vector<shared_ptr<DNSRecordContent>>& records, bool auth) :
77 d_records(records), d_qname(key.get<0>()), d_netmask(key.get<2>()), d_state(Indeterminate), d_ttd(0), d_qtype(key.get<1>()), d_auth(auth)
78 {}
79
80 typedef vector<std::shared_ptr<DNSRecordContent>> records_t;
81 time_t getTTD() const
82 {
83 return d_ttd;
84 }
85
86 records_t d_records;
87 std::vector<std::shared_ptr<RRSIGRecordContent>> d_signatures;
88 std::vector<std::shared_ptr<DNSRecord>> d_authorityRecs;
89 DNSName d_qname;
90 Netmask d_netmask;
91 mutable vState d_state;
92 time_t d_ttd;
93 uint16_t d_qtype;
94 bool d_auth;
95 };
96
97 /* The ECS Index (d_ecsIndex) keeps track of whether there is any ECS-specific
98 entry for a given (qname,qtype) entry in the cache (d_cache), and if so
99 provides a NetmaskTree of those ECS entries.
100 This allows figuring out quickly if we should look for an entry
101 specific to the requestor IP, and if so which entry is the most
102 specific one.
103 Keeping the entries in the regular cache is currently necessary
104 because of the way we manage expired entries (moving them to the
105 front of the expunge queue to be deleted at a regular interval).
106 */
107 class ECSIndexEntry
108 {
109 public:
110 ECSIndexEntry(const DNSName& qname, uint16_t qtype): d_qname(qname), d_qtype(qtype)
111 {
112 }
113
114 Netmask lookupBestMatch(const ComboAddress& addr) const
115 {
116 Netmask result = Netmask();
117
118 const auto best = d_nmt.lookup(addr);
119 if (best != nullptr) {
120 result = best->first;
121 }
122
123 return result;
124 }
125
126 void addMask(const Netmask& nm) const
127 {
128 d_nmt.insert(nm).second = true;
129 }
130
131 void removeNetmask(const Netmask& nm) const
132 {
133 d_nmt.erase(nm);
134 }
135
136 bool isEmpty() const
137 {
138 return d_nmt.empty();
139 }
140
141 mutable NetmaskTree<bool> d_nmt;
142 DNSName d_qname;
143 uint16_t d_qtype;
144 };
145
146 typedef multi_index_container<
147 CacheEntry,
148 indexed_by <
149 ordered_unique<
150 composite_key<
151 CacheEntry,
152 member<CacheEntry,DNSName,&CacheEntry::d_qname>,
153 member<CacheEntry,uint16_t,&CacheEntry::d_qtype>,
154 member<CacheEntry,Netmask,&CacheEntry::d_netmask>
155 >,
156 composite_key_compare<CanonDNSNameCompare, std::less<uint16_t>, std::less<Netmask> >
157 >,
158 sequenced<>
159 >
160 > cache_t;
161 typedef multi_index_container<
162 ECSIndexEntry,
163 indexed_by <
164 ordered_unique <
165 composite_key<
166 ECSIndexEntry,
167 member<ECSIndexEntry,DNSName,&ECSIndexEntry::d_qname>,
168 member<ECSIndexEntry,uint16_t,&ECSIndexEntry::d_qtype>
169 >
170 >
171 >
172 > ecsIndex_t;
173
174 cache_t d_cache;
175 ecsIndex_t d_ecsIndex;
176 pair<cache_t::iterator, cache_t::iterator> d_cachecache;
177 DNSName d_cachedqname;
178 bool d_cachecachevalid;
179
180 bool attemptToRefreshNSTTL(const QType& qt, const vector<DNSRecord>& content, const CacheEntry& stored);
181 bool entryMatches(cache_t::const_iterator& entry, uint16_t qt, bool requireAuth, const ComboAddress& who);
182 std::pair<cache_t::const_iterator, cache_t::const_iterator> getEntries(const DNSName &qname, const QType& qt);
183 cache_t::const_iterator getEntryUsingECSIndex(time_t now, const DNSName &qname, uint16_t qtype, bool requireAuth, const ComboAddress& who);
184 int32_t handleHit(cache_t::iterator entry, const DNSName& qname, const ComboAddress& who, vector<DNSRecord>* res, vector<std::shared_ptr<RRSIGRecordContent>>* signatures, std::vector<std::shared_ptr<DNSRecord>>* authorityRecs, bool* variable, vState* state, bool* wasAuth);
185
186 public:
187 void preRemoval(const CacheEntry& entry)
188 {
189 if (entry.d_netmask.empty()) {
190 return;
191 }
192
193 auto key = tie(entry.d_qname, entry.d_qtype);
194 auto ecsIndexEntry = d_ecsIndex.find(key);
195 if (ecsIndexEntry != d_ecsIndex.end()) {
196 ecsIndexEntry->removeNetmask(entry.d_netmask);
197 if (ecsIndexEntry->isEmpty()) {
198 d_ecsIndex.erase(ecsIndexEntry);
199 }
200 }
201 }
202 };
203 #endif