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

Revision 2211, 5.3 KB (checked in by ahu, 2 years ago)

fix corner case where the packetcache might div up the wrong answer if there was a question of identical length under some circumstances - has not been observed

  • 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 - 2011  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#include "dns.hh"
27#include <boost/version.hpp>
28#include "namespaces.hh"
29using namespace ::boost::multi_index;
30
31#include "namespaces.hh"
32#include "dnspacket.hh"
33#include "lock.hh"
34#include "statbag.hh"
35
36/** This class performs 'whole packet caching'. Feed it a question packet and it will
37    try to find an answer. If you have an answer, insert it to have it cached for later use.
38    Take care not to replace existing cache entries. While this works, it is wasteful. Only
39    insert packets that where not found by get()
40
41    Locking!
42
43    The cache itself is protected by a read/write lock. Because deleting is a two step process, which
44    first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
45*/
46
47struct CIBackwardsStringCompare: public std::binary_function<string, string, bool> 
48{
49  bool operator()(const string& str_a, const string& str_b) const
50  {
51    string::const_reverse_iterator ra, rb;
52    char a=0, b=0;
53    for(ra = str_a.rbegin(), rb = str_b.rbegin();
54        ra < str_a.rend() && rb < str_b.rend() && (a=dns_tolower(*ra)) == (b=dns_tolower(*rb));
55        ra++, rb++);
56   
57    if (ra < str_a.rend() && rb==str_b.rend()) { a=*(ra++); b=0; return false; } // we are at the beginning of b -> b smaller
58    if (rb < str_b.rend() && ra==str_a.rend()) { b=*(rb++); a=0; return true; } // we are at the beginning of a -> a smaller
59    // if BOTH are at their ends, a and b will be equal, and we should return false, which we will
60    return a < b;
61  }
62};
63
64
65class PacketCache : public boost::noncopyable
66{
67public:
68  PacketCache();
69  ~PacketCache();
70  enum CacheEntryType { PACKETCACHE, QUERYCACHE};
71
72  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
73
74  void insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false,
75    unsigned int maxReplyLen=512, bool dnssecOk=false);
76
77  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.
78  bool getEntry(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, 
79    bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false);
80
81  int size(); //!< number of entries in the cache
82  void cleanup(); //!< force the cache to preen itself from expired packets
83  int purge(const vector<string>&matches= vector<string>());
84
85  map<char,int> getCounts();
86private:
87  bool getEntryLocked(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, 
88    bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false);
89  struct CacheEntry
90  {
91    CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false; dnssecOk=false;}
92
93    string qname;
94    uint16_t qtype;
95    uint16_t ctype;
96    int zoneID;
97    time_t ttd;
98    bool meritsRecursion;
99    unsigned int maxReplyLen;
100    bool dnssecOk;
101    string value;
102  };
103
104  void getTTLS();
105
106  typedef multi_index_container<
107    CacheEntry,
108    indexed_by <
109                ordered_unique<
110                      composite_key< 
111                        CacheEntry,
112                        member<CacheEntry,string,&CacheEntry::qname>,
113                        member<CacheEntry,uint16_t,&CacheEntry::qtype>,
114                        member<CacheEntry,uint16_t, &CacheEntry::ctype>,
115                        member<CacheEntry,int, &CacheEntry::zoneID>,
116                        member<CacheEntry,bool, &CacheEntry::meritsRecursion>,
117                        member<CacheEntry,unsigned int, &CacheEntry::maxReplyLen>,
118                        member<CacheEntry,bool, &CacheEntry::dnssecOk>
119                        >,
120                        composite_key_compare<CIBackwardsStringCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool>, 
121                          std::less<unsigned int>, std::less<bool> >
122                            >,
123                           sequenced<>
124                           >
125  > cmap_t;
126
127
128  cmap_t d_map;
129
130  pthread_rwlock_t d_mut;
131
132  unsigned int d_ops;
133  int d_ttl;
134  int d_recursivettl;
135  bool d_doRecursion;
136  unsigned int *d_statnumhit;
137  unsigned int *d_statnummiss;
138  unsigned int *d_statnumentries;
139};
140
141
142
143#endif /* PACKETCACHE_HH */
144
Note: See TracBrowser for help on using the browser.