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-05-28 00:19:25 +00:00
|
|
|
#include <crypto/crypto_noop.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>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.h>
|
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>
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-07-26 01:00:15 +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
|
|
|
|
Context::CallSafe(std::function< void(void) > f)
|
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
return logic && LogicCall(logic, f);
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
bool
|
2018-12-24 16:09:05 +00:00
|
|
|
Context::Configure()
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
// llarp::LogInfo("loading config at ", configfile);
|
2019-10-04 18:10:58 +00:00
|
|
|
if(configfile.size())
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
if(!config->Load(configfile.c_str()))
|
|
|
|
{
|
|
|
|
config.release();
|
|
|
|
llarp::LogError("failed to load config file ", configfile);
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
2019-04-07 17:55:14 +00:00
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
// System config
|
|
|
|
if(!config->system.pidfile.empty())
|
2019-04-07 17:55:14 +00:00
|
|
|
{
|
2019-07-02 21:28:28 +00:00
|
|
|
SetPIDFile(config->system.pidfile);
|
2019-04-07 17:55:14 +00:00
|
|
|
}
|
2019-07-13 11:57:41 +00:00
|
|
|
auto threads = config->router.workerThreads();
|
2019-07-13 11:54:58 +00:00
|
|
|
if(threads <= 0)
|
|
|
|
threads = 1;
|
2019-07-15 16:56:09 +00:00
|
|
|
worker = std::make_shared< llarp::thread::ThreadPool >(threads, 1024,
|
|
|
|
"llarp-worker");
|
2019-11-25 21:30:34 +00:00
|
|
|
auto jobQueueSize = config->router.jobQueueSize();
|
|
|
|
if(jobQueueSize < 1024)
|
|
|
|
jobQueueSize = 1024;
|
|
|
|
logic = std::make_shared< Logic >(jobQueueSize);
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-07-08 23:29:37 +00:00
|
|
|
nodedb_dir = config->netdb.nodedbDir();
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
return true;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 15:45:18 +00:00
|
|
|
void
|
|
|
|
Context::SetPIDFile(const std::string &fname)
|
2019-01-18 13:24:33 +00:00
|
|
|
{
|
|
|
|
pidfile = fname;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-12-10 23:29:58 +00:00
|
|
|
if(!llarp_nodedb::ensure_dir(nodedb_dir.c_str()))
|
2018-06-13 11:40:49 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("nodedb_dir is incorrect");
|
2018-06-13 11:40:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-19 09:44:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-04 09:10:55 +00:00
|
|
|
Context::Setup()
|
2018-06-19 09:44:53 +00:00
|
|
|
{
|
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");
|
2019-02-11 14:43:48 +00:00
|
|
|
mainloop = llarp_make_ev_loop();
|
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);
|
|
|
|
|
2019-10-04 09:10:55 +00:00
|
|
|
crypto = std::make_unique< sodium::CryptoLibSodium >();
|
2019-05-28 19:45:08 +00:00
|
|
|
cryptoManager = std::make_unique< CryptoManager >(crypto.get());
|
|
|
|
|
2019-07-09 13:47:24 +00:00
|
|
|
router = std::make_unique< Router >(worker, mainloop, logic);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-01-14 20:12:47 +00:00
|
|
|
nodedb = std::make_unique< llarp_nodedb >(router->diskworker(), nodedb_dir);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
if(!router->Configure(config.get(), nodedb.get()))
|
2018-06-06 17:02:57 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to configure router");
|
2018-06-23 14:56:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
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
|
2018-12-28 15:34:41 +00:00
|
|
|
if(!this->LoadDatabase())
|
|
|
|
return 1;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2018-11-26 22:46:22 +00:00
|
|
|
return 0;
|
2018-06-23 14:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-04 18:10:58 +00:00
|
|
|
Context::Run(llarp_main_runtime_opts opts)
|
2018-06-23 14:56:59 +00:00
|
|
|
{
|
2018-12-02 18:07:07 +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-18 13:24:33 +00:00
|
|
|
if(!WritePIDFile())
|
|
|
|
return 1;
|
2018-11-26 22:46:22 +00:00
|
|
|
// run
|
2019-10-04 09:10:55 +00:00
|
|
|
if(!router->StartJsonRpc())
|
2018-12-02 18:07:07 +00:00
|
|
|
return 1;
|
2019-01-21 15:45:18 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
if(!opts.background)
|
2019-10-04 09:10:55 +00:00
|
|
|
{
|
|
|
|
if(!router->Run())
|
|
|
|
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");
|
2019-07-09 13:47:24 +00:00
|
|
|
llarp_ev_loop_run_single_process(mainloop, logic);
|
2019-10-09 13:08:38 +00:00
|
|
|
if(closeWaiter)
|
|
|
|
{
|
2019-10-09 13:21:41 +00:00
|
|
|
// inform promise if called by CloseAsync
|
2019-10-09 13:08:38 +00:00
|
|
|
closeWaiter->set_value();
|
|
|
|
}
|
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
|
|
|
|
if(closeWaiter)
|
|
|
|
return;
|
2019-10-09 13:21:41 +00:00
|
|
|
if(CallSafe(std::bind(&Context::HandleSignal, this, SIGTERM)))
|
2019-10-09 13:08:38 +00:00
|
|
|
closeWaiter = std::make_unique< std::promise< void > >();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Wait()
|
|
|
|
{
|
|
|
|
if(closeWaiter)
|
|
|
|
{
|
|
|
|
closeWaiter->get_future().wait();
|
|
|
|
closeWaiter.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:24:33 +00:00
|
|
|
bool
|
|
|
|
Context::WritePIDFile() const
|
|
|
|
{
|
|
|
|
if(pidfile.size())
|
|
|
|
{
|
|
|
|
std::ofstream f(pidfile);
|
|
|
|
f << std::to_string(getpid());
|
|
|
|
return f.good();
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return true;
|
2019-01-18 13:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 17:44:01 +00:00
|
|
|
void
|
|
|
|
Context::HandleSignal(int sig)
|
|
|
|
{
|
2018-11-29 13:12:28 +00:00
|
|
|
if(sig == SIGINT || sig == SIGTERM)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
|
|
|
SigINT();
|
|
|
|
}
|
2018-08-09 11:22:57 +00:00
|
|
|
// TODO(despair): implement hot-reloading config on NT
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifndef _WIN32
|
2018-05-27 17:44:01 +00:00
|
|
|
if(sig == SIGHUP)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("SIGHUP");
|
2018-12-24 16:09:05 +00:00
|
|
|
if(router)
|
|
|
|
{
|
2019-05-07 17:46:38 +00: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 18:15:22 +00:00
|
|
|
router->PumpLL();
|
2019-02-11 14:43:48 +00:00
|
|
|
Config newconfig;
|
2019-05-07 17:46:38 +00:00
|
|
|
if(!newconfig.Load(configfile.c_str()))
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to load config file ", configfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// validate config
|
2019-02-11 14:43:48 +00:00
|
|
|
if(!router->ValidateConfig(&newconfig))
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("new configuration is invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// reconfigure
|
2019-02-11 14:43:48 +00:00
|
|
|
if(!router->Reconfigure(&newconfig))
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Failed to reconfigure so we will stop.");
|
|
|
|
router->Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llarp::LogInfo("router reconfigured");
|
|
|
|
}
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::SigINT()
|
|
|
|
{
|
2018-12-24 16:09:05 +00:00
|
|
|
if(router)
|
|
|
|
{
|
|
|
|
/// async stop router on sigint
|
|
|
|
router->Stop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(logic)
|
|
|
|
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("stop workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
if(worker)
|
2019-07-09 13:47:24 +00:00
|
|
|
worker->stop();
|
2018-05-27 17:44:01 +00:00
|
|
|
|
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 workers");
|
2019-07-09 13:47:24 +00:00
|
|
|
worker.reset();
|
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
|
|
|
|
2019-01-18 13:24:33 +00:00
|
|
|
RemovePIDFile();
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::LoadConfig(const std::string &fname)
|
|
|
|
{
|
2019-02-11 14:43:48 +00:00
|
|
|
config = std::make_unique< Config >();
|
2018-05-27 17:44:01 +00:00
|
|
|
configfile = fname;
|
2018-12-24 16:09:05 +00:00
|
|
|
return Configure();
|
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
|
|
|
struct llarp_main
|
|
|
|
{
|
|
|
|
llarp_main(llarp_config *conf);
|
|
|
|
~llarp_main() = default;
|
|
|
|
std::unique_ptr< llarp::Context > ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct llarp_config
|
2018-05-27 18:03:10 +00:00
|
|
|
{
|
2019-10-08 14:52:01 +00:00
|
|
|
llarp::Config impl;
|
|
|
|
llarp_config() = default;
|
|
|
|
|
|
|
|
llarp_config(const llarp_config *other) : impl(other->impl)
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
};
|
2019-10-04 18:10:58 +00:00
|
|
|
|
2019-10-08 14:52:01 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
llarp_main_size()
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2019-10-08 14:52:01 +00:00
|
|
|
return sizeof(llarp_main);
|
|
|
|
}
|
2019-10-04 18:10:58 +00:00
|
|
|
|
2019-10-08 14:52:01 +00:00
|
|
|
size_t
|
|
|
|
llarp_config_size()
|
|
|
|
{
|
|
|
|
return sizeof(llarp_config);
|
|
|
|
}
|
2019-10-04 18:10:58 +00:00
|
|
|
|
|
|
|
struct llarp_config *
|
|
|
|
llarp_default_config()
|
|
|
|
{
|
2019-10-08 14:52:01 +00:00
|
|
|
llarp_config *conf = new llarp_config();
|
2019-10-04 18:10:58 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
// put andrid config overrides here
|
|
|
|
#endif
|
|
|
|
#ifdef IOS
|
|
|
|
// put IOS config overrides here
|
|
|
|
#endif
|
2019-10-08 14:52:01 +00:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_config_free(struct llarp_config *conf)
|
|
|
|
{
|
|
|
|
if(conf)
|
|
|
|
delete conf;
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct llarp_main *
|
|
|
|
llarp_main_init_from_config(struct llarp_config *conf)
|
|
|
|
{
|
|
|
|
if(conf == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return nullptr;
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp_main *m = new llarp_main(conf);
|
|
|
|
if(m->ctx->Configure())
|
|
|
|
return m;
|
|
|
|
delete m;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-09 13:08:38 +00:00
|
|
|
bool
|
|
|
|
llarp_config_read_file(struct llarp_config *conf, const char *fname)
|
|
|
|
{
|
|
|
|
if(conf == nullptr)
|
|
|
|
return false;
|
|
|
|
return conf->impl.Load(fname);
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
bool
|
|
|
|
llarp_config_load_file(const char *fname, struct llarp_config **conf)
|
|
|
|
{
|
|
|
|
llarp_config *c = new llarp_config();
|
|
|
|
if(c->impl.Load(fname))
|
2018-08-09 11:22:57 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
*conf = c;
|
|
|
|
return true;
|
2018-08-09 11:22:57 +00:00
|
|
|
}
|
2019-10-04 18:10:58 +00:00
|
|
|
delete c;
|
|
|
|
*conf = nullptr;
|
|
|
|
return false;
|
2018-07-12 13:43:37 +00:00
|
|
|
}
|
2018-07-27 03:41:55 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
llarp_main_signal(struct llarp_main *ptr, int sig)
|
2018-05-27 18:03:10 +00:00
|
|
|
{
|
2019-11-14 21:56:01 +00:00
|
|
|
LogicCall(ptr->ctx->logic,
|
|
|
|
std::bind(&llarp::Context::HandleSignal, ptr->ctx.get(), sig));
|
2018-05-27 18:03:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:41:55 +00:00
|
|
|
int
|
2019-10-04 09:10:55 +00:00
|
|
|
llarp_main_setup(struct llarp_main *ptr)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2019-10-04 09:10:55 +00:00
|
|
|
return ptr->ctx->Setup();
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-06-23 14:56:59 +00:00
|
|
|
|
2018-07-27 03:41:55 +00:00
|
|
|
int
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp_main_run(struct llarp_main *ptr, struct llarp_main_runtime_opts opts)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
return ptr->ctx->Run(opts);
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-05-27 18:03:10 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
const char *
|
|
|
|
llarp_version()
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2019-12-12 02:32:27 +00:00
|
|
|
return llarp::VERSION_FULL;
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-07-03 11:25:36 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
ssize_t
|
|
|
|
llarp_vpn_io_readpkt(struct llarp_vpn_pkt_reader *r, unsigned char *dst,
|
|
|
|
size_t dstlen)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
if(r == nullptr)
|
|
|
|
return -1;
|
|
|
|
if(not r->queue.enabled())
|
|
|
|
return -1;
|
|
|
|
auto pkt = r->queue.popFront();
|
|
|
|
ManagedBuffer mbuf = pkt.ConstBuffer();
|
|
|
|
const llarp_buffer_t &buf = mbuf;
|
|
|
|
if(buf.sz > dstlen || buf.sz == 0)
|
|
|
|
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
|
2019-10-04 18:10:58 +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
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
if(pktlen == 0 || pktbuf == nullptr)
|
|
|
|
return false;
|
|
|
|
if(w == nullptr)
|
|
|
|
return false;
|
|
|
|
llarp_vpn_pkt_queue::Packet_t pkt;
|
|
|
|
llarp_buffer_t buf(pktbuf, pktlen);
|
|
|
|
if(not pkt.Load(buf))
|
|
|
|
return false;
|
|
|
|
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
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp_main_inject_vpn_by_name(struct llarp_main *ptr, const char *name,
|
|
|
|
struct llarp_vpn_io *io,
|
|
|
|
struct llarp_vpn_ifaddr_info info)
|
2018-09-22 10:23:23 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
if(name == nullptr || io == nullptr)
|
|
|
|
return false;
|
|
|
|
if(ptr == nullptr || ptr->ctx == nullptr || ptr->ctx->router == nullptr)
|
|
|
|
return false;
|
|
|
|
auto ep = ptr->ctx->router->hiddenServiceContext().GetEndpointByName(name);
|
|
|
|
return ep && ep->InjectVPN(io, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_vpn_io_close_async(struct llarp_vpn_io *io)
|
|
|
|
{
|
|
|
|
if(io == nullptr || io->impl == nullptr)
|
|
|
|
return;
|
|
|
|
static_cast< llarp_vpn_io_impl * >(io->impl)->AsyncClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_vpn_io_init(struct llarp_main *ptr, struct llarp_vpn_io *io)
|
|
|
|
{
|
|
|
|
if(io == nullptr || ptr == nullptr)
|
|
|
|
return false;
|
|
|
|
llarp_vpn_io_impl *impl = new llarp_vpn_io_impl(ptr, io);
|
|
|
|
io->impl = impl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct llarp_vpn_pkt_writer *
|
|
|
|
llarp_vpn_io_packet_writer(struct llarp_vpn_io *io)
|
|
|
|
{
|
|
|
|
if(io == nullptr || io->impl == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
llarp_vpn_io_impl *vpn = static_cast< llarp_vpn_io_impl * >(io->impl);
|
|
|
|
return &vpn->writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct llarp_vpn_pkt_reader *
|
|
|
|
llarp_vpn_io_packet_reader(struct llarp_vpn_io *io)
|
|
|
|
{
|
|
|
|
if(io == nullptr || io->impl == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
llarp_vpn_io_impl *vpn = static_cast< llarp_vpn_io_impl * >(io->impl);
|
|
|
|
return &vpn->reader;
|
2018-09-22 10:23:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:41:55 +00:00
|
|
|
void
|
|
|
|
llarp_main_free(struct llarp_main *ptr)
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
delete ptr;
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-06-29 12:15:15 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
const char *
|
|
|
|
llarp_main_get_default_endpoint_name(struct llarp_main *)
|
2018-09-22 10:23:23 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
return "default";
|
2018-09-22 10:23:23 +00:00
|
|
|
}
|
2019-10-09 13:08:38 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
llarp_main_stop(struct llarp_main *ptr)
|
|
|
|
{
|
|
|
|
if(ptr == nullptr)
|
|
|
|
return;
|
2019-10-09 13:10:48 +00:00
|
|
|
ptr->ctx->CloseAsync();
|
2019-10-09 13:08:38 +00:00
|
|
|
ptr->ctx->Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf)
|
|
|
|
{
|
|
|
|
if(ptr == nullptr || conf == nullptr)
|
|
|
|
return false;
|
|
|
|
// give new config
|
|
|
|
ptr->ctx->config.reset(new llarp::Config(conf->impl));
|
|
|
|
return ptr->ctx->Configure();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_main_is_running(struct llarp_main *ptr)
|
|
|
|
{
|
|
|
|
return ptr && ptr->ctx->router && ptr->ctx->router->IsRunning();
|
|
|
|
}
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp_main::llarp_main(llarp_config *conf)
|
2019-04-19 18:24:33 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
: ctx(new llarp::Context())
|
|
|
|
{
|
|
|
|
ctx->config.reset(new llarp::Config(conf->impl));
|
|
|
|
}
|
2019-04-19 18:24:33 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
Context *
|
|
|
|
Context::Get(llarp_main *m)
|
|
|
|
{
|
|
|
|
if(m == nullptr || m->ctx == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return m->ctx.get();
|
2018-07-26 10:52:23 +00:00
|
|
|
}
|
2019-10-04 18:10:58 +00:00
|
|
|
} // namespace llarp
|