SRV records fixes (#1332)

* fix a log print log level

* correctly match SRV record service and protocol...

* tests for new dns question functions
pull/1330/head
Thomas Winget 4 years ago committed by GitHub
parent b1c14af938
commit 2c6e7b86c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@
#include <util/logging/logger.hpp>
#include <util/printer.hpp>
#include <util/str.hpp>
namespace llarp
{
@ -59,6 +60,31 @@ namespace llarp
return IsName(other + ".");
}
bool
Question::IsLocalhost() const
{
return (qname == "localhost.loki." or llarp::ends_with(qname, ".localhost.loki."));
}
std::string
Question::Subdomains() const
{
if (qname.size() < 2)
return "";
size_t pos;
pos = qname.rfind('.', qname.size() - 2);
if (pos == std::string::npos or pos == 0)
return "";
pos = qname.rfind('.', pos - 1);
if (pos == std::string::npos or pos == 0)
return "";
return qname.substr(0, pos);
}
std::string
Question::Name() const
{

@ -40,6 +40,14 @@ namespace llarp
bool
IsName(const std::string& other) const;
/// is the name [something.]localhost.loki. ?
bool
IsLocalhost() const;
/// get subdomain(s), if any, from qname
std::string
Subdomains() const;
/// return qname with no trailing .
std::string
Name() const;

@ -245,7 +245,7 @@ namespace llarp
static bool
is_localhost_loki(const dns::Message& msg)
{
return msg.questions[0].IsName("localhost.loki");
return msg.questions[0].IsLocalhost();
}
template <>
@ -324,17 +324,7 @@ namespace llarp
return;
const auto& introset = ctx->GetCurrentIntroSet();
std::vector<llarp::dns::SRVData> records;
size_t numRecords = introset.SRVs.size();
if (numRecords > 0)
{
records.reserve(numRecords);
for (const auto& record : introset.SRVs)
{
records.push_back(std::move(llarp::dns::SRVData::fromTuple(record)));
}
}
msg->AddSRVReply(records);
msg->AddSRVReply(introset.GetMatchingSRVRecords(addr.subdomain));
reply(*msg);
},
2s);
@ -511,20 +501,20 @@ namespace llarp
reply(msg);
return true;
}
// TODO: SRV Record
else if (msg.questions[0].qtype == dns::qTypeSRV)
{
llarp::service::Address addr;
if (is_localhost_loki(msg))
{
msg.AddNXReply();
msg.AddSRVReply(introSet().GetMatchingSRVRecords(msg.questions[0].Subdomains()));
reply(msg);
return true;
}
else if (addr.FromString(qname, ".loki"))
{
llarp::LogWarn("SRV request for: ", qname);
llarp::LogDebug("SRV request for: ", qname);
return ReplyToLokiSRVWhenReady(addr, std::make_shared<dns::Message>(msg));
}
}

@ -459,7 +459,6 @@ namespace llarp
std::shared_ptr<IAuthPolicy> m_AuthPolicy;
std::unordered_map<Address, AuthInfo, Address::Hash> m_RemoteAuthInfos;
private:
void
FlushRecvData();

@ -282,6 +282,22 @@ namespace llarp
return GetNewestIntroExpiration() < now;
}
std::vector<llarp::dns::SRVData>
IntroSet::GetMatchingSRVRecords(std::string_view service_proto) const
{
std::vector<llarp::dns::SRVData> records;
for (const auto& tuple : SRVs)
{
if (std::get<0>(tuple) == service_proto)
{
records.push_back(llarp::dns::SRVData::fromTuple(tuple));
}
}
return records;
}
bool
IntroSet::Verify(llarp_time_t now) const
{

@ -58,6 +58,9 @@ namespace llarp
bool
IsExpired(llarp_time_t now) const;
std::vector<llarp::dns::SRVData>
GetMatchingSRVRecords(std::string_view service_proto) const;
bool
BEncode(llarp_buffer_t* buf) const;

@ -48,6 +48,64 @@ TEST_F(DNSLibTest, TestHasTLD)
ASSERT_FALSE(question.HasTLD(tld));
};
TEST_F(DNSLibTest, TestIsLocalhost)
{
llarp::dns::Question question;
question.qname = "localhost.loki.";
ASSERT_TRUE(question.IsLocalhost());
question.qname = "foo.localhost.loki.";
ASSERT_TRUE(question.IsLocalhost());
question.qname = "foo.bar.localhost.loki.";
ASSERT_TRUE(question.IsLocalhost());
question.qname = "something.loki.";
ASSERT_FALSE(question.IsLocalhost());
question.qname = "localhost.something.loki.";
ASSERT_FALSE(question.IsLocalhost());
question.qname = "notlocalhost.loki.";
ASSERT_FALSE(question.IsLocalhost());
};
TEST_F(DNSLibTest, TestGetSubdomains)
{
llarp::dns::Question question;
std::string expected;
question.qname = "localhost.loki.";
expected = "";
ASSERT_EQ(question.Subdomains(), expected);
question.qname = "foo.localhost.loki.";
expected = "foo";
ASSERT_EQ(question.Subdomains(), expected);
question.qname = "foo.bar.localhost.loki.";
expected = "foo.bar";
ASSERT_EQ(question.Subdomains(), expected);
// not legal, but test it anyway
question.qname = ".localhost.loki.";
expected = "";
ASSERT_EQ(question.Subdomains(), expected);
question.qname = ".loki.";
expected = "";
ASSERT_EQ(question.Subdomains(), expected);
question.qname = "loki.";
expected = "";
ASSERT_EQ(question.Subdomains(), expected);
question.qname = ".";
expected = "";
ASSERT_EQ(question.Subdomains(), expected);
question.qname = "";
expected = "";
ASSERT_EQ(question.Subdomains(), expected);
};
TEST_F(DNSLibTest, TestPTR)
{
llarp::huint128_t ip = {0};

Loading…
Cancel
Save