Skip to content

Commit

Permalink
Smarter hashing of header and query
Browse files Browse the repository at this point in the history
  • Loading branch information
omoerbeek committed Jun 1, 2022
1 parent 1b5aa11 commit b2c3da4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pdns/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ pdns_server_SOURCES = \
backends/gsql/ssql.hh \
base32.cc base32.hh \
base64.cc base64.hh \
burtle.hh \
bind-dnssec.schema.sqlite3.sql.h \
bindlexer.l \
bindparser.cc \
burtle.hh \
cachecleaner.hh \
circular_buffer.hh \
comment.hh \
Expand Down
16 changes: 4 additions & 12 deletions pdns/packetcache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,19 @@ public:

static uint32_t hashHeaderAndQName(const std::string& packet, size_t& pos)
{
uint32_t currentHash = 0;
const size_t packetSize = packet.size();
assert(packetSize >= sizeof(dnsheader));
currentHash = burtle(reinterpret_cast<const unsigned char*>(&packet.at(2)), sizeof(dnsheader) - 2, currentHash); // rest of dnsheader, skip id
pos = sizeof(dnsheader);
uint32_t currentHash = burtle(reinterpret_cast<const unsigned char*>(&packet.at(2)), sizeof(dnsheader) - 2, 0); // rest of dnsheader, skip id

for (; pos < packetSize; ) {
for (pos = sizeof(dnsheader); pos < packetSize; ) {
const unsigned char labelLen = static_cast<unsigned char>(packet.at(pos));
currentHash = burtle(&labelLen, 1, currentHash);
++pos;
if (labelLen == 0) {
break;
}

for (size_t idx = 0; idx < labelLen && pos < packetSize; ++idx, ++pos) {
const unsigned char l = dns_tolower(packet.at(pos));
currentHash = burtle(&l, 1, currentHash);
}
pos = std::min(pos + labelLen, packetSize);
}

return currentHash;
return burtleCI(reinterpret_cast<const unsigned char*>(&packet.at(sizeof(dnsheader))), pos - sizeof(dnsheader), currentHash);
}

/* hash the packet from the beginning, including the qname. This skips:
Expand Down
8 changes: 4 additions & 4 deletions pdns/test-packetcache_hh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheAuthCollision) {
pw1.getHeader()->rd = true;
pw1.getHeader()->qr = false;
pw1.getHeader()->id = 0x42;
opt.source = Netmask("10.0.152.74/32");
opt.source = Netmask("10.0.59.220/32");
ednsOptions.clear();
ednsOptions.emplace_back(EDNSOptionCode::ECS, makeEDNSSubnetOptsString(opt));
pw1.addOpt(512, 0, 0, ednsOptions);
Expand All @@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheAuthCollision) {
pw2.getHeader()->rd = true;
pw2.getHeader()->qr = false;
pw2.getHeader()->id = 0x84;
opt.source = Netmask("10.2.70.250/32");
opt.source = Netmask("10.0.167.48/32");
ednsOptions.clear();
ednsOptions.emplace_back(EDNSOptionCode::ECS, makeEDNSSubnetOptsString(opt));
pw2.addOpt(512, 0, 0, ednsOptions);
Expand Down Expand Up @@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheAuthCollision) {
pw1.getHeader()->rd = true;
pw1.getHeader()->qr = false;
pw1.getHeader()->id = 0x42;
opt.source = Netmask("10.0.34.159/32");
opt.source = Netmask("10.0.41.6/32");
ednsOptions.clear();
ednsOptions.emplace_back(EDNSOptionCode::ECS, makeEDNSSubnetOptsString(opt));
pw1.addOpt(512, 0, EDNSOpts::DNSSECOK, ednsOptions);
Expand All @@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheAuthCollision) {
pw2.getHeader()->rd = true;
pw2.getHeader()->qr = false;
pw2.getHeader()->id = 0x84;
opt.source = Netmask("10.0.179.58/32");
opt.source = Netmask("10.0.119.79/32");
ednsOptions.clear();
ednsOptions.emplace_back(EDNSOptionCode::ECS, makeEDNSSubnetOptsString(opt));
/* no EDNSOpts::DNSSECOK !! */
Expand Down

0 comments on commit b2c3da4

Please sign in to comment.