2020-01-17 21:54:34 +00:00
|
|
|
#include <chrono>
|
2019-06-26 21:39:29 +00:00
|
|
|
#include <router/rc_lookup_handler.hpp>
|
|
|
|
|
|
|
|
#include <link/i_link_manager.hpp>
|
|
|
|
#include <link/server.hpp>
|
|
|
|
#include <crypto/crypto.hpp>
|
|
|
|
#include <service/context.hpp>
|
|
|
|
#include <router_contact.hpp>
|
|
|
|
#include <util/types.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/threading.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
#include <nodedb.hpp>
|
|
|
|
#include <dht/context.hpp>
|
2020-01-14 20:12:47 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <functional>
|
2020-01-16 02:16:18 +00:00
|
|
|
#include <random>
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
void
|
|
|
|
RCLookupHandler::AddValidRouter(const RouterID &router)
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
whitelistRouters.insert(router);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::RemoveValidRouter(const RouterID &router)
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
whitelistRouters.erase(router);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::SetRouterWhitelist(const std::vector< RouterID > &routers)
|
|
|
|
{
|
2019-10-14 15:38:34 +00:00
|
|
|
if(routers.empty())
|
|
|
|
return;
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
whitelistRouters.clear();
|
|
|
|
for(auto &router : routers)
|
|
|
|
{
|
|
|
|
whitelistRouters.emplace(router);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogInfo("lokinet service node list now has ", whitelistRouters.size(),
|
|
|
|
" routers");
|
|
|
|
}
|
|
|
|
|
2020-01-16 04:12:38 +00:00
|
|
|
bool
|
|
|
|
RCLookupHandler::HaveReceivedWhitelist()
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2020-01-16 21:32:52 +00:00
|
|
|
return not whitelistRouters.empty();
|
2020-01-16 04:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2020-01-14 20:12:47 +00:00
|
|
|
RCLookupHandler::GetRC(const RouterID &router, RCRequestCallback callback,
|
|
|
|
bool forceLookup)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
RouterContact remoteRC;
|
2020-01-14 20:12:47 +00:00
|
|
|
if(not forceLookup)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-01-14 20:12:47 +00:00
|
|
|
if(_nodedb->Get(router, remoteRC))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-01-14 20:12:47 +00:00
|
|
|
if(callback)
|
|
|
|
{
|
|
|
|
callback(router, &remoteRC, RCRequestResult::Success);
|
|
|
|
}
|
|
|
|
FinalizeRequest(router, &remoteRC, RCRequestResult::Success);
|
|
|
|
return;
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool shouldDoLookup = false;
|
|
|
|
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
auto itr_pair = pendingCallbacks.emplace(router, CallbacksQueue{});
|
|
|
|
|
|
|
|
if(callback)
|
|
|
|
{
|
|
|
|
itr_pair.first->second.push_back(callback);
|
|
|
|
}
|
|
|
|
shouldDoLookup = itr_pair.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(shouldDoLookup)
|
|
|
|
{
|
|
|
|
auto fn = std::bind(&RCLookupHandler::HandleDHTLookupResult, this, router,
|
|
|
|
std::placeholders::_1);
|
|
|
|
|
|
|
|
// if we are a client try using the hidden service endpoints
|
|
|
|
if(!isServiceNode)
|
|
|
|
{
|
|
|
|
bool sent = false;
|
|
|
|
LogInfo("Lookup ", router, " anonymously");
|
|
|
|
_hiddenServiceContext->ForEachService(
|
|
|
|
[&](const std::string &,
|
|
|
|
const std::shared_ptr< service::Endpoint > &ep) -> bool {
|
|
|
|
const bool success = ep->LookupRouterAnon(router, fn);
|
|
|
|
sent = sent || success;
|
|
|
|
return !success;
|
|
|
|
});
|
|
|
|
if(sent)
|
|
|
|
return;
|
|
|
|
LogWarn("cannot lookup ", router, " anonymously");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_dht->impl->LookupRouter(router, fn))
|
|
|
|
{
|
|
|
|
FinalizeRequest(router, nullptr, RCRequestResult::RouterNotFound);
|
|
|
|
}
|
2020-01-17 21:54:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_routerLookupTimes[router] = std::chrono::steady_clock::now();
|
|
|
|
}
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RCLookupHandler::RemoteIsAllowed(const RouterID &remote) const
|
|
|
|
{
|
|
|
|
if(_strictConnectPubkeys.size() && _strictConnectPubkeys.count(remote) == 0
|
|
|
|
&& !RemoteInBootstrap(remote))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
if(useWhitelist && whitelistRouters.find(remote) == whitelistRouters.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RCLookupHandler::CheckRC(const RouterContact &rc) const
|
|
|
|
{
|
|
|
|
if(not RemoteIsAllowed(rc.pubkey))
|
|
|
|
{
|
|
|
|
_dht->impl->DelRCNodeAsync(dht::Key_t{rc.pubkey});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not rc.Verify(_dht->impl->Now()))
|
|
|
|
{
|
2019-08-28 11:02:00 +00:00
|
|
|
LogWarn("RC for ", RouterID(rc.pubkey), " is invalid");
|
2019-06-26 21:39:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update nodedb if required
|
|
|
|
if(rc.IsPublicRouter())
|
|
|
|
{
|
2019-07-28 13:00:12 +00:00
|
|
|
LogDebug("Adding or updating RC for ", RouterID(rc.pubkey),
|
2019-07-28 15:26:38 +00:00
|
|
|
" to nodedb and dht.");
|
2019-06-26 21:39:29 +00:00
|
|
|
_nodedb->UpdateAsyncIfNewer(rc);
|
|
|
|
_dht->impl->PutRCNodeAsync(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:56:56 +00:00
|
|
|
size_t
|
|
|
|
RCLookupHandler::NumberOfStrictConnectRouters() const
|
|
|
|
{
|
|
|
|
return _strictConnectPubkeys.size();
|
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
|
|
|
RCLookupHandler::GetRandomWhitelistRouter(RouterID &router) const
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
const auto sz = whitelistRouters.size();
|
|
|
|
auto itr = whitelistRouters.begin();
|
|
|
|
if(sz == 0)
|
|
|
|
return false;
|
|
|
|
if(sz > 1)
|
|
|
|
std::advance(itr, randint() % sz);
|
|
|
|
router = *itr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RCLookupHandler::CheckRenegotiateValid(RouterContact newrc,
|
|
|
|
RouterContact oldrc)
|
|
|
|
{
|
|
|
|
// missmatch of identity ?
|
|
|
|
if(newrc.pubkey != oldrc.pubkey)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!RemoteIsAllowed(newrc.pubkey))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto func = std::bind(&RCLookupHandler::CheckRC, this, newrc);
|
|
|
|
_threadpool->addJob(func);
|
|
|
|
|
|
|
|
// update dht if required
|
|
|
|
if(_dht->impl->Nodes()->HasNode(dht::Key_t{newrc.pubkey}))
|
|
|
|
{
|
|
|
|
_dht->impl->Nodes()->PutNode(newrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check for other places that need updating the RC
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::PeriodicUpdate(llarp_time_t now)
|
|
|
|
{
|
|
|
|
// try looking up stale routers
|
|
|
|
std::set< RouterID > routersToLookUp;
|
|
|
|
|
|
|
|
_nodedb->VisitInsertedBefore(
|
|
|
|
[&](const RouterContact &rc) {
|
|
|
|
if(HavePendingLookup(rc.pubkey))
|
|
|
|
return;
|
|
|
|
routersToLookUp.insert(rc.pubkey);
|
|
|
|
},
|
|
|
|
now - RouterContact::UpdateInterval);
|
|
|
|
|
|
|
|
for(const auto &router : routersToLookUp)
|
|
|
|
{
|
2020-01-14 20:12:47 +00:00
|
|
|
GetRC(router, nullptr, true);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_nodedb->RemoveStaleRCs(_bootstrapRouterIDList,
|
|
|
|
now - RouterContact::StaleInsertionAge);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::ExploreNetwork()
|
|
|
|
{
|
2020-01-23 17:49:33 +00:00
|
|
|
const size_t known = _nodedb->num_loaded();
|
|
|
|
if(_bootstrapRCList.empty() && known == 0)
|
|
|
|
{
|
|
|
|
LogError("we have no bootstrap nodes specified");
|
|
|
|
}
|
|
|
|
else if(known <= _bootstrapRCList.size())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
for(const auto &rc : _bootstrapRCList)
|
|
|
|
{
|
|
|
|
LogInfo("Doing explore via bootstrap node: ", RouterID(rc.pubkey));
|
|
|
|
_dht->impl->ExploreNetworkVia(dht::Key_t{rc.pubkey});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:12:47 +00:00
|
|
|
if(useWhitelist)
|
|
|
|
{
|
2020-01-17 21:54:34 +00:00
|
|
|
static constexpr auto RerequestInterval = 10min;
|
2020-01-18 21:53:42 +00:00
|
|
|
static constexpr size_t LookupPerTick = 5;
|
2020-01-16 02:16:18 +00:00
|
|
|
|
|
|
|
std::vector< RouterID > lookupRouters;
|
|
|
|
lookupRouters.reserve(LookupPerTick);
|
|
|
|
|
2020-01-17 21:54:34 +00:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
|
2020-01-14 20:12:47 +00:00
|
|
|
{
|
|
|
|
// if we are using a whitelist look up a few routers we don't have
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2020-01-14 20:12:47 +00:00
|
|
|
for(const auto &r : whitelistRouters)
|
|
|
|
{
|
2020-01-17 21:54:34 +00:00
|
|
|
if(now > _routerLookupTimes[r] + RerequestInterval
|
|
|
|
and not _nodedb->Has(r))
|
|
|
|
{
|
|
|
|
lookupRouters.emplace_back(r);
|
|
|
|
}
|
2020-01-14 20:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 02:16:18 +00:00
|
|
|
|
2020-01-16 04:15:37 +00:00
|
|
|
if(lookupRouters.size() > LookupPerTick)
|
2020-01-16 02:16:18 +00:00
|
|
|
{
|
|
|
|
static std::mt19937_64 rng{std::random_device{}()};
|
|
|
|
std::shuffle(lookupRouters.begin(), lookupRouters.end(), rng);
|
|
|
|
lookupRouters.resize(LookupPerTick);
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:12:47 +00:00
|
|
|
for(const auto &r : lookupRouters)
|
|
|
|
GetRC(r, nullptr, true);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-30 22:10:56 +00:00
|
|
|
// service nodes gossip, not explore
|
|
|
|
if(_dht->impl->GetRouter()->IsServiceNode())
|
|
|
|
return;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
// explore via every connected peer
|
|
|
|
_linkManager->ForEachPeer([&](ILinkSession *s) {
|
|
|
|
if(!s->IsEstablished())
|
|
|
|
return;
|
|
|
|
const RouterContact rc = s->GetRemoteRC();
|
|
|
|
if(rc.IsPublicRouter()
|
|
|
|
&& (_bootstrapRCList.find(rc) == _bootstrapRCList.end()))
|
|
|
|
{
|
2020-02-06 18:57:39 +00:00
|
|
|
LogDebug("Doing explore via public node: ", RouterID(rc.pubkey));
|
2019-06-26 21:39:29 +00:00
|
|
|
_dht->impl->ExploreNetworkVia(dht::Key_t{rc.pubkey});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::Init(llarp_dht_context *dht, llarp_nodedb *nodedb,
|
|
|
|
std::shared_ptr< llarp::thread::ThreadPool > threadpool,
|
|
|
|
ILinkManager *linkManager,
|
|
|
|
service::Context *hiddenServiceContext,
|
|
|
|
const std::set< RouterID > &strictConnectPubkeys,
|
|
|
|
const std::set< RouterContact > &bootstrapRCList,
|
|
|
|
bool useWhitelist_arg, bool isServiceNode_arg)
|
|
|
|
{
|
|
|
|
_dht = dht;
|
|
|
|
_nodedb = nodedb;
|
|
|
|
_threadpool = threadpool;
|
|
|
|
_hiddenServiceContext = hiddenServiceContext;
|
|
|
|
_strictConnectPubkeys = strictConnectPubkeys;
|
|
|
|
_bootstrapRCList = bootstrapRCList;
|
|
|
|
_linkManager = linkManager;
|
|
|
|
useWhitelist = useWhitelist_arg;
|
|
|
|
isServiceNode = isServiceNode_arg;
|
|
|
|
|
|
|
|
for(const auto &rc : _bootstrapRCList)
|
|
|
|
{
|
|
|
|
_bootstrapRouterIDList.insert(rc.pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::HandleDHTLookupResult(
|
|
|
|
RouterID remote, const std::vector< RouterContact > &results)
|
|
|
|
{
|
|
|
|
if(not results.size())
|
|
|
|
{
|
|
|
|
FinalizeRequest(remote, nullptr, RCRequestResult::RouterNotFound);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not RemoteIsAllowed(remote))
|
|
|
|
{
|
|
|
|
FinalizeRequest(remote, &results[0], RCRequestResult::InvalidRouter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not CheckRC(results[0]))
|
|
|
|
{
|
|
|
|
FinalizeRequest(remote, &results[0], RCRequestResult::BadRC);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FinalizeRequest(remote, &results[0], RCRequestResult::Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RCLookupHandler::HavePendingLookup(RouterID remote) const
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
return pendingCallbacks.find(remote) != pendingCallbacks.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RCLookupHandler::RemoteInBootstrap(const RouterID &remote) const
|
|
|
|
{
|
|
|
|
for(const auto &rc : _bootstrapRCList)
|
|
|
|
{
|
|
|
|
if(rc.pubkey == remote)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::FinalizeRequest(const RouterID &router,
|
|
|
|
const RouterContact *const rc,
|
|
|
|
RCRequestResult result)
|
|
|
|
{
|
|
|
|
CallbacksQueue movedCallbacks;
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock l(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
auto itr = pendingCallbacks.find(router);
|
|
|
|
|
|
|
|
if(itr != pendingCallbacks.end())
|
|
|
|
{
|
|
|
|
movedCallbacks.splice(movedCallbacks.begin(), itr->second);
|
|
|
|
pendingCallbacks.erase(itr);
|
|
|
|
}
|
|
|
|
} // lock
|
|
|
|
|
|
|
|
for(const auto &callback : movedCallbacks)
|
|
|
|
{
|
|
|
|
callback(router, rc, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|