diff --git a/llarp/dht/tx.hpp b/llarp/dht/tx.hpp index 542707abd..4cef92464 100644 --- a/llarp/dht/tx.hpp +++ b/llarp/dht/tx.hpp @@ -98,25 +98,23 @@ namespace llarp // explicit next peer provided peer = *next; } - else if(!GetNextPeer(peer, peersAsked)) + else if(GetNextPeer(peer, peersAsked)) { - // no more peers - llarp::LogInfo("no more peers for request asking for ", target); - return false; - } - - const Key_t targetKey{target}; - if((prevPeer ^ targetKey) < (peer ^ targetKey)) - { - // next peer is not closer - llarp::LogInfo("next peer ", peer, " is not closer to ", target, - " than ", prevPeer); - return false; + const Key_t targetKey{target}; + if((prevPeer ^ targetKey) < (peer ^ targetKey)) + { + // next peer is not closer + llarp::LogInfo("next peer ", peer, " is not closer to ", target, + " than ", prevPeer); + return false; + } } else { - peersAsked.insert(peer); + llarp::LogInfo("no more peers for request asking for ", target); + return false; } + peersAsked.insert(peer); DoNextRequest(peer); return true; } diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index 6c9809730..6b6d3b230 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -153,7 +153,7 @@ namespace llarp } void - Message::AddINReply(llarp::huint32_t ip, RR_TTL_t ttl) + Message::AddINReply(llarp::huint32_t ip, bool isV6, RR_TTL_t ttl) { if(questions.size()) { @@ -161,11 +161,19 @@ namespace llarp const auto& question = questions[0]; ResourceRecord rec; rec.rr_name = question.qname; - rec.rr_type = qTypeA; rec.rr_class = qClassIN; rec.ttl = ttl; - rec.rData.resize(4); - htobe32buf(rec.rData.data(), ip.h); + if(isV6) + { + rec.rr_type = qTypeAAAA; + ip.SIIT(rec.rData); + } + else + { + rec.rr_type = qTypeA; + rec.rData.resize(4); + htobe32buf(rec.rData.data(), ip.h); + } answers.emplace_back(std::move(rec)); } } diff --git a/llarp/dns/message.hpp b/llarp/dns/message.hpp index dadcdd402..5015f8598 100644 --- a/llarp/dns/message.hpp +++ b/llarp/dns/message.hpp @@ -61,7 +61,7 @@ namespace llarp AddCNAMEReply(std::string name, RR_TTL_t ttl = 1); void - AddINReply(llarp::huint32_t addr, RR_TTL_t ttl = 1); + AddINReply(llarp::huint32_t addr, bool isV6, RR_TTL_t ttl = 1); void AddAReply(std::string name, RR_TTL_t ttl = 1); diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 92f1ff686..4a069bb9b 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -70,7 +70,8 @@ namespace llarp return m_OurRange.Contains(ip); } else if(msg.questions[0].qtype == dns::qTypeA - || msg.questions[0].qtype == dns::qTypeCNAME) + || msg.questions[0].qtype == dns::qTypeCNAME + || msg.questions[0].qtype == dns::qTypeAAAA) { // hook for forward dns or cname when using snode tld if(msg.questions[0].qname.find(".snode.") @@ -130,8 +131,10 @@ namespace llarp else msg.AddNXReply(); } - else if(msg.questions[0].qtype == dns::qTypeA) + else if(msg.questions[0].qtype == dns::qTypeA + || msg.questions[0].qtype == dns::qTypeAAAA) { + const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA; if(msg.questions[0].qname == "random.snode" || msg.questions[0].qname == "random.snode.") { @@ -146,7 +149,7 @@ namespace llarp if(msg.questions[0].qname == "localhost.loki." || msg.questions[0].qname == "localhost.loki") { - msg.AddINReply(GetIfAddr()); + msg.AddINReply(GetIfAddr(), isV6); reply(msg); return true; } @@ -161,7 +164,7 @@ namespace llarp // we do not have it mapped // map it ip = ObtainServiceNodeIP(r); - msg.AddINReply(ip); + msg.AddINReply(ip, isV6); } else { @@ -170,7 +173,7 @@ namespace llarp if(itr != m_KeyToIP.end()) { ip = itr->second; - msg.AddINReply(ip); + msg.AddINReply(ip, isV6); } else // fallback case that should never happen (probably) msg.AddNXReply(); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 5612b0223..4ec394256 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -248,7 +248,8 @@ namespace llarp TunEndpoint::HandleHookedDNSMessage( dns::Message &&msg, std::function< void(dns::Message) > reply) { - //llarp::LogInfo("Tun.HandleHookedDNSMessage ", msg.questions[0].qname, " of type", msg.questions[0].qtype); + // llarp::LogInfo("Tun.HandleHookedDNSMessage ", msg.questions[0].qname, " + // of type", msg.questions[0].qtype); if(msg.questions.size() != 1) { llarp::LogWarn("bad number of dns questions: ", msg.questions.size()); @@ -294,8 +295,10 @@ namespace llarp msg.AddNXReply(); reply(msg); } - else if(msg.questions[0].qtype == dns::qTypeA) + else if(msg.questions[0].qtype == dns::qTypeA + || msg.questions[0].qtype == dns::qTypeAAAA) { + const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA; llarp::service::Address addr; // on MacOS this is a typeA query if(is_random_snode(msg)) @@ -315,7 +318,7 @@ namespace llarp huint32_t ip = service->GetIfAddr(); if(ip.h) { - msg.AddINReply(ip); + msg.AddINReply(ip, isV6); ++counter; } return true; @@ -328,7 +331,7 @@ namespace llarp if(HasAddress(addr)) { huint32_t ip = ObtainIPForAddr(addr, false); - msg.AddINReply(ip); + msg.AddINReply(ip, isV6); } else { @@ -336,7 +339,7 @@ namespace llarp return EnsurePathToService( addr, [=](const service::Address &remote, OutboundContext *ctx) { - SendDNSReply(remote, ctx, replyMsg, reply, false); + SendDNSReply(remote, ctx, replyMsg, reply, false, isV6); }, 2000); } @@ -346,7 +349,8 @@ namespace llarp dns::Message *replyMsg = new dns::Message(std::move(msg)); EnsurePathToSNode(addr.as_array(), [=](const RouterID &remote, exit::BaseSession *s) { - SendDNSReply(remote, s, replyMsg, reply, true); + SendDNSReply(remote, s, replyMsg, reply, true, + isV6); }); return true; } diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index b83d11d93..2e5ad0f3b 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -196,12 +196,13 @@ namespace llarp template < typename Addr_t, typename Endpoint_t > void SendDNSReply(Addr_t addr, Endpoint_t* ctx, dns::Message* query, - std::function< void(dns::Message) > reply, bool snode) + std::function< void(dns::Message) > reply, bool snode, + bool sendIPv6) { if(ctx) { huint32_t ip = ObtainIPForAddr(addr, snode); - query->AddINReply(ip); + query->AddINReply(ip, sendIPv6); } else query->AddNXReply(); diff --git a/llarp/net/net_int.hpp b/llarp/net/net_int.hpp index 342df07c8..c2696b232 100644 --- a/llarp/net/net_int.hpp +++ b/llarp/net/net_int.hpp @@ -53,6 +53,16 @@ namespace llarp return out; } + template + void SIIT(Container & c) + { + c.resize(16); + std::fill(c.begin(), c.end(), 0); + htobe32buf(c.data() + 12, h); + c[11] = 0xff; + c[10] = 0xff; + } + std::string ToString() const { uint32_t n = htonl(h); diff --git a/test/dht/test_llarp_dht_tx.cpp b/test/dht/test_llarp_dht_tx.cpp index d2c268ee1..5a99cc468 100644 --- a/test/dht/test_llarp_dht_tx.cpp +++ b/test/dht/test_llarp_dht_tx.cpp @@ -118,7 +118,6 @@ TEST_F(TestDhtTx, ask_next_peer) const auto key0 = makeBuf< dht::Key_t >(0x00); const auto key1 = makeBuf< dht::Key_t >(0x01); const auto key2 = makeBuf< dht::Key_t >(0x02); - { // GetNextPeer fails EXPECT_CALL(tx, GetNextPeer(_, _)).WillOnce(Return(false)); diff --git a/test/dns/test_llarp_dns_dns.cpp b/test/dns/test_llarp_dns_dns.cpp index e1034b841..5ee7eebc3 100644 --- a/test/dns/test_llarp_dns_dns.cpp +++ b/test/dns/test_llarp_dns_dns.cpp @@ -115,7 +115,7 @@ TEST_F(DNSLibTest, TestSerializeMessage) q.qname = "whatever.tld"; q.qclass = 1; q.qtype = 1; - m.AddINReply({1}); + m.AddINReply({1}, false); ASSERT_EQ(m.questions.size(), 1U); ASSERT_EQ(m.answers.size(), 1U); ASSERT_TRUE(m.Encode(&buf));