diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 0225c7259..cf4ba1735 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -1000,10 +1000,10 @@ namespace llarp throw std::invalid_argument{"[paths]:unique-range-size must be between 4 and 32"}; m_UniqueHopsNetmaskSize = arg; }, - Comment{"In path hop selection use this as the default netmkask for testing if we should " - "include a relay in our hop list", - "i.e. 32 for uniuqe ip addresses for every hop, 24 for all hops are in a different " - "/24, 16 for all hops are in a different /16, etc..."}); + Comment{"Netmask for router path selection; each router must be from a distinct IP subnet " + "of the given size.", + "E.g. 16 ensures that all routers are using distinct /16 IP addresses."}); + #ifdef WITH_GEOIP conf.defineOption( "paths", @@ -1014,32 +1014,27 @@ namespace llarp m_ExcludeCountries.emplace(lowercase_ascii_string(std::move(arg))); }, Comment{"exclude a country given its 2 letter country code from being used in path builds", - "i.e. exlcude-country=DE", - "can be listed multiple times to exlcude multiple countries"}); + "e.g. exclude-country=DE", + "can be listed multiple times to exclude multiple countries"}); #endif } bool - PeerSelectionConfig::Acceptable(std::set rcs) const + PeerSelectionConfig::Acceptable(const std::set& rcs) const { if (m_UniqueHopsNetmaskSize) { - auto makeRange = - [netmask = netmask_ipv6_bits(96 + *m_UniqueHopsNetmaskSize)](IpAddress addr) -> IPRange { - return IPRange{net::ExpandV4(addr.toIP()) & netmask, netmask}; - }; - + const auto netmask = netmask_ipv6_bits(96 + *m_UniqueHopsNetmaskSize); std::set seenRanges; for (const auto& hop : rcs) { for (const auto& addr : hop.addrs) { - const auto range = makeRange(addr.toIpAddress()); - if (seenRanges.count(range)) + const auto network_addr = net::In6ToHUInt(addr.ip) & netmask; + if (auto [it, inserted] = seenRanges.emplace(network_addr, netmask); not inserted) { return false; } - seenRanges.emplace(range); } } } diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 8c93e8836..cecf47d48 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -87,7 +87,7 @@ namespace llarp /// return true if this set of router contacts is acceptable against this config bool - Acceptable(std::set hops) const; + Acceptable(const std::set& hops) const; }; struct NetworkConfig diff --git a/llarp/net/ip_range.hpp b/llarp/net/ip_range.hpp index fcb3b97fe..fc26db94f 100644 --- a/llarp/net/ip_range.hpp +++ b/llarp/net/ip_range.hpp @@ -14,6 +14,12 @@ namespace llarp huint128_t addr = {0}; huint128_t netmask_bits = {0}; + constexpr IPRange() + {} + constexpr IPRange(huint128_t address, huint128_t netmask) + : addr{std::move(address)}, netmask_bits{std::move(netmask)} + {} + static constexpr IPRange FromIPv4(byte_t a, byte_t b, byte_t c, byte_t d, byte_t mask) { diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 648b11df4..d9550ac98 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -206,7 +206,7 @@ namespace llarp } std::optional - Builder::SelectFirstHop(std::set exclude) const + Builder::SelectFirstHop(const std::set& exclude) const { std::optional found = std::nullopt; m_router->ForEachPeer( @@ -295,7 +295,7 @@ namespace llarp } std::optional> - Builder::GetHopsAlignedToForBuild(RouterID endpoint, std::set exclude) + Builder::GetHopsAlignedToForBuild(RouterID endpoint, const std::set& exclude) { const auto pathConfig = m_router->GetConfig()->paths; @@ -329,9 +329,8 @@ namespace llarp return false; std::set hopsSet; - hopsSet.emplace(endpointRC); - for (const auto& hop : hops) - hopsSet.emplace(hop); + hopsSet.insert(endpointRC); + hopsSet.insert(hops.begin(), hops.end()); if (r->routerProfiling().IsBadForPath(rc.pubkey)) return false; @@ -341,7 +340,7 @@ namespace llarp return false; } - hopsSet.emplace(rc); + hopsSet.insert(rc); if (not pathConfig.Acceptable(hopsSet)) return false; diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index ea07bc5bc..89732900d 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -94,14 +94,14 @@ namespace llarp BuildOneAlignedTo(const RouterID endpoint) override; std::optional> - GetHopsAlignedToForBuild(RouterID endpoint, std::set exclude = {}); + GetHopsAlignedToForBuild(RouterID endpoint, const std::set& exclude = {}); void Build(std::vector hops, PathRole roles = ePathRoleAny) override; /// pick a first hop std::optional - SelectFirstHop(std::set exclude = {}) const; + SelectFirstHop(const std::set& exclude = {}) const; virtual std::optional> GetHopsForBuild() override;