2018-10-03 11:02:56 +00:00
|
|
|
#include <llarp/logic.h>
|
2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/nodedb.hpp>
|
|
|
|
#include <llarp/router_contact.hpp>
|
2018-05-31 13:08:06 +00:00
|
|
|
|
2018-05-27 16:45:04 +00:00
|
|
|
#include <fstream>
|
2018-06-12 11:57:14 +00:00
|
|
|
#include <llarp/crypto.hpp>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <unordered_map>
|
2018-05-27 16:45:04 +00:00
|
|
|
#include "buffer.hpp"
|
2018-05-31 13:08:06 +00:00
|
|
|
#include "encode.hpp"
|
2018-06-13 11:37:44 +00:00
|
|
|
#include "fs.hpp"
|
2018-05-30 00:40:02 +00:00
|
|
|
#include "logger.hpp"
|
2018-06-13 11:37:44 +00:00
|
|
|
#include "mem.hpp"
|
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
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_nodedb
|
|
|
|
{
|
2018-05-27 16:45:04 +00:00
|
|
|
llarp_nodedb(llarp_crypto *c) : crypto(c)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
}
|
2018-05-16 18:13:18 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_crypto *crypto;
|
2018-06-14 17:35:12 +00:00
|
|
|
// std::map< llarp::pubkey, llarp_rc > entries;
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::util::Mutex access;
|
|
|
|
std::unordered_map< llarp::PubKey, llarp::RouterContact, llarp::PubKey::Hash >
|
|
|
|
entries;
|
2018-06-13 11:41:23 +00:00
|
|
|
fs::path nodePath;
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
bool
|
|
|
|
Remove(const llarp::PubKey &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-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
Clear()
|
2018-05-16 18:13:18 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::util::Lock lock(access);
|
|
|
|
entries.clear();
|
2018-06-14 17:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-08-30 18:48:43 +00:00
|
|
|
Get(const llarp::PubKey &pk, llarp::RouterContact &result)
|
2018-06-14 17:35:12 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::util::Lock lock(access);
|
|
|
|
auto itr = entries.find(pk);
|
|
|
|
if(itr == entries.end())
|
|
|
|
return false;
|
|
|
|
result = itr->second;
|
|
|
|
return true;
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 11:37:44 +00:00
|
|
|
bool
|
2018-08-30 18:48:43 +00:00
|
|
|
Has(const llarp::PubKey &pk)
|
2018-05-31 13:08:06 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::util::Lock lock(access);
|
2018-05-30 00:40:02 +00:00
|
|
|
return entries.find(pk) != entries.end();
|
|
|
|
}
|
|
|
|
|
2018-06-13 12:58:51 +00:00
|
|
|
std::string
|
2018-08-02 23:30:34 +00:00
|
|
|
getRCFilePath(const byte_t *pubkey) const
|
2018-06-13 12:58:51 +00:00
|
|
|
{
|
|
|
|
char ftmp[68] = {0};
|
|
|
|
const char *hexname =
|
|
|
|
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(pubkey, ftmp);
|
|
|
|
std::string hexString(hexname);
|
2018-08-02 23:30:34 +00:00
|
|
|
std::string skiplistDir;
|
|
|
|
skiplistDir += hexString[hexString.length() - 1];
|
2018-08-24 17:25:47 +00:00
|
|
|
hexString += RC_FILE_EXT;
|
2018-08-02 23:30:34 +00:00
|
|
|
fs::path filepath = nodePath / skiplistDir / hexString;
|
2018-08-02 23:37:54 +00:00
|
|
|
return filepath.string();
|
2018-06-13 12:58:51 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/// insert and write to disk
|
2018-06-13 11:37:44 +00:00
|
|
|
bool
|
2018-08-30 18:48:43 +00:00
|
|
|
Insert(const llarp::RouterContact &rc)
|
2018-06-13 11:37:44 +00:00
|
|
|
{
|
2018-05-30 00:40:02 +00:00
|
|
|
byte_t tmp[MAX_RC_SIZE];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
llarp::util::Lock lock(access);
|
|
|
|
entries.insert(std::make_pair(rc.pubkey, rc));
|
|
|
|
}
|
|
|
|
if(!rc.BEncode(&buf))
|
|
|
|
return false;
|
2018-05-30 00:40:02 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
auto filepath = getRCFilePath(rc.pubkey);
|
|
|
|
llarp::LogDebug("saving RC.pubkey ", filepath);
|
|
|
|
std::ofstream ofs(
|
|
|
|
filepath,
|
2018-10-01 02:08:03 +00:00
|
|
|
std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
2018-08-30 18:48:43 +00:00
|
|
|
ofs.write((char *)buf.base, buf.sz);
|
|
|
|
ofs.close();
|
|
|
|
if(!ofs)
|
2018-05-31 13:08:06 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::LogError("Failed to write: ", filepath);
|
|
|
|
return false;
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::LogDebug("saved RC.pubkey: ", filepath);
|
|
|
|
return true;
|
2018-05-30 00:40:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
ssize_t
|
|
|
|
Load(const fs::path &path)
|
|
|
|
{
|
2018-04-08 12:18:16 +00:00
|
|
|
std::error_code ec;
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!fs::exists(path, ec))
|
|
|
|
{
|
2018-04-08 12:18:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ssize_t loaded = 0;
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
for(const char &ch : skiplist_subdirs)
|
|
|
|
{
|
2018-08-02 23:30:34 +00:00
|
|
|
if(!ch)
|
|
|
|
continue;
|
2018-05-29 12:15:48 +00:00
|
|
|
std::string p;
|
|
|
|
p += ch;
|
|
|
|
fs::path sub = path / p;
|
2018-06-13 13:09:19 +00:00
|
|
|
|
|
|
|
ssize_t l = loadSubdir(sub);
|
|
|
|
if(l > 0)
|
|
|
|
loaded += l;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:40:02 +00:00
|
|
|
ssize_t
|
|
|
|
loadSubdir(const fs::path &dir)
|
|
|
|
{
|
|
|
|
ssize_t sz = 0;
|
2018-08-26 12:51:22 +00:00
|
|
|
llarp::util::IterDir(dir, [&](const fs::path &f) -> bool {
|
|
|
|
if(fs::is_regular_file(f) && loadfile(f))
|
2018-05-30 00:40:02 +00:00
|
|
|
sz++;
|
2018-08-26 12:51:22 +00:00
|
|
|
return true;
|
|
|
|
});
|
2018-05-30 00:40:02 +00:00
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
loadfile(const fs::path &fpath)
|
|
|
|
{
|
2018-08-02 23:30:34 +00:00
|
|
|
if(fpath.extension() != RC_FILE_EXT)
|
2018-06-21 11:14:14 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::RouterContact rc;
|
|
|
|
if(!rc.Read(fpath.string().c_str()))
|
2018-06-21 09:33:23 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::LogError("failed to read file ", fpath);
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-06-21 11:14:14 +00:00
|
|
|
}
|
2018-10-15 12:02:32 +00:00
|
|
|
if(!rc.Verify(crypto))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-10-15 12:02:32 +00:00
|
|
|
llarp::LogError(fpath, " contains invalid RC");
|
2018-06-21 11:14:14 +00:00
|
|
|
return false;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
llarp::util::Lock lock(access);
|
|
|
|
entries.insert(std::make_pair(rc.pubkey, rc));
|
|
|
|
}
|
2018-06-21 11:14:14 +00:00
|
|
|
return true;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-09-11 15:53:54 +00:00
|
|
|
void
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
bool
|
2018-10-03 10:42:12 +00:00
|
|
|
iterate(struct llarp_nodedb_iter &i)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
i.index = 0;
|
|
|
|
llarp::util::Lock lock(access);
|
2018-06-21 09:33:23 +00:00
|
|
|
auto itr = entries.begin();
|
|
|
|
while(itr != entries.end())
|
|
|
|
{
|
2018-06-21 11:33:28 +00:00
|
|
|
i.rc = &itr->second;
|
|
|
|
i.visit(&i);
|
2018-06-21 09:33:23 +00:00
|
|
|
|
2018-06-21 11:33:28 +00:00
|
|
|
// advance
|
|
|
|
i.index++;
|
|
|
|
itr++;
|
2018-06-21 09:33:23 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:40:02 +00:00
|
|
|
/*
|
|
|
|
bool Save()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-30 00:40:02 +00:00
|
|
|
auto itr = entries.begin();
|
|
|
|
while(itr != entries.end())
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-30 00:40:02 +00:00
|
|
|
llarp::pubkey pk = itr->first;
|
|
|
|
llarp_rc *rc= itr->second;
|
|
|
|
|
|
|
|
itr++; // advance
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-05-30 00:40:02 +00:00
|
|
|
return true;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-05-30 00:40:02 +00:00
|
|
|
*/
|
2018-04-08 12:18:16 +00:00
|
|
|
};
|
|
|
|
|
2018-06-07 09:36:30 +00:00
|
|
|
// call request hook
|
2018-06-13 11:37:44 +00:00
|
|
|
void
|
|
|
|
logic_threadworker_callback(void *user)
|
|
|
|
{
|
2018-06-07 09:36:30 +00:00
|
|
|
llarp_async_verify_rc *verify_request =
|
2018-06-13 11:37:44 +00:00
|
|
|
static_cast< llarp_async_verify_rc * >(user);
|
2018-06-07 09:36:30 +00:00
|
|
|
verify_request->hook(verify_request);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write it to disk
|
2018-06-13 11:37:44 +00:00
|
|
|
void
|
|
|
|
disk_threadworker_setRC(void *user)
|
|
|
|
{
|
2018-06-07 09:36:30 +00:00
|
|
|
llarp_async_verify_rc *verify_request =
|
2018-06-13 11:37:44 +00:00
|
|
|
static_cast< llarp_async_verify_rc * >(user);
|
2018-08-30 18:48:43 +00:00
|
|
|
verify_request->valid = verify_request->nodedb->Insert(verify_request->rc);
|
2018-08-10 21:34:11 +00:00
|
|
|
if(verify_request->logic)
|
|
|
|
llarp_logic_queue_job(verify_request->logic,
|
|
|
|
{verify_request, &logic_threadworker_callback});
|
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
|
|
|
|
crypto_threadworker_verifyrc(void *user)
|
2018-06-07 09:36:30 +00:00
|
|
|
{
|
|
|
|
llarp_async_verify_rc *verify_request =
|
2018-06-13 11:37:44 +00:00
|
|
|
static_cast< llarp_async_verify_rc * >(user);
|
2018-09-06 11:46:19 +00:00
|
|
|
llarp::RouterContact rc = verify_request->rc;
|
2018-10-15 12:02:32 +00:00
|
|
|
verify_request->valid = rc.Verify(verify_request->nodedb->crypto);
|
2018-06-07 09:36:30 +00:00
|
|
|
// if it's valid we need to set it
|
2018-09-06 11:46:19 +00:00
|
|
|
if(verify_request->valid && rc.IsPublicRouter())
|
2018-06-07 09:36:30 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("RC is valid, saving to disk");
|
2018-06-07 09:36:30 +00:00
|
|
|
llarp_threadpool_queue_job(verify_request->diskworker,
|
2018-06-13 11:37:44 +00:00
|
|
|
{verify_request, &disk_threadworker_setRC});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-07 09:36:30 +00:00
|
|
|
// callback to logic thread
|
2018-08-02 04:34:16 +00:00
|
|
|
if(!verify_request->valid)
|
|
|
|
llarp::LogWarn("RC is not valid, can't save to disk");
|
2018-06-07 09:36:30 +00:00
|
|
|
llarp_logic_queue_job(verify_request->logic,
|
2018-06-13 11:37:44 +00:00
|
|
|
{verify_request, &logic_threadworker_callback});
|
2018-06-07 09:36:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 12:58:51 +00: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 18:48:43 +00:00
|
|
|
job->nodedb->Get(job->pubkey, job->result);
|
2018-06-13 12:58:51 +00:00
|
|
|
}
|
|
|
|
llarp_logic_queue_job(job->logic, {job, &nodedb_inform_load_rc});
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_nodedb *
|
2018-05-28 13:49:44 +00:00
|
|
|
llarp_nodedb_new(struct llarp_crypto *crypto)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-27 16:45:04 +00:00
|
|
|
return new llarp_nodedb(crypto);
|
2018-04-30 16:14:20 +00:00
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb_free(struct llarp_nodedb **n)
|
|
|
|
{
|
|
|
|
if(*n)
|
2018-05-16 18:13:18 +00:00
|
|
|
{
|
2018-05-28 20:51:15 +00:00
|
|
|
auto i = *n;
|
|
|
|
*n = nullptr;
|
|
|
|
i->Clear();
|
|
|
|
delete i;
|
2018-05-16 18:13:18 +00:00
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_put_rc(struct llarp_nodedb *n, const llarp::RouterContact &rc)
|
|
|
|
{
|
|
|
|
return n->Insert(rc);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_ensure_dir(const char *dir)
|
|
|
|
{
|
2018-04-30 16:14:20 +00:00
|
|
|
fs::path path(dir);
|
|
|
|
std::error_code ec;
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!fs::exists(dir, ec))
|
2018-09-29 08:16:54 +00:00
|
|
|
fs::create_directory(path, ec);
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
if(ec)
|
|
|
|
return false;
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!fs::is_directory(path))
|
|
|
|
return false;
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
for(const char &ch : skiplist_subdirs)
|
|
|
|
{
|
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
|
|
|
|
if(!ch)
|
|
|
|
return true;
|
2018-05-29 12:15:48 +00:00
|
|
|
std::string p;
|
|
|
|
p += ch;
|
|
|
|
fs::path sub = path / p;
|
2018-04-30 16:14:20 +00:00
|
|
|
fs::create_directory(sub, ec);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(ec)
|
|
|
|
return false;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-24 17:25:47 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb_set_dir(struct llarp_nodedb *n, const char *dir)
|
|
|
|
{
|
|
|
|
n->nodePath = dir;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
ssize_t
|
|
|
|
llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir)
|
|
|
|
{
|
2018-06-13 11:41:23 +00:00
|
|
|
std::error_code ec;
|
|
|
|
if(!fs::exists(dir, ec))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2018-08-24 17:25:47 +00:00
|
|
|
llarp_nodedb_set_dir(n, dir);
|
2018-04-30 16:14:20 +00:00
|
|
|
return n->Load(dir);
|
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2018-06-21 09:33:23 +00:00
|
|
|
int
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_nodedb_iterate_all(struct llarp_nodedb *n, struct llarp_nodedb_iter i)
|
|
|
|
{
|
2018-06-21 11:33:28 +00:00
|
|
|
n->iterate(i);
|
2018-06-21 09:33:23 +00:00
|
|
|
return n->entries.size();
|
|
|
|
}
|
|
|
|
|
2018-09-11 15:53:54 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb_visit_loaded(
|
|
|
|
struct llarp_nodedb *n,
|
|
|
|
std::function< bool(const llarp::RouterContact &) > visit)
|
|
|
|
{
|
|
|
|
return n->visit(visit);
|
|
|
|
}
|
|
|
|
|
2018-06-23 14:55:25 +00:00
|
|
|
/// maybe rename to verify_and_set
|
2018-05-30 20:56:47 +00:00
|
|
|
void
|
2018-06-13 11:41:23 +00:00
|
|
|
llarp_nodedb_async_verify(struct llarp_async_verify_rc *job)
|
2018-05-30 20:56:47 +00:00
|
|
|
{
|
2018-06-19 17:11:24 +00:00
|
|
|
// switch to crypto threadpool and continue with
|
|
|
|
// crypto_threadworker_verifyrc
|
2018-06-13 11:53:18 +00:00
|
|
|
llarp_threadpool_queue_job(job->cryptoworker,
|
2018-06-13 11:37:44 +00:00
|
|
|
{job, &crypto_threadworker_verifyrc});
|
2018-05-30 20:56:47 +00:00
|
|
|
}
|
2018-06-01 14:08:54 +00:00
|
|
|
|
2018-06-23 14:55:25 +00:00
|
|
|
// disabled for now
|
|
|
|
/*
|
2018-06-13 12:58:51 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb_async_load_rc(struct llarp_async_load_rc *job)
|
2018-06-01 14:08:54 +00:00
|
|
|
{
|
2018-06-13 12:58:51 +00: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 14:55:25 +00:00
|
|
|
*/
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_get_rc(struct llarp_nodedb *n, const llarp::RouterID &pk,
|
|
|
|
llarp::RouterContact &result)
|
2018-06-14 17:35:12 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
// llarp::LogInfo("llarp_nodedb_get_rc [", pk, "]");
|
2018-08-30 18:48:43 +00:00
|
|
|
return n->Get(pk, result);
|
2018-06-14 17:35:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
size_t
|
|
|
|
llarp_nodedb_num_loaded(struct llarp_nodedb *n)
|
|
|
|
{
|
|
|
|
return n->entries.size();
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_del_rc(struct llarp_nodedb *n, const llarp::RouterID &pk)
|
|
|
|
{
|
|
|
|
return n->Remove(pk);
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_select_random_exit(struct llarp_nodedb *n,
|
|
|
|
llarp::RouterContact &result)
|
|
|
|
{
|
|
|
|
const auto sz = n->entries.size();
|
|
|
|
auto itr = n->entries.begin();
|
|
|
|
if(sz < 3)
|
|
|
|
return false;
|
|
|
|
auto idx = llarp_randint() % sz;
|
|
|
|
if(idx)
|
|
|
|
std::advance(itr, idx - 1);
|
|
|
|
while(itr != n->entries.end())
|
|
|
|
{
|
|
|
|
if(itr->second.IsExit())
|
|
|
|
{
|
|
|
|
result = itr->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
// wrap arround
|
|
|
|
itr = n->entries.begin();
|
|
|
|
while(idx--)
|
|
|
|
{
|
|
|
|
if(itr->second.IsExit())
|
|
|
|
{
|
|
|
|
result = itr->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:41:04 +00:00
|
|
|
bool
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp_nodedb_select_random_hop(struct llarp_nodedb *n,
|
|
|
|
const llarp::RouterContact &prev,
|
|
|
|
llarp::RouterContact &result, size_t N)
|
2018-06-19 17:11:24 +00:00
|
|
|
{
|
2018-08-02 20:50:16 +00:00
|
|
|
/// checking for "guard" status for N = 0 is done by caller inside of
|
|
|
|
/// pathbuilder's scope
|
2018-06-20 12:34:48 +00:00
|
|
|
auto sz = n->entries.size();
|
2018-09-06 20:31:58 +00:00
|
|
|
if(sz < 3)
|
2018-08-31 14:41:04 +00:00
|
|
|
return false;
|
2018-09-06 20:31:58 +00:00
|
|
|
size_t tries = 5;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(N)
|
2018-06-19 17:11:24 +00:00
|
|
|
{
|
2018-06-20 12:34:48 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
auto itr = n->entries.begin();
|
|
|
|
if(sz > 1)
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
auto idx = llarp_randint() % sz;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(idx)
|
|
|
|
std::advance(itr, idx - 1);
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
if(prev.pubkey == itr->second.pubkey)
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
|
|
|
if(tries--)
|
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
if(itr->second.addrs.size())
|
2018-07-28 22:20:32 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
result = itr->second;
|
2018-08-31 14:41:04 +00:00
|
|
|
return true;
|
2018-07-28 22:20:32 +00:00
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
} while(tries--);
|
|
|
|
return false;
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto itr = n->entries.begin();
|
|
|
|
if(sz > 1)
|
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
auto idx = llarp_randint() % sz;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(idx)
|
|
|
|
std::advance(itr, idx - 1);
|
2018-06-20 12:34:48 +00:00
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
result = itr->second;
|
2018-08-31 14:41:04 +00:00
|
|
|
return true;
|
2018-06-19 17:11:24 +00:00
|
|
|
}
|
|
|
|
}
|