clang-format

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

@ -0,0 +1,108 @@
---
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
...

@ -11,6 +11,10 @@ STATIC_OBJ = $(STATIC_SRC_CPP:.cpp=.cpp.o) $(STATIC_SRC_C:.c=.c.o)
DAEMON_SRC = $(wildcard $(REPO)/daemon/*.c) DAEMON_SRC = $(wildcard $(REPO)/daemon/*.c)
DAEMON_OBJ = $(DAEMON_SRC:.c=.c.o) DAEMON_OBJ = $(DAEMON_SRC:.c=.c.o)
HDRS = $(wildcard $(REPO)/llarp/*.hpp) $(wildcard $(REPO)/include/*/*.h)
SRCS = $(DAEMON_SRC) $(STATIC_SRC_CPP) $(STATIC_SRC_C)
FORMAT = clang-format
SODIUM_FLAGS = $(shell pkg-config --cflags libsodium) SODIUM_FLAGS = $(shell pkg-config --cflags libsodium)
SODIUM_LIBS = $(shell pkg-config --libs libsodium) SODIUM_LIBS = $(shell pkg-config --libs libsodium)
@ -31,6 +35,9 @@ REQUIRED_LDFLAGS = $(LDFLAGS) -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS)
all: build all: build
format: $(HDRS) $(SRCS)
$(FORMAT) -i $^
build: $(EXE) build: $(EXE)
$(EXE): $(DAEMON_OBJ) $(STATIC_LIB) $(EXE): $(DAEMON_OBJ) $(STATIC_LIB)

@ -2,32 +2,27 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
struct llarp_main struct llarp_main {
{ struct llarp_router *router;
struct llarp_router * router; struct llarp_threadpool *tp;
struct llarp_threadpool * tp; struct llarp_config *config;
struct llarp_config * config; struct llarp_ev_loop *mainloop;
struct llarp_ev_loop * mainloop;
}; };
void iter_main_config(struct llarp_config_iterator * itr, const char * section, const char * key, const char * val) void iter_main_config(struct llarp_config_iterator *itr, const char *section,
{ const char *key, const char *val) {
struct llarp_main * m = (struct llarp_main *) itr->user; struct llarp_main *m = (struct llarp_main *)itr->user;
if(!strcmp(section, "threadpool")) if (!strcmp(section, "threadpool")) {
{ if (!strcmp(key, "workers")) {
if(!strcmp(key, "workers"))
{
int workers = atoi(val); int workers = atoi(val);
if(!m->tp && workers > 0) if (!m->tp && workers > 0) {
{
m->tp = llarp_init_threadpool(workers); m->tp = llarp_init_threadpool(workers);
} }
} }
} }
} }
int shutdown_llarp(struct llarp_main * m) int shutdown_llarp(struct llarp_main *m) {
{
printf("Shutting down ."); printf("Shutting down .");
llarp_stop_router(m->router); llarp_stop_router(m->router);
printf("."); printf(".");
@ -43,42 +38,34 @@ int shutdown_llarp(struct llarp_main * m)
printf("."); printf(".");
llarp_free_threadpool(&m->tp); llarp_free_threadpool(&m->tp);
printf(".\n"); printf(".\n");
} }
int main(int argc, char * argv[]) int main(int argc, char *argv[]) {
{ struct llarp_main llarp = {NULL, NULL, NULL, NULL};
struct llarp_main llarp = { const char *conffname = "daemon.ini";
NULL, NULL, NULL, NULL
};
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);
printf("%s loaded\n", LLARP_VERSION); printf("%s loaded\n", LLARP_VERSION);
if(!llarp_load_config(llarp.config, conffname)) if (!llarp_load_config(llarp.config, conffname)) {
{
printf("Loaded config %s\n", conffname); printf("Loaded config %s\n", conffname);
struct llarp_config_iterator iter; struct llarp_config_iterator iter;
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");
llarp_run_router(llarp.router, llarp.mainloop); llarp_run_router(llarp.router, llarp.mainloop);
llarp_ev_loop_run(llarp.mainloop); llarp_ev_loop_run(llarp.mainloop);
} } else
else
printf("Failed to configure router\n"); printf("Failed to configure router\n");
} } else
else
printf("Failed to load config %s\n", conffname); printf("Failed to load config %s\n", conffname);
return shutdown_llarp(&llarp); return shutdown_llarp(&llarp);
} }

@ -10,45 +10,44 @@ extern "C" {
#endif #endif
#define MAX_AI_DIALECT_SIZE 5 #define MAX_AI_DIALECT_SIZE 5
struct llarp_ai struct llarp_ai {
{ uint16_t rank;
uint16_t rank; llarp_pubkey_t enc_key;
llarp_pubkey_t enc_key; uint8_t dialect[MAX_AI_DIALECT_SIZE + 1];
uint8_t dialect[MAX_AI_DIALECT_SIZE+1]; struct in6_addr ip;
struct in6_addr ip; uint16_t port;
uint16_t port; };
};
/** allocator for address info */
/** allocator for address info */ extern struct llarp_alloc *llarp_ai_alloc;
extern struct llarp_alloc * llarp_ai_alloc;
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff);
bool llarp_ai_bencode(struct llarp_ai * ai, llarp_buffer_t * buff); bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t buff);
bool llarp_ai_bdecode(struct llarp_ai * ai, llarp_buffer_t buff);
struct llarp_ai_list;
struct llarp_ai_list;
struct llarp_ai_list *llarp_ai_list_new();
struct llarp_ai_list * llarp_ai_list_new(); void llarp_ai_list_free(struct llarp_ai_list *l);
void llarp_ai_list_free(struct llarp_ai_list * l);
struct llarp_ai llarp_ai_list_popfront(struct llarp_ai_list *l);
struct llarp_ai llarp_ai_list_popfront(struct llarp_ai_list * l); void llarp_ai_list_pushback(struct llarp_ai_list *l, struct llarp_ai *ai);
void llarp_ai_list_pushback(struct llarp_ai_list * l, struct llarp_ai * ai); size_t llarp_ai_list_size(struct llarp_ai_list *l);
size_t llarp_ai_list_size(struct llarp_ai_list * l); struct llarp_ai *llarp_ai_list_index(struct llarp_ai_list *l, ssize_t idx);
struct llarp_ai * llarp_ai_list_index(struct llarp_ai_list * l, ssize_t idx);
struct llarp_ai_list_iter {
struct llarp_ai_list_iter void *user;
{ /** set by llarp_ai_list_iterate() */
void * user; struct llarp_ai_list *list;
/** set by llarp_ai_list_iterate() */ /** return false to break iteration */
struct llarp_ai_list * list; bool (*visit)(struct llarp_ai_list_iter *, struct llarp_ai *);
/** return false to break iteration */ };
bool (*visit)(struct llarp_ai_list_iter *, struct llarp_ai *);
}; void llarp_ai_list_iterate(struct llarp_ai_list *l,
struct llarp_ai_list_iter *iter);
void llarp_ai_list_iterate(struct llarp_ai_list * l, struct llarp_ai_list_iter * iter);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

@ -1,22 +1,22 @@
#ifndef LLARP_BUFFER_H_ #ifndef LLARP_BUFFER_H_
#define LLARP_BUFFER_H_ #define LLARP_BUFFER_H_
#include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef struct llarp_buffer_t { typedef struct llarp_buffer_t {
char * base; char *base;
size_t sz; size_t sz;
char * cur; char *cur;
} llarp_buffer_t; } llarp_buffer_t;
size_t llarp_buffer_size_left(llarp_buffer_t *buff);
bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz);
size_t llarp_buffer_size_left(llarp_buffer_t * buff);
bool llarp_buffer_write(llarp_buffer_t * buff, const void * data, size_t sz);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

@ -5,25 +5,26 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_config; struct llarp_config;
void llarp_new_config(struct llarp_config ** conf); void llarp_new_config(struct llarp_config **conf);
void llarp_free_config(struct llarp_config ** conf); void llarp_free_config(struct llarp_config **conf);
/** @brief return -1 on fail otherwiwse 0 */ /** @brief return -1 on fail otherwiwse 0 */
int llarp_load_config(struct llarp_config * conf, const char * fname); int llarp_load_config(struct llarp_config *conf, const char *fname);
struct llarp_config_iterator {
void *user;
/** set by llarp_config_iter */
struct llarp_config *conf;
/** visit (self, section, key, value) */
void (*visit)(struct llarp_config_iterator *, const char *, const char *,
const char *);
};
void llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter);
struct llarp_config_iterator
{
void * user;
/** set by llarp_config_iter */
struct llarp_config * conf;
/** visit (self, section, key, value) */
void (*visit)(struct llarp_config_iterator *, const char *, const char *, const char *);
};
void llarp_config_iter(struct llarp_config * conf, struct llarp_config_iterator * iter);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -1,8 +1,8 @@
#ifndef LLARP_CRYPTO_H_ #ifndef LLARP_CRYPTO_H_
#define LLARP_CRYPTO_H_ #define LLARP_CRYPTO_H_
#include <llarp/buffer.h> #include <llarp/buffer.h>
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -16,30 +16,31 @@ extern "C" {
#define SIGSIZE 64 #define SIGSIZE 64
#define TUNNOUNCESIZE 32 #define TUNNOUNCESIZE 32
typedef uint8_t llarp_pubkey_t[PUBKEYSIZE]; typedef uint8_t llarp_pubkey_t[PUBKEYSIZE];
typedef uint8_t llarp_seckey_t[SECKEYSIZE]; typedef uint8_t llarp_seckey_t[SECKEYSIZE];
typedef uint8_t llarp_nounce_t[NOUNCESIZE]; typedef uint8_t llarp_nounce_t[NOUNCESIZE];
typedef uint8_t llarp_sharedkey_t[SHAREDKEYSIZE]; typedef uint8_t llarp_sharedkey_t[SHAREDKEYSIZE];
typedef uint8_t llarp_hash_t[HASHSIZE]; typedef uint8_t llarp_hash_t[HASHSIZE];
typedef uint8_t llarp_hmacsec_t[HMACSECSIZE]; 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_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); bool (*dh_client)(llarp_sharedkey_t *, llarp_pubkey_t, llarp_tunnel_nounce_t,
bool (*dh_client)(llarp_sharedkey_t *, llarp_pubkey_t, llarp_tunnel_nounce_t, llarp_seckey_t); llarp_seckey_t);
bool (*dh_server)(llarp_sharedkey_t *, llarp_pubkey_t, llarp_tunnel_nounce_t, llarp_seckey_t); bool (*dh_server)(llarp_sharedkey_t *, llarp_pubkey_t, llarp_tunnel_nounce_t,
bool (*hash)(llarp_hash_t *, llarp_buffer_t); llarp_seckey_t);
bool (*hmac)(llarp_hash_t *, llarp_buffer_t, llarp_hmacsec_t); bool (*hash)(llarp_hash_t *, llarp_buffer_t);
bool (*sign)(llarp_sig_t *, llarp_seckey_t, llarp_buffer_t); bool (*hmac)(llarp_hash_t *, llarp_buffer_t, llarp_hmacsec_t);
bool (*verify)(llarp_pubkey_t, llarp_buffer_t, llarp_sig_t); bool (*sign)(llarp_sig_t *, llarp_seckey_t, llarp_buffer_t);
}; bool (*verify)(llarp_pubkey_t, llarp_buffer_t, llarp_sig_t);
};
void llarp_crypto_libsodium_init(struct llarp_crypto * c); void llarp_crypto_libsodium_init(struct llarp_crypto *c);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

@ -1,55 +1,54 @@
#ifndef LLARP_EV_H #ifndef LLARP_EV_H
#define LLARP_EV_H #define LLARP_EV_H
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/socket.h> #include <sys/socket.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct llarp_ev_loop; struct llarp_ev_loop;
void llarp_ev_loop_alloc(struct llarp_ev_loop ** ev); void llarp_ev_loop_alloc(struct llarp_ev_loop **ev);
void llarp_ev_loop_free(struct llarp_ev_loop ** ev); void llarp_ev_loop_free(struct llarp_ev_loop **ev);
int llarp_ev_loop_run(struct llarp_ev_loop * ev); int llarp_ev_loop_run(struct llarp_ev_loop *ev);
/** stop event loop and wait for it to complete all jobs */ /** stop event loop and wait for it to complete all jobs */
void llarp_ev_loop_stop(struct llarp_ev_loop * ev); void llarp_ev_loop_stop(struct llarp_ev_loop *ev);
struct llarp_udp_listener struct llarp_udp_listener {
{ char *host;
char * host; uint16_t port;
uint16_t port; void *user;
void * user; void *impl;
void * impl; void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *,
void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *, ssize_t); ssize_t);
void (*closed)(struct llarp_udp_listener *); void (*closed)(struct llarp_udp_listener *);
}; };
int llarp_ev_add_udp_listener(struct llarp_ev_loop * ev, struct llarp_udp_listener * listener); int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
struct llarp_udp_listener *listener);
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_job struct llarp_ev_job {
{ /** the loop this job belongs to */
/** the loop this job belongs to */ struct llarp_ev_loop *loop;
struct llarp_ev_loop * loop; /** user data */
/** user data */ void *user;
void * user; /** work is called async when ready in the event loop thread */
/** work is called async when ready in the event loop thread */ void (*work)(struct llarp_ev_job *);
void (*work)(struct llarp_ev_job *); };
};
/**
/** call work async in event loop thread (thread safe)
call work async in event loop thread (thread safe) return true if we queued the job otherwise return false
return true if we queued the job otherwise return false */
*/ bool llarp_ev_async(struct llarp_ev_loop *ev, struct llarp_ev_job job);
bool llarp_ev_async(struct llarp_ev_loop * ev, struct llarp_ev_job job);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -6,19 +6,17 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct llarp_xi
{
in6_addr address;
in6_addr netmask;
};
struct llarp_xi {
bool llarp_xi_bdecode(struct llarp_xi * xi, llarp_buffer_t * buf); in6_addr address;
bool llarp_xi_bencode(struct llarp_xi * xi, llarp_buffer_t * buf); in6_addr netmask;
};
struct llarp_xi_list;
bool llarp_xi_bdecode(struct llarp_xi *xi, llarp_buffer_t *buf);
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buf);
struct llarp_xi_list;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -7,21 +7,22 @@
extern "C" { extern "C" {
#endif #endif
// forward declare // forward declare
struct llarp_msg_muxer; struct llarp_msg_muxer;
struct llarp_link_queue; struct llarp_link_queue;
struct llarp_link_queue * llarp_init_link_queue(); struct llarp_link_queue *llarp_init_link_queue();
void llarp_free_link_queue(struct llarp_link_queue ** queue); void llarp_free_link_queue(struct llarp_link_queue **queue);
/** /**
offer a full frame to the inbound frame queue offer a full frame to the inbound frame queue
return true if successfully added return true if successfully added
return false if the queue is full return false if the queue is full
*/ */
bool llarp_link_offer_frame(struct llarp_link_queue * queue, llarp_buffer_t msg); bool llarp_link_offer_frame(struct llarp_link_queue *queue, llarp_buffer_t msg);
/** return true if we have more messages to process */ /** return true if we have more messages to process */
bool llarp_link_queue_process(struct llarp_link_queue * queue, struct llarp_msg_muxer * muxer); bool llarp_link_queue_process(struct llarp_link_queue *queue,
struct llarp_msg_muxer *muxer);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -1,26 +1,26 @@
#ifndef LLARP_IBMQ_H_ #ifndef LLARP_IBMQ_H_
#define LLARP_IBMQ_H_ #define LLARP_IBMQ_H_
#include <llarp/buffer.h> #include <llarp/buffer.h>
#include <stdbool.h>
#include <llarp/msg_handler.h> #include <llarp/msg_handler.h>
#include <stdbool.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct llarp_mq; struct llarp_mq;
struct llarp_mq * llarp_init_mq(); struct llarp_mq *llarp_init_mq();
void llarp_free_mq(struct llarp_mq ** queue); void llarp_free_mq(struct llarp_mq **queue);
/** /**
offer a full message to the inbound message queue offer a full message to the inbound message queue
return true if successfully added return true if successfully added
return false if the queue is full return false if the queue is full
*/ */
bool llarp_mq_offer(struct llarp_mq * queue, llarp_buffer_t msg); bool llarp_mq_offer(struct llarp_mq *queue, llarp_buffer_t msg);
size_t llarp_mq_peek(struct llarp_mq * queue); size_t llarp_mq_peek(struct llarp_mq *queue);
/** return true if we have more messages to process */ /** return true if we have more messages to process */
bool llarp_mq_process(struct llarp_mq * queue, struct llarp_msg_muxer * muxer); bool llarp_mq_process(struct llarp_mq *queue, struct llarp_msg_muxer *muxer);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -1,59 +1,59 @@
#ifndef LLARP_LINK_H_ #ifndef LLARP_LINK_H_
#define LLARP_LINK_H_ #define LLARP_LINK_H_
#include <llarp/mem.h> #include <llarp/crypto.h>
#include <llarp/ibmq.h> #include <llarp/ibmq.h>
#include <llarp/mem.h>
#include <llarp/obmd.h> #include <llarp/obmd.h>
#include <llarp/crypto.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct llarp_link; struct llarp_link;
struct llarp_link *llarp_link_alloc();
void llarp_link_free(struct llarp_link **link);
struct llarp_link * llarp_link_alloc(); bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af);
void llarp_link_free(struct llarp_link ** link);
bool llarp_link_configure(struct llarp_link * link, const char * ifname, int af); /** get link listener for events */
struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *link);
/** get link listener for events */ void llarp_link_stop(struct llarp_link *link);
struct llarp_udp_listener * llarp_link_udp_listener(struct llarp_link * link);
void llarp_link_stop(struct llarp_link * link); struct llarp_link_session;
struct llarp_link_session; struct llarp_link_session_listener {
void *user;
struct llarp_link_session_listener /** set by llarp_try_establish_session */
{ struct llarp_link *link;
void * user; /** set by llarp_try_establish_session */
/** set by llarp_try_establish_session */ struct llarp_rc *rc;
struct llarp_link * link; void (*result)(struct llarp_link_session_listener *,
/** set by llarp_try_establish_session */ struct llarp_link_session *);
struct llarp_rc * rc; };
void (*result)(struct llarp_link_session_listener *, struct llarp_link_session *);
};
/** information for establishing an outbound session */ /** information for establishing an outbound session */
struct llarp_link_establish_job struct llarp_link_establish_job {
{ struct llarp_rc *rc;
struct llarp_rc * rc; uint64_t timeout;
uint64_t timeout; };
};
void llarp_link_try_establish_session(struct llarp_link * link, struct llarp_link_establish_job * job, struct llarp_link_session_listener * l); void llarp_link_try_establish_session(struct llarp_link *link,
struct llarp_link_establish_job *job,
struct llarp_link_session_listener *l);
struct llarp_link_session_iter {
void *user;
struct llarp_link *link;
bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *);
};
struct llarp_link_session_iter void llarp_link_iter_sessions(struct llarp_link *l,
{ struct llarp_link_session_iter *i);
void * user;
struct llarp_link * link;
bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *);
};
void llarp_link_iter_sessions(struct llarp_link * l, struct llarp_link_session_iter * i); struct llarp_rc *llarp_link_session_rc(struct llarp_link_session *s);
struct llarp_rc * llarp_link_session_rc(struct llarp_link_session * s);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -7,24 +7,22 @@ extern "C" {
#include <stdlib.h> #include <stdlib.h>
struct llarp_alloc struct llarp_alloc {
{ void *(*alloc)(size_t sz, size_t align);
void * (*alloc)(size_t sz, size_t align); void (*free)(void *ptr);
void (*free)(void * ptr); };
};
/** global memory allocator */
/** global memory allocator */ extern struct llarp_alloc llarp_g_mem;
extern struct llarp_alloc llarp_g_mem; /** init llarp_g_mem with stdlib malloc */
/** init llarp_g_mem with stdlib malloc */ void llarp_mem_stdlib();
void llarp_mem_stdlib(); /** init llarp_g_mem with jemalloc */
/** init llarp_g_mem with jemalloc */ void llarp_mem_jemalloc();
void llarp_mem_jemalloc(); /** init llarp_g_mem with dmalloc */
/** init llarp_g_mem with dmalloc */ void llarp_mem_dmalloc();
void llarp_mem_dmalloc();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

@ -8,28 +8,26 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_frame_handler struct llarp_frame_handler {
{ struct llarp_obmd *outbound;
struct llarp_obmd * outbound; struct llarp_ibmq *inbound;
struct llarp_ibmq * inbound; bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *,
bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *, llarp_buffer_t); llarp_buffer_t);
}; };
struct llarp_msg_handler struct llarp_msg_handler {
{ struct llarp_path_context *paths;
struct llarp_path_context * paths; };
};
struct llarp_msg_muxer
{
/** get a message handler for a link level message given msg.a */
struct llarp_frame_handler * (*link_handler_for)(const char *);
/** get a message handler for a routing layer message given msg.A */
struct llarp_msg_handler * (*routing_handler_for)(const char *);
};
/** fill function pointers with default values */ struct llarp_msg_muxer {
void llarp_msg_handler_mux_init(struct llarp_msg_muxer * muxer); /** get a message handler for a link level message given msg.a */
struct llarp_frame_handler *(*link_handler_for)(const char *);
/** get a message handler for a routing layer message given msg.A */
struct llarp_msg_handler *(*routing_handler_for)(const char *);
};
/** fill function pointers with default values */
void llarp_msg_handler_mux_init(struct llarp_msg_muxer *muxer);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -1,8 +1,8 @@
#ifndef SARP_NET_H #ifndef SARP_NET_H
#define SARP_NET_H #define SARP_NET_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif #endif

@ -8,18 +8,20 @@
extern "C" { extern "C" {
#endif #endif
// forward declair // forward declair
struct llarp_link; struct llarp_link;
struct llarp_link_dispatcher; struct llarp_link_dispatcher;
struct llarp_link_dispatcher * llarp_init_link_dispatcher(); struct llarp_link_dispatcher *llarp_init_link_dispatcher();
void llarp_free_link_dispatcher(struct llarp_link_dispatcher ** dispatcher); void llarp_free_link_dispatcher(struct llarp_link_dispatcher **dispatcher);
void llarp_link_sendto(struct llarp_link_dispatcher * dispatcher, llarp_pubkey_t pubkey, llarp_buffer_t msg); void llarp_link_sendto(struct llarp_link_dispatcher *dispatcher,
llarp_pubkey_t pubkey, llarp_buffer_t msg);
void llarp_link_register(struct llarp_link_dispatcher * dispatcher, struct llarp_link * link);
void llarp_link_register(struct llarp_link_dispatcher *dispatcher,
struct llarp_link *link);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -5,8 +5,7 @@
typedef uint64_t llarp_path_id_t; typedef uint64_t llarp_path_id_t;
struct llarp_transit_hop struct llarp_transit_hop {
{
llarp_path_id_t id; llarp_path_id_t id;
llarp_sharedkey_t symkey; llarp_sharedkey_t symkey;
llarp_pubkey_t nextHop; llarp_pubkey_t nextHop;
@ -15,5 +14,4 @@ struct llarp_transit_hop
llarp_version_t version; llarp_version_t version;
}; };
#endif #endif

@ -10,21 +10,22 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_router; struct llarp_router;
struct llarp_router * llarp_init_router(struct llarp_threadpool * tp); struct llarp_router *llarp_init_router(struct llarp_threadpool *tp);
void llarp_free_router(struct llarp_router ** router); void llarp_free_router(struct llarp_router **router);
int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf); int llarp_configure_router(struct llarp_router *router,
struct llarp_config *conf);
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop);
void llarp_stop_router(struct llarp_router * router);
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop);
/** get router's inbound link level frame queue */ void llarp_stop_router(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 inbound link level frame queue */
struct llarp_link_dispatcher * llarp_router_link_dispatcher(struct llarp_router * router); 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);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -1,28 +1,25 @@
#ifndef LLARP_RC_H #ifndef LLARP_RC_H
#define LLARP_RC_H #define LLARP_RC_H
#include <llarp/address_info.h> #include <llarp/address_info.h>
#include <llarp/exit_info.h>
#include <llarp/crypto.h> #include <llarp/crypto.h>
#include <llarp/exit_info.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct llarp_rc
{
llarp_buffer_t raw;
struct llarp_ai_list * addrs;
llarp_pubkey_t pubkey;
struct llarp_xi_list * exits;
llarp_sig_t signature;
};
bool llarp_rc_bdecode(struct llarp_rc * rc, llarp_buffer_t * buf); struct llarp_rc {
bool llarp_rc_bencode(struct llarp_rc * rc, llarp_buffer_t * buf); llarp_buffer_t raw;
void llarp_rc_free(struct llarp_rc ** rc); struct llarp_ai_list *addrs;
bool llarp_rc_verify_sig(struct llarp_rc * rc); llarp_pubkey_t pubkey;
struct llarp_xi_list *exits;
llarp_sig_t signature;
};
bool llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buf);
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buf);
void llarp_rc_free(struct llarp_rc **rc);
bool llarp_rc_verify_sig(struct llarp_rc *rc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -6,18 +6,19 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_service_info struct llarp_service_info {
{ llarp_buffer_t name;
llarp_buffer_t name; llarp_pubkey_t signingkey;
llarp_pubkey_t signingkey; llarp_buffer_t vanity;
llarp_buffer_t vanity; };
};
void llarp_service_info_hash(struct llarp_service_info *si, llarp_hash_t *h);
bool llarp_service_info_bencode(struct llarp_serivce_info *si,
llarp_buffer_t *buff);
bool llarp_service_info_bdecode(struct llarp_serivce_info *si,
llarp_buffer_t buff);
void llarp_service_info_free(struct llarp_service_info **si);
void llarp_service_info_hash(struct llarp_service_info * si, llarp_hash_t * h);
bool llarp_service_info_bencode(struct llarp_serivce_info * si, llarp_buffer_t * buff);
bool llarp_service_info_bdecode(struct llarp_serivce_info * si, llarp_buffer_t buff);
void llarp_service_info_free(struct llarp_service_info ** si);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -5,28 +5,28 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_threadpool; 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);
/** 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_job *result;
struct llarp_ev_job * result; /** 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 */ void (*work)(void *);
void (*work)(void *); };
};
void llarp_threadpool_queue_job(struct llarp_threadpool * tp, struct llarp_thread_job j);
void llarp_threadpool_start(struct llarp_threadpool * tp); void llarp_threadpool_queue_job(struct llarp_threadpool *tp,
void llarp_threadpool_join(struct llarp_threadpool * tp); struct llarp_thread_job j);
void llarp_threadpool_start(struct llarp_threadpool *tp);
void llarp_threadpool_join(struct llarp_threadpool *tp);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -5,9 +5,9 @@
extern "C" { extern "C" {
#endif #endif
llarp_time_t llarp_time_now_ms(); llarp_time_t llarp_time_now_ms();
llarp_seconds_t llarp_time_now_sec(); llarp_seconds_t llarp_time_now_sec();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -15,11 +15,14 @@
#ifndef LLARP_VERSION_NUM #ifndef LLARP_VERSION_NUM
#ifdef GIT_REV #ifdef GIT_REV
#define LLARP_VERSION_NUM "-"LLARP_VERSION_MAJ"."LLARP_VERSION_MIN"."LLARP_VERSION_PATCH"-"GIT_REV #define LLARP_VERSION_NUM \
"-" LLARP_VERSION_MAJ "." LLARP_VERSION_MIN "." LLARP_VERSION_PATCH \
"-" GIT_REV
#else #else
#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
#endif #endif
#endif #endif
#define LLARP_VERSION "llarpd"LLARP_VERSION_NUM #define LLARP_VERSION "llarpd" LLARP_VERSION_NUM
#endif #endif

@ -2,48 +2,38 @@
#include "bencode.hpp" #include "bencode.hpp"
#include "str.hpp" #include "str.hpp"
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
{
}
}
extern "C" { extern "C" {
struct llarp_alloc * llarp_ai_alloc = &llarp_g_mem; struct llarp_alloc *llarp_ai_alloc = &llarp_g_mem;
bool llarp_ai_bencode(struct llarp_ai * ai, llarp_buffer_t * buff) bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
{ return llarp::BEncode(*ai, buff);
return llarp::BEncode(*ai, buff); }
}
void llarp_ai_list_iterate(struct llarp_ai_list *l,
void llarp_ai_list_iterate(struct llarp_ai_list * l, struct llarp_ai_list_iter * itr) struct llarp_ai_list_iter *itr) {
{ 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))
{ return;
if(!itr->visit(itr, cur->data)) cur = cur->next;
return; } while (cur->next);
cur = cur->next; }
}
while(cur->next);
}
} }
namespace llarp namespace llarp {
{ bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) {
bool BEncode(const llarp_ai & a, llarp_buffer_t * buff) return bencodeDict(buff) && bencodeDict_Int(buff, "c", a.rank) &&
{ bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) &&
return bencodeDict(buff) && bencodeDict_Bytes(buff, "d", a.dialect,
bencodeDict_Int(buff, "c", a.rank) && UStrLen(a.dialect, sizeof(a.dialect))) &&
bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) && bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) &&
bencodeDict_Bytes(buff, "d", a.dialect, UStrLen(a.dialect, sizeof(a.dialect))) && bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) &&
bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) && bencodeEnd(buff);
bencodeDict_Int(buff, "p", a.port) &&
bencodeDict_Int(buff, "v", 0) &&
bencodeEnd(buff);
}
} }
} // namespace llarp

@ -1,31 +1,25 @@
#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; llarp_ai_list *next = nullptr;
llarp_ai_list * next = nullptr;
}; };
static std::list<llarp_ai> ai_list_to_std(struct llarp_ai_list * l) static std::list<llarp_ai> ai_list_to_std(struct llarp_ai_list *l) {
{
std::list<llarp_ai> list; std::list<llarp_ai> list;
if(l && l->data) if (l && l->data) {
{ do {
do
{
llarp_ai copy; llarp_ai copy;
memcpy(&copy, l->data, sizeof(llarp_ai)); memcpy(&copy, l->data, sizeof(llarp_ai));
list.push_back(copy); list.push_back(copy);
l = l->next; l = l->next;
} } while (l->next);
while(l->next);
} }
return list; return list;
} }
#endif #endif

@ -1,70 +1,61 @@
#ifndef LLARP_BENCODE_HPP #ifndef LLARP_BENCODE_HPP
#define LLARP_BENCODE_HPP #define LLARP_BENCODE_HPP
#include <llarp/buffer.h>
#include <list> #include <list>
#include <llarp/buffer.h>
#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> template <typename ValType> bool BDecode(ValType &t, llarp_buffer_t *buff);
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'; return llarp_buffer_write(buff, &c, 1);
return llarp_buffer_write(buff, &c, 1); }
}
static bool bencodeList(llarp_buffer_t * buff)
{
static uint8_t c = 'l';
return llarp_buffer_write(buff, &c, 1);
}
static bool bencodeEnd(llarp_buffer_t * buff)
{
static char c = 'e';
return llarp_buffer_write(buff, &c, 1);
}
static bool bencodeDictKey(llarp_buffer_t * buff, const std::string & key) static bool bencodeList(llarp_buffer_t *buff) {
{ static uint8_t c = 'l';
std::string kstr = std::to_string(key.size()) + ":" + key; return llarp_buffer_write(buff, &c, 1);
return llarp_buffer_write(buff, kstr.c_str(), kstr.size()); }
}
template<typename IntType>
static bool bencodeDict_Int(llarp_buffer_t * buff, const std::string & key, IntType i)
{
std::string istr = "i" + std::to_string(i) + "e";
return bencodeDictKey(buff, key) &&
llarp_buffer_write(buff, istr.c_str(), istr.size());
}
static bool bencodeDict_Bytes(llarp_buffer_t * buff, const std::string & key, const void * data, size_t sz) static bool bencodeEnd(llarp_buffer_t *buff) {
{ static char c = 'e';
std::string sz_str = std::to_string(sz) + ":"; return llarp_buffer_write(buff, &c, 1);
return bencodeDictKey(buff, key) && }
llarp_buffer_write(buff, sz_str.c_str(), sz_str.size()) &&
llarp_buffer_write(buff, data, sz); static bool bencodeDictKey(llarp_buffer_t *buff, const std::string &key) {
} std::string kstr = std::to_string(key.size()) + ":" + key;
return llarp_buffer_write(buff, kstr.c_str(), kstr.size());
}
template <typename IntType>
static bool bencodeDict_Int(llarp_buffer_t *buff, const std::string &key,
IntType i) {
std::string istr = "i" + std::to_string(i) + "e";
return bencodeDictKey(buff, key) &&
llarp_buffer_write(buff, istr.c_str(), istr.size());
}
static bool bencodeDict_Bytes(llarp_buffer_t *buff, const std::string &key,
const void *data, size_t sz) {
std::string sz_str = std::to_string(sz) + ":";
return bencodeDictKey(buff, key) &&
llarp_buffer_write(buff, sz_str.c_str(), sz_str.size()) &&
llarp_buffer_write(buff, data, sz);
}
template<typename T> 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)
{ if (!BEncode(itr, buff))
for(const auto & itr : l) return false;
if(!BEncode(itr, buff)) return bencodeEnd(buff);
return false;
return bencodeEnd(buff);
}
return false;
} }
return false;
} }
} // namespace llarp
#endif #endif

@ -2,15 +2,13 @@
extern "C" { extern "C" {
size_t llarp_buffer_size_left(llarp_buffer_t * buff) size_t llarp_buffer_size_left(llarp_buffer_t *buff) {
{ std::ptrdiff_t diff = buff->cur - buff->base;
std::ptrdiff_t diff = buff->cur - buff->base; if (diff < 0) {
if(diff < 0) return 0;
{ } else if (diff > buff->sz)
return 0; return 0;
} else
else if(diff > buff->sz) return 0; return buff->sz - diff;
else return buff->sz - diff; }
}
} }

@ -1,75 +1,65 @@
#include <llarp/config.h>
#include "config.hpp" #include "config.hpp"
#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>
static Section find_section(Config & c, const std::string & name, const Section & fallback)
{
if(c.sections.find(name) == c.sections.end())
return fallback;
return c.sections[name].values;
}
bool Config::Load(const char * fname)
{
std::ifstream f;
f.open(fname);
if(f.is_open())
{
ini::Parser parser(f);
auto & top = parser.top();
router = find_section(top, "router", section_t{});
network = find_section(top, "network", section_t{});
netdb = find_section(top, "netdb", section_t{});
links = find_section(top, "links", section_t{});
return true;
}
return false;
};
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;
return c.sections[name].values;
} }
bool Config::Load(const char *fname) {
std::ifstream f;
f.open(fname);
if (f.is_open()) {
ini::Parser parser(f);
auto &top = parser.top();
router = find_section(top, "router", section_t{});
network = find_section(top, "network", section_t{});
netdb = find_section(top, "netdb", section_t{});
links = find_section(top, "links", section_t{});
return true;
}
return false;
};
extern "C" { } // namespace llarp
extern "C" {
void llarp_new_config(struct llarp_config **conf) {
void llarp_new_config(struct llarp_config ** conf) llarp_config *c = new llarp_config;
{ *conf = c;
llarp_config * c = new llarp_config; }
*conf = c;
}
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;
} }
void llarp_config_iter(struct llarp_config * conf, struct llarp_config_iterator * iter) void llarp_config_iter(struct llarp_config *conf,
{ struct llarp_config_iterator *iter) {
iter->conf = conf; iter->conf = conf;
std::map<std::string, llarp::Config::section_t&> sections = { std::map<std::string, llarp::Config::section_t &> sections = {
{"router", conf->impl.router}, {"router", conf->impl.router},
{"network", conf->impl.network}, {"network", conf->impl.network},
{"links", conf->impl.links}, {"links", conf->impl.links},
{"netdb", conf->impl.netdb} {"netdb", conf->impl.netdb}};
}; for (const auto &section : sections)
for(const auto & section : sections) for (const auto &item : section.second)
for(const auto & item : section.second) iter->visit(iter, section.first.c_str(), item.first.c_str(),
iter->visit(iter, section.first.c_str(), item.first.c_str(), item.second.c_str()); item.second.c_str());
} }
} }

@ -5,28 +5,23 @@
#include <llarp/config.h> #include <llarp/config.h>
namespace llarp namespace llarp {
{ struct Config {
struct Config typedef std::map<std::string, std::string> section_t;
{
typedef std::map<std::string, std::string> section_t;
section_t router;
section_t network;
section_t netdb;
section_t links;
bool Load(const char * fname); section_t router;
section_t network;
}; section_t netdb;
} section_t links;
bool Load(const char *fname);
};
} // namespace llarp
extern "C" { extern "C" {
struct llarp_config struct llarp_config {
{ llarp::Config impl;
llarp::Config impl; };
};
} }
#endif #endif

@ -1,90 +1,84 @@
#include <llarp/crypto.h> #include <llarp/crypto.h>
#include <sodium/crypto_stream_xchacha20.h>
#include <sodium/crypto_generichash.h> #include <sodium/crypto_generichash.h>
#include <sodium/crypto_scalarmult.h> #include <sodium/crypto_scalarmult.h>
#include <sodium/crypto_sign.h> #include <sodium/crypto_sign.h>
#include <sodium/crypto_stream_xchacha20.h>
namespace llarp namespace llarp {
{ namespace sodium {
namespace sodium bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n) {
{ uint8_t *base = (uint8_t *)buff.base;
bool xchacha20(llarp_buffer_t buff, llarp_sharedkey_t k, llarp_nounce_t n) return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
{ }
uint8_t * base = (uint8_t*)buff.base;
return crypto_stream_xchacha20_xor(base, base, buff.sz, n, k) == 0;
}
bool dh(llarp_sharedkey_t * shared, uint8_t * client_pk, uint8_t * server_pk, uint8_t * remote_key, uint8_t * local_key) 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; 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) return false; if (crypto_scalarmult(out, local_key, remote_key) == -1)
crypto_generichash_init(&h, NULL, 0U, outsz); return false;
crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t)); crypto_generichash_init(&h, NULL, 0U, outsz);
crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t)); crypto_generichash_update(&h, client_pk, sizeof(llarp_pubkey_t));
crypto_generichash_update(&h, out, crypto_scalarmult_BYTES); crypto_generichash_update(&h, server_pk, sizeof(llarp_pubkey_t));
crypto_generichash_final(&h, out, outsz); crypto_generichash_update(&h, out, crypto_scalarmult_BYTES);
return true; crypto_generichash_final(&h, out, outsz);
} return true;
}
bool dh_client(llarp_sharedkey_t * shared, llarp_pubkey_t pk, llarp_tunnel_nounce_t n, llarp_seckey_t sk) bool dh_client(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
{ 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)) {
{ return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1; TUNNOUNCESIZE) != -1;
} }
return false; return false;
} }
bool dh_server(llarp_sharedkey_t * shared, llarp_pubkey_t pk, llarp_tunnel_nounce_t n, llarp_seckey_t sk) bool dh_server(llarp_sharedkey_t *shared, llarp_pubkey_t pk,
{ 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)) {
{ return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n,
return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1; TUNNOUNCESIZE) != -1;
} }
return false; return false;
} }
bool hash(llarp_hash_t * result, llarp_buffer_t buff) bool hash(llarp_hash_t *result, llarp_buffer_t buff) {
{ const uint8_t *base = (const uint8_t *)buff.base;
const uint8_t * base = (const uint8_t *) buff.base; return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1;
return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1; }
}
bool hmac(llarp_hash_t * result, llarp_buffer_t buff, llarp_seckey_t secret) 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;
} }
bool sign(llarp_sig_t * result, llarp_seckey_t secret, llarp_buffer_t buff) 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; }
}
bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) {
{ const uint8_t *base = (const uint8_t *)buff.base;
const uint8_t * base = (const uint8_t *) buff.base; return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1;
}
}
} }
} // namespace sodium
} // namespace llarp
extern "C" { extern "C" {
void llarp_crypto_libsodium_init(struct llarp_crypto * c) void llarp_crypto_libsodium_init(struct llarp_crypto *c) {
{ c->xchacha20 = llarp::sodium::xchacha20;
c->xchacha20 = llarp::sodium::xchacha20; c->dh_client = llarp::sodium::dh_client;
c->dh_client = llarp::sodium::dh_client; c->dh_server = llarp::sodium::dh_server;
c->dh_server = llarp::sodium::dh_server; c->hash = llarp::sodium::hash;
c->hash = llarp::sodium::hash; c->hmac = llarp::sodium::hmac;
c->hmac = llarp::sodium::hmac; c->sign = llarp::sodium::sign;
c->sign = llarp::sodium::sign; c->verify = llarp::sodium::verify;
c->verify = llarp::sodium::verify; }
}
} }

@ -1,184 +1,154 @@
#include "mem.hpp"
#include <llarp/ev.h> #include <llarp/ev.h>
#include <uv.h> #include <uv.h>
#include "mem.hpp"
struct llarp_ev_loop struct llarp_ev_loop {
{
uv_loop_t _loop; uv_loop_t _loop;
static void * operator new(size_t sz) static void *operator new(size_t sz) {
{
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_ev_loop>()); return llarp_g_mem.alloc(sz, llarp::alignment<llarp_ev_loop>());
} }
static void operator delete(void * ptr) static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
{
llarp_g_mem.free(ptr); uv_loop_t *loop() { return &_loop; }
}
uv_loop_t * loop() { return &_loop; }
}; };
namespace llarp namespace llarp {
{ struct udp_listener {
struct udp_listener static void *operator new(size_t sz) {
{ return llarp_g_mem.alloc(sz, alignment<udp_listener>());
static void * operator new(size_t sz) }
{
return llarp_g_mem.alloc(sz, alignment<udp_listener>());
}
static void operator delete(void * ptr) static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
{
llarp_g_mem.free(ptr);
}
uv_udp_t _handle;
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);
}
/** called after closed */ uv_udp_t _handle;
void closed() struct llarp_udp_listener *listener;
{
if(listener->closed)
listener->closed(listener);
listener->impl = nullptr;
}
uv_udp_t * udp() { return &_handle; }
};
static void udp_alloc_cb(uv_handle_t * h, size_t sz, uv_buf_t * buf)
{
buf->base = static_cast<char *>(llarp_g_mem.alloc(sz, 1024));
buf->len = sz;
}
static void udp_recv_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) void recvfrom(const struct sockaddr *addr, char *buff, ssize_t sz) {
{ if (listener->recvfrom)
udp_listener * l = static_cast<udp_listener *>(handle->data); listener->recvfrom(listener, addr, buff, sz);
l->recvfrom(addr, buf->base, nread);
llarp_g_mem.free(buf->base);
} }
static void udp_close_cb(uv_handle_t * handle) /** called after closed */
{ void closed() {
udp_listener * l = static_cast<udp_listener *>(handle->data); if (listener->closed)
l->closed(); listener->closed(listener);
delete l; listener->impl = nullptr;
} }
uv_udp_t *udp() { return &_handle; }
};
static void udp_alloc_cb(uv_handle_t *h, size_t sz, uv_buf_t *buf) {
buf->base = static_cast<char *>(llarp_g_mem.alloc(sz, 1024));
buf->len = sz;
} }
static void udp_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
const struct sockaddr *addr, unsigned flags) {
udp_listener *l = static_cast<udp_listener *>(handle->data);
l->recvfrom(addr, buf->base, nread);
llarp_g_mem.free(buf->base);
}
namespace llarp static void udp_close_cb(uv_handle_t *handle) {
{ udp_listener *l = static_cast<udp_listener *>(handle->data);
l->closed();
delete l;
}
} // namespace llarp
static void ev_handle_async_closed(uv_handle_t * handle) namespace llarp {
{
struct llarp_ev_job * ev = static_cast<llarp_ev_job *>(handle->data); static void ev_handle_async_closed(uv_handle_t *handle) {
llarp_g_mem.free(ev); struct llarp_ev_job *ev = static_cast<llarp_ev_job *>(handle->data);
llarp_g_mem.free(handle); llarp_g_mem.free(ev);
} llarp_g_mem.free(handle);
static void ev_handle_async(uv_async_t * handle)
{
struct llarp_ev_job * ev = static_cast<llarp_ev_job *>(handle->data);
ev->work(ev);
uv_close((uv_handle_t *)handle, ev_handle_async_closed);
}
} }
static void ev_handle_async(uv_async_t *handle) {
struct llarp_ev_job *ev = static_cast<llarp_ev_job *>(handle->data);
ev->work(ev);
uv_close((uv_handle_t *)handle, ev_handle_async_closed);
}
} // 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) {
{ *ev = new llarp_ev_loop;
*ev = new llarp_ev_loop; if (*ev) {
if (*ev) uv_loop_init((*ev)->loop());
{
uv_loop_init((*ev)->loop());
}
}
void llarp_ev_loop_free(struct llarp_ev_loop ** ev)
{
if(*ev)
{
uv_loop_close((*ev)->loop());
llarp_g_mem.free(*ev);
}
*ev = nullptr;
} }
}
int llarp_ev_loop_run(struct llarp_ev_loop * ev) void llarp_ev_loop_free(struct llarp_ev_loop **ev) {
{ if (*ev) {
return uv_run(ev->loop(), UV_RUN_DEFAULT); uv_loop_close((*ev)->loop());
llarp_g_mem.free(*ev);
} }
*ev = nullptr;
int llarp_ev_add_udp_listener(struct llarp_ev_loop * ev, struct llarp_udp_listener * listener) }
{
sockaddr_in6 addr; int llarp_ev_loop_run(struct llarp_ev_loop *ev) {
uv_ip6_addr(listener->host, listener->port, &addr); return uv_run(ev->loop(), UV_RUN_DEFAULT);
int ret = 0; }
llarp::udp_listener * l = new llarp::udp_listener;
listener->impl = l; int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
l->udp()->data = l; struct llarp_udp_listener *listener) {
l->listener = listener; sockaddr_in6 addr;
uv_ip6_addr(listener->host, listener->port, &addr);
ret = uv_udp_init(ev->loop(), l->udp()); int ret = 0;
if (ret == 0) llarp::udp_listener *l = new llarp::udp_listener;
{ listener->impl = l;
ret = uv_udp_bind(l->udp(), (const sockaddr *)&addr, 0); l->udp()->data = l;
if (ret == 0) l->listener = listener;
{
ret = uv_udp_recv_start(l->udp(), llarp::udp_alloc_cb, llarp::udp_recv_cb); ret = uv_udp_init(ev->loop(), l->udp());
} if (ret == 0) {
ret = uv_udp_bind(l->udp(), (const sockaddr *)&addr, 0);
if (ret == 0) {
ret =
uv_udp_recv_start(l->udp(), llarp::udp_alloc_cb, llarp::udp_recv_cb);
} }
return ret;
} }
return ret;
}
int llarp_ev_close_udp_listener(struct llarp_udp_listener * listener) int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) {
{ int ret = -1;
int ret = -1; if (listener) {
if(listener) llarp::udp_listener *l = static_cast<llarp::udp_listener *>(listener->impl);
{ if (l) {
llarp::udp_listener * l = static_cast<llarp::udp_listener*>(listener->impl); if (!uv_udp_recv_stop(l->udp())) {
if(l) l->closed();
{ delete l;
if(!uv_udp_recv_stop(l->udp())) ret = 0;
{
l->closed();
delete l;
ret = 0;
}
} }
} }
return ret;
}
void llarp_ev_loop_stop(struct llarp_ev_loop * loop)
{
uv_stop(loop->loop());
} }
return ret;
}
bool llarp_ev_async(struct llarp_ev_loop * loop, struct llarp_ev_job job) void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { uv_stop(loop->loop()); }
{
struct llarp_ev_job * job_copy = static_cast<struct llarp_ev_job *>(llarp_g_mem.alloc(sizeof(struct llarp_ev_job), llarp::alignment<llarp_ev_job>())); bool llarp_ev_async(struct llarp_ev_loop *loop, struct llarp_ev_job job) {
job_copy->work = job.work; struct llarp_ev_job *job_copy =
job_copy->loop = loop; static_cast<struct llarp_ev_job *>(llarp_g_mem.alloc(
job_copy->user = job.user; sizeof(struct llarp_ev_job), llarp::alignment<llarp_ev_job>()));
uv_async_t * async = static_cast<uv_async_t *>(llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment<uv_async_t>())); job_copy->work = job.work;
async->data = job_copy; job_copy->loop = loop;
if(uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 && uv_async_send(async)) job_copy->user = job.user;
return true; uv_async_t *async = static_cast<uv_async_t *>(
else llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment<uv_async_t>()));
{ async->data = job_copy;
llarp_g_mem.free(job_copy); if (uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 &&
llarp_g_mem.free(async); uv_async_send(async))
return false; return true;
} else {
llarp_g_mem.free(job_copy);
llarp_g_mem.free(async);
return false;
} }
} }
}

@ -2,20 +2,16 @@
#include "bencode.hpp" #include "bencode.hpp"
extern "C" { extern "C" {
bool llarp_xi_bencode(struct llarp_xi * xi, llarp_buffer_t * buff) bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
{ return llarp::BEncode(*xi, buff);
return llarp::BEncode(*xi, buff); }
}
} }
namespace llarp namespace llarp {
{ bool BEncode(const llarp_xi &xi, llarp_buffer_t *buff) {
bool BEncode(const llarp_xi & xi, llarp_buffer_t * buff) return bencodeDict(buff) &&
{ bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) &&
return bencodeDict(buff) && bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) &&
bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) && bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff);
bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) &&
bencodeDict_Int(buff, "v", 0) &&
bencodeEnd(buff);
}
} }
} // namespace llarp

@ -1,18 +1,16 @@
#ifndef LLARP_XI_HPP #ifndef LLARP_XI_HPP
#define LLARP_XI_HPP #define LLARP_XI_HPP
#include <llarp/exit_info.h>
#include <list> #include <list>
#include <llarp/exit_info.h>
struct llarp_xi_list struct llarp_xi_list {
{
std::list<llarp_xi> list; std::list<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;
} }

@ -19,27 +19,26 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
* *
*/ */
#ifndef INI_HPP #ifndef INI_HPP
#define INI_HPP #define INI_HPP
#include <cassert> #include <cassert>
#include <map> #include <cstring>
#include <fstream>
#include <iostream>
#include <list> #include <list>
#include <map>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <cstring>
#include <iostream>
#include <fstream>
namespace ini { namespace ini {
struct Level struct Level {
{
Level() : parent(NULL), depth(0) {} Level() : parent(NULL), depth(0) {}
Level(Level* p) : parent(p), depth(0) {} Level(Level *p) : parent(p), depth(0) {}
typedef std::map<std::string, std::string> value_map_t; typedef std::map<std::string, std::string> value_map_t;
typedef std::map<std::string, Level> section_map_t; typedef std::map<std::string, Level> section_map_t;
@ -49,97 +48,95 @@ struct Level
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;
const std::string& operator[](const std::string& name) { return values[name]; } const std::string &operator[](const std::string &name) {
Level& operator()(const std::string& name) { return sections[name]; } return values[name];
}
Level &operator()(const std::string &name) { return sections[name]; }
}; };
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_;
std::string line_; std::string line_;
size_t ln_; size_t ln_;
}; };
inline void inline void Parser::err(const char *s) {
Parser::err(const char* s)
{
char buf[256]; char buf[256];
sprintf(buf, "%s on line #%ld", s, ln_); sprintf(buf, "%s on line #%ld", s, ln_);
throw std::runtime_error(buf); throw std::runtime_error(buf);
} }
inline std::string trim(const std::string& s) inline std::string trim(const std::string &s) {
{
char p[] = " \t\r\n"; char p[] = " \t\r\n";
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])) break; if (!strchr(p, s[sp]))
break;
for (; ep >= 0; --ep) for (; ep >= 0; --ep)
if (!strchr(p, s[ep])) break; if (!strchr(p, s[ep]))
return s.substr(sp, ep-sp+1); break;
return s.substr(sp, ep - sp + 1);
} }
inline inline Parser::Parser(const char *fn) : f0_(fn), f_(&f0_), ln_(0) {
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_);
} }
inline void inline void Parser::parseSLine(std::string &sname, size_t &depth) {
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] != '[') break; if (line_[depth] != '[')
break;
sname = line_.substr(depth, line_.length() - 2*depth); sname = line_.substr(depth, line_.length() - 2 * depth);
} }
inline void inline void Parser::parse(Level &l) {
Parser::parse(Level& l)
{
while (std::getline(*f_, line_)) { while (std::getline(*f_, line_)) {
++ln_; ++ln_;
if (line_[0] == '#' || line_[0] == ';') continue; if (line_[0] == '#' || line_[0] == ';')
continue;
line_ = trim(line_); line_ = trim(line_);
if (line_.empty()) continue; if (line_.empty())
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) lp = lp->parent; for (size_t i = 0; i < n; ++i)
lp = lp->parent;
parent = lp; parent = lp;
lp = &lp->sections[sname]; lp = &lp->sections[sname];
} }
@ -155,9 +152,9 @@ Parser::parse(Level& l)
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 = std::pair<Level::value_map_t::const_iterator, bool> res = l.values.insert(
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);
@ -165,22 +162,28 @@ Parser::parse(Level& l)
} }
} }
inline void inline void Parser::dump(std::ostream &s, const Level &l,
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 (Level::values_t::const_iterator it = l.ordered_values.begin(); it != l.ordered_values.end(); ++it) 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; s << (*it)->first << '=' << (*it)->second << std::endl;
for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); it != l.ordered_sections.end(); ++it) { for (Level::sections_t::const_iterator it = l.ordered_sections.begin();
assert((*it)->second.depth == l.depth+1); it != l.ordered_sections.end(); ++it) {
assert((*it)->second.depth == l.depth + 1);
dump(s, (*it)->second, (*it)->first); dump(s, (*it)->second, (*it)->first);
} }
} }
} } // namespace ini
#endif // INI_HPP #endif // INI_HPP

@ -2,35 +2,32 @@
#include <cstring> #include <cstring>
#include <llarp/time.h> #include <llarp/time.h>
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, sizeof(addr0.sin6_addr)) && addr0.sin6_port < addr1.sin6_port; sizeof(addr0.sin6_addr)) &&
addr0.sin6_port < addr1.sin6_port;
} }
extern "C"{ extern "C" {
struct llarp_link * llarp_link_alloc() struct llarp_link *llarp_link_alloc() {
{ return new llarp_link;
return new llarp_link; }
}
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;
} }
struct llarp_udp_listener * llarp_link_udp_listener(struct llarp_link *l) struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *l) {
{ return &l->listener;
return &l->listener; }
}
bool llarp_link_configure(struct llarp_link * link, const char * ifname, int af) bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af) {
{ return false;
return false; }
}
void llarp_link_stop(struct llarp_link * link) void llarp_link_stop(struct llarp_link *link) {
{ llarp_ev_close_udp_listener(&link->listener);
llarp_ev_close_udp_listener(&link->listener); }
}
} }

@ -1,33 +1,26 @@
#ifndef LLARP_LINK_HPP #ifndef LLARP_LINK_HPP
#define LLARP_LINK_HPP #define LLARP_LINK_HPP
#include <netinet/in.h>
#include <llarp/crypto.h>
#include <cstdint> #include <cstdint>
#include <functional>
#include <llarp/crypto.h>
#include <map> #include <map>
#include <memory> #include <memory>
#include <functional> #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)
{
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_link>()); return llarp_g_mem.alloc(sz, llarp::alignment<llarp_link>());
} }
static void operator delete(void * ptr) static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
{
llarp_g_mem.free(ptr);
}
struct sockaddr_in6 localaddr; struct sockaddr_in6 localaddr;
int af; int af;
llarp_udp_listener listener; llarp_udp_listener listener;
}; };
#endif #endif

@ -1,8 +1,5 @@
#include <llarp/mem.h> #include <llarp/mem.h>
extern "C" { extern "C" {
struct llarp_alloc llarp_g_mem = { struct llarp_alloc llarp_g_mem = {.alloc = nullptr, .free = nullptr};
.alloc = nullptr,
.free = nullptr
};
} }

@ -1,14 +1,11 @@
#ifndef LLARP_MEM_HPP #ifndef LLARP_MEM_HPP
#define LLARP_MEM_HPP #define LLARP_MEM_HPP
#include <llarp/mem.h>
#include <cmath> #include <cmath>
namespace llarp #include <llarp/mem.h>
{ namespace llarp {
template<typename T> template <typename T> static constexpr size_t alignment() {
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))));
}
} }
} // namespace llarp
#endif #endif

@ -1,23 +1,20 @@
#include <llarp/mem.h>
#include <jemalloc/jemalloc.h> #include <jemalloc/jemalloc.h>
#include <llarp/mem.h>
namespace llarp namespace llarp {
{ static void *jem_malloc(size_t sz, size_t align) {
static void * jem_malloc(size_t sz, size_t align) return mallocx(sz, MALLOCX_ALIGN(align));
{ }
return mallocx(sz, MALLOCX_ALIGN(align));
}
static void jem_free(void * ptr) static void jem_free(void *ptr) {
{ if (ptr)
if(ptr) free(ptr); free(ptr);
}
} }
} // namespace llarp
extern "C" { extern "C" {
void llarp_mem_jemalloc() void llarp_mem_jemalloc() {
{ llarp_g_mem.alloc = llarp::jem_malloc;
llarp_g_mem.alloc = llarp::jem_malloc; llarp_g_mem.free = llarp::jem_free;
llarp_g_mem.free = llarp::jem_free; }
}
} }

@ -1,26 +1,24 @@
#include <llarp/mem.h> #include <llarp/mem.h>
namespace llarp namespace llarp {
{ void *std_malloc(size_t sz, size_t align) {
void * std_malloc(size_t sz, size_t align) (void)align;
{ void *ptr = malloc(sz);
(void) align; if (ptr)
void * ptr = malloc(sz); return ptr;
if(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
extern "C" { extern "C" {
void llarp_mem_std() void llarp_mem_std() {
{ llarp_g_mem.alloc = llarp::std_malloc;
llarp_g_mem.alloc = llarp::std_malloc; llarp_g_mem.free = llarp::std_free;
llarp_g_mem.free = llarp::std_free; }
}
} }

@ -1,133 +1,105 @@
#include <llarp/router.h>
#include <llarp/link.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, const char * key, const char * val); const char *key, const char *val);
struct router_links struct router_links {
{ struct llarp_link *link = nullptr;
struct llarp_link * link = nullptr; struct router_links *next = nullptr;
struct router_links * next = nullptr; };
};
} } // namespace llarp
struct llarp_router struct llarp_router {
{ struct llarp_threadpool *tp;
struct llarp_threadpool * tp;
llarp::router_links links; llarp::router_links links;
llarp_crypto crypto; llarp_crypto crypto;
static void *operator new(size_t sz) {
static void * operator new(size_t sz)
{
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>()); return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
} }
static void operator delete(void * ptr) static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
{
llarp_g_mem.free(ptr);
}
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};
else else
head->link = link; head->link = link;
} }
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);
}
void Close()
{
ForEachLink(llarp_link_stop);
} }
};
void Close() { ForEachLink(llarp_link_stop); }
};
extern "C" { extern "C" {
struct llarp_router * llarp_init_router(struct llarp_threadpool * tp) struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) {
{ llarp_router *router = new llarp_router;
llarp_router * router = new llarp_router; router->tp = tp;
router->tp = tp; llarp_crypto_libsodium_init(&router->crypto);
llarp_crypto_libsodium_init(&router->crypto); return router;
return router; }
}
int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf) int llarp_configure_router(struct llarp_router *router,
{ struct llarp_config *conf) {
llarp_config_iterator iter; llarp_config_iterator iter;
iter.user = router; iter.user = router;
iter.visit = llarp::router_iter_config; iter.visit = llarp::router_iter_config;
llarp_config_iter(conf, &iter); llarp_config_iter(conf, &iter);
return 0; return 0;
} }
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop) void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) {
{ router->ForEachLink([loop](llarp_link *link) {
router->ForEachLink([loop](llarp_link * link) { llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link)); });
}); }
}
void llarp_stop_router(struct llarp_router * router) void llarp_stop_router(struct llarp_router *router) { router->Close(); }
{
router->Close();
}
void llarp_free_router(struct llarp_router ** router) void llarp_free_router(struct llarp_router **router) {
{ if (*router) {
if(*router) (*router)->ForEachLink([](llarp_link *link) { llarp_g_mem.free(link); });
{ delete *router;
(*router)->ForEachLink([](llarp_link * link) { llarp_g_mem.free(link); });
delete *router;
}
*router = nullptr;
} }
*router = nullptr;
}
} }
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, const char * key, const char * val) const char *key, const char *val) {
{ llarp_router *self = static_cast<llarp_router *>(iter->user);
llarp_router * self = static_cast<llarp_router *>(iter->user); if (StrEq(section, "links")) {
if (StrEq(section, "links")) if (StrEq(val, "ip")) {
{ struct llarp_link *link = llarp_link_alloc();
if(StrEq(val, "ip")) if (llarp_link_configure(link, key, AF_INET6))
{ self->AddLink(link);
struct llarp_link * link = llarp_link_alloc(); else {
if(llarp_link_configure(link, key, AF_INET6)) llarp_link_free(&link);
self->AddLink(link); printf("failed to configure %s link for %s\n", val, key);
else
{
llarp_link_free(&link);
printf("failed to configure %s link for %s\n", val, key);
}
}
else if (StrEq(val, "eth"))
{
/** todo: ethernet link */
} }
} else if (StrEq(val, "eth")) {
/** todo: ethernet link */
} }
} }
} }
} // namespace llarp

@ -1,24 +1,20 @@
#include "router_contact.hpp" #include "router_contact.hpp"
#include "exit_info.hpp"
#include "address_info.hpp" #include "address_info.hpp"
#include "bencode.hpp" #include "bencode.hpp"
#include "exit_info.hpp"
extern "C" { extern "C" {}
}
namespace llarp namespace llarp {
{ bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
bool BEncode(const llarp_rc & a, llarp_buffer_t * buff) std::list<llarp_ai> addresses = ai_list_to_std(a.addrs);
{ std::list<llarp_xi> exits = xi_list_to_std(a.exits);
std::list<llarp_ai> addresses = ai_list_to_std(a.addrs); return bencodeDict(buff) && bencodeDictKey(buff, "a") &&
std::list<llarp_xi> exits = xi_list_to_std(a.exits); BEncode(addresses, buff) &&
return bencodeDict(buff) && bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) &&
bencodeDictKey(buff, "a") && BEncode(addresses, buff) && bencodeDict_Int(buff, "v", 0) && bencodeDictKey(buff, "x") &&
bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) && BEncode(exits, buff) &&
bencodeDict_Int(buff, "v", 0) && bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
bencodeDictKey(buff, "x") && BEncode(exits, buff) && bencodeEnd(buff);
bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
bencodeEnd(buff);
}
} }
} // namespace llarp

@ -2,5 +2,4 @@
#define LLARP_RC_HPP #define LLARP_RC_HPP
#include <llarp/router_contact.h> #include <llarp/router_contact.h>
#endif #endif

@ -2,30 +2,25 @@
#define LIBLLARP_STR_HPP #define LIBLLARP_STR_HPP
#include <cstring> #include <cstring>
namespace llarp namespace llarp {
{ static bool StrEq(const char *s1, const char *s2) {
static bool StrEq(const char * s1, const char * s2) size_t sz1 = strlen(s1);
{ size_t sz2 = strlen(s2);
size_t sz1 = strlen(s1); if (sz1 == sz2) {
size_t sz2 = strlen(s2); return strncmp(s1, s2, sz1) == 0;
if (sz1 == sz2) } else
{ return false;
return strncmp(s1, s2, sz1) == 0; }
}
else
return false;
}
static size_t UStrLen(const uint8_t * data, size_t maxsz) static size_t UStrLen(const uint8_t *data, size_t maxsz) {
{ size_t sz = 0;
size_t sz = 0; while (*data++) {
while(*data++) sz++;
{ if (maxsz >= sz)
sz ++; return maxsz;
if(maxsz >= sz) return maxsz;
}
return sz;
} }
return sz;
} }
} // namespace llarp
#endif #endif

@ -1,72 +1,62 @@
#include "threadpool.hpp" #include "threadpool.hpp"
#include <iostream> #include <iostream>
namespace llarp namespace llarp {
{ namespace thread {
namespace thread Pool::Pool(size_t workers) {
{ stop.store(true);
Pool::Pool(size_t workers) while (workers--) {
{ threads.emplace_back([this] {
stop.store(true); for (;;) {
while(workers--) llarp_thread_job job;
{ {
threads.emplace_back( lock_t lock(this->queue_mutex);
[this] this->condition.wait(
{ lock, [this] { return this->stop || !this->jobs.empty(); });
for(;;) if (this->stop && this->jobs.empty())
{ return;
llarp_thread_job job; job = std::move(this->jobs.front());
{ this->jobs.pop();
lock_t lock(this->queue_mutex); }
this->condition.wait(lock, [this]{ return this->stop || !this->jobs.empty(); }); // do work
if(this->stop && this->jobs.empty()) job.work(job.user);
return; // inform result if needed
job = std::move(this->jobs.front()); if (job.result && job.result->loop)
this->jobs.pop(); if (!llarp_ev_async(job.result->loop, *job.result)) {
} std::cerr << "failed to queue result in thread worker" << std::endl;
// do work }
job.work(job.user);
// inform result if needed
if(job.result && job.result->loop)
if(!llarp_ev_async(job.result->loop, *job.result))
{
std::cerr << "failed to queue result in thread worker" << std::endl;
}
}
});
} }
} });
}
}
void Pool::Join() void Pool::Join() {
{ {
{ lock_t lock(queue_mutex);
lock_t lock(queue_mutex); stop.store(true);
stop.store(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) {
{ {
{ lock_t lock(queue_mutex);
lock_t lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
jobs.emplace(job); // don't allow enqueueing after stopping the pool
} if (stop)
condition.notify_one(); throw std::runtime_error("enqueue on stopped ThreadPool");
}
jobs.emplace(job);
} }
condition.notify_one();
} }
struct llarp_threadpool } // namespace thread
{ } // namespace llarp
struct llarp_threadpool {
llarp::thread::Pool impl; llarp::thread::Pool impl;
llarp_threadpool(int workers) : impl(workers) {} llarp_threadpool(int workers) : impl(workers) {}
@ -74,23 +64,17 @@ struct llarp_threadpool
extern "C" { extern "C" {
struct llarp_threadpool * llarp_init_threadpool(int workers) struct llarp_threadpool *llarp_init_threadpool(int workers) {
{ if (workers > 0)
if(workers > 0) return new llarp_threadpool(workers);
return new llarp_threadpool(workers); else
else return nullptr;
return nullptr; }
}
void llarp_threadpool_join(struct llarp_threadpool * pool) void llarp_threadpool_join(struct llarp_threadpool *pool) { pool->impl.Join(); }
{
pool->impl.Join();
}
void llarp_free_threadpool(struct llarp_threadpool ** pool) void llarp_free_threadpool(struct llarp_threadpool **pool) {
{ delete *pool;
delete *pool; *pool = nullptr;
*pool = nullptr; }
}
} }

@ -6,31 +6,28 @@
#include <atomic> #include <atomic>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <thread>
#include <queue> #include <queue>
#include <thread>
#include <vector> #include <vector>
namespace llarp namespace llarp {
{ namespace thread {
namespace thread typedef std::mutex mtx_t;
{ typedef std::unique_lock<mtx_t> lock_t;
typedef std::mutex mtx_t; struct Pool {
typedef std::unique_lock<mtx_t> lock_t;
struct Pool Pool(size_t sz);
{ void QueueJob(llarp_thread_job job);
void Join();
Pool(size_t sz); std::vector<std::thread> threads;
void QueueJob(llarp_thread_job job); std::queue<llarp_thread_job> jobs;
void Join();
std::vector<std::thread> threads; mtx_t queue_mutex;
std::queue<llarp_thread_job> jobs; std::condition_variable condition;
std::atomic<bool> stop;
};
mtx_t queue_mutex; } // namespace thread
std::condition_variable condition; } // namespace llarp
std::atomic<bool> stop;
};
}
}
#endif #endif

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

Loading…
Cancel
Save