Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnsdist: Refactoring to merge the UDP and TCP paths #7526

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pdns/dnscrypt.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@

#ifndef HAVE_DNSCRYPT

/* let's just define a few types and values so that the rest of
the code can ignore whether DNSCrypt support is available */
#define DNSCRYPT_MAX_RESPONSE_PADDING_AND_MAC_SIZE (0)

class DNSCryptContext
{
};

class DNSCryptQuery
{
DNSCryptQuery(const std::shared_ptr<DNSCryptContext>& ctx): d_ctx(ctx)
{
}
private:
std::shared_ptr<DNSCryptContext> d_ctx{nullptr};
};

#else
#else /* HAVE_DNSCRYPT */

#include <memory>
#include <string>
Expand Down
38 changes: 31 additions & 7 deletions pdns/dnsdist-ecs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,7 @@ static int removeEDNSOptionFromOptions(unsigned char* optionsStart, const uint16

int removeEDNSOptionFromOPT(char* optStart, size_t* optLen, const uint16_t optionCodeToRemove)
{
/* we need at least:
root label (1), type (2), class (2), ttl (4) + rdlen (2)*/
if (*optLen < 11) {
if (*optLen < optRecordMinimumSize) {
return EINVAL;
}
const unsigned char* end = (const unsigned char*) optStart + *optLen;
Expand All @@ -490,15 +488,13 @@ int removeEDNSOptionFromOPT(char* optStart, size_t* optLen, const uint16_t optio

bool isEDNSOptionInOpt(const std::string& packet, const size_t optStart, const size_t optLen, const uint16_t optionCodeToFind, size_t* optContentStart, uint16_t* optContentLen)
{
/* we need at least:
root label (1), type (2), class (2), ttl (4) + rdlen (2)*/
if (optLen < 11) {
if (optLen < optRecordMinimumSize) {
return false;
}
size_t p = optStart + 9;
uint16_t rdLen = (0x100*packet.at(p) + packet.at(p+1));
p += sizeof(rdLen);
if (rdLen > (optLen - 11)) {
if (rdLen > (optLen - optRecordMinimumSize)) {
return false;
}

Expand Down Expand Up @@ -741,3 +737,31 @@ bool queryHasEDNS(const DNSQuestion& dq)

return false;
}

bool getEDNS0Record(const DNSQuestion& dq, EDNS0Record& edns0)
{
uint16_t optStart;
size_t optLen = 0;
bool last = false;
const char * packet = reinterpret_cast<const char*>(dq.dh);
std::string packetStr(packet, dq.len);
int res = locateEDNSOptRR(packetStr, &optStart, &optLen, &last);
if (res != 0) {
// no EDNS OPT RR
return false;
}

if (optLen < optRecordMinimumSize) {
return false;
}

if (optStart < dq.len && packetStr.at(optStart) != 0) {
// OPT RR Name != '.'
return false;
}

static_assert(sizeof(EDNS0Record) == sizeof(uint32_t), "sizeof(EDNS0Record) must match sizeof(uint32_t) AKA RR TTL size");
rgacogne marked this conversation as resolved.
Show resolved Hide resolved
// copy out 4-byte "ttl" (really the EDNS0 record), after root label (1) + type (2) + class (2).
memcpy(&edns0, packet + optStart + 5, sizeof edns0);
return true;
}
4 changes: 4 additions & 0 deletions pdns/dnsdist-ecs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
#pragma once

// root label (1), type (2), class (2), ttl (4) + rdlen (2)
static const size_t optRecordMinimumSize = 11;

extern size_t g_EdnsUDPPayloadSize;
extern uint16_t g_PayloadSizeSelfGenAnswers;

Expand All @@ -42,3 +45,4 @@ bool parseEDNSOptions(DNSQuestion& dq);

int getEDNSZ(const DNSQuestion& dq);
bool queryHasEDNS(const DNSQuestion& dq);
bool getEDNS0Record(const DNSQuestion& dq, EDNS0Record& edns0);
Loading