2018-04-08 12:18:16 +00:00
|
|
|
#include <llarp/nodedb.h>
|
|
|
|
#include <llarp/router_contact.h>
|
2018-05-27 16:45:04 +00:00
|
|
|
#include <fstream>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include <map>
|
2018-05-27 16:45:04 +00:00
|
|
|
#include "buffer.hpp"
|
2018-04-08 12:18:16 +00:00
|
|
|
#include "crypto.hpp"
|
|
|
|
#include "fs.hpp"
|
2018-05-16 18:13:18 +00:00
|
|
|
#include "mem.hpp"
|
2018-04-08 12:18:16 +00:00
|
|
|
|
|
|
|
static const char skiplist_subdirs[] = "0123456789ABCDEF";
|
|
|
|
|
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;
|
|
|
|
std::map< llarp::pubkey, llarp_rc * > entries;
|
2018-04-08 12:18:16 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
Clear()
|
2018-05-16 18:13:18 +00:00
|
|
|
{
|
|
|
|
auto itr = entries.begin();
|
|
|
|
while(itr != entries.end())
|
|
|
|
{
|
2018-05-27 16:45:04 +00:00
|
|
|
delete itr->second;
|
2018-05-16 18:13:18 +00:00
|
|
|
itr = entries.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
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-05-29 12:15:48 +00:00
|
|
|
std::string p;
|
|
|
|
p += ch;
|
|
|
|
fs::path sub = path / p;
|
2018-05-22 15:54:19 +00:00
|
|
|
for(auto &f : fs::directory_iterator(sub))
|
|
|
|
{
|
2018-04-08 12:18:16 +00:00
|
|
|
ssize_t l = loadSubdir(f);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(l > 0)
|
|
|
|
loaded += l;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
loadfile(const fs::path &fpath)
|
|
|
|
{
|
2018-05-27 16:45:04 +00:00
|
|
|
std::ifstream f(fpath, std::ios::binary);
|
|
|
|
if(!f.is_open())
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-05-27 16:45:04 +00:00
|
|
|
|
|
|
|
byte_t tmp[MAX_RC_SIZE];
|
|
|
|
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
f.seekg(0, std::ios::end);
|
|
|
|
size_t sz = f.tellg();
|
|
|
|
f.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
if(sz > buf.sz)
|
2018-04-08 12:18:16 +00:00
|
|
|
return false;
|
2018-05-27 16:45:04 +00:00
|
|
|
|
|
|
|
// TODO: error checking
|
|
|
|
f.read((char *)buf.base, sz);
|
|
|
|
buf.sz = sz;
|
|
|
|
|
|
|
|
llarp_rc *rc = new llarp_rc;
|
2018-05-20 17:45:47 +00:00
|
|
|
llarp::Zero(rc, sizeof(llarp_rc));
|
2018-05-27 16:45:04 +00:00
|
|
|
if(llarp_rc_bdecode(rc, &buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
if(llarp_rc_verify_sig(crypto, rc))
|
|
|
|
{
|
2018-04-08 12:18:16 +00:00
|
|
|
llarp::pubkey pk;
|
|
|
|
memcpy(pk.data(), rc->pubkey, pk.size());
|
|
|
|
entries[pk] = rc;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llarp_rc_free(rc);
|
2018-05-27 16:45:04 +00:00
|
|
|
delete rc;
|
2018-04-08 12:18:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
ssize_t
|
|
|
|
loadSubdir(const fs::path &dir)
|
|
|
|
{
|
2018-04-08 12:18:16 +00:00
|
|
|
ssize_t sz = 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
for(auto &path : fs::directory_iterator(dir))
|
|
|
|
{
|
|
|
|
if(loadfile(path))
|
|
|
|
sz++;
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
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-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-05-22 15:54:19 +00:00
|
|
|
if(!fs::exists(dir, ec))
|
|
|
|
fs::create_directories(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-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-05-22 15:54:19 +00:00
|
|
|
ssize_t
|
|
|
|
llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir)
|
|
|
|
{
|
2018-04-30 16:14:20 +00:00
|
|
|
return n->Load(dir);
|
|
|
|
}
|
2018-04-08 12:18:16 +00:00
|
|
|
}
|