]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recpacketcache.hh
dnsbulktest: Explicitely check that find() returned 0
[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 <inttypes.h>
26 #include "dns.hh"
27 #include "namespaces.hh"
28 #include <iostream>
29 #include <boost/multi_index_container.hpp>
30 #include <boost/multi_index/ordered_index.hpp>
31 #include <boost/multi_index/hashed_index.hpp>
32 #include <boost/tuple/tuple_comparison.hpp>
33 #include <boost/multi_index/sequenced_index.hpp>
34
35 #include "packetcache.hh"
36 #include "validate.hh"
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 #include "rec-protobuf.hh"
42
43
44 using namespace ::boost::multi_index;
45
46 //! Stores whole packets, ready for lobbing back at the client. Not threadsafe.
47 /* Note: we store answers as value AND KEY, and with careful work, we make sure that
48 you can use a query as a key too. But query and answer must compare as identical!
49
50 This precludes doing anything smart with EDNS directly from the packet */
51 class RecursorPacketCache: public PacketCache
52 {
53 public:
54 RecursorPacketCache();
55 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now, std::string* responsePacket, uint32_t* age, uint32_t* qhash);
56 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);
57 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, vState* valState, uint32_t* qhash, uint16_t* ecsBegin, uint16_t* ecsEnd, RecProtoBufMessage* protobufMessage);
58 bool getResponsePacket(unsigned int tag, const std::string& queryPacket, DNSName& qname, uint16_t* qtype, uint16_t* qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, uint16_t* ecsBegin, uint16_t* ecsEnd, RecProtoBufMessage* protobufMessage);
59 void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, uint16_t ecsBegin, uint16_t ecsEnd, boost::optional<RecProtoBufMessage>&& protobufMessage);
60 void doPruneTo(unsigned int maxSize=250000);
61 uint64_t doDump(int fd);
62 int doWipePacketCache(const DNSName& name, uint16_t qtype=0xffff, bool subtree=false);
63
64 void prune();
65 uint64_t d_hits, d_misses;
66 uint64_t size();
67 uint64_t bytes();
68
69 private:
70 struct HashTag {};
71 struct NameTag {};
72 struct Entry
73 {
74 Entry(const DNSName& qname, std::string&& packet, std::string&& query): d_name(qname), d_packet(std::move(packet)), d_query(std::move(query))
75 {
76 }
77
78 DNSName d_name;
79 mutable std::string d_packet; // "I know what I am doing"
80 mutable std::string d_query;
81 #ifdef HAVE_PROTOBUF
82 mutable boost::optional<RecProtoBufMessage> d_protobufMessage;
83 #endif
84 mutable time_t d_ttd;
85 mutable time_t d_creation; // so we can 'age' our packets
86 uint32_t d_qhash;
87 uint32_t d_tag;
88 uint16_t d_type;
89 uint16_t d_class;
90 mutable uint16_t d_ecsBegin;
91 mutable uint16_t d_ecsEnd;
92 mutable vState d_vstate;
93 inline bool operator<(const struct Entry& rhs) const;
94
95 time_t getTTD() const
96 {
97 return d_ttd;
98 }
99 };
100
101 typedef multi_index_container<
102 Entry,
103 indexed_by <
104 hashed_non_unique<tag<HashTag>, composite_key<Entry, member<Entry,uint32_t,&Entry::d_tag>, member<Entry,uint32_t,&Entry::d_qhash> > >,
105 sequenced<> ,
106 ordered_non_unique<tag<NameTag>, member<Entry,DNSName,&Entry::d_name>, CanonDNSNameCompare >
107 >
108 > packetCache_t;
109
110 packetCache_t d_packetCache;
111
112 static bool qrMatch(const packetCache_t::index<HashTag>::type::iterator& iter, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t ecsBegin, uint16_t ecsEnd);
113 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, vState* valState, RecProtoBufMessage* protobufMessage, uint16_t ecsBegin, uint16_t ecsEnd);
114
115 public:
116 void preRemoval(const Entry& entry)
117 {
118 }
119 };
120
121 #endif