pull/1/head
Jeff Becker 6 years ago
parent 9bf457b4e7
commit 02dfb7c3a9
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -47,8 +47,8 @@ else
endif
REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c11 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++14 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
LIB_LDFLAGS = $(MALLOC_LIB) $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++
REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++17 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
LIB_LDFLAGS = $(MALLOC_LIB) $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++ -lstdc++fs
REQUIRED_LDFLAGS = -L$(REPO) -lllarp
TEST_LDFLAGS = $(STATIC_LIB) $(LIB_LDFLAGS)

@ -1,9 +1,11 @@
#ifndef LLARP_BUFFER_H_
#define LLARP_BUFFER_H_
#include <llarp/common.h>
#include <llarp/mem.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
@ -35,6 +37,8 @@ bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data,
bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
bool llarp_buffer_readfile(llarp_buffer_t * buff, FILE * f,struct llarp_alloc * mem);
#ifdef __cplusplus
}
#endif

@ -1,5 +1,5 @@
#ifndef SARP_NET_H
#define SARP_NET_H
#ifndef LLARP_NET_H
#define LLARP_NET_H
#include <netinet/in.h>
#include <sys/socket.h>

@ -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

@ -16,10 +16,11 @@ struct llarp_rc {
bool llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buf);
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buf);
void llarp_rc_free(struct llarp_rc **rc);
void llarp_rc_free(struct llarp_rc *rc);
bool llarp_rc_verify_sig(struct llarp_rc *rc);
#ifdef __cplusplus
}
#endif
#endif

@ -14,4 +14,22 @@ bool llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) {
buff->sz += written;
return true;
}
bool llarp_buffer_readfile(llarp_buffer_t * buff, FILE * f, llarp_alloc * mem)
{
ssize_t len;
fseek(f, 0, SEEK_END);
len = ftell(f);
rewind(f);
if(len > 0)
{
buff->base = static_cast<char *>(mem->alloc(len, 8));
buff->cur = buff->base;
buff->sz = len;
size_t sz = fread(buff->base, len, 1, f);
rewind(f);
return sz == len;
}
return false;
}
}

@ -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);
}
}

@ -20,6 +20,15 @@ static bool bencode_rc_xi_write(struct llarp_xi_list_iter *i,
} // namespace llarp
extern "C" {
void llarp_rc_free(struct llarp_rc *rc)
{
if(rc->exits)
llarp_xi_list_free(rc->exits);
if(rc->addrs)
llarp_ai_list_free(rc->addrs);
}
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
/* write dict begin */
if (!bencode_start_dict(buff)) return false;

Loading…
Cancel
Save