]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/packetcache.hh
b0785fd9d96dfbf8c5b9bd585bb8faef5ffc9a2a
[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 "ednsoptions.hh"
26 #include "misc.hh"
27 #include "iputils.hh"
28
29 class PacketCache : public boost::noncopyable
30 {
31 protected:
32 static uint32_t canHashPacket(const std::string& packet, bool skipECS=true)
33 {
34 uint32_t ret = 0;
35 ret=burtle((const unsigned char*)packet.c_str() + 2, 10, ret); // rest of dnsheader, skip id
36 size_t packetSize = packet.size();
37 size_t pos = 12;
38 const char* end = packet.c_str() + packetSize;
39 const char* p = packet.c_str() + pos;
40
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
45
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)
52 = 16
53 */
54 if(skipECS && ntohs(dh->arcount)==1 && (pos+16) < packetSize) {
55 char* optionBegin = nullptr;
56 size_t optionLen = 0;
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);
60 if (res == 0) {
61 skipBegin = optionBegin;
62 skipEnd = optionBegin + optionLen;
63 }
64 }
65 if (skipBegin > p) {
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);
68 }
69 if (skipEnd < end) {
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);
72 }
73
74 return ret;
75 }
76 };
77
78 #endif /* PACKETCACHE_HH */