]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/auth-packetcache.hh
Merge pull request #8223 from PowerDNS/omoerbeek-patch-1
[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 bool enabled()
75 {
76 return (d_ttl > 0);
77 }
78 private:
79
80 struct CacheEntry
81 {
82 mutable string query;
83 mutable string value;
84 DNSName qname;
85
86 mutable time_t created{0};
87 mutable time_t ttd{0};
88 uint32_t hash{0};
89 uint16_t qtype{0};
90 bool tcp{false};
91 };
92
93 struct HashTag{};
94 struct NameTag{};
95 struct SequenceTag{};
96 typedef multi_index_container<
97 CacheEntry,
98 indexed_by <
99 hashed_non_unique<tag<HashTag>, member<CacheEntry,uint32_t,&CacheEntry::hash> >,
100 ordered_non_unique<tag<NameTag>, member<CacheEntry,DNSName,&CacheEntry::qname>, CanonDNSNameCompare >,
101 sequenced<tag<SequenceTag>>
102 >
103 > cmap_t;
104
105 struct MapCombo
106 {
107 pthread_rwlock_t d_mut;
108 cmap_t d_map;
109 };
110
111 vector<MapCombo> d_maps;
112 MapCombo& getMap(const DNSName& name)
113 {
114 return d_maps[name.hash() % d_maps.size()];
115 }
116
117 static bool entryMatches(cmap_t::index<HashTag>::type::iterator& iter, const std::string& query, const DNSName& qname, uint16_t qtype, bool tcp);
118 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);
119 void cleanupIfNeeded();
120
121 AtomicCounter d_ops{0};
122 AtomicCounter *d_statnumhit;
123 AtomicCounter *d_statnummiss;
124 AtomicCounter *d_statnumentries;
125
126 uint64_t d_maxEntries{0};
127 time_t d_lastclean; // doesn't need to be atomic
128 unsigned long d_nextclean{4096};
129 unsigned int d_cleaninterval{4096};
130 uint32_t d_ttl{0};
131 bool d_cleanskipped{false};
132
133 static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
134 };
135
136 #endif /* AUTH_PACKETCACHE_HH */