diff --git a/llarp/net/ip_range.hpp b/llarp/net/ip_range.hpp index 201473852..ca3ff25c4 100644 --- a/llarp/net/ip_range.hpp +++ b/llarp/net/ip_range.hpp @@ -97,6 +97,12 @@ namespace llarp return Contains(net::ExpandV4(ip)); } + inline bool + Contains(const net::ipaddr_t& ip) const + { + return var::visit([this](auto&& ip) { return Contains(ToHost(ip)); }, ip); + } + /// get the highest address on this range constexpr huint128_t HighestAddr() const diff --git a/llarp/net/win32.cpp b/llarp/net/win32.cpp index 4f166fb84..295a5e3b2 100644 --- a/llarp/net/win32.cpp +++ b/llarp/net/win32.cpp @@ -201,6 +201,11 @@ namespace llarp::net saddr.asIPv6(), ipaddr_netmask_bits(addr->OnLinkPrefixLength, addr->Address.lpSockaddr->sa_family)); } + if (auto* addr = a->FirstGatewayAddress) + { + SockAddr gw{*addr->Address.lpSockaddr}; + cur.gateway = gw.getIP(); + } }); return all; } diff --git a/llarp/router/route_poker.cpp b/llarp/router/route_poker.cpp index 1c55e38ad..e1946bceb 100644 --- a/llarp/router/route_poker.cpp +++ b/llarp/router/route_poker.cpp @@ -25,6 +25,7 @@ namespace llarp DisableRoute(ip, gw); // update and add new mapping gw = *m_CurrentGateway; + log::info(logcat, "add route {} via {}", ip, gw); EnableRoute(ip, gw); } else @@ -57,7 +58,7 @@ namespace llarp const auto itr = m_PokedRoutes.find(ip); if (itr == m_PokedRoutes.end()) return; - + log::info(logcat, "del route {} via {}", itr->first, itr->second); DisableRoute(itr->first, itr->second); m_PokedRoutes.erase(itr); } @@ -69,7 +70,10 @@ namespace llarp if (m_Router->IsServiceNode()) return; - m_Router->loop()->call_every(100ms, weak_from_this(), [this]() { Update(); }); + m_Router->loop()->call_every(100ms, weak_from_this(), [self = weak_from_this()]() { + if (auto ptr = self.lock()) + ptr->Update(); + }); } void @@ -186,6 +190,8 @@ namespace llarp m_CurrentGateway = next_gw; } } + else if (m_Router->HasClientExit()) + Up(); } void @@ -201,7 +207,7 @@ namespace llarp void RoutePoker::Up() { - if (IsEnabled()) + if (IsEnabled() and m_CurrentGateway and not m_up) { vpn::IRouteManager& route = m_Router->GetVPNPlatform()->RouteManager(); @@ -216,6 +222,9 @@ namespace llarp const auto ep = m_Router->hiddenServiceContext().GetDefault(); if (auto* vpn = ep->GetVPNInterface()) route.AddDefaultRouteViaInterface(*vpn); + + m_up = true; + log::info(logcat, "route poker up"); } SetDNSMode(true); } @@ -229,7 +238,7 @@ namespace llarp // remove default route - if (IsEnabled()) + if (IsEnabled() and m_up) { vpn::IRouteManager& route = m_Router->GetVPNPlatform()->RouteManager(); const auto ep = m_Router->hiddenServiceContext().GetDefault(); @@ -238,6 +247,8 @@ namespace llarp // delete route blackhole route.DelBlackhole(); + m_up = false; + log::info(logcat, "route poker down"); } SetDNSMode(false); } diff --git a/llarp/router/route_poker.hpp b/llarp/router/route_poker.hpp index 7473617e1..9a0feb65c 100644 --- a/llarp/router/route_poker.hpp +++ b/llarp/router/route_poker.hpp @@ -63,5 +63,6 @@ namespace llarp std::optional m_CurrentGateway; AbstractRouter* m_Router = nullptr; + bool m_up{false}; }; } // namespace llarp diff --git a/llarp/vpn/win32.cpp b/llarp/vpn/win32.cpp index 1c3dcfc74..1562cef1c 100644 --- a/llarp/vpn/win32.cpp +++ b/llarp/vpn/win32.cpp @@ -72,18 +72,20 @@ namespace llarp::win32 std::vector VPNPlatform::GetGatewaysNotOnInterface(NetworkInterface& vpn) { - std::vector gateways; + std::set gateways; - auto idx = vpn.Info().index; - using UInt_t = decltype(idx); + const auto ifaddr = vpn.Info()[0]; for (const auto& iface : Net().AllNetworkInterfaces()) { - if (static_cast(iface.index) == idx) + if (not iface.gateway) continue; - if (iface.gateway) - gateways.emplace_back(*iface.gateway); + for (const auto& range : iface.addrs) + { + if (not range.Contains(ifaddr)) + gateways.emplace(*iface.gateway); + } } - return gateways; + return {gateways.begin(), gateways.end()}; } void