From 19cfed329ea3c5052a415a6aca3fbcb6879a9bb3 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sun, 14 Feb 2021 16:57:41 +0300 Subject: LookupServer: Make lookup() return DNSAnswer's instead of strings This way, we propagate proper TTL. None of the callers currently care, though. --- .../Services/LookupServer/ClientConnection.cpp | 14 ++++--- Userland/Services/LookupServer/LookupServer.cpp | 45 ++++++++++++++-------- Userland/Services/LookupServer/LookupServer.h | 4 +- 3 files changed, 40 insertions(+), 23 deletions(-) (limited to 'Userland/Services') diff --git a/Userland/Services/LookupServer/ClientConnection.cpp b/Userland/Services/LookupServer/ClientConnection.cpp index 4c16802dc5..26a0716d95 100644 --- a/Userland/Services/LookupServer/ClientConnection.cpp +++ b/Userland/Services/LookupServer/ClientConnection.cpp @@ -50,9 +50,13 @@ void ClientConnection::die() OwnPtr ClientConnection::handle(const Messages::LookupServer::LookupName& message) { - auto addresses = LookupServer::the().lookup(message.name(), T_A); - if (addresses.is_empty()) + auto answers = LookupServer::the().lookup(message.name(), T_A); + if (answers.is_empty()) return make(1, Vector()); + Vector addresses; + for (auto& answer : answers) { + addresses.append(answer.record_data()); + } return make(0, move(addresses)); } @@ -66,9 +70,9 @@ OwnPtr ClientConnection::handle(c address[2], address[1], address[0]); - auto hosts = LookupServer::the().lookup(name, T_PTR); - if (hosts.is_empty()) + auto answers = LookupServer::the().lookup(name, T_PTR); + if (answers.is_empty()) return make(1, String()); - return make(0, hosts[0]); + return make(0, answers[0].record_data()); } } diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp index 3489bada08..a6367f0a4b 100644 --- a/Userland/Services/LookupServer/LookupServer.cpp +++ b/Userland/Services/LookupServer/LookupServer.cpp @@ -126,22 +126,32 @@ void LookupServer::load_etc_hosts() } } -Vector LookupServer::lookup(const DNSName& name, unsigned short record_type) +Vector LookupServer::lookup(const DNSName& name, unsigned short record_type) { #if LOOKUPSERVER_DEBUG dbgln("Got request for '{}'", name.as_string()); #endif - Vector responses; + Vector answers; + auto add_answer = [&](const DNSAnswer& answer) { + DNSAnswer answer_with_original_case { + name, + answer.type(), + answer.class_code(), + answer.ttl(), + answer.record_data() + }; + answers.append(answer_with_original_case); + }; // First, try local data. if (auto local_answers = m_etc_hosts.get(name); local_answers.has_value()) { for (auto& answer : local_answers.value()) { if (answer.type() == record_type) - responses.append(answer.name().as_string()); + add_answer(answer); } - if (!responses.is_empty()) - return responses; + if (!answers.is_empty()) + return answers; } // Second, try our cache. @@ -152,11 +162,11 @@ Vector LookupServer::lookup(const DNSName& name, unsigned short record_t #if LOOKUPSERVER_DEBUG dbgln("Cache hit: {} -> {}", name.as_string(), answer.record_data()); #endif - responses.append(answer.record_data()); + add_answer(answer); } } - if (!responses.is_empty()) - return responses; + if (!answers.is_empty()) + return answers; } // Third, ask the upstream nameservers. @@ -166,12 +176,15 @@ Vector LookupServer::lookup(const DNSName& name, unsigned short record_t #endif bool did_get_response = false; int retries = 3; + Vector upstream_answers; do { - responses = lookup(name, nameserver, did_get_response, record_type); + upstream_answers = lookup(name, nameserver, did_get_response, record_type); if (did_get_response) break; } while (--retries); - if (!responses.is_empty()) { + if (!upstream_answers.is_empty()) { + for (auto& answer : upstream_answers) + add_answer(answer); break; } else { if (!did_get_response) @@ -180,15 +193,15 @@ Vector LookupServer::lookup(const DNSName& name, unsigned short record_t dbgln("Received response from '{}' but no result(s), trying next nameserver", nameserver); } } - if (responses.is_empty()) { + if (answers.is_empty()) { fprintf(stderr, "LookupServer: Tried all nameservers but never got a response :(\n"); return {}; } - return move(responses); + return move(answers); } -Vector LookupServer::lookup(const DNSName& name, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase should_randomize_case) +Vector LookupServer::lookup(const DNSName& name, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase should_randomize_case) { DNSPacket request; request.set_is_query(); @@ -270,15 +283,15 @@ Vector LookupServer::lookup(const DNSName& name, const String& nameserve return {}; } - Vector responses; + Vector answers; for (auto& answer : response.answers()) { put_in_cache(answer); if (answer.type() != record_type) continue; - responses.append(answer.record_data()); + answers.append(answer); } - return responses; + return move(answers); } void LookupServer::put_in_cache(const DNSAnswer& answer) diff --git a/Userland/Services/LookupServer/LookupServer.h b/Userland/Services/LookupServer/LookupServer.h index 8cf9b7ea38..7c01a2a432 100644 --- a/Userland/Services/LookupServer/LookupServer.h +++ b/Userland/Services/LookupServer/LookupServer.h @@ -39,7 +39,7 @@ class LookupServer final : public Core::Object { public: static LookupServer& the(); - Vector lookup(const DNSName& name, unsigned short record_type); + Vector lookup(const DNSName& name, unsigned short record_type); private: LookupServer(); @@ -47,7 +47,7 @@ private: void load_etc_hosts(); void put_in_cache(const DNSAnswer&); - Vector lookup(const DNSName& hostname, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes); + Vector lookup(const DNSName& hostname, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes); RefPtr m_local_server; -- cgit v1.2.3