diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index 58088dbc20..c80b060536 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -81,6 +81,7 @@ namespace libuv m_Accept->close = &ExplicitCloseAccept; m_Conn.write = nullptr; m_Conn.closed = nullptr; + m_Conn.tick = nullptr; } conn_glue(conn_glue* parent) : m_TCP(nullptr), m_Accept(nullptr) @@ -304,9 +305,13 @@ namespace libuv Tick() { if(m_Accept && m_Accept->tick) + { m_Accept->tick(m_Accept); + } if(m_Conn.tick) + { m_Conn.tick(&m_Conn); + } } void @@ -346,6 +351,7 @@ namespace libuv bool Server() { + uv_check_start(&m_Ticker, &OnTick); m_Accept->close = &ExplicitCloseAccept; return uv_tcp_bind(&m_Handle, m_Addr, 0) == 0 && uv_listen(Stream(), 5, &OnAccept) == 0; diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 7858f7dc09..7221381f89 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -407,8 +407,26 @@ 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); + auto ReplyToSNodeDNSWhenReady = [self = this, reply = reply]( + RouterID snode, auto msg, + bool isV6) -> bool { + return self->EnsurePathToSNode( + snode, [=](const RouterID &, exit::BaseSession_ptr s) { + self->SendDNSReply(snode, s, msg, reply, true, isV6); + }); + }; + auto ReplyToLokiDNSWhenReady = [self = this, reply = reply]( + service::Address addr, auto msg, + bool isV6) -> bool { + using service::Address; + using service::OutboundContext; + return self->EnsurePathToService( + addr, + [=](const Address &, OutboundContext *ctx) { + self->SendDNSReply(addr, ctx, msg, reply, false, isV6); + }, + 2s); + }; std::string qname; if(msg.answers.size() > 0) { @@ -424,10 +442,7 @@ namespace llarp return false; auto replyMsg = std::make_shared< dns::Message >(clear_dns_message(msg)); - return EnsurePathToSNode( - addr.as_array(), [=](const RouterID &, exit::BaseSession_ptr s) { - SendDNSReply(addr, s, replyMsg, reply, true, false); - }); + return ReplyToSNodeDNSWhenReady(addr, std::move(replyMsg), false); } else if(answer.HasCNameForTLD(".loki")) { @@ -438,26 +453,9 @@ namespace llarp service::Address addr; if(not addr.FromString(qname)) return false; - clear_dns_message(msg); - if(HasAddress(addr)) - { - huint128_t ip = ObtainIPForAddr(addr, false); - msg.AddINReply(ip, false); - reply(msg); - return true; - } - else - { - auto replyMsg = std::make_shared< dns::Message >(std::move(msg)); - using service::Address; - using service::OutboundContext; - return EnsurePathToService( - addr, - [=](const Address &, OutboundContext *ctx) { - SendDNSReply(addr, ctx, replyMsg, reply, false, false); - }, - 2s); - } + auto replyMsg = + std::make_shared< dns::Message >(clear_dns_message(msg)); + return ReplyToLokiDNSWhenReady(addr, replyMsg, false); } } if(msg.questions.size() != 1) @@ -484,7 +482,9 @@ namespace llarp { RouterID random; if(Router()->GetRandomGoodRouter(random)) + { msg.AddCNAMEReply(random.ToString(), 1); + } else msg.AddNXReply(); } @@ -521,7 +521,11 @@ namespace llarp { RouterID random; if(Router()->GetRandomGoodRouter(random)) + { msg.AddCNAMEReply(random.ToString(), 1); + return ReplyToSNodeDNSWhenReady( + random, std::make_shared< dns::Message >(msg), isV6); + } else msg.AddNXReply(); } @@ -550,23 +554,10 @@ namespace llarp { msg.hdr_fields |= dns::flags_QR | dns::flags_AA | dns::flags_RA; } - else if(HasAddress(addr)) - { - huint128_t ip = ObtainIPForAddr(addr, false); - msg.AddINReply(ip, isV6); - } else { - auto replyMsg = std::make_shared< dns::Message >(msg); - using service::Address; - using service::OutboundContext; - return EnsurePathToService( - addr, - [=](const Address &, OutboundContext *ctx) { - SendDNSReply(addr, ctx, replyMsg, reply, false, - isV6 || !isV4); - }, - 2s); + return ReplyToLokiDNSWhenReady( + addr, std::make_shared< dns::Message >(msg), isV6); } } else if(addr.FromString(qname, ".snode")) @@ -577,12 +568,8 @@ namespace llarp } else { - auto replyMsg = std::make_shared< dns::Message >(std::move(msg)); - return EnsurePathToSNode( - addr.as_array(), - [=](const RouterID &, exit::BaseSession_ptr s) { - SendDNSReply(addr, s, replyMsg, reply, true, isV6); - }); + return ReplyToSNodeDNSWhenReady( + addr.as_array(), std::make_shared< dns::Message >(msg), isV6); } } else diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 64c774cfe6..fcce851600 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -186,7 +186,10 @@ namespace llarp if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf)) return false; - return read; + if(read) + return true; + + return bencode_discard(buf); } bool