2018-08-30 18:48:43 +00:00
|
|
|
#ifndef LLARP_NODEDB_HPP
|
|
|
|
#define LLARP_NODEDB_HPP
|
2018-11-19 23:27:59 +00:00
|
|
|
#include <llarp/common.hpp>
|
2018-04-08 12:18:16 +00:00
|
|
|
#include <llarp/crypto.h>
|
2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/router_contact.hpp>
|
|
|
|
#include <llarp/router_id.hpp>
|
2018-05-31 13:08:06 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-30 18:48:43 +00:00
|
|
|
* nodedb.hpp
|
2018-05-31 13:08:06 +00:00
|
|
|
*
|
|
|
|
* persistent storage API for router contacts
|
|
|
|
*/
|
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
struct llarp_nodedb;
|
|
|
|
|
|
|
|
/// create an empty nodedb
|
|
|
|
struct llarp_nodedb *
|
|
|
|
llarp_nodedb_new(struct llarp_crypto *crypto);
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
2018-08-24 17:25:47 +00:00
|
|
|
void
|
|
|
|
llarp_nodedb_set_dir(struct llarp_nodedb *n, const char *dir);
|
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
/// 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;
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::RouterContact *rc;
|
2018-06-19 09:46:21 +00:00
|
|
|
size_t index;
|
2018-06-18 22:05:02 +00:00
|
|
|
bool (*visit)(struct llarp_nodedb_iter *);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// iterate over all loaded rc with an iterator
|
2018-06-19 09:46:21 +00:00
|
|
|
int
|
2018-06-18 22:05:02 +00:00
|
|
|
llarp_nodedb_iterate_all(struct llarp_nodedb *n, struct llarp_nodedb_iter i);
|
|
|
|
|
2018-09-11 15:53:54 +00:00
|
|
|
/// visit all loaded rc
|
|
|
|
/// stop iteration if visit return false
|
|
|
|
void
|
|
|
|
llarp_nodedb_visit_loaded(
|
|
|
|
struct llarp_nodedb *n,
|
|
|
|
std::function< bool(const llarp::RouterContact &) > visit);
|
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
/// return number of RC loaded
|
|
|
|
size_t
|
|
|
|
llarp_nodedb_num_loaded(struct llarp_nodedb *n);
|
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
/**
|
|
|
|
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
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp_nodedb_put_rc(struct llarp_nodedb *n, const llarp::RouterContact &rc);
|
2018-06-18 22:05:02 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_get_rc(struct llarp_nodedb *n, const llarp::RouterID &pk,
|
|
|
|
llarp::RouterContact &result);
|
2018-06-18 22:05:02 +00:00
|
|
|
|
2018-09-13 16:41:53 +00:00
|
|
|
/**
|
|
|
|
remove rc by public key from nodedb
|
|
|
|
returns true if removed
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
llarp_nodedb_del_rc(struct llarp_nodedb *n, const llarp::RouterID &pk);
|
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
/// struct for async rc verification
|
|
|
|
struct llarp_async_verify_rc;
|
|
|
|
|
|
|
|
typedef void (*llarp_async_verify_rc_hook_func)(struct llarp_async_verify_rc *);
|
|
|
|
|
|
|
|
/// verify rc request
|
|
|
|
struct llarp_async_verify_rc
|
|
|
|
{
|
|
|
|
/// async_verify_context
|
|
|
|
void *user;
|
|
|
|
/// nodedb storage
|
|
|
|
struct llarp_nodedb *nodedb;
|
|
|
|
// llarp_logic for llarp_logic_queue_job
|
|
|
|
struct llarp_logic *logic; // includes a llarp_threadpool
|
|
|
|
// struct llarp_crypto *crypto; // probably don't need this because we have
|
|
|
|
// it in the nodedb
|
|
|
|
struct llarp_threadpool *cryptoworker;
|
|
|
|
struct llarp_threadpool *diskworker;
|
|
|
|
|
2018-07-03 13:13:56 +00:00
|
|
|
/// router contact
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::RouterContact rc;
|
2018-06-18 22:05:02 +00:00
|
|
|
/// result
|
|
|
|
bool valid;
|
|
|
|
/// hook
|
|
|
|
llarp_async_verify_rc_hook_func hook;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
struct for async rc verification
|
|
|
|
data is loaded in disk io threadpool
|
|
|
|
crypto is done on the crypto worker threadpool
|
|
|
|
result is called on the logic thread
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
llarp_nodedb_async_verify(struct llarp_async_verify_rc *job);
|
|
|
|
|
|
|
|
struct llarp_async_load_rc;
|
|
|
|
|
|
|
|
typedef void (*llarp_async_load_rc_hook_func)(struct llarp_async_load_rc *);
|
|
|
|
|
|
|
|
struct llarp_async_load_rc
|
|
|
|
{
|
|
|
|
/// async_verify_context
|
|
|
|
void *user;
|
|
|
|
/// nodedb storage
|
|
|
|
struct llarp_nodedb *nodedb;
|
|
|
|
/// llarp_logic for calling hook
|
|
|
|
struct llarp_logic *logic;
|
|
|
|
/// disk worker threadpool
|
|
|
|
struct llarp_threadpool *diskworker;
|
|
|
|
/// target pubkey
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::PubKey pubkey;
|
2018-06-18 22:05:02 +00:00
|
|
|
/// router contact result
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp::RouterContact result;
|
2018-06-18 22:05:02 +00:00
|
|
|
/// set to true if we loaded the rc
|
|
|
|
bool loaded;
|
|
|
|
/// hook function called in logic thread
|
|
|
|
llarp_async_load_rc_hook_func hook;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// asynchronously load an rc from disk
|
|
|
|
void
|
|
|
|
llarp_nodedb_async_load_rc(struct llarp_async_load_rc *job);
|
2018-06-13 12:58:51 +00:00
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
bool
|
|
|
|
llarp_nodedb_select_random_exit(struct llarp_nodedb *n,
|
|
|
|
llarp::RouterContact &rc);
|
|
|
|
|
2018-08-31 14:41:04 +00:00
|
|
|
bool
|
2018-08-30 18:48:43 +00:00
|
|
|
llarp_nodedb_select_random_hop(struct llarp_nodedb *n,
|
|
|
|
const llarp::RouterContact &prev,
|
|
|
|
llarp::RouterContact &result, size_t N);
|
|
|
|
|
2018-04-08 12:18:16 +00:00
|
|
|
#endif
|