2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
25 #include <unordered_map>
30 class DNSDistPacketCache : boost::noncopyable
33 DNSDistPacketCache(size_t maxEntries, uint32_t maxTTL=86400, uint32_t minTTL=0, uint32_t tempFailureTTL=60, uint32_t staleTTL=60);
34 ~DNSDistPacketCache();
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);
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();
54 static uint32_t getMinTTL(const char* packet, uint16_t length);
60 time_t getTTD() const { return validity; }
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);
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};
85 uint32_t d_tempFailureTTL;