Skip to content

Commit

Permalink
dnsdist: Reduce the dependency on dnsdist::configuration.hh
Browse files Browse the repository at this point in the history
Getting rid of the ugly DNSQuestion stubs in the meantime.
  • Loading branch information
rgacogne committed Jul 5, 2024
1 parent 30b06ae commit d0e851b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 54 deletions.
3 changes: 3 additions & 0 deletions pdns/dnsdistdist/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ dnsdist_SOURCES = \
dnsdist-discovery.cc dnsdist-discovery.hh \
dnsdist-dnscrypt.cc \
dnsdist-dnsparser.cc dnsdist-dnsparser.hh \
dnsdist-dnsquestion.cc \
dnsdist-doh-common.cc dnsdist-doh-common.hh \
dnsdist-downstream-connection.hh \
dnsdist-dynblocks.cc dnsdist-dynblocks.hh \
Expand Down Expand Up @@ -280,6 +281,7 @@ testrunner_SOURCES = \
dnsdist-configuration.cc dnsdist-configuration.hh \
dnsdist-crypto.cc dnsdist-crypto.hh \
dnsdist-dnsparser.cc dnsdist-dnsparser.hh \
dnsdist-dnsquestion.cc \
dnsdist-doh-common.cc dnsdist-doh-common.hh \
dnsdist-downstream-connection.hh \
dnsdist-dynblocks.cc dnsdist-dynblocks.hh \
Expand Down Expand Up @@ -554,6 +556,7 @@ fuzz_target_dnsdistcache_SOURCES = \
dnsdist-cache.cc dnsdist-cache.hh \
dnsdist-configuration.cc dnsdist-configuration.hh \
dnsdist-dnsparser.cc dnsdist-dnsparser.hh \
dnsdist-dnsquestion.cc \
dnsdist-ecs.cc dnsdist-ecs.hh \
dnsdist-idstate.hh \
dnsdist-protocols.cc dnsdist-protocols.hh \
Expand Down
60 changes: 60 additions & 0 deletions pdns/dnsdistdist/dnsdist-dnsquestion.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
*/
#include "dnsdist.hh"
#include "dnsdist-dnsparser.hh"

std::string DNSQuestion::getTrailingData() const
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto* message = reinterpret_cast<const char*>(this->getData().data());
const uint16_t messageLen = getDNSPacketLength(message, this->getData().size());
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
return {message + messageLen, this->getData().size() - messageLen};
}

bool DNSQuestion::setTrailingData(const std::string& tail)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const char* message = reinterpret_cast<const char*>(this->data.data());
const uint16_t messageLen = getDNSPacketLength(message, this->data.size());
this->data.resize(messageLen);
if (!tail.empty()) {
if (!hasRoomFor(tail.size())) {
return false;
}
this->data.insert(this->data.end(), tail.begin(), tail.end());
}
return true;
}

bool DNSQuestion::editHeader(const std::function<bool(dnsheader&)>& editFunction)
{
if (data.size() < sizeof(dnsheader)) {
throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer");
}
return dnsdist::PacketMangling::editDNSHeaderFromPacket(data, editFunction);
}

DNSQuestion::DNSQuestion(InternalQueryState& ids_, PacketBuffer& data_) :
data(data_), ids(ids_), ecsPrefixLength(ids.origRemote.sin4.sin_family == AF_INET ? dnsdist::configuration::getCurrentRuntimeConfiguration().d_ECSSourcePrefixV4 : dnsdist::configuration::getCurrentRuntimeConfiguration().d_ECSSourcePrefixV6), ecsOverride(dnsdist::configuration::getCurrentRuntimeConfiguration().d_ecsOverride)
{
}
34 changes: 0 additions & 34 deletions pdns/dnsdistdist/dnsdist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "dnsdist-console.hh"
#include "dnsdist-crypto.hh"
#include "dnsdist-discovery.hh"
#include "dnsdist-dnsparser.hh"
#include "dnsdist-dynblocks.hh"
#include "dnsdist-ecs.hh"
#include "dnsdist-edns.hh"
Expand All @@ -72,7 +71,6 @@
#include "doh.hh"
#include "dolog.hh"
#include "dnsname.hh"
#include "dnsparser.hh"
#include "ednsoptions.hh"
#include "gettime.hh"
#include "lock.hh"
Expand Down Expand Up @@ -188,38 +186,6 @@ struct DelayedPacket
static std::unique_ptr<DelayPipe<DelayedPacket>> g_delay{nullptr};
#endif /* DISABLE_DELAY_PIPE */

std::string DNSQuestion::getTrailingData() const
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto* message = reinterpret_cast<const char*>(this->getData().data());
const uint16_t messageLen = getDNSPacketLength(message, this->getData().size());
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
return {message + messageLen, this->getData().size() - messageLen};
}

bool DNSQuestion::setTrailingData(const std::string& tail)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const char* message = reinterpret_cast<const char*>(this->data.data());
const uint16_t messageLen = getDNSPacketLength(message, this->data.size());
this->data.resize(messageLen);
if (!tail.empty()) {
if (!hasRoomFor(tail.size())) {
return false;
}
this->data.insert(this->data.end(), tail.begin(), tail.end());
}
return true;
}

bool DNSQuestion::editHeader(const std::function<bool(dnsheader&)>& editFunction)
{
if (data.size() < sizeof(dnsheader)) {
throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer");
}
return dnsdist::PacketMangling::editDNSHeaderFromPacket(data, editFunction);
}

static void doLatencyStats(dnsdist::Protocol protocol, double udiff)
{
constexpr auto doAvg = [](double& var, double n, double weight) {
Expand Down
6 changes: 1 addition & 5 deletions pdns/dnsdistdist/dnsdist.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "circular_buffer.hh"
#include "dnscrypt.hh"
#include "dnsdist-cache.hh"
#include "dnsdist-configuration.hh"
#include "dnsdist-dynbpf.hh"
#include "dnsdist-idstate.hh"
#include "dnsdist-lbpolicies.hh"
Expand Down Expand Up @@ -67,10 +66,7 @@ struct ClientState;

struct DNSQuestion
{
DNSQuestion(InternalQueryState& ids_, PacketBuffer& data_) :
data(data_), ids(ids_), ecsPrefixLength(ids.origRemote.sin4.sin_family == AF_INET ? dnsdist::configuration::getCurrentRuntimeConfiguration().d_ECSSourcePrefixV4 : dnsdist::configuration::getCurrentRuntimeConfiguration().d_ECSSourcePrefixV6), ecsOverride(dnsdist::configuration::getCurrentRuntimeConfiguration().d_ecsOverride)
{
}
DNSQuestion(InternalQueryState& ids_, PacketBuffer& data_);
DNSQuestion(const DNSQuestion&) = delete;
DNSQuestion& operator=(const DNSQuestion&) = delete;
DNSQuestion(DNSQuestion&&) = default;
Expand Down
3 changes: 0 additions & 3 deletions pdns/dnsdistdist/test-dnsdist-lua-ffi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,11 @@ BOOST_AUTO_TEST_CASE(test_Query)

{
BOOST_CHECK_EQUAL(dnsdist_ffi_dnsquestion_get_trailing_data(&lightDQ, nullptr), 0U);
#if 0
// DNSQuestion::setTrailingData() and DNSQuestion::getTrailingData() are currently stubs in the test runner
std::string garbage("thisissomegarbagetrailingdata");
BOOST_CHECK_EQUAL(dnsdist_ffi_dnsquestion_set_trailing_data(&lightDQ, garbage.data(), garbage.size()), true);
const char* buffer = nullptr;
BOOST_REQUIRE_EQUAL(dnsdist_ffi_dnsquestion_get_trailing_data(&lightDQ, &buffer), garbage.size());
BOOST_CHECK_EQUAL(garbage, std::string(buffer));
#endif
}

{
Expand Down
12 changes: 0 additions & 12 deletions pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ bool TLSFrontend::setupTLS()
return true;
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
std::string DNSQuestion::getTrailingData() const
{
return "";
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
bool DNSQuestion::setTrailingData(const std::string& tail)
{
return false;
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple..
bool DNSDistSNMPAgent::sendDNSTrap(const DNSQuestion& dnsQuestion, const std::string& reason)
{
Expand Down

0 comments on commit d0e851b

Please sign in to comment.