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

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

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

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

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

@ -10,6 +10,8 @@
namespace llarp::dns namespace llarp::dns
{ {
static auto logcat = log::Cat("dns");
bool bool
SRVData::IsValid() const SRVData::IsValid() const
{ {
@ -22,7 +24,7 @@ namespace llarp::dns
// check target size is not absurd // check target size is not absurd
if (target.size() > TARGET_MAX_SIZE) 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; return false;
} }
@ -41,7 +43,7 @@ namespace llarp::dns
} }
// if we're here, target is invalid // if we're here, target is invalid
LogWarn("SRVData invalid"); log::warning(logcat, "SRVData invalid");
return false; return false;
} }
@ -64,14 +66,14 @@ namespace llarp::dns
bool bool
SRVData::fromString(std::string_view srvString) SRVData::fromString(std::string_view srvString)
{ {
LogDebug("SRVData::fromString(\"", srvString, "\")"); log::debug(logcat, "SRVData::fromString(\"{}\")", srvString);
// split on spaces, discard trailing empty strings // split on spaces, discard trailing empty strings
auto splits = split(srvString, " ", false); auto splits = split(srvString, " ", false);
if (splits.size() != 5 && splits.size() != 4) 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; return false;
} }
@ -79,19 +81,19 @@ namespace llarp::dns
if (not parse_int(splits[1], priority)) 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; return false;
} }
if (not parse_int(splits[2], weight)) 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; return false;
} }
if (not parse_int(splits[3], port)) 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; return false;
} }

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

Loading…
Cancel
Save