llarp_main_queryDHT_RC(), llarp_main_init_dnsd(), actually query DHT on dns lookup

This commit is contained in:
Ryan Tharp 2018-08-09 04:22:57 -07:00
parent c0ee5ab590
commit 3ada1e15be
3 changed files with 209 additions and 116 deletions

View File

@ -13,6 +13,8 @@
#include "llarp/net.hpp"
#include "logger.hpp"
#include "crypto.hpp" // for llarp::pubkey
#include <algorithm> // for std::generate_n
#include <thread> // for multithreaded version
#include <vector>
@ -31,6 +33,11 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName);
#include <pthread_np.h>
#endif
// CHECK: is multiprocess still a thing?
#ifndef TESTNET
#define TESTNET 0
#endif
struct llarp_main *ctx = 0;
bool done = false;
@ -123,6 +130,20 @@ llarp_dnsd_checkQuery(void *u, uint64_t orig, uint64_t left)
delete qr;
}
void
HandleDHTLocate(llarp_router_lookup_job *job)
{
llarp::LogInfo("DHT result: ", job->found ? "found" : "not found");
if(job->found)
{
// save to nodedb?
char ftmp[68] = {0};
const char *hexPubSigKey =
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(job->target, ftmp);
llarp::LogInfo("WE FOUND DHT LOOKUP FOR ", hexPubSigKey);
}
}
dnsd_query_hook_response *
hookChecker(std::string name, const struct sockaddr *from,
struct dnsd_question_request *request)
@ -148,6 +169,25 @@ hookChecker(std::string name, const struct sockaddr *from,
delete response;
return cache_check->second;
}
std::string b32addr = lName.substr(0, lName.size() - 5);
b32addr.erase(32, 1);
llarp::LogInfo("Hex address: ", b32addr);
llarp::PubKey binaryPK;
llarp::HexDecode(b32addr.c_str(), binaryPK.data());
llarp::LogInfo("Queueing job");
llarp_router_lookup_job *job = new llarp_router_lookup_job;
job->iterative = true;
job->found = false;
job->hook = &HandleDHTLocate;
llarp_rc_new(&job->result);
memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target
// llarp_dht_lookup_router(ctx->router->dht, job);
llarp_main_queryDHT_RC(ctx, job);
// check_query_request *query_request = new check_query_request;
// query_request->hook = &llarp_dnsd_checkQuery_resolved;
check_query_simple_request *qr = new check_query_simple_request;
@ -202,7 +242,6 @@ main(int argc, char *argv[])
dnsr_config.upstream_port = 53;
llarp_config *config_reader;
llarp_new_config(&config_reader);
// ctx = llarp_main_init(conffname, multiThreaded);
if(llarp_load_config(config_reader, conffname))
{
@ -223,6 +262,32 @@ main(int argc, char *argv[])
// llarp::SetLogLevel(llarp::eLogDebug);
if(1)
{
// libev version w/router context
ctx = llarp_main_init(conffname, !TESTNET);
if(!ctx)
{
llarp::LogError("Cant set up context");
return 0;
}
llarp_main_setup(ctx);
signal(SIGINT, handle_signal);
struct dnsd_context dnsd;
if(!llarp_main_init_dnsd(ctx, &dnsd, server_port,
(const char *)dnsr_config.upstream_host.c_str(),
dnsr_config.upstream_port))
{
llarp::LogError("Couldnt init dns daemon");
}
// Configure intercept
dnsd.intercept = &hookChecker;
// run system and wait
llarp_main_run(ctx);
llarp_main_free(ctx);
}
else if(0)
{
// libev version
llarp_ev_loop *netloop = nullptr;

View File

@ -68,10 +68,21 @@ extern "C"
check_online_request_hook_func hook;
};
/// get RC from DHT
/// get RC from DHT but wait until online
void
llarp_main_queryDHT(struct check_online_request *request);
/// get RC from DHT
void
llarp_main_queryDHT_RC(struct llarp_main *ptr,
struct llarp_router_lookup_job *job);
/// set up DNS libs with a context
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);
struct llarp_rc *
llarp_main_getLocalRC(struct llarp_main *ptr);

View File

@ -10,6 +10,8 @@
#include "math.h"
#include "router.hpp"
#include "dnsd.hpp"
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
#include <pthread_np.h>
#endif
@ -200,7 +202,6 @@ namespace llarp
}
else
{
llarp::LogInfo("running mainloop");
return llarp_ev_loop_run(mainloop, logic);
}
@ -215,7 +216,7 @@ namespace llarp
llarp::LogInfo("SIGINT");
SigINT();
}
// TODO(despair): implement hot-reloading config on NT
// TODO(despair): implement hot-reloading config on NT
#ifndef _WIN32
if(sig == SIGHUP)
{
@ -305,194 +306,210 @@ extern "C"
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;
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;
}
void
llarp_main_signal(struct llarp_main *ptr, int sig)
{
ptr->ctx->HandleSignal(sig);
ptr->ctx->HandleSignal(sig);
}
int
llarp_main_setup(struct llarp_main *ptr)
{
return ptr->ctx->Setup();
return ptr->ctx->Setup();
}
int
llarp_main_run(struct llarp_main *ptr)
{
return ptr->ctx->Run();
return ptr->ctx->Run();
}
void
llarp_main_abort(struct llarp_main *ptr)
{
llarp_logic_stop_timer(ptr->ctx->router->logic);
llarp_logic_stop_timer(ptr->ctx->router->logic);
}
int
llarp_main_loadDatabase(struct llarp_main *ptr)
{
return ptr->ctx->LoadDatabase();
return ptr->ctx->LoadDatabase();
}
int
llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i)
{
return ptr->ctx->IterateDatabase(i);
return ptr->ctx->IterateDatabase(i);
}
bool
llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc)
{
return ptr->ctx->PutDatabase(rc);
return ptr->ctx->PutDatabase(rc);
}
struct llarp_rc *
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk)
{
return ptr->ctx->GetDatabase(pk);
return ptr->ctx->GetDatabase(pk);
}
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;
//
/*
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;
}
void
llarp_main_checkOnline(void *u, uint64_t orig, uint64_t left)
{
// 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);
// 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);
}
void
llarp_main_queryDHT_online(struct check_online_request *request)
{
// 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);
}
// 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);
}
}
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);
// 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);
}
void
llarp_main_free(struct llarp_main *ptr)
{
delete ptr;
delete ptr;
}
const char *
handleBaseCmdLineArgs(int argc, char *argv[])
{
const char *conffname = "daemon.ini";
int c;
while(1)
{
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)
const char *conffname = "daemon.ini";
int c;
while(1)
{
case 0:
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;
case 'c':
conffname = optarg;
break;
case 'o':
switch(c)
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
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);
}
{
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);
}
else if(strncmp(optarg, "error",
MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
break;
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
break;
}
}
}
return conffname;
return conffname;
}
}