root/trunk/pdns/pdns/packetcache.hh @ 1221

Revision 1221, 4.6 KB (checked in by ahu, 5 years ago)

implement purging whole lists of domains or suffixes instead of 1 at a time

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1/*
2    PowerDNS Versatile Database Driven Nameserver
3    Copyright (C) 2002 - 2008  PowerDNS.COM BV
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License version 2
7    as published by the Free Software Foundation
8   
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18*/
19#ifndef PACKETCACHE_HH
20#define PACKETCACHE_HH
21
22#include <string>
23#include <utility>
24#include <map>
25#include <map>
26
27#include <boost/multi_index_container.hpp>
28#include <boost/multi_index/ordered_index.hpp>
29#include <boost/tuple/tuple_comparison.hpp>
30#include <boost/multi_index/key_extractors.hpp>
31#include <boost/multi_index/sequenced_index.hpp>
32#include <boost/version.hpp>
33using namespace std;
34using namespace ::boost::multi_index;
35
36using namespace boost;
37#include "dnspacket.hh"
38#include "lock.hh"
39#include "statbag.hh"
40
41/** This class performs 'whole packet caching'. Feed it a question packet and it will
42    try to find an answer. If you have an answer, insert it to have it cached for later use.
43    Take care not to replace existing cache entries. While this works, it is wasteful. Only
44    insert packets that where not found by get()
45
46    Locking!
47
48    The cache itself is protected by a read/write lock. Because deleting is a two step process, which
49    first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
50*/
51
52struct CIBackwardsStringCompare: public binary_function<string, string, bool> 
53{
54  bool operator()(const string& a, const string& b) const
55  {
56    const unsigned char *p1 = (const unsigned char *) a.c_str() + a.length();
57    const unsigned char *p2 = (const unsigned char *) b.c_str() + b.length();
58    int result;
59   
60    if (p1 == p2) {
61      return 0;
62    }
63   
64    while ((result = dns_tolower (*p1--) - dns_tolower (*p2--)) == 0)
65      if (p1 == (unsigned char*)a.c_str() || p2 == (unsigned char*) b.c_str())
66        break;
67   
68    if(result==0) { // one of our strings ended, the shortest one is smaller then
69      return a.length() < b.length();
70    }
71
72    return result < 0;
73  }
74};
75
76
77class PacketCache
78{
79public:
80  PacketCache();
81  enum CacheEntryType { PACKETCACHE, QUERYCACHE, NEGCACHE};
82
83
84  void insert(DNSPacket *q, DNSPacket *r);  //!< We copy the contents of *p into our cache. Do not needlessly call this to insert questions already in the cache as it wastes resources
85
86  void insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false);
87
88  int get(DNSPacket *p, DNSPacket *q); //!< We return a dynamically allocated copy out of our cache. You need to delete it. You also need to spoof in the right ID with the DNSPacket.spoofID() method.
89  bool getEntry(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, bool meritsRecursion=false);
90
91  int size(); //!< number of entries in the cache
92  void cleanup(); //!< force the cache to preen itself from expired packets
93  int purge(const vector<string>&matches= vector<string>());
94
95  map<char,int> getCounts();
96private:
97  struct CacheEntry
98  {
99    CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false;}
100
101    string qname;
102    uint16_t qtype;
103    uint16_t ctype;
104    int zoneID;
105    time_t ttd;
106    bool meritsRecursion;
107    string value;
108  };
109
110  void getTTLS();
111
112  typedef multi_index_container<
113    CacheEntry,
114    indexed_by <
115                ordered_unique<
116                      composite_key< 
117                        CacheEntry,
118                        member<CacheEntry,string,&CacheEntry::qname>,
119                        member<CacheEntry,uint16_t,&CacheEntry::qtype>,
120                        member<CacheEntry,uint16_t, &CacheEntry::ctype>,
121                        member<CacheEntry,int, &CacheEntry::zoneID>,
122                        member<CacheEntry,bool, &CacheEntry::meritsRecursion>
123                      >,
124                  composite_key_compare<CIBackwardsStringCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool> >
125                >,
126               sequenced<>
127               >
128  > cmap_t;
129
130
131  cmap_t d_map;
132
133  pthread_rwlock_t d_mut;
134
135  int d_hit;
136  int d_miss;
137  int d_ttl;
138  int d_recursivettl;
139  bool d_doRecursion;
140  int *statnumhit;
141  int *statnummiss;
142  int *statnumentries;
143};
144
145
146
147#endif /* PACKETCACHE_HH */
148
Note: See TracBrowser for help on using the browser.