]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recpacketcache.hh
Allocate DNSRecord objects as smart pointers right away
[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
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include "rec-protobuf.hh"
29
30 #include <string>
31 #include <set>
32 #include <inttypes.h>
33 #include "dns.hh"
34 #include "namespaces.hh"
35 #include <iostream>
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>
41
42 #include "packetcache.hh"
43
44
45
46 using namespace ::boost::multi_index;
47
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!
51
52 This precludes doing anything smart with EDNS directly from the packet */
53 class RecursorPacketCache: public PacketCache
54 {
55 public:
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);
66
67 void prune();
68 uint64_t d_hits, d_misses;
69 uint64_t size();
70 uint64_t bytes();
71
72 private:
73 struct HashTag {};
74 struct NameTag {};
75 struct Entry
76 {
77 mutable time_t d_ttd;
78 mutable time_t d_creation; // so we can 'age' our packets
79 DNSName d_name;
80 uint16_t d_type;
81 uint16_t d_class;
82 mutable std::string d_packet; // "I know what I am doing"
83 #ifdef HAVE_PROTOBUF
84 mutable RecProtoBufMessage d_protobufMessage;
85 #endif
86 uint32_t d_qhash;
87 uint32_t d_tag;
88 inline bool operator<(const struct Entry& rhs) const;
89
90 time_t getTTD() const
91 {
92 return d_ttd;
93 }
94 };
95
96 typedef multi_index_container<
97 Entry,
98 indexed_by <
99 hashed_non_unique<tag<HashTag>, composite_key<Entry, member<Entry,uint32_t,&Entry::d_tag>, member<Entry,uint32_t,&Entry::d_qhash> > >,
100 sequenced<> ,
101 ordered_non_unique<tag<NameTag>, member<Entry,DNSName,&Entry::d_name>, CanonDNSNameCompare >
102 >
103 > packetCache_t;
104
105 packetCache_t d_packetCache;
106
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);
108
109 public:
110 void preRemoval(const Entry& entry)
111 {
112 }
113 };
114
115 #endif