2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "message.hpp"
|
2022-06-22 16:14:33 +00:00
|
|
|
#include "platform.hpp"
|
2022-04-07 20:44:23 +00:00
|
|
|
#include <llarp/config/config.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/net/net.hpp>
|
2022-04-07 20:44:23 +00:00
|
|
|
#include <llarp/util/fs.hpp>
|
|
|
|
#include <set>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
namespace llarp::dns
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
/// a job handling 1 dns query
|
|
|
|
class QueryJob_Base
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
protected:
|
|
|
|
/// the original dns query
|
|
|
|
Message m_Query;
|
2018-12-13 22:20:12 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
public:
|
|
|
|
explicit QueryJob_Base(Message query) : m_Query{std::move(query)}
|
|
|
|
{}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
virtual ~QueryJob_Base() = default;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
Message&
|
|
|
|
Underlying()
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
return m_Query;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
const Message&
|
|
|
|
Underlying() const
|
|
|
|
{
|
|
|
|
return m_Query;
|
|
|
|
}
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
/// cancel this operation and inform anyone who cares
|
|
|
|
void
|
|
|
|
Cancel() const;
|
2018-12-04 16:35:25 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
/// send a raw buffer back to the querier
|
|
|
|
virtual void
|
|
|
|
SendReply(llarp::OwnedBuffer replyBuf) const = 0;
|
|
|
|
};
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
class PacketSource_Base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~PacketSource_Base() = default;
|
|
|
|
|
|
|
|
/// return true if traffic with source and dest addresses would cause a
|
|
|
|
/// loop in resolution and thus should not be sent to query handlers
|
|
|
|
virtual bool
|
|
|
|
WouldLoop(const SockAddr& to, const SockAddr& from) const = 0;
|
|
|
|
|
|
|
|
/// send packet with src and dst address containing buf on this packet source
|
|
|
|
virtual void
|
|
|
|
SendTo(const SockAddr& to, const SockAddr& from, OwnedBuffer buf) const = 0;
|
|
|
|
|
|
|
|
/// stop reading packets and end operation
|
|
|
|
virtual void
|
|
|
|
Stop() = 0;
|
|
|
|
|
|
|
|
/// returns the sockaddr we are bound on if applicable
|
|
|
|
virtual std::optional<SockAddr>
|
|
|
|
BoundOn() const = 0;
|
|
|
|
};
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
/// a packet source which will override the sendto function of an wrapped packet source to
|
|
|
|
/// construct a raw ip packet as a reply
|
|
|
|
class PacketSource_Wrapper : public PacketSource_Base
|
|
|
|
{
|
|
|
|
std::weak_ptr<PacketSource_Base> m_Wrapped;
|
|
|
|
std::function<void(net::IPPacket)> m_WritePacket;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PacketSource_Wrapper(
|
|
|
|
std::weak_ptr<PacketSource_Base> wrapped, std::function<void(net::IPPacket)> write_packet)
|
|
|
|
: m_Wrapped{wrapped}, m_WritePacket{write_packet}
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WouldLoop(const SockAddr& to, const SockAddr& from) const override
|
|
|
|
{
|
|
|
|
if (auto ptr = m_Wrapped.lock())
|
|
|
|
return ptr->WouldLoop(to, from);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SendTo(const SockAddr& to, const SockAddr& from, OwnedBuffer buf) const override
|
|
|
|
{
|
|
|
|
m_WritePacket(net::IPPacket::make_udp(to, from, std::move(buf)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// stop reading packets and end operation
|
|
|
|
void
|
|
|
|
Stop() override
|
|
|
|
{
|
|
|
|
if (auto ptr = m_Wrapped.lock())
|
|
|
|
ptr->Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns the sockaddr we are bound on if applicable
|
|
|
|
std::optional<SockAddr>
|
|
|
|
BoundOn() const override
|
|
|
|
{
|
|
|
|
if (auto ptr = m_Wrapped.lock())
|
|
|
|
return ptr->BoundOn();
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
/// non complex implementation of QueryJob_Base for use in things that
|
|
|
|
/// only ever called on the mainloop thread
|
|
|
|
class QueryJob : public QueryJob_Base, std::enable_shared_from_this<QueryJob>
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
std::shared_ptr<PacketSource_Base> src;
|
2022-04-07 20:44:23 +00:00
|
|
|
const SockAddr resolver;
|
|
|
|
const SockAddr asker;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit QueryJob(
|
2022-07-28 16:07:38 +00:00
|
|
|
std::shared_ptr<PacketSource_Base> source,
|
2022-04-07 20:44:23 +00:00
|
|
|
const Message& query,
|
|
|
|
const SockAddr& to_,
|
|
|
|
const SockAddr& from_)
|
|
|
|
: QueryJob_Base{query}, src{source}, resolver{to_}, asker{from_}
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
SendReply(llarp::OwnedBuffer replyBuf) const override
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
src->SendTo(asker, resolver, std::move(replyBuf));
|
2022-04-07 20:44:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// handler of dns query hooking
|
|
|
|
/// intercepts dns for internal processing
|
|
|
|
class Resolver_Base
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// return the sorting order for this resolver
|
|
|
|
/// lower means it will be tried first
|
|
|
|
virtual int
|
|
|
|
Rank() const = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~Resolver_Base() = default;
|
|
|
|
|
|
|
|
/// less than via rank
|
|
|
|
bool
|
|
|
|
operator<(const Resolver_Base& other) const
|
|
|
|
{
|
|
|
|
return Rank() < other.Rank();
|
|
|
|
}
|
2019-05-22 16:20:03 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
/// greater than via rank
|
|
|
|
bool
|
|
|
|
operator>(const Resolver_Base& other) const
|
|
|
|
{
|
|
|
|
return Rank() > other.Rank();
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
/// get local socket address that queries are sent from
|
|
|
|
virtual std::optional<SockAddr>
|
|
|
|
GetLocalAddr() const
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
/// get printable name
|
|
|
|
virtual std::string_view
|
|
|
|
ResolverName() const = 0;
|
|
|
|
|
|
|
|
/// reset state
|
|
|
|
virtual void
|
|
|
|
ResetInternalState(){};
|
|
|
|
|
|
|
|
/// cancel all pending requests and ceace further operation
|
|
|
|
virtual void
|
|
|
|
CancelPendingQueries(){};
|
|
|
|
/// attempt to handle a dns message
|
|
|
|
/// returns true if we consumed this query and it should not be processed again
|
|
|
|
virtual bool
|
|
|
|
MaybeHookDNS(
|
2022-07-28 16:07:38 +00:00
|
|
|
std::shared_ptr<PacketSource_Base> source,
|
2022-04-07 20:44:23 +00:00
|
|
|
const Message& query,
|
|
|
|
const SockAddr& to,
|
|
|
|
const SockAddr& from) = 0;
|
|
|
|
|
|
|
|
/// Returns true if a packet with to and from addresses is something that would cause a
|
|
|
|
/// resolution loop and thus should not be used on this resolver
|
|
|
|
virtual bool
|
|
|
|
WouldLoop(const SockAddr& to, const SockAddr& from) const
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
(void)to;
|
|
|
|
(void)from;
|
|
|
|
return false;
|
2018-12-03 22:22:59 +00:00
|
|
|
};
|
2022-04-07 20:44:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Base class for DNS proxy
|
|
|
|
class Server : public std::enable_shared_from_this<Server>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// add a packet source to this server, does share ownership
|
|
|
|
void
|
|
|
|
AddPacketSource(std::shared_ptr<PacketSource_Base> resolver);
|
|
|
|
/// add a resolver to this packet handler, does share ownership
|
|
|
|
void
|
|
|
|
AddResolver(std::shared_ptr<Resolver_Base> resolver);
|
|
|
|
|
2022-06-22 16:14:33 +00:00
|
|
|
/// create the platform dependant dns stuff
|
|
|
|
virtual std::shared_ptr<I_Platform>
|
|
|
|
CreatePlatform() const;
|
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
public:
|
|
|
|
virtual ~Server() = default;
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
explicit Server(EventLoop_ptr loop, llarp::DnsConfig conf, unsigned int netif_index);
|
2022-04-07 20:44:23 +00:00
|
|
|
|
|
|
|
/// returns all sockaddr we have from all of our PacketSources
|
|
|
|
std::vector<SockAddr>
|
|
|
|
BoundPacketSourceAddrs() const;
|
|
|
|
|
|
|
|
/// returns the first sockaddr we have on our packet sources if we have one
|
|
|
|
std::optional<SockAddr>
|
|
|
|
FirstBoundPacketSourceAddr() const;
|
|
|
|
|
|
|
|
/// add a resolver to this packet handler, does not share ownership
|
|
|
|
void
|
|
|
|
AddResolver(std::weak_ptr<Resolver_Base> resolver);
|
|
|
|
|
|
|
|
/// add a packet source to this server, does not share ownership
|
|
|
|
void
|
|
|
|
AddPacketSource(std::weak_ptr<PacketSource_Base> resolver);
|
|
|
|
|
|
|
|
/// create a packet source bound on bindaddr but does not add it
|
|
|
|
virtual std::shared_ptr<PacketSource_Base>
|
|
|
|
MakePacketSourceOn(const SockAddr& bindaddr, const llarp::DnsConfig& conf);
|
|
|
|
|
|
|
|
/// sets up all internal binds and such and begins operation
|
|
|
|
virtual void
|
|
|
|
Start();
|
|
|
|
|
|
|
|
/// stops all operation
|
|
|
|
virtual void
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
/// reset the internal state
|
|
|
|
virtual void
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
/// create the default resolver for out config
|
|
|
|
virtual std::shared_ptr<Resolver_Base>
|
|
|
|
MakeDefaultResolver();
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
std::vector<std::weak_ptr<Resolver_Base>>
|
|
|
|
GetAllResolvers() const;
|
|
|
|
|
|
|
|
/// feed a packet buffer from a packet source.
|
2022-04-07 20:44:23 +00:00
|
|
|
/// returns true if we decided to process the packet and consumed it
|
|
|
|
/// returns false if we dont want to process the packet
|
|
|
|
bool
|
|
|
|
MaybeHandlePacket(
|
2022-07-28 16:07:38 +00:00
|
|
|
std::shared_ptr<PacketSource_Base> pktsource,
|
2022-04-07 20:44:23 +00:00
|
|
|
const SockAddr& resolver,
|
|
|
|
const SockAddr& from,
|
|
|
|
llarp::OwnedBuffer buf);
|
2022-06-22 16:14:33 +00:00
|
|
|
/// set which dns mode we are in.
|
|
|
|
/// true for intercepting all queries. false for just .loki and .snode
|
|
|
|
void
|
|
|
|
SetDNSMode(bool all_queries);
|
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
protected:
|
|
|
|
EventLoop_ptr m_Loop;
|
|
|
|
llarp::DnsConfig m_Config;
|
2022-06-22 16:14:33 +00:00
|
|
|
std::shared_ptr<I_Platform> m_Platform;
|
2022-04-07 20:44:23 +00:00
|
|
|
|
|
|
|
private:
|
2022-07-28 16:07:38 +00:00
|
|
|
const unsigned int m_NetIfIndex;
|
2022-04-07 20:44:23 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
std::vector<std::weak_ptr<PacketSource_Base>> m_PacketSources;
|
|
|
|
std::vector<std::shared_ptr<PacketSource_Base>> m_OwnedPacketSources;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp::dns
|