Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

RouterInfo: caps refactoring #629

Merged
merged 4 commits into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/core/router/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ void RouterContext::NewRouterInfo() {
routerInfo.AddSSUAddress(m_Host, m_Port, routerInfo.GetIdentHash());
routerInfo.AddNTCPAddress(m_Host, m_Port);
routerInfo.SetCaps(
core::RouterInfo::Caps::Reachable |
core::RouterInfo::Caps::SSUTesting |
core::RouterInfo::Caps::SSUIntroducer);
core::RouterInfo::Cap::Reachable |
core::RouterInfo::Cap::SSUTesting |
core::RouterInfo::Cap::SSUIntroducer);
routerInfo.SetProperty("netId", I2P_NETWORK_ID);
routerInfo.SetProperty("router.version", I2P_VERSION);
routerInfo.CreateBuffer(m_Keys);
Expand Down Expand Up @@ -156,10 +156,10 @@ void RouterContext::SetFloodfill(
m_IsFloodfill = floodfill;
if (floodfill) {
m_RouterInfo.SetCaps(
m_RouterInfo.GetCaps() | core::RouterInfo::Caps::Floodfill);
m_RouterInfo.GetCaps() | core::RouterInfo::Cap::Floodfill);
} else {
m_RouterInfo.SetCaps(
m_RouterInfo.GetCaps() & ~core::RouterInfo::Caps::Floodfill);
m_RouterInfo.GetCaps() & ~core::RouterInfo::Cap::Floodfill);
// we don't publish number of routers and leaseset for non-floodfill
m_RouterInfo.DeleteProperty(ROUTER_INFO_PROPERTY_LEASESETS);
m_RouterInfo.DeleteProperty(ROUTER_INFO_PROPERTY_ROUTERS);
Expand All @@ -168,29 +168,29 @@ void RouterContext::SetFloodfill(
}

void RouterContext::SetHighBandwidth() {
if (!m_RouterInfo.IsHighBandwidth()) {
m_RouterInfo.SetCaps(
m_RouterInfo.GetCaps() | core::RouterInfo::Caps::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::Caps::HighBandwidth);
auto cap = core::RouterInfo::Cap::HighBandwidth;
if (m_RouterInfo.HasCap(cap)) {
m_RouterInfo.SetCaps(m_RouterInfo.GetCaps() & ~cap);
UpdateRouterInfo();
}
}

bool RouterContext::IsUnreachable() const {
return m_RouterInfo.GetCaps() & core::RouterInfo::Caps::Unreachable;
return m_RouterInfo.GetCaps() & core::RouterInfo::Cap::Unreachable;
}

void RouterContext::SetUnreachable() {
// set caps
m_RouterInfo.SetCaps( // LU, B
core::RouterInfo::Caps::Unreachable | core::RouterInfo::Caps::SSUTesting);
core::RouterInfo::Cap::Unreachable | core::RouterInfo::Cap::SSUTesting);
// remove NTCP address
RemoveTransport(core::RouterInfo::Transport::NTCP);
// delete previous introducers
Expand All @@ -203,11 +203,11 @@ void RouterContext::SetUnreachable() {
void RouterContext::SetReachable() {
// update caps
std::uint8_t caps = m_RouterInfo.GetCaps();
caps &= ~core::RouterInfo::Caps::Unreachable;
caps |= core::RouterInfo::Caps::Reachable;
caps |= core::RouterInfo::Caps::SSUIntroducer;
caps &= ~core::RouterInfo::Cap::Unreachable;
caps |= core::RouterInfo::Cap::Reachable;
caps |= core::RouterInfo::Cap::SSUIntroducer;
if (m_IsFloodfill)
caps |= core::RouterInfo::Caps::Floodfill;
caps |= core::RouterInfo::Cap::Floodfill;
m_RouterInfo.SetCaps(caps);

// insert NTCP back
Expand Down Expand Up @@ -254,8 +254,8 @@ void RouterContext::SetSupportsSSU(bool supportsSSU) {
RemoveTransport(core::RouterInfo::Transport::SSU);
// Remove SSU-related flags
m_RouterInfo.SetCaps(m_RouterInfo.GetCaps()
& ~core::RouterInfo::Caps::SSUTesting
& ~core::RouterInfo::Caps::SSUIntroducer);
& ~core::RouterInfo::Cap::SSUTesting
& ~core::RouterInfo::Cap::SSUIntroducer);

UpdateRouterInfo();
}
Expand Down
145 changes: 71 additions & 74 deletions src/core/router/info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ void RouterInfo::ParseRouterInfo(const std::string& router_info)
value.c_str(), value.size(), address.key, 32);
break;
case Trait::Caps:
ExtractCaps(value.c_str());
SetCaps(value);
break;
default:
// Test for introducers
Expand Down Expand Up @@ -450,7 +450,7 @@ void RouterInfo::ParseRouterInfo(const std::string& router_info)
// Set capabilities
// TODO(anonimal): review setter implementation
if (key == GetTrait(Trait::Caps))
ExtractCaps(value.c_str());
SetCaps(value);
}

// Router *should* be unreachable
Expand All @@ -463,69 +463,78 @@ void RouterInfo::ParseRouterInfo(const std::string& router_info)
}
}

// TODO(anonimal): refactor + rename as setter
void RouterInfo::ExtractCaps(
const char* value) {
const char* cap = value;
while (*cap) {
switch (GetTrait(*cap)) {
case CapsFlag::Floodfill:
m_Caps |= Caps::Floodfill;
break;
case CapsFlag::UnlimitedBandwidth:
m_Caps |= Caps::UnlimitedBandwidth;
break;
case CapsFlag::HighBandwidth1:
case CapsFlag::HighBandwidth2:
case CapsFlag::HighBandwidth3:
case CapsFlag::HighBandwidth4:
m_Caps |= Caps::HighBandwidth;
break;
case CapsFlag::Hidden:
m_Caps |= Caps::Hidden;
break;
case CapsFlag::Reachable:
m_Caps |= Caps::Reachable;
break;
case CapsFlag::Unreachable:
m_Caps |= Caps::Unreachable;
break;
case CapsFlag::SSUTesting:
m_Caps |= Caps::SSUTesting;
break;
case CapsFlag::SSUIntroducer:
m_Caps |= Caps::SSUIntroducer;
break;
default: {}
void RouterInfo::SetCaps(const std::string& caps)
{
LOG(debug) << "RouterInfo: " << __func__ << ": setting caps " << caps;
for (const auto& cap : caps)
{
switch (GetTrait(cap))
{
case CapFlag::Floodfill:
m_Caps |= Cap::Floodfill;
break;
case CapFlag::UnlimitedBandwidth:
m_Caps |= Cap::UnlimitedBandwidth;
break;
case CapFlag::HighBandwidth1:
case CapFlag::HighBandwidth2:
case CapFlag::HighBandwidth3:
case CapFlag::HighBandwidth4:
m_Caps |= Cap::HighBandwidth;
break;
case CapFlag::LowBandwidth1:
case CapFlag::LowBandwidth2:
// TODO(anonimal): implement!
break;
case CapFlag::Hidden:
m_Caps |= Cap::Hidden;
break;
case CapFlag::Reachable:
m_Caps |= Cap::Reachable;
break;
case CapFlag::Unreachable:
m_Caps |= Cap::Unreachable;
break;
case CapFlag::SSUTesting:
m_Caps |= Cap::SSUTesting;
break;
case CapFlag::SSUIntroducer:
m_Caps |= Cap::SSUIntroducer;
break;
case CapFlag::Unknown:
default:
{
LOG(error) << "RouterInfo: " << __func__
<< ": ignoring unknown cap " << cap;
}
}
}
cap++;
}
}

void RouterInfo::UpdateCapsProperty()
{
std::string caps;

if (m_Caps & Caps::Floodfill)
if (m_Caps & Cap::Floodfill)
{
caps += GetTrait(CapsFlag::HighBandwidth4); // highest bandwidth
caps += GetTrait(CapsFlag::Floodfill);
caps += GetTrait(CapFlag::HighBandwidth4); // highest bandwidth
caps += GetTrait(CapFlag::Floodfill);
}
else
{
caps += (m_Caps & Caps::HighBandwidth)
? GetTrait(CapsFlag::HighBandwidth3)
: GetTrait(CapsFlag::LowBandwidth2);
caps += (m_Caps & Cap::HighBandwidth)
? GetTrait(CapFlag::HighBandwidth3)
: GetTrait(CapFlag::LowBandwidth2);
}

if (m_Caps & Caps::Hidden)
caps += GetTrait(CapsFlag::Hidden);
if (m_Caps & Cap::Hidden)
caps += GetTrait(CapFlag::Hidden);

if (m_Caps & Caps::Reachable)
caps += GetTrait(CapsFlag::Reachable);
if (m_Caps & Cap::Reachable)
caps += GetTrait(CapFlag::Reachable);

if (m_Caps & Caps::Unreachable)
caps += GetTrait(CapsFlag::Unreachable);
if (m_Caps & Cap::Unreachable)
caps += GetTrait(CapFlag::Unreachable);

SetProperty("caps", caps);
}
Expand Down Expand Up @@ -583,12 +592,12 @@ void RouterInfo::CreateRouterInfo(
{
router_info.WriteByteAndString(GetTrait(Trait::SSU));

// Get SSU capabilities
// Get/Set SSU capabilities flags
std::string caps;
if (IsPeerTesting())
caps += GetTrait(CapsFlag::SSUTesting);
if (IsIntroducer())
caps += GetTrait(CapsFlag::SSUIntroducer);
if (HasCap(Cap::SSUTesting))
caps += GetTrait(CapFlag::SSUTesting);
if (HasCap(Cap::SSUIntroducer))
caps += GetTrait(CapFlag::SSUIntroducer);

// Write SSU capabilities
options.WriteKeyPair(GetTrait(Trait::Caps), caps);
Expand Down Expand Up @@ -770,8 +779,8 @@ void RouterInfo::AddSSUAddress(
m_Addresses.push_back(addr);
m_SupportedTransports |=
addr.host.is_v6() ? SupportedTransport::SSUv6 : SupportedTransport::SSUv4;
m_Caps |= Caps::SSUTesting;
m_Caps |= Caps::SSUIntroducer;
m_Caps |= Cap::SSUTesting;
m_Caps |= Cap::SSUIntroducer;
}

bool RouterInfo::AddIntroducer(
Expand Down Expand Up @@ -818,14 +827,6 @@ void RouterInfo::SetCaps(
UpdateCapsProperty();
}

// TODO(anonimal): refactor this setter, it should be simpler
void RouterInfo::SetCaps(
const char* caps) {
SetProperty("caps", caps);
m_Caps = 0;
ExtractCaps(caps);
}

void RouterInfo::SetProperty(
const std::string& key,
const std::string& value) {
Expand All @@ -837,10 +838,6 @@ void RouterInfo::DeleteProperty(
m_Options.erase(key);
}

bool RouterInfo::IsFloodfill() const {
return m_Caps & Caps::Floodfill;
}

bool RouterInfo::IsNTCP(
bool v4only) const {
if (v4only)
Expand Down Expand Up @@ -896,7 +893,7 @@ void RouterInfo::DisableV6() {
}

bool RouterInfo::UsesIntroducer() const {
return m_Caps & Caps::Unreachable; // non-reachable
return HasCap(Cap::Unreachable); // Router is unreachable, must use introducer
}

const RouterInfo::Address* RouterInfo::GetNTCPAddress(
Expand Down Expand Up @@ -947,10 +944,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(CapsFlag::SSUTesting)
: GetTrait(CapsFlag::Unknown))
<< (IsIntroducer() ? GetTrait(CapsFlag::SSUIntroducer)
: GetTrait(CapsFlag::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)
Expand Down
Loading