mirror of https://github.com/oxen-io/lokinet
restucture dbus parts
* move dbus into llarp/linux/dbus.hpp and llarp/linux/dbus.cpp * provide platform abstraction for setting dns in preparation for network managerpull/1969/head
parent
2d586145ee
commit
253d22db4f
@ -1,21 +1,59 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <llarp/net/sock_addr.hpp>
|
||||
#include <llarp/util/logging.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct AbstractRouter;
|
||||
}
|
||||
|
||||
namespace llarp::dns
|
||||
{
|
||||
/// Attempts to set lokinet as the DNS server for systemd-resolved.
|
||||
/// Returns true if successful, false if unsupported or fails.
|
||||
///
|
||||
/// If systemd support is enabled it will attempt via dbus call to system-resolved
|
||||
/// When compiled without systemd support this always return false without doing anything.
|
||||
///
|
||||
/// \param if_name -- the interface name to which we add the DNS servers, e.g. lokitun0.
|
||||
/// Typically tun_endpoint.GetIfName().
|
||||
/// \param dns -- the listening address of the lokinet DNS server
|
||||
/// \param global -- whether to set up lokinet for all DNS queries (true) or just .loki & .snode
|
||||
/// addresses (false).
|
||||
bool
|
||||
set_resolver(std::string if_name, llarp::SockAddr dns, bool global);
|
||||
/// sets dns settings in a platform dependant way
|
||||
class I_SystemSettings
|
||||
{
|
||||
public:
|
||||
virtual ~I_SystemSettings() = default;
|
||||
|
||||
/// Attempts to set lokinet as the DNS server.
|
||||
/// throws if unsupported or fails.
|
||||
///
|
||||
///
|
||||
/// \param if_name -- the interface name to which we add the DNS servers, e.g. lokitun0.
|
||||
/// Typically tun_endpoint.GetIfName().
|
||||
/// \param dns -- the listening address of the lokinet DNS server
|
||||
/// \param global -- whether to set up lokinet for all DNS queries (true) or just .loki & .snode
|
||||
/// addresses (false).
|
||||
virtual void
|
||||
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
|
||||
|
@ -0,0 +1,26 @@
|
||||
#ifdef WITH_SYSTEMD
|
||||
#include "dbus.hpp"
|
||||
|
||||
namespace llarp::linux
|
||||
{
|
||||
system_bus_exception::system_bus_exception(int err)
|
||||
: std::runtime_error{"cannot connect to system bus: " + std::string{strerror(-err)}}
|
||||
{}
|
||||
|
||||
dbus_call_exception::dbus_call_exception(std::string meth, int err)
|
||||
: std::runtime_error{
|
||||
"failed to call dbus function '" + meth + "': " + std::string{strerror(-err)}}
|
||||
{}
|
||||
|
||||
DBUS::DBUS(std::string _interface, std::string _instance, std::string _call)
|
||||
: m_interface{std::move(_interface)}
|
||||
, m_instance{std::move(_instance)}
|
||||
, m_call{std::move(_call)}
|
||||
{
|
||||
sd_bus* bus{nullptr};
|
||||
if (auto err = sd_bus_open_system(&bus); err < 0)
|
||||
throw system_bus_exception{err};
|
||||
m_ptr.reset(bus);
|
||||
}
|
||||
} // namespace llarp::linux
|
||||
#endif
|
@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <systemd/sd-bus.h>
|
||||
}
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace llarp::linux
|
||||
{
|
||||
/// exception for connecting to system bus
|
||||
class system_bus_exception : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
explicit system_bus_exception(int err);
|
||||
};
|
||||
|
||||
/// exception for a failed calling of a dbus method
|
||||
class dbus_call_exception : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
explicit dbus_call_exception(std::string meth, int err);
|
||||
};
|
||||
|
||||
class DBUS
|
||||
{
|
||||
struct sd_bus_deleter
|
||||
{
|
||||
void
|
||||
operator()(sd_bus* ptr) const
|
||||
{
|
||||
sd_bus_unref(ptr);
|
||||
}
|
||||
};
|
||||
std::unique_ptr<sd_bus, sd_bus_deleter> m_ptr;
|
||||
const std::string m_interface;
|
||||
const std::string m_instance;
|
||||
const std::string m_call;
|
||||
|
||||
public:
|
||||
DBUS(std::string _interface, std::string _instance, std::string _call);
|
||||
|
||||
template <typename... T>
|
||||
void
|
||||
operator()(std::string method, const char* arg_format, T... args)
|
||||
{
|
||||
sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
sd_bus_message* msg = nullptr;
|
||||
auto r = sd_bus_call_method(
|
||||
m_ptr.get(),
|
||||
m_interface.c_str(),
|
||||
m_instance.c_str(),
|
||||
m_call.c_str(),
|
||||
method.c_str(),
|
||||
&error,
|
||||
&msg,
|
||||
arg_format,
|
||||
args...);
|
||||
|
||||
if (r < 0)
|
||||
throw dbus_call_exception{std::move(method), r};
|
||||
|
||||
sd_bus_message_unref(msg);
|
||||
sd_bus_error_free(&error);
|
||||
}
|
||||
};
|
||||
} // namespace llarp::linux
|
Loading…
Reference in New Issue