root/trunk/pdns/pdns/recursor_cache.hh

Revision 1265, 3.5 kB (checked in by ahu, 9 months ago)

reported by Andreas Jakum & investigated by Stefan Schmidt - make sure we don't hammer parent zones in case of failed delegations to servers that don't exist at all. Only invalidate once every 10 seconds

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
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#include <boost/utility.hpp>
10#undef L
11#include <boost/multi_index_container.hpp>
12#include <boost/multi_index/ordered_index.hpp>
13#include <boost/tuple/tuple_comparison.hpp>
14#include <boost/multi_index/key_extractors.hpp>
15#include <boost/multi_index/sequenced_index.hpp>
16#include <boost/version.hpp>
17#if BOOST_VERSION >= 103300
18#include <boost/multi_index/hashed_index.hpp>
19#endif
20
21#undef max
22
23#define L theL()
24using namespace boost;
25using namespace ::boost::multi_index;
26
27class MemRecursorCache : public boost::noncopyable //  : public RecursorCache
28{
29public:
30  MemRecursorCache() : d_followRFC2181(false), d_cachecachevalid(false)
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  void doDumpAndClose(int fd);
41  int doWipeCache(const string& name, uint16_t qtype=0xffff);
42  bool doAgeCache(time_t now, const string& name, uint16_t qtype, int32_t newTTL);
43  uint64_t cacheHits, cacheMisses;
44  bool d_followRFC2181;
45
46private:
47  struct StoredRecord
48  {
49    mutable uint32_t d_ttd;
50
51    string d_string;
52
53    bool operator<(const StoredRecord& rhs) const
54    {
55      return d_string < rhs.d_string;
56    }
57
58    unsigned int size() const
59    {
60      return ( unsigned int ) 4+d_string.size();
61    }
62
63  };
64
65  struct predicate
66  {
67    predicate(uint32_t limit) : d_limit(limit)
68    {
69    }
70   
71    bool operator()(const StoredRecord& sr) const
72    {
73      return sr.d_ttd <= d_limit;
74    }
75    uint32_t d_limit;
76  };
77
78  //   typedef __gnu_cxx::hash_map<string, vector<StoredRecord> > cache_t;
79  struct CacheEntry
80  {
81    string d_qname;
82    uint16_t d_qtype;
83    bool d_auth;
84
85    CacheEntry(const tuple<string, uint16_t>& key, const vector<StoredRecord>& records, bool auth) : 
86      d_qname(key.get<0>()), d_qtype(key.get<1>()), d_auth(auth), d_records(records)
87    {}
88
89    typedef vector<StoredRecord> records_t;
90    records_t d_records;
91    uint32_t getTTD() const
92    {
93      if(d_records.size()==1)
94        return d_records.begin()->d_ttd;
95
96      uint32_t earliest=numeric_limits<uint32_t>::max();
97      for(records_t::const_iterator i=d_records.begin(); i != d_records.end(); ++i)
98        earliest=min(earliest, i->d_ttd);
99      return earliest;
100    }
101  };
102
103  typedef multi_index_container<
104    CacheEntry,
105    indexed_by <
106                ordered_unique<
107                      composite_key< 
108                        CacheEntry,
109                        member<CacheEntry,string,&CacheEntry::d_qname>,
110                        member<CacheEntry,uint16_t,&CacheEntry::d_qtype>
111                      >,
112                      composite_key_compare<CIStringCompare, std::less<uint16_t> >
113                >,
114               sequenced<>
115               >
116  > cache_t;
117
118  cache_t d_cache;
119  pair<cache_t::iterator, cache_t::iterator> d_cachecache;
120  string d_cachedqname;
121  bool d_cachecachevalid;
122  bool attemptToRefreshNSTTL(const QType& qt, const set<DNSResourceRecord>& content, const CacheEntry& stored);
123
124};
125string DNSRR2String(const DNSResourceRecord& rr);
126DNSResourceRecord String2DNSRR(const string& qname, const QType& qt, const string& serial, uint32_t ttd);
127
128#endif
Note: See TracBrowser for help on using the browser.