diff --git a/daemon/main.c b/daemon/main.c index 2aed6af51..bad04dfb0 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -4,7 +4,8 @@ struct llarp_main { struct llarp_router *router; - struct llarp_threadpool *tp; + struct llarp_threadpool *worker; + struct llarp_threadpool *logic; struct llarp_config *config; struct llarp_ev_loop *mainloop; }; @@ -15,41 +16,57 @@ void iter_main_config(struct llarp_config_iterator *itr, const char *section, if (!strcmp(section, "router")) { if (!strcmp(key, "threads")) { int workers = atoi(val); - if (workers > 0 && m->tp == NULL) { + if (workers > 0 && m->worker == NULL) { printf("%s: %d worker threads\n", section, workers); - m->tp = llarp_init_threadpool(workers); + m->worker = llarp_init_threadpool(workers); } } } } -int shutdown_llarp(struct llarp_main *m) { - printf("Shutting down ."); - llarp_stop_router(m->router); +static void progress() +{ printf("."); fflush(stdout); +} + +int shutdown_llarp(struct llarp_main *m) { + printf("Shutting down "); + progress(); + llarp_stop_router(m->router); + progress(); llarp_ev_loop_stop(m->mainloop); - printf("."); - fflush(stdout); - llarp_threadpool_join(m->tp); - printf("."); - fflush(stdout); + progress(); + llarp_threadpool_stop(m->worker); + progress(); + llarp_threadpool_join(m->worker); + progress(); + if(m->logic) + llarp_threadpool_wait(m->logic); + progress(); + if(m->logic) + llarp_threadpool_join(m->logic); + progress(); llarp_free_router(&m->router); - printf("."); - fflush(stdout); + progress(); llarp_free_config(&m->config); - printf("."); - fflush(stdout); + progress(); llarp_ev_loop_free(&m->mainloop); - printf("."); + progress(); + llarp_free_threadpool(&m->worker); + progress(); + if(m->logic) + llarp_free_threadpool(&m->logic); + progress(); + printf("\n"); fflush(stdout); - llarp_free_threadpool(&m->tp); - printf(".\n"); return 0; } +struct llarp_main llarp; + int main(int argc, char *argv[]) { - struct llarp_main llarp = {NULL, NULL, NULL, NULL}; + memset(&llarp, 0, sizeof(struct llarp_main)); const char *conffname = "daemon.ini"; if (argc > 1) conffname = argv[1]; llarp_mem_stdlib(); @@ -63,12 +80,14 @@ int main(int argc, char *argv[]) { iter.visit = iter_main_config; llarp_config_iter(llarp.config, &iter); - if (!llarp.tp) llarp.tp = llarp_init_threadpool(2); - llarp.router = llarp_init_router(llarp.tp); + if (!llarp.worker) llarp.worker = llarp_init_threadpool(2); + llarp.router = llarp_init_router(llarp.worker); if (llarp_configure_router(llarp.router, llarp.config)) { - printf("Running\n"); - llarp_run_router(llarp.router, llarp.mainloop); + llarp.logic = llarp_init_threadpool(1); + printf("starting router\n"); + llarp_run_router(llarp.router, llarp.logic); + printf("running mainloop\n"); llarp_ev_loop_run(llarp.mainloop); } else printf("Failed to configure router\n"); diff --git a/include/llarp/buffer.h b/include/llarp/buffer.h index 582422500..109b7f717 100644 --- a/include/llarp/buffer.h +++ b/include/llarp/buffer.h @@ -3,9 +3,9 @@ #include #include #include +#include #include #include -#include #ifdef __cplusplus extern "C" { @@ -37,8 +37,9 @@ bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data, bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...); -bool llarp_buffer_readfile(llarp_buffer_t * buff, FILE * f,struct llarp_alloc * mem); - +bool llarp_buffer_readfile(llarp_buffer_t *buff, FILE *f, + struct llarp_alloc *mem); + #ifdef __cplusplus } #endif diff --git a/include/llarp/crypto_async.h b/include/llarp/crypto_async.h index d90c3852f..9cdb9f345 100644 --- a/include/llarp/crypto_async.h +++ b/include/llarp/crypto_async.h @@ -45,10 +45,9 @@ struct llarp_cipher_result { llarp_cipher_complete_hook hook; }; -struct llarp_async_cipher *llarp_async_cipher_new(llarp_sharedkey_t key, - struct llarp_crypto *crypto, - struct llarp_threadpool *result, - struct llarp_threadpool *worker); +struct llarp_async_cipher *llarp_async_cipher_new( + llarp_sharedkey_t key, struct llarp_crypto *crypto, + struct llarp_threadpool *result, struct llarp_threadpool *worker); void llarp_async_cipher_free(struct llarp_async_cipher **c); diff --git a/include/llarp/ev.h b/include/llarp/ev.h index a140461be..7bc2ba3fe 100644 --- a/include/llarp/ev.h +++ b/include/llarp/ev.h @@ -23,16 +23,16 @@ struct llarp_udp_io { struct sockaddr_in6 *addr; void *user; void *impl; - void (*recvfrom)(struct llarp_udp_io *, const struct sockaddr *, char *, + void (*recvfrom)(struct llarp_udp_io *, const struct sockaddr *, void *, ssize_t); }; - -int llarp_ev_add_udp(struct llarp_ev_loop *ev, - struct llarp_udp_io *udp); -int llarp_ev_udp_sendto(struct llarp_udp_io * udp, const struct sockaddr * to, const void * data, size_t sz); +int llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp); -int llarp_ev_close_udp(struct llarp_udp_io * udp); +int llarp_ev_udp_sendto(struct llarp_udp_io *udp, const struct sockaddr *to, + const void *data, size_t sz); + +int llarp_ev_close_udp(struct llarp_udp_io *udp); #ifdef __cplusplus } diff --git a/include/llarp/link.h b/include/llarp/link.h index 9afb941f3..71f8ffe74 100644 --- a/include/llarp/link.h +++ b/include/llarp/link.h @@ -65,7 +65,7 @@ struct llarp_link { int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener); void (*deregister_listener)(struct llarp_link *, int); bool (*configure)(struct llarp_link *, const char *, int, uint16_t); - bool (*start_link)(struct llarp_link *, struct llarp_ev_loop *); + bool (*start_link)(struct llarp_link *, struct llarp_threadpool *); bool (*stop_link)(struct llarp_link *); bool (*put_ai)(struct llarp_link *, struct llarp_ai *); void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter); diff --git a/include/llarp/nodedb.h b/include/llarp/nodedb.h index b8ea3d76b..6ff8fca39 100644 --- a/include/llarp/nodedb.h +++ b/include/llarp/nodedb.h @@ -6,53 +6,52 @@ extern "C" { #endif - struct llarp_nodedb; - - /** create an empty nodedb */ - struct llarp_nodedb * llarp_nodedb_new(); - - /** free a nodedb and all loaded rc */ - void llarp_nodedb_free(struct llarp_nodedb ** n); - - /** ensure a nodedb fs skiplist structure is at dir - create if not there. - */ - bool llarp_nodedb_ensure_dir(const char * dir); - - /** load entire nodedb from fs skiplist at dir */ - ssize_t llarp_nodedb_load_dir(struct llarp_nodedb * n, const char * dir); - - /** store entire nodedb to fs skiplist at dir */ - ssize_t llarp_nodedb_store_dir(struct llarp_nodedb * n, const char * dir); - - struct llarp_nodedb_iter - { - void * user; - struct llarp_rc * rc; - bool (*visit)(struct llarp_nodedb_iter *); - }; - - /** - iterate over all loaded rc with an iterator - */ - void llarp_nodedb_iterate_all(struct llarp_nodedb * n, struct llarp_nodedb_iter i); - - /** - find rc by rc.k being value k - returns true if found otherwise returns false - */ - bool llarp_nodedb_find_rc(struct llarp_nodedb * n, struct llarp_rc * rc, llarp_pubkey_t k); - - /** - put an rc into the node db - overwrites with new contents if already present - flushes the single entry to disk - returns true on success and false on error - */ - bool llarp_nodedb_put_rc(struct llarp_nodedb * n, struct llarp_rc * rc); - - - +struct llarp_nodedb; + +/** create an empty nodedb */ +struct llarp_nodedb *llarp_nodedb_new(); + +/** free a nodedb and all loaded rc */ +void llarp_nodedb_free(struct llarp_nodedb **n); + +/** ensure a nodedb fs skiplist structure is at dir + create if not there. + */ +bool llarp_nodedb_ensure_dir(const char *dir); + +/** load entire nodedb from fs skiplist at dir */ +ssize_t llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir); + +/** store entire nodedb to fs skiplist at dir */ +ssize_t llarp_nodedb_store_dir(struct llarp_nodedb *n, const char *dir); + +struct llarp_nodedb_iter { + void *user; + struct llarp_rc *rc; + bool (*visit)(struct llarp_nodedb_iter *); +}; + +/** + iterate over all loaded rc with an iterator + */ +void llarp_nodedb_iterate_all(struct llarp_nodedb *n, + struct llarp_nodedb_iter i); + +/** + find rc by rc.k being value k + returns true if found otherwise returns false + */ +bool llarp_nodedb_find_rc(struct llarp_nodedb *n, struct llarp_rc *rc, + llarp_pubkey_t k); + +/** + put an rc into the node db + overwrites with new contents if already present + flushes the single entry to disk + returns true on success and false on error + */ +bool llarp_nodedb_put_rc(struct llarp_nodedb *n, struct llarp_rc *rc); + #ifdef __cplusplus } #endif diff --git a/include/llarp/router.h b/include/llarp/router.h index 791e0541c..0e7325aec 100644 --- a/include/llarp/router.h +++ b/include/llarp/router.h @@ -12,13 +12,13 @@ extern "C" { struct llarp_router; -struct llarp_router *llarp_init_router(struct llarp_threadpool *tp); +struct llarp_router *llarp_init_router(struct llarp_threadpool *worker); void llarp_free_router(struct llarp_router **router); bool 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_run_router(struct llarp_router *router, struct llarp_threadpool * logic); void llarp_stop_router(struct llarp_router *router); /** get router's inbound link level frame queue */ diff --git a/include/llarp/router_contact.h b/include/llarp/router_contact.h index 5a876553e..039db04f3 100644 --- a/include/llarp/router_contact.h +++ b/include/llarp/router_contact.h @@ -20,7 +20,6 @@ void llarp_rc_free(struct llarp_rc *rc); bool llarp_rc_verify_sig(struct llarp_rc *rc); #ifdef __cplusplus - } #endif #endif diff --git a/include/llarp/threadpool.h b/include/llarp/threadpool.h index eb78ba8c5..9c3a7c384 100644 --- a/include/llarp/threadpool.h +++ b/include/llarp/threadpool.h @@ -24,9 +24,9 @@ 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_stop(struct llarp_threadpool * tp); +void llarp_threadpool_stop(struct llarp_threadpool *tp); void llarp_threadpool_join(struct llarp_threadpool *tp); - + void llarp_threadpool_wait(struct llarp_threadpool *tp); #ifdef __cplusplus diff --git a/llarp/buffer.cpp b/llarp/buffer.cpp index 9ddb86924..7f30314bc 100644 --- a/llarp/buffer.cpp +++ b/llarp/buffer.cpp @@ -15,21 +15,19 @@ bool llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) { return true; } - bool llarp_buffer_readfile(llarp_buffer_t * buff, FILE * f, llarp_alloc * mem) - { - ssize_t len; - fseek(f, 0, SEEK_END); - len = ftell(f); +bool llarp_buffer_readfile(llarp_buffer_t* buff, FILE* f, llarp_alloc* mem) { + ssize_t len; + fseek(f, 0, SEEK_END); + len = ftell(f); + rewind(f); + if (len > 0) { + buff->base = static_cast(mem->alloc(len, 8)); + buff->cur = buff->base; + buff->sz = len; + size_t sz = fread(buff->base, len, 1, f); rewind(f); - if(len > 0) - { - buff->base = static_cast(mem->alloc(len, 8)); - buff->cur = buff->base; - buff->sz = len; - size_t sz = fread(buff->base, len, 1, f); - rewind(f); - return sz == len; - } - return false; + return sz == len; } + return false; +} } diff --git a/llarp/crypto.hpp b/llarp/crypto.hpp index 02dfda253..f113e5bb4 100644 --- a/llarp/crypto.hpp +++ b/llarp/crypto.hpp @@ -1,12 +1,11 @@ #ifndef LLARP_CRYPTO_HPP #define LLARP_CRYPTO_HPP -#include #include +#include -namespace llarp -{ - typedef std::array pubkey; +namespace llarp { +typedef std::array pubkey; } #endif diff --git a/llarp/crypto_async.c b/llarp/crypto_async.c index 10394bc7f..29527aecd 100644 --- a/llarp/crypto_async.c +++ b/llarp/crypto_async.c @@ -15,23 +15,20 @@ struct llarp_dh_internal { llarp_tunnel_nounce_t nounce; struct llarp_dh_result result; llarp_dh_func func; - struct llarp_async_dh * parent; + struct llarp_async_dh *parent; }; - static void llarp_crypto_dh_result(void *call) { struct llarp_dh_internal *impl = (struct llarp_dh_internal *)call; impl->result.hook(&impl->result); llarp_g_mem.free(impl); } - static void llarp_crypto_dh_work(void *user) { struct llarp_dh_internal *impl = (struct llarp_dh_internal *)user; impl->func(&impl->result.sharedkey, impl->theirkey, impl->nounce, impl->parent->ourkey); - struct llarp_thread_job job = { .user = impl, - .work = &llarp_crypto_dh_result }; + struct llarp_thread_job job = {.user = impl, .work = &llarp_crypto_dh_result}; llarp_threadpool_queue_job(impl->parent->result, job); } @@ -41,8 +38,7 @@ static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func, llarp_dh_complete_hook result, void *user) { struct llarp_dh_internal *impl = llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32); - struct llarp_thread_job job = {.user = impl, - .work = &llarp_crypto_dh_work}; + struct llarp_thread_job job = {.user = impl, .work = &llarp_crypto_dh_work}; memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t)); memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t)); impl->parent = dh; @@ -88,7 +84,7 @@ void llarp_async_dh_free(struct llarp_async_dh **dh) { struct llarp_async_cipher_internal { llarp_nounce_t nounce; struct llarp_cipher_result result; - struct llarp_async_cipher * parent; + struct llarp_async_cipher *parent; llarp_sym_cipher_func func; }; @@ -110,10 +106,8 @@ static void llarp_crypto_cipher_work(void *work) { struct llarp_async_cipher_internal *impl = (struct llarp_async_cipher_internal *)work; impl->func(impl->result.buff, impl->parent->key, impl->nounce); - struct llarp_thread_job job = { - .user = impl, - .work = &llarp_crypto_cipher_result - }; + struct llarp_thread_job job = {.user = impl, + .work = &llarp_crypto_cipher_result}; llarp_threadpool_queue_job(impl->parent->result, job); } @@ -134,10 +128,9 @@ void llarp_async_cipher_queue_op(struct llarp_async_cipher *c, llarp_threadpool_queue_job(c->worker, job); } -struct llarp_async_cipher *llarp_async_cipher_new(llarp_sharedkey_t key, - struct llarp_crypto *crypto, - struct llarp_threadpool *result, - struct llarp_threadpool *worker) { +struct llarp_async_cipher *llarp_async_cipher_new( + llarp_sharedkey_t key, struct llarp_crypto *crypto, + struct llarp_threadpool *result, struct llarp_threadpool *worker) { struct llarp_async_cipher *cipher = llarp_g_mem.alloc(sizeof(struct llarp_async_cipher), 16); cipher->func = crypto->xchacha20; diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 4b8eb9493..428c4d834 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -23,9 +23,7 @@ void llarp_ev_loop_free(struct llarp_ev_loop **ev) { *ev = nullptr; } -int llarp_ev_loop_run(struct llarp_ev_loop *ev) { - return ev->run(); -} +int llarp_ev_loop_run(struct llarp_ev_loop *ev) { return ev->run(); } int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev, struct llarp_udp_listener *listener) { @@ -39,6 +37,4 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) { } void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { loop->stop(); } - - } diff --git a/llarp/ev.hpp b/llarp/ev.hpp index beca3a731..fa4190b7e 100644 --- a/llarp/ev.hpp +++ b/llarp/ev.hpp @@ -4,32 +4,25 @@ #include -namespace llarp -{ - struct ev_io - { - char buff[2048]; - int fd; - ev_io(int f) : fd(f) {}; - virtual int read() = 0; - virtual int sendto(const sockaddr * dst, const void * data, size_t sz) = 0; - virtual ~ev_io() - { - ::close(fd); - }; - }; +namespace llarp { +struct ev_io { + char buff[2048]; + int fd; + ev_io(int f) : fd(f){}; + virtual int read(void * buf, size_t sz) = 0; + virtual int sendto(const sockaddr* dst, const void* data, size_t sz) = 0; + virtual ~ev_io() { ::close(fd); }; }; +}; // namespace llarp struct llarp_ev_loop { - virtual bool init() = 0; virtual int run() = 0; virtual void stop() = 0; - virtual bool udp_listen(llarp_udp_io * l) = 0; - - virtual ~llarp_ev_loop() {}; - + virtual bool udp_listen(llarp_udp_io* l) = 0; + + virtual ~llarp_ev_loop(){}; }; #endif diff --git a/llarp/ev_epoll.hpp b/llarp/ev_epoll.hpp index ac7ff88b9..4ab7b715d 100644 --- a/llarp/ev_epoll.hpp +++ b/llarp/ev_epoll.hpp @@ -1,38 +1,30 @@ #ifndef EV_EPOLL_HPP #define EV_EPOLL_HPP -#include "ev.hpp" -#include -#include #include +#include +#include +#include "ev.hpp" +namespace llarp { +struct udp_listener : public ev_io { + llarp_udp_io* udp; -namespace llarp -{ - struct udp_listener : public ev_io - { - llarp_udp_io * udp; - - udp_listener(int fd, llarp_udp_io * u) : - ev_io(fd), - udp(u) {}; + udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){}; - ~udp_listener() {} - - virtual int read() - { - sockaddr src; - socklen_t slen; - int ret = ::recvfrom(fd, buff, sizeof(buff), 0, &src, &slen); - if (ret == -1) return -1; - udp->recvfrom(udp, &src, buff, ret); - return 0; - } + ~udp_listener() {} - virtual int sendto(const sockaddr * to, const void * data, size_t sz) - { - socklen_t slen; - switch(to->sa_family) - { + virtual int read(void * buf, size_t sz) { + sockaddr src; + socklen_t slen; + int ret = ::recvfrom(fd, buf, sz, 0, &src, &slen); + if (ret == -1) return -1; + udp->recvfrom(udp, &src, buf, ret); + return 0; + } + + virtual int sendto(const sockaddr* to, const void* data, size_t sz) { + socklen_t slen; + switch (to->sa_family) { case AF_INET: slen = sizeof(struct sockaddr_in); break; @@ -41,106 +33,84 @@ namespace llarp break; default: return -1; - } - return ::sendto(fd, data, sz, 0, to, slen); } - }; + return ::sendto(fd, data, sz, 0, to, slen); + } }; +}; // namespace llarp -struct llarp_epoll_loop : public llarp_ev_loop -{ - +struct llarp_epoll_loop : public llarp_ev_loop { int epollfd; - - llarp_epoll_loop() : epollfd(-1) - { - - } - ~llarp_epoll_loop() - { - } + llarp_epoll_loop() : epollfd(-1) {} + + ~llarp_epoll_loop() {} - bool init() - { - if(epollfd == -1) - epollfd = epoll_create1(EPOLL_CLOEXEC); + bool init() { + if (epollfd == -1) epollfd = epoll_create1(EPOLL_CLOEXEC); return epollfd != -1; } - int run() - { + int run() { epoll_event events[1024]; int result; - do - { + char readbuf[2048]; + do { result = epoll_wait(epollfd, events, 1024, 0); - if(result > 0) - { + if (result > 0) { int idx = 0; - while(idx < result) - { - llarp::ev_io * ev = static_cast(events[idx].data.ptr); - if(events[idx].events & EPOLLIN) - { - if ( ev->read() == -1) - { + while (idx < result) { + llarp::ev_io* ev = static_cast(events[idx].data.ptr); + if (events[idx].events & EPOLLIN) { + if (ev->read(readbuf, sizeof(readbuf)) == -1) { epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, nullptr); } } ++idx; } } - } - while(result != -1); + } while (result != -1); return result; } - int udp_bind(const sockaddr * addr) - { + int udp_bind(const sockaddr* addr) { socklen_t slen; - switch(addr->sa_family) - { - case AF_INET: - slen = sizeof(struct sockaddr_in); - break; - case AF_INET6: - slen = sizeof(struct sockaddr_in6); - break; - default: - return -1; + switch (addr->sa_family) { + case AF_INET: + slen = sizeof(struct sockaddr_in); + break; + case AF_INET6: + slen = sizeof(struct sockaddr_in6); + break; + default: + return -1; } int fd = socket(addr->sa_family, SOCK_DGRAM, 0); if (fd == -1) return -1; - if(bind(fd, addr, slen) == -1) - { + if (bind(fd, addr, slen) == -1) { close(fd); return -1; } return fd; } - bool udp_listen(llarp_udp_io * l) - { + bool udp_listen(llarp_udp_io* l) { int fd = udp_bind((sockaddr*)l->addr); if (fd == -1) return false; - llarp::udp_listener * listener = new llarp::udp_listener(fd, l); + llarp::udp_listener* listener = new llarp::udp_listener(fd, l); epoll_event ev; ev.data.ptr = listener; ev.events = EPOLLIN; - if(epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) - { + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) { delete listener; return false; } return true; } - - void stop() - { - if(epollfd != -1) - ::close(epollfd); - + + void stop() { + if (epollfd != -1) ::close(epollfd); + epollfd = -1; } }; diff --git a/llarp/iwp.cpp b/llarp/iwp.cpp index bbc26dc44..e03e85089 100644 --- a/llarp/iwp.cpp +++ b/llarp/iwp.cpp @@ -26,7 +26,7 @@ struct link_impl { const char *name() { return linkname.c_str(); } - bool start(llarp_ev_loop *loop) { + bool start(llarp_threadpool *logic) { // todo: implement return false; } @@ -43,9 +43,9 @@ static bool configure(struct llarp_link *l, const char *ifname, int af, return link->configure(ifname, af, port); } -static bool start_link(struct llarp_link *l, struct llarp_ev_loop *loop) { +static bool start_link(struct llarp_link *l, struct llarp_threadpool *logic) { link_impl *link = static_cast(l->impl); - return link->start(loop); + return link->start(logic); } static bool stop_link(struct llarp_link *l) { diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 8b3a1ab3e..556cb1205 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -1,53 +1,43 @@ #include #include +#include #include "crypto.hpp" #include "fs.hpp" -#include - static const char skiplist_subdirs[] = "0123456789ABCDEF"; -struct llarp_nodedb -{ +struct llarp_nodedb { std::map entries; - ssize_t Load(const fs::path & path) - { + ssize_t Load(const fs::path &path) { std::error_code ec; - if(!fs::exists(path, ec)) - { + if (!fs::exists(path, ec)) { return -1; } ssize_t loaded = 0; - - for (const char & ch : skiplist_subdirs) - { + + for (const char &ch : skiplist_subdirs) { fs::path sub = path / std::string(ch, 1); - for(auto & f : fs::directory_iterator(sub)) - { + for (auto &f : fs::directory_iterator(sub)) { ssize_t l = loadSubdir(f); - if(l > 0) loaded += l; + if (l > 0) loaded += l; } } return loaded; } - bool loadfile(const fs::path & fpath) - { - llarp_rc * rc = new llarp_rc; + bool loadfile(const fs::path &fpath) { + llarp_rc *rc = new llarp_rc; llarp_buffer_t buff; - FILE * f = fopen(fpath.c_str(), "rb"); - if(!f) return false; - if(!llarp_buffer_readfile(&buff, f, &llarp_g_mem)) - { + FILE *f = fopen(fpath.c_str(), "rb"); + if (!f) return false; + if (!llarp_buffer_readfile(&buff, f, &llarp_g_mem)) { fclose(f); return false; } fclose(f); - if(llarp_rc_bdecode(rc, &buff)) - { - if(llarp_rc_verify_sig(rc)) - { + if (llarp_rc_bdecode(rc, &buff)) { + if (llarp_rc_verify_sig(rc)) { llarp::pubkey pk; memcpy(pk.data(), rc->pubkey, pk.size()); entries[pk] = rc; @@ -58,58 +48,45 @@ struct llarp_nodedb delete rc; return false; } - - ssize_t loadSubdir(const fs::path & dir) - { + + ssize_t loadSubdir(const fs::path &dir) { ssize_t sz = 0; - for (auto & path : fs::directory_iterator(dir)) - { - if(loadfile(path)) sz++; + for (auto &path : fs::directory_iterator(dir)) { + if (loadfile(path)) sz++; } return sz; } - }; extern "C" { - struct llarp_nodedb * llarp_nodedb_new() - { - return new llarp_nodedb; - } +struct llarp_nodedb *llarp_nodedb_new() { + return new llarp_nodedb; +} - void llarp_nodedb_free(struct llarp_nodedb ** n) - { - if(*n) - delete *n; - *n = nullptr; - } +void llarp_nodedb_free(struct llarp_nodedb **n) { + if (*n) delete *n; + *n = nullptr; +} +bool llarp_nodedb_ensure_dir(const char *dir) { + fs::path path(dir); + std::error_code ec; + if (!fs::exists(dir, ec)) fs::create_directories(path, ec); - bool llarp_nodedb_ensure_dir(const char * dir) - { - fs::path path(dir); - std::error_code ec; - if(!fs::exists(dir, ec)) - fs::create_directories(path, ec); - - if(ec) - return false; - - if (!fs::is_directory(path)) - return false; + if (ec) return false; - for (const char & ch : skiplist_subdirs) - { - fs::path sub = path / std::string(ch, 1); - fs::create_directory(sub, ec); - if(ec) return false; - } - return true; - } - - ssize_t llarp_nodedb_load_dir(struct llarp_nodedb * n, const char * dir) - { - return n->Load(dir); + if (!fs::is_directory(path)) return false; + + for (const char &ch : skiplist_subdirs) { + fs::path sub = path / std::string(ch, 1); + fs::create_directory(sub, ec); + if (ec) return false; } + return true; +} + +ssize_t llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir) { + return n->Load(dir); +} } diff --git a/llarp/router.cpp b/llarp/router.cpp index 8fa63c742..63c2cd0e7 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -58,9 +58,9 @@ bool llarp_configure_router(struct llarp_router *router, return router->Ready(); } -void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) { - router->ForEachLink([loop](llarp_link *link) { - int result = link->start_link(link, loop); +void llarp_run_router(struct llarp_router *router, struct llarp_threadpool * logic) { + router->ForEachLink([logic](llarp_link *link) { + int result = link->start_link(link, logic); if (result == -1) printf("link %s failed to start\n", link->name(link)); }); } diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 3dacc4ee2..863910579 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -21,14 +21,11 @@ static bool bencode_rc_xi_write(struct llarp_xi_list_iter *i, extern "C" { - void llarp_rc_free(struct llarp_rc *rc) - { - if(rc->exits) - llarp_xi_list_free(rc->exits); - if(rc->addrs) - llarp_ai_list_free(rc->addrs); - } - +void llarp_rc_free(struct llarp_rc *rc) { + if (rc->exits) llarp_xi_list_free(rc->exits); + if (rc->addrs) llarp_ai_list_free(rc->addrs); +} + bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) { /* write dict begin */ if (!bencode_start_dict(buff)) return false; diff --git a/llarp/threadpool.cpp b/llarp/threadpool.cpp index 12f739647..7d57a100b 100644 --- a/llarp/threadpool.cpp +++ b/llarp/threadpool.cpp @@ -23,8 +23,7 @@ Pool::Pool(size_t workers) { } } -void Pool::Stop() -{ +void Pool::Stop() { { lock_t lock(queue_mutex); stop = true; @@ -72,13 +71,9 @@ void llarp_threadpool_join(struct llarp_threadpool *pool) { pool->impl.Join(); } void llarp_threadpool_start(struct llarp_threadpool *pool) { /** no op */ } -void llarp_threadpool_stop(struct llarp_threadpool *pool) -{ - pool->impl.Stop(); -} +void llarp_threadpool_stop(struct llarp_threadpool *pool) { pool->impl.Stop(); } -void llarp_threadpool_wait(struct llarp_threadpool *pool) -{ +void llarp_threadpool_wait(struct llarp_threadpool *pool) { std::mutex mtx; { std::unique_lock lock(mtx); diff --git a/test/test_async_cipher.c b/test/test_async_cipher.c index efc4a5c9f..21bbd64ef 100644 --- a/test/test_async_cipher.c +++ b/test/test_async_cipher.c @@ -7,7 +7,7 @@ struct bench_main { size_t completed; size_t num; size_t jobs; - struct llarp_threadpool * pool; + struct llarp_threadpool *pool; struct llarp_async_cipher *cipher; struct llarp_crypto crypto; }; @@ -42,8 +42,8 @@ int main(int argc, char *argv[]) { llarp_threadpool_start(b_main.pool); tp = llarp_init_threadpool(8); - b_main.num = 500000; - b_main.jobs = 10; + b_main.num = 5000000; + b_main.jobs = 5000; b_main.completed = 0; llarp_sharedkey_t key; b_main.crypto.randbytes(key, sizeof(llarp_sharedkey_t)); @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) { llarp_async_cipher_queue_op(b_main.cipher, msg, nounce, handle_cipher_complete, &b_main); } - + printf("started %ld jobs\n", b_main.jobs); llarp_threadpool_wait(b_main.pool); llarp_threadpool_join(b_main.pool); llarp_threadpool_stop(tp); diff --git a/test/test_async_dh.c b/test/test_async_dh.c index 65164a9c2..d28cf56f3 100644 --- a/test/test_async_dh.c +++ b/test/test_async_dh.c @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) { tp = llarp_init_threadpool(8); dh_main.result = llarp_init_threadpool(1); llarp_threadpool_start(dh_main.result); - + dh_main.num = 500000; dh_main.completed = 0; llarp_seckey_t ourkey; @@ -65,10 +65,10 @@ int main(int argc, char *argv[]) { llarp_threadpool_join(dh_main.result); llarp_threadpool_stop(tp); llarp_threadpool_join(tp); - + llarp_free_threadpool(&tp); llarp_free_threadpool(&dh_main.result); - + llarp_async_dh_free(&dh_main.dh); printf("did %ld of %ld work\n", dh_main.completed, dh_main.num); return 0;