2019-01-11 01:12:43 +01:00
|
|
|
#include <llarp.hpp>
|
2019-10-04 20:10:58 +02:00
|
|
|
#include <constants/version.hpp>
|
2019-01-11 01:12:43 +01:00
|
|
|
|
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-01-11 01:12:43 +01:00
|
|
|
#include <dht/context.hpp>
|
2019-01-11 02:19:36 +01:00
|
|
|
#include <ev/ev.hpp>
|
2019-10-04 20:10:58 +02:00
|
|
|
#include <ev/vpnio.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-10-04 20:10:58 +02:00
|
|
|
#include <service/context.hpp>
|
2020-06-11 13:44:02 +02:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-01-11 01:12:43 +01:00
|
|
|
|
2019-04-19 20:24:33 +02:00
|
|
|
#include <cxxopts.hpp>
|
2019-07-31 01:42:13 +02:00
|
|
|
#include <csignal>
|
2020-06-29 21:55:59 +02:00
|
|
|
#include <stdexcept>
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2020-04-07 20:38:56 +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-10-04 20:10:58 +02:00
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
Context::CallSafe(std::function<void(void)> f)
|
2019-10-04 20:10:58 +02:00
|
|
|
{
|
2019-11-14 22:56:01 +01:00
|
|
|
return logic && LogicCall(logic, f);
|
2019-10-04 20:10:58 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:13:01 +02:00
|
|
|
void
|
|
|
|
Context::Configure(Config conf)
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
2020-07-06 21:13:01 +02:00
|
|
|
if (nullptr != config.get())
|
|
|
|
throw std::runtime_error("Config already exists");
|
2020-06-29 21:55:59 +02:00
|
|
|
|
2020-08-27 14:43:53 +02:00
|
|
|
config = std::make_shared<Config>(std::move(conf));
|
2019-04-07 19:55:14 +02:00
|
|
|
|
2020-06-01 15:17:44 +02:00
|
|
|
logic = std::make_shared<Logic>();
|
2019-07-02 23:28:28 +02:00
|
|
|
|
2020-05-20 20:27:19 +02:00
|
|
|
nodedb_dir = fs::path(config->router.m_dataDir / nodedb_dirname).string();
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 20:58:19 +01:00
|
|
|
bool
|
|
|
|
Context::IsUp() const
|
|
|
|
{
|
|
|
|
return router && router->IsRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::LooksAlive() const
|
|
|
|
{
|
|
|
|
return router && router->LooksAlive();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-04-02 22:11:07 +02:00
|
|
|
llarp_nodedb::ensure_dir(nodedb_dir.c_str());
|
2018-06-19 11:44:53 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:55:59 +02:00
|
|
|
void
|
2020-07-01 16:38:56 +02:00
|
|
|
Context::Setup(const RuntimeOptions& opts)
|
2018-06-19 11:44:53 +02:00
|
|
|
{
|
2020-07-06 21:13:01 +02:00
|
|
|
/// Call one of the Configure() methods before calling Setup()
|
|
|
|
if (not config)
|
|
|
|
throw std::runtime_error("Cannot call Setup() on context without a Config");
|
|
|
|
|
2019-12-12 03:32:27 +01:00
|
|
|
llarp::LogInfo(llarp::VERSION_FULL, " ", llarp::RELEASE_MOTTO);
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogInfo("starting up");
|
2020-04-07 20:38:56 +02:00
|
|
|
if (mainloop == nullptr)
|
2020-06-01 15:17:44 +02:00
|
|
|
{
|
2020-06-01 19:31:12 +02:00
|
|
|
auto jobQueueSize = std::max(event_loop_queue_size, config->router.m_JobQueueSize);
|
2020-06-01 15:17:44 +02:00
|
|
|
mainloop = llarp_make_ev_loop(jobQueueSize);
|
|
|
|
}
|
2019-11-23 05:47:08 +01:00
|
|
|
logic->set_event_loop(mainloop.get());
|
2018-05-27 19:44:01 +02:00
|
|
|
|
2019-12-10 16:43:27 +01:00
|
|
|
mainloop->set_logic(logic);
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
crypto = std::make_unique<sodium::CryptoLibSodium>();
|
|
|
|
cryptoManager = std::make_unique<CryptoManager>(crypto.get());
|
2019-05-28 21:45:08 +02:00
|
|
|
|
2020-07-02 18:35:44 +02:00
|
|
|
router = makeRouter(mainloop, logic);
|
2019-06-26 23:39:29 +02:00
|
|
|
|
2020-06-11 13:44:02 +02:00
|
|
|
nodedb = std::make_unique<llarp_nodedb>(
|
2020-06-30 18:02:29 +02:00
|
|
|
nodedb_dir, [r = router.get()](auto call) { r->QueueDiskIO(std::move(call)); });
|
2019-06-26 23:39:29 +02:00
|
|
|
|
2020-08-27 14:43:53 +02:00
|
|
|
if (!router->Configure(config, opts.isRouter, nodedb.get()))
|
2020-06-29 21:55:59 +02:00
|
|
|
throw std::runtime_error("Failed to configure router");
|
2019-06-26 23:39:29 +02:00
|
|
|
|
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
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!this->LoadDatabase())
|
2020-06-29 21:55:59 +02:00
|
|
|
throw std::runtime_error("Config::Setup() failed to load database");
|
2018-06-23 16:56:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:44:13 +02:00
|
|
|
std::unique_ptr<AbstractRouter>
|
2020-07-27 13:25:11 +02:00
|
|
|
Context::makeRouter(llarp_ev_loop_ptr netloop, std::shared_ptr<Logic> logic)
|
2020-06-29 17:44:13 +02:00
|
|
|
{
|
2020-07-02 18:35:44 +02:00
|
|
|
return std::make_unique<Router>(netloop, logic);
|
2020-06-29 17:44:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 16:56:59 +02:00
|
|
|
int
|
2020-06-29 21:55:59 +02:00
|
|
|
Context::Run(const RuntimeOptions& opts)
|
2018-06-23 16:56:59 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02: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-21 16:45:18 +01:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!opts.background)
|
2019-10-04 11:10:55 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!router->Run())
|
2019-10-04 11:10:55 +02:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:56:59 +02:00
|
|
|
// run net io thread
|
2018-09-21 14:30:57 +02:00
|
|
|
llarp::LogInfo("running mainloop");
|
2020-02-26 22:54:16 +01:00
|
|
|
|
|
|
|
llarp_ev_loop_run_single_process(mainloop, logic);
|
2020-04-07 20:38:56 +02:00
|
|
|
if (closeWaiter)
|
2019-10-09 15:08:38 +02:00
|
|
|
{
|
2020-02-26 22:54:16 +01:00
|
|
|
// inform promise if called by CloseAsync
|
|
|
|
closeWaiter->set_value();
|
2019-10-09 15:08:38 +02:00
|
|
|
}
|
2018-06-23 16:56:59 +02:00
|
|
|
return 0;
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:08:38 +02:00
|
|
|
void
|
2019-10-09 15:10:48 +02:00
|
|
|
Context::CloseAsync()
|
2019-10-09 15:08:38 +02:00
|
|
|
{
|
|
|
|
/// already closing
|
2020-04-07 20:38:56 +02:00
|
|
|
if (closeWaiter)
|
2019-10-09 15:08:38 +02:00
|
|
|
return;
|
2020-06-16 16:22:43 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (CallSafe(std::bind(&Context::HandleSignal, this, SIGTERM)))
|
|
|
|
closeWaiter = std::make_unique<std::promise<void>>();
|
2019-10-09 15:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Wait()
|
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (closeWaiter)
|
2019-10-09 15:08:38 +02:00
|
|
|
{
|
|
|
|
closeWaiter->get_future().wait();
|
|
|
|
closeWaiter.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:44:01 +02:00
|
|
|
void
|
|
|
|
Context::HandleSignal(int sig)
|
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (sig == SIGINT || sig == SIGTERM)
|
2018-05-27 19:44:01 +02:00
|
|
|
{
|
|
|
|
SigINT();
|
|
|
|
}
|
2020-08-27 14:43:53 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (sig == SIGHUP)
|
|
|
|
{
|
|
|
|
Reload();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Reload()
|
|
|
|
{
|
2018-05-27 19:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::SigINT()
|
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (router)
|
2018-12-24 17:09:05 +01:00
|
|
|
{
|
|
|
|
/// async stop router on sigint
|
|
|
|
router->Stop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (logic)
|
2018-12-24 17:09:05 +01:00
|
|
|
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("free config");
|
2020-08-27 14:43:53 +02:00
|
|
|
config.reset();
|
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
|
|
|
}
|
2018-07-05 17:44:06 +02:00
|
|
|
} // namespace llarp
|
2018-05-27 20:03:10 +02:00
|
|
|
|
2019-10-08 16:52:01 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
2019-10-04 20:10:58 +02:00
|
|
|
ssize_t
|
2020-04-07 20:38:56 +02:00
|
|
|
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader* r, unsigned char* dst, size_t dstlen)
|
2018-07-27 05:41:55 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (r == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return -1;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (not r->queue.enabled())
|
2019-10-04 20:10:58 +02:00
|
|
|
return -1;
|
2020-04-07 20:38:56 +02:00
|
|
|
auto pkt = r->queue.popFront();
|
|
|
|
ManagedBuffer mbuf = pkt.ConstBuffer();
|
|
|
|
const llarp_buffer_t& buf = mbuf;
|
|
|
|
if (buf.sz > dstlen || buf.sz == 0)
|
2019-10-04 20:10:58 +02:00
|
|
|
return -1;
|
|
|
|
std::copy_n(buf.base, buf.sz, dst);
|
|
|
|
return buf.sz;
|
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
|
2020-04-07 20:38:56 +02:00
|
|
|
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer* w, unsigned char* pktbuf, size_t pktlen)
|
2018-07-27 05:41:55 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (pktlen == 0 || pktbuf == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (w == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return false;
|
|
|
|
llarp_vpn_pkt_queue::Packet_t pkt;
|
|
|
|
llarp_buffer_t buf(pktbuf, pktlen);
|
2020-04-07 20:38:56 +02:00
|
|
|
if (not pkt.Load(buf))
|
2019-10-04 20:10:58 +02:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
return w->queue.pushBack(std::move(pkt)) == llarp::thread::QueueReturn::Success;
|
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
|
2020-04-07 20:38:56 +02:00
|
|
|
llarp_main_inject_vpn_by_name(
|
2020-06-29 22:51:13 +02:00
|
|
|
llarp::Context* ctx,
|
2020-04-07 20:38:56 +02:00
|
|
|
const char* name,
|
|
|
|
struct llarp_vpn_io* io,
|
|
|
|
struct llarp_vpn_ifaddr_info info)
|
2018-09-22 12:23:23 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (name == nullptr || io == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return false;
|
2020-06-29 22:51:13 +02:00
|
|
|
if (ctx == nullptr || ctx->router == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return false;
|
2020-06-29 22:51:13 +02:00
|
|
|
auto ep = ctx->router->hiddenServiceContext().GetEndpointByName(name);
|
2019-10-04 20:10:58 +02:00
|
|
|
return ep && ep->InjectVPN(io, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 20:38:56 +02:00
|
|
|
llarp_vpn_io_close_async(struct llarp_vpn_io* io)
|
2019-10-04 20:10:58 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (io == nullptr || io->impl == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return;
|
2020-04-07 20:38:56 +02:00
|
|
|
static_cast<llarp_vpn_io_impl*>(io->impl)->AsyncClose();
|
2019-10-04 20:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-06-29 22:39:31 +02:00
|
|
|
llarp_vpn_io_init(llarp::Context* ctx, struct llarp_vpn_io* io)
|
2019-10-04 20:10:58 +02:00
|
|
|
{
|
2020-06-29 22:39:31 +02:00
|
|
|
if (io == nullptr || ctx == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return false;
|
2020-06-29 22:39:31 +02:00
|
|
|
llarp_vpn_io_impl* impl = new llarp_vpn_io_impl(ctx, io);
|
2020-04-07 20:38:56 +02:00
|
|
|
io->impl = impl;
|
2019-10-04 20:10:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
struct llarp_vpn_pkt_writer*
|
|
|
|
llarp_vpn_io_packet_writer(struct llarp_vpn_io* io)
|
2019-10-04 20:10:58 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (io == nullptr || io->impl == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return nullptr;
|
2020-04-07 20:38:56 +02:00
|
|
|
llarp_vpn_io_impl* vpn = static_cast<llarp_vpn_io_impl*>(io->impl);
|
2019-10-04 20:10:58 +02:00
|
|
|
return &vpn->writer;
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
struct llarp_vpn_pkt_reader*
|
|
|
|
llarp_vpn_io_packet_reader(struct llarp_vpn_io* io)
|
2019-10-04 20:10:58 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (io == nullptr || io->impl == nullptr)
|
2019-10-04 20:10:58 +02:00
|
|
|
return nullptr;
|
2020-04-07 20:38:56 +02:00
|
|
|
llarp_vpn_io_impl* vpn = static_cast<llarp_vpn_io_impl*>(io->impl);
|
2019-10-04 20:10:58 +02:00
|
|
|
return &vpn->reader;
|
2018-09-22 12:23:23 +02:00
|
|
|
}
|
2019-10-04 20:10:58 +02:00
|
|
|
}
|