diff --git a/src/core/router/context.cc b/src/core/router/context.cc index 6736ed3c..06d54f7b 100644 --- a/src/core/router/context.cc +++ b/src/core/router/context.cc @@ -168,17 +168,17 @@ void RouterContext::SetFloodfill( } void RouterContext::SetHighBandwidth() { - if (!m_RouterInfo.IsHighBandwidth()) { - m_RouterInfo.SetCaps( - m_RouterInfo.GetCaps() | core::RouterInfo::Cap::HighBandwidth); + auto cap = core::RouterInfo::Cap::HighBandwidth; + if (!m_RouterInfo.HasCap(cap)) { + m_RouterInfo.SetCaps(m_RouterInfo.GetCaps() | cap); UpdateRouterInfo(); } } void RouterContext::SetLowBandwidth() { - if (m_RouterInfo.IsHighBandwidth()) { - m_RouterInfo.SetCaps( - m_RouterInfo.GetCaps() & ~core::RouterInfo::Cap::HighBandwidth); + auto cap = core::RouterInfo::Cap::HighBandwidth; + if (m_RouterInfo.HasCap(cap)) { + m_RouterInfo.SetCaps(m_RouterInfo.GetCaps() & ~cap); UpdateRouterInfo(); } } diff --git a/src/core/router/info.cc b/src/core/router/info.cc index b913a762..42263971 100644 --- a/src/core/router/info.cc +++ b/src/core/router/info.cc @@ -583,11 +583,11 @@ void RouterInfo::CreateRouterInfo( { router_info.WriteByteAndString(GetTrait(Trait::SSU)); - // Get SSU capabilities + // Get/Set SSU capabilities flags std::string caps; - if (IsPeerTesting()) + if (HasCap(Cap::SSUTesting)) caps += GetTrait(CapFlag::SSUTesting); - if (IsIntroducer()) + if (HasCap(Cap::SSUIntroducer)) caps += GetTrait(CapFlag::SSUIntroducer); // Write SSU capabilities @@ -837,10 +837,6 @@ void RouterInfo::DeleteProperty( m_Options.erase(key); } -bool RouterInfo::IsFloodfill() const { - return m_Caps & Cap::Floodfill; -} - bool RouterInfo::IsNTCP( bool v4only) const { if (v4only) @@ -896,7 +892,7 @@ void RouterInfo::DisableV6() { } bool RouterInfo::UsesIntroducer() const { - return m_Caps & Cap::Unreachable; // non-reachable + return HasCap(Cap::Unreachable); // Router is unreachable, must use introducer } const RouterInfo::Address* RouterInfo::GetNTCPAddress( @@ -947,10 +943,10 @@ const std::string RouterInfo::GetDescription(const std::string& tabs) const for (const auto& opt : m_Options) ss << tabs << "\t\t[" << opt.first << "] : [" << opt.second << "]" << std::endl; ss << tabs << "\tSSU Caps: [" - << (IsPeerTesting() ? GetTrait(CapFlag::SSUTesting) - : GetTrait(CapFlag::Unknown)) - << (IsIntroducer() ? GetTrait(CapFlag::SSUIntroducer) - : GetTrait(CapFlag::Unknown)) + << (HasCap(Cap::SSUTesting) ? GetTrait(CapFlag::SSUTesting) + : GetTrait(CapFlag::Unknown)) + << (HasCap(Cap::SSUIntroducer) ? GetTrait(CapFlag::SSUIntroducer) + : GetTrait(CapFlag::Unknown)) << "]" << std::endl; ss << tabs << "\tAddresses(" << m_Addresses.size() << "): " << std::endl; for (const auto& address : m_Addresses) diff --git a/src/core/router/info.h b/src/core/router/info.h index f5a3d0fb..4055beda 100644 --- a/src/core/router/info.h +++ b/src/core/router/info.h @@ -445,8 +445,6 @@ class RouterInfo : public RoutingDestination { m_Options.clear(); } - bool IsFloodfill() const; - bool IsNTCP( bool v4only = true) const; @@ -466,20 +464,11 @@ class RouterInfo : public RoutingDestination { bool UsesIntroducer() const; - bool IsIntroducer() const { - return m_Caps & Cap::SSUIntroducer; - } - - bool IsPeerTesting() const { - return m_Caps & Cap::SSUTesting; - } - - bool IsHidden() const { - return m_Caps & Cap::Hidden; - } - - bool IsHighBandwidth() const { - return m_Caps & Cap::HighBandwidth; + bool HasCap(Cap cap) const + { + bool has_cap = m_Caps & cap; + LOG(debug) << "RouterInfo: " << __func__ << ": " << has_cap; + return has_cap; } std::uint8_t GetCaps() const { @@ -555,6 +544,7 @@ class RouterInfo : public RoutingDestination { return m_Options; } + // TODO(anonimal): really?... bool IsDestination() const { return false; } diff --git a/src/core/router/net_db/impl.cc b/src/core/router/net_db/impl.cc index 05775c51..127ba686 100644 --- a/src/core/router/net_db/impl.cc +++ b/src/core/router/net_db/impl.cc @@ -215,7 +215,7 @@ void NetDb::AddRouterInfo( std::unique_lock l(m_RouterInfosMutex); m_RouterInfos[r->GetIdentHash()] = r; } - if (r->IsFloodfill()) { + if (r->HasCap(RouterInfo::Cap::Floodfill)) { std::unique_lock l(m_FloodfillsMutex); m_Floodfills.push_back(r); } @@ -351,7 +351,7 @@ bool NetDb::Load() router->DeleteBuffer(); router->ClearProperties(); // properties are not used for regular routers m_RouterInfos.insert(std::make_pair(router->GetIdentHash(), router)); - if (router->IsFloodfill()) + if (router->HasCap(RouterInfo::Cap::Floodfill)) m_Floodfills.push_back(router); num_routers++; } @@ -451,7 +451,7 @@ void NetDb::SaveUpdated() { if (is_removed) deleted_count++; // delete from floodfills list - if (it.second->IsFloodfill()) { + if (it.second->HasCap(RouterInfo::Cap::Floodfill)) { std::unique_lock l(m_FloodfillsMutex); m_Floodfills.remove(it.second); } @@ -867,10 +867,11 @@ void NetDb::Publish() { } } +// TODO(anonimal): refactor these getters into fewer functions std::shared_ptr NetDb::GetRandomRouter() const { return GetRandomRouter( [](std::shared_ptr router)->bool { - return !router->IsHidden(); + return !router->HasCap(RouterInfo::Cap::Hidden); }); } @@ -878,7 +879,7 @@ std::shared_ptr NetDb::GetRandomRouter( std::shared_ptr compatible_with) const { return GetRandomRouter( [compatible_with](std::shared_ptr router)->bool { - return !router->IsHidden() && router != compatible_with && + return !router->HasCap(RouterInfo::Cap::Hidden) && router != compatible_with && router->IsCompatible(*compatible_with); }); } @@ -886,14 +887,14 @@ std::shared_ptr NetDb::GetRandomRouter( std::shared_ptr NetDb::GetRandomPeerTestRouter() const { return GetRandomRouter( [](std::shared_ptr router)->bool { - return !router->IsHidden() && router->IsPeerTesting(); + return !router->HasCap(RouterInfo::Cap::Hidden) && router->HasCap(RouterInfo::Cap::SSUTesting); }); } std::shared_ptr NetDb::GetRandomIntroducer() const { return GetRandomRouter( [](std::shared_ptr router)->bool { - return !router->IsHidden() && router->IsIntroducer(); + return !router->HasCap(RouterInfo::Cap::Hidden) && router->HasCap(RouterInfo::Cap::SSUIntroducer); }); } @@ -901,7 +902,7 @@ std::shared_ptr NetDb::GetHighBandwidthRandomRouter( std::shared_ptr compatible_with) const { return GetRandomRouter( [compatible_with](std::shared_ptr router)->bool { - return !router->IsHidden() && + return !router->HasCap(RouterInfo::Cap::Hidden) && router != compatible_with && router->IsCompatible(*compatible_with) && (router->GetCaps() & RouterInfo::Cap::HighBandwidth); @@ -1014,7 +1015,7 @@ std::shared_ptr NetDb::GetClosestNonFloodfill( min_metric.SetMax(); // must be called from NetDb thread only for (auto it : m_RouterInfos) { - if (!it.second->IsFloodfill()) { + if (!it.second->HasCap(RouterInfo::Cap::Floodfill)) { XORMetric m = dest_key ^ it.first; if (m < min_metric && !excluded.count(it.first)) { min_metric = m; diff --git a/src/core/router/transports/impl.cc b/src/core/router/transports/impl.cc index 546cdfbc..2737f40a 100644 --- a/src/core/router/transports/impl.cc +++ b/src/core/router/transports/impl.cc @@ -270,7 +270,7 @@ bool Transports::IsBandwidthExceeded() const { LOG(debug) << "Transports: bandwidth has been exceeded"; return true; } - if (kovri::context.GetRouterInfo().IsHighBandwidth()) + if (kovri::context.GetRouterInfo().HasCap(RouterInfo::Cap::HighBandwidth)) LOG(debug) << "Transports: bandwidth has not been exceeded"; return false; } diff --git a/src/core/router/transports/ssu/session.cc b/src/core/router/transports/ssu/session.cc index 67a73ae4..36c0d177 100644 --- a/src/core/router/transports/ssu/session.cc +++ b/src/core/router/transports/ssu/session.cc @@ -477,7 +477,7 @@ void SSUSession::SendSessionCreated( s.Insert (htobe16(address->port)); // our port std::uint32_t relay_tag = 0; - if (kovri::context.GetRouterInfo().IsIntroducer()) { + if (kovri::context.GetRouterInfo().HasCap(RouterInfo::Cap::SSUIntroducer)) { relay_tag = kovri::core::Rand(); if (!relay_tag) relay_tag = 1; @@ -1408,7 +1408,7 @@ void SSUSession::Established() { // send database store m_Data.Send(CreateDatabaseStoreMsg()); transports.PeerConnected(shared_from_this()); - if (m_PeerTest && (m_RemoteRouter && m_RemoteRouter->IsPeerTesting())) + if (m_PeerTest && (m_RemoteRouter && m_RemoteRouter->HasCap(RouterInfo::Cap::SSUTesting))) SendPeerTest(); ScheduleTermination(); }