diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index 0c9af092e..d6a0e9879 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -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) { diff --git a/llarp/dns/message.hpp b/llarp/dns/message.hpp index 894005caf..ccf70f3ee 100644 --- a/llarp/dns/message.hpp +++ b/llarp/dns/message.hpp @@ -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); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 859b3b337..4c48fa7ef 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -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; diff --git a/llarp/router.cpp b/llarp/router.cpp index f7d27c847..8f2c36f66 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -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 diff --git a/llarp/router.hpp b/llarp/router.hpp index 699935414..b5ab4b0d2 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -199,6 +199,9 @@ namespace llarp bool InitOutboundLink(); + bool + GetRandomGoodRouter(RouterID &r); + /// initialize us as a service node /// return true on success bool diff --git a/llarp/rpc.cpp b/llarp/rpc.cpp index 64cea102d..3572c3b51 100644 --- a/llarp/rpc.cpp +++ b/llarp/rpc.cpp @@ -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)