]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-cache.hh
Add suffixmatch option to expungeByName
[thirdparty/pdns.git] / pdns / dnsdist-cache.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 #pragma once
23
24 #include <atomic>
25 #include <unordered_map>
26 #include "lock.hh"
27
28 struct DNSQuestion;
29
30 class DNSDistPacketCache : boost::noncopyable
31 {
32 public:
33 DNSDistPacketCache(size_t maxEntries, uint32_t maxTTL=86400, uint32_t minTTL=0, uint32_t tempFailureTTL=60, uint32_t staleTTL=60);
34 ~DNSDistPacketCache();
35
36 void insert(uint32_t key, const DNSName& qname, uint16_t qtype, uint16_t qclass, const char* response, uint16_t responseLen, bool tcp, uint8_t rcode);
37 bool get(const DNSQuestion& dq, uint16_t consumed, uint16_t queryId, char* response, uint16_t* responseLen, uint32_t* keyOut, uint32_t allowExpired=0, bool skipAging=false);
38 void purgeExpired(size_t upTo=0);
39 void expunge(size_t upTo=0);
40 void expungeByName(const DNSName& name, uint16_t qtype=QType::ANY, bool suffixMatch=false);
41 bool isFull();
42 string toString();
43 uint64_t getSize() const { return d_map.size(); }
44 uint64_t getHits() const { return d_hits; }
45 uint64_t getMisses() const { return d_misses; }
46 uint64_t getDeferredLookups() const { return d_deferredLookups; }
47 uint64_t getDeferredInserts() const { return d_deferredInserts; }
48 uint64_t getLookupCollisions() const { return d_lookupCollisions; }
49 uint64_t getInsertCollisions() const { return d_insertCollisions; }
50 uint64_t getMaxEntries() const { return d_maxEntries; }
51 uint64_t getTTLTooShorts() const { return d_ttlTooShorts; }
52 uint64_t getEntriesCount();
53
54 static uint32_t getMinTTL(const char* packet, uint16_t length);
55
56 private:
57
58 struct CacheValue
59 {
60 time_t getTTD() const { return validity; }
61 std::string value;
62 DNSName qname;
63 uint16_t qtype{0};
64 uint16_t qclass{0};
65 time_t added{0};
66 time_t validity{0};
67 uint16_t len{0};
68 bool tcp{false};
69 };
70
71 static uint32_t getKey(const DNSName& qname, uint16_t consumed, const unsigned char* packet, uint16_t packetLen, bool tcp);
72 static bool cachedValueMatches(const CacheValue& cachedValue, const DNSName& qname, uint16_t qtype, uint16_t qclass, bool tcp);
73
74 pthread_rwlock_t d_lock;
75 std::unordered_map<uint32_t,CacheValue> d_map;
76 std::atomic<uint64_t> d_deferredLookups{0};
77 std::atomic<uint64_t> d_deferredInserts{0};
78 std::atomic<uint64_t> d_hits{0};
79 std::atomic<uint64_t> d_misses{0};
80 std::atomic<uint64_t> d_insertCollisions{0};
81 std::atomic<uint64_t> d_lookupCollisions{0};
82 std::atomic<uint64_t> d_ttlTooShorts{0};
83 size_t d_maxEntries;
84 uint32_t d_maxTTL;
85 uint32_t d_tempFailureTTL;
86 uint32_t d_minTTL;
87 uint32_t d_staleTTL;
88 };