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

462 lines
9.7 KiB
C++
Raw Normal View History

2018-06-13 13:37:44 +02:00
#include <llarp/crypto_async.h>
2018-04-08 14:18:16 +02:00
#include <llarp/nodedb.h>
#include <llarp/router_contact.h>
#include <fstream>
#include <llarp/crypto.hpp>
2018-06-10 16:05:48 +02:00
#include <unordered_map>
#include "buffer.hpp"
#include "encode.hpp"
2018-06-13 13:37:44 +02:00
#include "fs.hpp"
2018-05-30 02:40:02 +02:00
#include "logger.hpp"
2018-06-13 13:37:44 +02:00
#include "mem.hpp"
2018-05-30 02:40:02 +02:00
2018-06-13 15:09:19 +02:00
static const char skiplist_subdirs[] = "0123456789abcdef";
2018-05-30 02:40:02 +02:00
struct llarp_nodedb
{
llarp_nodedb(llarp_crypto *c) : crypto(c)
{
}
2018-05-16 20:13:18 +02:00
llarp_crypto *crypto;
2018-06-14 19:35:12 +02:00
// std::map< llarp::pubkey, llarp_rc > entries;
std::unordered_map< llarp::PubKey, llarp_rc, llarp::PubKeyHash > entries;
fs::path nodePath;
2018-04-08 14:18:16 +02:00
void
Clear()
2018-05-16 20:13:18 +02:00
{
auto itr = entries.begin();
while(itr != entries.end())
{
2018-06-14 19:35:12 +02:00
llarp_rc_clear(&itr->second);
2018-05-16 20:13:18 +02:00
itr = entries.erase(itr);
}
}
2018-06-13 13:37:44 +02:00
llarp_rc *
2018-06-14 19:35:12 +02:00
getRC(const llarp::PubKey &pk)
{
2018-06-14 19:35:12 +02:00
return &entries[pk];
}
bool
Has(const llarp::PubKey &pk)
{
return entries.find(pk) != entries.end();
2018-05-30 02:40:02 +02:00
}
/*
bool
Has(const byte_t *pk)
{
llarp::PubKey test(pk);
auto itr = this->entries.begin();
while(itr != this->entries.end())
{
llarp::LogInfo("Has byte_t [", test.size(), "] vs [", itr->first.size(),
"]"); if (memcmp(test.data(), itr->first.data(), 32) == 0) {
llarp::LogInfo("Match");
}
itr++;
}
return entries.find(pk) != entries.end();
}
*/
2018-06-13 13:37:44 +02:00
bool
pubKeyExists(llarp_rc *rc)
{
2018-05-30 02:40:02 +02:00
// extract pk from rc
2018-06-13 13:37:44 +02:00
llarp::PubKey pk = rc->pubkey;
2018-05-30 02:40:02 +02:00
// return true if we found before end
return entries.find(pk) != entries.end();
}
2018-06-13 13:37:44 +02:00
bool
check(llarp_rc *rc)
{
2018-06-13 13:37:44 +02:00
if(!pubKeyExists(rc))
{
2018-05-30 02:40:02 +02:00
// we don't have it
return false;
}
2018-06-13 13:37:44 +02:00
llarp::PubKey pk = rc->pubkey;
2018-05-30 02:40:02 +02:00
// TODO: zero out any fields you don't want to compare
// serialize both and memcmp
byte_t nodetmp[MAX_RC_SIZE];
auto nodebuf = llarp::StackBuffer< decltype(nodetmp) >(nodetmp);
2018-06-14 19:35:12 +02:00
if(llarp_rc_bencode(&entries[pk], &nodebuf))
{
2018-05-30 02:40:02 +02:00
byte_t paramtmp[MAX_RC_SIZE];
auto parambuf = llarp::StackBuffer< decltype(paramtmp) >(paramtmp);
2018-06-13 13:37:44 +02:00
if(llarp_rc_bencode(rc, &parambuf))
{
2018-06-14 19:35:12 +02:00
if(nodebuf.sz == parambuf.sz)
return memcmp(&parambuf, &nodebuf, parambuf.sz) == 0;
2018-05-30 02:40:02 +02:00
}
}
return false;
}
2018-06-13 14:58:51 +02:00
std::string
getRCFilePath(const byte_t *pubkey)
{
char ftmp[68] = {0};
const char *hexname =
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(pubkey, ftmp);
std::string hexString(hexname);
std::string filepath = nodePath;
filepath.append(PATH_SEP);
filepath.append(&hexString[hexString.length() - 1]);
filepath.append(PATH_SEP);
filepath.append(hexname);
filepath.append(".signed");
return filepath;
}
2018-06-13 13:37:44 +02:00
bool
setRC(llarp_rc *rc)
{
2018-05-30 02:40:02 +02:00
byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
// extract pk from rc
2018-06-13 13:37:44 +02:00
llarp::PubKey pk = rc->pubkey;
2018-05-30 02:40:02 +02:00
2018-06-14 19:35:12 +02:00
// set local db entry to have a copy we own
llarp_rc entry;
llarp::Zero(&entry, sizeof(entry));
llarp_rc_copy(&entry, rc);
entries[pk] = entry;
2018-05-30 02:40:02 +02:00
2018-06-14 19:35:12 +02:00
if(llarp_rc_bencode(&entry, &buf))
{
2018-06-13 15:09:19 +02:00
buf.sz = buf.cur - buf.base;
2018-06-14 19:35:12 +02:00
auto filepath = getRCFilePath(pk);
llarp::LogDebug("saving RC.pubkey ", filepath);
2018-07-17 06:37:50 +02:00
std::ofstream ofs(
filepath,
std::ofstream::out & std::ofstream::binary & std::ofstream::trunc);
2018-05-30 02:40:02 +02:00
ofs.write((char *)buf.base, buf.sz);
ofs.close();
2018-06-13 13:37:44 +02:00
if(!ofs)
{
llarp::LogError("Failed to write: ", filepath);
2018-05-30 02:40:02 +02:00
return false;
}
llarp::LogDebug("saved RC.pubkey: ", filepath);
2018-05-30 02:40:02 +02:00
return true;
}
return false;
}
ssize_t
Load(const fs::path &path)
{
2018-04-08 14:18:16 +02:00
std::error_code ec;
if(!fs::exists(path, ec))
{
2018-04-08 14:18:16 +02:00
return -1;
}
ssize_t loaded = 0;
2018-04-30 18:14:20 +02:00
for(const char &ch : skiplist_subdirs)
{
2018-05-29 14:15:48 +02:00
std::string p;
p += ch;
fs::path sub = path / p;
2018-06-13 15:09:19 +02:00
ssize_t l = loadSubdir(sub);
if(l > 0)
loaded += l;
2018-04-08 14:18:16 +02:00
}
return loaded;
}
2018-05-30 02:40:02 +02:00
ssize_t
loadSubdir(const fs::path &dir)
{
ssize_t sz = 0;
2018-06-14 22:33:05 +02:00
fs::directory_iterator i(dir);
auto itr = i.begin();
while(itr != itr.end())
2018-05-30 02:40:02 +02:00
{
if(fs::is_regular_file(itr->symlink_status()) && loadfile(*itr))
2018-05-30 02:40:02 +02:00
sz++;
2018-06-14 22:33:05 +02:00
++itr;
2018-05-30 02:40:02 +02:00
}
return sz;
}
bool
loadfile(const fs::path &fpath)
{
#if __APPLE__ && __MACH__
// skip .DS_Store files
if(strstr(fpath.c_str(), ".DS_Store") != 0)
{
return false;
}
#endif
llarp_rc *rc = llarp_rc_read(fpath.c_str());
if(!rc)
{
llarp::LogError("Signature read failed", fpath);
return false;
}
if(!llarp_rc_verify_sig(crypto, rc))
{
llarp::LogError("Signature verify failed", fpath);
return false;
2018-04-08 14:18:16 +02:00
}
llarp::PubKey pk(rc->pubkey);
entries[pk] = *rc;
return true;
2018-04-08 14:18:16 +02:00
}
2018-04-30 18:14:20 +02:00
bool
iterate(struct llarp_nodedb_iter i)
{
i.index = 0;
auto itr = entries.begin();
while(itr != entries.end())
{
2018-06-21 13:33:28 +02:00
i.rc = &itr->second;
i.visit(&i);
2018-06-21 13:33:28 +02:00
// advance
i.index++;
itr++;
}
return true;
}
2018-05-30 02:40:02 +02:00
/*
bool Save()
{
2018-05-30 02:40:02 +02:00
auto itr = entries.begin();
while(itr != entries.end())
{
2018-05-30 02:40:02 +02:00
llarp::pubkey pk = itr->first;
llarp_rc *rc= itr->second;
itr++; // advance
2018-04-08 14:18:16 +02:00
}
2018-05-30 02:40:02 +02:00
return true;
2018-04-08 14:18:16 +02:00
}
2018-05-30 02:40:02 +02:00
*/
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);
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);
verify_request->valid = verify_request->nodedb->setRC(&verify_request->rc);
2018-06-13 13:37:44 +02:00
llarp_logic_queue_job(verify_request->logic,
{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);
verify_request->valid =
llarp_rc_verify_sig(verify_request->nodedb->crypto, &verify_request->rc);
// if it's valid we need to set it
2018-06-13 13:37:44 +02:00
if(verify_request->valid)
{
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
llarp::LogWarn("RC is not valid, can't save to disk");
llarp_logic_queue_job(verify_request->logic,
2018-06-13 13:37:44 +02:00
{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-06-26 15:39:29 +02:00
llarp_rc_clear(&job->rc);
2018-06-13 14:58:51 +02:00
llarp_rc_copy(&job->rc, job->nodedb->getRC(job->pubkey));
}
llarp_logic_queue_job(job->logic, {job, &nodedb_inform_load_rc});
}
struct llarp_nodedb *
llarp_nodedb_new(struct llarp_crypto *crypto)
{
return new llarp_nodedb(crypto);
2018-04-30 18:14:20 +02:00
}
2018-04-08 14:18:16 +02:00
void
llarp_nodedb_free(struct llarp_nodedb **n)
{
if(*n)
2018-05-16 20:13:18 +02:00
{
2018-05-28 22:51:15 +02:00
auto i = *n;
*n = nullptr;
i->Clear();
delete i;
2018-05-16 20:13:18 +02:00
}
2018-04-30 18:14:20 +02:00
}
2018-04-08 14:18:16 +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_directories(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)
{
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;
}
ssize_t
llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir)
{
std::error_code ec;
if(!fs::exists(dir, ec))
{
return -1;
}
n->nodePath = dir;
2018-04-30 18:14:20 +02:00
return n->Load(dir);
}
2018-05-30 22:56:47 +02:00
2018-06-23 16:55:25 +02:00
/// c api for nodedb::setRC
/// maybe better to use llarp_nodedb_async_verify
bool
llarp_nodedb_put_rc(struct llarp_nodedb *n, struct llarp_rc *rc)
{
return n->setRC(rc);
}
int
llarp_nodedb_iterate_all(struct llarp_nodedb *n, struct llarp_nodedb_iter i)
{
2018-06-21 13:33:28 +02:00
n->iterate(i);
return n->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-14 19:35:12 +02:00
struct llarp_rc *
llarp_nodedb_get_rc(struct llarp_nodedb *n, const byte_t *pk)
{
// llarp::LogInfo("llarp_nodedb_get_rc [", pk, "]");
2018-06-14 19:35:12 +02:00
if(n->Has(pk))
return n->getRC(pk);
else
return nullptr;
}
2018-06-19 19:11:24 +02:00
size_t
llarp_nodedb_num_loaded(struct llarp_nodedb *n)
{
return n->entries.size();
}
void
2018-06-20 14:34:48 +02:00
llarp_nodedb_select_random_hop(struct llarp_nodedb *n, struct llarp_rc *prev,
struct llarp_rc *result, size_t N)
2018-06-19 19:11:24 +02:00
{
/// TODO: check for "guard" status for N = 0?
2018-06-20 14:34:48 +02:00
auto sz = n->entries.size();
if(prev)
2018-06-19 19:11:24 +02:00
{
2018-06-20 14:34:48 +02:00
do
{
auto itr = n->entries.begin();
if(sz > 1)
{
auto idx = rand() % sz;
2018-06-20 14:34:48 +02:00
std::advance(itr, idx);
}
if(memcmp(prev->pubkey, itr->second.pubkey, PUBKEYSIZE) == 0)
continue;
llarp_rc_copy(result, &itr->second);
return;
} while(true);
}
else
{
auto itr = n->entries.begin();
if(sz > 1)
{
2018-06-22 15:59:50 +02:00
auto idx = rand() % sz;
2018-06-20 14:34:48 +02:00
std::advance(itr, idx);
}
llarp_rc_copy(result, &itr->second);
2018-06-19 19:11:24 +02:00
}
}