2018-07-27 03:41:55 +00:00
|
|
|
#include <getopt.h>
|
2018-05-27 18:03:10 +00:00
|
|
|
#include <llarp.h>
|
2018-07-12 13:43:37 +00:00
|
|
|
#include <llarp/logger.h>
|
2018-05-27 17:44:01 +00:00
|
|
|
#include <signal.h>
|
2018-07-27 03:41:55 +00:00
|
|
|
#include <sys/param.h> // for MIN
|
2018-05-27 17:44:01 +00:00
|
|
|
#include <llarp.hpp>
|
2018-06-29 12:15:15 +00:00
|
|
|
#include "router.hpp"
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-09-24 13:10:36 +00:00
|
|
|
#include <llarp/dnsd.hpp>
|
|
|
|
#include <llarp/dns_dotlokilookup.hpp>
|
2018-08-09 11:22:57 +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
|
|
|
|
{
|
|
|
|
Context::~Context()
|
|
|
|
{
|
2018-09-21 12:30:57 +00:00
|
|
|
llarp_ev_loop_free(&mainloop);
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::progress()
|
|
|
|
{
|
2018-07-24 06:33:22 +00:00
|
|
|
std::cout << "." << std::flush;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::ReloadConfig()
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
// llarp::LogInfo("loading config at ", configfile);
|
2018-06-19 09:44:53 +00:00
|
|
|
if(llarp_load_config(config, configfile.c_str()))
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2018-06-19 09:44:53 +00:00
|
|
|
llarp_free_config(&config);
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("failed to load config file ", configfile);
|
2018-06-19 09:44:53 +00:00
|
|
|
return false;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
2018-06-19 09:44:53 +00:00
|
|
|
llarp_config_iterator iter;
|
|
|
|
iter.user = this;
|
|
|
|
iter.visit = &iter_config;
|
|
|
|
llarp_config_iter(config, &iter);
|
2018-09-17 11:47:34 +00:00
|
|
|
return router->ReloadConfig(config);
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
|
|
|
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, "net-threads"))
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
|
|
|
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;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strcmp(section, "netdb"))
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "dir"))
|
|
|
|
{
|
2018-09-17 12:02:09 +00:00
|
|
|
ctx->nodedb_dir = val;
|
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-09-02 18:25:42 +00:00
|
|
|
llarp_crypto_init(&crypto);
|
2018-05-28 13:49:44 +00:00
|
|
|
nodedb = llarp_nodedb_new(&crypto);
|
2018-06-13 11:40:49 +00:00
|
|
|
|
2018-09-17 12:02:09 +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-07-05 15:44:06 +00:00
|
|
|
// llarp::LogInfo("nodedb_dir [", nodedb_dir, "] configured!");
|
2018-09-17 12:02:09 +00:00
|
|
|
ssize_t loaded = llarp_nodedb_load_dir(nodedb, nodedb_dir.c_str());
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("nodedb_dir loaded ", loaded, " RCs from [", nodedb_dir,
|
|
|
|
"]");
|
2018-06-17 15:26:00 +00:00
|
|
|
if(loaded < 0)
|
2018-06-13 11:40:49 +00:00
|
|
|
{
|
|
|
|
// shouldn't be possible
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("nodedb_dir directory doesn't exist");
|
2018-06-13 11:40:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-19 09:44:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Context::IterateDatabase(struct llarp_nodedb_iter i)
|
|
|
|
{
|
|
|
|
return llarp_nodedb_iterate_all(nodedb, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-07 15:30:22 +00:00
|
|
|
Context::PutDatabase(__attribute__((unused)) struct llarp::RouterContact &rc)
|
2018-06-19 09:44:53 +00:00
|
|
|
{
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME
|
2018-09-22 10:23:23 +00:00
|
|
|
// return llarp_nodedb_put_rc(nodedb, rc);
|
2018-09-19 12:22:34 +00:00
|
|
|
return false;
|
2018-06-19 09:44:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 12:22:34 +00:00
|
|
|
llarp::RouterContact *
|
2018-11-07 15:30:22 +00:00
|
|
|
Context::GetDatabase(__attribute__((unused)) const byte_t *pk)
|
2018-06-21 11:11:55 +00:00
|
|
|
{
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME
|
2018-09-22 10:23:23 +00:00
|
|
|
// return llarp_nodedb_get_rc(nodedb, pk);
|
2018-09-19 12:22:34 +00:00
|
|
|
return nullptr;
|
2018-06-21 11:11:55 +00:00
|
|
|
}
|
2018-06-19 09:44:53 +00:00
|
|
|
|
|
|
|
int
|
2018-06-23 14:56:59 +00:00
|
|
|
Context::Setup()
|
2018-06-19 09:44:53 +00:00
|
|
|
{
|
2018-07-24 06:37:12 +00:00
|
|
|
llarp::LogInfo(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO);
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("starting up");
|
2018-09-17 12:02:09 +00:00
|
|
|
if(!this->LoadDatabase())
|
|
|
|
return -1;
|
2018-06-19 09:44:53 +00:00
|
|
|
llarp_ev_loop_alloc(&mainloop);
|
2018-06-13 11:40:49 +00:00
|
|
|
|
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)
|
2018-05-27 17:44:01 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
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-05-27 17:44:01 +00:00
|
|
|
|
2018-06-06 17:02:57 +00:00
|
|
|
router = llarp_init_router(worker, mainloop, logic);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-06-23 14:56:59 +00:00
|
|
|
if(!llarp_configure_router(router, config))
|
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;
|
|
|
|
}
|
|
|
|
// set nodedb, load our RC, establish DHT
|
|
|
|
llarp_run_router(router, nodedb);
|
|
|
|
|
2018-06-29 12:15:15 +00:00
|
|
|
return 0; // success
|
2018-06-23 14:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-06-23 14:56:59 +00:00
|
|
|
{
|
|
|
|
// set up all requirements
|
2018-06-29 12:15:15 +00:00
|
|
|
if(this->Setup())
|
2018-06-06 17:02:57 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to setup router");
|
2018-06-23 14:56:59 +00:00
|
|
|
return 1;
|
2018-06-06 17:02:57 +00:00
|
|
|
}
|
2018-06-23 14:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run net io thread
|
2018-09-21 12:30:57 +00:00
|
|
|
llarp::LogInfo("running mainloop");
|
|
|
|
llarp_ev_loop_run_single_process(mainloop, worker, logic);
|
2018-06-23 14:56:59 +00:00
|
|
|
return 0;
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::HandleSignal(int sig)
|
|
|
|
{
|
|
|
|
if(sig == SIGINT)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("SIGINT");
|
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-05-27 17:44:01 +00:00
|
|
|
ReloadConfig();
|
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
2018-05-27 17:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::SigINT()
|
|
|
|
{
|
2018-05-28 20:51:15 +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 router");
|
2018-05-28 20:51:15 +00:00
|
|
|
if(router)
|
|
|
|
llarp_stop_router(router);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("stop workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
if(worker)
|
|
|
|
llarp_threadpool_stop(worker);
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("join workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
if(worker)
|
|
|
|
llarp_threadpool_join(worker);
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("stop logic");
|
2018-05-28 20:51:15 +00:00
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
if(logic)
|
|
|
|
llarp_logic_stop(logic);
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free config");
|
2018-05-27 17:44:01 +00:00
|
|
|
llarp_free_config(&config);
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free workers");
|
2018-05-27 17:44:01 +00:00
|
|
|
llarp_free_threadpool(&worker);
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free nodedb");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_nodedb_free(&nodedb);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-09-21 12:30:57 +00:00
|
|
|
llarp::LogDebug("stopping event loop");
|
|
|
|
llarp_ev_loop_stop(mainloop);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free router");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_free_router(&router);
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("free logic");
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_free_logic(&logic);
|
2018-05-27 17:44:01 +00:00
|
|
|
|
|
|
|
for(auto &t : netio_threads)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("join netio thread");
|
2018-05-27 17:44:01 +00:00
|
|
|
t.join();
|
|
|
|
}
|
2018-05-28 20:51:15 +00:00
|
|
|
|
2018-05-27 17:44:01 +00:00
|
|
|
netio_threads.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::LoadConfig(const std::string &fname)
|
|
|
|
{
|
|
|
|
llarp_new_config(&config);
|
|
|
|
configfile = fname;
|
|
|
|
return ReloadConfig();
|
|
|
|
}
|
2018-07-05 15:44:06 +00:00
|
|
|
} // 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)
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2018-08-09 11:22:57 +00:00
|
|
|
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-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
|
|
|
{
|
2018-08-09 11:22:57 +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)
|
|
|
|
{
|
2018-08-09 11:22:57 +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
|
|
|
|
llarp_main_run(struct llarp_main *ptr)
|
|
|
|
{
|
2018-10-03 11:00:47 +00:00
|
|
|
if(!ptr)
|
|
|
|
{
|
|
|
|
llarp::LogError("No ptr passed in");
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-09 11:22:57 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-08-09 11:22:57 +00:00
|
|
|
llarp_logic_stop_timer(ptr->ctx->router->logic);
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-07-03 11:25:36 +00:00
|
|
|
|
2018-09-19 12:22:34 +00:00
|
|
|
void
|
|
|
|
llarp_main_queryDHT_RC(struct llarp_main *ptr,
|
|
|
|
struct llarp_router_lookup_job *job)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2018-09-19 12:22:34 +00:00
|
|
|
llarp_dht_lookup_router(ptr->ctx->router->dht, job);
|
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
|
2018-09-19 12:22:34 +00:00
|
|
|
llarp_main_init_dnsd(struct llarp_main *ptr, struct dnsd_context *dnsd,
|
2018-09-29 10:27:38 +00:00
|
|
|
const llarp::Addr &dnsd_sockaddr,
|
|
|
|
const llarp::Addr &dnsc_sockaddr)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2018-09-29 10:27:38 +00:00
|
|
|
return llarp_dnsd_init(dnsd, ptr->ctx->logic, ptr->ctx->mainloop,
|
|
|
|
dnsd_sockaddr, dnsc_sockaddr);
|
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
|
|
|
|
llarp_main_init_dotLokiLookup(struct llarp_main *ptr,
|
|
|
|
struct dotLokiLookup *dll)
|
|
|
|
{
|
|
|
|
dll->logic = ptr->ctx->logic;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
int
|
|
|
|
llarp_main_loadDatabase(struct llarp_main *ptr)
|
|
|
|
{
|
|
|
|
return ptr->ctx->LoadDatabase();
|
|
|
|
}
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
int
|
|
|
|
llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i)
|
|
|
|
{
|
|
|
|
return ptr->ctx->IterateDatabase(i);
|
|
|
|
}
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
bool
|
2018-09-24 13:10:36 +00:00
|
|
|
llarp_main_putDatabase(struct llarp_main *ptr, llarp::RouterContact &rc)
|
2018-09-22 10:23:23 +00:00
|
|
|
{
|
|
|
|
return ptr->ctx->PutDatabase(rc);
|
|
|
|
}
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
llarp::RouterContact *
|
|
|
|
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk)
|
|
|
|
{
|
|
|
|
return ptr->ctx->GetDatabase(pk);
|
|
|
|
}
|
2018-09-19 12:22:34 +00:00
|
|
|
|
|
|
|
llarp::RouterContact *
|
2018-11-07 15:30:22 +00:00
|
|
|
llarp_main_getLocalRC(__attribute__((unused)) struct llarp_main *ptr)
|
2018-07-27 03:41:55 +00:00
|
|
|
{
|
2018-08-09 11:22:57 +00:00
|
|
|
//
|
|
|
|
/*
|
|
|
|
llarp_config_iterator iter;
|
|
|
|
iter.user = this;
|
|
|
|
iter.visit = &iter_config;
|
|
|
|
llarp_config_iter(ctx->config, &iter);
|
|
|
|
*/
|
2018-09-22 10:23:23 +00:00
|
|
|
// llarp_rc *rc = new llarp_rc;
|
2018-09-29 10:27:38 +00:00
|
|
|
// llarp::RouterContact *rc = new llarp::RouterContact;
|
2018-09-22 10:23:23 +00:00
|
|
|
// llarp_rc_new(rc);
|
|
|
|
// llarp::LogInfo("FIXME: Loading ", ptr->ctx->conatctFile);
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME
|
|
|
|
/*
|
2018-08-09 11:22:57 +00:00
|
|
|
if(llarp_rc_read(ptr->ctx->conatctFile, rc))
|
|
|
|
return rc;
|
|
|
|
else
|
2018-09-19 12:22:34 +00:00
|
|
|
*/
|
2018-09-22 10:23:23 +00:00
|
|
|
return nullptr;
|
2018-07-27 03:41:55 +00:00
|
|
|
}
|
2018-06-28 11:32:26 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
void
|
2018-11-07 15:30:22 +00:00
|
|
|
llarp_main_checkOnline(void *u, __attribute__((unused)) uint64_t orig,
|
|
|
|
uint64_t left)
|
2018-09-22 10:23:23 +00:00
|
|
|
{
|
|
|
|
// llarp::Info("checkOnline - check ", left);
|
|
|
|
if(left)
|
|
|
|
return;
|
|
|
|
struct check_online_request *request =
|
|
|
|
static_cast< struct check_online_request * >(u);
|
|
|
|
// llarp::Debug("checkOnline - running");
|
|
|
|
// llarp::Info("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::Info("checkOnline - Going to say we're online");
|
|
|
|
request->online = true;
|
|
|
|
}
|
|
|
|
request->hook(request);
|
|
|
|
// reschedue our self
|
|
|
|
llarp_main_queryDHT(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_main_queryDHT_online(struct check_online_request *request)
|
2018-08-09 11:22:57 +00:00
|
|
|
{
|
2018-09-22 10:23:23 +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
|
|
|
}
|
2018-06-23 14:56:59 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
void
|
|
|
|
llarp_main_queryDHT(struct check_online_request *request)
|
|
|
|
{
|
|
|
|
// llarp::Info("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);
|
|
|
|
}
|
2018-09-20 12:34:53 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
bool
|
|
|
|
main_router_mapAddress(struct llarp_main *ptr,
|
|
|
|
const llarp::service::Address &addr, uint32_t ip)
|
|
|
|
{
|
|
|
|
auto *endpoint = &ptr->ctx->router->hiddenServiceContext;
|
2018-10-10 15:14:45 +00:00
|
|
|
return endpoint->MapAddress(addr, llarp::huint32_t{ip});
|
2018-09-22 10:23:23 +00:00
|
|
|
}
|
2018-09-21 12:53:20 +00:00
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
bool
|
|
|
|
main_router_prefetch(struct llarp_main *ptr,
|
|
|
|
const llarp::service::Address &addr)
|
|
|
|
{
|
|
|
|
auto *endpoint = &ptr->ctx->router->hiddenServiceContext;
|
|
|
|
return endpoint->Prefetch(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::handlers::TunEndpoint *
|
|
|
|
main_router_getFirstTunEndpoint(struct llarp_main *ptr)
|
|
|
|
{
|
2018-10-03 11:00:47 +00:00
|
|
|
auto *context = &ptr->ctx->router->hiddenServiceContext;
|
|
|
|
return context->getFirstTun();
|
|
|
|
}
|
|
|
|
|
|
|
|
//#include <llarp/service/context.hpp>
|
|
|
|
bool
|
|
|
|
main_router_endpoint_iterator(
|
|
|
|
struct llarp_main *ptr, struct llarp::service::Context::endpoint_iter &i)
|
|
|
|
{
|
|
|
|
auto *context = &ptr->ctx->router->hiddenServiceContext;
|
|
|
|
return context->iterate(i);
|
2018-09-22 10:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp_tun_io *
|
|
|
|
main_router_getRange(struct llarp_main *ptr)
|
|
|
|
{
|
2018-10-03 11:00:47 +00:00
|
|
|
auto *context = &ptr->ctx->router->hiddenServiceContext;
|
|
|
|
return context->getRange();
|
2018-09-22 10:23:23 +00:00
|
|
|
}
|
2018-09-20 12:34:53 +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
|
|
|
{
|
2018-08-09 11:22:57 +00:00
|
|
|
const char *conffname = "daemon.ini";
|
|
|
|
int c;
|
|
|
|
while(1)
|
2018-07-26 10:52:23 +00:00
|
|
|
{
|
2018-08-09 11:22:57 +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)
|
2018-07-30 04:38:14 +00:00
|
|
|
break;
|
2018-08-09 11:22:57 +00:00
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
conffname = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
2018-11-06 14:06:09 +00:00
|
|
|
if(strncmp(optarg, "debug", std::min(strlen(optarg), size_t(5))) == 0)
|
2018-08-09 11:22:57 +00:00
|
|
|
{
|
|
|
|
cSetLogLevel(eLogDebug);
|
|
|
|
}
|
2018-11-06 14:06:09 +00:00
|
|
|
else if(strncmp(optarg, "info", std::min(strlen(optarg), size_t(4)))
|
2018-08-09 11:22:57 +00:00
|
|
|
== 0)
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogInfo);
|
|
|
|
}
|
2018-11-06 14:06:09 +00:00
|
|
|
else if(strncmp(optarg, "warn", std::min(strlen(optarg), size_t(4)))
|
2018-08-09 11:22:57 +00:00
|
|
|
== 0)
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogWarn);
|
|
|
|
}
|
2018-11-06 14:06:09 +00:00
|
|
|
else if(strncmp(optarg, "error", std::min(strlen(optarg), size_t(5)))
|
2018-08-09 11:22:57 +00:00
|
|
|
== 0)
|
|
|
|
{
|
|
|
|
cSetLogLevel(eLogError);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-07-26 10:52:23 +00:00
|
|
|
}
|
2018-08-09 11:22:57 +00:00
|
|
|
return conffname;
|
2018-07-26 10:52:23 +00:00
|
|
|
}
|
|
|
|
}
|