1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef PACKETCACHE_HH
#define PACKETCACHE_HH
#include "ednsoptions.hh"
#include "misc.hh"
#include "iputils.hh"
class PacketCache : public boost::noncopyable
{
public:
static uint32_t canHashPacket(const std::string& packet, uint16_t* ecsBegin, uint16_t* ecsEnd)
{
uint32_t ret = 0;
ret = burtle(reinterpret_cast<const unsigned char*>(packet.c_str()) + 2, sizeof(dnsheader) - 2, ret); // rest of dnsheader, skip id
size_t packetSize = packet.size();
size_t pos = sizeof(dnsheader);
const char* end = packet.c_str() + packetSize;
const char* p = packet.c_str() + pos;
for(; p < end && *p; ++p, ++pos) { // XXX if you embed a 0 in your qname we'll stop lowercasing there
const unsigned char l = dns_tolower(*p); // label lengths can safely be lower cased
ret=burtle(&l, 1, ret);
} // XXX the embedded 0 in the qname will break the subnet stripping
const struct dnsheader* dh = reinterpret_cast<const struct dnsheader*>(packet.c_str());
const char* skipBegin = p;
const char* skipEnd = p;
if (ecsBegin != nullptr && ecsEnd != nullptr) {
*ecsBegin = 0;
*ecsEnd = 0;
}
/* we need at least 1 (final empty label) + 2 (QTYPE) + 2 (QCLASS)
+ OPT root label (1), type (2), class (2) and ttl (4)
+ the OPT RR rdlen (2)
= 16
*/
if(ntohs(dh->arcount)==1 && (pos+16) < packetSize) {
char* optionBegin = nullptr;
size_t optionLen = 0;
/* skip the final empty label (1), the qtype (2), qclass (2) */
/* root label (1), type (2), class (2) and ttl (4) */
int res = getEDNSOption(const_cast<char*>(reinterpret_cast<const char*>(p)) + 14, end - (p + 14), EDNSOptionCode::ECS, &optionBegin, &optionLen);
if (res == 0) {
skipBegin = optionBegin;
skipEnd = optionBegin + optionLen;
if (ecsBegin != nullptr && ecsEnd != nullptr) {
*ecsBegin = optionBegin - packet.c_str();
*ecsEnd = *ecsBegin + optionLen;
}
}
}
if (skipBegin > p) {
ret = burtle(reinterpret_cast<const unsigned char*>(p), skipBegin-p, ret);
}
if (skipEnd < end) {
ret = burtle(reinterpret_cast<const unsigned char*>(skipEnd), end-skipEnd, ret);
}
return ret;
}
static uint32_t canHashPacket(const std::string& packet)
{
uint32_t ret = 0;
ret = burtle(reinterpret_cast<const unsigned char*>(packet.c_str()) + 2, sizeof(dnsheader) - 2, ret); // rest of dnsheader, skip id
size_t packetSize = packet.size();
size_t pos = sizeof(dnsheader);
const char* end = packet.c_str() + packetSize;
const char* p = packet.c_str() + pos;
for(; p < end && *p; ++p) { // XXX if you embed a 0 in your qname we'll stop lowercasing there
const unsigned char l = dns_tolower(*p); // label lengths can safely be lower cased
ret=burtle(&l, 1, ret);
} // XXX the embedded 0 in the qname will break the subnet stripping
if (p < end) {
ret = burtle(reinterpret_cast<const unsigned char*>(p), end-p, ret);
}
return ret;
}
static bool queryHeaderMatches(const std::string& cachedQuery, const std::string& query)
{
if (cachedQuery.size() != query.size()) {
return false;
}
return (cachedQuery.compare(/* skip the ID */ 2, sizeof(dnsheader) - 2, query, 2, sizeof(dnsheader) - 2) == 0);
}
static bool queryMatches(const std::string& cachedQuery, const std::string& query, const DNSName& qname)
{
if (!queryHeaderMatches(cachedQuery, query)) {
return false;
}
size_t pos = sizeof(dnsheader) + qname.wirelength();
return (cachedQuery.compare(pos, cachedQuery.size() - pos, query, pos, query.size() - pos) == 0);
}
static bool queryMatches(const std::string& cachedQuery, const std::string& query, const DNSName& qname, uint16_t ecsBegin, uint16_t ecsEnd)
{
if (!queryHeaderMatches(cachedQuery, query)) {
return false;
}
size_t pos = sizeof(dnsheader) + qname.wirelength();
if (ecsBegin != 0 && ecsBegin >= pos && ecsEnd > ecsBegin) {
if (cachedQuery.compare(pos, ecsBegin - pos, query, pos, ecsBegin - pos) != 0) {
return false;
}
if (cachedQuery.compare(ecsEnd, cachedQuery.size() - ecsEnd, query, ecsEnd, query.size() - ecsEnd) != 0) {
return false;
}
}
else {
if (cachedQuery.compare(pos, cachedQuery.size() - pos, query, pos, query.size() - pos) != 0) {
return false;
}
}
return true;
}
};
#endif /* PACKETCACHE_HH */
|