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 PDNS_RECPACKETCACHE_HH
23 #define PDNS_RECPACKETCACHE_HH
28 #include "rec-protobuf.hh"
34 #include "namespaces.hh"
36 #include <boost/multi_index_container.hpp>
37 #include <boost/multi_index/ordered_index.hpp>
38 #include <boost/multi_index/hashed_index.hpp>
39 #include <boost/tuple/tuple_comparison.hpp>
40 #include <boost/multi_index/sequenced_index.hpp>
42 #include "packetcache.hh"
46 using namespace ::boost::multi_index;
48 //! Stores whole packets, ready for lobbing back at the client. Not threadsafe.
49 /* Note: we store answers as value AND KEY, and with careful work, we make sure that
50 you can use a query as a key too. But query and answer must compare as identical!
52 This precludes doing anything smart with EDNS directly from the packet */
53 class RecursorPacketCache: public PacketCache
56 RecursorPacketCache();
57 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now, std::string* responsePacket, uint32_t* age, uint32_t* qhash);
58 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now, std::string* responsePacket, uint32_t* age, uint32_t* qhash, RecProtoBufMessage* protobufMessage);
59 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, uint32_t* qhash);
60 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, uint32_t* qhash, RecProtoBufMessage* protobufMessage);
61 void insertResponsePacket(unsigned int tag, uint32_t qhash, const DNSName& qname, uint16_t qtype, uint16_t qclass, const std::string& responsePacket, time_t now, uint32_t ttl);
62 void insertResponsePacket(unsigned int tag, uint32_t qhash, const DNSName& qname, uint16_t qtype, uint16_t qclass, const std::string& responsePacket, time_t now, uint32_t ttl, const RecProtoBufMessage* protobufMessage);
63 void doPruneTo(unsigned int maxSize=250000);
64 uint64_t doDump(int fd);
65 int doWipePacketCache(const DNSName& name, uint16_t qtype=0xffff, bool subtree=false);
68 uint64_t d_hits, d_misses;
78 mutable time_t d_creation; // so we can 'age' our packets
82 mutable std::string d_packet; // "I know what I am doing"
84 mutable RecProtoBufMessage d_protobufMessage;
88 inline bool operator<(const struct Entry& rhs) const;
96 typedef multi_index_container<
99 hashed_non_unique<tag<HashTag>, composite_key<Entry, member<Entry,uint32_t,&Entry::d_tag>, member<Entry,uint32_t,&Entry::d_qhash> > >,
101 ordered_non_unique<tag<NameTag>, member<Entry,DNSName,&Entry::d_name>, CanonDNSNameCompare >
105 packetCache_t d_packetCache;
107 bool checkResponseMatches(std::pair<packetCache_t::index<HashTag>::type::iterator, packetCache_t::index<HashTag>::type::iterator> range, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, RecProtoBufMessage* protobufMessage);
110 void preRemoval(const Entry& entry)