summaryrefslogtreecommitdiff
path: root/Userland/Services
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2021-02-14 16:57:41 +0300
committerAndreas Kling <kling@serenityos.org>2021-02-15 09:14:42 +0100
commit19cfed329ea3c5052a415a6aca3fbcb6879a9bb3 (patch)
treeccc9bf47d22e65af77d79b70a396bab62b9907e9 /Userland/Services
parent3fba6bfb5eed31323bb112bbf4c24f2de7602bb1 (diff)
downloadserenity-19cfed329ea3c5052a415a6aca3fbcb6879a9bb3.zip
LookupServer: Make lookup() return DNSAnswer's instead of strings
This way, we propagate proper TTL. None of the callers currently care, though.
Diffstat (limited to 'Userland/Services')
-rw-r--r--Userland/Services/LookupServer/ClientConnection.cpp14
-rw-r--r--Userland/Services/LookupServer/LookupServer.cpp45
-rw-r--r--Userland/Services/LookupServer/LookupServer.h4
3 files changed, 40 insertions, 23 deletions
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<Messages::LookupServer::LookupNameResponse> 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<Messages::LookupServer::LookupNameResponse>(1, Vector<String>());
+ Vector<String> addresses;
+ for (auto& answer : answers) {
+ addresses.append(answer.record_data());
+ }
return make<Messages::LookupServer::LookupNameResponse>(0, move(addresses));
}
@@ -66,9 +70,9 @@ OwnPtr<Messages::LookupServer::LookupAddressResponse> 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<Messages::LookupServer::LookupAddressResponse>(1, String());
- return make<Messages::LookupServer::LookupAddressResponse>(0, hosts[0]);
+ return make<Messages::LookupServer::LookupAddressResponse>(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<String> LookupServer::lookup(const DNSName& name, unsigned short record_type)
+Vector<DNSAnswer> LookupServer::lookup(const DNSName& name, unsigned short record_type)
{
#if LOOKUPSERVER_DEBUG
dbgln("Got request for '{}'", name.as_string());
#endif
- Vector<String> responses;
+ Vector<DNSAnswer> 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<String> 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<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
#endif
bool did_get_response = false;
int retries = 3;
+ Vector<DNSAnswer> 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<String> 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<String> LookupServer::lookup(const DNSName& name, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase should_randomize_case)
+Vector<DNSAnswer> 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<String> LookupServer::lookup(const DNSName& name, const String& nameserve
return {};
}
- Vector<String, 8> responses;
+ Vector<DNSAnswer, 8> 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<String> lookup(const DNSName& name, unsigned short record_type);
+ Vector<DNSAnswer> 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<String> lookup(const DNSName& hostname, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes);
+ Vector<DNSAnswer> lookup(const DNSName& hostname, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase = ShouldRandomizeCase::Yes);
RefPtr<Core::LocalServer> m_local_server;