]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursor_cache.hh
Merge pull request #5332 from rgacogne/edns-truncate-tests
[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/tuple/tuple_comparison.hpp>
37 #include <boost/multi_index/key_extractors.hpp>
38 #include <boost/multi_index/sequenced_index.hpp>
39 #include <boost/version.hpp>
40 #include "iputils.hh"
41 #undef max
42
43 #define L theL()
44 #include "namespaces.hh"
45 using namespace ::boost::multi_index;
46
47 class MemRecursorCache : public boost::noncopyable // : public RecursorCache
48 {
49 public:
50 MemRecursorCache() : d_cachecachevalid(false)
51 {
52 cacheHits = cacheMisses = 0;
53 }
54 unsigned int size();
55 unsigned int bytes();
56 int32_t get(time_t, const DNSName &qname, const QType& qt, vector<DNSRecord>* res, const ComboAddress& who, vector<std::shared_ptr<RRSIGRecordContent>>* signatures=0);
57
58 void replace(time_t, const DNSName &qname, const QType& qt, const vector<DNSRecord>& content, const vector<shared_ptr<RRSIGRecordContent>>& signatures, bool auth, boost::optional<Netmask> ednsmask=boost::optional<Netmask>());
59 void doPrune(void);
60 uint64_t doDump(int fd);
61 uint64_t doDumpNSSpeeds(int fd);
62
63 int doWipeCache(const DNSName& name, bool sub, uint16_t qtype=0xffff);
64 bool doAgeCache(time_t now, const DNSName& name, uint16_t qtype, uint32_t newTTL);
65
66 uint64_t cacheHits, cacheMisses;
67
68 private:
69
70 struct CacheEntry
71 {
72 CacheEntry(const boost::tuple<DNSName, uint16_t, Netmask>& key, const vector<shared_ptr<DNSRecordContent>>& records, bool auth) :
73 d_qname(key.get<0>()), d_qtype(key.get<1>()), d_auth(auth), d_ttd(0), d_records(records), d_netmask(key.get<2>())
74 {}
75
76 typedef vector<std::shared_ptr<DNSRecordContent>> records_t;
77 vector<std::shared_ptr<RRSIGRecordContent>> d_signatures;
78 time_t getTTD() const
79 {
80 return d_ttd;
81 }
82
83 DNSName d_qname;
84 uint16_t d_qtype;
85 bool d_auth;
86 time_t d_ttd;
87 records_t d_records;
88 Netmask d_netmask;
89 };
90
91 typedef multi_index_container<
92 CacheEntry,
93 indexed_by <
94 ordered_unique<
95 composite_key<
96 CacheEntry,
97 member<CacheEntry,DNSName,&CacheEntry::d_qname>,
98 member<CacheEntry,uint16_t,&CacheEntry::d_qtype>,
99 member<CacheEntry,Netmask,&CacheEntry::d_netmask>
100 >,
101 composite_key_compare<CanonDNSNameCompare, std::less<uint16_t>, std::less<Netmask> >
102 >,
103 sequenced<>
104 >
105 > cache_t;
106
107 cache_t d_cache;
108 pair<cache_t::iterator, cache_t::iterator> d_cachecache;
109 DNSName d_cachedqname;
110 bool d_cachecachevalid;
111 bool attemptToRefreshNSTTL(const QType& qt, const vector<DNSRecord>& content, const CacheEntry& stored);
112 };
113 #endif