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/null.cpp
handlers/tun.cpp
link/curvecp.cpp
link/encoder.cpp
link/iwp.cpp
link/server.cpp

@ -423,7 +423,7 @@ namespace llarp
auto itr = m_Paths.find(next);
if(itr != m_Paths.end())
return false;
m_Paths.insert(std::make_pair(next, remote));
m_Paths.emplace(next, remote);
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)
{
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 >
@ -160,24 +160,26 @@ namespace llarp
IHopHandler*
PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id)
{
auto own = MapGet(m_OurPaths, id,
[](__attribute__((unused)) const PathSet* s) -> bool {
// TODO: is this right?
return true;
},
[remote, id](PathSet* p) -> IHopHandler* {
return p->GetByUpstream(remote, id);
});
auto own = MapGet(
m_OurPaths, id,
[](__attribute__((unused)) const PathSet* s) -> bool {
// TODO: is this right?
return true;
},
[remote, id](PathSet* p) -> IHopHandler* {
return p->GetByUpstream(remote, id);
});
if(own)
return own;
return MapGet(m_TransitPaths, id,
[remote](const std::shared_ptr< TransitHop >& hop) -> bool {
return hop->info.upstream == remote;
},
[](const std::shared_ptr< TransitHop >& h) -> IHopHandler* {
return h.get();
});
return MapGet(
m_TransitPaths, id,
[remote](const std::shared_ptr< TransitHop >& hop) -> bool {
return hop->info.upstream == remote;
},
[](const std::shared_ptr< TransitHop >& h) -> IHopHandler* {
return h.get();
});
}
bool
@ -194,13 +196,14 @@ namespace llarp
IHopHandler*
PathContext::GetByDownstream(const RouterID& remote, const PathID_t& id)
{
return MapGet(m_TransitPaths, id,
[remote](const std::shared_ptr< TransitHop >& hop) -> bool {
return hop->info.downstream == remote;
},
[](const std::shared_ptr< TransitHop >& h) -> IHopHandler* {
return h.get();
});
return MapGet(
m_TransitPaths, id,
[remote](const std::shared_ptr< TransitHop >& hop) -> bool {
return hop->info.downstream == remote;
},
[](const std::shared_ptr< TransitHop >& h) -> IHopHandler* {
return h.get();
});
}
PathSet*

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

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

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

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

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

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

@ -371,7 +371,7 @@ namespace llarp
{
llarp::LogInfo("autostarting 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;
}
llarp::LogError("failed to start hidden service endpoint ", conf.first);
@ -380,7 +380,7 @@ namespace llarp
else
{
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;
}
}

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

Loading…
Cancel
Save