2019-01-10 19:41:51 +00:00
|
|
|
#include <nodedb.hpp>
|
|
|
|
|
2019-01-13 14:00:50 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2020-02-13 22:19:12 +00:00
|
|
|
#include <crypto/types.hpp>
|
2018-12-12 01:55:30 +00:00
|
|
|
#include <router_contact.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/fs.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2020-04-02 20:11:07 +00:00
|
|
|
#include <util/str.hpp>
|
2020-01-22 22:30:24 +00:00
|
|
|
#include <dht/kademlia.hpp>
|
2018-05-31 13:08:06 +00:00
|
|
|
|
2020-02-13 22:19:12 +00:00
|
|
|
#include <algorithm>
|
2018-05-27 16:45:04 +00:00
|
|
|
#include <fstream>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <unordered_map>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2018-06-13 13:09:19 +00:00
|
|
|
static const char skiplist_subdirs[] = "0123456789abcdef";
|
2018-08-02 23:36:34 +00:00
|
|
|
static const std::string RC_FILE_EXT = ".signed";
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
llarp_nodedb::NetDBEntry::NetDBEntry(llarp::RouterContact value)
|
|
|
|
: rc(std::move(value)), inserted(llarp::time_now_ms())
|
Config file improvements (#1397)
* Config file API/comment improvements
API improvements:
=================
Make the config API use position-independent tag parameters (Required,
Default{123}, MultiValue) rather than a sequence of bools with
overloads. For example, instead of:
conf.defineOption<int>("a", "b", false, true, 123, [] { ... });
you now write:
conf.defineOption<int>("a", "b", MultiValue, Default{123}, [] { ... });
The tags are:
- Required
- MultiValue
- Default{value}
plus new abilities (see below):
- Hidden
- RelayOnly
- ClientOnly
- Comment{"line1", "line2", "line3"}
Made option definition more powerful:
=====================================
- `Hidden` allows you to define an option that won't show up in the
generated config file if it isn't set.
- `RelayOnly`/`ClientOnly` sets up an option that is only accepted and
only shows up for relay or client configs. (If neither is specified
the option shows up in both modes).
- `Comment{...}` lets the option comments be specified as part of the
defineOption.
Comment improvements
====================
- Rewrote comments for various options to expand on details.
- Inlined all the comments with the option definitions.
- Several options that were missing comments got comments added.
- Made various options for deprecated and or internal options hidden by
default so that they don't show up in a default config file.
- show the section comment (but not option comments) *after* the
[section] tag instead of before it as it makes more sense that way
(particularly for the [bind] section which has a new long comment to
describe how it works).
Disable profiling by default
============================
We had this weird state where we use and store profiling by default but
never *load* it when starting up. This commit makes us just not use
profiling at all unless explicitly enabled.
Other misc changes:
===================
- change default worker threads to 0 (= num cpus) instead of 1, and fix
it to allow 0.
- Actually apply worker-threads option
- fixed default data-dir value erroneously having quotes around it
- reordered ifname/ifaddr/mapaddr (was previously mapaddr/ifaddr/ifname)
as mapaddr is a sort of specialization of ifaddr and so makes more
sense to come after it (particularly because it now references ifaddr
in its help message).
- removed peer-stats option (since we always require it for relays and
never use it for clients)
- removed router profiles filename option (this doesn't need to be
configurable)
- removed defunct `service-node-seed` option
- Change default logging output file to "" (which means stdout), and
also made "-" work for stdout.
* Router hive compilation fixes
* Comments for SNApp SRV settings in ini file
* Add extra blank line after section comments
* Better deprecated option handling
Allow {client,relay}-only options in {relay,client} configs to be
specified as implicitly deprecated options: they warn, and don't set
anything.
Add an explicit `Deprecated` tag and move deprecated option handling
into definition.cpp.
* Move backwards compat options into section definitions
Keep the "addBackwardsCompatibleConfigOptions" only for options in
sections that no longer exist.
* Fix INI parsing issues & C++17-ify
- don't allow inline comments because it seems they aren't allowed in
ini formats in general, and is going to cause problems if there is a
comment character in a value (e.g. an exit auth string). Additionally
it was breaking on a line such as:
# some comment; see?
because it was treating only `; see?` as the comment and then producing
an error message about the rest of the line being invalid.
- make section parsing stricter: the `[` and `]` have to be at the
beginning at end of the line now (after stripping whitespace).
- Move whitespace stripping to the top since everything in here does it.
- chop off string_view suffix/prefix rather than maintaining position
values
- fix potential infinite loop/segfault when given a line such as `]foo[`
* Make config parsing failure fatal
Load() LogError's and returns false on failure, so we weren't aborting
on config file errors.
* Formatting: allow `{}` for empty functions/structs
Instead of using two lines when empty:
{
}
* Make default dns bind 127.0.0.1 on non-Linux
* Don't show empty section; fix tests
We can conceivably have sections that only make sense for clients or
relays, and so want to completely omit that section if we have no
options for the type of config being generated.
Also fixes missing empty lines between tests.
Co-authored-by: Thomas Winget <tewinget@gmail.com>
2020-10-07 22:22:58 +00:00
|
|
|
{}
|
2019-06-10 12:47:21 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::Remove(const llarp::RouterID& pk)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-04-14 11:25:15 +00:00
|
|
|
bool removed = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
RemoveIf([&](const llarp::RouterContact& rc) -> bool {
|
|
|
|
if (rc.pubkey == pk)
|
2019-04-14 11:25:15 +00:00
|
|
|
{
|
|
|
|
removed = true;
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-10 23:29:58 +00:00
|
|
|
return false;
|
2019-04-14 11:25:15 +00:00
|
|
|
});
|
|
|
|
return removed;
|
2018-12-10 23:29:58 +00:00
|
|
|
}
|
2018-05-16 18:13:18 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb::Clear()
|
|
|
|
{
|
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
|
|
|
llarp::util::Lock lock(access);
|
2018-12-10 23:29:58 +00:00
|
|
|
entries.clear();
|
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::Get(const llarp::RouterID& pk, llarp::RouterContact& result)
|
2018-12-10 23:29:58 +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
|
|
|
llarp::util::Lock l(access);
|
2018-12-10 23:29:58 +00:00
|
|
|
auto itr = entries.find(pk);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr == entries.end())
|
2018-12-10 23:29:58 +00:00
|
|
|
return false;
|
2019-06-10 12:47:21 +00:00
|
|
|
result = itr->second.rc;
|
2018-12-10 23:29:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-13 16:41:53 +00:00
|
|
|
|
2019-03-25 13:52:22 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::RemoveIf(std::function<bool(const llarp::RouterContact& rc)> filter)
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<std::string> files;
|
2019-03-25 13:52:22 +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
|
|
|
llarp::util::Lock l(access);
|
2019-03-25 13:52:22 +00:00
|
|
|
auto itr = entries.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != entries.end())
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (filter(itr->second.rc))
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2019-06-10 12:47:21 +00:00
|
|
|
files.insert(getRCFilePath(itr->second.rc.pubkey));
|
2019-03-25 13:52:22 +00:00
|
|
|
itr = entries.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 14:14:45 +00:00
|
|
|
disk([files = std::move(files)]() {
|
|
|
|
for (const auto& file : files)
|
|
|
|
fs::remove(file);
|
|
|
|
});
|
2019-03-25 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::Has(const llarp::RouterID& pk)
|
2018-12-10 23:29:58 +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
|
|
|
llarp::util::Lock lock(access);
|
2018-12-10 23:29:58 +00:00
|
|
|
return entries.find(pk) != entries.end();
|
|
|
|
}
|
2018-06-14 17:35:12 +00:00
|
|
|
|
2020-01-22 22:30:24 +00:00
|
|
|
llarp::RouterContact
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::FindClosestTo(const llarp::dht::Key_t& location)
|
2020-01-22 22:30:24 +00:00
|
|
|
{
|
|
|
|
llarp::RouterContact rc;
|
|
|
|
const llarp::dht::XorMetric compare(location);
|
2020-04-07 18:38:56 +00:00
|
|
|
visit([&rc, compare](const auto& otherRC) -> bool {
|
|
|
|
if (rc.pubkey.IsZero())
|
2020-01-22 22:30:24 +00:00
|
|
|
{
|
|
|
|
rc = otherRC;
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (compare(
|
|
|
|
llarp::dht::Key_t{otherRC.pubkey.as_array()}, llarp::dht::Key_t{rc.pubkey.as_array()}))
|
2020-01-22 22:30:24 +00:00
|
|
|
rc = otherRC;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<llarp::RouterContact>
|
|
|
|
llarp_nodedb::FindClosestTo(const llarp::dht::Key_t& location, uint32_t numRouters)
|
2020-02-13 22:19:12 +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
|
|
|
llarp::util::Lock lock(access);
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<const llarp::RouterContact*> all;
|
2020-02-13 22:19:12 +00:00
|
|
|
|
|
|
|
all.reserve(entries.size());
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& entry : entries)
|
2020-02-13 22:19:12 +00:00
|
|
|
{
|
2020-02-14 20:15:16 +00:00
|
|
|
all.push_back(&entry.second.rc);
|
2020-02-13 22:19:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 20:15:16 +00:00
|
|
|
auto it_mid = numRouters < all.size() ? all.begin() + numRouters : all.end();
|
2020-04-07 18:38:56 +00:00
|
|
|
std::partial_sort(
|
|
|
|
all.begin(),
|
|
|
|
it_mid,
|
|
|
|
all.end(),
|
|
|
|
[compare = llarp::dht::XorMetric{location}](auto* a, auto* b) { return compare(*a, *b); });
|
2020-02-13 22:19:12 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<llarp::RouterContact> closest;
|
2020-02-14 20:15:16 +00:00
|
|
|
closest.reserve(numRouters);
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto it = all.begin(); it != it_mid; ++it)
|
2020-02-14 20:21:14 +00:00
|
|
|
closest.push_back(**it);
|
2020-02-13 22:19:12 +00:00
|
|
|
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:17:51 +00:00
|
|
|
/// skiplist directory is hex encoded first nibble
|
|
|
|
/// skiplist filename is <base32encoded>.snode.signed
|
2018-12-10 23:29:58 +00:00
|
|
|
std::string
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::getRCFilePath(const llarp::RouterID& pubkey) const
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
2020-06-29 21:46:25 +00:00
|
|
|
std::string hexString = lokimq::to_hex(pubkey.begin(), pubkey.end());
|
2018-12-10 23:29:58 +00:00
|
|
|
std::string skiplistDir;
|
2018-12-28 15:17:51 +00:00
|
|
|
|
|
|
|
llarp::RouterID r(pubkey);
|
|
|
|
std::string fname = r.ToString();
|
|
|
|
|
|
|
|
skiplistDir += hexString[0];
|
|
|
|
fname += RC_FILE_EXT;
|
|
|
|
fs::path filepath = nodePath / skiplistDir / fname;
|
2018-12-10 23:29:58 +00:00
|
|
|
return filepath.string();
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::InsertAsync(
|
|
|
|
llarp::RouterContact rc,
|
|
|
|
std::shared_ptr<llarp::Logic> logic,
|
|
|
|
std::function<void(void)> completionHandler)
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
disk([this, rc, logic, completionHandler]() {
|
2019-09-12 18:19:25 +00:00
|
|
|
this->Insert(rc);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (logic && completionHandler)
|
2019-09-12 18:19:25 +00:00
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
LogicCall(logic, completionHandler);
|
2019-09-12 18:19:25 +00:00
|
|
|
}
|
|
|
|
});
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 14:23:38 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::UpdateAsyncIfNewer(
|
|
|
|
llarp::RouterContact rc,
|
|
|
|
std::shared_ptr<llarp::Logic> logic,
|
|
|
|
std::function<void(void)> completionHandler)
|
2019-06-17 14:23:38 +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
|
|
|
llarp::util::Lock lock(access);
|
2019-06-17 14:23:38 +00:00
|
|
|
auto itr = entries.find(rc.pubkey);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr == entries.end() || itr->second.rc.OtherIsNewer(rc))
|
2019-06-17 14:23:38 +00:00
|
|
|
{
|
|
|
|
InsertAsync(rc, logic, completionHandler);
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != entries.end())
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
// insertion time is set on...insertion. But it should be updated here
|
|
|
|
// even if there is no insertion of a new RC, to show that the existing one
|
|
|
|
// is not "stale"
|
|
|
|
itr->second.inserted = llarp::time_now_ms();
|
|
|
|
}
|
2019-06-17 14:23:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-16 04:44:25 +00:00
|
|
|
/// insert
|
2018-12-10 23:29:58 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::Insert(const llarp::RouterContact& rc)
|
2018-12-10 23:29:58 +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
|
|
|
llarp::util::Lock lock(access);
|
2020-01-16 04:44:25 +00:00
|
|
|
auto itr = entries.find(rc.pubkey.as_array());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != entries.end())
|
2020-01-16 04:44:25 +00:00
|
|
|
entries.erase(itr);
|
|
|
|
entries.emplace(rc.pubkey.as_array(), rc);
|
2020-04-07 18:38:56 +00:00
|
|
|
LogDebug(
|
|
|
|
"Added or updated RC for ",
|
|
|
|
llarp::RouterID(rc.pubkey),
|
|
|
|
" to nodedb. Current nodedb count is: ",
|
|
|
|
entries.size());
|
2018-12-10 23:29:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
ssize_t
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::Load(const fs::path& path)
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
|
|
|
std::error_code ec;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!fs::exists(path, ec))
|
2018-06-13 12:58:51 +00:00
|
|
|
{
|
2018-12-10 23:29:58 +00:00
|
|
|
return -1;
|
2018-06-13 12:58:51 +00:00
|
|
|
}
|
2018-12-10 23:29:58 +00:00
|
|
|
ssize_t loaded = 0;
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const char& ch : skiplist_subdirs)
|
2018-06-13 11:37:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!ch)
|
2018-12-10 23:29:58 +00:00
|
|
|
continue;
|
|
|
|
std::string p;
|
|
|
|
p += ch;
|
|
|
|
fs::path sub = path / p;
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
ssize_t l = loadSubdir(sub);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (l > 0)
|
2018-12-10 23:29:58 +00:00
|
|
|
loaded += l;
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
2020-01-14 17:01:41 +00:00
|
|
|
m_NextSaveToDisk = llarp::time_now_ms() + m_SaveInterval;
|
2018-12-10 23:29:58 +00:00
|
|
|
return loaded;
|
|
|
|
}
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2019-06-17 14:23:38 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb::SaveAll()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::array<byte_t, MAX_RC_SIZE> tmp;
|
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
|
|
|
llarp::util::Lock lock(access);
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& item : entries)
|
2019-06-17 14:23:38 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t buf(tmp);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!item.second.rc.BEncode(&buf))
|
2019-06-17 14:23:38 +00:00
|
|
|
continue;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2019-06-17 14:23:38 +00:00
|
|
|
const auto filepath = getRCFilePath(item.second.rc.pubkey);
|
2020-04-07 18:38:56 +00:00
|
|
|
auto optional_ofs = llarp::util::OpenFileStream<std::ofstream>(
|
|
|
|
filepath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
|
|
|
if (!optional_ofs)
|
2019-06-24 16:39:03 +00:00
|
|
|
continue;
|
2020-05-20 19:46:08 +00:00
|
|
|
auto& ofs = *optional_ofs;
|
2020-04-07 18:38:56 +00:00
|
|
|
ofs.write((char*)buf.base, buf.sz);
|
2019-06-17 14:23:38 +00:00
|
|
|
ofs.flush();
|
|
|
|
ofs.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:01:41 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb::ShouldSaveToDisk(llarp_time_t now) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (now == 0s)
|
2020-01-14 17:01:41 +00:00
|
|
|
now = llarp::time_now_ms();
|
2020-02-24 19:40:45 +00:00
|
|
|
return m_NextSaveToDisk > 0s && m_NextSaveToDisk <= now;
|
2020-01-14 17:01:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 14:23:38 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb::AsyncFlushToDisk()
|
|
|
|
{
|
2020-07-01 14:14:45 +00:00
|
|
|
disk([this]() { SaveAll(); });
|
2020-01-14 17:01:41 +00:00
|
|
|
m_NextSaveToDisk = llarp::time_now_ms() + m_SaveInterval;
|
2019-06-17 14:23:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
ssize_t
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::loadSubdir(const fs::path& dir)
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
|
|
|
ssize_t sz = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::util::IterDir(dir, [&](const fs::path& f) -> bool {
|
|
|
|
if (fs::is_regular_file(f) && loadfile(f))
|
2018-12-10 23:29:58 +00:00
|
|
|
sz++;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return sz;
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::loadfile(const fs::path& fpath)
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fpath.extension() != RC_FILE_EXT)
|
2018-12-10 23:29:58 +00:00
|
|
|
return false;
|
|
|
|
llarp::RouterContact rc;
|
2020-05-27 03:36:46 +00:00
|
|
|
if (!rc.Read(fpath))
|
2018-12-10 23:29:58 +00:00
|
|
|
{
|
2020-05-27 03:42:01 +00:00
|
|
|
llarp::LogError("failed to read file ", fpath);
|
2018-12-10 23:29:58 +00:00
|
|
|
return false;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!rc.Verify(llarp::time_now_ms()))
|
2018-05-30 00:40:02 +00:00
|
|
|
{
|
2020-05-27 03:42:01 +00:00
|
|
|
llarp::LogError(fpath, " contains invalid RC");
|
2018-12-10 23:29:58 +00:00
|
|
|
return false;
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +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
|
|
|
llarp::util::Lock lock(access);
|
2019-01-02 01:03:53 +00:00
|
|
|
entries.emplace(rc.pubkey.as_array(), rc);
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-12-10 23:29:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-12-10 23:29:58 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::visit(std::function<bool(const llarp::RouterContact&)> visit)
|
2018-12-10 23:29:58 +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
|
|
|
llarp::util::Lock lock(access);
|
2018-12-10 23:29:58 +00:00
|
|
|
auto itr = entries.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != entries.end())
|
2018-09-11 15:53:54 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!visit(itr->second.rc))
|
2018-12-10 23:29:58 +00:00
|
|
|
return;
|
|
|
|
++itr;
|
2018-09-11 15:53:54 +00:00
|
|
|
}
|
2018-12-10 23:29:58 +00:00
|
|
|
}
|
2018-09-11 15:53:54 +00:00
|
|
|
|
2019-06-10 12:47:21 +00:00
|
|
|
void
|
2019-06-20 14:00:04 +00:00
|
|
|
llarp_nodedb::VisitInsertedBefore(
|
2020-04-07 18:38:56 +00:00
|
|
|
std::function<void(const llarp::RouterContact&)> visit, llarp_time_t insertedAfter)
|
2018-12-10 23:29:58 +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
|
|
|
llarp::util::Lock lock(access);
|
2018-12-10 23:29:58 +00:00
|
|
|
auto itr = entries.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != entries.end())
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.inserted < insertedAfter)
|
2019-06-10 12:47:21 +00:00
|
|
|
visit(itr->second.rc);
|
|
|
|
++itr;
|
2018-06-21 09:33:23 +00:00
|
|
|
}
|
2018-12-10 23:29:58 +00:00
|
|
|
}
|
2018-06-21 09:33:23 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::RemoveStaleRCs(const std::set<llarp::RouterID>& keep, llarp_time_t cutoff)
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::set<llarp::RouterID> removeStale;
|
2019-06-26 21:39:29 +00:00
|
|
|
// remove stale routers
|
|
|
|
VisitInsertedBefore(
|
2020-04-07 18:38:56 +00:00
|
|
|
[&](const llarp::RouterContact& rc) {
|
|
|
|
if (keep.find(rc.pubkey) != keep.end())
|
2019-06-26 21:39:29 +00:00
|
|
|
return;
|
|
|
|
LogInfo("removing stale router: ", llarp::RouterID(rc.pubkey));
|
|
|
|
removeStale.insert(rc.pubkey);
|
|
|
|
},
|
|
|
|
cutoff);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
RemoveIf([&removeStale](const llarp::RouterContact& rc) -> bool {
|
2019-06-26 21:39:29 +00:00
|
|
|
return removeStale.count(rc.pubkey) > 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-07 09:36:30 +00:00
|
|
|
// write it to disk
|
2018-06-13 11:37:44 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
disk_threadworker_setRC(llarp_async_verify_rc* verify_request)
|
2018-06-13 11:37:44 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
verify_request->valid = verify_request->nodedb->Insert(verify_request->rc);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (verify_request->logic)
|
2020-06-11 11:44:02 +00:00
|
|
|
{
|
|
|
|
LogicCall(verify_request->logic, [verify_request]() {
|
|
|
|
if (verify_request->hook)
|
|
|
|
verify_request->hook(verify_request);
|
|
|
|
});
|
|
|
|
}
|
2018-06-07 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we run the crypto verify in the crypto threadpool worker
|
2018-06-13 11:37:44 +00:00
|
|
|
void
|
2020-06-11 11:44:02 +00:00
|
|
|
crypto_threadworker_verifyrc(llarp_async_verify_rc* verify_request)
|
2018-06-07 09:36:30 +00:00
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
llarp::RouterContact rc = verify_request->rc;
|
2020-04-07 18:38:56 +00:00
|
|
|
verify_request->valid = rc.Verify(llarp::time_now_ms());
|
2018-06-07 09:36:30 +00:00
|
|
|
// if it's valid we need to set it
|
2020-04-07 18:38:56 +00:00
|
|
|
if (verify_request->valid && rc.IsPublicRouter())
|
2018-06-07 09:36:30 +00:00
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
if (verify_request->disk)
|
2020-01-12 12:29:36 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("RC is valid, saving to disk");
|
2020-06-11 11:44:02 +00:00
|
|
|
verify_request->disk(std::bind(&disk_threadworker_setRC, verify_request));
|
2020-01-12 12:29:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-07 09:36:30 +00:00
|
|
|
}
|
2020-01-12 12:29:36 +00:00
|
|
|
// callback to logic thread
|
2020-06-11 11:44:02 +00:00
|
|
|
LogicCall(verify_request->logic, [verify_request]() {
|
|
|
|
if (verify_request->hook)
|
|
|
|
verify_request->hook(verify_request);
|
|
|
|
});
|
2018-06-13 12:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 12:29:36 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb_async_verify(struct llarp_async_verify_rc* job)
|
2020-01-12 12:29:36 +00:00
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
job->worker(std::bind(&crypto_threadworker_verifyrc, job));
|
2018-06-13 12:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 20:11:07 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb::ensure_dir(const fs::path& nodedbDir)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2020-04-02 20:11:07 +00:00
|
|
|
if (not fs::exists(nodedbDir))
|
2020-04-02 17:56:13 +00:00
|
|
|
{
|
2020-04-02 20:11:07 +00:00
|
|
|
// if the old 'netdb' directory exists, move it to this one
|
|
|
|
fs::path parent = nodedbDir.parent_path();
|
|
|
|
fs::path old = parent / "netdb";
|
|
|
|
if (fs::exists(old))
|
|
|
|
fs::rename(old, nodedbDir);
|
|
|
|
else
|
|
|
|
fs::create_directory(nodedbDir);
|
2020-04-02 17:56:13 +00:00
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2020-04-02 20:11:07 +00:00
|
|
|
if (not fs::is_directory(nodedbDir))
|
|
|
|
throw std::runtime_error(llarp::stringify("nodedb ", nodedbDir, " is not a directory"));
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const char& ch : skiplist_subdirs)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-07-30 04:38:14 +00:00
|
|
|
// this seems to be a problem on all targets
|
2018-08-02 20:50:16 +00:00
|
|
|
// perhaps cpp17::fs is just as screwed-up
|
2018-07-30 04:38:14 +00:00
|
|
|
// attempting to create a folder with no name
|
2020-04-02 20:11:07 +00:00
|
|
|
// what does this mean...?
|
2020-04-07 20:41:11 +00:00
|
|
|
if (!ch)
|
2020-04-02 20:11:07 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
fs::path sub = nodedbDir / std::string(&ch, 1);
|
|
|
|
fs::create_directory(sub);
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
ssize_t
|
2020-01-14 20:12:47 +00:00
|
|
|
llarp_nodedb::LoadAll()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2020-01-14 20:12:47 +00:00
|
|
|
return Load(nodePath.c_str());
|
2018-04-30 16:14:20 +00:00
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
size_t
|
2018-12-10 23:29:58 +00:00
|
|
|
llarp_nodedb::num_loaded() const
|
2018-09-13 16:41:53 +00:00
|
|
|
{
|
2020-05-12 19:42:35 +00:00
|
|
|
std::shared_lock l{access};
|
2018-12-10 23:29:58 +00:00
|
|
|
return entries.size();
|
2018-09-13 16:41:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_nodedb::select_random_exit(llarp::RouterContact& result)
|
2018-11-14 18:02:27 +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
|
|
|
llarp::util::Lock lock(access);
|
2018-12-10 23:29:58 +00:00
|
|
|
const auto sz = entries.size();
|
2020-04-07 18:38:56 +00:00
|
|
|
auto itr = entries.begin();
|
|
|
|
if (sz < 3)
|
2018-11-14 18:02:27 +00:00
|
|
|
return false;
|
2018-12-11 00:53:11 +00:00
|
|
|
auto idx = llarp::randint() % sz;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (idx)
|
2018-11-14 18:02:27 +00:00
|
|
|
std::advance(itr, idx - 1);
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != entries.end())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.rc.IsExit())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2019-06-10 12:47:21 +00:00
|
|
|
result = itr->second.rc;
|
2018-11-14 18:02:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
2018-12-10 23:29:58 +00:00
|
|
|
// wrap around
|
|
|
|
itr = entries.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (idx--)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.rc.IsExit())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2019-06-10 12:47:21 +00:00
|
|
|
result = itr->second.rc;
|
2018-11-14 18:02:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:58:31 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb::select_random_hop_excluding(
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::RouterContact& result, const std::set<llarp::RouterID>& exclude)
|
2019-03-11 13:58:31 +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
|
|
|
llarp::util::Lock lock(access);
|
2019-03-11 13:58:31 +00:00
|
|
|
/// checking for "guard" status for N = 0 is done by caller inside of
|
|
|
|
/// pathbuilder's scope
|
2019-03-25 13:52:22 +00:00
|
|
|
const size_t sz = entries.size();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz < 3)
|
2019-03-25 13:52:22 +00:00
|
|
|
{
|
2019-03-11 13:58:31 +00:00
|
|
|
return false;
|
2019-03-25 13:52:22 +00:00
|
|
|
}
|
2019-03-11 13:58:31 +00:00
|
|
|
|
2020-03-10 16:19:24 +00:00
|
|
|
const size_t pos = llarp::randint() % sz;
|
2020-03-11 20:45:48 +00:00
|
|
|
const auto start = std::next(entries.begin(), pos);
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto itr = start; itr != entries.end(); ++itr)
|
2020-03-11 20:45:48 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (exclude.count(itr->first) == 0 and itr->second.rc.IsPublicRouter())
|
2020-03-11 20:45:48 +00:00
|
|
|
{
|
|
|
|
result = itr->second.rc;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto itr = entries.begin(); itr != start; ++itr)
|
2019-03-11 13:58:31 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (exclude.count(itr->first) == 0 and itr->second.rc.IsPublicRouter())
|
2019-03-11 13:58:31 +00:00
|
|
|
{
|
2020-03-11 20:25:19 +00:00
|
|
|
result = itr->second.rc;
|
|
|
|
return true;
|
2019-03-11 13:58:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|