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

Revision 1230, 4.5 KB (checked in by ahu, 2 years ago)

remove some empty lines

  • 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& str_a, const string& str_b) const
55  {
56    string::const_reverse_iterator ra, rb;
57    char a=0, b=0;
58    for(ra = str_a.rbegin(), rb = str_b.rbegin();
59        ra < str_a.rend() && rb < str_b.rend() && (a=dns_tolower(*ra)) == (b=dns_tolower(*rb));
60        ra++, rb++);
61   
62    if (ra < str_a.rend() && rb==str_b.rend()) { a=*(ra++); b=0; }
63    if (rb < str_b.rend() && ra==str_a.rend()) { b=*(rb++); a=0; }
64
65    return a < b;
66  }
67};
68
69
70class PacketCache
71{
72public:
73  PacketCache();
74  enum CacheEntryType { PACKETCACHE, QUERYCACHE, NEGCACHE};
75
76  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
77
78  void insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false);
79
80  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.
81  bool getEntry(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, bool meritsRecursion=false);
82
83  int size(); //!< number of entries in the cache
84  void cleanup(); //!< force the cache to preen itself from expired packets
85  int purge(const vector<string>&matches= vector<string>());
86
87  map<char,int> getCounts();
88private:
89  struct CacheEntry
90  {
91    CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false;}
92
93    string qname;
94    uint16_t qtype;
95    uint16_t ctype;
96    int zoneID;
97    time_t ttd;
98    bool meritsRecursion;
99    string value;
100  };
101
102  void getTTLS();
103
104  typedef multi_index_container<
105    CacheEntry,
106    indexed_by <
107                ordered_unique<
108                      composite_key< 
109                        CacheEntry,
110                        member<CacheEntry,string,&CacheEntry::qname>,
111                        member<CacheEntry,uint16_t,&CacheEntry::qtype>,
112                        member<CacheEntry,uint16_t, &CacheEntry::ctype>,
113                        member<CacheEntry,int, &CacheEntry::zoneID>,
114                        member<CacheEntry,bool, &CacheEntry::meritsRecursion>
115                      >,
116                  composite_key_compare<CIBackwardsStringCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool> >
117                >,
118               sequenced<>
119               >
120  > cmap_t;
121
122
123  cmap_t d_map;
124
125  pthread_rwlock_t d_mut;
126
127  int d_hit;
128  int d_miss;
129  int d_ttl;
130  int d_recursivettl;
131  bool d_doRecursion;
132  int *statnumhit;
133  int *statnummiss;
134  int *statnumentries;
135};
136
137
138
139#endif /* PACKETCACHE_HH */
140
Note: See TracBrowser for help on using the browser.