]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursor_cache.hh
Merge pull request #1782 from zeha/api-key
[thirdparty/pdns.git] / pdns / recursor_cache.hh
1 #ifndef RECURSOR_CACHE_HH
2 #define RECURSOR_CACHE_HH
3 #include <string>
4 #include <set>
5 #include "dns.hh"
6 #include "qtype.hh"
7 #include "misc.hh"
8 #include <iostream>
9
10 #include <boost/utility.hpp>
11 #undef L
12 #include <boost/multi_index_container.hpp>
13 #include <boost/multi_index/ordered_index.hpp>
14 #include <boost/tuple/tuple_comparison.hpp>
15 #include <boost/multi_index/key_extractors.hpp>
16 #include <boost/multi_index/sequenced_index.hpp>
17 #include <boost/version.hpp>
18
19 #undef max
20
21 #define L theL()
22 #include "namespaces.hh"
23 using namespace ::boost::multi_index;
24
25 class MemRecursorCache : public boost::noncopyable // : public RecursorCache
26 {
27 public:
28 MemRecursorCache() : d_cachecachevalid(false)
29 {
30 cacheHits = cacheMisses = 0;
31 }
32 unsigned int size();
33 unsigned int bytes();
34 int get(time_t, const string &qname, const QType& qt, set<DNSResourceRecord>* res);
35
36 int getDirect(time_t now, const char* qname, const QType& qt, uint32_t ttd[10], char* data[10], uint16_t len[10]);
37 void replace(time_t, const string &qname, const QType& qt, const set<DNSResourceRecord>& content, bool auth);
38 void doPrune(void);
39 void doSlash(int perc);
40 uint64_t doDump(int fd);
41 uint64_t doDumpNSSpeeds(int fd);
42
43 int doWipeCache(const string& name, uint16_t qtype=0xffff);
44 bool doAgeCache(time_t now, const string& name, uint16_t qtype, int32_t newTTL);
45 uint64_t cacheHits, cacheMisses;
46
47 private:
48 struct StoredRecord
49 {
50 mutable uint32_t d_ttd;
51
52 string d_string;
53
54 bool operator<(const StoredRecord& rhs) const
55 {
56 return d_string < rhs.d_string;
57 }
58
59 unsigned int size() const
60 {
61 return sizeof(*this) + d_string.size();
62 }
63
64 };
65
66 struct CacheEntry
67 {
68 CacheEntry(const boost::tuple<string, uint16_t>& key, const vector<StoredRecord>& records, bool auth) :
69 d_qname(key.get<0>()), d_qtype(key.get<1>()), d_auth(auth), d_records(records)
70 {}
71
72 typedef vector<StoredRecord> records_t;
73
74 uint32_t getTTD() const
75 {
76 if(d_records.size()==1)
77 return d_records.begin()->d_ttd;
78
79 uint32_t earliest=std::numeric_limits<uint32_t>::max();
80 for(records_t::const_iterator i=d_records.begin(); i != d_records.end(); ++i)
81 earliest=min(earliest, i->d_ttd);
82 return earliest;
83 }
84
85 string d_qname;
86 uint16_t d_qtype;
87 bool d_auth;
88 records_t d_records;
89 };
90
91 typedef multi_index_container<
92 CacheEntry,
93 indexed_by <
94 ordered_unique<
95 composite_key<
96 CacheEntry,
97 member<CacheEntry,string,&CacheEntry::d_qname>,
98 member<CacheEntry,uint16_t,&CacheEntry::d_qtype>
99 >,
100 composite_key_compare<CIStringCompare, std::less<uint16_t> >
101 >,
102 sequenced<>
103 >
104 > cache_t;
105
106 cache_t d_cache;
107 pair<cache_t::iterator, cache_t::iterator> d_cachecache;
108 string d_cachedqname;
109 bool d_cachecachevalid;
110 bool attemptToRefreshNSTTL(const QType& qt, const set<DNSResourceRecord>& content, const CacheEntry& stored);
111 };
112 string DNSRR2String(const DNSResourceRecord& rr);
113 DNSResourceRecord String2DNSRR(const string& qname, const QType& qt, const string& serial, uint32_t ttd);
114
115 #endif