2019-01-11 01:12:43 +01:00
|
|
|
#include <llarp.hpp>
|
|
|
|
#include <llarp.h>
|
|
|
|
|
2019-07-02 23:28:28 +02:00
|
|
|
#include <config/config.hpp>
|
2019-01-26 16:40:58 +01:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-05-28 02:19:25 +02:00
|
|
|
#include <crypto/crypto_noop.hpp>
|
2019-01-11 01:12:43 +01:00
|
|
|
#include <dht/context.hpp>
|
2018-12-12 03:52:51 +01:00
|
|
|
#include <dnsd.hpp>
|
2019-01-11 02:19:36 +01:00
|
|
|
#include <ev/ev.hpp>
|
2019-04-14 18:08:51 +02:00
|
|
|
#include <metrics/metrictank_publisher.hpp>
|
2019-06-15 16:48:48 +02:00
|
|
|
#include <metrics/json_publisher.hpp>
|
|
|
|
#include <metrics/stream_publisher.hpp>
|
2019-01-11 01:12:43 +01:00
|
|
|
#include <nodedb.hpp>
|
2019-01-14 22:46:07 +01:00
|
|
|
#include <router/router.hpp>
|
2019-01-11 02:19:36 +01:00
|
|
|
#include <util/logger.h>
|
2019-06-02 23:19:10 +02:00
|
|
|
#include <util/memfn.hpp>
|
2019-04-05 11:20:48 +02:00
|
|
|
#include <util/metrics.hpp>
|
|
|
|
#include <util/scheduler.hpp>
|
2019-01-11 01:12:43 +01:00
|
|
|
|
2019-04-14 18:08:51 +02:00
|
|
|
#include <absl/strings/str_split.h>
|
2019-04-19 20:24:33 +02:00
|
|
|
#include <cxxopts.hpp>
|
2018-12-12 03:52:51 +01:00
|
|
|
#include <signal.h>
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2018-07-26 03:00:15 +02:00
|
|
|
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
|
2018-05-29 14:13:33 +02:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-27 19:44:01 +02:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-04-05 11:20:48 +02:00
|
|
|
Context::Context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:44:01 +02:00
|
|
|
Context::~Context()
|
|
|
|
{
|
2019-04-05 11:20:48 +02:00
|
|
|
m_scheduler->stop();
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::progress()
|
|
|
|
{
|
2018-07-24 08:33:22 +02:00
|
|
|
std::cout << "." << std::flush;
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-12-24 17:09:05 +01:00
|
|
|
Context::Configure()
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
// llarp::LogInfo("loading config at ", configfile);
|
2019-02-12 01:33:19 +01:00
|
|
|
if(!config->Load(configfile.c_str()))
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2019-02-11 15:43:48 +01:00
|
|
|
config.release();
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("failed to load config file ", configfile);
|
2018-06-19 11:44:53 +02:00
|
|
|
return false;
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
2019-04-07 19:55:14 +02:00
|
|
|
|
2019-07-02 23:28:28 +02:00
|
|
|
// System config
|
|
|
|
if(!config->system.pidfile.empty())
|
2019-04-07 19:55:14 +02:00
|
|
|
{
|
2019-07-02 23:28:28 +02:00
|
|
|
SetPIDFile(config->system.pidfile);
|
2019-04-07 19:55:14 +02:00
|
|
|
}
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2019-07-02 23:28:28 +02:00
|
|
|
// Router config
|
|
|
|
if(!singleThreaded && config->router.workerThreads > 0 && !worker)
|
2019-01-18 14:24:33 +01:00
|
|
|
{
|
2019-07-02 23:28:28 +02:00
|
|
|
worker.reset(
|
|
|
|
llarp_init_threadpool(config->router.workerThreads, "llarp-worker"));
|
2019-01-18 14:24:33 +01:00
|
|
|
}
|
2019-07-02 23:28:28 +02:00
|
|
|
|
|
|
|
if(singleThreaded)
|
2019-04-07 19:55:14 +02:00
|
|
|
{
|
2019-07-02 23:28:28 +02:00
|
|
|
num_nethreads = 0;
|
2019-04-07 19:55:14 +02:00
|
|
|
}
|
2019-07-02 23:28:28 +02:00
|
|
|
else
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2019-07-02 23:28:28 +02:00
|
|
|
num_nethreads = config->router.num_nethreads;
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
2019-07-02 23:28:28 +02:00
|
|
|
|
|
|
|
nodedb_dir = config->netdb.nodedb_dir;
|
|
|
|
|
|
|
|
if(!config->metrics.disableMetrics)
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2019-07-02 23:28:28 +02:00
|
|
|
auto &metricsConfig = config->metrics;
|
|
|
|
auto &tags = metricsConfig.metricTags;
|
|
|
|
tags["netid"] = config->router.netid;
|
|
|
|
tags["nickname"] = config->router.rc.Nick();
|
|
|
|
setupMetrics(metricsConfig);
|
|
|
|
if(!config->metrics.disableMetricLogs)
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2019-07-02 23:28:28 +02:00
|
|
|
m_metricsManager->instance()->addGlobalPublisher(
|
|
|
|
std::make_shared< metrics::StreamPublisher >(std::cerr));
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 23:28:28 +02:00
|
|
|
return true;
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 19:55:14 +02:00
|
|
|
void
|
2019-07-02 23:28:28 +02:00
|
|
|
Context::setupMetrics(const MetricsConfig &metricsConfig)
|
2019-04-07 19:55:14 +02:00
|
|
|
{
|
|
|
|
if(!m_scheduler)
|
|
|
|
{
|
|
|
|
m_scheduler = std::make_unique< thread::Scheduler >();
|
|
|
|
}
|
|
|
|
if(!m_metricsManager)
|
|
|
|
{
|
|
|
|
m_metricsManager = std::make_unique< metrics::DefaultManagerGuard >();
|
|
|
|
}
|
|
|
|
if(!m_metricsPublisher)
|
|
|
|
{
|
|
|
|
m_metricsPublisher = std::make_unique< metrics::PublisherScheduler >(
|
|
|
|
*m_scheduler, m_metricsManager->instance());
|
|
|
|
}
|
|
|
|
|
2019-07-02 23:28:28 +02:00
|
|
|
if(!metricsConfig.jsonMetricsPath.native().empty())
|
2019-04-16 03:26:05 +02:00
|
|
|
{
|
|
|
|
m_metricsManager->instance()->addGlobalPublisher(
|
|
|
|
std::make_shared< metrics::JsonPublisher >(
|
|
|
|
std::bind(&metrics::JsonPublisher::directoryPublisher,
|
2019-07-02 23:28:28 +02:00
|
|
|
std::placeholders::_1, metricsConfig.jsonMetricsPath)));
|
2019-04-16 03:26:05 +02:00
|
|
|
}
|
2019-04-14 18:08:51 +02:00
|
|
|
|
2019-07-02 23:28:28 +02:00
|
|
|
if(!metricsConfig.metricTankHost.empty())
|
2019-04-14 18:08:51 +02:00
|
|
|
{
|
|
|
|
if(std::getenv("LOKINET_ENABLE_METRIC_TANK"))
|
|
|
|
{
|
|
|
|
static std::string WARNING = R"(
|
|
|
|
__ ___ ____ _ _ ___ _ _ ____
|
|
|
|
\ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
|
|
|
|
\ \ /\ / / _ \ | |_) | \| || || \| | | _
|
|
|
|
\ V V / ___ \| _ <| |\ || || |\ | |_| |
|
|
|
|
\_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
|
|
|
|
|
2019-05-28 02:19:25 +02:00
|
|
|
This Lokinet session is not private!!
|
2019-04-14 18:08:51 +02:00
|
|
|
|
2019-05-28 02:19:25 +02:00
|
|
|
Sending connection metrics to metrictank!!
|
2019-04-14 18:08:51 +02:00
|
|
|
__ ___ ____ _ _ ___ _ _ ____
|
|
|
|
\ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
|
|
|
|
\ \ /\ / / _ \ | |_) | \| || || \| | | _
|
|
|
|
\ V V / ___ \| _ <| |\ || || |\ | |_| |
|
|
|
|
\_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
std::cerr << WARNING << '\n';
|
|
|
|
|
|
|
|
std::pair< std::string, std::string > split =
|
2019-07-02 23:28:28 +02:00
|
|
|
absl::StrSplit(metricsConfig.metricTankHost, ':');
|
2019-04-14 18:08:51 +02:00
|
|
|
|
|
|
|
m_metricsManager->instance()->addGlobalPublisher(
|
|
|
|
std::make_shared< metrics::MetricTankPublisher >(
|
2019-07-02 23:28:28 +02:00
|
|
|
metricsConfig.metricTags, split.first, stoi(split.second)));
|
2019-04-14 18:08:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "metrictank host specified, but "
|
|
|
|
"LOKINET_ENABLE_METRIC_TANK not set, skipping\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 19:23:58 +02:00
|
|
|
m_metricsPublisher->setDefault(absl::Seconds(30));
|
2019-04-07 19:55:14 +02:00
|
|
|
|
|
|
|
m_scheduler->start();
|
|
|
|
}
|
|
|
|
|
2019-01-21 16:45:18 +01:00
|
|
|
void
|
|
|
|
Context::SetPIDFile(const std::string &fname)
|
2019-01-18 14:24:33 +01:00
|
|
|
{
|
|
|
|
pidfile = fname;
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:44:01 +02:00
|
|
|
int
|
2018-06-19 11:44:53 +02:00
|
|
|
Context::LoadDatabase()
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2019-05-28 21:45:08 +02:00
|
|
|
nodedb = std::make_unique< llarp_nodedb >(router->diskworker());
|
2018-06-13 13:40:49 +02:00
|
|
|
|
2018-12-11 00:29:58 +01:00
|
|
|
if(!llarp_nodedb::ensure_dir(nodedb_dir.c_str()))
|
2018-06-13 13:40:49 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("nodedb_dir is incorrect");
|
2018-06-13 13:40:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-05 17:44:06 +02:00
|
|
|
// llarp::LogInfo("nodedb_dir [", nodedb_dir, "] configured!");
|
2018-12-11 00:29:58 +01:00
|
|
|
ssize_t loaded = nodedb->load_dir(nodedb_dir.c_str());
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogInfo("nodedb_dir loaded ", loaded, " RCs from [", nodedb_dir,
|
|
|
|
"]");
|
2018-06-17 17:26:00 +02:00
|
|
|
if(loaded < 0)
|
2018-06-13 13:40:49 +02:00
|
|
|
{
|
|
|
|
// shouldn't be possible
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("nodedb_dir directory doesn't exist");
|
2018-06-13 13:40:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-19 11:44:53 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-07 16:30:22 +01:00
|
|
|
Context::PutDatabase(__attribute__((unused)) struct llarp::RouterContact &rc)
|
2018-06-19 11:44:53 +02:00
|
|
|
{
|
2018-09-19 14:22:34 +02:00
|
|
|
// FIXME
|
2018-09-22 12:23:23 +02:00
|
|
|
// return llarp_nodedb_put_rc(nodedb, rc);
|
2018-09-19 14:22:34 +02:00
|
|
|
return false;
|
2018-06-19 11:44:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 14:22:34 +02:00
|
|
|
llarp::RouterContact *
|
2018-11-07 16:30:22 +01:00
|
|
|
Context::GetDatabase(__attribute__((unused)) const byte_t *pk)
|
2018-06-21 13:11:55 +02:00
|
|
|
{
|
2018-09-19 14:22:34 +02:00
|
|
|
// FIXME
|
2018-09-22 12:23:23 +02:00
|
|
|
// return llarp_nodedb_get_rc(nodedb, pk);
|
2018-09-19 14:22:34 +02:00
|
|
|
return nullptr;
|
2018-06-21 13:11:55 +02:00
|
|
|
}
|
2018-06-19 11:44:53 +02:00
|
|
|
|
|
|
|
int
|
2019-05-28 02:19:25 +02:00
|
|
|
Context::Setup(bool debug)
|
2018-06-19 11:44:53 +02:00
|
|
|
{
|
2018-07-24 08:37:12 +02:00
|
|
|
llarp::LogInfo(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO);
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogInfo("starting up");
|
2019-02-11 15:43:48 +01:00
|
|
|
mainloop = llarp_make_ev_loop();
|
2018-06-06 19:02:57 +02:00
|
|
|
// ensure worker thread pool
|
|
|
|
if(!worker && !singleThreaded)
|
2019-02-11 15:43:48 +01:00
|
|
|
worker.reset(llarp_init_threadpool(2, "llarp-worker"));
|
2018-06-06 19:02:57 +02:00
|
|
|
else if(singleThreaded)
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogInfo("running in single threaded mode");
|
2019-02-11 15:43:48 +01:00
|
|
|
worker.reset(llarp_init_same_process_threadpool());
|
2018-06-06 19:02:57 +02:00
|
|
|
}
|
|
|
|
// ensure netio thread
|
|
|
|
if(singleThreaded)
|
|
|
|
{
|
2019-05-22 18:20:03 +02:00
|
|
|
logic = std::make_shared< Logic >(worker.get());
|
2018-06-06 19:02:57 +02:00
|
|
|
}
|
|
|
|
else
|
2019-05-22 18:20:03 +02:00
|
|
|
logic = std::make_shared< Logic >();
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2019-05-28 02:19:25 +02:00
|
|
|
if(debug)
|
|
|
|
{
|
|
|
|
static std::string WARNING = R"(
|
|
|
|
__ ___ ____ _ _ ___ _ _ ____
|
|
|
|
\ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
|
|
|
|
\ \ /\ / / _ \ | |_) | \| || || \| | | _
|
|
|
|
\ V V / ___ \| _ <| |\ || || |\ | |_| |
|
|
|
|
\_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
|
|
|
|
|
|
|
|
This Lokinet session is not private!!
|
|
|
|
|
|
|
|
Sending traffic unencrypted!!
|
|
|
|
__ ___ ____ _ _ ___ _ _ ____
|
|
|
|
\ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
|
|
|
|
\ \ /\ / / _ \ | |_) | \| || || \| | | _
|
|
|
|
\ V V / ___ \| _ <| |\ || || |\ | |_| |
|
|
|
|
\_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
std::cerr << WARNING << '\n';
|
|
|
|
crypto = std::make_unique< NoOpCrypto >();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
crypto = std::make_unique< sodium::CryptoLibSodium >();
|
|
|
|
}
|
2019-05-28 21:45:08 +02:00
|
|
|
cryptoManager = std::make_unique< CryptoManager >(crypto.get());
|
|
|
|
|
2019-05-22 18:20:03 +02:00
|
|
|
router = std::make_unique< Router >(worker.get(), mainloop, logic);
|
2019-02-11 15:43:48 +01:00
|
|
|
if(!router->Configure(config.get()))
|
2018-06-06 19:02:57 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("Failed to configure router");
|
2018-06-23 16:56:59 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-12-28 16:34:41 +01:00
|
|
|
// must be done after router is made so we can use its disk io worker
|
2019-04-14 18:08:51 +02:00
|
|
|
// must also be done after configure so that netid is properly set if it
|
|
|
|
// is provided by config
|
2018-12-28 16:34:41 +01:00
|
|
|
if(!this->LoadDatabase())
|
|
|
|
return 1;
|
2018-11-26 23:46:22 +01:00
|
|
|
return 0;
|
2018-06-23 16:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Context::Run()
|
|
|
|
{
|
2018-12-02 19:07:07 +01:00
|
|
|
if(router == nullptr)
|
2018-06-23 16:56:59 +02:00
|
|
|
{
|
2018-12-02 19:07:07 +01:00
|
|
|
// we are not set up so we should die
|
|
|
|
llarp::LogError("cannot run non configured context");
|
|
|
|
return 1;
|
2018-06-23 16:56:59 +02:00
|
|
|
}
|
2019-01-18 14:24:33 +01:00
|
|
|
if(!WritePIDFile())
|
|
|
|
return 1;
|
2018-11-26 23:46:22 +01:00
|
|
|
// run
|
2019-02-11 15:43:48 +01:00
|
|
|
if(!router->Run(nodedb.get()))
|
2018-12-02 19:07:07 +01:00
|
|
|
return 1;
|
2019-01-21 16:45:18 +01:00
|
|
|
|
2018-06-23 16:56:59 +02:00
|
|
|
// run net io thread
|
2018-09-21 14:30:57 +02:00
|
|
|
llarp::LogInfo("running mainloop");
|
2019-05-22 18:20:03 +02:00
|
|
|
llarp_ev_loop_run_single_process(mainloop, worker.get(), logic);
|
2018-12-24 17:09:05 +01:00
|
|
|
// waits for router graceful stop
|
2018-06-23 16:56:59 +02:00
|
|
|
return 0;
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:24:33 +01:00
|
|
|
bool
|
|
|
|
Context::WritePIDFile() const
|
|
|
|
{
|
|
|
|
if(pidfile.size())
|
|
|
|
{
|
|
|
|
std::ofstream f(pidfile);
|
|
|
|
f << std::to_string(getpid());
|
|
|
|
return f.good();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::RemovePIDFile() const
|
|
|
|
{
|
|
|
|
if(pidfile.size())
|
|
|
|
{
|
|
|
|
fs::path f = pidfile;
|
|
|
|
std::error_code ex;
|
|
|
|
if(fs::exists(f, ex))
|
|
|
|
{
|
|
|
|
if(!ex)
|
|
|
|
fs::remove(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:44:01 +02:00
|
|
|
void
|
|
|
|
Context::HandleSignal(int sig)
|
|
|
|
{
|
2018-11-29 14:12:28 +01:00
|
|
|
if(sig == SIGINT || sig == SIGTERM)
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
|
|
|
SigINT();
|
|
|
|
}
|
2018-08-09 13:22:57 +02:00
|
|
|
// TODO(despair): implement hot-reloading config on NT
|
2018-07-30 06:38:14 +02:00
|
|
|
#ifndef _WIN32
|
2018-05-27 19:44:01 +02:00
|
|
|
if(sig == SIGHUP)
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogInfo("SIGHUP");
|
2018-12-24 17:09:05 +01:00
|
|
|
if(router)
|
|
|
|
{
|
2019-05-07 19:46:38 +02:00
|
|
|
router->hiddenServiceContext().ForEachService(
|
|
|
|
[](const std::string &name,
|
|
|
|
const llarp::service::Endpoint_ptr &ep) -> bool {
|
|
|
|
ep->ResetInternalState();
|
|
|
|
llarp::LogInfo("Reset internal state for ", name);
|
|
|
|
return true;
|
|
|
|
});
|
2019-05-07 20:15:22 +02:00
|
|
|
router->PumpLL();
|
2019-02-11 15:43:48 +01:00
|
|
|
Config newconfig;
|
2019-05-07 19:46:38 +02:00
|
|
|
if(!newconfig.Load(configfile.c_str()))
|
2018-12-24 17:09:05 +01:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to load config file ", configfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// validate config
|
2019-02-11 15:43:48 +01:00
|
|
|
if(!router->ValidateConfig(&newconfig))
|
2018-12-24 17:09:05 +01:00
|
|
|
{
|
|
|
|
llarp::LogWarn("new configuration is invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// reconfigure
|
2019-02-11 15:43:48 +01:00
|
|
|
if(!router->Reconfigure(&newconfig))
|
2018-12-24 17:09:05 +01:00
|
|
|
{
|
|
|
|
llarp::LogError("Failed to reconfigure so we will stop.");
|
|
|
|
router->Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llarp::LogInfo("router reconfigured");
|
|
|
|
}
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
2018-07-30 06:38:14 +02:00
|
|
|
#endif
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::SigINT()
|
|
|
|
{
|
2018-12-24 17:09:05 +01:00
|
|
|
if(router)
|
|
|
|
{
|
|
|
|
/// async stop router on sigint
|
|
|
|
router->Stop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(logic)
|
|
|
|
logic->stop();
|
2019-06-02 23:17:05 +02:00
|
|
|
llarp_ev_loop_stop(mainloop);
|
2018-12-24 17:09:05 +01:00
|
|
|
Close();
|
|
|
|
}
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Close()
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("stop workers");
|
2018-05-27 19:44:01 +02:00
|
|
|
if(worker)
|
2019-02-11 15:43:48 +01:00
|
|
|
llarp_threadpool_stop(worker.get());
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("join workers");
|
2018-05-27 19:44:01 +02:00
|
|
|
if(worker)
|
2019-02-11 15:43:48 +01:00
|
|
|
llarp_threadpool_join(worker.get());
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("free config");
|
2019-02-11 15:43:48 +01:00
|
|
|
config.release();
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("free workers");
|
2019-02-11 15:43:48 +01:00
|
|
|
worker.release();
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogDebug("free nodedb");
|
2019-02-11 15:43:48 +01:00
|
|
|
nodedb.release();
|
|
|
|
|
|
|
|
llarp::LogDebug("free router");
|
|
|
|
router.release();
|
|
|
|
|
|
|
|
llarp::LogDebug("free logic");
|
2019-05-22 18:20:03 +02:00
|
|
|
logic.reset();
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2019-01-18 14:24:33 +01:00
|
|
|
RemovePIDFile();
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::LoadConfig(const std::string &fname)
|
|
|
|
{
|
2019-02-11 15:43:48 +01:00
|
|
|
config = std::make_unique< Config >();
|
2018-05-27 19:44:01 +02:00
|
|
|
configfile = fname;
|
2018-12-24 17:09:05 +01:00
|
|
|
return Configure();
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
2018-07-05 17:44:06 +02:00
|
|
|
} // namespace llarp
|
2018-05-27 20:03:10 +02:00
|
|
|
|
|
|
|
struct llarp_main
|
|
|
|
{
|
|
|
|
std::unique_ptr< llarp::Context > ctx;
|
|
|
|
};
|
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
extern "C"
|
2018-05-27 20:03:10 +02:00
|
|
|
{
|
2018-07-27 05:41:55 +02:00
|
|
|
struct llarp_main *
|
|
|
|
llarp_main_init(const char *fname, bool multiProcess)
|
2018-07-12 15:43:37 +02:00
|
|
|
{
|
2018-08-09 13:22:57 +02:00
|
|
|
if(!fname)
|
|
|
|
fname = "daemon.ini";
|
|
|
|
char *var = getenv("LLARP_DEBUG");
|
|
|
|
if(var && *var == '1')
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogDebug);
|
|
|
|
}
|
2019-04-05 11:20:48 +02:00
|
|
|
llarp_main *m = new llarp_main;
|
|
|
|
m->ctx = std::make_unique< llarp::Context >();
|
2018-08-09 13:22:57 +02:00
|
|
|
m->ctx->singleThreaded = !multiProcess;
|
|
|
|
if(!m->ctx->LoadConfig(fname))
|
|
|
|
{
|
|
|
|
m->ctx->Close();
|
|
|
|
delete m;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return m;
|
2018-07-12 15:43:37 +02:00
|
|
|
}
|
2018-07-27 05:41:55 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
llarp_main_signal(struct llarp_main *ptr, int sig)
|
2018-05-27 20:03:10 +02:00
|
|
|
{
|
2018-08-09 13:22:57 +02:00
|
|
|
ptr->ctx->HandleSignal(sig);
|
2018-05-27 20:03:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
int
|
2019-05-28 02:19:25 +02:00
|
|
|
llarp_main_setup(struct llarp_main *ptr, bool debug)
|
2018-07-27 05:41:55 +02:00
|
|
|
{
|
2019-05-28 02:19:25 +02:00
|
|
|
return ptr->ctx->Setup(debug);
|
2018-07-27 05:41:55 +02:00
|
|
|
}
|
2018-06-23 16:56:59 +02:00
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
int
|
|
|
|
llarp_main_run(struct llarp_main *ptr)
|
|
|
|
{
|
2018-10-03 13:00:47 +02:00
|
|
|
if(!ptr)
|
|
|
|
{
|
|
|
|
llarp::LogError("No ptr passed in");
|
2018-11-26 23:46:22 +01:00
|
|
|
return 1;
|
2018-10-03 13:00:47 +02:00
|
|
|
}
|
2018-08-09 13:22:57 +02:00
|
|
|
return ptr->ctx->Run();
|
2018-07-27 05:41:55 +02:00
|
|
|
}
|
2018-05-27 20:03:10 +02:00
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
void
|
|
|
|
llarp_main_abort(struct llarp_main *ptr)
|
|
|
|
{
|
2019-01-29 03:16:31 +01:00
|
|
|
ptr->ctx->router->logic()->stop_timer();
|
2018-07-27 05:41:55 +02:00
|
|
|
}
|
2018-07-03 13:25:36 +02:00
|
|
|
|
2018-09-19 14:22:34 +02:00
|
|
|
void
|
|
|
|
llarp_main_queryDHT_RC(struct llarp_main *ptr,
|
|
|
|
struct llarp_router_lookup_job *job)
|
2018-07-27 05:41:55 +02:00
|
|
|
{
|
2019-01-29 03:16:31 +01:00
|
|
|
llarp_dht_lookup_router(ptr->ctx->router->dht(), job);
|
2018-07-27 05:41:55 +02:00
|
|
|
}
|
2018-06-19 11:44:53 +02:00
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
bool
|
2018-09-19 14:22:34 +02:00
|
|
|
llarp_main_init_dnsd(struct llarp_main *ptr, struct dnsd_context *dnsd,
|
2018-09-29 12:27:38 +02:00
|
|
|
const llarp::Addr &dnsd_sockaddr,
|
|
|
|
const llarp::Addr &dnsc_sockaddr)
|
2018-07-27 05:41:55 +02:00
|
|
|
{
|
2019-02-11 15:43:48 +01:00
|
|
|
return llarp_dnsd_init(dnsd, ptr->ctx->logic.get(),
|
|
|
|
ptr->ctx->mainloop.get(), dnsd_sockaddr,
|
|
|
|
dnsc_sockaddr);
|
2018-07-27 05:41:55 +02:00
|
|
|
}
|
2018-06-19 11:44:53 +02:00
|
|
|
|
2018-09-22 12:23:23 +02:00
|
|
|
bool
|
|
|
|
llarp_main_init_dotLokiLookup(struct llarp_main *ptr,
|
|
|
|
struct dotLokiLookup *dll)
|
|
|
|
{
|
2018-12-03 23:22:59 +01:00
|
|
|
(void)ptr;
|
|
|
|
(void)dll;
|
2019-02-11 15:43:48 +01:00
|
|
|
// TODO: gut me
|
2018-12-03 23:22:59 +01:00
|
|
|
return false;
|
2018-09-22 12:23:23 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
void
|
|
|
|
llarp_main_free(struct llarp_main *ptr)
|
|
|
|
{
|
2018-08-08 19:43:46 +02:00
|
|
|
delete ptr;
|
2018-07-27 05:41:55 +02:00
|
|
|
}
|
2018-06-29 14:15:15 +02:00
|
|
|
|
2018-09-22 12:23:23 +02:00
|
|
|
int
|
|
|
|
llarp_main_loadDatabase(struct llarp_main *ptr)
|
|
|
|
{
|
|
|
|
return ptr->ctx->LoadDatabase();
|
|
|
|
}
|
2018-09-19 14:22:34 +02:00
|
|
|
|
2018-07-27 05:41:55 +02:00
|
|
|
const char *
|
|
|
|
handleBaseCmdLineArgs(int argc, char *argv[])
|
2018-07-26 12:52:23 +02:00
|
|
|
{
|
2019-04-22 14:25:25 +02:00
|
|
|
// clang-format off
|
2019-04-19 20:24:33 +02:00
|
|
|
cxxopts::Options options(
|
|
|
|
"lokinet",
|
|
|
|
"Lokinet is a private, decentralized and IP based overlay network for the internet"
|
|
|
|
);
|
|
|
|
options.add_options()
|
|
|
|
("c,config", "Config file", cxxopts::value< std::string >()->default_value("daemon.ini"))
|
|
|
|
("o,logLevel", "logging level");
|
2019-04-22 14:25:25 +02:00
|
|
|
// clang-format on
|
2019-04-19 20:24:33 +02:00
|
|
|
|
2019-04-22 14:25:25 +02:00
|
|
|
auto result = options.parse(argc, argv);
|
2019-04-19 20:24:33 +02:00
|
|
|
std::string logLevel = result["logLevel"].as< std::string >();
|
|
|
|
|
|
|
|
if(logLevel == "debug")
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogDebug);
|
2018-07-26 12:52:23 +02:00
|
|
|
}
|
2019-04-19 20:24:33 +02:00
|
|
|
else if(logLevel == "info")
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogInfo);
|
|
|
|
}
|
|
|
|
else if(logLevel == "warn")
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogWarn);
|
|
|
|
}
|
|
|
|
else if(logLevel == "error")
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogError);
|
|
|
|
}
|
|
|
|
|
2019-04-22 14:25:25 +02:00
|
|
|
// this isn't thread safe, but reconfiguring during run is likely unsafe
|
|
|
|
// either way
|
|
|
|
static std::string confname = result["config"].as< std::string >();
|
2019-04-19 20:24:33 +02:00
|
|
|
|
2019-04-22 14:25:25 +02:00
|
|
|
return confname.c_str();
|
2018-07-26 12:52:23 +02:00
|
|
|
}
|
|
|
|
}
|