---
title: llarp/dns/server.hpp

---

# llarp/dns/server.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[llarp::dns](Namespaces/namespacellarp_1_1dns.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| class | **[llarp::dns::QueryJob_Base](Classes/classllarp_1_1dns_1_1QueryJob__Base.md)** <br>a job handling 1 dns query  |
| class | **[llarp::dns::PacketSource_Base](Classes/classllarp_1_1dns_1_1PacketSource__Base.md)**  |
| class | **[llarp::dns::QueryJob](Classes/classllarp_1_1dns_1_1QueryJob.md)** <br>non complex implementation of [QueryJob_Base](Classes/classllarp_1_1dns_1_1QueryJob__Base.md) for use in things that only ever called on the mainloop thread  |
| class | **[llarp::dns::Resolver_Base](Classes/classllarp_1_1dns_1_1Resolver__Base.md)** <br>handler of dns query hooking intercepts dns for internal processing  |
| class | **[llarp::dns::Server](Classes/classllarp_1_1dns_1_1Server.md)**  |




## Source code

```cpp
#pragma once

#include "message.hpp"
#include "platform.hpp"
#include <llarp/config/config.hpp>
#include <llarp/ev/ev.hpp>
#include <llarp/net/net.hpp>
#include <llarp/util/fs.hpp>
#include <set>

namespace llarp::dns
{
  class QueryJob_Base
  {
   protected:
    Message m_Query;

   public:
    explicit QueryJob_Base(Message query) : m_Query{std::move(query)}
    {}

    virtual ~QueryJob_Base() = default;

    Message&
    Underlying()
    {
      return m_Query;
    }

    const Message&
    Underlying() const
    {
      return m_Query;
    }

    void
    Cancel() const;

    virtual void
    SendReply(llarp::OwnedBuffer replyBuf) const = 0;
  };

  class PacketSource_Base
  {
   public:
    virtual ~PacketSource_Base() = default;

    virtual bool
    WouldLoop(const SockAddr& to, const SockAddr& from) const = 0;

    virtual void
    SendTo(const SockAddr& to, const SockAddr& from, OwnedBuffer buf) const = 0;

    virtual void
    Stop() = 0;

    virtual std::optional<SockAddr>
    BoundOn() const = 0;
  };

  class QueryJob : public QueryJob_Base, std::enable_shared_from_this<QueryJob>
  {
    std::shared_ptr<PacketSource_Base> src;
    const SockAddr resolver;
    const SockAddr asker;

   public:
    explicit QueryJob(
        std::shared_ptr<PacketSource_Base> source,
        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
    {
      src->SendTo(asker, resolver, std::move(replyBuf));
    }
  };

  class Resolver_Base
  {
   protected:
    virtual int
    Rank() const = 0;

   public:
    virtual ~Resolver_Base() = default;

    bool
    operator<(const Resolver_Base& other) const
    {
      return Rank() < other.Rank();
    }

    bool
    operator>(const Resolver_Base& other) const
    {
      return Rank() > other.Rank();
    }

    virtual std::string_view
    ResolverName() const = 0;

    virtual void
    ResetInternalState(){};

    virtual void
    CancelPendingQueries(){};
    virtual bool
    MaybeHookDNS(
        std::shared_ptr<PacketSource_Base> source,
        const Message& query,
        const SockAddr& to,
        const SockAddr& from) = 0;

    virtual bool
    WouldLoop(const SockAddr& to, const SockAddr& from) const
    {
      (void)to;
      (void)from;
      return false;
    };
  };

  // Base class for DNS proxy
  class Server : public std::enable_shared_from_this<Server>
  {
   protected:
    void
    AddPacketSource(std::shared_ptr<PacketSource_Base> resolver);
    void
    AddResolver(std::shared_ptr<Resolver_Base> resolver);

    virtual std::shared_ptr<I_Platform>
    CreatePlatform() const;

   public:
    virtual ~Server() = default;

    explicit Server(EventLoop_ptr loop, llarp::DnsConfig conf, unsigned int netif_index);

    std::vector<SockAddr>
    BoundPacketSourceAddrs() const;

    std::optional<SockAddr>
    FirstBoundPacketSourceAddr() const;

    void
    AddResolver(std::weak_ptr<Resolver_Base> resolver);

    void
    AddPacketSource(std::weak_ptr<PacketSource_Base> resolver);

    virtual std::shared_ptr<PacketSource_Base>
    MakePacketSourceOn(const SockAddr& bindaddr, const llarp::DnsConfig& conf);

    virtual void
    Start();

    virtual void
    Stop();

    virtual void
    Reset();

    virtual std::shared_ptr<Resolver_Base>
    MakeDefaultResolver();

    bool
    MaybeHandlePacket(
        std::shared_ptr<PacketSource_Base> pktsource,
        const SockAddr& resolver,
        const SockAddr& from,
        llarp::OwnedBuffer buf);

    void
    SetDNSMode(bool all_queries);

   protected:
    EventLoop_ptr m_Loop;
    llarp::DnsConfig m_Config;
    std::shared_ptr<I_Platform> m_Platform;

   private:
    const unsigned int m_NetIfIndex;
    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
```


-------------------------------

Updated on 2022-09-01 at 18:16:54 +0000
