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: dump more packet cache #14658

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion pdns/dnsdistdist/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,9 @@ bin_PROGRAMS += \
$(fuzz_targets_programs)

fuzz_targets_libs = \
$(LIBCRYPTO_LIBS) \
$(ARC4RANDOM_LIBS) \
$(LIBCRYPTO_LIBS) \
$(LIBSODIUM_LIBS) \
$(LIB_FUZZING_ENGINE)

fuzz_targets_ldflags = \
Expand All @@ -556,6 +557,7 @@ fuzz_target_dnsdistcache_SOURCES = \
dns.cc dns.hh \
dnsdist-cache.cc dnsdist-cache.hh \
dnsdist-configuration.cc dnsdist-configuration.hh \
dnsdist-crypto.cc dnsdist-crypto.hh \
dnsdist-dnsparser.cc dnsdist-dnsparser.hh \
dnsdist-dnsquestion.cc \
dnsdist-ecs.cc dnsdist-ecs.hh \
Expand Down
11 changes: 9 additions & 2 deletions pdns/dnsdistdist/dnsdist-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "dnsdist-ecs.hh"
#include "ednssubnet.hh"
#include "packetcache.hh"
#include "base64.hh"

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters): too cumbersome to change at this point
DNSDistPacketCache::DNSDistPacketCache(size_t maxEntries, uint32_t maxTTL, uint32_t minTTL, uint32_t tempFailureTTL, uint32_t maxNegativeTTL, uint32_t staleTTL, bool dontAge, uint32_t shards, bool deferrableInsertLock, bool parseECS) :
Expand Down Expand Up @@ -481,7 +482,7 @@ uint64_t DNSDistPacketCache::getEntriesCount()
return getSize();
}

uint64_t DNSDistPacketCache::dump(int fileDesc)
uint64_t DNSDistPacketCache::dump(int fileDesc, bool rawResponse)
{
auto filePtr = pdns::UniqueFilePtr(fdopen(dup(fileDesc), "w"));
if (filePtr == nullptr) {
Expand All @@ -507,7 +508,13 @@ uint64_t DNSDistPacketCache::dump(int fileDesc)
rcode = dnsHeader.rcode;
}

fprintf(filePtr.get(), "%s %" PRId64 " %s ; rcode %" PRIu8 ", key %" PRIu32 ", length %" PRIu16 ", received over UDP %d, added %" PRId64 "\n", value.qname.toString().c_str(), static_cast<int64_t>(value.validity - now), QType(value.qtype).toString().c_str(), rcode, entry.first, value.len, value.receivedOverUDP ? 1 : 0, static_cast<int64_t>(value.added));
fprintf(filePtr.get(), "%s %" PRId64 " %s %s ; ecs %s, rcode %" PRIu8 ", key %" PRIu32 ", length %" PRIu16 ", received over UDP %d, added %" PRId64 ", dnssecOK %d, raw query flags %" PRIu16, value.qname.toString().c_str(), static_cast<int64_t>(value.validity - now), QClass(value.qclass).toString().c_str(), QType(value.qtype).toString().c_str(), value.subnet ? value.subnet.get().toString().c_str() : "empty", rcode, entry.first, value.len, value.receivedOverUDP ? 1 : 0, static_cast<int64_t>(value.added), value.dnssecOK ? 1 : 0, value.queryFlags);

if (rawResponse) {
std::string rawDataResponse = Base64Encode(value.value);
fprintf(filePtr.get(), ", base64response %s", rawDataResponse.c_str());
}
fprintf(filePtr.get(), "\n");
}
catch (...) {
fprintf(filePtr.get(), "; error printing '%s'\n", value.qname.empty() ? "EMPTY" : value.qname.toString().c_str());
Expand Down
2 changes: 1 addition & 1 deletion pdns/dnsdistdist/dnsdist-cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public:
uint64_t getTTLTooShorts() const { return d_ttlTooShorts.load(); }
uint64_t getCleanupCount() const { return d_cleanupCount.load(); }
uint64_t getEntriesCount();
uint64_t dump(int fileDesc);
uint64_t dump(int fileDesc, bool rawResponse = false);

/* get the list of domains (qnames) that contains the given address in an A or AAAA record */
std::set<DNSName> getDomainsContainingRecords(const ComboAddress& addr);
Expand Down
4 changes: 2 additions & 2 deletions pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client)
return results;
});

luaCtx.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const std::string& fname)const>("dump", [](const std::shared_ptr<DNSDistPacketCache>& cache, const std::string& fname) {
luaCtx.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const std::string& fname, boost::optional<bool> rawResponse)const>("dump", [](const std::shared_ptr<DNSDistPacketCache>& cache, const std::string& fname, boost::optional<bool> rawResponse) {
if (cache) {

int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
Expand All @@ -213,7 +213,7 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client)

uint64_t records = 0;
try {
records = cache->dump(fd);
records = cache->dump(fd, rawResponse? *rawResponse : false);
}
catch (const std::exception& e) {
close(fd);
Expand Down
8 changes: 6 additions & 2 deletions pdns/dnsdistdist/docs/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1004,11 +1004,15 @@ See :doc:`../guides/cache` for a how to.

Represents a cache that can be part of :class:`ServerPool`.

.. method:: PacketCache:dump(fname)
.. method:: PacketCache:dump(fname [, rawResponse=false])

Dump a summary of the cache entries to a file.
.. versionchanged:: 2.0.0
``rawResponse`` added

Dump a summary of the cache entries to a file. The raw response packet can be decoded by passing it to ``sdig``: ``echo [base64 encoded packet] | openssl base64 -d | sdig stdin 0 . A``

:param str fname: The path to a file where the cache summary should be dumped. Note that if the target file already exists, it will not be overwritten.
:param bool rawResponse: Dump the raw packet response encoded with base64.

.. method:: PacketCache:expunge(n)

Expand Down
Loading