diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..a8a7ddd4a --- /dev/null +++ b/.clang-format @@ -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 +... + diff --git a/Makefile b/Makefile index 72fb96aa9..c0297e813 100644 --- a/Makefile +++ b/Makefile @@ -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_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_LIBS = $(shell pkg-config --libs libsodium) @@ -31,6 +35,9 @@ REQUIRED_LDFLAGS = $(LDFLAGS) -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) all: build +format: $(HDRS) $(SRCS) + $(FORMAT) -i $^ + build: $(EXE) $(EXE): $(DAEMON_OBJ) $(STATIC_LIB) diff --git a/daemon/main.c b/daemon/main.c index 1082948d8..79cb307be 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -2,32 +2,27 @@ #include #include -struct llarp_main -{ - struct llarp_router * router; - struct llarp_threadpool * tp; - struct llarp_config * config; - struct llarp_ev_loop * mainloop; +struct llarp_main { + struct llarp_router *router; + struct llarp_threadpool *tp; + struct llarp_config *config; + struct llarp_ev_loop *mainloop; }; -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; - if(!strcmp(section, "threadpool")) - { - if(!strcmp(key, "workers")) - { +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; + if (!strcmp(section, "threadpool")) { + if (!strcmp(key, "workers")) { int workers = atoi(val); - if(!m->tp && workers > 0) - { + if (!m->tp && workers > 0) { m->tp = llarp_init_threadpool(workers); } } } } -int shutdown_llarp(struct llarp_main * m) -{ +int shutdown_llarp(struct llarp_main *m) { printf("Shutting down ."); llarp_stop_router(m->router); printf("."); @@ -43,42 +38,34 @@ int shutdown_llarp(struct llarp_main * m) printf("."); llarp_free_threadpool(&m->tp); printf(".\n"); - } -int main(int argc, char * argv[]) -{ - struct llarp_main llarp = { - NULL, NULL, NULL, NULL - }; - const char * conffname = "daemon.ini"; +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]; llarp_mem_jemalloc(); llarp_new_config(&llarp.config); llarp_ev_loop_alloc(&llarp.mainloop); 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); struct llarp_config_iterator iter; iter.user = &llarp; iter.visit = iter_main_config; llarp_config_iter(llarp.config, &iter); - if(!llarp.tp) + if (!llarp.tp) llarp.tp = llarp_init_threadpool(2); 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"); llarp_run_router(llarp.router, llarp.mainloop); llarp_ev_loop_run(llarp.mainloop); - } - else + } else printf("Failed to configure router\n"); - } - else + } else printf("Failed to load config %s\n", conffname); - + return shutdown_llarp(&llarp); } diff --git a/include/llarp/address_info.h b/include/llarp/address_info.h index 5a653e3b4..66a0fd9f7 100644 --- a/include/llarp/address_info.h +++ b/include/llarp/address_info.h @@ -10,45 +10,44 @@ extern "C" { #endif #define MAX_AI_DIALECT_SIZE 5 - - struct llarp_ai - { - uint16_t rank; - llarp_pubkey_t enc_key; - uint8_t dialect[MAX_AI_DIALECT_SIZE+1]; - struct in6_addr ip; - uint16_t port; - }; - - /** allocator for address info */ - extern struct llarp_alloc * llarp_ai_alloc; - - bool llarp_ai_bencode(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 * llarp_ai_list_new(); - void llarp_ai_list_free(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); - 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_list_iter - { - void * user; - /** set by llarp_ai_list_iterate() */ - struct llarp_ai_list * list; - /** 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); - + +struct llarp_ai { + uint16_t rank; + llarp_pubkey_t enc_key; + uint8_t dialect[MAX_AI_DIALECT_SIZE + 1]; + struct in6_addr ip; + uint16_t port; +}; + +/** allocator for address info */ +extern struct llarp_alloc *llarp_ai_alloc; + +bool llarp_ai_bencode(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 *llarp_ai_list_new(); +void llarp_ai_list_free(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); +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_list_iter { + void *user; + /** set by llarp_ai_list_iterate() */ + struct llarp_ai_list *list; + /** 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); + #ifdef __cplusplus } #endif - + #endif diff --git a/include/llarp/buffer.h b/include/llarp/buffer.h index 1e3a79248..b9f77cf61 100644 --- a/include/llarp/buffer.h +++ b/include/llarp/buffer.h @@ -1,22 +1,22 @@ #ifndef LLARP_BUFFER_H_ #define LLARP_BUFFER_H_ -#include #include +#include #ifdef __cplusplus extern "C" { #endif - typedef struct llarp_buffer_t { - char * base; - size_t sz; - char * cur; - } llarp_buffer_t; +typedef struct llarp_buffer_t { + char *base; + size_t sz; + char *cur; +} 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 } #endif - + #endif diff --git a/include/llarp/config.h b/include/llarp/config.h index ef27c2813..52d1c6cb3 100644 --- a/include/llarp/config.h +++ b/include/llarp/config.h @@ -5,25 +5,26 @@ extern "C" { #endif - struct llarp_config; +struct llarp_config; - void llarp_new_config(struct llarp_config ** conf); - void llarp_free_config(struct llarp_config ** conf); +void llarp_new_config(struct llarp_config **conf); +void llarp_free_config(struct llarp_config **conf); - /** @brief return -1 on fail otherwiwse 0 */ - int llarp_load_config(struct llarp_config * conf, const char * fname); +/** @brief return -1 on fail otherwiwse 0 */ +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 } #endif diff --git a/include/llarp/crypto.h b/include/llarp/crypto.h index e5cc16d0d..8b24ed913 100644 --- a/include/llarp/crypto.h +++ b/include/llarp/crypto.h @@ -1,8 +1,8 @@ #ifndef LLARP_CRYPTO_H_ #define LLARP_CRYPTO_H_ #include -#include #include +#include #ifdef __cplusplus extern "C" { #endif @@ -16,30 +16,31 @@ extern "C" { #define SIGSIZE 64 #define TUNNOUNCESIZE 32 - typedef uint8_t llarp_pubkey_t[PUBKEYSIZE]; - typedef uint8_t llarp_seckey_t[SECKEYSIZE]; - typedef uint8_t llarp_nounce_t[NOUNCESIZE]; - typedef uint8_t llarp_sharedkey_t[SHAREDKEYSIZE]; - typedef uint8_t llarp_hash_t[HASHSIZE]; - typedef uint8_t llarp_hmacsec_t[HMACSECSIZE]; - typedef uint8_t llarp_sig_t[SIGSIZE]; - typedef uint8_t llarp_tunnel_nounce_t[TUNNOUNCESIZE]; +typedef uint8_t llarp_pubkey_t[PUBKEYSIZE]; +typedef uint8_t llarp_seckey_t[SECKEYSIZE]; +typedef uint8_t llarp_nounce_t[NOUNCESIZE]; +typedef uint8_t llarp_sharedkey_t[SHAREDKEYSIZE]; +typedef uint8_t llarp_hash_t[HASHSIZE]; +typedef uint8_t llarp_hmacsec_t[HMACSECSIZE]; +typedef uint8_t llarp_sig_t[SIGSIZE]; +typedef uint8_t llarp_tunnel_nounce_t[TUNNOUNCESIZE]; - struct llarp_crypto - { - bool (*xchacha20)(llarp_buffer_t, llarp_sharedkey_t, llarp_nounce_t); - bool (*dh_client)(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, llarp_seckey_t); - bool (*hash)(llarp_hash_t *, llarp_buffer_t); - bool (*hmac)(llarp_hash_t *, llarp_buffer_t, llarp_hmacsec_t); - bool (*sign)(llarp_sig_t *, llarp_seckey_t, llarp_buffer_t); - bool (*verify)(llarp_pubkey_t, llarp_buffer_t, llarp_sig_t); - }; +struct llarp_crypto { + bool (*xchacha20)(llarp_buffer_t, llarp_sharedkey_t, llarp_nounce_t); + bool (*dh_client)(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, + llarp_seckey_t); + bool (*hash)(llarp_hash_t *, llarp_buffer_t); + bool (*hmac)(llarp_hash_t *, llarp_buffer_t, llarp_hmacsec_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 } #endif - + #endif diff --git a/include/llarp/ev.h b/include/llarp/ev.h index fd9d04615..351c8edbe 100644 --- a/include/llarp/ev.h +++ b/include/llarp/ev.h @@ -1,55 +1,54 @@ #ifndef LLARP_EV_H #define LLARP_EV_H -#include -#include #include +#include +#include #include #ifdef __cplusplus extern "C" { #endif - struct llarp_ev_loop; - - void llarp_ev_loop_alloc(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); - /** stop event loop and wait for it to complete all jobs */ - void llarp_ev_loop_stop(struct llarp_ev_loop * ev); - - struct llarp_udp_listener - { - char * host; - uint16_t port; - void * user; - void * impl; - void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *, ssize_t); - 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_close_udp_listener(struct llarp_udp_listener * listener); - - - struct llarp_ev_job - { - /** the loop this job belongs to */ - struct llarp_ev_loop * loop; - /** user data */ - void * user; - /** work is called async when ready in the event loop thread */ - void (*work)(struct llarp_ev_job *); - }; - - /** - call work async in event loop thread (thread safe) - return true if we queued the job otherwise return false - */ - bool llarp_ev_async(struct llarp_ev_loop * ev, struct llarp_ev_job job); - +struct llarp_ev_loop; + +void llarp_ev_loop_alloc(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); +/** stop event loop and wait for it to complete all jobs */ +void llarp_ev_loop_stop(struct llarp_ev_loop *ev); + +struct llarp_udp_listener { + char *host; + uint16_t port; + void *user; + void *impl; + void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *, + ssize_t); + 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_close_udp_listener(struct llarp_udp_listener *listener); + +struct llarp_ev_job { + /** the loop this job belongs to */ + struct llarp_ev_loop *loop; + /** user data */ + void *user; + /** work is called async when ready in the event loop thread */ + void (*work)(struct llarp_ev_job *); +}; + +/** + call work async in event loop thread (thread safe) + return true if we queued the job otherwise return false + */ +bool llarp_ev_async(struct llarp_ev_loop *ev, struct llarp_ev_job job); + #ifdef __cplusplus } #endif diff --git a/include/llarp/exit_info.h b/include/llarp/exit_info.h index 0eb35af12..e09c7e24b 100644 --- a/include/llarp/exit_info.h +++ b/include/llarp/exit_info.h @@ -6,19 +6,17 @@ #ifdef __cplusplus extern "C" { #endif - - struct llarp_xi - { - in6_addr address; - in6_addr netmask; - }; - - 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; - +struct llarp_xi { + in6_addr address; + in6_addr netmask; +}; + +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 } #endif diff --git a/include/llarp/ibfq.h b/include/llarp/ibfq.h index a7404ce77..1957774fe 100644 --- a/include/llarp/ibfq.h +++ b/include/llarp/ibfq.h @@ -7,21 +7,22 @@ extern "C" { #endif - // forward declare - struct llarp_msg_muxer; - - struct llarp_link_queue; - - struct llarp_link_queue * llarp_init_link_queue(); - void llarp_free_link_queue(struct llarp_link_queue ** queue); - /** - offer a full frame to the inbound frame queue - return true if successfully added - return false if the queue is full - */ - bool llarp_link_offer_frame(struct llarp_link_queue * queue, llarp_buffer_t msg); - /** return true if we have more messages to process */ - bool llarp_link_queue_process(struct llarp_link_queue * queue, struct llarp_msg_muxer * muxer); +// forward declare +struct llarp_msg_muxer; + +struct llarp_link_queue; + +struct llarp_link_queue *llarp_init_link_queue(); +void llarp_free_link_queue(struct llarp_link_queue **queue); +/** + offer a full frame to the inbound frame queue + return true if successfully added + return false if the queue is full + */ +bool llarp_link_offer_frame(struct llarp_link_queue *queue, llarp_buffer_t msg); +/** return true if we have more messages to process */ +bool llarp_link_queue_process(struct llarp_link_queue *queue, + struct llarp_msg_muxer *muxer); #ifdef __cplusplus } diff --git a/include/llarp/ibmq.h b/include/llarp/ibmq.h index 9893d48d1..3f5d4c6bd 100644 --- a/include/llarp/ibmq.h +++ b/include/llarp/ibmq.h @@ -1,26 +1,26 @@ #ifndef LLARP_IBMQ_H_ #define LLARP_IBMQ_H_ #include -#include #include +#include #ifdef __cplusplus extern "C" { #endif - struct llarp_mq; - - struct llarp_mq * llarp_init_mq(); - void llarp_free_mq(struct llarp_mq ** queue); - /** - offer a full message to the inbound message queue - return true if successfully added - return false if the queue is full - */ - bool llarp_mq_offer(struct llarp_mq * queue, llarp_buffer_t msg); - size_t llarp_mq_peek(struct llarp_mq * queue); - /** return true if we have more messages to process */ - bool llarp_mq_process(struct llarp_mq * queue, struct llarp_msg_muxer * muxer); +struct llarp_mq; + +struct llarp_mq *llarp_init_mq(); +void llarp_free_mq(struct llarp_mq **queue); +/** + offer a full message to the inbound message queue + return true if successfully added + return false if the queue is full + */ +bool llarp_mq_offer(struct llarp_mq *queue, llarp_buffer_t msg); +size_t llarp_mq_peek(struct llarp_mq *queue); +/** return true if we have more messages to process */ +bool llarp_mq_process(struct llarp_mq *queue, struct llarp_msg_muxer *muxer); #ifdef __cplusplus } diff --git a/include/llarp/link.h b/include/llarp/link.h index 5098fbf26..2cbd85c9c 100644 --- a/include/llarp/link.h +++ b/include/llarp/link.h @@ -1,59 +1,59 @@ #ifndef LLARP_LINK_H_ #define LLARP_LINK_H_ -#include +#include #include +#include #include -#include #ifdef __cplusplus extern "C" { #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(); - void llarp_link_free(struct llarp_link ** link); +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); +/** get link listener for events */ +struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *link); - /** get link listener for events */ - struct llarp_udp_listener * llarp_link_udp_listener(struct llarp_link * link); +void llarp_link_stop(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; - /** set by llarp_try_establish_session */ - struct llarp_link * link; - /** set by llarp_try_establish_session */ - struct llarp_rc * rc; - void (*result)(struct llarp_link_session_listener *, struct llarp_link_session *); - }; +struct llarp_link_session_listener { + void *user; + /** set by llarp_try_establish_session */ + struct llarp_link *link; + /** set by llarp_try_establish_session */ + struct llarp_rc *rc; + void (*result)(struct llarp_link_session_listener *, + struct llarp_link_session *); +}; - /** information for establishing an outbound session */ - struct llarp_link_establish_job - { - struct llarp_rc * rc; - uint64_t timeout; - }; +/** information for establishing an outbound session */ +struct llarp_link_establish_job { + struct llarp_rc *rc; + 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 * 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); - 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 } #endif diff --git a/include/llarp/mem.h b/include/llarp/mem.h index fabf53448..a5d3ad54e 100644 --- a/include/llarp/mem.h +++ b/include/llarp/mem.h @@ -7,24 +7,22 @@ extern "C" { #include - struct llarp_alloc - { - void * (*alloc)(size_t sz, size_t align); - void (*free)(void * ptr); - }; - - /** global memory allocator */ - extern struct llarp_alloc llarp_g_mem; - /** init llarp_g_mem with stdlib malloc */ - void llarp_mem_stdlib(); - /** init llarp_g_mem with jemalloc */ - void llarp_mem_jemalloc(); - /** init llarp_g_mem with dmalloc */ - void llarp_mem_dmalloc(); +struct llarp_alloc { + void *(*alloc)(size_t sz, size_t align); + void (*free)(void *ptr); +}; + +/** global memory allocator */ +extern struct llarp_alloc llarp_g_mem; +/** init llarp_g_mem with stdlib malloc */ +void llarp_mem_stdlib(); +/** init llarp_g_mem with jemalloc */ +void llarp_mem_jemalloc(); +/** init llarp_g_mem with dmalloc */ +void llarp_mem_dmalloc(); #ifdef __cplusplus } #endif - #endif diff --git a/include/llarp/msg_handler.h b/include/llarp/msg_handler.h index e46e5e5e4..5f9f0c624 100644 --- a/include/llarp/msg_handler.h +++ b/include/llarp/msg_handler.h @@ -8,28 +8,26 @@ extern "C" { #endif - struct llarp_frame_handler - { - struct llarp_obmd * outbound; - struct llarp_ibmq * inbound; - bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *, llarp_buffer_t); - }; +struct llarp_frame_handler { + struct llarp_obmd *outbound; + struct llarp_ibmq *inbound; + bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *, + llarp_buffer_t); +}; - struct llarp_msg_handler - { - 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 *); - }; +struct llarp_msg_handler { + struct llarp_path_context *paths; +}; - /** fill function pointers with default values */ - void llarp_msg_handler_mux_init(struct llarp_msg_muxer * muxer); +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 */ +void llarp_msg_handler_mux_init(struct llarp_msg_muxer *muxer); #ifdef __cplusplus } diff --git a/include/llarp/net.h b/include/llarp/net.h index ac0f74220..dda6621b3 100644 --- a/include/llarp/net.h +++ b/include/llarp/net.h @@ -1,8 +1,8 @@ #ifndef SARP_NET_H #define SARP_NET_H -#include -#include #include +#include +#include #endif diff --git a/include/llarp/obmd.h b/include/llarp/obmd.h index 66e7a74ee..a3408da0e 100644 --- a/include/llarp/obmd.h +++ b/include/llarp/obmd.h @@ -8,18 +8,20 @@ extern "C" { #endif - // forward declair - struct llarp_link; - - struct llarp_link_dispatcher; - - struct llarp_link_dispatcher * llarp_init_link_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_register(struct llarp_link_dispatcher * dispatcher, struct llarp_link * link); - +// forward declair +struct llarp_link; + +struct llarp_link_dispatcher; + +struct llarp_link_dispatcher *llarp_init_link_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_register(struct llarp_link_dispatcher *dispatcher, + struct llarp_link *link); + #ifdef __cplusplus } #endif diff --git a/include/llarp/path.h b/include/llarp/path.h index 1048acb14..50e5135f6 100644 --- a/include/llarp/path.h +++ b/include/llarp/path.h @@ -5,8 +5,7 @@ typedef uint64_t llarp_path_id_t; -struct llarp_transit_hop -{ +struct llarp_transit_hop { llarp_path_id_t id; llarp_sharedkey_t symkey; llarp_pubkey_t nextHop; @@ -15,5 +14,4 @@ struct llarp_transit_hop llarp_version_t version; }; - #endif diff --git a/include/llarp/router.h b/include/llarp/router.h index 50e5d6dc6..dba127822 100644 --- a/include/llarp/router.h +++ b/include/llarp/router.h @@ -10,21 +10,22 @@ extern "C" { #endif - struct llarp_router; +struct llarp_router; - struct llarp_router * llarp_init_router(struct llarp_threadpool * tp); - void llarp_free_router(struct llarp_router ** router); +struct llarp_router *llarp_init_router(struct llarp_threadpool *tp); +void llarp_free_router(struct llarp_router **router); - 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); +int llarp_configure_router(struct llarp_router *router, + struct llarp_config *conf); - - /** 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); +void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop); +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); #ifdef __cplusplus } diff --git a/include/llarp/router_contact.h b/include/llarp/router_contact.h index 3499d6aeb..515dde4fa 100644 --- a/include/llarp/router_contact.h +++ b/include/llarp/router_contact.h @@ -1,28 +1,25 @@ #ifndef LLARP_RC_H #define LLARP_RC_H #include -#include #include +#include #ifdef __cplusplus extern "C" { #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); - 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); +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); +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 } #endif diff --git a/include/llarp/service_info.h b/include/llarp/service_info.h index 0d20be203..d24fc681e 100644 --- a/include/llarp/service_info.h +++ b/include/llarp/service_info.h @@ -6,18 +6,19 @@ extern "C" { #endif - struct llarp_service_info - { - llarp_buffer_t name; - llarp_pubkey_t signingkey; - llarp_buffer_t vanity; - }; +struct llarp_service_info { + llarp_buffer_t name; + llarp_pubkey_t signingkey; + 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 } #endif diff --git a/include/llarp/threadpool.h b/include/llarp/threadpool.h index c3b92703d..55efd3503 100644 --- a/include/llarp/threadpool.h +++ b/include/llarp/threadpool.h @@ -5,28 +5,28 @@ extern "C" { #endif - struct llarp_threadpool; +struct llarp_threadpool; - struct llarp_threadpool * llarp_init_threadpool(int workers); - void llarp_free_threadpool(struct llarp_threadpool ** tp); +struct llarp_threadpool *llarp_init_threadpool(int workers); +void llarp_free_threadpool(struct llarp_threadpool **tp); - /** job to be done in worker thread */ - struct llarp_thread_job - { - /** - called async after work is executed - */ - struct llarp_ev_job * result; - /** user data to pass to work function */ - void * user; - /** called in threadpool worker thread */ - void (*work)(void *); - }; - - void llarp_threadpool_queue_job(struct llarp_threadpool * tp, struct llarp_thread_job j); +/** job to be done in worker thread */ +struct llarp_thread_job { + /** + called async after work is executed + */ + struct llarp_ev_job *result; + /** user data to pass to work function */ + void *user; + /** called in threadpool worker thread */ + void (*work)(void *); +}; - void llarp_threadpool_start(struct llarp_threadpool * tp); - void llarp_threadpool_join(struct llarp_threadpool * tp); +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_join(struct llarp_threadpool *tp); #ifdef __cplusplus } diff --git a/include/llarp/time.h b/include/llarp/time.h index 1801fbd79..24a4209df 100644 --- a/include/llarp/time.h +++ b/include/llarp/time.h @@ -5,9 +5,9 @@ extern "C" { #endif - llarp_time_t llarp_time_now_ms(); - llarp_seconds_t llarp_time_now_sec(); - +llarp_time_t llarp_time_now_ms(); +llarp_seconds_t llarp_time_now_sec(); + #ifdef __cplusplus } #endif diff --git a/include/llarp/version.h b/include/llarp/version.h index 674f7148e..94ba65f31 100644 --- a/include/llarp/version.h +++ b/include/llarp/version.h @@ -15,11 +15,14 @@ #ifndef LLARP_VERSION_NUM #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 -#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 -#define LLARP_VERSION "llarpd"LLARP_VERSION_NUM +#define LLARP_VERSION "llarpd" LLARP_VERSION_NUM #endif diff --git a/llarp/address_info.cpp b/llarp/address_info.cpp index 4ef740e68..716251789 100644 --- a/llarp/address_info.cpp +++ b/llarp/address_info.cpp @@ -2,48 +2,38 @@ #include "bencode.hpp" #include "str.hpp" -namespace llarp -{ - static void * ai_alloc(size_t sz, size_t align) - { - - } -} +namespace llarp { +static void *ai_alloc(size_t sz, size_t align) {} +} // namespace llarp extern "C" { - - struct llarp_alloc * llarp_ai_alloc = &llarp_g_mem; - - bool llarp_ai_bencode(struct llarp_ai * ai, llarp_buffer_t * buff) - { - return llarp::BEncode(*ai, buff); - } - - void llarp_ai_list_iterate(struct llarp_ai_list * l, struct llarp_ai_list_iter * itr) - { - itr->list = l; - struct llarp_ai_list * cur = l; - do - { - if(!itr->visit(itr, cur->data)) - return; - cur = cur->next; - } - while(cur->next); - } + +struct llarp_alloc *llarp_ai_alloc = &llarp_g_mem; + +bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) { + return llarp::BEncode(*ai, buff); +} + +void llarp_ai_list_iterate(struct llarp_ai_list *l, + struct llarp_ai_list_iter *itr) { + itr->list = l; + struct llarp_ai_list *cur = l; + do { + if (!itr->visit(itr, cur->data)) + return; + cur = cur->next; + } while (cur->next); +} } -namespace llarp -{ - 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)) && - bencodeDict_Bytes(buff, "d", a.dialect, UStrLen(a.dialect, sizeof(a.dialect))) && - bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) && - bencodeDict_Int(buff, "p", a.port) && - bencodeDict_Int(buff, "v", 0) && - bencodeEnd(buff); - } +namespace llarp { +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)) && + bencodeDict_Bytes(buff, "d", a.dialect, + UStrLen(a.dialect, sizeof(a.dialect))) && + bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) && + bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) && + bencodeEnd(buff); } +} // namespace llarp diff --git a/llarp/address_info.hpp b/llarp/address_info.hpp index 43c3a4279..095eb1e31 100644 --- a/llarp/address_info.hpp +++ b/llarp/address_info.hpp @@ -1,31 +1,25 @@ #ifndef LLARP_AI_HPP #define LLARP_AI_HPP -#include #include #include +#include -struct llarp_ai_list -{ - llarp_ai * data; - llarp_ai_list * next = nullptr; +struct llarp_ai_list { + llarp_ai *data; + llarp_ai_list *next = nullptr; }; -static std::list ai_list_to_std(struct llarp_ai_list * l) -{ +static std::list ai_list_to_std(struct llarp_ai_list *l) { std::list list; - if(l && l->data) - { - do - { + if (l && l->data) { + do { llarp_ai copy; memcpy(©, l->data, sizeof(llarp_ai)); list.push_back(copy); l = l->next; - } - while(l->next); + } while (l->next); } return list; } #endif - diff --git a/llarp/bencode.hpp b/llarp/bencode.hpp index b09cf3d9a..91dc60d0e 100644 --- a/llarp/bencode.hpp +++ b/llarp/bencode.hpp @@ -1,70 +1,61 @@ #ifndef LLARP_BENCODE_HPP #define LLARP_BENCODE_HPP -#include #include +#include #include -namespace llarp -{ - template - bool BEncode(const ValType & t, llarp_buffer_t * buff); +namespace llarp { +template +bool BEncode(const ValType &t, llarp_buffer_t *buff); - template - bool BDecode(ValType & t, llarp_buffer_t * buff); +template bool BDecode(ValType &t, llarp_buffer_t *buff); - static bool bencodeDict(llarp_buffer_t * buff) - { - static uint8_t c = 'd'; - 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 bencodeDict(llarp_buffer_t *buff) { + static uint8_t c = 'd'; + return llarp_buffer_write(buff, &c, 1); +} - 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 - 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 bencodeList(llarp_buffer_t *buff) { + static uint8_t c = 'l'; + return llarp_buffer_write(buff, &c, 1); +} - 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); - } +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) { + std::string kstr = std::to_string(key.size()) + ":" + key; + return llarp_buffer_write(buff, kstr.c_str(), kstr.size()); +} + +template +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 - bool BEncode(const std::list & l, llarp_buffer_t * buff) - { - if(bencodeList(buff)) - { - for(const auto & itr : l) - if(!BEncode(itr, buff)) - return false; - return bencodeEnd(buff); - } - return false; +template +bool BEncode(const std::list &l, llarp_buffer_t *buff) { + if (bencodeList(buff)) { + for (const auto &itr : l) + if (!BEncode(itr, buff)) + return false; + return bencodeEnd(buff); } + return false; } +} // namespace llarp #endif diff --git a/llarp/buffer.cpp b/llarp/buffer.cpp index 88753ed97..e0e3b69ef 100644 --- a/llarp/buffer.cpp +++ b/llarp/buffer.cpp @@ -2,15 +2,13 @@ extern "C" { - size_t llarp_buffer_size_left(llarp_buffer_t * buff) - { - std::ptrdiff_t diff = buff->cur - buff->base; - if(diff < 0) - { - return 0; - } - else if(diff > buff->sz) return 0; - else return buff->sz - diff; - } - +size_t llarp_buffer_size_left(llarp_buffer_t *buff) { + std::ptrdiff_t diff = buff->cur - buff->base; + if (diff < 0) { + return 0; + } else if (diff > buff->sz) + return 0; + else + return buff->sz - diff; +} } diff --git a/llarp/config.cpp b/llarp/config.cpp index c6154afc5..ffb46ff11 100644 --- a/llarp/config.cpp +++ b/llarp/config.cpp @@ -1,75 +1,65 @@ -#include #include "config.hpp" #include "ini.hpp" #include "mem.hpp" +#include -namespace llarp -{ - - template - 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; - }; +namespace llarp { - +template +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) - { - llarp_config * c = new llarp_config; - *conf = c; - } +void llarp_new_config(struct llarp_config **conf) { + llarp_config *c = new llarp_config; + *conf = c; +} - void llarp_free_config(struct llarp_config ** conf) - { - if(*conf) - delete *conf; - *conf = nullptr; - } +void llarp_free_config(struct llarp_config **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; - return 0; - } +int llarp_load_config(struct llarp_config *conf, const char *fname) { + if (!conf->impl.Load(fname)) + return -1; + return 0; +} - void llarp_config_iter(struct llarp_config * conf, struct llarp_config_iterator * iter) - { - iter->conf = conf; - std::map sections = { +void llarp_config_iter(struct llarp_config *conf, + struct llarp_config_iterator *iter) { + iter->conf = conf; + std::map sections = { {"router", conf->impl.router}, {"network", conf->impl.network}, {"links", conf->impl.links}, - {"netdb", conf->impl.netdb} - }; - for(const auto & section : sections) - for(const auto & item : section.second) - iter->visit(iter, section.first.c_str(), item.first.c_str(), item.second.c_str()); - } + {"netdb", conf->impl.netdb}}; + for (const auto §ion : sections) + for (const auto &item : section.second) + iter->visit(iter, section.first.c_str(), item.first.c_str(), + item.second.c_str()); +} } diff --git a/llarp/config.hpp b/llarp/config.hpp index f960e267d..492ddd3cf 100644 --- a/llarp/config.hpp +++ b/llarp/config.hpp @@ -5,28 +5,23 @@ #include -namespace llarp -{ - struct Config - { - typedef std::map section_t; - - section_t router; - section_t network; - section_t netdb; - section_t links; +namespace llarp { +struct Config { + typedef std::map section_t; - 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" { - struct llarp_config - { - llarp::Config impl; - }; +struct llarp_config { + llarp::Config impl; +}; } #endif diff --git a/llarp/crypto_libsodium.cpp b/llarp/crypto_libsodium.cpp index 98fae34e1..cb352e022 100644 --- a/llarp/crypto_libsodium.cpp +++ b/llarp/crypto_libsodium.cpp @@ -1,90 +1,84 @@ #include -#include #include #include #include +#include -namespace llarp -{ - namespace sodium - { - 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; - } +namespace llarp { +namespace sodium { +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; +} - 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; - 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)); - crypto_generichash_update(&h, out, crypto_scalarmult_BYTES); - crypto_generichash_final(&h, out, outsz); - return true; - } +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; + 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)); + crypto_generichash_update(&h, out, crypto_scalarmult_BYTES); + 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) - { - llarp_pubkey_t local_pk; - crypto_scalarmult_base(local_pk, sk); - if(dh(shared, local_pk, pk, pk, sk)) - { - return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1; - } - return false; - } +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; + crypto_scalarmult_base(local_pk, sk); + if (dh(shared, local_pk, pk, pk, sk)) { + return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, + TUNNOUNCESIZE) != -1; + } + return false; +} - 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; - crypto_scalarmult_base(local_pk, sk); - if(dh(shared, pk, local_pk, pk, sk)) - { - return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, TUNNOUNCESIZE) != -1; - } - return false; - } +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; + crypto_scalarmult_base(local_pk, sk); + if (dh(shared, pk, local_pk, pk, sk)) { + return crypto_generichash(*shared, SHAREDKEYSIZE, *shared, SHAREDKEYSIZE, n, + TUNNOUNCESIZE) != -1; + } + return false; +} - bool hash(llarp_hash_t * result, llarp_buffer_t buff) - { - const uint8_t * base = (const uint8_t *) buff.base; - return crypto_generichash(*result, HASHSIZE, base, buff.sz, nullptr, 0) != -1; - } +bool hash(llarp_hash_t *result, llarp_buffer_t buff) { + const uint8_t *base = (const uint8_t *)buff.base; + 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) - { - const uint8_t * base = (const uint8_t *) buff.base; - return crypto_generichash(*result, sizeof(llarp_hash_t), base, buff.sz, secret, HMACSECSIZE) != -1; - } +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; +} - 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; - } +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; +} - bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) - { - const uint8_t * base = (const uint8_t *) buff.base; - return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1; - } - } +bool verify(llarp_pubkey_t pub, llarp_buffer_t buff, llarp_sig_t sig) { + const uint8_t *base = (const uint8_t *)buff.base; + return crypto_sign_verify_detached(sig, base, buff.sz, pub) != -1; } +} // namespace sodium +} // namespace llarp extern "C" { - void llarp_crypto_libsodium_init(struct llarp_crypto * c) - { - c->xchacha20 = llarp::sodium::xchacha20; - c->dh_client = llarp::sodium::dh_client; - c->dh_server = llarp::sodium::dh_server; - c->hash = llarp::sodium::hash; - c->hmac = llarp::sodium::hmac; - c->sign = llarp::sodium::sign; - c->verify = llarp::sodium::verify; - } +void llarp_crypto_libsodium_init(struct llarp_crypto *c) { + c->xchacha20 = llarp::sodium::xchacha20; + c->dh_client = llarp::sodium::dh_client; + c->dh_server = llarp::sodium::dh_server; + c->hash = llarp::sodium::hash; + c->hmac = llarp::sodium::hmac; + c->sign = llarp::sodium::sign; + c->verify = llarp::sodium::verify; +} } diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 3b06d7755..16ebad5db 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -1,184 +1,154 @@ +#include "mem.hpp" #include #include -#include "mem.hpp" -struct llarp_ev_loop -{ +struct llarp_ev_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()); } - static void operator delete(void * ptr) - { - llarp_g_mem.free(ptr); - } - - uv_loop_t * loop() { return &_loop; } + static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } + + uv_loop_t *loop() { return &_loop; } }; -namespace llarp -{ - struct udp_listener - { - static void * operator new(size_t sz) - { - return llarp_g_mem.alloc(sz, alignment()); - } +namespace llarp { +struct udp_listener { + static void *operator new(size_t sz) { + return llarp_g_mem.alloc(sz, alignment()); + } - static void operator delete(void * 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); - } + static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } - /** called after closed */ - void closed() - { - 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(llarp_g_mem.alloc(sz, 1024)); - buf->len = sz; - } + uv_udp_t _handle; + struct llarp_udp_listener *listener; - 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(handle->data); - l->recvfrom(addr, buf->base, nread); - llarp_g_mem.free(buf->base); + void recvfrom(const struct sockaddr *addr, char *buff, ssize_t sz) { + if (listener->recvfrom) + listener->recvfrom(listener, addr, buff, sz); } - static void udp_close_cb(uv_handle_t * handle) - { - udp_listener * l = static_cast(handle->data); - l->closed(); - delete l; + /** called after closed */ + void closed() { + 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(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(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(handle->data); + l->closed(); + delete l; +} +} // namespace llarp - static void ev_handle_async_closed(uv_handle_t * handle) - { - struct llarp_ev_job * ev = static_cast(handle->data); - 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(handle->data); - ev->work(ev); - uv_close((uv_handle_t *)handle, ev_handle_async_closed); - } +namespace llarp { + +static void ev_handle_async_closed(uv_handle_t *handle) { + struct llarp_ev_job *ev = static_cast(handle->data); + 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(handle->data); + ev->work(ev); + uv_close((uv_handle_t *)handle, ev_handle_async_closed); +} +} // namespace llarp + extern "C" { - void llarp_ev_loop_alloc(struct llarp_ev_loop ** ev) - { - *ev = new llarp_ev_loop; - if (*ev) - { - 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; +void llarp_ev_loop_alloc(struct llarp_ev_loop **ev) { + *ev = new llarp_ev_loop; + if (*ev) { + uv_loop_init((*ev)->loop()); } +} - int llarp_ev_loop_run(struct llarp_ev_loop * ev) - { - return uv_run(ev->loop(), UV_RUN_DEFAULT); +void llarp_ev_loop_free(struct llarp_ev_loop **ev) { + if (*ev) { + uv_loop_close((*ev)->loop()); + llarp_g_mem.free(*ev); } - - int llarp_ev_add_udp_listener(struct llarp_ev_loop * ev, struct llarp_udp_listener * listener) - { - sockaddr_in6 addr; - uv_ip6_addr(listener->host, listener->port, &addr); - int ret = 0; - llarp::udp_listener * l = new llarp::udp_listener; - listener->impl = l; - l->udp()->data = l; - l->listener = listener; - - 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); - } + *ev = nullptr; +} + +int llarp_ev_loop_run(struct llarp_ev_loop *ev) { + return uv_run(ev->loop(), UV_RUN_DEFAULT); +} + +int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev, + struct llarp_udp_listener *listener) { + sockaddr_in6 addr; + uv_ip6_addr(listener->host, listener->port, &addr); + int ret = 0; + llarp::udp_listener *l = new llarp::udp_listener; + listener->impl = l; + l->udp()->data = l; + l->listener = listener; + + 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 ret = -1; - if(listener) - { - llarp::udp_listener * l = static_cast(listener->impl); - if(l) - { - if(!uv_udp_recv_stop(l->udp())) - { - l->closed(); - delete l; - ret = 0; - } +int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) { + int ret = -1; + if (listener) { + llarp::udp_listener *l = static_cast(listener->impl); + if (l) { + if (!uv_udp_recv_stop(l->udp())) { + 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) - { - struct llarp_ev_job * job_copy = static_cast(llarp_g_mem.alloc(sizeof(struct llarp_ev_job), llarp::alignment())); - job_copy->work = job.work; - job_copy->loop = loop; - job_copy->user = job.user; - uv_async_t * async = static_cast(llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment())); - async->data = job_copy; - if(uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 && uv_async_send(async)) - return true; - else - { - llarp_g_mem.free(job_copy); - llarp_g_mem.free(async); - return false; - } +void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { uv_stop(loop->loop()); } + +bool llarp_ev_async(struct llarp_ev_loop *loop, struct llarp_ev_job job) { + struct llarp_ev_job *job_copy = + static_cast(llarp_g_mem.alloc( + sizeof(struct llarp_ev_job), llarp::alignment())); + job_copy->work = job.work; + job_copy->loop = loop; + job_copy->user = job.user; + uv_async_t *async = static_cast( + llarp_g_mem.alloc(sizeof(uv_async_t), llarp::alignment())); + async->data = job_copy; + if (uv_async_init(loop->loop(), async, llarp::ev_handle_async) == 0 && + uv_async_send(async)) + return true; + else { + llarp_g_mem.free(job_copy); + llarp_g_mem.free(async); + return false; } } +} diff --git a/llarp/exit_info.cpp b/llarp/exit_info.cpp index 16f1f4ac4..333b61013 100644 --- a/llarp/exit_info.cpp +++ b/llarp/exit_info.cpp @@ -2,20 +2,16 @@ #include "bencode.hpp" extern "C" { - bool llarp_xi_bencode(struct llarp_xi * xi, llarp_buffer_t * buff) - { - return llarp::BEncode(*xi, buff); - } +bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) { + return llarp::BEncode(*xi, buff); +} } -namespace llarp -{ - bool BEncode(const llarp_xi & xi, llarp_buffer_t * buff) - { - return bencodeDict(buff) && - bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) && - bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) && - bencodeDict_Int(buff, "v", 0) && - bencodeEnd(buff); - } +namespace llarp { +bool BEncode(const llarp_xi &xi, llarp_buffer_t *buff) { + return bencodeDict(buff) && + bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) && + bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) && + bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff); } +} // namespace llarp diff --git a/llarp/exit_info.hpp b/llarp/exit_info.hpp index 6f10ad6e9..206e0b377 100644 --- a/llarp/exit_info.hpp +++ b/llarp/exit_info.hpp @@ -1,18 +1,16 @@ #ifndef LLARP_XI_HPP #define LLARP_XI_HPP -#include #include +#include -struct llarp_xi_list -{ +struct llarp_xi_list { std::list list; }; -static std::list xi_list_to_std(struct llarp_xi_list * l) -{ +static std::list xi_list_to_std(struct llarp_xi_list *l) { std::list list; - if(l->list.size()) - for(const auto & xi : l->list) + if (l->list.size()) + for (const auto &xi : l->list) list.push_back(xi); return list; } diff --git a/llarp/ini.hpp b/llarp/ini.hpp index 534d2b116..cb410a544 100644 --- a/llarp/ini.hpp +++ b/llarp/ini.hpp @@ -19,27 +19,26 @@ * 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 * THE SOFTWARE. - * - */ + * + */ #ifndef INI_HPP #define INI_HPP #include -#include +#include +#include +#include #include +#include #include #include -#include -#include -#include namespace ini { -struct Level -{ +struct Level { Level() : parent(NULL), depth(0) {} - Level(Level* p) : parent(p), depth(0) {} + Level(Level *p) : parent(p), depth(0) {} typedef std::map value_map_t; typedef std::map section_map_t; @@ -49,97 +48,95 @@ struct Level section_map_t sections; values_t ordered_values; // original order in the ini file sections_t ordered_sections; - Level* parent; + Level *parent; size_t depth; - const std::string& operator[](const std::string& name) { return values[name]; } - Level& operator()(const std::string& name) { return sections[name]; } + const std::string &operator[](const std::string &name) { + return values[name]; + } + Level &operator()(const std::string &name) { return sections[name]; } }; -class Parser -{ +class Parser { 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(), ""); } + 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: - 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); + 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: Level top_; std::ifstream f0_; - std::istream* f_; + std::istream *f_; std::string line_; size_t ln_; }; -inline void -Parser::err(const char* s) -{ +inline void Parser::err(const char *s) { char buf[256]; sprintf(buf, "%s on line #%ld", s, ln_); 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"; 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; - return s.substr(sp, ep-sp+1); + 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) -{ +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); parse(top_); } -inline void -Parser::parseSLine(std::string& sname, size_t& depth) -{ +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); + sname = line_.substr(depth, line_.length() - 2 * depth); } -inline void -Parser::parse(Level& l) -{ +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; + Level *lp = NULL; + Level *parent = &l; if (depth > l.depth + 1) err("section with wrong depth"); - if (l.depth == depth-1) + 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]; } @@ -155,9 +152,9 @@ Parser::parse(Level& l) size_t n = line_.find('='); if (n == std::string::npos) err("no '=' found"); - std::pair res = - l.values.insert(std::make_pair(trim(line_.substr(0, n)), - trim(line_.substr(n+1, line_.length()-n-1)))); + std::pair 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"); l.ordered_values.push_back(res.first); @@ -165,22 +162,28 @@ 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; - for (Level::values_t::const_iterator it = l.ordered_values.begin(); it != l.ordered_values.end(); ++it) +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; + for (Level::values_t::const_iterator it = l.ordered_values.begin(); + it != l.ordered_values.end(); ++it) s << (*it)->first << '=' << (*it)->second << std::endl; - for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); it != l.ordered_sections.end(); ++it) { - assert((*it)->second.depth == l.depth+1); + for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); + it != l.ordered_sections.end(); ++it) { + assert((*it)->second.depth == l.depth + 1); dump(s, (*it)->second, (*it)->first); } } -} +} // namespace ini #endif // INI_HPP diff --git a/llarp/link.cpp b/llarp/link.cpp index bc804e011..0e02aa9b0 100644 --- a/llarp/link.cpp +++ b/llarp/link.cpp @@ -2,35 +2,32 @@ #include #include -bool operator < (const sockaddr_in6 addr0, const sockaddr_in6 addr1) -{ - return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr, sizeof(addr0.sin6_addr)) && addr0.sin6_port < addr1.sin6_port; +bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) { + return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr, + sizeof(addr0.sin6_addr)) && + addr0.sin6_port < addr1.sin6_port; } -extern "C"{ - struct llarp_link * llarp_link_alloc() - { - return new llarp_link; - } +extern "C" { +struct llarp_link *llarp_link_alloc() { + return new llarp_link; +} - void llarp_link_free(struct llarp_link ** l) - { - if(*l) delete *l; - *l =nullptr; - } +void llarp_link_free(struct llarp_link **l) { + if (*l) + delete *l; + *l = nullptr; +} - struct llarp_udp_listener * llarp_link_udp_listener(struct llarp_link *l) - { - return &l->listener; - } +struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *l) { + return &l->listener; +} - bool llarp_link_configure(struct llarp_link * link, const char * ifname, int af) - { - return false; - } +bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af) { + return false; +} - void llarp_link_stop(struct llarp_link * link) - { - llarp_ev_close_udp_listener(&link->listener); - } +void llarp_link_stop(struct llarp_link *link) { + llarp_ev_close_udp_listener(&link->listener); +} } diff --git a/llarp/link.hpp b/llarp/link.hpp index 63e38a714..89c0388ec 100644 --- a/llarp/link.hpp +++ b/llarp/link.hpp @@ -1,33 +1,26 @@ #ifndef LLARP_LINK_HPP #define LLARP_LINK_HPP -#include -#include #include +#include +#include #include #include -#include +#include +#include "mem.hpp" #include #include -#include "mem.hpp" - -struct llarp_link -{ - static void * operator new(size_t sz) - { +struct llarp_link { + static void *operator new(size_t sz) { return llarp_g_mem.alloc(sz, llarp::alignment()); } - - static void operator delete(void * ptr) - { - llarp_g_mem.free(ptr); - } - + + static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } + struct sockaddr_in6 localaddr; int af; llarp_udp_listener listener; }; - #endif diff --git a/llarp/mem.cpp b/llarp/mem.cpp index 36d65b928..8a11c7911 100644 --- a/llarp/mem.cpp +++ b/llarp/mem.cpp @@ -1,8 +1,5 @@ #include extern "C" { - struct llarp_alloc llarp_g_mem = { - .alloc = nullptr, - .free = nullptr - }; +struct llarp_alloc llarp_g_mem = {.alloc = nullptr, .free = nullptr}; } diff --git a/llarp/mem.hpp b/llarp/mem.hpp index f414e25be..0924f5fa9 100644 --- a/llarp/mem.hpp +++ b/llarp/mem.hpp @@ -1,14 +1,11 @@ #ifndef LLARP_MEM_HPP #define LLARP_MEM_HPP -#include #include -namespace llarp -{ - template - static constexpr size_t alignment() - { - return std::exp2(1+std::floor(std::log2(sizeof(T)))); - } +#include +namespace llarp { +template static constexpr size_t alignment() { + return std::exp2(1 + std::floor(std::log2(sizeof(T)))); } +} // namespace llarp #endif diff --git a/llarp/mem_jemalloc.cpp b/llarp/mem_jemalloc.cpp index f8f9295c3..fc603453e 100644 --- a/llarp/mem_jemalloc.cpp +++ b/llarp/mem_jemalloc.cpp @@ -1,23 +1,20 @@ -#include #include +#include -namespace llarp -{ - static void * jem_malloc(size_t sz, size_t align) - { - return mallocx(sz, MALLOCX_ALIGN(align)); - } +namespace llarp { +static void *jem_malloc(size_t sz, size_t align) { + return mallocx(sz, MALLOCX_ALIGN(align)); +} - static void jem_free(void * ptr) - { - if(ptr) free(ptr); - } +static void jem_free(void *ptr) { + if (ptr) + free(ptr); } +} // namespace llarp extern "C" { - void llarp_mem_jemalloc() - { - llarp_g_mem.alloc = llarp::jem_malloc; - llarp_g_mem.free = llarp::jem_free; - } +void llarp_mem_jemalloc() { + llarp_g_mem.alloc = llarp::jem_malloc; + llarp_g_mem.free = llarp::jem_free; +} } diff --git a/llarp/mem_std.cpp b/llarp/mem_std.cpp index 863098ace..33db5dd2f 100644 --- a/llarp/mem_std.cpp +++ b/llarp/mem_std.cpp @@ -1,26 +1,24 @@ #include -namespace llarp -{ - void * std_malloc(size_t sz, size_t align) - { - (void) align; - void * ptr = malloc(sz); - if(ptr) return ptr; - abort(); - } +namespace llarp { +void *std_malloc(size_t sz, size_t align) { + (void)align; + void *ptr = malloc(sz); + if (ptr) + return ptr; + abort(); +} - void std_free(void * ptr) - { - if(ptr) free(ptr); - } - +void std_free(void *ptr) { + if (ptr) + free(ptr); } +} // namespace llarp + extern "C" { - void llarp_mem_std() - { - llarp_g_mem.alloc = llarp::std_malloc; - llarp_g_mem.free = llarp::std_free; - } +void llarp_mem_std() { + llarp_g_mem.alloc = llarp::std_malloc; + llarp_g_mem.free = llarp::std_free; +} } diff --git a/llarp/router.cpp b/llarp/router.cpp index 24b0b4f00..08d3c6e5b 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -1,133 +1,105 @@ -#include -#include #include "link.hpp" #include "mem.hpp" #include "str.hpp" +#include +#include -namespace llarp -{ - void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val); - struct router_links - { - struct llarp_link * link = nullptr; - struct router_links * next = nullptr; - }; +namespace llarp { +void router_iter_config(llarp_config_iterator *iter, const char *section, + const char *key, const char *val); +struct router_links { + struct llarp_link *link = nullptr; + struct router_links *next = nullptr; +}; -} +} // namespace llarp -struct llarp_router -{ - struct llarp_threadpool * tp; +struct llarp_router { + struct llarp_threadpool *tp; llarp::router_links links; 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()); } - static void operator delete(void * ptr) - { - llarp_g_mem.free(ptr); - } + static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } - void AddLink(struct llarp_link * link) - { - llarp::router_links * head = &links; - while(head->next && head->link) + void AddLink(struct llarp_link *link) { + llarp::router_links *head = &links; + while (head->next && head->link) head = head->next; - - if(head->link) + + if (head->link) head->next = new llarp::router_links{link, nullptr}; else head->link = link; } - - void ForEachLink(std::function visitor) - { - llarp::router_links * cur = &links; - do - { - if(cur->link) + + void ForEachLink(std::function visitor) { + llarp::router_links *cur = &links; + do { + if (cur->link) visitor(cur->link); cur = cur->next; - } - while(cur); - } - - void Close() - { - ForEachLink(llarp_link_stop); + } while (cur); } -}; + void Close() { ForEachLink(llarp_link_stop); } +}; extern "C" { - struct llarp_router * llarp_init_router(struct llarp_threadpool * tp) - { - llarp_router * router = new llarp_router; - router->tp = tp; - llarp_crypto_libsodium_init(&router->crypto); - return router; - } +struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) { + llarp_router *router = new llarp_router; + router->tp = tp; + llarp_crypto_libsodium_init(&router->crypto); + return router; +} - int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf) - { - llarp_config_iterator iter; - iter.user = router; - iter.visit = llarp::router_iter_config; - llarp_config_iter(conf, &iter); - return 0; - } +int llarp_configure_router(struct llarp_router *router, + struct llarp_config *conf) { + llarp_config_iterator iter; + iter.user = router; + iter.visit = llarp::router_iter_config; + llarp_config_iter(conf, &iter); + return 0; +} - void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop) - { - router->ForEachLink([loop](llarp_link * link) { - llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link)); - }); - } +void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) { + router->ForEachLink([loop](llarp_link *link) { + llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link)); + }); +} - void llarp_stop_router(struct llarp_router * router) - { - router->Close(); - } +void llarp_stop_router(struct llarp_router *router) { router->Close(); } - void llarp_free_router(struct llarp_router ** router) - { - if(*router) - { - (*router)->ForEachLink([](llarp_link * link) { llarp_g_mem.free(link); }); - delete *router; - } - *router = nullptr; +void llarp_free_router(struct llarp_router **router) { + if (*router) { + (*router)->ForEachLink([](llarp_link *link) { llarp_g_mem.free(link); }); + delete *router; } + *router = nullptr; +} } -namespace llarp -{ - - void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val) - { - llarp_router * self = static_cast(iter->user); - if (StrEq(section, "links")) - { - if(StrEq(val, "ip")) - { - struct llarp_link * link = llarp_link_alloc(); - if(llarp_link_configure(link, key, AF_INET6)) - self->AddLink(link); - else - { - llarp_link_free(&link); - printf("failed to configure %s link for %s\n", val, key); - } - } - else if (StrEq(val, "eth")) - { - /** todo: ethernet link */ +namespace llarp { + +void router_iter_config(llarp_config_iterator *iter, const char *section, + const char *key, const char *val) { + llarp_router *self = static_cast(iter->user); + if (StrEq(section, "links")) { + if (StrEq(val, "ip")) { + struct llarp_link *link = llarp_link_alloc(); + if (llarp_link_configure(link, key, AF_INET6)) + self->AddLink(link); + else { + llarp_link_free(&link); + printf("failed to configure %s link for %s\n", val, key); } + } else if (StrEq(val, "eth")) { + /** todo: ethernet link */ } } } +} // namespace llarp diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 03796c565..aa97f2819 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -1,24 +1,20 @@ #include "router_contact.hpp" -#include "exit_info.hpp" #include "address_info.hpp" #include "bencode.hpp" +#include "exit_info.hpp" -extern "C" { - -} +extern "C" {} -namespace llarp -{ - bool BEncode(const llarp_rc & a, llarp_buffer_t * buff) - { - std::list addresses = ai_list_to_std(a.addrs); - std::list exits = xi_list_to_std(a.exits); - return bencodeDict(buff) && - bencodeDictKey(buff, "a") && BEncode(addresses, buff) && - bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) && - bencodeDict_Int(buff, "v", 0) && - bencodeDictKey(buff, "x") && BEncode(exits, buff) && - bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) && - bencodeEnd(buff); - } +namespace llarp { +bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) { + std::list addresses = ai_list_to_std(a.addrs); + std::list exits = xi_list_to_std(a.exits); + return bencodeDict(buff) && bencodeDictKey(buff, "a") && + BEncode(addresses, buff) && + bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) && + bencodeDict_Int(buff, "v", 0) && bencodeDictKey(buff, "x") && + BEncode(exits, buff) && + bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) && + bencodeEnd(buff); } +} // namespace llarp diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index efb202c77..236a3f7ca 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -2,5 +2,4 @@ #define LLARP_RC_HPP #include - #endif diff --git a/llarp/str.hpp b/llarp/str.hpp index 6d2e8bbcc..a130bf367 100644 --- a/llarp/str.hpp +++ b/llarp/str.hpp @@ -2,30 +2,25 @@ #define LIBLLARP_STR_HPP #include -namespace llarp -{ - static bool StrEq(const char * s1, const char * s2) - { - size_t sz1 = strlen(s1); - size_t sz2 = strlen(s2); - if (sz1 == sz2) - { - return strncmp(s1, s2, sz1) == 0; - } - else - return false; - } +namespace llarp { +static bool StrEq(const char *s1, const char *s2) { + size_t sz1 = strlen(s1); + size_t sz2 = strlen(s2); + if (sz1 == sz2) { + return strncmp(s1, s2, sz1) == 0; + } else + return false; +} - static size_t UStrLen(const uint8_t * data, size_t maxsz) - { - size_t sz = 0; - while(*data++) - { - sz ++; - if(maxsz >= sz) return maxsz; - } - return sz; +static size_t UStrLen(const uint8_t *data, size_t maxsz) { + size_t sz = 0; + while (*data++) { + sz++; + if (maxsz >= sz) + return maxsz; } + return sz; } +} // namespace llarp #endif diff --git a/llarp/threadpool.cpp b/llarp/threadpool.cpp index 764b6df7e..2027c5d57 100644 --- a/llarp/threadpool.cpp +++ b/llarp/threadpool.cpp @@ -1,72 +1,62 @@ #include "threadpool.hpp" #include -namespace llarp -{ - namespace thread - { - Pool::Pool(size_t workers) - { - stop.store(true); - while(workers--) - { - threads.emplace_back( - [this] - { - 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; - job = std::move(this->jobs.front()); - this->jobs.pop(); - } - // 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; - } - } - }); +namespace llarp { +namespace thread { +Pool::Pool(size_t workers) { + stop.store(true); + while (workers--) { + threads.emplace_back([this] { + 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; + job = std::move(this->jobs.front()); + this->jobs.pop(); + } + // 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() - { - { - lock_t lock(queue_mutex); - stop.store(true); - } - condition.notify_all(); - for(auto & t : threads) - t.join(); - } +void Pool::Join() { + { + lock_t lock(queue_mutex); + stop.store(true); + } + condition.notify_all(); + for (auto &t : threads) + t.join(); +} - 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"); +void Pool::QueueJob(llarp_thread_job job) { + { + lock_t lock(queue_mutex); - jobs.emplace(job); - } - condition.notify_one(); - } - + // don't allow enqueueing after stopping the pool + if (stop) + 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_threadpool(int workers) : impl(workers) {} @@ -74,23 +64,17 @@ struct llarp_threadpool extern "C" { - struct llarp_threadpool * llarp_init_threadpool(int workers) - { - if(workers > 0) - return new llarp_threadpool(workers); - else - return nullptr; - } +struct llarp_threadpool *llarp_init_threadpool(int workers) { + if (workers > 0) + return new llarp_threadpool(workers); + else + return nullptr; +} - void llarp_threadpool_join(struct llarp_threadpool * pool) - { - pool->impl.Join(); - } +void llarp_threadpool_join(struct llarp_threadpool *pool) { pool->impl.Join(); } - void llarp_free_threadpool(struct llarp_threadpool ** pool) - { - delete *pool; - *pool = nullptr; - } - +void llarp_free_threadpool(struct llarp_threadpool **pool) { + delete *pool; + *pool = nullptr; +} } diff --git a/llarp/threadpool.hpp b/llarp/threadpool.hpp index b20bbb91b..3d0e42136 100644 --- a/llarp/threadpool.hpp +++ b/llarp/threadpool.hpp @@ -6,31 +6,28 @@ #include #include #include -#include #include +#include #include -namespace llarp -{ - namespace thread - { - typedef std::mutex mtx_t; - typedef std::unique_lock lock_t; - struct Pool - { - - Pool(size_t sz); - void QueueJob(llarp_thread_job job); - void Join(); - std::vector threads; - std::queue jobs; +namespace llarp { +namespace thread { +typedef std::mutex mtx_t; +typedef std::unique_lock lock_t; +struct Pool { + + Pool(size_t sz); + void QueueJob(llarp_thread_job job); + void Join(); + std::vector threads; + std::queue jobs; + + mtx_t queue_mutex; + std::condition_variable condition; + std::atomic stop; +}; - mtx_t queue_mutex; - std::condition_variable condition; - std::atomic stop; - }; - - } -} +} // namespace thread +} // namespace llarp #endif diff --git a/llarp/time.cpp b/llarp/time.cpp index 279f6f445..0a9696ab6 100644 --- a/llarp/time.cpp +++ b/llarp/time.cpp @@ -1,25 +1,22 @@ -#include #include +#include -namespace llarp -{ - typedef std::chrono::steady_clock clock_t; +namespace llarp { +typedef std::chrono::steady_clock clock_t; - template - static IntType time_since_epoch() - { - return std::chrono::duration_cast(llarp::clock_t::now().time_since_epoch()).count(); - } +template static IntType time_since_epoch() { + return std::chrono::duration_cast( + llarp::clock_t::now().time_since_epoch()) + .count(); } +} // namespace llarp extern "C" { - llarp_time_t llarp_time_now_ms() - { - return llarp::time_since_epoch(); - } - - llarp_seconds_t llarp_time_now_sec() - { - return llarp::time_since_epoch(); - } +llarp_time_t llarp_time_now_ms() { + return llarp::time_since_epoch(); +} + +llarp_seconds_t llarp_time_now_sec() { + return llarp::time_since_epoch(); +} }