]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recpacketcache.hh
Standardize license text in all PDNS files
[thirdparty/pdns.git] / pdns / recpacketcache.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
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
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifndef PDNS_RECPACKETCACHE_HH
23 #define PDNS_RECPACKETCACHE_HH
24 #include <string>
25 #include <set>
26 #include <inttypes.h>
27 #include "dns.hh"
28 #include "namespaces.hh"
29 #include <iostream>
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>
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include "rec-protobuf.hh"
40
41
42 using namespace ::boost::multi_index;
43
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!
47
48 This precludes doing anything smart with EDNS directly from the packet */
49 class RecursorPacketCache
50 {
51 public:
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);
60
61 void prune();
62 uint64_t d_hits, d_misses;
63 uint64_t size();
64 uint64_t bytes();
65
66 private:
67 struct HashTag {};
68 struct NameTag {};
69 struct Entry
70 {
71 mutable uint32_t d_ttd;
72 mutable uint32_t d_creation; // so we can 'age' our packets
73 DNSName d_name;
74 uint16_t d_type;
75 mutable std::string d_packet; // "I know what I am doing"
76 #ifdef HAVE_PROTOBUF
77 mutable RecProtoBufMessage d_protobufMessage;
78 #endif
79 uint32_t d_qhash;
80 uint32_t d_tag;
81 inline bool operator<(const struct Entry& rhs) const;
82
83 uint32_t getTTD() const
84 {
85 return d_ttd;
86 }
87 };
88 uint32_t canHashPacket(const std::string& origPacket);
89 typedef multi_index_container<
90 Entry,
91 indexed_by <
92 hashed_non_unique<tag<HashTag>, composite_key<Entry, member<Entry,uint32_t,&Entry::d_tag>, member<Entry,uint32_t,&Entry::d_qhash> > >,
93 sequenced<> ,
94 ordered_non_unique<tag<NameTag>, member<Entry,DNSName,&Entry::d_name>, CanonDNSNameCompare >
95 >
96 > packetCache_t;
97
98 packetCache_t d_packetCache;
99 };
100
101 #endif