diff --git a/llarp/dns/name.cpp b/llarp/dns/name.cpp index e9726139c..bee64f284 100644 --- a/llarp/dns/name.cpp +++ b/llarp/dns/name.cpp @@ -73,22 +73,22 @@ namespace llarp bool DecodePTR(const Name_t& name, huint32_t& ip) { - auto pos = name.find(".in-addr.arpa."); - if(pos == Name_t::npos) + auto pos = name.find(".in-addr.arpa"); + if(pos == std::string::npos) return false; - std::string sub = name.substr(0, pos - 1); + std::string sub = name.substr(0, pos + 1); if(std::count(sub.begin(), sub.end(), '.') == 4) { uint8_t a, b, c, d; pos = sub.find('.'); d = atoi(sub.substr(0, pos).c_str()); - sub = sub.substr(pos); + sub = sub.substr(pos + 1); pos = sub.find('.'); c = atoi(sub.substr(0, pos).c_str()); - sub = sub.substr(pos); + sub = sub.substr(pos + 1); pos = sub.find('.'); b = atoi(sub.substr(0, pos).c_str()); - sub = sub.substr(pos); + sub = sub.substr(pos + 1); pos = sub.find('.'); a = atoi(sub.substr(0, pos).c_str()); ip = llarp::ipaddr_ipv4_bits(a, b, c, d); diff --git a/test/test_dnslib.cpp b/test/test_dnslib.cpp index 2a760d5e7..ae7c9bc1b 100644 --- a/test/test_dnslib.cpp +++ b/test/test_dnslib.cpp @@ -1,6 +1,7 @@ #include #include #include +#include struct DNSLibTest : public ::testing::Test { @@ -22,6 +23,14 @@ struct DNSLibTest : public ::testing::Test } }; +TEST_F(DNSLibTest, TestPTR) +{ + llarp::huint32_t ip = {0}; + llarp::huint32_t expected = llarp::ipaddr_ipv4_bits(10, 10, 10, 1); + ASSERT_TRUE(llarp::dns::DecodePTR("1.10.10.10.in-addr.arpa.", ip)); + ASSERT_EQ(ip, expected); +}; + TEST_F(DNSLibTest, TestSerializeHeader) { llarp::dns::MessageHeader hdr, other;