diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ccfcda24..f431ae069 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) find_package(sodium) set(EXE llarpd) -set(EXE_SRC daemon/main.c) +set(EXE_SRC daemon/main.cpp) set(LIBS ${sodium_LIBRARY_RELEASE} pthread stdc++fs jemalloc mbedx509 mbedcrypto mbedtls) diff --git a/daemon.ini b/daemon.ini index 68d5d28c0..f27c11acc 100644 --- a/daemon.ini +++ b/daemon.ini @@ -1,9 +1,14 @@ [router] threads=1 +contact-file=router-contact.signed [netdb] dir=./tmp-nodes +[iwp-connect] +#named-node0=/path/to/routercontact0 +#named-node1=/path/to/routercontact1 + [iwp-links] lo=1090 #lo=eth diff --git a/daemon/main.c b/daemon/main.cpp similarity index 69% rename from daemon/main.c rename to daemon/main.cpp index 92b89503e..006f55bc1 100644 --- a/daemon/main.c +++ b/daemon/main.cpp @@ -3,22 +3,78 @@ #include #include -struct llarp_main { + +static void progress() { + printf("."); + fflush(stdout); +} + +struct llarp_main +{ struct llarp_alloc mem; - struct llarp_router *router; - struct llarp_threadpool *worker; - struct llarp_threadpool *thread; - struct llarp_logic *logic; - struct llarp_config *config; - struct llarp_nodedb *nodedb; - struct llarp_ev_loop *mainloop; + struct llarp_router *router = nullptr; + struct llarp_threadpool *worker = nullptr; + struct llarp_threadpool *thread = nullptr; + struct llarp_logic *logic = nullptr; + struct llarp_config *config = nullptr; + struct llarp_nodedb *nodedb = nullptr; + struct llarp_ev_loop *mainloop = nullptr; char nodedb_dir[256]; int exitcode; + + int shutdown() + { + printf("Shutting down "); + + progress(); + if(mainloop) + llarp_ev_loop_stop(mainloop); + + progress(); + if(worker) + llarp_threadpool_stop(worker); + + progress(); + + if(worker) + llarp_threadpool_join(worker); + + progress(); + if (logic) + llarp_logic_stop(logic); + + progress(); + + if(router) + llarp_stop_router(router); + + progress(); + llarp_free_router(&router); + + progress(); + llarp_free_config(&config); + + progress(); + llarp_ev_loop_free(&mainloop); + + progress(); + llarp_free_threadpool(&worker); + + progress(); + + llarp_free_logic(&logic); + progress(); + + printf("\n"); + fflush(stdout); + return exitcode; + } + }; 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; + llarp_main *m = static_cast(itr->user); if (!strcmp(section, "router")) { if (!strcmp(key, "threads")) { @@ -35,78 +91,14 @@ void iter_main_config(struct llarp_config_iterator *itr, const char *section, } } -static void progress() { - printf("."); - fflush(stdout); -} -int shutdown_llarp(struct llarp_main *m) { - printf("Shutting down "); - - progress(); - if(m->mainloop) - llarp_ev_loop_stop(m->mainloop); - - progress(); - if(m->worker) - llarp_threadpool_stop(m->worker); - - progress(); - - if(m->worker) - llarp_threadpool_join(m->worker); - - progress(); - if (m->logic) - { - llarp_logic_stop(m->logic); - } - - progress(); - if(m->router) - llarp_stop_router(m->router); - - progress(); - llarp_free_router(&m->router); - - progress(); - llarp_free_config(&m->config); - - progress(); - llarp_ev_loop_free(&m->mainloop); - - progress(); - llarp_free_threadpool(&m->worker); - - progress(); - - llarp_free_logic(&m->logic); - progress(); - - printf("\n"); - fflush(stdout); - return m->exitcode; -} - -struct llarp_main llarp = { - {0,0,0}, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - {0}, - 1 -}; +llarp_main llarp; void run_net(void * user) { - llarp_ev_loop_run(user); + llarp_ev_loop_run(static_cast(user)); } - void handle_signal(int sig) { printf("interrupted\n"); @@ -162,8 +154,7 @@ int main(int argc, char *argv[]) { printf("failed to initialize nodedb at %s\n", dir); } else printf("no nodedb defined\n"); - - return shutdown_llarp(&llarp); + return llarp.shutdown(); } else printf("Failed to load config %s\n", conffname); diff --git a/include/llarp/logic.h b/include/llarp/logic.h index b84bcdb3b..25246dde4 100644 --- a/include/llarp/logic.h +++ b/include/llarp/logic.h @@ -1,5 +1,6 @@ #ifndef LLARP_LOGIC_H #define LLARP_LOGIC_H +#include #include #include #ifdef __cplusplus @@ -8,7 +9,7 @@ extern "C" { struct llarp_logic; -struct llarp_logic* llarp_init_logic(); +struct llarp_logic* llarp_init_logic(struct llarp_alloc * mem); void llarp_free_logic(struct llarp_logic** logic); diff --git a/include/llarp/mem.h b/include/llarp/mem.h index c07534599..1c34e76c0 100644 --- a/include/llarp/mem.h +++ b/include/llarp/mem.h @@ -3,6 +3,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { diff --git a/llarp/config.cpp b/llarp/config.cpp index 9fc5a1c83..b8c0787a1 100644 --- a/llarp/config.cpp +++ b/llarp/config.cpp @@ -6,7 +6,7 @@ namespace llarp { template -static Section find_section(Config &c, const std::string &name, +static const 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; @@ -20,9 +20,9 @@ bool Config::Load(const char *fname) { auto &top = parser.top(); router = find_section(top, "router", section_t{}); network = find_section(top, "network", section_t{}); + connect = find_section(top, "iwp-connect", section_t{}); netdb = find_section(top, "netdb", section_t{}); iwp_links = find_section(top, "iwp-links", section_t{}); - dtls_links = find_section(top, "dtls-links", section_t{}); return true; } return false; @@ -53,7 +53,7 @@ void llarp_config_iter(struct llarp_config *conf, std::map sections = { {"router", conf->impl.router}, {"network", conf->impl.network}, - {"dtls-links", conf->impl.dtls_links}, + {"iwp-connect", conf->impl.connect}, {"iwp-links", conf->impl.iwp_links}, {"netdb", conf->impl.netdb}}; for (const auto §ion : sections) diff --git a/llarp/config.hpp b/llarp/config.hpp index 22805aa29..9e07cae18 100644 --- a/llarp/config.hpp +++ b/llarp/config.hpp @@ -13,7 +13,7 @@ struct Config { section_t network; section_t netdb; section_t iwp_links; - section_t dtls_links; + section_t connect; bool Load(const char *fname); };