]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/packetcache.hh
Merge pull request #5062 from pieterlexis/issue-512-pdnsutil-validate-NS-MX
[thirdparty/pdns.git] / pdns / 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 PACKETCACHE_HH
23 #define PACKETCACHE_HH
24
25 #include <string>
26 #include <utility>
27 #include <map>
28 #include <map>
29 #include "dns.hh"
30 #include <boost/version.hpp>
31 #include "namespaces.hh"
32 using namespace ::boost::multi_index;
33
34 #include "namespaces.hh"
35 #include <boost/multi_index/hashed_index.hpp>
36 #include "dnspacket.hh"
37 #include "lock.hh"
38 #include "statbag.hh"
39
40 /** This class performs 'whole packet caching'. Feed it a question packet and it will
41 try to find an answer. If you have an answer, insert it to have it cached for later use.
42 Take care not to replace existing cache entries. While this works, it is wasteful. Only
43 insert packets that where not found by get()
44
45 Locking!
46
47 The cache itself is protected by a read/write lock. Because deleting is a two step process, which
48 first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
49 */
50
51 class PacketCache : public boost::noncopyable
52 {
53 public:
54 PacketCache();
55 ~PacketCache();
56 enum CacheEntryType { PACKETCACHE, QUERYCACHE};
57
58 void insert(DNSPacket *q, DNSPacket *r, unsigned int maxttl=UINT_MAX); //!< 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
59
60 void insert(const DNSName &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1,
61 unsigned int maxReplyLen=512, bool dnssecOk=false, bool EDNS=false);
62
63 void insert(const DNSName &qname, const QType& qtype, CacheEntryType cet, const vector<DNSZoneRecord>& content, unsigned int ttl, int zoneID=-1);
64
65 int 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.
66 bool getEntry(const DNSName &qname, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
67 unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false);
68 bool getEntry(const DNSName &qname, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
69
70
71 int size() { return *d_statnumentries; } //!< number of entries in the cache
72 void cleanupIfNeeded();
73 void cleanup(); //!< force the cache to preen itself from expired packets
74 int purge();
75 int purge(const std::string& match); // could be $ terminated. Is not a dnsname!
76 int purgeExact(const DNSName& qname); // no wildcard matching here
77
78 map<char,int> getCounts();
79 private:
80 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
81 unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false);
82 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
83
84
85 struct CacheEntry
86 {
87 CacheEntry() { qtype = ctype = 0; zoneID = -1; dnssecOk=false; hasEDNS=false; created=0; ttd=0; maxReplyLen=512;}
88
89 DNSName qname;
90 string value;
91 vector<DNSZoneRecord> drs;
92 time_t created;
93 time_t ttd;
94
95 uint16_t qtype;
96 uint16_t ctype;
97 int zoneID;
98 unsigned int maxReplyLen;
99
100 bool dnssecOk;
101 bool hasEDNS;
102 };
103
104 void getTTLS();
105
106 struct UnorderedNameTag{};
107 struct SequenceTag{};
108 typedef multi_index_container<
109 CacheEntry,
110 indexed_by <
111 ordered_unique<
112 composite_key<
113 CacheEntry,
114 member<CacheEntry,DNSName,&CacheEntry::qname>,
115 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
116 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
117 member<CacheEntry,int, &CacheEntry::zoneID>,
118 member<CacheEntry,unsigned int, &CacheEntry::maxReplyLen>,
119 member<CacheEntry,bool, &CacheEntry::dnssecOk>,
120 member<CacheEntry,bool, &CacheEntry::hasEDNS>
121 >,
122 composite_key_compare<CanonDNSNameCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>,
123 std::less<unsigned int>, std::less<bool>, std::less<bool> >
124 >,
125 hashed_non_unique<tag<UnorderedNameTag>, composite_key<CacheEntry,
126 member<CacheEntry,DNSName,&CacheEntry::qname>,
127 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
128 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
129 member<CacheEntry,int, &CacheEntry::zoneID> > > ,
130 sequenced<tag<SequenceTag>>
131 >
132 > cmap_t;
133
134
135 struct MapCombo
136 {
137 pthread_rwlock_t d_mut;
138 cmap_t d_map;
139 };
140
141 vector<MapCombo> d_maps;
142 MapCombo& getMap(const DNSName& qname)
143 {
144 return d_maps[qname.hash() % d_maps.size()];
145 }
146
147 AtomicCounter d_ops;
148 time_t d_lastclean; // doesn't need to be atomic
149 unsigned long d_nextclean;
150 unsigned int d_cleaninterval;
151 bool d_cleanskipped;
152 AtomicCounter *d_statnumhit;
153 AtomicCounter *d_statnummiss;
154 AtomicCounter *d_statnumentries;
155
156 int d_ttl;
157
158 static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
159 };
160
161
162
163 #endif /* PACKETCACHE_HH */