]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/packetcache.hh
Merge branch 'master' of github.com:PowerDNS/pdns
[thirdparty/pdns.git] / pdns / packetcache.hh
CommitLineData
12c86877
BH
1/*
2 PowerDNS Versatile Database Driven Nameserver
cf00d8d3 3 Copyright (C) 2002 - 2011 PowerDNS.COM BV
12c86877
BH
4
5 This program is free software; you can redistribute it and/or modify
22dc646a
BH
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
8
f782fe38
MH
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
12c86877
BH
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
06bd9ccf 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12c86877
BH
21*/
22#ifndef PACKETCACHE_HH
23#define PACKETCACHE_HH
24
25#include <string>
26#include <utility>
27#include <map>
ba45c866 28#include <map>
5f8fcf64 29#include "dns.hh"
ba45c866 30#include <boost/version.hpp>
10f4eea8 31#include "namespaces.hh"
ba45c866 32using namespace ::boost::multi_index;
12c86877 33
61b26744 34#include "namespaces.hh"
12c86877
BH
35#include "dnspacket.hh"
36#include "lock.hh"
37#include "statbag.hh"
38
39/** This class performs 'whole packet caching'. Feed it a question packet and it will
40 try to find an answer. If you have an answer, insert it to have it cached for later use.
41 Take care not to replace existing cache entries. While this works, it is wasteful. Only
42 insert packets that where not found by get()
43
44 Locking!
45
46 The cache itself is protected by a read/write lock. Because deleting is a two step process, which
47 first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
12c86877 48*/
ba45c866 49
3dbfa51e 50class PacketCache : public boost::noncopyable
12c86877
BH
51{
52public:
53 PacketCache();
3dbfa51e 54 ~PacketCache();
2f24bcd2 55 enum CacheEntryType { PACKETCACHE, QUERYCACHE};
ba45c866 56
75fde355 57 void insert(DNSPacket *q, DNSPacket *r, bool recursive, unsigned int maxttl=UINT_MAX); //!< 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
12c86877 58
cf00d8d3 59 void insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false,
17d0b1e6 60 unsigned int maxReplyLen=512, bool dnssecOk=false, bool EDNS=false);
2667dc9a 61
75fde355
KM
62 int get(DNSPacket *p, DNSPacket *q, bool recursive); //!< 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.
63 bool getEntry(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
64 bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0);
ba45c866 65
12c86877
BH
66 int size(); //!< number of entries in the cache
67 void cleanup(); //!< force the cache to preen itself from expired packets
27fdc3fc
BH
68 int purge();
69 int purge(const string &match);
2667dc9a 70
12c86877
BH
71 map<char,int> getCounts();
72private:
75fde355
KM
73 bool getEntryLocked(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
74 bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0);
801812e6 75 string pcReverse(const string &content);
ba45c866 76 struct CacheEntry
12c86877 77 {
17d0b1e6 78 CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false; dnssecOk=false; hasEDNS=false;}
ba45c866
BH
79
80 string qname;
81 uint16_t qtype;
82 uint16_t ctype;
83 int zoneID;
75fde355 84 time_t created;
12c86877 85 time_t ttd;
ba45c866 86 bool meritsRecursion;
cf00d8d3 87 unsigned int maxReplyLen;
a637d0a5 88 bool dnssecOk;
17d0b1e6 89 bool hasEDNS;
12c86877
BH
90 string value;
91 };
92
12c86877 93 void getTTLS();
ba45c866
BH
94
95 typedef multi_index_container<
96 CacheEntry,
97 indexed_by <
98 ordered_unique<
99 composite_key<
100 CacheEntry,
101 member<CacheEntry,string,&CacheEntry::qname>,
102 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
cf00d8d3
BH
103 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
104 member<CacheEntry,int, &CacheEntry::zoneID>,
105 member<CacheEntry,bool, &CacheEntry::meritsRecursion>,
a637d0a5 106 member<CacheEntry,unsigned int, &CacheEntry::maxReplyLen>,
17d0b1e6
PD
107 member<CacheEntry,bool, &CacheEntry::dnssecOk>,
108 member<CacheEntry,bool, &CacheEntry::hasEDNS>
cf00d8d3 109 >,
801812e6 110 composite_key_compare<std::less<string>, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool>,
17d0b1e6 111 std::less<unsigned int>, std::less<bool>, std::less<bool> >
cf00d8d3
BH
112 >,
113 sequenced<>
114 >
ba45c866
BH
115 > cmap_t;
116
12c86877 117
12c86877
BH
118 cmap_t d_map;
119
120 pthread_rwlock_t d_mut;
12c86877 121
16f7d28d 122 AtomicCounter d_ops;
12c86877
BH
123 int d_ttl;
124 int d_recursivettl;
125 bool d_doRecursion;
1566533a 126 AtomicCounter *d_statnumhit;
127 AtomicCounter *d_statnummiss;
128 AtomicCounter *d_statnumentries;
12c86877
BH
129};
130
12c86877
BH
131
132
133#endif /* PACKETCACHE_HH */
134