pull/174/head
Jeff Becker 6 years ago
parent c025755b99
commit 6fc42dc7ad
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -144,6 +144,12 @@ namespace llarp
nodes.erase(itr);
}
bool
HasNode(const Key_t& key) const
{
return nodes.find(key) != nodes.end();
}
BucketStorage_t nodes;
};
} // namespace dht

@ -163,7 +163,7 @@ namespace llarp
}
bool
ILinkLayer::TryEstablishTo(const RouterContact& rc)
ILinkLayer::TryEstablishTo(RouterContact rc)
{
llarp::AddressInfo to;
if(!PickAddress(rc, to))

@ -104,7 +104,7 @@ namespace llarp
PickAddress(const RouterContact& rc, AddressInfo& picked) const;
bool
TryEstablishTo(const RouterContact& rc);
TryEstablishTo(RouterContact rc);
bool
Start(llarp::Logic* l);

@ -1011,6 +1011,7 @@ namespace llarp
{
llarp::LogError("Message Integrity Failed: got ", digest, " from ",
remoteAddr, " instead of ", expected);
Close();
return false;
}

@ -421,7 +421,8 @@ llarp_nodedb::select_random_hop(const llarp::RouterContact &prev,
auto sz = entries.size();
if(sz < 3)
return false;
size_t tries = 5;
size_t tries = 5;
llarp_time_t now = llarp::time_now_ms();
if(N)
{
do
@ -439,7 +440,7 @@ llarp_nodedb::select_random_hop(const llarp::RouterContact &prev,
continue;
return false;
}
if(itr->second.addrs.size())
if(itr->second.addrs.size() && !itr->second.IsExpired(now))
{
result = itr->second;
return true;
@ -448,15 +449,5 @@ llarp_nodedb::select_random_hop(const llarp::RouterContact &prev,
return false;
}
else
{
auto itr = entries.begin();
if(sz > 1)
{
auto idx = llarp::randint() % sz;
if(idx)
std::advance(itr, idx - 1);
}
result = itr->second;
return true;
}
return false;
}

@ -653,17 +653,16 @@ namespace llarp
{
encryption = nextOnionKey;
// propagate RC by renegotiating sessions
ForEachPeer([&](llarp::ILinkSession *s) {
if(!s->RenegotiateSession())
{
llarp::LogWarn("failed to renegotiate session with ",
s->GetRemoteEndpoint());
}
ForEachPeer([](llarp::ILinkSession *s) {
if(s->RenegotiateSession())
llarp::LogInfo("renegotiated session");
else
llarp::LogWarn("failed to renegotiate session");
});
}
// TODO: do this async
return SaveRC();
}
} // namespace llarp
bool
Router::CheckRenegotiateValid(RouterContact newrc, RouterContact oldrc)
@ -674,6 +673,11 @@ namespace llarp
// store it in nodedb async
nodedb->InsertAsync(newrc);
// update dht if required
if(dht->impl.nodes->HasNode(newrc.pubkey.data()))
{
dht->impl.nodes->PutNode(newrc);
}
// update valid routers
{
auto itr = validRouters.find(newrc.pubkey);
@ -686,17 +690,37 @@ namespace llarp
return true;
}
void
Router::ServiceNodeLookupRouterWhenExpired(RouterID router)
{
dht->impl.LookupRouter(router,
std::bind(&Router::HandleDHTLookupForExplore, this,
router, std::placeholders::_1));
}
void
Router::Tick()
{
// llarp::LogDebug("tick router");
auto now = llarp_ev_loop_time_now_ms(netloop);
if(_rc.ExpiresSoon(now))
if(_rc.ExpiresSoon(now, llarp::randint() % 10000))
{
if(!UpdateOurRC())
llarp::LogInfo("regenerating RC");
if(!UpdateOurRC(IsServiceNode()))
llarp::LogError("Failed to update our RC");
}
if(IsServiceNode())
{
// only do this as service node
// client endpoints do this on their own
nodedb->visit([&](const RouterContact &rc) -> bool {
if(rc.ExpiresSoon(now, llarp::randint() % 10000))
ServiceNodeLookupRouterWhenExpired(rc.pubkey);
return true;
});
}
paths.TickPaths(now);
paths.ExpirePaths(now);
{
@ -1124,6 +1148,8 @@ namespace llarp
self->nodedb->visit(
[self, &want](const llarp::RouterContact &other) -> bool {
// check if we really want to
if(other.ExpiresSoon(self->Now(), 30000))
return want > 0;
if(!self->ConnectionToRouterAllowed(other.pubkey))
return want > 0;
if(llarp::randint() % 2 == 0
@ -1199,7 +1225,8 @@ namespace llarp
auto itr = netConfigDefaults.begin();
while(itr != netConfigDefaults.end())
{
if(netConfig.count(itr->first) == 0)
auto found = netConfig.find(itr->first);
if(found == netConfig.end() || found->second.empty())
{
netConfig.emplace(std::make_pair(itr->first, itr->second()));
}

@ -295,6 +295,10 @@ namespace llarp
void
TryEstablishTo(const llarp::RouterID &remote);
/// lookup a router by pubkey when it expires when we are a service node
void
ServiceNodeLookupRouterWhenExpired(llarp::RouterID remote);
void
HandleDHTLookupForExplore(
llarp::RouterID remote,

@ -18,8 +18,8 @@ namespace llarp
// 1 minute for testnet
llarp_time_t RouterContact::Lifetime = 60 * 1000;
#else
/// 1 hour for real network
llarp_time_t RouterContact::Lifetime = 60 * 60 * 1000;
/// 1 day for real network
llarp_time_t RouterContact::Lifetime = 24 * 60 * 60 * 1000;
#endif
NetID::NetID() : AlignedBuffer< 8 >((const byte_t *)LLARP_NET_ID)
{
@ -200,13 +200,17 @@ namespace llarp
bool
RouterContact::IsExpired(llarp_time_t now) const
{
return now > last_updated && now - last_updated >= Lifetime;
auto expiresAt = last_updated + Lifetime;
return now >= expiresAt;
}
bool
RouterContact::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const
{
return now - (last_updated + Lifetime) >= dlt;
if(IsExpired(now))
return true;
auto expiresAt = last_updated + Lifetime;
return expiresAt - now <= dlt;
}
std::string

@ -2,6 +2,7 @@
#include <handlers/tun.hpp>
#include <service/context.hpp>
#include <service/endpoint.hpp>
#include <router.hpp>
namespace llarp
{
@ -29,6 +30,11 @@ namespace llarp
itr->second->Tick(now);
++itr;
}
m_Router->nodedb->visit([&](const RouterContact &rc) -> bool {
if(rc.IsExpired(now))
getFirstEndpoint()->LookupRouterAnon(rc.pubkey);
return true;
});
}
bool

@ -715,7 +715,7 @@ namespace llarp
job->nodedb = m_Router->nodedb;
job->cryptoworker = m_Router->tp;
job->diskworker = m_Router->disk;
job->logic = nullptr;
job->logic = m_Router->logic;
job->hook = nullptr;
job->rc = msg->R[0];
llarp_nodedb_async_verify(job);
@ -732,25 +732,31 @@ namespace llarp
RouterContact rc;
if(!m_Router->nodedb->Get(router, rc))
{
if(m_PendingRouters.find(router) == m_PendingRouters.end())
{
auto path = GetEstablishedPathClosestTo(router);
routing::DHTMessage msg;
auto txid = GenTXID();
msg.M.emplace_back(new dht::FindRouterMessage(txid, router));
LookupRouterAnon(router);
}
}
if(path && path->SendRoutingMessage(&msg, m_Router))
{
llarp::LogInfo(Name(), " looking up ", router);
m_PendingRouters.insert(
std::make_pair(router, RouterLookupJob(this)));
}
else
{
llarp::LogError("failed to send request for router lookup");
}
bool
Endpoint::LookupRouterAnon(RouterID router)
{
if(m_PendingRouters.find(router) == m_PendingRouters.end())
{
auto path = GetEstablishedPathClosestTo(router);
routing::DHTMessage msg;
auto txid = GenTXID();
msg.M.emplace_back(new dht::FindRouterMessage(txid, router));
if(path && path->SendRoutingMessage(&msg, m_Router))
{
llarp::LogInfo(Name(), " looking up ", router);
m_PendingRouters.insert(
std::make_pair(router, RouterLookupJob(this)));
return true;
}
else
llarp::LogError("failed to send request for router lookup");
}
return false;
}
void

@ -143,6 +143,10 @@ namespace llarp
void
EnsureRouterIsKnown(const RouterID& router);
/// lookup a router via closest path
bool
LookupRouterAnon(RouterID router);
const Identity&
GetIdentity() const
{

Loading…
Cancel
Save