mirror of https://github.com/oxen-io/lokinet
more
parent
9bf457b4e7
commit
02dfb7c3a9
@ -0,0 +1,59 @@
|
||||
#ifndef LLARP_NODEDB_H
|
||||
#define LLARP_NODEDB_H
|
||||
#include <llarp/common.h>
|
||||
#include <llarp/crypto.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct llarp_nodedb;
|
||||
|
||||
/** create an empty nodedb */
|
||||
struct llarp_nodedb * llarp_nodedb_new();
|
||||
|
||||
/** free a nodedb and all loaded rc */
|
||||
void llarp_nodedb_free(struct llarp_nodedb ** n);
|
||||
|
||||
/** ensure a nodedb fs skiplist structure is at dir
|
||||
create if not there.
|
||||
*/
|
||||
bool llarp_nodedb_ensure_dir(const char * dir);
|
||||
|
||||
/** load entire nodedb from fs skiplist at dir */
|
||||
ssize_t llarp_nodedb_load_dir(struct llarp_nodedb * n, const char * dir);
|
||||
|
||||
/** store entire nodedb to fs skiplist at dir */
|
||||
ssize_t llarp_nodedb_store_dir(struct llarp_nodedb * n, const char * dir);
|
||||
|
||||
struct llarp_nodedb_iter
|
||||
{
|
||||
void * user;
|
||||
struct llarp_rc * rc;
|
||||
bool (*visit)(struct llarp_nodedb_iter *);
|
||||
};
|
||||
|
||||
/**
|
||||
iterate over all loaded rc with an iterator
|
||||
*/
|
||||
void llarp_nodedb_iterate_all(struct llarp_nodedb * n, struct llarp_nodedb_iter i);
|
||||
|
||||
/**
|
||||
find rc by rc.k being value k
|
||||
returns true if found otherwise returns false
|
||||
*/
|
||||
bool llarp_nodedb_find_rc(struct llarp_nodedb * n, struct llarp_rc * rc, llarp_pubkey_t k);
|
||||
|
||||
/**
|
||||
put an rc into the node db
|
||||
overwrites with new contents if already present
|
||||
flushes the single entry to disk
|
||||
returns true on success and false on error
|
||||
*/
|
||||
bool llarp_nodedb_put_rc(struct llarp_nodedb * n, struct llarp_rc * rc);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
#ifndef LLARP_CRYPTO_HPP
|
||||
#define LLARP_CRYPTO_HPP
|
||||
|
||||
#include <array>
|
||||
#include <llarp/crypto.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
typedef std::array<uint8_t, sizeof(llarp_pubkey_t)> pubkey;
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
#ifndef LLARP_FS_HPP
|
||||
#define LLARP_FS_HPP
|
||||
|
||||
#include <experimental/filesystem>
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
#endif
|
@ -0,0 +1,115 @@
|
||||
#include <llarp/nodedb.h>
|
||||
#include <llarp/router_contact.h>
|
||||
#include "crypto.hpp"
|
||||
#include "fs.hpp"
|
||||
#include <map>
|
||||
|
||||
|
||||
static const char skiplist_subdirs[] = "0123456789ABCDEF";
|
||||
|
||||
struct llarp_nodedb
|
||||
{
|
||||
std::map<llarp::pubkey, llarp_rc *> entries;
|
||||
|
||||
ssize_t Load(const fs::path & path)
|
||||
{
|
||||
std::error_code ec;
|
||||
if(!fs::exists(path, ec))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
ssize_t loaded = 0;
|
||||
|
||||
for (const char & ch : skiplist_subdirs)
|
||||
{
|
||||
fs::path sub = path / std::string(ch, 1);
|
||||
for(auto & f : fs::directory_iterator(sub))
|
||||
{
|
||||
ssize_t l = loadSubdir(f);
|
||||
if(l > 0) loaded += l;
|
||||
}
|
||||
}
|
||||
return loaded;
|
||||
}
|
||||
|
||||
bool loadfile(const fs::path & fpath)
|
||||
{
|
||||
llarp_rc * rc = new llarp_rc;
|
||||
llarp_buffer_t buff;
|
||||
FILE * f = fopen(fpath.c_str(), "rb");
|
||||
if(!f) return false;
|
||||
if(!llarp_buffer_readfile(&buff, f, &llarp_g_mem))
|
||||
{
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
fclose(f);
|
||||
if(llarp_rc_bdecode(rc, &buff))
|
||||
{
|
||||
if(llarp_rc_verify_sig(rc))
|
||||
{
|
||||
llarp::pubkey pk;
|
||||
memcpy(pk.data(), rc->pubkey, pk.size());
|
||||
entries[pk] = rc;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
llarp_rc_free(rc);
|
||||
delete rc;
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t loadSubdir(const fs::path & dir)
|
||||
{
|
||||
ssize_t sz = 0;
|
||||
for (auto & path : fs::directory_iterator(dir))
|
||||
{
|
||||
if(loadfile(path)) sz++;
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct llarp_nodedb * llarp_nodedb_new()
|
||||
{
|
||||
return new llarp_nodedb;
|
||||
}
|
||||
|
||||
void llarp_nodedb_free(struct llarp_nodedb ** n)
|
||||
{
|
||||
if(*n)
|
||||
delete *n;
|
||||
*n = nullptr;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
if(ec)
|
||||
return false;
|
||||
|
||||
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;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ssize_t llarp_nodedb_load_dir(struct llarp_nodedb * n, const char * dir)
|
||||
{
|
||||
return n->Load(dir);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue