diff --git a/llarp/crypto/encrypted_frame.hpp b/llarp/crypto/encrypted_frame.hpp index d10d799a3..7731d74e5 100644 --- a/llarp/crypto/encrypted_frame.hpp +++ b/llarp/crypto/encrypted_frame.hpp @@ -80,7 +80,7 @@ namespace llarp AsyncDecrypt(const EncryptedFrame& frame, User_ptr u, WorkerFunction_t worker) { target = frame; - worker(std::bind(&AsyncFrameDecrypter::Decrypt, this, std::move(u))); + worker([this, u = std::move(u)]() mutable { Decrypt(std::move(u)); }); } }; } // namespace llarp diff --git a/llarp/dht/explorenetworkjob.cpp b/llarp/dht/explorenetworkjob.cpp index 379f2d4b4..55c35a33c 100644 --- a/llarp/dht/explorenetworkjob.cpp +++ b/llarp/dht/explorenetworkjob.cpp @@ -30,14 +30,13 @@ namespace llarp llarp::LogDebug("got ", valuesFound.size(), " routers from exploration"); auto router = parent->GetRouter(); - using std::placeholders::_1; for (const auto& pk : valuesFound) { // lookup router if (router and router->nodedb()->Has(pk)) continue; parent->LookupRouter( - pk, std::bind(&AbstractRouter::HandleDHTLookupForExplore, router, pk, _1)); + pk, [router, pk](const auto& res) { router->HandleDHTLookupForExplore(pk, res); }); } } } // namespace dht diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index f775bcd3e..7c3d28a21 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -92,8 +92,7 @@ namespace llarp } return std::nullopt; } - else - return std::nullopt; + return std::nullopt; } const EventLoop_ptr& @@ -112,16 +111,13 @@ namespace llarp return false; if (auto* rid = std::get_if(&*maybeAddr)) { - auto range = m_ActiveExits.equal_range(PubKey{*rid}); - auto itr = range.first; - while (itr != range.second) + for (auto [itr, end] = m_ActiveExits.equal_range(PubKey{*rid}); itr != end; ++itr) { if (not itr->second->LooksDead(Now())) { if (itr->second->QueueInboundTraffic(ManagedBuffer{payload}, type)) return true; } - ++itr; } if (not m_Router->PathToRouterAllowed(*rid)) @@ -136,8 +132,7 @@ namespace llarp } return true; } - else - return false; + return false; } bool @@ -357,13 +352,9 @@ namespace llarp ExitEndpoint::VisitEndpointsFor( const PubKey& pk, std::function visit) const { - auto range = m_ActiveExits.equal_range(pk); - auto itr = range.first; - while (itr != range.second) + for (auto [itr, end] = m_ActiveExits.equal_range(pk); itr != end; ++itr) { - if (visit(itr->second.get())) - ++itr; - else + if (not visit(itr->second.get())) return true; } return false; @@ -422,25 +413,17 @@ namespace llarp " as we have no working endpoints"); } }); + for (auto& [pubkey, endpoint] : m_ActiveExits) { - auto itr = m_ActiveExits.begin(); - while (itr != m_ActiveExits.end()) + if (!endpoint->Flush()) { - if (!itr->second->Flush()) - { - LogWarn("exit session with ", itr->first, " dropped packets"); - } - ++itr; + LogWarn("exit session with ", pubkey, " dropped packets"); } } + for (auto& [id, session] : m_SNodeSessions) { - auto itr = m_SNodeSessions.begin(); - while (itr != m_SNodeSessions.end()) - { - itr->second->FlushUpstream(); - itr->second->FlushDownstream(); - ++itr; - } + session->FlushUpstream(); + session->FlushDownstream(); } m_Router->PumpLL(); } @@ -558,15 +541,13 @@ namespace llarp // find oldest activity ip address huint128_t found = {0}; llarp_time_t min = std::numeric_limits::max(); - auto itr = m_IPActivity.begin(); - while (itr != m_IPActivity.end()) + for (const auto& [addr, time] : m_IPActivity) { - if (itr->second < min) + if (time < min) { - found.h = itr->first.h; - min = itr->second; + found.h = addr.h; + min = time; } - ++itr; } // kick old ident off exit // TODO: DoS @@ -620,9 +601,9 @@ namespace llarp ExitEndpoint::AllRemoteEndpoints() const { std::unordered_set remote; - for (auto itr = m_Paths.begin(); itr != m_Paths.end(); ++itr) + for (const auto& [path, pubkey] : m_Paths) { - remote.insert(RouterID{itr->second}); + remote.insert(RouterID{pubkey}); } return remote; } @@ -640,9 +621,7 @@ namespace llarp huint128_t ip = m_KeyToIP[pk]; m_KeyToIP.erase(pk); m_IPToKey.erase(ip); - auto range = m_ActiveExits.equal_range(pk); - auto exit_itr = range.first; - while (exit_itr != range.second) + for (auto [exit_itr, end] = m_ActiveExits.equal_range(pk); exit_itr != end;) exit_itr = m_ActiveExits.erase(exit_itr); } @@ -677,19 +656,14 @@ namespace llarp { exit::Endpoint* endpoint = nullptr; PubKey pk; - { - auto itr = m_Paths.find(path); - if (itr == m_Paths.end()) - return nullptr; + if (auto itr = m_Paths.find(path); itr != m_Paths.end()) pk = itr->second; - } + else + return nullptr; + if (auto itr = m_ActiveExits.find(pk); itr != m_ActiveExits.end()) { - auto itr = m_ActiveExits.find(pk); - if (itr != m_ActiveExits.end()) - { - if (itr->second->PubKey() == pk) - endpoint = itr->second.get(); - } + if (itr->second->PubKey() == pk) + endpoint = itr->second.get(); } return endpoint; } @@ -698,8 +672,7 @@ namespace llarp ExitEndpoint::UpdateEndpointPath(const PubKey& remote, const PathID_t& next) { // check if already mapped - auto itr = m_Paths.find(next); - if (itr != m_Paths.end()) + if (auto itr = m_Paths.find(next); itr != m_Paths.end()) return false; m_Paths.emplace(next, remote); return true; @@ -780,7 +753,7 @@ namespace llarp { auto session = std::make_shared( other, - std::bind(&ExitEndpoint::QueueSNodePacket, this, std::placeholders::_1, ip), + [this, ip](const auto& buf) { return QueueSNodePacket(buf, ip); }, GetRouter(), 2, 1, @@ -837,18 +810,14 @@ namespace llarp void ExitEndpoint::RemoveExit(const exit::Endpoint* ep) { - auto range = m_ActiveExits.equal_range(ep->PubKey()); - auto itr = range.first; - while (itr != range.second) + for (auto [itr, end] = m_ActiveExits.equal_range(ep->PubKey()); itr != end; ++itr) { if (itr->second->GetCurrentPath() == ep->GetCurrentPath()) { - itr = m_ActiveExits.erase(itr); + m_ActiveExits.erase(itr); // now ep is gone af return; } - - ++itr; } } diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index cfea855e9..d5053b8d2 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -103,12 +103,10 @@ namespace llarp void CalculateTrafficStats(Stats& stats) { - auto itr = m_ActiveExits.begin(); - while (itr != m_ActiveExits.end()) + for (auto& [pubkey, endpoint] : m_ActiveExits) { - stats[itr->first].first += itr->second->TxRate(); - stats[itr->first].second += itr->second->RxRate(); - ++itr; + stats[pubkey].first += endpoint->TxRate(); + stats[pubkey].second += endpoint->RxRate(); } } diff --git a/llarp/hook/shell.cpp b/llarp/hook/shell.cpp index 9de5e2aee..2a740ea14 100644 --- a/llarp/hook/shell.cpp +++ b/llarp/hook/shell.cpp @@ -136,7 +136,7 @@ namespace llarp { auto job = std::make_shared(shared_from_this(), std::move(params)); - m_ThreadPool.addJob(std::bind(&ExecShellHookJob::Exec, job)); + m_ThreadPool.addJob([job = std::move(job)] { job->Exec(); }); } Backend_ptr diff --git a/llarp/messages/relay_status.cpp b/llarp/messages/relay_status.cpp index 77c3664a5..9ea58a55b 100644 --- a/llarp/messages/relay_status.cpp +++ b/llarp/messages/relay_status.cpp @@ -31,12 +31,12 @@ namespace llarp uint64_t _status, HopHandler_ptr _hop, AbstractRouter* _router, - const PathID_t& pathid) + PathID_t pathid) : frames{std::move(_frames)} , status{_status} , hop{std::move(_hop)} , router{_router} - , pathid{pathid} + , pathid{std::move(pathid)} {} ~LRSM_AsyncHandler() = default; @@ -51,7 +51,7 @@ namespace llarp void queue_handle() { - auto func = std::bind(&llarp::LRSM_AsyncHandler::handle, shared_from_this()); + auto func = [self = shared_from_this()] { self->handle(); }; router->QueueWork(func); } }; diff --git a/llarp/router/rc_lookup_handler.cpp b/llarp/router/rc_lookup_handler.cpp index 7a6255cd3..48150090f 100644 --- a/llarp/router/rc_lookup_handler.cpp +++ b/llarp/router/rc_lookup_handler.cpp @@ -94,8 +94,7 @@ namespace llarp if (shouldDoLookup) { - auto fn = - std::bind(&RCLookupHandler::HandleDHTLookupResult, this, router, std::placeholders::_1); + auto fn = [this, router](const auto& res) { HandleDHTLookupResult(router, res); }; // if we are a client try using the hidden service endpoints if (!isServiceNode) @@ -232,7 +231,7 @@ namespace llarp if (!SessionIsAllowed(newrc.pubkey)) return false; - auto func = std::bind(&RCLookupHandler::CheckRC, this, newrc); + auto func = [this, newrc] { CheckRC(newrc); }; _work(func); // update dht if required