google format

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

@ -1,108 +0,0 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 2
UseTab: Never
...

@ -45,8 +45,8 @@ REQUIRED_LDFLAGS = $(LDFLAGS) -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc
all: build all: build
format: $(HDRS) $(SRCS) format:
$(FORMAT) -i $^ $(FORMAT) -style=Google -i $(HDRS) $(SRCS)
build: $(EXE) build: $(EXE)

@ -43,8 +43,7 @@ int shutdown_llarp(struct llarp_main *m) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct llarp_main llarp = {NULL, NULL, NULL, NULL}; struct llarp_main llarp = {NULL, NULL, NULL, NULL};
const char *conffname = "daemon.ini"; const char *conffname = "daemon.ini";
if (argc > 1) if (argc > 1) conffname = argv[1];
conffname = argv[1];
llarp_mem_jemalloc(); llarp_mem_jemalloc();
llarp_new_config(&llarp.config); llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop); llarp_ev_loop_alloc(&llarp.mainloop);
@ -55,8 +54,7 @@ int main(int argc, char *argv[]) {
iter.user = &llarp; iter.user = &llarp;
iter.visit = iter_main_config; iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter); llarp_config_iter(llarp.config, &iter);
if (!llarp.tp) if (!llarp.tp) llarp.tp = llarp_init_threadpool(2);
llarp.tp = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.tp); llarp.router = llarp_init_router(llarp.tp);
if (!llarp_configure_router(llarp.router, llarp.config)) { if (!llarp_configure_router(llarp.router, llarp.config)) {
printf("Running\n"); printf("Running\n");

@ -25,13 +25,13 @@ typedef uint8_t llarp_hmacsec_t[HMACSECSIZE];
typedef uint8_t llarp_sig_t[SIGSIZE]; typedef uint8_t llarp_sig_t[SIGSIZE];
typedef uint8_t llarp_tunnel_nounce_t[TUNNOUNCESIZE]; typedef uint8_t llarp_tunnel_nounce_t[TUNNOUNCESIZE];
struct llarp_keypair struct llarp_keypair {
{
llarp_pubkey_t pub; llarp_pubkey_t pub;
llarp_seckey_t sec; llarp_seckey_t sec;
}; };
typedef bool (*llarp_dh_func)(llarp_sharedkey_t *, llarp_pubkey_t, llarp_tunnel_nounce_t, llarp_seckey_t); typedef bool (*llarp_dh_func)(llarp_sharedkey_t *, llarp_pubkey_t,
llarp_tunnel_nounce_t, llarp_seckey_t);
struct llarp_crypto { struct llarp_crypto {
bool (*xchacha20)(llarp_buffer_t, llarp_sharedkey_t, llarp_nounce_t); bool (*xchacha20)(llarp_buffer_t, llarp_sharedkey_t, llarp_nounce_t);

@ -8,28 +8,34 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_async_dh; struct llarp_async_dh;
struct llarp_async_dh * llarp_async_dh_new(struct llarp_crypto * crypto, struct llarp_ev_loop * ev, struct llarp_threadpool * tp); struct llarp_async_dh *llarp_async_dh_new(struct llarp_crypto *crypto,
void llarp_async_dh_free(struct llarp_async_dh ** dh); struct llarp_ev_loop *ev,
struct llarp_threadpool *tp);
struct llarp_dh_result; void llarp_async_dh_free(struct llarp_async_dh **dh);
typedef void (*llarp_dh_complete_hook)(struct llarp_dh_result *); struct llarp_dh_result;
typedef void (*llarp_dh_complete_hook)(struct llarp_dh_result *);
struct llarp_dh_internal;
struct llarp_dh_internal;
struct llarp_dh_result
{ struct llarp_dh_result {
struct llarp_dh_internal * impl; struct llarp_dh_internal *impl;
llarp_sharedkey_t result; llarp_sharedkey_t result;
void * user; void *user;
llarp_dh_complete_hook hook; llarp_dh_complete_hook hook;
}; };
void llarp_async_client_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user); void llarp_async_client_dh(struct llarp_async_dh *dh, llarp_seckey_t ourkey,
void llarp_async_server_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user); llarp_pubkey_t theirkey,
llarp_tunnel_nounce_t nounce,
llarp_dh_complete_hook result, void *user);
void llarp_async_server_dh(struct llarp_async_dh *dh, llarp_seckey_t ourkey,
llarp_pubkey_t theirkey,
llarp_tunnel_nounce_t nounce,
llarp_dh_complete_hook result, void *user);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -34,33 +34,33 @@ int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener); int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener);
struct llarp_ev_async_call; struct llarp_ev_async_call;
typedef void (*llarp_ev_work_func)(struct llarp_ev_async_call *); typedef void (*llarp_ev_work_func)(struct llarp_ev_async_call *);
struct llarp_ev_caller;
struct llarp_ev_caller;
struct llarp_ev_async_call { struct llarp_ev_async_call {
/** the loop this job belongs to */ /** the loop this job belongs to */
const struct llarp_ev_loop *loop; const struct llarp_ev_loop *loop;
/** private implementation */ /** private implementation */
const struct llarp_ev_caller * parent; const struct llarp_ev_caller *parent;
/** user data */ /** user data */
const void *user; const void *user;
/** /**
work is called async when ready in the event loop thread work is called async when ready in the event loop thread
must not free from inside this call as it is done elsewhere must not free from inside this call as it is done elsewhere
*/ */
const llarp_ev_work_func work; const llarp_ev_work_func work;
}; };
struct llarp_ev_caller * llarp_ev_prepare_async(struct llarp_ev_loop *ev, llarp_ev_work_func func); struct llarp_ev_caller *llarp_ev_prepare_async(struct llarp_ev_loop *ev,
llarp_ev_work_func func);
bool llarp_ev_call_async(struct llarp_ev_caller *c, void *user);
bool llarp_ev_call_async(struct llarp_ev_caller * c, void * user); void llarp_ev_caller_stop(struct llarp_ev_caller *c);
void llarp_ev_caller_stop(struct llarp_ev_caller * c);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -24,8 +24,8 @@ void llarp_stop_router(struct llarp_router *router);
/** get router's inbound link level frame queue */ /** get router's inbound link level frame queue */
struct llarp_link_queue *llarp_router_link_queue(struct llarp_router *router); struct llarp_link_queue *llarp_router_link_queue(struct llarp_router *router);
/** get router's outbound link level frame dispatcher */ /** get router's outbound link level frame dispatcher */
struct llarp_link_dispatcher * struct llarp_link_dispatcher *llarp_router_link_dispatcher(
llarp_router_link_dispatcher(struct llarp_router *router); struct llarp_router *router);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -10,17 +10,16 @@ struct llarp_threadpool;
struct llarp_threadpool *llarp_init_threadpool(int workers); struct llarp_threadpool *llarp_init_threadpool(int workers);
void llarp_free_threadpool(struct llarp_threadpool **tp); void llarp_free_threadpool(struct llarp_threadpool **tp);
typedef void (*llarp_thread_work_func)(void*); typedef void (*llarp_thread_work_func)(void *);
/** job to be done in worker thread */ /** job to be done in worker thread */
struct llarp_thread_job { struct llarp_thread_job {
/** /**
called async after work is executed called async after work is executed
*/ */
struct llarp_ev_caller * caller; struct llarp_ev_caller *caller;
void * data; void *data;
/** user data to pass to work function */ /** user data to pass to work function */
void *user; void *user;
/** called in threadpool worker thread */ /** called in threadpool worker thread */

@ -15,11 +15,11 @@
#ifndef LLARP_VERSION_NUM #ifndef LLARP_VERSION_NUM
#ifdef GIT_REV #ifdef GIT_REV
#define LLARP_VERSION_NUM \ #define LLARP_VERSION_NUM \
"-" LLARP_VERSION_MAJ "." LLARP_VERSION_MIN "." LLARP_VERSION_PATCH \ "-" LLARP_VERSION_MAJ "." LLARP_VERSION_MIN "." LLARP_VERSION_PATCH \
"-" GIT_REV "-" GIT_REV
#else #else
#define LLARP_VERSION_NUM \ #define LLARP_VERSION_NUM \
"-" LLARP_VERSION_MAJ "." LLARP_VERSION_MIN "." LLARP_VERSION_PATCH "-" LLARP_VERSION_MAJ "." LLARP_VERSION_MIN "." LLARP_VERSION_PATCH
#endif #endif
#endif #endif

@ -4,7 +4,7 @@
namespace llarp { namespace llarp {
static void *ai_alloc(size_t sz, size_t align) {} static void *ai_alloc(size_t sz, size_t align) {}
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
@ -19,8 +19,7 @@ void llarp_ai_list_iterate(struct llarp_ai_list *l,
itr->list = l; itr->list = l;
struct llarp_ai_list *cur = l; struct llarp_ai_list *cur = l;
do { do {
if (!itr->visit(itr, cur->data)) if (!itr->visit(itr, cur->data)) return;
return;
cur = cur->next; cur = cur->next;
} while (cur->next); } while (cur->next);
} }
@ -36,4 +35,4 @@ bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) {
bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) && bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) &&
bencodeEnd(buff); bencodeEnd(buff);
} }
} // namespace llarp } // namespace llarp

@ -1,8 +1,8 @@
#ifndef LLARP_AI_HPP #ifndef LLARP_AI_HPP
#define LLARP_AI_HPP #define LLARP_AI_HPP
#include <llarp/address_info.h>
#include <cstring> #include <cstring>
#include <list> #include <list>
#include <llarp/address_info.h>
struct llarp_ai_list { struct llarp_ai_list {
llarp_ai *data; llarp_ai *data;

@ -1,14 +1,15 @@
#ifndef LLARP_BENCODE_HPP #ifndef LLARP_BENCODE_HPP
#define LLARP_BENCODE_HPP #define LLARP_BENCODE_HPP
#include <list>
#include <llarp/buffer.h> #include <llarp/buffer.h>
#include <list>
#include <string> #include <string>
namespace llarp { namespace llarp {
template <typename ValType> template <typename ValType>
bool BEncode(const ValType &t, llarp_buffer_t *buff); bool BEncode(const ValType &t, llarp_buffer_t *buff);
template <typename ValType> bool BDecode(ValType &t, llarp_buffer_t *buff); template <typename ValType>
bool BDecode(ValType &t, llarp_buffer_t *buff);
static bool bencodeDict(llarp_buffer_t *buff) { static bool bencodeDict(llarp_buffer_t *buff) {
static uint8_t c = 'd'; static uint8_t c = 'd';
@ -50,12 +51,11 @@ template <typename T>
bool BEncode(const std::list<T> &l, llarp_buffer_t *buff) { bool BEncode(const std::list<T> &l, llarp_buffer_t *buff) {
if (bencodeList(buff)) { if (bencodeList(buff)) {
for (const auto &itr : l) for (const auto &itr : l)
if (!BEncode(itr, buff)) if (!BEncode(itr, buff)) return false;
return false;
return bencodeEnd(buff); return bencodeEnd(buff);
} }
return false; return false;
} }
} // namespace llarp } // namespace llarp
#endif #endif

@ -1,15 +1,14 @@
#include "config.hpp" #include "config.hpp"
#include <llarp/config.h>
#include "ini.hpp" #include "ini.hpp"
#include "mem.hpp" #include "mem.hpp"
#include <llarp/config.h>
namespace llarp { namespace llarp {
template <typename Config, typename Section> template <typename Config, typename Section>
static Section find_section(Config &c, const std::string &name, static Section find_section(Config &c, const std::string &name,
const Section &fallback) { const Section &fallback) {
if (c.sections.find(name) == c.sections.end()) if (c.sections.find(name) == c.sections.end()) return fallback;
return fallback;
return c.sections[name].values; return c.sections[name].values;
} }
@ -28,7 +27,7 @@ bool Config::Load(const char *fname) {
return false; return false;
}; };
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
@ -38,14 +37,12 @@ void llarp_new_config(struct llarp_config **conf) {
} }
void llarp_free_config(struct llarp_config **conf) { void llarp_free_config(struct llarp_config **conf) {
if (*conf) if (*conf) delete *conf;
delete *conf;
*conf = nullptr; *conf = nullptr;
} }
int llarp_load_config(struct llarp_config *conf, const char *fname) { int llarp_load_config(struct llarp_config *conf, const char *fname) {
if (!conf->impl.Load(fname)) if (!conf->impl.Load(fname)) return -1;
return -1;
return 0; return 0;
} }

@ -16,7 +16,7 @@ struct Config {
bool Load(const char *fname); bool Load(const char *fname);
}; };
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
struct llarp_config { struct llarp_config {

@ -2,16 +2,14 @@
#include <llarp/mem.h> #include <llarp/mem.h>
#include <string.h> #include <string.h>
struct llarp_async_dh struct llarp_async_dh {
{
llarp_dh_func client; llarp_dh_func client;
llarp_dh_func server; llarp_dh_func server;
struct llarp_threadpool * tp; struct llarp_threadpool *tp;
struct llarp_ev_caller * caller; struct llarp_ev_caller *caller;
}; };
struct llarp_dh_internal struct llarp_dh_internal {
{
llarp_dh_func func; llarp_dh_func func;
llarp_pubkey_t theirkey; llarp_pubkey_t theirkey;
llarp_seckey_t ourkey; llarp_seckey_t ourkey;
@ -19,22 +17,23 @@ struct llarp_dh_internal
struct llarp_dh_result result; struct llarp_dh_result result;
}; };
static void llarp_crypto_dh_work(void * user) static void llarp_crypto_dh_work(void *user) {
{ struct llarp_dh_internal *impl = (struct llarp_dh_internal *)user;
struct llarp_dh_internal * impl = (struct llarp_dh_internal *)user;
impl->func(&impl->result.result, impl->theirkey, impl->nounce, impl->ourkey); impl->func(&impl->result.result, impl->theirkey, impl->nounce, impl->ourkey);
} }
static void llarp_crypto_dh_result(struct llarp_ev_async_call * call) static void llarp_crypto_dh_result(struct llarp_ev_async_call *call) {
{ struct llarp_dh_internal *impl = (struct llarp_dh_internal *)call->user;
struct llarp_dh_internal * impl = (struct llarp_dh_internal *) call->user;
impl->result.hook(&impl->result); impl->result.hook(&impl->result);
llarp_g_mem.free(impl); llarp_g_mem.free(impl);
} }
static void llarp_async_dh_exec(struct llarp_async_dh * dh, llarp_dh_func func, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user) static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func,
{ llarp_seckey_t ourkey, llarp_pubkey_t theirkey,
struct llarp_dh_internal * impl = llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 16); llarp_tunnel_nounce_t nounce,
llarp_dh_complete_hook result, void *user) {
struct llarp_dh_internal *impl =
llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 16);
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t)); memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
memcpy(impl->ourkey, ourkey, sizeof(llarp_seckey_t)); memcpy(impl->ourkey, ourkey, sizeof(llarp_seckey_t));
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t)); memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
@ -42,31 +41,32 @@ static void llarp_async_dh_exec(struct llarp_async_dh * dh, llarp_dh_func func,
impl->result.user = user; impl->result.user = user;
impl->result.hook = result; impl->result.hook = result;
impl->func = func; impl->func = func;
struct llarp_thread_job job = { struct llarp_thread_job job = {.caller = dh->caller,
.caller = dh->caller, .data = impl,
.data = impl, .user = impl,
.user = impl, .work = &llarp_crypto_dh_work};
.work = &llarp_crypto_dh_work
};
llarp_threadpool_queue_job(dh->tp, job); llarp_threadpool_queue_job(dh->tp, job);
} }
void llarp_async_client_dh(struct llarp_async_dh *dh, llarp_seckey_t ourkey,
void llarp_async_client_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user) llarp_pubkey_t theirkey,
{ llarp_tunnel_nounce_t nounce,
llarp_dh_complete_hook result, void *user) {
llarp_async_dh_exec(dh, dh->client, ourkey, theirkey, nounce, result, user); llarp_async_dh_exec(dh, dh->client, ourkey, theirkey, nounce, result, user);
} }
void llarp_async_server_dh(struct llarp_async_dh * dh, llarp_seckey_t ourkey, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void * user) void llarp_async_server_dh(struct llarp_async_dh *dh, llarp_seckey_t ourkey,
{ llarp_pubkey_t theirkey,
llarp_tunnel_nounce_t nounce,
llarp_dh_complete_hook result, void *user) {
llarp_async_dh_exec(dh, dh->server, ourkey, theirkey, nounce, result, user); llarp_async_dh_exec(dh, dh->server, ourkey, theirkey, nounce, result, user);
} }
struct llarp_async_dh *llarp_async_dh_new(struct llarp_crypto *crypto,
struct llarp_ev_loop *ev,
struct llarp_async_dh * llarp_async_dh_new(struct llarp_crypto * crypto, struct llarp_ev_loop * ev, struct llarp_threadpool * tp) struct llarp_threadpool *tp) {
{ struct llarp_async_dh *dh =
struct llarp_async_dh * dh = llarp_g_mem.alloc(sizeof(struct llarp_async_dh), 16); llarp_g_mem.alloc(sizeof(struct llarp_async_dh), 16);
dh->client = crypto->dh_client; dh->client = crypto->dh_client;
dh->server = crypto->dh_server; dh->server = crypto->dh_server;
dh->tp = tp; dh->tp = tp;
@ -74,10 +74,8 @@ struct llarp_async_dh * llarp_async_dh_new(struct llarp_crypto * crypto, struct
return dh; return dh;
} }
void llarp_async_dh_free(struct llarp_async_dh ** dh) void llarp_async_dh_free(struct llarp_async_dh **dh) {
{ if (*dh) {
if(*dh)
{
llarp_ev_caller_stop((*dh)->caller); llarp_ev_caller_stop((*dh)->caller);
llarp_g_mem.free(*dh); llarp_g_mem.free(*dh);
*dh = NULL; *dh = NULL;

@ -1,21 +1,21 @@
#include <assert.h>
#include <llarp/crypto.h> #include <llarp/crypto.h>
#include <sodium.h> #include <sodium.h>
#include <assert.h>
namespace llarp { namespace llarp {
namespace sodium { namespace sodium {
static bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n) { static bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k,
llarp_nounce_t n) {
uint8_t *base = (uint8_t *)buff.base; uint8_t *base = (uint8_t *)buff.base;
return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0; return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
} }
static bool dh(llarp_sharedkey_t *shared, uint8_t *client_pk, uint8_t *server_pk, static bool dh(llarp_sharedkey_t *shared, uint8_t *client_pk,
uint8_t *remote_key, uint8_t *local_key) { uint8_t *server_pk, uint8_t *remote_key, uint8_t *local_key) {
uint8_t *out = *shared; uint8_t *out = *shared;
const size_t outsz = SHAREDKEYSIZE; const size_t outsz = SHAREDKEYSIZE;
crypto_generichash_state h; crypto_generichash_state h;
if (crypto_scalarmult(out, local_key, remote_key) == -1) if (crypto_scalarmult(out, local_key, remote_key) == -1) return false;
return false;
crypto_generichash_init(&h, NULL, 0U, outsz); crypto_generichash_init(&h, NULL, 0U, outsz);
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t)); crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t)); crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
@ -25,7 +25,7 @@ static bool dh(llarp_sharedkey_t *shared, uint8_t *client_pk, uint8_t *server_pk
} }
static bool dh_client(llarp_sharedkey_t *shared, llarp_pubkey_t pk, static bool dh_client(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
llarp_tunnel_nounce_t n, llarp_seckey_t sk) { llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
llarp_pubkey_t local_pk; llarp_pubkey_t local_pk;
crypto_scalarmult_base(local_pk, sk); crypto_scalarmult_base(local_pk, sk);
if (dh(shared, local_pk, pk, pk, sk)) { if (dh(shared, local_pk, pk, pk, sk)) {
@ -36,7 +36,7 @@ static bool dh_client(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
} }
static bool dh_server(llarp_sharedkey_t *shared, llarp_pubkey_t pk, static bool dh_server(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
llarp_tunnel_nounce_t n, llarp_seckey_t sk) { llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
llarp_pubkey_t local_pk; llarp_pubkey_t local_pk;
crypto_scalarmult_base(local_pk, sk); crypto_scalarmult_base(local_pk, sk);
if (dh(shared, pk, local_pk, pk, sk)) { if (dh(shared, pk, local_pk, pk, sk)) {
@ -51,13 +51,15 @@ static bool hash(llarp_hash_t *result, llarp_buffer_t buff) {
return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1; return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1;
} }
static bool hmac(llarp_hash_t *result, llarp_buffer_t buff, llarp_seckey_t secret) { static bool hmac(llarp_hash_t *result, llarp_buffer_t buff,
llarp_seckey_t secret) {
const uint8_t *base = (const uint8_t *)buff.base; const uint8_t *base = (const uint8_t *)buff.base;
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz, return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz,
secret, HMACSECSIZE) != -1; secret, HMACSECSIZE) != -1;
} }
static bool sign(llarp_sig_t *result, llarp_seckey_t secret, llarp_buffer_t buff) { static bool sign(llarp_sig_t *result, llarp_seckey_t secret,
llarp_buffer_t buff) {
const uint8_t *base = (const uint8_t *)buff.base; const uint8_t *base = (const uint8_t *)buff.base;
return crypto_sign_detached(*result, nullptr, base, buff.sz, secret) != -1; return crypto_sign_detached(*result, nullptr, base, buff.sz, secret) != -1;
} }
@ -67,19 +69,17 @@ static bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) {
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1; return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
} }
static void randomize(llarp_buffer_t buff) static void randomize(llarp_buffer_t buff) {
{ randombytes((unsigned char *)buff.base, buff.sz);
randombytes((unsigned char *)buff.base, buff.sz); }
}
static void keygen(struct llarp_keypair * keys) static void keygen(struct llarp_keypair *keys) {
{ randombytes(keys->sec, sizeof(llarp_seckey_t));
randombytes(keys->sec, sizeof(llarp_seckey_t)); unsigned char sk[64];
unsigned char sk[64]; crypto_sign_seed_keypair(keys->pub, sk, keys->sec);
crypto_sign_seed_keypair(keys->pub, sk, keys->sec); }
} } // namespace sodium
} // namespace sodium } // namespace llarp
} // namespace llarp
extern "C" { extern "C" {
void llarp_crypto_libsodium_init(struct llarp_crypto *c) { void llarp_crypto_libsodium_init(struct llarp_crypto *c) {

@ -1,65 +1,48 @@
#include "mem.hpp"
#include <llarp/ev.h> #include <llarp/ev.h>
#include <uv.h> #include <uv.h>
#include "mem.hpp"
#include <mutex> #include <mutex>
#include <queue> #include <queue>
struct llarp_ev_caller struct llarp_ev_caller {
{ static void *operator new(size_t sz) {
static void * operator new(size_t sz)
{
return llarp::Alloc<llarp_ev_caller>(); return llarp::Alloc<llarp_ev_caller>();
} }
static void operator delete(void * ptr) static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
{
llarp_g_mem.free(ptr);
}
llarp_ev_caller(llarp_ev_loop * ev, llarp_ev_work_func func) : llarp_ev_caller(llarp_ev_loop *ev, llarp_ev_work_func func)
loop(ev), : loop(ev), work(func) {
work(func)
{
async.data = this; async.data = this;
} }
~llarp_ev_caller() ~llarp_ev_caller() {}
{
}
bool appendCall(void * user) bool appendCall(void *user) {
{
std::unique_lock<std::mutex> lock(access); std::unique_lock<std::mutex> lock(access);
bool should = pending.size() == 0; bool should = pending.size() == 0;
llarp_ev_async_call * call = new llarp_ev_async_call{ llarp_ev_async_call *call =
loop, new llarp_ev_async_call{loop, this, user, this->work};
this,
user,
this->work};
pending.push(call); pending.push(call);
return should; return should;
} }
void Call() void Call() {
{
std::unique_lock<std::mutex> lock(access); std::unique_lock<std::mutex> lock(access);
while(pending.size() > 0) while (pending.size() > 0) {
{ auto &front = pending.front();
auto & front = pending.front();
front->work(front); front->work(front);
pending.pop(); pending.pop();
} }
} }
std::mutex access; std::mutex access;
struct llarp_ev_loop * loop; struct llarp_ev_loop *loop;
uv_async_t async; uv_async_t async;
std::queue<llarp_ev_async_call *> pending; std::queue<llarp_ev_async_call *> pending;
llarp_ev_work_func work; llarp_ev_work_func work;
}; };
struct llarp_ev_loop { struct llarp_ev_loop {
uv_loop_t _loop; uv_loop_t _loop;
@ -85,14 +68,12 @@ struct udp_listener {
struct llarp_udp_listener *listener; struct llarp_udp_listener *listener;
void recvfrom(const struct sockaddr *addr, char *buff, ssize_t sz) { void recvfrom(const struct sockaddr *addr, char *buff, ssize_t sz) {
if (listener->recvfrom) if (listener->recvfrom) listener->recvfrom(listener, addr, buff, sz);
listener->recvfrom(listener, addr, buff, sz);
} }
/** called after closed */ /** called after closed */
void closed() { void closed() {
if (listener->closed) if (listener->closed) listener->closed(listener);
listener->closed(listener);
listener->impl = nullptr; listener->impl = nullptr;
} }
@ -116,7 +97,7 @@ static void udp_close_cb(uv_handle_t *handle) {
l->closed(); l->closed();
delete l; delete l;
} }
} // namespace llarp } // namespace llarp
namespace llarp { namespace llarp {
@ -126,10 +107,10 @@ static void ev_caller_async_closed(uv_handle_t *handle) {
} }
static void ev_handle_async_call(uv_async_t *handle) { static void ev_handle_async_call(uv_async_t *handle) {
llarp_ev_caller * caller = static_cast<llarp_ev_caller *>(handle->data); llarp_ev_caller *caller = static_cast<llarp_ev_caller *>(handle->data);
caller->Call(); caller->Call();
} }
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
void llarp_ev_loop_alloc(struct llarp_ev_loop **ev) { void llarp_ev_loop_alloc(struct llarp_ev_loop **ev) {
@ -189,29 +170,26 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) {
void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { uv_stop(loop->loop()); } void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { uv_stop(loop->loop()); }
struct llarp_ev_caller * llarp_ev_prepare_async(struct llarp_ev_loop * loop, llarp_ev_work_func work) struct llarp_ev_caller *llarp_ev_prepare_async(struct llarp_ev_loop *loop,
{ llarp_ev_work_func work) {
llarp_ev_caller * caller = new llarp_ev_caller(loop, work); llarp_ev_caller *caller = new llarp_ev_caller(loop, work);
if(uv_async_init(loop->loop(), &caller->async, llarp::ev_handle_async_call) == 0) if (uv_async_init(loop->loop(), &caller->async,
llarp::ev_handle_async_call) == 0)
return caller; return caller;
else else {
{
delete caller; delete caller;
return nullptr; return nullptr;
} }
} }
bool llarp_ev_call_async(struct llarp_ev_caller * caller, void * user) bool llarp_ev_call_async(struct llarp_ev_caller *caller, void *user) {
{ if (caller->appendCall(user))
if(caller->appendCall(user))
return uv_async_send(&caller->async) == 0; return uv_async_send(&caller->async) == 0;
else else
return true; return true;
} }
void llarp_ev_caller_stop(struct llarp_ev_caller * caller) void llarp_ev_caller_stop(struct llarp_ev_caller *caller) {
{ uv_close((uv_handle_t *)&caller->async, llarp::ev_caller_async_closed);
uv_close((uv_handle_t*)&caller->async, llarp::ev_caller_async_closed);
} }
} }

@ -14,4 +14,4 @@ bool BEncode(const llarp_xi &xi, llarp_buffer_t *buff) {
bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) && bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) &&
bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff); bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff);
} }
} // namespace llarp } // namespace llarp

@ -1,7 +1,7 @@
#ifndef LLARP_XI_HPP #ifndef LLARP_XI_HPP
#define LLARP_XI_HPP #define LLARP_XI_HPP
#include <list>
#include <llarp/exit_info.h> #include <llarp/exit_info.h>
#include <list>
struct llarp_xi_list { struct llarp_xi_list {
std::list<llarp_xi> list; std::list<llarp_xi> list;
@ -10,8 +10,7 @@ struct llarp_xi_list {
static std::list<llarp_xi> xi_list_to_std(struct llarp_xi_list *l) { static std::list<llarp_xi> xi_list_to_std(struct llarp_xi_list *l) {
std::list<llarp_xi> list; std::list<llarp_xi> list;
if (l->list.size()) if (l->list.size())
for (const auto &xi : l->list) for (const auto &xi : l->list) list.push_back(xi);
list.push_back(xi);
return list; return list;
} }

@ -46,7 +46,7 @@ struct Level {
typedef std::list<section_map_t::const_iterator> sections_t; typedef std::list<section_map_t::const_iterator> sections_t;
value_map_t values; value_map_t values;
section_map_t sections; section_map_t sections;
values_t ordered_values; // original order in the ini file values_t ordered_values; // original order in the ini file
sections_t ordered_sections; sections_t ordered_sections;
Level *parent; Level *parent;
size_t depth; size_t depth;
@ -58,20 +58,20 @@ struct Level {
}; };
class Parser { class Parser {
public: public:
Parser(const std::string &fname) : Parser(fname.c_str()) {} Parser(const std::string &fname) : Parser(fname.c_str()) {}
Parser(const char *fn); Parser(const char *fn);
Parser(std::istream &f) : f_(&f), ln_(0) { parse(top_); } Parser(std::istream &f) : f_(&f), ln_(0) { parse(top_); }
Level &top() { return top_; } Level &top() { return top_; }
void dump(std::ostream &s) { dump(s, top(), ""); } void dump(std::ostream &s) { dump(s, top(), ""); }
private: private:
void dump(std::ostream &s, const Level &l, const std::string &sname); void dump(std::ostream &s, const Level &l, const std::string &sname);
void parse(Level &l); void parse(Level &l);
void parseSLine(std::string &sname, size_t &depth); void parseSLine(std::string &sname, size_t &depth);
void err(const char *s); void err(const char *s);
private: private:
Level top_; Level top_;
std::ifstream f0_; std::ifstream f0_;
std::istream *f_; std::istream *f_;
@ -90,17 +90,14 @@ inline std::string trim(const std::string &s) {
long sp = 0; long sp = 0;
long ep = s.length() - 1; long ep = s.length() - 1;
for (; sp <= ep; ++sp) for (; sp <= ep; ++sp)
if (!strchr(p, s[sp])) if (!strchr(p, s[sp])) break;
break;
for (; ep >= 0; --ep) for (; ep >= 0; --ep)
if (!strchr(p, s[ep])) if (!strchr(p, s[ep])) break;
break;
return s.substr(sp, ep - sp + 1); return s.substr(sp, ep - sp + 1);
} }
inline Parser::Parser(const char *fn) : f0_(fn), f_(&f0_), ln_(0) { inline Parser::Parser(const char *fn) : f0_(fn), f_(&f0_), ln_(0) {
if (!f0_) if (!f0_) throw std::runtime_error(std::string("failed to open file: ") + fn);
throw std::runtime_error(std::string("failed to open file: ") + fn);
parse(top_); parse(top_);
} }
@ -108,8 +105,7 @@ inline Parser::Parser(const char *fn) : f0_(fn), f_(&f0_), ln_(0) {
inline void Parser::parseSLine(std::string &sname, size_t &depth) { inline void Parser::parseSLine(std::string &sname, size_t &depth) {
depth = 0; depth = 0;
for (; depth < line_.length(); ++depth) for (; depth < line_.length(); ++depth)
if (line_[depth] != '[') if (line_[depth] != '[') break;
break;
sname = line_.substr(depth, line_.length() - 2 * depth); sname = line_.substr(depth, line_.length() - 2 * depth);
} }
@ -117,31 +113,26 @@ inline void Parser::parseSLine(std::string &sname, size_t &depth) {
inline void Parser::parse(Level &l) { inline void Parser::parse(Level &l) {
while (std::getline(*f_, line_)) { while (std::getline(*f_, line_)) {
++ln_; ++ln_;
if (line_[0] == '#' || line_[0] == ';') if (line_[0] == '#' || line_[0] == ';') continue;
continue;
line_ = trim(line_); line_ = trim(line_);
if (line_.empty()) if (line_.empty()) continue;
continue;
if (line_[0] == '[') { if (line_[0] == '[') {
size_t depth; size_t depth;
std::string sname; std::string sname;
parseSLine(sname, depth); parseSLine(sname, depth);
Level *lp = NULL; Level *lp = NULL;
Level *parent = &l; Level *parent = &l;
if (depth > l.depth + 1) if (depth > l.depth + 1) err("section with wrong depth");
err("section with wrong depth");
if (l.depth == depth - 1) if (l.depth == depth - 1)
lp = &l.sections[sname]; lp = &l.sections[sname];
else { else {
lp = l.parent; lp = l.parent;
size_t n = l.depth - depth; size_t n = l.depth - depth;
for (size_t i = 0; i < n; ++i) for (size_t i = 0; i < n; ++i) lp = lp->parent;
lp = lp->parent;
parent = lp; parent = lp;
lp = &lp->sections[sname]; lp = &lp->sections[sname];
} }
if (lp->depth != 0) if (lp->depth != 0) err("duplicate section name on the same level");
err("duplicate section name on the same level");
if (!lp->parent) { if (!lp->parent) {
lp->depth = depth; lp->depth = depth;
lp->parent = parent; lp->parent = parent;
@ -150,13 +141,11 @@ inline void Parser::parse(Level &l) {
parse(*lp); parse(*lp);
} else { } else {
size_t n = line_.find('='); size_t n = line_.find('=');
if (n == std::string::npos) if (n == std::string::npos) err("no '=' found");
err("no '=' found");
std::pair<Level::value_map_t::const_iterator, bool> res = l.values.insert( std::pair<Level::value_map_t::const_iterator, bool> res = l.values.insert(
std::make_pair(trim(line_.substr(0, n)), std::make_pair(trim(line_.substr(0, n)),
trim(line_.substr(n + 1, line_.length() - n - 1)))); trim(line_.substr(n + 1, line_.length() - n - 1))));
if (!res.second) if (!res.second) err("duplicated key found");
err("duplicated key found");
l.ordered_values.push_back(res.first); l.ordered_values.push_back(res.first);
} }
} }
@ -164,16 +153,11 @@ inline void Parser::parse(Level &l) {
inline void Parser::dump(std::ostream &s, const Level &l, inline void Parser::dump(std::ostream &s, const Level &l,
const std::string &sname) { const std::string &sname) {
if (!sname.empty()) if (!sname.empty()) s << '\n';
s << '\n'; for (size_t i = 0; i < l.depth; ++i) s << '[';
for (size_t i = 0; i < l.depth; ++i) if (!sname.empty()) s << sname;
s << '['; for (size_t i = 0; i < l.depth; ++i) s << ']';
if (!sname.empty()) if (!sname.empty()) s << std::endl;
s << sname;
for (size_t i = 0; i < l.depth; ++i)
s << ']';
if (!sname.empty())
s << std::endl;
for (Level::values_t::const_iterator it = l.ordered_values.begin(); for (Level::values_t::const_iterator it = l.ordered_values.begin();
it != l.ordered_values.end(); ++it) it != l.ordered_values.end(); ++it)
s << (*it)->first << '=' << (*it)->second << std::endl; s << (*it)->first << '=' << (*it)->second << std::endl;
@ -184,6 +168,6 @@ inline void Parser::dump(std::ostream &s, const Level &l,
} }
} }
} // namespace ini } // namespace ini
#endif // INI_HPP #endif // INI_HPP

@ -1,6 +1,6 @@
#include "link.hpp" #include "link.hpp"
#include <cstring>
#include <llarp/time.h> #include <llarp/time.h>
#include <cstring>
bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) { bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) {
return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr, return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr,
@ -14,8 +14,7 @@ struct llarp_link *llarp_link_alloc() {
} }
void llarp_link_free(struct llarp_link **l) { void llarp_link_free(struct llarp_link **l) {
if (*l) if (*l) delete *l;
delete *l;
*l = nullptr; *l = nullptr;
} }

@ -1,15 +1,15 @@
#ifndef LLARP_LINK_HPP #ifndef LLARP_LINK_HPP
#define LLARP_LINK_HPP #define LLARP_LINK_HPP
#include <llarp/crypto.h>
#include <netinet/in.h>
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <llarp/crypto.h>
#include <map> #include <map>
#include <memory> #include <memory>
#include <netinet/in.h>
#include "mem.hpp"
#include <llarp/ev.h> #include <llarp/ev.h>
#include <llarp/router_contact.h> #include <llarp/router_contact.h>
#include "mem.hpp"
struct llarp_link { struct llarp_link {
static void *operator new(size_t sz) { static void *operator new(size_t sz) {

@ -1,18 +1,17 @@
#ifndef LLARP_MEM_HPP #ifndef LLARP_MEM_HPP
#define LLARP_MEM_HPP #define LLARP_MEM_HPP
#include <cmath>
#include <llarp/mem.h> #include <llarp/mem.h>
#include <cmath>
namespace llarp { namespace llarp {
template <typename T> static constexpr size_t alignment() { template <typename T>
static constexpr size_t alignment() {
return std::exp2(1 + std::floor(std::log2(sizeof(T)))); return std::exp2(1 + std::floor(std::log2(sizeof(T))));
} }
template<typename T> template <typename T>
static T * Alloc(llarp_alloc * mem=&llarp_g_mem) static T *Alloc(llarp_alloc *mem = &llarp_g_mem) {
{ return static_cast<T *>(mem->alloc(sizeof(T), alignment<T>()));
return static_cast<T *>( }
mem->alloc(sizeof(T), alignment<T>())); } // namespace llarp
}
} // namespace llarp
#endif #endif

@ -7,10 +7,9 @@ static void *jem_malloc(size_t sz, size_t align) {
} }
static void jem_free(void *ptr) { static void jem_free(void *ptr) {
if (ptr) if (ptr) free(ptr);
free(ptr);
} }
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
void llarp_mem_jemalloc() { void llarp_mem_jemalloc() {

@ -4,17 +4,15 @@ namespace llarp {
void *std_malloc(size_t sz, size_t align) { void *std_malloc(size_t sz, size_t align) {
(void)align; (void)align;
void *ptr = malloc(sz); void *ptr = malloc(sz);
if (ptr) if (ptr) return ptr;
return ptr;
abort(); abort();
} }
void std_free(void *ptr) { void std_free(void *ptr) {
if (ptr) if (ptr) free(ptr);
free(ptr);
} }
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
void llarp_mem_std() { void llarp_mem_std() {

@ -1,8 +1,8 @@
#include <llarp/link.h>
#include <llarp/router.h>
#include "link.hpp" #include "link.hpp"
#include "mem.hpp" #include "mem.hpp"
#include "str.hpp" #include "str.hpp"
#include <llarp/link.h>
#include <llarp/router.h>
namespace llarp { namespace llarp {
void router_iter_config(llarp_config_iterator *iter, const char *section, void router_iter_config(llarp_config_iterator *iter, const char *section,
@ -12,7 +12,7 @@ struct router_links {
struct router_links *next = nullptr; struct router_links *next = nullptr;
}; };
} // namespace llarp } // namespace llarp
struct llarp_router { struct llarp_router {
struct llarp_threadpool *tp; struct llarp_threadpool *tp;
@ -27,8 +27,7 @@ struct llarp_router {
void AddLink(struct llarp_link *link) { void AddLink(struct llarp_link *link) {
llarp::router_links *head = &links; llarp::router_links *head = &links;
while (head->next && head->link) while (head->next && head->link) head = head->next;
head = head->next;
if (head->link) if (head->link)
head->next = new llarp::router_links{link, nullptr}; head->next = new llarp::router_links{link, nullptr};
@ -39,8 +38,7 @@ struct llarp_router {
void ForEachLink(std::function<void(llarp_link *)> visitor) { void ForEachLink(std::function<void(llarp_link *)> visitor) {
llarp::router_links *cur = &links; llarp::router_links *cur = &links;
do { do {
if (cur->link) if (cur->link) visitor(cur->link);
visitor(cur->link);
cur = cur->next; cur = cur->next;
} while (cur); } while (cur);
} }
@ -102,4 +100,4 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
} }
} }
} }
} // namespace llarp } // namespace llarp

@ -3,7 +3,11 @@
#include "bencode.hpp" #include "bencode.hpp"
#include "exit_info.hpp" #include "exit_info.hpp"
extern "C" {} extern "C" {
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
return llarp::BEncode(*rc, buff);
}
}
namespace llarp { namespace llarp {
bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) { bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
@ -17,4 +21,4 @@ bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) && bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
bencodeEnd(buff); bencodeEnd(buff);
} }
} // namespace llarp } // namespace llarp

@ -16,11 +16,10 @@ static size_t UStrLen(const uint8_t *data, size_t maxsz) {
size_t sz = 0; size_t sz = 0;
while (*data++) { while (*data++) {
sz++; sz++;
if (maxsz >= sz) if (maxsz >= sz) return maxsz;
return maxsz;
} }
return sz; return sz;
} }
} // namespace llarp } // namespace llarp
#endif #endif

@ -4,25 +4,23 @@
namespace llarp { namespace llarp {
namespace thread { namespace thread {
Pool::Pool(size_t workers) { Pool::Pool(size_t workers) {
stop.store(false); stop = false;
while (workers--) { while (workers--) {
threads.emplace_back([this] { threads.emplace_back([this] {
llarp_thread_job job;
for (;;) { for (;;) {
llarp_thread_job job;
{ {
lock_t lock(this->queue_mutex); lock_t lock(this->queue_mutex);
this->condition.wait( this->condition.wait(
lock, [this] { return this->stop || !this->jobs.empty(); }); lock, [this] { return this->stop || !this->jobs.empty(); });
if (this->stop && this->jobs.empty()) if (this->stop && this->jobs.empty()) return;
return;
job = std::move(this->jobs.front()); job = std::move(this->jobs.front());
this->jobs.pop(); this->jobs.pop();
} }
// do work // do work
job.work(job.user); job.work(job.user);
// inform result if needed // inform result if needed
if (job.caller) if (job.caller) {
{
if (!llarp_ev_call_async(job.caller, job.data)) { if (!llarp_ev_call_async(job.caller, job.data)) {
std::cerr << "failed to queue result in thread worker" << std::endl; std::cerr << "failed to queue result in thread worker" << std::endl;
} }
@ -35,11 +33,10 @@ Pool::Pool(size_t workers) {
void Pool::Join() { void Pool::Join() {
{ {
lock_t lock(queue_mutex); lock_t lock(queue_mutex);
stop.store(true); stop = true;
} }
condition.notify_all(); condition.notify_all();
for (auto &t : threads) for (auto &t : threads) t.join();
t.join();
} }
void Pool::QueueJob(llarp_thread_job job) { void Pool::QueueJob(llarp_thread_job job) {
@ -47,16 +44,15 @@ void Pool::QueueJob(llarp_thread_job job) {
lock_t lock(queue_mutex); lock_t lock(queue_mutex);
// don't allow enqueueing after stopping the pool // don't allow enqueueing after stopping the pool
if (stop) if (stop) throw std::runtime_error("enqueue on stopped ThreadPool");
throw std::runtime_error("enqueue on stopped ThreadPool");
jobs.emplace(job); jobs.emplace(job);
} }
condition.notify_one(); condition.notify_one();
} }
} // namespace thread } // namespace thread
} // namespace llarp } // namespace llarp
struct llarp_threadpool { struct llarp_threadpool {
llarp::thread::Pool impl; llarp::thread::Pool impl;
@ -75,16 +71,14 @@ struct llarp_threadpool *llarp_init_threadpool(int workers) {
void llarp_threadpool_join(struct llarp_threadpool *pool) { pool->impl.Join(); } void llarp_threadpool_join(struct llarp_threadpool *pool) { pool->impl.Join(); }
void llarp_threadpool_start(struct llarp_threadpool * pool) void llarp_threadpool_start(struct llarp_threadpool *pool) { /** no op */
{ }
/** no op */
void llarp_threadpool_queue_job(struct llarp_threadpool *pool,
struct llarp_thread_job job) {
pool->impl.QueueJob(job);
} }
void llarp_threadpool_queue_job(struct llarp_threadpool * pool, struct llarp_thread_job job)
{
pool->impl.QueueJob(job);
}
void llarp_free_threadpool(struct llarp_threadpool **pool) { void llarp_free_threadpool(struct llarp_threadpool **pool) {
delete *pool; delete *pool;
*pool = nullptr; *pool = nullptr;

@ -3,7 +3,6 @@
#include <llarp/threadpool.h> #include <llarp/threadpool.h>
#include <atomic>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <queue> #include <queue>
@ -15,7 +14,6 @@ namespace thread {
typedef std::mutex mtx_t; typedef std::mutex mtx_t;
typedef std::unique_lock<mtx_t> lock_t; typedef std::unique_lock<mtx_t> lock_t;
struct Pool { struct Pool {
Pool(size_t sz); Pool(size_t sz);
void QueueJob(llarp_thread_job job); void QueueJob(llarp_thread_job job);
void Join(); void Join();
@ -24,10 +22,10 @@ struct Pool {
mtx_t queue_mutex; mtx_t queue_mutex;
std::condition_variable condition; std::condition_variable condition;
std::atomic<bool> stop; bool stop;
}; };
} // namespace thread } // namespace thread
} // namespace llarp } // namespace llarp
#endif #endif

@ -1,15 +1,16 @@
#include <chrono>
#include <llarp/time.h> #include <llarp/time.h>
#include <chrono>
namespace llarp { namespace llarp {
typedef std::chrono::steady_clock clock_t; typedef std::chrono::steady_clock clock_t;
template <typename Res, typename IntType> static IntType time_since_epoch() { template <typename Res, typename IntType>
static IntType time_since_epoch() {
return std::chrono::duration_cast<Res>( return std::chrono::duration_cast<Res>(
llarp::clock_t::now().time_since_epoch()) llarp::clock_t::now().time_since_epoch())
.count(); .count();
} }
} // namespace llarp } // namespace llarp
extern "C" { extern "C" {
llarp_time_t llarp_time_now_ms() { llarp_time_t llarp_time_now_ms() {

@ -3,33 +3,28 @@
#include <stdio.h> #include <stdio.h>
struct dh_bench_main struct dh_bench_main {
{
size_t completed; size_t completed;
size_t num; size_t num;
struct llarp_ev_loop * ev; struct llarp_ev_loop *ev;
struct llarp_async_dh * dh; struct llarp_async_dh *dh;
}; };
static void handle_dh_complete(struct llarp_dh_result * res) static void handle_dh_complete(struct llarp_dh_result *res) {
{ struct dh_bench_main *m = (struct dh_bench_main *)res->user;
struct dh_bench_main * m = (struct dh_bench_main *) res->user;
m->completed++; m->completed++;
if(m->completed % 1000 == 0) if (m->completed % 1000 == 0) printf("completed %ld\n", m->completed);
printf("completed %ld\n", m->completed); if (m->completed == m->num) {
if(m->completed == m->num)
{
printf("we done\n"); printf("we done\n");
llarp_ev_loop_stop(m->ev); llarp_ev_loop_stop(m->ev);
} }
} }
int main(int argc, char * argv[]) int main(int argc, char *argv[]) {
{
struct dh_bench_main dh_main; struct dh_bench_main dh_main;
struct llarp_crypto crypto; struct llarp_crypto crypto;
struct llarp_threadpool * tp; struct llarp_threadpool *tp;
llarp_mem_jemalloc(); llarp_mem_jemalloc();
llarp_crypto_libsodium_init(&crypto); llarp_crypto_libsodium_init(&crypto);
llarp_ev_loop_alloc(&dh_main.ev); llarp_ev_loop_alloc(&dh_main.ev);
@ -38,32 +33,31 @@ int main(int argc, char * argv[])
dh_main.dh = llarp_async_dh_new(&crypto, dh_main.ev, tp); dh_main.dh = llarp_async_dh_new(&crypto, dh_main.ev, tp);
llarp_threadpool_start(tp); llarp_threadpool_start(tp);
/* do work here */ dh_main.num = 1000000;
dh_main.num = 100000;
dh_main.completed = 0; dh_main.completed = 0;
struct llarp_keypair ourkey; struct llarp_keypair ourkey;
struct llarp_keypair theirkey; struct llarp_keypair theirkey;
crypto.keygen(&ourkey); crypto.keygen(&ourkey);
crypto.keygen(&theirkey); crypto.keygen(&theirkey);
llarp_tunnel_nounce_t nounce; llarp_tunnel_nounce_t nounce;
llarp_buffer_t n_buff; llarp_buffer_t n_buff;
n_buff.base = nounce; n_buff.base = nounce;
n_buff.cur = n_buff.base; n_buff.cur = n_buff.base;
n_buff.sz = sizeof(llarp_tunnel_nounce_t); n_buff.sz = sizeof(llarp_tunnel_nounce_t);
size_t sz = dh_main.num; size_t sz = dh_main.num;
printf("starting %ld dh jobs\n", sz); printf("starting %ld dh jobs\n", sz);
while(sz--) /* do work here */
{ while (sz--) {
crypto.randomize(n_buff); crypto.randomize(n_buff);
llarp_async_client_dh(dh_main.dh, ourkey.sec, theirkey.pub, nounce, handle_dh_complete, &dh_main); llarp_async_client_dh(dh_main.dh, ourkey.sec, theirkey.pub, nounce,
handle_dh_complete, &dh_main);
} }
printf("started %ld dh jobs\n", dh_main.num); printf("started %ld dh jobs\n", dh_main.num);
llarp_ev_loop_run(dh_main.ev); llarp_ev_loop_run(dh_main.ev);
llarp_threadpool_join(tp); llarp_threadpool_join(tp);
llarp_async_dh_free(&dh_main.dh); llarp_async_dh_free(&dh_main.dh);

@ -1,6 +1,3 @@
int main(int argc, char * argv[]) int main(int argc, char *argv[]) { return 0; }
{
return 0;
}

Loading…
Cancel
Save