mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:27:45 +00:00
LookupServer: Move cache check into the outer lookup() method
Where it belongs, alongside the /etc/hosts check. The inner lookup() method is really about talking to a specific DNS server. Also, don't bail out on a empty name. An empty DNSName is actually '.' — a single dot — aka the DNS root.
This commit is contained in:
parent
af6aac8c55
commit
3fba6bfb5e
1 changed files with 34 additions and 33 deletions
|
@ -134,6 +134,7 @@ Vector<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
|
|||
|
||||
Vector<String> responses;
|
||||
|
||||
// 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)
|
||||
|
@ -143,7 +144,22 @@ Vector<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
|
|||
return responses;
|
||||
}
|
||||
|
||||
if (!name.as_string().is_empty()) {
|
||||
// Second, try our cache.
|
||||
if (auto cached_answers = m_lookup_cache.get(name); cached_answers.has_value()) {
|
||||
for (auto& answer : cached_answers.value()) {
|
||||
// TODO: Actually remove expired answers from the cache.
|
||||
if (answer.type() == record_type && !answer.has_expired()) {
|
||||
#if LOOKUPSERVER_DEBUG
|
||||
dbgln("Cache hit: {} -> {}", name.as_string(), answer.record_data());
|
||||
#endif
|
||||
responses.append(answer.record_data());
|
||||
}
|
||||
}
|
||||
if (!responses.is_empty())
|
||||
return responses;
|
||||
}
|
||||
|
||||
// Third, ask the upstream nameservers.
|
||||
for (auto& nameserver : m_nameservers) {
|
||||
#if LOOKUPSERVER_DEBUG
|
||||
dbgln("Doing lookup using nameserver '{}'", nameserver);
|
||||
|
@ -168,27 +184,12 @@ Vector<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
|
|||
fprintf(stderr, "LookupServer: Tried all nameservers but never got a response :(\n");
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
return move(responses);
|
||||
}
|
||||
|
||||
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); cached_answers.has_value()) {
|
||||
Vector<String> responses;
|
||||
for (auto& answer : cached_answers.value()) {
|
||||
if (answer.type() == record_type && !answer.has_expired()) {
|
||||
#if LOOKUPSERVER_DEBUG
|
||||
dbgln("Cache hit: {} -> {}", name.as_string(), answer.record_data());
|
||||
#endif
|
||||
responses.append(answer.record_data());
|
||||
}
|
||||
}
|
||||
if (!responses.is_empty())
|
||||
return responses;
|
||||
}
|
||||
|
||||
DNSPacket request;
|
||||
request.set_is_query();
|
||||
request.set_id(arc4random_uniform(UINT16_MAX));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue