try fixing leak also make format

pull/576/head
Jeff Becker 5 years ago
parent 7cb659343b
commit d50b18d7b0
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -107,7 +107,8 @@ namespace llarp
template < typename User > template < typename User >
struct AsyncFrameDecrypter struct AsyncFrameDecrypter
{ {
using DecryptHandler = std::function< void(llarp_buffer_t*, User*) >; using User_ptr = std::shared_ptr< User >;
using DecryptHandler = std::function< void(llarp_buffer_t*, User_ptr) >;
static void static void
Decrypt(void* user) Decrypt(void* user)
@ -119,10 +120,11 @@ namespace llarp
{ {
auto buf = ctx->target.Buffer(); auto buf = ctx->target.Buffer();
buf->cur = buf->base + EncryptedFrameOverheadSize; buf->cur = buf->base + EncryptedFrameOverheadSize;
ctx->result(buf, ctx->context); ctx->result(buf, ctx->user);
} }
else else
ctx->result(nullptr, ctx->context); ctx->result(nullptr, ctx->user);
ctx->user = nullptr;
} }
AsyncFrameDecrypter(llarp::Crypto* c, const SecretKey& secretkey, AsyncFrameDecrypter(llarp::Crypto* c, const SecretKey& secretkey,
@ -132,17 +134,17 @@ namespace llarp
} }
DecryptHandler result; DecryptHandler result;
User* context; User_ptr user;
llarp::Crypto* crypto; llarp::Crypto* crypto;
const SecretKey& seckey; const SecretKey& seckey;
EncryptedFrame target; EncryptedFrame target;
void void
AsyncDecrypt(llarp_threadpool* worker, const EncryptedFrame& frame, AsyncDecrypt(llarp_threadpool* worker, const EncryptedFrame& frame,
User* user) User_ptr u)
{ {
target = frame; target = frame;
context = user; user = u;
llarp_threadpool_queue_job(worker, {this, &Decrypt}); llarp_threadpool_queue_job(worker, {this, &Decrypt});
} }
}; };

@ -32,7 +32,7 @@ namespace llarp
for(std::string arg; std::getline(s, arg, ' ');) for(std::string arg; std::getline(s, arg, ' ');)
{ {
_args.emplace_back(std::move(arg)); _args.emplace_back(std::move(arg));
char * ptr = (char *) _args.back().c_str(); char *ptr = (char *)_args.back().c_str();
args.push_back(ptr); args.push_back(ptr);
} }
args.push_back(0); args.push_back(0);
@ -53,7 +53,7 @@ namespace llarp
for(const auto &item : self->m_env) for(const auto &item : self->m_env)
{ {
_env.emplace_back(item.first + "=" + item.second); _env.emplace_back(item.first + "=" + item.second);
char * ptr = (char *) _env.back().c_str(); char *ptr = (char *)_env.back().c_str();
env.push_back(ptr); env.push_back(ptr);
} }
env.push_back(0); env.push_back(0);

@ -184,7 +184,8 @@ namespace llarp
typedef llarp::path::PathContext Context; typedef llarp::path::PathContext Context;
typedef llarp::path::TransitHop Hop; typedef llarp::path::TransitHop Hop;
typedef AsyncFrameDecrypter< LRCMFrameDecrypt > Decrypter; typedef AsyncFrameDecrypter< LRCMFrameDecrypt > Decrypter;
std::unique_ptr< Decrypter > decrypter; using Decrypter_ptr = std::unique_ptr< Decrypter >;
Decrypter_ptr decrypter;
std::array< EncryptedFrame, 8 > frames; std::array< EncryptedFrame, 8 > frames;
Context* context; Context* context;
// decrypted record // decrypted record
@ -192,9 +193,9 @@ namespace llarp
// the actual hop // the actual hop
std::shared_ptr< Hop > hop; std::shared_ptr< Hop > hop;
LRCMFrameDecrypt(Context* ctx, Decrypter* dec, LRCMFrameDecrypt(Context* ctx, Decrypter_ptr dec,
const LR_CommitMessage* commit) const LR_CommitMessage* commit)
: decrypter(dec) : decrypter(std::move(dec))
, frames(commit->frames) , frames(commit->frames)
, context(ctx) , context(ctx)
, hop(std::make_shared< Hop >()) , hop(std::make_shared< Hop >())
@ -208,10 +209,8 @@ namespace llarp
/// this is done from logic thread /// this is done from logic thread
static void static void
SendLRCM(void* user) SendLRCM(std::shared_ptr< LRCMFrameDecrypt > self)
{ {
std::shared_ptr< LRCMFrameDecrypt > self(
static_cast< LRCMFrameDecrypt* >(user));
if(!self->context->Router()->ConnectionToRouterAllowed( if(!self->context->Router()->ConnectionToRouterAllowed(
self->hop->info.upstream)) self->hop->info.upstream))
{ {
@ -240,18 +239,16 @@ namespace llarp
&& self->record.nextRC->Verify(self->context->crypto(), now)) && self->record.nextRC->Verify(self->context->crypto(), now))
{ {
llarp_nodedb* n = self->context->Router()->nodedb(); llarp_nodedb* n = self->context->Router()->nodedb();
const RouterContact rc = std::move(*self->record.nextRC); const RouterContact rc = *self->record.nextRC;
// store it into netdb if we don't have it // store it into netdb if we don't have it
if(!n->Has(rc.pubkey)) if(!n->Has(rc.pubkey))
{ {
static std::function< void(std::shared_ptr< LRCMFrameDecrypt >) > cb =
[](std::shared_ptr< LRCMFrameDecrypt > ctx) {
ctx->context->ForwardLRCM(ctx->hop->info.upstream,
ctx->frames);
ctx->hop = nullptr;
};
llarp::Logic* logic = self->context->Router()->logic(); llarp::Logic* logic = self->context->Router()->logic();
n->InsertAsync(rc, logic, std::bind(cb, self)); n->InsertAsync(rc, logic, [=]() {
self->context->ForwardLRCM(self->hop->info.upstream,
self->frames);
self->hop = nullptr;
});
return; return;
} }
} }
@ -264,17 +261,15 @@ namespace llarp
// this is called from the logic thread // this is called from the logic thread
static void static void
SendPathConfirm(void* user) SendPathConfirm(std::shared_ptr< LRCMFrameDecrypt > self)
{ {
std::unique_ptr< LRCMFrameDecrypt > self(
static_cast< LRCMFrameDecrypt* >(user));
// persist session to downstream until path expiration // persist session to downstream until path expiration
self->context->Router()->PersistSessionUntil( self->context->Router()->PersistSessionUntil(
self->hop->info.downstream, self->hop->ExpireTime() + 10000); self->hop->info.downstream, self->hop->ExpireTime() + 10000);
// put hop // put hop
self->context->PutTransitHop(self->hop); self->context->PutTransitHop(self->hop);
// send path confirmation // send path confirmation
llarp::routing::PathConfirmMessage confirm(self->hop->lifetime); const llarp::routing::PathConfirmMessage confirm(self->hop->lifetime);
if(!self->hop->SendRoutingMessage(confirm, self->context->Router())) if(!self->hop->SendRoutingMessage(confirm, self->context->Router()))
{ {
llarp::LogError("failed to send path confirmation for ", llarp::LogError("failed to send path confirmation for ",
@ -284,14 +279,15 @@ namespace llarp
} }
static void static void
HandleDecrypted(llarp_buffer_t* buf, LRCMFrameDecrypt* self) HandleDecrypted(llarp_buffer_t* buf,
std::shared_ptr< LRCMFrameDecrypt > self)
{ {
auto now = self->context->Router()->Now(); auto now = self->context->Router()->Now();
auto& info = self->hop->info; auto& info = self->hop->info;
if(!buf) if(!buf)
{ {
llarp::LogError("LRCM decrypt failed from ", info.downstream); llarp::LogError("LRCM decrypt failed from ", info.downstream);
delete self; self->decrypter = nullptr;
return; return;
} }
buf->cur = buf->base + EncryptedFrameOverheadSize; buf->cur = buf->base + EncryptedFrameOverheadSize;
@ -300,7 +296,7 @@ namespace llarp
if(!self->record.BDecode(buf)) if(!self->record.BDecode(buf))
{ {
llarp::LogError("malformed frame inside LRCM from ", info.downstream); llarp::LogError("malformed frame inside LRCM from ", info.downstream);
delete self; self->decrypter = nullptr;
return; return;
} }
@ -310,7 +306,7 @@ namespace llarp
if(self->context->HasTransitHop(info)) if(self->context->HasTransitHop(info))
{ {
llarp::LogError("duplicate transit hop ", info); llarp::LogError("duplicate transit hop ", info);
delete self; self->decrypter = nullptr;
return; return;
} }
// generate path key as we are in a worker thread // generate path key as we are in a worker thread
@ -320,7 +316,7 @@ namespace llarp
self->record.tunnelNonce)) self->record.tunnelNonce))
{ {
llarp::LogError("LRCM DH Failed ", info); llarp::LogError("LRCM DH Failed ", info);
delete self; self->decrypter = nullptr;
return; return;
} }
// generate hash of hop key for nonce mutation // generate hash of hop key for nonce mutation
@ -366,13 +362,19 @@ namespace llarp
// we are the farthest hop // we are the farthest hop
llarp::LogDebug("We are the farthest hop for ", info); llarp::LogDebug("We are the farthest hop for ", info);
// send a LRAM down the path // send a LRAM down the path
self->context->logic()->queue_job({self, &SendPathConfirm}); self->context->logic()->queue_func([=]() {
SendPathConfirm(self);
self->decrypter = nullptr;
});
} }
else else
{ {
// forward upstream // forward upstream
// we are still in the worker thread so post job to logic // we are still in the worker thread so post job to logic
self->context->logic()->queue_job({self, &SendLRCM}); self->context->logic()->queue_func([=]() {
SendLRCM(self);
self->decrypter = nullptr;
});
} }
} }
}; };
@ -380,16 +382,16 @@ namespace llarp
bool bool
LR_CommitMessage::AsyncDecrypt(llarp::path::PathContext* context) const LR_CommitMessage::AsyncDecrypt(llarp::path::PathContext* context) const
{ {
LRCMFrameDecrypt::Decrypter* decrypter = new LRCMFrameDecrypt::Decrypter( auto decrypter = std::make_unique< LRCMFrameDecrypt::Decrypter >(
context->crypto(), context->EncryptionSecretKey(), context->crypto(), context->EncryptionSecretKey(),
&LRCMFrameDecrypt::HandleDecrypted); &LRCMFrameDecrypt::HandleDecrypted);
// copy frames so we own them // copy frames so we own them
LRCMFrameDecrypt* frameDecrypt = auto frameDecrypt = std::make_shared< LRCMFrameDecrypt >(
new LRCMFrameDecrypt(context, decrypter, this); context, std::move(decrypter), this);
// decrypt frames async // decrypt frames async
decrypter->AsyncDecrypt(context->Worker(), frameDecrypt->frames[0], frameDecrypt->decrypter->AsyncDecrypt(
frameDecrypt); context->Worker(), frameDecrypt->frames[0], frameDecrypt);
return true; return true;
} }
} // namespace llarp } // namespace llarp

@ -49,6 +49,11 @@ namespace llarp
{ {
std::array< EncryptedFrame, 8 > frames; std::array< EncryptedFrame, 8 > frames;
LR_CommitMessage(const std::array< EncryptedFrame, 8 > &_frames)
: ILinkMessage(), frames(_frames)
{
}
LR_CommitMessage() : ILinkMessage() LR_CommitMessage() : ILinkMessage()
{ {
} }

@ -87,8 +87,7 @@ namespace llarp
const std::array< EncryptedFrame, 8 >& frames) const std::array< EncryptedFrame, 8 >& frames)
{ {
LogDebug("forwarding LRCM to ", nextHop); LogDebug("forwarding LRCM to ", nextHop);
LR_CommitMessage msg; const LR_CommitMessage msg(frames);
msg.frames = frames;
return m_Router->SendToOrQueue(nextHop, &msg); return m_Router->SendToOrQueue(nextHop, &msg);
} }
template < typename Map_t, typename Key_t, typename CheckValue_t, template < typename Map_t, typename Key_t, typename CheckValue_t,

@ -970,8 +970,9 @@ namespace llarp
RemoveConvoTag(frame.T); RemoveConvoTag(frame.T);
return true; return true;
} }
if(!frame.AsyncDecryptAndVerify(EndpointLogic(), crypto(), p, CryptoWorker(), if(!frame.AsyncDecryptAndVerify(EndpointLogic(), crypto(), p,
m_Identity, m_DataHandler)) CryptoWorker(), m_Identity,
m_DataHandler))
{ {
// send discard // send discard
ProtocolFrame f; ProtocolFrame f;

Loading…
Cancel
Save