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.
22 #ifndef AUTH_PACKETCACHE_HH
23 #define AUTH_PACKETCACHE_HH
28 #include <boost/version.hpp>
29 #include "namespaces.hh"
30 using namespace ::boost::multi_index;
32 #include <boost/multi_index/hashed_index.hpp>
34 #include "dnspacket.hh"
36 #include "packetcache.hh"
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()
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.
49 class AuthPacketCache : public PacketCache
52 AuthPacketCache(size_t mapsCount=1024);
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
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.
59 void cleanup(); //!< force the cache to preen itself from expired packets
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
64 uint64_t size() const { return *d_statnumentries; };
66 void setMaxEntries(uint64_t maxEntries)
68 d_maxEntries = maxEntries;
70 void setTTL(uint32_t ttl)
82 mutable time_t created{0};
83 mutable time_t ttd{0};
92 typedef multi_index_container<
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>>
103 pthread_rwlock_t d_mut;
107 vector<MapCombo> d_maps;
108 MapCombo& getMap(const DNSName& name)
110 return d_maps[name.hash() % d_maps.size()];
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();
117 AtomicCounter d_ops{0};
118 AtomicCounter *d_statnumhit;
119 AtomicCounter *d_statnummiss;
120 AtomicCounter *d_statnumentries;
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};
127 bool d_cleanskipped{false};
129 static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
132 #endif /* AUTH_PACKETCACHE_HH */