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
format: $(HDRS) $(SRCS)
$(FORMAT) -i $^
format:
$(FORMAT) -style=Google -i $(HDRS) $(SRCS)
build: $(EXE)

@ -43,8 +43,7 @@ int shutdown_llarp(struct llarp_main *m) {
int main(int argc, char *argv[]) {
struct llarp_main llarp = {NULL, NULL, NULL, NULL};
const char *conffname = "daemon.ini";
if (argc > 1)
conffname = argv[1];
if (argc > 1) conffname = argv[1];
llarp_mem_jemalloc();
llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop);
@ -55,8 +54,7 @@ int main(int argc, char *argv[]) {
iter.user = &llarp;
iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter);
if (!llarp.tp)
llarp.tp = llarp_init_threadpool(2);
if (!llarp.tp) llarp.tp = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.tp);
if (!llarp_configure_router(llarp.router, llarp.config)) {
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_tunnel_nounce_t[TUNNOUNCESIZE];
struct llarp_keypair
{
struct llarp_keypair {
llarp_pubkey_t pub;
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 {
bool (*xchacha20)(llarp_buffer_t, llarp_sharedkey_t, llarp_nounce_t);

@ -8,28 +8,34 @@
extern "C" {
#endif
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);
void llarp_async_dh_free(struct llarp_async_dh ** dh);
struct llarp_dh_result;
typedef void (*llarp_dh_complete_hook)(struct llarp_dh_result *);
struct llarp_dh_internal;
struct llarp_dh_result
{
struct llarp_dh_internal * impl;
llarp_sharedkey_t result;
void * user;
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_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);
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);
void llarp_async_dh_free(struct llarp_async_dh **dh);
struct llarp_dh_result;
typedef void (*llarp_dh_complete_hook)(struct llarp_dh_result *);
struct llarp_dh_internal;
struct llarp_dh_result {
struct llarp_dh_internal *impl;
llarp_sharedkey_t result;
void *user;
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_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
}

@ -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);
struct llarp_ev_async_call;
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 {
/** the loop this job belongs to */
const struct llarp_ev_loop *loop;
/** private implementation */
const struct llarp_ev_caller * parent;
const struct llarp_ev_caller *parent;
/** user data */
const void *user;
/**
/**
work is called async when ready in the event loop thread
must not free from inside this call as it is done elsewhere
*/
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
}
#endif

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

@ -10,17 +10,16 @@ struct llarp_threadpool;
struct llarp_threadpool *llarp_init_threadpool(int workers);
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 */
struct llarp_thread_job {
/**
called async after work is executed
*/
struct llarp_ev_caller * caller;
void * data;
struct llarp_ev_caller *caller;
void *data;
/** user data to pass to work function */
void *user;
/** called in threadpool worker thread */

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

@ -4,7 +4,7 @@
namespace llarp {
static void *ai_alloc(size_t sz, size_t align) {}
} // namespace llarp
} // namespace llarp
extern "C" {
@ -19,8 +19,7 @@ void llarp_ai_list_iterate(struct llarp_ai_list *l,
itr->list = l;
struct llarp_ai_list *cur = l;
do {
if (!itr->visit(itr, cur->data))
return;
if (!itr->visit(itr, cur->data)) return;
cur = 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) &&
bencodeEnd(buff);
}
} // namespace llarp
} // namespace llarp

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

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

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

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

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

@ -1,21 +1,21 @@
#include <assert.h>
#include <llarp/crypto.h>
#include <sodium.h>
#include <assert.h>
namespace llarp {
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;
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,
uint8_t *remote_key, uint8_t *local_key) {
static bool dh(llarp_sharedkey_t *shared, uint8_t *client_pk,
uint8_t *server_pk, uint8_t *remote_key, uint8_t *local_key) {
uint8_t *out = *shared;
const size_t outsz = SHAREDKEYSIZE;
crypto_generichash_state h;
if (crypto_scalarmult(out, local_key, remote_key) == -1)
return false;
if (crypto_scalarmult(out, local_key, remote_key) == -1) return false;
crypto_generichash_init(&h, NULL, 0U, outsz);
crypto_generichash_update(&h, client_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,
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
llarp_pubkey_t local_pk;
crypto_scalarmult_base(local_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,
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
llarp_tunnel_nounce_t n, llarp_seckey_t sk) {
llarp_pubkey_t local_pk;
crypto_scalarmult_base(local_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;
}
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;
return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz,
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;
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;
}
static void randomize(llarp_buffer_t buff)
{
randombytes((unsigned char *)buff.base, buff.sz);
}
static void randomize(llarp_buffer_t buff) {
randombytes((unsigned char *)buff.base, buff.sz);
}
static void keygen(struct llarp_keypair * keys)
{
randombytes(keys->sec, sizeof(llarp_seckey_t));
unsigned char sk[64];
crypto_sign_seed_keypair(keys->pub, sk, keys->sec);
}
} // namespace sodium
} // namespace llarp
static void keygen(struct llarp_keypair *keys) {
randombytes(keys->sec, sizeof(llarp_seckey_t));
unsigned char sk[64];
crypto_sign_seed_keypair(keys->pub, sk, keys->sec);
}
} // namespace sodium
} // namespace llarp
extern "C" {
void llarp_crypto_libsodium_init(struct llarp_crypto *c) {

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

@ -1,7 +1,7 @@
#ifndef LLARP_XI_HPP
#define LLARP_XI_HPP
#include <list>
#include <llarp/exit_info.h>
#include <list>
struct 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) {
std::list<llarp_xi> list;
if (l->list.size())
for (const auto &xi : l->list)
list.push_back(xi);
for (const auto &xi : l->list) list.push_back(xi);
return list;
}

@ -46,7 +46,7 @@ struct Level {
typedef std::list<section_map_t::const_iterator> sections_t;
value_map_t values;
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;
Level *parent;
size_t depth;
@ -58,20 +58,20 @@ struct Level {
};
class Parser {
public:
public:
Parser(const std::string &fname) : Parser(fname.c_str()) {}
Parser(const char *fn);
Parser(std::istream &f) : f_(&f), ln_(0) { parse(top_); }
Level &top() { return top_; }
void dump(std::ostream &s) { dump(s, top(), ""); }
private:
private:
void dump(std::ostream &s, const Level &l, const std::string &sname);
void parse(Level &l);
void parseSLine(std::string &sname, size_t &depth);
void err(const char *s);
private:
private:
Level top_;
std::ifstream f0_;
std::istream *f_;
@ -90,17 +90,14 @@ inline std::string trim(const std::string &s) {
long sp = 0;
long ep = s.length() - 1;
for (; sp <= ep; ++sp)
if (!strchr(p, s[sp]))
break;
if (!strchr(p, s[sp])) break;
for (; ep >= 0; --ep)
if (!strchr(p, s[ep]))
break;
if (!strchr(p, s[ep])) break;
return s.substr(sp, ep - sp + 1);
}
inline Parser::Parser(const char *fn) : f0_(fn), f_(&f0_), ln_(0) {
if (!f0_)
throw std::runtime_error(std::string("failed to open file: ") + fn);
if (!f0_) throw std::runtime_error(std::string("failed to open file: ") + fn);
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) {
depth = 0;
for (; depth < line_.length(); ++depth)
if (line_[depth] != '[')
break;
if (line_[depth] != '[') break;
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) {
while (std::getline(*f_, line_)) {
++ln_;
if (line_[0] == '#' || line_[0] == ';')
continue;
if (line_[0] == '#' || line_[0] == ';') continue;
line_ = trim(line_);
if (line_.empty())
continue;
if (line_.empty()) continue;
if (line_[0] == '[') {
size_t depth;
std::string sname;
parseSLine(sname, depth);
Level *lp = NULL;
Level *parent = &l;
if (depth > l.depth + 1)
err("section with wrong depth");
if (depth > l.depth + 1) err("section with wrong depth");
if (l.depth == depth - 1)
lp = &l.sections[sname];
else {
lp = l.parent;
size_t n = l.depth - depth;
for (size_t i = 0; i < n; ++i)
lp = lp->parent;
for (size_t i = 0; i < n; ++i) lp = lp->parent;
parent = lp;
lp = &lp->sections[sname];
}
if (lp->depth != 0)
err("duplicate section name on the same level");
if (lp->depth != 0) err("duplicate section name on the same level");
if (!lp->parent) {
lp->depth = depth;
lp->parent = parent;
@ -150,13 +141,11 @@ inline void Parser::parse(Level &l) {
parse(*lp);
} else {
size_t n = line_.find('=');
if (n == std::string::npos)
err("no '=' found");
if (n == std::string::npos) err("no '=' found");
std::pair<Level::value_map_t::const_iterator, bool> res = l.values.insert(
std::make_pair(trim(line_.substr(0, n)),
trim(line_.substr(n + 1, line_.length() - n - 1))));
if (!res.second)
err("duplicated key found");
if (!res.second) err("duplicated key found");
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,
const std::string &sname) {
if (!sname.empty())
s << '\n';
for (size_t i = 0; i < l.depth; ++i)
s << '[';
if (!sname.empty())
s << sname;
for (size_t i = 0; i < l.depth; ++i)
s << ']';
if (!sname.empty())
s << std::endl;
if (!sname.empty()) s << '\n';
for (size_t i = 0; i < l.depth; ++i) s << '[';
if (!sname.empty()) 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();
it != l.ordered_values.end(); ++it)
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 <cstring>
#include <llarp/time.h>
#include <cstring>
bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) {
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) {
if (*l)
delete *l;
if (*l) delete *l;
*l = nullptr;
}

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

@ -1,18 +1,17 @@
#ifndef LLARP_MEM_HPP
#define LLARP_MEM_HPP
#include <cmath>
#include <llarp/mem.h>
#include <cmath>
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))));
}
template<typename T>
static T * Alloc(llarp_alloc * mem=&llarp_g_mem)
{
return static_cast<T *>(
mem->alloc(sizeof(T), alignment<T>()));
}
} // namespace llarp
template <typename T>
static T *Alloc(llarp_alloc *mem = &llarp_g_mem) {
return static_cast<T *>(mem->alloc(sizeof(T), alignment<T>()));
}
} // namespace llarp
#endif

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

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

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

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

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

@ -1,15 +1,16 @@
#include <chrono>
#include <llarp/time.h>
#include <chrono>
namespace llarp {
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>(
llarp::clock_t::now().time_since_epoch())
.count();
}
} // namespace llarp
} // namespace llarp
extern "C" {
llarp_time_t llarp_time_now_ms() {

@ -3,33 +3,28 @@
#include <stdio.h>
struct dh_bench_main
{
struct dh_bench_main {
size_t completed;
size_t num;
struct llarp_ev_loop * ev;
struct llarp_async_dh * dh;
struct llarp_ev_loop *ev;
struct llarp_async_dh *dh;
};
static void handle_dh_complete(struct llarp_dh_result * res)
{
struct dh_bench_main * m = (struct dh_bench_main *) res->user;
static void handle_dh_complete(struct llarp_dh_result *res) {
struct dh_bench_main *m = (struct dh_bench_main *)res->user;
m->completed++;
if(m->completed % 1000 == 0)
printf("completed %ld\n", m->completed);
if(m->completed == m->num)
{
if (m->completed % 1000 == 0) printf("completed %ld\n", m->completed);
if (m->completed == m->num) {
printf("we done\n");
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 llarp_crypto crypto;
struct llarp_threadpool * tp;
struct llarp_threadpool *tp;
llarp_mem_jemalloc();
llarp_crypto_libsodium_init(&crypto);
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);
llarp_threadpool_start(tp);
/* do work here */
dh_main.num = 100000;
dh_main.num = 1000000;
dh_main.completed = 0;
struct llarp_keypair ourkey;
struct llarp_keypair theirkey;
crypto.keygen(&ourkey);
crypto.keygen(&theirkey);
llarp_tunnel_nounce_t nounce;
llarp_buffer_t n_buff;
n_buff.base = nounce;
n_buff.cur = n_buff.base;
n_buff.sz = sizeof(llarp_tunnel_nounce_t);
size_t sz = dh_main.num;
printf("starting %ld dh jobs\n", sz);
while(sz--)
{
/* do work here */
while (sz--) {
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);
llarp_ev_loop_run(dh_main.ev);
llarp_threadpool_join(tp);
llarp_async_dh_free(&dh_main.dh);

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

Loading…
Cancel
Save