| 1 | #ifndef PDNS_RECPACKETCACHE_HH |
|---|
| 2 | #define PDNS_RECPACKETCACHE_HH |
|---|
| 3 | #include <string> |
|---|
| 4 | #include <set> |
|---|
| 5 | #include <inttypes.h> |
|---|
| 6 | #include "dns.hh" |
|---|
| 7 | #include "namespaces.hh" |
|---|
| 8 | |
|---|
| 9 | class RecursorPacketCache |
|---|
| 10 | { |
|---|
| 11 | public: |
|---|
| 12 | RecursorPacketCache(); |
|---|
| 13 | bool getResponsePacket(const std::string& queryPacket, time_t now, std::string* responsePacket); |
|---|
| 14 | void insertResponsePacket(const std::string& responsePacket, time_t now, uint32_t ttd); |
|---|
| 15 | |
|---|
| 16 | void prune(); |
|---|
| 17 | private: |
|---|
| 18 | |
|---|
| 19 | struct Entry |
|---|
| 20 | { |
|---|
| 21 | mutable uint32_t d_ttd; |
|---|
| 22 | mutable std::string d_packet; // "I know what I am doing" |
|---|
| 23 | |
|---|
| 24 | inline bool operator<(const struct Entry& rhs) const; |
|---|
| 25 | }; |
|---|
| 26 | typedef std::set<struct Entry> packetCache_t; |
|---|
| 27 | packetCache_t d_packetCache; |
|---|
| 28 | pthread_rwlock_t d_rwlock; |
|---|
| 29 | }; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | // needs to take into account: qname, qtype, opcode, rd, qdcount, EDNS size |
|---|
| 33 | inline bool RecursorPacketCache::Entry::operator<(const struct RecursorPacketCache::Entry &rhs) const |
|---|
| 34 | { |
|---|
| 35 | const struct dnsheader* |
|---|
| 36 | dh=(const struct dnsheader*) d_packet.c_str(), |
|---|
| 37 | *rhsdh=(const struct dnsheader*)rhs.d_packet.c_str(); |
|---|
| 38 | if(make_tuple(dh->opcode, dh->rd, dh->qdcount) < |
|---|
| 39 | make_tuple(rhsdh->opcode, rhsdh->rd, rhsdh->qdcount)) |
|---|
| 40 | return true; |
|---|
| 41 | |
|---|
| 42 | if((d_packet.size() > 13 && rhs.d_packet.size() > 13) && |
|---|
| 43 | (d_packet[12] && rhs.d_packet[12]) && |
|---|
| 44 | (d_packet[13] < rhs.d_packet[13])) |
|---|
| 45 | return true; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | uint16_t qtype, rhsqtype; |
|---|
| 49 | string qname=questionExpand(d_packet.c_str(), d_packet.length(), qtype); |
|---|
| 50 | string rhsqname=questionExpand(rhs.d_packet.c_str(), rhs.d_packet.length(), rhsqtype); |
|---|
| 51 | |
|---|
| 52 | return tie(qtype, qname) < tie(rhsqtype, rhsqname); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | #endif |
|---|