pull/1/head
Ryan Tharp 6 years ago
commit 8799d52180

@ -15,7 +15,7 @@
extern "C" {
#endif
/// context for doing asynchronous crpytography for iwp
/// context for doing asynchronous cryptography for iwp
/// with a worker threadpool
/// defined in crypto_async.cpp
struct llarp_async_iwp;
@ -32,6 +32,22 @@ llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
void
llarp_async_iwp_free(struct llarp_async_iwp *iwp);
/// context for doing asynchronous cryptography for rc
/// with a worker threadpool
/// defined in crypto_async.cpp
struct llarp_async_rc;
/// rc async context allocator
struct llarp_async_rc *
llarp_async_rc_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
struct llarp_threadpool *worker);
/// deallocator
void
llarp_async_rc_free(struct llarp_async_rc *rc);
struct iwp_async_keygen;
/// define functor for keygen
@ -42,7 +58,7 @@ struct iwp_async_keygen
{
/// internal wire protocol async configuration
struct llarp_async_iwp *iwp;
/// a customizable pointer to pass data to iteration functor
/// a pointer to pass ourself to thread worker
void *user;
/// destination key buffer
uint8_t *keybuf;
@ -167,6 +183,7 @@ struct iwp_async_frame
/// true if decryption succeded
bool success;
struct llarp_async_iwp *iwp;
/// a pointer to pass ourself
void *user;
/// current session key
uint8_t *sessionkey;
@ -188,6 +205,34 @@ void
iwp_call_async_frame_encrypt(struct llarp_async_iwp *iwp,
struct iwp_async_frame *frame);
/// define functor for rc_verify
typedef void (*rc_verify_hook)(struct rc_async_verify *);
/// rc verify request
struct rc_async_verify
{
/// router contact crypto async configuration
struct llarp_async_rc *context;
/// a pointer to pass ourself to thread worker
void *self;
/// the router contact
struct llarp_rc *rc;
/// result
bool result;
/// result handler callback
rc_verify_hook hook;
/// extra data to pass to hook
void *hook_user;
};
/// rc verify
void rc_call_async_verify(struct llarp_async_rc *context,
struct rc_async_verify *request,
struct llarp_rc *rc);
#ifdef __cplusplus
}
#endif

@ -3,6 +3,13 @@
#include <llarp/common.h>
#include <llarp/crypto.h>
#include <llarp/router_contact.h>
/**
* nodedb.h
*
* persistent storage API for router contacts
*/
#ifdef __cplusplus
extern "C" {
#endif
@ -68,18 +75,38 @@ llarp_nodedb_has_rc(struct llarp_nodedb *n, llarp_pubkey_t k);
bool
llarp_nodedb_put_rc(struct llarp_nodedb *n, struct llarp_rc *rc);
// defined in nodedb.cpp
/*
struct llarp_async_verify_job_context {
struct llarp_nodedb *nodedb;
struct llarp_logic *logic;
struct llarp_crypto *crypto;
struct llarp_threadpool *cryptoworker;
struct llarp_threadpool *diskworker;
};
*/
/**
struct for async rc verification
*/
struct llarp_async_verify_rc;
struct llarp_async_verify_job_context; // forward definition (defined in nodedb.cpp)
typedef void (*llarp_async_verify_rc_hook_func)(struct llarp_async_verify_rc *);
/// verify rc request
struct llarp_async_verify_rc
{
/// user pointers
void *user;
/// context
llarp_async_verify_job_context *context;
/// router contact (should this be a pointer?)
struct llarp_rc rc;
/// result
bool valid;
/// hook
llarp_async_verify_rc_hook_func hook;
};

@ -6,7 +6,7 @@ extern "C" {
#endif
#ifndef __FreeBSD__
#if !(__APPLE__ && __MACH__)
# if !(__APPLE__ && __MACH__)
size_t INLINE
strnlen(const char* str, size_t sz)
{
@ -15,7 +15,7 @@ strnlen(const char* str, size_t sz)
slen++;
return slen;
}
#endif
# endif
#endif
#ifdef __cplusplus

@ -1,4 +1,5 @@
#include <llarp/crypto_async.h>
#include <llarp/router_contact.h>
#include <llarp/mem.h>
#include <string.h>
#include "buffer.hpp"
@ -381,6 +382,33 @@ namespace iwp
}
}
// REFACTOR: same as llarp_async_iwp (unify and rename)
struct llarp_async_rc
{
struct llarp_crypto *crypto;
struct llarp_logic *logic;
struct llarp_threadpool *worker;
};
namespace rc {
void
inform_verify(void *user)
{
rc_async_verify *request = static_cast< rc_async_verify * >(user);
request->hook(request);
}
void
verify(void *user)
{
rc_async_verify *request = static_cast< rc_async_verify * >(user);
request->result = llarp_rc_verify_sig(request->context->crypto, request->rc);
llarp_thread_job job = {.user = user, .work = &inform_verify};
llarp_logic_queue_job(request->context->logic, job);
}
}
extern "C" {
void
@ -456,6 +484,16 @@ iwp_call_async_verify_session_start(struct llarp_async_iwp *iwp,
{session, &iwp::verify_session_start});
}
void rc_call_async_verify(struct llarp_async_rc *context,
struct rc_async_verify *request,
struct llarp_rc *rc)
{
request->context = context;
request->rc = rc;
llarp_threadpool_queue_job(context->worker,
{request, &rc::verify});
}
struct llarp_async_iwp *
llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
struct llarp_threadpool *worker)
@ -475,4 +513,25 @@ llarp_async_iwp_free(struct llarp_async_iwp *iwp)
{
delete iwp;
}
struct llarp_async_rc *
llarp_async_rc_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
struct llarp_threadpool *worker)
{
llarp_async_rc *context = new llarp_async_rc;
if(context)
{
context->crypto = crypto;
context->logic = logic;
context->worker = worker;
}
return context;
}
void
llarp_async_rc_free(struct llarp_async_rc *context)
{
delete context;
}
}

@ -3,6 +3,11 @@
#include <cstdlib>
#include <cstring>
struct llarp_alloc {
void *(*alloc)(struct llarp_alloc *mem, size_t sz, size_t align);
void (*free)(struct llarp_alloc *mem, void *ptr);
};
namespace llarp
{
void *

@ -1,27 +1,70 @@
#include <llarp/nodedb.h>
#include <llarp/router_contact.h>
#include <llarp/crypto_async.h>
#include <llarp/threadpool.h>
#include <fstream>
#include <map>
#include "buffer.hpp"
#include "crypto.hpp"
#include "fs.hpp"
#include "mem.hpp"
#include "encode.hpp"
#include "logger.hpp"
constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
// probably used for more than verify tbh
struct llarp_async_verify_job_context
{
struct llarp_logic *logic;
struct llarp_crypto *crypto;
struct llarp_threadpool *cryptoworker;
struct llarp_threadpool *diskworker;
};
static const char skiplist_subdirs[] = "0123456789ABCDEF";
std::string hexStr(unsigned char *data, int len)
static void on_crypt_verify_rc(rc_async_verify *job)
{
std::string s(len * 2, ' ');
for (int i = 0; i < len; ++i) {
s[2 * i] = hexmap[(data[i] & 0xF0) >> 4];
s[2 * i + 1] = hexmap[data[i] & 0x0F];
if (job->result) {
// set up disk request
// how do we get our diskworker?
} else {
// it's not valid, don't update db
// send back to logic thread
// make generic job based on previous job
//llarp_thread_job job = {.user = job->user, .work = &inform_verify_rc};
//llarp_logic_queue_job(job->context->logic, job);
}
return s;
// TODO: is there any deallocation we need to do
delete (llarp_async_verify_job_context*)job->context; // clean up our temp context created in verify_rc
delete job; // we're done with the rc_async_verify
}
static const char skiplist_subdirs[] = "0123456789ABCDEF";
void verify_rc(void *user)
{
llarp_async_verify_rc *verify_request =
static_cast< llarp_async_verify_rc * >(user);
// transfer context
// FIXME: move this allocation to more a long term home?
llarp_async_rc *async_rc_context = llarp_async_rc_new(
verify_request->context->crypto, verify_request->context->logic,
verify_request->context->cryptoworker);
// set up request
rc_async_verify *async_rc_request = new rc_async_verify;
// rc_call_async_verify will set up context, rc
// user?
async_rc_request->result = false; // just initialize it to something secure
async_rc_request->hook = &on_crypt_verify_rc;
rc_call_async_verify(async_rc_context, async_rc_request,
&verify_request->rc);
// crypto verify
// if success write to disk
//verify_request->context->crypto
//llarp_thread_job job = {.user = user, .work = &inform_keygen};
//llarp_logic_queue_job(keygen->iwp->logic, job);
}
struct llarp_nodedb
{
@ -43,25 +86,30 @@ struct llarp_nodedb
}
}
inline llarp::pubkey getPubKeyFromRC(llarp_rc *rc) {
inline llarp::pubkey getPubKeyFromRC(llarp_rc *rc)
{
llarp::pubkey pk;
memcpy(pk.data(), rc->pubkey, pk.size());
return pk;
}
llarp_rc *getRC(llarp::pubkey pk) {
llarp_rc *getRC(llarp::pubkey pk)
{
return entries[pk];
}
bool pubKeyExists(llarp_rc *rc) {
bool pubKeyExists(llarp_rc *rc)
{
// extract pk from rc
llarp::pubkey pk = getPubKeyFromRC(rc);
// return true if we found before end
return entries.find(pk) != entries.end();
}
bool check(llarp_rc *rc) {
if (!pubKeyExists(rc)) {
bool check(llarp_rc *rc)
{
if (!pubKeyExists(rc))
{
// we don't have it
return false;
}
@ -72,11 +120,14 @@ struct llarp_nodedb
// serialize both and memcmp
byte_t nodetmp[MAX_RC_SIZE];
auto nodebuf = llarp::StackBuffer< decltype(nodetmp) >(nodetmp);
if (llarp_rc_bencode(entries[pk], &nodebuf)) {
if (llarp_rc_bencode(entries[pk], &nodebuf))
{
byte_t paramtmp[MAX_RC_SIZE];
auto parambuf = llarp::StackBuffer< decltype(paramtmp) >(paramtmp);
if (llarp_rc_bencode(rc, &parambuf)) {
if (memcmp(&parambuf, &nodebuf, MAX_RC_SIZE) == 0) {
if (llarp_rc_bencode(rc, &parambuf))
{
if (memcmp(&parambuf, &nodebuf, MAX_RC_SIZE) == 0)
{
return true;
}
}
@ -94,15 +145,21 @@ struct llarp_nodedb
// set local db
entries[pk] = rc;
if (llarp_rc_bencode(rc, &buf)) {
if (llarp_rc_bencode(rc, &buf))
{
// write buf to disk
auto filename = hexStr(pk.data(), sizeof(pk)) + ".rc";
//auto filename = hexStr(pk.data(), sizeof(pk)) + ".rc";
char ftmp[68] = {0};
const char *hexname =
llarp::HexEncode< llarp::pubkey, decltype(ftmp) >(pk, ftmp);
std::string filename(hexname);
// FIXME: path?
printf("filename[%s]\n", filename.c_str());
std::ofstream ofs (filename, std::ofstream::out & std::ofstream::binary & std::ofstream::trunc);
ofs.write((char *)buf.base, buf.sz);
ofs.close();
if (!ofs) {
if (!ofs)
{
llarp::Error(__FILE__, "Failed to write", filename);
return false;
}
@ -254,6 +311,24 @@ llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir)
return n->Load(dir);
}
/// allocate verify job context
struct llarp_async_verify_job_context*
llarp_async_verify_job_new(struct llarp_threadpool *cryptoworker,
struct llarp_threadpool *diskworker) {
llarp_async_verify_job_context *context = new llarp_async_verify_job_context;
if (context)
{
context->cryptoworker = cryptoworker;
context->diskworker = diskworker;
}
return context;
}
void
llarp_async_verify_job_free(struct llarp_async_verify_job_context *context) {
delete context;
}
void
llarp_nodedb_async_verify(struct llarp_nodedb *nodedb,
struct llarp_logic *logic,
@ -262,5 +337,13 @@ llarp_nodedb_async_verify(struct llarp_nodedb *nodedb,
struct llarp_threadpool *diskworker,
struct llarp_async_verify_rc *job)
{
printf("llarp_nodedb_async_verify\n");
// set up context
llarp_async_verify_job_context *context = llarp_async_verify_job_new(
cryptoworker, diskworker);
// set up anything we need (in job)
job->context = context;
// queue the crypto check
llarp_threadpool_queue_job(cryptoworker, { job, &verify_rc });
}
}

@ -259,12 +259,11 @@ llarp_router::async_verify_RC(llarp_link_session *session,
bool isExpectingClient,
llarp_link_establish_job *establish_job)
{
llarp_async_verify_rc *job = new llarp_async_verify_rc{
new llarp::async_verify_context{this, establish_job},
{},
false,
nullptr,
};
llarp_async_verify_rc *job = new llarp_async_verify_rc;
job->user = new llarp::async_verify_context{this, establish_job};
job->rc = {};
job->valid = false;
job->hook = nullptr;
llarp_rc_copy(&job->rc, session->get_remote_router(session));
if(isExpectingClient)
job->hook = &llarp_router::on_verify_client_rc;

@ -123,6 +123,9 @@ llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buff)
bool
llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
{
// maybe we should copy rc before modifying it
// would that make it more thread safe?
// jeff agrees
bool result = false;
llarp_sig_t sig;
byte_t tmp[MAX_RC_SIZE];

Loading…
Cancel
Save