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

118 lines
2.7 KiB
C++
Raw Normal View History

2018-04-08 14:18:16 +02:00
#include <llarp/nodedb.h>
#include <llarp/router_contact.h>
2018-04-30 18:14:20 +02:00
#include <map>
2018-04-08 14:18:16 +02:00
#include "crypto.hpp"
#include "fs.hpp"
2018-05-16 20:13:18 +02:00
#include "mem.hpp"
2018-04-08 14:18:16 +02:00
static const char skiplist_subdirs[] = "0123456789ABCDEF";
2018-04-30 18:14:20 +02:00
struct llarp_nodedb {
2018-05-16 20:13:18 +02:00
2018-05-20 19:45:47 +02:00
llarp_nodedb(llarp_alloc * m, llarp_crypto * c) : mem(m), crypto(c) {}
2018-05-16 20:13:18 +02:00
llarp_alloc * mem;
2018-05-20 19:45:47 +02:00
llarp_crypto * crypto;
2018-04-08 14:18:16 +02:00
std::map<llarp::pubkey, llarp_rc *> entries;
2018-05-16 20:13:18 +02:00
void Clear()
{
auto itr = entries.begin();
while(itr != entries.end())
{
mem->free(mem, itr->second);
itr = entries.erase(itr);
}
}
2018-04-30 18:14:20 +02:00
ssize_t Load(const fs::path &path) {
2018-04-08 14:18:16 +02:00
std::error_code ec;
2018-04-30 18:14:20 +02:00
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-04-08 14:18:16 +02:00
fs::path sub = path / std::string(ch, 1);
2018-04-30 18:14:20 +02:00
for (auto &f : fs::directory_iterator(sub)) {
2018-04-08 14:18:16 +02:00
ssize_t l = loadSubdir(f);
2018-04-30 18:14:20 +02:00
if (l > 0) loaded += l;
2018-04-08 14:18:16 +02:00
}
}
return loaded;
}
2018-04-30 18:14:20 +02:00
bool loadfile(const fs::path &fpath) {
2018-04-08 14:18:16 +02:00
llarp_buffer_t buff;
2018-04-30 18:14:20 +02:00
FILE *f = fopen(fpath.c_str(), "rb");
if (!f) return false;
2018-05-16 20:13:18 +02:00
if (!llarp_buffer_readfile(&buff, f, mem)) {
2018-04-08 14:18:16 +02:00
fclose(f);
return false;
}
fclose(f);
2018-05-16 20:16:23 +02:00
llarp_rc *rc = llarp::Alloc<llarp_rc>(mem);
2018-05-20 19:45:47 +02:00
llarp::Zero(rc, sizeof(llarp_rc));
2018-05-16 20:13:18 +02:00
if (llarp_rc_bdecode(mem, rc, &buff)) {
2018-05-20 19:45:47 +02:00
if (llarp_rc_verify_sig(crypto, rc)) {
2018-04-08 14:18:16 +02:00
llarp::pubkey pk;
memcpy(pk.data(), rc->pubkey, pk.size());
entries[pk] = rc;
return true;
}
}
llarp_rc_free(rc);
2018-05-16 20:13:18 +02:00
mem->free(mem, rc);
2018-04-08 14:18:16 +02:00
return false;
}
2018-04-30 18:14:20 +02:00
ssize_t loadSubdir(const fs::path &dir) {
2018-04-08 14:18:16 +02:00
ssize_t sz = 0;
2018-04-30 18:14:20 +02:00
for (auto &path : fs::directory_iterator(dir)) {
if (loadfile(path)) sz++;
2018-04-08 14:18:16 +02:00
}
return sz;
}
};
extern "C" {
2018-05-20 19:45:47 +02:00
struct llarp_nodedb *llarp_nodedb_new(struct llarp_alloc * mem, struct llarp_crypto * crypto) {
2018-05-16 20:13:18 +02:00
void * ptr = mem->alloc(mem, sizeof(llarp_nodedb), llarp::alignment<llarp_nodedb>());
if(!ptr) return nullptr;
2018-05-20 19:45:47 +02:00
return new (ptr) llarp_nodedb(mem, crypto);
2018-04-30 18:14:20 +02:00
}
2018-04-08 14:18:16 +02:00
2018-04-30 18:14:20 +02:00
void llarp_nodedb_free(struct llarp_nodedb **n) {
2018-05-16 20:13:18 +02:00
if (*n)
{
struct llarp_alloc *mem = (*n)->mem;
(*n)->Clear();
(*n)->~llarp_nodedb();
mem->free(mem, *n);
}
2018-04-30 18:14:20 +02:00
*n = nullptr;
}
2018-04-08 14:18:16 +02:00
2018-04-30 18:14:20 +02:00
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);
2018-04-08 14:18:16 +02:00
2018-04-30 18:14:20 +02:00
if (ec) return false;
2018-04-08 14:18:16 +02:00
2018-04-30 18:14:20 +02:00
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;
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) {
return n->Load(dir);
}
2018-04-08 14:18:16 +02:00
}