feedback from jason

* use emplace in PeerSectionConfig::Acceptable use insert everywhere else
* use const l-value references
* fix typos and spelling mistakes
pull/1539/head
Jeff Becker 3 years ago
parent 5d465264a8
commit 3425069b41
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -1000,10 +1000,10 @@ namespace llarp
throw std::invalid_argument{"[paths]:unique-range-size must be between 4 and 32"}; throw std::invalid_argument{"[paths]:unique-range-size must be between 4 and 32"};
m_UniqueHopsNetmaskSize = arg; m_UniqueHopsNetmaskSize = arg;
}, },
Comment{"In path hop selection use this as the default netmkask for testing if we should " Comment{"Netmask for router path selection; each router must be from a distinct IP subnet "
"include a relay in our hop list", "of the given size.",
"i.e. 32 for uniuqe ip addresses for every hop, 24 for all hops are in a different " "E.g. 16 ensures that all routers are using distinct /16 IP addresses."});
"/24, 16 for all hops are in a different /16, etc..."});
#ifdef WITH_GEOIP #ifdef WITH_GEOIP
conf.defineOption<std::string>( conf.defineOption<std::string>(
"paths", "paths",
@ -1014,32 +1014,27 @@ namespace llarp
m_ExcludeCountries.emplace(lowercase_ascii_string(std::move(arg))); 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", Comment{"exclude a country given its 2 letter country code from being used in path builds",
"i.e. exlcude-country=DE", "e.g. exclude-country=DE",
"can be listed multiple times to exlcude multiple countries"}); "can be listed multiple times to exclude multiple countries"});
#endif #endif
} }
bool bool
PeerSelectionConfig::Acceptable(std::set<RouterContact> rcs) const PeerSelectionConfig::Acceptable(const std::set<RouterContact>& rcs) const
{ {
if (m_UniqueHopsNetmaskSize) if (m_UniqueHopsNetmaskSize)
{ {
auto makeRange = const auto netmask = netmask_ipv6_bits(96 + *m_UniqueHopsNetmaskSize);
[netmask = netmask_ipv6_bits(96 + *m_UniqueHopsNetmaskSize)](IpAddress addr) -> IPRange {
return IPRange{net::ExpandV4(addr.toIP()) & netmask, netmask};
};
std::set<IPRange> seenRanges; std::set<IPRange> seenRanges;
for (const auto& hop : rcs) for (const auto& hop : rcs)
{ {
for (const auto& addr : hop.addrs) for (const auto& addr : hop.addrs)
{ {
const auto range = makeRange(addr.toIpAddress()); const auto network_addr = net::In6ToHUInt(addr.ip) & netmask;
if (seenRanges.count(range)) if (auto [it, inserted] = seenRanges.emplace(network_addr, netmask); not inserted)
{ {
return false; return false;
} }
seenRanges.emplace(range);
} }
} }
} }

@ -87,7 +87,7 @@ namespace llarp
/// return true if this set of router contacts is acceptable against this config /// return true if this set of router contacts is acceptable against this config
bool bool
Acceptable(std::set<RouterContact> hops) const; Acceptable(const std::set<RouterContact>& hops) const;
}; };
struct NetworkConfig struct NetworkConfig

@ -14,6 +14,12 @@ namespace llarp
huint128_t addr = {0}; huint128_t addr = {0};
huint128_t netmask_bits = {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 static constexpr IPRange
FromIPv4(byte_t a, byte_t b, byte_t c, byte_t d, byte_t mask) FromIPv4(byte_t a, byte_t b, byte_t c, byte_t d, byte_t mask)
{ {

@ -206,7 +206,7 @@ namespace llarp
} }
std::optional<RouterContact> std::optional<RouterContact>
Builder::SelectFirstHop(std::set<RouterID> exclude) const Builder::SelectFirstHop(const std::set<RouterID>& exclude) const
{ {
std::optional<RouterContact> found = std::nullopt; std::optional<RouterContact> found = std::nullopt;
m_router->ForEachPeer( m_router->ForEachPeer(
@ -295,7 +295,7 @@ namespace llarp
} }
std::optional<std::vector<RouterContact>> std::optional<std::vector<RouterContact>>
Builder::GetHopsAlignedToForBuild(RouterID endpoint, std::set<RouterID> exclude) Builder::GetHopsAlignedToForBuild(RouterID endpoint, const std::set<RouterID>& exclude)
{ {
const auto pathConfig = m_router->GetConfig()->paths; const auto pathConfig = m_router->GetConfig()->paths;
@ -329,9 +329,8 @@ namespace llarp
return false; return false;
std::set<RouterContact> hopsSet; std::set<RouterContact> hopsSet;
hopsSet.emplace(endpointRC); hopsSet.insert(endpointRC);
for (const auto& hop : hops) hopsSet.insert(hops.begin(), hops.end());
hopsSet.emplace(hop);
if (r->routerProfiling().IsBadForPath(rc.pubkey)) if (r->routerProfiling().IsBadForPath(rc.pubkey))
return false; return false;
@ -341,7 +340,7 @@ namespace llarp
return false; return false;
} }
hopsSet.emplace(rc); hopsSet.insert(rc);
if (not pathConfig.Acceptable(hopsSet)) if (not pathConfig.Acceptable(hopsSet))
return false; return false;

@ -94,14 +94,14 @@ namespace llarp
BuildOneAlignedTo(const RouterID endpoint) override; BuildOneAlignedTo(const RouterID endpoint) override;
std::optional<std::vector<RouterContact>> std::optional<std::vector<RouterContact>>
GetHopsAlignedToForBuild(RouterID endpoint, std::set<RouterID> exclude = {}); GetHopsAlignedToForBuild(RouterID endpoint, const std::set<RouterID>& exclude = {});
void void
Build(std::vector<RouterContact> hops, PathRole roles = ePathRoleAny) override; Build(std::vector<RouterContact> hops, PathRole roles = ePathRoleAny) override;
/// pick a first hop /// pick a first hop
std::optional<RouterContact> std::optional<RouterContact>
SelectFirstHop(std::set<RouterID> exclude = {}) const; SelectFirstHop(const std::set<RouterID>& exclude = {}) const;
virtual std::optional<std::vector<RouterContact>> virtual std::optional<std::vector<RouterContact>>
GetHopsForBuild() override; GetHopsForBuild() override;

Loading…
Cancel
Save