lokinet/llarp/apple/route_manager.cpp

102 lines
2.7 KiB
C++
Raw Normal View History

#include "route_manager.hpp"
#include <llarp/handlers/tun.hpp>
#include <llarp/service/context.hpp>
#include <llarp.hpp>
#include <memory>
2021-09-01 18:31:45 +00:00
namespace llarp::apple
{
void
RouteManager::check_trampoline(bool enable)
{
if (trampoline_active == enable)
return;
auto router = context.router;
if (!router)
{
LogError("Cannot reconfigure to use DNS trampoline: no router");
return;
}
2021-09-01 18:31:45 +00:00
std::shared_ptr<llarp::handlers::TunEndpoint> tun;
router->hiddenServiceContext().ForEachService([&tun](const auto& name, const auto ep) {
tun = std::dynamic_pointer_cast<llarp::handlers::TunEndpoint>(ep);
return !tun;
2021-09-01 18:31:45 +00:00
});
2021-09-01 18:31:45 +00:00
if (!tun)
{
LogError("Cannot reconfigure to use DNS trampoline: no tun endpoint found (!?)");
return;
}
2021-09-01 18:31:45 +00:00
if (enable)
saved_upstream_dns =
tun->ReconfigureDNS({SockAddr{127, 0, 0, 1, huint16_t{dns_trampoline_port}}});
else
tun->ReconfigureDNS(std::move(saved_upstream_dns));
trampoline_active = enable;
}
2021-09-01 18:31:45 +00:00
void RouteManager::AddDefaultRouteViaInterface(std::string)
{
check_trampoline(true);
if (callback_context and route_callbacks.add_default_route)
2021-09-01 18:31:45 +00:00
route_callbacks.add_default_route(callback_context);
}
2021-09-01 18:31:45 +00:00
void RouteManager::DelDefaultRouteViaInterface(std::string)
{
check_trampoline(false);
if (callback_context and route_callbacks.del_default_route)
2021-09-01 18:31:45 +00:00
route_callbacks.del_default_route(callback_context);
}
2021-09-01 18:31:45 +00:00
void
RouteManager::AddRouteViaInterface(vpn::NetworkInterface&, IPRange range)
{
check_trampoline(true);
if (callback_context)
{
2021-09-01 18:31:45 +00:00
if (range.IsV4())
{
if (route_callbacks.add_ipv4_route)
route_callbacks.add_ipv4_route(
range.BaseAddressString().c_str(),
net::TruncateV6(range.netmask_bits).ToString().c_str(),
callback_context);
}
else
{
if (route_callbacks.add_ipv6_route)
route_callbacks.add_ipv6_route(
range.BaseAddressString().c_str(), range.HostmaskBits(), callback_context);
}
}
2021-09-01 18:31:45 +00:00
}
2021-09-01 18:31:45 +00:00
void
RouteManager::DelRouteViaInterface(vpn::NetworkInterface&, IPRange range)
{
check_trampoline(false);
if (callback_context)
{
2021-09-01 18:31:45 +00:00
if (range.IsV4())
{
if (route_callbacks.del_ipv4_route)
route_callbacks.del_ipv4_route(
range.BaseAddressString().c_str(),
net::TruncateV6(range.netmask_bits).ToString().c_str(),
callback_context);
}
else
{
if (route_callbacks.del_ipv6_route)
route_callbacks.del_ipv6_route(
range.BaseAddressString().c_str(), range.HostmaskBits(), callback_context);
}
}
2021-09-01 18:31:45 +00:00
}
2021-09-01 18:31:45 +00:00
} // namespace llarp::apple