remove compat wrapper

pull/1969/head
Jeff 2 years ago committed by Jeff Becker
parent 253d22db4f
commit baddad9564
No known key found for this signature in database
GPG Key ID: 025C02EE3A092F2D

@ -84,9 +84,8 @@ extern "C"
JNIEXPORT jint JNICALL
Java_network_loki_lokinet_LokinetDaemon_GetUDPSocket(JNIEnv* env, jobject self)
{
auto ptr = GetImpl<llarp::Context>(env, self);
if (const auto& router = ptr->router; ptr and ptr->router)
return router->OutboundUDPSocket();
if (auto ptr = GetImpl<llarp::Context>(env, self); ptr and ptr->router)
return ptr->router->OutboundUDPSocket();
return -1;
}

@ -77,12 +77,14 @@ add_library(liblokinet
dns/message.cpp
dns/name.cpp
dns/multi_platform.cpp
dns/nm_platform.cpp
dns/sd_platform.cpp
dns/question.cpp
dns/rr.cpp
dns/serialize.cpp
dns/server.cpp
dns/srv_data.cpp
dns/resolver.cpp
consensus/table.cpp
consensus/reachability_testing.cpp

@ -0,0 +1,30 @@
#include "multi_platform.hpp"
namespace llarp::dns
{
void
Multi_Platform::add_impl(std::unique_ptr<I_Platform> impl)
{
m_Impls.emplace_back(std::move(impl));
}
void
Multi_Platform::set_resolver(std::string ifname, llarp::SockAddr dns, bool global)
{
size_t fails{0};
for (const auto& ptr : m_Impls)
{
try
{
ptr->set_resolver(ifname, dns, global);
}
catch (std::exception& ex)
{
LogWarn(ex.what());
fails++;
}
}
if (fails == m_Impls.size())
throw std::runtime_error{"tried all ways to set resolver and failed"};
}
} // namespace llarp::dns

@ -0,0 +1,22 @@
#pragma once
#include "platform.hpp"
#include <vector>
namespace llarp::dns
{
/// a collection of dns platforms that are tried in order when setting dns
class Multi_Platform : public I_Platform
{
std::vector<std::unique_ptr<I_Platform>> m_Impls;
public:
/// add a platform to be owned
void
add_impl(std::unique_ptr<I_Platform> impl);
/// try all owned platforms to set the resolver, throws if none of them work
void
set_resolver(std::string ifname, llarp::SockAddr dns, bool global) override;
};
} // namespace llarp::dns

@ -0,0 +1,24 @@
#include "nm_platform.hpp"
#ifdef WITH_SYSTEMD
extern "C"
{
#include <net/if.h>
}
#include <llarp/linux/dbus.hpp>
using namespace std::literals;
namespace llarp::dns::nm
{
Platform::Platform()
{}
void
Platform::set_resolver(std::string, llarp::SockAddr, bool)
{
// todo: implement me eventually
}
} // namespace llarp::dns::nm
#endif

@ -0,0 +1,24 @@
#pragma once
#include "platform.hpp"
#include "null_platform.hpp"
#include <llarp/constants/platform.hpp>
#include <type_traits>
#include <unordered_map>
namespace llarp::dns
{
namespace nm
{
// a dns platform that sets dns via network manager
class Platform : public I_Platform
{
public:
virtual ~Platform() = default;
void
set_resolver(std::string ifname, llarp::SockAddr dns, bool global) override;
};
}; // namespace nm
using NM_Platform_t = std::conditional_t<false, nm::Platform, Null_Platform>;
} // namespace llarp::dns

@ -0,0 +1,14 @@
#pragma once
#include "platform.hpp"
namespace llarp::dns
{
/// a dns platform does silently does nothing, successfully
class Null_Platform : public I_Platform
{
public:
void
set_resolver(std::string, llarp::SockAddr, bool) override
{}
};
} // namespace llarp::dns

@ -6,18 +6,13 @@
#include <stdexcept>
namespace llarp
{
struct AbstractRouter;
}
namespace llarp::dns
{
/// sets dns settings in a platform dependant way
class I_SystemSettings
class I_Platform
{
public:
virtual ~I_SystemSettings() = default;
virtual ~I_Platform() = default;
/// Attempts to set lokinet as the DNS server.
/// throws if unsupported or fails.
@ -32,28 +27,4 @@ namespace llarp::dns
set_resolver(std::string if_name, llarp::SockAddr dns, bool global) = 0;
};
/// creates for the current platform
std::shared_ptr<I_SystemSettings>
MakeSystemSettings();
/// compat wrapper
inline bool
set_resolver(std::string if_name, llarp::SockAddr dns, bool global)
{
try
{
if (auto settings = MakeSystemSettings())
{
settings->set_resolver(std::move(if_name), std::move(dns), global);
return true;
}
}
catch (std::exception& ex)
{
LogError("failed to set DNS: ", ex.what());
}
LogWarn("did not set dns");
return false;
}
} // namespace llarp::dns

@ -1,220 +0,0 @@
#include "resolver.hpp"
#include <llarp/util/logging.hpp>
#include <llarp/constants/platform.hpp>
namespace llarp::dns
{
class Null_SystemSettings : public I_SystemSettings
{
void
set_resolver(std::string, llarp::SockAddr, bool) override
{
LogDebug("lokinet is not built with systemd support, cannot set systemd resolved DNS");
}
};
} // namespace llarp::dns
#ifdef WITH_SYSTEMD
extern "C"
{
#include <net/if.h>
}
#include <llarp/linux/dbus.hpp>
using namespace std::literals;
namespace llarp::dns
{
class SD_SystemSettings : public I_SystemSettings
{
public:
void
set_resolver(std::string ifname, llarp::SockAddr dns, bool global) override
{
unsigned int if_ndx = if_nametoindex(ifname.c_str());
if (if_ndx == 0)
{
throw std::runtime_error{"No such interface '" + ifname + "'"};
}
linux::DBUS _dbus{
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager"};
// This passing address by bytes and using two separate calls for ipv4/ipv6 is gross, but
// the alternative is to build up a bunch of crap with va_args, which is slightly more
// gross.
const bool isStandardDNSPort = dns.getPort() == 53;
if (dns.isIPv6())
{
auto ipv6 = dns.getIPv6();
static_assert(sizeof(ipv6) == 16);
auto* a = reinterpret_cast<const uint8_t*>(&ipv6);
if (isStandardDNSPort)
{
_dbus(
"SetLinkDNS",
"ia(iay)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET6, // network address type
(int)16, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15] // yuck
// clang-format on
);
}
else
{
_dbus(
"SetLinkDNSEx",
"ia(iayqs)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET6, // network address type
(int)16, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15], // yuck
// clang-format on
(uint16_t)dns.getPort(),
nullptr // dns server name (for TLS SNI which we don't care about)
);
}
}
else
{
auto ipv4 = dns.getIPv4();
static_assert(sizeof(ipv4) == 4);
auto* a = reinterpret_cast<const uint8_t*>(&ipv4);
if (isStandardDNSPort)
{
_dbus(
"SetLinkDNS",
"ia(iay)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET, // network address type
(int)4, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3] // yuck
// clang-format on
);
}
else
{
_dbus(
"SetLinkDNSEx",
"ia(iayqs)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET, // network address type
(int)4, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3], // yuck
// clang-format on
(uint16_t)dns.getPort(),
nullptr // dns server name (for TLS SNI which we don't care about)
);
}
}
if (global)
// Setting "." as a routing domain gives this DNS server higher priority in resolution
// compared to dns servers that are set without a domain (e.g. the default for a
// DHCP-configured DNS server)
_dbus(
"SetLinkDomains",
"ia(sb)",
(int32_t)if_ndx,
(int)1, // array size
"." // global DNS root
);
else
// Only resolve .loki and .snode through lokinet (so you keep using your local DNS
// server for everything else, which is nicer than forcing everything though lokinet's
// upstream DNS).
_dbus(
"SetLinkDomains",
"ia(sb)",
(int32_t)if_ndx,
(int)2, // array size
"loki", // domain
(int)1, // routing domain = true
"snode", // domain
(int)1 // routing domain = true
);
}
};
/// network manager dns setter
class NM_SystemSettings : public I_SystemSettings
{
public:
void
set_resolver(std::string ifname, llarp::SockAddr dns, bool global) override
{
unsigned int if_ndx = if_nametoindex(ifname.c_str());
if (if_ndx == 0)
{
throw std::runtime_error{"No such interface '" + ifname + "'"};
}
(void)dns;
(void)global;
// TODO: implement network manager shit
}
};
} // namespace llarp::dns
#endif // WITH_SYSTEMD
namespace llarp::dns
{
class MultiSettings : public I_SystemSettings
{
std::vector<std::unique_ptr<I_SystemSettings>> m_Impls;
public:
void
add_impl(std::unique_ptr<I_SystemSettings> impl)
{
m_Impls.emplace_back(std::move(impl));
}
void
set_resolver(std::string ifname, llarp::SockAddr dns, bool global) override
{
size_t fails{0};
for (const auto& ptr : m_Impls)
{
try
{
ptr->set_resolver(ifname, dns, global);
}
catch (std::exception& ex)
{
LogWarn(ex.what());
fails++;
}
}
if (fails == m_Impls.size())
throw std::runtime_error{"tried all ways to set resolver and failed"};
}
};
std::shared_ptr<I_SystemSettings>
MakeSystemSettings()
{
auto settings = std::make_shared<MultiSettings>();
settings->add_impl(std::make_unique<Null_SystemSettings>());
if constexpr (llarp::platform::has_systemd)
{
settings->add_impl(std::make_unique<SD_SystemSettings>());
settings->add_impl(std::make_unique<NM_SystemSettings>());
}
return settings;
}
} // namespace llarp::dns

@ -0,0 +1,134 @@
#ifdef WITH_SYSTEMD
#include "sd_platform.hpp"
extern "C"
{
#include <net/if.h>
}
#include <llarp/linux/dbus.hpp>
using namespace std::literals;
namespace llarp::dns::sd
{
void
Platform::set_resolver(std::string ifname, llarp::SockAddr dns, bool global)
{
unsigned int if_ndx = if_nametoindex(ifname.c_str());
if (if_ndx == 0)
{
throw std::runtime_error{"No such interface '" + ifname + "'"};
}
linux::DBUS _dbus{
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager"};
// This passing address by bytes and using two separate calls for ipv4/ipv6 is gross, but
// the alternative is to build up a bunch of crap with va_args, which is slightly more
// gross.
const bool isStandardDNSPort = dns.getPort() == 53;
if (dns.isIPv6())
{
auto ipv6 = dns.getIPv6();
static_assert(sizeof(ipv6) == 16);
auto* a = reinterpret_cast<const uint8_t*>(&ipv6);
if (isStandardDNSPort)
{
_dbus(
"SetLinkDNS",
"ia(iay)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET6, // network address type
(int)16, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15] // yuck
// clang-format on
);
}
else
{
_dbus(
"SetLinkDNSEx",
"ia(iayqs)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET6, // network address type
(int)16, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15], // yuck
// clang-format on
(uint16_t)dns.getPort(),
nullptr // dns server name (for TLS SNI which we don't care about)
);
}
}
else
{
auto ipv4 = dns.getIPv4();
static_assert(sizeof(ipv4) == 4);
auto* a = reinterpret_cast<const uint8_t*>(&ipv4);
if (isStandardDNSPort)
{
_dbus(
"SetLinkDNS",
"ia(iay)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET, // network address type
(int)4, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3] // yuck
// clang-format on
);
}
else
{
_dbus(
"SetLinkDNSEx",
"ia(iayqs)",
(int32_t)if_ndx,
(int)1, // number of "iayqs"s we are passing
(int32_t)AF_INET, // network address type
(int)4, // network addr byte size
// clang-format off
a[0], a[1], a[2], a[3], // yuck
// clang-format on
(uint16_t)dns.getPort(),
nullptr // dns server name (for TLS SNI which we don't care about)
);
}
}
if (global)
// Setting "." as a routing domain gives this DNS server higher priority in resolution
// compared to dns servers that are set without a domain (e.g. the default for a
// DHCP-configured DNS server)
_dbus(
"SetLinkDomains",
"ia(sb)",
(int32_t)if_ndx,
(int)1, // array size
"." // global DNS root
);
else
// Only resolve .loki and .snode through lokinet (so you keep using your local DNS
// server for everything else, which is nicer than forcing everything though lokinet's
// upstream DNS).
_dbus(
"SetLinkDomains",
"ia(sb)",
(int32_t)if_ndx,
(int)2, // array size
"loki", // domain
(int)1, // routing domain = true
"snode", // domain
(int)1 // routing domain = true
);
}
} // namespace llarp::dns::sd
#endif

@ -0,0 +1,24 @@
#pragma once
#include "platform.hpp"
#include "null_platform.hpp"
#include <llarp/constants/platform.hpp>
#include <type_traits>
namespace llarp::dns
{
namespace sd
{
/// a dns platform that sets dns via systemd resolved
class Platform : public I_Platform
{
public:
virtual ~Platform() = default;
void
set_resolver(std::string ifname, llarp::SockAddr dns, bool global) override;
};
} // namespace sd
using SD_Platform_t =
std::conditional_t<llarp::platform::has_systemd, sd::Platform, Null_Platform>;
} // namespace llarp::dns

@ -9,6 +9,10 @@
#include <unbound.h>
#include <uvw.hpp>
#include "multi_platform.hpp"
#include "sd_platform.hpp"
#include "nm_platform.hpp"
namespace llarp::dns
{
void
@ -398,8 +402,11 @@ namespace llarp::dns
}
} // namespace libunbound
Server::Server(EventLoop_ptr loop, llarp::DnsConfig conf)
: m_Loop{std::move(loop)}, m_Config{std::move(conf)}
Server::Server(EventLoop_ptr loop, llarp::DnsConfig conf, std::string netif)
: m_Loop{std::move(loop)}
, m_Config{std::move(conf)}
, m_Platform{CreatePlatform()}
, m_NetifName{std::move(netif)}
{}
void
@ -417,6 +424,18 @@ namespace llarp::dns
AddResolver(ptr);
}
std::shared_ptr<I_Platform>
Server::CreatePlatform() const
{
auto plat = std::make_shared<Multi_Platform>();
if constexpr (llarp::platform::has_systemd)
{
plat->add_impl(std::make_unique<SD_Platform_t>());
plat->add_impl(std::make_unique<NM_Platform_t>());
}
return plat;
}
std::shared_ptr<PacketSource_Base>
Server::MakePacketSourceOn(const llarp::SockAddr& addr, const llarp::DnsConfig&)
{
@ -508,6 +527,13 @@ namespace llarp::dns
}
}
void
Server::SetDNSMode(bool all_queries)
{
if (auto maybe_addr = FirstBoundPacketSourceAddr())
m_Platform->set_resolver(m_NetifName, *maybe_addr, all_queries);
}
bool
Server::MaybeHandlePacket(
std::weak_ptr<PacketSource_Base> src,

@ -1,6 +1,7 @@
#pragma once
#include "message.hpp"
#include "platform.hpp"
#include <llarp/config/config.hpp>
#include <llarp/ev/ev.hpp>
#include <llarp/net/net.hpp>
@ -160,9 +161,13 @@ namespace llarp::dns
void
AddResolver(std::shared_ptr<Resolver_Base> resolver);
/// create the platform dependant dns stuff
virtual std::shared_ptr<I_Platform>
CreatePlatform() const;
public:
virtual ~Server() = default;
explicit Server(EventLoop_ptr loop, llarp::DnsConfig conf);
explicit Server(EventLoop_ptr loop, llarp::DnsConfig conf, std::string netif_name);
/// returns all sockaddr we have from all of our PacketSources
std::vector<SockAddr>
@ -210,11 +215,18 @@ namespace llarp::dns
const SockAddr& from,
llarp::OwnedBuffer buf);
/// set which dns mode we are in.
/// true for intercepting all queries. false for just .loki and .snode
void
SetDNSMode(bool all_queries);
protected:
EventLoop_ptr m_Loop;
llarp::DnsConfig m_Config;
std::shared_ptr<I_Platform> m_Platform;
private:
const std::string m_NetifName;
std::set<std::shared_ptr<Resolver_Base>, ComparePtr<std::shared_ptr<Resolver_Base>>>
m_OwnedResolvers;
std::set<std::weak_ptr<Resolver_Base>, CompareWeakPtr<Resolver_Base>> m_Resolvers;

@ -713,8 +713,6 @@ namespace llarp
m_ShouldInitTun = false;
}
m_Resolver = std::make_shared<dns::Server>(m_Router->loop(), dnsConfig);
m_OurRange = networkConfig.m_ifaddr;
if (!m_OurRange.addr.h)
{
@ -745,8 +743,7 @@ namespace llarp
return llarp::SockAddr{ifaddr, huint16_t{port}};
});
}
// TODO: "exit-whitelist" and "exit-blacklist"
// (which weren't originally implemented)
m_Resolver = std::make_shared<dns::Server>(m_Router->loop(), dnsConfig, m_ifname);
}
huint128_t

@ -123,7 +123,7 @@ namespace llarp
virtual ~TunDNS() = default;
explicit TunDNS(TunEndpoint* ep, const llarp::DnsConfig& conf)
: dns::Server{ep->Router()->loop(), conf}, m_Endpoint{ep}
: dns::Server{ep->Router()->loop(), conf, ep->GetIfName()}, m_Endpoint{ep}
{}
std::shared_ptr<dns::PacketSource_Base>
@ -144,7 +144,7 @@ namespace llarp
#endif
TunEndpoint::TunEndpoint(AbstractRouter* r, service::Context* parent)
: service::Endpoint(r, parent)
: service::Endpoint{r, parent}
{
m_PacketRouter = std::make_unique<vpn::PacketRouter>(
[this](net::IPPacket pkt) { HandleGotUserPacket(std::move(pkt)); });
@ -174,7 +174,7 @@ namespace llarp
HandleGotUserPacket(std::move(pkt));
});
#else
m_DNS = std::make_shared<dns::Server>(Loop(), m_DnsConfig);
m_DNS = std::make_shared<dns::Server>(Loop(), m_DnsConfig, GetIfName());
#endif
m_DNS->AddResolver(weak_from_this());
m_DNS->Start();

@ -45,6 +45,11 @@ namespace llarp
struct I_RCLookupHandler;
struct RoutePoker;
namespace dns
{
class I_SystemSettings;
}
namespace net
{
class Platform;
@ -355,7 +360,10 @@ namespace llarp
}
virtual int
OutboundUDPSocket() const = 0;
OutboundUDPSocket() const
{
return -1;
}
protected:
/// Virtual function to handle RouterEvent. HiveRouter overrides this in

@ -2,7 +2,7 @@
#include "abstractrouter.hpp"
#include "net/sock_addr.hpp"
#include <llarp/service/context.hpp>
#include <llarp/dns/resolver.hpp>
#include <llarp/dns/platform.hpp>
#include <unordered_set>
namespace llarp
@ -163,22 +163,8 @@ namespace llarp
void
RoutePoker::SetDNSMode(bool exit_mode_on) const
{
if (auto dns = m_Router->hiddenServiceContext().GetDefault()->DNS())
{
if (auto maybe_addr = dns->FirstBoundPacketSourceAddr())
{
if (dns::set_resolver(
m_Router->hiddenServiceContext().GetDefault()->GetIfName(),
*maybe_addr,
exit_mode_on))
{
LogInfo(
"DNS set to ",
*maybe_addr,
exit_mode_on ? " for all traffic" : " for just lokinet traffic");
}
}
}
if (auto dns_server = m_Router->hiddenServiceContext().GetDefault()->DNS())
dns_server->SetDNSMode(exit_mode_on);
}
void

@ -355,6 +355,12 @@ namespace llarp
return m_peerDb;
}
inline int
OutboundUDPSocket() const override
{
return m_OutboundUDPSocket;
}
void
GossipRCIfNeeded(const RouterContact rc) override;

Loading…
Cancel
Save