]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/recpacketcache.cc
we're going to do a 3.3.1 - this isn't it yet, but there won't be an RC
[thirdparty/pdns.git] / pdns / recpacketcache.cc
CommitLineData
3ea54bf0
BH
1#include <iostream>
2#include "recpacketcache.hh"
38c9ceaa 3#include "cachecleaner.hh"
3ea54bf0
BH
4#include "dns.hh"
5#include "namespaces.hh"
6#include "lock.hh"
7
8
9RecursorPacketCache::RecursorPacketCache()
10{
16beeaa4 11 d_hits = d_misses = 0;
3ea54bf0
BH
12}
13
2c73e580
BH
14bool RecursorPacketCache::getResponsePacket(const std::string& queryPacket, time_t now,
15 std::string* responsePacket, uint32_t* age)
3ea54bf0 16{
3ea54bf0
BH
17 struct Entry e;
18 e.d_packet=queryPacket;
19
20 packetCache_t::const_iterator iter = d_packetCache.find(e);
3f3459f0
BH
21
22 if(iter == d_packetCache.end()) {
23 d_misses++;
24 return false;
25 }
3f3459f0
BH
26
27 if((uint32_t)now < iter->d_ttd) { // it is fresh!
2c73e580
BH
28// cerr<<"Fresh for another "<<iter->d_ttd - now<<" seconds!"<<endl;
29 *age = now - iter->d_creation;
3ea54bf0
BH
30 uint16_t id = ((struct dnsheader*)queryPacket.c_str())->id;
31 *responsePacket = iter->d_packet;
32 ((struct dnsheader*)responsePacket->c_str())->id=id;
16beeaa4 33 d_hits++;
c2567ad1 34 moveCacheItemToBack(d_packetCache, iter);
3f3459f0 35
3ea54bf0
BH
36 return true;
37 }
c2567ad1 38 moveCacheItemToFront(d_packetCache, iter);
16beeaa4 39 d_misses++;
3ea54bf0
BH
40 return false;
41}
42
43void RecursorPacketCache::insertResponsePacket(const std::string& responsePacket, time_t now, uint32_t ttl)
44{
3ea54bf0
BH
45 struct Entry e;
46 e.d_packet = responsePacket;
47 e.d_ttd = now+ttl;
2c73e580 48 e.d_creation = now;
3ea54bf0 49 packetCache_t::iterator iter = d_packetCache.find(e);
3f3459f0 50
3ea54bf0
BH
51 if(iter != d_packetCache.end()) {
52 iter->d_packet = responsePacket;
3f3459f0 53 iter->d_ttd = now + ttl;
2c73e580 54 iter->d_creation = now;
3ea54bf0
BH
55 }
56 else
57 d_packetCache.insert(e);
58}
59
16beeaa4
BH
60uint64_t RecursorPacketCache::size()
61{
62 return d_packetCache.size();
63}
3f3459f0 64
3f3459f0
BH
65void RecursorPacketCache::doPruneTo(unsigned int maxCached)
66{
38c9ceaa 67 pruneCollection(d_packetCache, maxCached);
3f3459f0
BH
68}
69