Merge pull request #143 from majestrate/dev

random.snode
This commit is contained in:
Jeff 2018-12-12 19:04:29 -05:00 committed by GitHub
commit 13c593d595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 4 deletions

View File

@ -189,6 +189,30 @@ namespace llarp
}
}
void
Message::AddCNAMEReply(std::string name, uint32_t ttl)
{
if(questions.size())
{
hdr_fields |= (1 << 15);
const auto& question = questions[0];
answers.emplace_back();
auto& rec = answers.back();
rec.rr_name = question.qname;
rec.rr_type = 5;
rec.rr_class = 1;
rec.ttl = ttl;
byte_t tmp[512] = {0};
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(EncodeName(&buf, name))
{
buf.sz = buf.cur - buf.base;
rec.rData.resize(buf.sz);
memcpy(rec.rData.data(), buf.base, buf.sz);
}
}
}
void
Message::AddMXReply(std::string name, uint16_t priority)
{

View File

@ -56,6 +56,9 @@ namespace llarp
void
AddMXReply(std::string name, uint16_t priority);
void
AddCNAMEReply(std::string name, uint32_t ttl);
void
AddINReply(llarp::huint32_t addr);

View File

@ -200,8 +200,15 @@ namespace llarp
{
// forward dns
llarp::service::Address addr;
if(addr.FromString(qname, ".loki"))
if(qname == "random.snode" || qname == "random.snode.")
{
RouterID random;
if(Router()->GetRandomGoodRouter(random))
msg.AddCNAMEReply(random.ToString(), 1);
else
msg.AddNXReply();
}
else if(addr.FromString(qname, ".loki"))
{
if(HasAddress(addr.data()))
{
@ -274,6 +281,9 @@ namespace llarp
// always hook mx records
if(msg.questions[0].qtype == 15)
return true;
if(msg.questions[0].qname == "random.snode"
|| msg.questions[0].qname == "random.snode.")
return true;
// always hook .loki
if(addr.FromString(msg.questions[0].qname, ".loki"))
return true;

View File

@ -10,6 +10,7 @@
#include <router.hpp>
#include <rpc.hpp>
#include <str.hpp>
#include <crypto.hpp>
#include <fstream>
#include <cstdlib>
@ -233,6 +234,19 @@ namespace llarp
std::max(until, m_PersistingSessions[remote]);
}
bool
Router::GetRandomGoodRouter(RouterID &router)
{
auto sz = nodedb->entries.size();
if(sz == 0)
return false;
auto itr = nodedb->entries.begin();
if(sz > 1)
std::advance(itr, randint() % sz);
router = itr->first;
return true;
}
constexpr size_t MaxPendingSendQueueSize = 8;
bool

View File

@ -199,6 +199,9 @@ namespace llarp
bool
InitOutboundLink();
bool
GetRandomGoodRouter(RouterID &r);
/// initialize us as a service node
/// return true on success
bool

View File

@ -179,7 +179,7 @@ namespace llarp
struct Handler : public ::abyss::httpd::IRPCHandler
{
llarp::Router* router;
llarp::Router* router;
Handler(::abyss::httpd::ConnImpl* conn, llarp::Router* r)
: ::abyss::httpd::IRPCHandler(conn), router(r)
{
@ -273,7 +273,7 @@ namespace llarp
struct ServerImpl
{
llarp::Router* router;
llarp::Router* router;
ReqHandlerImpl _handler;
ServerImpl(llarp::Router* r) : router(r), _handler(r, 2000)