summaryrefslogtreecommitdiff
path: root/Userland/Services/LookupServer
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2021-02-14 16:33:16 +0300
committerAndreas Kling <kling@serenityos.org>2021-02-15 09:14:42 +0100
commite9387e43db4f729dc52c48c548b4e1db3da6dd66 (patch)
treeac886f2c762bea8345ac5bc2d59d7f9c65415875 /Userland/Services/LookupServer
parentbacbde31f3e4e2de48bce6848f6d7c610fe6a7be (diff)
downloadserenity-e9387e43db4f729dc52c48c548b4e1db3da6dd66.zip
LookupServer: Store DNSName's in HashMap's directly
DNSName can now take care of case conversion when comparing using traits. It still intentionally doesn't implement operator ==; you have to explicitly decide whether you want case-sensitive or case-insensitive comparison. This change makes caches (and /etc/hosts) case-transparent: we will now match domains if they're the same except for the case.
Diffstat (limited to 'Userland/Services/LookupServer')
-rw-r--r--Userland/Services/LookupServer/DNSName.cpp10
-rw-r--r--Userland/Services/LookupServer/DNSName.h6
-rw-r--r--Userland/Services/LookupServer/LookupServer.cpp10
-rw-r--r--Userland/Services/LookupServer/LookupServer.h4
4 files changed, 23 insertions, 7 deletions
diff --git a/Userland/Services/LookupServer/DNSName.cpp b/Userland/Services/LookupServer/DNSName.cpp
index 0bbafd73ad..19ddc785f4 100644
--- a/Userland/Services/LookupServer/DNSName.cpp
+++ b/Userland/Services/LookupServer/DNSName.cpp
@@ -105,4 +105,14 @@ OutputStream& operator<<(OutputStream& stream, const DNSName& name)
return stream;
}
+unsigned DNSName::Traits::hash(const DNSName& name)
+{
+ return CaseInsensitiveStringTraits::hash(name.as_string());
+}
+
+bool DNSName::Traits::equals(const DNSName& a, const DNSName& b)
+{
+ return CaseInsensitiveStringTraits::equals(a.as_string(), b.as_string());
+}
+
}
diff --git a/Userland/Services/LookupServer/DNSName.h b/Userland/Services/LookupServer/DNSName.h
index f3afa9a8a4..585af15a69 100644
--- a/Userland/Services/LookupServer/DNSName.h
+++ b/Userland/Services/LookupServer/DNSName.h
@@ -43,6 +43,12 @@ public:
void randomize_case();
+ class Traits : public AK::Traits<DNSName> {
+ public:
+ static unsigned hash(const DNSName& name);
+ static bool equals(const DNSName&, const DNSName&);
+ };
+
private:
String m_name;
};
diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp
index 826094a0eb..b4226d09a8 100644
--- a/Userland/Services/LookupServer/LookupServer.cpp
+++ b/Userland/Services/LookupServer/LookupServer.cpp
@@ -97,7 +97,7 @@ void LookupServer::load_etc_hosts()
};
auto raw_addr = addr.to_in_addr_t();
- auto name = fields[1];
+ DNSName name = fields[1];
m_etc_hosts.set(name, String { (const char*)&raw_addr, sizeof(raw_addr) });
IPv4Address reverse_addr {
@@ -121,7 +121,7 @@ Vector<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
Vector<String> responses;
- if (auto known_host = m_etc_hosts.get(name.as_string()); known_host.has_value()) {
+ if (auto known_host = m_etc_hosts.get(name); known_host.has_value()) {
responses.append(known_host.value());
} else if (!name.as_string().is_empty()) {
for (auto& nameserver : m_nameservers) {
@@ -155,7 +155,7 @@ Vector<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
Vector<String> LookupServer::lookup(const DNSName& name, const String& nameserver, bool& did_get_response, unsigned short record_type, ShouldRandomizeCase should_randomize_case)
{
- if (auto cached_answers = m_lookup_cache.get(name.as_string()); cached_answers.has_value()) {
+ if (auto cached_answers = m_lookup_cache.get(name); cached_answers.has_value()) {
Vector<String> responses;
for (auto& answer : cached_answers.value()) {
if (answer.type() == record_type && !answer.has_expired()) {
@@ -270,9 +270,9 @@ void LookupServer::put_in_cache(const DNSAnswer& answer)
if (m_lookup_cache.size() >= 256)
m_lookup_cache.remove(m_lookup_cache.begin());
- auto it = m_lookup_cache.find(answer.name().as_string());
+ auto it = m_lookup_cache.find(answer.name());
if (it == m_lookup_cache.end())
- m_lookup_cache.set(answer.name().as_string(), { answer });
+ m_lookup_cache.set(answer.name(), { answer });
else
it->value.append(answer);
}
diff --git a/Userland/Services/LookupServer/LookupServer.h b/Userland/Services/LookupServer/LookupServer.h
index fb46add0c9..b126caffb4 100644
--- a/Userland/Services/LookupServer/LookupServer.h
+++ b/Userland/Services/LookupServer/LookupServer.h
@@ -52,8 +52,8 @@ private:
RefPtr<Core::LocalServer> m_local_server;
Vector<String> m_nameservers;
- HashMap<String, String> m_etc_hosts;
- HashMap<String, Vector<DNSAnswer>> m_lookup_cache;
+ HashMap<DNSName, String, DNSName::Traits> m_etc_hosts;
+ HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_lookup_cache;
};
}