count routers correctly

This commit is contained in:
Jeff Becker 2019-05-09 11:36:39 -04:00
parent 767b521325
commit ecc39428f6
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
3 changed files with 89 additions and 19 deletions

View File

@ -173,9 +173,14 @@ namespace llarp
routing::IMessageHandler *h,
const PathID_t &rxid) = 0;
/// count the number of service nodes we are connected to
virtual size_t
NumberOfConnectedRouters() const = 0;
/// count the number of clients that are connected to us
virtual size_t
NumberOfConnectedClients() const = 0;
virtual bool
GetRandomConnectedRouter(RouterContact &result) const = 0;

View File

@ -285,7 +285,7 @@ namespace llarp
if(whitelistRouters)
{
const auto sz = lokinetRouters.size();
auto itr = lokinetRouters.begin();
auto itr = lokinetRouters.begin();
if(sz == 0)
return false;
if(sz > 1)
@ -711,12 +711,46 @@ namespace llarp
}
}
size_t
Router::NumberOfRoutersMatchingFilter(
std::function< bool(const ILinkSession *) > filter) const
{
std::set< RouterID > connected;
ForEachPeer([&](const auto *link, bool) {
if(filter(link))
connected.insert(link->GetPubKey());
});
return connected.size();
}
size_t
Router::NumberOfConnectedRouters() const
{
size_t s = 0;
ForEachPeer([&s](const auto *, bool) { ++s; });
return s;
return NumberOfRoutersMatchingFilter([&](const ILinkSession *link) -> bool {
const RouterContact rc(link->GetRemoteRC());
return rc.IsPublicRouter() && ConnectionToRouterAllowed(rc.pubkey);
});
}
size_t
Router::NumberOfConnectedClients() const
{
return NumberOfRoutersMatchingFilter([&](const ILinkSession *link) -> bool {
const RouterContact rc(link->GetRemoteRC());
return !rc.IsPublicRouter();
});
}
size_t
Router::NumberOfConnectionsMatchingFilter(
std::function< bool(const ILinkSession *) > filter) const
{
size_t sz = 0;
ForEachPeer([&](const auto *link, bool) {
if(filter(link))
++sz;
});
return sz;
}
bool
@ -922,7 +956,7 @@ namespace llarp
if(StrEq(key, "file"))
{
LogInfo("open log file: ", val);
FILE *logfile = ::fopen(val, "a");
FILE *const logfile = ::fopen(val, "a");
if(logfile)
{
LogContext::Instance().logStream =
@ -983,18 +1017,21 @@ namespace llarp
|| (StrEq(section, "bootstrap") && StrEq(key, "add-node")))
{
// llarp::LogDebug("connect section has ", key, "=", val);
bootstrapRCList.emplace_back();
auto &rc = bootstrapRCList.back();
RouterContact rc;
if(!rc.Read(val))
{
llarp::LogWarn("failed to decode bootstrap RC, file='", val,
"' rc=", rc);
bootstrapRCList.pop_back();
;
return;
}
if(rc.Verify(crypto(), Now()))
{
llarp::LogInfo("Added bootstrap node ", RouterID(rc.pubkey));
const auto result = bootstrapRCList.insert(std::move(rc));
if(result.second)
llarp::LogInfo("Added bootstrap node ", RouterID(rc.pubkey));
else
llarp::LogWarn("Duplicate bootstrap node ", RouterID(rc.pubkey));
}
else
{
@ -1007,7 +1044,6 @@ namespace llarp
{
llarp::LogError("malformed rc file='", val, "' rc=", rc);
}
bootstrapRCList.pop_back();
}
}
else if(StrEq(section, "router"))
@ -1155,14 +1191,12 @@ namespace llarp
}
bool
Router::IsBootstrapNode(RouterID r) const
Router::IsBootstrapNode(const RouterID r) const
{
for(const auto &rc : bootstrapRCList)
{
if(rc.pubkey == r)
return true;
}
return false;
return std::count_if(
bootstrapRCList.begin(), bootstrapRCList.end(),
[r](const RouterContact &rc) -> bool { return rc.pubkey == r; })
> 0;
}
void
@ -1185,6 +1219,11 @@ namespace llarp
LogError("Failed to update our RC");
}
// kill nodes that are not allowed by network policy
nodedb()->RemoveIf([&](const RouterContact &rc) -> bool {
return !ConnectionToRouterAllowed(rc.pubkey);
});
// only do this as service node
// client endpoints do this on their own
nodedb()->visit([&](const RouterContact &rc) -> bool {
@ -1197,9 +1236,14 @@ namespace llarp
{
// kill dead nodes if client
nodedb()->RemoveIf([&](const RouterContact &rc) -> bool {
// don't kill first hop nodes
if(strictConnectPubkeys.count(rc.pubkey))
return false;
// don't kill "non-bad" nodes
if(!routerProfiling().IsBad(rc.pubkey))
return false;
routerProfiling().ClearProfile(rc.pubkey);
// don't kill bootstrap nodes
return !IsBootstrapNode(rc.pubkey);
});
}
@ -1227,8 +1271,14 @@ namespace llarp
}
else
{
LogInfo("commit to ", itr->first, " expired");
const RouterID r(itr->first);
LogInfo("commit to ", r, " expired");
itr = m_PersistingSessions.erase(itr);
// close all the session because the commit to this router expired
ForEachPeer([&](ILinkSession *s) {
if(s->GetPubKey() == r)
s->Close();
});
}
}
}

View File

@ -246,7 +246,7 @@ namespace llarp
std::set< RouterID > strictConnectPubkeys;
/// bootstrap RCs
std::list< RouterContact > bootstrapRCList;
std::set< RouterContact > bootstrapRCList;
bool
ExitEnabled() const
@ -501,9 +501,24 @@ namespace llarp
void
ConnectToRandomRouters(int N) override;
/// count the number of unique service nodes connected via pubkey
size_t
NumberOfConnectedRouters() const override;
/// count the number of unique clients connected by pubkey
size_t
NumberOfConnectedClients() const override;
/// count unique router id's given filter to match session
size_t
NumberOfRoutersMatchingFilter(
std::function< bool(const ILinkSession *) > filter) const;
/// count the number of connections that match filter
size_t
NumberOfConnectionsMatchingFilter(
std::function< bool(const ILinkSession *) > filter) const;
bool
TryConnectAsync(RouterContact rc, uint16_t tries) override;