2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
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.
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
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef PACKETCACHE_HH
23 #define PACKETCACHE_HH
30 #include <boost/version.hpp>
31 #include "namespaces.hh"
32 using namespace ::boost::multi_index;
34 #include "namespaces.hh"
35 #include <boost/multi_index/hashed_index.hpp>
36 #include "dnspacket.hh"
40 /** This class performs 'whole packet caching'. Feed it a question packet and it will
41 try to find an answer. If you have an answer, insert it to have it cached for later use.
42 Take care not to replace existing cache entries. While this works, it is wasteful. Only
43 insert packets that where not found by get()
47 The cache itself is protected by a read/write lock. Because deleting is a two step process, which
48 first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
51 class PacketCache : public boost::noncopyable
56 enum CacheEntryType { PACKETCACHE, QUERYCACHE};
58 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
60 void insert(const DNSName &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false,
61 unsigned int maxReplyLen=512, bool dnssecOk=false, bool EDNS=false);
63 void insert(const DNSName &qname, const QType& qtype, CacheEntryType cet, const vector<DNSZoneRecord>& content, unsigned int ttl, int zoneID=-1);
65 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.
66 bool getEntry(const DNSName &qname, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
67 bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0);
68 bool getEntry(const DNSName &qname, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
71 int size() { return *d_statnumentries; } //!< number of entries in the cache
72 void cleanupIfNeeded();
73 void cleanup(); //!< force the cache to preen itself from expired packets
75 int purge(const std::string& match); // could be $ terminated. Is not a dnsname!
76 int purgeExact(const DNSName& qname); // no wildcard matching here
78 map<char,int> getCounts();
80 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
81 bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0);
82 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
87 CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false; dnssecOk=false; hasEDNS=false; created=0; ttd=0; maxReplyLen=512;}
91 vector<DNSZoneRecord> drs;
98 unsigned int maxReplyLen;
100 bool meritsRecursion;
107 struct UnorderedNameTag{};
108 struct SequenceTag{};
109 typedef multi_index_container<
115 member<CacheEntry,DNSName,&CacheEntry::qname>,
116 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
117 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
118 member<CacheEntry,int, &CacheEntry::zoneID>,
119 member<CacheEntry,bool, &CacheEntry::meritsRecursion>,
120 member<CacheEntry,unsigned int, &CacheEntry::maxReplyLen>,
121 member<CacheEntry,bool, &CacheEntry::dnssecOk>,
122 member<CacheEntry,bool, &CacheEntry::hasEDNS>
124 composite_key_compare<CanonDNSNameCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool>,
125 std::less<unsigned int>, std::less<bool>, std::less<bool> >
127 hashed_non_unique<tag<UnorderedNameTag>, composite_key<CacheEntry,
128 member<CacheEntry,DNSName,&CacheEntry::qname>,
129 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
130 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
131 member<CacheEntry,int, &CacheEntry::zoneID> > > ,
132 sequenced<tag<SequenceTag>>
139 pthread_rwlock_t d_mut;
143 vector<MapCombo> d_maps;
144 MapCombo& getMap(const DNSName& qname)
146 return d_maps[qname.hash() % d_maps.size()];
150 time_t d_lastclean; // doesn't need to be atomic
151 unsigned long d_nextclean;
152 unsigned int d_cleaninterval;
154 AtomicCounter *d_statnumhit;
155 AtomicCounter *d_statnummiss;
156 AtomicCounter *d_statnumentries;
162 static constexpr unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
167 #endif /* PACKETCACHE_HH */