google format

pull/1/head
Jeff Becker 7 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);

@ -10,26 +10,32 @@ extern "C" {
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,
struct llarp_ev_loop *ev,
struct llarp_threadpool *tp);
void llarp_async_dh_free(struct llarp_async_dh **dh); void llarp_async_dh_free(struct llarp_async_dh **dh);
struct llarp_dh_result; struct llarp_dh_result;
typedef void (*llarp_dh_complete_hook)(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
} }

@ -38,7 +38,6 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener);
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 {
@ -55,7 +54,8 @@ struct llarp_ev_async_call {
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);

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

@ -12,7 +12,6 @@ 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 {
/** /**

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

@ -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,8 +51,7 @@ 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;

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

@ -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));
@ -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,13 +69,11 @@ 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);

@ -1,52 +1,36 @@
#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();
@ -60,7 +44,6 @@ struct llarp_ev_caller
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;
} }
@ -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);
} }
} }

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

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

@ -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,17 +1,16 @@
#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

@ -7,8 +7,7 @@ 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

@ -4,14 +4,12 @@ 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

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

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

@ -16,8 +16,7 @@ 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;
} }

@ -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] {
for (;;) {
llarp_thread_job job; llarp_thread_job job;
for (;;) {
{ {
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,8 +44,7 @@ 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);
} }
@ -75,13 +71,11 @@ 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) void llarp_threadpool_queue_job(struct llarp_threadpool *pool,
{ struct llarp_thread_job job) {
pool->impl.QueueJob(job); pool->impl.QueueJob(job);
} }

@ -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,7 +22,7 @@ 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

@ -1,10 +1,11 @@
#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();

@ -3,29 +3,24 @@
#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;
@ -38,8 +33,7 @@ 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;
@ -47,7 +41,6 @@ int main(int argc, char * argv[])
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;
@ -56,10 +49,11 @@ int main(int argc, char * argv[])
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);

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

Loading…
Cancel
Save