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, 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,
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); //!< 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 unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false);
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 unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false);
82 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
87 CacheEntry() { qtype = ctype = 0; zoneID = -1; dnssecOk=false; hasEDNS=false; created=0; ttd=0; maxReplyLen=512;}
91 vector<DNSZoneRecord> drs;
98 unsigned int maxReplyLen;
106 struct UnorderedNameTag{};
107 struct SequenceTag{};
108 typedef multi_index_container<
114 member<CacheEntry,DNSName,&CacheEntry::qname>,
115 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
116 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
117 member<CacheEntry,int, &CacheEntry::zoneID>,
118 member<CacheEntry,unsigned int, &CacheEntry::maxReplyLen>,
119 member<CacheEntry,bool, &CacheEntry::dnssecOk>,
120 member<CacheEntry,bool, &CacheEntry::hasEDNS>
122 composite_key_compare<CanonDNSNameCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>,
123 std::less<unsigned int>, std::less<bool>, std::less<bool> >
125 hashed_non_unique<tag<UnorderedNameTag>, composite_key<CacheEntry,
126 member<CacheEntry,DNSName,&CacheEntry::qname>,
127 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
128 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
129 member<CacheEntry,int, &CacheEntry::zoneID> > > ,
130 sequenced<tag<SequenceTag>>
137 pthread_rwlock_t d_mut;
141 vector<MapCombo> d_maps;
142 MapCombo& getMap(const DNSName& qname)
144 return d_maps[qname.hash() % d_maps.size()];
148 time_t d_lastclean; // doesn't need to be atomic
149 unsigned long d_nextclean;
150 unsigned int d_cleaninterval;
152 AtomicCounter *d_statnumhit;
153 AtomicCounter *d_statnummiss;
154 AtomicCounter *d_statnumentries;
158 static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
163 #endif /* PACKETCACHE_HH */