From 52c6cd497ffde243a199a95845fab23e3ddcb651 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Fri, 16 Sep 2022 13:20:24 -0300 Subject: [PATCH] Apple DNS fix WIP --- llarp/dns/platform.cpp | 2 +- llarp/dns/server.cpp | 60 ++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/llarp/dns/platform.cpp b/llarp/dns/platform.cpp index a6cf24d4b..a2381bfc3 100644 --- a/llarp/dns/platform.cpp +++ b/llarp/dns/platform.cpp @@ -12,7 +12,7 @@ namespace llarp::dns Multi_Platform::set_resolver(unsigned int index, llarp::SockAddr dns, bool global) { if (m_Impls.empty()) - return; + return; size_t fails{0}; for (const auto& ptr : m_Impls) { diff --git a/llarp/dns/server.cpp b/llarp/dns/server.cpp index 7c9cf4a14..8b3a7efe3 100644 --- a/llarp/dns/server.cpp +++ b/llarp/dns/server.cpp @@ -171,10 +171,24 @@ namespace llarp::dns query->SendReply(std::move(pkt)); } - void ConfigureUpstream(const llarp::DnsConfig& conf) + void + AddUpstreamResolver(const SockAddr& dns) { - auto* ctx = m_ctx.get(); + std::string str = dns.hostString(); + + if (const auto port = dns.getPort(); port != 53) + fmt::format_to(std::back_inserter(str), "@{}", port); + + if (auto err = ub_ctx_set_fwd(m_ctx.get(), str.c_str())) + { + throw std::runtime_error{ + fmt::format("cannot use {} as upstream dns: {}", str, ub_strerror(err))}; + } + } + void + ConfigureUpstream(const llarp::DnsConfig& conf) + { if constexpr (platform::is_apple) { // On Apple, when we turn on exit mode, we can't directly connect to upstream from here @@ -192,36 +206,21 @@ namespace llarp::dns // Not at all clear why this is needed but without it we get "send failed: Can't // assign requested address" when unbound tries to connect to the localhost address // using a source address of 0.0.0.0. Yay apple. - ub_ctx_set_option(ctx, "outgoing-interface:", "127.0.0.1"); + SetOpt("outgoing-interface:", "127.0.0.1"); // The trampoline expects just a single source port (and sends everything back to it) - ub_ctx_set_option(ctx, "outgoing-range:", "1"); - ub_ctx_set_option(ctx, "outgoing-port-avoid:", "0-65535"); - ub_ctx_set_option( - ctx, - "outgoing-port-permit:", - std::to_string(apple::dns_trampoline_source_port).c_str()); + SetOpt("outgoing-range:", "1"); + SetOpt("outgoing-port-avoid:", "0-65535"); + SetOpt("outgoing-port-permit:", "{}", apple::dns_trampoline_source_port); + + AddUpstreamResolver(SockAddr{127, 0, 0, 1, {apple::dns_trampoline_port}}); return; } // set up forward dns for (const auto& dns : conf.m_upstreamDNS) - { - std::string str = dns.hostString(); - - if (const auto port = dns.getPort(); port != 53) - fmt::format_to(std::back_inserter(str), "@{}", port); - - log::critical(logcat, "Using upstream dns {}", str); - - if (auto err = ub_ctx_set_fwd(ctx, str.c_str())) - { - throw std::runtime_error{ - fmt::format("cannot use {} as upstream dns: {}", str, ub_strerror(err))}; - } - - } + AddUpstreamResolver(dns); if (auto maybe_addr = conf.m_QueryBind) { @@ -276,16 +275,25 @@ namespace llarp::dns SetOpt("outgoing-interface:", host); SetOpt("outgoing-range:", "1"); SetOpt("outgoing-port-avoid:", "0-65535"); - SetOpt("outgoing-port-permit:", std::to_string(addr.getPort())); + SetOpt("outgoing-port-permit:", "{}", addr.getPort()); } } void - SetOpt(std::string key, std::string val) + SetOpt(const std::string& key, const std::string& val) { ub_ctx_set_option(m_ctx.get(), key.c_str(), val.c_str()); } + // Wrapper around the above that takes 3+ arguments: the 2nd arg gets formatted with the + // remaining args, and the formatted string passed to the above as `val`. + template = 0> + void + SetOpt(const std::string& key, std::string_view format, FmtArgs&&... args) + { + SetOpt(key, fmt::format(format, std::forward(args)...)); + } + llarp::DnsConfig m_conf; public: