2020-01-17 21:54:34 +00:00
|
|
|
#include <chrono>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "rc_lookup_handler.hpp"
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/link/i_link_manager.hpp>
|
|
|
|
#include <llarp/link/server.hpp>
|
|
|
|
#include <llarp/crypto/crypto.hpp>
|
|
|
|
#include <llarp/service/context.hpp>
|
|
|
|
#include <llarp/router_contact.hpp>
|
|
|
|
#include <llarp/util/types.hpp>
|
|
|
|
#include <llarp/util/thread/threading.hpp>
|
|
|
|
#include <llarp/nodedb.hpp>
|
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include "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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::AddValidRouter(const RouterID& router)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::RemoveValidRouter(const RouterID& router)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::SetRouterWhitelist(const std::vector<RouterID>& routers)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (routers.empty())
|
2019-10-14 15:38:34 +00:00
|
|
|
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();
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& router : routers)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
whitelistRouters.emplace(router);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
LogInfo("lokinet service node list now has ", whitelistRouters.size(), " routers");
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::GetRC(const RouterID& router, RCRequestCallback callback, bool forceLookup)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
RouterContact remoteRC;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not forceLookup)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
if (const auto maybe = _nodedb->Get(router); maybe.has_value())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
remoteRC = *maybe;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (callback)
|
2020-01-14 20:12:47 +00:00
|
|
|
{
|
|
|
|
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{});
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (callback)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
itr_pair.first->second.push_back(callback);
|
|
|
|
}
|
|
|
|
shouldDoLookup = itr_pair.second;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (shouldDoLookup)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto fn =
|
|
|
|
std::bind(&RCLookupHandler::HandleDHTLookupResult, this, router, std::placeholders::_1);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
// if we are a client try using the hidden service endpoints
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!isServiceNode)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
bool sent = false;
|
|
|
|
LogInfo("Lookup ", router, " anonymously");
|
|
|
|
_hiddenServiceContext->ForEachService(
|
2020-04-07 18:38:56 +00:00
|
|
|
[&](const std::string&, const std::shared_ptr<service::Endpoint>& ep) -> bool {
|
2019-06-26 21:39:29 +00:00
|
|
|
const bool success = ep->LookupRouterAnon(router, fn);
|
2020-04-07 18:38:56 +00:00
|
|
|
sent = sent || success;
|
2019-06-26 21:39:29 +00:00
|
|
|
return !success;
|
|
|
|
});
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sent)
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
LogWarn("cannot lookup ", router, " anonymously");
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!_dht->impl->LookupRouter(router, fn))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::RemoteIsAllowed(const RouterID& remote) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_strictConnectPubkeys.size() && _strictConnectPubkeys.count(remote) == 0
|
|
|
|
&& !RemoteInBootstrap(remote))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (useWhitelist && whitelistRouters.find(remote) == whitelistRouters.end())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::CheckRC(const RouterContact& rc) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not RemoteIsAllowed(rc.pubkey))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
_dht->impl->DelRCNodeAsync(dht::Key_t{rc.pubkey});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not rc.Verify(_dht->impl->Now()))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc.IsPublicRouter())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogDebug("Adding or updating RC for ", RouterID(rc.pubkey), " to nodedb and dht.");
|
2021-03-02 15:23:38 +00:00
|
|
|
_loop->call([rc, n = _nodedb] { n->PutIfNewer(rc); });
|
2019-06-26 21:39:29 +00:00
|
|
|
_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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::GetRandomWhitelistRouter(RouterID& router) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
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();
|
2020-04-07 18:38:56 +00:00
|
|
|
auto itr = whitelistRouters.begin();
|
|
|
|
if (sz == 0)
|
2019-06-26 21:39:29 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz > 1)
|
2019-06-26 21:39:29 +00:00
|
|
|
std::advance(itr, randint() % sz);
|
|
|
|
router = *itr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::CheckRenegotiateValid(RouterContact newrc, RouterContact oldrc)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-05-14 01:35:15 +00:00
|
|
|
// mismatch of identity ?
|
2020-04-07 18:38:56 +00:00
|
|
|
if (newrc.pubkey != oldrc.pubkey)
|
2019-06-26 21:39:29 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!RemoteIsAllowed(newrc.pubkey))
|
2019-06-26 21:39:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto func = std::bind(&RCLookupHandler::CheckRC, this, newrc);
|
2020-06-11 11:44:02 +00:00
|
|
|
_work(func);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
// update dht if required
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_dht->impl->Nodes()->HasNode(dht::Key_t{newrc.pubkey}))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
_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
|
2021-02-02 14:35:40 +00:00
|
|
|
std::unordered_set<RouterID> routersToLookUp;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
_nodedb->VisitInsertedBefore(
|
2020-04-07 18:38:56 +00:00
|
|
|
[&](const RouterContact& rc) {
|
|
|
|
if (HavePendingLookup(rc.pubkey))
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
routersToLookUp.insert(rc.pubkey);
|
|
|
|
},
|
|
|
|
now - RouterContact::UpdateInterval);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& router : routersToLookUp)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-01-14 20:12:47 +00:00
|
|
|
GetRC(router, nullptr, true);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
_nodedb->RemoveStaleRCs(_bootstrapRouterIDList, now - RouterContact::StaleInsertionAge);
|
2019-06-26 21:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RCLookupHandler::ExploreNetwork()
|
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
const size_t known = _nodedb->NumLoaded();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_bootstrapRCList.empty() && known == 0)
|
2020-01-23 17:49:33 +00:00
|
|
|
{
|
|
|
|
LogError("we have no bootstrap nodes specified");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (known <= _bootstrapRCList.size())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& rc : _bootstrapRCList)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
LogInfo("Doing explore via bootstrap node: ", RouterID(rc.pubkey));
|
|
|
|
_dht->impl->ExploreNetworkVia(dht::Key_t{rc.pubkey});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (useWhitelist)
|
2020-01-14 20:12:47 +00:00
|
|
|
{
|
2020-01-17 21:54:34 +00:00
|
|
|
static constexpr auto RerequestInterval = 10min;
|
2020-04-07 18:38:56 +00:00
|
|
|
static constexpr size_t LookupPerTick = 5;
|
2020-01-16 02:16:18 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<RouterID> lookupRouters;
|
2020-01-16 02:16:18 +00:00
|
|
|
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-04-07 18:38:56 +00:00
|
|
|
for (const auto& r : whitelistRouters)
|
2020-01-14 20:12:47 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (now > _routerLookupTimes[r] + RerequestInterval and not _nodedb->Has(r))
|
2020-01-17 21:54:34 +00:00
|
|
|
{
|
|
|
|
lookupRouters.emplace_back(r);
|
|
|
|
}
|
2020-01-14 20:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 02:16:18 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (lookupRouters.size() > LookupPerTick)
|
2020-01-16 02:16:18 +00:00
|
|
|
{
|
2021-03-18 21:36:53 +00:00
|
|
|
std::shuffle(lookupRouters.begin(), lookupRouters.end(), CSRNG{});
|
2020-01-16 02:16:18 +00:00
|
|
|
lookupRouters.resize(LookupPerTick);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& r : lookupRouters)
|
2020-01-14 20:12:47 +00:00
|
|
|
GetRC(r, nullptr, true);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-30 22:10:56 +00:00
|
|
|
// service nodes gossip, not explore
|
2020-04-07 18:38:56 +00:00
|
|
|
if (_dht->impl->GetRouter()->IsServiceNode())
|
2020-01-30 22:10:56 +00:00
|
|
|
return;
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
// explore via every connected peer
|
2020-04-07 18:38:56 +00:00
|
|
|
_linkManager->ForEachPeer([&](ILinkSession* s) {
|
|
|
|
if (!s->IsEstablished())
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
const RouterContact rc = s->GetRemoteRC();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc.IsPublicRouter() && (_bootstrapRCList.find(rc) == _bootstrapRCList.end()))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::Init(
|
|
|
|
llarp_dht_context* dht,
|
2021-02-02 14:35:40 +00:00
|
|
|
std::shared_ptr<NodeDB> nodedb,
|
2021-03-02 07:02:59 +00:00
|
|
|
EventLoop_ptr loop,
|
2020-06-11 11:44:02 +00:00
|
|
|
WorkerFunc_t dowork,
|
2020-04-07 18:38:56 +00:00
|
|
|
ILinkManager* linkManager,
|
|
|
|
service::Context* hiddenServiceContext,
|
2021-02-02 14:35:40 +00:00
|
|
|
const std::unordered_set<RouterID>& strictConnectPubkeys,
|
2020-04-07 18:38:56 +00:00
|
|
|
const std::set<RouterContact>& bootstrapRCList,
|
|
|
|
bool useWhitelist_arg,
|
|
|
|
bool isServiceNode_arg)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
_dht = dht;
|
2021-03-02 07:02:59 +00:00
|
|
|
_nodedb = std::move(nodedb);
|
|
|
|
_loop = std::move(loop);
|
|
|
|
_work = std::move(dowork);
|
2019-06-26 21:39:29 +00:00
|
|
|
_hiddenServiceContext = hiddenServiceContext;
|
|
|
|
_strictConnectPubkeys = strictConnectPubkeys;
|
2020-04-07 18:38:56 +00:00
|
|
|
_bootstrapRCList = bootstrapRCList;
|
|
|
|
_linkManager = linkManager;
|
|
|
|
useWhitelist = useWhitelist_arg;
|
|
|
|
isServiceNode = isServiceNode_arg;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& rc : _bootstrapRCList)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
_bootstrapRouterIDList.insert(rc.pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::HandleDHTLookupResult(RouterID remote, const std::vector<RouterContact>& results)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not results.size())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
FinalizeRequest(remote, nullptr, RCRequestResult::RouterNotFound);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not RemoteIsAllowed(remote))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
FinalizeRequest(remote, &results[0], RCRequestResult::InvalidRouter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not CheckRC(results[0]))
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::RemoteInBootstrap(const RouterID& remote) const
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& rc : _bootstrapRCList)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc.pubkey == remote)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
RCLookupHandler::FinalizeRequest(
|
|
|
|
const RouterID& router, const RouterContact* const rc, RCRequestResult result)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != pendingCallbacks.end())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
movedCallbacks.splice(movedCallbacks.begin(), itr->second);
|
|
|
|
pendingCallbacks.erase(itr);
|
|
|
|
}
|
|
|
|
} // lock
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& callback : movedCallbacks)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
callback(router, rc, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|