---
title: llarp/layers/platform/platform_layer.hpp

---

# llarp/layers/platform/platform_layer.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[llarp::dns](Namespaces/namespacellarp_1_1dns.md)**  |
| **[llarp::vpn](Namespaces/namespacellarp_1_1vpn.md)**  |
| **[llarp::layers](Namespaces/namespacellarp_1_1layers.md)**  |
| **[llarp::layers::flow](Namespaces/namespacellarp_1_1layers_1_1flow.md)**  |
| **[llarp::layers::platform](Namespaces/namespacellarp_1_1layers_1_1platform.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| class | **[llarp::layers::platform::OSTraffic_IO_Base](Classes/classllarp_1_1layers_1_1platform_1_1OSTraffic__IO__Base.md)**  |
| class | **[llarp::layers::platform::PlatformLayer](Classes/classllarp_1_1layers_1_1platform_1_1PlatformLayer.md)** <br>responsible for bridging the os and the flow layer.  |




## Source code

```cpp
#pragma once

#include "addr_mapper.hpp"
#include "llarp/layers/flow/flow_addr.hpp"
#include "llarp/layers/platform/platform_addr.hpp"
#include "name_cache.hpp"
#include "platform_stats.hpp"
#include "os_traffic.hpp"
#include <llarp/config/config.hpp>
#include <llarp/net/ip_packet.hpp>
#include <llarp/net/ip_range.hpp>

#include <llarp/util/formattable.hpp>
#include <llarp/util/types.hpp>

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <valarray>
#include <vector>

namespace llarp::dns
{
  class Server;
}

namespace llarp::vpn
{
  class NetworkInterface;
  class IRouteManager;
}  // namespace llarp::vpn

namespace llarp::layers::flow
{
  class FlowLayer;
  class FlowAddr;
  struct FlowInfo;
  struct FlowTraffic;

}  // namespace llarp::layers::flow

namespace llarp::layers::platform
{

  class DNSZone;
  class DNSQueryHandler;

  // handles reading and writing os traffic, like ip packets or whatever embedded lokinet wants to
  // do.
  class OSTraffic_IO_Base
  {
    PlatformAddr _our_addr;
    std::vector<OSTraffic> _recv_queue;
    std::atomic<bool> _running;

    void got_ip_packet(net::IPPacket);

   protected:
    std::weak_ptr<EventLoop> _loop;
    std::shared_ptr<vpn::NetworkInterface> _netif;

   public:
    explicit OSTraffic_IO_Base(const EventLoop_ptr&);

    constexpr const PlatformAddr&
    our_platform_addr() const
    {
      return _our_addr;
    }

    void
    attach(
        std::shared_ptr<vpn::NetworkInterface> netif, std::shared_ptr<EventLoopWakeup> wakeup_recv);

    std::vector<OSTraffic>
    read_platform_traffic();

    void
    write_platform_traffic(std::vector<OSTraffic>&& traff);

    void
    detach();
  };

  struct platform_io_wakeup_handler;

  class PlatformLayer
  {
   protected:
    friend struct platform_io_wakeup_handler;

    AbstractRouter& _router;
    flow::FlowLayer& _flow_layer;

    std::shared_ptr<EventLoopWakeup> _os_send;
    std::shared_ptr<EventLoopWakeup> _os_recv;

    NetworkConfig _netconf;

    std::shared_ptr<DNSQueryHandler> _dns_query_handler;

    std::weak_ptr<vpn::NetworkInterface> _netif;

    OSTraffic_IO_Base _io;
    void
    on_io();

   public:
    const std::shared_ptr<DNSZone> dns;

    AddrMapper addr_mapper;
    PlatformLayer(const PlatformLayer&) = delete;
    PlatformLayer(PlatformLayer&&) = delete;

    PlatformLayer(
        AbstractRouter& router, flow::FlowLayer& flow_layer, const NetworkConfig& netconf);

    void
    start();

    void
    stop();

    std::shared_ptr<vpn::NetworkInterface>
    vpn_interface() const;

    vpn::IRouteManager*
    route_manager() const;

    void
    os_traffic(std::vector<OSTraffic>&& pkts);

    void
    flow_traffic(std::vector<flow::FlowTraffic>&& traff);

    void
    map_remote(
        std::string name,
        std::string auth,
        std::vector<IPRange> ranges,
        std::optional<PlatformAddr> src = std::nullopt,
        std::function<void(std::optional<flow::FlowInfo>, std::string)> result_handler = nullptr);

    void
    map_exit(
        std::string name,
        std::string auth,
        std::vector<IPRange> ranges,
        std::function<void(bool, std::string)> result_handler);

    void
    unmap_exit(flow::FlowAddr remote, std::optional<IPRange> range = std::nullopt);

    void
    unmap_all_exits_on_range(IPRange range);

    PlatformStats
    current_stats() const;
  };
}  // namespace llarp::layers::platform
```


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

Updated on 2023-03-06 at 20:36:34 +0000
