]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/auth-packetcache.hh
Merge pull request #14118 from jap/patch-2
[thirdparty/pdns.git] / pdns / auth-packetcache.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 AUTH_PACKETCACHE_HH
23 #define AUTH_PACKETCACHE_HH
24
25 #include <string>
26 #include <map>
27 #include "dns.hh"
28 #include <boost/version.hpp>
29 #include "namespaces.hh"
30 using namespace ::boost::multi_index;
31
32 #include <boost/multi_index/hashed_index.hpp>
33
34 #include "dnspacket.hh"
35 #include "lock.hh"
36 #include "packetcache.hh"
37
38 /** This class performs 'whole packet caching'. Feed it a question packet and it will
39 try to find an answer. If you have an answer, insert it to have it cached for later use.
40 Take care not to replace existing cache entries. While this works, it is wasteful. Only
41 insert packets that where not found by get()
42
43 Locking!
44
45 The cache itself is protected by a read/write lock. Because deleting is a two step process, which
46 first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
47 */
48
49 class AuthPacketCache : public PacketCache
50 {
51 public:
52 AuthPacketCache(size_t mapsCount=1024);
53 ~AuthPacketCache();
54
55 void insert(DNSPacket *q, DNSPacket *r, uint32_t maxTTL); //!< We copy the contents of *p into our cache. Do not needlessly call this to insert questions already in the cache as it wastes resources
56
57 bool get(DNSPacket *p, DNSPacket *q); //!< We return a dynamically allocated copy out of our cache. You need to delete it. You also need to spoof in the right ID with the DNSPacket.spoofID() method.
58
59 void cleanup(); //!< force the cache to preen itself from expired packets
60 uint64_t purge();
61 uint64_t purge(const std::string& match); // could be $ terminated. Is not a dnsname!
62 uint64_t purgeExact(const DNSName& qname); // no wildcard matching here
63
64 uint64_t size() const { return *d_statnumentries; };
65
66 void setMaxEntries(uint64_t maxEntries)
67 {
68 d_maxEntries = maxEntries;
69 }
70 void setTTL(uint32_t ttl)
71 {
72 d_ttl = ttl;
73 }
74 private:
75
76 struct CacheEntry
77 {
78 mutable string query;
79 mutable string value;
80 DNSName qname;
81
82 mutable time_t created{0};
83 mutable time_t ttd{0};
84 uint32_t hash{0};
85 uint16_t qtype{0};
86 bool tcp{false};
87 };
88
89 struct HashTag{};
90 struct NameTag{};
91 struct SequenceTag{};
92 typedef multi_index_container<
93 CacheEntry,
94 indexed_by <
95 hashed_non_unique<tag<HashTag>, member<CacheEntry,uint32_t,&CacheEntry::hash> >,
96 ordered_non_unique<tag<NameTag>, member<CacheEntry,DNSName,&CacheEntry::qname>, CanonDNSNameCompare >,
97 sequenced<tag<SequenceTag>>
98 >
99 > cmap_t;
100
101 struct MapCombo
102 {
103 pthread_rwlock_t d_mut;
104 cmap_t d_map;
105 };
106
107 vector<MapCombo> d_maps;
108 MapCombo& getMap(const DNSName& name)
109 {
110 return d_maps[name.hash() % d_maps.size()];
111 }
112
113 static bool entryMatches(cmap_t::index<HashTag>::type::iterator& iter, const std::string& query, const DNSName& qname, uint16_t qtype, bool tcp);
114 bool getEntryLocked(cmap_t& map, const std::string& query, uint32_t hash, const DNSName &qname, uint16_t qtype, bool tcp, time_t now, string& entry);
115 void cleanupIfNeeded();
116
117 AtomicCounter d_ops{0};
118 AtomicCounter *d_statnumhit;
119 AtomicCounter *d_statnummiss;
120 AtomicCounter *d_statnumentries;
121
122 uint64_t d_maxEntries{0};
123 time_t d_lastclean; // doesn't need to be atomic
124 unsigned long d_nextclean{4096};
125 unsigned int d_cleaninterval{4096};
126 uint32_t d_ttl{0};
127 bool d_cleanskipped{false};
128
129 static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
130 };
131
132 #endif /* AUTH_PACKETCACHE_HH */