1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/nodedb.cpp

459 lines
9.4 KiB
C++
Raw Normal View History

#include <nodedb.hpp>
2019-01-13 15:00:50 +01:00
#include <crypto/crypto.hpp>
2018-12-12 02:55:30 +01:00
#include <router_contact.hpp>
#include <util/buffer.hpp>
#include <util/encode.hpp>
#include <util/fs.hpp>
#include <util/logger.hpp>
2019-01-11 02:19:36 +01:00
#include <util/logic.hpp>
#include <util/mem.hpp>
#include <fstream>
2018-06-10 16:05:48 +02:00
#include <unordered_map>
2018-05-30 02:40:02 +02:00
2018-06-13 15:09:19 +02:00
static const char skiplist_subdirs[] = "0123456789abcdef";
2018-08-03 01:36:34 +02:00
static const std::string RC_FILE_EXT = ".signed";
2018-05-30 02:40:02 +02:00
bool
llarp_nodedb::Remove(const llarp::RouterID &pk)
{
llarp::util::Lock lock(access);
auto itr = entries.find(pk);
if(itr == entries.end())
return false;
entries.erase(itr);
fs::remove(fs::path(getRCFilePath(pk)));
return true;
}
2018-05-16 20:13:18 +02:00
void
llarp_nodedb::Clear()
{
llarp::util::Lock lock(access);
entries.clear();
}
2018-04-08 14:18:16 +02:00
bool
llarp_nodedb::Get(const llarp::RouterID &pk, llarp::RouterContact &result)
{
llarp::util::Lock lock(access);
auto itr = entries.find(pk);
if(itr == entries.end())
return false;
result = itr->second;
return true;
}
bool
llarp_nodedb::Has(const llarp::RouterID &pk)
{
llarp::util::Lock lock(access);
return entries.find(pk) != entries.end();
}
2018-06-14 19:35:12 +02:00
/// skiplist directory is hex encoded first nibble
/// skiplist filename is <base32encoded>.snode.signed
std::string
llarp_nodedb::getRCFilePath(const llarp::RouterID &pubkey) const
{
char ftmp[68] = {0};
const char *hexname =
llarp::HexEncode< llarp::AlignedBuffer< 32 >, decltype(ftmp) >(pubkey,
ftmp);
std::string hexString(hexname);
std::string skiplistDir;
llarp::RouterID r(pubkey);
std::string fname = r.ToString();
skiplistDir += hexString[0];
fname += RC_FILE_EXT;
fs::path filepath = nodePath / skiplistDir / fname;
return filepath.string();
}
struct async_insert_rc
{
llarp_nodedb *nodedb;
llarp::RouterContact rc;
async_insert_rc(llarp_nodedb *n, const llarp::RouterContact &r)
: nodedb(n), rc(r)
{
}
};
static void
handle_async_insert_rc(void *u)
{
async_insert_rc *job = static_cast< async_insert_rc * >(u);
job->nodedb->Insert(job->rc);
delete job;
}
void
llarp_nodedb::InsertAsync(llarp::RouterContact rc)
{
async_insert_rc *ctx = new async_insert_rc(this, rc);
llarp_threadpool_queue_job(disk, {ctx, &handle_async_insert_rc});
}
/// insert and write to disk
bool
llarp_nodedb::Insert(const llarp::RouterContact &rc)
{
2019-02-03 00:12:42 +01:00
std::array< byte_t, MAX_RC_SIZE > tmp;
llarp_buffer_t buf(tmp);
2018-06-14 19:35:12 +02:00
{
2018-08-30 20:48:43 +02:00
llarp::util::Lock lock(access);
entries.emplace(rc.pubkey.as_array(), rc);
2018-05-30 02:40:02 +02:00
}
if(!rc.BEncode(&buf))
return false;
2018-05-30 02:40:02 +02:00
buf.sz = buf.cur - buf.base;
auto filepath = getRCFilePath(rc.pubkey);
llarp::LogDebug("saving RC.pubkey ", filepath);
std::ofstream ofs(
filepath,
std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
ofs.write((char *)buf.base, buf.sz);
ofs.close();
if(!ofs)
{
llarp::LogError("Failed to write: ", filepath);
return false;
2018-05-30 02:40:02 +02:00
}
llarp::LogDebug("saved RC.pubkey: ", filepath);
return true;
}
2018-05-30 02:40:02 +02:00
ssize_t
llarp_nodedb::Load(const fs::path &path)
{
std::error_code ec;
if(!fs::exists(path, ec))
2018-06-13 14:58:51 +02:00
{
return -1;
2018-06-13 14:58:51 +02:00
}
ssize_t loaded = 0;
2018-06-13 14:58:51 +02:00
for(const char &ch : skiplist_subdirs)
2018-06-13 13:37:44 +02:00
{
if(!ch)
continue;
std::string p;
p += ch;
fs::path sub = path / p;
2018-05-30 02:40:02 +02:00
ssize_t l = loadSubdir(sub);
if(l > 0)
loaded += l;
2018-05-30 02:40:02 +02:00
}
return loaded;
}
2018-05-30 02:40:02 +02:00
ssize_t
llarp_nodedb::loadSubdir(const fs::path &dir)
{
ssize_t sz = 0;
llarp::util::IterDir(dir, [&](const fs::path &f) -> bool {
if(fs::is_regular_file(f) && loadfile(f))
sz++;
return true;
});
return sz;
}
2018-04-30 18:14:20 +02:00
bool
llarp_nodedb::loadfile(const fs::path &fpath)
{
if(fpath.extension() != RC_FILE_EXT)
return false;
llarp::RouterContact rc;
if(!rc.Read(fpath.string().c_str()))
{
llarp::LogError("failed to read file ", fpath);
return false;
2018-04-08 14:18:16 +02:00
}
if(!rc.Verify(crypto, llarp::time_now_ms()))
2018-05-30 02:40:02 +02:00
{
llarp::LogError(fpath, " contains invalid RC");
return false;
2018-05-30 02:40:02 +02:00
}
{
llarp::util::Lock lock(access);
entries.emplace(rc.pubkey.as_array(), rc);
2018-04-08 14:18:16 +02:00
}
return true;
}
2018-04-30 18:14:20 +02:00
void
llarp_nodedb::visit(std::function< bool(const llarp::RouterContact &) > visit)
{
llarp::util::Lock lock(access);
auto itr = entries.begin();
while(itr != entries.end())
{
if(!visit(itr->second))
return;
++itr;
}
}
bool
llarp_nodedb::iterate(llarp_nodedb_iter &i)
{
i.index = 0;
llarp::util::Lock lock(access);
auto itr = entries.begin();
while(itr != entries.end())
{
i.rc = &itr->second;
i.visit(&i);
// advance
i.index++;
itr++;
}
return true;
}
/*
bool
llarp_nodedb::Save()
{
auto itr = entries.begin();
while(itr != entries.end())
{
llarp::pubkey pk = itr->first;
llarp_rc *rc= itr->second;
2018-05-30 02:40:02 +02:00
itr++; // advance
2018-04-08 14:18:16 +02:00
}
return true;
}
*/
2018-04-08 14:18:16 +02:00
// call request hook
2018-06-13 13:37:44 +02:00
void
logic_threadworker_callback(void *user)
{
llarp_async_verify_rc *verify_request =
2018-06-13 13:37:44 +02:00
static_cast< llarp_async_verify_rc * >(user);
2018-12-29 16:44:25 +01:00
if(verify_request->hook)
verify_request->hook(verify_request);
}
// write it to disk
2018-06-13 13:37:44 +02:00
void
disk_threadworker_setRC(void *user)
{
llarp_async_verify_rc *verify_request =
2018-06-13 13:37:44 +02:00
static_cast< llarp_async_verify_rc * >(user);
2018-08-30 20:48:43 +02:00
verify_request->valid = verify_request->nodedb->Insert(verify_request->rc);
2018-08-10 23:34:11 +02:00
if(verify_request->logic)
2018-12-10 15:14:55 +01:00
verify_request->logic->queue_job(
{verify_request, &logic_threadworker_callback});
}
// we run the crypto verify in the crypto threadpool worker
2018-06-13 13:37:44 +02:00
void
crypto_threadworker_verifyrc(void *user)
{
llarp_async_verify_rc *verify_request =
2018-06-13 13:37:44 +02:00
static_cast< llarp_async_verify_rc * >(user);
2018-09-06 13:46:19 +02:00
llarp::RouterContact rc = verify_request->rc;
verify_request->valid =
rc.Verify(verify_request->nodedb->crypto, llarp::time_now_ms());
// if it's valid we need to set it
2018-09-06 13:46:19 +02:00
if(verify_request->valid && rc.IsPublicRouter())
{
llarp::LogDebug("RC is valid, saving to disk");
llarp_threadpool_queue_job(verify_request->diskworker,
2018-06-13 13:37:44 +02:00
{verify_request, &disk_threadworker_setRC});
}
else
{
// callback to logic thread
2018-12-10 15:14:55 +01:00
verify_request->logic->queue_job(
{verify_request, &logic_threadworker_callback});
}
}
2018-06-13 14:58:51 +02:00
void
nodedb_inform_load_rc(void *user)
{
llarp_async_load_rc *job = static_cast< llarp_async_load_rc * >(user);
job->hook(job);
}
void
nodedb_async_load_rc(void *user)
{
llarp_async_load_rc *job = static_cast< llarp_async_load_rc * >(user);
auto fpath = job->nodedb->getRCFilePath(job->pubkey);
job->loaded = job->nodedb->loadfile(fpath);
if(job->loaded)
{
2018-08-30 20:48:43 +02:00
job->nodedb->Get(job->pubkey, job->result);
2018-06-13 14:58:51 +02:00
}
2018-12-10 15:14:55 +01:00
job->logic->queue_job({job, &nodedb_inform_load_rc});
2018-06-13 14:58:51 +02:00
}
2018-08-31 14:46:54 +02:00
bool
llarp_nodedb::ensure_dir(const char *dir)
{
2018-04-30 18:14:20 +02:00
fs::path path(dir);
std::error_code ec;
if(!fs::exists(dir, ec))
fs::create_directory(path, ec);
2018-04-08 14:18:16 +02:00
if(ec)
return false;
2018-04-08 14:18:16 +02:00
if(!fs::is_directory(path))
return false;
2018-04-30 18:14:20 +02:00
for(const char &ch : skiplist_subdirs)
{
// this seems to be a problem on all targets
2018-08-02 22:50:16 +02:00
// perhaps cpp17::fs is just as screwed-up
// attempting to create a folder with no name
if(!ch)
return true;
2018-05-29 14:15:48 +02:00
std::string p;
p += ch;
fs::path sub = path / p;
2018-04-30 18:14:20 +02:00
fs::create_directory(sub, ec);
if(ec)
return false;
2018-04-08 14:18:16 +02:00
}
2018-04-30 18:14:20 +02:00
return true;
}
2018-08-24 19:25:47 +02:00
void
llarp_nodedb::set_dir(const char *dir)
2018-08-24 19:25:47 +02:00
{
nodePath = dir;
2018-08-24 19:25:47 +02:00
}
ssize_t
llarp_nodedb::load_dir(const char *dir)
{
std::error_code ec;
if(!fs::exists(dir, ec))
{
return -1;
}
set_dir(dir);
return Load(dir);
2018-04-30 18:14:20 +02:00
}
2018-05-30 22:56:47 +02:00
int
llarp_nodedb::iterate_all(struct llarp_nodedb_iter i)
{
iterate(i);
return entries.size();
}
2018-06-23 16:55:25 +02:00
/// maybe rename to verify_and_set
2018-05-30 22:56:47 +02:00
void
llarp_nodedb_async_verify(struct llarp_async_verify_rc *job)
2018-05-30 22:56:47 +02:00
{
2018-06-19 19:11:24 +02:00
// switch to crypto threadpool and continue with
// crypto_threadworker_verifyrc
llarp_threadpool_queue_job(job->cryptoworker,
2018-06-13 13:37:44 +02:00
{job, &crypto_threadworker_verifyrc});
2018-05-30 22:56:47 +02:00
}
2018-06-01 16:08:54 +02:00
2018-06-23 16:55:25 +02:00
// disabled for now
/*
2018-06-13 14:58:51 +02:00
void
llarp_nodedb_async_load_rc(struct llarp_async_load_rc *job)
2018-06-01 16:08:54 +02:00
{
2018-06-13 14:58:51 +02:00
// call in the disk io thread so we don't bog down the others
llarp_threadpool_queue_job(job->diskworker, {job, &nodedb_async_load_rc});
}
2018-06-23 16:55:25 +02:00
*/
2018-06-13 14:58:51 +02:00
2018-06-19 19:11:24 +02:00
size_t
llarp_nodedb::num_loaded() const
{
return entries.size();
}
2018-11-14 19:02:27 +01:00
bool
llarp_nodedb::select_random_exit(llarp::RouterContact &result)
2018-11-14 19:02:27 +01:00
{
const auto sz = entries.size();
auto itr = entries.begin();
2018-11-14 19:02:27 +01:00
if(sz < 3)
return false;
auto idx = llarp::randint() % sz;
2018-11-14 19:02:27 +01:00
if(idx)
std::advance(itr, idx - 1);
while(itr != entries.end())
2018-11-14 19:02:27 +01:00
{
if(itr->second.IsExit())
{
result = itr->second;
return true;
}
++itr;
}
// wrap around
itr = entries.begin();
2018-11-14 19:02:27 +01:00
while(idx--)
{
if(itr->second.IsExit())
{
result = itr->second;
return true;
}
++itr;
}
return false;
}
2018-08-31 16:41:04 +02:00
bool
llarp_nodedb::select_random_hop(const llarp::RouterContact &prev,
llarp::RouterContact &result, size_t N)
2018-06-19 19:11:24 +02:00
{
2018-08-02 22:50:16 +02:00
/// checking for "guard" status for N = 0 is done by caller inside of
/// pathbuilder's scope
size_t sz = entries.size();
2018-09-06 22:31:58 +02:00
if(sz < 3)
2018-08-31 16:41:04 +02:00
return false;
2018-12-19 18:48:29 +01:00
size_t tries = 5;
llarp_time_t now = llarp::time_now_ms();
2018-08-30 20:48:43 +02:00
if(N)
2018-06-19 19:11:24 +02:00
{
2018-06-20 14:34:48 +02:00
do
{
auto itr = entries.begin();
std::advance(itr, llarp::randint() % sz);
if(itr == entries.end())
{
--tries;
continue;
}
2018-08-30 20:48:43 +02:00
if(prev.pubkey == itr->second.pubkey)
2018-09-06 22:31:58 +02:00
{
--tries;
continue;
2018-09-06 22:31:58 +02:00
}
2018-12-19 18:48:29 +01:00
if(itr->second.addrs.size() && !itr->second.IsExpired(now))
{
2018-08-30 20:48:43 +02:00
result = itr->second;
2018-08-31 16:41:04 +02:00
return true;
}
2018-09-06 22:31:58 +02:00
} while(tries--);
return false;
2018-06-20 14:34:48 +02:00
}
else
2018-12-19 18:48:29 +01:00
return false;
2018-06-19 19:11:24 +02:00
}