2021-04-28 19:48:10 +00:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
2022-05-23 16:45:06 +00:00
|
|
|
#include <memory>
|
2021-04-28 19:48:10 +00:00
|
|
|
#include <llarp/net/sock_addr.hpp>
|
2022-05-23 16:45:06 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
namespace llarp::dns
|
2021-04-28 19:48:10 +00:00
|
|
|
{
|
2022-05-23 16:45:06 +00:00
|
|
|
/// sets dns settings in a platform dependant way
|
2022-06-22 16:14:33 +00:00
|
|
|
class I_Platform
|
2022-05-23 16:45:06 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-06-22 16:14:33 +00:00
|
|
|
virtual ~I_Platform() = default;
|
2022-05-23 16:45:06 +00:00
|
|
|
|
|
|
|
/// Attempts to set lokinet as the DNS server.
|
|
|
|
/// throws if unsupported or fails.
|
|
|
|
///
|
|
|
|
///
|
2022-07-28 16:07:38 +00:00
|
|
|
/// \param if_index -- the interface index to which we add the DNS servers, this can be gotten
|
|
|
|
/// from the interface name e.g. lokitun0 (Typically tun_endpoint.GetIfName().) and then put
|
|
|
|
/// through if_nametoindex().
|
2022-05-23 16:45:06 +00:00
|
|
|
/// \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
|
2022-07-28 16:07:38 +00:00
|
|
|
set_resolver(unsigned int if_index, llarp::SockAddr dns, bool global) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// a dns platform does silently does nothing, successfully
|
|
|
|
class Null_Platform : public I_Platform
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~Null_Platform() override = default;
|
|
|
|
void
|
|
|
|
set_resolver(unsigned int, llarp::SockAddr, bool) override
|
|
|
|
{}
|
2022-05-23 16:45:06 +00:00
|
|
|
};
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
/// 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:
|
|
|
|
~Multi_Platform() override = default;
|
|
|
|
/// 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(unsigned int if_index, llarp::SockAddr dns, bool global) override;
|
|
|
|
};
|
2022-04-07 20:44:23 +00:00
|
|
|
} // namespace llarp::dns
|