You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/nodedb.cpp

118 lines
2.7 KiB
C++

6 years ago
#include <llarp/nodedb.h>
#include <llarp/router_contact.h>
6 years ago
#include <map>
6 years ago
#include "crypto.hpp"
#include "fs.hpp"
6 years ago
#include "mem.hpp"
6 years ago
static const char skiplist_subdirs[] = "0123456789ABCDEF";
6 years ago
struct llarp_nodedb {
6 years ago
llarp_nodedb(llarp_alloc * m, llarp_crypto * c) : mem(m), crypto(c) {}
6 years ago
llarp_alloc * mem;
llarp_crypto * crypto;
6 years ago
std::map<llarp::pubkey, llarp_rc *> entries;
6 years ago
void Clear()
{
auto itr = entries.begin();
while(itr != entries.end())
{
mem->free(mem, itr->second);
itr = entries.erase(itr);
}
}
6 years ago
ssize_t Load(const fs::path &path) {
6 years ago
std::error_code ec;
6 years ago
if (!fs::exists(path, ec)) {
6 years ago
return -1;
}
ssize_t loaded = 0;
6 years ago
for (const char &ch : skiplist_subdirs) {
6 years ago
fs::path sub = path / std::string(ch, 1);
6 years ago
for (auto &f : fs::directory_iterator(sub)) {
6 years ago
ssize_t l = loadSubdir(f);
6 years ago
if (l > 0) loaded += l;
6 years ago
}
}
return loaded;
}
6 years ago
bool loadfile(const fs::path &fpath) {
6 years ago
llarp_buffer_t buff;
6 years ago
FILE *f = fopen(fpath.c_str(), "rb");
if (!f) return false;
6 years ago
if (!llarp_buffer_readfile(&buff, f, mem)) {
6 years ago
fclose(f);
return false;
}
fclose(f);
6 years ago
llarp_rc *rc = llarp::Alloc<llarp_rc>(mem);
llarp::Zero(rc, sizeof(llarp_rc));
6 years ago
if (llarp_rc_bdecode(mem, rc, &buff)) {
if (llarp_rc_verify_sig(crypto, rc)) {
6 years ago
llarp::pubkey pk;
memcpy(pk.data(), rc->pubkey, pk.size());
entries[pk] = rc;
return true;
}
}
llarp_rc_free(rc);
6 years ago
mem->free(mem, rc);
6 years ago
return false;
}
6 years ago
ssize_t loadSubdir(const fs::path &dir) {
6 years ago
ssize_t sz = 0;
6 years ago
for (auto &path : fs::directory_iterator(dir)) {
if (loadfile(path)) sz++;
6 years ago
}
return sz;
}
};
extern "C" {
struct llarp_nodedb *llarp_nodedb_new(struct llarp_alloc * mem, struct llarp_crypto * crypto) {
6 years ago
void * ptr = mem->alloc(mem, sizeof(llarp_nodedb), llarp::alignment<llarp_nodedb>());
if(!ptr) return nullptr;
return new (ptr) llarp_nodedb(mem, crypto);
6 years ago
}
6 years ago
6 years ago
void llarp_nodedb_free(struct llarp_nodedb **n) {
6 years ago
if (*n)
{
struct llarp_alloc *mem = (*n)->mem;
(*n)->Clear();
(*n)->~llarp_nodedb();
mem->free(mem, *n);
}
6 years ago
*n = nullptr;
}
6 years ago
6 years ago
bool llarp_nodedb_ensure_dir(const char *dir) {
fs::path path(dir);
std::error_code ec;
if (!fs::exists(dir, ec)) fs::create_directories(path, ec);
6 years ago
6 years ago
if (ec) return false;
6 years ago
6 years ago
if (!fs::is_directory(path)) return false;
for (const char &ch : skiplist_subdirs) {
fs::path sub = path / std::string(ch, 1);
fs::create_directory(sub, ec);
if (ec) return false;
6 years ago
}
6 years ago
return true;
}
ssize_t llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir) {
return n->Load(dir);
}
6 years ago
}