lokinet/llarp/router/route_poker.cpp
Jeff 49b9ad7197
tun code refactor (#1495)
* partial tun code refactor

* take out the trash

* move vpn platform code into llarp/vpn/platform.cpp

* fix hive build

* fix win32

* fix memory leak on win32

* reduce cpu use

* make macos compile

* win32 patches:

* use wepoll for zmq
* use all cores on windows iocp read loop

* fix zmq patch for windows

* clean up cmake for win32

* add uninstall before reinstall option to win32 installer

* more ipv6 stuff

* make it compile

* fix up route poker

* remove an unneeded code block in macos wtf

* always use call to system

* fix route poker behavior on macos

* disable ipv6 on windows for now

* cpu perf improvement:

* colease calls to Router::PumpLL to 1 per event loop wakeup

* set up THEN add addresses

* emulate proactor event loop on win32

* remove excessively verbose error message

* fix issue #1499

* exclude uv_poll from win32 so that it can start up

* update logtag to include directory

* create minidump on windows if there was a crash

* make windows happy

* use dmp suffix on minidump files

* typo fix

* address feedback from jason
* use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR
* quote $@ in apply-patches in case path has spaces in it

* address feedback from tom

* remove llarp/ev/pipe
* add comments for clairification
* make event loop queue size constant named
2021-01-11 18:13:22 -05:00

160 lines
3.4 KiB
C++

#include <router/route_poker.hpp>
#include <router/abstractrouter.hpp>
#include <net/route.hpp>
#include <service/context.hpp>
#include <unordered_set>
namespace llarp
{
void
RoutePoker::AddRoute(huint32_t ip)
{
m_PokedRoutes.emplace(ip, m_CurrentGateway);
if (m_CurrentGateway.h == 0)
{
llarp::LogDebug("RoutePoker::AddRoute no current gateway, cannot enable route.");
}
else if (m_Enabled or m_Enabling)
{
llarp::LogInfo(
"RoutePoker::AddRoute enabled, enabling route to ", ip, " via ", m_CurrentGateway);
EnableRoute(ip, m_CurrentGateway);
}
else
{
llarp::LogDebug("RoutePoker::AddRoute disabled, not enabling route.");
}
}
void
RoutePoker::DisableRoute(huint32_t ip, huint32_t gateway)
{
net::DelRoute(ip.ToString(), gateway.ToString());
}
void
RoutePoker::EnableRoute(huint32_t ip, huint32_t gateway)
{
net::AddRoute(ip.ToString(), gateway.ToString());
}
void
RoutePoker::DelRoute(huint32_t ip)
{
const auto itr = m_PokedRoutes.find(ip);
if (itr == m_PokedRoutes.end())
return;
m_PokedRoutes.erase(itr);
if (m_Enabled)
DisableRoute(itr->first, itr->second);
}
void
RoutePoker::Init(AbstractRouter* router, bool enable)
{
m_Router = router;
m_Enabled = enable;
m_CurrentGateway = {0};
}
void
RoutePoker::DeleteAllRoutes()
{
// DelRoute will check enabled, so no need here
for (const auto& [ip, gateway] : m_PokedRoutes)
DelRoute(ip);
}
void
RoutePoker::DisableAllRoutes()
{
for (const auto& [ip, gateway] : m_PokedRoutes)
DisableRoute(ip, gateway);
}
void
RoutePoker::EnableAllRoutes()
{
for (auto& [ip, gateway] : m_PokedRoutes)
{
gateway = m_CurrentGateway;
EnableRoute(ip, m_CurrentGateway);
}
}
RoutePoker::~RoutePoker()
{
for (const auto& [ip, gateway] : m_PokedRoutes)
{
if (gateway.h)
net::DelRoute(ip.ToString(), gateway.ToString());
}
}
std::optional<huint32_t>
RoutePoker::GetDefaultGateway() const
{
if (not m_Router)
throw std::runtime_error("Attempting to use RoutePoker before calling Init");
const auto ep = m_Router->hiddenServiceContext().GetDefault();
const auto gateways = net::GetGatewaysNotOnInterface(ep->GetIfName());
if (gateways.empty())
{
return std::nullopt;
}
huint32_t addr{};
addr.FromString(gateways[0]);
return addr;
}
void
RoutePoker::Update()
{
if (not m_Router)
throw std::runtime_error("Attempting to use RoutePoker before calling Init");
const auto maybe = GetDefaultGateway();
if (not maybe.has_value())
{
LogError("Network is down");
return;
}
const huint32_t gateway = *maybe;
if (m_CurrentGateway != gateway or m_Enabling)
{
LogInfo("found default gateway: ", gateway);
m_CurrentGateway = gateway;
if (not m_Enabling) // if route was already set up
DisableAllRoutes();
EnableAllRoutes();
const auto ep = m_Router->hiddenServiceContext().GetDefault();
net::AddDefaultRouteViaInterface(ep->GetIfName());
}
}
void
RoutePoker::Enable()
{
if (m_Enabled)
return;
m_Enabling = true;
Update();
m_Enabling = false;
m_Enabled = true;
}
void
RoutePoker::Disable()
{
if (not m_Enabled)
return;
DisableAllRoutes();
}
} // namespace llarp