Skip to content

Commit

Permalink
fall back to null vpn platform when we have no implementation for the…
Browse files Browse the repository at this point in the history
… current target
  • Loading branch information
majestrate committed Jun 22, 2022
1 parent e338597 commit 4c16a37
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 17 deletions.
96 changes: 96 additions & 0 deletions llarp/vpn/null.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once
#include <llarp/ev/vpn.hpp>
#include <unistd.h>

namespace llarp::vpn
{
class NullInterface : public NetworkInterface
{
/// we use a pipe because it isnt going to poll itself
int m_pipe[2];

public:
NullInterface()
{
::pipe(m_pipe);
}

virtual ~NullInterface()
{
::close(m_pipe[1]);
::close(m_pipe[0]);
}

int
PollFD() const override
{
return m_pipe[0];
}

/// the interface's name
std::string
IfName() const override
{
return "";
}

net::IPPacket
ReadNextPacket() override
{
return net::IPPacket{};
}

/// write a packet to the interface
/// returns false if we dropped it
bool WritePacket(net::IPPacket) override
{
return true;
}
};

class NopRouteManager : public IRouteManager
{
public:
void AddRoute(IPVariant_t, IPVariant_t) override{};

void DelRoute(IPVariant_t, IPVariant_t) override{};

void AddDefaultRouteViaInterface(std::string) override{};

void DelDefaultRouteViaInterface(std::string) override{};

void
AddRouteViaInterface(NetworkInterface&, IPRange) override{};

void
DelRouteViaInterface(NetworkInterface&, IPRange) override{};

std::vector<IPVariant_t> GetGatewaysNotOnInterface(std::string) override
{
return {};
}
};

class NullPlatform : public Platform
{
NopRouteManager _routes;

public:
NullPlatform() : Platform{}, _routes{}
{}

virtual ~NullPlatform() = default;

std::shared_ptr<NetworkInterface>
ObtainInterface(InterfaceInfo, AbstractRouter*)
{
return std::static_pointer_cast<NetworkInterface>(std::make_shared<NullInterface>());
}

IRouteManager&
RouteManager() override
{
return _routes;
}
};
} // namespace llarp::vpn
26 changes: 9 additions & 17 deletions llarp/vpn/platform.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

#include <llarp/ev/vpn.hpp>

#include <llarp/vpn/null.hpp>
#ifdef _WIN32
#include "win32.hpp"
#endif
Expand All @@ -12,27 +11,20 @@
#endif
#endif

#include <exception>

namespace llarp::vpn
{
std::shared_ptr<Platform>
MakeNativePlatform(llarp::Context* ctx)
{
(void)ctx;
std::shared_ptr<Platform> plat;
#ifdef _WIN32
plat = std::make_shared<vpn::Win32Platform>();
#endif
#ifdef __linux__
#ifdef ANDROID
plat = std::make_shared<vpn::AndroidPlatform>(ctx);
#else
plat = std::make_shared<vpn::LinuxPlatform>();
#endif
#endif
#ifdef __APPLE__
throw std::runtime_error{"not supported"};
auto nullplat = std::make_shared<NullPlatform>();
std::shared_ptr<Platform> plat = nullplat;
#if defined(_WIN32)
plat = std::make_shared<Win32Platform>();
#elif defined(ANDROID)
plat = std::make_shared<AndroidPlatform>(ctx);
#elif defined(__linux__)
plat = std::make_shared<LinuxPlatform>();
#endif
return plat;
}
Expand Down

0 comments on commit 4c16a37

Please sign in to comment.