| 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_followRFC2181(false), 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 | 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 | |
|---|
| 46 | private: |
|---|
| 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 CacheEntry |
|---|
| 66 | { |
|---|
| 67 | CacheEntry(const tuple<string, uint16_t>& key, const vector<StoredRecord>& records, bool auth) : |
|---|
| 68 | d_qname(key.get<0>()), d_qtype(key.get<1>()), d_auth(auth), d_records(records) |
|---|
| 69 | {} |
|---|
| 70 | |
|---|
| 71 | typedef vector<StoredRecord> records_t; |
|---|
| 72 | |
|---|
| 73 | uint32_t getTTD() const |
|---|
| 74 | { |
|---|
| 75 | if(d_records.size()==1) |
|---|
| 76 | return d_records.begin()->d_ttd; |
|---|
| 77 | |
|---|
| 78 | uint32_t earliest=numeric_limits<uint32_t>::max(); |
|---|
| 79 | for(records_t::const_iterator i=d_records.begin(); i != d_records.end(); ++i) |
|---|
| 80 | earliest=min(earliest, i->d_ttd); |
|---|
| 81 | return earliest; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | string d_qname; |
|---|
| 85 | uint16_t d_qtype; |
|---|
| 86 | bool d_auth; |
|---|
| 87 | records_t d_records; |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | typedef multi_index_container< |
|---|
| 91 | CacheEntry, |
|---|
| 92 | indexed_by < |
|---|
| 93 | ordered_unique< |
|---|
| 94 | composite_key< |
|---|
| 95 | CacheEntry, |
|---|
| 96 | member<CacheEntry,string,&CacheEntry::d_qname>, |
|---|
| 97 | member<CacheEntry,uint16_t,&CacheEntry::d_qtype> |
|---|
| 98 | >, |
|---|
| 99 | composite_key_compare<CIStringCompare, std::less<uint16_t> > |
|---|
| 100 | >, |
|---|
| 101 | sequenced<> |
|---|
| 102 | > |
|---|
| 103 | > cache_t; |
|---|
| 104 | |
|---|
| 105 | cache_t d_cache; |
|---|
| 106 | pair<cache_t::iterator, cache_t::iterator> d_cachecache; |
|---|
| 107 | string d_cachedqname; |
|---|
| 108 | bool d_cachecachevalid; |
|---|
| 109 | bool attemptToRefreshNSTTL(const QType& qt, const set<DNSResourceRecord>& content, const CacheEntry& stored); |
|---|
| 110 | }; |
|---|
| 111 | string DNSRR2String(const DNSResourceRecord& rr); |
|---|
| 112 | DNSResourceRecord String2DNSRR(const string& qname, const QType& qt, const string& serial, uint32_t ttd); |
|---|
| 113 | |
|---|
| 114 | #endif |
|---|