lokinet/llarp/context.cpp

516 lines
12 KiB
C++
Raw Normal View History

2018-07-27 03:41:55 +00:00
#include <getopt.h>
2018-05-27 18:03:10 +00:00
#include <llarp.h>
#include <llarp/logger.h>
#include <signal.h>
#ifndef _MSC_VER
2018-07-27 03:41:55 +00:00
#include <sys/param.h> // for MIN
#endif
#include <llarp.hpp>
#include "logger.hpp"
2018-06-28 11:40:46 +00:00
#include "math.h"
2018-06-29 12:15:15 +00:00
#include "router.hpp"
#include "dnsd.hpp"
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
#include <pthread_np.h>
#endif
// keep this once jeff reenables concurrency
#ifdef _MSC_VER
extern "C" void
SetThreadName(DWORD dwThreadID, LPCSTR szThreadName);
#endif
#if _WIN32 || __sun
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
#define MIN wmin
#endif
2018-07-30 22:35:54 +00:00
namespace llarp
{
Context::~Context()
{
}
void
Context::progress()
{
2018-07-24 06:33:22 +00:00
std::cout << "." << std::flush;
}
bool
Context::ReloadConfig()
{
// llarp::LogInfo("loading config at ", configfile);
if(llarp_load_config(config, configfile.c_str()))
{
llarp_free_config(&config);
llarp::LogError("failed to load config file ", configfile);
return false;
}
llarp_config_iterator iter;
iter.user = this;
iter.visit = &iter_config;
llarp_config_iter(config, &iter);
llarp::LogInfo("config [", configfile, "] loaded");
return true;
}
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)
{
int workers = atoi(val);
if(workers > 0 && ctx->worker == nullptr)
{
ctx->worker = llarp_init_threadpool(workers, "llarp-worker");
}
2018-06-29 12:15:15 +00:00
}
else if(!strcmp(key, "contact-file"))
{
strncpy(ctx->conatctFile, val, fmin(255, strlen(val)));
2018-06-29 12:15:15 +00:00
}
else 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;
}
}
if(!strcmp(section, "netdb"))
{
if(!strcmp(key, "dir"))
{
strncpy(ctx->nodedb_dir, val, sizeof(ctx->nodedb_dir));
}
}
}
int
Context::LoadDatabase()
{
llarp_crypto_libsodium_init(&crypto);
nodedb = llarp_nodedb_new(&crypto);
if(!nodedb_dir[0])
{
llarp::LogError("no nodedb_dir configured");
return 0;
}
nodedb_dir[sizeof(nodedb_dir) - 1] = 0;
if(!llarp_nodedb_ensure_dir(nodedb_dir))
{
llarp::LogError("nodedb_dir is incorrect");
return 0;
}
// llarp::LogInfo("nodedb_dir [", nodedb_dir, "] configured!");
ssize_t loaded = llarp_nodedb_load_dir(nodedb, nodedb_dir);
llarp::LogInfo("nodedb_dir loaded ", loaded, " RCs from [", nodedb_dir,
"]");
2018-06-17 15:26:00 +00:00
if(loaded < 0)
{
// shouldn't be possible
llarp::LogError("nodedb_dir directory doesn't exist");
return 0;
}
return 1;
}
int
Context::IterateDatabase(struct llarp_nodedb_iter i)
{
return llarp_nodedb_iterate_all(nodedb, i);
}
bool
Context::PutDatabase(struct llarp_rc *rc)
{
return llarp_nodedb_put_rc(nodedb, rc);
}
2018-06-21 11:11:55 +00:00
struct llarp_rc *
Context::GetDatabase(const byte_t *pk)
{
return llarp_nodedb_get_rc(nodedb, pk);
}
int
Context::Setup()
{
2018-07-24 06:37:12 +00:00
llarp::LogInfo(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO);
llarp::LogInfo("starting up");
this->LoadDatabase();
llarp_ev_loop_alloc(&mainloop);
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)
{
llarp::LogInfo("running in single threaded mode");
2018-06-06 17:02:57 +00:00
worker = llarp_init_same_process_threadpool();
}
// ensure netio thread
if(singleThreaded)
{
logic = llarp_init_single_process_logic(worker);
}
else
logic = llarp_init_logic();
2018-06-06 17:02:57 +00:00
router = llarp_init_router(worker, mainloop, logic);
if(!llarp_configure_router(router, config))
2018-06-06 17:02:57 +00:00
{
llarp::LogError("Failed to configure router");
return 1;
}
// set nodedb, load our RC, establish DHT
llarp_run_router(router, nodedb);
2018-06-29 12:15:15 +00:00
return 0; // success
}
int
Context::Run()
{
2018-06-29 12:15:15 +00:00
// just check to make sure it's not already set up (either this or we add a
// bool and/or add another function)
if(!this->router)
{
// set up all requirements
2018-06-29 12:15:15 +00:00
if(this->Setup())
2018-06-06 17:02:57 +00:00
{
llarp::LogError("Failed to setup router");
return 1;
2018-06-06 17:02:57 +00:00
}
}
// run net io thread
if(singleThreaded)
{
llarp::LogInfo("running mainloop");
llarp_ev_loop_run_single_process(mainloop, worker, logic);
}
else
{
llarp::LogInfo("running mainloop");
return llarp_ev_loop_run(mainloop, logic);
}
return 0;
}
void
Context::HandleSignal(int sig)
{
if(sig == SIGINT)
{
llarp::LogInfo("SIGINT");
SigINT();
}
// TODO(despair): implement hot-reloading config on NT
#ifndef _WIN32
if(sig == SIGHUP)
{
llarp::LogInfo("SIGHUP");
ReloadConfig();
}
#endif
}
void
Context::SigINT()
{
2018-05-28 20:51:15 +00:00
Close();
}
void
Context::Close()
{
llarp::LogDebug("stop router");
2018-05-28 20:51:15 +00:00
if(router)
llarp_stop_router(router);
llarp::LogDebug("stop workers");
if(worker)
llarp_threadpool_stop(worker);
llarp::LogDebug("join workers");
if(worker)
llarp_threadpool_join(worker);
llarp::LogDebug("stop logic");
2018-05-28 20:51:15 +00:00
if(logic)
llarp_logic_stop(logic);
llarp::LogDebug("free config");
llarp_free_config(&config);
llarp::LogDebug("free workers");
llarp_free_threadpool(&worker);
llarp::LogDebug("free nodedb");
2018-05-28 20:51:15 +00:00
llarp_nodedb_free(&nodedb);
2018-05-28 20:51:15 +00:00
for(size_t i = 0; i < netio_threads.size(); ++i)
{
if(mainloop)
{
llarp::LogDebug("stopping event loop thread ", i);
2018-05-28 20:51:15 +00:00
llarp_ev_loop_stop(mainloop);
}
}
llarp::LogDebug("free router");
2018-05-28 20:51:15 +00:00
llarp_free_router(&router);
llarp::LogDebug("free logic");
2018-05-28 20:51:15 +00:00
llarp_free_logic(&logic);
for(auto &t : netio_threads)
{
llarp::LogDebug("join netio thread");
t.join();
}
2018-05-28 20:51:15 +00:00
netio_threads.clear();
llarp::LogDebug("free mainloop");
2018-05-28 20:51:15 +00:00
llarp_ev_loop_free(&mainloop);
}
bool
Context::LoadConfig(const std::string &fname)
{
llarp_new_config(&config);
configfile = fname;
return ReloadConfig();
}
} // namespace llarp
2018-05-27 18:03:10 +00:00
struct llarp_main
{
std::unique_ptr< llarp::Context > ctx;
};
2018-07-27 03:41:55 +00:00
extern "C"
2018-05-27 18:03:10 +00:00
{
2018-07-27 03:41:55 +00:00
struct llarp_main *
llarp_main_init(const char *fname, bool multiProcess)
{
if(!fname)
fname = "daemon.ini";
char *var = getenv("LLARP_DEBUG");
if(var && *var == '1')
{
cSetLogLevel(eLogDebug);
}
llarp_main *m = new llarp_main;
m->ctx.reset(new llarp::Context());
m->ctx->singleThreaded = !multiProcess;
if(!m->ctx->LoadConfig(fname))
{
m->ctx->Close();
delete m;
return nullptr;
}
return m;
}
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
{
ptr->ctx->HandleSignal(sig);
2018-05-27 18:03:10 +00:00
}
2018-07-27 03:41:55 +00:00
int
llarp_main_setup(struct llarp_main *ptr)
{
return ptr->ctx->Setup();
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
int
llarp_main_run(struct llarp_main *ptr)
{
return ptr->ctx->Run();
2018-07-27 03:41:55 +00:00
}
2018-05-27 18:03:10 +00:00
2018-07-27 03:41:55 +00:00
void
llarp_main_abort(struct llarp_main *ptr)
{
llarp_logic_stop_timer(ptr->ctx->router->logic);
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
int
llarp_main_loadDatabase(struct llarp_main *ptr)
{
return ptr->ctx->LoadDatabase();
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
int
llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i)
{
return ptr->ctx->IterateDatabase(i);
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
bool
llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc)
{
return ptr->ctx->PutDatabase(rc);
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
struct llarp_rc *
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk)
{
return ptr->ctx->GetDatabase(pk);
2018-07-27 03:41:55 +00:00
}
2018-06-29 12:15:15 +00:00
2018-07-27 03:41:55 +00:00
struct llarp_rc *
llarp_main_getLocalRC(struct llarp_main *ptr)
{
//
/*
llarp_config_iterator iter;
iter.user = this;
iter.visit = &iter_config;
llarp_config_iter(ctx->config, &iter);
*/
llarp_rc *rc = new llarp_rc;
llarp_rc_new(rc);
llarp::LogInfo("Loading ", ptr->ctx->conatctFile);
if(llarp_rc_read(ptr->ctx->conatctFile, rc))
return rc;
else
return nullptr;
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
void
llarp_main_checkOnline(void *u, uint64_t orig, uint64_t left)
2018-06-29 12:15:15 +00:00
{
// llarp::LogInfo("checkOnline - check ", left);
if(left)
return;
struct check_online_request *request =
static_cast< struct check_online_request * >(u);
// llarp::LogDebug("checkOnline - running");
// llarp::LogInfo("checkOnline - DHT nodes ",
// request->ptr->ctx->router->dht->impl.nodes->nodes.size());
request->online = false;
request->nodes = request->ptr->ctx->router->dht->impl.nodes->nodes.size();
if(request->ptr->ctx->router->dht->impl.nodes->nodes.size())
{
// llarp::LogInfo("checkOnline - Going to say we're online");
request->online = true;
}
request->hook(request);
// reschedue our self
llarp_main_queryDHT(request);
}
2018-07-27 03:41:55 +00:00
void
llarp_main_queryDHT_online(struct check_online_request *request)
2018-06-29 12:15:15 +00:00
{
// Info("llarp_main_queryDHT_online: ", request->online ? "online" :
// "offline");
if(request->online && !request->first)
{
request->first = true;
llarp::LogInfo("llarp_main_queryDHT_online - We're online");
llarp::LogInfo("llarp_main_queryDHT_online - Querying DHT");
llarp_dht_lookup_router(request->ptr->ctx->router->dht, request->job);
}
}
2018-07-27 03:41:55 +00:00
void
llarp_main_queryDHT(struct check_online_request *request)
{
// llarp::LogInfo("llarp_main_queryDHT - setting up timer");
request->hook = &llarp_main_queryDHT_online;
llarp_logic_call_later(request->ptr->ctx->router->logic,
{1000, request, &llarp_main_checkOnline});
// llarp_dht_lookup_router(ptr->ctx->router->dht, job);
}
void
llarp_main_queryDHT_RC(struct llarp_main *ptr,
struct llarp_router_lookup_job *job)
{
llarp_dht_lookup_router(ptr->ctx->router->dht, job);
}
bool
llarp_main_init_dnsd(struct llarp_main *ptr, struct dnsd_context *dnsd,
uint16_t server_port, const char *upstream_host,
uint16_t upstream_port)
{
return llarp_dnsd_init(dnsd, ptr->ctx->mainloop, ptr->ctx->logic, "*",
server_port, upstream_host, upstream_port);
2018-07-27 03:41:55 +00:00
}
2018-07-27 03:41:55 +00:00
void
llarp_main_free(struct llarp_main *ptr)
{
delete ptr;
2018-07-27 03:41:55 +00:00
}
2018-07-26 10:52:23 +00:00
2018-07-27 03:41:55 +00:00
const char *
handleBaseCmdLineArgs(int argc, char *argv[])
2018-07-26 10:52:23 +00:00
{
const char *conffname = "daemon.ini";
int c;
while(1)
2018-07-26 10:52:23 +00:00
{
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
2018-07-27 03:41:55 +00:00
if(strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogDebug);
}
else if(strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogInfo);
}
else if(strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogWarn);
}
2018-07-27 03:41:55 +00:00
else if(strncmp(optarg, "error",
MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
break;
}
2018-07-26 10:52:23 +00:00
}
return conffname;
2018-07-26 10:52:23 +00:00
}
}