llarp/dns logging refactor

Convert everything in llarp/dns to new-style logging.
pull/1969/head
Jason Rhinelander 2 years ago
parent 49223a7853
commit 05ed9d6de0
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262

@ -13,6 +13,8 @@ namespace llarp
{
namespace dns
{
static auto logcat = log::Cat("dns");
bool
MessageHeader::Encode(llarp_buffer_t* buf) const
{
@ -116,16 +118,16 @@ namespace llarp
{
if (!qd.Decode(buf))
{
LogError("failed to decode question");
log::error(logcat, "failed to decode question");
return false;
}
LogDebug("dns question: ", qd);
log::debug(logcat, "question: {}", qd);
}
for (auto& an : answers)
{
if (not an.Decode(buf))
{
LogDebug("failed to decode answer");
log::debug(logcat, "failed to decode answer");
return false;
}
}

@ -20,7 +20,7 @@ namespace llarp::dns
}
catch (std::exception& ex)
{
LogWarn(ex.what());
log::warning(log::Cat("dns"), "{}", ex.what());
fails++;
}
}

@ -8,6 +8,8 @@ namespace llarp
{
namespace dns
{
static auto logcat = log::Cat("dns");
Question::Question(Question&& other)
: qname(std::move(other.qname))
, qtype(std::move(other.qtype))
@ -41,17 +43,17 @@ namespace llarp
qname = *std::move(name);
else
{
llarp::LogError("failed to decode name");
log::error(logcat, "failed to decode name");
return false;
}
if (!buf->read_uint16(qtype))
{
llarp::LogError("failed to decode type");
log::error(logcat, "failed to decode type");
return false;
}
if (!buf->read_uint16(qclass))
{
llarp::LogError("failed to decode class");
log::error(logcat, "failed to decode class");
return false;
}
return true;

@ -8,6 +8,8 @@ namespace llarp
{
namespace dns
{
static auto logcat = log::Cat("dns");
ResourceRecord::ResourceRecord(const ResourceRecord& other)
: rr_name(other.rr_name)
, rr_type(other.rr_type)
@ -64,22 +66,22 @@ namespace llarp
return false;
if (!buf->read_uint16(rr_type))
{
llarp::LogDebug("failed to decode rr type");
log::debug(logcat, "failed to decode rr type");
return false;
}
if (!buf->read_uint16(rr_class))
{
llarp::LogDebug("failed to decode rr class");
log::debug(logcat, "failed to decode rr class");
return false;
}
if (!buf->read_uint32(ttl))
{
llarp::LogDebug("failed to decode ttl");
log::debug(logcat, "failed to decode ttl");
return false;
}
if (!DecodeRData(buf, rData))
{
llarp::LogDebug("failed to decode rr rdata ", *this);
log::debug(logcat, "failed to decode rr rdata {}", *this);
return false;
}
return true;

@ -10,12 +10,15 @@
#include <unbound.h>
#include <uvw.hpp>
#include "oxen/log.hpp"
#include "sd_platform.hpp"
#include "nm_platform.hpp"
#include "win32_platform.hpp"
namespace llarp::dns
{
static auto logcat = log::Cat("dns");
void
QueryJob_Base::Cancel() const
{
@ -40,7 +43,7 @@ namespace llarp::dns
return;
if (not m_DNS.MaybeHandlePacket(shared_from_this(), m_LocalAddr, src, std::move(buf)))
{
LogWarn("did not handle dns packet from ", src, " to ", m_LocalAddr);
log::warning(logcat, "did not handle dns packet from {} to {}", src, m_LocalAddr);
}
});
m_udp->listen(bindaddr);
@ -250,24 +253,29 @@ namespace llarp::dns
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
throw std::invalid_argument{fmt::format("Failed to create UDP socket for unbound: {}", strerror(errno))};
if (0 != bind(fd, static_cast<const sockaddr*>(addr), addr.sockaddr_len())) {
throw std::invalid_argument{
fmt::format("Failed to create UDP socket for unbound: {}", strerror(errno))};
if (0 != bind(fd, static_cast<const sockaddr*>(addr), addr.sockaddr_len()))
{
close(fd);
throw std::invalid_argument{fmt::format("Failed to bind UDP socket for unbound: {}", strerror(errno))};
throw std::invalid_argument{
fmt::format("Failed to bind UDP socket for unbound: {}", strerror(errno))};
}
struct sockaddr_storage sas;
auto* sa = reinterpret_cast<struct sockaddr*>(&sas);
socklen_t sa_len;
if (0 != getsockname(fd, sa, &sa_len)) {
if (0 != getsockname(fd, sa, &sa_len))
{
close(fd);
throw std::invalid_argument{fmt::format("Failed to query UDP port for unbound: {}", strerror(errno))};
throw std::invalid_argument{
fmt::format("Failed to query UDP port for unbound: {}", strerror(errno))};
}
addr = SockAddr{*sa};
close(fd);
}
m_LocalAddr = addr;
LogInfo(fmt::format("sending dns queries from {}:{}", host, addr.getPort()));
log::info(logcat, "sending dns queries from {}:{}", host, addr.getPort());
// set up query bind port if needed
SetOpt("outgoing-interface:", host);
SetOpt("outgoing-range:", "1");
@ -372,7 +380,7 @@ namespace llarp::dns
if (auto loop = m_Loop.lock())
loop->call(std::forward<Callable>(f));
else
LogError("no mainloop?");
log::critical(logcat, "no mainloop?");
}
bool
@ -415,7 +423,8 @@ namespace llarp::dns
nullptr))
{
// take back ownership on fail
LogWarn("failed to send upstream query with libunbound: ", ub_strerror(err));
log::warning(
logcat, "failed to send upstream query with libunbound: {}", ub_strerror(err));
tmp.reset(pending);
tmp->Cancel();
}
@ -433,7 +442,7 @@ namespace llarp::dns
});
}
else
LogError("no source or parent");
log::error(logcat, "no source or parent");
}
} // namespace libunbound
@ -495,7 +504,8 @@ namespace llarp::dns
{
if (m_Config.m_upstreamDNS.empty())
{
LogInfo(
log::info(
logcat,
"explicitly no upstream dns providers specified, we will not resolve anything but .loki "
"and .snode");
return nullptr;
@ -592,14 +602,14 @@ namespace llarp::dns
// dont process to prevent feedback loop
if (ptr->WouldLoop(to, from))
{
LogWarn("preventing dns packet replay to=", to, " from=", from);
log::warning(logcat, "preventing dns packet replay to={} from={}", to, from);
return false;
}
auto maybe = MaybeParseDNSMessage(buf);
if (not maybe)
{
LogWarn("invalid dns message format from ", from, " to dns listener on ", to);
log::warning(logcat, "invalid dns message format from {} to dns listener on {}", from, to);
return false;
}
@ -626,7 +636,8 @@ namespace llarp::dns
{
if (auto res_ptr = resolver.lock())
{
LogDebug("check resolver ", res_ptr->ResolverName(), " for dns from ", from, " to ", to);
log::debug(
logcat, "check resolver {} for dns from {} to {}", res_ptr->ResolverName(), from, to);
if (res_ptr->MaybeHookDNS(ptr, msg, to, from))
return true;
}

@ -10,6 +10,8 @@
namespace llarp::dns
{
static auto logcat = log::Cat("dns");
bool
SRVData::IsValid() const
{
@ -22,7 +24,7 @@ namespace llarp::dns
// check target size is not absurd
if (target.size() > TARGET_MAX_SIZE)
{
LogWarn("SRVData target larger than max size (", TARGET_MAX_SIZE, ")");
log::warning(logcat, "SRVData target larger than max size ({})", TARGET_MAX_SIZE);
return false;
}
@ -41,7 +43,7 @@ namespace llarp::dns
}
// if we're here, target is invalid
LogWarn("SRVData invalid");
log::warning(logcat, "SRVData invalid");
return false;
}
@ -64,14 +66,14 @@ namespace llarp::dns
bool
SRVData::fromString(std::string_view srvString)
{
LogDebug("SRVData::fromString(\"", srvString, "\")");
log::debug(logcat, "SRVData::fromString(\"{}\")", srvString);
// split on spaces, discard trailing empty strings
auto splits = split(srvString, " ", false);
if (splits.size() != 5 && splits.size() != 4)
{
LogWarn("SRV record should have either 4 or 5 space-separated parts");
log::warning(logcat, "SRV record should have either 4 or 5 space-separated parts");
return false;
}
@ -79,19 +81,19 @@ namespace llarp::dns
if (not parse_int(splits[1], priority))
{
LogWarn("SRV record failed to parse \"", splits[1], "\" as uint16_t (priority)");
log::warning(logcat, "SRV record failed to parse \"{}\" as uint16_t (priority)", splits[1]);
return false;
}
if (not parse_int(splits[2], weight))
{
LogWarn("SRV record failed to parse \"", splits[2], "\" as uint16_t (weight)");
log::warning(logcat, "SRV record failed to parse \"{}\" as uint16_t (weight)", splits[2]);
return false;
}
if (not parse_int(splits[3], port))
{
LogWarn("SRV record failed to parse \"", splits[3], "\" as uint16_t (port)");
log::warning(logcat, "SRV record failed to parse \"{}\" as uint16_t (port)", splits[3]);
return false;
}

@ -11,6 +11,8 @@
namespace llarp::dns
{
static auto logcat = log::Cat("dns");
struct PendingUnboundLookup
{
std::weak_ptr<UnboundResolver> resolver;
@ -156,7 +158,7 @@ namespace llarp::dns
upstream += std::to_string(port);
}
LogError("Adding upstream resolver ", upstream);
log::info("Adding upstream resolver ", upstream);
if (ub_ctx_set_fwd(unboundContext, upstream.c_str()) != 0)
{
Reset();
@ -198,17 +200,12 @@ namespace llarp::dns
void
UnboundResolver::AddHostsFile(const fs::path& file)
{
LogDebug("adding hosts file ", file);
log::debug(logcat, "adding hosts file {}", file);
const auto str = file.u8string();
if (auto ret = ub_ctx_hosts(unboundContext, str.c_str()))
{
throw std::runtime_error{
fmt::format("Failed to add host file {}: {}", file, ub_strerror(ret))};
}
else
{
LogInfo("added hosts file ", file);
}
log::info(logcat, "added hosts file {}", file);
}
void

Loading…
Cancel
Save