diff --git a/llarp/vpn/null.hpp b/llarp/vpn/null.hpp new file mode 100644 index 0000000000..c247b99e9f --- /dev/null +++ b/llarp/vpn/null.hpp @@ -0,0 +1,96 @@ +#pragma once +#include +#include + +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 GetGatewaysNotOnInterface(std::string) override + { + return {}; + } + }; + + class NullPlatform : public Platform + { + NopRouteManager _routes; + + public: + NullPlatform() : Platform{}, _routes{} + {} + + virtual ~NullPlatform() = default; + + std::shared_ptr + ObtainInterface(InterfaceInfo, AbstractRouter*) + { + return std::static_pointer_cast(std::make_shared()); + } + + IRouteManager& + RouteManager() override + { + return _routes; + } + }; +} // namespace llarp::vpn diff --git a/llarp/vpn/platform.cpp b/llarp/vpn/platform.cpp index 341e28fcba..b09586034b 100644 --- a/llarp/vpn/platform.cpp +++ b/llarp/vpn/platform.cpp @@ -1,6 +1,5 @@ -#include - +#include #ifdef _WIN32 #include "win32.hpp" #endif @@ -12,27 +11,20 @@ #endif #endif -#include - namespace llarp::vpn { std::shared_ptr MakeNativePlatform(llarp::Context* ctx) { (void)ctx; - std::shared_ptr plat; -#ifdef _WIN32 - plat = std::make_shared(); -#endif -#ifdef __linux__ -#ifdef ANDROID - plat = std::make_shared(ctx); -#else - plat = std::make_shared(); -#endif -#endif -#ifdef __APPLE__ - throw std::runtime_error{"not supported"}; + auto nullplat = std::make_shared(); + std::shared_ptr plat = nullplat; +#if defined(_WIN32) + plat = std::make_shared(); +#elif defined(ANDROID) + plat = std::make_shared(ctx); +#elif defined(__linux__) + plat = std::make_shared(); #endif return plat; }