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 PACKETCACHE_HH
23 #define PACKETCACHE_HH
25 #include "ednsoptions.hh"
29 class PacketCache : public boost::noncopyable
32 static uint32_t canHashPacket(const std::string& packet, bool skipECS=true)
35 ret=burtle((const unsigned char*)packet.c_str() + 2, 10, ret); // rest of dnsheader, skip id
36 size_t packetSize = packet.size();
38 const char* end = packet.c_str() + packetSize;
39 const char* p = packet.c_str() + pos;
41 for(; p < end && *p; ++p) { // XXX if you embed a 0 in your qname we'll stop lowercasing there
42 const unsigned char l = dns_tolower(*p); // label lengths can safely be lower cased
43 ret=burtle(&l, 1, ret);
44 } // XXX the embedded 0 in the qname will break the subnet stripping
46 struct dnsheader* dh = (struct dnsheader*)packet.c_str();
47 const char* skipBegin = p;
48 const char* skipEnd = p;
49 /* we need at least 1 (final empty label) + 2 (QTYPE) + 2 (QCLASS)
50 + OPT root label (1), type (2), class (2) and ttl (4)
51 + the OPT RR rdlen (2)
54 if(skipECS && ntohs(dh->arcount)==1 && (pos+16) < packetSize) {
55 char* optionBegin = nullptr;
57 /* skip the final empty label (1), the qtype (2), qclass (2) */
58 /* root label (1), type (2), class (2) and ttl (4) */
59 int res = getEDNSOption((char*) p + 14, end - (p + 14), EDNSOptionCode::ECS, &optionBegin, &optionLen);
61 skipBegin = optionBegin;
62 skipEnd = optionBegin + optionLen;
66 // cerr << "Hashing from " << (p-packet.c_str()) << " for " << skipBegin-p << "bytes, end is at "<< end-packet.c_str() << endl;
67 ret = burtle((const unsigned char*)p, skipBegin-p, ret);
70 // cerr << "Hashing from " << (skipEnd-packet.c_str()) << " for " << end-skipEnd << "bytes, end is at " << end-packet.c_str() << endl;
71 ret = burtle((const unsigned char*) skipEnd, end-skipEnd, ret);
78 #endif /* PACKETCACHE_HH */