mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 23:38:12 +00:00
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.
This commit is contained in:
parent
bacbde31f3
commit
e9387e43db
4 changed files with 23 additions and 7 deletions
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue