diff --git a/CMakeLists.txt b/CMakeLists.txt index 319d9d63e..dca31b269 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,8 @@ set(LIBS sodium pthread stdc++fs) set(LIB llarp) +set(SHARED_LIB ${LIB}) +set(STATIC_LIB ${LIB}-static) set(LIB_SRC llarp/address_info.cpp @@ -52,9 +54,11 @@ include_directories(include) include_directories(${sodium_INCLUDE_DIR}) -add_library(${LIB} STATIC ${LIB_SRC}) +add_library(${STATIC_LIB} STATIC ${LIB_SRC}) +add_library(${SHARED_LIB} SHARED ${LIB_SRC}) + add_executable(${EXE} ${EXE_SRC}) -target_link_libraries(${EXE} ${LIB} ${LIBS}) +target_link_libraries(${EXE} ${STATIC_LIB} ${LIBS}) add_executable(rcutil daemon/rcutil.cpp) -target_link_libraries(rcutil ${LIB} ${LIBS}) +target_link_libraries(rcutil ${STATIC_LIB} ${LIBS}) diff --git a/daemon/main.cpp b/daemon/main.cpp index f45e89618..62ac253b6 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -1,14 +1,14 @@ +#include #include -#include #include -std::unique_ptr< llarp::Context > ctx; +struct llarp_main *ctx = 0; void handle_signal(int sig) { if(ctx) - ctx->HandleSignal(sig); + llarp_main_signal(ctx, sig); } int @@ -18,14 +18,9 @@ main(int argc, char *argv[]) if(argc > 1) conffname = argv[1]; - ctx.reset(new llarp::Context(std::cout)); - - signal(SIGINT, handle_signal); - - if(!ctx->LoadConfig(conffname)) - return 1; - - auto exitcode = ctx->Run(); - ctx->Close(); - return exitcode; + if(llarp_main_init(&ctx, conffname)) + { + signal(SIGINT, handle_signal); + return llarp_main_run(ctx); + } } diff --git a/include/llarp.h b/include/llarp.h index 688982ef3..4daadc5f7 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -6,4 +6,30 @@ #include #include #include + +#ifdef __cplusplus +extern "C" { +#endif + +/** llarp application context for C api */ +struct llarp_main; + +/** initialize application context and load config */ +bool +llarp_main_init(struct llarp_main **ptr, const char *fname); + +/** handle signal for main context */ +void +llarp_main_signal(struct llarp_main *ptr, int sig); + +/** run main context */ +int +llarp_main_run(struct llarp_main *ptr); + +void +llarp_main_free(struct llarp_main **ptr); + +#ifdef __cplusplus +} +#endif #endif diff --git a/llarp/context.cpp b/llarp/context.cpp index 66cfc713d..d0f7b4f5a 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "logger.hpp" @@ -30,6 +31,8 @@ namespace llarp llarp_config_iter(config, &iter); return true; } + llarp_free_config(&config); + llarp::Error(__FILE__, "failed to load config file ", configfile); return false; } @@ -201,3 +204,50 @@ namespace llarp return ReloadConfig(); } } + +extern "C" { +struct llarp_main +{ + std::unique_ptr< llarp::Context > ctx; +}; + +bool +llarp_main_init(struct llarp_main **ptr, const char *fname) +{ + if(!fname) + return false; + + llarp_main *m = new llarp_main; + m->ctx.reset(new llarp::Context(std::cout)); + if(!m->ctx->LoadConfig(fname)) + { + m->ctx->Close(); + delete m; + return false; + } + *ptr = m; + return true; +} + +void +llarp_main_signal(struct llarp_main *ptr, int sig) +{ + ptr->ctx->HandleSignal(sig); +} + +int +llarp_main_run(struct llarp_main *ptr) +{ + auto code = ptr->ctx->Run(); + ptr->ctx->Close(); + return code; +} + +void +llarp_main_free(struct llarp_main **ptr) +{ + if(*ptr) + delete *ptr; + *ptr = nullptr; +} +}