2019-01-11 00:12:43 +00:00
|
|
|
#include <llarp.hpp>
|
2019-10-04 18:10:58 +00:00
|
|
|
#include <constants/version.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
#include <config/config.hpp>
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
#include <dht/context.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.hpp>
|
2019-10-04 18:10:58 +00:00
|
|
|
#include <ev/vpnio.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
#include <nodedb.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <router/router.hpp>
|
2019-10-04 18:10:58 +00:00
|
|
|
#include <service/context.hpp>
|
2020-06-11 11:44:02 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
|
2019-04-19 18:24:33 +00:00
|
|
|
#include <cxxopts.hpp>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <csignal>
|
2020-06-29 19:55:59 +00:00
|
|
|
#include <stdexcept>
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
#if (__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
|
2018-05-29 12:13:33 +00:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Context::CallSafe(std::function<void(void)> f)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
return logic && LogicCall(logic, f);
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 19:13:01 +00:00
|
|
|
void
|
|
|
|
Context::Configure(Config conf)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2020-07-06 19:13:01 +00:00
|
|
|
if (nullptr != config.get())
|
|
|
|
throw std::runtime_error("Config already exists");
|
2020-06-29 19:55:59 +00:00
|
|
|
|
2020-07-06 19:13:01 +00:00
|
|
|
config = std::make_unique<Config>(std::move(conf));
|
2019-04-07 17:55:14 +00:00
|
|
|
|
2020-06-01 13:17:44 +00:00
|
|
|
logic = std::make_shared<Logic>();
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-05-20 18:27:19 +00:00
|
|
|
nodedb_dir = fs::path(config->router.m_dataDir / nodedb_dirname).string();
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 19:58:19 +00:00
|
|
|
bool
|
|
|
|
Context::IsUp() const
|
|
|
|
{
|
|
|
|
return router && router->IsRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::LooksAlive() const
|
|
|
|
{
|
|
|
|
return router && router->LooksAlive();
|
|
|
|
}
|
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
int
|
2018-06-19 09:44:53 +00:00
|
|
|
Context::LoadDatabase()
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2020-04-02 20:11:07 +00:00
|
|
|
llarp_nodedb::ensure_dir(nodedb_dir.c_str());
|
2018-06-19 09:44:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:55:59 +00:00
|
|
|
void
|
2020-07-01 14:38:56 +00:00
|
|
|
Context::Setup(const RuntimeOptions& opts)
|
2018-06-19 09:44:53 +00:00
|
|
|
{
|
2020-07-06 19:13:01 +00: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 02:32:27 +00:00
|
|
|
llarp::LogInfo(llarp::VERSION_FULL, " ", llarp::RELEASE_MOTTO);
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("starting up");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (mainloop == nullptr)
|
2020-06-01 13:17:44 +00:00
|
|
|
{
|
2020-06-01 17:31:12 +00:00
|
|
|
auto jobQueueSize = std::max(event_loop_queue_size, config->router.m_JobQueueSize);
|
2020-06-01 13:17:44 +00:00
|
|
|
mainloop = llarp_make_ev_loop(jobQueueSize);
|
|
|
|
}
|
2019-11-23 04:47:08 +00:00
|
|
|
logic->set_event_loop(mainloop.get());
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2019-12-10 15:43:27 +00:00
|
|
|
mainloop->set_logic(logic);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
crypto = std::make_unique<sodium::CryptoLibSodium>();
|
|
|
|
cryptoManager = std::make_unique<CryptoManager>(crypto.get());
|
2019-05-28 19:45:08 +00:00
|
|
|
|
2020-07-02 16:35:44 +00:00
|
|
|
router = makeRouter(mainloop, logic);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-06-11 11:44:02 +00:00
|
|
|
nodedb = std::make_unique<llarp_nodedb>(
|
2020-06-30 16:02:29 +00:00
|
|
|
nodedb_dir, [r = router.get()](auto call) { r->QueueDiskIO(std::move(call)); });
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-07-06 19:13:01 +00:00
|
|
|
if (!router->Configure(*config.get(), opts.isRouter, nodedb.get()))
|
2020-06-29 19:55:59 +00:00
|
|
|
throw std::runtime_error("Failed to configure router");
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2018-12-28 15:34:41 +00:00
|
|
|
// must be done after router is made so we can use its disk io worker
|
2019-04-14 16:08:51 +00:00
|
|
|
// must also be done after configure so that netid is properly set if it
|
|
|
|
// is provided by config
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!this->LoadDatabase())
|
2020-06-29 19:55:59 +00:00
|
|
|
throw std::runtime_error("Config::Setup() failed to load database");
|
2018-06-23 14:56:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:44:13 +00:00
|
|
|
std::unique_ptr<AbstractRouter>
|
2020-07-27 11:25:11 +00:00
|
|
|
Context::makeRouter(llarp_ev_loop_ptr netloop, std::shared_ptr<Logic> logic)
|
2020-06-29 15:44:13 +00:00
|
|
|
{
|
2020-07-02 16:35:44 +00:00
|
|
|
return std::make_unique<Router>(netloop, logic);
|
2020-06-29 15:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 14:56:59 +00:00
|
|
|
int
|
2020-06-29 19:55:59 +00:00
|
|
|
Context::Run(const RuntimeOptions& opts)
|
2018-06-23 14:56:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (router == nullptr)
|
2018-06-23 14:56:59 +00:00
|
|
|
{
|
2018-12-02 18:07:07 +00:00
|
|
|
// we are not set up so we should die
|
|
|
|
llarp::LogError("cannot run non configured context");
|
|
|
|
return 1;
|
2018-06-23 14:56:59 +00:00
|
|
|
}
|
2019-01-21 15:45:18 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!opts.background)
|
2019-10-04 09:10:55 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!router->Run())
|
2019-10-04 09:10:55 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-06-23 14:56:59 +00:00
|
|
|
// run net io thread
|
2018-09-21 12:30:57 +00:00
|
|
|
llarp::LogInfo("running mainloop");
|
2020-02-26 21:54:16 +00:00
|
|
|
|
|
|
|
llarp_ev_loop_run_single_process(mainloop, logic);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closeWaiter)
|
2019-10-09 13:08:38 +00:00
|
|
|
{
|
2020-02-26 21:54:16 +00:00
|
|
|
// inform promise if called by CloseAsync
|
|
|
|
closeWaiter->set_value();
|
2019-10-09 13:08:38 +00:00
|
|
|
}
|
2018-06-23 14:56:59 +00:00
|
|
|
return 0;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 13:08:38 +00:00
|
|
|
void
|
2019-10-09 13:10:48 +00:00
|
|
|
Context::CloseAsync()
|
2019-10-09 13:08:38 +00:00
|
|
|
{
|
|
|
|
/// already closing
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closeWaiter)
|
2019-10-09 13:08:38 +00:00
|
|
|
return;
|
2020-06-16 14:22:43 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (CallSafe(std::bind(&Context::HandleSignal, this, SIGTERM)))
|
|
|
|
closeWaiter = std::make_unique<std::promise<void>>();
|
2019-10-09 13:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Wait()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closeWaiter)
|
2019-10-09 13:08:38 +00:00
|
|
|
{
|
|
|
|
closeWaiter->get_future().wait();
|
|
|
|
closeWaiter.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
void
|
|
|
|
Context::HandleSignal(int sig)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sig == SIGINT || sig == SIGTERM)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
|
|
|
SigINT();
|
|
|
|
}
|
2020-03-27 00:57:13 +00:00
|
|
|
// TODO: Hot reloading would be kewl
|
|
|
|
// (it used to exist here, but wasn't maintained)
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::SigINT()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (router)
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
/// async stop router on sigint
|
|
|
|
router->Stop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (logic)
|
2018-12-24 16:09:05 +00:00
|
|
|
logic->stop();
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp_ev_loop_stop(mainloop);
|
2018-12-24 16:09:05 +00:00
|
|
|
Close();
|
|
|
|
}
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Close()
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free config");
|
2019-02-11 14:43:48 +00:00
|
|
|
config.release();
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free nodedb");
|
2019-02-11 14:43:48 +00:00
|
|
|
nodedb.release();
|
|
|
|
|
|
|
|
llarp::LogDebug("free router");
|
|
|
|
router.release();
|
|
|
|
|
|
|
|
llarp::LogDebug("free logic");
|
2019-05-22 16:20:03 +00:00
|
|
|
logic.reset();
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
2018-07-05 15:44:06 +00:00
|
|
|
} // namespace llarp
|
2018-05-27 18:03:10 +00:00
|
|
|
|
2019-10-08 14:52:01 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
ssize_t
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader* r, unsigned char* dst, size_t dstlen)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (r == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return -1;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not r->queue.enabled())
|
2019-10-04 18:10:58 +00:00
|
|
|
return -1;
|
2020-04-07 18:38:56 +00: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 18:10:58 +00:00
|
|
|
return -1;
|
|
|
|
std::copy_n(buf.base, buf.sz, dst);
|
|
|
|
return buf.sz;
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-06-19 09:44:53 +00:00
|
|
|
|
2018-07-27 03:41:55 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_vpn_io_writepkt(struct llarp_vpn_pkt_writer* w, unsigned char* pktbuf, size_t pktlen)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pktlen == 0 || pktbuf == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (w == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return false;
|
|
|
|
llarp_vpn_pkt_queue::Packet_t pkt;
|
|
|
|
llarp_buffer_t buf(pktbuf, pktlen);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not pkt.Load(buf))
|
2019-10-04 18:10:58 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
return w->queue.pushBack(std::move(pkt)) == llarp::thread::QueueReturn::Success;
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-06-19 09:44:53 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main_inject_vpn_by_name(
|
2020-06-29 20:51:13 +00:00
|
|
|
llarp::Context* ctx,
|
2020-04-07 18:38:56 +00:00
|
|
|
const char* name,
|
|
|
|
struct llarp_vpn_io* io,
|
|
|
|
struct llarp_vpn_ifaddr_info info)
|
2018-09-22 10:23:23 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (name == nullptr || io == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return false;
|
2020-06-29 20:51:13 +00:00
|
|
|
if (ctx == nullptr || ctx->router == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return false;
|
2020-06-29 20:51:13 +00:00
|
|
|
auto ep = ctx->router->hiddenServiceContext().GetEndpointByName(name);
|
2019-10-04 18:10:58 +00:00
|
|
|
return ep && ep->InjectVPN(io, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_vpn_io_close_async(struct llarp_vpn_io* io)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (io == nullptr || io->impl == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return;
|
2020-04-07 18:38:56 +00:00
|
|
|
static_cast<llarp_vpn_io_impl*>(io->impl)->AsyncClose();
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-06-29 20:39:31 +00:00
|
|
|
llarp_vpn_io_init(llarp::Context* ctx, struct llarp_vpn_io* io)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2020-06-29 20:39:31 +00:00
|
|
|
if (io == nullptr || ctx == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return false;
|
2020-06-29 20:39:31 +00:00
|
|
|
llarp_vpn_io_impl* impl = new llarp_vpn_io_impl(ctx, io);
|
2020-04-07 18:38:56 +00:00
|
|
|
io->impl = impl;
|
2019-10-04 18:10:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_vpn_pkt_writer*
|
|
|
|
llarp_vpn_io_packet_writer(struct llarp_vpn_io* io)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (io == nullptr || io->impl == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return nullptr;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_vpn_io_impl* vpn = static_cast<llarp_vpn_io_impl*>(io->impl);
|
2019-10-04 18:10:58 +00:00
|
|
|
return &vpn->writer;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
struct llarp_vpn_pkt_reader*
|
|
|
|
llarp_vpn_io_packet_reader(struct llarp_vpn_io* io)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (io == nullptr || io->impl == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return nullptr;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_vpn_io_impl* vpn = static_cast<llarp_vpn_io_impl*>(io->impl);
|
2019-10-04 18:10:58 +00:00
|
|
|
return &vpn->reader;
|
2018-09-22 10:23:23 +00:00
|
|
|
}
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|