remove more locking and make it safe

pull/830/head
Jeff Becker 5 years ago
parent ab64c0d013
commit 5cdd92e2a3
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -238,6 +238,15 @@ namespace llarp
static void
SendLRCM(std::shared_ptr< LRCMFrameDecrypt > self)
{
if(self->context->HasTransitHop(self->hop->info))
{
llarp::LogError("duplicate transit hop", self->hop->info);
OnForwardLRCMResult(self->context->Router(), self->hop->info.rxID,
self->hop->info.downstream, self->hop->pathKey,
SendStatus::Congestion);
self->hop = nullptr;
return;
}
if(!self->context->Router()->ConnectionToRouterAllowed(
self->hop->info.upstream))
{
@ -287,15 +296,21 @@ namespace llarp
static void
SendPathConfirm(std::shared_ptr< LRCMFrameDecrypt > self)
{
// persist session to downstream until path expiration
self->context->Router()->PersistSessionUntil(
self->hop->info.downstream, self->hop->ExpireTime() + 10000);
// put hop
self->context->PutTransitHop(self->hop);
// send path confirmation
// TODO: other status flags?
uint64_t status = LR_StatusRecord::SUCCESS;
if(self->context->HasTransitHop(self->hop->info))
{
status = LR_StatusRecord::FAIL_DUPLICATE_HOP;
}
else
{
// persist session to downstream until path expiration
self->context->Router()->PersistSessionUntil(
self->hop->info.downstream, self->hop->ExpireTime() + 10000);
// put hop
self->context->PutTransitHop(self->hop);
}
if(!LR_StatusMessage::CreateAndSend(
self->context->Router(), self->hop->info.rxID,
@ -332,12 +347,7 @@ namespace llarp
info.txID = self->record.txid;
info.rxID = self->record.rxid;
info.upstream = self->record.nextHop;
if(self->context->HasTransitHop(info))
{
llarp::LogError("duplicate transit hop ", info);
self->decrypter = nullptr;
return;
}
// generate path key as we are in a worker thread
auto crypto = CryptoManager::instance();
if(!crypto->dh_server(self->hop->pathKey, self->record.commkey,
@ -387,7 +397,7 @@ namespace llarp
{
// we are the farthest hop
llarp::LogDebug("We are the farthest hop for ", info);
// send a LRAM down the path
// send a LRSM down the path
self->context->logic()->queue_func([=]() {
SendPathConfirm(self);
self->decrypter = nullptr;

@ -23,7 +23,7 @@ namespace llarp
struct LR_StatusRecord
{
static constexpr uint64_t SUCCESS = 1;
static constexpr uint64_t SUCCESS = 1 << 0;
static constexpr uint64_t FAIL_TIMEOUT = 1 << 1;
static constexpr uint64_t FAIL_CONGESTION = 1 << 2;
static constexpr uint64_t FAIL_DEST_UNKNOWN = 1 << 3;
@ -31,6 +31,7 @@ namespace llarp
static constexpr uint64_t FAIL_MALFORMED_RECORD = 1 << 5;
static constexpr uint64_t FAIL_DEST_INVALID = 1 << 6;
static constexpr uint64_t FAIL_CANNOT_CONNECT = 1 << 7;
static constexpr uint64_t FAIL_DUPLICATE_HOP = 1 << 8;
uint64_t status = 0;
uint64_t version = 0;

@ -88,7 +88,7 @@ namespace llarp
HopHandler_ptr
MapGet(Map_t& map, const Key_t& k, CheckValue_t check, GetFunc_t get)
{
util::Lock lock(&map.first);
Map_t::Lock_t lock(&map.first);
auto range = map.second.equal_range(k);
for(auto i = range.first; i != range.second; ++i)
{
@ -102,7 +102,7 @@ namespace llarp
bool
MapHas(Map_t& map, const Key_t& k, CheckValue_t check)
{
util::Lock lock(&map.first);
Map_t::Lock_t lock(&map.first);
auto range = map.second.equal_range(k);
for(auto i = range.first; i != range.second; ++i)
{
@ -116,7 +116,7 @@ namespace llarp
void
MapPut(Map_t& map, const Key_t& k, const Value_t& v)
{
util::Lock lock(&map.first);
Map_t::Lock_t lock(&map.first);
map.second.emplace(k, v);
}
@ -124,7 +124,7 @@ namespace llarp
void
MapIter(Map_t& map, Visit_t v)
{
util::Lock lock(map.first);
Map_t::Lock_t lock(map.first);
for(const auto& item : map.second)
v(item);
}
@ -133,7 +133,7 @@ namespace llarp
void
MapDel(Map_t& map, const Key_t& k, Check_t check)
{
util::Lock lock(map.first);
Map_t::Lock_t lock(map.first);
auto range = map.second.equal_range(k);
for(auto i = range.first; i != range.second;)
{
@ -190,7 +190,7 @@ namespace llarp
PathContext::TransitHopPreviousIsRouter(const PathID_t& path,
const RouterID& otherRouter)
{
util::Lock lock(&m_TransitPaths.first);
decltype(m_TransitPaths)::Lock_t lock(&m_TransitPaths.first);
auto itr = m_TransitPaths.second.find(path);
if(itr == m_TransitPaths.second.end())
return false;
@ -214,7 +214,7 @@ namespace llarp
PathContext::GetLocalPathSet(const PathID_t& id)
{
auto& map = m_OurPaths;
util::Lock lock(&map.first);
decltype(m_OurPaths)::Lock_t lock(&map.first);
auto itr = map.second.find(id);
if(itr != map.second.end())
{
@ -241,7 +241,7 @@ namespace llarp
RouterID us(OurRouterID());
auto& map = m_TransitPaths;
{
util::Lock lock(&map.first);
decltype(m_TransitPaths)::Lock_t lock(&map.first);
auto range = map.second.equal_range(id);
for(auto i = range.first; i != range.second; ++i)
{
@ -263,7 +263,7 @@ namespace llarp
PathContext::ExpirePaths(llarp_time_t now)
{
{
util::Lock lock(&m_TransitPaths.first);
decltype(m_TransitPaths)::Lock_t lock(&m_TransitPaths.first);
auto& map = m_TransitPaths.second;
auto itr = map.begin();
while(itr != map.end())
@ -277,7 +277,7 @@ namespace llarp
}
}
{
util::Lock lock(&m_OurPaths.first);
decltype(m_OurPaths)::Lock_t lock(&m_OurPaths.first);
auto& map = m_OurPaths.second;
for(auto& item : map)
{
@ -299,7 +299,7 @@ namespace llarp
const RouterID us(OurRouterID());
auto& map = m_TransitPaths;
{
util::Lock lock(&map.first);
decltype(m_TransitPaths)::Lock_t lock(&map.first);
auto range = map.second.equal_range(id);
for(auto i = range.first; i != range.second; ++i)
{
@ -313,7 +313,7 @@ namespace llarp
void
PathContext::RemovePathSet(PathSet_ptr set)
{
util::Lock lock(&m_OurPaths.first);
decltype(m_OurPaths)::Lock_t lock(&m_OurPaths.first);
auto& map = m_OurPaths.second;
auto itr = map.begin();
while(itr != map.end())

@ -102,14 +102,17 @@ namespace llarp
struct SyncTransitMap_t
{
util::Mutex first; // protects second
using Mutex_t = util::NullMutex;
using Lock_t = util::NullLock;
Mutex_t first; // protects second
TransitHopsMap_t second GUARDED_BY(first);
void
ForEach(std::function< void(const TransitHop_ptr&) > visit)
LOCKS_EXCLUDED(first)
{
util::Lock lock(&first);
Lock_t lock(&first);
for(const auto& item : second)
visit(item.second);
}
@ -120,13 +123,15 @@ namespace llarp
struct SyncOwnedPathsMap_t
{
util::Mutex first; // protects second
using Mutex_t = util::Mutex;
using Lock_t = util::Lock;
Mutex_t first; // protects second
OwnedPathsMap_t second GUARDED_BY(first);
void
ForEach(std::function< void(const PathSet_ptr&) > visit)
{
util::Lock lock(&first);
Lock_t lock(&first);
for(const auto& item : second)
visit(item.second);
}

Loading…
Cancel
Save