2018-05-27 18:03:10 +00:00
|
|
|
#include <llarp.h>
|
2018-05-27 17:44:01 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <llarp.hpp>
|
|
|
|
#include "logger.hpp"
|
2018-06-01 14:08:54 +00:00
|
|
|
#include "router.hpp"
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-05-30 20:56:47 +00:00
|
|
|
#if(__FreeBSD__)
|
2018-05-29 12:13:33 +00:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
Context::Context(std::ostream &stdout, bool singleThread)
|
|
|
|
: singleThreaded(singleThread), out(stdout)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO);
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Context::~Context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::progress()
|
|
|
|
{
|
|
|
|
out << "." << std::flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::ReloadConfig()
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info("loading config at ", configfile);
|
2018-05-27 17:44:01 +00:00
|
|
|
if(!llarp_load_config(config, configfile.c_str()))
|
|
|
|
{
|
|
|
|
llarp_config_iterator iter;
|
|
|
|
iter.user = this;
|
|
|
|
iter.visit = &iter_config;
|
|
|
|
llarp_config_iter(config, &iter);
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info("config loaded");
|
2018-05-27 17:44:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-27 18:03:10 +00:00
|
|
|
llarp_free_config(&config);
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("failed to load config file ", configfile);
|
2018-05-27 17:44:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::iter_config(llarp_config_iterator *itr, const char *section,
|
|
|
|
const char *key, const char *val)
|
|
|
|
{
|
|
|
|
Context *ctx = static_cast< Context * >(itr->user);
|
|
|
|
if(!strcmp(section, "router"))
|
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
if(!strcmp(key, "worker-threads") && !ctx->singleThreaded)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
|
|
|
int workers = atoi(val);
|
|
|
|
if(workers > 0 && ctx->worker == nullptr)
|
|
|
|
{
|
|
|
|
ctx->worker = llarp_init_threadpool(workers, "llarp-worker");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strcmp(key, "net-threads"))
|
|
|
|
{
|
|
|
|
ctx->num_nethreads = atoi(val);
|
|
|
|
if(ctx->num_nethreads <= 0)
|
|
|
|
ctx->num_nethreads = 1;
|
2018-06-06 12:46:26 +00:00
|
|
|
if(ctx->singleThreaded)
|
|
|
|
ctx->num_nethreads = 0;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strcmp(section, "netdb"))
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "dir"))
|
|
|
|
{
|
|
|
|
strncpy(ctx->nodedb_dir, val, sizeof(ctx->nodedb_dir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Context::Run()
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info("starting up");
|
2018-05-27 17:44:01 +00:00
|
|
|
llarp_ev_loop_alloc(&mainloop);
|
|
|
|
llarp_crypto_libsodium_init(&crypto);
|
2018-05-28 13:49:44 +00:00
|
|
|
nodedb = llarp_nodedb_new(&crypto);
|
2018-06-13 11:40:49 +00:00
|
|
|
if(!nodedb_dir[0])
|
|
|
|
{
|
|
|
|
llarp::Error("no nodedb_dir configured");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodedb_dir[sizeof(nodedb_dir) - 1] = 0;
|
|
|
|
if(!llarp_nodedb_ensure_dir(nodedb_dir))
|
|
|
|
{
|
|
|
|
llarp::Error("nodedb_dir is incorrect");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
llarp::Info("nodedb_dir configured!");
|
|
|
|
ssize_t loaded = llarp_nodedb_load_dir(nodedb, nodedb_dir);
|
|
|
|
llarp::Info("nodedb_dir configured! loaded ", loaded, " RCs");
|
|
|
|
if (loaded < 0)
|
|
|
|
{
|
|
|
|
// shouldn't be possible
|
|
|
|
llarp::Error("nodedb_dir directory doesn't exist");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-06 17:02:57 +00:00
|
|
|
// ensure worker thread pool
|
|
|
|
if(!worker && !singleThreaded)
|
|
|
|
worker = llarp_init_threadpool(2, "llarp-worker");
|
|
|
|
else if(singleThreaded)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2018-06-06 17:02:57 +00:00
|
|
|
llarp::Info("running in single threaded mode");
|
|
|
|
worker = llarp_init_same_process_threadpool();
|
|
|
|
}
|
|
|
|
// ensure netio thread
|
|
|
|
if(singleThreaded)
|
|
|
|
{
|
|
|
|
logic = llarp_init_single_process_logic(worker);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logic = llarp_init_logic();
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-06-06 17:02:57 +00:00
|
|
|
router = llarp_init_router(worker, mainloop, logic);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-06-06 17:02:57 +00:00
|
|
|
if(llarp_configure_router(router, config))
|
|
|
|
{
|
|
|
|
if(custom_dht_func)
|
|
|
|
{
|
|
|
|
llarp::Info("using custom dht function");
|
|
|
|
llarp_dht_set_msg_handler(router->dht, custom_dht_func);
|
|
|
|
}
|
|
|
|
llarp_run_router(router, nodedb);
|
|
|
|
// run net io thread
|
|
|
|
if(singleThreaded)
|
|
|
|
{
|
|
|
|
llarp::Info("running mainloop");
|
|
|
|
llarp_ev_loop_run_single_process(mainloop, worker, logic);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto netio = mainloop;
|
|
|
|
while(num_nethreads--)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2018-06-06 17:02:57 +00:00
|
|
|
netio_threads.emplace_back([netio]() { llarp_ev_loop_run(netio); });
|
2018-05-30 20:56:47 +00:00
|
|
|
#if(__APPLE__ && __MACH__)
|
2018-05-29 12:13:33 +00:00
|
|
|
|
2018-05-30 20:56:47 +00:00
|
|
|
#elif(__FreeBSD__)
|
2018-06-06 17:02:57 +00:00
|
|
|
pthread_set_name_np(netio_threads.back().native_handle(),
|
|
|
|
"llarp-netio");
|
2018-05-29 12:13:33 +00:00
|
|
|
#else
|
2018-06-06 17:02:57 +00:00
|
|
|
pthread_setname_np(netio_threads.back().native_handle(),
|
|
|
|
"llarp-netio");
|
2018-05-29 12:13:33 +00:00
|
|
|
#endif
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
2018-06-06 17:02:57 +00:00
|
|
|
llarp::Info("running mainloop");
|
|
|
|
llarp_logic_mainloop(logic);
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
2018-06-06 17:02:57 +00:00
|
|
|
return 0;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
else
|
2018-06-06 17:02:57 +00:00
|
|
|
llarp::Error("Failed to configure router");
|
2018-05-27 17:44:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::HandleSignal(int sig)
|
|
|
|
{
|
|
|
|
if(sig == SIGINT)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info("SIGINT");
|
2018-05-27 17:44:01 +00:00
|
|
|
SigINT();
|
|
|
|
}
|
|
|
|
if(sig == SIGHUP)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info("SIGHUP");
|
2018-05-27 17:44:01 +00:00
|
|
|
ReloadConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::SigINT()
|
|
|
|
{
|
2018-05-28 20:51:15 +00:00
|
|
|
Close();
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Close()
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("stop router");
|
2018-05-28 20:51:15 +00:00
|
|
|
if(router)
|
|
|
|
llarp_stop_router(router);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("stop workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
if(worker)
|
|
|
|
llarp_threadpool_stop(worker);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("join workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
if(worker)
|
|
|
|
llarp_threadpool_join(worker);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("stop logic");
|
2018-05-28 20:51:15 +00:00
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
if(logic)
|
|
|
|
llarp_logic_stop(logic);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("free config");
|
2018-05-27 17:44:01 +00:00
|
|
|
llarp_free_config(&config);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("free workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
llarp_free_threadpool(&worker);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("free nodedb");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_nodedb_free(&nodedb);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-05-28 20:51:15 +00:00
|
|
|
for(size_t i = 0; i < netio_threads.size(); ++i)
|
|
|
|
{
|
|
|
|
if(mainloop)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("stopping event loop thread ", i);
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_ev_loop_stop(mainloop);
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("free router");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_free_router(&router);
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("free logic");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_free_logic(&logic);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
|
|
|
for(auto &t : netio_threads)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("join netio thread");
|
2018-05-27 17:44:01 +00:00
|
|
|
t.join();
|
|
|
|
}
|
2018-05-28 20:51:15 +00:00
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
netio_threads.clear();
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("free mainloop");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_ev_loop_free(&mainloop);
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::LoadConfig(const std::string &fname)
|
|
|
|
{
|
|
|
|
llarp_new_config(&config);
|
|
|
|
configfile = fname;
|
|
|
|
return ReloadConfig();
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 18:03:10 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
struct llarp_main
|
|
|
|
{
|
|
|
|
std::unique_ptr< llarp::Context > ctx;
|
|
|
|
};
|
|
|
|
|
2018-05-27 19:13:25 +00:00
|
|
|
struct llarp_main *
|
2018-06-06 12:49:46 +00:00
|
|
|
llarp_main_init(const char *fname, bool multiProcess)
|
2018-05-27 18:03:10 +00:00
|
|
|
{
|
|
|
|
if(!fname)
|
2018-05-28 20:51:15 +00:00
|
|
|
fname = "daemon.ini";
|
2018-05-27 18:03:10 +00:00
|
|
|
|
|
|
|
llarp_main *m = new llarp_main;
|
2018-06-06 12:49:46 +00:00
|
|
|
m->ctx.reset(new llarp::Context(std::cout, !multiProcess));
|
2018-05-27 18:03:10 +00:00
|
|
|
if(!m->ctx->LoadConfig(fname))
|
|
|
|
{
|
|
|
|
m->ctx->Close();
|
|
|
|
delete m;
|
2018-05-27 19:13:25 +00:00
|
|
|
return nullptr;
|
2018-05-27 18:03:10 +00:00
|
|
|
}
|
2018-05-27 19:13:25 +00:00
|
|
|
return m;
|
2018-05-27 18:03:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
void
|
|
|
|
llarp_main_set_dht_handler(struct llarp_main *ptr, llarp_dht_msg_handler func)
|
|
|
|
{
|
|
|
|
ptr->ctx->custom_dht_func = func;
|
|
|
|
}
|
|
|
|
|
2018-05-27 18:03:10 +00:00
|
|
|
void
|
|
|
|
llarp_main_signal(struct llarp_main *ptr, int sig)
|
|
|
|
{
|
|
|
|
ptr->ctx->HandleSignal(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
llarp_main_run(struct llarp_main *ptr)
|
|
|
|
{
|
2018-05-27 19:13:25 +00:00
|
|
|
return ptr->ctx->Run();
|
2018-05-27 18:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-27 19:13:25 +00:00
|
|
|
llarp_main_free(struct llarp_main *ptr)
|
2018-05-27 18:03:10 +00:00
|
|
|
{
|
2018-05-27 19:13:25 +00:00
|
|
|
delete ptr;
|
2018-05-27 18:03:10 +00:00
|
|
|
}
|
|
|
|
}
|