Replace insert(make_pair()) with emplace()

pull/318/head
Michael 5 years ago
parent 8e51d3a491
commit 887fb4ac62
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -143,7 +143,6 @@ set(LIB_SRC
handlers/exit.cpp handlers/exit.cpp
handlers/null.cpp handlers/null.cpp
handlers/tun.cpp handlers/tun.cpp
link/curvecp.cpp
link/encoder.cpp link/encoder.cpp
link/iwp.cpp link/iwp.cpp
link/server.cpp link/server.cpp

@ -423,7 +423,7 @@ namespace llarp
auto itr = m_Paths.find(next); auto itr = m_Paths.find(next);
if(itr != m_Paths.end()) if(itr != m_Paths.end())
return false; return false;
m_Paths.insert(std::make_pair(next, remote)); m_Paths.emplace(next, remote);
return true; return true;
} }

@ -1,16 +0,0 @@
#include <link/curvecp.hpp>
#include <link/server.hpp>
#include <messages/link_intro.hpp>
namespace llarp
{
namespace curvecp
{
std::unique_ptr< ILinkLayer >
NewServer(__attribute__((unused)) llarp::Router* r)
{
return nullptr;
}
} // namespace curvecp
} // namespace llarp

@ -1,18 +0,0 @@
#ifndef LLARP_LINK_CURVECP_HPP
#define LLARP_LINK_CURVECP_HPP
#include <memory>
namespace llarp
{
struct ILinkLayer;
struct Router;
namespace curvecp
{
std::unique_ptr< ILinkLayer >
NewServer(llarp::Router* r);
}
} // namespace llarp
#endif

@ -113,7 +113,7 @@ namespace llarp
MapPut(Map_t& map, const Key_t& k, const Value_t& v) MapPut(Map_t& map, const Key_t& k, const Value_t& v)
{ {
util::Lock lock(map.first); util::Lock lock(map.first);
map.second.insert(std::make_pair(k, v)); map.second.emplace(k, v);
} }
template < typename Map_t, typename Visit_t > template < typename Map_t, typename Visit_t >
@ -160,7 +160,8 @@ namespace llarp
IHopHandler* IHopHandler*
PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id) PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id)
{ {
auto own = MapGet(m_OurPaths, id, auto own = MapGet(
m_OurPaths, id,
[](__attribute__((unused)) const PathSet* s) -> bool { [](__attribute__((unused)) const PathSet* s) -> bool {
// TODO: is this right? // TODO: is this right?
return true; return true;
@ -171,7 +172,8 @@ namespace llarp
if(own) if(own)
return own; return own;
return MapGet(m_TransitPaths, id, return MapGet(
m_TransitPaths, id,
[remote](const std::shared_ptr< TransitHop >& hop) -> bool { [remote](const std::shared_ptr< TransitHop >& hop) -> bool {
return hop->info.upstream == remote; return hop->info.upstream == remote;
}, },
@ -194,7 +196,8 @@ namespace llarp
IHopHandler* IHopHandler*
PathContext::GetByDownstream(const RouterID& remote, const PathID_t& id) PathContext::GetByDownstream(const RouterID& remote, const PathID_t& id)
{ {
return MapGet(m_TransitPaths, id, return MapGet(
m_TransitPaths, id,
[remote](const std::shared_ptr< TransitHop >& hop) -> bool { [remote](const std::shared_ptr< TransitHop >& hop) -> bool {
return hop->info.downstream == remote; return hop->info.downstream == remote;
}, },

@ -15,8 +15,8 @@ namespace llarp
template < typename User > template < typename User >
struct AsyncPathKeyExchangeContext struct AsyncPathKeyExchangeContext
{ {
typedef llarp::path::Path Path_t; typedef path::Path Path_t;
typedef llarp::path::Builder PathSet_t; typedef path::Builder PathSet_t;
PathSet_t* pathset = nullptr; PathSet_t* pathset = nullptr;
Path_t* path = nullptr; Path_t* path = nullptr;
typedef std::function< void(AsyncPathKeyExchangeContext< User >*) > Handler; typedef std::function< void(AsyncPathKeyExchangeContext< User >*) > Handler;
@ -26,8 +26,8 @@ namespace llarp
size_t idx = 0; size_t idx = 0;
AbstractRouter* router = nullptr; AbstractRouter* router = nullptr;
llarp_threadpool* worker = nullptr; llarp_threadpool* worker = nullptr;
llarp::Logic* logic = nullptr; Logic* logic = nullptr;
llarp::Crypto* crypto = nullptr; Crypto* crypto = nullptr;
LR_CommitMessage LRCM; LR_CommitMessage LRCM;
~AsyncPathKeyExchangeContext() ~AsyncPathKeyExchangeContext()
@ -61,7 +61,7 @@ namespace llarp
if(!ctx->crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey, if(!ctx->crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey,
hop.nonce)) hop.nonce))
{ {
llarp::LogError("Failed to generate shared key for path build"); LogError("Failed to generate shared key for path build");
delete ctx; delete ctx;
return; return;
} }
@ -87,7 +87,7 @@ namespace llarp
record.rxid = hop.rxID; record.rxid = hop.rxID;
record.tunnelNonce = hop.nonce; record.tunnelNonce = hop.nonce;
record.nextHop = hop.upstream; record.nextHop = hop.upstream;
record.commkey = llarp::seckey_topublic(hop.commkey); record.commkey = seckey_topublic(hop.commkey);
auto buf = frame.Buffer(); auto buf = frame.Buffer();
buf->cur = buf->base + EncryptedFrameOverheadSize; buf->cur = buf->base + EncryptedFrameOverheadSize;
@ -95,8 +95,8 @@ namespace llarp
if(!record.BEncode(buf)) if(!record.BEncode(buf))
{ {
// failed to encode? // failed to encode?
llarp::LogError("Failed to generate Commit Record"); LogError("Failed to generate Commit Record");
llarp::DumpBuffer(*buf); DumpBuffer(*buf);
delete ctx; delete ctx;
return; return;
} }
@ -105,7 +105,7 @@ namespace llarp
ctx->crypto->encryption_keygen(framekey); ctx->crypto->encryption_keygen(framekey);
if(!frame.EncryptInPlace(framekey, hop.rc.enckey, ctx->crypto)) if(!frame.EncryptInPlace(framekey, hop.rc.enckey, ctx->crypto))
{ {
llarp::LogError("Failed to encrypt LRCR"); LogError("Failed to encrypt LRCR");
delete ctx; delete ctx;
return; return;
} }
@ -122,14 +122,14 @@ namespace llarp
} }
} }
AsyncPathKeyExchangeContext(llarp::Crypto* c) : crypto(c) AsyncPathKeyExchangeContext(Crypto* c) : crypto(c)
{ {
} }
/// Generate all keys asynchronously and call handler when done /// Generate all keys asynchronously and call handler when done
void void
AsyncGenerateKeys(Path_t* p, llarp::Logic* l, llarp_threadpool* pool, AsyncGenerateKeys(Path_t* p, Logic* l, llarp_threadpool* pool, User* u,
User* u, Handler func) Handler func)
{ {
path = p; path = p;
logic = l; logic = l;
@ -161,7 +161,7 @@ namespace llarp
ctx->path = nullptr; ctx->path = nullptr;
} }
else else
llarp::LogError("failed to send LRCM to ", remote); LogError("failed to send LRCM to ", remote);
} }
// decrement keygen counter // decrement keygen counter
ctx->pathset->keygens--; ctx->pathset->keygens--;
@ -171,10 +171,7 @@ namespace llarp
{ {
Builder::Builder(AbstractRouter* p_router, struct llarp_dht_context* p_dht, Builder::Builder(AbstractRouter* p_router, struct llarp_dht_context* p_dht,
size_t pathNum, size_t hops) size_t pathNum, size_t hops)
: llarp::path::PathSet(pathNum) : path::PathSet(pathNum), router(p_router), dht(p_dht), numHops(hops)
, router(p_router)
, dht(p_dht)
, numHops(hops)
{ {
p_router->pathContext().AddPathBuilder(this); p_router->pathContext().AddPathBuilder(this);
p_router->crypto()->encryption_keygen(enckey); p_router->crypto()->encryption_keygen(enckey);
@ -281,7 +278,7 @@ namespace llarp
{ {
if(!SelectHop(nodedb, hops[0], hops[0], 0, roles)) if(!SelectHop(nodedb, hops[0], hops[0], 0, roles))
{ {
llarp::LogError("failed to select first hop"); LogError("failed to select first hop");
return false; return false;
} }
} }
@ -290,7 +287,7 @@ namespace llarp
if(!SelectHop(nodedb, hops[idx - 1], hops[idx], idx, roles)) if(!SelectHop(nodedb, hops[idx - 1], hops[idx], idx, roles))
{ {
/// TODO: handle this failure properly /// TODO: handle this failure properly
llarp::LogWarn("Failed to select hop ", idx); LogWarn("Failed to select hop ", idx);
return false; return false;
} }
} }
@ -316,9 +313,9 @@ namespace llarp
new AsyncPathKeyExchangeContext< Builder >(router->crypto()); new AsyncPathKeyExchangeContext< Builder >(router->crypto());
ctx->router = router; ctx->router = router;
ctx->pathset = this; ctx->pathset = this;
auto path = new llarp::path::Path(hops, this, roles); auto path = new path::Path(hops, this, roles);
path->SetBuildResultHook(std::bind(&llarp::path::Builder::HandlePathBuilt, path->SetBuildResultHook(std::bind(&path::Builder::HandlePathBuilt, this,
this, std::placeholders::_1)); std::placeholders::_1));
++keygens; ++keygens;
ctx->AsyncGenerateKeys(path, router->logic(), router->threadpool(), this, ctx->AsyncGenerateKeys(path, router->logic(), router->threadpool(), this,
&PathBuilderKeysGenerated); &PathBuilderKeysGenerated);
@ -344,7 +341,7 @@ namespace llarp
void void
Builder::ManualRebuild(size_t num, PathRole roles) Builder::ManualRebuild(size_t num, PathRole roles)
{ {
llarp::LogDebug("manual rebuild ", num); LogDebug("manual rebuild ", num);
while(num--) while(num--)
BuildOne(roles); BuildOne(roles);
} }

@ -24,7 +24,7 @@ namespace llarp
public: public:
AbstractRouter* router; AbstractRouter* router;
llarp_dht_context* dht; llarp_dht_context* dht;
llarp::SecretKey enckey; SecretKey enckey;
size_t numHops; size_t numHops;
llarp_time_t lastBuild = 0; llarp_time_t lastBuild = 0;
llarp_time_t buildIntervalLimit = MIN_PATH_BUILD_INTERVAL; llarp_time_t buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;

@ -207,7 +207,7 @@ namespace llarp
Lock_t l(m_PathsMutex); Lock_t l(m_PathsMutex);
auto upstream = path->Upstream(); // RouterID auto upstream = path->Upstream(); // RouterID
auto RXID = path->RXID(); // PathID auto RXID = path->RXID(); // PathID
m_Paths.insert(std::make_pair(std::make_pair(upstream, RXID), path)); m_Paths.emplace(std::make_pair(upstream, RXID), path);
} }
void void
@ -234,8 +234,8 @@ namespace llarp
bool bool
PathSet::GetCurrentIntroductionsWithFilter( PathSet::GetCurrentIntroductionsWithFilter(
std::set< llarp::service::Introduction >& intros, std::set< service::Introduction >& intros,
std::function< bool(const llarp::service::Introduction&) > filter) const std::function< bool(const service::Introduction&) > filter) const
{ {
intros.clear(); intros.clear();
size_t count = 0; size_t count = 0;
@ -255,7 +255,7 @@ namespace llarp
bool bool
PathSet::GetCurrentIntroductions( PathSet::GetCurrentIntroductions(
std::set< llarp::service::Introduction >& intros) const std::set< service::Introduction >& intros) const
{ {
intros.clear(); intros.clear();
size_t count = 0; size_t count = 0;
@ -276,7 +276,7 @@ namespace llarp
void void
PathSet::HandlePathBuildTimeout(Path* p) PathSet::HandlePathBuildTimeout(Path* p)
{ {
llarp::LogInfo("path build for ", p->Name(), " has timed out"); LogInfo("path build for ", p->Name(), " has timed out");
} }
bool bool
@ -314,7 +314,7 @@ namespace llarp
auto sz = established.size(); auto sz = established.size();
if(sz) if(sz)
{ {
return established[llarp::randint() % sz]; return established[randint() % sz];
} }
else else
return nullptr; return nullptr;

@ -139,7 +139,7 @@ namespace llarp
/// override me in subtype /// override me in subtype
virtual bool virtual bool
HandleGotIntroMessage(__attribute__((unused)) HandleGotIntroMessage(__attribute__((unused))
const llarp::dht::GotIntroMessage* msg) const dht::GotIntroMessage* msg)
{ {
return false; return false;
} }
@ -147,7 +147,7 @@ namespace llarp
/// override me in subtype /// override me in subtype
virtual bool virtual bool
HandleGotRouterMessage(__attribute__((unused)) HandleGotRouterMessage(__attribute__((unused))
const llarp::dht::GotRouterMessage* msg) const dht::GotRouterMessage* msg)
{ {
return false; return false;
} }
@ -177,13 +177,11 @@ namespace llarp
bool bool
GetCurrentIntroductionsWithFilter( GetCurrentIntroductionsWithFilter(
std::set< llarp::service::Introduction >& intros, std::set< service::Introduction >& intros,
std::function< bool(const llarp::service::Introduction&) > filter) std::function< bool(const service::Introduction&) > filter) const;
const;
bool bool
GetCurrentIntroductions( GetCurrentIntroductions(std::set< service::Introduction >& intros) const;
std::set< llarp::service::Introduction >& intros) const;
virtual bool virtual bool
PublishIntroSet(__attribute__((unused)) AbstractRouter* r) PublishIntroSet(__attribute__((unused)) AbstractRouter* r)
@ -232,8 +230,8 @@ namespace llarp
return RouterID::Hash()(i.first) ^ PathID_t::Hash()(i.second); return RouterID::Hash()(i.first) ^ PathID_t::Hash()(i.second);
} }
}; };
using Mtx_t = llarp::util::NullMutex; using Mtx_t = util::NullMutex;
using Lock_t = llarp::util::NullLock; using Lock_t = util::NullLock;
using PathMap_t = std::unordered_map< PathInfo_t, Path*, PathInfoHash >; using PathMap_t = std::unordered_map< PathInfo_t, Path*, PathInfoHash >;
mutable Mtx_t m_PathsMutex; mutable Mtx_t m_PathsMutex;
PathMap_t m_Paths; PathMap_t m_Paths;

@ -142,7 +142,7 @@ namespace llarp
if(!profile.BDecode(buf)) if(!profile.BDecode(buf))
return false; return false;
RouterID pk = k.base; RouterID pk = k.base;
return m_Profiles.insert(std::make_pair(pk, profile)).second; return m_Profiles.emplace(pk, profile).second;
} }
bool bool

@ -315,7 +315,7 @@ namespace llarp
auto itr = outboundMessageQueue.find(remote); auto itr = outboundMessageQueue.find(remote);
if(itr == outboundMessageQueue.end()) if(itr == outboundMessageQueue.end())
{ {
outboundMessageQueue.insert(std::make_pair(remote, MessageQueue())); outboundMessageQueue.emplace(remote, MessageQueue());
} }
// encode // encode
llarp_buffer_t buf(linkmsg_buffer); llarp_buffer_t buf(linkmsg_buffer);
@ -450,7 +450,7 @@ namespace llarp
void void
Router::AddInboundLink(std::unique_ptr< ILinkLayer > &link) Router::AddInboundLink(std::unique_ptr< ILinkLayer > &link)
{ {
inboundLinks.insert(std::move(link)); inboundLinks.emplace(std::move(link));
} }
bool bool
@ -808,7 +808,7 @@ namespace llarp
} }
else else
{ {
netConfig.insert(std::make_pair(key, val)); netConfig.emplace(key, val);
} }
} }
else if(StrEq(section, "api")) else if(StrEq(section, "api"))
@ -1522,10 +1522,10 @@ namespace llarp
LogError( LogError(
"Could not find any free lokitun interface names, can't "Could not find any free lokitun interface names, can't
auto set up " "default HS context for client"); defaultIfAddr = "no"; auto set up " "default HS context for client"); defaultIfAddr = "no";
netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); netConfig.emplace("defaultIfAddr", defaultIfAddr);
return false; return false;
} }
netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); netConfig.emplace("defaultIfAddr", defaultIfAddr);
} }
if(defaultIfName == "auto") if(defaultIfName == "auto")
{ {
@ -1536,10 +1536,10 @@ namespace llarp
LogError( LogError(
"Could not find any free private ip ranges, can't auto "Could not find any free private ip ranges, can't auto
set up " "default HS context for client"); defaultIfName = "no"; set up " "default HS context for client"); defaultIfName = "no";
netConfig.emplace(std::make_pair("defaultIfName", defaultIfName)); netConfig.emplace("defaultIfName", defaultIfName);
return false; return false;
} }
netConfig.emplace(std::make_pair("defaultIfName", defaultIfName)); netConfig.emplace("defaultIfName", defaultIfName);
} }
*/ */
return true; return true;
@ -1720,7 +1720,7 @@ namespace llarp
auto found = netConfig.find(itr->first); auto found = netConfig.find(itr->first);
if(found == netConfig.end() || found->second.empty()) if(found == netConfig.end() || found->second.empty())
{ {
netConfig.emplace(std::make_pair(itr->first, itr->second())); netConfig.emplace(itr->first, itr->second());
} }
++itr; ++itr;
} }

@ -371,7 +371,7 @@ namespace llarp
{ {
llarp::LogInfo("autostarting hidden service endpoint ", llarp::LogInfo("autostarting hidden service endpoint ",
service->Name()); service->Name());
m_Endpoints.insert(std::make_pair(conf.first, std::move(service))); m_Endpoints.emplace(conf.first, std::move(service));
return true; return true;
} }
llarp::LogError("failed to start hidden service endpoint ", conf.first); llarp::LogError("failed to start hidden service endpoint ", conf.first);
@ -380,7 +380,7 @@ namespace llarp
else else
{ {
llarp::LogInfo("added hidden service endpoint ", service->Name()); llarp::LogInfo("added hidden service endpoint ", service->Name());
m_Endpoints.insert(std::make_pair(conf.first, std::move(service))); m_Endpoints.emplace(conf.first, std::move(service));
return true; return true;
} }
} }

@ -267,7 +267,7 @@ namespace llarp
if(itr == m_PrefetchedTags.end()) if(itr == m_PrefetchedTags.end())
{ {
itr = m_PrefetchedTags itr = m_PrefetchedTags
.insert(std::make_pair(tag, CachedTagResult(tag, this))) .emplace(tag, CachedTagResult(tag, this))
.first; .first;
} }
for(const auto& introset : itr->second.result) for(const auto& introset : itr->second.result)
@ -322,8 +322,7 @@ namespace llarp
if(itr->second->Tick(now)) if(itr->second->Tick(now))
{ {
itr->second->Stop(); itr->second->Stop();
m_DeadSessions.insert( m_DeadSessions.emplace(itr->first, std::move(itr->second));
std::make_pair(itr->first, std::move(itr->second)));
itr = m_RemoteSessions.erase(itr); itr = m_RemoteSessions.erase(itr);
} }
else else
@ -406,10 +405,9 @@ namespace llarp
Endpoint::PutLookup(IServiceLookup* lookup, uint64_t txid) Endpoint::PutLookup(IServiceLookup* lookup, uint64_t txid)
{ {
// std::unique_ptr< service::IServiceLookup > ptr(lookup); // std::unique_ptr< service::IServiceLookup > ptr(lookup);
// m_PendingLookups.insert(std::make_pair(txid, ptr)); // m_PendingLookups.emplace(txid, ptr);
// m_PendingLookups[txid] = std::move(ptr); // m_PendingLookups[txid] = std::move(ptr);
m_PendingLookups.insert( m_PendingLookups.emplace(txid, std::unique_ptr< IServiceLookup >(lookup));
std::make_pair(txid, std::unique_ptr< IServiceLookup >(lookup)));
} }
bool bool
@ -457,7 +455,7 @@ namespace llarp
auto itr = m_Sessions.find(tag); auto itr = m_Sessions.find(tag);
if(itr == m_Sessions.end()) if(itr == m_Sessions.end())
{ {
itr = m_Sessions.insert(std::make_pair(tag, Session{})).first; itr = m_Sessions.emplace(tag, Session{}).first;
} }
itr->second.remote = info; itr->second.remote = info;
itr->second.lastUsed = Now(); itr->second.lastUsed = Now();
@ -479,7 +477,7 @@ namespace llarp
auto itr = m_Sessions.find(tag); auto itr = m_Sessions.find(tag);
if(itr == m_Sessions.end()) if(itr == m_Sessions.end())
{ {
itr = m_Sessions.insert(std::make_pair(tag, Session{})).first; itr = m_Sessions.emplace(tag, Session{}).first;
} }
itr->second.intro = intro; itr->second.intro = intro;
itr->second.lastUsed = Now(); itr->second.lastUsed = Now();
@ -529,7 +527,7 @@ namespace llarp
auto itr = m_Sessions.find(tag); auto itr = m_Sessions.find(tag);
if(itr == m_Sessions.end()) if(itr == m_Sessions.end())
{ {
itr = m_Sessions.insert(std::make_pair(tag, Session{})).first; itr = m_Sessions.emplace(tag, Session{}).first;
} }
itr->second.sharedKey = k; itr->second.sharedKey = k;
itr->second.lastUsed = Now(); itr->second.lastUsed = Now();
@ -786,8 +784,7 @@ namespace llarp
} }
OutboundContext* ctx = new OutboundContext(introset, this); OutboundContext* ctx = new OutboundContext(introset, this);
m_RemoteSessions.insert( m_RemoteSessions.emplace(addr, std::unique_ptr< OutboundContext >(ctx));
std::make_pair(addr, std::unique_ptr< OutboundContext >(ctx)));
llarp::LogInfo("Created New outbound context for ", addr.ToString()); llarp::LogInfo("Created New outbound context for ", addr.ToString());
// inform pending // inform pending
@ -848,8 +845,7 @@ namespace llarp
if(path && path->SendRoutingMessage(&msg, m_Router)) if(path && path->SendRoutingMessage(&msg, m_Router))
{ {
llarp::LogInfo(Name(), " looking up ", router); llarp::LogInfo(Name(), " looking up ", router);
m_PendingRouters.insert( m_PendingRouters.emplace(router, RouterLookupJob(this));
std::make_pair(router, RouterLookupJob(this)));
return true; return true;
} }
else else
@ -1089,7 +1085,7 @@ namespace llarp
llarp::LogInfo("doing lookup for ", remote, " via ", path->Endpoint()); llarp::LogInfo("doing lookup for ", remote, " via ", path->Endpoint());
if(job->SendRequestViaPath(path, Router())) if(job->SendRequestViaPath(path, Router()))
{ {
m_PendingServiceLookups.insert(std::make_pair(remote, hook)); m_PendingServiceLookups.emplace(remote, hook);
return true; return true;
} }
llarp::LogError("send via path failed"); llarp::LogError("send via path failed");
@ -1197,15 +1193,14 @@ namespace llarp
if(m_SNodeSessions.count(snode) == 0) if(m_SNodeSessions.count(snode) == 0)
{ {
auto themIP = ObtainIPForAddr(snode, true); auto themIP = ObtainIPForAddr(snode, true);
m_SNodeSessions.emplace(std::make_pair( m_SNodeSessions.emplace(
snode, snode,
std::unique_ptr< llarp::exit::BaseSession >( std::make_unique< exit::SNodeSession >(
new llarp::exit::SNodeSession(
snode, snode,
std::bind(&Endpoint::HandleWriteIPPacket, this, std::bind(&Endpoint::HandleWriteIPPacket, this,
std::placeholders::_1, std::placeholders::_1,
[themIP]() -> huint32_t { return themIP; }), [themIP]() -> huint32_t { return themIP; }),
m_Router, 2, numHops)))); m_Router, 2, numHops));
} }
} }

Loading…
Cancel
Save