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 "namespaces.hh"
30 #include <boost/multi_index_container.hpp>
31 #include <boost/multi_index/ordered_index.hpp>
32 #include <boost/multi_index/hashed_index.hpp>
33 #include <boost/tuple/tuple_comparison.hpp>
34 #include <boost/multi_index/sequenced_index.hpp>
39 #include "rec-protobuf.hh"
42 using namespace ::boost::multi_index;
44 //! Stores whole packets, ready for lobbing back at the client. Not threadsafe.
45 /* Note: we store answers as value AND KEY, and with careful work, we make sure that
46 you can use a query as a key too. But query and answer must compare as identical!
48 This precludes doing anything smart with EDNS directly from the packet */
49 class RecursorPacketCache
52 RecursorPacketCache();
53 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now, std::string* responsePacket, uint32_t* age);
54 void insertResponsePacket(unsigned int tag, const DNSName& qname, uint16_t qtype, const std::string& queryPacket, const std::string& responsePacket, time_t now, uint32_t ttd);
55 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now, std::string* responsePacket, uint32_t* age, RecProtoBufMessage* protobufMessage);
56 void insertResponsePacket(unsigned int tag, const DNSName& qname, uint16_t qtype, const std::string& queryPacket, const std::string& responsePacket, time_t now, uint32_t ttd, const RecProtoBufMessage* protobufMessage);
57 void doPruneTo(unsigned int maxSize=250000);
58 uint64_t doDump(int fd);
59 int doWipePacketCache(const DNSName& name, uint16_t qtype=0xffff, bool subtree=false);
62 uint64_t d_hits, d_misses;
71 mutable uint32_t d_ttd;
72 mutable uint32_t d_creation; // so we can 'age' our packets
75 mutable std::string d_packet; // "I know what I am doing"
77 mutable RecProtoBufMessage d_protobufMessage;
81 inline bool operator<(const struct Entry& rhs) const;
83 uint32_t getTTD() const
88 uint32_t canHashPacket(const std::string& origPacket);
89 typedef multi_index_container<
92 hashed_non_unique<tag<HashTag>, composite_key<Entry, member<Entry,uint32_t,&Entry::d_tag>, member<Entry,uint32_t,&Entry::d_qhash> > >,
94 ordered_non_unique<tag<NameTag>, member<Entry,DNSName,&Entry::d_name>, CanonDNSNameCompare >
98 packetCache_t d_packetCache;